mod.rs 4.74 KB
Newer Older
1
pub mod inst_sel;
2
pub mod reg_alloc;
3
pub mod peephole_opt;
4
pub mod code_emission;
5

qinsoon's avatar
qinsoon committed
6
use utils::ByteSize;
qinsoon's avatar
qinsoon committed
7 8

// X86_64
qinsoon's avatar
qinsoon committed
9

10
#[cfg(target_arch = "x86_64")]
11
#[path = "arch/x86_64/mod.rs"]
12 13
mod x86_64;

qinsoon's avatar
qinsoon committed
14
#[cfg(target_arch = "x86_64")]
qinsoon's avatar
qinsoon committed
15
pub use compiler::backend::x86_64::init_machine_regs_for_func;
qinsoon's avatar
qinsoon committed
16 17

#[cfg(target_arch = "x86_64")]
qinsoon's avatar
qinsoon committed
18
pub use compiler::backend::x86_64::number_of_regs_in_group;
qinsoon's avatar
qinsoon committed
19
#[cfg(target_arch = "x86_64")]
qinsoon's avatar
qinsoon committed
20 21 22
pub use compiler::backend::x86_64::number_of_all_regs;
#[cfg(target_arch = "x86_64")]
pub use compiler::backend::x86_64::all_regs;
23
#[cfg(target_arch = "x86_64")]
24 25
pub use compiler::backend::x86_64::all_usable_regs;
#[cfg(target_arch = "x86_64")]
26
pub use compiler::backend::x86_64::pick_group_for_reg;
27 28
#[cfg(target_arch = "x86_64")]
pub use compiler::backend::x86_64::is_callee_saved;
29 30 31 32
#[cfg(target_arch = "x86_64")]
pub use compiler::backend::x86_64::emit_code;
#[cfg(target_arch = "x86_64")]
pub use compiler::backend::x86_64::emit_context;
qinsoon's avatar
qinsoon committed
33

qinsoon's avatar
qinsoon committed
34 35
// ARM

qinsoon's avatar
qinsoon committed
36 37 38
#[cfg(target_arch = "arm")]
#[path = "arch/arm/mod.rs"]
mod arm;
qinsoon's avatar
qinsoon committed
39 40 41

// common data structure with target specific info

qinsoon's avatar
qinsoon committed
42
use vm::VM;
qinsoon's avatar
qinsoon committed
43 44
use ast::types::*;
use ast::ptr::*;
45
use ast::ir::*;
qinsoon's avatar
qinsoon committed
46
pub fn resolve_backend_type_info (ty: &MuType, vm: &VM) -> BackendTypeInfo {
qinsoon's avatar
qinsoon committed
47
    match ty.v {
qinsoon's avatar
qinsoon committed
48
        // integral
qinsoon's avatar
qinsoon committed
49
        MuType_::Int(size_in_bit) => {
qinsoon's avatar
qinsoon committed
50 51 52 53 54 55 56 57 58
            match size_in_bit {
                8  => BackendTypeInfo{size: 1, alignment: 1, struct_layout: None},
                16 => BackendTypeInfo{size: 2, alignment: 2, struct_layout: None},
                32 => BackendTypeInfo{size: 4, alignment: 4, struct_layout: None},
                64 => BackendTypeInfo{size: 8, alignment: 8, struct_layout: None},
                _ => unimplemented!()
            }
        },
        // pointer of any type
qinsoon's avatar
qinsoon committed
59 60 61 62 63 64 65 66 67
        MuType_::Ref(_)
        | MuType_::IRef(_)
        | MuType_::WeakRef(_)
        | MuType_::UPtr(_)
        | MuType_::FuncRef(_)
        | MuType_::UFuncPtr(_)
        | MuType_::Tagref64
        | MuType_::ThreadRef
        | MuType_::StackRef => BackendTypeInfo{size: 8, alignment: 8, struct_layout: None},
qinsoon's avatar
qinsoon committed
68
        // floating point
qinsoon's avatar
qinsoon committed
69 70
        MuType_::Float => BackendTypeInfo{size: 4, alignment: 4, struct_layout: None},
        MuType_::Double => BackendTypeInfo{size: 8, alignment: 8, struct_layout: None},
qinsoon's avatar
qinsoon committed
71
        // array
qinsoon's avatar
qinsoon committed
72
        MuType_::Array(ref ty, len) => {
73
            let ele_ty = vm.get_backend_type_info(ty.id());
qinsoon's avatar
qinsoon committed
74 75 76 77
            
            BackendTypeInfo{size: ele_ty.size * len, alignment: ele_ty.alignment, struct_layout: None}
        }
        // struct
qinsoon's avatar
qinsoon committed
78
        MuType_::Struct(name) => {
qinsoon's avatar
qinsoon committed
79 80 81 82 83 84 85 86 87 88 89
            let read_lock = STRUCT_TAG_MAP.read().unwrap();
            let struc = read_lock.get(name).unwrap();
            let tys = struc.get_tys();            
            
            trace!("layout struct: {}", struc);
            layout_struct(tys, vm)
        }
        // hybrid 
        // - align is the most strict aligned element (from all fix tys and var ty)
        // - size is fixed tys size
        // - layout is fixed tys layout
qinsoon's avatar
qinsoon committed
90
        MuType_::Hybrid(ref fix_tys, ref var_ty) => {
qinsoon's avatar
qinsoon committed
91 92 93 94
            // treat fix_tys as struct
            let mut ret = layout_struct(fix_tys, vm);
            
            // treat var_ty as array (getting its alignment)
95
            let var_align = vm.get_backend_type_info(var_ty.id()).alignment;
qinsoon's avatar
qinsoon committed
96 97 98 99 100 101 102 103
            
            if ret.alignment < var_align {
                ret.alignment = var_align;
            }
            
            ret
        }
        // void
qinsoon's avatar
qinsoon committed
104
        MuType_::Void => BackendTypeInfo{size: 0, alignment: 8, struct_layout: None},
qinsoon's avatar
qinsoon committed
105
        // vector
qinsoon's avatar
qinsoon committed
106
        MuType_::Vector(_, _) => unimplemented!()
qinsoon's avatar
qinsoon committed
107 108 109
    }
}

qinsoon's avatar
qinsoon committed
110
fn layout_struct(tys: &Vec<P<MuType>>, vm: &VM) -> BackendTypeInfo {
qinsoon's avatar
qinsoon committed
111 112 113 114 115
    let mut offsets : Vec<ByteSize> = vec![];
    let mut cur : ByteSize = 0;
    let mut struct_align : ByteSize = 0;
    
    for ty in tys.iter() {
116
        let ty_info = vm.get_backend_type_info(ty.id());
qinsoon's avatar
qinsoon committed
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
        trace!("examining field: {}, {:?}", ty, ty_info);
        
        let align = ty_info.alignment;
        if struct_align < align {
            struct_align = align;
        }
        
        if cur % align != 0 {
            // move cursor to next aligned offset
            cur = (cur / align + 1) * align;
        }
        
        offsets.push(cur);
        trace!("aligned to {}", cur);
        
        cur += ty_info.size;
    }
    
    // if we need padding at the end
    if cur % struct_align != 0 {
        cur = (cur / struct_align + 1) * struct_align;
    }
    
    BackendTypeInfo {
        size: cur,
        alignment: struct_align,
        struct_layout: Some(offsets)
    }
}

#[derive(Clone, Debug)]
pub struct BackendTypeInfo {
    size: ByteSize,
    alignment: ByteSize,
    struct_layout: Option<Vec<ByteSize>>
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
qinsoon's avatar
qinsoon committed
155
pub enum RegGroup {GPR, FPR}