|
| 1 | +//! The definition for the TACKY three address code IR used in the middle end |
| 2 | +
|
| 3 | +use crate::{ast, shared_types::Identifier}; |
| 4 | + |
| 5 | +pub struct Program { |
| 6 | + pub function: Function, |
| 7 | +} |
| 8 | + |
| 9 | +pub struct Function { |
| 10 | + pub name: Identifier, |
| 11 | + pub body: Vec<Instruction>, |
| 12 | +} |
| 13 | + |
| 14 | +pub enum Value { |
| 15 | + IntConstant(i32), |
| 16 | + Var(Identifier), |
| 17 | +} |
| 18 | + |
| 19 | +pub enum Instruction { |
| 20 | + Return(Value), |
| 21 | + UnaryMinus { |
| 22 | + src: Value, |
| 23 | + dst: Value, |
| 24 | + }, |
| 25 | + Complement { |
| 26 | + src: Value, |
| 27 | + dst: Value, |
| 28 | + }, |
| 29 | + LogicalNot { |
| 30 | + src: Value, |
| 31 | + dst: Value, |
| 32 | + }, |
| 33 | + BinaryOp { |
| 34 | + op: ast::BinaryOp, |
| 35 | + lhs: Value, |
| 36 | + rhs: Value, |
| 37 | + dst: Value, |
| 38 | + }, |
| 39 | + Jump(Identifier), |
| 40 | + JumpIfZero { |
| 41 | + cond: Value, |
| 42 | + target: Identifier, |
| 43 | + }, |
| 44 | + JumpIfNotZero { |
| 45 | + cond: Value, |
| 46 | + target: Identifier, |
| 47 | + }, |
| 48 | + Label(Identifier), |
| 49 | + Copy { |
| 50 | + src: Value, |
| 51 | + dst: Value, |
| 52 | + }, |
| 53 | +} |
| 54 | + |
| 55 | +#[cfg(test)] |
| 56 | +mod tests { |
| 57 | + use crate::{ast, shared_types::Identifier, tacky}; |
| 58 | + use pretty_assertions::assert_eq; |
| 59 | + #[test] |
| 60 | + fn construct_tacky() { |
| 61 | + let var0 = tacky::Value::Var(Identifier::new("tmp0")); |
| 62 | + let var1 = tacky::Value::Var(Identifier::new("tmp1")); |
| 63 | + let c0 = tacky::Value::IntConstant(37); |
| 64 | + let comp = tacky::Instruction::Complement { src: c0, dst: var0 }; |
| 65 | + let uminus = tacky::Instruction::UnaryMinus { src: c0, dst: var1 }; |
| 66 | + let ret = tacky::Instruction::Return(var1); |
| 67 | + let func = tacky::Function { |
| 68 | + name: Identifier::new("main"), |
| 69 | + body: vec![comp, uminus, ret], |
| 70 | + }; |
| 71 | + let prog = tacky::Program { function: func }; |
| 72 | + let div_op = tacky::Instruction::BinaryOp { |
| 73 | + op: ast::BinaryOp::Divide, |
| 74 | + lhs: c0, |
| 75 | + rhs: var0, |
| 76 | + dst: var1, |
| 77 | + }; |
| 78 | + let plus_op = tacky::Instruction::BinaryOp { |
| 79 | + op: ast::BinaryOp::Plus, |
| 80 | + lhs: c0, |
| 81 | + rhs: var0, |
| 82 | + dst: var1, |
| 83 | + }; |
| 84 | + let minus_op = tacky::Instruction::BinaryOp { |
| 85 | + op: ast::BinaryOp::Subtract, |
| 86 | + lhs: c0, |
| 87 | + rhs: var0, |
| 88 | + dst: var1, |
| 89 | + }; |
| 90 | + let times_op = tacky::Instruction::BinaryOp { |
| 91 | + op: ast::BinaryOp::Multiply, |
| 92 | + lhs: c0, |
| 93 | + rhs: var0, |
| 94 | + dst: var1, |
| 95 | + }; |
| 96 | + let remainder_op = tacky::Instruction::BinaryOp { |
| 97 | + op: ast::BinaryOp::Remainder, |
| 98 | + lhs: c0, |
| 99 | + rhs: var0, |
| 100 | + dst: var1, |
| 101 | + }; |
| 102 | + let not = tacky::Instruction::LogicalNot { src: c0, dst: var0 }; |
| 103 | + let label_name = Identifier::new("label1"); |
| 104 | + let label = tacky::Instruction::Label(label_name); |
| 105 | + let jmp = tacky::Instruction::Jump(label_name); |
| 106 | + let j0 = tacky::Instruction::JumpIfZero { |
| 107 | + cond: var1, |
| 108 | + target: label_name, |
| 109 | + }; |
| 110 | + let jn0 = tacky::Instruction::JumpIfNotZero { |
| 111 | + cond: c0, |
| 112 | + target: label_name, |
| 113 | + }; |
| 114 | + } |
| 115 | +} |
0 commit comments