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

inst_sel.rs 162 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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
lazy_static! {
    pub static ref LONG_4_TYPE : P<MuType> = P(
        MuType::new(new_internal_id(), MuType_::mustruct(Mu("long_4"), vec![UINT32_TYPE.clone(); 4]))
    );

    pub static ref UITOFP_C0 : P<Value> = P(Value{
        hdr: MuEntityHeader::named(new_internal_id(), Mu("UITOFP_C0")),
        ty : LONG_4_TYPE.clone(),
        v  : Value_::Constant(Constant::List(vec![
            P(Value{
                hdr: MuEntityHeader::unnamed(new_internal_id()),
                ty: UINT32_TYPE.clone(),
                v : Value_::Constant(Constant::Int(1127219200u64))
            }),
            P(Value{
                hdr: MuEntityHeader::unnamed(new_internal_id()),
                ty: UINT32_TYPE.clone(),
                v : Value_::Constant(Constant::Int(1160773632u64))
            }),
            P(Value{
                hdr: MuEntityHeader::unnamed(new_internal_id()),
                ty: UINT32_TYPE.clone(),
                v : Value_::Constant(Constant::Int(0u64))
            }),
            P(Value{
                hdr: MuEntityHeader::unnamed(new_internal_id()),
                ty: UINT32_TYPE.clone(),
                v : Value_::Constant(Constant::Int(0u64))
            })
        ]))
    });

    pub static ref QUAD_2_TYPE : P<MuType> = P(
        MuType::new(new_internal_id(), MuType_::mustruct(Mu("quad_2"), vec![UINT64_TYPE.clone(); 2]))
    );

    pub static ref UITOFP_C1 : P<Value> = P(Value{
        hdr: MuEntityHeader::named(new_internal_id(), Mu("UITOFP_C1")),
        ty : QUAD_2_TYPE.clone(),
        v  : Value_::Constant(Constant::List(vec![
            P(Value{
                hdr: MuEntityHeader::unnamed(new_internal_id()),
                ty: UINT64_TYPE.clone(),
                v : Value_::Constant(Constant::Int(4841369599423283200u64))
            }),
            P(Value{
                hdr: MuEntityHeader::unnamed(new_internal_id()),
                ty: UINT64_TYPE.clone(),
                v : Value_::Constant(Constant::Int(4985484787499139072u64))
            })
        ]))
    });
79
80
81
82
83
84

    pub static ref FPTOUI_C : P<Value> = P(Value{
        hdr: MuEntityHeader::named(new_internal_id(), Mu("FPTOUI_C")),
        ty : UINT64_TYPE.clone(),
        v  : Value_::Constant(Constant::Int(4890909195324358656u64))
    });
85
86
}

87
pub struct InstructionSelection {
88
    name: &'static str,
89
90
    backend: Box<CodeGenerator>,
    
qinsoon's avatar
qinsoon committed
91
    current_callsite_id: usize,
qinsoon's avatar
qinsoon committed
92
93
    current_frame: Option<Frame>,
    current_block: Option<MuName>,
qinsoon's avatar
qinsoon committed
94
95
96
97
    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
98
99
100
101
    current_exn_blocks: HashMap<MuID, ValueLocation>,

    current_constants: HashMap<MuID, P<Value>>,
    current_constants_locs: HashMap<MuID, P<Value>>
102
103
}

104
impl <'a> InstructionSelection {
qinsoon's avatar
qinsoon committed
105
    #[cfg(feature = "aot")]
106
    pub fn new() -> InstructionSelection {
107
108
        InstructionSelection{
            name: "Instruction Selection (x64)",
109
            backend: Box::new(ASMCodeGen::new()),
qinsoon's avatar
qinsoon committed
110
            
qinsoon's avatar
qinsoon committed
111
            current_callsite_id: 0,
qinsoon's avatar
qinsoon committed
112
113
114
            current_frame: None,
            current_block: None,
            current_func_start: None,
qinsoon's avatar
qinsoon committed
115
116
            // key: block id, val: callsite that names the block as exception block
            current_exn_callsites: HashMap::new(), 
117
118
119
120
            current_exn_blocks: HashMap::new(),

            current_constants: HashMap::new(),
            current_constants_locs: HashMap::new()
121
122
        }
    }
qinsoon's avatar
qinsoon committed
123
124
125
126
127

    #[cfg(feature = "jit")]
    pub fn new() -> InstructionSelection {
        unimplemented!()
    }
128
129
130
131
132
133
134
    
    // 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)]
