GitLab will be upgraded to the 12.10.14-ce.0 on 28 Sept 2020 at 2.00pm (AEDT) to 2.30pm (AEDT). During the update, GitLab and Mattermost services will not be available. If you have any concerns with this, please talk to us at N110 (b) CSIT building.

machine_code.rs 4.41 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
    // not emitting this
16
    pub mc: Option<Box<MachineCode + Send + Sync>>,
qinsoon's avatar
qinsoon committed
17 18 19 20 21 22 23 24 25 26 27
    
    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| {
qinsoon's avatar
qinsoon committed
28
            trace!("......serializing func_id");    
qinsoon's avatar
qinsoon committed
29
            try!(s.emit_struct_field("func_id",     0, |s| self.func_id.encode(s)));
qinsoon's avatar
qinsoon committed
30
            trace!("......serializing func_ver_id");
qinsoon's avatar
qinsoon committed
31
            try!(s.emit_struct_field("func_ver_id", 1, |s| self.func_ver_id.encode(s)));
qinsoon's avatar
qinsoon committed
32
            trace!("......serializing temps");
qinsoon's avatar
qinsoon committed
33
            try!(s.emit_struct_field("temps",       2, |s| self.temps.encode(s)));
qinsoon's avatar
qinsoon committed
34 35
            trace!("......serializing frame");
            trace!("{}", self.frame);
qinsoon's avatar
qinsoon committed
36
            try!(s.emit_struct_field("frame",       3, |s| self.frame.encode(s)));
qinsoon's avatar
qinsoon committed
37
            trace!("......serializing start");
qinsoon's avatar
qinsoon committed
38
            try!(s.emit_struct_field("start",       4, |s| self.start.encode(s)));
qinsoon's avatar
qinsoon committed
39
            trace!("......serializing end");
qinsoon's avatar
qinsoon committed
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
            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 {
77
    pub fn mc(&self) -> &Box<MachineCode + Send + Sync> {
qinsoon's avatar
qinsoon committed
78 79 80 81 82 83 84 85
        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)")
        }
    }
    
86
    pub fn mc_mut(&mut self) -> &mut Box<MachineCode + Send + Sync> {
qinsoon's avatar
qinsoon committed
87 88 89 90 91
        match self.mc {
            Some(ref mut mc) => mc,
            None => panic!("no mc found from a compiled function")
        }
    }
92 93 94
}

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

    // functions for rewrite
120
    fn replace_reg(&mut self, from: MuID, to: MuID);
121
    fn replace_reg_for_inst(&mut self, from: MuID, to: MuID, inst: usize);
122
    fn set_inst_nop(&mut self, index: usize);
123
}
124 125 126 127

pub trait MachineInst {

}