A few ASM fixes

This commit is contained in:
Daniel Lubarov 2022-07-20 15:05:09 -07:00
parent 90f7e8a181
commit 47ea00d6c7
5 changed files with 40 additions and 16 deletions

View File

@ -6,13 +6,13 @@ global panic:
// Consumes the top element and asserts that it is zero.
%macro assert_zero
%jumpi panic
%jumpi(panic)
%endmacro
// Consumes the top element and asserts that it is nonzero.
%macro assert_nonzero
ISZERO
%jumpi panic
%jumpi(panic)
%endmacro
%macro assert_eq
@ -49,34 +49,34 @@ global panic:
%endmacro
%macro assert_eq_const(c)
%eq_const(c)
%eq_const($c)
%assert_nonzero
%endmacro
%macro assert_lt_const(c)
// %assert_zero is cheaper than %assert_nonzero, so we will leverage the
// fact that (x < c) == !(x >= c).
%ge_const(c)
%ge_const($c)
%assert_zero
%endmacro
%macro assert_le_const(c)
// %assert_zero is cheaper than %assert_nonzero, so we will leverage the
// fact that (x <= c) == !(x > c).
%gt_const(c)
%gt_const($c)
%assert_zero
%endmacro
%macro assert_gt_const(c)
// %assert_zero is cheaper than %assert_nonzero, so we will leverage the
// fact that (x > c) == !(x <= c).
%le_const(c)
%le_const($c)
%assert_zero
%endmacro
%macro assert_ge_const(c)
// %assert_zero is cheaper than %assert_nonzero, so we will leverage the
// fact that (x >= c) == !(x < c).
%lt_const(c)
%lt_const($c)
%assert_zero
%endmacro

View File

@ -6,7 +6,7 @@
// stack: offset
PUSH $segment
// stack: segment, offset
CURRENT_CONTEXT
GET_CONTEXT
// stack: context, segment, offset
MLOAD_GENERAL
// stack: value
@ -20,7 +20,7 @@
// stack: offset, value
PUSH $segment
// stack: segment, offset, value
CURRENT_CONTEXT
GET_CONTEXT
// stack: context, segment, offset, value
MSTORE_GENERAL
// stack: (empty)

View File

@ -132,13 +132,29 @@ fn expand_macro_call(
args.len()
);
let get_arg = |var| {
let param_index = _macro.get_param_index(var);
args[param_index].clone()
};
let expanded_item = _macro
.items
.iter()
.map(|item| {
if let Item::Push(PushTarget::MacroVar(var)) = item {
let param_index = _macro.get_param_index(var);
Item::Push(args[param_index].clone())
Item::Push(get_arg(var))
} else if let Item::MacroCall(name, args) = item {
let expanded_args = args
.iter()
.map(|arg| {
if let PushTarget::MacroVar(var) = arg {
get_arg(var)
} else {
arg.clone()
}
})
.collect();
Item::MacroCall(name.clone(), expanded_args)
} else {
item.clone()
}
@ -419,6 +435,17 @@ mod tests {
assert_eq!(kernel.code, vec![push1, 2, push1, 3, add]);
}
#[test]
fn macro_in_macro_with_vars() {
let kernel = parse_and_assemble(&[
"%macro foo(x) %bar($x) %bar($x) %endmacro",
"%macro bar(y) PUSH $y %endmacro",
"%foo(42)",
]);
let push = get_push_opcode(1);
assert_eq!(kernel.code, vec![push, 42, push, 42]);
}
#[test]
#[should_panic]
fn macro_with_wrong_vars() {

View File

@ -59,6 +59,7 @@ pub(crate) fn get_opcode(mnemonic: &str) -> u8 {
"GASLIMIT" => 0x45,
"CHAINID" => 0x46,
"BASEFEE" => 0x48,
"PROVER_INPUT" => 0x49,
"POP" => 0x50,
"MLOAD" => 0x51,
"MSTORE" => 0x52,

View File

@ -21,12 +21,10 @@ pub(crate) enum Segment {
TxnData = 7,
/// Raw RLP data.
RlpRaw = 8,
/// RLP data that has been parsed and converted to a more "friendly" format.
RlpParsed = 9,
}
impl Segment {
pub(crate) const COUNT: usize = 10;
pub(crate) const COUNT: usize = 9;
pub(crate) fn all() -> [Self; Self::COUNT] {
[
@ -39,7 +37,6 @@ impl Segment {
Self::KernelGeneral,
Self::TxnData,
Self::RlpRaw,
Self::RlpParsed,
]
}
@ -55,7 +52,6 @@ impl Segment {
Segment::KernelGeneral => "SEGMENT_KERNEL_GENERAL",
Segment::TxnData => "SEGMENT_TXN_DATA",
Segment::RlpRaw => "SEGMENT_RLP_RAW",
Segment::RlpParsed => "SEGMENT_RLP_PARSED",
}
}
}