| 1 | package pass |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | |
| 6 | "github.com/mmcloughlin/avo/ir" |
| 7 | "github.com/mmcloughlin/avo/operand" |
| 8 | ) |
| 9 | |
| 10 | // Verify pass validates an avo file. |
| 11 | var Verify = Concat( |
| 12 | InstructionPass(VerifyMemOperands), |
| 13 | ) |
| 14 | |
| 15 | // VerifyMemOperands checks the instruction's memory operands. |
| 16 | func VerifyMemOperands(i *ir.Instruction) error { |
| 17 | for _, op := range i.Operands { |
| 18 | m, ok := op.(operand.Mem) |
| 19 | if !ok { |
| 20 | continue |
| 21 | } |
| 22 | |
| 23 | if m.Base == nil { |
| 24 | return errors.New("bad memory operand: missing base register") |
| 25 | } |
| 26 | |
| 27 | if m.Index != nil && m.Scale == 0 { |
| 28 | return errors.New("bad memory operand: index register with scale 0") |
| 29 | } |
| 30 | } |
| 31 | return nil |
| 32 | } |