mod.rs 12.9 KB
Newer Older
qinsoon's avatar
qinsoon committed
1 2 3
#![allow(dead_code)]
#![allow(non_upper_case_globals)]

4 5 6 7 8 9 10
pub mod inst_sel;

mod codegen;
pub use compiler::backend::x86_64::codegen::CodeGenerator;

mod asm_backend;
pub use compiler::backend::x86_64::asm_backend::ASMCodeGen;
11 12
pub use compiler::backend::x86_64::asm_backend::emit_code;
pub use compiler::backend::x86_64::asm_backend::emit_context;
13 14
#[cfg(feature = "aot")]
pub use compiler::backend::x86_64::asm_backend::spill_rewrite;
15 16 17

use ast::ptr::P;
use ast::ir::*;
qinsoon's avatar
qinsoon committed
18
use ast::types::*;
qinsoon's avatar
qinsoon committed
19
use compiler::backend::RegGroup;
20
use utils::ByteSize;
qinsoon's avatar
qinsoon committed
21

22 23
use std::collections::HashMap;

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
macro_rules! GPR8 {
    ($id:expr, $name: expr) => {
        {
            P(Value {
                hdr: MuEntityHeader::named($id, $name.to_string()),
                ty: UINT8_TYPE.clone(),
                v: Value_::SSAVar($id)
            })
        }
    };
}

macro_rules! GPR16 {
    ($id:expr, $name: expr) => {
        {
            P(Value {
                hdr: MuEntityHeader::named($id, $name.to_string()),
                ty: UINT16_TYPE.clone(),
                v: Value_::SSAVar($id)
            })
        }
    };
}

macro_rules! GPR32 {
    ($id:expr, $name: expr) => {
        {
            P(Value {
                hdr: MuEntityHeader::named($id, $name.to_string()),
                ty: UINT32_TYPE.clone(),
                v: Value_::SSAVar($id)
            })
        }
    };
}

macro_rules! GPR64 {
qinsoon's avatar
qinsoon committed
61
    ($id:expr, $name: expr) => {
qinsoon's avatar
qinsoon committed
62 63
        {
            P(Value {
qinsoon's avatar
qinsoon committed
64
                hdr: MuEntityHeader::named($id, $name.to_string()),
qinsoon's avatar
qinsoon committed
65
                ty: UINT64_TYPE.clone(),
qinsoon's avatar
qinsoon committed
66
                v: Value_::SSAVar($id)
qinsoon's avatar
qinsoon committed
67 68
            })
        }
qinsoon's avatar
qinsoon committed
69 70 71 72
    };
}

macro_rules! FPR {
qinsoon's avatar
qinsoon committed
73
    ($id:expr, $name: expr) => {
qinsoon's avatar
qinsoon committed
74 75
        {
            P(Value {
qinsoon's avatar
qinsoon committed
76
                hdr: MuEntityHeader::named($id, $name.to_string()),
qinsoon's avatar
qinsoon committed
77
                ty: DOUBLE_TYPE.clone(),
qinsoon's avatar
qinsoon committed
78
                v: Value_::SSAVar($id)
qinsoon's avatar
qinsoon committed
79 80
            })
        }
qinsoon's avatar
qinsoon committed
81 82 83
    };
}

84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
macro_rules! alias4 {
    ($r8: expr, $r16: expr, $r32: expr, $r64: expr) => {
        hashmap!{
            8  => $r8.clone(),
            16 => $r16.clone(),
            32 => $r32.clone(),
            64 => $r64.clone()
        }
    };
}

macro_rules! alias3 {
    ($r16: expr, $r32: expr, $r64: expr) => {
        hashmap!{
            16 => $r16.clone(),
            32 => $r32.clone(),
            64 => $r64.clone()
        }
    };
}

macro_rules! alias2 {
    ($r32: expr, $r64: expr) => {
        hashmap!{
            32 => $r32.clone(),
            64 => $r64.clone()
        }
    };
}

