From df1df84109c8bb9ab4bc9e1bbf871f5fe5df735e Mon Sep 17 00:00:00 2001 From: longfangsong Date: Fri, 6 Jan 2023 03:20:11 +0800 Subject: [PATCH] fix a stupid li bug --- Cargo.toml | 4 ++++ build.rs | 3 +++ src/filter.rs | 4 ++-- src/main.rs | 22 +++++++++++++--------- test_cases/li.cases | 8 +++++++- 5 files changed, 29 insertions(+), 12 deletions(-) create mode 100644 build.rs diff --git a/Cargo.toml b/Cargo.toml index 07bcb62..7d8fcc5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..4a0dfc4 --- /dev/null +++ b/build.rs @@ -0,0 +1,3 @@ +fn main() -> shadow_rs::SdResult<()> { + shadow_rs::new() +} diff --git a/src/filter.rs b/src/filter.rs index 14e65e1..947b08f 100644 --- a/src/filter.rs +++ b/src/filter.rs @@ -31,14 +31,14 @@ pub fn filter_bits_at(n: &Value, params: &HashMap) -> 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", diff --git a/src/main.rs b/src/main.rs index 5ae4d67..6d20615 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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, @@ -54,15 +58,15 @@ fn replace_complex_pseudo(preprocessed: &[Line]) -> Vec { 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(¶ms[1]).unwrap(); + let param: i64 = parse_int::parse(¶ms[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(), @@ -71,12 +75,12 @@ fn replace_complex_pseudo(preprocessed: &[Line]) -> Vec { } 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 { @@ -84,7 +88,7 @@ fn replace_complex_pseudo(preprocessed: &[Line]) -> Vec { params: vec![ params[0].clone(), params[0].clone(), - format!("{}", lower), + format!("{lower}"), ], })); } @@ -247,7 +251,7 @@ fn parse_param(code_param: &str, labels: &HashMap) -> ParsedParam { } else if let Some(imm) = labels.get(code_param) { ParsedParam::Immediate(*imm) } else { - panic!("unknown parameter: {}", code_param); + panic!("unknown parameter: {code_param}"); } } @@ -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)] @@ -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(); } } } diff --git a/test_cases/li.cases b/test_cases/li.cases index bc29ec4..978b506 100644 --- a/test_cases/li.cases +++ b/test_cases/li.cases @@ -27,4 +27,10 @@ fffff337 ~ li x6, -65535 ffff0337 -00130313 \ No newline at end of file +00130313 +~ +li a0, 2147491840 +80002537 +~ +li a0, -2147475456 +80002537 \ No newline at end of file