135
    fn instruction_select(&mut self, node: &'a TreeNode, f_content: &FunctionContent, f_context: &mut FunctionContext, vm: &VM) {
qinsoon's avatar
qinsoon committed
136
137
138
        trace!("instsel on node {}", node);
        
        match node.v {
139
140
            TreeNode_::Instruction(ref inst) => {
                match inst.v {
qinsoon's avatar
qinsoon committed
141
                    Instruction_::Branch2{cond, ref true_dest, ref false_dest, true_prob} => {
qinsoon's avatar
qinsoon committed
142
                        trace!("instsel on BRANCH2");
143
144
                        // '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)
145
146
147
148
149
                        let (fallthrough_dest, branch_dest, branch_if_true) = {
                            if true_prob > 0.5f32 {
                                (true_dest, false_dest, false)
                            } else {
                                (false_dest, true_dest, true)
150
                            }
151
                        };
152
                        
qinsoon's avatar
qinsoon committed
153
                        let ops = inst.ops.read().unwrap();
154
                        
155
156
                        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
157
                        
158
                        let branch_target = f_content.get_block(branch_dest.target).name().unwrap();
159
160
161
    
                        let ref cond = ops[cond];
                        
qinsoon's avatar
qinsoon committed
162
                        if self.match_cmp_res(cond) {
163
                            trace!("emit cmp_res-branch2");
164
                            match self.emit_cmp_res(cond, f_content, f_context, vm) {
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
                                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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280

                                // floating point

                                op::CmpOp::FOEQ | op::CmpOp::FUEQ => {
                                    if branch_if_true {
                                        self.backend.emit_je(branch_target);
                                    } else {
                                        self.backend.emit_jne(branch_target);
                                    }
                                },
                                op::CmpOp::FONE | op::CmpOp::FUNE => {
                                    if branch_if_true {
                                        self.backend.emit_jne(branch_target);
                                    } else {
                                        self.backend.emit_je(branch_target);
                                    }
                                },
                                op::CmpOp::FOGT | op::CmpOp::FUGT => {
                                    if branch_if_true {
                                        self.backend.emit_ja(branch_target);
                                    } else {
                                        self.backend.emit_jbe(branch_target);
                                    }
                                },
                                op::CmpOp::FOGE | op::CmpOp::FUGE => {
                                    if branch_if_true {
                                        self.backend.emit_jae(branch_target);
                                    } else {
                                        self.backend.emit_jb(branch_target);
                                    }
                                },
                                op::CmpOp::FOLT | op::CmpOp::FULT => {
                                    if branch_if_true {
                                        self.backend.emit_jb(branch_target);
                                    } else {
                                        self.backend.emit_jae(branch_target);
                                    }
                                },
                                op::CmpOp::FOLE | op::CmpOp::FULE => {
                                    if branch_if_true {
                                        self.backend.emit_jbe(branch_target);
                                    } else {
                                        self.backend.emit_ja(branch_target);
                                    }
                                },

qinsoon's avatar
qinsoon committed
281
282
283
284
                                _ => unimplemented!()
                            }
                        } else if self.match_ireg(cond) {
                            trace!("emit ireg-branch2");
285
                            
286
                            let cond_reg = self.emit_ireg(cond, f_content, f_context, vm);
287
                            
qinsoon's avatar
qinsoon committed
288
                            // emit: cmp cond_reg 1
qinsoon's avatar
qinsoon committed
289
                            self.backend.emit_cmp_imm_r(1, &cond_reg);
qinsoon's avatar
qinsoon committed
290
                            // emit: je #branch_dest
291
                            self.backend.emit_je(branch_target);
qinsoon's avatar
qinsoon committed
292
293
                        } else {
                            unimplemented!();
294
                        }
295
                    },
qinsoon's avatar
qinsoon committed
296
297

                    Instruction_::Select{cond, true_val, false_val} => {
qinsoon's avatar
qinsoon committed
298
299
                        use ast::op::CmpOp::*;

qinsoon's avatar
qinsoon committed
300
                        trace!("instsel on SELECT");
qinsoon's avatar
qinsoon committed
301
302
303
304
305
306
307
308
309
                        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);
qinsoon's avatar
qinsoon committed
310

qinsoon's avatar
qinsoon committed
311
312
313
                            // generate compare
                            let cmpop = if self.match_cmp_res(cond) {
                                self.emit_cmp_res(cond, f_content, f_context, vm)
qinsoon's avatar
qinsoon committed
314
315
316
                            } else if self.match_ireg(cond) {
                                let tmp_cond = self.emit_ireg(cond, f_content, f_context, vm);
                                // emit: cmp cond_reg 1
qinsoon's avatar
qinsoon committed
317
                                self.backend.emit_cmp_imm_r(1, &tmp_cond);
qinsoon's avatar
qinsoon committed
318

qinsoon's avatar
qinsoon committed
319
                                EQ
qinsoon's avatar
qinsoon committed
320
                            } else {
qinsoon's avatar
qinsoon committed
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
                                panic!("expected ireg, found {}", cond)
                            };

                            // use cmov for 16/32/64bit integeer
                            // use jcc  for 8 bit
                            match tmp_res.ty.get_int_length() {
                                // cmov
                                Some(len) if len > 8 => {
                                    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);

                                    // mov tmp_false -> tmp_res
                                    self.backend.emit_mov_r_r(&tmp_res, &tmp_false);

                                    match cmpop {
                                        EQ  => self.backend.emit_cmove_r_r (&tmp_res, &tmp_true),
                                        NE  => self.backend.emit_cmovne_r_r(&tmp_res, &tmp_true),
                                        SGE => self.backend.emit_cmovge_r_r(&tmp_res, &tmp_true),
                                        SGT => self.backend.emit_cmovg_r_r (&tmp_res, &tmp_true),
                                        SLE => self.backend.emit_cmovle_r_r(&tmp_res, &tmp_true),
                                        SLT => self.backend.emit_cmovl_r_r (&tmp_res, &tmp_true),
                                        UGE => self.backend.emit_cmovae_r_r(&tmp_res, &tmp_true),
                                        UGT => self.backend.emit_cmova_r_r (&tmp_res, &tmp_true),
                                        ULE => self.backend.emit_cmovbe_r_r(&tmp_res, &tmp_true),
                                        ULT => self.backend.emit_cmovb_r_r (&tmp_res, &tmp_true),

                                        FOEQ | FUEQ => self.backend.emit_cmove_r_r (&tmp_res, &tmp_true),
                                        FONE | FUNE => self.backend.emit_cmovne_r_r(&tmp_res, &tmp_true),
                                        FOGT | FUGT => self.backend.emit_cmova_r_r (&tmp_res, &tmp_true),
                                        FOGE | FUGE => self.backend.emit_cmovae_r_r(&tmp_res, &tmp_true),
                                        FOLT | FULT => self.backend.emit_cmovb_r_r (&tmp_res, &tmp_true),
                                        FOLE | FULE => self.backend.emit_cmovbe_r_r(&tmp_res, &tmp_true),

                                        _ => unimplemented!()
                                    }
                                }
                                // jcc
                                _ => {
                                    let blk_true = format!("{}_select_true", node.id());
                                    let blk_end   = format!("{}_select_end", node.id());

                                    // jump to blk_true if true
                                    match cmpop {
                                        EQ  => self.backend.emit_je (blk_true.clone()),
                                        NE  => self.backend.emit_jne(blk_true.clone()),
                                        SGE => self.backend.emit_jge(blk_true.clone()),
                                        SGT => self.backend.emit_jg (blk_true.clone()),
                                        SLE => self.backend.emit_jle(blk_true.clone()),
                                        SLT => self.backend.emit_jl (blk_true.clone()),
                                        UGE => self.backend.emit_jae(blk_true.clone()),
                                        UGT => self.backend.emit_ja (blk_true.clone()),
                                        ULE => self.backend.emit_jbe(blk_true.clone()),
                                        ULT => self.backend.emit_jb (blk_true.clone()),

                                        FOEQ | FUEQ => self.backend.emit_je (blk_true.clone()),
                                        FONE | FUNE => self.backend.emit_jne(blk_true.clone()),
                                        FOGT | FUGT => self.backend.emit_ja (blk_true.clone()),
                                        FOGE | FUGE => self.backend.emit_jae(blk_true.clone()),
                                        FOLT | FULT => self.backend.emit_jb (blk_true.clone()),
                                        FOLE | FULE => self.backend.emit_jbe(blk_true.clone()),

                                        _ => unimplemented!()
                                    }

                                    // mov false result here
                                    self.emit_move_node_to_value(&tmp_res, &false_val, f_content, f_context, vm);

                                    // jmp to end
                                    self.backend.emit_jmp(blk_end.clone());

                                    // finishing current block
                                    let cur_block = self.current_block.as_ref().unwrap().clone();
                                    self.backend.end_block(cur_block.clone());

                                    // blk_true:
                                    self.current_block = Some(blk_true.clone());
                                    self.backend.start_block(blk_true.clone());
                                    // mov true value -> result
                                    self.emit_move_node_to_value(&tmp_res, &true_val, f_content, f_context, vm);

                                    self.backend.end_block(blk_true.clone());

                                    // blk_end:
                                    self.backend.start_block(blk_end.clone());
                                    self.current_block = Some(blk_end.clone());
                                }
qinsoon's avatar
qinsoon committed
407
408
409
410
411
412
413
                            }
                        } else {
                            // moving vectors, floatingpoints
                            unimplemented!()
                        }
                    },

414
                    Instruction_::CmpOp(op, op1, op2) => {
qinsoon's avatar
qinsoon committed
415
416
                        use ast::op::CmpOp::*;

qinsoon's avatar
qinsoon committed
417
                        trace!("instsel on CMPOP");
418
419
420
421
                        let ops = inst.ops.read().unwrap();
                        let ref op1 = ops[op1];
                        let ref op2 = ops[op2];

qinsoon's avatar
qinsoon committed
422
                        let tmp_res = self.get_result_value(node);
qinsoon's avatar
qinsoon committed
423

qinsoon's avatar
qinsoon committed
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
                        // cmov only take (16/32/64bits registers)
                        // 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);

                        // 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);

                        // cmov 1 to result
                        match self.emit_cmp_res(node, f_content, f_context, vm) {
                            EQ  => self.backend.emit_cmove_r_r (&tmp_res64, &tmp_1),
                            NE  => self.backend.emit_cmovne_r_r(&tmp_res64, &tmp_1),
                            SGE => self.backend.emit_cmovge_r_r(&tmp_res64, &tmp_1),
                            SGT => self.backend.emit_cmovg_r_r (&tmp_res64, &tmp_1),
                            SLE => self.backend.emit_cmovle_r_r(&tmp_res64, &tmp_1),
                            SLT => self.backend.emit_cmovl_r_r (&tmp_res64, &tmp_1),
                            UGE => self.backend.emit_cmovae_r_r(&tmp_res64, &tmp_1),
                            UGT => self.backend.emit_cmova_r_r (&tmp_res64, &tmp_1),
                            ULE => self.backend.emit_cmovbe_r_r(&tmp_res64, &tmp_1),
                            ULT => self.backend.emit_cmovb_r_r (&tmp_res64, &tmp_1),

                            FOEQ | FUEQ => self.backend.emit_cmove_r_r (&tmp_res64, &tmp_1),
                            FONE | FUNE => self.backend.emit_cmovne_r_r(&tmp_res64, &tmp_1),
                            FOGT | FUGT => self.backend.emit_cmova_r_r (&tmp_res64, &tmp_1),
                            FOGE | FUGE => self.backend.emit_cmovae_r_r(&tmp_res64, &tmp_1),
                            FOLT | FULT => self.backend.emit_cmovb_r_r (&tmp_res64, &tmp_1),
                            FOLE | FULE => self.backend.emit_cmovbe_r_r(&tmp_res64, &tmp_1),

                            _ => unimplemented!()
454
                        }
qinsoon's avatar
qinsoon committed
455
456
457

                        // truncate tmp_res64 to tmp_res (probably u8)
                        self.backend.emit_mov_r_r(&tmp_res, &tmp_res64);
458
459
                    }

qinsoon's avatar
qinsoon committed
460
                    Instruction_::Branch1(ref dest) => {
qinsoon's avatar
qinsoon committed
461
                        trace!("instsel on BRANCH1");
qinsoon's avatar
qinsoon committed
462
                        let ops = inst.ops.read().unwrap();
463
                                            
464
                        self.process_dest(&ops, dest, f_content, f_context, vm);
465
                        
466
                        let target = f_content.get_block(dest.target).name().unwrap();
qinsoon's avatar
qinsoon committed
467
                        
qinsoon's avatar
qinsoon committed
468
                        trace!("emit branch1");
469
                        // jmp
qinsoon's avatar
qinsoon committed
470
                        self.backend.emit_jmp(target);
471
                    },
qinsoon's avatar
qinsoon committed
472
473

                    Instruction_::Switch{cond, ref default, ref branches} => {
qinsoon's avatar
qinsoon committed
474
                        trace!("instsel on SWITCH");
qinsoon's avatar
qinsoon committed
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
                        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
495
                                    self.backend.emit_cmp_imm_r(imm, &tmp_cond);
qinsoon's avatar
qinsoon committed
496
497
498
499
500
501
                                    // 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
502
                                    self.backend.emit_cmp_r_r(&tmp_case_op, &tmp_cond);
qinsoon's avatar
qinsoon committed
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
                                    // 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);
                        }
                    }
