triton_isa

Macro triton_asm

Source
macro_rules! triton_asm {
    (@fmt $fmt:expr, $($args:expr,)*; ) => { ... };
    (@fmt $fmt:expr, $($args:expr,)*;
        hint $var:ident: $ty:ident = stack[$start:literal..$end:literal] $($tail:tt)*) => { ... };
    (@fmt $fmt:expr, $($args:expr,)*;
        hint $var:ident = stack[$start:literal..$end:literal] $($tail:tt)*) => { ... };
    (@fmt $fmt:expr, $($args:expr,)*;
        hint $var:ident: $ty:ident = stack[$index:literal] $($tail:tt)*) => { ... };
    (@fmt $fmt:expr, $($args:expr,)*;
        hint $var:ident = stack[$index:literal] $($tail:tt)*) => { ... };
    (@fmt $fmt:expr, $($args:expr,)*; $label_declaration:ident: $($tail:tt)*) => { ... };
    (@fmt $fmt:expr, $($args:expr,)*; $instruction:ident $($tail:tt)*) => { ... };
    (@fmt $fmt:expr, $($args:expr,)*; $instruction_argument:literal $($tail:tt)*) => { ... };
    (@fmt $fmt:expr, $($args:expr,)*; {$label_declaration:expr}: $($tail:tt)*) => { ... };
    (@fmt $fmt:expr, $($args:expr,)*; {&$instruction_list:expr} $($tail:tt)*) => { ... };
    (@fmt $fmt:expr, $($args:expr,)*; {$expression:expr} $($tail:tt)*) => { ... };
    [pop $arg:literal; $num:expr] => { ... };
    [push $arg:literal; $num:expr] => { ... };
    [divine $arg:literal; $num:expr] => { ... };
    [pick $arg:literal; $num:expr] => { ... };
    [place $arg:literal; $num:expr] => { ... };
    [dup $arg:literal; $num:expr] => { ... };
    [swap $arg:literal; $num:expr] => { ... };
    [call $arg:ident; $num:expr] => { ... };
    [read_mem $arg:literal; $num:expr] => { ... };
    [write_mem $arg:literal; $num:expr] => { ... };
    [read_io $arg:literal; $num:expr] => { ... };
    [write_io $arg:literal; $num:expr] => { ... };
    [$instr:ident; $num:expr] => { ... };
    {$($source_code:tt)*} => { ... };
}
Expand description

Compile Triton assembly into a list of labelled Instructions. Similar to triton_program!, it is possible to use string-like interpolation to insert instructions, arguments, labels, or other expressions.

Similar to vec!, a single instruction can be repeated a specified number of times.

Furthermore, a list of LabelledInstructions can be inserted like so: {&list}.

The labels for instruction call, if any, are also parsed. Instruction call can refer to a label defined later in the program, i.e., labels are not checked for existence or uniqueness by this parser.

§Examples

let push_argument = 42;
let instructions = triton_asm!(
    push 1 call some_label
    push {push_argument}
    some_other_label: skiz halt return
);
assert_eq!(7, instructions.len());

One instruction repeated several times:

let instructions = triton_asm![sponge_absorb; 3];
assert_eq!(3, instructions.len());
assert_eq!(LabelledInstruction::Instruction(SpongeAbsorb), instructions[0]);
assert_eq!(LabelledInstruction::Instruction(SpongeAbsorb), instructions[1]);
assert_eq!(LabelledInstruction::Instruction(SpongeAbsorb), instructions[2]);

Inserting substring of labelled instructions:

let insert_me = triton_asm!(
    pop 1
    nop
    pop 1
);
let surrounding_code = triton_asm!(
    push 0
    {&insert_me}
    push 1
);
assert_eq!(LabelledInstruction::Instruction(Pop(N1)), surrounding_code[1]);
assert_eq!(LabelledInstruction::Instruction(Pop(N1)), surrounding_code[3]);

§Panics

Panics if the instructions cannot be parsed. For examples, see triton_program!, with the exception that labels are not checked for existence or uniqueness.