pass: VerifyMemOperands (#127)

Introduces a pass to verify memory operands. This is added to a new
Verify pass, that in future could do much more.

Updates #125
This commit is contained in:
Michael McLoughlin
2020-01-22 19:59:02 -08:00
committed by GitHub
parent ff7a160610
commit 126469f13d
3 changed files with 107 additions and 0 deletions

32
pass/verify.go Normal file
View File

@@ -0,0 +1,32 @@
package pass
import (
"errors"
"github.com/mmcloughlin/avo/ir"
"github.com/mmcloughlin/avo/operand"
)
// Verify pass validates an avo file.
var Verify = Concat(
InstructionPass(VerifyMemOperands),
)
// VerifyMemOperands checks the instruction's memory operands.
func VerifyMemOperands(i *ir.Instruction) error {
for _, op := range i.Operands {
m, ok := op.(operand.Mem)
if !ok {
continue
}
if m.Base == nil {
return errors.New("bad memory operand: missing base register")
}
if m.Index != nil && m.Scale == 0 {
return errors.New("bad memory operand: index register with scale 0")
}
}
return nil
}