mod.rs 11.6 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
macro_rules! alias {
85 86 87 88 89 90 91 92 93 94
    ($r8: expr, $r16: expr, $r32: expr, $r64: expr) => {
        hashmap!{
            8  => $r8.clone(),
            16 => $r16.clone(),
            32 => $r32.clone(),
            64 => $r64.clone()
        }
    };
}

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

97 98 99 100 101 102 103 104 105
macro_rules! init_alias_regs {
    ($start_id: expr, $r8: ident, $r16: ident, $r32: ident, $r64: ident, $alias: ident) => {
        lazy_static!{
            pub static ref $r8 : P<Value> = GPR8! ($start_id,     stringify!($r8).to_lowercase());
            pub static ref $r16: P<Value> = GPR16!($start_id + 1, stringify!($r16).to_lowercase());
            pub static ref $r32: P<Value> = GPR32!($start_id + 2, stringify!($r32).to_lowercase());
            pub static ref $r64: P<Value> = GPR64!($start_id + 3, stringify!($r64).to_lowercase());

            pub static ref $alias : RegisterAliasMap = alias!($r8, $r16, $r32, $r64);
106
        }
107
    }
108 109
}

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
init_alias_regs!(0,  AL  , AX  , EAX,  RAX, RAX_ALIAS);
init_alias_regs!(4,  CL  , CX  , ECX,  RCX, RCX_ALIAS);
init_alias_regs!(8,  DL  , DX  , EDX,  RDX, RDX_ALIAS);
init_alias_regs!(12, BL  , BX  , EBX,  RBX, RBX_ALIAS);
init_alias_regs!(16, SPL , SP  , ESP,  RSP, RSP_ALIAS);
init_alias_regs!(20, BPL , BP  , EBP,  RBP, RBP_ALIAS);
init_alias_regs!(24, SIL , SI  , ESI,  RSI, RSI_ALIAS);
init_alias_regs!(28, DIL , DI  , EDI,  RDI, RDI_ALIAS);
init_alias_regs!(32, R8L , R8W , R8D,  R8,  R8_ALIAS );
init_alias_regs!(36, R9L , R9W , R9D,  R9,  R9_ALIAS );
init_alias_regs!(40, R10L, R10W, R10D, R10, R10_ALIAS);
init_alias_regs!(44, R11L, R11W, R11D, R11, R11_ALIAS);
init_alias_regs!(48, R12L, R12W, R12D, R12, R12_ALIAS);
init_alias_regs!(52, R13L, R13W, R13D, R13, R13_ALIAS);
init_alias_regs!(56, R14L, R14W, R14D, R14, R14_ALIAS);
init_alias_regs!(60, R15L, R15W, R15D, R15, R15_ALIAS);
126

127
lazy_static! {
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
    pub static ref RIP : P<Value> = GPR64!(65,"rip");

    pub static ref ALL_GPR_ALIAS_MAPs : Vec<RegisterAliasMap> = vec![
        RAX_ALIAS.clone(),
        RCX_ALIAS.clone(),
        RDX_ALIAS.clone(),
        RBX_ALIAS.clone(),
        RSP_ALIAS.clone(),
        RBP_ALIAS.clone(),
        RSI_ALIAS.clone(),
        RDI_ALIAS.clone(),
        R8_ALIAS.clone(),
        R9_ALIAS.clone(),
        R10_ALIAS.clone(),
        R11_ALIAS.clone(),
        R12_ALIAS.clone(),
        R13_ALIAS.clone(),
        R14_ALIAS.clone(),
        R15_ALIAS.clone(),
    ];
148 149
}

150 151 152 153 154 155 156 157 158 159 160 161 162
macro_rules! pick_regs_of_length {
    ($len: expr) => {
        {
            let mut ret = vec![];
            for map in ALL_GPR_ALIAS_MAPs.iter() {
                match map.get(&$len) {
                    Some(reg) => ret.push(reg.clone()),
                    None => {}
                }
            }
            ret
        }
    }
163 164
}

165 166 167 168 169
macro_rules! pick_regs_of_alias {
    ($alias: ident) => {
        $alias.values().map(|x| x.clone()).collect()
    }
}
170

