Skip to content

Commit

Permalink
feat: add ir level symbol.
Browse files Browse the repository at this point in the history
  • Loading branch information
Shylock-Hg committed Jan 29, 2025
1 parent 9e6140e commit 68c4168
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 14 deletions.
23 changes: 9 additions & 14 deletions tigerc/src/frame.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use indexmap::IndexMap;

use crate::ident_pool::{self, Symbol};
use crate::ident_pool::Symbol;
use crate::ir;

pub enum Access {
// offset to frame pointer
Expand All @@ -18,43 +19,37 @@ pub struct Variable {
}

pub trait Frame {
fn new(name: Symbol, parameters: IndexMap<Symbol, Variable>) -> Self;
fn new(name: Symbol, parameters: IndexMap<ir::LowerIdent, Variable>) -> Self;
fn name(&self) -> Symbol;
fn allocate_local(&mut self, symbol: Symbol, var: Variable);
fn allocate_local(&mut self, symbol: ir::LowerIdent, var: Variable);
}

// frame of a function
struct FrameAmd64 {
// function name
name: Symbol,
// parameters
parameters: IndexMap<Symbol, Variable>,
parameters: IndexMap<ir::LowerIdent, Variable>,

Check warning on line 32 in tigerc/src/frame.rs

View workflow job for this annotation

GitHub Actions / cargo test

field `parameters` is never read
// local variables
locals: IndexMap<Symbol, Variable>,
// counting same name variable
id: usize,
locals: IndexMap<ir::LowerIdent, Variable>,
}

impl Frame for FrameAmd64 {
fn new(name: Symbol, parameters: IndexMap<Symbol, Variable>) -> Self {
fn new(name: Symbol, parameters: IndexMap<ir::LowerIdent, Variable>) -> Self {
FrameAmd64 {
name,
parameters,
locals: IndexMap::new(),
id: 0,
}
}

fn name(&self) -> Symbol {
self.name
}

fn allocate_local(&mut self, symbol: Symbol, var: Variable) {
fn allocate_local(&mut self, symbol: ir::LowerIdent, var: Variable) {
if self.locals.contains_key(&symbol) {
self.locals.insert(
ident_pool::create_symbol(&format!("{}:{}", symbol, self.id)),
var,
);
self.locals.insert(symbol, var);
} else {
self.locals.insert(symbol, var);
}
Expand Down
38 changes: 38 additions & 0 deletions tigerc/src/ir.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use std::hash::Hash;

use crate::ident_pool::Symbol;

// Identify different symbol with same name
// We will flatten symbol scope when translate it to IR which close to machine code
#[derive(Eq, Debug)]
pub struct LowerIdent {
// this is used just for debug
pub symbol: Symbol,
pub number: usize,
}

impl LowerIdent {
pub fn new(symbol: Symbol) -> LowerIdent {
// It's safe in single thread
static mut COUNTER: usize = 0;
LowerIdent {
symbol,
number: unsafe {
COUNTER += 1;
COUNTER
},
}
}
}

impl PartialEq for LowerIdent {
fn eq(&self, other: &Self) -> bool {
self.number == other.number
}
}

impl Hash for LowerIdent {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.number.hash(state);
}
}
1 change: 1 addition & 0 deletions tigerc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub mod cursor;
pub mod escape;
pub mod frame;
pub mod ident_pool;
pub mod ir;
pub mod parser;
pub mod symbol_table;
pub mod tokenizer;
Expand Down

0 comments on commit 68c4168

Please sign in to comment.