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

inst_sel.rs 42.4 KB
Newer Older
1
use ast::ir::*;
2
use ast::ptr::*;
qinsoon's avatar
qinsoon committed
3
use ast::inst::Instruction;
4
5
use ast::inst::Destination;
use ast::inst::DestArg;
qinsoon's avatar
qinsoon committed
6
use ast::inst::Instruction_;
7
use ast::inst::MemoryOrder;
8
use ast::op;
qinsoon's avatar
qinsoon committed
9
use ast::types;
qinsoon's avatar
qinsoon committed
10
use ast::types::*;
qinsoon's avatar
qinsoon committed
11
use vm::VM;
qinsoon's avatar
qinsoon committed
12
use vm::CompiledFunction;
qinsoon's avatar
qinsoon committed
13
use runtime::mm;
14
15
16
17
use runtime::ValueLocation;
use runtime::thread;
use runtime::entrypoints;
use runtime::entrypoints::RuntimeEntrypoint;
18
19

use compiler::CompilerPass;
qinsoon's avatar
qinsoon committed
20
21
22
use compiler::backend::x86_64;
use compiler::backend::x86_64::CodeGenerator;
use compiler::backend::x86_64::ASMCodeGen;
23

24
25
use std::collections::HashMap;

26
pub struct InstructionSelection {
27
28
    name: &'static str,
    
qinsoon's avatar
qinsoon committed
29
    backend: Box<CodeGenerator>
30
31
}

32
33
34
35
36
37
38
39
40
41
42
43
44
45
impl <'a> InstructionSelection {
    pub fn new() -> InstructionSelection {
        InstructionSelection{
            name: "Instruction Selection (x64)",
            backend: Box::new(ASMCodeGen::new())
        }
    }
    
    // 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)]