171 172 173 174 175
lazy_static! {
    pub static ref ALL_GPR8s  : Vec<P<Value>> = pick_regs_of_length!(8);
    pub static ref ALL_GPR16s : Vec<P<Value>> = pick_regs_of_length!(16);
    pub static ref ALL_GPR32s : Vec<P<Value>> = pick_regs_of_length!(32);
    pub static ref ALL_GPR64s : Vec<P<Value>> = pick_regs_of_length!(64);
176 177
}

178
// only use 64bit registers here
179
lazy_static!{
qinsoon's avatar
qinsoon committed
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
    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
202 203 204 205 206 207 208 209 210 211 212 213

    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
214 215 216
}

lazy_static!{
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
    pub static ref XMM0  : P<Value> = FPR!(66,"xmm0");
    pub static ref XMM1  : P<Value> = FPR!(67,"xmm1");
    pub static ref XMM2  : P<Value> = FPR!(68,"xmm2");
    pub static ref XMM3  : P<Value> = FPR!(69,"xmm3");
    pub static ref XMM4  : P<Value> = FPR!(70,"xmm4");
    pub static ref XMM5  : P<Value> = FPR!(71,"xmm5");
    pub static ref XMM6  : P<Value> = FPR!(72,"xmm6");
    pub static ref XMM7  : P<Value> = FPR!(73,"xmm7");
    pub static ref XMM8  : P<Value> = FPR!(74,"xmm8");
    pub static ref XMM9  : P<Value> = FPR!(75,"xmm9");
    pub static ref XMM10 : P<Value> = FPR!(76,"xmm10");
    pub static ref XMM11 : P<Value> = FPR!(77,"xmm11");
    pub static ref XMM12 : P<Value> = FPR!(78,"xmm12");
    pub static ref XMM13 : P<Value> = FPR!(79,"xmm13");
    pub static ref XMM14 : P<Value> = FPR!(80,"xmm14");
    pub static ref XMM15 : P<Value> = FPR!(81,"xmm15");
qinsoon's avatar
qinsoon committed
233 234 235 236 237 238
    
    pub static ref RETURN_FPRs : [P<Value>; 2] = [
        XMM0.clone(),
        XMM1.clone()
    ];
    
qinsoon's avatar
qinsoon committed
239 240 241
    pub static ref ARGUMENT_FPRs : [P<Value>; 8] = [
        XMM0.clone(),
        XMM1.clone(),
qinsoon's avatar
qinsoon committed
242 243 244 245 246 247 248
        XMM2.clone(),
        XMM3.clone(),
        XMM4.clone(),
        XMM5.clone(),
        XMM6.clone(),
        XMM7.clone()
    ];
249
    
qinsoon's avatar
qinsoon committed
250
    pub static ref CALLEE_SAVED_FPRs : [P<Value>; 0] = [];
qinsoon's avatar
qinsoon committed
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269

    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
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
    
    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(),
    ];
289 290
}

