From dae0e997d3677f534e3b4bf2f838a0ddbf865fe6 Mon Sep 17 00:00:00 2001 From: chriseth Date: Tue, 19 Dec 2023 13:44:27 +0100 Subject: [PATCH 1/2] Improve parsing of public references. --- parser/src/powdr.lalrpop | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/parser/src/powdr.lalrpop b/parser/src/powdr.lalrpop index 4682c1966..0a95f1920 100644 --- a/parser/src/powdr.lalrpop +++ b/parser/src/powdr.lalrpop @@ -473,7 +473,7 @@ Term: Box> = { FunctionCall => Box::new(Expression::FunctionCall(<>)), ConstantIdentifier => Box::new(Expression::Reference(NamespacedPolynomialReference{namespace: None, name: <>})), NamespacedPolynomialReference => Box::new(Expression::Reference(<>)), - PublicReference => Box::new(Expression::PublicReference(<>)), + PublicIdentifier => Box::new(Expression::PublicReference(<>)), FieldElement => Box::new(Expression::Number(<>)), StringLiteral => Box::new(Expression::String(<>)), MatchExpression, @@ -496,10 +496,6 @@ NamespacedPolynomialReference: NamespacedPolynomialReference = { "." )?> => NamespacedPolynomialReference{<>}, } -PublicReference: String = { - ":" -} - MatchExpression: Box> = { "match" "{" "}" => Box::new(Expression::MatchExpression(<>)) } @@ -542,6 +538,11 @@ ConstantIdentifier: String = { r"%[a-zA-Z_][a-zA-Z$_0-9@]*" => <>.to_string(), } +PublicIdentifier: String = { + r":[a-zA-Z_][a-zA-Z$_0-9@]*" => <>.strip_prefix(":").unwrap().to_string() + +} + FieldElement: T = { r"[0-9][0-9_]*" => T::from_str(&<>.replace('_', "")), r"0x[0-9A-Fa-f][0-9A-Fa-f_]*" => T::from_str_radix(&<>[2..].replace('_', ""), 16).unwrap(), From bef7d80c705e9db7c01307b424f9392deb222466 Mon Sep 17 00:00:00 2001 From: chriseth Date: Tue, 19 Dec 2023 14:07:21 +0100 Subject: [PATCH 2/2] Change labels to require just a single colon. --- analysis/README.md | 22 ++++++++--------- asm_to_pil/src/romgen.rs | 24 +++++++++---------- ast/src/asm_analysis/display.rs | 2 +- ast/src/parsed/display.rs | 2 +- linker/src/lib.rs | 2 +- parser/src/powdr.lalrpop | 2 +- riscv/src/bootloader.rs | 8 +++---- riscv/src/compiler.rs | 4 ++-- test_data/asm/batching/labels.asm | 8 +++---- test_data/asm/book/function.asm | 4 ++-- test_data/asm/different_signatures.asm | 2 +- test_data/asm/palindrome.asm | 8 +++---- test_data/asm/simple_sum.asm | 4 ++-- .../asm/vm_to_vm_dynamic_trace_length.asm | 4 ++-- 14 files changed, 48 insertions(+), 48 deletions(-) diff --git a/analysis/README.md b/analysis/README.md index 3524c3644..482ffedea 100644 --- a/analysis/README.md +++ b/analysis/README.md @@ -41,7 +41,7 @@ machine Main { instr nothing = sub.nothing function main { - start:: + start: A <== one(); return; } @@ -115,21 +115,21 @@ For our example, we get one ROM for each virtual machine. For `DifferentSignatur ``` rom { - _start:: + _start: _reset; // END BATCH Unimplemented _jump_to_operation; // END BATCH Label - _identity:: + _identity: return _input_0; // END BATCH Label - _nothing:: + _nothing: return 0; // END BATCH Label - _one:: + _one: return 1; // END BATCH Label - _sink:: + _sink: _loop; // END BATCH } @@ -138,18 +138,18 @@ rom { For `Main`: ``` rom { - _start:: + _start: _reset; // END BATCH Unimplemented _jump_to_operation; // END BATCH Label - _main:: - start:: + _main: + start: A <=Y= one(); // END BATCH Unimplemented return; // END BATCH Label - _sink:: + _sink: _loop; // END BATCH } @@ -234,7 +234,7 @@ The diff for our example program is as follows: - instr nothing = sub.nothing; - - function main { -- start:: +- start: - A <=Y= one(); - // END BATCH Unimplemented - return; diff --git a/asm_to_pil/src/romgen.rs b/asm_to_pil/src/romgen.rs index 061da2ef9..93a0e6148 100644 --- a/asm_to_pil/src/romgen.rs +++ b/asm_to_pil/src/romgen.rs @@ -90,7 +90,7 @@ pub fn generate_machine_rom( // add the beginning of the dispatcher rom.extend(vec![ Batch::from(vec![ - parse_function_statement("_start::"), + parse_function_statement("_start:"), parse_function_statement(&format!("{RESET_NAME};")), ]) .reason(IncompatibleSet::from(Incompatible::Unimplemented)), @@ -189,7 +189,7 @@ pub fn generate_machine_rom( .first_mut() .expect("function should have at least one statement as it must return") .statements - .insert(0, parse_function_statement(&format!("_{}::", name))); + .insert(0, parse_function_statement(&format!("_{}:", name))); // modify the last batch to be caused by the coming label let last = batches @@ -217,7 +217,7 @@ pub fn generate_machine_rom( let sink_id = T::from(rom.len() as u64); rom.extend(vec![Batch::from(vec![ - parse_function_statement("_sink::"), + parse_function_statement("_sink:"), parse_function_statement("_loop;"), ])]); @@ -283,12 +283,12 @@ mod tests { .to_string() .replace('\t', " "), r#" -_start:: +_start: _reset; // END BATCH Unimplemented _jump_to_operation; // END BATCH Label -_sink:: +_sink: _loop; // END BATCH "# @@ -321,15 +321,15 @@ _loop; .to_string() .replace('\t', " "), r#" -_start:: +_start: _reset; // END BATCH Unimplemented _jump_to_operation; // END BATCH Label -_identity:: +_identity: return _input_0; // END BATCH Label -_sink:: +_sink: _loop; // END BATCH "# @@ -380,22 +380,22 @@ _loop; .to_string() .replace('\t', " "), r#" -_start:: +_start: _reset; // END BATCH Unimplemented _jump_to_operation; // END BATCH Label -_f_add:: +_f_add: A <=Z= add(_input_0, _input_1); // END BATCH return A; // END BATCH Label -_f_assert_zero:: +_f_assert_zero: assert_zero _input_0; // END BATCH return 0; // END BATCH Label -_sink:: +_sink: _loop; // END BATCH "# diff --git a/ast/src/asm_analysis/display.rs b/ast/src/asm_analysis/display.rs index a02650ceb..3e7df57e1 100644 --- a/ast/src/asm_analysis/display.rs +++ b/ast/src/asm_analysis/display.rs @@ -140,7 +140,7 @@ impl Display for Return { impl Display for LabelStatement { fn fmt(&self, f: &mut Formatter<'_>) -> Result { - write!(f, "{}::", self.name) + write!(f, "{}:", self.name) } } diff --git a/ast/src/parsed/display.rs b/ast/src/parsed/display.rs index ac23386dc..d6784e0a2 100644 --- a/ast/src/parsed/display.rs +++ b/ast/src/parsed/display.rs @@ -214,7 +214,7 @@ impl Display for FunctionStatement { format!(" {}", inputs.iter().format(", ")) } ), - FunctionStatement::Label(_, name) => write!(f, "{name}::"), + FunctionStatement::Label(_, name) => write!(f, "{name}:"), FunctionStatement::DebugDirective(_, dir) => write!(f, "{dir}"), FunctionStatement::Return(_, values) => write!( f, diff --git a/linker/src/lib.rs b/linker/src/lib.rs index a106b1a69..c8f7e8d55 100644 --- a/linker/src/lib.rs +++ b/linker/src/lib.rs @@ -443,7 +443,7 @@ machine Machine { function main { inc_fp 7; - loop:: + loop: adjust_fp -2, loop; } } diff --git a/parser/src/powdr.lalrpop b/parser/src/powdr.lalrpop index 0a95f1920..7a8308cb0 100644 --- a/parser/src/powdr.lalrpop +++ b/parser/src/powdr.lalrpop @@ -333,7 +333,7 @@ DebugDirectiveStatement: FunctionStatement = { } LabelStatement: FunctionStatement = { - <@L> "::" => FunctionStatement::Label(<>) + <@L> ":" => FunctionStatement::Label(<>) } // ---------------------------- Expressions ----------------------------- diff --git a/riscv/src/bootloader.rs b/riscv/src/bootloader.rs index 99fadfb01..03a655097 100644 --- a/riscv/src/bootloader.rs +++ b/riscv/src/bootloader.rs @@ -86,7 +86,7 @@ x2 <=X= 0; branch_if_zero x1, end_page_loop; -start_page_loop:: +start_page_loop: // Page number x3 <== load_bootloader_input(x2 * {bootloader_inputs_per_page} + {num_registers} + 1); @@ -156,7 +156,7 @@ P5 <=X= 0; P6 <=X= 0; P7 <=X= 0; jump level_{i}_end; -level_{i}_is_right:: +level_{i}_is_right: P4 <=X= P0; P5 <=X= P1; P6 <=X= P2; @@ -165,7 +165,7 @@ P0 <=X= 0; P1 <=X= 0; P2 <=X= 0; P3 <=X= 0; -level_{i}_end:: +level_{i}_end: P0, P1, P2, P3 <== poseidon_gl(P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11); "# )); @@ -178,7 +178,7 @@ x2 <=X= x2 + 1; branch_if_nonzero x2 - x1, start_page_loop; -end_page_loop:: +end_page_loop: // Initialize registers, starting with index 0 "#, diff --git a/riscv/src/compiler.rs b/riscv/src/compiler.rs index 99d5263fd..e0ea6ceec 100644 --- a/riscv/src/compiler.rs +++ b/riscv/src/compiler.rs @@ -217,7 +217,7 @@ pub fn compile( .into_iter() .flat_map(|v| process_statement(v, coprocessors)), ) - .chain(["// This is the data initialization routine.\n__data_init::".to_string()]) + .chain(["// This is the data initialization routine.\n__data_init:".to_string()]) .chain(data_code) .chain(["// This is the end of the data initialization routine.\nret;".to_string()]) .collect(); @@ -746,7 +746,7 @@ fn runtime(coprocessors: &CoProcessors) -> String { fn process_statement(s: Statement, coprocessors: &CoProcessors) -> Vec { match &s { - Statement::Label(l) => vec![format!("{}::", escape_label(l))], + Statement::Label(l) => vec![format!("{}:", escape_label(l))], Statement::Directive(directive, args) => match (directive.as_str(), &args[..]) { ( ".loc", diff --git a/test_data/asm/batching/labels.asm b/test_data/asm/batching/labels.asm index 86da88467..fa9e8a8e5 100644 --- a/test_data/asm/batching/labels.asm +++ b/test_data/asm/batching/labels.asm @@ -5,18 +5,18 @@ machine Main { reg A; function main { - label_with_next:: + label_with_next: A <=X= 1; // END BATCH Label - labels_with_next:: - other_label_just_after:: + labels_with_next: + other_label_just_after: A <=X= 2; // END BATCH Unimplemented A <=X= 2; // END BATCH Label - end:: + end: return; // END BATCH } diff --git a/test_data/asm/book/function.asm b/test_data/asm/book/function.asm index 7e6a1dee0..12b478322 100644 --- a/test_data/asm/book/function.asm +++ b/test_data/asm/book/function.asm @@ -43,7 +43,7 @@ machine Machine { CNT <=X= 3; // ANCHOR_END: literals // ANCHOR: label - start:: + start: // ANCHOR_END: label // if `CNT` is `0`, jump to `end` jmpz CNT, end; @@ -57,7 +57,7 @@ machine Machine { // ANCHOR_END: instruction // jump back to `start` jmp start; - end:: + end: // check that `A == ((2**2)**2)**2` // ANCHOR: instruction_statement assert_zero A - ((2**2)**2)**2; diff --git a/test_data/asm/different_signatures.asm b/test_data/asm/different_signatures.asm index f45f0a41b..84ac2ec8f 100644 --- a/test_data/asm/different_signatures.asm +++ b/test_data/asm/different_signatures.asm @@ -14,7 +14,7 @@ machine Main { instr nothing = sub.nothing function main { - start:: + start: A <== one(); return; } diff --git a/test_data/asm/palindrome.asm b/test_data/asm/palindrome.asm index 3fa1dea4d..9e667e290 100644 --- a/test_data/asm/palindrome.asm +++ b/test_data/asm/palindrome.asm @@ -42,19 +42,19 @@ machine Palindrome { ADDR <=X= 0; mstore CNT; - store_values:: + store_values: jmpz CNT, check_start; ADDR <=X= CNT; mstore ${ ("input", CNT) }; CNT <=X= CNT - 1; jmp store_values; - check_start:: + check_start: ADDR <=X= 0; mload CNT; I <=X= 0; - check:: + check: jmpz I - CNT, end; ADDR <=X= I + 1; mload A; @@ -64,7 +64,7 @@ machine Palindrome { I <=X= I + 1; jmp check; - end:: + end: return; } } \ No newline at end of file diff --git a/test_data/asm/simple_sum.asm b/test_data/asm/simple_sum.asm index 369a97927..9ce824c60 100644 --- a/test_data/asm/simple_sum.asm +++ b/test_data/asm/simple_sum.asm @@ -31,14 +31,14 @@ machine Main { function main { CNT <=X= ${ ("input", 1) }; - start:: + start: jmpz CNT, check; A <=X= A + ${ ("input", CNT + 1) }; // Could use "CNT <=X= CNT - 1", but that would need X. dec_CNT; jmp start; - check:: + check: A <=X= A - ${ ("input", 0) }; assert_zero A; return; diff --git a/test_data/asm/vm_to_vm_dynamic_trace_length.asm b/test_data/asm/vm_to_vm_dynamic_trace_length.asm index 31e1b5152..d5f317d53 100644 --- a/test_data/asm/vm_to_vm_dynamic_trace_length.asm +++ b/test_data/asm/vm_to_vm_dynamic_trace_length.asm @@ -57,13 +57,13 @@ machine Pow { A <=X= 1; CNT <=X= y; - start:: + start: jmpz CNT, done; A <== mul(A, x); CNT <=X= CNT - 1; jmp start; - done:: + done: return A; }