asm_backend.rs 89.1 KB
Newer Older
1 2
#![allow(unused_variables)]

3
use compiler::backend::AOT_EMIT_CONTEXT_FILE;
qinsoon's avatar
qinsoon committed
4
use compiler::backend::RegGroup;
qinsoon's avatar
qinsoon committed
5
use utils::ByteSize;
6
use compiler::backend::x86_64;
7
use compiler::backend::x86_64::CodeGenerator;
qinsoon's avatar
qinsoon committed
8
use compiler::backend::{Reg, Mem};
qinsoon's avatar
qinsoon committed
9
use compiler::backend::x86_64::check_op_len;
qinsoon's avatar
qinsoon committed
10
use compiler::machine_code::MachineCode;
qinsoon's avatar
qinsoon committed
11
use vm::VM;
qinsoon's avatar
qinsoon committed
12
use runtime::ValueLocation;
13

qinsoon's avatar
qinsoon committed
14
use utils::vec_utils;
15
use utils::string_utils;
16 17
use ast::ptr::P;
use ast::ir::*;
qinsoon's avatar
qinsoon committed
18
use ast::types::*;
19

20 21
use std::collections::HashMap;
use std::str;
22
use std::usize;
23
use std::slice::Iter;
24
use std::ops;
25 26

struct ASMCode {
qinsoon's avatar
qinsoon committed
27
    name: MuName, 
qinsoon's avatar
qinsoon committed
28
    code: Vec<ASMInst>,
qinsoon's avatar
qinsoon committed
29

qinsoon's avatar
qinsoon committed
30 31 32
    blocks: HashMap<MuName, ASMBlock>,

    frame_size_patchpoints: Vec<ASMLocation>
33 34
}

qinsoon's avatar
qinsoon committed
35 36 37
unsafe impl Send for ASMCode {} 
unsafe impl Sync for ASMCode {}

38
impl ASMCode {
qinsoon's avatar
qinsoon committed
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
    fn get_use_locations(&self, reg: MuID) -> Vec<ASMLocation> {
        let mut ret = vec![];

        for inst in self.code.iter() {
            match inst.uses.get(&reg) {
                Some(ref locs) => {
                    ret.append(&mut locs.to_vec());
                },
                None => {}
            }
        }

        ret
    }

    fn get_define_locations(&self, reg: MuID) -> Vec<ASMLocation> {
        let mut ret = vec![];

        for inst in self.code.iter() {
            match inst.defines.get(&reg) {
                Some(ref locs) => {
                    ret.append(&mut locs.to_vec());
                },
                None => {}
            }
        }

        ret
    }

qinsoon's avatar
qinsoon committed
69 70 71 72 73 74 75 76 77
    fn is_block_start(&self, inst: usize) -> bool {
        for block in self.blocks.values() {
            if block.start_inst == inst {
                return true;
            }
        }
        false
    }

78
    fn is_last_inst_in_block(&self, inst: usize) -> bool {
qinsoon's avatar
qinsoon committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
        for block in self.blocks.values() {
            if block.end_inst == inst + 1 {
                return true;
            }
        }
        false
    }

    fn get_block_by_inst(&self, inst: usize) -> (&String, &ASMBlock) {
        for (name, block) in self.blocks.iter() {
            if inst >= block.start_inst && inst < block.end_inst {
                return (name, block);
            }
        }

        panic!("didnt find any block for inst {}", inst)
    }

qinsoon's avatar
qinsoon committed
97 98 99 100 101 102 103 104 105 106
    fn get_block_by_start_inst(&self, inst: usize) -> Option<&ASMBlock> {
        for block in self.blocks.values() {
            if block.start_inst == inst {
                return Some(block);
            }
        }

        None
    }

107 108 109 110 111
    fn rewrite_insert(
        &self,
        insert_before: HashMap<usize, Vec<Box<ASMCode>>>,
        insert_after: HashMap<usize, Vec<Box<ASMCode>>>) -> Box<ASMCode>
    {
112
        trace!("insert spilling code");
113 114 115
        let mut ret = ASMCode {
            name: self.name.clone(),
            code: vec![],
qinsoon's avatar
qinsoon committed
116
            blocks: hashmap!{},
qinsoon's avatar
qinsoon committed
117
            frame_size_patchpoints: vec![]
118 119 120 121
        };

        // iterate through old machine code
        let mut inst_offset = 0;    // how many instructions has been inserted
qinsoon's avatar
qinsoon committed
122
        let mut cur_block_start = usize::MAX;
123

qinsoon's avatar
qinsoon committed
124 125 126 127
        // inst N in old machine code is N' in new machine code
        // this map stores the relationship
        let mut location_map : HashMap<usize, usize> = HashMap::new();

128
        for i in 0..self.number_of_insts() {
129 130
            trace!("Inst{}", i);

qinsoon's avatar
qinsoon committed
131 132
            if self.is_block_start(i) {
                cur_block_start = i + inst_offset;
133
                trace!("  block start is shifted to {}", cur_block_start);
qinsoon's avatar
qinsoon committed
134 135
            }

136 137 138 139 140
            // insert code before this instruction
            if insert_before.contains_key(&i) {
                for insert in insert_before.get(&i).unwrap() {
                    ret.append_code_sequence_all(insert);
                    inst_offset += insert.number_of_insts();
141
                    trace!("  inserted {} insts before", insert.number_of_insts());
142 143 144 145
                }
            }

            // copy this instruction
qinsoon's avatar
qinsoon committed
146 147
            let mut inst = self.code[i].clone();

qinsoon's avatar
qinsoon committed
148 149
            // old ith inst is now the (i + inst_offset)th instruction
            location_map.insert(i, i + inst_offset);
150
            trace!("  Inst{} is now Inst{}", i, i + inst_offset);
qinsoon's avatar
qinsoon committed
151

qinsoon's avatar
qinsoon committed
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
            // this instruction has been offset by several instructions('inst_offset')
            // update its info
            // 1. fix defines and uses
            for locs in inst.defines.values_mut() {
                for loc in locs {
                    debug_assert!(loc.line == i);
                    loc.line += inst_offset;
                }
            }
            for locs in inst.uses.values_mut() {
                for loc in locs {
                    debug_assert!(loc.line == i);
                    loc.line += inst_offset;
                }
            }
qinsoon's avatar
qinsoon committed
167 168 169
            // 2. we need to delete existing preds/succs - CFA is required later
            inst.preds.clear();
            inst.succs.clear();
qinsoon's avatar
qinsoon committed
170 171
            // 3. add the inst
            ret.code.push(inst);
172 173 174 175


            // insert code after this instruction
            if insert_after.contains_key(&i) {
qinsoon's avatar
qinsoon committed
176 177 178
                for insert in insert_after.get(&i).unwrap() {
                    ret.append_code_sequence_all(insert);
                    inst_offset += insert.number_of_insts();
179
                    trace!("  inserted {} insts after", insert.number_of_insts());
qinsoon's avatar
qinsoon committed
180 181 182
                }
            }

183 184
            if self.is_last_inst_in_block(i) {
                let cur_block_end = i + 1 + inst_offset;
185

qinsoon's avatar
qinsoon committed
186 187 188
                // copy the block
                let (name, block) = self.get_block_by_inst(i);

qinsoon's avatar
qinsoon committed
189
                let new_block = ASMBlock{
190 191 192 193 194 195 196 197 198 199
                    start_inst: cur_block_start,
                    end_inst: cur_block_end,

                    livein: vec![],
                    liveout: vec![]
                };

                trace!("  old block: {:?}", block);
                trace!("  new block: {:?}", new_block);

qinsoon's avatar
qinsoon committed
200 201 202 203
                cur_block_start = usize::MAX;

                // add to the new code
                ret.blocks.insert(name.clone(), new_block);
204 205 206
            }
        }

qinsoon's avatar
qinsoon committed
207 208 209 210 211
        // fix patchpoint
        for patchpoint in self.frame_size_patchpoints.iter() {
            let new_patchpoint = ASMLocation {
                line: *location_map.get(&patchpoint.line).unwrap(),
                index: patchpoint.index,
qinsoon's avatar
qinsoon committed
212 213
                len: patchpoint.len,
                oplen: patchpoint.oplen
qinsoon's avatar
qinsoon committed
214 215 216 217 218
            };

            ret.frame_size_patchpoints.push(new_patchpoint);
        }

qinsoon's avatar
qinsoon committed
219 220 221
        ret.control_flow_analysis();

        Box::new(ret)
222 223 224 225 226 227 228 229
    }

