WARNING! Access to this system is limited to authorised users only.
Unauthorised users may be subject to prosecution.
Unauthorised access to this system is a criminal offence under Australian law (Federal Crimes Act 1914 Part VIA)
It is a criminal offence to:
(1) Obtain access to data without authority. -Penalty 2 years imprisonment.
(2) Damage, delete, alter or insert data without authority. -Penalty 10 years imprisonment.
User activity is monitored and recorded. Anyone using this system expressly consents to such monitoring and recording.

To protect your data, the CISO officer has suggested users to enable 2FA as soon as possible.
Currently 2.7% of users enabled 2FA.

mod.rs 11.2 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;
qinsoon's avatar
qinsoon committed
20

21
22
use std::collections::HashMap;

qinsoon's avatar
qinsoon committed
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
macro_rules! GPR_ALIAS {
    ($alias: ident: ($id64: expr, $r64: ident) -> $r32: ident, $r16: ident, $r8: ident) => {
        lazy_static!{
            pub static ref $r64 : P<Value> = GPR!($id64,    stringify!($r64), UINT64_TYPE);
            pub static ref $r32 : P<Value> = GPR!($id64 +1, stringify!($r32), UINT32_TYPE);
            pub static ref $r16 : P<Value> = GPR!($id64 +2, stringify!($r16), UINT16_TYPE);
            pub static ref $r8  : P<Value> = GPR!($id64 +3, stringify!($r8) , UINT8_TYPE );

            pub static ref $alias : [P<Value>; 4] = [$r64.clone(), $r32.clone(), $r16.clone(), $r8.clone()];
        }
    };

    ($alias: ident: ($id64: expr, $r64: ident)) => {
        lazy_static!{
            pub static ref $r64 : P<Value> = GPR!($id64,    stringify!($r64), UINT64_TYPE);

            pub static ref $alias : [P<Value>; 4] = [$r64.clone(), $r64.clone(), $r64.clone(), $r64.clone()];
        }
    };
}

44
macro_rules! GPR {
qinsoon's avatar
qinsoon committed
45
    ($id:expr, $name: expr, $ty: ident) => {
qinsoon's avatar
qinsoon committed
46
47
        {
            P(Value {
qinsoon's avatar
qinsoon committed
48
                hdr: MuEntityHeader::named($id, $name.to_string()),
qinsoon's avatar
qinsoon committed
49
                ty: $ty.clone(),
qinsoon's avatar
qinsoon committed
50
                v: Value_::SSAVar($id)
qinsoon's avatar
qinsoon committed
51
52
            })
        }
qinsoon's avatar
qinsoon committed
53
54
55
56
    };
}

macro_rules! FPR {
qinsoon's avatar
qinsoon committed
57
    ($id:expr, $name: expr) => {
qinsoon's avatar
qinsoon committed
58
59
        {
            P(Value {
qinsoon's avatar
qinsoon committed
60
                hdr: MuEntityHeader::named($id, $name.to_string()),
qinsoon's avatar
qinsoon committed
61
                ty: DOUBLE_TYPE.clone(),
qinsoon's avatar
qinsoon committed
62
                v: Value_::SSAVar($id)
qinsoon's avatar
qinsoon committed
63
64
            })
        }
qinsoon's avatar
qinsoon committed
65
66
67
    };
}

qinsoon's avatar
qinsoon committed
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
GPR_ALIAS!(RAX_ALIAS: (0, RAX)  -> EAX, AX , AL);
GPR_ALIAS!(RCX_ALIAS: (4, RCX)  -> ECX, CX , CL);
GPR_ALIAS!(RDX_ALIAS: (8, RDX)  -> EDX, DX , DL);
GPR_ALIAS!(RBX_ALIAS: (12,RBX)  -> EBX, BX , BL);
GPR_ALIAS!(RSP_ALIAS: (16,RSP)  -> ESP, SP , SPL);
GPR_ALIAS!(RBP_ALIAS: (20,RBP)  -> EBP, BP , BPL);
GPR_ALIAS!(RSI_ALIAS: (24,RSI)  -> ESI, SI , SIL);
GPR_ALIAS!(RDI_ALIAS: (28,RDI)  -> EDI, DI , DIL);
GPR_ALIAS!(R8_ALIAS : (32,R8 )  -> R8D, R8W, R8L);
GPR_ALIAS!(R9_ALIAS : (36,R9 )  -> R9D, R9W, R9L);
GPR_ALIAS!(R10_ALIAS: (40,R10) -> R10D,R10W,R10L);
GPR_ALIAS!(R11_ALIAS: (44,R11) -> R11D,R11W,R11L);
GPR_ALIAS!(R12_ALIAS: (48,R12) -> R12D,R12W,R12L);
GPR_ALIAS!(R13_ALIAS: (52,R13) -> R13D,R13W,R13L);
GPR_ALIAS!(R14_ALIAS: (56,R14) -> R14D,R14W,R14L);
GPR_ALIAS!(R15_ALIAS: (60,R15) -> R15D,R15W,R15L);
GPR_ALIAS!(RIP_ALIAS: (64,RIP));