pub type RegisterAliasMap = HashMap<ByteSize, P<Value>>;

qinsoon's avatar
qinsoon committed
116
// put into several segments to avoid 'recursion limit reached' error
117
lazy_static! {
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
    pub static ref RAX : P<Value> = GPR64!(0, "rax");
    pub static ref RCX : P<Value> = GPR64!(1, "rcx");
    pub static ref RDX : P<Value> = GPR64!(2, "rdx");
    pub static ref RBX : P<Value> = GPR64!(3, "rbx");
    pub static ref RSP : P<Value> = GPR64!(4, "rsp");
    pub static ref RBP : P<Value> = GPR64!(5, "rbp");
    pub static ref RSI : P<Value> = GPR64!(6, "rsi");
    pub static ref RDI : P<Value> = GPR64!(7, "rdi");
    pub static ref R8  : P<Value> = GPR64!(8, "r8");
    pub static ref R9  : P<Value> = GPR64!(9, "r9");
    pub static ref R10 : P<Value> = GPR64!(10,"r10");
    pub static ref R11 : P<Value> = GPR64!(11,"r11");
    pub static ref R12 : P<Value> = GPR64!(12,"r12");
    pub static ref R13 : P<Value> = GPR64!(13,"r13");
    pub static ref R14 : P<Value> = GPR64!(14,"r14");
    pub static ref R15 : P<Value> = GPR64!(15,"r15");
134
    
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
    pub static ref RIP : P<Value> = GPR64!(32,"rip");
}

lazy_static! {
    pub static ref AL  : P<Value> = GPR8! (33, "al");
    pub static ref AX  : P<Value> = GPR16!(34, "ax");
    pub static ref EAX : P<Value> = GPR32!(35, "eax");
    pub static ref RAX_ALIAS : RegisterAliasMap = alias4!(AL, AX, EAX, RAX);

    pub static ref CL  : P<Value> = GPR8! (36, "cl");
    pub static ref CX  : P<Value> = GPR16!(37, "cx");
    pub static ref ECX : P<Value> = GPR32!(38, "ecx");
    pub static ref RCX_ALIAS : RegisterAliasMap = alias4!(CL, CX, ECX, RCX);

    pub static ref DL  : P<Value> = GPR8! (39, "dl");
    pub static ref DX  : P<Value> = GPR16!(40, "dx");
    pub static ref EDX : P<Value> = GPR32!(41, "edx");
    pub static ref RDX_ALIAS : RegisterAliasMap = alias4!(DL, DX, EDX, RDX);

    pub static ref BL  : P<Value> = GPR8! (42, "bl");
    pub static ref BX  : P<Value> = GPR16!(43, "bx");
    pub static ref EBX : P<Value> = GPR32!(44, "ebx");
    pub static ref RBX_ALIAS : RegisterAliasMap = alias4!(BL, BX, EBX, RBX);
}

lazy_static!{
    pub static ref SI  : P<Value> = GPR16!(45, "si");
    pub static ref ESI : P<Value> = GPR32!(46, "esi");
    pub static ref RSI_ALIAS : RegisterAliasMap = alias3!(SI, ESI, RSI);

    pub static ref DI  : P<Value> = GPR16!(47, "di");
    pub static ref EDI : P<Value> = GPR32!(48, "edi");
    pub static ref RDI_ALIAS : RegisterAliasMap = alias3!(DI, EDI, RDI);

    pub static ref R8D : P<Value>  = GPR32!(49, "r8d");
    pub static ref R8_ALIAS : RegisterAliasMap = alias2!(R8D, R8);

    pub static ref R9D : P<Value>  = GPR32!(50, "r9d");
    pub static ref R9_ALIAS : RegisterAliasMap = alias2!(R9D, R9);

    pub static ref R10D : P<Value> = GPR32!(51, "r10d");
    pub static ref R10_ALIAS : RegisterAliasMap = alias2!(R10D, R10);

    pub static ref R11D : P<Value> = GPR32!(52, "r11d");
    pub static ref R11_ALIAS : RegisterAliasMap = alias2!(R11D, R11);

    pub static ref R12D : P<Value> = GPR32!(53, "r12d");
    pub static ref R12_ALIAS : RegisterAliasMap = alias2!(R12D, R12);

    pub static ref R13D : P<Value> = GPR32!(54, "r13d");
    pub static ref R13_ALIAS : RegisterAliasMap = alias2!(R13D, R13);

    pub static ref R14D : P<Value> = GPR32!(55, "r14d");
    pub static ref R14_ALIAS : RegisterAliasMap = alias2!(R14D, R14);

    pub static ref R15D : P<Value> = GPR32!(56, "r15d");
    pub static ref R15_ALIAS : RegisterAliasMap = alias2!(R15D, R15);
}

