From 01daee374a299646c595dd245c8e4a4990769002 Mon Sep 17 00:00:00 2001 From: David Li Date: Sun, 26 Nov 2017 19:59:00 -0500 Subject: Translate variable bindings --- src/semantic/frame.rs | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) (limited to 'src/semantic/frame.rs') diff --git a/src/semantic/frame.rs b/src/semantic/frame.rs index ddfc330..95d18a1 100644 --- a/src/semantic/frame.rs +++ b/src/semantic/frame.rs @@ -1,3 +1,4 @@ +use super::ir; use super::temp; pub enum Location { @@ -16,17 +17,20 @@ pub trait Frame { fn new(name: temp::TempLabel, formals: Vec) -> Self; fn name(&self) -> temp::TempLabel; fn formals(&self) -> &[Location]; - fn alloc_local(&mut self, escapes: Escape) -> Location; + fn alloc_local(&mut self, temp: &mut temp::Temp, escapes: Escape) -> Location; + fn location_to_exp(&mut self, location: Location) -> ir::Expression; } pub struct Amd64Frame { + offset: u64, name: temp::TempLabel, } impl Frame for Amd64Frame { fn new(name: temp::TempLabel, formals: Vec) -> Self { Amd64Frame { - name, + offset: 0, + name: name, } } @@ -38,7 +42,28 @@ impl Frame for Amd64Frame { unimplemented!(); } - fn alloc_local(&mut self, escapes: Escape) -> Location { - unimplemented!(); + fn alloc_local(&mut self, temp: &mut temp::Temp, escapes: Escape) -> Location { + // TODO: this is WRONG + Location::InFrame { offset: 0 } + } + + fn location_to_exp(&mut self, location: Location) -> ir::Expression { + match location { + Location::InFrame { offset } => { + ir::Expression::Memory( + Box::new( + ir::Expression::BinOp( + ir::BinOp::Plus, + Box::new(ir::Expression::Const(self.offset)), + Box::new(ir::Expression::Const(offset as u64)), + ) + ) + ) + } + Location::Register(name) => { + ir::Expression::Temp(name) + } + } + } } -- cgit v1.2.3