46
    fn instruction_select(&mut self, node: &'a TreeNode, f_content: &FunctionContent, f_context: &mut FunctionContext, vm: &VM) {
qinsoon's avatar
qinsoon committed
47
48
49
        trace!("instsel on node {}", node);
        
        match node.v {
50
51
            TreeNode_::Instruction(ref inst) => {
                match inst.v {
qinsoon's avatar
qinsoon committed
52
53
54
                    Instruction_::Branch2{cond, ref true_dest, ref false_dest, true_prob} => {
                        // move this to trace generation
                        // assert here
55
56
57
58
59
                        let (fallthrough_dest, branch_dest, branch_if_true) = {
                            if true_prob > 0.5f32 {
                                (true_dest, false_dest, false)
                            } else {
                                (false_dest, true_dest, true)
60
                            }
61
                        };
62
                        
qinsoon's avatar
qinsoon committed
63
                        let ops = inst.ops.read().unwrap();
64
                        
65
66
                        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
67
                        
68
                        let branch_target = f_content.get_block(branch_dest.target);
69
70
71
    
                        let ref cond = ops[cond];
                        
qinsoon's avatar
qinsoon committed
72
73
                        if self.match_cmp_res(cond) {
                            trace!("emit cmp_eq-branch2");
74
                            match self.emit_cmp_res(cond, f_content, f_context, vm) {
qinsoon's avatar
qinsoon committed
75
76
77
78
79
80
81
82
83
84
                                op::CmpOp::EQ => self.backend.emit_je(branch_target),
                                op::CmpOp::NE => self.backend.emit_jne(branch_target),
                                op::CmpOp::UGE => self.backend.emit_jae(branch_target),
                                op::CmpOp::UGT => self.backend.emit_ja(branch_target),
                                op::CmpOp::ULE => self.backend.emit_jbe(branch_target),
                                op::CmpOp::ULT => self.backend.emit_jb(branch_target),
                                op::CmpOp::SGE => self.backend.emit_jge(branch_target),
                                op::CmpOp::SGT => self.backend.emit_jg(branch_target),
                                op::CmpOp::SLE => self.backend.emit_jle(branch_target),
                                op::CmpOp::SLT => self.backend.emit_jl(branch_target),
qinsoon's avatar
qinsoon committed
85
86
87
88
                                _ => unimplemented!()
                            }
                        } else if self.match_ireg(cond) {
                            trace!("emit ireg-branch2");
89
                            
90
                            let cond_reg = self.emit_ireg(cond, f_content, f_context, vm);
91
                            
qinsoon's avatar
qinsoon committed
92
93
94
                            // emit: cmp cond_reg 1
                            self.backend.emit_cmp_r64_imm32(&cond_reg, 1);
                            // emit: je #branch_dest
qinsoon's avatar
qinsoon committed
95
                            self.backend.emit_je(branch_target);                            
qinsoon's avatar
qinsoon committed
96
97
                        } else {
                            unimplemented!();
98
                        }
99
100
                    },
                    
qinsoon's avatar
qinsoon committed
101
                    Instruction_::Branch1(ref dest) => {
qinsoon's avatar
qinsoon committed
102
                        let ops = inst.ops.read().unwrap();
103
                                            
104
                        self.process_dest(&ops, dest, f_content, f_context, vm);
105
                        
106
                        let target = f_content.get_block(dest.target);
qinsoon's avatar
qinsoon committed
107
                        
qinsoon's avatar
qinsoon committed
108
                        trace!("emit branch1");
109
                        // jmp
qinsoon's avatar
qinsoon committed
110
                        self.backend.emit_jmp(target);
111
112
                    },
                    
qinsoon's avatar
qinsoon committed
113
114
                    Instruction_::ExprCall{ref data, is_abort} => {
                        trace!("deal with pre-call convention");
115
                        
qinsoon's avatar
qinsoon committed
116
                        let ops = inst.ops.read().unwrap();
117
118
119
120
                        let rets = inst.value.as_ref().unwrap();
                        let ref func = ops[data.func];
                        let ref func_sig = match func.v {
                            TreeNode_::Value(ref pv) => {
qinsoon's avatar
qinsoon committed
121
122
123
124
                                let ty : &MuType = &pv.ty;
                                match ty.v {
                                    MuType_::FuncRef(ref sig)
                                    | MuType_::UFuncPtr(ref sig) => sig,
125
126
127
128
129
130
131
132
133
                                    _ => panic!("expected funcref/ptr type")
                                }
                            },
                            _ => panic!("expected funcref/ptr type")
                        };
                        
                        debug_assert!(func_sig.ret_tys.len() == data.args.len());
                        debug_assert!(func_sig.arg_tys.len() == rets.len());
                                                
qinsoon's avatar
qinsoon committed
134
                        let mut gpr_arg_count = 0;
135
                        // TODO: let mut fpr_arg_count = 0;
136
137
138
                        for arg_index in data.args.iter() {
                            let ref arg = ops[*arg_index];
                            trace!("arg {}", arg);
qinsoon's avatar
qinsoon committed
139
140
                            
                            if self.match_ireg(arg) {
141
                                let arg = self.emit_ireg(arg, f_content, f_context, vm);
qinsoon's avatar
qinsoon committed
142
                                
143
144
145
146
147
148
149
                                if gpr_arg_count < x86_64::ARGUMENT_GPRs.len() {
                                    self.backend.emit_mov_r64_r64(&x86_64::ARGUMENT_GPRs[gpr_arg_count], &arg);
                                    gpr_arg_count += 1;
                                } else {
                                    // use stack to pass argument
                                    unimplemented!();
                                }
qinsoon's avatar
qinsoon committed
150
151
152
                            } else if self.match_iimm(arg) {
                                let arg = self.emit_get_iimm(arg);
                                
153
154
155
156
157
158
159
                                if gpr_arg_count < x86_64::ARGUMENT_GPRs.len() {
                                    self.backend.emit_mov_r64_imm32(&x86_64::ARGUMENT_GPRs[gpr_arg_count], arg);
                                    gpr_arg_count += 1;
                                } else {
                                    // use stack to pass argument
                                    unimplemented!();
                                }
qinsoon's avatar
qinsoon committed
160
161
                            } else {
                                unimplemented!();
162
                            }
163
164
                        }
                        
165
166
                        // check direct call or indirect
                        if self.match_funcref_const(func) {
qinsoon's avatar
qinsoon committed
167
168
                            let target_id = self.emit_get_funcref_const(func);
                            let funcs = vm.funcs().read().unwrap();
qinsoon's avatar
qinsoon committed
169
                            let target = funcs.get(&target_id).unwrap().read().unwrap();
qinsoon's avatar
qinsoon committed
170
171
172
173
174
175
                                                        
                            if vm.is_running() {
                                unimplemented!()
                            } else {
                                self.backend.emit_call_near_rel32(target.name().unwrap());
                            }
176
                        } else if self.match_ireg(func) {
177
                            let target = self.emit_ireg(func, f_content, f_context, vm);
178
179
180
181
182
183
184
185
186
                            
                            self.backend.emit_call_near_r64(&target);
                        } else if self.match_mem(func) {
                            let target = self.emit_mem(func);
                            
                            self.backend.emit_call_near_mem64(&target);
                        } else {
                            unimplemented!();
                        }
187
                        
qinsoon's avatar
qinsoon committed
188
                        // deal with ret vals
189
                        let mut gpr_ret_count = 0;
190
                        // TODO: let mut fpr_ret_count = 0;
191
192
193
194
195
196
197
198
                        for val in rets {
                            if val.is_int_reg() {
                                if gpr_ret_count < x86_64::RETURN_GPRs.len() {
                                    self.backend.emit_mov_r64_r64(&val, &x86_64::RETURN_GPRs[gpr_ret_count]);
                                    gpr_ret_count += 1;
                                } else {
                                    // get return value by stack
                                    unimplemented!();
199
                                }
200
201
202
                            } else {
                                // floating point register
                                unimplemented!();
203
                            }
204
                        }
205
206
207
                    },
                    
                    Instruction_::Return(_) => {
208
                        self.emit_common_epilogue(inst, f_content, f_context, vm);
209
                        
qinsoon's avatar
qinsoon committed
210
                        self.backend.emit_ret();
211
212
                    },
                    
qinsoon's avatar
qinsoon committed
213
                    Instruction_::BinOp(op, op1, op2) => {
qinsoon's avatar
qinsoon committed
214
                        let ops = inst.ops.read().unwrap();
qinsoon's avatar
qinsoon committed
215
                        
216
217
                        match op {
                            op::BinOp::Add => {
qinsoon's avatar
qinsoon committed
218
219
220
                                if self.match_ireg(&ops[op1]) && self.match_ireg(&ops[op2]) {
                                    trace!("emit add-ireg-ireg");
                                    
221
222
                                    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);
qinsoon's avatar
qinsoon committed
223
224
225
226
227
228
229
230
231
                                    let res_tmp = self.emit_get_result(node);
                                    
                                    // mov op1, res
                                    self.backend.emit_mov_r64_r64(&res_tmp, &reg_op1);
                                    // add op2 res
                                    self.backend.emit_add_r64_r64(&res_tmp, &reg_op2);
                                } else if self.match_ireg(&ops[op1]) && self.match_iimm(&ops[op2]) {
                                    trace!("emit add-ireg-imm");
                                    
232
                                    let reg_op1 = self.emit_ireg(&ops[op1], f_content, f_context, vm);
qinsoon's avatar
qinsoon committed
233
234
235
236
237
238
239
240
241
242
243
244
245
                                    let reg_op2 = self.emit_get_iimm(&ops[op2]);
                                    let res_tmp = self.emit_get_result(node);
                                    
                                    // mov op1, res
                                    self.backend.emit_mov_r64_r64(&res_tmp, &reg_op1);
                                    // add op2, res
                                    self.backend.emit_add_r64_imm32(&res_tmp, reg_op2);
                                } else if self.match_iimm(&ops[op1]) && self.match_ireg(&ops[op2]) {
                                    trace!("emit add-imm-ireg");
                                    unimplemented!();
                                } else if self.match_ireg(&ops[op1]) && self.match_mem(&ops[op2]) {
                                    trace!("emit add-ireg-mem");
                                    
246
                                    let reg_op1 = self.emit_ireg(&ops[op1], f_content, f_context, vm);
qinsoon's avatar
qinsoon committed
247
248
249
250
251
252
253
254
255
256
257
258
259
                                    let reg_op2 = self.emit_mem(&ops[op2]);
                                    let res_tmp = self.emit_get_result(node);
                                    
                                    // mov op1, res
                                    self.backend.emit_mov_r64_r64(&res_tmp, &reg_op1);
                                    // add op2 res
                                    self.backend.emit_add_r64_mem64(&res_tmp, &reg_op2);
                                } else if self.match_mem(&ops[op1]) && self.match_ireg(&ops[op2]) {
                                    trace!("emit add-mem-ireg");
                                    unimplemented!();
                                } else {
                                    unimplemented!()
                                }
260
261
                            },
                            op::BinOp::Sub => {
qinsoon's avatar
qinsoon committed
262
263
264
                                if self.match_ireg(&ops[op1]) && self.match_ireg(&ops[op2]) {
                                    trace!("emit sub-ireg-ireg");
                                    
265
266
                                    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);
qinsoon's avatar
qinsoon committed
267
268
269
270
271
272
273
274
275
                                    let res_tmp = self.emit_get_result(node);
                                    
                                    // mov op1, res
                                    self.backend.emit_mov_r64_r64(&res_tmp, &reg_op1);
                                    // add op2 res
                                    self.backend.emit_sub_r64_r64(&res_tmp, &reg_op2);
                                } else if self.match_ireg(&ops[op1]) && self.match_iimm(&ops[op2]) {
                                    trace!("emit sub-ireg-imm");

276
                                    let reg_op1 = self.emit_ireg(&ops[op1], f_content, f_context, vm);
277
                                    let imm_op2 = self.emit_get_iimm(&ops[op2]);
qinsoon's avatar
qinsoon committed
278
279
280
281
282
                                    let res_tmp = self.emit_get_result(node);
                                    
                                    // mov op1, res
                                    self.backend.emit_mov_r64_r64(&res_tmp, &reg_op1);
                                    // add op2, res
283
                                    self.backend.emit_sub_r64_imm32(&res_tmp, imm_op2);
qinsoon's avatar
qinsoon committed
284
285
286
287
288
289
                                } else if self.match_iimm(&ops[op1]) && self.match_ireg(&ops[op2]) {
                                    trace!("emit sub-imm-ireg");
                                    unimplemented!();
                                } else if self.match_ireg(&ops[op1]) && self.match_mem(&ops[op2]) {
                                    trace!("emit sub-ireg-mem");
                                    
290
                                    let reg_op1 = self.emit_ireg(&ops[op1], f_content, f_context, vm);
291
                                    let mem_op2 = self.emit_mem(&ops[op2]);
qinsoon's avatar
qinsoon committed
292
293
294
295
296
                                    let res_tmp = self.emit_get_result(node);
                                    
                                    // mov op1, res
                                    self.backend.emit_mov_r64_r64(&res_tmp, &reg_op1);
                                    // sub op2 res
297
                                    self.backend.emit_sub_r64_mem64(&res_tmp, &mem_op2);
qinsoon's avatar
qinsoon committed
298
299
300
301
302
303
                                } else if self.match_mem(&ops[op1]) && self.match_ireg(&ops[op2]) {
                                    trace!("emit add-mem-ireg");
                                    unimplemented!();
                                } else {
                                    unimplemented!()
                                }
304
305
                            },
                            op::BinOp::Mul => {
306
307
308
309
                                // mov op1 -> rax
                                let rax = x86_64::RAX.clone();
                                let op1 = &ops[op1];
                                if self.match_ireg(op1) {
310
                                    let reg_op1 = self.emit_ireg(op1, f_content, f_context, vm);
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
                                    
                                    self.backend.emit_mov_r64_r64(&rax, &reg_op1);
                                } else if self.match_iimm(op1) {
                                    let imm_op1 = self.emit_get_iimm(op1);
                                    
                                    self.backend.emit_mov_r64_imm32(&rax, imm_op1);
                                } else if self.match_mem(op1) {
                                    let mem_op1 = self.emit_mem(op1);
                                    
                                    self.backend.emit_mov_r64_mem64(&rax, &mem_op1);
                                } else {
                                    unimplemented!();
                                }
                                
                                // mul op2 -> rax
                                let op2 = &ops[op2];
                                if self.match_ireg(op2) {
328
                                    let reg_op2 = self.emit_ireg(op2, f_content, f_context, vm);
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
                                    
                                    self.backend.emit_mul_r64(&reg_op2);
                                } else if self.match_iimm(op2) {
                                    let imm_op2 = self.emit_get_iimm(op2);
                                    
                                    // put imm in a temporary
                                    // here we use result reg as temporary
                                    let res_tmp = self.emit_get_result(node);
                                    self.backend.emit_mov_r64_imm32(&res_tmp, imm_op2);
                                    
                                    self.backend.emit_mul_r64(&res_tmp);
                                } else if self.match_mem(op2) {
                                    let mem_op2 = self.emit_mem(op2);
                                    
                                    self.backend.emit_mul_mem64(&mem_op2);
                                } else {
                                    unimplemented!();
                                }
                                
                                // mov rax -> result
                                let res_tmp = self.emit_get_result(node);
                                self.backend.emit_mov_r64_r64(&res_tmp, &rax);
351
352
353
                            },
                            
                            _ => unimplemented!()
354
355
                        }
                    }
356
                    
357
358
                    // load on x64 generates mov inst (no matter what order is specified)
                    // https://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html
359
                    Instruction_::Load{is_ptr, order, mem_loc} => {
qinsoon's avatar
qinsoon committed
360
                        let ops = inst.ops.read().unwrap();
361
                        let ref loc_op = ops[mem_loc];
362
363
364
365
366
367
368
369
370
                        
                        // check order
                        match order {
                            MemoryOrder::Relaxed 
                            | MemoryOrder::Consume 
                            | MemoryOrder::Acquire
                            | MemoryOrder::SeqCst => {},
                            _ => panic!("didnt expect order {:?} with store inst", order)
                        }                        
371
372
373
374
375
376

                        let resolved_loc = self.emit_get_mem(loc_op, vm);                        
                        let res_temp = self.emit_get_result(node);
                        
                        if self.match_ireg(node) {
                            // emit mov(GPR)
377
378
379
380
381
382
383
384
                            self.backend.emit_mov_r64_mem64(&res_temp, &resolved_loc);
                        } else {
                            // emit mov(FPR)
                            unimplemented!()
                        }
                    }
                    
                    Instruction_::Store{is_ptr, order, mem_loc, value} => {
qinsoon's avatar
qinsoon committed
385
                        let ops = inst.ops.read().unwrap();
386
387
388
389
390
391
392
393
394
395
396
397
398
399
                        let ref loc_op = ops[mem_loc];
                        let ref val_op = ops[value];
                        
                        let generate_plain_mov : bool = {
                            match order {
                                MemoryOrder::Relaxed | MemoryOrder::Release => true,
                                MemoryOrder::SeqCst => false,
                                _ => panic!("didnt expect order {:?} with store inst", order)
                            }
                        };
                        
                        let resolved_loc = self.emit_get_mem(loc_op, vm);
                        
                        if self.match_ireg(val_op) {
400
                            let val = self.emit_ireg(val_op, f_content, f_context, vm);
401
402
403
404
405
406
407
408
409
410
411
412
                            if generate_plain_mov {
                                self.backend.emit_mov_mem64_r64(&resolved_loc, &val);
                            } else {
                                unimplemented!()
                            }
                        } else if self.match_iimm(val_op) {
                            let val = self.emit_get_iimm(val_op);
                            if generate_plain_mov {
                                self.backend.emit_mov_mem64_imm32(&resolved_loc, val);
                            } else {
                                unimplemented!()
                            }
413
414
415
416
417
                        } else {
                            // emit mov(FPR)
                            unimplemented!()
                        }
                    }
418
419
                    
                    Instruction_::ThreadExit => {
420
                        // emit a call to swap_back_to_native_stack(sp_loc: Address)
421
422
423
424
425
426
                        
                        // get thread local and add offset to get sp_loc
                        let tl = self.emit_get_threadlocal(f_content, f_context, vm);
                        self.backend.emit_add_r64_imm32(&tl, *thread::NATIVE_SP_LOC_OFFSET as u32);
                        
                        self.emit_runtime_entry(&entrypoints::SWAP_BACK_TO_NATIVE_STACK, vec![tl.clone()], f_content, f_context, vm);
427
                    }
qinsoon's avatar
qinsoon committed
428
429
430
431
432
433
434
435
436
437
438
439
440
441
                    
                    Instruction_::New(ref ty) => {
                        let ty_info = vm.get_backend_type_info(ty.id());
                        
                        if ty_info.size > mm::LARGE_OBJECT_THRESHOLD {
                            // emit large object allocation
                            unimplemented!()
                        } else {
                            // emit immix allocation
                            
                            // get allocator
                            let tl = self.emit_get_threadlocal(f_content, f_context, vm);
                        }
                    }
442
443
444
445
446
447
    
                    _ => unimplemented!()
                } // main switch
            },
            
            TreeNode_::Value(ref p) => {
448
        
449
450
451
452
            }
        }
    }
    
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
    fn emit_get_threadlocal (&mut self, f_content: &FunctionContent, f_context: &mut FunctionContext, vm: &VM) -> P<Value> {
        let mut rets = self.emit_runtime_entry(&entrypoints::GET_THREAD_LOCAL, vec![], f_content, f_context, vm);
        
        rets.pop().unwrap()
    }
    
    fn emit_runtime_entry (&mut self, entry: &RuntimeEntrypoint, args: Vec<P<Value>>, f_content: &FunctionContent, f_context: &mut FunctionContext, vm: &VM) -> Vec<P<Value>> {
        let sig = entry.sig.clone();
        
        let entry_name = {
            if vm.is_running() {
                unimplemented!()
            } else {
                let ref entry_loc = entry.aot;
                
                match entry_loc {
                    &ValueLocation::Relocatable(_, ref name) => name.clone(),
                    _ => panic!("expecting a relocatable value")
                }
            }
        };
        
        self.emit_c_call(entry_name, sig, args, None, f_content, f_context, vm)
    }
    