    fn append_code_sequence(
        &mut self,
        another: &Box<ASMCode>,
        start_inst: usize,
        n_insts: usize)
    {
qinsoon's avatar
qinsoon committed
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
        let base_line = self.number_of_insts();

        for i in 0..n_insts {
            let cur_line_in_self = base_line + i;
            let cur_line_from_copy = start_inst + i;

            let mut inst = another.code[cur_line_from_copy].clone();

            // fix info
            for locs in inst.defines.values_mut() {
                for loc in locs {
                    debug_assert!(loc.line == i);
                    loc.line = cur_line_in_self;
                }
            }
            for locs in inst.uses.values_mut() {
                for loc in locs {
                    debug_assert!(loc.line == i);
                    loc.line = cur_line_in_self;
                }
            }
            // ignore preds/succs

            // add to self
            self.code.push(inst);
        }
256 257 258 259 260 261
    }

    fn append_code_sequence_all(&mut self, another: &Box<ASMCode>) {
        let n_insts = another.number_of_insts();
        self.append_code_sequence(another, 0, n_insts)
    }
qinsoon's avatar
qinsoon committed
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280

    fn control_flow_analysis(&mut self) {
        const TRACE_CFA : bool = false;

        // control flow analysis
        let n_insts = self.number_of_insts();

        let ref blocks = self.blocks;
        let ref mut asm = self.code;

        let block_start = {
            let mut ret = vec![];
            for block in blocks.values() {
                ret.push(block.start_inst);
            }
            ret
        };

        for i in 0..n_insts {
qinsoon's avatar
[wip]  
qinsoon committed
281 282 283
            if TRACE_CFA {
                trace!("---inst {}---", i);
            }
qinsoon's avatar
qinsoon committed
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
            // determine predecessor - if cur is not block start, its predecessor is previous insts
            let is_block_start = block_start.contains(&i);
            if !is_block_start {
                if i > 0 {
                    if TRACE_CFA {
                        trace!("inst {}: not a block start", i);
                        trace!("inst {}: set PREDS as previous inst {}", i, i - 1);
                    }
                    asm[i].preds.push(i - 1);
                }
            } else {
                // if cur is a branch target, we already set its predecessor
                // if cur is a fall-through block, we set it in a sanity check pass
            }

            // determine successor
            let branch = asm[i].branch.clone();
            match branch {
                ASMBranchTarget::Unconditional(ref target) => {
                    // branch to target
                    let target_n = self.blocks.get(target).unwrap().start_inst;

                    // cur inst's succ is target
                    asm[i].succs.push(target_n);

                    // target's pred is cur
                    asm[target_n].preds.push(i);

                    if TRACE_CFA {
                        trace!("inst {}: is a branch to {}", i, target);
                        trace!("inst {}: branch target index is {}", i, target_n);
                        trace!("inst {}: set SUCCS as branch target {}", i, target_n);
                        trace!("inst {}: set PREDS as branch source {}", target_n, i);
                    }
                },
                ASMBranchTarget::Conditional(ref target) => {
                    // branch to target
                    let target_n = self.blocks.get(target).unwrap().start_inst;

                    // cur insts' succ is target and next inst
                    asm[i].succs.push(target_n);

                    if TRACE_CFA {
                        trace!("inst {}: is a cond branch to {}", i, target);
                        trace!("inst {}: branch target index is {}", i, target_n);
                        trace!("inst {}: set SUCCS as branch target {}", i, target_n);
                    }

                    if i < n_insts - 1 {
                        if TRACE_CFA {
                            trace!("inst {}: set SUCCS as next inst", i + 1);
                        }
                        asm[i].succs.push(i + 1);
                    }

                    // target's pred is cur
                    asm[target_n].preds.push(i);
                    if TRACE_CFA {
                        trace!("inst {}: set PREDS as {}", target_n, i);
                    }
                },
qinsoon's avatar
[wip]  
qinsoon committed
345 346 347 348 349 350
                ASMBranchTarget::Return => {
                    if TRACE_CFA {
                        trace!("inst {}: is a return", i);
                        trace!("inst {}: has no successor", i);
                    }
                }
qinsoon's avatar
qinsoon committed
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
                ASMBranchTarget::None => {
                    // not branch nor cond branch, succ is next inst
                    if TRACE_CFA {
                        trace!("inst {}: not a branch inst", i);
                    }
                    if i < n_insts - 1 {
                        if TRACE_CFA {
                            trace!("inst {}: set SUCCS as next inst {}", i, i + 1);
                        }
                        asm[i].succs.push(i + 1);
                    }
                }
            }
        }

        // a sanity check for fallthrough blocks
        for i in 0..n_insts {
            if i != 0 && asm[i].preds.len() == 0 {
                asm[i].preds.push(i - 1);
            }
        }
    }
qinsoon's avatar
qinsoon committed
373 374 375 376