519
                    
qinsoon's avatar
qinsoon committed
520
                    Instruction_::ExprCall{ref data, is_abort} => {
qinsoon's avatar
qinsoon committed
521
522
                        trace!("instsel on EXPRCALL");

qinsoon's avatar
qinsoon committed
523
524
                        if is_abort {
                            unimplemented!()
525
                        }
526
                        
qinsoon's avatar
qinsoon committed
527
528
529
530
531
                        self.emit_mu_call(
                            inst, // inst: &Instruction,
                            data, // calldata: &CallData,
                            None, // resumption: Option<&ResumptionData>,
                            node, // cur_node: &TreeNode, 
532
                            f_content, f_context, vm);
533
534
                    },
                    
qinsoon's avatar
qinsoon committed
535
                    Instruction_::Call{ref data, ref resume} => {
qinsoon's avatar
qinsoon committed
536
537
                        trace!("instsel on CALL");

qinsoon's avatar
qinsoon committed
538
539
540
541
542
543
                        self.emit_mu_call(
                            inst, 
                            data, 
                            Some(resume), 
                            node, 
                            f_content, f_context, vm);
qinsoon's avatar
qinsoon committed
544
545
546
                    },

                    Instruction_::ExprCCall{ref data, is_abort} => {
qinsoon's avatar
qinsoon committed
547
548
                        trace!("instsel on EXPRCCALL");

qinsoon's avatar
qinsoon committed
549
550
551
552
553
554
555
556
                        if is_abort {
                            unimplemented!()
                        }

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

                    Instruction_::CCall{ref data, ref resume} => {
qinsoon's avatar
qinsoon committed
557
558
                        trace!("instsel on CCALL");

qinsoon's avatar
qinsoon committed
559
                        self.emit_c_call_ir(inst, data, Some(resume), node, f_content, f_context, vm);
qinsoon's avatar
qinsoon committed
560
561
                    }
                    
562
                    Instruction_::Return(_) => {
qinsoon's avatar
qinsoon committed
563
564
                        trace!("instsel on RETURN");

565
                        self.emit_common_epilogue(inst, f_content, f_context, vm);
566
                        
qinsoon's avatar
qinsoon committed
567
                        self.backend.emit_ret();
568
569
                    },
                    
qinsoon's avatar
qinsoon committed
570
                    Instruction_::BinOp(op, op1, op2) => {
qinsoon's avatar
qinsoon committed
571
572
                        trace!("instsel on BINOP");

qinsoon's avatar
qinsoon committed
573
                        let ops = inst.ops.read().unwrap();
574
575

                        let res_tmp = self.get_result_value(node);
qinsoon's avatar
qinsoon committed
576
                        
577
578
                        match op {
                            op::BinOp::Add => {
qinsoon's avatar
qinsoon committed
579
                                if self.match_ireg(&ops[op1]) && self.match_iimm(&ops[op2]) {
qinsoon's avatar
qinsoon committed
580
581
                                    trace!("emit add-ireg-imm");
                                    
582
                                    let reg_op1 = self.emit_ireg(&ops[op1], f_content, f_context, vm);
583
                                    let reg_op2 = self.node_iimm_to_i32(&ops[op2]);
qinsoon's avatar
qinsoon committed
584
585
                                    
                                    // mov op1, res
qinsoon's avatar
qinsoon committed
586
                                    self.backend.emit_mov_r_r(&res_tmp, &reg_op1);
qinsoon's avatar
qinsoon committed
587
                                    // add op2, res
qinsoon's avatar
qinsoon committed
588
                                    self.backend.emit_add_r_imm(&res_tmp, reg_op2);
qinsoon's avatar
qinsoon committed
589
590
591
                                } else if self.match_ireg(&ops[op1]) && self.match_mem(&ops[op2]) {
                                    trace!("emit add-ireg-mem");
                                    
592
                                    let reg_op1 = self.emit_ireg(&ops[op1], f_content, f_context, vm);
qinsoon's avatar
shl    
qinsoon committed
593
                                    let reg_op2 = self.emit_mem(&ops[op2], vm);
qinsoon's avatar
qinsoon committed
594
595
                                    
                                    // mov op1, res
qinsoon's avatar
qinsoon committed
596
                                    self.backend.emit_mov_r_r(&res_tmp, &reg_op1);
qinsoon's avatar
qinsoon committed
597
                                    // add op2 res
qinsoon's avatar
qinsoon committed
598
                                    self.backend.emit_add_r_mem(&res_tmp, &reg_op2);
qinsoon's avatar
qinsoon committed
599
600
601
602
603
604
605
                                } 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
606
                                    self.backend.emit_mov_r_r(&res_tmp, &reg_op1);
qinsoon's avatar
qinsoon committed
607
                                    // add op2 res
qinsoon's avatar
qinsoon committed
608
                                    self.backend.emit_add_r_r(&res_tmp, &reg_op2);
qinsoon's avatar
qinsoon committed
609
610
611
                                } else {
                                    unimplemented!()
                                }
612
613
                            },
                            op::BinOp::Sub => {
614
                                if self.match_ireg(&ops[op1]) && self.match_iimm(&ops[op2]) {
qinsoon's avatar
qinsoon committed
615
616
                                    trace!("emit sub-ireg-imm");

617
                                    let reg_op1 = self.emit_ireg(&ops[op1], f_content, f_context, vm);
618
                                    let imm_op2 = self.node_iimm_to_i32(&ops[op2]);
qinsoon's avatar
qinsoon committed
619
620
                                    
                                    // mov op1, res
qinsoon's avatar
qinsoon committed
621
                                    self.backend.emit_mov_r_r(&res_tmp, &reg_op1);
qinsoon's avatar
qinsoon committed
622
                                    // add op2, res
qinsoon's avatar
qinsoon committed
623
                                    self.backend.emit_sub_r_imm(&res_tmp, imm_op2);
qinsoon's avatar
qinsoon committed
624
625
626
                                } else if self.match_ireg(&ops[op1]) && self.match_mem(&ops[op2]) {
                                    trace!("emit sub-ireg-mem");
                                    
627
                                    let reg_op1 = self.emit_ireg(&ops[op1], f_content, f_context, vm);
qinsoon's avatar
shl    
qinsoon committed
628
                                    let mem_op2 = self.emit_mem(&ops[op2], vm);
qinsoon's avatar
qinsoon committed
629
630
                                    
                                    // mov op1, res
qinsoon's avatar
qinsoon committed
631
                                    self.backend.emit_mov_r_r(&res_tmp, &reg_op1);
qinsoon's avatar
qinsoon committed
632
                                    // sub op2 res
qinsoon's avatar
qinsoon committed
633
                                    self.backend.emit_sub_r_mem(&res_tmp, &mem_op2);
634
635
                                } else if self.match_ireg(&ops[op1]) && self.match_ireg(&ops[op2]) {
                                    trace!("emit sub-ireg-ireg");
636

637
638
                                    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);
639

640
                                    // mov op1, res
qinsoon's avatar
qinsoon committed
641
                                    self.backend.emit_mov_r_r(&res_tmp, &reg_op1);
642
                                    // add op2 res
qinsoon's avatar
qinsoon committed
643
                                    self.backend.emit_sub_r_r(&res_tmp, &reg_op2);
qinsoon's avatar
qinsoon committed
644
645
646
                                } else {
                                    unimplemented!()
                                }
647
                            },
648
649
650
651
652
653
654
655
656
657
658
                            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
659
                                    self.backend.emit_mov_r_r(&res_tmp, &tmp_op1);
660
                                    // and op2, res -> res
qinsoon's avatar
qinsoon committed
661
                                    self.backend.emit_and_r_imm(&res_tmp, imm_op2);
662
663
664
665
666
667
668
                                } 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
