codegen.rs 9.7 KB
Newer Older
1 2
use ast::ptr::P;
use ast::ir::*;
qinsoon's avatar
qinsoon committed
3
use runtime::ValueLocation;
4

qinsoon's avatar
qinsoon committed
5
use compiler::machine_code::MachineCode;
6

7 8 9
pub type Reg<'a> = &'a P<Value>;
pub type Mem<'a> = &'a P<Value>;

10
pub trait CodeGenerator {
qinsoon's avatar
qinsoon committed
11
    fn start_code(&mut self, func_name: MuName) -> ValueLocation;
12
    fn finish_code(&mut self, func_name: MuName) -> (Box<MachineCode + Sync + Send>, ValueLocation);
13 14 15 16

    // generate unnamed sequence of linear code (no branch)
    fn start_code_sequence(&mut self);
    fn finish_code_sequence(&mut self) -> Box<MachineCode + Sync + Send>;
17 18 19
    
    fn print_cur_code(&self);
    
qinsoon's avatar
qinsoon committed
20
    fn start_block(&mut self, block_name: MuName);
qinsoon's avatar
qinsoon committed
21
    fn start_exception_block(&mut self, block_name: MuName) -> ValueLocation;
qinsoon's avatar
qinsoon committed
22 23 24
    fn set_block_livein(&mut self, block_name: MuName, live_in: &Vec<P<Value>>);
    fn set_block_liveout(&mut self, block_name: MuName, live_out: &Vec<P<Value>>);
    fn end_block(&mut self, block_name: MuName);
qinsoon's avatar
qinsoon committed
25 26 27

    fn emit_frame_grow(&mut self);
    fn emit_frame_shrink(&mut self);
28
    
29
    fn emit_nop(&mut self, bytes: usize);
30 31

    // comparison
32
    fn emit_cmp_r64_r64  (&mut self, op1: Reg, op2: Reg);
33 34
    fn emit_cmp_imm32_r64(&mut self, op1: i32, op2: Reg);
    fn emit_cmp_mem64_r64(&mut self, op1: Mem, op2: Reg);
35

36
    fn emit_cmp_r32_r32  (&mut self, op1: Reg, op2: Reg);
37 38
    fn emit_cmp_imm32_r32(&mut self, op1: i32, op2: Reg);
    fn emit_cmp_mem32_r32(&mut self, op1: Mem, op2: Reg);
39

40
    fn emit_cmp_r16_r16  (&mut self, op1: Reg, op2: Reg);
41 42
    fn emit_cmp_imm16_r16(&mut self, op1: i16, op2: Reg);
    fn emit_cmp_mem16_r16(&mut self, op1: Mem, op2: Reg);
43

44 45 46
    fn emit_cmp_r8_r8  (&mut self, op1: Reg, op2: Reg);
    fn emit_cmp_imm8_r8(&mut self, op1: i8, op2: Reg);
    fn emit_cmp_mem8_r8(&mut self, op1: Mem, op2: Reg);
47 48

    // gpr move
49 50

    fn emit_mov_r64_imm64  (&mut self, dest: Reg, src: i64);
51
    
52 53 54 55 56 57 58 59 60 61 62
    fn emit_mov_r64_imm32  (&mut self, dest: Reg, src: i32);
    fn emit_mov_r64_mem64  (&mut self, dest: Reg, src: Mem); // load
    fn emit_mov_r64_r64    (&mut self, dest: Reg, src: Reg);
    fn emit_mov_mem64_r64  (&mut self, dest: Mem, src: Reg); // store
    fn emit_mov_mem64_imm32(&mut self, dest: Mem, src: i32);

