diff --git a/src/codegen/Step.go b/src/codegen/Step.go index f48cc00..922aa98 100644 --- a/src/codegen/Step.go +++ b/src/codegen/Step.go @@ -19,6 +19,10 @@ type Step struct { // hint adds a register hint to the step. func (s *Step) hint(reg cpu.Register) { + if reg == -1 { + panic("invalid register hint") + } + if len(s.Hints) == 0 { s.Register = reg } diff --git a/src/core/removeDeadValue.go b/src/core/removeDeadValue.go index 1088aff..b7aeca8 100644 --- a/src/core/removeDeadValue.go +++ b/src/core/removeDeadValue.go @@ -36,6 +36,14 @@ func (f *Function) removeDeadValue(block *ssa.Block, i int, value ssa.Value, fol phi, isPhi = value.(*ssa.Phi) } + if deadPhi != nil { + for _, user := range value.Users() { + if user != deadPhi { + return nil + } + } + } + structure, isFieldOfStruct := f.valueToStruct[value] if isFieldOfStruct { @@ -56,22 +64,10 @@ func (f *Function) removeDeadValue(block *ssa.Block, i int, value ssa.Value, fol source := value.(errors.Source) - if source.Start() == 0 && source.End() == 0 { + if !f.Env.Build.LintDeadCode || source.Start() == 0 || source.End() == 0 { block.RemoveAt(i) return nil } - if deadPhi != nil { - for _, user := range value.Users() { - if user != deadPhi { - return nil - } - } - } - - if f.Env.Build.LintDeadCode { - return errors.New(&UnusedValue{Value: source.StringFrom(f.File.Bytes)}, f.File, token.NewSource(source.Start(), source.End())) - } - - return nil + return errors.New(&UnusedValue{Value: source.StringFrom(f.File.Bytes)}, f.File, token.NewSource(source.Start(), source.End())) } \ No newline at end of file diff --git a/tests/switch-dead-value.q b/tests/switch-dead-value.q new file mode 100644 index 0000000..01aaf31 --- /dev/null +++ b/tests/switch-dead-value.q @@ -0,0 +1,20 @@ +main() { + t0 := -50 + t1 := -8 + t2 := -50 + t3 := -29 + s := 0 + + loop i := 0..1 { + s = s + t0 + } + + bb := 0 + + switch { + t0 > 0 { bb = 10 } + _ { bb = -46 } + } + + assert (t1 << 0) - bb == 38 +} \ No newline at end of file diff --git a/tests/tests_test.go b/tests/tests_test.go index 5c39a26..1714954 100644 --- a/tests/tests_test.go +++ b/tests/tests_test.go @@ -50,6 +50,7 @@ var tests = []run{ {"jump-near", nil, "", "", 0}, {"switch", nil, "", "", 0}, {"switch-expression", nil, "", "", 0}, + {"switch-dead-value", nil, "", "", 0}, {"phi", nil, "", "", 0}, {"phi-simple", nil, "", "", 0}, {"phi-advanced", nil, "", "", 0},