machine_code.rs 3.92 KB
Newer Older
1
use ast::ir::*;
qinsoon's avatar
qinsoon committed
2
use compiler::frame::*;
qinsoon's avatar
qinsoon committed
3 4
use runtime::ValueLocation;

5
use std::ops;
6
use std::collections::HashMap;
7

qinsoon's avatar
qinsoon committed
8 9
use rustc_serialize::{Encodable, Encoder, Decodable, Decoder};

10
pub struct CompiledFunction {
qinsoon's avatar
qinsoon committed
11
    pub func_id: MuID,
qinsoon's avatar
qinsoon committed
12
    pub func_ver_id: MuID,
13
    pub temps: HashMap<MuID, MuID>, // assumes one temperary maps to one register
qinsoon's avatar
qinsoon committed
14
    
qinsoon's avatar
qinsoon committed
15 16 17 18 19 20 21 22 23 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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
    // not emitting this
    pub mc: Option<Box<MachineCode>>,
    
    pub frame: Frame,
    pub start: ValueLocation,
    pub end: ValueLocation
}

const CF_SERIALIZE_FIELDS : usize = 6;

impl Encodable for CompiledFunction {
    fn encode<S: Encoder> (&self, s: &mut S) -> Result<(), S::Error> {
        s.emit_struct("CompiledFunction", CF_SERIALIZE_FIELDS, |s| {
            try!(s.emit_struct_field("func_id",     0, |s| self.func_id.encode(s)));
            try!(s.emit_struct_field("func_ver_id", 1, |s| self.func_ver_id.encode(s)));
            try!(s.emit_struct_field("temps",       2, |s| self.temps.encode(s)));
            try!(s.emit_struct_field("frame",       3, |s| self.frame.encode(s)));
            try!(s.emit_struct_field("start",       4, |s| self.start.encode(s)));
            try!(s.emit_struct_field("end",         5, |s| self.end.encode(s)));
            
            Ok(())
        })
    }
}

impl Decodable for CompiledFunction {
    fn decode<D: Decoder>(d: &mut D) -> Result<CompiledFunction, D::Error> {
        d.read_struct("CompiledFunction", CF_SERIALIZE_FIELDS, |d| {
            let func_id = 
                try!(d.read_struct_field("func_id",     0, |d| Decodable::decode(d)));
            let func_ver_id = 
                try!(d.read_struct_field("func_ver_id", 1, |d| Decodable::decode(d)));
            let temps = 
                try!(d.read_struct_field("temps",       2, |d| Decodable::decode(d)));
            let frame = 
                try!(d.read_struct_field("frame",       3, |d| Decodable::decode(d)));
            let start = 
                try!(d.read_struct_field("start",       4, |d| Decodable::decode(d)));
            let end =
                try!(d.read_struct_field("end",         5, |d| Decodable::decode(d)));
            
            Ok(CompiledFunction{
                func_id: func_id,
                func_ver_id: func_ver_id,
                temps: temps,
                mc: None,
                frame: frame,
                start: start,
                end: end
            })
        })
    }
}

impl CompiledFunction {
    pub fn mc(&self) -> &Box<MachineCode> {
        match self.mc {
            Some(ref mc) => mc,
            None => panic!("trying to get mc from a compiled function. 
                But machine code is None (probably this compiled function is restored from
                boot image and mc is thrown away)")
        }
    }
    
    pub fn mc_mut(&mut self) -> &mut Box<MachineCode> {
        match self.mc {
            Some(ref mut mc) => mc,
            None => panic!("no mc found from a compiled function")
        }
    }
85 86 87
}

pub trait MachineCode {
88 89 90
    fn trace_mc(&self);
    fn trace_inst(&self, index: usize);
    
91
    fn emit(&self) -> Vec<u8>;
92 93
    
    fn number_of_insts(&self) -> usize;
qinsoon's avatar
qinsoon committed
94
    
95
    fn is_move(&self, index: usize) -> bool;
qinsoon's avatar
qinsoon committed
96 97
    fn is_using_mem_op(&self, index: usize) -> bool;
    
qinsoon's avatar
qinsoon committed
98 99
    fn get_succs(&self, index: usize) -> &Vec<usize>;
    fn get_preds(&self, index: usize) -> &Vec<usize>;
100 101 102
    
    fn get_inst_reg_uses(&self, index: usize) -> &Vec<MuID>;
    fn get_inst_reg_defines(&self, index: usize) -> &Vec<MuID>;
103
    
104 105
    fn get_ir_block_livein(&self, block: &str) -> Option<&Vec<MuID>>;
    fn get_ir_block_liveout(&self, block: &str) -> Option<&Vec<MuID>>;
106 107
    fn set_ir_block_livein(&mut self, block: &str, set: Vec<MuID>);
    fn set_ir_block_liveout(&mut self, block: &str, set: Vec<MuID>);
108
    
qinsoon's avatar
qinsoon committed
109
    fn get_all_blocks(&self) -> &Vec<MuName>;
110
    fn get_block_range(&self, block: &str) -> Option<ops::Range<usize>>;
111 112 113
    
    fn replace_reg(&mut self, from: MuID, to: MuID);
    fn set_inst_nop(&mut self, index: usize);
114
}