    fn emit_mov_r32_imm32  (&mut self, dest: Reg, src: i32);
    fn emit_mov_r32_mem32  (&mut self, dest: Reg, src: Mem); // load
    fn emit_mov_r32_r32    (&mut self, dest: Reg, src: Reg);
    fn emit_mov_mem32_r32  (&mut self, dest: Mem, src: Reg); // store
    fn emit_mov_mem32_imm32(&mut self, dest: Mem, src: i32);
qinsoon's avatar
qinsoon committed
63
    
64 65 66 67 68 69 70 71 72 73 74
    fn emit_mov_r16_imm16  (&mut self, dest: Reg, src: i16);
    fn emit_mov_r16_mem16  (&mut self, dest: Reg, src: Mem); // load
    fn emit_mov_r16_r16    (&mut self, dest: Reg, src: Reg);
    fn emit_mov_mem16_r16  (&mut self, dest: Mem, src: Reg); // store
    fn emit_mov_mem16_imm16(&mut self, dest: Mem, src: i16);

    fn emit_mov_r8_imm8    (&mut self, dest: Reg, src: i8);
    fn emit_mov_r8_mem8    (&mut self, dest: Reg, src: Mem); // load
    fn emit_mov_r8_r8      (&mut self, dest: Reg, src: Mem);
    fn emit_mov_mem8_r8    (&mut self, dest: Mem, src: Reg); // store
    fn emit_mov_mem8_imm8  (&mut self, dest: Mem, src: i8);
75 76

    // lea
77
    fn emit_lea_r64(&mut self, dest: Reg, src: Mem);
78 79

    // and
80 81 82
    fn emit_and_r64_imm32(&mut self, dest: Reg, src: i32);
    fn emit_and_r64_r64  (&mut self, dest: Reg, src: Reg);
    fn emit_and_r64_mem64(&mut self, dest: Reg, src: Mem);
83

84 85 86
    fn emit_and_r32_imm32(&mut self, dest: Reg, src: i32);
    fn emit_and_r32_r32  (&mut self, dest: Reg, src: Reg);
    fn emit_and_r32_mem32(&mut self, dest: Reg, src: Mem);
87

88 89 90
    fn emit_and_r16_imm16(&mut self, dest: Reg, src: i16);
    fn emit_and_r16_r16  (&mut self, dest: Reg, src: Reg);
    fn emit_and_r16_mem16(&mut self, dest: Reg, src: Mem);
91

92 93 94
    fn emit_and_r8_imm8  (&mut self, dest: Reg, src: i8);
    fn emit_and_r8_r8    (&mut self, dest: Reg, src: Reg);
    fn emit_and_r8_mem8  (&mut self, dest: Reg, src: Mem);
95

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    // xor
    fn emit_xor_r64_r64  (&mut self, dest: Reg, src: Reg);
    fn emit_xor_r64_mem64(&mut self, dest: Reg, src: Mem);
    fn emit_xor_r64_imm32(&mut self, dest: Reg, src: i32);

    fn emit_xor_r32_r32  (&mut self, dest: Reg, src: Reg);
    fn emit_xor_r32_mem32(&mut self, dest: Reg, src: Mem);
    fn emit_xor_r32_imm32(&mut self, dest: Reg, src: i32);

    fn emit_xor_r16_r16  (&mut self, dest: Reg, src: Reg);
    fn emit_xor_r16_mem16(&mut self, dest: Reg, src: Reg);
    fn emit_xor_r16_imm16(&mut self, dest: Reg, src: i16);

    fn emit_xor_r8_r8    (&mut self, dest: Reg, src: Reg);
    fn emit_xor_r8_mem8  (&mut self, dest: Reg, src: Reg);
    fn emit_xor_r8_imm8  (&mut self, dest: Reg, src: i8);

113
    // add
114 115 116
    fn emit_add_r64_r64  (&mut self, dest: Reg, src: Reg);
    fn emit_add_r64_mem64(&mut self, dest: Reg, src: Mem);
    fn emit_add_r64_imm32(&mut self, dest: Reg, src: i32);
117 118 119 120 121 122 123 124 125 126 127 128