qinsoon's avatar
qinsoon committed
86
lazy_static! {
qinsoon's avatar
qinsoon committed
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
116
117
118
119
120
121
122
123
124
125
    pub static ref GPR_ALIAS_LOOKUP_TABLE : HashMap<MuID, Vec<P<Value>>> = {
        let mut ret = HashMap::new();

        ret.insert(RAX.id(), RAX_ALIAS.to_vec());
        ret.insert(RCX.id(), RCX_ALIAS.to_vec());
        ret.insert(RDX.id(), RDX_ALIAS.to_vec());
        ret.insert(RBX.id(), RBX_ALIAS.to_vec());
        ret.insert(RSP.id(), RSP_ALIAS.to_vec());
        ret.insert(RBP.id(), RBP_ALIAS.to_vec());
        ret.insert(RSI.id(), RSI_ALIAS.to_vec());
        ret.insert(RDI.id(), RDI_ALIAS.to_vec());
        ret.insert(R8.id() , R8_ALIAS.to_vec() );
        ret.insert(R9.id() , R9_ALIAS.to_vec() );
        ret.insert(R10.id(), R10_ALIAS.to_vec());
        ret.insert(R11.id(), R11_ALIAS.to_vec());
        ret.insert(R12.id(), R12_ALIAS.to_vec());
        ret.insert(R13.id(), R13_ALIAS.to_vec());
        ret.insert(R14.id(), R14_ALIAS.to_vec());
        ret.insert(R15.id(), R15_ALIAS.to_vec());
        ret.insert(RIP.id(), RIP_ALIAS.to_vec());

        ret
    };
}

pub fn get_gpr_alias(id: MuID, length: usize) -> P<Value> {
    let vec = match GPR_ALIAS_LOOKUP_TABLE.get(&id) {
        Some(vec) => vec,
        None => panic!("didnt find {} as GPR", id)
    };

    match length {
        64 => vec[0].clone(),
        32 => vec[1].clone(),
        16 => vec[2].clone(),
        8  => vec[3].clone(),
        _  => panic!("unexpected length: {}", length)
    }
}
qinsoon's avatar
qinsoon committed
126

qinsoon's avatar
qinsoon committed
127
lazy_static! {
qinsoon's avatar
qinsoon committed
128
129
130
131
    pub static ref RETURN_GPRs : [P<Value>; 2] = [
        RAX.clone(),
        RDX.clone(),
    ];
132

qinsoon's avatar
qinsoon committed
133
134
135
136
137
138
139
140
    pub static ref ARGUMENT_GPRs : [P<Value>; 6] = [
        RDI.clone(),
        RSI.clone(),
        RDX.clone(),
        RCX.clone(),
        R8.clone(),
        R9.clone()
    ];
141

qinsoon's avatar
qinsoon committed
142
143
144
145
146
147
148
149
    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
150
151
152
153
154
155
156
157
158
159
160
161

    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()
    ];
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180

    pub static ref ALL_GPRs : [P<Value>; 15] = [
        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()
    ];
qinsoon's avatar
qinsoon committed
181
182
183
}

