Get rid of useless helper functions, show mem addr of ops in the output

This commit is contained in:
Lumi Kalt 2024-01-23 01:19:24 +00:00
parent 650390a676
commit 78165b9b4e
6 changed files with 35 additions and 41 deletions

View file

@ -21,6 +21,7 @@ pub struct Env {
registers: [u32; 32],
pub stack: Vec<u32>, // TODO: Find the size of the stack
pub instructions: Vec<u32>,
pc: u32,
}
impl Env {
@ -71,6 +72,7 @@ impl Env {
registers: [0; 32],
stack: Vec::from([0; 1024]), // 1024 * 64 = 64 KiB stack
instructions: Vec::new(),
pc: 0,
}
}
@ -82,10 +84,7 @@ impl Env {
self.registers[reg as usize]
}
pub fn alias_to_register(&self, reg: &str) -> Option<u32> {
self.register_alias.get(reg).copied()
}
pub fn xn_to_register(&self, reg: &str) -> Option<u32> {
pub fn str_to_register(&self, reg: &str) -> Option<u32> {
if reg == "x0" {
Some(0)
} else if reg.starts_with("x") && !reg[1..].starts_with("0") {
@ -94,21 +93,9 @@ impl Env {
_ => None,
}
} else {
None
self.register_alias.get(reg).copied()
}
}
pub fn reg_to_register(&self, reg: &str) -> Option<u32> {
if reg.starts_with("x") {
self.xn_to_register(reg)
} else {
self.alias_to_register(reg)
}
}
pub fn is_valid_register(&self, reg: &str) -> bool {
self.alias_to_register(reg)
.or_else(|| self.xn_to_register(reg))
.is_some()
}
pub fn add_label(&mut self, label: &str, value: u32) {
self.labels.insert(label.to_string(), value);
@ -164,7 +151,7 @@ impl Env {
}
Arg::Register(id) => {
if let Token::Register(r) = &args[k].0 {
regs[id] = self.reg_to_register(&r).unwrap();
regs[id] = self.str_to_register(&r).unwrap();
Ok(())
} else {
Err((
@ -181,7 +168,7 @@ impl Env {
if let Token::Memory(i, r) = &args[k].0 {
if r.is_some() {
regs[k] = self
.reg_to_register(&if let Token::Register(r) =
.str_to_register(&if let Token::Register(r) =
*(r.clone().unwrap())
{
r

View file

@ -13,7 +13,7 @@ pub enum SyntaxErr {
UnmatchedParen(bool),
UnexpectedChar,
OutsideOp(String),
MemoryInvalidRegister,
InvalidRegister,
}
impl Display for SyntaxErr {
@ -22,7 +22,7 @@ impl Display for SyntaxErr {
SyntaxErr::UnmatchedParen(_) => write!(f, "unmatched parenthesis"),
SyntaxErr::UnexpectedChar => write!(f, "unexpected character"),
SyntaxErr::OutsideOp(kind) => write!(f, "`{kind}` before opcode"),
SyntaxErr::MemoryInvalidRegister => write!(f, "invalid register"),
SyntaxErr::InvalidRegister => write!(f, "invalid register"),
}
}
}
@ -34,7 +34,7 @@ impl SyntaxErr {
SyntaxErr::UnmatchedParen(true) => "add `(` before the register name".to_string(),
SyntaxErr::UnexpectedChar => "ensure the input is well-formed".to_string(),
SyntaxErr::OutsideOp(_) => format!("only add arguments after the opcode"),
SyntaxErr::MemoryInvalidRegister => {
SyntaxErr::InvalidRegister => {
"registers are either xN (N < 32 with no leading 0) or the standard aliases"
.to_string()
}

View file

@ -25,6 +25,7 @@ fn main() -> anyhow::Result<()> {
Ok(tokens) => {
let lines: Vec<&str> = input.lines().collect();
let size = lines.iter().map(|l| l.len()).max().unwrap();
let mut i = 0;
tokens.iter().for_each(|(token, loc)| {
let token = token.clone();
@ -33,16 +34,20 @@ fn main() -> anyhow::Result<()> {
match token.clone() {
Token::Op(..) => match env.assemble_op((token, loc.clone())) {
Ok(op) => {
let addr = (loc.line - 1) * 4;
let mut formatted = format!(
"{:<1$} {2:032b}",
lines[loc.line - 1].to_string() + ":",
"{:<1$} {3:02x}: {2:032b}",
lines[loc.line - 1],
size + 3,
op[0]
op[0],
i
);
i += 4;
if op.len() > 1 {
for op in op[1..].iter() {
formatted += &format!("\n{:<1$} {2:032b}", "", size + 3, op);
formatted += &format!("\n{:<1$} {3:02x}: {2:032b}", "", size + 3, op, i);
i += 4;
}
}
println!("{}", formatted);
@ -66,7 +71,7 @@ fn main() -> anyhow::Result<()> {
},
Token::Label(name) => {
println!(
"{:<1$} <{2:04x}>",
"{:<1$} <{2:02x}>",
name.clone() + ":",
size + 3,
env.get_label(&name).unwrap()

View file

@ -130,9 +130,9 @@ fn parse_line(env: &Env, input: &str, loc: &mut Loc) -> Result<Vec<(Token, Loc)>
let end = loc.end + 1;
let reg = reg.trim();
if !env.is_valid_register(reg) {
if env.str_to_register(reg).is_none() {
let err = Err((
SyntaxErr::MemoryInvalidRegister,
SyntaxErr::InvalidRegister,
Loc {
line: loc.line,
start,
@ -233,7 +233,7 @@ fn parse_line(env: &Env, input: &str, loc: &mut Loc) -> Result<Vec<(Token, Loc)>
)]
}
};
if env.is_valid_register(&name) {
if env.str_to_register(&name).is_some() {
return vec![(
Token::Error((
SyntaxErr::OutsideOp("register".to_string()),
@ -247,7 +247,7 @@ fn parse_line(env: &Env, input: &str, loc: &mut Loc) -> Result<Vec<(Token, Loc)>
for (token, loc) in group[1..].iter() {
match token.clone() {
Token::Register(name) => {
if env.is_valid_register(&name) {
if env.str_to_register(&name).is_some() {
args.push((token.clone(), loc.clone()));
} else {
args.push((Token::Symbol(name.to_owned()), *loc))

View file

@ -49,7 +49,7 @@ fn lui() {
&with(
get_instruction("lui"),
13609 << 12,
vec![env.alias_to_register("a0").unwrap()]
vec![env.str_to_register("a0").unwrap()]
)
.0
.to_string(),
@ -78,8 +78,8 @@ fn sb() {
-4i32 as u32, // imm
vec![
0, // rd
env.alias_to_register("sp").unwrap(), // ra
env.alias_to_register("t5").unwrap() // rb
env.str_to_register("sp").unwrap(), // ra
env.str_to_register("t5").unwrap() // rb
],
)
.0
@ -108,9 +108,9 @@ fn add() {
get_instruction("add"),
0, // imm
vec![
env.alias_to_register("a0").unwrap(), // rd
env.alias_to_register("a0").unwrap(), // ra
env.alias_to_register("a1").unwrap() // rb
env.str_to_register("a0").unwrap(), // rd
env.str_to_register("a0").unwrap(), // ra
env.str_to_register("a1").unwrap() // rb
]
)
.0
@ -140,8 +140,8 @@ fn addi() {
get_instruction("addi"),
1,
vec![
env.alias_to_register("a0").unwrap(),
env.alias_to_register("a0").unwrap()
env.str_to_register("a0").unwrap(),
env.str_to_register("a0").unwrap()
],
)
.0
@ -173,8 +173,8 @@ fn beq() {
4,
vec![
0, // no rd
env.alias_to_register("a0").unwrap(),
env.alias_to_register("a1").unwrap()
env.str_to_register("a0").unwrap(),
env.str_to_register("a1").unwrap()
]
)
.0

2
test.s
View file

@ -7,4 +7,6 @@ factorial:
mul a1 a1 a0
addi a0 a0 -1
j factorial
end:
nop