qinsoon's avatar
qinsoon committed
291
lazy_static! {
292
    pub static ref ALL_MACHINE_REGs : HashMap<MuID, P<Value>> = {
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
        let mut ret = HashMap::new();

        // gprs
        for map in ALL_GPR_ALIAS_MAPs.iter() {
            for val in map.values() {
                ret.insert(val.id(), val.clone());
            }
        }

        ret.insert(RIP.id(), RIP.clone());

        // fprs
        for val in ALL_FPRs.iter() {
            ret.insert(val.id(), val.clone());
        }

        ret
310
    };
311
    
312
    // put caller saved regs first (they imposes no overhead if there is no call instruction)
313 314 315
    pub static ref ALL_USABLE_MACHINE_REGs : Vec<P<Value>> = {
        let mut ret = vec![];

qinsoon's avatar
qinsoon committed
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 345 346
//        ret.append(&mut pick_regs_of_alias!(RAX_ALIAS));
//        ret.append(&mut pick_regs_of_alias!(RCX_ALIAS));
//        ret.append(&mut pick_regs_of_alias!(RDX_ALIAS));
//        ret.append(&mut pick_regs_of_alias!(RBX_ALIAS));
//
//        ret.append(&mut pick_regs_of_alias!(RSI_ALIAS));
//        ret.append(&mut pick_regs_of_alias!(RDI_ALIAS));
//
//        ret.append(&mut pick_regs_of_alias!(R8_ALIAS));
//        ret.append(&mut pick_regs_of_alias!(R9_ALIAS));
//        ret.append(&mut pick_regs_of_alias!(R10_ALIAS));
//        ret.append(&mut pick_regs_of_alias!(R11_ALIAS));
//        ret.append(&mut pick_regs_of_alias!(R12_ALIAS));
//        ret.append(&mut pick_regs_of_alias!(R13_ALIAS));
//        ret.append(&mut pick_regs_of_alias!(R14_ALIAS));
//        ret.append(&mut pick_regs_of_alias!(R15_ALIAS));

        ret.push(RAX.clone());
        ret.push(RCX.clone());
        ret.push(RDX.clone());
        ret.push(RBX.clone());
        ret.push(RSI.clone());
        ret.push(RDI.clone());
        ret.push(R8.clone());
        ret.push(R9.clone());
        ret.push(R10.clone());
        ret.push(R11.clone());
        ret.push(R12.clone());
        ret.push(R13.clone());
        ret.push(R14.clone());
        ret.push(R15.clone());
347

348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
        ret.push(XMM0.clone());
        ret.push(XMM1.clone());
        ret.push(XMM2.clone());
        ret.push(XMM3.clone());
        ret.push(XMM4.clone());
        ret.push(XMM5.clone());
        ret.push(XMM6.clone());
        ret.push(XMM7.clone());
        ret.push(XMM8.clone());
        ret.push(XMM9.clone());
        ret.push(XMM10.clone());
        ret.push(XMM11.clone());
        ret.push(XMM12.clone());
        ret.push(XMM13.clone());
        ret.push(XMM14.clone());
        ret.push(XMM15.clone());

        ret
    };
qinsoon's avatar
qinsoon committed
367 368
}

qinsoon's avatar
qinsoon committed
369
pub fn init_machine_regs_for_func (func_context: &mut FunctionContext) {
370
    for reg in ALL_MACHINE_REGs.values() {
qinsoon's avatar
qinsoon committed
371
        let reg_id = reg.extract_ssa_id().unwrap();
qinsoon's avatar
qinsoon committed
372
        let entry = SSAVarEntry::new(reg.clone());
qinsoon's avatar
qinsoon committed
373 374 375 376 377 378 379
        
        func_context.values.insert(reg_id, entry);
    }
}

pub fn number_of_regs_in_group(group: RegGroup) -> usize {
    match group {
qinsoon's avatar
qinsoon committed
380 381 382
        RegGroup::GPR8  => 0,
        RegGroup::GPR16 => 0,
        RegGroup::GPR32 => 0,
383 384 385
        RegGroup::GPR64 => ALL_GPR64s.len(),
        RegGroup::FPR32 => 0,
        RegGroup::FPR64 => ALL_FPRs.len(),
qinsoon's avatar
qinsoon committed
386 387 388
    }
}

qinsoon's avatar
qinsoon committed
389 390 391 392
pub fn number_of_all_regs() -> usize {
    ALL_MACHINE_REGs.len()
}

393
pub fn all_regs() -> &'static HashMap<MuID, P<Value>> {
qinsoon's avatar
qinsoon committed
394 395 396
    &ALL_MACHINE_REGs
}

397 398 399 400
pub fn all_usable_regs() -> &'static Vec<P<Value>> {
    &ALL_USABLE_MACHINE_REGs
}

401
pub fn pick_group_for_reg(reg_id: MuID) -> RegGroup {
402
    let reg = all_regs().get(&reg_id).unwrap();
403
    RegGroup::get(&reg.ty)
404 405
}

406 407 408 409 410 411 412 413 414 415
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 
}

416
pub fn is_valid_x86_imm(op: &P<Value>) -> bool {
qinsoon's avatar
qinsoon committed
417 418
    use std::u32;
    match op.v {
qinsoon's avatar
qinsoon committed
419
        Value_::Constant(Constant::Int(val)) if val <= u32::MAX as u64 => {
qinsoon's avatar
qinsoon committed
420 421
            true
        },
422 423
        _ => false
    }
424
}