669
                                    self.backend.emit_mov_r_r(&res_tmp, &tmp_op1);
670
                                    // and op2, res -> res
qinsoon's avatar
qinsoon committed
671
                                    self.backend.emit_and_r_mem(&res_tmp, &mem_op2);
672
673
674
675
676
677
678
                                } 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
679
                                    self.backend.emit_mov_r_r(&res_tmp, &tmp_op1);
680
                                    // and op2, res -> res
qinsoon's avatar
qinsoon committed
681
                                    self.backend.emit_and_r_r(&res_tmp, &tmp_op2);
682
683
684
685
                                } else {
                                    unimplemented!()
                                }
                            },
686
687
688
689
690
691
692
693
694
695
696
                            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
697
                                    self.backend.emit_mov_r_r(&res_tmp, &tmp_op1);
698
                                    // Or op2, res -> res
qinsoon's avatar
qinsoon committed
699
                                    self.backend.emit_or_r_imm(&res_tmp, imm_op2);
700
701
702
703
704
705
706
                                } 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
707
                                    self.backend.emit_mov_r_r(&res_tmp, &tmp_op1);
708
                                    // Or op2, res -> res
qinsoon's avatar
qinsoon committed
709
                                    self.backend.emit_or_r_mem(&res_tmp, &mem_op2);
