WARNING! Access to this system is limited to authorised users only.
Unauthorised users may be subject to prosecution.
Unauthorised access to this system is a criminal offence under Australian law (Federal Crimes Act 1914 Part VIA)
It is a criminal offence to:
(1) Obtain access to data without authority. -Penalty 2 years imprisonment.
(2) Damage, delete, alter or insert data without authority. -Penalty 10 years imprisonment.
User activity is monitored and recorded. Anyone using this system expressly consents to such monitoring and recording.

To protect your data, the CISO officer has suggested users to enable 2FA as soon as possible.
Currently 2.7% of users enabled 2FA.

inst_sel.rs 118 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
96
                        if self.match_cmp_res(cond) {
                            trace!("emit cmp_eq-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
585
                                // mov op1 -> rax
                                let rax = x86_64::RAX.clone();
                                let op1 = &ops[op1];
586
                                if self.match_iimm(op1) {
587
                                    let imm_op1 = self.node_iimm_to_i32(op1);
588
                                    
qinsoon's avatar
qinsoon committed
589
                                    self.backend.emit_mov_r_imm(&rax, imm_op1);
590
                                } else if self.match_mem(op1) {
qinsoon's avatar
shl    
qinsoon committed
591
                                    let mem_op1 = self.emit_mem(op1, vm);
592
                                    
qinsoon's avatar
qinsoon committed
593
                                    self.backend.emit_mov_r_mem(&rax, &mem_op1);
594
595
596
                                } else if self.match_ireg(op1) {
                                    let reg_op1 = self.emit_ireg(op1, f_content, f_context, vm);

qinsoon's avatar
qinsoon committed
597
                                    self.backend.emit_mov_r_r(&rax, &reg_op1);
598
599
600
601
602
603
                                } else {
                                    unimplemented!();
                                }
                                
                                // mul op2 -> rax
                                let op2 = &ops[op2];
604
                                if self.match_iimm(op2) {
605
                                    let imm_op2 = self.node_iimm_to_i32(op2);
606
607
608
                                    
                                    // put imm in a temporary
                                    // here we use result reg as temporary
qinsoon's avatar
qinsoon committed
609
                                    self.backend.emit_mov_r_imm(&res_tmp, imm_op2);
610
                                    
qinsoon's avatar
qinsoon committed
611
                                    self.backend.emit_mul_r(&res_tmp);
612
                                } else if self.match_mem(op2) {
qinsoon's avatar
shl    
qinsoon committed
613
                                    let mem_op2 = self.emit_mem(op2, vm);
614
                                    
qinsoon's avatar
qinsoon committed
615
                                    self.backend.emit_mul_mem(&mem_op2);
616
617
618
                                } else if self.match_ireg(op2) {
                                    let reg_op2 = self.emit_ireg(op2, f_content, f_context, vm);

qinsoon's avatar
qinsoon committed
619
                                    self.backend.emit_mul_r(&reg_op2);
620
621
622
623
624
                                } else {
                                    unimplemented!();
                                }
                                
                                // mov rax -> result
qinsoon's avatar
qinsoon committed
625
                                self.backend.emit_mov_r_r(&res_tmp, &rax);
626
                            },
qinsoon's avatar
qinsoon committed
627
628
629
630
                            op::BinOp::Udiv => {
                                let op1 = &ops[op1];
                                let op2 = &ops[op2];

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

qinsoon's avatar
sdiv    
qinsoon committed
633
                                // mov rax -> result
qinsoon's avatar
qinsoon committed
634
                                self.backend.emit_mov_r_r(&res_tmp, &x86_64::RAX);
qinsoon's avatar
sdiv    
qinsoon committed
635
636
637
638
                            },
                            op::BinOp::Sdiv => {
                                let op1 = &ops[op1];
                                let op2 = &ops[op2];
qinsoon's avatar
qinsoon committed
639

qinsoon's avatar
sdiv    
qinsoon committed
640
                                self.emit_idiv(op1, op2, f_content, f_context, vm);
qinsoon's avatar
qinsoon committed
641
642

                                // mov rax -> result
qinsoon's avatar
qinsoon committed
643
                                self.backend.emit_mov_r_r(&res_tmp, &x86_64::RAX);
qinsoon's avatar
sdiv    
qinsoon committed
644
                            },
qinsoon's avatar
qinsoon committed
645
646
647
648
649
650
651
                            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
652
                                self.backend.emit_mov_r_r(&res_tmp, &x86_64::RDX);
qinsoon's avatar
qinsoon committed
653
654
655
656
657
658
659
660
                            },
                            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
661
                                self.backend.emit_mov_r_r(&res_tmp, &x86_64::RDX);
