mod.rs 7.57 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 15

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

macro_rules! GPR {
    ($name: expr, $id: expr) => {
        P(Value {
qinsoon's avatar
qinsoon committed
22 23
            id: $id,
            name: Some($name),
qinsoon's avatar
qinsoon committed
24 25 26 27 28 29 30 31 32
            ty: GPR_TY.clone(),
            v: Value_::SSAVar($id)
        })
    };
}

macro_rules! FPR {
    ($name: expr, $id: expr) => {
        P(Value {
qinsoon's avatar
qinsoon committed
33 34
            id: $id,
            name: Some($name),
qinsoon's avatar
qinsoon committed
35 36 37 38 39 40 41 42 43 44
            ty: FPR_TY.clone(),
            v: Value_::SSAVar($id)
        })
    };
}

lazy_static! {
    pub static ref GPR_TY : P<MuType> = P(MuType::int(64));
    pub static ref FPR_TY : P<MuType> = P(MuType::double());
}
qinsoon's avatar
qinsoon committed
45

qinsoon's avatar
qinsoon committed
46
// put into several segments to avoid 'recursion limit reached' error
47
lazy_static! {
qinsoon's avatar
qinsoon committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
    pub static ref RAX : P<Value> = GPR!("rax", 0);
    pub static ref RCX : P<Value> = GPR!("rcx", 1);
    pub static ref RDX : P<Value> = GPR!("rdx", 2);
    pub static ref RBX : P<Value> = GPR!("rbx", 3);
    pub static ref RSP : P<Value> = GPR!("rsp", 4);
    pub static ref RBP : P<Value> = GPR!("rbp", 5);
    pub static ref RSI : P<Value> = GPR!("rsi", 6);
    pub static ref RDI : P<Value> = GPR!("rdi", 7);
    pub static ref R8  : P<Value> = GPR!("r8",  8);
    pub static ref R9  : P<Value> = GPR!("r9",  9);
    pub static ref R10 : P<Value> = GPR!("r10", 10);
    pub static ref R11 : P<Value> = GPR!("r11", 11);
    pub static ref R12 : P<Value> = GPR!("r12", 12);
    pub static ref R13 : P<Value> = GPR!("r13", 13);
    pub static ref R14 : P<Value> = GPR!("r14", 14);
    pub static ref R15 : P<Value> = GPR!("r15", 15);
    
65 66
    pub static ref RIP : P<Value> = GPR!("rip", 32);
    
qinsoon's avatar
qinsoon committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    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()
    ];
89
    
90
    pub static ref ALL_GPRs : [P<Value>; 15] = [
91 92 93 94 95
        RAX.clone(),
        RCX.clone(),
        RDX.clone(),
        RBX.clone(),
        RSP.clone(),
96
//        RBP.clone(),
97 98 99 100 101 102 103 104 105 106 107
        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
108 109 110
}

lazy_static!{
qinsoon's avatar
qinsoon committed
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
    pub static ref XMM0  : P<Value> = FPR!("xmm0", 16);
    pub static ref XMM1  : P<Value> = FPR!("xmm1", 17);
    pub static ref XMM2  : P<Value> = FPR!("xmm2", 18);
    pub static ref XMM3  : P<Value> = FPR!("xmm3", 19);
    pub static ref XMM4  : P<Value> = FPR!("xmm4", 20);
    pub static ref XMM5  : P<Value> = FPR!("xmm5", 21);
    pub static ref XMM6  : P<Value> = FPR!("xmm6", 22);
    pub static ref XMM7  : P<Value> = FPR!("xmm7", 23);
    pub static ref XMM8  : P<Value> = FPR!("xmm8", 24);
    pub static ref XMM9  : P<Value> = FPR!("xmm9", 25);
    pub static ref XMM10 : P<Value> = FPR!("xmm10",26);
    pub static ref XMM11 : P<Value> = FPR!("xmm11",27);
    pub static ref XMM12 : P<Value> = FPR!("xmm12",28);
    pub static ref XMM13 : P<Value> = FPR!("xmm13",29);
    pub static ref XMM14 : P<Value> = FPR!("xmm14",30);
    pub static ref XMM15 : P<Value> = FPR!("xmm15",31); 
qinsoon's avatar
qinsoon committed
127 128 129 130 131 132 133 134 135 136 137 138 139 140
    
    pub static ref RETURN_FPRs : [P<Value>; 2] = [
        XMM0.clone(),
        XMM1.clone()
    ];
    
    pub static ref ARGUMENT_FPRs : [P<Value>; 6] = [
        XMM2.clone(),
        XMM3.clone(),
        XMM4.clone(),
        XMM5.clone(),
        XMM6.clone(),
        XMM7.clone()
    ];
141
    
qinsoon's avatar
qinsoon committed
142
    pub static ref CALLEE_SAVED_FPRs : [P<Value>; 0] = [];
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
    
    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(),
    ];