710
711
712
713
714
715
716
                                } 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
717
                                    self.backend.emit_mov_r_r(&res_tmp, &tmp_op1);
718
                                    // Or op2, res -> res
qinsoon's avatar
qinsoon committed
719
                                    self.backend.emit_or_r_r(&res_tmp, &tmp_op2);
720
721
722
723
                                } else {
                                    unimplemented!()
                                }
                            },
724
725
726
727
728
729
730
731
732
733
734
                            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
735
                                    self.backend.emit_mov_r_r(&res_tmp, &tmp_op1);
736
                                    // xor op2, res -> res
qinsoon's avatar
qinsoon committed
737
                                    self.backend.emit_xor_r_imm(&res_tmp, imm_op2);
738
739
740
741
742
743
744
                                } 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
745
                                    self.backend.emit_mov_r_r(&res_tmp, &tmp_op1);
746
                                    // xor op2, res -> res
qinsoon's avatar
qinsoon committed
747
                                    self.backend.emit_xor_r_mem(&res_tmp, &mem_op2);
748
749
750
751
752
753
754
                                } 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
755
                                    self.backend.emit_mov_r_r(&res_tmp, &tmp_op1);
756
                                    // xor op2, res -> res
qinsoon's avatar
qinsoon committed
757
                                    self.backend.emit_xor_r_r(&res_tmp, &tmp_op2);
