Skip to content

Commit

Permalink
better names
Browse files Browse the repository at this point in the history
  • Loading branch information
oflatt committed May 29, 2024
1 parent f7b1b09 commit cfa288b
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions src/canonicalize_names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ fn canonicalize_func_names(func: &Function) -> Function {
}

impl Renamer {
fn get_name(&mut self, name: &str) -> String {
/// Get a new name for a variable. If the variable has already been renamed,
/// return the new name. Otherwise, generate a new name and return it.
fn get_name(&mut self, name: &str, prefix: String) -> String {
if let Some(new_name) = self.name_map.get(name) {
new_name.clone()
} else {
let new_name = format!("v{}_", self.name_map.len());
let new_name = format!("{}{}_", prefix, self.name_map.len());
self.name_map.insert(name.to_string(), new_name.clone());
new_name
}
Expand All @@ -58,7 +60,7 @@ impl Renamer {
Code::Instruction(self.canonicalize_instruction_names(instr))
}
Code::Label { label, pos } => Code::Label {
label: self.get_name(label),
label: self.get_name(label, "b".to_string()),
pos: pos.clone(),
},
}
Expand All @@ -73,7 +75,7 @@ impl Renamer {
const_type,
value,
} => Instruction::Constant {
dest: self.get_name(dest),
dest: self.get_name(dest, "c".to_string()),
op: *op,
pos: pos.clone(),
const_type: const_type.clone(),
Expand All @@ -89,16 +91,22 @@ impl Renamer {
op_type,
} => Instruction::Value {
args: {
let mut args: Vec<_> = args.iter().map(|arg| self.get_name(arg)).collect();
let mut args: Vec<_> = args
.iter()
.map(|arg| self.get_name(arg, "v".to_string()))
.collect();
use bril_rs::ValueOps::*;
if matches!(op, Add | Mul | Eq | And | Or) {
args.sort();
}
args
},
dest: self.get_name(dest),
dest: self.get_name(dest, "v".to_string()),
funcs: funcs.clone(),
labels: labels.iter().map(|label| self.get_name(label)).collect(),
labels: labels
.iter()
.map(|label| self.get_name(label, "b".to_string()))
.collect(),
op: *op,
pos: pos.clone(),
op_type: op_type.clone(),
Expand All @@ -110,9 +118,15 @@ impl Renamer {
op,
pos,
} => Instruction::Effect {
args: args.iter().map(|arg| self.get_name(arg)).collect(),
args: args
.iter()
.map(|arg| self.get_name(arg, "v".to_string()))
.collect(),
funcs: funcs.clone(),
labels: labels.iter().map(|label| self.get_name(label)).collect(),
labels: labels
.iter()
.map(|label| self.get_name(label, "b".to_string()))
.collect(),
op: *op,
pos: pos.clone(),
},
Expand Down

0 comments on commit cfa288b

Please sign in to comment.