| 1 | package ir |
| 2 | |
| 3 | import ( |
| 4 | "reflect" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/mmcloughlin/avo/operand" |
| 8 | "github.com/mmcloughlin/avo/reg" |
| 9 | ) |
| 10 | |
| 11 | func TestFunctionLabels(t *testing.T) { |
| 12 | f := NewFunction("labels") |
| 13 | f.AddInstruction(&Instruction{}) |
| 14 | f.AddLabel("a") |
| 15 | f.AddInstruction(&Instruction{}) |
| 16 | f.AddLabel("b") |
| 17 | f.AddInstruction(&Instruction{}) |
| 18 | f.AddLabel("c") |
| 19 | f.AddInstruction(&Instruction{}) |
| 20 | |
| 21 | expect := []Label{"a", "b", "c"} |
| 22 | if got := f.Labels(); !reflect.DeepEqual(expect, got) { |
| 23 | t.Fatalf("f.Labels() = %v; expect %v", got, expect) |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | func TestInputRegisters(t *testing.T) { |
| 28 | cases := []struct { |
| 29 | Name string |
| 30 | Inst *Instruction |
| 31 | Expect []reg.Register |
| 32 | }{ |
| 33 | { |
| 34 | Name: "reg", |
| 35 | Inst: &Instruction{ |
| 36 | Inputs: []operand.Op{ |
| 37 | reg.RAX, |
| 38 | reg.R13, |
| 39 | }, |
| 40 | Outputs: []operand.Op{ |
| 41 | reg.RBX, |
| 42 | }, |
| 43 | }, |
| 44 | Expect: []reg.Register{reg.RAX, reg.R13}, |
| 45 | }, |
| 46 | { |
| 47 | Name: "mem", |
| 48 | Inst: &Instruction{ |
| 49 | Inputs: []operand.Op{ |
| 50 | operand.Mem{ |
| 51 | Base: reg.RSI, |
| 52 | Index: reg.RDI, |
| 53 | }, |
| 54 | reg.R13, |
| 55 | }, |
| 56 | Outputs: []operand.Op{ |
| 57 | operand.Mem{ |
| 58 | Base: reg.R9, |
| 59 | Index: reg.R11, |
| 60 | }, |
| 61 | }, |
| 62 | }, |
| 63 | Expect: []reg.Register{ |
| 64 | reg.RSI, |
| 65 | reg.RDI, |
| 66 | reg.R13, |
| 67 | reg.R9, |
| 68 | reg.R11, |
| 69 | }, |
| 70 | }, |
| 71 | { |
| 72 | Name: "cancelling_inputs", |
| 73 | Inst: &Instruction{ |
| 74 | CancellingInputs: true, |
| 75 | Inputs: []operand.Op{ |
| 76 | reg.R13, |
| 77 | reg.R13, |
| 78 | }, |
| 79 | Outputs: []operand.Op{ |
| 80 | operand.Mem{ |
| 81 | Base: reg.R9, |
| 82 | Index: reg.R11, |
| 83 | }, |
| 84 | }, |
| 85 | }, |
| 86 | Expect: []reg.Register{reg.R9, reg.R11}, |
| 87 | }, |
| 88 | } |
| 89 | for _, c := range cases { |
| 90 | if got := c.Inst.InputRegisters(); !reflect.DeepEqual(got, c.Expect) { |
| 91 | t.Errorf("%s: got %v; expect %v", c.Inst.Opcode, got, c.Expect) |
| 92 | } |
| 93 | } |
| 94 | } |