lazy_static!{
qinsoon's avatar
qinsoon committed
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
    pub static ref RETURN_GPRs : [P<Value>; 2] = [
        RAX.clone(),
        RDX.clone(),
    ];
    
    pub static ref ARGUMENT_GPRs : [P<Value>; 6] = [
        RDI.clone(),
        RSI.clone(),
        RDX.clone(),
        RCX.clone(),
        R8.clone(),
        R9.clone()
    ];
    
    pub static ref CALLEE_SAVED_GPRs : [P<Value>; 6] = [
        RBX.clone(),
        RBP.clone(),
        R12.clone(),
        R13.clone(),
        R14.clone(),
        R15.clone()
    ];
qinsoon's avatar
qinsoon committed
217 218 219 220 221 222 223 224 225 226 227 228

    pub static ref CALLER_SAVED_GPRs : [P<Value>; 9] = [
        RAX.clone(),
        RCX.clone(),
        RDX.clone(),
        RSI.clone(),
        RDI.clone(),
        R8.clone(),
        R9.clone(),
        R10.clone(),
        R11.clone()
    ];
qinsoon's avatar
qinsoon committed
229
    
230
    pub static ref ALL_GPRs : [P<Value>; 15] = [
qinsoon's avatar
qinsoon committed
231 232 233 234 235
        RAX.clone(),
        RCX.clone(),
        RDX.clone(),
        RBX.clone(),
        RSP.clone(),
236
//        RBP.clone(),
qinsoon's avatar
qinsoon committed
237 238 239 240 241 242 243 244 245 246 247
        RSI.clone(),
        RDI.clone(),
        R8.clone(),
        R9.clone(),
        R10.clone(),
        R11.clone(),
        R12.clone(),
        R13.clone(),
        R14.clone(),
        R15.clone()
    ];
qinsoon's avatar
qinsoon committed
248 249 250
}