    fn add_frame_size_patchpoint(&mut self, patchpoint: ASMLocation) {
        self.frame_size_patchpoints.push(patchpoint);
    }
377 378 379 380
}

use std::any::Any;

381
impl MachineCode for ASMCode {
382 383 384
    fn as_any(&self) -> &Any {
        self
    }
385 386 387
    fn number_of_insts(&self) -> usize {
        self.code.len()
    }
388
    
389 390 391
    fn is_move(&self, index: usize) -> bool {
        let inst = self.code.get(index);
        match inst {
qinsoon's avatar
qinsoon committed
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
            Some(inst) => {
                let ref inst = inst.code;

                if inst.starts_with("movsd") || inst.starts_with("movss") {
                    // floating point move
                    true
                } else if inst.starts_with("movs") || inst.starts_with("movz") {
                    // sign extend, zero extend
                    false
                } else if inst.starts_with("mov") {
                    // normal mov
                    true
                } else {
                    false
                }
            },
408 409 410 411
            None => false
        }
    }
    
qinsoon's avatar
qinsoon committed
412
    fn is_using_mem_op(&self, index: usize) -> bool {
qinsoon's avatar
qinsoon committed
413
        self.code[index].is_mem_op_used
qinsoon's avatar
qinsoon committed
414
    }
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438

    fn is_jmp(&self, index: usize) -> Option<MuName> {
        let inst = self.code.get(index);
        match inst {
            Some(inst) if inst.code.starts_with("jmp") => {
                let split : Vec<&str> = inst.code.split(' ').collect();

                Some(String::from(split[1]))
            }
            _ => None
        }
    }

    fn is_label(&self, index: usize) -> Option<MuName> {
        let inst = self.code.get(index);
        match inst {
            Some(inst) if inst.code.ends_with(':') => {
                let split : Vec<&str> = inst.code.split(':').collect();

                Some(String::from(split[0]))
            }
            _ => None
        }
    }
qinsoon's avatar
qinsoon committed
439
    
qinsoon's avatar
qinsoon committed
440
    fn get_succs(&self, index: usize) -> &Vec<usize> {
qinsoon's avatar
qinsoon committed
441
        &self.code[index].succs
qinsoon's avatar
qinsoon committed
442 443 444
    }
    
    fn get_preds(&self, index: usize) -> &Vec<usize> {
qinsoon's avatar
qinsoon committed
445
        &self.code[index].preds
qinsoon's avatar
qinsoon committed
446 447
    }
    
qinsoon's avatar
qinsoon committed
448 449
    fn get_inst_reg_uses(&self, index: usize) -> Vec<MuID> {
        self.code[index].uses.keys().map(|x| *x).collect()
450 451
    }
    
qinsoon's avatar
qinsoon committed
452 453
    fn get_inst_reg_defines(&self, index: usize) -> Vec<MuID> {
        self.code[index].defines.keys().map(|x| *x).collect()
454
    }
455
    
456
    fn replace_reg(&mut self, from: MuID, to: MuID) {
qinsoon's avatar
qinsoon committed
457 458
        for loc in self.get_define_locations(from) {
            let ref mut inst_to_patch = self.code[loc.line];
qinsoon's avatar
qinsoon committed
459 460 461 462 463 464 465

            // pick the right reg based on length
            let to_reg = x86_64::get_alias_for_length(to, loc.oplen);
            let to_reg_tag = to_reg.name().unwrap();
            let to_reg_string = "%".to_string() + &to_reg_tag;

            string_utils::replace(&mut inst_to_patch.code, loc.index, &to_reg_string, to_reg_string.len());
466
        }
467

qinsoon's avatar
qinsoon committed
468 469
        for loc in self.get_use_locations(from) {
            let ref mut inst_to_patch = self.code[loc.line];
qinsoon's avatar
qinsoon committed
470 471 472 473 474 475 476

            // pick the right reg based on length
            let to_reg = x86_64::get_alias_for_length(to, loc.oplen);
            let to_reg_tag = to_reg.name().unwrap();
            let to_reg_string = "%".to_string() + &to_reg_tag;

            string_utils::replace(&mut inst_to_patch.code, loc.index, &to_reg_string, to_reg_string.len());
477 478
        }
    }
479

480
    fn replace_define_tmp_for_inst(&mut self, from: MuID, to: MuID, inst: usize) {
qinsoon's avatar
qinsoon committed
481
        let to_reg_string : MuName = REG_PLACEHOLDER.clone();
482

qinsoon's avatar
qinsoon committed
483 484 485 486 487 488
        let asm = &mut self.code[inst];
        // if this reg is defined, replace the define
        if asm.defines.contains_key(&from) {
            let define_locs = asm.defines.get(&from).unwrap().to_vec();
            // replace temps
            for loc in define_locs.iter() {
qinsoon's avatar
qinsoon committed
489
                string_utils::replace(&mut asm.code, loc.index, &to_reg_string, to_reg_string.len());
490 491
            }

qinsoon's avatar
qinsoon committed
492 493
            // remove old key, insert new one
            asm.defines.remove(&from);
494
            asm.defines.insert(to, define_locs);
495
        }
496 497 498
    }

    fn replace_use_tmp_for_inst(&mut self, from: MuID, to: MuID, inst: usize) {
qinsoon's avatar
qinsoon committed
499
        let to_reg_string : MuName = REG_PLACEHOLDER.clone();
500 501

        let asm = &mut self.code[inst];
qinsoon's avatar
qinsoon committed
502 503 504 505 506 507

        // if this reg is used, replace the use
        if asm.uses.contains_key(&from) {
            let use_locs = asm.uses.get(&from).unwrap().to_vec();
            // replace temps
            for loc in use_locs.iter() {
qinsoon's avatar
qinsoon committed
508
                string_utils::replace(&mut asm.code, loc.index, &to_reg_string, to_reg_string.len());
509 510
            }

qinsoon's avatar
qinsoon committed
511 512
            // remove old key, insert new one
            asm.uses.remove(&from);
513
            asm.uses.insert(to, use_locs);
514 515
        }
    }
516
    
517 518
    fn set_inst_nop(&mut self, index: usize) {
        self.code.remove(index);
qinsoon's avatar
qinsoon committed
519
        self.code.insert(index, ASMInst::nop());
520
    }
qinsoon's avatar
qinsoon committed
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 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570