qinsoon's avatar
qinsoon committed
662
                            },
qinsoon's avatar
qinsoon committed
663

qinsoon's avatar
shl    
qinsoon committed
664
665
666
667
                            op::BinOp::Shl => {
                                let op1 = &ops[op1];
                                let op2 = &ops[op2];

668
669
670
                                if self.match_mem(op1) {
                                    unimplemented!()
                                } else if self.match_ireg(op1) {
qinsoon's avatar
shl    
qinsoon committed
671
672
                                    let tmp_op1 = self.emit_ireg(op1, f_content, f_context, vm);

673
674
675
676
                                    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
677
                                        self.backend.emit_shl_r_imm8(&tmp_op1, imm_op2);
678
679

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

qinsoon's avatar
qinsoon committed
684
685
                                        // mov op2 -> cl
                                        self.backend.emit_mov_r_r(&x86_64::CL, &tmp_op2);
qinsoon's avatar
shl    
qinsoon committed
686
687

                                        // shl op1, cl -> op1
qinsoon's avatar
qinsoon committed
688
                                        self.backend.emit_shl_r_cl(&tmp_op1);
qinsoon's avatar
shl    
qinsoon committed
689
690

                                        // mov op1 -> result
qinsoon's avatar
qinsoon committed
691
                                        self.backend.emit_mov_r_r(&res_tmp, &tmp_op1);
qinsoon's avatar
shl    
qinsoon committed
692
693
694
                                    } else {
                                        panic!("unexpected op2 (not ireg not iimm): {}", op2);
                                    }
695
696
                                } else {
                                    panic!("unexpected op1 (not ireg not mem): {}", op1);
qinsoon's avatar
shl    
qinsoon committed
697
                                }
qinsoon's avatar
qinsoon committed
698
699
700
701
702
                            },
                            op::BinOp::Lshr => {
                                let op1 = &ops[op1];
                                let op2 = &ops[op2];

703
704
705
                                if self.match_mem(op1) {
                                    unimplemented!()
                                } else if self.match_ireg(op1) {
qinsoon's avatar
qinsoon committed
706
707
                                    let tmp_op1 = self.emit_ireg(op1, f_content, f_context, vm);

708
709
710
711
                                    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
712
                                        self.backend.emit_shr_r_imm8(&tmp_op1, imm_op2);
713
714

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

qinsoon's avatar
qinsoon committed
719
720
                                        // mov op2 -> cl
                                        self.backend.emit_mov_r_r(&x86_64::CL, &tmp_op2);
qinsoon's avatar
qinsoon committed
721
722

                                        // shr op1, cl -> op1
qinsoon's avatar
qinsoon committed
723
                                        self.backend.emit_shr_r_cl(&tmp_op1);
qinsoon's avatar
qinsoon committed
724
725

                                        // mov op1 -> result
qinsoon's avatar
qinsoon committed
726
                                        self.backend.emit_mov_r_r(&res_tmp, &tmp_op1);
qinsoon's avatar
qinsoon committed
727
728
729
                                    } else {
                                        panic!("unexpected op2 (not ireg not iimm): {}", op2);
                                    }
730
731
                                } else {
                                    panic!("unexpected op1 (not ireg not mem): {}", op1);
qinsoon's avatar
qinsoon committed
732
733
734
735
736
737
                                }
                            },
                            op::BinOp::Ashr => {
                                let op1 = &ops[op1];
                                let op2 = &ops[op2];

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

743
744
745
746
                                    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
747
                                        self.backend.emit_sar_r_imm8(&tmp_op1, imm_op2);
748
749

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

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

                                        // sar op1, cl -> op1
qinsoon's avatar
qinsoon committed
758
                                        self.backend.emit_sar_r_cl(&tmp_op1);
qinsoon's avatar
qinsoon committed
759
760

                                        // mov op1 -> result
qinsoon's avatar
qinsoon committed
761
                                        self.backend.emit_mov_r_r(&res_tmp, &tmp_op1);
762
                                    } else  {
qinsoon's avatar
qinsoon committed
763
764
                                        panic!("unexpected op2 (not ireg not iimm): {}", op2);
                                    }
765
766
                                } else {
                                    panic!("unexpected op1 (not ireg not mem): {}", op1);
qinsoon's avatar
qinsoon committed
767
768
769
                                }
                            },

qinsoon's avatar
shl    
qinsoon committed
770