758
759
760
761
                                } else {
                                    unimplemented!()
                                }
                            }
762
                            op::BinOp::Mul => {
763
764
                                // mov op1 -> rax
                                let op1 = &ops[op1];
qinsoon's avatar
qinsoon committed
765
766
767
768
769
770
771
772
773

                                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!()
                                };

774
                                if self.match_iimm(op1) {
775
                                    let imm_op1 = self.node_iimm_to_i32(op1);
776
                                    
qinsoon's avatar
qinsoon committed
777
                                    self.backend.emit_mov_r_imm(&mreg_op1, imm_op1);
778
                                } else if self.match_mem(op1) {
qinsoon's avatar
shl    
qinsoon committed
779
                                    let mem_op1 = self.emit_mem(op1, vm);
780
                                    
qinsoon's avatar
qinsoon committed
781
                                    self.backend.emit_mov_r_mem(&mreg_op1, &mem_op1);
782
783
784
                                } else if self.match_ireg(op1) {
                                    let reg_op1 = self.emit_ireg(op1, f_content, f_context, vm);

qinsoon's avatar
qinsoon committed
785
                                    self.backend.emit_mov_r_r(&mreg_op1, &reg_op1);
786
787
788
789
                                } else {
                                    unimplemented!();
                                }
                                
qinsoon's avatar
qinsoon committed
790
                                // mul op2
791
                                let op2 = &ops[op2];
792
                                if self.match_iimm(op2) {
793
                                    let imm_op2 = self.node_iimm_to_i32(op2);
794
795
796
                                    
                                    // put imm in a temporary
                                    // here we use result reg as temporary
qinsoon's avatar
qinsoon committed
797
                                    self.backend.emit_mov_r_imm(&res_tmp, imm_op2);
798
                                    
qinsoon's avatar
qinsoon committed
799
                                    self.backend.emit_mul_r(&res_tmp);
800
                                } else if self.match_mem(op2) {
qinsoon's avatar
shl    
qinsoon committed
801
                                    let mem_op2 = self.emit_mem(op2, vm);
802
                                    
qinsoon's avatar
qinsoon committed
803
                                    self.backend.emit_mul_mem(&mem_op2);
804
805
806
                                } else if self.match_ireg(op2) {
                                    let reg_op2 = self.emit_ireg(op2, f_content, f_context, vm);

qinsoon's avatar
qinsoon committed
807
                                    self.backend.emit_mul_r(&reg_op2);
808
809
810
811
812
                                } else {
                                    unimplemented!();
                                }
                                
                                // mov rax -> result
qinsoon's avatar
qinsoon committed
813
814
815
816
817
818
819
820
                                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!()
                                }

821
                            },
