Skip to content

Commit

Permalink
fix a stupid li bug
Browse files Browse the repository at this point in the history
  • Loading branch information
longfangsong committed Jan 5, 2023
1 parent 118eef2 commit df1df84
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 12 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ once_cell = "1.16.0"
parse_int = "0.6.0"
serde = { version = "1.0.151", features = ["derive"] }
serde_json = "1.0.91"
shadow-rs = "0.20.0"
tera = "1.17.1"

[build-dependencies]
shadow-rs = "0.20.0"
3 changes: 3 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() -> shadow_rs::SdResult<()> {
shadow_rs::new()
}
4 changes: 2 additions & 2 deletions src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ pub fn filter_bits_at(n: &Value, params: &HashMap<String, Value>) -> tera::Resul
.map(|it| it.as_u64().unwrap() as usize);
let width = bits.len();
let result = bits_at(n, bits);
Ok(Value::String(format!("{:0width$b}", result)))
Ok(Value::String(format!("{result:0width$b}")))
} else if let (Some(start), Some(end)) = (params.get("start"), params.get("end")) {
let start = start.as_u64().unwrap() as usize;
let end = end.as_u64().unwrap() as usize;
let bits = start..end;
let width = bits.len();
let result = bits_at(n, bits);
Ok(Value::String(format!("{:0width$b}", result)))
Ok(Value::String(format!("{result:0width$b}")))
} else {
Err(tera::Error::msg(
"indexes or start and end must be provided",
Expand Down
22 changes: 13 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ use tera::{Context, Tera};

mod filter;
mod param;
use shadow_rs::shadow;

use crate::{filter::*, param::ParsedParam};

shadow!(build);

#[derive(Debug, PartialEq, Eq, Clone)]
pub struct UnparsedInstruction {
name: String,
Expand Down Expand Up @@ -54,15 +58,15 @@ fn replace_complex_pseudo(preprocessed: &[Line]) -> Vec<Line> {
Line::Tag(tag) => result.push(Line::Tag(tag.to_string())),
Line::Instruction(UnparsedInstruction { name, params }) => match name.as_str() {
"li" => {
let param: i32 = parse_int::parse(&params[1]).unwrap();
let param: i64 = parse_int::parse(&params[1]).unwrap();
let lower = param & 0xfff;
let lower_is_negative = lower > 0x7ff;
let higher = if lower_is_negative {
let higher = (if lower_is_negative {
// lower is, in fact, a negative number when used in addi
(param >> 12) + 1
} else {
param >> 12
};
}) & 0xffffffff;
if higher == 0 && lower == 0 {
result.push(Line::Instruction(UnparsedInstruction {
name: "mv".to_string(),
Expand All @@ -71,20 +75,20 @@ fn replace_complex_pseudo(preprocessed: &[Line]) -> Vec<Line> {
} else if higher == 0 {
result.push(Line::Instruction(UnparsedInstruction {
name: "addi".to_string(),
params: vec![params[0].clone(), "x0".to_string(), format!("{}", lower)],
params: vec![params[0].clone(), "x0".to_string(), format!("{lower}")],
}));
} else {
result.push(Line::Instruction(UnparsedInstruction {
name: "lui".to_string(),
params: vec![params[0].clone(), format!("0x{:x}", higher)],
params: vec![params[0].clone(), format!("0x{higher:x}")],
}));
if lower != 0 {
result.push(Line::Instruction(UnparsedInstruction {
name: "addi".to_string(),
params: vec![
params[0].clone(),
params[0].clone(),
format!("{}", lower),
format!("{lower}"),
],
}));
}
Expand Down Expand Up @@ -247,7 +251,7 @@ fn parse_param(code_param: &str, labels: &HashMap<String, i32>) -> ParsedParam {
} else if let Some(imm) = labels.get(code_param) {
ParsedParam::Immediate(*imm)
} else {
panic!("unknown parameter: {}", code_param);
panic!("unknown parameter: {code_param}");
}
}

Expand All @@ -269,7 +273,7 @@ enum OutputFormat {

/// SHUOSC RISC-V assembler
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
#[command(version, long_version = build::CLAP_LONG_VERSION, about, long_about = None)]
struct Args {
/// Input file path.
#[arg(short, long)]
Expand Down Expand Up @@ -297,7 +301,7 @@ fn main() {
}
} else {
for binary_instruction in binaries {
writeln!(output_file, "{:08x}", binary_instruction).unwrap();
writeln!(output_file, "{binary_instruction:08x}").unwrap();
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion test_cases/li.cases
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,10 @@ fffff337
~
li x6, -65535
ffff0337
00130313
00130313
~
li a0, 2147491840
80002537
~
li a0, -2147475456
80002537

0 comments on commit df1df84

Please sign in to comment.