qinsoon's avatar
qinsoon committed
771
772
                            // floating point
                            op::BinOp::FAdd => {
773
                                if self.match_fpreg(&ops[op1]) && self.match_mem(&ops[op2]) {
qinsoon's avatar
qinsoon committed
774
775
776
                                    trace!("emit add-fpreg-mem");

                                    let reg_op1 = self.emit_fpreg(&ops[op1], f_content, f_context, vm);
qinsoon's avatar
shl    
qinsoon committed
777
                                    let mem_op2 = self.emit_mem(&ops[op2], vm);
qinsoon's avatar
qinsoon committed
778
779
780
781
782

                                    // 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);
783
784
785
786
787
788
789
790
791
792
                                } 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
793
794
795
796
                                } else {
                                    unimplemented!()
                                }
                            }
797
798
                            
                            _ => unimplemented!()
799
800
                        }
                    }
qinsoon's avatar
qinsoon committed
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823

                    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 => {
                                // currently only use 64bits register
                                // so only keep what is needed in the register (set others to 0)

                                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
824
                                    self.backend.emit_mov_r_r(&tmp_res, &tmp_op);
qinsoon's avatar
qinsoon committed
825
826
827
828
829
830
831
832
833
834
835
836
837
838
                                } else {
                                    panic!("unexpected op (expect ireg): {}", op);
                                }
                            }
                            op::ConvOp::ZEXT => {
                                // currently only use 64bits register
                                // so set irrelevant bits to 0
                                let from_ty_len = extract_int_len(from_ty);
                                let to_ty_len   = extract_int_len(to_ty);

                                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
839
840
                                    // movz op -> result
                                    self.backend.emit_movz_r_r(&tmp_res, &tmp_op);
qinsoon's avatar
qinsoon committed
841
842
843
844
845
846
847
848
849
850
851
                                } else {
                                    panic!("unexpected op (expect ireg): {}", op);
                                }
                            },
                            op::ConvOp::SEXT => {
                                // currently only use 64bits register
                                // we left shift the value, then arithmetic right shift back
                                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
852
853
                                    // movs op -> result
                                    self.backend.emit_movs_r_r(&tmp_res, &tmp_op);
qinsoon's avatar
qinsoon committed
854
855
856
857
                                } else {
                                    panic!("unexpected op (expect ireg): {}", op)
                                }
                            }
qinsoon's avatar
qinsoon committed
858
859
860
861
862
863
                            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
864
                                    self.backend.emit_mov_r_r(&tmp_res, &tmp_op);
qinsoon's avatar
qinsoon committed
865
866
867
868
                                } else {
                                    panic!("unexpected op (expect ireg): {}", op)
                                }
                            }
qinsoon's avatar
qinsoon committed
869
870
871
872

                            _ => unimplemented!()
                        }
                    }
873
                    
874
875
                    // load on x64 generates mov inst (no matter what order is specified)
                    // https://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html
876
                    Instruction_::Load{is_ptr, order, mem_loc} => {
qinsoon's avatar
qinsoon committed
877
                        let ops = inst.ops.read().unwrap();
878
                        let ref loc_op = ops[mem_loc];
879
880
881
882
883
884
                        
                        // check order
                        match order {
                            MemoryOrder::Relaxed 
                            | MemoryOrder::Consume 
                            | MemoryOrder::Acquire
885
886
                            | MemoryOrder::SeqCst
                            | MemoryOrder::NotAtomic => {},
887
888
                            _ => panic!("didnt expect order {:?} with store inst", order)
                        }                        
889

890
                        let resolved_loc = self.emit_node_addr_to_value(loc_op, f_content, f_context, vm);
qinsoon's avatar
qinsoon committed
891
                        let res_temp = self.get_result_value(node);
892
893
894
                        
                        if self.match_ireg(node) {
                            // emit mov(GPR)
qinsoon's avatar
qinsoon committed
895
                            self.backend.emit_mov_r_mem(&res_temp, &resolved_loc);
896
897
898
899
900
901
902
                        } else {
                            // emit mov(FPR)
                            unimplemented!()
                        }
                    }
                    
                    Instruction_::Store{is_ptr, order, mem_loc, value} => {
qinsoon's avatar
qinsoon committed
903
                        let ops = inst.ops.read().unwrap();
904
905
906
907
908
                        let ref loc_op = ops[mem_loc];
                        let ref val_op = ops[value];
                        
                        let generate_plain_mov : bool = {
                            match order {
909
910
911
                                MemoryOrder::Relaxed
                                | MemoryOrder::Release
                                | MemoryOrder::NotAtomic => true,
912
913
914
915
916
                                MemoryOrder::SeqCst => false,
                                _ => panic!("didnt expect order {:?} with store inst", order)
                            }
                        };
                        
917
918
919
920
                        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);
