|
| 1 | +use crate::BaseIR; |
| 2 | +use crate::IString; |
| 3 | +use crate::FunctionSignature; |
| 4 | +use crate::VariableType; |
| 5 | +use rustc_middle::{ |
| 6 | + mir::{ |
| 7 | + interpret::ConstValue, AggregateKind, BinOp, ConstantKind, Operand, Rvalue, Statement, |
| 8 | + StatementKind, Terminator, TerminatorKind, |
| 9 | + }, |
| 10 | + ty::{IntTy, TyKind}, |
| 11 | +}; |
| 12 | +#[derive(Debug)] |
| 13 | +pub(crate) struct CLRMethod { |
| 14 | + ops: Vec<BaseIR>, |
| 15 | + locals: Vec<Option<VariableType>>, |
| 16 | + op_types:Vec<Option<VariableType>>, |
| 17 | + sig: FunctionSignature, |
| 18 | + name:IString, |
| 19 | +} |
| 20 | +enum LocalPlacement { |
| 21 | + Arg(u32), |
| 22 | + Var(u32), |
| 23 | +} |
| 24 | +impl CLRMethod { |
| 25 | + fn name(&self)->&str{&self.name} |
| 26 | + pub(crate) fn get_arg_type(&self,arg:u32)->&VariableType{ |
| 27 | + &self.sig.inputs()[arg as usize] |
| 28 | + } |
| 29 | + fn set_trivial_types(&mut self){ |
| 30 | + for index in 0..(self.op_types.len()){ |
| 31 | + if let None = self.op_types[index]{ |
| 32 | + self.op_types[index] = self.ops[index].get_trivial_type(self); |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + pub(crate) fn typecheck(&mut self){ |
| 37 | + while self.op_types.len() < self.ops.len(){ |
| 38 | + self.op_types.push(None); |
| 39 | + } |
| 40 | + |
| 41 | + self.set_trivial_types(); |
| 42 | + println!("op_types:{:?}",self.op_types); |
| 43 | + } |
| 44 | + pub(crate) fn into_il_ir(&self)->String{ |
| 45 | + let output = self.sig.output().il_name(); |
| 46 | + let mut arg_list = String::new(); |
| 47 | + let mut arg_iter = self.sig.inputs.iter(); |
| 48 | + match arg_iter.next(){ |
| 49 | + Some(start)=>arg_list.push_str(&start.il_name()), |
| 50 | + None=>(), |
| 51 | + } |
| 52 | + for arg in arg_iter{ |
| 53 | + arg_list.push(','); |
| 54 | + arg_list.push_str(&arg.il_name()); |
| 55 | + } |
| 56 | + let mut ops_ir = String::new(); |
| 57 | + for op in &self.ops{ |
| 58 | + ops_ir.push_str(&op.clr_ir()); |
| 59 | + } |
| 60 | + format!(".method public static {output} {name}({arg_list}){{\n{ops_ir}}}\n",name = self.name()) |
| 61 | + } |
| 62 | + fn argc(&self) -> u32 { |
| 63 | + self.sig.inputs().len() as u32 |
| 64 | + } |
| 65 | + fn remove_sl(&mut self) -> usize { |
| 66 | + let mut opt_ops: Vec<BaseIR> = Vec::with_capacity(self.ops.len()); |
| 67 | + let mut ops_peek = self.ops.iter().peekable(); |
| 68 | + while let Some(op) = ops_peek.next() { |
| 69 | + match op { |
| 70 | + BaseIR::STLoc(local_id) => { |
| 71 | + if let Some(BaseIR::LDLoc(other_id)) = ops_peek.peek() { |
| 72 | + //Ops store and the load the same value, being effectively a NOP. |
| 73 | + if local_id == other_id { |
| 74 | + ops_peek.next(); |
| 75 | + continue; |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + _ => (), |
| 80 | + } |
| 81 | + opt_ops.push(op.clone()); |
| 82 | + } |
| 83 | + self.ops = opt_ops; |
| 84 | + self.ops.len() |
| 85 | + } |
| 86 | + pub(crate) fn opt(&mut self) { |
| 87 | + const MAX_OPT_PASS: usize = 8; |
| 88 | + for _ in 0..MAX_OPT_PASS { |
| 89 | + let prev_length = self.ops.len(); |
| 90 | + if !(self.remove_sl() < prev_length) { |
| 91 | + continue; |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + fn has_return(&self) -> bool { |
| 96 | + true |
| 97 | + } |
| 98 | + pub(crate) fn new(sig: FunctionSignature,name:&str) -> Self { |
| 99 | + Self { |
| 100 | + locals: Vec::new(), |
| 101 | + op_types: Vec::with_capacity(0x100), |
| 102 | + sig, |
| 103 | + name:name.into(), |
| 104 | + ops: Vec::with_capacity(0x100), |
| 105 | + } |
| 106 | + } |
| 107 | + fn var_live(&mut self, _local: u32) { |
| 108 | + //TODO: use variable lifetimes! |
| 109 | + } |
| 110 | + fn var_dead(&mut self, _local: u32) { |
| 111 | + //TODO: use variable lifetimes! |
| 112 | + } |
| 113 | + fn local_id_placement(&self, local: u32) -> LocalPlacement { |
| 114 | + // I assume local 0 is supposed to be the return value. TODO: check if this is always the case. |
| 115 | + if self.has_return() { |
| 116 | + if local == 0 { |
| 117 | + LocalPlacement::Var(0) |
| 118 | + } else if local - 1 < self.argc() { |
| 119 | + LocalPlacement::Arg(local - 1) |
| 120 | + } else { |
| 121 | + LocalPlacement::Var(local - self.argc()) |
| 122 | + } |
| 123 | + } else { |
| 124 | + panic!("Can't handle void functions yet. Diagnose MIR to determine what happens to the return var(0)!"); |
| 125 | + } |
| 126 | + } |
| 127 | + fn load(&mut self, local: u32) { |
| 128 | + self.ops.push(match self.local_id_placement(local) { |
| 129 | + LocalPlacement::Arg(arg_id) => BaseIR::LDArg(arg_id), |
| 130 | + LocalPlacement::Var(var_id) => BaseIR::LDLoc(var_id), |
| 131 | + }) |
| 132 | + } |
| 133 | + fn store(&mut self, local: u32) { |
| 134 | + self.ops.push(match self.local_id_placement(local) { |
| 135 | + LocalPlacement::Arg(arg_id) => BaseIR::STArg(arg_id), |
| 136 | + LocalPlacement::Var(var_id) => BaseIR::STLoc(var_id), |
| 137 | + }) |
| 138 | + } |
| 139 | + fn process_constant(&mut self, constant: ConstantKind) { |
| 140 | + self.ops.push(match constant { |
| 141 | + ConstantKind::Val(value, r#type) => match value { |
| 142 | + ConstValue::Scalar(scalar) => match r#type.kind() { |
| 143 | + TyKind::Int(IntTy::I32) => BaseIR::LDConstI32( |
| 144 | + scalar |
| 145 | + .to_i32() |
| 146 | + .expect("Type is i32, but odes not fit in i32."), |
| 147 | + ), |
| 148 | + _ => todo!("Unhandled type kind {:?}", r#type.kind()), |
| 149 | + }, |
| 150 | + _ => todo!("Unhanled constant value {value:?}"), |
| 151 | + }, |
| 152 | + _ => todo!("Unhanled constant {constant:?}"), |
| 153 | + }); |
| 154 | + } |
| 155 | + // Makes so the top of the stack is the value of RValue |
| 156 | + fn process_operand(&mut self, operand: &Operand) { |
| 157 | + match operand { |
| 158 | + Operand::Copy(place) => self.load(place.local.into()), |
| 159 | + //TODO:Do moves need to be treated any diffrently forom copies in the context of CLR? |
| 160 | + Operand::Move(place) => self.load(place.local.into()), |
| 161 | + Operand::Constant(const_val) => { |
| 162 | + self.process_constant(const_val.literal); |
| 163 | + } |
| 164 | + _ => todo!("Unhanled operand {operand:?}"), |
| 165 | + } |
| 166 | + } |
| 167 | + // Makes so the top of the stack is the value of RValue |
| 168 | + fn process_rvalue(&mut self, rvalue: &Rvalue) { |
| 169 | + match rvalue { |
| 170 | + Rvalue::Use(operand) => self.process_operand(operand), |
| 171 | + Rvalue::BinaryOp(binop, operands) => { |
| 172 | + let (a, b): (_, _) = (&operands.0, &operands.1); |
| 173 | + self.process_operand(a); |
| 174 | + self.process_operand(b); |
| 175 | + self.ops.push(match binop { |
| 176 | + BinOp::Add => BaseIR::Add, |
| 177 | + BinOp::Mul => BaseIR::Mul, |
| 178 | + BinOp::Shl => BaseIR::Shl, |
| 179 | + _ => todo!("Unknown binop:{binop:?}"), |
| 180 | + }); |
| 181 | + } |
| 182 | + Rvalue::Aggregate(kind, operands) => { |
| 183 | + match kind.as_ref() { |
| 184 | + AggregateKind::Adt(def_id, variant_idx, subst_ref, utai, fidx) => { |
| 185 | + //let (def_id,variant_idx,subst_ref,utai,fidx) = *adt; |
| 186 | + panic!("def_id:{def_id:?},variant_idx:{variant_idx:?},subst_ref:{subst_ref:?},utai:{utai:?},fidx:{fidx:?}"); |
| 187 | + } |
| 188 | + _ => todo!( |
| 189 | + "Can't yet handle the aggregate of kind {kind:?} and operands:{operands:?}" |
| 190 | + ), |
| 191 | + } |
| 192 | + } |
| 193 | + _ => todo!("Can't yet handle a rvalue of type {rvalue:?}"), |
| 194 | + } |
| 195 | + } |
| 196 | + pub(crate) fn add_statement(&mut self, statement: &Statement) { |
| 197 | + println!("statement:{statement:?}"); |
| 198 | + match &statement.kind { |
| 199 | + StatementKind::Assign(asign_box) => { |
| 200 | + let (place, rvalue) = (asign_box.0, &asign_box.1); |
| 201 | + self.process_rvalue(rvalue); |
| 202 | + self.store(place.local.into()); |
| 203 | + //panic!("place:{place:?},rvalue:{rvalue:?}"); |
| 204 | + } |
| 205 | + StatementKind::StorageLive(local) => { |
| 206 | + self.var_live((*local).into()); |
| 207 | + } |
| 208 | + StatementKind::StorageDead(local) => { |
| 209 | + self.var_dead((*local).into()); |
| 210 | + } |
| 211 | + _ => todo!("Unhanded statement:{:?}", statement.kind), |
| 212 | + } |
| 213 | + } |
| 214 | + pub(crate) fn add_terminator(&mut self, terminator: &Terminator) { |
| 215 | + match terminator.kind { |
| 216 | + TerminatorKind::Return => { |
| 217 | + if self.has_return() { |
| 218 | + self.load(0); |
| 219 | + self.ops.push(BaseIR::Return); |
| 220 | + } else { |
| 221 | + todo!("Can't yet return from a void method!"); |
| 222 | + } |
| 223 | + } |
| 224 | + _ => todo!("Unknown terminator type {terminator:?}"), |
| 225 | + } |
| 226 | + } |
| 227 | +} |
0 commit comments