    fn emit_add_r32_r32  (&mut self, dest: Reg, src: Reg);
    fn emit_add_r32_mem32(&mut self, dest: Reg, src: Mem);
    fn emit_add_r32_imm32(&mut self, dest: Reg, src: i32);

    fn emit_add_r16_r16  (&mut self, dest: Reg, src: Reg);
    fn emit_add_r16_mem16(&mut self, dest: Reg, src: Mem);
    fn emit_add_r16_imm16(&mut self, dest: Reg, src: i16);

    fn emit_add_r8_r8  (&mut self, dest: Reg, src: Reg);
    fn emit_add_r8_mem8(&mut self, dest: Reg, src: Mem);
    fn emit_add_r8_imm8(&mut self, dest: Reg, src: i8);
qinsoon's avatar
qinsoon committed
129
    
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
    // sub
    fn emit_sub_r64_r64  (&mut self, dest: Reg, src: Reg);
    fn emit_sub_r64_mem64(&mut self, dest: Reg, src: Mem);
    fn emit_sub_r64_imm32(&mut self, dest: Reg, src: i32);

    fn emit_sub_r32_r32  (&mut self, dest: Reg, src: Reg);
    fn emit_sub_r32_mem32(&mut self, dest: Reg, src: Mem);
    fn emit_sub_r32_imm32(&mut self, dest: Reg, src: i32);

    fn emit_sub_r16_r16  (&mut self, dest: Reg, src: Reg);
    fn emit_sub_r16_mem16(&mut self, dest: Reg, src: Mem);
    fn emit_sub_r16_imm16(&mut self, dest: Reg, src: i16);

    fn emit_sub_r8_r8  (&mut self, dest: Reg, src: Reg);
    fn emit_sub_r8_mem8(&mut self, dest: Reg, src: Mem);
    fn emit_sub_r8_imm8(&mut self, dest: Reg, src: i8);

    // floating point
    fn emit_addsd_f64_f64  (&mut self, dest: Reg, src: Reg);
    fn emit_addsd_f64_mem64(&mut self, dest: Reg, src: Mem);

    // multiply
    fn emit_mul_r64  (&mut self, src: Reg);
    fn emit_mul_r32  (&mut self, src: Reg);
    fn emit_mul_r16  (&mut self, src: Reg);
    fn emit_mul_r8   (&mut self, src: Reg);

    fn emit_mul_mem64(&mut self, src: Mem);
    fn emit_mul_mem32(&mut self, src: Mem);
    fn emit_mul_mem16(&mut self, src: Mem);
    fn emit_mul_mem8 (&mut self, src: Mem);

    // div
    fn emit_div_r64   (&mut self, src: Reg);
    fn emit_div_r32   (&mut self, src: Reg);
    fn emit_div_r16   (&mut self, src: Reg);
    fn emit_div_r8    (&mut self, src: Reg);

    fn emit_div_mem64 (&mut self, src: Mem);
    fn emit_div_mem32 (&mut self, src: Mem);
    fn emit_div_mem16 (&mut self, src: Mem);
    fn emit_div_mem8  (&mut self, src: Mem);

    // idiv
    fn emit_idiv_r64  (&mut self, src: Reg);
    fn emit_idiv_r32  (&mut self, src: Reg);
    fn emit_idiv_r16  (&mut self, src: Reg);
    fn emit_idiv_r8   (&mut self, src: Reg);

    fn emit_idiv_mem64(&mut self, src: Mem);
    fn emit_idiv_mem32(&mut self, src: Mem);
    fn emit_idiv_mem16(&mut self, src: Mem);
    fn emit_idiv_mem8 (&mut self, src: Mem);

    // shl
    fn emit_shl_r64_cl    (&mut self, dest: Reg);
//    fn emit_shl_r32_cl    (&mut self, dest: Reg);
//    fn emit_shl_r16_cl    (&mut self, dest: Reg);
//    fn emit_shl_r8_cl     (&mut self, dest: Reg);