921
                            if generate_plain_mov {
qinsoon's avatar
qinsoon committed
922
                                self.backend.emit_mov_mem_imm(&resolved_loc, val);
923
924
925
                            } else {
                                unimplemented!()
                            }
926
927
                        } else if self.match_ireg(val_op) {
                            let val = self.emit_ireg(val_op, f_content, f_context, vm);
928
                            if generate_plain_mov {
qinsoon's avatar
qinsoon committed
929
                                self.backend.emit_mov_mem_r(&resolved_loc, &val);
930
931
932
                            } else {
                                unimplemented!()
                            }
933
934
935
936
937
                        } else {
                            // emit mov(FPR)
                            unimplemented!()
                        }
                    }
qinsoon's avatar
qinsoon committed
938

939
940
941
942
943
944
945
                    // 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
946

947
                        self.backend.emit_lea_r64(&tmp_res, &mem_addr);
qinsoon's avatar
qinsoon committed
948
                    }
949
                    
950
                    Instruction_::ThreadExit => {
951
                        // emit a call to swap_back_to_native_stack(sp_loc: Address)
952
953
                        
                        // get thread local and add offset to get sp_loc
qinsoon's avatar
qinsoon committed
954
                        let tl = self.emit_get_threadlocal(Some(node), f_content, f_context, vm);
qinsoon's avatar
qinsoon committed
955
                        self.backend.emit_add_r_imm(&tl, *thread::NATIVE_SP_LOC_OFFSET as i32);
956
                        
qinsoon's avatar
qinsoon committed
957
                        self.emit_runtime_entry(&entrypoints::SWAP_BACK_TO_NATIVE_STACK, vec![tl.clone()], None, Some(node), f_content, f_context, vm);
958
                    }
qinsoon's avatar
qinsoon committed
959
960
                    
                    Instruction_::New(ref ty) => {
qinsoon's avatar
qinsoon committed
961
962
963
964
965
966
967
                        if cfg!(debug_assertions) {
                            match ty.v {
                                MuType_::Hybrid(_) => panic!("cannot use NEW for hybrid, use NEWHYBRID instead"),
                                _ => {}
                            }
                        }

qinsoon's avatar
qinsoon committed
968
                        let ty_info = vm.get_backend_type_info(ty.id());
qinsoon's avatar
qinsoon committed
969
                        let size = ty_info.size;
970
                        let ty_align= ty_info.alignment;
qinsoon's avatar
qinsoon committed
971
972

                        let const_size = self.make_value_int_const(size as u64, vm);
qinsoon's avatar
qinsoon committed
973
                        
qinsoon's avatar
qinsoon committed
974
975
976
977
978
979
980
981
982
                        self.emit_alloc_sequence(const_size, ty_align, node, f_content, f_context, vm);
                    }

                    Instruction_::NewHybrid(ref ty, var_len) => {
                        if cfg!(debug_assertions) {
                            match ty.v {
                                MuType_::Hybrid(_) => {},
                                _ => panic!("NEWHYBRID is only for allocating hybrid types, use NEW for others")
                            }
qinsoon's avatar
qinsoon committed
983
                        }
qinsoon's avatar
qinsoon committed
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032

                        let ty_info = vm.get_backend_type_info(ty.id());
                        let ty_align = ty_info.alignment;
                        let fix_part_size = ty_info.size;
                        let var_ty_size = match ty.v {
                            MuType_::Hybrid(ref name) => {
                                let map_lock = HYBRID_TAG_MAP.read().unwrap();
                                let hybrid_ty_ = map_lock.get(name).unwrap();
                                let var_ty = hybrid_ty_.get_var_ty();

                                vm.get_backend_type_info(var_ty.id()).size
                            },
                            _ => panic!("only expect HYBRID type here")
                        };

                        // actual size = fix_part_size + var_ty_size * len
                        let actual_size = {
                            let ops = inst.ops.read().unwrap();
                            let ref var_len = ops[var_len];

                            if self.match_iimm(var_len) {
                                let var_len = self.node_iimm_to_i32(var_len);
                                let actual_size = fix_part_size + var_ty_size * (var_len as usize);

                                self.make_value_int_const(actual_size as u64, vm)
                            } else {
                                let tmp_actual_size = self.make_temporary(f_context, UINT64_TYPE.clone(), vm);
                                let tmp_var_len = self.emit_ireg(var_len, f_content, f_context, vm);

                                let is_power_of_two = |x: usize| {
                                    use std::i8;

                                    let mut power_of_two = 1;
                                    let mut i: i8 = 0;
                                    while power_of_two < x && i < i8::MAX {
                                        power_of_two *= 2;
                                        i += 1;