|
16 | 16 | import com.plasstech.lang.c.codegen.Call; |
17 | 17 | import com.plasstech.lang.c.codegen.Cdq; |
18 | 18 | import com.plasstech.lang.c.codegen.Cmp; |
| 19 | +import com.plasstech.lang.c.codegen.Div; |
19 | 20 | import com.plasstech.lang.c.codegen.Idiv; |
20 | 21 | import com.plasstech.lang.c.codegen.Imm; |
21 | 22 | import com.plasstech.lang.c.codegen.Instruction; |
22 | 23 | import com.plasstech.lang.c.codegen.Jmp; |
23 | 24 | import com.plasstech.lang.c.codegen.JmpCC; |
24 | 25 | import com.plasstech.lang.c.codegen.Label; |
25 | 26 | import com.plasstech.lang.c.codegen.Mov; |
| 27 | +import com.plasstech.lang.c.codegen.MovZeroExtend; |
26 | 28 | import com.plasstech.lang.c.codegen.Movsx; |
27 | 29 | import com.plasstech.lang.c.codegen.Operand; |
28 | 30 | import com.plasstech.lang.c.codegen.Push; |
@@ -112,6 +114,17 @@ public List<Instruction> visit(Idiv n) { |
112 | 114 | return ImmutableList.of(n); |
113 | 115 | } |
114 | 116 |
|
| 117 | + @Override |
| 118 | + public List<Instruction> visit(Div n) { |
| 119 | + // Can't divide by a constant; use r10 as an intermediary. See page 290 |
| 120 | + if (n.operand() instanceof Imm) { |
| 121 | + return ImmutableList.of( |
| 122 | + new Mov(n.type(), n.operand(), R10), |
| 123 | + new Div(n.type(), R10)); |
| 124 | + } |
| 125 | + return ImmutableList.of(n); |
| 126 | + } |
| 127 | + |
115 | 128 | @Override |
116 | 129 | public List<Instruction> visit(AsmUnary n) { |
117 | 130 | return ImmutableList.of(n); |
@@ -232,4 +245,19 @@ public List<Instruction> visit(Movsx op) { |
232 | 245 | } |
233 | 246 | return ImmutableList.of(op); |
234 | 247 | } |
| 248 | + |
| 249 | + @Override |
| 250 | + public List<Instruction> visit(MovZeroExtend op) { |
| 251 | + // Page 290 |
| 252 | + if (op.dst().inMemory()) { |
| 253 | + // if dest is memory, rewrite to two: |
| 254 | + // 1. mov(longword, src, reg 11) |
| 255 | + // 2. mov(quadword, r11, dest) |
| 256 | + return ImmutableList.of( |
| 257 | + new Mov(AssemblyType.Longword, op.src(), R11), |
| 258 | + new Mov(AssemblyType.Quadword, R11, op.dst())); |
| 259 | + } |
| 260 | + // dest is a register: rewrite to mov longword src, dst |
| 261 | + return ImmutableList.of(new Mov(AssemblyType.Longword, op.src(), op.dst())); |
| 262 | + } |
235 | 263 | } |
0 commit comments