    fn emit_shl_mem64_cl  (&mut self, dest: Mem);
//    fn emit_shl_mem32_cl  (&mut self, dest: Mem);
//    fn emit_shl_mem16_cl  (&mut self, dest: Mem);
//    fn emit_shl_mem8_cl   (&mut self, dest: Mem);

    fn emit_shl_r64_imm8  (&mut self, dest: Reg, src: i8);
//    fn emit_shl_r32_imm8  (&mut self, dest: Reg, src: i8);
//    fn emit_shl_r16_imm8  (&mut self, dest: Reg, src: i8);
//    fn emit_shl_r8_imm8   (&mut self, dest: Reg, src: i8);

    fn emit_shl_mem64_imm8(&mut self, dest: Mem, src: i8);
//    fn emit_shl_mem32_imm8(&mut self, dest: Mem, src: i8);
//    fn emit_shl_mem16_imm8(&mut self, dest: Mem, src: i8);
//    fn emit_shl_mem8_imm8 (&mut self, dest: Mem, src: i8);
qinsoon's avatar
qinsoon committed
204

qinsoon's avatar
qinsoon committed
205 206 207 208 209 210 211 212 213 214
    fn emit_shr_r64_cl    (&mut self, dest: &P<Value>);
    fn emit_shr_mem64_cl  (&mut self, dest: &P<Value>);
    fn emit_shr_r64_imm8  (&mut self, dest: &P<Value>, src: i8);
    fn emit_shr_mem64_imm8(&mut self, dest: &P<Value>, src: i8);

    fn emit_sar_r64_cl    (&mut self, dest: &P<Value>);
    fn emit_sar_mem64_cl  (&mut self, dest: &P<Value>);
    fn emit_sar_r64_imm8  (&mut self, dest: &P<Value>, src: i8);
    fn emit_sar_mem64_imm8(&mut self, dest: &P<Value>, src: i8);

qinsoon's avatar
qinsoon committed
215
    fn emit_cqo(&mut self);
216
    
217 218 219 220 221 222 223 224 225 226 227
    fn emit_jmp(&mut self, dest: MuName);
    fn emit_je(&mut self, dest: MuName);
    fn emit_jne(&mut self, dest: MuName);
    fn emit_ja(&mut self, dest: MuName);
    fn emit_jae(&mut self, dest: MuName);
    fn emit_jb(&mut self, dest: MuName);
    fn emit_jbe(&mut self, dest: MuName);
    fn emit_jg(&mut self, dest: MuName);
    fn emit_jge(&mut self, dest: MuName);
    fn emit_jl(&mut self, dest: MuName);
    fn emit_jle(&mut self, dest: MuName);
qinsoon's avatar
qinsoon committed
228
    
qinsoon's avatar
qinsoon committed
229 230 231
    fn emit_call_near_rel32(&mut self, callsite: String, func: MuName) -> ValueLocation;
    fn emit_call_near_r64(&mut self, callsite: String, func: &P<Value>) -> ValueLocation;
    fn emit_call_near_mem64(&mut self, callsite: String, func: &P<Value>) -> ValueLocation;
232
    
qinsoon's avatar
qinsoon committed
233
    fn emit_ret(&mut self);
qinsoon's avatar
qinsoon committed
234

235
    fn emit_push_r64(&mut self, src: &P<Value>);
236
    fn emit_push_imm32(&mut self, src: i32);
237
    fn emit_pop_r64(&mut self, dest: &P<Value>);
238 239 240 241 242 243

    // fpr move

    fn emit_movsd_f64_f64  (&mut self, dest: &P<Value>, src: &P<Value>);
    fn emit_movsd_f64_mem64(&mut self, dest: &P<Value>, src: &P<Value>); // load
    fn emit_movsd_mem64_f64(&mut self, dest: &P<Value>, src: &P<Value>); // store
244
}