}
// Clear everything but ID (which we reuse).
id := v.ID
+
+ // Zero argument values might be cached, so remove them there.
+ nArgs := opcodeTable[v.Op].argLen
+ if nArgs == 0 {
+ vv := f.constants[v.AuxInt]
+ for i, cv := range vv {
+ if v == cv {
+ vv[i] = vv[len(vv)-1]
+ f.constants[v.AuxInt] = vv[0 : len(vv)-1]
+ break
+ }
+ }
+ }
*v = Value{}
v.ID = id
v.argstorage[0] = f.freeValues
vv := f.constants[c]
for _, v := range vv {
if v.Op == op && v.Type.Equal(t) {
+ if setAux && v.AuxInt != c {
+ panic(fmt.Sprintf("cached const %s should have AuxInt of %d", v.LongString(), c))
+ }
return v
}
}
}
}
+// TestConstCache ensures that the cache will not return
+// reused free'd values with a non-matching AuxInt
+func TestConstCache(t *testing.T) {
+ f := Fun(testConfig(t), "entry",
+ Bloc("entry",
+ Valu("mem", OpInitMem, TypeMem, 0, nil),
+ Exit("mem")))
+ v1 := f.f.ConstBool(0, TypeBool, false)
+ v2 := f.f.ConstBool(0, TypeBool, true)
+ f.f.freeValue(v1)
+ f.f.freeValue(v2)
+ v3 := f.f.ConstBool(0, TypeBool, false)
+ v4 := f.f.ConstBool(0, TypeBool, true)
+ if v3.AuxInt != 0 {
+ t.Errorf("expected %s to have auxint of 0\n", v3.LongString())
+ }
+ if v4.AuxInt != 1 {
+ t.Errorf("expected %s to have auxint of 1\n", v4.LongString())
+ }
+
+}
+
// opcodeMap returns a map from opcode to the number of times that opcode
// appears in the function.
func opcodeMap(f *Func) map[Op]int {