Merge pull request #854 from powdr-labs/fix_public_parsing

Fix public parsing and labels.
This commit is contained in:
chriseth
2023-12-19 15:40:10 +00:00
committed by GitHub
14 changed files with 54 additions and 53 deletions

View File

@@ -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;

View File

@@ -90,7 +90,7 @@ pub fn generate_machine_rom<T: FieldElement>(
// 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<T: FieldElement>(
.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<T: FieldElement>(
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
"#

View File

@@ -140,7 +140,7 @@ impl<T: Display> Display for Return<T> {
impl Display for LabelStatement {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
write!(f, "{}::", self.name)
write!(f, "{}:", self.name)
}
}

View File

@@ -214,7 +214,7 @@ impl<T: Display> Display for FunctionStatement<T> {
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,

View File

@@ -443,7 +443,7 @@ machine Machine {
function main {
inc_fp 7;
loop::
loop:
adjust_fp -2, loop;
}
}

View File

@@ -333,7 +333,7 @@ DebugDirectiveStatement: FunctionStatement<T> = {
}
LabelStatement: FunctionStatement<T> = {
<@L> <Identifier> "::" => FunctionStatement::Label(<>)
<@L> <Identifier> ":" => FunctionStatement::Label(<>)
}
// ---------------------------- Expressions -----------------------------
@@ -473,7 +473,7 @@ Term: Box<Expression<T>> = {
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 = {
<namespace:( <Identifier> "." )?> <name:Identifier> => NamespacedPolynomialReference{<>},
}
PublicReference: String = {
":" <Identifier>
}
MatchExpression: Box<Expression<T>> = {
"match" <BoxedExpression> "{" <MatchArms> "}" => 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(),

View File

@@ -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
"#,

View File

@@ -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<String> {
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",

View File

@@ -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
}

View File

@@ -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;

View File

@@ -14,7 +14,7 @@ machine Main {
instr nothing = sub.nothing
function main {
start::
start:
A <== one();
return;
}

View File

@@ -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;
}
}

View File

@@ -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;

View File

@@ -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;
}