Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support integer literals for PC-offset operands #55

Merged
merged 3 commits into from
Dec 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 58 additions & 14 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,17 +234,15 @@ impl AsmParser {
})
}
InstrKind::Br(flag) => {
let label_tok = self.expect(TokenKind::Label)?;
let dest_label = Label::try_fill(self.get_span(label_tok.span));
let dest_label = self.expect_lit_or_label(9)?;
Ok(AirStmt::Branch { flag, dest_label })
}
InstrKind::Jmp => {
let src_reg = self.expect_reg()?;
Ok(AirStmt::Jump { src_reg })
}
InstrKind::Jsr => {
let label_tok = self.expect(TokenKind::Label)?;
let dest_label = Label::try_fill(self.get_span(label_tok.span));
let dest_label = self.expect_lit_or_label(11)?;
Ok(AirStmt::JumbSub { dest_label })
}
InstrKind::Jsrr => {
Expand All @@ -253,14 +251,12 @@ impl AsmParser {
}
InstrKind::Ld => {
let dest = self.expect_reg()?;
let label_tok = self.expect(TokenKind::Label)?;
let src_label = Label::try_fill(self.get_span(label_tok.span));
let src_label = self.expect_lit_or_label(9)?;
Ok(AirStmt::Load { dest, src_label })
}
InstrKind::Ldi => {
let dest = self.expect_reg()?;
let label_tok = self.expect(TokenKind::Label)?;
let src_label = Label::try_fill(self.get_span(label_tok.span));
let src_label = self.expect_lit_or_label(9)?;
Ok(AirStmt::LoadInd { dest, src_label })
}
InstrKind::Ldr => {
Expand All @@ -275,8 +271,7 @@ impl AsmParser {
}
InstrKind::Lea => {
let dest = self.expect_reg()?;
let label_tok = self.expect(TokenKind::Label)?;
let src_label = Label::try_fill(self.get_span(label_tok.span));
let src_label = self.expect_lit_or_label(9)?;
Ok(AirStmt::LoadEAddr { dest, src_label })
}
InstrKind::Not => {
Expand All @@ -288,17 +283,15 @@ impl AsmParser {
InstrKind::Rti => Ok(AirStmt::Interrupt),
InstrKind::St => {
let src_reg = self.expect_reg()?;
let label_tok = self.expect(TokenKind::Label)?;
let dest_label = Label::try_fill(self.get_span(label_tok.span));
let dest_label = self.expect_lit_or_label(9)?;
Ok(AirStmt::Store {
src_reg,
dest_label,
})
}
InstrKind::Sti => {
let src_reg = self.expect_reg()?;
let label_tok = self.expect(TokenKind::Label)?;
let dest_label = Label::try_fill(self.get_span(label_tok.span));
let dest_label = self.expect_lit_or_label(9)?;
Ok(AirStmt::StoreInd {
src_reg,
dest_label,
Expand Down Expand Up @@ -435,6 +428,31 @@ impl AsmParser {
None => return Err(error::parse_eof(self.src)),
}
}

fn expect_lit_or_label(&mut self, bits: u8) -> Result<Label> {
match self.toks.peek() {
Some(tok) => match tok.kind {
TokenKind::Label => {
let label_tok = self.expect(TokenKind::Label)?;
let label = Label::try_fill(self.get_span(label_tok.span));
Ok(label)
}
TokenKind::Lit(_) => {
let val = self.expect_lit(Bits::Signed(bits))?;
let label = Label::Ref(self.line + 1 + val);
Ok(label)
}
_ => {
return Err(error::parse_generic_unexpected(
self.src,
"literal or label",
*tok,
))
}
},
None => return Err(error::parse_eof(self.src)),
}
}
}

/// Convenient way to pass around bit limits
Expand Down Expand Up @@ -655,6 +673,21 @@ mod test {
)
}

#[test]
fn parse_branch_lit() {
let air = AsmParser::new("br x2").unwrap().parse().unwrap();
assert_eq!(
air.get(0),
&AsmLine {
line: 1,
stmt: AirStmt::Branch {
flag: Flag::Nzp,
dest_label: Label::Ref(0x2 + 0x2)
}
}
)
}

#[test]
fn parse_fill() {
let air = AsmParser::new("label .fill x30").unwrap().parse().unwrap();
Expand Down Expand Up @@ -740,6 +773,7 @@ mod test {
label add r0 r0 r0
br label
br not_existing
br x30
"#,
)
.unwrap()
Expand Down Expand Up @@ -776,5 +810,15 @@ mod test {
}
}
);
assert_eq!(
air.get(3),
&AsmLine {
line: 4,
stmt: AirStmt::Branch {
flag: Flag::Nzp,
dest_label: Label::Ref(0x5 + 0x30),
}
}
);
}
}
Loading