    fn remove_unnecessary_callee_saved(&mut self, used_callee_saved: Vec<MuID>) -> Vec<MuID> {
        // we always save rbp
        let rbp = x86_64::RBP.extract_ssa_id().unwrap();
        // every push/pop will use/define rsp
        let rsp = x86_64::RSP.extract_ssa_id().unwrap();

        let find_op_other_than_rsp = |inst: &ASMInst| -> Option<MuID> {
            for id in inst.defines.keys() {
                if *id != rsp && *id != rbp {
                    return Some(*id);
                }
            }
            for id in inst.uses.keys() {
                if *id != rsp && *id != rbp {
                    return Some(*id);
                }
            }

            None
        };

        let mut inst_to_remove = vec![];
        let mut regs_to_remove = vec![];

        for i in 0..self.number_of_insts() {
            let ref inst = self.code[i];

            if inst.code.contains("push") || inst.code.contains("pop") {
                match find_op_other_than_rsp(inst) {
                    Some(op) => {
                        // if this push/pop instruction is about a callee saved register
                        // and the register is not used, we set the instruction as nop
                        if x86_64::is_callee_saved(op) && !used_callee_saved.contains(&op) {
                            trace!("removing instruction {:?} for save/restore unnecessary callee saved regs", inst);
                            regs_to_remove.push(op);
                            inst_to_remove.push(i);
                        }
                    }
                    None => {}
                }
            }
        }

        for i in inst_to_remove {
            self.set_inst_nop(i);
        }

        regs_to_remove
    }
qinsoon's avatar
qinsoon committed
571 572 573 574 575 576 577 578 579 580 581 582

    fn patch_frame_size(&mut self, size: usize) {
        let size = size.to_string();

        debug_assert!(size.len() <= FRAME_SIZE_PLACEHOLDER_LEN);

        for loc in self.frame_size_patchpoints.iter() {
            let ref mut inst = self.code[loc.line];

            string_utils::replace(&mut inst.code, loc.index, &size, size.len());
        }
    }
583
    
584 585 586 587 588 589 590 591 592 593 594
    fn emit(&self) -> Vec<u8> {
        let mut ret = vec![];
        
        for inst in self.code.iter() {
            ret.append(&mut inst.code.clone().into_bytes());
            ret.append(&mut "\n".to_string().into_bytes());
        }
        
        ret
    }
    
595 596
    fn trace_mc(&self) {
        trace!("");
597

598 599
        trace!("code for {}: \n", self.name);
        
600 601
        let n_insts = self.code.len();
        for i in 0..n_insts {
602
            self.trace_inst(i);
603 604
        }
        
605 606 607 608 609 610
        trace!("")      
    }
    
    fn trace_inst(&self, i: usize) {
        trace!("#{}\t{:30}\t\tdefine: {:?}\tuses: {:?}\tpred: {:?}\tsucc: {:?}", 
            i, self.code[i].code, self.get_inst_reg_defines(i), self.get_inst_reg_uses(i),
qinsoon's avatar
qinsoon committed
611
            self.code[i].preds, self.code[i].succs);
612
    }
613
    
614
    fn get_ir_block_livein(&self, block: &str) -> Option<&Vec<MuID>> {
qinsoon's avatar
qinsoon committed
615 616 617 618
        match self.blocks.get(block) {
            Some(ref block) => Some(&block.livein),
            None => None
        }
619 620
    }
    
621
    fn get_ir_block_liveout(&self, block: &str) -> Option<&Vec<MuID>> {
qinsoon's avatar
qinsoon committed
622 623 624 625
        match self.blocks.get(block) {
            Some(ref block) => Some(&block.liveout),
            None => None
        }
626 627
    }
    
628
    fn set_ir_block_livein(&mut self, block: &str, set: Vec<MuID>) {
qinsoon's avatar
qinsoon committed
629 630
        let block = self.blocks.get_mut(block).unwrap();
        block.livein = set;
631 632 633
    }
    
    fn set_ir_block_liveout(&mut self, block: &str, set: Vec<MuID>) {
qinsoon's avatar
qinsoon committed
634 635
        let block = self.blocks.get_mut(block).unwrap();
        block.liveout = set;
636 637
    }
    
qinsoon's avatar
qinsoon committed
638 639
    fn get_all_blocks(&self) -> Vec<MuName> {
        self.blocks.keys().map(|x| x.clone()).collect()
640 641
    }
    
642
    fn get_block_range(&self, block: &str) -> Option<ops::Range<usize>> {
qinsoon's avatar
qinsoon committed
643 644
        match self.blocks.get(block) {
            Some(ref block) => Some(block.start_inst..block.end_inst),
645 646 647
            None => None
        }
    }
648 649
}

qinsoon's avatar
qinsoon committed
650 651 652 653
#[derive(Clone, Debug)]
enum ASMBranchTarget {
    None,
    Conditional(MuName),
qinsoon's avatar
[wip]  
qinsoon committed
654 655
    Unconditional(MuName),
    Return
qinsoon's avatar
qinsoon committed
656 657
}

qinsoon's avatar
qinsoon committed
658
#[derive(Clone, Debug)]
qinsoon's avatar
qinsoon committed
659
struct ASMInst {
660
    code: String,
qinsoon's avatar
qinsoon committed
661 662 663 664 665 666 667 668

    defines: HashMap<MuID, Vec<ASMLocation>>,
    uses: HashMap<MuID, Vec<ASMLocation>>,

    is_mem_op_used: bool,
    preds: Vec<usize>,
    succs: Vec<usize>,
    branch: ASMBranchTarget
669 670
}

qinsoon's avatar
qinsoon committed
671 672 673
impl ASMInst {
    fn symbolic(line: String) -> ASMInst {
        ASMInst {
674
            code: line,
qinsoon's avatar
qinsoon committed
675 676 677 678 679 680
            defines: HashMap::new(),
            uses: HashMap::new(),
            is_mem_op_used: false,
            preds: vec![],
            succs: vec![],
            branch: ASMBranchTarget::None
681 682 683
        }
    }
    
qinsoon's avatar
qinsoon committed
684 685 686 687 688 689 690 691 692
    fn inst(
        inst: String,
        defines: HashMap<MuID, Vec<ASMLocation>>,
        uses: HashMap<MuID, Vec<ASMLocation>>,
        is_mem_op_used: bool,
        target: ASMBranchTarget
    ) -> ASMInst
    {
        ASMInst {
693 694
            code: inst,
            defines: defines,
qinsoon's avatar
qinsoon committed
695 696 697 698 699
            uses: uses,
            is_mem_op_used: is_mem_op_used,
            preds: vec![],
            succs: vec![],
            branch: target
700 701
        }
    }
702
    
qinsoon's avatar
qinsoon committed
703 704
    fn nop() -> ASMInst {
        ASMInst {
705
            code: "".to_string(),
qinsoon's avatar
qinsoon committed
706 707 708 709 710 711
            defines: HashMap::new(),
            uses: HashMap::new(),
            is_mem_op_used: false,
            preds: vec![],
            succs: vec![],
            branch: ASMBranchTarget::None
712 713
        }
    }
714 715
}