478
    #[allow(unused_variables)]
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
    fn emit_c_call (&mut self, func_name: CName, sig: P<CFuncSig>, args: Vec<P<Value>>, rets: Option<Vec<P<Value>>>, 
        f_content: &FunctionContent, f_context: &mut FunctionContext, vm: &VM) -> Vec<P<Value>> 
    {
        let mut gpr_arg_count = 0;
        for arg in args.iter() {
            if arg.is_int_reg() {
                if gpr_arg_count < x86_64::ARGUMENT_GPRs.len() {
                    self.backend.emit_mov_r64_r64(&x86_64::ARGUMENT_GPRs[gpr_arg_count], &arg);
                    gpr_arg_count += 1;
                } else {
                    // use stack to pass argument
                    unimplemented!()
                }
            } else if arg.is_int_const() {
                if x86_64::is_valid_x86_imm(arg) {                
                    let int_const = arg.extract_int_const() as u32;
                    
                    if gpr_arg_count < x86_64::ARGUMENT_GPRs.len() {
                        self.backend.emit_mov_r64_imm32(&x86_64::ARGUMENT_GPRs[gpr_arg_count], int_const);
                        gpr_arg_count += 1;
                    } else {
                        // use stack to pass argument
                        unimplemented!()
                    }
                } else {
                    // put the constant to memory
                    unimplemented!()
                }
            } else {
                // floating point
                unimplemented!()
            }
        }
        
        // make call
        if vm.is_running() {
            unimplemented!()
        } else {
            self.backend.emit_call_near_rel32(func_name);
        }
        
        // deal with ret vals
        let mut return_vals = vec![];
        
        let mut gpr_ret_count = 0;
        for ret_index in 0..sig.ret_tys.len() {
            let ref ty = sig.ret_tys[ret_index];
            
            let ret_val = match rets {
                Some(ref rets) => rets[ret_index].clone(),
                None => {
                    let tmp_node = f_context.make_temporary(vm.next_id(), ty.clone());
                    tmp_node.clone_value()
                }
            };
            
            if ret_val.is_int_reg() {
                if gpr_ret_count < x86_64::RETURN_GPRs.len() {
                    self.backend.emit_mov_r64_r64(&ret_val, &x86_64::RETURN_GPRs[gpr_ret_count]);
                    gpr_ret_count += 1;
                } else {
                    // get return value by stack
                    unimplemented!()
                }
            } else {
                // floating point register
                unimplemented!()
            }
            
            return_vals.push(ret_val);            
        }
        
        return_vals
    }
    
    #[allow(unused_variables)]
    fn process_dest(&mut self, ops: &Vec<P<TreeNode>>, dest: &Destination, f_content: &FunctionContent, f_context: &mut FunctionContext, vm: &VM) {
556
557
        for i in 0..dest.args.len() {
            let ref dest_arg = dest.args[i];
558
559
            match dest_arg {
                &DestArg::Normal(op_index) => {
qinsoon's avatar
qinsoon committed
560
                    let ref arg = ops[op_index];
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
//                    match arg.op {
//                        OpCode::RegI64 
//                        | OpCode::RegFP
//                        | OpCode::IntImmI64
//                        | OpCode::FPImm => {
//                            // do nothing
//                        },
//                        _ => {
//                            trace!("nested: compute arg for branch");
//                            // nested: compute arg
//                            self.instruction_select(arg, cur_func);
//                            
//                            self.emit_get_result(arg);
//                        }
//                    }
//                    
577
                    let ref target_args = f_content.get_block(dest.target).content.as_ref().unwrap().args;
578
579
                    let ref target_arg = target_args[i];
                    
580
                    self.emit_general_move(&arg, target_arg, f_content, f_context, vm);
581
582
583
584
                },
                &DestArg::Freshbound(_) => unimplemented!()
            }
        }
qinsoon's avatar
qinsoon committed
585
586
    }
    