lazy_static!{
qinsoon's avatar
qinsoon committed
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
    pub static ref XMM0  : P<Value> = FPR!(70,"xmm0");
    pub static ref XMM1  : P<Value> = FPR!(71,"xmm1");
    pub static ref XMM2  : P<Value> = FPR!(72,"xmm2");
    pub static ref XMM3  : P<Value> = FPR!(73,"xmm3");
    pub static ref XMM4  : P<Value> = FPR!(74,"xmm4");
    pub static ref XMM5  : P<Value> = FPR!(75,"xmm5");
    pub static ref XMM6  : P<Value> = FPR!(76,"xmm6");
    pub static ref XMM7  : P<Value> = FPR!(77,"xmm7");
    pub static ref XMM8  : P<Value> = FPR!(78,"xmm8");
    pub static ref XMM9  : P<Value> = FPR!(79,"xmm9");
    pub static ref XMM10 : P<Value> = FPR!(80,"xmm10");
    pub static ref XMM11 : P<Value> = FPR!(81,"xmm11");
    pub static ref XMM12 : P<Value> = FPR!(82,"xmm12");
    pub static ref XMM13 : P<Value> = FPR!(83,"xmm13");
    pub static ref XMM14 : P<Value> = FPR!(84,"xmm14");
    pub static ref XMM15 : P<Value> = FPR!(85,"xmm15");
200

qinsoon's avatar
qinsoon committed
201
202
203
204
    pub static ref RETURN_FPRs : [P<Value>; 2] = [
        XMM0.clone(),
        XMM1.clone()
    ];
205

qinsoon's avatar
qinsoon committed
206
207
208
    pub static ref ARGUMENT_FPRs : [P<Value>; 8] = [
        XMM0.clone(),
        XMM1.clone(),
qinsoon's avatar
qinsoon committed
209
210
211
212
213
214
215
        XMM2.clone(),
        XMM3.clone(),
        XMM4.clone(),
        XMM5.clone(),
        XMM6.clone(),
        XMM7.clone()
    ];
216

qinsoon's avatar
qinsoon committed
217
    pub static ref CALLEE_SAVED_FPRs : [P<Value>; 0] = [];
qinsoon's avatar
qinsoon committed
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236

    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(),
    ];
237

qinsoon's avatar
qinsoon committed
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
    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(),
    ];
qinsoon's avatar
qinsoon committed
256
257
}

258
259
260
pub const GPR_COUNT : usize = 16;
pub const FPR_COUNT : usize = 16;

qinsoon's avatar
qinsoon committed
261
lazy_static! {
262
    pub static ref ALL_MACHINE_REGs : HashMap<MuID, P<Value>> = {
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
        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
    };
300

301
302
303
304
305
306
307
308
309
310
311
    // put caller saved regs first (they imposes no overhead if there is no call instruction)
    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(),
312

313
314
315
316
317
        RBX.clone(),
        R12.clone(),
        R13.clone(),
        R14.clone(),
        R15.clone(),
318

319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
        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
336
337
}

qinsoon's avatar
qinsoon committed
338
pub fn init_machine_regs_for_func (func_context: &mut FunctionContext) {
339
    for reg in ALL_MACHINE_REGs.values() {
qinsoon's avatar
qinsoon committed
340
        let reg_id = reg.extract_ssa_id().unwrap();
qinsoon's avatar
qinsoon committed
341
        let entry = SSAVarEntry::new(reg.clone());
342

qinsoon's avatar
qinsoon committed
343
344
345
346
347
348
        func_context.values.insert(reg_id, entry);
    }
}

pub fn number_of_regs_in_group(group: RegGroup) -> usize {
    match group {
349
350
        RegGroup::GPR => ALL_GPRs.len(),
        RegGroup::FPR => ALL_FPRs.len()
qinsoon's avatar
qinsoon committed
351
352
353
    }
}

qinsoon's avatar
qinsoon committed
354
355
356
357
pub fn number_of_all_regs() -> usize {
    ALL_MACHINE_REGs.len()
}

358
pub fn all_regs() -> &'static HashMap<MuID, P<Value>> {
qinsoon's avatar
qinsoon committed
359
360
361
    &ALL_MACHINE_REGs
}

362
363
364
365
pub fn all_usable_regs() -> &'static Vec<P<Value>> {
    &ALL_USABLE_MACHINE_REGs
}

366
pub fn pick_group_for_reg(reg_id: MuID) -> RegGroup {
367
    let reg = all_regs().get(&reg_id).unwrap();
368
369
370
371
372
373
374
    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)
    }
375
376
}

377
378
379
380
381
382
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;
        }
    }
383
384

    false
385
386
}

387
pub fn is_valid_x86_imm(op: &P<Value>) -> bool {
388
    use std::i32;
qinsoon's avatar
qinsoon committed
389
    match op.v {
390
        Value_::Constant(Constant::Int(val)) if val <= i32::MAX as u64 => {
qinsoon's avatar
qinsoon committed
391
392
            true
        },
393
394
        _ => false
    }
395
}