716
#[derive(Clone, Debug, PartialEq, Eq)]
717 718 719
struct ASMLocation {
    line: usize,
    index: usize,
qinsoon's avatar
qinsoon committed
720 721
    len: usize,
    oplen: usize,
722 723 724
}

impl ASMLocation {
qinsoon's avatar
qinsoon committed
725
    fn new(line: usize, index: usize, len: usize, oplen: usize) -> ASMLocation {
726
        ASMLocation{
qinsoon's avatar
qinsoon committed
727
            line: line,
728
            index: index,
qinsoon's avatar
qinsoon committed
729 730
            len: len,
            oplen: oplen
731 732 733 734
        }
    }
}

qinsoon's avatar
qinsoon committed
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
#[derive(Clone, Debug)]
/// [start_inst, end_inst)
struct ASMBlock {
    start_inst: usize,
    end_inst: usize,

    livein: Vec<MuID>,
    liveout: Vec<MuID>
}

impl ASMBlock {
    fn new() -> ASMBlock {
        ASMBlock {
            start_inst: usize::MAX,
            end_inst: usize::MAX,
            livein: vec![],
            liveout: vec![]
        }
    }
}

756
pub struct ASMCodeGen {
757
    cur: Option<Box<ASMCode>>
758 759
}

760
const REG_PLACEHOLDER_LEN : usize = 5;
761 762 763 764 765 766
lazy_static! {
    pub static ref REG_PLACEHOLDER : String = {
        let blank_spaces = [' ' as u8; REG_PLACEHOLDER_LEN];
        
        format!("%{}", str::from_utf8(&blank_spaces).unwrap())
    };
767 768
}

qinsoon's avatar
qinsoon committed
769 770 771 772 773 774 775 776
const FRAME_SIZE_PLACEHOLDER_LEN : usize = 10; // a frame is smaller than 1 << 10
lazy_static! {
    pub static ref FRAME_SIZE_PLACEHOLDER : String = {
        let blank_spaces = [' ' as u8; FRAME_SIZE_PLACEHOLDER_LEN];
        format!("{}", str::from_utf8(&blank_spaces).unwrap())
    };
}

777 778
impl ASMCodeGen {
    pub fn new() -> ASMCodeGen {
779
        ASMCodeGen {
780
            cur: None
781 782 783 784 785 786 787 788 789 790 791
        }
    }
    
    fn cur(&self) -> &ASMCode {
        self.cur.as_ref().unwrap()
    }
    
    fn cur_mut(&mut self) -> &mut ASMCode {
        self.cur.as_mut().unwrap()
    }
    
792 793 794 795
    fn line(&self) -> usize {
        self.cur().code.len()
    }
    
796 797
    fn add_asm_label(&mut self, code: String) {
        let l = self.line();
qinsoon's avatar
qinsoon committed
798
        self.cur_mut().code.push(ASMInst::symbolic(code));
799 800
    }
    
801
    fn add_asm_block_label(&mut self, code: String, block_name: MuName) {
802
        let l = self.line();
qinsoon's avatar
qinsoon committed
803
        self.cur_mut().code.push(ASMInst::symbolic(code));
804 805
    }
    
806
    fn add_asm_symbolic(&mut self, code: String){
qinsoon's avatar
qinsoon committed
807
        self.cur_mut().code.push(ASMInst::symbolic(code));
808 809
    }
    
810 811
    fn prepare_machine_regs(&self, regs: Iter<P<Value>>) -> Vec<MuID> {
        regs.map(|x| self.prepare_machine_reg(x)).collect()
qinsoon's avatar
qinsoon committed
812
    }
813
    
814
    fn add_asm_call(&mut self, code: String) {
qinsoon's avatar
qinsoon committed
815
        // a call instruction will use all the argument registers
qinsoon's avatar
qinsoon committed
816
        // do not need
qinsoon's avatar
qinsoon committed
817
        let uses : HashMap<MuID, Vec<ASMLocation>> = HashMap::new();
qinsoon's avatar
qinsoon committed
818 819 820 821 822 823
//        for reg in x86_64::ARGUMENT_GPRs.iter() {
//            uses.insert(reg.id(), vec![]);
//        }
//        for reg in x86_64::ARGUMENT_FPRs.iter() {
//            uses.insert(reg.id(), vec![]);
//        }
qinsoon's avatar
qinsoon committed
824 825

        // defines: return registers
qinsoon's avatar
qinsoon committed
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
        let mut defines : HashMap<MuID, Vec<ASMLocation>> = HashMap::new();
        for reg in x86_64::RETURN_GPRs.iter() {
            defines.insert(reg.id(), vec![]);
        }
        for reg in x86_64::RETURN_FPRs.iter() {
            defines.insert(reg.id(), vec![]);
        }
        for reg in x86_64::CALLER_SAVED_GPRs.iter() {
            if !defines.contains_key(&reg.id()) {
                defines.insert(reg.id(), vec![]);
            }
        }
        for reg in x86_64::CALLER_SAVED_FPRs.iter() {
            if !defines.contains_key(&reg.id()) {
                defines.insert(reg.id(), vec![]);
            }
        }
843
          
qinsoon's avatar
qinsoon committed
844
        self.add_asm_inst(code, defines, uses, false);
845 846 847
    }
    
