diff --git a/crates/differential/src/lib.rs b/crates/differential/src/lib.rs index 74e91f8..714c15c 100644 --- a/crates/differential/src/lib.rs +++ b/crates/differential/src/lib.rs @@ -125,8 +125,8 @@ pub struct PreparedEvm { pub fn prepare(code: Vec, data: Vec) -> PreparedEvm { let state = RuntimeState { context: Context { - address: H160::default(), - caller: H160::default(), + address: H160::from_slice(&[1u8; 20]), + caller: H160::from_slice(&[2u8; 20]), apparent_value: U256::default(), }, transaction_context: TransactionContext { diff --git a/crates/llvm-context/src/polkavm/const/runtime_api.rs b/crates/llvm-context/src/polkavm/const/runtime_api.rs index a098f12..2e8dd40 100644 --- a/crates/llvm-context/src/polkavm/const/runtime_api.rs +++ b/crates/llvm-context/src/polkavm/const/runtime_api.rs @@ -10,8 +10,12 @@ pub static DEPLOY: &str = "deploy"; /// Useful for configuring common attributes and linkage. pub static EXPORTS: [&str; 2] = [CALL, DEPLOY]; +pub static ADDRESS: &str = "address"; + pub static BLOCK_NUMBER: &str = "block_number"; +pub static CALLER: &str = "caller"; + pub static GET_STORAGE: &str = "get_storage"; pub static HASH_KECCAK_256: &str = "hash_keccak_256"; diff --git a/crates/llvm-context/src/polkavm/context/mod.rs b/crates/llvm-context/src/polkavm/context/mod.rs index 287538b..85205c2 100644 --- a/crates/llvm-context/src/polkavm/context/mod.rs +++ b/crates/llvm-context/src/polkavm/context/mod.rs @@ -621,9 +621,9 @@ where ) -> (Pointer<'ctx>, Pointer<'ctx>) { let buffer_pointer = self.build_alloca(self.integer_type(bit_length), name); let symbol = match bit_length { - 256 => GLOBAL_I256_SIZE, - 128 => GLOBAL_I128_SIZE, - 64 => GLOBAL_I64_SIZE, + revive_common::BIT_LENGTH_WORD => GLOBAL_I256_SIZE, + revive_common::BIT_LENGTH_VALUE => GLOBAL_I128_SIZE, + revive_common::BIT_LENGTH_BLOCK_NUMBER => GLOBAL_I64_SIZE, _ => unreachable!("invalid stack parameter bit width: {bit_length}"), }; let length_pointer = self.get_global(symbol).expect("should be declared"); diff --git a/crates/llvm-context/src/polkavm/evm/context.rs b/crates/llvm-context/src/polkavm/evm/context.rs index da42f90..1d50f43 100644 --- a/crates/llvm-context/src/polkavm/evm/context.rs +++ b/crates/llvm-context/src/polkavm/evm/context.rs @@ -163,3 +163,41 @@ where .build_int_z_extend(heap_size, context.word_type(), "heap_size_extended")? .as_basic_value_enum()) } + +/// Translates the `address` instruction. +pub fn address<'ctx, D>( + context: &mut Context<'ctx, D>, +) -> anyhow::Result> +where + D: Dependency + Clone, +{ + let (output_pointer, output_length_pointer) = + context.build_stack_parameter(revive_common::BIT_LENGTH_WORD, "address_output"); + context.build_runtime_call( + runtime_api::ADDRESS, + &[ + output_pointer.to_int(context).into(), + output_length_pointer.to_int(context).into(), + ], + ); + context.build_load(output_pointer, "address") +} + +/// Translates the `caller` instruction. +pub fn caller<'ctx, D>( + context: &mut Context<'ctx, D>, +) -> anyhow::Result> +where + D: Dependency + Clone, +{ + let (output_pointer, output_length_pointer) = + context.build_stack_parameter(revive_common::BIT_LENGTH_WORD, "caller_output"); + context.build_runtime_call( + runtime_api::CALLER, + &[ + output_pointer.to_int(context).into(), + output_length_pointer.to_int(context).into(), + ], + ); + context.build_load(output_pointer, "caller") +} diff --git a/crates/solidity/src/evmla/ethereal_ir/function/block/element/mod.rs b/crates/solidity/src/evmla/ethereal_ir/function/block/element/mod.rs index 15efc12..d5de06e 100644 --- a/crates/solidity/src/evmla/ethereal_ir/function/block/element/mod.rs +++ b/crates/solidity/src/evmla/ethereal_ir/function/block/element/mod.rs @@ -1134,8 +1134,12 @@ where .map(Some) } - InstructionName::ADDRESS => todo!(), - InstructionName::CALLER => todo!(), + InstructionName::ADDRESS => { + revive_llvm_context::polkavm_evm_contract_context::address(context).map(Some) + } + InstructionName::CALLER => { + revive_llvm_context::polkavm_evm_contract_context::caller(context).map(Some) + } InstructionName::CALLVALUE => { revive_llvm_context::polkavm_evm_ether_gas::value(context).map(Some) diff --git a/crates/solidity/src/yul/parser/statement/expression/function_call/mod.rs b/crates/solidity/src/yul/parser/statement/expression/function_call/mod.rs index 214dcea..1c51775 100644 --- a/crates/solidity/src/yul/parser/statement/expression/function_call/mod.rs +++ b/crates/solidity/src/yul/parser/statement/expression/function_call/mod.rs @@ -907,10 +907,12 @@ impl FunctionCall { Ok(Some(arguments[0])) } - Name::Address | Name::Caller => { - Ok(Some(context.integer_const(256, 0).as_basic_value_enum())) + Name::Address => { + revive_llvm_context::polkavm_evm_contract_context::address(context).map(Some) + } + Name::Caller => { + revive_llvm_context::polkavm_evm_contract_context::caller(context).map(Some) } - Name::CallValue => revive_llvm_context::polkavm_evm_ether_gas::value(context).map(Some), Name::Gas => revive_llvm_context::polkavm_evm_ether_gas::gas(context).map(Some), Name::Balance => {