lazy_static!{
qinsoon's avatar
qinsoon committed
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
    pub static ref XMM0  : P<Value> = FPR!(16,"xmm0");
    pub static ref XMM1  : P<Value> = FPR!(17,"xmm1");
    pub static ref XMM2  : P<Value> = FPR!(18,"xmm2");
    pub static ref XMM3  : P<Value> = FPR!(19,"xmm3");
    pub static ref XMM4  : P<Value> = FPR!(20,"xmm4");
    pub static ref XMM5  : P<Value> = FPR!(21,"xmm5");
    pub static ref XMM6  : P<Value> = FPR!(22,"xmm6");
    pub static ref XMM7  : P<Value> = FPR!(23,"xmm7");
    pub static ref XMM8  : P<Value> = FPR!(24,"xmm8");
    pub static ref XMM9  : P<Value> = FPR!(25,"xmm9");
    pub static ref XMM10 : P<Value> = FPR!(26,"xmm10");
    pub static ref XMM11 : P<Value> = FPR!(27,"xmm11");
    pub static ref XMM12 : P<Value> = FPR!(28,"xmm12");
    pub static ref XMM13 : P<Value> = FPR!(29,"xmm13");
    pub static ref XMM14 : P<Value> = FPR!(30,"xmm14");
    pub static ref XMM15 : P<Value> = FPR!(31,"xmm15"); 
qinsoon's avatar
qinsoon committed
267 268 269 270 271 272
    
    pub static ref RETURN_FPRs : [P<Value>; 2] = [
        XMM0.clone(),
        XMM1.clone()
    ];
    
qinsoon's avatar
qinsoon committed
273 274 275
    pub static ref ARGUMENT_FPRs : [P<Value>; 8] = [
        XMM0.clone(),
        XMM1.clone(),
qinsoon's avatar
qinsoon committed
276 277 278 279 280 281 282
        XMM2.clone(),
        XMM3.clone(),
        XMM4.clone(),
        XMM5.clone(),
        XMM6.clone(),
        XMM7.clone()
    ];
283
    
qinsoon's avatar
qinsoon committed
284
    pub static ref CALLEE_SAVED_FPRs : [P<Value>; 0] = [];
qinsoon's avatar
qinsoon committed
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303

    pub static ref CALLER_SAVED_FPRs : [P<Value>; 16] = [
        XMM0.clone(),
        XMM1.clone(),
        XMM2.clone(),
        XMM3.clone(),
        XMM4.clone(),
        XMM5.clone(),
        XMM6.clone(),
        XMM7.clone(),
        XMM8.clone(),
        XMM9.clone(),
        XMM10.clone(),
        XMM11.clone(),
        XMM12.clone(),
        XMM13.clone(),
        XMM14.clone(),
        XMM15.clone(),
    ];
qinsoon's avatar
qinsoon committed
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
    
    pub static ref ALL_FPRs : [P<Value>; 16] = [
        XMM0.clone(),
        XMM1.clone(),
        XMM2.clone(),
        XMM3.clone(),
        XMM4.clone(),
        XMM5.clone(),
        XMM6.clone(),
        XMM7.clone(),
        XMM8.clone(),
        XMM9.clone(),
        XMM10.clone(),
        XMM11.clone(),
        XMM12.clone(),
        XMM13.clone(),
        XMM14.clone(),
        XMM15.clone(),
    ];
323 324
}

qinsoon's avatar
qinsoon committed
325 326 327
pub const GPR_COUNT : usize = 16;
pub const FPR_COUNT : usize = 16;

qinsoon's avatar
qinsoon committed
328
lazy_static! {
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
    pub static ref ALL_MACHINE_REGs : HashMap<MuID, P<Value>> = {
        let mut map = HashMap::new();
        map.insert(RAX.id(), RAX.clone());
        map.insert(RCX.id(), RCX.clone());
        map.insert(RDX.id(), RDX.clone());
        map.insert(RBX.id(), RBX.clone());
        map.insert(RSP.id(), RSP.clone());
        map.insert(RBP.id(), RBP.clone());
        map.insert(RSI.id(), RSI.clone());
        map.insert(RDI.id(), RDI.clone());
        map.insert(R8.id(), R8.clone());
        map.insert(R9.id(), R9.clone());
        map.insert(R10.id(), R10.clone());
        map.insert(R11.id(), R11.clone());
        map.insert(R12.id(), R12.clone());
        map.insert(R13.id(), R13.clone());
        map.insert(R14.id(), R14.clone());
        map.insert(R15.id(), R15.clone());
        map.insert(XMM0.id(), XMM0.clone());
        map.insert(XMM1.id(), XMM1.clone());
        map.insert(XMM2.id(), XMM2.clone());
        map.insert(XMM3.id(), XMM3.clone());
        map.insert(XMM4.id(), XMM4.clone());
        map.insert(XMM5.id(), XMM5.clone());
        map.insert(XMM6.id(), XMM6.clone());
        map.insert(XMM7.id(), XMM7.clone());
        map.insert(XMM8.id(), XMM8.clone());
        map.insert(XMM9.id(), XMM9.clone());
        map.insert(XMM10.id(), XMM10.clone());
        map.insert(XMM11.id(), XMM11.clone());
        map.insert(XMM12.id(), XMM12.clone());
        map.insert(XMM13.id(), XMM13.clone());
        map.insert(XMM14.id(), XMM14.clone());
        map.insert(XMM15.id(), XMM15.clone());
        map.insert(RIP.id(), RIP.clone());
        
        map
    };
367
    
368
    // put caller saved regs first (they imposes no overhead if there is no call instruction)
369 370 371 372 373 374 375 376 377 378
    pub static ref ALL_USABLE_MACHINE_REGs : Vec<P<Value>> = vec![
        RAX.clone(),
        RCX.clone(),
        RDX.clone(),
        RSI.clone(),
        RDI.clone(),
        R8.clone(),
        R9.clone(),
        R10.clone(),
        R11.clone(),
379 380 381 382 383 384 385

        RBX.clone(),
        R12.clone(),
        R13.clone(),
        R14.clone(),
        R15.clone(),

386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
        XMM0.clone(),
        XMM1.clone(),
        XMM2.clone(),
        XMM3.clone(),
        XMM4.clone(),
        XMM5.clone(),
        XMM6.clone(),
        XMM7.clone(),
        XMM8.clone(),
        XMM9.clone(),
        XMM10.clone(),
        XMM11.clone(),
        XMM12.clone(),
        XMM13.clone(),
        XMM14.clone(),
        XMM15.clone()
    ];
qinsoon's avatar
qinsoon committed
403 404
}

