To protect your data, the CISO officer has suggested users to enable GitLab 2FA as soon as possible.

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

qinsoon's avatar
qinsoon committed
3
/// Tree pattern matching instruction selection.
4
5
6
pub mod inst_sel;

mod codegen;
qinsoon's avatar
qinsoon committed
7
8
9
/// CodeGenerator trait serves as an interface to the backend code generator, which
/// may generate assembly code or binary (not implemented yet)
use compiler::backend::x86_64::codegen::CodeGenerator;
10

qinsoon's avatar
qinsoon committed
11
12
13
14
15
16
/// assembly backend as AOT compiler
mod asm_backend;
use compiler::backend::x86_64::asm_backend::ASMCodeGen;

// re-export a few functions for AOT compilation
#[cfg(feature = "aot")]
17
pub use compiler::backend::x86_64::asm_backend::emit_code;
qinsoon's avatar
qinsoon committed
18
#[cfg(feature = "aot")]
19
pub use compiler::backend::x86_64::asm_backend::emit_context;
qinsoon's avatar
qinsoon committed
20
#[cfg(feature = "aot")]
qinsoon's avatar
qinsoon committed
21
pub use compiler::backend::x86_64::asm_backend::emit_context_with_reloc;
22
23
#[cfg(feature = "aot")]
pub use compiler::backend::x86_64::asm_backend::spill_rewrite;
24
25
26

use ast::ptr::P;
use ast::ir::*;
qinsoon's avatar
qinsoon committed
27
use ast::types::*;
qinsoon's avatar
qinsoon committed
28
use compiler::backend::RegGroup;
qinsoon's avatar
qinsoon committed
29

30
use utils::LinkedHashMap;
31
32
use std::collections::HashMap;

qinsoon's avatar
qinsoon committed
33
/// a macro to declare a set of general purpose registers that are aliased to the first one
qinsoon's avatar
qinsoon committed
34
macro_rules! GPR_ALIAS {
qinsoon's avatar
qinsoon committed
35
36
37
38
39
40
41
42
43
44
45
46
    ($alias: ident: ($id64: expr, $r64: ident) -> $r32: ident, $r16: ident, $r8l: ident, $r8h: 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 $r8l : P<Value> = GPR!($id64 +3, stringify!($r8l), UINT8_TYPE);
            pub static ref $r8h : P<Value> = GPR!($id64 +4, stringify!($r8h), UINT8_TYPE);

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

qinsoon's avatar
qinsoon committed
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
    ($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()];
        }
    };
}

qinsoon's avatar
qinsoon committed
67
/// a macro to declare a general purpose register
68
macro_rules! GPR {
qinsoon's avatar
qinsoon committed
69
    ($id:expr, $name: expr, $ty: ident) => {
qinsoon's avatar
qinsoon committed
70
71
        {
            P(Value {
qinsoon's avatar
qinsoon committed
72
                hdr: MuEntityHeader::named($id, $name.to_string()),
qinsoon's avatar
qinsoon committed
73
                ty: $ty.clone(),
qinsoon's avatar
qinsoon committed
74
                v: Value_::SSAVar($id)
qinsoon's avatar
qinsoon committed
75
76
            })
        }
qinsoon's avatar
qinsoon committed
77
78
79
    };
}

qinsoon's avatar
qinsoon committed
80
/// a macro to declare a floating point register
qinsoon's avatar
qinsoon committed
81
macro_rules! FPR {
qinsoon's avatar
qinsoon committed
82
    ($id:expr, $name: expr) => {
qinsoon's avatar
qinsoon committed
83
84
        {
            P(Value {
qinsoon's avatar
qinsoon committed
85
                hdr: MuEntityHeader::named($id, $name.to_string()),
qinsoon's avatar
qinsoon committed
86
                ty: DOUBLE_TYPE.clone(),
qinsoon's avatar
qinsoon committed
87
                v: Value_::SSAVar($id)
qinsoon's avatar
qinsoon committed
88
89
            })
        }
qinsoon's avatar
qinsoon committed
90
91
92
    };
}

qinsoon's avatar
qinsoon committed
93
94
95
// declare all general purpose registers for x86_64
// non 64-bit registers are alias of its 64-bit one

