| 1 | package pass |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/mmcloughlin/avo/ir" |
| 8 | "github.com/mmcloughlin/avo/operand" |
| 9 | "github.com/mmcloughlin/avo/reg" |
| 10 | ) |
| 11 | |
| 12 | func TestVerifyMemOperands(t *testing.T) { |
| 13 | i := &ir.Instruction{ |
| 14 | Operands: []operand.Op{ |
| 15 | reg.RAX, |
| 16 | operand.Mem{ |
| 17 | Base: reg.R10, |
| 18 | Disp: 42, |
| 19 | }, |
| 20 | }, |
| 21 | } |
| 22 | if err := VerifyMemOperands(i); err != nil { |
| 23 | t.Fatal(err) |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | func TestVerifyMemOperandsErrors(t *testing.T) { |
| 28 | cases := []struct { |
| 29 | Operands []operand.Op |
| 30 | ErrorSubstring string |
| 31 | }{ |
| 32 | { |
| 33 | Operands: []operand.Op{ |
| 34 | reg.RAX, |
| 35 | operand.Mem{ |
| 36 | Disp: 42, |
| 37 | }, |
| 38 | }, |
| 39 | ErrorSubstring: "missing base", |
| 40 | }, |
| 41 | { |
| 42 | Operands: []operand.Op{ |
| 43 | operand.Mem{ |
| 44 | Base: reg.EBX, |
| 45 | Index: reg.R9L, |
| 46 | }, |
| 47 | reg.ECX, |
| 48 | }, |
| 49 | ErrorSubstring: "index register with scale 0", |
| 50 | }, |
| 51 | } |
| 52 | for _, c := range cases { |
| 53 | i := &ir.Instruction{Operands: c.Operands} |
| 54 | if err := VerifyMemOperands(i); err == nil || !strings.Contains(err.Error(), c.ErrorSubstring) { |
| 55 | t.Errorf("got error %v; expected error to contain %q", err, c.ErrorSubstring) |
| 56 | } |
| 57 | } |
| 58 | } |