Skip to content

Commit b7cdf8c

Browse files
aykevldeadprogram
authored andcommitted
interp: refactor to eliminate lots of code
This may cause a small performance penalty, but the code is easier to maange as a result.
1 parent cfc1a66 commit b7cdf8c

File tree

3 files changed

+89
-330
lines changed

3 files changed

+89
-330
lines changed

interp/frame.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,18 @@ func (fr *frame) evalBasicBlock(bb, incoming llvm.BasicBlock, indent string) (re
8484

8585
// Memory operators
8686
case !inst.IsAAllocaInst().IsNil():
87-
fr.locals[inst] = &AllocaValue{
88-
Underlying: getZeroValue(inst.Type().ElementType()),
89-
Dirty: false,
87+
allocType := inst.Type().ElementType()
88+
alloca := llvm.AddGlobal(fr.Mod, allocType, fr.pkgName+"$alloca")
89+
alloca.SetInitializer(getZeroValue(allocType))
90+
alloca.SetLinkage(llvm.InternalLinkage)
91+
fr.locals[inst] = &LocalValue{
92+
Underlying: alloca,
9093
Eval: fr.Eval,
9194
}
9295
case !inst.IsALoadInst().IsNil():
93-
operand := fr.getLocal(inst.Operand(0))
96+
operand := fr.getLocal(inst.Operand(0)).(*LocalValue)
9497
var value llvm.Value
95-
if !operand.IsConstant() || inst.IsVolatile() {
98+
if !operand.IsConstant() || inst.IsVolatile() || operand.Underlying.Opcode() == llvm.BitCast {
9699
value = fr.builder.CreateLoad(operand.Value(), inst.Name())
97100
} else {
98101
value = operand.Load()
@@ -173,11 +176,8 @@ func (fr *frame) evalBasicBlock(bb, incoming llvm.BasicBlock, indent string) (re
173176
continue // special case: bitcast of alloc
174177
}
175178
}
176-
value := fr.getLocal(operand)
177-
if bc, ok := value.(*PointerCastValue); ok {
178-
value = bc.Underlying // avoid double bitcasts
179-
}
180-
fr.locals[inst] = &PointerCastValue{Eval: fr.Eval, Underlying: value, CastType: inst.Type()}
179+
value := fr.getLocal(operand).(*LocalValue)
180+
fr.locals[inst] = &LocalValue{fr.Eval, fr.builder.CreateBitCast(value.Value(), inst.Type(), "")}
181181

182182
// Other operators
183183
case !inst.IsAICmpInst().IsNil():
@@ -222,7 +222,7 @@ func (fr *frame) evalBasicBlock(bb, incoming llvm.BasicBlock, indent string) (re
222222
alloc := llvm.AddGlobal(fr.Mod, allocType, fr.pkgName+"$alloc")
223223
alloc.SetInitializer(getZeroValue(allocType))
224224
alloc.SetLinkage(llvm.InternalLinkage)
225-
result := &GlobalValue{
225+
result := &LocalValue{
226226
Underlying: alloc,
227227
Eval: fr.Eval,
228228
}
@@ -246,15 +246,15 @@ func (fr *frame) evalBasicBlock(bb, incoming llvm.BasicBlock, indent string) (re
246246
m := fr.getLocal(inst.Operand(0)).(*MapValue)
247247
// "key" is a Go string value, which in the TinyGo calling convention is split up
248248
// into separate pointer and length parameters.
249-
keyBuf := fr.getLocal(inst.Operand(1))
250-
keyLen := fr.getLocal(inst.Operand(2))
251-
valPtr := fr.getLocal(inst.Operand(3))
249+
keyBuf := fr.getLocal(inst.Operand(1)).(*LocalValue)
250+
keyLen := fr.getLocal(inst.Operand(2)).(*LocalValue)
251+
valPtr := fr.getLocal(inst.Operand(3)).(*LocalValue)
252252
m.PutString(keyBuf, keyLen, valPtr)
253253
case callee.Name() == "runtime.hashmapBinarySet":
254254
// set a binary (int etc.) key in the map
255255
m := fr.getLocal(inst.Operand(0)).(*MapValue)
256-
keyBuf := fr.getLocal(inst.Operand(1))
257-
valPtr := fr.getLocal(inst.Operand(2))
256+
keyBuf := fr.getLocal(inst.Operand(1)).(*LocalValue)
257+
valPtr := fr.getLocal(inst.Operand(2)).(*LocalValue)
258258
m.PutBinary(keyBuf, valPtr)
259259
case callee.Name() == "runtime.stringConcat":
260260
// adding two strings together

interp/interp.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,7 @@ func (e *Eval) function(fn llvm.Value, params []Value, pkgName, indent string) (
124124
// getValue determines what kind of LLVM value it gets and returns the
125125
// appropriate Value type.
126126
func (e *Eval) getValue(v llvm.Value) Value {
127-
if !v.IsAGlobalVariable().IsNil() {
128-
return &GlobalValue{e, v}
129-
} else {
130-
return &LocalValue{e, v}
131-
}
127+
return &LocalValue{e, v}
132128
}
133129

134130
// markDirty marks the passed-in LLVM value dirty, recursively. For example,

0 commit comments

Comments
 (0)