Skip to content
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
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ alloy-dyn-abi = "0.8"
alloy-primitives = "0.8"
ariadne = { version = "0.4.1", features = ["auto-color"] }
clap = { version = "4.5.20", features = ["derive"] }
evm-glue = { git = "https://github.com/Philogy/evm-glue.git", rev = "d0186623" }
evm-glue = { git = "https://github.com/Philogy/evm-glue.git", rev = "6be3e8c8" }
chumsky = { git = "https://github.com/zesterer/chumsky.git", rev = "716bec8" }

[profile.profiling]
Expand Down
4 changes: 2 additions & 2 deletions crates/analysis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub fn analyze_entry_point<'src, 'ast: 'src, E: FnMut(AnalysisError<'ast, 'src>)
return;
};

if entry_point.args.0.len() != 0 {
if !entry_point.args.0.is_empty() {
emit_error(AnalysisError::EntryPointHasArgs {
target: entry_point,
});
Expand Down Expand Up @@ -274,7 +274,7 @@ impl<'a, 'src, 'ast: 'src, E: FnMut(AnalysisError<'ast, 'src>)> MacroAnalysis<'a
}
if self.global_defs.get(code_ref.ident()).is_some_and(|defs| {
defs.iter()
.any(|def| matches!(def, Definition::Macro(m) if m.args.0.len() > 0))
.any(|def| matches!(def, Definition::Macro(m) if !m.args.0.is_empty()))
}) {
self.emit(AnalysisError::NotYetSupported {
intent: "code introspection for macros with arguments".to_owned(),
Expand Down
70 changes: 52 additions & 18 deletions crates/compilation/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use alloy_primitives::U256;
use evm_glue::{assemble_maximized, assemble_minimized, utils::MarkTracker};
use evm_glue::{assemble_maximized, assemble_minimized, evm_asm, utils::MarkTracker};
use evm_glue::{
assembly::{Asm, MarkRef, RefType},
opcodes::Opcode,
Expand Down Expand Up @@ -78,24 +78,58 @@ pub fn generate_for_entrypoint<'src>(
globals.assemble(asm.as_slice())
}

/// WARNING: Only to be used as standalone constructor, may break if added after other code due to
/// reliance on `RETURNDATASIZE` being `0`.
pub fn generate_default_constructor(runtime: Vec<u8>) -> Box<[Asm]> {
let mut mtracker = MarkTracker::default();
let runtime_start = mtracker.next_mark();
let runtime_end = mtracker.next_mark();
Box::new([
// Constructor
Asm::delta_ref(runtime_start, runtime_end), // rt_size
Asm::Op(Opcode::DUP1), // rt_size, rt_size
Asm::mref(runtime_start), // rt_size, rt_size, rt_start
Asm::Op(Opcode::RETURNDATASIZE), // rt_size, rt_size, rt_start, 0
Asm::Op(Opcode::CODECOPY), // rt_size
Asm::Op(Opcode::RETURNDATASIZE), // rt_size, 0
Asm::Op(Opcode::RETURN), // -- end
// Runtime body
Asm::Mark(runtime_start),
Asm::Data(runtime),
Asm::Mark(runtime_end),
])
use Opcode::*;

match runtime.len() {
0 => Vec::new(),
1..=32 => {
let code_push = u256_as_push(U256::from_be_slice(runtime.as_slice()));
let len: u8 = runtime.len().try_into().unwrap();

if len == 32 {
evm_asm!(
Op(code_push),
RETURNDATASIZE,
MSTORE,
MSIZE,
RETURNDATASIZE,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice use of RETURNDATASIZE. Have you thought about what would happen if generate_default_constructor() is executed after a DELEGATECALL via a proxy? It's really the only scenario I can think of where RETURNDATASIZE could be zero.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The assumption is that it's going to be a standalone constructor but I'll add a comment 👍

RETURN
)
} else {
evm_asm!(
Op(code_push),
RETURNDATASIZE,
MSTORE,
PUSH1([len]),
PUSH1([32 - len]),
RETURN
)
}
}
_ => {
let mut mtracker = MarkTracker::default();
let runtime_start = mtracker.next_mark();
let runtime_end = mtracker.next_mark();
evm_asm!(
// Constructor
Asm::delta_ref(runtime_start, runtime_end), // rt_size
DUP1, // rt_size, rt_size
Asm::mref(runtime_start), // rt_size, rt_size, rt_start
RETURNDATASIZE, // rt_size, rt_size, rt_start, 0
CODECOPY, // rt_size
RETURNDATASIZE, // rt_size, 0
RETURN, // -- end
// Runtime body
Mark(runtime_start),
Data(runtime),
Mark(runtime_end),
)
}
}
.into_boxed_slice()
}

fn generate_for_macro<'src: 'cmp, 'cmp>(
Expand Down