Skip to content

Commit

Permalink
Merge pull request #722 from powdr-labs/zero_with_second_argument
Browse files Browse the repository at this point in the history
Fix handling of ".zero" with second argument.
  • Loading branch information
chriseth authored Oct 25, 2023
2 parents 27ec685 + ee0ba2e commit 1090391
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 35 deletions.
69 changes: 34 additions & 35 deletions asm_utils/src/data_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,15 @@ fn extract_data_value<R: Register, F: FunctionOpKind>(
arguments: &[Argument<R, F>],
) -> Vec<DataValue> {
match (directive, arguments) {
(".zero", [Argument::Expression(Expression::Number(n))]) => {
vec![DataValue::Zero(*n as usize)]
}
(
".zero",
[Argument::Expression(Expression::Number(n))]
// TODO not clear what the second argument is
| [Argument::Expression(Expression::Number(n)), _],
[Argument::Expression(Expression::Number(n)), Argument::Expression(Expression::Number(value))],
) => {
vec![DataValue::Zero(*n as usize)]
assert!(0 <= *value && *value <= 0xff);
vec![DataValue::Direct(vec![*value as u8; *n as usize])]
}
(".ascii", [Argument::StringLiteral(data)]) => {
vec![DataValue::Direct(data.clone())]
Expand All @@ -107,47 +109,44 @@ fn extract_data_value<R: Register, F: FunctionOpKind>(
data.push(0);
vec![DataValue::Direct(data)]
}
(".word", data) => {
data
.iter()
.map(|x| {
match x {
Argument::Expression(Expression::Number(n)) =>{
let n = *n as u32;
DataValue::Direct(vec![
(n & 0xff) as u8,
(n >> 8 & 0xff) as u8,
(n >> 16 & 0xff) as u8,
(n >> 24 & 0xff) as u8,
])
}
Argument::Expression(Expression::Symbol(sym)) => {
DataValue::Reference(sym.clone())
}
Argument::Expression(
Expression::BinaryOp(BinaryOpKind::Sub, args)
) => match args.as_slice() {
[Expression::Symbol(a), Expression::Symbol(b)] => DataValue::Offset(a.to_string(), b.to_string()),
_ => panic!("Invalid .word directive")
}
_ => panic!("Invalid .word directive")
(".word", data) => data
.iter()
.map(|x| match x {
Argument::Expression(Expression::Number(n)) => {
let n = *n as u32;
DataValue::Direct(vec![
(n & 0xff) as u8,
(n >> 8 & 0xff) as u8,
(n >> 16 & 0xff) as u8,
(n >> 24 & 0xff) as u8,
])
}
Argument::Expression(Expression::Symbol(sym)) => DataValue::Reference(sym.clone()),
Argument::Expression(Expression::BinaryOp(BinaryOpKind::Sub, args)) => {
match args.as_slice() {
[Expression::Symbol(a), Expression::Symbol(b)] => {
DataValue::Offset(a.to_string(), b.to_string())
}
})
.collect::<Vec<DataValue>>()
}
_ => panic!("Invalid .word directive"),
}
}
_ => panic!("Invalid .word directive"),
})
.collect::<Vec<DataValue>>(),
(".byte", data) => {
// TODO alignment?
vec![DataValue::Direct(data
.iter()
vec![DataValue::Direct(
data.iter()
.map(|x| {
if let Argument::Expression(Expression::Number(n)) = x {
*n as u8
} else {
panic!("Invalid argument to .byte directive")
}
})
.collect::<Vec<u8>>())]
.collect::<Vec<u8>>(),
)]
}
_ => panic!()
_ => panic!(),
}
}
7 changes: 7 additions & 0 deletions riscv/tests/riscv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ fn test_trivial() {
verify_file(case, vec![], &CoProcessors::base())
}

#[test]
#[ignore = "Too slow"]
fn test_zero_with_values() {
let case = "zero_with_values.rs";
verify_file(case, vec![], &CoProcessors::base())
}

#[test]
#[ignore = "Too slow"]
fn test_poseidon_gl() {
Expand Down
12 changes: 12 additions & 0 deletions riscv/tests/riscv_data/zero_with_values.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#![no_std]

// This is stored as a data with the ".zero" directive,
// but in the variant where it repeats something else than zero.
const DATA: &str = "1111111111111111";

#[no_mangle]
pub fn main() {
for c in DATA.chars() {
assert_eq!(c, '1');
}
}

0 comments on commit 1090391

Please sign in to comment.