codegen.rs 7.01 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
pub trait CodeGenerator {
qinsoon's avatar
qinsoon committed
8
    fn start_code(&mut self, func_name: MuName) -> ValueLocation;
9
    fn finish_code(&mut self, func_name: MuName) -> (Box<MachineCode + Sync + Send>, ValueLocation);
10 11 12 13

    // generate unnamed sequence of linear code (no branch)
    fn start_code_sequence(&mut self);
    fn finish_code_sequence(&mut self) -> Box<MachineCode + Sync + Send>;
14 15 16
    
    fn print_cur_code(&self);
    
qinsoon's avatar
qinsoon committed
17
    fn start_block(&mut self, block_name: MuName);
qinsoon's avatar
qinsoon committed
18
    fn start_exception_block(&mut self, block_name: MuName) -> ValueLocation;
qinsoon's avatar
qinsoon committed
19 20 21
    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);
22
    
23
    fn emit_nop(&mut self, bytes: usize);
24 25 26

    // comparison
    fn emit_cmp_r64_r64  (&mut self, op1: &P<Value>, op2: &P<Value>);
27
    fn emit_cmp_r64_imm32(&mut self, op1: &P<Value>, op2: i32);
28
    fn emit_cmp_r64_mem64(&mut self, op1: &P<Value>, op2: &P<Value>);
29 30 31 32 33 34 35 36 37 38 39 40 41 42

    fn emit_cmp_r32_r32  (&mut self, op1: &P<Value>, op2: &P<Value>);
    fn emit_cmp_r32_imm32(&mut self, op1: &P<Value>, op2: i32);
    fn emit_cmp_r32_mem32(&mut self, op1: &P<Value>, op2: &P<Value>);

    fn emit_cmp_r16_r16  (&mut self, op1: &P<Value>, op2: &P<Value>);
    fn emit_cmp_r16_imm16(&mut self, op1: &P<Value>, op2: i16);
    fn emit_cmp_r16_mem16(&mut self, op1: &P<Value>, op2: &P<Value>);

    fn emit_cmp_r8_r8    (&mut self, op1: &P<Value>, op2: &P<Value>);
    fn emit_cmp_r8_imm8  (&mut self, op1: &P<Value>, op2: i8);
    fn emit_cmp_r8_mem8  (&mut self, op1: &P<Value>, op2: &P<Value>);

    // gpr move
43
    
qinsoon's avatar
qinsoon committed
44 45 46 47
    fn emit_mov_r64_imm32  (&mut self, dest: &P<Value>, src: i32);
    fn emit_mov_r64_mem64  (&mut self, dest: &P<Value>, src: &P<Value>); // load
    fn emit_mov_r64_r64    (&mut self, dest: &P<Value>, src: &P<Value>);
    fn emit_mov_mem64_r64  (&mut self, dest: &P<Value>, src: &P<Value>); // store
48
    fn emit_mov_mem64_imm32(&mut self, dest: &P<Value>, src: i32);
qinsoon's avatar
qinsoon committed
49

50 51 52 53 54
    fn emit_mov_r32_imm32  (&mut self, dest: &P<Value>, src: i32);
    fn emit_mov_r32_mem32  (&mut self, dest: &P<Value>, src: &P<Value>); // load
    fn emit_mov_r32_r32    (&mut self, dest: &P<Value>, src: &P<Value>);
    fn emit_mov_mem32_r32  (&mut self, dest: &P<Value>, src: &P<Value>); // store
    fn emit_mov_mem32_imm32(&mut self, dest: &P<Value>, src: i32);
qinsoon's avatar
qinsoon committed
55
    
56 57 58 59 60 61 62 63 64 65 66 67 68
    fn emit_mov_r16_imm16  (&mut self, dest: &P<Value>, src: i16);
    fn emit_mov_r16_mem16  (&mut self, dest: &P<Value>, src: &P<Value>); // load
    fn emit_mov_r16_r16    (&mut self, dest: &P<Value>, src: &P<Value>);
    fn emit_mov_mem16_r16  (&mut self, dest: &P<Value>, src: &P<Value>); // store
    fn emit_mov_mem16_imm16(&mut self, dest: &P<Value>, src: i16);

    fn emit_mov_r8_imm8    (&mut self, dest: &P<Value>, src: i8);
    fn emit_mov_r8_mem8    (&mut self, dest: &P<Value>, src: &P<Value>); // load
    fn emit_mov_r8_r8      (&mut self, dest: &P<Value>, src: &P<Value>);
    fn emit_mov_mem8_r8    (&mut self, dest: &P<Value>, src: &P<Value>); // store
    fn emit_mov_mem8_imm8  (&mut self, dest: &P<Value>, src: i8);

    // lea