qinsoon's avatar
qinsoon committed
405
pub fn init_machine_regs_for_func (func_context: &mut FunctionContext) {
406
    for reg in ALL_MACHINE_REGs.values() {
qinsoon's avatar
qinsoon committed
407
        let reg_id = reg.extract_ssa_id().unwrap();
qinsoon's avatar
qinsoon committed
408
        let entry = SSAVarEntry::new(reg.clone());
qinsoon's avatar
qinsoon committed
409 410 411 412 413 414 415 416 417
        
        func_context.values.insert(reg_id, entry);
    }
}

pub fn number_of_regs_in_group(group: RegGroup) -> usize {
    match group {
        RegGroup::GPR => ALL_GPRs.len(),
        RegGroup::FPR => ALL_FPRs.len()
qinsoon's avatar
qinsoon committed
418 419 420
    }
}

qinsoon's avatar
qinsoon committed
421 422 423 424
pub fn number_of_all_regs() -> usize {
    ALL_MACHINE_REGs.len()
}

425
pub fn all_regs() -> &'static HashMap<MuID, P<Value>> {
qinsoon's avatar
qinsoon committed
426 427 428
    &ALL_MACHINE_REGs
}

429 430 431 432
pub fn all_usable_regs() -> &'static Vec<P<Value>> {
    &ALL_USABLE_MACHINE_REGs
}

433
pub fn pick_group_for_reg(reg_id: MuID) -> RegGroup {
434 435 436 437 438 439 440
    let reg = all_regs().get(&reg_id).unwrap();
    if reg.is_int_reg() {
        RegGroup::GPR
    } else if reg.is_fp_reg() {
        RegGroup::FPR
    } else {
        panic!("expect a machine reg to be either a GPR or a FPR: {}", reg)
441 442 443
    }
}

444 445 446 447 448 449 450 451 452 453
pub fn is_callee_saved(reg_id: MuID) -> bool {
    for reg in CALLEE_SAVED_GPRs.iter() {
        if reg_id == reg.extract_ssa_id().unwrap() {
            return true;
        }
    }
    
    false 
}

454
pub fn is_valid_x86_imm(op: &P<Value>) -> bool {
qinsoon's avatar
qinsoon committed
455 456
    use std::u32;
    match op.v {
qinsoon's avatar
qinsoon committed
457
        Value_::Constant(Constant::Int(val)) if val <= u32::MAX as u64 => {
qinsoon's avatar
qinsoon committed
458 459
            true
        },
460 461
        _ => false
    }
462
}