96
97
GPR_ALIAS!(RAX_ALIAS: (0, RAX)  -> EAX, AX , AL, AH);
GPR_ALIAS!(RCX_ALIAS: (5, RCX)  -> ECX, CX , CL, CH);
98
GPR_ALIAS!(RDX_ALIAS: (10,RDX)  -> EDX, DX , DL, DH);
99
100
101
102
103
104
105
106
107
108
109
110
111
112
GPR_ALIAS!(RBX_ALIAS: (15,RBX)  -> EBX, BX , BL, BH);
GPR_ALIAS!(RSP_ALIAS: (20,RSP)  -> ESP, SP , SPL);
GPR_ALIAS!(RBP_ALIAS: (24,RBP)  -> EBP, BP , BPL);
GPR_ALIAS!(RSI_ALIAS: (28,RSI)  -> ESI, SI , SIL);
GPR_ALIAS!(RDI_ALIAS: (32,RDI)  -> EDI, DI , DIL);
GPR_ALIAS!(R8_ALIAS : (36,R8 )  -> R8D, R8W, R8B);
GPR_ALIAS!(R9_ALIAS : (40,R9 )  -> R9D, R9W, R9B);
GPR_ALIAS!(R10_ALIAS: (44,R10) -> R10D,R10W,R10B);
GPR_ALIAS!(R11_ALIAS: (48,R11) -> R11D,R11W,R11B);
GPR_ALIAS!(R12_ALIAS: (52,R12) -> R12D,R12W,R12B);
GPR_ALIAS!(R13_ALIAS: (56,R13) -> R13D,R13W,R13B);
GPR_ALIAS!(R14_ALIAS: (60,R14) -> R14D,R14W,R14B);
GPR_ALIAS!(R15_ALIAS: (64,R15) -> R15D,R15W,R15B);
GPR_ALIAS!(RIP_ALIAS: (68,RIP));
qinsoon's avatar
qinsoon committed
113

qinsoon's avatar
qinsoon committed
114
lazy_static! {
qinsoon's avatar
qinsoon committed
115
116
    /// a map from 64-bit register IDs to a vector of its aliased register (Values),
    /// including the 64-bit register
117
118
    pub static ref GPR_ALIAS_TABLE : LinkedHashMap<MuID, Vec<P<Value>>> = {
        let mut ret = LinkedHashMap::new();
qinsoon's avatar
qinsoon committed
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140

        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
    };

qinsoon's avatar
qinsoon committed
141
    /// a map from any register to its 64-bit alias
qinsoon's avatar
qinsoon committed
142
143
144
145
146
147
148
149
150
151
152
153
    pub static ref GPR_ALIAS_LOOKUP : HashMap<MuID, P<Value>> = {
        let mut ret = HashMap::new();

        for vec in GPR_ALIAS_TABLE.values() {
            let colorable = vec[0].clone();

            for gpr in vec {
                ret.insert(gpr.id(), colorable.clone());
            }
        }

        ret
qinsoon's avatar
qinsoon committed
154
    };
qinsoon's avatar
qinsoon committed
155
156
}

qinsoon's avatar
qinsoon committed
157
158
/// returns P<Value> for a register ID of its alias of the given length
/// panics if the ID is not a machine register ID
qinsoon's avatar
qinsoon committed
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
pub fn get_alias_for_length(id: MuID, length: usize) -> P<Value> {
    if id < FPR_ID_START {
        let vec = match GPR_ALIAS_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(),
            1 => vec[3].clone(),
            _ => panic!("unexpected length {} for {}", length, vec[0])
        }
    } else {
qinsoon's avatar
qinsoon committed
175
        for r in ALL_FPRS.iter() {
qinsoon's avatar
qinsoon committed
176
177
178
179
180
181
182
183
            if r.id() == id {
                return r.clone();
            }
        }

        panic!("didnt find {} as FPR", id)
    }
}
qinsoon's avatar
qinsoon committed
184

