Skip to content

Commit

Permalink
feat: re implement using a single IR function
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Jul 18, 2024
1 parent 84547c0 commit c3f686f
Show file tree
Hide file tree
Showing 15 changed files with 568 additions and 394 deletions.
1 change: 1 addition & 0 deletions crates/revmc-backend/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ pub trait Builder: BackendTypes + TypeMethods {
self.str_const(value.to_str().unwrap())
}
fn str_const(&mut self, value: &str) -> Self::Value;
fn nullptr(&mut self) -> Self::Value;

fn new_stack_slot(&mut self, ty: Self::Type, name: &str) -> Pointer<Self> {
Pointer::new_stack_slot(self, ty, name)
Expand Down
7 changes: 7 additions & 0 deletions crates/revmc-builtins/src/ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ macro_rules! builtins {
const LOG: u8 = LOG0;
const DORETURN: u8 = RETURN;
const RESIZEMEMORY: u8 = 0;
const FUNCSTACKPUSH: u8 = 0;
const FUNCSTACKPOP: u8 = 0;
const FUNCSTACKGROW: u8 = 0;

match self {
$(Self::$ident => [<$ident:upper>]),*
Expand Down Expand Up @@ -251,5 +254,9 @@ builtins! {
DoReturn = __revmc_builtin_do_return(@[ecx] ptr, @[sp] ptr, u8) Some(u8),
SelfDestruct = __revmc_builtin_selfdestruct(@[ecx] ptr, @[sp] ptr, u8) Some(u8),

FuncStackPush = __revmc_builtin_func_stack_push(@[ecx] ptr, ptr, usize) Some(u8),
FuncStackPop = __revmc_builtin_func_stack_pop(@[ecx] ptr) Some(ptr),
FuncStackGrow = __revmc_builtin_func_stack_grow(@[ecx] ptr) None,

ResizeMemory = __revmc_builtin_resize_memory(@[ecx] ptr, usize) Some(u8),
}
27 changes: 25 additions & 2 deletions crates/revmc-builtins/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ extern crate tracing;
use alloc::{boxed::Box, vec::Vec};
use revm_interpreter::{
as_u64_saturated, as_usize_saturated, CallInputs, CallScheme, CallValue, CreateInputs,
EOFCreateInputs, InstructionResult, InterpreterAction, InterpreterResult, LoadAccountResult,
SStoreResult,
EOFCreateInputs, FunctionStack, InstructionResult, InterpreterAction, InterpreterResult,
LoadAccountResult, SStoreResult,
};
use revm_primitives::{
eof::EofHeader, Address, Bytes, CreateScheme, Eof, Log, LogData, SpecId, KECCAK_EMPTY,
Expand Down Expand Up @@ -867,6 +867,29 @@ pub unsafe extern "C" fn __revmc_builtin_selfdestruct(
InstructionResult::Continue
}

#[no_mangle]
pub unsafe extern "C" fn __revmc_builtin_func_stack_push(
ecx: &mut EvmContext<'_>,
pc: usize,
new_idx: usize,
) -> InstructionResult {
if ecx.func_stack.return_stack_len() >= 1024 {
return InstructionResult::EOFFunctionStackOverflow;
}
ecx.func_stack.push(pc, new_idx);
InstructionResult::Continue
}

#[no_mangle]
pub unsafe extern "C" fn __revmc_builtin_func_stack_pop(ecx: &mut EvmContext<'_>) -> usize {
ecx.func_stack.pop().expect("RETF with empty return stack").pc
}

#[no_mangle]
pub unsafe extern "C" fn __revmc_builtin_func_stack_grow(func_stack: &mut FunctionStack) {
func_stack.return_stack.reserve(1);
}

#[no_mangle]
pub unsafe extern "C" fn __revmc_builtin_resize_memory(
ecx: &mut EvmContext<'_>,
Expand Down
2 changes: 1 addition & 1 deletion crates/revmc-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ fn main() -> Result<()> {
}

if cli.parse_only {
let _ = compiler.parse(bytecode, spec_id)?;
let _ = compiler.parse(bytecode.into(), spec_id)?;
return Ok(());
}

Expand Down
11 changes: 7 additions & 4 deletions crates/revmc-context/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ pub struct EvmContext<'a> {
pub next_action: &'a mut InterpreterAction,
/// The return data.
pub return_data: &'a [u8],
/// The length of the return stack.
pub return_stack_len: usize,
/// The function stack.
pub func_stack: &'a mut FunctionStack,
/// Whether the context is static.
pub is_static: bool,
/// Whether the context is EOF init.
Expand Down Expand Up @@ -76,7 +76,7 @@ impl<'a> EvmContext<'a> {
host,
next_action: &mut interpreter.next_action,
return_data: &interpreter.return_data_buffer,
return_stack_len: 0,
func_stack: &mut interpreter.function_stack,
is_static: interpreter.is_static,
is_eof_init: interpreter.is_eof_init,
resume_at,
Expand All @@ -91,7 +91,10 @@ impl<'a> EvmContext<'a> {
is_eof: self.contract.bytecode.is_eof(),
instruction_pointer: bytecode.as_ptr(),
bytecode,
function_stack: FunctionStack::new(),
function_stack: FunctionStack {
return_stack: self.func_stack.return_stack.clone(),
current_code_idx: self.func_stack.current_code_idx,
},
is_eof_init: self.is_eof_init,
contract: self.contract.clone(),
instruction_result: InstructionResult::Continue,
Expand Down
4 changes: 4 additions & 0 deletions crates/revmc-cranelift/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,10 @@ impl<'a> Builder for EvmCraneliftBuilder<'a> {
self.bcx.ins().global_value(self.ptr_type, local_msg_id)
}

fn nullptr(&mut self) -> Self::Value {
self.iconst(self.ptr_type, 0)
}

fn new_stack_slot_raw(&mut self, ty: Self::Type, name: &str) -> Self::StackSlot {
// https://github.com/rust-lang/rustc_codegen_cranelift/blob/1122338eb88648ec36a2eb2b1c27031fa897964d/src/common.rs#L388

Expand Down
4 changes: 4 additions & 0 deletions crates/revmc-llvm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,10 @@ impl<'a, 'ctx> Builder for EvmLlvmBuilder<'a, 'ctx> {
self.bcx.build_global_string_ptr(value, "").unwrap().as_pointer_value().into()
}

fn nullptr(&mut self) -> Self::Value {
self.ty_ptr.const_null().into()
}

fn new_stack_slot_raw(&mut self, ty: Self::Type, name: &str) -> Self::StackSlot {
// let ty = self.ty_i8.array_type(size);
// let ptr = self.bcx.build_alloca(ty, name).unwrap();
Expand Down
Loading

0 comments on commit c3f686f

Please sign in to comment.