Skip to content

Commit 9c572ae

Browse files
authored
Fix error where an opcode could not be handled as second argument in a macro call (Fixes: #50) (#60)
1 parent 0ff3b08 commit 9c572ae

File tree

5 files changed

+53
-0
lines changed

5 files changed

+53
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
## [Unreleased]
66
- Throw error if the argument count for a macro call is not equal to the macro definition (Fixes: #49).
7+
- Fix error where an opcode could not be handled as second argument in a macro call (Fixes: #50).
78

89
## [1.1.6] - 2025-03-29
910
- Throw an error if an argument (e.g. `<arg>`) is used but not defined (Fixes: #46).

crates/codegen/src/irgen/arg_calls.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ pub fn bubble_arg_call(
3939
*offset += push_bytes.len() / 2;
4040
bytes.push((starting_offset, Bytes(push_bytes)));
4141
}
42+
MacroArg::Opcode(o) => {
43+
tracing::info!(target: "codegen", "GOT \"{:?}\" OPCODE FROM MACRO INVOCATION", o);
44+
let b = Bytes(o.to_string());
45+
*offset += b.0.len() / 2;
46+
bytes.push((starting_offset, b));
47+
}
4248
MacroArg::ArgCall(ac, arg_span) => {
4349
tracing::info!(target: "codegen", "GOT ARG CALL \"{}\" ARG FROM MACRO INVOCATION", ac);
4450
tracing::debug!(target: "codegen", "~~~ BUBBLING UP ARG CALL");

crates/core/tests/macro_invoc_args.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,3 +586,43 @@ fn test_unexpected_arguments() {
586586
assert!(codegen_result.is_err());
587587
assert_eq!(codegen_result.unwrap_err().kind, CodegenErrorKind::InvalidArgumentCount(String::from("TEST"), 1, 2));
588588
}
589+
590+
#[test]
591+
fn test_second_opcode_parameter() {
592+
let source: &str = r#"
593+
#define macro DO_NOTHING() = {
594+
}
595+
596+
#define macro ADD_OP(a, b) = {
597+
<a>
598+
<b>
599+
}
600+
601+
#define macro MAIN() = {
602+
ADD_OP(DO_NOTHING(), dup1)
603+
}
604+
"#;
605+
606+
// Parse tokens
607+
let flattened_source = FullFileSource { source, file: None, spans: vec![] };
608+
let lexer = Lexer::new(flattened_source);
609+
let tokens = lexer.into_iter().map(|x| x.unwrap()).collect::<Vec<Token>>();
610+
let mut parser = Parser::new(tokens, None);
611+
612+
// Parse the AST
613+
let mut contract = parser.parse().unwrap();
614+
615+
// Derive storage pointers
616+
contract.derive_storage_pointers();
617+
618+
// Instantiate Codegen
619+
let cg = Codegen::new();
620+
621+
// The codegen instance should have no artifact
622+
assert!(cg.artifact.is_none());
623+
624+
// Have Codegen create the runtime bytecode
625+
let r_bytes = Codegen::generate_main_bytecode(&EVMVersion::default(), &contract, None).unwrap();
626+
// 80 = DUP1
627+
assert_eq!(&r_bytes, "80");
628+
}

crates/parser/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,6 +1032,10 @@ impl Parser {
10321032
args.push(MacroArg::Literal(lit));
10331033
self.consume();
10341034
}
1035+
TokenKind::Opcode(o) => {
1036+
args.push(MacroArg::Opcode(o));
1037+
self.consume();
1038+
}
10351039
TokenKind::Ident(ident) => {
10361040
if self.peek().unwrap().kind == TokenKind::OpenParen {
10371041
// It's a nested macro call

crates/utils/src/ast/huff.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,8 @@ pub enum MacroArg {
475475
ArgCall(String, AstSpan),
476476
/// A Nested Macro Call
477477
MacroCall(MacroInvocation),
478+
/// Opcode Argument
479+
Opcode(Opcode),
478480
}
479481

480482
/// Free Storage Pointer Unit Struct

0 commit comments

Comments
 (0)