To protect your data, the CISO officer has suggested users to enable GitLab 2FA as soon as possible.

inst_sel.rs 126 KB
Newer Older
1
use ast::ir::*;
2
use ast::ptr::*;
qinsoon's avatar
qinsoon committed
3
use ast::inst::*;
4
use ast::op;
qinsoon's avatar
qinsoon committed
5
use ast::op::OpCode;
qinsoon's avatar
qinsoon committed
6
use ast::types;
qinsoon's avatar
qinsoon committed
7
use ast::types::*;
qinsoon's avatar
qinsoon committed
8
use vm::VM;
qinsoon's avatar
qinsoon committed
9
use runtime::mm;
10
11
12
13
use runtime::ValueLocation;
use runtime::thread;
use runtime::entrypoints;
use runtime::entrypoints::RuntimeEntrypoint;
14
15

use compiler::CompilerPass;
16
use compiler::backend;
qinsoon's avatar
qinsoon committed
17
use compiler::backend::PROLOGUE_BLOCK_NAME;
qinsoon's avatar
qinsoon committed
18
19
20
use compiler::backend::x86_64;
use compiler::backend::x86_64::CodeGenerator;
use compiler::backend::x86_64::ASMCodeGen;
qinsoon's avatar
qinsoon committed
21
22
use compiler::machine_code::CompiledFunction;
use compiler::frame::Frame;
23

24
use std::collections::HashMap;
qinsoon's avatar
qinsoon committed
25
use std::any::Any;
26

27
pub struct InstructionSelection {
28
    name: &'static str,
29
30
    backend: Box<CodeGenerator>,
    
qinsoon's avatar
qinsoon committed
31
    current_callsite_id: usize,
qinsoon's avatar
qinsoon committed
32
33
    current_frame: Option<Frame>,
    current_block: Option<MuName>,
qinsoon's avatar
qinsoon committed
34
35
36
37
38
    current_func_start: Option<ValueLocation>,
    // key: block id, val: callsite that names the block as exception block
    current_exn_callsites: HashMap<MuID, Vec<ValueLocation>>,
    // key: block id, val: block location
    current_exn_blocks: HashMap<MuID, ValueLocation>     
39
40
}

41
impl <'a> InstructionSelection {
qinsoon's avatar
qinsoon committed
42
    #[cfg(feature = "aot")]
43
    pub fn new() -> InstructionSelection {
44
45
        InstructionSelection{
            name: "Instruction Selection (x64)",
46
            backend: Box::new(ASMCodeGen::new()),
qinsoon's avatar
qinsoon committed
47
            
qinsoon's avatar
qinsoon committed
48
            current_callsite_id: 0,
qinsoon's avatar
qinsoon committed
49
50
51
            current_frame: None,
            current_block: None,
            current_func_start: None,
qinsoon's avatar
qinsoon committed
52
53
54
            // key: block id, val: callsite that names the block as exception block
            current_exn_callsites: HashMap::new(), 
            current_exn_blocks: HashMap::new()
55
56
        }
    }
qinsoon's avatar
qinsoon committed
57
58
59
60
61

    #[cfg(feature = "jit")]
    pub fn new() -> InstructionSelection {
        unimplemented!()
    }
62
63
64
65
66
67
68
    
    // in this pass, we assume that
    // 1. all temporaries will use 64bit registers
    // 2. we do not need to backup/restore caller-saved registers
    // 3. we need to backup/restore all the callee-saved registers
    // if any of these assumption breaks, we will need to re-emit the code
    #[allow(unused_variables)]