    fn add_asm_ret(&mut self, code: String) {
848 849 850 851
        // return instruction does not use anything (not RETURN REGS)
        // otherwise it will keep RETURN REGS alive
        // and if there is no actual move into RETURN REGS, it will keep RETURN REGS for alive for very long
        // and prevents anything using those regsiters
qinsoon's avatar
[wip]  
qinsoon committed
852
        self.add_asm_inst_internal(code, hashmap!{}, hashmap!{}, false, ASMBranchTarget::Return);
853 854
    }
    
855
    fn add_asm_branch(&mut self, code: String, target: MuName) {
qinsoon's avatar
qinsoon committed
856
        self.add_asm_inst_internal(code, hashmap!{}, hashmap!{}, false, ASMBranchTarget::Unconditional(target));
857 858
    }
    
859
    fn add_asm_branch2(&mut self, code: String, target: MuName) {
qinsoon's avatar
qinsoon committed
860
        self.add_asm_inst_internal(code, hashmap!{}, hashmap!{}, false, ASMBranchTarget::Conditional(target));
861 862 863 864 865
    }
    
    fn add_asm_inst(
        &mut self, 
        code: String, 
qinsoon's avatar
qinsoon committed
866 867 868 869 870 871 872 873 874 875 876 877 878 879
        defines: HashMap<MuID, Vec<ASMLocation>>,
        uses: HashMap<MuID, Vec<ASMLocation>>,
        is_using_mem_op: bool)
    {
        self.add_asm_inst_internal(code, defines, uses, is_using_mem_op, ASMBranchTarget::None)
    }

    fn add_asm_inst_internal(
        &mut self,
        code: String,
        defines: HashMap<MuID, Vec<ASMLocation>>,
        uses: HashMap<MuID, Vec<ASMLocation>>,
        is_using_mem_op: bool,
        target: ASMBranchTarget)
880
    {
881 882
        let line = self.line();
        trace!("asm: {}", code);
qinsoon's avatar
qinsoon committed
883 884
        trace!("     defines: {:?}", defines);
        trace!("     uses: {:?}", uses);
885
        let mc = self.cur_mut();
qinsoon's avatar
qinsoon committed
886

887
        // put the instruction
qinsoon's avatar
qinsoon committed
888
        mc.code.push(ASMInst::inst(code, defines, uses, is_using_mem_op, target));
889 890
    }
    
891
    fn prepare_reg(&self, op: &P<Value>, loc: usize) -> (String, MuID, ASMLocation) {
892 893 894 895 896 897 898
        if cfg!(debug_assertions) {
            match op.v {
                Value_::SSAVar(_) => {},
                _ => panic!("expecting register op")
            }
        }
        
899 900
        let str = self.asm_reg_op(op);
        let len = str.len();
qinsoon's avatar
qinsoon committed
901 902 903 904 905 906 907 908 909 910 911 912 913 914
        (str, op.extract_ssa_id().unwrap(), ASMLocation::new(self.line(), loc, len, check_op_len(op)))
    }

    fn prepare_fpreg(&self, op: &P<Value>, loc: usize) -> (String, MuID, ASMLocation) {
        if cfg!(debug_assertions) {
            match op.v {
                Value_::SSAVar(_) => {},
                _ => panic!("expecting register op")
            }
        }

        let str = self.asm_reg_op(op);
        let len = str.len();
        (str, op.extract_ssa_id().unwrap(), ASMLocation::new(self.line(), loc, len, 64))
915 916 917
    }
    
    fn prepare_machine_reg(&self, op: &P<Value>) -> MuID {
918 919 920 921 922 923 924
        if cfg!(debug_assertions) {
            match op.v {
                Value_::SSAVar(_) => {},
                _ => panic!("expecting machine register op")
            }
        }        
        
925
        op.extract_ssa_id().unwrap()
926
    }
927
    
928
    #[allow(unused_assignments)]
qinsoon's avatar
qinsoon committed
929
    fn prepare_mem(&self, op: &P<Value>, loc: usize) -> (String, HashMap<MuID, Vec<ASMLocation>>) {
930 931 932
        if cfg!(debug_assertions) {
            match op.v {
                Value_::Memory(_) => {},
qinsoon's avatar
qinsoon committed
933
                _ => panic!("expecting memory op")
934 935
            }
        }        
qinsoon's avatar
qinsoon committed
936

937 938 939 940
        let mut ids : Vec<MuID> = vec![];
        let mut locs : Vec<ASMLocation> = vec![];
        let mut result_str : String = "".to_string();
        
941
        let mut loc_cursor : usize = loc;
942 943 944 945 946 947 948 949 950 951 952
        
        match op.v {
            // offset(base,index,scale)
            Value_::Memory(MemoryLocation::Address{ref base, ref offset, ref index, scale}) => {
                // deal with offset
                if offset.is_some() {
                    let offset = offset.as_ref().unwrap();
                    
                    match offset.v {
                        Value_::SSAVar(id) => {
                            // temp as offset
qinsoon's avatar
qinsoon committed
953
                            let (str, id, loc) = self.prepare_reg(offset, loc_cursor);
954 955 956 957 958 959 960 961
                            
                            result_str.push_str(&str);
                            ids.push(id);
                            locs.push(loc);
                            
                            loc_cursor += str.len();
                        },
                        Value_::Constant(Constant::Int(val)) => {
qinsoon's avatar
qinsoon committed
962
                            let str = (val as i32).to_string();
963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999
                            
                            result_str.push_str(&str);
                            loc_cursor += str.len();
                        },
                        _ => panic!("unexpected offset type: {:?}", offset)
                    }
                }
                
                result_str.push('(');
                loc_cursor += 1; 
                
                // deal with base, base is ssa
                let (str, id, loc) = self.prepare_reg(base, loc_cursor);
                result_str.push_str(&str);
                ids.push(id);
                locs.push(loc);
                loc_cursor += str.len();
                
                // deal with index (ssa or constant)
                if index.is_some() {
                    result_str.push(',');
                    loc_cursor += 1; // plus 1 for ,                    
                    
                    let index = index.as_ref().unwrap();
                    
                    match index.v {
                        Value_::SSAVar(id) => {
                            // temp as offset
                            let (str, id, loc) = self.prepare_reg(index, loc_cursor);
                            
                            result_str.push_str(&str);
                            ids.push(id);
                            locs.push(loc);
                            
                            loc_cursor += str.len();
                        },
                        Value_::Constant(Constant::Int(val)) => {
qinsoon's avatar
qinsoon committed
1000
                            let str = (val as i32).to_string();
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
                            
                            result_str.push_str(&str);
                            loc_cursor += str.len();
                        },
                        _ => panic!("unexpected index type: {:?}", index)
                    }
                    
                    // scale
                    if scale.is_some() {
                        result_str.push(',');
                        loc_cursor += 1;
                        
                        let scale = scale.unwrap();
                        let str = scale.to_string();
                        
                        result_str.push_str(&str);
                        loc_cursor += str.len();
                    }
                }
                
                result_str.push(')');
                loc_cursor += 1;
            },
1024 1025
            Value_::Memory(MemoryLocation::Symbolic{ref base, ref label}) => {
                result_str.push_str(&symbol(label.clone()));
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
                loc_cursor += label.len();
                
                if base.is_some() {
                    result_str.push('(');
                    loc_cursor += 1;
                    
                    let (str, id, loc) = self.prepare_reg(base.as_ref().unwrap(), loc_cursor);
                    result_str.push_str(&str);
                    ids.push(id);
                    locs.push(loc);
                    loc_cursor += str.len();
qinsoon's avatar
qinsoon committed
1037

1038
                    result_str.push(')');
qinsoon's avatar
qinsoon committed
1039
                    loc_cursor += 1;
1040 1041 1042 1043
                }
            },
            _ => panic!("expect mem location as value")
        }
qinsoon's avatar
qinsoon committed
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061

        let uses : HashMap<MuID, Vec<ASMLocation>> = {
            let mut map : HashMap<MuID, Vec<ASMLocation>> = hashmap!{};
            for i in 0..ids.len() {
                let id = ids[i];
                let loc = locs[i].clone();

                if map.contains_key(&id) {
                    map.get_mut(&id).unwrap().push(loc);
                } else {
                    map.insert(id, vec![loc]);
                }
            }
            map
        };


        (result_str, uses)
1062
    }
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072