587
    fn emit_common_prologue(&mut self, args: &Vec<P<Value>>) {
588
589
        let block_name = "prologue".to_string();
        self.backend.start_block(block_name.clone());
590
591
592
        
        // no livein
        // liveout = entry block's args
593
594
        self.backend.set_block_livein(block_name.clone(), &vec![]);
        self.backend.set_block_liveout(block_name.clone(), args);
qinsoon's avatar
qinsoon committed
595
        
596
597
598
        // push rbp
        self.backend.emit_push_r64(&x86_64::RBP);
        // mov rsp -> rbp
qinsoon's avatar
qinsoon committed
599
        self.backend.emit_mov_r64_r64(&x86_64::RBP, &x86_64::RSP);
600
        
601
        // push all callee-saved registers
602
603
604
605
606
607
        for i in 0..x86_64::CALLEE_SAVED_GPRs.len() {
            let ref reg = x86_64::CALLEE_SAVED_GPRs[i];
            // not pushing rbp (as we have done taht)
            if reg.extract_ssa_id().unwrap() != x86_64::RBP.extract_ssa_id().unwrap() {
                self.backend.emit_push_r64(&reg);
            }
608
609
610
611
        }
        
        // unload arguments
        let mut gpr_arg_count = 0;
612
        // TODO: let mut fpr_arg_count = 0;
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
        for arg in args {
            if arg.is_int_reg() {
                if gpr_arg_count < x86_64::ARGUMENT_GPRs.len() {
                    self.backend.emit_mov_r64_r64(&arg, &x86_64::ARGUMENT_GPRs[gpr_arg_count]);
                    gpr_arg_count += 1;
                } else {
                    // unload from stack
                    unimplemented!();
                }
            } else if arg.is_fp_reg() {
                unimplemented!();
            } else {
                panic!("expect an arg value to be either int reg or fp reg");
            }
        }
628
629
        
        self.backend.end_block(block_name);
630
631
    }
    
