Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve Triton assembly macros #240

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 45 additions & 61 deletions triton-vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,14 +302,31 @@ macro_rules! triton_program {
/// [tasm]: https://triton-vm.org/spec/instructions.html
#[macro_export]
macro_rules! triton_asm {
(@fmt $fmt:expr, $($args:expr,)*; ) => {
format_args!($fmt $(,$args)*).to_string()
};

(@fmt $fmt:expr, $($args:expr,)*; $label_declaration:ident: $($tail:tt)*) => {
$crate::triton_asm!(@fmt
concat!($fmt, " ", stringify!($label_declaration), ": "), $($args,)*; $($tail)*
)
};
(@fmt $fmt:expr, $($args:expr,)*; {$label_declaration:expr}: $($tail:tt)*) => {
$crate::triton_asm!(@fmt concat!($fmt, " {}: "), $($args,)* $label_declaration,; $($tail)*)
};
(@fmt $fmt:expr, $($args:expr,)*; {$label_head:expr}$label_tail:ident: $($tail:tt)*) => {
$crate::triton_asm!(
@fmt concat!($fmt, " {}{}: "),
$($args,)*
$label_head,
stringify!($label_tail),;
$($tail)*
)
};

(@fmt $fmt:expr, $($args:expr,)*; call {$label_head:expr}$label_tail:ident $($tail:tt)*) => {
$crate::triton_asm!(@fmt
concat!($fmt, " call {}{} "), $($args,)* $label_head, stringify!($label_tail),; $($tail)*
)
};

(@fmt $fmt:expr, $($args:expr,)*; $instruction:ident $($tail:tt)*) => {
$crate::triton_asm!(@fmt
concat!($fmt, " ", stringify!($instruction), " "), $($args,)*; $($tail)*
Expand All @@ -320,32 +337,25 @@ macro_rules! triton_asm {
concat!($fmt, " ", stringify!($instruction_argument), " "), $($args,)*; $($tail)*
)
};
(@fmt $fmt:expr, $($args:expr,)*; {$label_declaration:expr}: $($tail:tt)*) => {
$crate::triton_asm!(@fmt concat!($fmt, "{}: "), $($args,)* $label_declaration,; $($tail)*)
};
(@fmt $fmt:expr, $($args:expr,)*; {&$instruction_list:expr} $($tail:tt)*) => {
$crate::triton_asm!(@fmt
concat!($fmt, "{} "), $($args,)*
concat!($fmt, " {} "), $($args,)*
$instruction_list.iter().map(|instr| instr.to_string()).collect::<Vec<_>>().join(" "),;
$($tail)*
)
};
(@fmt $fmt:expr, $($args:expr,)*; {$expression:expr} $($tail:tt)*) => {
$crate::triton_asm!(@fmt concat!($fmt, "{} "), $($args,)* $expression,; $($tail)*)
$crate::triton_asm!(@fmt concat!($fmt, " {} "), $($args,)* $expression,; $($tail)*)
};
(@fmt $fmt:expr, $($args:expr,)*; ) => {
format_args!($fmt $(,$args)*).to_string()
};

// repeated instructions
[pop $arg:literal; $num:expr] => { vec![ $crate::triton_instr!(pop $arg); $num ] };
[push $arg:literal; $num:expr] => { vec![ $crate::triton_instr!(push $arg); $num ] };
[divine $arg:literal; $num:expr] => { vec![ $crate::triton_instr!(divine $arg); $num ] };
[dup $arg:literal; $num:expr] => { vec![ $crate::triton_instr!(dup $arg); $num ] };
[swap $arg:literal; $num:expr] => { vec![ $crate::triton_instr!(swap $arg); $num ] };
[call $arg:ident; $num:expr] => { vec![ $crate::triton_instr!(call $arg); $num ] };
[read_mem $arg:literal; $num:expr] => { vec![ $crate::triton_instr!(read_mem $arg); $num ] };
[write_mem $arg:literal; $num:expr] => { vec![ $crate::triton_instr!(write_mem $arg); $num ] };
[read_io $arg:literal; $num:expr] => { vec![ $crate::triton_instr!(read_io $arg); $num ] };
[write_io $arg:literal; $num:expr] => { vec![ $crate::triton_instr!(write_io $arg); $num ] };
[$instr:ident; $num:expr] => { vec![ $crate::triton_instr!($instr); $num ] };
[push $arg:expr; $num:expr] => { vec![ $crate::triton_instr!(push $arg); $num ] };
[call $arg:ident; $num:expr] => { vec![ $crate::triton_instr!(call $arg); $num ] };
[$instr:ident $arg:literal; $num:expr] => { vec![ $crate::triton_instr!($instr $arg); $num ] };

// entry point
{$($source_code:tt)*} => {{
Expand All @@ -371,59 +381,22 @@ macro_rules! triton_asm {
/// [tasm]: https://triton-vm.org/spec/instructions.html
#[macro_export]
macro_rules! triton_instr {
(pop $arg:literal) => {{
let argument: $crate::op_stack::NumberOfWords = u32::try_into($arg).unwrap();
let instruction = $crate::instruction::AnInstruction::<String>::Pop(argument);
$crate::instruction::LabelledInstruction::Instruction(instruction)
($instr:ident) => {{
let (_, instructions) = $crate::parser::tokenize(stringify!($instr)).unwrap();
instructions[0].to_labelled_instruction()
}};
(push $arg:expr) => {{
let argument = $crate::BFieldElement::new($arg);
let instruction = $crate::instruction::AnInstruction::<String>::Push(argument);
$crate::instruction::LabelledInstruction::Instruction(instruction)
}};
(divine $arg:literal) => {{
let argument: $crate::op_stack::NumberOfWords = u32::try_into($arg).unwrap();
let instruction = $crate::instruction::AnInstruction::<String>::Divine(argument);
$crate::instruction::LabelledInstruction::Instruction(instruction)
}};
(dup $arg:literal) => {{
let argument: $crate::op_stack::OpStackElement = u32::try_into($arg).unwrap();
let instruction = $crate::instruction::AnInstruction::<String>::Dup(argument);
$crate::instruction::LabelledInstruction::Instruction(instruction)
}};
(swap $arg:literal) => {{
assert_ne!(0_u32, $arg, "`swap 0` is illegal.");
let argument: $crate::op_stack::OpStackElement = u32::try_into($arg).unwrap();
let instruction = $crate::instruction::AnInstruction::<String>::Swap(argument);
$crate::instruction::LabelledInstruction::Instruction(instruction)
}};
(call $arg:ident) => {{
let argument = stringify!($arg).to_string();
let instruction = $crate::instruction::AnInstruction::<String>::Call(argument);
$crate::instruction::LabelledInstruction::Instruction(instruction)
}};
(read_mem $arg:literal) => {{
let argument: $crate::op_stack::NumberOfWords = u32::try_into($arg).unwrap();
let instruction = $crate::instruction::AnInstruction::<String>::ReadMem(argument);
$crate::instruction::LabelledInstruction::Instruction(instruction)
}};
(write_mem $arg:literal) => {{
let argument: $crate::op_stack::NumberOfWords = u32::try_into($arg).unwrap();
let instruction = $crate::instruction::AnInstruction::<String>::WriteMem(argument);
$crate::instruction::LabelledInstruction::Instruction(instruction)
}};
(read_io $arg:literal) => {{
let argument: $crate::op_stack::NumberOfWords = u32::try_into($arg).unwrap();
let instruction = $crate::instruction::AnInstruction::<String>::ReadIo(argument);
$crate::instruction::LabelledInstruction::Instruction(instruction)
}};
(write_io $arg:literal) => {{
let argument: $crate::op_stack::NumberOfWords = u32::try_into($arg).unwrap();
let instruction = $crate::instruction::AnInstruction::<String>::WriteIo(argument);
$crate::instruction::LabelledInstruction::Instruction(instruction)
}};
($instr:ident) => {{
let (_, instructions) = $crate::parser::tokenize(stringify!($instr)).unwrap();
($instr:ident $arg:literal) => {{
let (_, instructions) = $crate::parser::tokenize(stringify!($instr $arg)).unwrap();
instructions[0].to_labelled_instruction()
}};
}
Expand Down Expand Up @@ -777,8 +750,19 @@ mod tests {
}

#[test]
#[should_panic(expected = "Index 0 is out of range for `NumberOfWords`")]
#[should_panic(expected = "expecting label, instruction or eof")]
fn parsing_pop_with_illegal_argument_fails() {
let _ = triton_instr!(pop 0);
}

#[test]
fn concatenate_labels_in_tasm_macros() {
let label = "my_label";
let program = triton_program! {
call {label}_inner
{label}_inner: halt
{label}: assert // intentional crash if this path is taken
};
let _ = program.run([].into(), [].into());
}
}
Loading