69
    fn emit_lea_r64(&mut self, dest: &P<Value>, src: &P<Value>);
70 71

    // and
72
    fn emit_and_r64_imm32(&mut self, dest: &P<Value>, src: i32);
qinsoon's avatar
qinsoon committed
73
    fn emit_and_r64_r64  (&mut self, dest: &P<Value>, src: &P<Value>);
74 75 76 77 78 79 80 81 82 83 84 85 86
    fn emit_and_r64_mem64(&mut self, dest: &P<Value>, src: &P<Value>);

    fn emit_and_r32_imm32(&mut self, dest: &P<Value>, src: i32);
    fn emit_and_r32_r32  (&mut self, dest: &P<Value>, src: &P<Value>);
    fn emit_and_r32_mem32(&mut self, dest: &P<Value>, src: &P<Value>);

    fn emit_and_r16_imm16(&mut self, dest: &P<Value>, src: i16);
    fn emit_and_r16_r16  (&mut self, dest: &P<Value>, src: &P<Value>);
    fn emit_and_r16_mem16(&mut self, dest: &P<Value>, src: &P<Value>);

    fn emit_and_r8_imm8  (&mut self, dest: &P<Value>, src: i8);
    fn emit_and_r8_r8    (&mut self, dest: &P<Value>, src: &P<Value>);
    fn emit_and_r8_mem8  (&mut self, dest: &P<Value>, src: &P<Value>);
87 88 89 90

    fn emit_xor_r64_r64  (&mut self, dest: &P<Value>, src: &P<Value>);
    fn emit_xor_r64_mem64(&mut self, dest: &P<Value>, src: &P<Value>);
    fn emit_xor_r64_imm32(&mut self, dest: &P<Value>, src: i32);
91
    
qinsoon's avatar
qinsoon committed
92
    fn emit_add_r64_r64  (&mut self, dest: &P<Value>, src: &P<Value>);
qinsoon's avatar
qinsoon committed
93
    fn emit_add_r64_mem64(&mut self, dest: &P<Value>, src: &P<Value>);
94
    fn emit_add_r64_imm32(&mut self, dest: &P<Value>, src: i32);
qinsoon's avatar
qinsoon committed
95 96 97

    fn emit_addsd_f64_f64  (&mut self, dest: &P<Value>, src: &P<Value>);
    fn emit_addsd_f64_mem64(&mut self, dest: &P<Value>, src: &P<Value>);
qinsoon's avatar
qinsoon committed
98 99 100
    
    fn emit_sub_r64_r64(&mut self, dest: &P<Value>, src: &P<Value>);
    fn emit_sub_r64_mem64(&mut self, dest: &P<Value>, src: &P<Value>);
101
    fn emit_sub_r64_imm32(&mut self, dest: &P<Value>, src: i32);
qinsoon's avatar
qinsoon committed
102
    
qinsoon's avatar
qinsoon committed
103
    fn emit_mul_r64  (&mut self, src: &P<Value>);
104
    fn emit_mul_mem64(&mut self, src: &P<Value>);
105

qinsoon's avatar
qinsoon committed
106 107 108 109 110
    fn emit_div_r64   (&mut self, src: &P<Value>);
    fn emit_div_mem64 (&mut self, src: &P<Value>);
    fn emit_idiv_r64  (&mut self, src: &P<Value>);
    fn emit_idiv_mem64(&mut self, src: &P<Value>);

111 112 113 114
    fn emit_shl_r64_cl    (&mut self, dest: &P<Value>);
    fn emit_shl_mem64_cl  (&mut self, dest: &P<Value>);
    fn emit_shl_r64_imm8  (&mut self, dest: &P<Value>, src: i8);
    fn emit_shl_mem64_imm8(&mut self, dest: &P<Value>, src: i8);
qinsoon's avatar
qinsoon committed
115

qinsoon's avatar
qinsoon committed
116 117 118 119 120 121 122 123 124 125
    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
126
    fn emit_cqo(&mut self);
127
    
128 129 130 131 132 133 134 135 136 137 138
    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
139
    
qinsoon's avatar
qinsoon committed
140 141 142
    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;
143
    
qinsoon's avatar
qinsoon committed
144
    fn emit_ret(&mut self);
qinsoon's avatar
qinsoon committed
145

146
    fn emit_push_r64(&mut self, src: &P<Value>);
147
    fn emit_push_imm32(&mut self, src: i32);
148
    fn emit_pop_r64(&mut self, dest: &P<Value>);
149 150 151 152 153 154

    // 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
155
}