Skip to content
This repository has been archived by the owner on Aug 16, 2024. It is now read-only.

feat: fixing far_call modifiers, adding support for uma static read & write #15

Merged
merged 2 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions src/assembly/instruction/far_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ impl FarCall {
}
}
}
if !modifiers.is_empty() {
return Err(InstructionReadError::UnknownArgument(format!(
"{:?}",
modifiers
)));
}

if result.is_none() {
// our default behavior
Expand Down
11 changes: 9 additions & 2 deletions src/assembly/instruction/uma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,20 @@ pub struct UMA {
}

impl UMA {
pub const ALL_CANONICAL_MODIFIERS: [&'static str; 5] = [
pub const ALL_CANONICAL_MODIFIERS: [&'static str; 7] = [
"heap_read",
"heap_write",
"aux_heap_read",
"aux_heap_write",
"fat_ptr_read",
"static_read",
"static_write",
];

pub const INCREMENT_OFFSET_MODIFIER: &'static str = "inc";

pub const ALL_SHORTHARD_MODIFIERS: [&'static str; 5] = ["rh", "wh", "rah", "wah", "rptr"];
pub const ALL_SHORTHARD_MODIFIERS: [&'static str; 7] =
["rh", "wh", "rah", "wah", "rptr", "rs", "ws"];

#[track_caller]
pub fn build_from_parts(
Expand Down Expand Up @@ -80,6 +83,8 @@ impl UMA {
2 => UMAOpcode::AuxHeapRead,
3 => UMAOpcode::AuxHeapWrite,
4 => UMAOpcode::FatPointerRead,
5 => UMAOpcode::StaticMemoryRead,
6 => UMAOpcode::StaticMemoryWrite,
_ => {
unreachable!()
}
Expand All @@ -106,6 +111,8 @@ impl UMA {
2 => UMAOpcode::AuxHeapRead,
3 => UMAOpcode::AuxHeapWrite,
4 => UMAOpcode::FatPointerRead,
5 => UMAOpcode::StaticMemoryRead,
6 => UMAOpcode::StaticMemoryWrite,
_ => {
unreachable!()
}
Expand Down
4 changes: 2 additions & 2 deletions src/assembly/mnemonic/binop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@ mod test {
#[test]
fn test_parse_mov() {
let example = "mov r2, r3";
let r = parse_mov_combinator(example).simplify();
let r = parse_mov_combinator(example).simplify().unwrap();
dbg!(r);
}

#[test]
fn test_parse_xor() {
let example = "xor.s.set_flags r2, r3, r0";
let r = parse_xor_combinator(example).simplify();
let r = parse_xor_combinator(example).simplify().unwrap();
dbg!(r);
}
}
2 changes: 1 addition & 1 deletion src/assembly/mnemonic/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ mod test {
#[test]
fn test_parse_gas_left() {
let example = "context.gas_left r1";
let r = parse_gas_left_combinator(example).simplify();
let r = parse_gas_left_combinator(example).simplify().unwrap();
dbg!(r);
}
}
2 changes: 1 addition & 1 deletion src/assembly/mnemonic/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ mod test {
#[test]
fn test_parse_event() {
let example = "event.first r6, r5";
let r = parse_event_combinator(example).simplify();
let r = parse_event_combinator(example).simplify().unwrap();
dbg!(r);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/assembly/mnemonic/set_flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ mod test {
#[test]
fn test_canonicalize_set_flags() {
let example = "add! r2, r3, r4";
let r = parse_set_flags_combinator(example).simplify();
let r = parse_set_flags_combinator(example).simplify().unwrap();
dbg!(r);
}

#[test]
fn test_canonicalize_set_flags2() {
let example = "sub.s! r2, r3, r4";
let r = parse_set_flags_combinator(example).simplify();
let r = parse_set_flags_combinator(example).simplify().unwrap();
dbg!(r);
}
}
2 changes: 1 addition & 1 deletion src/assembly/mnemonic/shift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ mod test {
#[test]
fn test_parse_rol() {
let example = "shl.s.set_flags r2, r3, r4";
let r = parse_shl_combinator(example).simplify();
let r = parse_shl_combinator(example).simplify().unwrap();
dbg!(r);
}
}
4 changes: 2 additions & 2 deletions src/assembly/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ mod test {
fn parse_full_assembly_line() {
let asm = "sub.s r1, r2, r3";

let res = try_parse_opcode_and_modifiers(asm);
let res = try_parse_opcode_and_modifiers(asm).unwrap();
dbg!(res);
}

Expand Down Expand Up @@ -417,7 +417,7 @@ __eh:
fn test_parse_tmp() {
let mut assembly = Assembly::try_from(TMP.to_owned()).unwrap();
let _ = assembly.compile_to_bytecode().unwrap();
let instructions = assembly.opcodes::<8, EncodingModeProduction>();
let instructions = assembly.opcodes::<8, EncodingModeProduction>().unwrap();
dbg!(&instructions);
}
}
70 changes: 70 additions & 0 deletions src/assembly/parse/addressing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,8 @@ fn parse_immediate_value<'a>(input: &'a str) -> IResult<&str, u64> {

#[cfg(test)]
mod test {
use zkevm_opcode_defs::Opcode;

use super::*;

#[test]
Expand Down Expand Up @@ -666,6 +668,18 @@ mod test {
dbg!(operand);
}

#[test]
fn test_far_call_with_invalid_modifiers() {
use crate::assembly::parse::code_element::parse_code_element;

let error =
parse_code_element("far_call.foobar r2, r3, @.BB5_2").expect_err("Should have failed");
assert!(
matches!(error, InstructionReadError::UnknownArgument(_)),
"Expected Uknown argument error"
);
}

#[test]
fn test_uma_imm() {
use crate::assembly::parse::code_element::parse_code_element;
Expand All @@ -690,5 +704,61 @@ mod test {
dbg!(&operand);
let opcode: DecodedOpcode<8, EncodingModeProduction> = operand.try_into().unwrap();
dbg!(&opcode);

assert!(
matches!(
opcode.variant.opcode,
Opcode::UMA(zkevm_opcode_defs::UMAOpcode::HeapWrite)
),
"wrong decode"
);
}

#[test]
fn test_uma_variants() {
use crate::assembly::parse::code_element::parse_code_element;

let operand = parse_code_element("uma.static_read 123, r0, r1, r0").unwrap();
let opcode: DecodedOpcode<8, EncodingModeProduction> = operand.try_into().unwrap();

assert!(
matches!(
opcode.variant.opcode,
Opcode::UMA(zkevm_opcode_defs::UMAOpcode::StaticMemoryRead)
),
"wrong decode"
);

let operand = parse_code_element("uma.rs 123, r0, r1, r0").unwrap();
let opcode: DecodedOpcode<8, EncodingModeProduction> = operand.try_into().unwrap();

assert!(
matches!(
opcode.variant.opcode,
Opcode::UMA(zkevm_opcode_defs::UMAOpcode::StaticMemoryRead)
),
"wrong decode"
);

let operand = parse_code_element("uma.static_write r2, r0, r1, r0").unwrap();
let opcode: DecodedOpcode<8, EncodingModeProduction> = operand.try_into().unwrap();
assert!(
matches!(
opcode.variant.opcode,
Opcode::UMA(zkevm_opcode_defs::UMAOpcode::StaticMemoryWrite)
),
"wrong decode"
);

let operand = parse_code_element("uma.ws 123, r0, r1, r0").unwrap();
let opcode: DecodedOpcode<8, EncodingModeProduction> = operand.try_into().unwrap();

assert!(
matches!(
opcode.variant.opcode,
Opcode::UMA(zkevm_opcode_defs::UMAOpcode::StaticMemoryWrite)
),
"wrong decode"
);
}
}
Loading