632
    fn emit_common_epilogue(&mut self, ret_inst: &Instruction, f_content: &FunctionContent, f_context: &mut FunctionContext, vm: &VM) {
633
634
        // epilogue is not a block (its a few instruction inserted before return)
        // FIXME: this may change in the future
635
        
636
        // prepare return regs
qinsoon's avatar
qinsoon committed
637
        let ref ops = ret_inst.ops.read().unwrap();
638
639
640
641
642
643
        let ret_val_indices = match ret_inst.v {
            Instruction_::Return(ref vals) => vals,
            _ => panic!("expected ret inst")
        };
        
        let mut gpr_ret_count = 0;
644
        // TODO: let mut fpr_ret_count = 0;
645
646
647
        for i in ret_val_indices {
            let ref ret_val = ops[*i];
            if self.match_ireg(ret_val) {
648
                let reg_ret_val = self.emit_ireg(ret_val, f_content, f_context, vm);
649
650
651
652
653
654
655
656
657
658
659
                
                self.backend.emit_mov_r64_r64(&x86_64::RETURN_GPRs[gpr_ret_count], &reg_ret_val);
                gpr_ret_count += 1;
            } else if self.match_iimm(ret_val) {
                let imm_ret_val = self.emit_get_iimm(ret_val);
                
                self.backend.emit_mov_r64_imm32(&x86_64::RETURN_GPRs[gpr_ret_count], imm_ret_val);
                gpr_ret_count += 1;
            } else {
                unimplemented!();
            }
660
661
662
663
664
665
666
667
        }        
        
        // pop all callee-saved registers - reverse order
        for i in (0..x86_64::CALLEE_SAVED_GPRs.len()).rev() {
            let ref reg = x86_64::CALLEE_SAVED_GPRs[i];
            if reg.extract_ssa_id().unwrap() != x86_64::RBP.extract_ssa_id().unwrap() {
                self.backend.emit_pop_r64(&reg);
            }
668
        }
669
670
671
        
        // pop rbp
        self.backend.emit_pop_r64(&x86_64::RBP);
672
673
    }
    