qinsoon's avatar
qinsoon committed
822
823
824
825
                            op::BinOp::Udiv => {
                                let op1 = &ops[op1];
                                let op2 = &ops[op2];

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

qinsoon's avatar
sdiv    
qinsoon committed
828
                                // mov rax -> result
qinsoon's avatar
qinsoon committed
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
                                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
844
845
846
847
                            },
                            op::BinOp::Sdiv => {
                                let op1 = &ops[op1];
                                let op2 = &ops[op2];
qinsoon's avatar
qinsoon committed
848

qinsoon's avatar
sdiv    
qinsoon committed
849
                                self.emit_idiv(op1, op2, f_content, f_context, vm);
qinsoon's avatar
qinsoon committed
850
851

                                // mov rax -> result
qinsoon's avatar
qinsoon committed
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
                                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
867
                            },
qinsoon's avatar
qinsoon committed
868
869
870
871
872
873
874
                            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
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
                                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
890
891
892
893
894
895
896
897
                            },
                            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
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
                                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
913
                            },
qinsoon's avatar
qinsoon committed
914

qinsoon's avatar
shl    
qinsoon committed
915
916
917
918
                            op::BinOp::Shl => {
                                let op1 = &ops[op1];
                                let op2 = &ops[op2];

919
920
921
                                if self.match_mem(op1) {
                                    unimplemented!()
                                } else if self.match_ireg(op1) {
qinsoon's avatar
shl    
qinsoon committed
922
923
                                    let tmp_op1 = self.emit_ireg(op1, f_content, f_context, vm);

924
925
926
927
                                    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
928
                                        self.backend.emit_shl_r_imm8(&tmp_op1, imm_op2);
929
930

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

qinsoon's avatar
qinsoon committed
935
936
                                        // mov op2 -> cl
                                        self.backend.emit_mov_r_r(&x86_64::CL, &tmp_op2);
qinsoon's avatar
shl    
qinsoon committed
937
938

                                        // shl op1, cl -> op1
qinsoon's avatar
qinsoon committed
939
                                        self.backend.emit_shl_r_cl(&tmp_op1);
qinsoon's avatar
shl    
qinsoon committed
940
941

                                        // mov op1 -> result
qinsoon's avatar
qinsoon committed
942
                                        self.backend.emit_mov_r_r(&res_tmp, &tmp_op1);
qinsoon's avatar
shl    
qinsoon committed
943
944
945
                                    } else {
                                        panic!("unexpected op2 (not ireg not iimm): {}", op2);
                                    }
946
947
                                } else {
                                    panic!("unexpected op1 (not ireg not mem): {}", op1);
qinsoon's avatar
shl    
qinsoon committed
948
                                }
qinsoon's avatar
qinsoon committed
949
950
951
952
953
                            },
                            op::BinOp::Lshr => {
                                let op1 = &ops[op1];
                                let op2 = &ops[op2];

954
955
956
                                if self.match_mem(op1) {
                                    unimplemented!()
                                } else if self.match_ireg(op1) {
qinsoon's avatar
qinsoon committed
957
958
                                    let tmp_op1 = self.emit_ireg(op1, f_content, f_context, vm);

959
960
961
962
                                    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
963
                                        self.backend.emit_shr_r_imm8(&tmp_op1, imm_op2);
964
965

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

qinsoon's avatar
qinsoon committed
970
971
                                        // mov op2 -> cl
                                        self.backend.emit_mov_r_r(&x86_64::CL, &tmp_op2);
qinsoon's avatar
qinsoon committed
972
973

                                        // shr op1, cl -> op1
qinsoon's avatar
qinsoon committed
974
                                        self.backend.emit_shr_r_cl(&tmp_op1);
qinsoon's avatar
qinsoon committed
975
976

                                        // mov op1 -> result
qinsoon's avatar
qinsoon committed
977
                                        self.backend.emit_mov_r_r(&res_tmp, &tmp_op1);
qinsoon's avatar
qinsoon committed
978
979
980
                                    } else {
                                        panic!("unexpected op2 (not ireg not iimm): {}", op2);
                                    }
981
982
                                } else {
                                    panic!("unexpected op1 (not ireg not mem): {}", op1);
qinsoon's avatar
qinsoon committed
983
984
985
986
987
988
                                }
                            },
                            op::BinOp::Ashr => {
                                let op1 = &ops[op1];
                                let op2 = &ops[op2];

989
990
991
                                if self.match_mem(op1) {
                                    unimplemented!()
                                } else if self.match_ireg(op1) {
qinsoon's avatar
qinsoon committed
992
993
                                    let tmp_op1 = self.emit_ireg(op1, f_content, f_context, vm);

994
995
996
997
                                    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
998
                                        self.backend.emit_sar_r_imm8(&tmp_op1, imm_op2);
999
1000

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

qinsoon's avatar
qinsoon committed
1005
1006
                                        // mov op2 -> cl
                                        self.backend.emit_mov_r_r(&x86_64::CL, &tmp_op2);
qinsoon's avatar
qinsoon committed
1007
1008

                                        // sar op1, cl -> op1
qinsoon's avatar
qinsoon committed
1009
                                        self.backend.emit_sar_r_cl(&tmp_op1);
qinsoon's avatar
qinsoon committed
1010
1011

                                        // mov op1 -> result
qinsoon's avatar
qinsoon committed
1012
                                        self.backend.emit_mov_r_r(&res_tmp, &tmp_op1);
1013
                                    } else  {
qinsoon's avatar
qinsoon committed
1014
1015
                                        panic!("unexpected op2 (not ireg not iimm): {}", op2);
                                    }
1016
1017
                                } else {
                                    panic!("unexpected op1 (not ireg not mem): {}", op1);
qinsoon's avatar
qinsoon committed
1018
1019
1020
                                }
                            },

