Skip to content

Commit 54c07b7

Browse files
aykevldeadprogram
authored andcommitted
interp: move some often-repeated code into a function
1 parent 2fb866c commit 54c07b7

File tree

2 files changed

+20
-44
lines changed

2 files changed

+20
-44
lines changed

interp/interpreter.go

Lines changed: 4 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -346,16 +346,7 @@ func (r *runner) run(fn *function, params []value, parentMem *memoryView, indent
346346
dstObj.buffer = dstBuf
347347
mem.put(dst.index(), dstObj)
348348
}
349-
switch inst.llvmInst.Type().IntTypeWidth() {
350-
case 16:
351-
locals[inst.localIndex] = literalValue{uint16(n)}
352-
case 32:
353-
locals[inst.localIndex] = literalValue{uint32(n)}
354-
case 64:
355-
locals[inst.localIndex] = literalValue{uint64(n)}
356-
default:
357-
panic("unknown integer type width")
358-
}
349+
locals[inst.localIndex] = makeLiteralInt(n, inst.llvmInst.Type().IntTypeWidth())
359350
case strings.HasPrefix(callFn.name, "llvm.memcpy.p0") || strings.HasPrefix(callFn.name, "llvm.memmove.p0"):
360351
// Copy a block of memory from one pointer to another.
361352
dst, err := operands[1].asPointer(r)
@@ -647,16 +638,7 @@ func (r *runner) run(fn *function, params []value, parentMem *memoryView, indent
647638
}
648639
// GEP on fixed pointer value (for example, memory-mapped I/O).
649640
ptrValue := operands[0].Uint() + offset
650-
switch operands[0].len(r) {
651-
case 8:
652-
locals[inst.localIndex] = literalValue{uint64(ptrValue)}
653-
case 4:
654-
locals[inst.localIndex] = literalValue{uint32(ptrValue)}
655-
case 2:
656-
locals[inst.localIndex] = literalValue{uint16(ptrValue)}
657-
default:
658-
panic("pointer operand is not of a known pointer size")
659-
}
641+
locals[inst.localIndex] = makeLiteralInt(ptrValue, int(operands[0].len(r)*8))
660642
continue
661643
}
662644
ptr = ptr.addOffset(int64(offset))
@@ -810,18 +792,7 @@ func (r *runner) run(fn *function, params []value, parentMem *memoryView, indent
810792
default:
811793
panic("unreachable")
812794
}
813-
switch lhs.len(r) {
814-
case 8:
815-
locals[inst.localIndex] = literalValue{result}
816-
case 4:
817-
locals[inst.localIndex] = literalValue{uint32(result)}
818-
case 2:
819-
locals[inst.localIndex] = literalValue{uint16(result)}
820-
case 1:
821-
locals[inst.localIndex] = literalValue{uint8(result)}
822-
default:
823-
panic("unknown integer size")
824-
}
795+
locals[inst.localIndex] = makeLiteralInt(result, int(lhs.len(r)*8))
825796
if r.debug {
826797
fmt.Fprintln(os.Stderr, indent+instructionNameMap[inst.opcode]+":", lhs, rhs, "->", result)
827798
}
@@ -843,18 +814,7 @@ func (r *runner) run(fn *function, params []value, parentMem *memoryView, indent
843814
if r.debug {
844815
fmt.Fprintln(os.Stderr, indent+instructionNameMap[inst.opcode]+":", value, bitwidth)
845816
}
846-
switch bitwidth {
847-
case 64:
848-
locals[inst.localIndex] = literalValue{value}
849-
case 32:
850-
locals[inst.localIndex] = literalValue{uint32(value)}
851-
case 16:
852-
locals[inst.localIndex] = literalValue{uint16(value)}
853-
case 8:
854-
locals[inst.localIndex] = literalValue{uint8(value)}
855-
default:
856-
panic("unknown integer size in sext/zext/trunc")
857-
}
817+
locals[inst.localIndex] = makeLiteralInt(value, int(bitwidth))
858818
case llvm.SIToFP, llvm.UIToFP:
859819
var value float64
860820
switch inst.opcode {

interp/memory.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,22 @@ type literalValue struct {
373373
value interface{}
374374
}
375375

376+
// Make a literalValue given the number of bits.
377+
func makeLiteralInt(value uint64, bits int) literalValue {
378+
switch bits {
379+
case 64:
380+
return literalValue{value}
381+
case 32:
382+
return literalValue{uint32(value)}
383+
case 16:
384+
return literalValue{uint16(value)}
385+
case 8:
386+
return literalValue{uint8(value)}
387+
default:
388+
panic("unknown integer size")
389+
}
390+
}
391+
376392
func (v literalValue) len(r *runner) uint32 {
377393
switch v.value.(type) {
378394
case uint64:

0 commit comments

Comments
 (0)