qinsoon's avatar
qinsoon committed
674
675
676
677
678
679
680
681
682
683
684
685
    fn match_cmp_res(&mut self, op: &P<TreeNode>) -> bool {
        match op.v {
            TreeNode_::Instruction(ref inst) => {
                match inst.v {
                    Instruction_::CmpOp(_, _, _) => true,
                    _ => false
                }
            }
            TreeNode_::Value(_) => false
        }
    }
    
686
    fn emit_cmp_res(&mut self, cond: &P<TreeNode>, f_content: &FunctionContent, f_context: &mut FunctionContext, vm: &VM) -> op::CmpOp {
qinsoon's avatar
qinsoon committed
687
688
        match cond.v {
            TreeNode_::Instruction(ref inst) => {
qinsoon's avatar
qinsoon committed
689
                let ops = inst.ops.read().unwrap();                
qinsoon's avatar
qinsoon committed
690
691
692
693
694
695
696
697
                
                match inst.v {
                    Instruction_::CmpOp(op, op1, op2) => {
                        let op1 = &ops[op1];
                        let op2 = &ops[op2];
                        
                        if op::is_int_cmp(op) {                        
                            if self.match_ireg(op1) && self.match_ireg(op2) {
698
699
                                let reg_op1 = self.emit_ireg(op1, f_content, f_context, vm);
                                let reg_op2 = self.emit_ireg(op2, f_content, f_context, vm);
qinsoon's avatar
qinsoon committed
700
701
702
                                
                                self.backend.emit_cmp_r64_r64(&reg_op1, &reg_op2);
                            } else if self.match_ireg(op1) && self.match_iimm(op2) {
703
                                let reg_op1 = self.emit_ireg(op1, f_content, f_context, vm);
qinsoon's avatar
qinsoon committed
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
                                let iimm_op2 = self.emit_get_iimm(op2);
                                
                                self.backend.emit_cmp_r64_imm32(&reg_op1, iimm_op2);
                            } else {
                                unimplemented!()
                            }
                        } else {
                            unimplemented!()
                        }
                        
                        op
                    }
                    
                    _ => panic!("expect cmp res to emit")
                }
            }
            _ => panic!("expect cmp res to emit")
        }
    }    
    