qinsoon's avatar
qinsoon committed
185
/// are two registers aliased? (both must be machine register IDs, otherwise this function panics)
186
pub fn is_aliased(id1: MuID, id2: MuID) -> bool {
qinsoon's avatar
qinsoon committed
187
    if get_color_for_precolored(id1) == get_color_for_precolored(id2) {
qinsoon's avatar
qinsoon committed
188
        true
189
    } else {
qinsoon's avatar
qinsoon committed
190
        false
191
192
193
    }
}

qinsoon's avatar
mod.rs    
qinsoon committed
194
/// gets the color for a machine register (returns 64-bit alias for it)
qinsoon's avatar
qinsoon committed
195
196
197
pub fn get_color_for_precolored(id: MuID) -> MuID {
    debug_assert!(id < MACHINE_ID_END);

qinsoon's avatar
qinsoon committed
198
199
200
201
202
203
204
205
    if id < FPR_ID_START {
        match GPR_ALIAS_LOOKUP.get(&id) {
            Some(val) => val.id(),
            None => panic!("cannot find GPR {}", id)
        }
    } else {
        // we do not have alias for FPRs
        id
qinsoon's avatar
qinsoon committed
206
207
    }
}
qinsoon's avatar
qinsoon committed
208

qinsoon's avatar
mod.rs    
qinsoon committed
209
/// returns register length (in bits) for an integer operand
qinsoon's avatar
qinsoon committed
210
211
212
213
214
215
216
217
#[inline(always)]
pub fn check_op_len(op: &P<Value>) -> usize {
    match op.ty.get_int_length() {
        Some(64) => 64,
        Some(32) => 32,
        Some(16) => 16,
        Some(8)  => 8,
        Some(1)  => 8,
qinsoon's avatar
mod.rs    
qinsoon committed
218
        _ => panic!("unsupported register length for x64: {}", op.ty)
qinsoon's avatar
qinsoon committed
219
220
221
    }
}