    fn prepare_imm(&self, op: i32, len: usize) -> i32 {
        match len {
            64 => op,
            32 => op,
            16 => op as i16 as i32,
            8  => op as i8  as i32,
            _ => unimplemented!()
        }
    }
1073
    
1074 1075
    fn asm_reg_op(&self, op: &P<Value>) -> String {
        let id = op.extract_ssa_id().unwrap();
qinsoon's avatar
qinsoon committed
1076
        if id < MACHINE_ID_END {
1077
            // machine reg
1078
            format!("%{}", op.name().unwrap())
1079 1080 1081 1082 1083 1084
        } else {
            // virtual register, use place holder
            REG_PLACEHOLDER.clone()
        }
    }
    
qinsoon's avatar
qinsoon committed
1085 1086
    fn mangle_block_label(&self, label: MuName) -> String {
        format!("{}_{}", self.cur().name, label)
1087
    }
1088 1089 1090 1091

    fn finish_code_sequence_asm(&mut self) -> Box<ASMCode> {
        self.cur.take().unwrap()
    }
1092

qinsoon's avatar
qinsoon committed
1093 1094
    fn internal_binop_no_def_r_r(&mut self, inst: &str, op1: &P<Value>, op2: &P<Value>) {
        let len = check_op_len(op1);
1095

qinsoon's avatar
qinsoon committed
1096 1097 1098
        // with postfix
        let inst = inst.to_string() + &op_postfix(len);
        trace!("emit: {} {} {}", inst, op1, op2);
1099

qinsoon's avatar
qinsoon committed
1100 1101
        let (reg1, id1, loc1) = self.prepare_reg(op1, inst.len() + 1);
        let (reg2, id2, loc2) = self.prepare_reg(op2, inst.len() + 1 + reg1.len() + 1);
1102

qinsoon's avatar
qinsoon committed
1103
        let asm = format!("{} {},{}", inst, reg1, reg2);
1104

qinsoon's avatar
qinsoon committed
1105 1106 1107 1108 1109 1110 1111
        self.add_asm_inst(
            asm,
            hashmap!{},
            {
                if id1 == id2 {
                    hashmap!{
                        id1 => vec![loc1, loc2]
1112
                    }
qinsoon's avatar
qinsoon committed
1113 1114 1115 1116 1117 1118 1119 1120 1121
                } else {
                    hashmap!{
                        id1 => vec![loc1],
                        id2 => vec![loc2]
                    }
                }
            },
            false
        )
1122 1123
    }

qinsoon's avatar
qinsoon committed
1124 1125
    fn internal_binop_no_def_imm_r(&mut self, inst: &str, op1: i32, op2: &P<Value>) {
        let len = check_op_len(op2);
1126

qinsoon's avatar
qinsoon committed
1127 1128
        let inst = inst.to_string() + &op_postfix(len);
        trace!("emit: {} {} {}", inst, op1, op2);
1129

1130 1131
        let imm = self.prepare_imm(op1, len);
        let (reg2, id2, loc2) = self.prepare_reg(op2, inst.len() + 1 + 1 + imm.to_string().len() + 1);
1132

1133
        let asm = format!("{} ${},{}", inst, imm, reg2);
1134

qinsoon's avatar
qinsoon committed
1135 1136 1137 1138 1139 1140 1141 1142
        self.add_asm_inst(
            asm,
            hashmap!{},
            hashmap!{
                id2 => vec![loc2]
            },
            false
        )
1143 1144
    }

qinsoon's avatar
qinsoon committed
1145 1146
    fn internal_binop_no_def_mem_r(&mut self, inst: &str, op1: &P<Value>, op2: &P<Value>) {
        let len = check_op_len(op2);
1147

qinsoon's avatar
qinsoon committed
1148 1149
        let inst = inst.to_string() + &op_postfix(len);
        trace!("emit: {} {} {}", inst, op1, op2);
1150

qinsoon's avatar
qinsoon committed
1151 1152
        let (mem, mut uses)  = self.prepare_mem(op1, inst.len() + 1);
        let (reg, id1, loc1) = self.prepare_reg(op2, inst.len() + 1 + mem.len() + 1);
1153

qinsoon's avatar
qinsoon committed
1154
        let asm = format!("{} {},{}", inst, mem, reg);
1155

qinsoon's avatar
qinsoon committed
1156 1157 1158 1159 1160 1161
        // merge use vec
        if uses.contains_key(&id1) {
            let mut locs = uses.get_mut(&id1).unwrap();
            vec_utils::add_unique(locs, loc1.clone());
        } else {
            uses.insert(id1, vec![loc1]);
1162
        }
qinsoon's avatar
qinsoon committed
1163 1164 1165 1166 1167 1168 1169

        self.add_asm_inst(
            asm,
            hashmap!{},
            uses,
            true
        )
1170 1171
    }

qinsoon's avatar
qinsoon committed
1172 1173
    fn internal_binop_no_def_r_mem(&mut self, inst: &str, op1: &P<Value>, op2: &P<Value>) {
        let len = check_op_len(op1);
1174

qinsoon's avatar
qinsoon committed
1175 1176
        let inst = inst.to_string() + &op_postfix(len);
        trace!("emit: {} {} {}", inst, op1, op2);
1177

qinsoon's avatar
qinsoon committed
1178 1179
        let (mem, mut uses) = self.prepare_mem(op2, inst.len() + 1);
        let (reg, id1, loc1) = self.prepare_reg(op1, inst.len() + 1 + mem.len() + 1);
1180

qinsoon's avatar
qinsoon committed
1181 1182 1183 1184 1185
        if uses.contains_key(&id1) {
            let mut locs = uses.get_mut(&id1).unwrap();
            vec_utils::add_unique(locs, loc1.clone());
        } else {
            uses.insert(id1, vec![loc1.clone()]);
1186 1187
        }

qinsoon's avatar
qinsoon committed
1188
        let asm = format!("{} {},{}", inst, mem, reg);
1189

qinsoon's avatar
qinsoon committed
1190 1191 1192 1193 1194 1195
        self.add_asm_inst(
            asm,
            hashmap!{},
            uses,
            true
        )
1196 1197
    }

qinsoon's avatar
qinsoon committed
1198 1199
    fn internal_binop_def_r_r(&mut self, inst: &str, dest: &P<Value>, src: &P<Value>) {
        let len = check_op_len(src);
1200

qinsoon's avatar
qinsoon committed
1201 1202
        let inst = inst.to_string() + &op_postfix(len);
        trace!("emit: {} {}, {} -> {}", inst, src, dest, dest);
1203

qinsoon's avatar
qinsoon committed
1204 1205
        let (reg1, id1, loc1) = self.prepare_reg(src, inst.len() + 1);
        let (reg2, id2, loc2) = self.prepare_reg(dest, inst.len() + 1 + reg1.len() + 1);
1206

qinsoon's avatar
qinsoon committed
1207
        let asm = format!("{} {},{}", inst, reg1, reg2);
1208

qinsoon's avatar
qinsoon committed
1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227
        self.add_asm_inst(
            asm,
            hashmap!{
                id2 => vec![loc2.clone()]
            },
            {
                if id1 == id2 {
                    hashmap!{
                        id1 => vec![loc1, loc2]
                    }
                } else {
                    hashmap!{
                        id1 => vec![loc1],
                        id2 => vec![loc2]
                    }
                }
            },
            false
        )
1228 1229
    }

qinsoon's avatar
qinsoon committed
1230 1231
    fn internal_binop_def_r_mr(&mut self, inst: &str, dest: &P<Value>, src: &P<Value>) {
        let len = check_op_len(dest);
1232

qinsoon's avatar
qinsoon committed
1233 1234
        let inst = inst.to_string() + &op_postfix(len);
        trace!("emit: {} {}, {} -> {}", inst, src, dest, dest);
1235

qinsoon's avatar
qinsoon committed
1236 1237 1238
        let mreg = self.prepare_machine_reg(src);
        let mreg_name = src.name().unwrap();
        let (reg2, id2, loc2) = self.prepare_reg(dest, inst.len() + 1 + 1 + mreg_name.len() + 1);
1239

qinsoon's avatar
qinsoon committed
1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
        let asm = format!("{} %{},{}", inst, mreg_name, reg2);

        self.add_asm_inst(
            asm,
            hashmap!{
                id2 => vec![loc2.clone()]
            },
            hashmap!{
                id2 => vec![loc2],
                mreg => vec![]
            },
            false
        )
1253 1254
    }

qinsoon's avatar
qinsoon committed
1255 1256
    fn internal_binop_def_r_imm(&mut self, inst: &str, dest: &P<Value>, src: i32) {
        let len = check_op_len(dest);
1257

qinsoon's avatar
qinsoon committed
1258 1259
        let inst = inst.to_string() + &op_postfix(len);
        trace!("emit: {} {}, {} -> {}", inst, src, dest, dest);
1260

1261 1262
        let imm = self.prepare_imm(src, len);
        let (reg1, id1, loc1) = self.prepare_reg(dest, inst.len() + 1 + 1 + imm.to_string().len() + 1);
1263

1264
        let asm = format!("{} ${},{}", inst, imm, reg1);
qinsoon's avatar
qinsoon committed
1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275

        self.add_asm_inst(
            asm,
            hashmap!{
                id1 => vec![loc1.clone()]
            },
            hashmap!{
                id1 => vec![loc1]
            },
            false
        )
1276 1277
    }

qinsoon's avatar
qinsoon committed
1278 1279 1280 1281 1282
    fn internal_binop_def_r_mem(&mut self, inst: &str, dest: &P<Value>, src: &P<Value>) {
        let len = match dest.ty.get_int_length() {
            Some(n) if n == 64 | 32 | 16 | 8 => n,
            _ => panic!("unimplemented int types: {}", dest.ty)
        };
1283

qinsoon's avatar
qinsoon committed
1284 1285
        let inst = inst.to_string() + &op_postfix(len);
        trace!("emit: {} {}, {} -> {}", inst, src, dest, dest);
1286

qinsoon's avatar
qinsoon committed
1287 1288
        let (mem, mut uses) = self.prepare_mem(src, inst.len() + 1);
        let (reg, id1, loc1) = self.prepare_reg(dest, inst.len() + 1 + mem.len() + 1);
1289

qinsoon's avatar
qinsoon committed
1290 1291 1292 1293 1294
        if uses.contains_key(&id1) {
            let mut locs = uses.get_mut(&id1).unwrap();
            vec_utils::add_unique(locs, loc1.clone());
        } else {
            uses.insert(id1, vec![loc1.clone()]);
1295 1296
        }

qinsoon's avatar
qinsoon committed
1297
        let asm = format!("{} {},{}", inst, mem, reg);
1298

qinsoon's avatar
qinsoon committed
1299 1300 1301 1302 1303 1304 1305 1306
        self.add_asm_inst(
            asm,
            hashmap!{
                id1 => vec![loc1]
            },
            uses,
            true
        )
1307 1308
    }

qinsoon's avatar
qinsoon committed
1309 1310 1311
    fn internal_mov_r64_imm64(&mut self, inst: &str, dest: &P<Value>, src: i64) {
        let inst = inst.to_string() + &op_postfix(64);
        trace!("emit: {} {} -> {}", inst, src, dest);
1312

qinsoon's avatar
qinsoon committed
1313
        let (reg1, id1, loc1) = self.prepare_reg(dest, inst.len() + 1 + 1 + src.to_string().len() + 1);
1314

qinsoon's avatar
qinsoon committed
1315
        let asm = format!("{} ${},{}", inst, src, reg1);
1316