qinsoon's avatar
qinsoon committed
724
    fn match_ireg(&mut self, op: &TreeNode) -> bool {
qinsoon's avatar
qinsoon committed
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
        match op.v {
            TreeNode_::Instruction(ref inst) => {
                if inst.value.is_some() {
                    if inst.value.as_ref().unwrap().len() > 1 {
                        return false;
                    }
                    
                    let ref value = inst.value.as_ref().unwrap()[0];
                    
                    if types::is_scalar(&value.ty) {
                        true
                    } else {
                        false
                    }
                } else {
                    false
                }
            }
            
            TreeNode_::Value(ref pv) => {
                pv.is_int_reg()
            }
        }
    }
    
750
    fn emit_ireg(&mut self, op: &P<TreeNode>, f_content: &FunctionContent, f_context: &mut FunctionContext, vm: &VM) -> P<Value> {
qinsoon's avatar
qinsoon committed
751
752
        match op.v {
            TreeNode_::Instruction(_) => {
753
                self.instruction_select(op, f_content, f_context, vm);
qinsoon's avatar
qinsoon committed
754
755
756
757
758
                
                self.emit_get_result(op)
            },
            TreeNode_::Value(ref pv) => {
                match pv.v {
759
                    Value_::Constant(_)
760
                    | Value_::Global(_)
761
                    | Value_::Memory(_) => panic!("expected ireg"),
qinsoon's avatar
qinsoon committed
762
763
                    Value_::SSAVar(_) => {
                        pv.clone()
qinsoon's avatar
qinsoon committed
764
                    },
qinsoon's avatar
qinsoon committed
765
766
767
768
769
                }
            }
        }
    }
    
770
    #[allow(unused_variables)]
771
772
773
774
    fn match_fpreg(&mut self, op: &P<TreeNode>) -> bool {
        unimplemented!()
    }
    
qinsoon's avatar
qinsoon committed
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
    fn match_iimm(&mut self, op: &P<TreeNode>) -> bool {
        match op.v {
            TreeNode_::Value(ref pv) if x86_64::is_valid_x86_imm(pv) => true,
            _ => false
        }
    }
    
    fn emit_get_iimm(&mut self, op: &P<TreeNode>) -> u32 {
        match op.v {
            TreeNode_::Value(ref pv) => {
                match pv.v {
                    Value_::Constant(Constant::Int(val)) => {
                        val as u32
                    },
                    _ => panic!("expected iimm")
                }
            },
            _ => panic!("expected iimm")
        }
    }
    
qinsoon's avatar
qinsoon committed
796
    fn emit_get_mem(&mut self, op: &P<TreeNode>, vm: &VM) -> P<Value> {
797
798
799
800
        match op.v {
            TreeNode_::Value(ref pv) => {
                match pv.v {
                    Value_::SSAVar(_) => P(Value{
801
                        hdr: MuEntityHeader::unnamed(vm.next_id()),
802
803
804
805
806
807
808
809
                        ty: types::get_referent_ty(& pv.ty).unwrap(),
                        v: Value_::Memory(MemoryLocation::Address{
                            base: pv.clone(),
                            offset: None,
                            index: None,
                            scale: None
                        })
                    }),
810
                    Value_::Global(_) => {
811
812
813
814
815
816
                        if vm.is_running() {
                            // get address from vm
                            unimplemented!()
                        } else {
                            // symbolic
                            P(Value{
817
                                hdr: MuEntityHeader::unnamed(vm.next_id()),
818
819
820
                                ty: types::get_referent_ty(&pv.ty).unwrap(),
                                v: Value_::Memory(MemoryLocation::Symbolic{
                                    base: Some(x86_64::RIP.clone()),
821
                                    label: pv.name().unwrap()
822
823
824
825
826
827
828
829
830
831
832
833
                                })
                            })
                        }
                    },
                    Value_::Memory(_) => pv.clone(),
                    Value_::Constant(_) => unimplemented!()
                }
            }
            TreeNode_::Instruction(_) => unimplemented!()
        }
    }
    
834
835
836
837
838
839
840
841
842
843
844
845
846
    fn match_funcref_const(&mut self, op: &P<TreeNode>) -> bool {
        match op.v {
            TreeNode_::Value(ref pv) => {
                match pv.v {
                    Value_::Constant(Constant::FuncRef(_)) => true,
                    Value_::Constant(Constant::UFuncRef(_)) => true,
                    _ => false
                }
            },
            _ => false 
        }
    }
    
qinsoon's avatar
qinsoon committed
847
    fn emit_get_funcref_const(&mut self, op: &P<TreeNode>) -> MuID {
848
849
850
        match op.v {
            TreeNode_::Value(ref pv) => {
                match pv.v {
qinsoon's avatar
qinsoon committed
851
852
                    Value_::Constant(Constant::FuncRef(id))
                    | Value_::Constant(Constant::UFuncRef(id)) => id,
853
854
855
856
857
858
859
                    _ => panic!("expected a (u)funcref const")
                }
            },
            _ => panic!("expected a (u)funcref const")
        }
    }
    