qinsoon's avatar
qinsoon committed
222
lazy_static! {
qinsoon's avatar
mod.rs    
qinsoon committed
223
224
    /// GPRs for returning values
    //  order matters
qinsoon's avatar
qinsoon committed
225
    pub static ref RETURN_GPRS : [P<Value>; 2] = [
qinsoon's avatar
qinsoon committed
226
227
228
        RAX.clone(),
        RDX.clone(),
    ];
229

qinsoon's avatar
mod.rs    
qinsoon committed
230
231
    /// GPRs for passing arguments
    //  order matters
qinsoon's avatar
qinsoon committed
232
    pub static ref ARGUMENT_GPRS : [P<Value>; 6] = [
qinsoon's avatar
qinsoon committed
233
234
235
236
237
238
239
        RDI.clone(),
        RSI.clone(),
        RDX.clone(),
        RCX.clone(),
        R8.clone(),
        R9.clone()
    ];
240

qinsoon's avatar
mod.rs    
qinsoon committed
241
    /// callee saved GPRs
qinsoon's avatar
qinsoon committed
242
    pub static ref CALLEE_SAVED_GPRS : [P<Value>; 6] = [
qinsoon's avatar
qinsoon committed
243
244
245
246
247
248
249
        RBX.clone(),
        RBP.clone(),
        R12.clone(),
        R13.clone(),
        R14.clone(),
        R15.clone()
    ];
qinsoon's avatar
qinsoon committed
250

qinsoon's avatar
mod.rs    
qinsoon committed
251
    /// caller saved GPRs
qinsoon's avatar
qinsoon committed
252
    pub static ref CALLER_SAVED_GPRS : [P<Value>; 9] = [
qinsoon's avatar
qinsoon committed
253
254
255
256
257
258
259
260
261
262
        RAX.clone(),
        RCX.clone(),
        RDX.clone(),
        RSI.clone(),
        RDI.clone(),
        R8.clone(),
        R9.clone(),
        R10.clone(),
        R11.clone()
    ];
263

qinsoon's avatar
mod.rs    
qinsoon committed
264
265
    /// all the genral purpose registers
    //  FIXME: why RBP is commented out?
qinsoon's avatar
qinsoon committed
266
    static ref ALL_GPRS : [P<Value>; 15] = [
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
        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
284
285
}

286
pub const FPR_ID_START : usize = 100;
qinsoon's avatar
qinsoon committed
287

qinsoon's avatar
qinsoon committed
288
lazy_static!{
qinsoon's avatar
mod.rs    
qinsoon committed
289
    // floating point registers, we use SSE registers
qinsoon's avatar
qinsoon committed
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
    pub static ref XMM0  : P<Value> = FPR!(FPR_ID_START,    "xmm0");
    pub static ref XMM1  : P<Value> = FPR!(FPR_ID_START + 1,"xmm1");
    pub static ref XMM2  : P<Value> = FPR!(FPR_ID_START + 2,"xmm2");
    pub static ref XMM3  : P<Value> = FPR!(FPR_ID_START + 3,"xmm3");
    pub static ref XMM4  : P<Value> = FPR!(FPR_ID_START + 4,"xmm4");
    pub static ref XMM5  : P<Value> = FPR!(FPR_ID_START + 5,"xmm5");
    pub static ref XMM6  : P<Value> = FPR!(FPR_ID_START + 6,"xmm6");
    pub static ref XMM7  : P<Value> = FPR!(FPR_ID_START + 7,"xmm7");
    pub static ref XMM8  : P<Value> = FPR!(FPR_ID_START + 8,"xmm8");
    pub static ref XMM9  : P<Value> = FPR!(FPR_ID_START + 9,"xmm9");
    pub static ref XMM10 : P<Value> = FPR!(FPR_ID_START + 10,"xmm10");
    pub static ref XMM11 : P<Value> = FPR!(FPR_ID_START + 11,"xmm11");
    pub static ref XMM12 : P<Value> = FPR!(FPR_ID_START + 12,"xmm12");
    pub static ref XMM13 : P<Value> = FPR!(FPR_ID_START + 13,"xmm13");
    pub static ref XMM14 : P<Value> = FPR!(FPR_ID_START + 14,"xmm14");
    pub static ref XMM15 : P<Value> = FPR!(FPR_ID_START + 15,"xmm15");
306

qinsoon's avatar
mod.rs    
qinsoon committed
307
308
    /// FPRs to return values
    //  order matters
qinsoon's avatar
qinsoon committed
309
    pub static ref RETURN_FPRS : [P<Value>; 2] = [
qinsoon's avatar
qinsoon committed
310
311
312
        XMM0.clone(),
        XMM1.clone()
    ];
313

qinsoon's avatar
mod.rs    
qinsoon committed
314
315
    /// FPRs to pass arguments
    //  order matters
qinsoon's avatar
qinsoon committed
316
    pub static ref ARGUMENT_FPRS : [P<Value>; 8] = [
qinsoon's avatar
qinsoon committed
317
318
        XMM0.clone(),
        XMM1.clone(),
qinsoon's avatar
qinsoon committed
319
320
321
322
323
324
325
        XMM2.clone(),
        XMM3.clone(),
        XMM4.clone(),
        XMM5.clone(),
        XMM6.clone(),
        XMM7.clone()
    ];
326

qinsoon's avatar
mod.rs    
qinsoon committed
327
    /// callee saved FPRs (none for x86_64)
qinsoon's avatar
qinsoon committed
328
    pub static ref CALLEE_SAVED_FPRS : [P<Value>; 0] = [];
qinsoon's avatar
qinsoon committed
329

qinsoon's avatar
mod.rs    
qinsoon committed
330
    /// caller saved FPRs
qinsoon's avatar
qinsoon committed
331
    pub static ref CALLER_SAVED_FPRS : [P<Value>; 16] = [
qinsoon's avatar
qinsoon committed
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
        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(),
    ];
349

qinsoon's avatar
mod.rs    
qinsoon committed
350
    /// all the floating point registers
qinsoon's avatar
qinsoon committed
351
    static ref ALL_FPRS : [P<Value>; 16] = [
qinsoon's avatar
qinsoon committed
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
        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
369
370
}

qinsoon's avatar
qinsoon committed
371
lazy_static! {
qinsoon's avatar
mod.rs    
qinsoon committed
372
    /// a map for all the machine registers, from ID to P<Value>
qinsoon's avatar
qinsoon committed
373
    pub static ref ALL_MACHINE_REGS : LinkedHashMap<MuID, P<Value>> = {
374
        let mut map = LinkedHashMap::new();
qinsoon's avatar
qinsoon committed
375
376
377
378
379
380
381

        for vec in GPR_ALIAS_TABLE.values() {
            for reg in vec {
                map.insert(reg.id(), reg.clone());
            }
        }

382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
        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
    };
401

qinsoon's avatar
mod.rs    
qinsoon committed
402
403
404
405
    /// all the usable registers for register allocators to assign
    //  order matters here (since register allocator will prioritize assigning temporaries
    //  to a register that appears early)
    //  we put caller saved regs first (they imposes no overhead if there is no call instruction)
qinsoon's avatar
qinsoon committed
406
    pub static ref ALL_USABLE_MACHINE_REGS : Vec<P<Value>> = vec![
qinsoon's avatar
mod.rs    
qinsoon committed
407
        // caller saved registers
408
409
410
411
412
413
414
415
416
        RAX.clone(),
        RCX.clone(),
        RDX.clone(),
        RSI.clone(),
        RDI.clone(),
        R8.clone(),
        R9.clone(),
        R10.clone(),
        R11.clone(),
qinsoon's avatar
mod.rs    
qinsoon committed
417
        // callee saved registers
418
419
420
421
422
        RBX.clone(),
        R12.clone(),
        R13.clone(),
        R14.clone(),
        R15.clone(),
qinsoon's avatar
mod.rs    
qinsoon committed
423
        // floating point registers
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
        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
441
442
}

qinsoon's avatar
mod.rs    
qinsoon committed
443
/// creates context for each machine register in FunctionContext
qinsoon's avatar
qinsoon committed
444
pub fn init_machine_regs_for_func (func_context: &mut FunctionContext) {
qinsoon's avatar
qinsoon committed
445
    for reg in ALL_MACHINE_REGS.values() {
qinsoon's avatar
qinsoon committed
446
        let reg_id = reg.extract_ssa_id().unwrap();
qinsoon's avatar
qinsoon committed
447
        let entry = SSAVarEntry::new(reg.clone());
448

qinsoon's avatar
qinsoon committed
449
450
451
452
        func_context.values.insert(reg_id, entry);
    }
}

qinsoon's avatar
mod.rs    
qinsoon committed
453
/// gets the number of registers in a certain register group
qinsoon's avatar
qinsoon committed
454
455
pub fn number_of_regs_in_group(group: RegGroup) -> usize {
    match group {
qinsoon's avatar
qinsoon committed
456
457
458
        RegGroup::GPR   => ALL_GPRS.len(),
        RegGroup::GPREX => ALL_GPRS.len(),
        RegGroup::FPR   => ALL_FPRS.len()
qinsoon's avatar
qinsoon committed
459
460
461
    }
}

qinsoon's avatar
mod.rs    
qinsoon committed
462
/// returns the number of all registers on this platform
qinsoon's avatar
qinsoon committed
463
pub fn number_of_all_regs() -> usize {
qinsoon's avatar
qinsoon committed
464
    ALL_MACHINE_REGS.len()
qinsoon's avatar
qinsoon committed
465
466
}

qinsoon's avatar
mod.rs    
qinsoon committed
467
/// returns a reference to a map for all the registers
468
pub fn all_regs() -> &'static LinkedHashMap<MuID, P<Value>> {
qinsoon's avatar
qinsoon committed
469
    &ALL_MACHINE_REGS
qinsoon's avatar
qinsoon committed
470
471
}

qinsoon's avatar
mod.rs    
qinsoon committed
472
/// returns a reference to a vector of all usable registers
473
pub fn all_usable_regs() -> &'static Vec<P<Value>> {
qinsoon's avatar
qinsoon committed
474
    &ALL_USABLE_MACHINE_REGS
475
476
}

qinsoon's avatar
mod.rs    
qinsoon committed
477
478
/// returns RegGroup for a given machine register (by ID)
/// panics if the ID is not a machine register
479
pub fn pick_group_for_reg(reg_id: MuID) -> RegGroup {
480
    let reg = all_regs().get(&reg_id).unwrap();
481
    RegGroup::get_from_value(reg)
482
483
}

qinsoon's avatar
mod.rs    
qinsoon committed
484
485
/// is a machine register (by ID) callee saved?
/// returns false if the ID is not a machine register
486
pub fn is_callee_saved(reg_id: MuID) -> bool {
qinsoon's avatar
qinsoon committed
487
    for reg in CALLEE_SAVED_GPRS.iter() {
488
489
490
491
        if reg_id == reg.extract_ssa_id().unwrap() {
            return true;
        }
    }
492
493

    false
494
495
}

qinsoon's avatar
mod.rs    
qinsoon committed
496
/// is a constant a valid x86_64 immediate number (32 bits integer)?
497
pub fn is_valid_x86_imm(op: &P<Value>) -> bool {
qinsoon's avatar
qinsoon committed
498
    use std::u32;
qinsoon's avatar
qinsoon committed
499
    match op.v {
qinsoon's avatar
qinsoon committed
500
        Value_::Constant(Constant::Int(val)) if val <= u32::MAX as u64 => {
qinsoon's avatar
qinsoon committed
501
502
            true
        },
503
504
        _ => false
    }
qinsoon's avatar
qinsoon committed
505
506
507
}

use ast::inst::*;
qinsoon's avatar
mod.rs    
qinsoon committed
508
509

/// estimate the number of machine instruction for each IR instruction
qinsoon's avatar
qinsoon committed
510
511
512
513
514
515
pub fn estimate_insts_for_ir(inst: &Instruction) -> usize {
    use ast::inst::Instruction_::*;

    match inst.v {
        // simple
        BinOp(_, _, _)  => 1,
qinsoon's avatar
qinsoon committed
516
        BinOpWithStatus(_, _, _, _) => 2,
qinsoon's avatar
qinsoon committed
517
        CmpOp(_, _, _)  => 1,
qinsoon's avatar
qinsoon committed
518
        ConvOp{..}      => 0,
qinsoon's avatar
qinsoon committed
519
520

        // control flow
qinsoon's avatar
qinsoon committed
521
522
523
        Branch1(_)     => 1,
        Branch2{..}    => 1,
        Select{..}     => 2,
qinsoon's avatar
qinsoon committed
524
        Watchpoint{..} => 1,
qinsoon's avatar
qinsoon committed
525
526
        WPBranch{..}   => 2,
        Switch{..}     => 3,
qinsoon's avatar
qinsoon committed
527
528
529

        // call
        ExprCall{..} | ExprCCall{..} | Call{..} | CCall{..} => 5,
qinsoon's avatar
qinsoon committed
530
        Return(_)   => 1,
qinsoon's avatar
qinsoon committed
531
532
533
534
        TailCall(_) => 1,

        // memory access
        Load{..} | Store{..} => 1,
qinsoon's avatar
qinsoon committed
535
536
537
538
539
        CmpXchg{..}          => 1,
        AtomicRMW{..}        => 1,
        AllocA(_)            => 1,
        AllocAHybrid(_, _)   => 1,
        Fence(_)             => 1,
qinsoon's avatar
qinsoon committed
540
541
542
543

        // memory addressing
        GetIRef(_) | GetFieldIRef{..} | GetElementIRef{..} | ShiftIRef{..} | GetVarPartIRef{..} => 0,

qinsoon's avatar
mod.rs    
qinsoon committed
544
        // runtime call
qinsoon's avatar
qinsoon committed
545
546
        New(_) | NewHybrid(_, _) => 10,
        NewStack(_) | NewThread(_, _) | NewThreadExn(_, _) | NewFrameCursor(_) => 10,
qinsoon's avatar
qinsoon committed
547
548
        ThreadExit    => 10,
        Throw(_)      => 10,
qinsoon's avatar
qinsoon committed
549
550
        SwapStack{..} => 10,
        CommonInst_GetThreadLocal | CommonInst_SetThreadLocal(_) => 10,
qinsoon's avatar
qinsoon committed
551
        CommonInst_Pin(_) | CommonInst_Unpin(_) => 10,
qinsoon's avatar
qinsoon committed
552
553
554

        // others
        Move(_) => 0,
555
        PrintHex(_) => 10,
qinsoon's avatar
qinsoon committed
556
557
        ExnInstruction{ref inner, ..} => estimate_insts_for_ir(&inner)
    }
558
}