qinsoon's avatar
shl    
qinsoon committed
1021

qinsoon's avatar
qinsoon committed
1022
1023
                            // floating point
                            op::BinOp::FAdd => {
1024
                                if self.match_fpreg(&ops[op1]) && self.match_mem(&ops[op2]) {
qinsoon's avatar
qinsoon committed
1025
1026
1027
                                    trace!("emit add-fpreg-mem");

                                    let reg_op1 = self.emit_fpreg(&ops[op1], f_content, f_context, vm);
qinsoon's avatar
shl    
qinsoon committed
1028
                                    let mem_op2 = self.emit_mem(&ops[op2], vm);
qinsoon's avatar
qinsoon committed
1029
1030
1031
1032
1033

                                    // 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);
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
                                } 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
1044
                                } else {
qinsoon's avatar
qinsoon committed
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
                                    panic!("unexpected fadd: {}", node)
                                }
                            }

                            op::BinOp::FSub => {
                                if self.match_fpreg(&ops[op1]) && self.match_mem(&ops[op2]) {
                                    trace!("emit sub-fpreg-mem");

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

                                    // mov op1, res
                                    self.backend.emit_movsd_f64_f64(&res_tmp, &reg_op1);
                                    // sub op2 res
                                    self.backend.emit_subsd_f64_mem64(&res_tmp, &mem_op2);
                                } else if self.match_fpreg(&ops[op1]) && self.match_fpreg(&ops[op2]) {
                                    trace!("emit sub-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);
                                    // sub op2 res
                                    self.backend.emit_subsd_f64_f64(&res_tmp, &reg_op2);
                                } else {
                                    panic!("unexpected fsub: {}", node)
                                }
                            }

                            op::BinOp::FMul => {
                                if self.match_fpreg(&ops[op1]) && self.match_mem(&ops[op2]) {
                                    trace!("emit mul-fpreg-mem");

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

                                    // mov op1, res
                                    self.backend.emit_movsd_f64_f64(&res_tmp, &reg_op1);
                                    // mul op2 res
                                    self.backend.emit_mulsd_f64_mem64(&res_tmp, &mem_op2);
                                } else if self.match_fpreg(&ops[op1]) && self.match_fpreg(&ops[op2]) {
                                    trace!("emit mul-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);
                                    // mul op2 res
                                    self.backend.emit_mulsd_f64_f64(&res_tmp, &reg_op2);
                                } else {
                                    panic!("unexpected fmul: {}", node)
                                }
                            }

                            op::BinOp::FDiv => {
                                if self.match_fpreg(&ops[op1]) && self.match_mem(&ops[op2]) {
                                    trace!("emit div-fpreg-mem");

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

                                    // mov op1, res
                                    self.backend.emit_movsd_f64_f64(&res_tmp, &reg_op1);
                                    // div op2 res
                                    self.backend.emit_divsd_f64_mem64(&res_tmp, &mem_op2);
                                } else if self.match_fpreg(&ops[op1]) && self.match_fpreg(&ops[op2]) {
                                    trace!("emit div-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);
                                    // div op2 res
                                    self.backend.emit_divsd_f64_f64(&res_tmp, &reg_op2);
                                } else {
                                    panic!("unexpected fdiv: {}", node)
qinsoon's avatar
qinsoon committed
1124
1125
                                }
                            }
qinsoon's avatar
qinsoon committed
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140

                            op::BinOp::FRem => {
                                if self.match_fpreg(&ops[op1]) && self.match_fpreg(&ops[op2]) {
                                    trace!("emit frem-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);

                                    let reg_tmp = self.get_result_value(node);

                                    self.emit_runtime_entry(&entrypoints::FREM, vec![reg_op1.clone(), reg_op2.clone()], Some(vec![reg_tmp.clone()]), Some(node), f_content, f_context, vm);
                                } else {
                                    panic!("unexpected fdiv: {}", node)
                                }
                            }
1141
1142
                        }
                    }
qinsoon's avatar
qinsoon committed
1143
1144

                    Instruction_::ConvOp{operation, ref from_ty, ref to_ty, operand} => {
qinsoon's avatar
qinsoon committed
1145
1146
                        trace!("instsel on CONVOP");

qinsoon's avatar
qinsoon committed
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
                        let ops = inst.ops.read().unwrap();

                        let ref op = ops[operand];

                        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
1158
                                    self.backend.emit_mov_r_r(&tmp_res, &tmp_op);
qinsoon's avatar
qinsoon committed
1159
1160
1161
1162
1163
                                } else {
                                    panic!("unexpected op (expect ireg): {}", op);
                                }
                            }
                            op::ConvOp::ZEXT => {
1164
1165
1166
                                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
1167

1168
1169
1170
                                    // 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;