Skip to content

Commit

Permalink
add debug names to locals
Browse files Browse the repository at this point in the history
  • Loading branch information
edg-l committed Feb 6, 2024
1 parent 97e873d commit 7dc6701
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 43 deletions.
19 changes: 17 additions & 2 deletions crates/concrete_ir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,28 @@ pub enum PlaceElem {
#[derive(Debug, Clone)]
pub struct Local {
pub span: Option<Span>,
pub debug_name: Option<String>,
pub ty: Ty,
pub kind: LocalKind,
}

impl Local {
pub fn new(span: Option<Span>, kind: LocalKind, ty: Ty) -> Self {
Self { span, kind, ty }
pub fn new(span: Option<Span>, kind: LocalKind, ty: Ty, debug_name: Option<String>) -> Self {
Self {
span,
kind,
ty,
debug_name,
}
}

pub const fn temp(ty: Ty) -> Self {
Self {
span: None,
ty,
kind: LocalKind::Temp,
debug_name: None,
}
}
}

Expand Down
58 changes: 18 additions & 40 deletions crates/concrete_ir/src/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,18 @@ fn lower_func(ctx: ModuleBody, func: &FunctionDef, module_id: DefId) -> ModuleBo
builder
.body
.locals
.push(Local::new(None, LocalKind::ReturnPointer, ret_ty));
.push(Local::new(None, LocalKind::ReturnPointer, ret_ty, None));

for (arg, ty) in func.decl.params.iter().zip(args_ty) {
builder
.name_to_local
.insert(arg.name.name.clone(), builder.body.locals.len());
builder
.body
.locals
.push(Local::new(Some(arg.name.span), LocalKind::Arg, ty));
builder.body.locals.push(Local::new(
Some(arg.name.span),
LocalKind::Arg,
ty,
Some(arg.name.name.clone()),
));
}

// Get all locals
Expand All @@ -146,10 +148,12 @@ fn lower_func(ctx: ModuleBody, func: &FunctionDef, module_id: DefId) -> ModuleBo
builder
.name_to_local
.insert(name.name.clone(), builder.body.locals.len());
builder
.body
.locals
.push(Local::new(Some(name.span), LocalKind::Temp, ty));
builder.body.locals.push(Local::new(
Some(name.span),
LocalKind::Temp,
ty,
Some(name.name.clone()),
));
}
LetStmtTarget::Destructure(_) => todo!(),
}
Expand Down Expand Up @@ -205,14 +209,7 @@ fn lower_while(builder: &mut FnBodyBuilder, info: &WhileStmt) {

let discriminator = lower_expression(builder, &info.value, Some(TyKind::Bool));

let local = builder.add_local(Local {
span: None,
ty: Ty {
span: None,
kind: TyKind::Bool,
},
kind: LocalKind::Temp,
});
let local = builder.add_temp_local(TyKind::Bool);
let place = Place {
local,
projection: vec![],
Expand Down Expand Up @@ -280,14 +277,7 @@ fn lower_while(builder: &mut FnBodyBuilder, info: &WhileStmt) {
fn lower_if_statement(builder: &mut FnBodyBuilder, info: &IfExpr) {
let discriminator = lower_expression(builder, &info.value, Some(TyKind::Bool));

let local = builder.add_local(Local {
span: None,
ty: Ty {
span: None,
kind: TyKind::Bool,
},
kind: LocalKind::Temp,
});
let local = builder.add_temp_local(TyKind::Bool);
let place = Place {
local,
projection: vec![],
Expand Down Expand Up @@ -469,11 +459,7 @@ fn lower_fn_call(builder: &mut FnBodyBuilder, info: &FnCallOp) -> Rvalue {
args.push(rvalue);
}

let dest_local = builder.add_local(Local {
span: None,
kind: LocalKind::Temp,
ty: ret_ty,
});
let dest_local = builder.add_local(Local::temp(ret_ty));

let dest_place = Place {
local: dest_local,
Expand Down Expand Up @@ -517,16 +503,8 @@ fn lower_binary_op(
span: None,
kind: expr_type.clone(),
};
let lhs_local = builder.add_local(Local {
span: None,
ty: local_ty.clone(),
kind: LocalKind::Temp,
});
let rhs_local = builder.add_local(Local {
span: None,
ty: local_ty.clone(),
kind: LocalKind::Temp,
});
let lhs_local = builder.add_local(Local::temp(local_ty.clone()));
let rhs_local = builder.add_local(Local::temp(local_ty.clone()));
let lhs_place = Place {
local: lhs_local,
projection: vec![],
Expand Down
11 changes: 10 additions & 1 deletion crates/concrete_ir/src/lowering/common.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashMap;

use crate::{DefId, FnBody, Local, LocalIndex, ModuleBody, ProgramBody, Statement, Ty};
use crate::{DefId, FnBody, Local, LocalIndex, ModuleBody, ProgramBody, Statement, Ty, TyKind};

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct IdGenerator {
Expand Down Expand Up @@ -79,6 +79,15 @@ impl FnBodyBuilder {
id
}

pub fn add_temp_local(&mut self, ty_kind: TyKind) -> LocalIndex {
let id = self.body.locals.len();
self.body.locals.push(Local::temp(Ty {
span: None,
kind: ty_kind,
}));
id
}

pub fn get_local(&self, name: &str) -> Option<&Local> {
self.body.locals.get(*(self.name_to_local.get(name)?))
}
Expand Down

0 comments on commit 7dc6701

Please sign in to comment.