860
    #[allow(unused_variables)]
861
862
863
864
    fn match_mem(&mut self, op: &P<TreeNode>) -> bool {
        unimplemented!()
    }
    
865
    #[allow(unused_variables)]
866
867
868
869
    fn emit_mem(&mut self, op: &P<TreeNode>) -> P<Value> {
        unimplemented!()
    }
    
qinsoon's avatar
qinsoon committed
870
    fn emit_get_result(&mut self, node: &TreeNode) -> P<Value> {
qinsoon's avatar
qinsoon committed
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
        match node.v {
            TreeNode_::Instruction(ref inst) => {
                if inst.value.is_some() {
                    if inst.value.as_ref().unwrap().len() > 1 {
                        panic!("expected ONE result from the node {}", node);
                    }
                    
                    let ref value = inst.value.as_ref().unwrap()[0];
                    
                    value.clone()
                } else {
                    panic!("expected result from the node {}", node);
                }
            }
            
            TreeNode_::Value(ref pv) => {
                pv.clone()
            }
        }
890
891
    }
    
892
    fn emit_general_move(&mut self, src: &P<TreeNode>, dest: &P<Value>, f_content: &FunctionContent, f_context: &mut FunctionContext, vm: &VM) {
893
894
895
896
        let ref dst_ty = dest.ty;
        
        if !types::is_fp(dst_ty) && types::is_scalar(dst_ty) {
            if self.match_ireg(src) {
897
                let src_reg = self.emit_ireg(src, f_content, f_context, vm);
898
899
900
901
902
903
904
905
906
907
908
909
910
                self.backend.emit_mov_r64_r64(dest, &src_reg);
            } else if self.match_iimm(src) {
                let src_imm = self.emit_get_iimm(src);
                self.backend.emit_mov_r64_imm32(dest, src_imm);
            } else {
                panic!("expected an int type op");
            }
        } else if !types::is_fp(dst_ty) && types::is_scalar(dst_ty) {
            unimplemented!()
        } else {
            panic!("unexpected type for move");
        } 
    }
911
}
912

913
914
915
impl CompilerPass for InstructionSelection {
    fn name(&self) -> &'static str {
        self.name
916
    }
917

918
    #[allow(unused_variables)]
qinsoon's avatar
qinsoon committed
919
    fn start_function(&mut self, vm: &VM, func_ver: &mut MuFunctionVersion) {
920
        debug!("{}", self.name());
qinsoon's avatar
qinsoon committed
921
        
qinsoon's avatar
qinsoon committed
922
        let funcs = vm.funcs().read().unwrap();
qinsoon's avatar
qinsoon committed
923
        let func = funcs.get(&func_ver.func_id).unwrap().read().unwrap();
qinsoon's avatar
qinsoon committed
924
        self.backend.start_code(func.name().unwrap());
qinsoon's avatar
qinsoon committed
925
926
        
        // prologue (get arguments from entry block first)        
qinsoon's avatar
qinsoon committed
927
        let entry_block = func_ver.content.as_ref().unwrap().get_entry_block();
928
929
        let ref args = entry_block.content.as_ref().unwrap().args;
        self.emit_common_prologue(args);
930
931
932
    }

    #[allow(unused_variables)]
qinsoon's avatar
qinsoon committed
933
    fn visit_function(&mut self, vm: &VM, func: &mut MuFunctionVersion) {
934
935
        let f_content = func.content.as_ref().unwrap();
        
qinsoon's avatar
qinsoon committed
936
        for block_id in func.block_trace.as_ref().unwrap() {
937
            let block = f_content.get_block(*block_id);
938
            let block_label = block.name().unwrap();
qinsoon's avatar
qinsoon committed
939
            
940
            self.backend.start_block(block_label.clone());
941

942
            let block_content = block.content.as_ref().unwrap();
943
944
            
            // live in is args of the block
945
            self.backend.set_block_livein(block_label.clone(), &block_content.args);
946
947
948
            
            // live out is the union of all branch args of this block
            let live_out = block_content.get_out_arguments();
949
            self.backend.set_block_liveout(block_label.clone(), &live_out);
950

951
            for inst in block_content.body.iter() {
952
                self.instruction_select(&inst, f_content, &mut func.context, vm);
953
            }
954
            
qinsoon's avatar
qinsoon committed
955
            self.backend.end_block(block_label);
956
957
        }
    }
qinsoon's avatar
qinsoon committed
958
959
    
    #[allow(unused_variables)]
qinsoon's avatar
qinsoon committed
960
    fn finish_function(&mut self, vm: &VM, func: &mut MuFunctionVersion) {
qinsoon's avatar
qinsoon committed
961
962
        self.backend.print_cur_code();
        
963
964
        let mc = self.backend.finish_code();
        let compiled_func = CompiledFunction {
qinsoon's avatar
qinsoon committed
965
            func_id: func.func_id,
966
            func_ver_id: func.id(),
967
            temps: HashMap::new(),
968
969
970
            mc: mc
        };
        
qinsoon's avatar
qinsoon committed
971
        vm.add_compiled_func(compiled_func);
qinsoon's avatar
qinsoon committed
972
    }
973
}