162 163
}

qinsoon's avatar
qinsoon committed
164 165 166
pub const GPR_COUNT : usize = 16;
pub const FPR_COUNT : usize = 16;

qinsoon's avatar
qinsoon committed
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
lazy_static! {
    pub static ref ALL_MACHINE_REGs : Vec<P<Value>> = vec![
        RAX.clone(),
        RCX.clone(),
        RDX.clone(),
        RBX.clone(),
        RSP.clone(),
        RBP.clone(),
        RSI.clone(),
        RDI.clone(),
        R8.clone(),
        R9.clone(),
        R10.clone(),
        R11.clone(),
        R12.clone(),
        R13.clone(),
        R14.clone(),
        R15.clone(),
        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(),
200 201
        XMM15.clone(),
        RIP.clone()
qinsoon's avatar
qinsoon committed
202
    ];
203
    
204
    // put callee saved regs first
205
    pub static ref ALL_USABLE_MACHINE_REGs : Vec<P<Value>> = vec![
206 207 208 209 210 211
        RBX.clone(),
        R12.clone(),
        R13.clone(),
        R14.clone(),
        R15.clone(),
            
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
        RAX.clone(),
        RCX.clone(),
        RDX.clone(),
        RSI.clone(),
        RDI.clone(),
        R8.clone(),
        R9.clone(),
        R10.clone(),
        R11.clone(),
        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
238 239
}

240 241 242 243 244 245 246
pub fn init_machine_regs_for_func (func_context: &mut FunctionContext) {
    use std::cell::Cell;
    
    for reg in ALL_MACHINE_REGs.iter() {
        let reg_id = reg.extract_ssa_id().unwrap();
        let entry = SSAVarEntry {
            id: reg_id,
qinsoon's avatar
qinsoon committed
247
            name: reg.name, 
248 249 250 251 252
            ty: reg.ty.clone(),
            use_count: Cell::new(0),
            expr: None
        };
        
qinsoon's avatar
qinsoon committed
253
        func_context.value_tags.insert(reg.name.unwrap(), reg_id);
254 255 256 257 258 259 260 261
        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
262 263 264
    }
}

265 266 267 268 269 270 271 272
pub fn number_of_all_regs() -> usize {
    ALL_MACHINE_REGs.len()
}

pub fn all_regs() -> &'static Vec<P<Value>> {
    &ALL_MACHINE_REGs
}

273 274 275 276
pub fn all_usable_regs() -> &'static Vec<P<Value>> {
    &ALL_USABLE_MACHINE_REGs
}

277 278 279 280 281 282 283 284
pub fn pick_group_for_reg(reg_id: MuID) -> RegGroup {
    match reg_id {
        0...15  => RegGroup::GPR,
        16...31 => RegGroup::FPR,
        _ => panic!("expected a machine reg ID, got {}", reg_id)
    }
}

285 286 287 288 289 290 291 292 293 294
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 
}

295
pub fn is_valid_x86_imm(op: &P<Value>) -> bool {
qinsoon's avatar
qinsoon committed
296 297 298 299 300
    use std::u32;
    match op.v {
        Value_::Constant(Constant::Int(val)) if val <= u32::MAX as usize => {
            true
        },
301 302
        _ => false
    }
303
}