69
    fn instruction_select(&mut self, node: &'a TreeNode, f_content: &FunctionContent, f_context: &mut FunctionContext, vm: &VM) {
qinsoon's avatar
qinsoon committed
70
71
72
        trace!("instsel on node {}", node);
        
        match node.v {
73
74
            TreeNode_::Instruction(ref inst) => {
                match inst.v {
qinsoon's avatar
qinsoon committed
75
                    Instruction_::Branch2{cond, ref true_dest, ref false_dest, true_prob} => {
76
77
                        // 'branch_if_true' == true, we emit cjmp the same as CmpOp  (je  for EQ, jne for NE)
                        // 'branch_if_true' == false, we emit opposite cjmp as CmpOp (jne for EQ, je  for NE)
78
79
80
81
82
                        let (fallthrough_dest, branch_dest, branch_if_true) = {
                            if true_prob > 0.5f32 {
                                (true_dest, false_dest, false)
                            } else {
                                (false_dest, true_dest, true)
83
                            }
84
                        };
85
                        
qinsoon's avatar
qinsoon committed
86
                        let ops = inst.ops.read().unwrap();
87
                        
88
89
                        self.process_dest(&ops, fallthrough_dest, f_content, f_context, vm);
                        self.process_dest(&ops, branch_dest, f_content, f_context, vm);
qinsoon's avatar
qinsoon committed
90
                        
91
                        let branch_target = f_content.get_block(branch_dest.target).name().unwrap();
92
93
94
    
                        let ref cond = ops[cond];
                        
qinsoon's avatar
qinsoon committed
95
                        if self.match_cmp_res(cond) {
96
                            trace!("emit cmp_res-branch2");
97
                            match self.emit_cmp_res(cond, f_content, f_context, vm) {
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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
                                op::CmpOp::EQ => {
                                    if branch_if_true {
                                        self.backend.emit_je(branch_target);
                                    } else {
                                        self.backend.emit_jne(branch_target);
                                    }
                                },
                                op::CmpOp::NE => {
                                    if branch_if_true {
                                        self.backend.emit_jne(branch_target);
                                    } else {
                                        self.backend.emit_je(branch_target);
                                    }
                                },
                                op::CmpOp::UGE => {
                                    if branch_if_true {
                                        self.backend.emit_jae(branch_target);
                                    } else {
                                        self.backend.emit_jb(branch_target);
                                    }
                                },
                                op::CmpOp::UGT => {
                                    if branch_if_true {
                                        self.backend.emit_ja(branch_target);
                                    } else {
                                        self.backend.emit_jbe(branch_target);
                                    }
                                },
                                op::CmpOp::ULE => {
                                    if branch_if_true {
                                        self.backend.emit_jbe(branch_target);
                                    } else {
                                        self.backend.emit_ja(branch_target);
                                    }
                                },
                                op::CmpOp::ULT => {
                                    if branch_if_true {
                                        self.backend.emit_jb(branch_target);
                                    } else {
                                        self.backend.emit_jae(branch_target);
                                    }
                                },
                                op::CmpOp::SGE => {
                                    if branch_if_true {
                                        self.backend.emit_jge(branch_target);
                                    } else {
                                        self.backend.emit_jl(branch_target);
                                    }
                                },
                                op::CmpOp::SGT => {
                                    if branch_if_true {
                                        self.backend.emit_jg(branch_target);
                                    } else {
                                        self.backend.emit_jle(branch_target);
                                    }
                                },
                                op::CmpOp::SLE => {
                                    if branch_if_true {
                                        self.backend.emit_jle(branch_target);
                                    } else {
                                        self.backend.emit_jg(branch_target);
                                    }
                                },
                                op::CmpOp::SLT => {
                                    if branch_if_true {
                                        self.backend.emit_jl(branch_target);
                                    } else {
                                        self.backend.emit_jge(branch_target);
                                    }
                                },
qinsoon's avatar
qinsoon committed
168
169
170
171
                                _ => unimplemented!()
                            }
                        } else if self.match_ireg(cond) {
                            trace!("emit ireg-branch2");
172
                            
173
                            let cond_reg = self.emit_ireg(cond, f_content, f_context, vm);
174
                            
qinsoon's avatar
qinsoon committed
175
                            // emit: cmp cond_reg 1
qinsoon's avatar
qinsoon committed
176
                            self.backend.emit_cmp_imm_r(1, &cond_reg);
qinsoon's avatar
qinsoon committed
177
                            // emit: je #branch_dest
178
                            self.backend.emit_je(branch_target);
qinsoon's avatar
qinsoon committed
179
180
                        } else {
                            unimplemented!();
181
                        }
182
                    },
qinsoon's avatar
qinsoon committed
183
184
185
186
187
188
189
190
191
192
193
194
195
196

                    Instruction_::Select{cond, true_val, false_val} => {
                        let ops = inst.ops.read().unwrap();

                        let ref cond = ops[cond];
                        let ref true_val = ops[true_val];
                        let ref false_val = ops[false_val];

                        if self.match_ireg(true_val) {
                            // moving integers/pointers
                            let tmp_res   = self.get_result_value(node);
                            let tmp_true  = self.emit_ireg(true_val, f_content, f_context, vm);
                            let tmp_false = self.emit_ireg(false_val, f_content, f_context, vm);

qinsoon's avatar
qinsoon committed
197
198
199
                            // mov tmp_false -> tmp_res
                            self.backend.emit_mov_r_r(&tmp_res, &tmp_false);

qinsoon's avatar
qinsoon committed
200
201
202
                            if self.match_cmp_res(cond) {
                                match self.emit_cmp_res(cond, f_content, f_context, vm) {
                                    op::CmpOp::EQ => {
qinsoon's avatar
qinsoon committed
203
                                        self.backend.emit_cmove_r_r (&tmp_res, &tmp_true);
qinsoon's avatar
qinsoon committed
204
205
                                    }
                                    op::CmpOp::NE => {
qinsoon's avatar
qinsoon committed
206
                                        self.backend.emit_cmovne_r_r(&tmp_res, &tmp_true);
qinsoon's avatar
qinsoon committed
207
208
                                    }
                                    op::CmpOp::SGE => {
qinsoon's avatar
qinsoon committed
209
                                        self.backend.emit_cmovge_r_r(&tmp_res, &tmp_true);
qinsoon's avatar
qinsoon committed
210
211
                                    }
                                    op::CmpOp::SGT => {
qinsoon's avatar
qinsoon committed
212
                                        self.backend.emit_cmovg_r_r (&tmp_res, &tmp_true);
qinsoon's avatar
qinsoon committed
213
214
                                    }
                                    op::CmpOp::SLE => {
qinsoon's avatar
qinsoon committed
215
                                        self.backend.emit_cmovle_r_r(&tmp_res, &tmp_true);
qinsoon's avatar
qinsoon committed
216
217
                                    }
                                    op::CmpOp::SLT => {
qinsoon's avatar
qinsoon committed
218
                                        self.backend.emit_cmovl_r_r (&tmp_res, &tmp_true);
qinsoon's avatar
qinsoon committed
219
220
                                    }
                                    op::CmpOp::UGE => {
qinsoon's avatar
qinsoon committed
221
                                        self.backend.emit_cmovae_r_r(&tmp_res, &tmp_true);
qinsoon's avatar
qinsoon committed
222
223
                                    }
                                    op::CmpOp::UGT => {
qinsoon's avatar
qinsoon committed
224
                                        self.backend.emit_cmova_r_r (&tmp_res, &tmp_true);
qinsoon's avatar
qinsoon committed
225
226
                                    }
                                    op::CmpOp::ULE => {
qinsoon's avatar
qinsoon committed
227
                                        self.backend.emit_cmovbe_r_r(&tmp_res, &tmp_true);
qinsoon's avatar
qinsoon committed
228
229
                                    }
                                    op::CmpOp::ULT => {
qinsoon's avatar
qinsoon committed
230
                                        self.backend.emit_cmovb_r_r (&tmp_res, &tmp_true);
qinsoon's avatar
qinsoon committed
231
232
233
234
235
236
                                    }
                                    _ => panic!("expecting CmpOp for integers")
                                }
                            } else if self.match_ireg(cond) {
                                let tmp_cond = self.emit_ireg(cond, f_content, f_context, vm);

qinsoon's avatar
qinsoon committed
237
238
239
                                // emit: mov tmp_false -> tmp_res
                                self.backend.emit_mov_r_r(&tmp_res, &tmp_false);

qinsoon's avatar
qinsoon committed
240
                                // emit: cmp cond_reg 1
qinsoon's avatar
qinsoon committed
241
                                self.backend.emit_cmp_imm_r(1, &tmp_cond);
qinsoon's avatar
qinsoon committed
242
243

                                // emit: cmove tmp_true -> tmp_res
qinsoon's avatar
qinsoon committed
244
                                self.backend.emit_cmove_r_r(&tmp_res, &tmp_true);
qinsoon's avatar
qinsoon committed
245
246
247
248
249
250
251
252
253
                            } else {
                                unimplemented!()
                            }
                        } else {
                            // moving vectors, floatingpoints
                            unimplemented!()
                        }
                    },

254
255
256
257
258
259
260
261
262
263
                    Instruction_::CmpOp(op, op1, op2) => {
                        let ops = inst.ops.read().unwrap();
                        let ref op1 = ops[op1];
                        let ref op2 = ops[op2];

                        if self.match_ireg(op1) {
                            debug_assert!(self.match_ireg(op2));

                            let tmp_res = self.get_result_value(node);

qinsoon's avatar
qinsoon committed
264
265
266
                            // make res64, and set to zero
                            let tmp_res64 = self.make_temporary(f_context, UINT64_TYPE.clone(), vm);
                            self.backend.emit_xor_r_r(&tmp_res64, &tmp_res64);
267

qinsoon's avatar
qinsoon committed
268
269
270
                            // set tmp1 as 1 (cmov doesnt allow immediate or reg8 as operand)
                            let tmp_1 = self.make_temporary(f_context, UINT64_TYPE.clone(), vm);
                            self.backend.emit_mov_r_imm(&tmp_1, 1);
271
272
273

                            // cmov 1 to result
                            match self.emit_cmp_res(node, f_content, f_context, vm) {
qinsoon's avatar
qinsoon committed
274
275
276
277
278
279
280
281
282
283
                                op::CmpOp::EQ  => self.backend.emit_cmove_r_r (&tmp_res64, &tmp_1),
                                op::CmpOp::NE  => self.backend.emit_cmovne_r_r(&tmp_res64, &tmp_1),
                                op::CmpOp::SGE => self.backend.emit_cmovge_r_r(&tmp_res64, &tmp_1),
                                op::CmpOp::SGT => self.backend.emit_cmovg_r_r (&tmp_res64, &tmp_1),
                                op::CmpOp::SLE => self.backend.emit_cmovle_r_r(&tmp_res64, &tmp_1),
                                op::CmpOp::SLT => self.backend.emit_cmovl_r_r (&tmp_res64, &tmp_1),
                                op::CmpOp::UGE => self.backend.emit_cmovae_r_r(&tmp_res64, &tmp_1),
                                op::CmpOp::UGT => self.backend.emit_cmova_r_r (&tmp_res64, &tmp_1),
                                op::CmpOp::ULE => self.backend.emit_cmovbe_r_r(&tmp_res64, &tmp_1),
                                op::CmpOp::ULT => self.backend.emit_cmovb_r_r (&tmp_res64, &tmp_1),
284
285
                                _ => panic!("expecting integer comparison op with int values")
                            }
qinsoon's avatar
qinsoon committed
286
287
288

                            // truncate tmp_res64 to tmp_res (probably u8)
                            self.backend.emit_mov_r_r(&tmp_res, &tmp_res64);
289
290
291
292
293
                        } else {
                            unimplemented!()
                        }
                    }

qinsoon's avatar
qinsoon committed
294
                    Instruction_::Branch1(ref dest) => {
qinsoon's avatar
qinsoon committed
295
                        let ops = inst.ops.read().unwrap();
296
                                            
297
                        self.process_dest(&ops, dest, f_content, f_context, vm);
298
                        
299
                        let target = f_content.get_block(dest.target).name().unwrap();
qinsoon's avatar
qinsoon committed
300
                        
qinsoon's avatar
qinsoon committed
301
                        trace!("emit branch1");
302
                        // jmp
qinsoon's avatar
qinsoon committed
303
                        self.backend.emit_jmp(target);
304
                    },
qinsoon's avatar
qinsoon committed
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326

                    Instruction_::Switch{cond, ref default, ref branches} => {
                        let ops = inst.ops.read().unwrap();

                        let ref cond = ops[cond];

                        if self.match_ireg(cond) {
                            let tmp_cond = self.emit_ireg(cond, f_content, f_context, vm);

                            // emit each branch
                            for &(case_op_index, ref case_dest) in branches {
                                let ref case_op = ops[case_op_index];

                                // process dest
                                self.process_dest(&ops, case_dest, f_content, f_context, vm);

                                let target = f_content.get_block(case_dest.target).name().unwrap();

                                if self.match_iimm(case_op) {
                                    let imm = self.node_iimm_to_i32(case_op);

                                    // cmp case cond
qinsoon's avatar
qinsoon committed
327
                                    self.backend.emit_cmp_imm_r(imm, &tmp_cond);
qinsoon's avatar
qinsoon committed
328
329
330
331
332
333
                                    // je dest
                                    self.backend.emit_je(target);
                                } else if self.match_ireg(case_op) {
                                    let tmp_case_op = self.emit_ireg(case_op, f_content, f_context, vm);

                                    // cmp case cond
qinsoon's avatar
qinsoon committed
334
                                    self.backend.emit_cmp_r_r(&tmp_case_op, &tmp_cond);
qinsoon's avatar
qinsoon committed
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
                                    // je dest
                                    self.backend.emit_je(target);
                                } else {
                                    panic!("expecting ireg cond to be either iimm or ireg: {}", cond);
                                }
                            }

                            // emit default
                            self.process_dest(&ops, default, f_content, f_context, vm);
                            
                            let default_target = f_content.get_block(default.target).name().unwrap();
                            self.backend.emit_jmp(default_target);
                        } else {
                            panic!("expecting cond in switch to be ireg: {}", cond);
                        }
                    }
351
                    
qinsoon's avatar
qinsoon committed
352
                    Instruction_::ExprCall{ref data, is_abort} => {
qinsoon's avatar
qinsoon committed
353
354
                        if is_abort {
                            unimplemented!()
355
                        }
356
                        
qinsoon's avatar
qinsoon committed
357
358
359
360
361
                        self.emit_mu_call(
                            inst, // inst: &Instruction,
                            data, // calldata: &CallData,
                            None, // resumption: Option<&ResumptionData>,
                            node, // cur_node: &TreeNode, 
362
                            f_content, f_context, vm);
363
364
                    },
                    
qinsoon's avatar
qinsoon committed
365
366
367
368
369
370
371
                    Instruction_::Call{ref data, ref resume} => {
                        self.emit_mu_call(
                            inst, 
                            data, 
                            Some(resume), 
                            node, 
                            f_content, f_context, vm);
qinsoon's avatar
qinsoon committed
372
373
374
375
376
377
378
379
380
381
382
383
                    },

                    Instruction_::ExprCCall{ref data, is_abort} => {
                        if is_abort {
                            unimplemented!()
                        }

                        self.emit_c_call_ir(inst, data, None, node, f_content, f_context, vm);
                    }

                    Instruction_::CCall{ref data, ref resume} => {
                        self.emit_c_call_ir(inst, data, Some(resume), node, f_content, f_context, vm);
qinsoon's avatar
qinsoon committed
384
385
                    }
                    
386
                    Instruction_::Return(_) => {
387
                        self.emit_common_epilogue(inst, f_content, f_context, vm);
388
                        
qinsoon's avatar
qinsoon committed
389
                        self.backend.emit_ret();
390
391
                    },
                    
qinsoon's avatar
qinsoon committed
392
                    Instruction_::BinOp(op, op1, op2) => {
qinsoon's avatar
qinsoon committed
393
                        let ops = inst.ops.read().unwrap();
394
395

                        let res_tmp = self.get_result_value(node);
qinsoon's avatar
qinsoon committed
396
                        
397
398
                        match op {
                            op::BinOp::Add => {
qinsoon's avatar
qinsoon committed
399
                                if self.match_ireg(&ops[op1]) && self.match_iimm(&ops[op2]) {
qinsoon's avatar
qinsoon committed
400
401
                                    trace!("emit add-ireg-imm");
                                    
402
                                    let reg_op1 = self.emit_ireg(&ops[op1], f_content, f_context, vm);
403
                                    let reg_op2 = self.node_iimm_to_i32(&ops[op2]);
qinsoon's avatar
qinsoon committed
404
405
                                    
                                    // mov op1, res
qinsoon's avatar
qinsoon committed
406
                                    self.backend.emit_mov_r_r(&res_tmp, &reg_op1);
qinsoon's avatar
qinsoon committed
407
                                    // add op2, res
qinsoon's avatar
qinsoon committed
408
                                    self.backend.emit_add_r_imm(&res_tmp, reg_op2);
qinsoon's avatar
qinsoon committed
409
410
411
                                } else if self.match_ireg(&ops[op1]) && self.match_mem(&ops[op2]) {
                                    trace!("emit add-ireg-mem");
                                    
412
                                    let reg_op1 = self.emit_ireg(&ops[op1], f_content, f_context, vm);
qinsoon's avatar
shl    
qinsoon committed
413
                                    let reg_op2 = self.emit_mem(&ops[op2], vm);
qinsoon's avatar
qinsoon committed
414
415
                                    
                                    // mov op1, res
qinsoon's avatar
qinsoon committed
416
                                    self.backend.emit_mov_r_r(&res_tmp, &reg_op1);
qinsoon's avatar
qinsoon committed
417
                                    // add op2 res
qinsoon's avatar
qinsoon committed
418
                                    self.backend.emit_add_r_mem(&res_tmp, &reg_op2);
qinsoon's avatar
qinsoon committed
419
420
421
422
423
424
425
                                } else if self.match_ireg(&ops[op1]) && self.match_ireg(&ops[op2]) {
                                    trace!("emit add-ireg-ireg");

                                    let reg_op1 = self.emit_ireg(&ops[op1], f_content, f_context, vm);
                                    let reg_op2 = self.emit_ireg(&ops[op2], f_content, f_context, vm);

                                    // mov op1, res
qinsoon's avatar
qinsoon committed
426
                                    self.backend.emit_mov_r_r(&res_tmp, &reg_op1);
qinsoon's avatar
qinsoon committed
427
                                    // add op2 res
qinsoon's avatar
qinsoon committed
428
                                    self.backend.emit_add_r_r(&res_tmp, &reg_op2);
qinsoon's avatar
qinsoon committed
429
430
431
                                } else {
                                    unimplemented!()
                                }
432
433
                            },
                            op::BinOp::Sub => {
434
                                if self.match_ireg(&ops[op1]) && self.match_iimm(&ops[op2]) {
qinsoon's avatar
qinsoon committed
435
436
                                    trace!("emit sub-ireg-imm");

437
                                    let reg_op1 = self.emit_ireg(&ops[op1], f_content, f_context, vm);
438
                                    let imm_op2 = self.node_iimm_to_i32(&ops[op2]);
qinsoon's avatar
qinsoon committed
439
440
                                    
                                    // mov op1, res
qinsoon's avatar
qinsoon committed
441
                                    self.backend.emit_mov_r_r(&res_tmp, &reg_op1);
qinsoon's avatar
qinsoon committed
442
                                    // add op2, res
qinsoon's avatar
qinsoon committed
443
                                    self.backend.emit_sub_r_imm(&res_tmp, imm_op2);
qinsoon's avatar
qinsoon committed
444
445
446
                                } else if self.match_ireg(&ops[op1]) && self.match_mem(&ops[op2]) {
                                    trace!("emit sub-ireg-mem");
                                    
447
                                    let reg_op1 = self.emit_ireg(&ops[op1], f_content, f_context, vm);
qinsoon's avatar
shl    
qinsoon committed
448
                                    let mem_op2 = self.emit_mem(&ops[op2], vm);
qinsoon's avatar
qinsoon committed
449
450
                                    
                                    // mov op1, res
qinsoon's avatar
qinsoon committed
451
                                    self.backend.emit_mov_r_r(&res_tmp, &reg_op1);
qinsoon's avatar
qinsoon committed
452
                                    // sub op2 res
qinsoon's avatar
qinsoon committed
453
                                    self.backend.emit_sub_r_mem(&res_tmp, &mem_op2);
454
455
                                } else if self.match_ireg(&ops[op1]) && self.match_ireg(&ops[op2]) {
                                    trace!("emit sub-ireg-ireg");
456

457
458
                                    let reg_op1 = self.emit_ireg(&ops[op1], f_content, f_context, vm);
                                    let reg_op2 = self.emit_ireg(&ops[op2], f_content, f_context, vm);
459

460
                                    // mov op1, res
qinsoon's avatar
qinsoon committed
461
                                    self.backend.emit_mov_r_r(&res_tmp, &reg_op1);
462
                                    // add op2 res
qinsoon's avatar
qinsoon committed
463
                                    self.backend.emit_sub_r_r(&res_tmp, &reg_op2);
qinsoon's avatar
qinsoon committed
464
465
466
                                } else {
                                    unimplemented!()
                                }
467
                            },
468
469
470
471
472
473
474
475
476
477
478
                            op::BinOp::And => {
                                let op1 = &ops[op1];
                                let op2 = &ops[op2];

                                if self.match_ireg(op1) && self.match_iimm(op2) {
                                    trace!("emit and-ireg-iimm");

                                    let tmp_op1 = self.emit_ireg(op1, f_content, f_context, vm);
                                    let imm_op2 = self.node_iimm_to_i32(op2);

                                    // mov op1 -> res
qinsoon's avatar
qinsoon committed
479
                                    self.backend.emit_mov_r_r(&res_tmp, &tmp_op1);
480
                                    // and op2, res -> res
qinsoon's avatar
qinsoon committed
481
                                    self.backend.emit_and_r_imm(&res_tmp, imm_op2);
482
483
484
485
486
487
488
                                } else if self.match_ireg(op1) && self.match_mem(op2) {
                                    trace!("emit and-ireg-mem");

                                    let tmp_op1 = self.emit_ireg(op1, f_content, f_context, vm);
                                    let mem_op2 = self.emit_mem(op2, vm);

                                    // mov op1, res
qinsoon's avatar
qinsoon committed
489
                                    self.backend.emit_mov_r_r(&res_tmp, &tmp_op1);
490
                                    // and op2, res -> res
qinsoon's avatar
qinsoon committed
491
                                    self.backend.emit_and_r_mem(&res_tmp, &mem_op2);
492
493
494
495
496
497
498
                                } else if self.match_ireg(op1) && self.match_ireg(op2) {
                                    trace!("emit and-ireg-ireg");

                                    let tmp_op1 = self.emit_ireg(op1, f_content, f_context, vm);
                                    let tmp_op2 = self.emit_ireg(op2, f_content, f_context, vm);

                                    // mov op1, res
qinsoon's avatar
qinsoon committed
499
                                    self.backend.emit_mov_r_r(&res_tmp, &tmp_op1);
500
                                    // and op2, res -> res
qinsoon's avatar
qinsoon committed
501
                                    self.backend.emit_and_r_r(&res_tmp, &tmp_op2);
502
503
504
505
                                } else {
                                    unimplemented!()
                                }
                            },
506
507
508
509
510
511
512
513
514
515
516
                            op::BinOp::Or => {
                                let op1 = &ops[op1];
                                let op2 = &ops[op2];

                                if self.match_ireg(op1) && self.match_iimm(op2) {
                                    trace!("emit or-ireg-iimm");

                                    let tmp_op1 = self.emit_ireg(op1, f_content, f_context, vm);
                                    let imm_op2 = self.node_iimm_to_i32(op2);

                                    // mov op1 -> res
qinsoon's avatar
qinsoon committed
517
                                    self.backend.emit_mov_r_r(&res_tmp, &tmp_op1);
518
                                    // Or op2, res -> res
qinsoon's avatar
qinsoon committed
519
                                    self.backend.emit_or_r_imm(&res_tmp, imm_op2);
520
521
522
523
524
525
526
                                } else if self.match_ireg(op1) && self.match_mem(op2) {
                                    trace!("emit or-ireg-mem");

                                    let tmp_op1 = self.emit_ireg(op1, f_content, f_context, vm);
                                    let mem_op2 = self.emit_mem(op2, vm);

                                    // mov op1, res
qinsoon's avatar
qinsoon committed
527
                                    self.backend.emit_mov_r_r(&res_tmp, &tmp_op1);
528
                                    // Or op2, res -> res
qinsoon's avatar
qinsoon committed
529
                                    self.backend.emit_or_r_mem(&res_tmp, &mem_op2);
530
531
532
533
534
535
536
                                } else if self.match_ireg(op1) && self.match_ireg(op2) {
                                    trace!("emit or-ireg-ireg");

                                    let tmp_op1 = self.emit_ireg(op1, f_content, f_context, vm);
                                    let tmp_op2 = self.emit_ireg(op2, f_content, f_context, vm);

                                    // mov op1, res
qinsoon's avatar
qinsoon committed
537
                                    self.backend.emit_mov_r_r(&res_tmp, &tmp_op1);
538
                                    // Or op2, res -> res
qinsoon's avatar
qinsoon committed
539
                                    self.backend.emit_or_r_r(&res_tmp, &tmp_op2);
540
541
542
543
                                } else {
                                    unimplemented!()
                                }
                            },
544
545
546
547
548
549
550
551
552
553
554
                            op::BinOp::Xor => {
                                let op1 = &ops[op1];
                                let op2 = &ops[op2];

                                if self.match_ireg(op1) && self.match_iimm(op2) {
                                    trace!("emit xor-ireg-iimm");

                                    let tmp_op1 = self.emit_ireg(op1, f_content, f_context, vm);
                                    let imm_op2 = self.node_iimm_to_i32(op2);

                                    // mov op1 -> res
qinsoon's avatar
qinsoon committed
555
                                    self.backend.emit_mov_r_r(&res_tmp, &tmp_op1);
556
                                    // xor op2, res -> res
qinsoon's avatar
qinsoon committed
557
                                    self.backend.emit_xor_r_imm(&res_tmp, imm_op2);
558
559
560
561
562
563
564
                                } else if self.match_ireg(op1) && self.match_mem(op2) {
                                    trace!("emit xor-ireg-mem");

                                    let tmp_op1 = self.emit_ireg(op1, f_content, f_context, vm);
                                    let mem_op2 = self.emit_mem(op2, vm);

                                    // mov op1, res
qinsoon's avatar
qinsoon committed
565
                                    self.backend.emit_mov_r_r(&res_tmp, &tmp_op1);
566
                                    // xor op2, res -> res
qinsoon's avatar
qinsoon committed
567
                                    self.backend.emit_xor_r_mem(&res_tmp, &mem_op2);
568
569
570
571
572
573
574
                                } else if self.match_ireg(op1) && self.match_ireg(op2) {
                                    trace!("emit xor-ireg-ireg");

                                    let tmp_op1 = self.emit_ireg(op1, f_content, f_context, vm);
                                    let tmp_op2 = self.emit_ireg(op2, f_content, f_context, vm);

                                    // mov op1, res
qinsoon's avatar
qinsoon committed
575
                                    self.backend.emit_mov_r_r(&res_tmp, &tmp_op1);
576
                                    // xor op2, res -> res
qinsoon's avatar
qinsoon committed
577
                                    self.backend.emit_xor_r_r(&res_tmp, &tmp_op2);
578
579
580
581
                                } else {
                                    unimplemented!()
                                }
                            }
582
                            op::BinOp::Mul => {
583
584
                                // mov op1 -> rax
                                let op1 = &ops[op1];
qinsoon's avatar
qinsoon committed
585
586
587
588
589
590
591
592
593

                                let mreg_op1 = match op1.clone_value().ty.get_int_length() {
                                    Some(64) => x86_64::RAX.clone(),
                                    Some(32) => x86_64::EAX.clone(),
                                    Some(16) => x86_64::AX.clone(),
                                    Some(8)  => x86_64::AL.clone(),
                                    _ => unimplemented!()
                                };

594
                                if self.match_iimm(op1) {
595
                                    let imm_op1 = self.node_iimm_to_i32(op1);
596
                                    
qinsoon's avatar
qinsoon committed
597
                                    self.backend.emit_mov_r_imm(&mreg_op1, imm_op1);
598
                                } else if self.match_mem(op1) {
qinsoon's avatar
shl    
qinsoon committed
599
                                    let mem_op1 = self.emit_mem(op1, vm);
600
                                    
qinsoon's avatar
qinsoon committed
601
                                    self.backend.emit_mov_r_mem(&mreg_op1, &mem_op1);
602
603
604
                                } else if self.match_ireg(op1) {
                                    let reg_op1 = self.emit_ireg(op1, f_content, f_context, vm);

qinsoon's avatar
qinsoon committed
605
                                    self.backend.emit_mov_r_r(&mreg_op1, &reg_op1);
606
607
608
609
                                } else {
                                    unimplemented!();
                                }
                                
qinsoon's avatar
qinsoon committed
610
                                // mul op2
611
                                let op2 = &ops[op2];
612
                                if self.match_iimm(op2) {
613
                                    let imm_op2 = self.node_iimm_to_i32(op2);
614
615
616
                                    
                                    // put imm in a temporary
                                    // here we use result reg as temporary
qinsoon's avatar
qinsoon committed
617
                                    self.backend.emit_mov_r_imm(&res_tmp, imm_op2);
618
                                    
qinsoon's avatar
qinsoon committed
619
                                    self.backend.emit_mul_r(&res_tmp);
620
                                } else if self.match_mem(op2) {
qinsoon's avatar
shl    
qinsoon committed
621
                                    let mem_op2 = self.emit_mem(op2, vm);
622
                                    
qinsoon's avatar
qinsoon committed
623
                                    self.backend.emit_mul_mem(&mem_op2);
624
625
626
                                } else if self.match_ireg(op2) {
                                    let reg_op2 = self.emit_ireg(op2, f_content, f_context, vm);

qinsoon's avatar
qinsoon committed
627
                                    self.backend.emit_mul_r(&reg_op2);
628
629
630
631
632
                                } else {
                                    unimplemented!();
                                }
                                
                                // mov rax -> result
qinsoon's avatar
qinsoon committed
633
634
635
636
637
638
639
640
                                match res_tmp.ty.get_int_length() {
                                    Some(64) => self.backend.emit_mov_r_r(&res_tmp, &x86_64::RAX),
                                    Some(32) => self.backend.emit_mov_r_r(&res_tmp, &x86_64::EAX),
                                    Some(16) => self.backend.emit_mov_r_r(&res_tmp, &x86_64::AX),
                                    Some(8)  => self.backend.emit_mov_r_r(&res_tmp, &x86_64::AL),
                                    _ => unimplemented!()
                                }

641
                            },
qinsoon's avatar
qinsoon committed
642
643
644
645
                            op::BinOp::Udiv => {
                                let op1 = &ops[op1];
                                let op2 = &ops[op2];

qinsoon's avatar
sdiv    
qinsoon committed
646
                                self.emit_udiv(op1, op2, f_content, f_context, vm);
qinsoon's avatar
qinsoon committed
647

qinsoon's avatar
sdiv    
qinsoon committed
648
                                // mov rax -> result
qinsoon's avatar
qinsoon committed
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
                                match res_tmp.ty.get_int_length() {
                                    Some(64) => {
                                        self.backend.emit_mov_r_r(&res_tmp, &x86_64::RAX);
                                    }
                                    Some(32) => {
                                        self.backend.emit_mov_r_r(&res_tmp, &x86_64::EAX);
                                    }
                                    Some(16) => {
                                        self.backend.emit_mov_r_r(&res_tmp, &x86_64::AX);
                                    }
                                    Some(8)  => {
                                        self.backend.emit_mov_r_r(&res_tmp, &x86_64::AL);
                                    }
                                    _ => unimplemented!()
                                }
qinsoon's avatar
sdiv    
qinsoon committed
664
665
666
667
                            },
                            op::BinOp::Sdiv => {
                                let op1 = &ops[op1];
                                let op2 = &ops[op2];
qinsoon's avatar
qinsoon committed
668

qinsoon's avatar
sdiv    
qinsoon committed
669
                                self.emit_idiv(op1, op2, f_content, f_context, vm);
qinsoon's avatar
qinsoon committed
670
671

                                // mov rax -> result
qinsoon's avatar
qinsoon committed
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
                                match res_tmp.ty.get_int_length() {
                                    Some(64) => {
                                        self.backend.emit_mov_r_r(&res_tmp, &x86_64::RAX);
                                    }
                                    Some(32) => {
                                        self.backend.emit_mov_r_r(&res_tmp, &x86_64::EAX);
                                    }
                                    Some(16) => {
                                        self.backend.emit_mov_r_r(&res_tmp, &x86_64::AX);
                                    }
                                    Some(8)  => {
                                        self.backend.emit_mov_r_r(&res_tmp, &x86_64::AL);
                                    }
                                    _ => unimplemented!()
                                }
qinsoon's avatar
sdiv    
qinsoon committed
687
                            },
qinsoon's avatar
qinsoon committed
688
689
690
691
692
693
694
                            op::BinOp::Urem => {
                                let op1 = &ops[op1];
                                let op2 = &ops[op2];

                                self.emit_udiv(op1, op2, f_content, f_context, vm);

                                // mov rdx -> result
qinsoon's avatar
qinsoon committed
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
                                match res_tmp.ty.get_int_length() {
                                    Some(64) => {
                                        self.backend.emit_mov_r_r(&res_tmp, &x86_64::RDX);
                                    }
                                    Some(32) => {
                                        self.backend.emit_mov_r_r(&res_tmp, &x86_64::EDX);
                                    }
                                    Some(16) => {
                                        self.backend.emit_mov_r_r(&res_tmp, &x86_64::DX);
                                    }
                                    Some(8)  => {
                                        self.backend.emit_mov_r_r(&res_tmp, &x86_64::AH);
                                    }
                                    _ => unimplemented!()
                                }
qinsoon's avatar
qinsoon committed
710
711
712
713
714
715
716
717
                            },
                            op::BinOp::Srem => {
                                let op1 = &ops[op1];
                                let op2 = &ops[op2];

                                self.emit_idiv(op1, op2, f_content, f_context, vm);

                                // mov rdx -> result
qinsoon's avatar
qinsoon committed
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
                                match res_tmp.ty.get_int_length() {
                                    Some(64) => {
                                        self.backend.emit_mov_r_r(&res_tmp, &x86_64::RDX);
                                    }
                                    Some(32) => {
                                        self.backend.emit_mov_r_r(&res_tmp, &x86_64::EDX);
                                    }
                                    Some(16) => {
                                        self.backend.emit_mov_r_r(&res_tmp, &x86_64::DX);
                                    }
                                    Some(8)  => {
                                        self.backend.emit_mov_r_r(&res_tmp, &x86_64::AH);
                                    }
                                    _ => unimplemented!()
                                }
qinsoon's avatar
qinsoon committed
733
                            },
qinsoon's avatar
qinsoon committed
734

qinsoon's avatar
shl    
qinsoon committed
735
736
737
738
                            op::BinOp::Shl => {
                                let op1 = &ops[op1];
                                let op2 = &ops[op2];

739
740
741
                                if self.match_mem(op1) {
                                    unimplemented!()
                                } else if self.match_ireg(op1) {
qinsoon's avatar
shl    
qinsoon committed
742
743
                                    let tmp_op1 = self.emit_ireg(op1, f_content, f_context, vm);

744
745
746
747
                                    if self.match_iimm(op2) {
                                        let imm_op2 = self.node_iimm_to_i32(op2) as i8;

                                        // shl op1, op2 -> op1
qinsoon's avatar
qinsoon committed
748
                                        self.backend.emit_shl_r_imm8(&tmp_op1, imm_op2);
749
750

                                        // mov op1 -> result
qinsoon's avatar
qinsoon committed
751
                                        self.backend.emit_mov_r_r(&res_tmp, &tmp_op1);
752
                                    } else if self.match_ireg(op2) {
qinsoon's avatar
shl    
qinsoon committed
753
754
                                        let tmp_op2 = self.emit_ireg(op2, f_content, f_context, vm);

qinsoon's avatar
qinsoon committed
755
756
                                        // mov op2 -> cl
                                        self.backend.emit_mov_r_r(&x86_64::CL, &tmp_op2);
qinsoon's avatar
shl    
qinsoon committed
757
758

                                        // shl op1, cl -> op1
qinsoon's avatar
qinsoon committed
759
                                        self.backend.emit_shl_r_cl(&tmp_op1);
qinsoon's avatar
shl    
qinsoon committed
760
761

                                        // mov op1 -> result
qinsoon's avatar
qinsoon committed
762
                                        self.backend.emit_mov_r_r(&res_tmp, &tmp_op1);
qinsoon's avatar
shl    
qinsoon committed
763
764
765
                                    } else {
                                        panic!("unexpected op2 (not ireg not iimm): {}", op2);
                                    }
766
767
                                } else {
                                    panic!("unexpected op1 (not ireg not mem): {}", op1);
qinsoon's avatar
shl    
qinsoon committed
768
                                }
qinsoon's avatar
qinsoon committed
769
770
771
772
773
                            },
                            op::BinOp::Lshr => {
                                let op1 = &ops[op1];
                                let op2 = &ops[op2];

774
775
776
                                if self.match_mem(op1) {
                                    unimplemented!()
                                } else if self.match_ireg(op1) {
qinsoon's avatar
qinsoon committed
777
778
                                    let tmp_op1 = self.emit_ireg(op1, f_content, f_context, vm);

779
780
781
782
                                    if self.match_iimm(op2) {
                                        let imm_op2 = self.node_iimm_to_i32(op2) as i8;

                                        // shr op1, op2 -> op1
qinsoon's avatar
qinsoon committed
783
                                        self.backend.emit_shr_r_imm8(&tmp_op1, imm_op2);
784
785

                                        // mov op1 -> result
qinsoon's avatar
qinsoon committed
786
                                        self.backend.emit_mov_r_r(&res_tmp, &tmp_op1);
787
                                    } else if self.match_ireg(op2) {
qinsoon's avatar
qinsoon committed
788
789
                                        let tmp_op2 = self.emit_ireg(op2, f_content, f_context, vm);

qinsoon's avatar
qinsoon committed
790
791
                                        // mov op2 -> cl
                                        self.backend.emit_mov_r_r(&x86_64::CL, &tmp_op2);
qinsoon's avatar
qinsoon committed
792
793

                                        // shr op1, cl -> op1
qinsoon's avatar
qinsoon committed
794
                                        self.backend.emit_shr_r_cl(&tmp_op1);
qinsoon's avatar
qinsoon committed
795
796

                                        // mov op1 -> result
qinsoon's avatar
qinsoon committed
797
                                        self.backend.emit_mov_r_r(&res_tmp, &tmp_op1);
qinsoon's avatar
qinsoon committed
798
799
800
                                    } else {
                                        panic!("unexpected op2 (not ireg not iimm): {}", op2);
                                    }
801
802
                                } else {
                                    panic!("unexpected op1 (not ireg not mem): {}", op1);
qinsoon's avatar
qinsoon committed
803
804
805
806
807
808
                                }
                            },
                            op::BinOp::Ashr => {
                                let op1 = &ops[op1];
                                let op2 = &ops[op2];

809
810
811
                                if self.match_mem(op1) {
                                    unimplemented!()
                                } else if self.match_ireg(op1) {
qinsoon's avatar
qinsoon committed
812
813
                                    let tmp_op1 = self.emit_ireg(op1, f_content, f_context, vm);

814
815
816
817
                                    if self.match_iimm(op2) {
                                        let imm_op2 = self.node_iimm_to_i32(op2) as i8;

                                        // sar op1, op2 -> op1
qinsoon's avatar
qinsoon committed
818
                                        self.backend.emit_sar_r_imm8(&tmp_op1, imm_op2);
819
820

                                        // mov op1 -> result
qinsoon's avatar
qinsoon committed
821
                                        self.backend.emit_mov_r_r(&res_tmp, &tmp_op1);
822
                                    } else if self.match_ireg(op2) {
qinsoon's avatar
qinsoon committed
823
824
                                        let tmp_op2 = self.emit_ireg(op2, f_content, f_context, vm);

qinsoon's avatar
qinsoon committed
825
826
                                        // mov op2 -> cl
                                        self.backend.emit_mov_r_r(&x86_64::CL, &tmp_op2);
qinsoon's avatar
qinsoon committed
827
828

                                        // sar op1, cl -> op1
qinsoon's avatar
qinsoon committed
829
                                        self.backend.emit_sar_r_cl(&tmp_op1);
qinsoon's avatar
qinsoon committed
830
831

                                        // mov op1 -> result
qinsoon's avatar
qinsoon committed
832
                                        self.backend.emit_mov_r_r(&res_tmp, &tmp_op1);
833
                                    } else  {
qinsoon's avatar
qinsoon committed
834
835
                                        panic!("unexpected op2 (not ireg not iimm): {}", op2);
                                    }
836
837
                                } else {
                                    panic!("unexpected op1 (not ireg not mem): {}", op1);
qinsoon's avatar
qinsoon committed
838
839
840
                                }
                            },

qinsoon's avatar
shl    
qinsoon committed
841

qinsoon's avatar
qinsoon committed
842
843
                            // floating point
                            op::BinOp::FAdd => {
844
                                if self.match_fpreg(&ops[op1]) && self.match_mem(&ops[op2]) {
qinsoon's avatar
qinsoon committed
845
846
847
                                    trace!("emit add-fpreg-mem");

                                    let reg_op1 = self.emit_fpreg(&ops[op1], f_content, f_context, vm);
qinsoon's avatar
shl    
qinsoon committed
848
                                    let mem_op2 = self.emit_mem(&ops[op2], vm);
qinsoon's avatar
qinsoon committed
849
850
851
852
853

                                    // mov op1, res
                                    self.backend.emit_movsd_f64_f64(&res_tmp, &reg_op1);
                                    // sub op2 res
                                    self.backend.emit_addsd_f64_mem64(&res_tmp, &mem_op2);
854
855
856
857
858
859
860
861
862
863
                                } else if self.match_fpreg(&ops[op1]) && self.match_fpreg(&ops[op2]) {
                                    trace!("emit add-fpreg-fpreg");

                                    let reg_op1 = self.emit_fpreg(&ops[op1], f_content, f_context, vm);
                                    let reg_op2 = self.emit_fpreg(&ops[op2], f_content, f_context, vm);

                                    // movsd op1, res
                                    self.backend.emit_movsd_f64_f64(&res_tmp, &reg_op1);
                                    // add op2 res
                                    self.backend.emit_addsd_f64_f64(&res_tmp, &reg_op2);
qinsoon's avatar
qinsoon committed
864
865
866
867
                                } else {
                                    unimplemented!()
                                }
                            }
868
869
                            
                            _ => unimplemented!()
870
871
                        }
                    }
qinsoon's avatar
qinsoon committed
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891

                    Instruction_::ConvOp{operation, ref from_ty, ref to_ty, operand} => {
                        let ops = inst.ops.read().unwrap();

                        let ref op = ops[operand];

                        let extract_int_len = |x: &P<MuType>| {
                            match x.v {
                                MuType_::Int(len) => len,
                                _ => panic!("only expect int types, found: {}", x)
                            }
                        };

                        match operation {
                            op::ConvOp::TRUNC => {
                                if self.match_ireg(op) {
                                    let tmp_op = self.emit_ireg(op, f_content, f_context, vm);
                                    let tmp_res = self.get_result_value(node);

                                    // mov op -> result
qinsoon's avatar
qinsoon committed
892
                                    self.backend.emit_mov_r_r(&tmp_res, &tmp_op);
qinsoon's avatar
qinsoon committed
893
894
895
896
897
                                } else {
                                    panic!("unexpected op (expect ireg): {}", op);
                                }
                            }
                            op::ConvOp::ZEXT => {
898
899
900
                                if self.match_ireg(op) {
                                    let tmp_op = self.emit_ireg(op, f_content, f_context, vm);
                                    let tmp_res = self.get_result_value(node);
qinsoon's avatar
qinsoon committed
901

902
903
904
                                    // movz op -> result
                                    let from_ty_size = vm.get_backend_type_info(from_ty.id()).size;
                                    let to_ty_size   = vm.get_backend_type_info(to_ty.id()).size;
qinsoon's avatar
qinsoon committed
905

906
                                    if from_ty_size != to_ty_size {
qinsoon's avatar
qinsoon committed
907
908
                                        self.backend.emit_movz_r_r(&tmp_res, &tmp_op);
                                    } else {
909
                                        self.backend.emit_mov_r_r(&tmp_res, &tmp_op);
qinsoon's avatar
qinsoon committed
910
                                    }
911
912
                                } else {
                                    panic!("unexpected op (expect ireg): {}", op);
qinsoon's avatar
qinsoon committed
913
914
915
                                }
                            },
                            op::ConvOp::SEXT => {
916
917
918
                                if self.match_ireg(op) {
                                    let tmp_op = self.emit_ireg(op, f_content, f_context, vm);
                                    let tmp_res = self.get_result_value(node);
qinsoon's avatar
qinsoon committed
919

920
921
922
                                    // movs op -> result
                                    let from_ty_size = vm.get_backend_type_info(from_ty.id()).size;
                                    let to_ty_size   = vm.get_backend_type_info(to_ty.id()).size;
qinsoon's avatar
qinsoon committed
923

924
                                    if from_ty_size != to_ty_size {
qinsoon's avatar
qinsoon committed
925
926
                                        self.backend.emit_movs_r_r(&tmp_res, &tmp_op);
                                    } else {
927
                                        self.backend.emit_mov_r_r(&tmp_res, &tmp_op);
qinsoon's avatar
qinsoon committed
928
                                    }
929
930
                                } else {
                                    panic!("unexpected op (expect ireg): {}", op)
qinsoon's avatar
qinsoon committed
931
932
                                }
                            }
qinsoon's avatar
qinsoon committed
933
934
935
936
937
938
                            op::ConvOp::REFCAST | op::ConvOp::PTRCAST => {
                                // just a mov (and hopefully reg alloc will coalesce it)
                                let tmp_res = self.get_result_value(node);

                                if self.match_ireg(op) {
                                    let tmp_op = self.emit_ireg(op, f_content, f_context, vm);
qinsoon's avatar
qinsoon committed
939
                                    self.backend.emit_mov_r_r(&tmp_res, &tmp_op);
qinsoon's avatar
qinsoon committed
940
941
942
943
                                } else {
                                    panic!("unexpected op (expect ireg): {}", op)
                                }
                            }
qinsoon's avatar
qinsoon committed
944
945
946
947

                            _ => unimplemented!()
                        }
                    }
948
                    
949
950
                    // load on x64 generates mov inst (no matter what order is specified)
                    // https://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html
951
                    Instruction_::Load{is_ptr, order, mem_loc} => {
qinsoon's avatar
qinsoon committed
952
                        let ops = inst.ops.read().unwrap();
953
                        let ref loc_op = ops[mem_loc];
954
955
956
957
958
959
                        
                        // check order
                        match order {
                            MemoryOrder::Relaxed 
                            | MemoryOrder::Consume 
                            | MemoryOrder::Acquire
960
961
                            | MemoryOrder::SeqCst
                            | MemoryOrder::NotAtomic => {},
962
963
                            _ => panic!("didnt expect order {:?} with store inst", order)
                        }                        
964

965
                        let resolved_loc = self.emit_node_addr_to_value(loc_op, f_content, f_context, vm);
qinsoon's avatar
qinsoon committed
966
                        let res_temp = self.get_result_value(node);
967
968
969
                        
                        if self.match_ireg(node) {
                            // emit mov(GPR)
qinsoon's avatar
qinsoon committed
970
                            self.backend.emit_mov_r_mem(&res_temp, &resolved_loc);
971
972
973
974
975
976
977
                        } else {
                            // emit mov(FPR)
                            unimplemented!()
                        }
                    }
                    
                    Instruction_::Store{is_ptr, order, mem_loc, value} => {
qinsoon's avatar
qinsoon committed
978
                        let ops = inst.ops.read().unwrap();
979
980
981
982
983
                        let ref loc_op = ops[mem_loc];
                        let ref val_op = ops[value];
                        
                        let generate_plain_mov : bool = {
                            match order {
984
985
986
                                MemoryOrder::Relaxed
                                | MemoryOrder::Release
                                | MemoryOrder::NotAtomic => true,
987
988
989
990
991
                                MemoryOrder::SeqCst => false,
                                _ => panic!("didnt expect order {:?} with store inst", order)
                            }
                        };
                        
992
993
994
995
                        let resolved_loc = self.emit_node_addr_to_value(loc_op, f_content, f_context, vm);

                        if self.match_iimm(val_op) {
                            let val = self.node_iimm_to_i32(val_op);
996
                            if generate_plain_mov {
qinsoon's avatar
qinsoon committed
997
                                self.backend.emit_mov_mem_imm(&resolved_loc, val);
998
999
1000
                            } else {
                                unimplemented!()
                            }
1001
1002
                        } else if self.match_ireg(val_op) {
                            let val = self.emit_ireg(val_op, f_content, f_context, vm);
1003
                            if generate_plain_mov {
qinsoon's avatar
qinsoon committed
1004
                                self.backend.emit_mov_mem_r(&resolved_loc, &val);
1005
1006
1007
                            } else {
                                unimplemented!()
                            }
1008
1009
1010
1011
1012
                        } else {
                            // emit mov(FPR)
                            unimplemented!()
                        }
                    }
qinsoon's avatar
qinsoon committed
1013

1014
1015
1016
1017
1018
1019
1020
                    // memory insts: calculate the address, then lea
                    Instruction_::GetIRef(_)
                    | Instruction_::GetFieldIRef{..}
                    | Instruction_::GetVarPartIRef{..}
                    | Instruction_::ShiftIRef{..} => {
                        let mem_addr = self.emit_get_mem_from_inst(node, f_content, f_context, vm);
                        let tmp_res  = self.get_result_value(node);
qinsoon's avatar
qinsoon committed
1021

1022
                        self.backend.emit_lea_r64(&tmp_res, &mem_addr);