mirror of
https://github.com/pezkuwichain/revive.git
synced 2026-04-30 21:07:57 +00:00
Merge remote-tracking branch 'origin/main' into resolc.js
This commit is contained in:
@@ -27,9 +27,11 @@ pub use self::polkavm::context::function::llvm_runtime::LLVMRuntime as PolkaVMLL
|
||||
pub use self::polkavm::context::function::r#return::Return as PolkaVMFunctionReturn;
|
||||
pub use self::polkavm::context::function::runtime::deploy_code::DeployCode as PolkaVMDeployCodeFunction;
|
||||
pub use self::polkavm::context::function::runtime::entry::Entry as PolkaVMEntryFunction;
|
||||
pub use self::polkavm::context::function::runtime::immutable_data_load::ImmutableDataLoad as PolkaVMImmutableDataLoadFunction;
|
||||
pub use self::polkavm::context::function::runtime::runtime_code::RuntimeCode as PolkaVMRuntimeCodeFunction;
|
||||
pub use self::polkavm::context::function::runtime::FUNCTION_DEPLOY_CODE as PolkaVMFunctionDeployCode;
|
||||
pub use self::polkavm::context::function::runtime::FUNCTION_ENTRY as PolkaVMFunctionEntry;
|
||||
pub use self::polkavm::context::function::runtime::FUNCTION_LOAD_IMMUTABLE_DATA as PolkaVMFunctionImmutableDataLoad;
|
||||
pub use self::polkavm::context::function::runtime::FUNCTION_RUNTIME_CODE as PolkaVMFunctionRuntimeCode;
|
||||
pub use self::polkavm::context::function::yul_data::YulData as PolkaVMFunctionYulData;
|
||||
pub use self::polkavm::context::function::Function as PolkaVMFunction;
|
||||
|
||||
@@ -21,8 +21,6 @@ pub struct Settings {
|
||||
|
||||
/// Fallback to optimizing for size if the bytecode is too large.
|
||||
pub is_fallback_to_size_enabled: bool,
|
||||
/// Whether the system request memoization is disabled.
|
||||
pub is_system_request_memoization_disabled: bool,
|
||||
|
||||
/// Whether the LLVM `verify each` option is enabled.
|
||||
pub is_verify_each_enabled: bool,
|
||||
@@ -43,7 +41,6 @@ impl Settings {
|
||||
level_back_end,
|
||||
|
||||
is_fallback_to_size_enabled: false,
|
||||
is_system_request_memoization_disabled: false,
|
||||
|
||||
is_verify_each_enabled: false,
|
||||
is_debug_logging_enabled: false,
|
||||
@@ -65,7 +62,6 @@ impl Settings {
|
||||
level_back_end,
|
||||
|
||||
is_fallback_to_size_enabled: false,
|
||||
is_system_request_memoization_disabled: false,
|
||||
|
||||
is_verify_each_enabled,
|
||||
is_debug_logging_enabled,
|
||||
@@ -206,20 +202,10 @@ impl Settings {
|
||||
self.is_fallback_to_size_enabled = true;
|
||||
}
|
||||
|
||||
/// Disables the system request memoization.
|
||||
pub fn disable_system_request_memoization(&mut self) {
|
||||
self.is_system_request_memoization_disabled = true;
|
||||
}
|
||||
|
||||
/// Whether the fallback to optimizing for size is enabled.
|
||||
pub fn is_fallback_to_size_enabled(&self) -> bool {
|
||||
self.is_fallback_to_size_enabled
|
||||
}
|
||||
|
||||
/// Whether the system request memoization is disabled.
|
||||
pub fn is_system_request_memoization_disabled(&self) -> bool {
|
||||
self.is_system_request_memoization_disabled
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for Settings {
|
||||
|
||||
@@ -1,14 +1,8 @@
|
||||
//! The LLVM context constants.
|
||||
|
||||
/// Runtime API methods.
|
||||
pub mod runtime_api;
|
||||
|
||||
/// The LLVM framework version.
|
||||
pub const LLVM_VERSION: semver::Version = semver::Version::new(18, 1, 4);
|
||||
|
||||
/// The PolkaVM version.
|
||||
pub const ZKEVM_VERSION: semver::Version = semver::Version::new(1, 3, 2);
|
||||
|
||||
/// The register width sized type
|
||||
pub static XLEN: usize = revive_common::BIT_LENGTH_X32;
|
||||
|
||||
@@ -21,55 +15,9 @@ pub static GLOBAL_CALLDATA_POINTER: &str = "ptr_calldata";
|
||||
/// The calldata size global variable name.
|
||||
pub static GLOBAL_CALLDATA_SIZE: &str = "calldatasize";
|
||||
|
||||
/// The return data pointer global variable name.
|
||||
pub static GLOBAL_RETURN_DATA_POINTER: &str = "ptr_return_data";
|
||||
|
||||
/// The return data size pointer global variable name.
|
||||
pub static GLOBAL_RETURN_DATA_SIZE: &str = "returndatasize";
|
||||
|
||||
/// The call flags global variable name.
|
||||
pub static GLOBAL_CALL_FLAGS: &str = "call_flags";
|
||||
|
||||
/// The extra ABI data global variable name.
|
||||
pub static GLOBAL_EXTRA_ABI_DATA: &str = "extra_abi_data";
|
||||
|
||||
/// The constant array global variable name prefix.
|
||||
pub static GLOBAL_CONST_ARRAY_PREFIX: &str = "const_array_";
|
||||
|
||||
/// The global verbatim getter identifier prefix.
|
||||
pub static GLOBAL_VERBATIM_GETTER_PREFIX: &str = "get_global::";
|
||||
|
||||
/// The static word size.
|
||||
pub static GLOBAL_I256_SIZE: &str = "i256_size";
|
||||
|
||||
/// The static value size.
|
||||
pub static GLOBAL_I160_SIZE: &str = "i160_size";
|
||||
|
||||
/// The static i64 size.
|
||||
pub static GLOBAL_I64_SIZE: &str = "i64_size";
|
||||
|
||||
/// The external call data offset in the auxiliary heap.
|
||||
pub const HEAP_AUX_OFFSET_EXTERNAL_CALL: u64 = 0;
|
||||
|
||||
/// The constructor return data offset in the auxiliary heap.
|
||||
pub const HEAP_AUX_OFFSET_CONSTRUCTOR_RETURN_DATA: u64 =
|
||||
8 * (revive_common::BYTE_LENGTH_WORD as u64);
|
||||
|
||||
/// The number of the extra ABI data arguments.
|
||||
pub const EXTRA_ABI_DATA_SIZE: usize = 0;
|
||||
|
||||
/// The `create` method deployer signature.
|
||||
pub static DEPLOYER_SIGNATURE_CREATE: &str = "create(bytes32,bytes32,bytes)";
|
||||
|
||||
/// The `create2` method deployer signature.
|
||||
pub static DEPLOYER_SIGNATURE_CREATE2: &str = "create2(bytes32,bytes32,bytes)";
|
||||
|
||||
/// The absence of system call bit.
|
||||
pub const NO_SYSTEM_CALL_BIT: bool = false;
|
||||
|
||||
/// The system call bit.
|
||||
pub const SYSTEM_CALL_BIT: bool = true;
|
||||
|
||||
/// The deployer call header size that consists of:
|
||||
/// - bytecode hash (32 bytes)
|
||||
pub const DEPLOYER_CALL_HEADER_SIZE: usize = revive_common::BYTE_LENGTH_WORD;
|
||||
|
||||
@@ -1,71 +0,0 @@
|
||||
//! Runtime API import and export symbols.
|
||||
|
||||
pub mod exports {
|
||||
/// The contract deploy export.
|
||||
pub static CALL: &str = "call";
|
||||
|
||||
/// The contract call export.
|
||||
pub static DEPLOY: &str = "deploy";
|
||||
|
||||
/// All exported symbols.
|
||||
/// Useful for configuring common attributes and linkage.
|
||||
pub static EXPORTS: [&str; 2] = [CALL, DEPLOY];
|
||||
}
|
||||
|
||||
pub mod imports {
|
||||
pub static ADDRESS: &str = "address";
|
||||
|
||||
pub static BALANCE: &str = "balance";
|
||||
|
||||
pub static BLOCK_NUMBER: &str = "block_number";
|
||||
|
||||
pub static CALL: &str = "seal_call";
|
||||
|
||||
pub static CALLER: &str = "caller";
|
||||
|
||||
pub static CODE_SIZE: &str = "code_size";
|
||||
|
||||
pub static DEPOSIT_EVENT: &str = "deposit_event";
|
||||
|
||||
pub static GET_STORAGE: &str = "get_storage";
|
||||
|
||||
pub static HASH_KECCAK_256: &str = "hash_keccak_256";
|
||||
|
||||
pub static INPUT: &str = "input";
|
||||
|
||||
pub static INSTANTIATE: &str = "instantiate";
|
||||
|
||||
pub static NOW: &str = "now";
|
||||
|
||||
pub static RETURN: &str = "seal_return";
|
||||
|
||||
pub static RETURNDATACOPY: &str = "returndatacopy";
|
||||
|
||||
pub static SET_STORAGE: &str = "set_storage";
|
||||
|
||||
pub static VALUE_TRANSFERRED: &str = "value_transferred";
|
||||
|
||||
/// All imported runtime API symbols.
|
||||
/// Useful for configuring common attributes and linkage.
|
||||
pub static IMPORTS: [&str; 16] = [
|
||||
ADDRESS,
|
||||
BALANCE,
|
||||
BLOCK_NUMBER,
|
||||
CALL,
|
||||
CALLER,
|
||||
CODE_SIZE,
|
||||
DEPOSIT_EVENT,
|
||||
GET_STORAGE,
|
||||
HASH_KECCAK_256,
|
||||
INPUT,
|
||||
INSTANTIATE,
|
||||
NOW,
|
||||
RETURN,
|
||||
RETURNDATACOPY,
|
||||
SET_STORAGE,
|
||||
VALUE_TRANSFERRED,
|
||||
];
|
||||
}
|
||||
|
||||
/// PolkaVM __sbrk API symbol to extend the heap memory.
|
||||
pub static SBRK: &str = "__sbrk";
|
||||
@@ -11,15 +11,6 @@ use crate::polkavm::context::function::Function;
|
||||
/// The functions are automatically linked to the LLVM implementations if the signatures match.
|
||||
#[derive(Debug)]
|
||||
pub struct LLVMRuntime<'ctx> {
|
||||
/// The corresponding LLVM runtime function.
|
||||
pub shl: FunctionDeclaration<'ctx>,
|
||||
/// The corresponding LLVM runtime function.
|
||||
pub shr: FunctionDeclaration<'ctx>,
|
||||
/// The corresponding LLVM runtime function.
|
||||
pub sar: FunctionDeclaration<'ctx>,
|
||||
/// The corresponding LLVM runtime function.
|
||||
pub byte: FunctionDeclaration<'ctx>,
|
||||
|
||||
/// The corresponding LLVM runtime function.
|
||||
pub add_mod: FunctionDeclaration<'ctx>,
|
||||
/// The corresponding LLVM runtime function.
|
||||
@@ -31,32 +22,9 @@ pub struct LLVMRuntime<'ctx> {
|
||||
|
||||
/// The corresponding LLVM runtime function.
|
||||
pub sha3: FunctionDeclaration<'ctx>,
|
||||
|
||||
/// The corresponding LLVM runtime function.
|
||||
pub r#return: FunctionDeclaration<'ctx>,
|
||||
/// The corresponding LLVM runtime function.
|
||||
pub revert: FunctionDeclaration<'ctx>,
|
||||
}
|
||||
|
||||
impl<'ctx> LLVMRuntime<'ctx> {
|
||||
/// The LLVM personality function name.
|
||||
pub const FUNCTION_PERSONALITY: &'static str = "__personality";
|
||||
|
||||
/// The LLVM exception throwing function name.
|
||||
pub const FUNCTION_CXA_THROW: &'static str = "__cxa_throw";
|
||||
|
||||
/// The corresponding runtime function name.
|
||||
pub const FUNCTION_SHL: &'static str = "__shl";
|
||||
|
||||
/// The corresponding runtime function name.
|
||||
pub const FUNCTION_SHR: &'static str = "__shr";
|
||||
|
||||
/// The corresponding runtime function name.
|
||||
pub const FUNCTION_SAR: &'static str = "__sar";
|
||||
|
||||
/// The corresponding runtime function name.
|
||||
pub const FUNCTION_BYTE: &'static str = "__byte";
|
||||
|
||||
/// The corresponding runtime function name.
|
||||
pub const FUNCTION_ADDMOD: &'static str = "__addmod";
|
||||
|
||||
@@ -72,121 +40,12 @@ impl<'ctx> LLVMRuntime<'ctx> {
|
||||
/// The corresponding runtime function name.
|
||||
pub const FUNCTION_SHA3: &'static str = "__sha3";
|
||||
|
||||
/// The corresponding runtime function name.
|
||||
pub const FUNCTION_SYSTEM_REQUEST: &'static str = "__system_request";
|
||||
|
||||
/// The corresponding runtime function name.
|
||||
pub const FUNCTION_FARCALL: &'static str = "__farcall";
|
||||
|
||||
/// The corresponding runtime function name.
|
||||
pub const FUNCTION_STATICCALL: &'static str = "__staticcall";
|
||||
|
||||
/// The corresponding runtime function name.
|
||||
pub const FUNCTION_DELEGATECALL: &'static str = "__delegatecall";
|
||||
|
||||
/// The corresponding runtime function name.
|
||||
pub const FUNCTION_MIMICCALL: &'static str = "__mimiccall";
|
||||
|
||||
/// The corresponding runtime function name.
|
||||
pub const FUNCTION_FARCALL_BYREF: &'static str = "__farcall_byref";
|
||||
|
||||
/// The corresponding runtime function name.
|
||||
pub const FUNCTION_STATICCALL_BYREF: &'static str = "__staticcall_byref";
|
||||
|
||||
/// The corresponding runtime function name.
|
||||
pub const FUNCTION_DELEGATECALL_BYREF: &'static str = "__delegatecall_byref";
|
||||
|
||||
/// The corresponding runtime function name.
|
||||
pub const FUNCTION_MIMICCALL_BYREF: &'static str = "__mimiccall_byref";
|
||||
|
||||
/// The corresponding runtime function name.
|
||||
pub const FUNCTION_RETURN: &'static str = "__return";
|
||||
|
||||
/// The corresponding runtime function name.
|
||||
pub const FUNCTION_REVERT: &'static str = "__revert";
|
||||
|
||||
/// A shortcut constructor.
|
||||
pub fn new(
|
||||
llvm: &'ctx inkwell::context::Context,
|
||||
module: &inkwell::module::Module<'ctx>,
|
||||
optimizer: &Optimizer,
|
||||
) -> Self {
|
||||
let shl = Self::declare(
|
||||
module,
|
||||
Self::FUNCTION_SHL,
|
||||
llvm.custom_width_int_type(revive_common::BIT_LENGTH_WORD as u32)
|
||||
.fn_type(
|
||||
vec![
|
||||
llvm.custom_width_int_type(revive_common::BIT_LENGTH_WORD as u32)
|
||||
.as_basic_type_enum()
|
||||
.into();
|
||||
2
|
||||
]
|
||||
.as_slice(),
|
||||
false,
|
||||
),
|
||||
Some(inkwell::module::Linkage::External),
|
||||
);
|
||||
Function::set_default_attributes(llvm, shl, optimizer);
|
||||
Function::set_pure_function_attributes(llvm, shl);
|
||||
|
||||
let shr = Self::declare(
|
||||
module,
|
||||
Self::FUNCTION_SHR,
|
||||
llvm.custom_width_int_type(revive_common::BIT_LENGTH_WORD as u32)
|
||||
.fn_type(
|
||||
vec![
|
||||
llvm.custom_width_int_type(revive_common::BIT_LENGTH_WORD as u32)
|
||||
.as_basic_type_enum()
|
||||
.into();
|
||||
2
|
||||
]
|
||||
.as_slice(),
|
||||
false,
|
||||
),
|
||||
Some(inkwell::module::Linkage::External),
|
||||
);
|
||||
Function::set_default_attributes(llvm, shr, optimizer);
|
||||
Function::set_pure_function_attributes(llvm, shr);
|
||||
|
||||
let sar = Self::declare(
|
||||
module,
|
||||
Self::FUNCTION_SAR,
|
||||
llvm.custom_width_int_type(revive_common::BIT_LENGTH_WORD as u32)
|
||||
.fn_type(
|
||||
vec![
|
||||
llvm.custom_width_int_type(revive_common::BIT_LENGTH_WORD as u32)
|
||||
.as_basic_type_enum()
|
||||
.into();
|
||||
2
|
||||
]
|
||||
.as_slice(),
|
||||
false,
|
||||
),
|
||||
Some(inkwell::module::Linkage::External),
|
||||
);
|
||||
Function::set_default_attributes(llvm, sar, optimizer);
|
||||
Function::set_pure_function_attributes(llvm, sar);
|
||||
|
||||
let byte = Self::declare(
|
||||
module,
|
||||
Self::FUNCTION_BYTE,
|
||||
llvm.custom_width_int_type(revive_common::BIT_LENGTH_WORD as u32)
|
||||
.fn_type(
|
||||
vec![
|
||||
llvm.custom_width_int_type(revive_common::BIT_LENGTH_WORD as u32)
|
||||
.as_basic_type_enum()
|
||||
.into();
|
||||
2
|
||||
]
|
||||
.as_slice(),
|
||||
false,
|
||||
),
|
||||
Some(inkwell::module::Linkage::External),
|
||||
);
|
||||
Function::set_default_attributes(llvm, byte, optimizer);
|
||||
Function::set_pure_function_attributes(llvm, byte);
|
||||
|
||||
let add_mod =
|
||||
Self::define(module, Self::FUNCTION_ADDMOD).expect("should be declared in stdlib");
|
||||
Function::set_default_attributes(llvm, add_mod, optimizer);
|
||||
@@ -236,54 +95,13 @@ impl<'ctx> LLVMRuntime<'ctx> {
|
||||
false,
|
||||
);
|
||||
|
||||
let r#return = Self::declare(
|
||||
module,
|
||||
Self::FUNCTION_RETURN,
|
||||
llvm.void_type().fn_type(
|
||||
vec![
|
||||
llvm.custom_width_int_type(revive_common::BIT_LENGTH_WORD as u32)
|
||||
.as_basic_type_enum()
|
||||
.into();
|
||||
3
|
||||
]
|
||||
.as_slice(),
|
||||
false,
|
||||
),
|
||||
Some(inkwell::module::Linkage::External),
|
||||
);
|
||||
Function::set_default_attributes(llvm, r#return, optimizer);
|
||||
let revert = Self::declare(
|
||||
module,
|
||||
Self::FUNCTION_REVERT,
|
||||
llvm.void_type().fn_type(
|
||||
vec![
|
||||
llvm.custom_width_int_type(revive_common::BIT_LENGTH_WORD as u32)
|
||||
.as_basic_type_enum()
|
||||
.into();
|
||||
3
|
||||
]
|
||||
.as_slice(),
|
||||
false,
|
||||
),
|
||||
Some(inkwell::module::Linkage::External),
|
||||
);
|
||||
Function::set_default_attributes(llvm, revert, optimizer);
|
||||
|
||||
Self {
|
||||
shl,
|
||||
shr,
|
||||
sar,
|
||||
byte,
|
||||
|
||||
add_mod,
|
||||
mul_mod,
|
||||
exp,
|
||||
sign_extend,
|
||||
|
||||
sha3,
|
||||
|
||||
r#return,
|
||||
revert,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -48,12 +48,6 @@ pub struct Function<'ctx> {
|
||||
}
|
||||
|
||||
impl<'ctx> Function<'ctx> {
|
||||
/// The near call ABI function prefix.
|
||||
pub const ZKSYNC_NEAR_CALL_ABI_PREFIX: &'static str = "ZKSYNC_NEAR_CALL";
|
||||
|
||||
/// The near call ABI exception handler name.
|
||||
pub const ZKSYNC_NEAR_CALL_ABI_EXCEPTION_HANDLER: &'static str = "ZKSYNC_CATCH_NEAR_CALL";
|
||||
|
||||
/// The stack hashmap default capacity.
|
||||
const STACK_HASHMAP_INITIAL_CAPACITY: usize = 64;
|
||||
|
||||
@@ -91,13 +85,8 @@ impl<'ctx> Function<'ctx> {
|
||||
|| (name.starts_with("__")
|
||||
&& name != self::runtime::FUNCTION_ENTRY
|
||||
&& name != self::runtime::FUNCTION_DEPLOY_CODE
|
||||
&& name != self::runtime::FUNCTION_RUNTIME_CODE)
|
||||
}
|
||||
|
||||
/// Checks whether the function is related to the near call ABI.
|
||||
pub fn is_near_call_abi(name: &str) -> bool {
|
||||
name.starts_with(Self::ZKSYNC_NEAR_CALL_ABI_PREFIX)
|
||||
|| name == Self::ZKSYNC_NEAR_CALL_ABI_EXCEPTION_HANDLER
|
||||
&& name != self::runtime::FUNCTION_RUNTIME_CODE
|
||||
&& name != self::runtime::FUNCTION_LOAD_IMMUTABLE_DATA)
|
||||
}
|
||||
|
||||
/// Returns the LLVM function declaration.
|
||||
|
||||
@@ -42,8 +42,7 @@ where
|
||||
D: Dependency + Clone,
|
||||
{
|
||||
fn declare(&mut self, context: &mut Context<D>) -> anyhow::Result<()> {
|
||||
let function_type =
|
||||
context.function_type::<inkwell::types::BasicTypeEnum>(vec![], 0, false);
|
||||
let function_type = context.function_type::<inkwell::types::BasicTypeEnum>(vec![], 0);
|
||||
context.add_function(
|
||||
runtime::FUNCTION_DEPLOY_CODE,
|
||||
function_type,
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
//! The entry function.
|
||||
|
||||
use inkwell::types::BasicType;
|
||||
use inkwell::values::BasicValue;
|
||||
|
||||
use crate::polkavm::context::address_space::AddressSpace;
|
||||
use crate::polkavm::context::function::runtime;
|
||||
use crate::polkavm::context::Context;
|
||||
use crate::polkavm::r#const::*;
|
||||
use crate::polkavm::Dependency;
|
||||
use crate::polkavm::WriteLLVM;
|
||||
|
||||
@@ -20,14 +18,8 @@ impl Entry {
|
||||
/// The call flags argument index.
|
||||
pub const ARGUMENT_INDEX_CALL_FLAGS: usize = 0;
|
||||
|
||||
/// The number of mandatory arguments.
|
||||
pub const MANDATORY_ARGUMENTS_COUNT: usize = 2;
|
||||
|
||||
/// Reserve 1mb for calldata.
|
||||
pub const MAX_CALLDATA_SIZE: usize = 1024 * 1024;
|
||||
|
||||
/// Reserve 1mb for returndata.
|
||||
pub const MAX_RETURNDATA_SIZE: usize = 1024 * 1024;
|
||||
/// Reserve 1kb for calldata.
|
||||
pub const MAX_CALLDATA_SIZE: usize = 1024;
|
||||
|
||||
/// Initializes the global variables.
|
||||
/// The pointers are not initialized, because it's not possible to create a null pointer.
|
||||
@@ -43,14 +35,6 @@ impl Entry {
|
||||
calldata_type.get_undef(),
|
||||
);
|
||||
|
||||
let returndata_type = context.array_type(context.byte_type(), Self::MAX_RETURNDATA_SIZE);
|
||||
context.set_global(
|
||||
crate::polkavm::GLOBAL_RETURN_DATA_POINTER,
|
||||
returndata_type,
|
||||
AddressSpace::Stack,
|
||||
returndata_type.get_undef(),
|
||||
);
|
||||
|
||||
context.set_global(
|
||||
crate::polkavm::GLOBAL_HEAP_MEMORY_POINTER,
|
||||
context.llvm().ptr_type(AddressSpace::Heap.into()),
|
||||
@@ -61,7 +45,10 @@ impl Entry {
|
||||
context
|
||||
.get_global(crate::polkavm::GLOBAL_HEAP_MEMORY_POINTER)?
|
||||
.into(),
|
||||
context.build_sbrk(context.integer_const(crate::polkavm::XLEN, 0))?,
|
||||
context.build_sbrk(
|
||||
context.xlen_type().const_zero(),
|
||||
context.xlen_type().const_zero(),
|
||||
)?,
|
||||
)?;
|
||||
|
||||
context.set_global(
|
||||
@@ -70,12 +57,6 @@ impl Entry {
|
||||
AddressSpace::Stack,
|
||||
context.word_undef(),
|
||||
);
|
||||
context.set_global(
|
||||
crate::polkavm::GLOBAL_RETURN_DATA_SIZE,
|
||||
context.xlen_type(),
|
||||
AddressSpace::Stack,
|
||||
context.xlen_type().const_zero().as_basic_value_enum(),
|
||||
);
|
||||
|
||||
context.set_global(
|
||||
crate::polkavm::GLOBAL_CALL_FLAGS,
|
||||
@@ -84,44 +65,6 @@ impl Entry {
|
||||
context.word_const(0),
|
||||
);
|
||||
|
||||
let extra_abi_data_type = context.array_type(
|
||||
context.word_type().as_basic_type_enum(),
|
||||
crate::polkavm::EXTRA_ABI_DATA_SIZE,
|
||||
);
|
||||
context.set_global(
|
||||
crate::polkavm::GLOBAL_EXTRA_ABI_DATA,
|
||||
extra_abi_data_type,
|
||||
AddressSpace::Stack,
|
||||
extra_abi_data_type.const_zero(),
|
||||
);
|
||||
|
||||
context.set_global(
|
||||
crate::polkavm::GLOBAL_I256_SIZE,
|
||||
context.xlen_type(),
|
||||
AddressSpace::Stack,
|
||||
context.integer_const(
|
||||
crate::polkavm::XLEN,
|
||||
revive_common::BYTE_LENGTH_X64 as u64 * 4,
|
||||
),
|
||||
);
|
||||
|
||||
context.set_global(
|
||||
crate::polkavm::GLOBAL_I160_SIZE,
|
||||
context.xlen_type(),
|
||||
AddressSpace::Stack,
|
||||
context.integer_const(
|
||||
crate::polkavm::XLEN,
|
||||
revive_common::BYTE_LENGTH_X64 as u64 * 2 + revive_common::BYTE_LENGTH_X32 as u64,
|
||||
),
|
||||
);
|
||||
|
||||
context.set_global(
|
||||
crate::polkavm::GLOBAL_I64_SIZE,
|
||||
context.xlen_type(),
|
||||
AddressSpace::Stack,
|
||||
context.integer_const(crate::polkavm::XLEN, revive_common::BYTE_LENGTH_X64 as u64),
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -141,7 +84,7 @@ impl Entry {
|
||||
"input_pointer_casted",
|
||||
)?;
|
||||
|
||||
let length_pointer = context.build_alloca(context.xlen_type(), "len_ptr");
|
||||
let length_pointer = context.build_alloca_at_entry(context.xlen_type(), "len_ptr");
|
||||
let length_pointer_casted = context.builder.build_ptr_to_int(
|
||||
length_pointer.value,
|
||||
context.xlen_type(),
|
||||
@@ -153,7 +96,7 @@ impl Entry {
|
||||
context.integer_const(crate::polkavm::XLEN, Self::MAX_CALLDATA_SIZE as u64),
|
||||
)?;
|
||||
context.build_runtime_call(
|
||||
runtime_api::imports::INPUT,
|
||||
revive_runtime_api::polkavm_imports::INPUT,
|
||||
&[input_pointer_casted.into(), length_pointer_casted.into()],
|
||||
);
|
||||
|
||||
@@ -232,12 +175,25 @@ where
|
||||
{
|
||||
fn declare(&mut self, context: &mut Context<D>) -> anyhow::Result<()> {
|
||||
let entry_arguments = vec![context.bool_type().as_basic_type_enum()];
|
||||
let entry_function_type = context.function_type(entry_arguments, 0, false);
|
||||
context.add_function(runtime::FUNCTION_ENTRY, entry_function_type, 0, None)?;
|
||||
let entry_function_type = context.function_type(entry_arguments, 0);
|
||||
context.add_function(
|
||||
runtime::FUNCTION_ENTRY,
|
||||
entry_function_type,
|
||||
0,
|
||||
Some(inkwell::module::Linkage::External),
|
||||
)?;
|
||||
|
||||
for symbol in runtime_api::exports::EXPORTS {
|
||||
context.declare_extern_function(symbol)?;
|
||||
}
|
||||
context.declare_global(
|
||||
revive_runtime_api::immutable_data::GLOBAL_IMMUTABLE_DATA_POINTER,
|
||||
context.word_type().array_type(0),
|
||||
AddressSpace::Stack,
|
||||
);
|
||||
|
||||
context.declare_global(
|
||||
revive_runtime_api::immutable_data::GLOBAL_IMMUTABLE_DATA_SIZE,
|
||||
context.xlen_type(),
|
||||
AddressSpace::Stack,
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -258,26 +214,6 @@ where
|
||||
true,
|
||||
);
|
||||
|
||||
context.set_current_function(runtime_api::exports::DEPLOY)?;
|
||||
context.set_basic_block(context.current_function().borrow().entry_block());
|
||||
|
||||
assert!(context
|
||||
.build_call(entry, &[context.bool_const(true).into()], "entry_deploy")
|
||||
.is_none());
|
||||
|
||||
context.set_basic_block(context.current_function().borrow().return_block);
|
||||
context.build_unreachable();
|
||||
|
||||
context.set_current_function(runtime_api::exports::CALL)?;
|
||||
context.set_basic_block(context.current_function().borrow().entry_block());
|
||||
|
||||
assert!(context
|
||||
.build_call(entry, &[context.bool_const(false).into()], "entry_call")
|
||||
.is_none());
|
||||
|
||||
context.set_basic_block(context.current_function().borrow().return_block);
|
||||
context.build_unreachable();
|
||||
|
||||
context.set_current_function(runtime::FUNCTION_ENTRY)?;
|
||||
context.set_basic_block(context.current_function().borrow().entry_block());
|
||||
|
||||
|
||||
@@ -0,0 +1,116 @@
|
||||
//! The immutable data runtime function.
|
||||
|
||||
use crate::polkavm::context::address_space::AddressSpace;
|
||||
use crate::polkavm::context::function::runtime;
|
||||
use crate::polkavm::context::pointer::Pointer;
|
||||
use crate::polkavm::context::Context;
|
||||
use crate::polkavm::Dependency;
|
||||
use crate::polkavm::WriteLLVM;
|
||||
|
||||
/// A function for requesting the immutable data from the runtime.
|
||||
/// This is a special function that is only used by the front-end generated code.
|
||||
///
|
||||
/// The runtime API is called lazily and subsequent calls are no-ops.
|
||||
///
|
||||
/// The bytes written is asserted to match the expected length.
|
||||
/// This should never fail; the length is known.
|
||||
/// However, this is a one time assertion, hence worth it.
|
||||
#[derive(Debug)]
|
||||
pub struct ImmutableDataLoad;
|
||||
|
||||
impl<D> WriteLLVM<D> for ImmutableDataLoad
|
||||
where
|
||||
D: Dependency + Clone,
|
||||
{
|
||||
fn declare(&mut self, context: &mut Context<D>) -> anyhow::Result<()> {
|
||||
context.add_function(
|
||||
runtime::FUNCTION_LOAD_IMMUTABLE_DATA,
|
||||
context.void_type().fn_type(Default::default(), false),
|
||||
0,
|
||||
Some(inkwell::module::Linkage::Private),
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn into_llvm(self, context: &mut Context<D>) -> anyhow::Result<()> {
|
||||
context.set_current_function(runtime::FUNCTION_LOAD_IMMUTABLE_DATA)?;
|
||||
context.set_basic_block(context.current_function().borrow().entry_block());
|
||||
|
||||
let immutable_data_size_pointer = context
|
||||
.get_global(revive_runtime_api::immutable_data::GLOBAL_IMMUTABLE_DATA_SIZE)?
|
||||
.value
|
||||
.as_pointer_value();
|
||||
let immutable_data_size = context.build_load(
|
||||
Pointer::new(
|
||||
context.xlen_type(),
|
||||
AddressSpace::Stack,
|
||||
immutable_data_size_pointer,
|
||||
),
|
||||
"immutable_data_size_load",
|
||||
)?;
|
||||
|
||||
let load_immutable_data_block = context.append_basic_block("load_immutables_block");
|
||||
let return_block = context.current_function().borrow().return_block();
|
||||
let immutable_data_size_is_zero = context.builder().build_int_compare(
|
||||
inkwell::IntPredicate::EQ,
|
||||
context.xlen_type().const_zero(),
|
||||
immutable_data_size.into_int_value(),
|
||||
"immutable_data_size_is_zero",
|
||||
)?;
|
||||
context.build_conditional_branch(
|
||||
immutable_data_size_is_zero,
|
||||
return_block,
|
||||
load_immutable_data_block,
|
||||
)?;
|
||||
|
||||
context.set_basic_block(load_immutable_data_block);
|
||||
let output_pointer = context
|
||||
.get_global(revive_runtime_api::immutable_data::GLOBAL_IMMUTABLE_DATA_POINTER)?
|
||||
.value
|
||||
.as_pointer_value();
|
||||
context.build_runtime_call(
|
||||
revive_runtime_api::polkavm_imports::GET_IMMUTABLE_DATA,
|
||||
&[
|
||||
context
|
||||
.builder()
|
||||
.build_ptr_to_int(output_pointer, context.xlen_type(), "ptr_to_xlen")?
|
||||
.into(),
|
||||
context
|
||||
.builder()
|
||||
.build_ptr_to_int(
|
||||
immutable_data_size_pointer,
|
||||
context.xlen_type(),
|
||||
"ptr_to_xlen",
|
||||
)?
|
||||
.into(),
|
||||
],
|
||||
);
|
||||
let bytes_written = context.builder().build_load(
|
||||
context.xlen_type(),
|
||||
immutable_data_size_pointer,
|
||||
"bytes_written",
|
||||
)?;
|
||||
context.builder().build_store(
|
||||
immutable_data_size_pointer,
|
||||
context.xlen_type().const_zero(),
|
||||
)?;
|
||||
let overflow_block = context.append_basic_block("immutable_data_overflow");
|
||||
let is_overflow = context.builder().build_int_compare(
|
||||
inkwell::IntPredicate::UGT,
|
||||
immutable_data_size.into_int_value(),
|
||||
bytes_written.into_int_value(),
|
||||
"is_overflow",
|
||||
)?;
|
||||
context.build_conditional_branch(is_overflow, overflow_block, return_block)?;
|
||||
|
||||
context.set_basic_block(overflow_block);
|
||||
context.build_call(context.intrinsics().trap, &[], "invalid_trap");
|
||||
context.build_unreachable();
|
||||
|
||||
context.set_basic_block(return_block);
|
||||
context.build_return(None);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
pub mod deploy_code;
|
||||
pub mod entry;
|
||||
pub mod immutable_data_load;
|
||||
pub mod runtime_code;
|
||||
|
||||
/// The main entry function name.
|
||||
@@ -12,3 +13,6 @@ pub const FUNCTION_DEPLOY_CODE: &str = "__deploy";
|
||||
|
||||
/// The runtime code function name.
|
||||
pub const FUNCTION_RUNTIME_CODE: &str = "__runtime";
|
||||
|
||||
/// The immutable data load function name.
|
||||
pub const FUNCTION_LOAD_IMMUTABLE_DATA: &str = "__immutable_data_load";
|
||||
|
||||
@@ -42,8 +42,7 @@ where
|
||||
D: Dependency + Clone,
|
||||
{
|
||||
fn declare(&mut self, context: &mut Context<D>) -> anyhow::Result<()> {
|
||||
let function_type =
|
||||
context.function_type::<inkwell::types::BasicTypeEnum>(vec![], 0, false);
|
||||
let function_type = context.function_type::<inkwell::types::BasicTypeEnum>(vec![], 0);
|
||||
context.add_function(
|
||||
runtime::FUNCTION_RUNTIME_CODE,
|
||||
function_type,
|
||||
|
||||
@@ -51,4 +51,31 @@ impl<'ctx> Global<'ctx> {
|
||||
|
||||
global
|
||||
}
|
||||
|
||||
/// Construct an external global.
|
||||
pub fn declare<D, T>(
|
||||
context: &mut Context<'ctx, D>,
|
||||
r#type: T,
|
||||
address_space: AddressSpace,
|
||||
name: &str,
|
||||
) -> Self
|
||||
where
|
||||
D: PolkaVMDependency + Clone,
|
||||
T: BasicType<'ctx>,
|
||||
{
|
||||
let r#type = r#type.as_basic_type_enum();
|
||||
|
||||
let value = context
|
||||
.module()
|
||||
.add_global(r#type, Some(address_space.into()), name);
|
||||
let global = Self { r#type, value };
|
||||
|
||||
global.value.set_linkage(inkwell::module::Linkage::External);
|
||||
global
|
||||
.value
|
||||
.set_visibility(inkwell::GlobalVisibility::Default);
|
||||
global.value.set_externally_initialized(true);
|
||||
|
||||
global
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,7 +26,6 @@ use inkwell::values::BasicValue;
|
||||
|
||||
use crate::optimizer::settings::Settings as OptimizerSettings;
|
||||
use crate::optimizer::Optimizer;
|
||||
use crate::polkavm::r#const::*;
|
||||
use crate::polkavm::DebugConfig;
|
||||
use crate::polkavm::Dependency;
|
||||
use crate::target_machine::target::Target;
|
||||
@@ -124,29 +123,19 @@ where
|
||||
.expect("the stdlib module should be linkable");
|
||||
}
|
||||
|
||||
/// Link in the PolkaVM guest module, containing imported and exported functions,
|
||||
/// Link in the PolkaVM imports module, containing imported functions,
|
||||
/// and marking them as external (they need to be relocatable as too).
|
||||
fn link_polkavm_guest_module(
|
||||
fn link_polkavm_imports(
|
||||
llvm: &'ctx inkwell::context::Context,
|
||||
module: &inkwell::module::Module<'ctx>,
|
||||
) {
|
||||
module
|
||||
.link_in_module(
|
||||
pallet_contracts_pvm_llapi::polkavm_guest::module(llvm, "polkavm_guest").unwrap(),
|
||||
revive_runtime_api::polkavm_imports::module(llvm, "polkavm_imports").unwrap(),
|
||||
)
|
||||
.expect("the PolkaVM guest API module should be linkable");
|
||||
.expect("the PolkaVM imports module should be linkable");
|
||||
|
||||
for export in runtime_api::exports::EXPORTS {
|
||||
module
|
||||
.get_function(export)
|
||||
.expect("should be declared")
|
||||
.add_attribute(
|
||||
inkwell::attributes::AttributeLoc::Function,
|
||||
llvm.create_enum_attribute(Attribute::NoReturn as u32, 0),
|
||||
);
|
||||
}
|
||||
|
||||
for import in runtime_api::imports::IMPORTS {
|
||||
for import in revive_runtime_api::polkavm_imports::IMPORTS {
|
||||
module
|
||||
.get_function(import)
|
||||
.expect("should be declared")
|
||||
@@ -154,6 +143,36 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
fn link_polkavm_exports(&self, contract_path: &str) -> anyhow::Result<()> {
|
||||
let exports = revive_runtime_api::polkavm_exports::module(self.llvm(), "polkavm_exports")
|
||||
.map_err(|error| {
|
||||
anyhow::anyhow!(
|
||||
"The contract `{}` exports module loading error: {}",
|
||||
contract_path,
|
||||
error
|
||||
)
|
||||
})?;
|
||||
self.module.link_in_module(exports).map_err(|error| {
|
||||
anyhow::anyhow!(
|
||||
"The contract `{}` exports module linking error: {}",
|
||||
contract_path,
|
||||
error
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
fn link_immutable_data(&self, contract_path: &str) -> anyhow::Result<()> {
|
||||
let size = self.solidity().immutables_size() as u32;
|
||||
let exports = revive_runtime_api::immutable_data::module(self.llvm(), size);
|
||||
self.module.link_in_module(exports).map_err(|error| {
|
||||
anyhow::anyhow!(
|
||||
"The contract `{}` immutable data module linking error: {}",
|
||||
contract_path,
|
||||
error
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
/// Configure the PolkaVM minimum stack size.
|
||||
fn set_polkavm_stack_size(
|
||||
llvm: &'ctx inkwell::context::Context,
|
||||
@@ -161,7 +180,7 @@ where
|
||||
size: u32,
|
||||
) {
|
||||
module
|
||||
.link_in_module(pallet_contracts_pvm_llapi::polkavm_guest::min_stack_size(
|
||||
.link_in_module(revive_runtime_api::calling_convention::min_stack_size(
|
||||
llvm,
|
||||
"polkavm_stack_size",
|
||||
size,
|
||||
@@ -191,7 +210,7 @@ where
|
||||
debug_config: Option<DebugConfig>,
|
||||
) -> Self {
|
||||
Self::link_stdlib_module(llvm, &module);
|
||||
Self::link_polkavm_guest_module(llvm, &module);
|
||||
Self::link_polkavm_imports(llvm, &module);
|
||||
Self::set_polkavm_stack_size(llvm, &module, Self::POLKAVM_STACK_SIZE);
|
||||
Self::set_module_flags(llvm, &module);
|
||||
|
||||
@@ -230,6 +249,9 @@ where
|
||||
) -> anyhow::Result<Build> {
|
||||
let module_clone = self.module.clone();
|
||||
|
||||
self.link_polkavm_exports(contract_path)?;
|
||||
self.link_immutable_data(contract_path)?;
|
||||
|
||||
let target_machine = TargetMachine::new(Target::PVM, self.optimizer.settings())?;
|
||||
target_machine.set_target_data(self.module());
|
||||
|
||||
@@ -371,6 +393,15 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
/// Declare an external global.
|
||||
pub fn declare_global<T>(&mut self, name: &str, r#type: T, address_space: AddressSpace)
|
||||
where
|
||||
T: BasicType<'ctx> + Clone + Copy,
|
||||
{
|
||||
let global = Global::declare(self, r#type, address_space, name);
|
||||
self.globals.insert(name.to_owned(), global);
|
||||
}
|
||||
|
||||
/// Returns the LLVM intrinsics collection reference.
|
||||
pub fn intrinsics(&self) -> &Intrinsics<'ctx> {
|
||||
&self.intrinsics
|
||||
@@ -381,47 +412,14 @@ where
|
||||
&self.llvm_runtime
|
||||
}
|
||||
|
||||
/// Declare a function already existing in the module.
|
||||
pub fn declare_extern_function(
|
||||
&mut self,
|
||||
name: &str,
|
||||
) -> anyhow::Result<Rc<RefCell<Function<'ctx>>>> {
|
||||
let function = self.module().get_function(name).ok_or_else(|| {
|
||||
anyhow::anyhow!("Failed to activate an undeclared function `{}`", name)
|
||||
})?;
|
||||
|
||||
let basic_block = self.llvm.append_basic_block(function, name);
|
||||
let declaration = FunctionDeclaration::new(
|
||||
self.function_type::<inkwell::types::BasicTypeEnum>(vec![], 0, false),
|
||||
function,
|
||||
);
|
||||
let function = Function::new(
|
||||
name.to_owned(),
|
||||
declaration,
|
||||
FunctionReturn::None,
|
||||
basic_block,
|
||||
basic_block,
|
||||
);
|
||||
Function::set_default_attributes(self.llvm, function.declaration(), &self.optimizer);
|
||||
|
||||
let function = Rc::new(RefCell::new(function));
|
||||
self.functions.insert(name.to_string(), function.clone());
|
||||
|
||||
Ok(function)
|
||||
}
|
||||
|
||||
/// Appends a function to the current module.
|
||||
pub fn add_function(
|
||||
&mut self,
|
||||
name: &str,
|
||||
r#type: inkwell::types::FunctionType<'ctx>,
|
||||
return_values_length: usize,
|
||||
mut linkage: Option<inkwell::module::Linkage>,
|
||||
linkage: Option<inkwell::module::Linkage>,
|
||||
) -> anyhow::Result<Rc<RefCell<Function<'ctx>>>> {
|
||||
if Function::is_near_call_abi(name) && self.is_system_mode() {
|
||||
linkage = Some(inkwell::module::Linkage::External);
|
||||
}
|
||||
|
||||
let value = self.module().add_function(name, r#type, linkage);
|
||||
|
||||
let entry_block = self.llvm.append_basic_block(value, "entry");
|
||||
@@ -434,12 +432,6 @@ where
|
||||
let pointer = self.build_alloca(self.word_type(), "return_pointer");
|
||||
FunctionReturn::primitive(pointer)
|
||||
}
|
||||
size if name.starts_with(Function::ZKSYNC_NEAR_CALL_ABI_PREFIX) => {
|
||||
let first_argument = value.get_first_param().expect("Always exists");
|
||||
let r#type = self.structure_type(vec![self.word_type(); size].as_slice());
|
||||
let pointer = first_argument.into_pointer_value();
|
||||
FunctionReturn::compound(Pointer::new(r#type, AddressSpace::Stack, pointer), size)
|
||||
}
|
||||
size => {
|
||||
self.set_basic_block(entry_block);
|
||||
let pointer = self.build_alloca(
|
||||
@@ -460,10 +452,6 @@ where
|
||||
return_block,
|
||||
);
|
||||
Function::set_default_attributes(self.llvm, function.declaration(), &self.optimizer);
|
||||
if Function::is_near_call_abi(function.name()) && self.is_system_mode() {
|
||||
Function::set_exception_handler_attributes(self.llvm, function.declaration());
|
||||
}
|
||||
|
||||
let function = Rc::new(RefCell::new(function));
|
||||
self.functions.insert(name.to_string(), function.clone());
|
||||
|
||||
@@ -524,10 +512,6 @@ where
|
||||
manager,
|
||||
name,
|
||||
self.optimizer.settings().to_owned(),
|
||||
self.yul_data
|
||||
.as_ref()
|
||||
.map(|data| data.is_system_mode())
|
||||
.unwrap_or_default(),
|
||||
self.include_metadata_hash,
|
||||
self.debug_config.clone(),
|
||||
)
|
||||
@@ -585,59 +569,70 @@ where
|
||||
self.builder.get_insert_block().expect("Always exists")
|
||||
}
|
||||
|
||||
/// Builds a stack allocation instruction.
|
||||
/// Sets the alignment to 128 bits.
|
||||
/// Builds an aligned stack allocation at the function entry.
|
||||
pub fn build_alloca_at_entry<T: BasicType<'ctx> + Clone + Copy>(
|
||||
&self,
|
||||
r#type: T,
|
||||
name: &str,
|
||||
) -> Pointer<'ctx> {
|
||||
let current_block = self.basic_block();
|
||||
let entry_block = self.current_function().borrow().entry_block();
|
||||
|
||||
match entry_block.get_first_instruction() {
|
||||
Some(instruction) => self.builder().position_before(&instruction),
|
||||
None => self.builder().position_at_end(entry_block),
|
||||
}
|
||||
|
||||
let pointer = self.build_alloca(r#type, name);
|
||||
self.set_basic_block(current_block);
|
||||
pointer
|
||||
}
|
||||
|
||||
/// Builds an aligned stack allocation at the current position.
|
||||
/// Use this if [`build_alloca_at_entry`] might change program semantics.
|
||||
/// Otherwise, alloca should always be built at the function prelude!
|
||||
pub fn build_alloca<T: BasicType<'ctx> + Clone + Copy>(
|
||||
&self,
|
||||
r#type: T,
|
||||
name: &str,
|
||||
) -> Pointer<'ctx> {
|
||||
let pointer = self.builder.build_alloca(r#type, name).unwrap();
|
||||
self.basic_block()
|
||||
.get_last_instruction()
|
||||
.expect("Always exists")
|
||||
pointer
|
||||
.as_instruction()
|
||||
.unwrap()
|
||||
.set_alignment(revive_common::BYTE_LENGTH_STACK_ALIGN as u32)
|
||||
.expect("Alignment is valid");
|
||||
|
||||
Pointer::new(r#type, AddressSpace::Stack, pointer)
|
||||
}
|
||||
|
||||
/// Allocate an int of size `bit_length` on the stack.
|
||||
/// Returns the allocation pointer and the length pointer.
|
||||
///
|
||||
/// Useful helper for passing runtime API parameters on the stack.
|
||||
pub fn build_stack_parameter(
|
||||
/// Truncate `address` to the ethereum address length and store it as bytes on the stack.
|
||||
/// The stack allocation will be at the function entry. Returns the stack pointer.
|
||||
/// This helper should be used when passing address arguments to the runtime, ensuring correct size and endianness.
|
||||
pub fn build_address_argument_store(
|
||||
&self,
|
||||
bit_length: usize,
|
||||
name: &str,
|
||||
) -> (Pointer<'ctx>, Pointer<'ctx>) {
|
||||
let buffer_pointer = self.build_alloca(self.integer_type(bit_length), name);
|
||||
let symbol = match bit_length {
|
||||
revive_common::BIT_LENGTH_WORD => GLOBAL_I256_SIZE,
|
||||
revive_common::BIT_LENGTH_ETH_ADDRESS => GLOBAL_I160_SIZE,
|
||||
revive_common::BIT_LENGTH_BLOCK_NUMBER => GLOBAL_I64_SIZE,
|
||||
_ => panic!("invalid stack parameter bit width: {bit_length}"),
|
||||
};
|
||||
let length_pointer = self.get_global(symbol).expect("should be declared");
|
||||
(buffer_pointer, length_pointer.into())
|
||||
address: inkwell::values::IntValue<'ctx>,
|
||||
) -> anyhow::Result<Pointer<'ctx>> {
|
||||
let address_type = self.integer_type(revive_common::BIT_LENGTH_ETH_ADDRESS);
|
||||
let address_pointer = self.build_alloca_at_entry(address_type, "address_pointer");
|
||||
let address_truncated =
|
||||
self.builder()
|
||||
.build_int_truncate(address, address_type, "address_truncated")?;
|
||||
let address_swapped = self.build_byte_swap(address_truncated.into())?;
|
||||
self.build_store(address_pointer, address_swapped)?;
|
||||
Ok(address_pointer)
|
||||
}
|
||||
|
||||
/// Load the integer at given pointer and zero extend it to the VM word size.
|
||||
pub fn build_load_word(
|
||||
/// Load the address at given pointer and zero extend it to the VM word size.
|
||||
pub fn build_load_address(
|
||||
&self,
|
||||
pointer: Pointer<'ctx>,
|
||||
bit_length: usize,
|
||||
name: &str,
|
||||
) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>> {
|
||||
let value = self.build_load(
|
||||
pointer.cast(self.integer_type(bit_length)),
|
||||
&format!("load_{name}"),
|
||||
)?;
|
||||
let value_extended = self.builder().build_int_z_extend(
|
||||
value.into_int_value(),
|
||||
self.word_type(),
|
||||
&format!("zext_{name}"),
|
||||
)?;
|
||||
Ok(value_extended.as_basic_value_enum())
|
||||
let address = self.build_byte_swap(self.build_load(pointer, "address_pointer")?)?;
|
||||
Ok(self
|
||||
.builder()
|
||||
.build_int_z_extend(address.into_int_value(), self.word_type(), "address_zext")?
|
||||
.into())
|
||||
}
|
||||
|
||||
/// Builds a stack load instruction.
|
||||
@@ -674,31 +669,25 @@ where
|
||||
self.build_byte_swap(value)
|
||||
}
|
||||
AddressSpace::Storage | AddressSpace::TransientStorage => {
|
||||
let storage_key_value = self.builder().build_ptr_to_int(
|
||||
pointer.value,
|
||||
self.word_type(),
|
||||
"storage_ptr_to_int",
|
||||
)?;
|
||||
let storage_key_pointer = self.build_alloca(self.word_type(), "storage_key");
|
||||
let storage_key_pointer_casted = self.builder().build_ptr_to_int(
|
||||
storage_key_pointer.value,
|
||||
self.xlen_type(),
|
||||
"storage_key_pointer_casted",
|
||||
)?;
|
||||
self.builder()
|
||||
.build_store(storage_key_pointer.value, storage_key_value)?;
|
||||
let storage_value_pointer =
|
||||
self.build_alloca(self.word_type(), "storage_value_pointer");
|
||||
self.build_store(storage_value_pointer, self.word_const(0))?;
|
||||
|
||||
let (storage_value_pointer, storage_value_length_pointer) = self
|
||||
.build_stack_parameter(revive_common::BIT_LENGTH_WORD, "storage_value_pointer");
|
||||
let storage_value_length_pointer =
|
||||
self.build_alloca(self.xlen_type(), "storage_value_length_pointer");
|
||||
self.build_store(
|
||||
storage_value_length_pointer,
|
||||
self.word_const(revive_common::BIT_LENGTH_WORD as u64),
|
||||
)?;
|
||||
|
||||
let transient = pointer.address_space == AddressSpace::TransientStorage;
|
||||
|
||||
self.build_runtime_call(
|
||||
runtime_api::imports::GET_STORAGE,
|
||||
revive_runtime_api::polkavm_imports::GET_STORAGE,
|
||||
&[
|
||||
self.xlen_type().const_int(transient as u64, false).into(),
|
||||
storage_key_pointer_casted.into(),
|
||||
self.integer_const(crate::polkavm::XLEN, 32).into(),
|
||||
pointer.to_int(self).into(),
|
||||
self.xlen_type().const_all_ones().into(),
|
||||
storage_value_pointer.to_int(self).into(),
|
||||
storage_value_length_pointer.to_int(self).into(),
|
||||
],
|
||||
@@ -766,18 +755,6 @@ where
|
||||
self.word_type().as_basic_type_enum()
|
||||
);
|
||||
|
||||
let storage_key_value = self.builder().build_ptr_to_int(
|
||||
pointer.value,
|
||||
self.word_type(),
|
||||
"storage_ptr_to_int",
|
||||
)?;
|
||||
let storage_key_pointer = self.build_alloca(self.word_type(), "storage_key");
|
||||
let storage_key_pointer_casted = self.builder().build_ptr_to_int(
|
||||
storage_key_pointer.value,
|
||||
self.xlen_type(),
|
||||
"storage_key_pointer_casted",
|
||||
)?;
|
||||
|
||||
let storage_value_pointer = self.build_alloca(self.word_type(), "storage_value");
|
||||
let storage_value_pointer_casted = self.builder().build_ptr_to_int(
|
||||
storage_value_pointer.value,
|
||||
@@ -785,19 +762,17 @@ where
|
||||
"storage_value_pointer_casted",
|
||||
)?;
|
||||
|
||||
self.builder()
|
||||
.build_store(storage_key_pointer.value, storage_key_value)?;
|
||||
self.builder()
|
||||
.build_store(storage_value_pointer.value, value)?;
|
||||
|
||||
let transient = pointer.address_space == AddressSpace::TransientStorage;
|
||||
|
||||
self.build_runtime_call(
|
||||
runtime_api::imports::SET_STORAGE,
|
||||
revive_runtime_api::polkavm_imports::SET_STORAGE,
|
||||
&[
|
||||
self.xlen_type().const_int(transient as u64, false).into(),
|
||||
storage_key_pointer_casted.into(),
|
||||
self.integer_const(crate::polkavm::XLEN, 32).into(),
|
||||
pointer.to_int(self).into(),
|
||||
self.xlen_type().const_all_ones().into(),
|
||||
storage_value_pointer_casted.into(),
|
||||
self.integer_const(crate::polkavm::XLEN, 32).into(),
|
||||
],
|
||||
@@ -905,13 +880,29 @@ where
|
||||
.copied()
|
||||
.map(inkwell::values::BasicMetadataValueEnum::from)
|
||||
.collect::<Vec<_>>(),
|
||||
&format!("runtime API call {name}"),
|
||||
&format!("runtime_api_{name}_return_value"),
|
||||
)
|
||||
.unwrap()
|
||||
.try_as_basic_value()
|
||||
.left()
|
||||
}
|
||||
|
||||
/// Builds a call to the runtime API `import`, where `import` is a "getter" API.
|
||||
/// This means that the supplied API method just writes back a single word.
|
||||
/// `import` is thus expect to have a single parameter, the 32 bytes output buffer,
|
||||
/// and no return value.
|
||||
pub fn build_runtime_call_to_getter(
|
||||
&self,
|
||||
import: &'static str,
|
||||
) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>>
|
||||
where
|
||||
D: Dependency + Clone,
|
||||
{
|
||||
let pointer = self.build_alloca_at_entry(self.word_type(), &format!("{import}_output"));
|
||||
self.build_runtime_call(import, &[pointer.to_int(self).into()]);
|
||||
self.build_load(pointer, import)
|
||||
}
|
||||
|
||||
/// Builds a call.
|
||||
pub fn build_call(
|
||||
&self,
|
||||
@@ -1012,7 +1003,7 @@ where
|
||||
)?;
|
||||
|
||||
self.build_runtime_call(
|
||||
runtime_api::imports::RETURN,
|
||||
revive_runtime_api::polkavm_imports::RETURN,
|
||||
&[flags.into(), offset_pointer.into(), length_pointer.into()],
|
||||
);
|
||||
self.build_unreachable();
|
||||
@@ -1070,16 +1061,20 @@ where
|
||||
Ok(truncated)
|
||||
}
|
||||
|
||||
/// Build a call to PolkaVM `sbrk` for extending the heap by `size`.
|
||||
/// Build a call to PolkaVM `sbrk` for extending the heap from offset by `size`.
|
||||
/// The allocation is aligned to 32 bytes.
|
||||
///
|
||||
/// This emulates the EVM linear memory until the runtime supports metered memory.
|
||||
pub fn build_sbrk(
|
||||
&self,
|
||||
offset: inkwell::values::IntValue<'ctx>,
|
||||
size: inkwell::values::IntValue<'ctx>,
|
||||
) -> anyhow::Result<inkwell::values::PointerValue<'ctx>> {
|
||||
Ok(self
|
||||
.builder()
|
||||
.build_call(
|
||||
self.runtime_api_method(runtime_api::SBRK),
|
||||
&[size.into()],
|
||||
self.runtime_api_method(revive_runtime_api::polkavm_imports::SBRK),
|
||||
&[offset.into(), size.into()],
|
||||
"call_sbrk",
|
||||
)?
|
||||
.try_as_basic_value()
|
||||
@@ -1088,14 +1083,29 @@ where
|
||||
.into_pointer_value())
|
||||
}
|
||||
|
||||
/// Call PolkaVM `sbrk` for extending the heap by `size`,
|
||||
/// Build a call to PolkaVM `msize` for querying the linear memory size.
|
||||
pub fn build_msize(&self) -> anyhow::Result<inkwell::values::IntValue<'ctx>> {
|
||||
Ok(self
|
||||
.builder()
|
||||
.build_call(
|
||||
self.runtime_api_method(revive_runtime_api::polkavm_imports::MEMORY_SIZE),
|
||||
&[],
|
||||
"call_msize",
|
||||
)?
|
||||
.try_as_basic_value()
|
||||
.left()
|
||||
.expect("sbrk returns an int")
|
||||
.into_int_value())
|
||||
}
|
||||
|
||||
/// Call PolkaVM `sbrk` for extending the heap by `offset` + `size`,
|
||||
/// trapping the contract if the call failed.
|
||||
/// Returns the end of memory pointer.
|
||||
pub fn build_heap_alloc(
|
||||
&self,
|
||||
offset: inkwell::values::IntValue<'ctx>,
|
||||
size: inkwell::values::IntValue<'ctx>,
|
||||
) -> anyhow::Result<inkwell::values::PointerValue<'ctx>> {
|
||||
let end_of_memory = self.build_sbrk(size)?;
|
||||
) -> anyhow::Result<()> {
|
||||
let end_of_memory = self.build_sbrk(offset, size)?;
|
||||
let return_is_nil = self.builder().build_int_compare(
|
||||
inkwell::IntPredicate::EQ,
|
||||
end_of_memory,
|
||||
@@ -1113,7 +1123,7 @@ where
|
||||
|
||||
self.set_basic_block(continue_block);
|
||||
|
||||
Ok(end_of_memory)
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Returns a pointer to `offset` into the heap, allocating
|
||||
@@ -1128,40 +1138,12 @@ where
|
||||
assert_eq!(offset.get_type(), self.xlen_type());
|
||||
assert_eq!(length.get_type(), self.xlen_type());
|
||||
|
||||
self.build_heap_alloc(offset, length)?;
|
||||
|
||||
let heap_start = self
|
||||
.get_global(crate::polkavm::GLOBAL_HEAP_MEMORY_POINTER)?
|
||||
.value
|
||||
.as_pointer_value();
|
||||
let heap_end = self.build_sbrk(self.integer_const(crate::polkavm::XLEN, 0))?;
|
||||
let value_end = self.build_gep(
|
||||
Pointer::new(self.byte_type(), AddressSpace::Stack, heap_start),
|
||||
&[self.builder().build_int_nuw_add(offset, length, "end")?],
|
||||
self.byte_type(),
|
||||
"heap_end_gep",
|
||||
);
|
||||
let is_out_of_bounds = self.builder().build_int_compare(
|
||||
inkwell::IntPredicate::UGT,
|
||||
value_end.value,
|
||||
heap_end,
|
||||
"is_value_overflowing_heap",
|
||||
)?;
|
||||
|
||||
let out_of_bounds_block = self.append_basic_block("heap_offset_out_of_bounds");
|
||||
let heap_offset_block = self.append_basic_block("build_heap_pointer");
|
||||
self.build_conditional_branch(is_out_of_bounds, out_of_bounds_block, heap_offset_block)?;
|
||||
|
||||
self.set_basic_block(out_of_bounds_block);
|
||||
let size = self.builder().build_int_nuw_sub(
|
||||
self.builder()
|
||||
.build_ptr_to_int(value_end.value, self.xlen_type(), "value_end")?,
|
||||
self.builder()
|
||||
.build_ptr_to_int(heap_end, self.xlen_type(), "heap_end")?,
|
||||
"heap_alloc_size",
|
||||
)?;
|
||||
self.build_heap_alloc(size)?;
|
||||
self.build_unconditional_branch(heap_offset_block);
|
||||
|
||||
self.set_basic_block(heap_offset_block);
|
||||
Ok(self.build_gep(
|
||||
Pointer::new(self.byte_type(), AddressSpace::Stack, heap_start),
|
||||
&[offset],
|
||||
@@ -1233,7 +1215,7 @@ where
|
||||
self.llvm.custom_width_int_type(crate::polkavm::XLEN as u32)
|
||||
}
|
||||
|
||||
/// Returns the register witdh sized type.
|
||||
/// Returns the sentinel pointer value.
|
||||
pub fn sentinel_pointer(&self) -> Pointer<'ctx> {
|
||||
let sentinel_pointer = self
|
||||
.xlen_type()
|
||||
@@ -1282,12 +1264,11 @@ where
|
||||
&self,
|
||||
argument_types: Vec<T>,
|
||||
return_values_size: usize,
|
||||
is_near_call_abi: bool,
|
||||
) -> inkwell::types::FunctionType<'ctx>
|
||||
where
|
||||
T: BasicType<'ctx>,
|
||||
{
|
||||
let mut argument_types: Vec<inkwell::types::BasicMetadataTypeEnum> = argument_types
|
||||
let argument_types: Vec<inkwell::types::BasicMetadataTypeEnum> = argument_types
|
||||
.as_slice()
|
||||
.iter()
|
||||
.map(T::as_basic_type_enum)
|
||||
@@ -1299,11 +1280,6 @@ where
|
||||
.void_type()
|
||||
.fn_type(argument_types.as_slice(), false),
|
||||
1 => self.word_type().fn_type(argument_types.as_slice(), false),
|
||||
_size if is_near_call_abi && self.is_system_mode() => {
|
||||
let return_type = self.llvm().ptr_type(AddressSpace::Stack.into());
|
||||
argument_types.insert(0, return_type.as_basic_type_enum().into());
|
||||
return_type.fn_type(argument_types.as_slice(), false)
|
||||
}
|
||||
size => self
|
||||
.structure_type(vec![self.word_type().as_basic_type_enum(); size].as_slice())
|
||||
.fn_type(argument_types.as_slice(), false),
|
||||
@@ -1496,12 +1472,4 @@ where
|
||||
anyhow::bail!("The immutable size data is not available");
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether the system mode is enabled.
|
||||
pub fn is_system_mode(&self) -> bool {
|
||||
self.yul_data
|
||||
.as_ref()
|
||||
.map(|data| data.is_system_mode())
|
||||
.unwrap_or_default()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ impl SolidityData {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
/// Returns the current number of immutables values in the contract.
|
||||
/// Returns the current size of immutable values in the contract.
|
||||
pub fn immutables_size(&self) -> usize {
|
||||
self.immutables.len() * revive_common::BYTE_LENGTH_WORD
|
||||
}
|
||||
|
||||
@@ -8,28 +8,12 @@ use num::Zero;
|
||||
/// Describes some data that is only relevant to Yul.
|
||||
#[derive(Debug, Default)]
|
||||
pub struct YulData {
|
||||
/// The system mode flag.
|
||||
/// The call simulations only work if this mode is enabled.
|
||||
is_system_mode: bool,
|
||||
/// The list of constant arrays in the code section.
|
||||
/// It is a temporary storage used until the finalization method is called.
|
||||
const_arrays: BTreeMap<u8, Vec<num::BigUint>>,
|
||||
}
|
||||
|
||||
impl YulData {
|
||||
/// A shortcut constructor.
|
||||
pub fn new(is_system_mode: bool) -> Self {
|
||||
Self {
|
||||
is_system_mode,
|
||||
const_arrays: BTreeMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether the system mode is enabled.
|
||||
pub fn is_system_mode(&self) -> bool {
|
||||
self.is_system_mode
|
||||
}
|
||||
|
||||
/// Declares a temporary constant array representation.
|
||||
pub fn const_array_declare(&mut self, index: u8, size: u16) -> anyhow::Result<()> {
|
||||
if self.const_arrays.contains_key(&index) {
|
||||
|
||||
@@ -5,18 +5,15 @@ use inkwell::values::BasicValue;
|
||||
use crate::polkavm::context::argument::Argument;
|
||||
use crate::polkavm::context::Context;
|
||||
use crate::polkavm::Dependency;
|
||||
use crate::polkavm_const::runtime_api;
|
||||
|
||||
static STATIC_CALL_FLAG: u32 = 0b0001_0000;
|
||||
const STATIC_CALL_FLAG: u32 = 0b0001_0000;
|
||||
const REENTRANT_CALL_FLAG: u32 = 0b0000_1000;
|
||||
|
||||
/// Translates a contract call.
|
||||
///
|
||||
/// If the `simulation_address` is specified, the call is
|
||||
/// substituted with another instruction according to the specification.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn call<'ctx, D>(
|
||||
context: &mut Context<'ctx, D>,
|
||||
gas: inkwell::values::IntValue<'ctx>,
|
||||
_gas: inkwell::values::IntValue<'ctx>,
|
||||
address: inkwell::values::IntValue<'ctx>,
|
||||
value: Option<inkwell::values::IntValue<'ctx>>,
|
||||
input_offset: inkwell::values::IntValue<'ctx>,
|
||||
@@ -29,59 +26,64 @@ pub fn call<'ctx, D>(
|
||||
where
|
||||
D: Dependency + Clone,
|
||||
{
|
||||
let address_pointer = context.build_alloca(context.word_type(), "address_ptr");
|
||||
context.build_store(address_pointer, address)?;
|
||||
let address_pointer = context.build_address_argument_store(address)?;
|
||||
|
||||
let value_pointer = if let Some(value) = value {
|
||||
let value_pointer = context.build_alloca(context.value_type(), "value");
|
||||
context.build_store(value_pointer, value)?;
|
||||
value_pointer
|
||||
} else {
|
||||
context.sentinel_pointer()
|
||||
};
|
||||
let value = value.unwrap_or_else(|| context.word_const(0));
|
||||
let value_pointer = context.build_alloca_at_entry(context.word_type(), "value_pointer");
|
||||
context.build_store(value_pointer, value)?;
|
||||
|
||||
let input_offset = context.safe_truncate_int_to_xlen(input_offset)?;
|
||||
let input_length = context.safe_truncate_int_to_xlen(input_length)?;
|
||||
let output_offset = context.safe_truncate_int_to_xlen(output_offset)?;
|
||||
let output_length = context.safe_truncate_int_to_xlen(output_length)?;
|
||||
|
||||
let gas = context
|
||||
// TODO: What to supply here? Is there a weight to gas?
|
||||
let _gas = context
|
||||
.builder()
|
||||
.build_int_truncate(gas, context.integer_type(64), "gas")?;
|
||||
|
||||
let flags = if static_call { STATIC_CALL_FLAG } else { 0 };
|
||||
.build_int_truncate(_gas, context.integer_type(64), "gas")?;
|
||||
|
||||
let input_pointer = context.build_heap_gep(input_offset, input_length)?;
|
||||
let output_pointer = context.build_heap_gep(output_offset, output_length)?;
|
||||
|
||||
let output_length_pointer = context.get_global(crate::polkavm::GLOBAL_RETURN_DATA_SIZE)?;
|
||||
context.build_store(output_length_pointer.into(), output_length)?;
|
||||
let output_length_pointer = context.build_alloca_at_entry(context.xlen_type(), "output_length");
|
||||
context.build_store(output_length_pointer, output_length)?;
|
||||
|
||||
let argument_pointer = pallet_contracts_pvm_llapi::calling_convention::Spill::new(
|
||||
let flags = if static_call {
|
||||
REENTRANT_CALL_FLAG | STATIC_CALL_FLAG
|
||||
} else {
|
||||
REENTRANT_CALL_FLAG
|
||||
};
|
||||
let flags = context.xlen_type().const_int(flags as u64, false);
|
||||
|
||||
let argument_type = revive_runtime_api::calling_convention::call(context.llvm());
|
||||
let argument_pointer = context.build_alloca_at_entry(argument_type, "call_arguments");
|
||||
let arguments = &[
|
||||
flags.as_basic_value_enum(),
|
||||
address_pointer.value.as_basic_value_enum(),
|
||||
context.integer_const(64, 0).as_basic_value_enum(),
|
||||
context.integer_const(64, 0).as_basic_value_enum(),
|
||||
context.sentinel_pointer().value.as_basic_value_enum(),
|
||||
value_pointer.value.as_basic_value_enum(),
|
||||
input_pointer.value.as_basic_value_enum(),
|
||||
input_length.as_basic_value_enum(),
|
||||
output_pointer.value.as_basic_value_enum(),
|
||||
output_length_pointer.value.as_basic_value_enum(),
|
||||
];
|
||||
revive_runtime_api::calling_convention::spill(
|
||||
context.builder(),
|
||||
pallet_contracts_pvm_llapi::calling_convention::call(context.llvm()),
|
||||
"call_arguments",
|
||||
)?
|
||||
.next(context.xlen_type().const_int(flags as u64, false))?
|
||||
.next(address_pointer.value)?
|
||||
.next(gas)?
|
||||
.skip()
|
||||
.next(context.sentinel_pointer().value)?
|
||||
.next(value_pointer.value)?
|
||||
.next(input_pointer.value)?
|
||||
.next(input_length)?
|
||||
.next(output_pointer.value)?
|
||||
.next(output_length_pointer.value)?
|
||||
.done();
|
||||
argument_pointer.value,
|
||||
argument_type,
|
||||
arguments,
|
||||
)?;
|
||||
|
||||
let name = runtime_api::imports::CALL;
|
||||
let arguments = context.builder().build_ptr_to_int(
|
||||
argument_pointer,
|
||||
let name = revive_runtime_api::polkavm_imports::CALL;
|
||||
let argument_pointer = context.builder().build_ptr_to_int(
|
||||
argument_pointer.value,
|
||||
context.xlen_type(),
|
||||
"argument_pointer",
|
||||
"call_argument_pointer",
|
||||
)?;
|
||||
let success = context
|
||||
.build_runtime_call(name, &[arguments.into()])
|
||||
.build_runtime_call(name, &[argument_pointer.into()])
|
||||
.unwrap_or_else(|| panic!("{name} should return a value"))
|
||||
.into_int_value();
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@ use inkwell::values::BasicValue;
|
||||
|
||||
use crate::polkavm::context::Context;
|
||||
use crate::polkavm::Dependency;
|
||||
use crate::polkavm_const::runtime_api;
|
||||
|
||||
/// Translates the `gas_limit` instruction.
|
||||
pub fn gas_limit<'ctx, D>(
|
||||
@@ -28,22 +27,29 @@ where
|
||||
|
||||
/// Translates the `tx.origin` instruction.
|
||||
pub fn origin<'ctx, D>(
|
||||
_context: &mut Context<'ctx, D>,
|
||||
context: &mut Context<'ctx, D>,
|
||||
) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>>
|
||||
where
|
||||
D: Dependency + Clone,
|
||||
{
|
||||
todo!()
|
||||
let address_type = context.integer_type(revive_common::BIT_LENGTH_ETH_ADDRESS);
|
||||
let address_pointer = context.build_alloca_at_entry(address_type, "origin_address");
|
||||
context.build_store(address_pointer, address_type.const_zero())?;
|
||||
context.build_runtime_call(
|
||||
revive_runtime_api::polkavm_imports::ORIGIN,
|
||||
&[address_pointer.to_int(context).into()],
|
||||
);
|
||||
context.build_load_address(address_pointer)
|
||||
}
|
||||
|
||||
/// Translates the `chain_id` instruction.
|
||||
pub fn chain_id<'ctx, D>(
|
||||
_context: &mut Context<'ctx, D>,
|
||||
context: &mut Context<'ctx, D>,
|
||||
) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>>
|
||||
where
|
||||
D: Dependency + Clone,
|
||||
{
|
||||
todo!()
|
||||
context.build_runtime_call_to_getter(revive_runtime_api::polkavm_imports::CHAIN_ID)
|
||||
}
|
||||
|
||||
/// Translates the `block_number` instruction.
|
||||
@@ -53,22 +59,7 @@ pub fn block_number<'ctx, D>(
|
||||
where
|
||||
D: Dependency + Clone,
|
||||
{
|
||||
let (output_pointer, output_length_pointer) = context.build_stack_parameter(
|
||||
revive_common::BIT_LENGTH_BLOCK_NUMBER,
|
||||
"block_timestamp_output",
|
||||
);
|
||||
context.build_runtime_call(
|
||||
runtime_api::imports::BLOCK_NUMBER,
|
||||
&[
|
||||
output_pointer.to_int(context).into(),
|
||||
output_length_pointer.to_int(context).into(),
|
||||
],
|
||||
);
|
||||
context.build_load_word(
|
||||
output_pointer,
|
||||
revive_common::BIT_LENGTH_BLOCK_NUMBER,
|
||||
"block_number",
|
||||
)
|
||||
context.build_runtime_call_to_getter(revive_runtime_api::polkavm_imports::BLOCK_NUMBER)
|
||||
}
|
||||
|
||||
/// Translates the `block_timestamp` instruction.
|
||||
@@ -78,22 +69,7 @@ pub fn block_timestamp<'ctx, D>(
|
||||
where
|
||||
D: Dependency + Clone,
|
||||
{
|
||||
let (output_pointer, output_length_pointer) = context.build_stack_parameter(
|
||||
revive_common::BIT_LENGTH_BLOCK_TIMESTAMP,
|
||||
"block_timestamp_output",
|
||||
);
|
||||
context.build_runtime_call(
|
||||
runtime_api::imports::NOW,
|
||||
&[
|
||||
output_pointer.to_int(context).into(),
|
||||
output_length_pointer.to_int(context).into(),
|
||||
],
|
||||
);
|
||||
context.build_load_word(
|
||||
output_pointer,
|
||||
revive_common::BIT_LENGTH_BLOCK_TIMESTAMP,
|
||||
"block_timestamp",
|
||||
)
|
||||
context.build_runtime_call_to_getter(revive_runtime_api::polkavm_imports::NOW)
|
||||
}
|
||||
|
||||
/// Translates the `block_hash` instruction.
|
||||
@@ -137,33 +113,6 @@ where
|
||||
Ok(context.word_const(0).as_basic_value_enum())
|
||||
}
|
||||
|
||||
/// Translates the `msize` instruction.
|
||||
pub fn msize<'ctx, D>(
|
||||
context: &mut Context<'ctx, D>,
|
||||
) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>>
|
||||
where
|
||||
D: Dependency + Clone,
|
||||
{
|
||||
let heap_end = context.build_sbrk(context.xlen_type().const_zero())?;
|
||||
let heap_start = context
|
||||
.get_global(crate::polkavm::GLOBAL_HEAP_MEMORY_POINTER)?
|
||||
.value
|
||||
.as_pointer_value();
|
||||
let heap_size = context.builder().build_int_nuw_sub(
|
||||
context
|
||||
.builder()
|
||||
.build_ptr_to_int(heap_end, context.xlen_type(), "heap_end")?,
|
||||
context
|
||||
.builder()
|
||||
.build_ptr_to_int(heap_start, context.xlen_type(), "heap_start")?,
|
||||
"heap_size",
|
||||
)?;
|
||||
Ok(context
|
||||
.builder()
|
||||
.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>,
|
||||
@@ -171,20 +120,15 @@ pub fn address<'ctx, D>(
|
||||
where
|
||||
D: Dependency + Clone,
|
||||
{
|
||||
let (output_pointer, output_length_pointer) =
|
||||
context.build_stack_parameter(revive_common::BIT_LENGTH_ETH_ADDRESS, "address_output");
|
||||
context.build_runtime_call(
|
||||
runtime_api::imports::ADDRESS,
|
||||
&[
|
||||
output_pointer.to_int(context).into(),
|
||||
output_length_pointer.to_int(context).into(),
|
||||
],
|
||||
let pointer = context.build_alloca_at_entry(
|
||||
context.integer_type(revive_common::BIT_LENGTH_ETH_ADDRESS),
|
||||
"address_output",
|
||||
);
|
||||
let value = context.build_byte_swap(context.build_load(output_pointer, "address")?)?;
|
||||
Ok(context
|
||||
.builder()
|
||||
.build_int_z_extend(value.into_int_value(), context.word_type(), "address_zext")?
|
||||
.into())
|
||||
context.build_runtime_call(
|
||||
revive_runtime_api::polkavm_imports::ADDRESS,
|
||||
&[pointer.to_int(context).into()],
|
||||
);
|
||||
context.build_load_address(pointer)
|
||||
}
|
||||
|
||||
/// Translates the `caller` instruction.
|
||||
@@ -194,18 +138,13 @@ pub fn caller<'ctx, D>(
|
||||
where
|
||||
D: Dependency + Clone,
|
||||
{
|
||||
let (output_pointer, output_length_pointer) =
|
||||
context.build_stack_parameter(revive_common::BIT_LENGTH_ETH_ADDRESS, "caller_output");
|
||||
context.build_runtime_call(
|
||||
runtime_api::imports::CALLER,
|
||||
&[
|
||||
output_pointer.to_int(context).into(),
|
||||
output_length_pointer.to_int(context).into(),
|
||||
],
|
||||
let pointer = context.build_alloca_at_entry(
|
||||
context.integer_type(revive_common::BIT_LENGTH_ETH_ADDRESS),
|
||||
"address_output",
|
||||
);
|
||||
let value = context.build_byte_swap(context.build_load(output_pointer, "caller")?)?;
|
||||
Ok(context
|
||||
.builder()
|
||||
.build_int_z_extend(value.into_int_value(), context.word_type(), "caller_zext")?
|
||||
.into())
|
||||
context.build_runtime_call(
|
||||
revive_runtime_api::polkavm_imports::CALLER,
|
||||
&[pointer.to_int(context).into()],
|
||||
);
|
||||
context.build_load_address(pointer)
|
||||
}
|
||||
|
||||
@@ -7,28 +7,15 @@ use crate::polkavm::context::argument::Argument;
|
||||
use crate::polkavm::context::code_type::CodeType;
|
||||
use crate::polkavm::context::Context;
|
||||
use crate::polkavm::Dependency;
|
||||
use crate::polkavm_const::runtime_api;
|
||||
|
||||
/// Translates the contract `create` instruction.
|
||||
/// The instruction is simulated by a call to a system contract.
|
||||
/// Translates the contract `create` and `create2` instruction.
|
||||
///
|
||||
/// A `salt` value of `None` is equivalent to `create1`.
|
||||
pub fn create<'ctx, D>(
|
||||
context: &mut Context<'ctx, D>,
|
||||
value: inkwell::values::IntValue<'ctx>,
|
||||
input_offset: inkwell::values::IntValue<'ctx>,
|
||||
input_length: inkwell::values::IntValue<'ctx>,
|
||||
) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>>
|
||||
where
|
||||
D: Dependency + Clone,
|
||||
{
|
||||
self::create2(context, value, input_offset, input_length, None)
|
||||
}
|
||||
|
||||
/// Translates the contract `create2` instruction.
|
||||
pub fn create2<'ctx, D>(
|
||||
context: &mut Context<'ctx, D>,
|
||||
value: inkwell::values::IntValue<'ctx>,
|
||||
input_offset: inkwell::values::IntValue<'ctx>,
|
||||
input_length: inkwell::values::IntValue<'ctx>,
|
||||
salt: Option<inkwell::values::IntValue<'ctx>>,
|
||||
) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>>
|
||||
where
|
||||
@@ -37,9 +24,6 @@ where
|
||||
let input_offset = context.safe_truncate_int_to_xlen(input_offset)?;
|
||||
let input_length = context.safe_truncate_int_to_xlen(input_length)?;
|
||||
|
||||
let value_pointer = context.build_alloca(context.value_type(), "value");
|
||||
context.build_store(value_pointer, value)?;
|
||||
|
||||
let code_hash_pointer = context.build_heap_gep(input_offset, input_length)?;
|
||||
|
||||
let input_data_pointer = context.build_gep(
|
||||
@@ -48,53 +32,68 @@ where
|
||||
.xlen_type()
|
||||
.const_int(revive_common::BYTE_LENGTH_WORD as u64, false)],
|
||||
context.byte_type(),
|
||||
"value_ptr_parameter_offset",
|
||||
"input_ptr_parameter_offset",
|
||||
);
|
||||
|
||||
let salt_pointer = context.build_alloca(context.word_type(), "salt");
|
||||
context.build_store(salt_pointer, salt.unwrap_or_else(|| context.word_const(0)))?;
|
||||
let value_pointer = context.build_alloca_at_entry(context.value_type(), "transferred_value");
|
||||
context.build_store(value_pointer, value)?;
|
||||
|
||||
let (address_pointer, address_length_pointer) =
|
||||
context.build_stack_parameter(revive_common::BIT_LENGTH_ETH_ADDRESS, "address_pointer");
|
||||
let salt_pointer = match salt {
|
||||
Some(salt) => {
|
||||
let salt_pointer = context.build_alloca_at_entry(context.word_type(), "salt_pointer");
|
||||
context.build_store(salt_pointer, salt)?;
|
||||
salt_pointer
|
||||
}
|
||||
None => context.sentinel_pointer(),
|
||||
};
|
||||
|
||||
let address_pointer = context.build_alloca_at_entry(
|
||||
context.integer_type(revive_common::BIT_LENGTH_ETH_ADDRESS),
|
||||
"address_pointer",
|
||||
);
|
||||
context.build_store(address_pointer, context.word_const(0))?;
|
||||
|
||||
let argument_pointer = pallet_contracts_pvm_llapi::calling_convention::Spill::new(
|
||||
let argument_type = revive_runtime_api::calling_convention::instantiate(context.llvm());
|
||||
let argument_pointer = context.build_alloca_at_entry(argument_type, "instantiate_arguments");
|
||||
let arguments = &[
|
||||
code_hash_pointer.value.as_basic_value_enum(),
|
||||
context.integer_const(64, 0).as_basic_value_enum(),
|
||||
context.integer_const(64, 0).as_basic_value_enum(),
|
||||
context.sentinel_pointer().value.as_basic_value_enum(),
|
||||
value_pointer.value.as_basic_value_enum(),
|
||||
input_data_pointer.value.as_basic_value_enum(),
|
||||
input_length.as_basic_value_enum(),
|
||||
address_pointer.value.as_basic_value_enum(),
|
||||
context.sentinel_pointer().value.as_basic_value_enum(),
|
||||
context.sentinel_pointer().value.as_basic_value_enum(),
|
||||
salt_pointer.value.as_basic_value_enum(),
|
||||
];
|
||||
revive_runtime_api::calling_convention::spill(
|
||||
context.builder(),
|
||||
pallet_contracts_pvm_llapi::calling_convention::instantiate(context.llvm()),
|
||||
"create2_arguments",
|
||||
)?
|
||||
.next(code_hash_pointer.value)?
|
||||
.skip()
|
||||
.skip()
|
||||
.next(context.sentinel_pointer().value)?
|
||||
.next(value_pointer.value)?
|
||||
.next(input_data_pointer.value)?
|
||||
.next(input_length)?
|
||||
.next(address_pointer.value)?
|
||||
.next(address_length_pointer.value)?
|
||||
.next(context.sentinel_pointer().value)?
|
||||
.next(context.sentinel_pointer().value)?
|
||||
.next(salt_pointer.value)?
|
||||
.next(
|
||||
context
|
||||
.xlen_type()
|
||||
.const_int(revive_common::BYTE_LENGTH_WORD as u64, false),
|
||||
)?
|
||||
.done();
|
||||
argument_pointer.value,
|
||||
argument_type,
|
||||
arguments,
|
||||
)?;
|
||||
|
||||
let argument_pointer = context.builder().build_ptr_to_int(
|
||||
argument_pointer.value,
|
||||
context.xlen_type(),
|
||||
"instantiate_argument_pointer",
|
||||
)?;
|
||||
context.build_runtime_call(
|
||||
runtime_api::imports::INSTANTIATE,
|
||||
&[context
|
||||
.builder()
|
||||
.build_ptr_to_int(argument_pointer, context.xlen_type(), "argument_pointer")?
|
||||
.into()],
|
||||
revive_runtime_api::polkavm_imports::INSTANTIATE,
|
||||
&[argument_pointer.into()],
|
||||
);
|
||||
|
||||
context.build_load_word(
|
||||
address_pointer,
|
||||
revive_common::BIT_LENGTH_ETH_ADDRESS,
|
||||
"address",
|
||||
)
|
||||
let address = context.build_byte_swap(context.build_load(address_pointer, "address")?)?;
|
||||
Ok(context
|
||||
.builder()
|
||||
.build_int_z_extend(
|
||||
address.into_int_value(),
|
||||
context.word_type(),
|
||||
"address_zext",
|
||||
)?
|
||||
.into())
|
||||
}
|
||||
|
||||
/// Translates the contract hash instruction, which is actually used to set the hash of the contract
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
use crate::polkavm::context::Context;
|
||||
use crate::polkavm::Dependency;
|
||||
use crate::polkavm_const::runtime_api;
|
||||
|
||||
/// Translates the `sha3` instruction.
|
||||
pub fn sha3<'ctx, D>(
|
||||
@@ -19,7 +18,7 @@ where
|
||||
let output_pointer = context.build_alloca(context.word_type(), "output_pointer");
|
||||
|
||||
context.build_runtime_call(
|
||||
runtime_api::imports::HASH_KECCAK_256,
|
||||
revive_runtime_api::polkavm_imports::HASH_KECCAK_256,
|
||||
&[
|
||||
input_pointer.to_int(context).into(),
|
||||
length_casted.into(),
|
||||
|
||||
@@ -4,7 +4,6 @@ use inkwell::values::BasicValue;
|
||||
|
||||
use crate::polkavm::context::Context;
|
||||
use crate::polkavm::Dependency;
|
||||
use crate::polkavm_const::runtime_api;
|
||||
|
||||
/// Translates the `gas` instruction.
|
||||
pub fn gas<'ctx, D>(
|
||||
@@ -23,20 +22,13 @@ pub fn value<'ctx, D>(
|
||||
where
|
||||
D: Dependency + Clone,
|
||||
{
|
||||
let (output_pointer, output_length_pointer) =
|
||||
context.build_stack_parameter(revive_common::BIT_LENGTH_VALUE, "value_transferred_output");
|
||||
let output_pointer = context.build_alloca(context.value_type(), "value_transferred");
|
||||
context.build_store(output_pointer, context.word_const(0))?;
|
||||
context.build_runtime_call(
|
||||
runtime_api::imports::VALUE_TRANSFERRED,
|
||||
&[
|
||||
output_pointer.to_int(context).into(),
|
||||
output_length_pointer.to_int(context).into(),
|
||||
],
|
||||
revive_runtime_api::polkavm_imports::VALUE_TRANSFERRED,
|
||||
&[output_pointer.to_int(context).into()],
|
||||
);
|
||||
context.build_load_word(
|
||||
output_pointer,
|
||||
revive_common::BIT_LENGTH_VALUE,
|
||||
"value_transferred",
|
||||
)
|
||||
context.build_load(output_pointer, "value_transferred")
|
||||
}
|
||||
|
||||
/// Translates the `balance` instructions.
|
||||
@@ -47,24 +39,40 @@ pub fn balance<'ctx, D>(
|
||||
where
|
||||
D: Dependency + Clone,
|
||||
{
|
||||
let balance_pointer = context.build_alloca(context.word_type(), "balance_pointer");
|
||||
let address_pointer = context.build_alloca(context.word_type(), "address_pointer");
|
||||
context.build_store(address_pointer, address)?;
|
||||
let address_pointer = context.build_address_argument_store(address)?;
|
||||
|
||||
let balance_pointer = context.build_alloca(context.word_type(), "balance_pointer");
|
||||
let balance = context.builder().build_ptr_to_int(
|
||||
balance_pointer.value,
|
||||
context.xlen_type(),
|
||||
"balance",
|
||||
)?;
|
||||
let address = context.builder().build_ptr_to_int(
|
||||
address_pointer.value,
|
||||
context.xlen_type(),
|
||||
"address",
|
||||
)?;
|
||||
|
||||
context.build_runtime_call(
|
||||
runtime_api::imports::BALANCE,
|
||||
&[address.into(), balance.into()],
|
||||
revive_runtime_api::polkavm_imports::BALANCE_OF,
|
||||
&[address_pointer.to_int(context).into(), balance.into()],
|
||||
);
|
||||
|
||||
context.build_load(balance_pointer, "balance")
|
||||
}
|
||||
|
||||
/// Translates the `selfbalance` instructions.
|
||||
pub fn self_balance<'ctx, D>(
|
||||
context: &mut Context<'ctx, D>,
|
||||
) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>>
|
||||
where
|
||||
D: Dependency + Clone,
|
||||
{
|
||||
let balance_pointer = context.build_alloca(context.word_type(), "balance_pointer");
|
||||
let balance = context.builder().build_ptr_to_int(
|
||||
balance_pointer.value,
|
||||
context.xlen_type(),
|
||||
"balance",
|
||||
)?;
|
||||
|
||||
context.build_runtime_call(
|
||||
revive_runtime_api::polkavm_imports::BALANCE,
|
||||
&[balance.into()],
|
||||
);
|
||||
|
||||
context.build_load(balance_pointer, "balance")
|
||||
|
||||
@@ -4,7 +4,6 @@ use inkwell::values::BasicValue;
|
||||
|
||||
use crate::polkavm::context::Context;
|
||||
use crate::polkavm::Dependency;
|
||||
use crate::polkavm_const::runtime_api;
|
||||
|
||||
/// Translates a log or event call.
|
||||
///
|
||||
@@ -43,6 +42,7 @@ where
|
||||
context.byte_type().array_type(topics_buffer_size as u32),
|
||||
"topics_buffer",
|
||||
);
|
||||
|
||||
for (n, topic) in topics.iter().enumerate() {
|
||||
let topic_buffer_offset = context
|
||||
.xlen_type()
|
||||
@@ -57,6 +57,7 @@ where
|
||||
context.build_byte_swap(topic.as_basic_value_enum())?,
|
||||
)?;
|
||||
}
|
||||
|
||||
[
|
||||
context
|
||||
.builder()
|
||||
@@ -68,14 +69,17 @@ where
|
||||
.as_basic_value_enum(),
|
||||
context
|
||||
.xlen_type()
|
||||
.const_int(topics_buffer_size as u64, false)
|
||||
.const_int(topics.len() as u64, false)
|
||||
.as_basic_value_enum(),
|
||||
input_pointer.as_basic_value_enum(),
|
||||
input_length.as_basic_value_enum(),
|
||||
]
|
||||
};
|
||||
|
||||
let _ = context.build_runtime_call(runtime_api::imports::DEPOSIT_EVENT, &arguments);
|
||||
let _ = context.build_runtime_call(
|
||||
revive_runtime_api::polkavm_imports::DEPOSIT_EVENT,
|
||||
&arguments,
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1,10 +1,7 @@
|
||||
//! Translates the external code operations.
|
||||
|
||||
use inkwell::values::BasicValue;
|
||||
|
||||
use crate::polkavm::context::Context;
|
||||
use crate::polkavm::Dependency;
|
||||
use crate::polkavm_const::runtime_api;
|
||||
|
||||
/// Translates the `extcodesize` instruction if `address` is `Some`.
|
||||
/// Otherwise, translates the `codesize` instruction.
|
||||
@@ -15,41 +12,52 @@ pub fn size<'ctx, D>(
|
||||
where
|
||||
D: Dependency + Clone,
|
||||
{
|
||||
let address_pointer = match address {
|
||||
Some(address) => {
|
||||
let address_pointer = context.build_alloca(context.word_type(), "value");
|
||||
context.build_store(address_pointer, address)?;
|
||||
address_pointer
|
||||
}
|
||||
None => context.sentinel_pointer(),
|
||||
let address = match address {
|
||||
Some(address) => address,
|
||||
None => super::context::address(context)?.into_int_value(),
|
||||
};
|
||||
|
||||
let address_pointer_casted = context.builder().build_ptr_to_int(
|
||||
address_pointer.value,
|
||||
context.xlen_type(),
|
||||
"address_pointer",
|
||||
)?;
|
||||
let value = context
|
||||
.build_runtime_call(
|
||||
runtime_api::imports::CODE_SIZE,
|
||||
&[address_pointer_casted.into()],
|
||||
)
|
||||
.unwrap_or_else(|| panic!("{} should return a value", runtime_api::imports::CODE_SIZE))
|
||||
.into_int_value();
|
||||
let address_pointer = context.build_address_argument_store(address)?;
|
||||
let output_pointer = context.build_alloca_at_entry(context.word_type(), "output_pointer");
|
||||
|
||||
Ok(context
|
||||
.builder()
|
||||
.build_int_z_extend(value, context.word_type(), "extcodesize")?
|
||||
.as_basic_value_enum())
|
||||
context.build_runtime_call(
|
||||
revive_runtime_api::polkavm_imports::CODE_SIZE,
|
||||
&[
|
||||
address_pointer.to_int(context).into(),
|
||||
output_pointer.to_int(context).into(),
|
||||
],
|
||||
);
|
||||
|
||||
context.build_load(output_pointer, "code_size")
|
||||
}
|
||||
|
||||
/// Translates the `extcodehash` instruction.
|
||||
pub fn hash<'ctx, D>(
|
||||
_context: &mut Context<'ctx, D>,
|
||||
_address: inkwell::values::IntValue<'ctx>,
|
||||
context: &mut Context<'ctx, D>,
|
||||
address: inkwell::values::IntValue<'ctx>,
|
||||
) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>>
|
||||
where
|
||||
D: Dependency + Clone,
|
||||
{
|
||||
todo!()
|
||||
let address_type = context.integer_type(revive_common::BIT_LENGTH_ETH_ADDRESS);
|
||||
let address_pointer = context.build_alloca_at_entry(address_type, "address_pointer");
|
||||
let address_truncated =
|
||||
context
|
||||
.builder()
|
||||
.build_int_truncate(address, address_type, "address_truncated")?;
|
||||
let address_swapped = context.build_byte_swap(address_truncated.into())?;
|
||||
context.build_store(address_pointer, address_swapped)?;
|
||||
|
||||
let extcodehash_pointer =
|
||||
context.build_alloca_at_entry(context.word_type(), "extcodehash_pointer");
|
||||
|
||||
context.build_runtime_call(
|
||||
revive_runtime_api::polkavm_imports::CODE_HASH,
|
||||
&[
|
||||
address_pointer.to_int(context).into(),
|
||||
extcodehash_pointer.to_int(context).into(),
|
||||
],
|
||||
);
|
||||
|
||||
context.build_byte_swap(context.build_load(extcodehash_pointer, "extcodehash_value")?)
|
||||
}
|
||||
|
||||
@@ -1,14 +1,19 @@
|
||||
//! Translates the contract immutable operations.
|
||||
|
||||
use inkwell::types::BasicType;
|
||||
|
||||
use crate::polkavm::context::address_space::AddressSpace;
|
||||
use crate::polkavm::context::code_type::CodeType;
|
||||
use crate::polkavm::context::function::runtime;
|
||||
use crate::polkavm::context::pointer::Pointer;
|
||||
use crate::polkavm::context::Context;
|
||||
use crate::polkavm::Dependency;
|
||||
|
||||
/// Translates the contract immutable load.
|
||||
/// In the deploy code the values are read from the auxiliary heap.
|
||||
/// In the runtime code they are requested from the system contract.
|
||||
///
|
||||
/// In deploy code the values are read from the stack.
|
||||
///
|
||||
/// In runtime code they are loaded lazily with the `get_immutable_data` syscall.
|
||||
pub fn load<'ctx, D>(
|
||||
context: &mut Context<'ctx, D>,
|
||||
index: inkwell::values::IntValue<'ctx>,
|
||||
@@ -20,38 +25,27 @@ where
|
||||
None => {
|
||||
anyhow::bail!("Immutables are not available if the contract part is undefined");
|
||||
}
|
||||
Some(CodeType::Deploy) => {
|
||||
let index_double = context.builder().build_int_mul(
|
||||
index,
|
||||
context.word_const(2),
|
||||
"immutable_load_index_double",
|
||||
)?;
|
||||
let offset_absolute = context.builder().build_int_add(
|
||||
index_double,
|
||||
context.word_const(
|
||||
crate::polkavm::HEAP_AUX_OFFSET_CONSTRUCTOR_RETURN_DATA
|
||||
+ (3 * revive_common::BYTE_LENGTH_WORD) as u64,
|
||||
),
|
||||
"immutable_offset_absolute",
|
||||
)?;
|
||||
let immutable_pointer = Pointer::new_with_offset(
|
||||
context,
|
||||
AddressSpace::default(),
|
||||
context.word_type(),
|
||||
offset_absolute,
|
||||
"immutable_pointer",
|
||||
);
|
||||
context.build_load(immutable_pointer, "immutable_value")
|
||||
}
|
||||
Some(CodeType::Deploy) => load_from_memory(context, index),
|
||||
Some(CodeType::Runtime) => {
|
||||
todo!()
|
||||
context.build_call(
|
||||
context
|
||||
.get_function(runtime::FUNCTION_LOAD_IMMUTABLE_DATA)
|
||||
.expect("is always declared for runtime code")
|
||||
.borrow()
|
||||
.declaration(),
|
||||
&[],
|
||||
runtime::FUNCTION_LOAD_IMMUTABLE_DATA,
|
||||
);
|
||||
load_from_memory(context, index)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Translates the contract immutable store.
|
||||
/// In the deploy code the values are written to the auxiliary heap at the predefined offset,
|
||||
/// being prepared for returning to the system contract for saving.
|
||||
///
|
||||
/// In deploy code the values are written to the stack at the predefined offset,
|
||||
/// being prepared for storing them using the `set_immutable_data` syscall.
|
||||
///
|
||||
/// Ignored in the runtime code.
|
||||
pub fn store<'ctx, D>(
|
||||
context: &mut Context<'ctx, D>,
|
||||
@@ -66,46 +60,48 @@ where
|
||||
anyhow::bail!("Immutables are not available if the contract part is undefined");
|
||||
}
|
||||
Some(CodeType::Deploy) => {
|
||||
let index_double = context.builder().build_int_mul(
|
||||
index,
|
||||
context.word_const(2),
|
||||
"immutable_load_index_double",
|
||||
)?;
|
||||
let index_offset_absolute = context.builder().build_int_add(
|
||||
index_double,
|
||||
context.word_const(
|
||||
crate::polkavm::HEAP_AUX_OFFSET_CONSTRUCTOR_RETURN_DATA
|
||||
+ (2 * revive_common::BYTE_LENGTH_WORD) as u64,
|
||||
let immutable_data_pointer = context
|
||||
.get_global(revive_runtime_api::immutable_data::GLOBAL_IMMUTABLE_DATA_POINTER)?
|
||||
.value
|
||||
.as_pointer_value();
|
||||
let immutable_pointer = context.build_gep(
|
||||
Pointer::new(
|
||||
context.word_type(),
|
||||
AddressSpace::Stack,
|
||||
immutable_data_pointer,
|
||||
),
|
||||
"index_offset_absolute",
|
||||
)?;
|
||||
let index_offset_pointer = Pointer::new_with_offset(
|
||||
context,
|
||||
AddressSpace::default(),
|
||||
context.word_type(),
|
||||
index_offset_absolute,
|
||||
"immutable_index_pointer",
|
||||
&[index],
|
||||
context.word_type().as_basic_type_enum(),
|
||||
"immutable_variable_pointer",
|
||||
);
|
||||
context.build_store(index_offset_pointer, index)?;
|
||||
|
||||
let value_offset_absolute = context.builder().build_int_add(
|
||||
index_offset_absolute,
|
||||
context.word_const(revive_common::BYTE_LENGTH_WORD as u64),
|
||||
"value_offset_absolute",
|
||||
)?;
|
||||
let value_offset_pointer = Pointer::new_with_offset(
|
||||
context,
|
||||
AddressSpace::default(),
|
||||
context.word_type(),
|
||||
value_offset_absolute,
|
||||
"immutable_value_pointer",
|
||||
);
|
||||
context.build_store(value_offset_pointer, value)?;
|
||||
|
||||
Ok(())
|
||||
context.build_store(immutable_pointer, value)
|
||||
}
|
||||
Some(CodeType::Runtime) => {
|
||||
anyhow::bail!("Immutable writes are not available in the runtime code");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn load_from_memory<'ctx, D>(
|
||||
context: &mut Context<'ctx, D>,
|
||||
index: inkwell::values::IntValue<'ctx>,
|
||||
) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>>
|
||||
where
|
||||
D: Dependency + Clone,
|
||||
{
|
||||
let immutable_data_pointer = context
|
||||
.get_global(revive_runtime_api::immutable_data::GLOBAL_IMMUTABLE_DATA_POINTER)?
|
||||
.value
|
||||
.as_pointer_value();
|
||||
let immutable_pointer = context.build_gep(
|
||||
Pointer::new(
|
||||
context.word_type(),
|
||||
AddressSpace::Stack,
|
||||
immutable_data_pointer,
|
||||
),
|
||||
&[index],
|
||||
context.word_type().as_basic_type_enum(),
|
||||
"immutable_variable_pointer",
|
||||
);
|
||||
context.build_load(immutable_pointer, "immutable_value")
|
||||
}
|
||||
|
||||
@@ -1,10 +1,29 @@
|
||||
//! Translates the heap memory operations.
|
||||
|
||||
use inkwell::values::BasicValue;
|
||||
|
||||
use crate::polkavm::context::address_space::AddressSpace;
|
||||
use crate::polkavm::context::pointer::Pointer;
|
||||
use crate::polkavm::context::Context;
|
||||
use crate::polkavm::Dependency;
|
||||
|
||||
/// Translates the `msize` instruction.
|
||||
pub fn msize<'ctx, D>(
|
||||
context: &mut Context<'ctx, D>,
|
||||
) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>>
|
||||
where
|
||||
D: Dependency + Clone,
|
||||
{
|
||||
Ok(context
|
||||
.builder()
|
||||
.build_int_z_extend(
|
||||
context.build_msize()?,
|
||||
context.word_type(),
|
||||
"heap_size_extended",
|
||||
)?
|
||||
.as_basic_value_enum())
|
||||
}
|
||||
|
||||
/// Translates the `mload` instruction.
|
||||
/// Uses the main heap.
|
||||
pub fn load<'ctx, D>(
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
//! Translates the transaction return operations.
|
||||
|
||||
use crate::polkavm::context::address_space::AddressSpace;
|
||||
use crate::polkavm::context::code_type::CodeType;
|
||||
use crate::polkavm::context::pointer::Pointer;
|
||||
use crate::polkavm::context::Context;
|
||||
use crate::polkavm::Dependency;
|
||||
|
||||
@@ -12,8 +15,60 @@ pub fn r#return<'ctx, D>(
|
||||
where
|
||||
D: Dependency + Clone,
|
||||
{
|
||||
if context.code_type().is_none() {
|
||||
anyhow::bail!("Return is not available if the contract part is undefined");
|
||||
match context.code_type() {
|
||||
None => anyhow::bail!("Return is not available if the contract part is undefined"),
|
||||
Some(CodeType::Deploy) => {
|
||||
let immutable_data_size_pointer = context
|
||||
.get_global(revive_runtime_api::immutable_data::GLOBAL_IMMUTABLE_DATA_SIZE)?
|
||||
.value
|
||||
.as_pointer_value();
|
||||
let immutable_data_size = context.build_load(
|
||||
Pointer::new(
|
||||
context.xlen_type(),
|
||||
AddressSpace::Stack,
|
||||
immutable_data_size_pointer,
|
||||
),
|
||||
"immutable_data_size_load",
|
||||
)?;
|
||||
|
||||
let write_immutable_data_block = context.append_basic_block("write_immutables_block");
|
||||
let join_return_block = context.append_basic_block("join_return_block");
|
||||
let immutable_data_size_is_zero = context.builder().build_int_compare(
|
||||
inkwell::IntPredicate::EQ,
|
||||
context.xlen_type().const_zero(),
|
||||
immutable_data_size.into_int_value(),
|
||||
"immutable_data_size_is_zero",
|
||||
)?;
|
||||
context.build_conditional_branch(
|
||||
immutable_data_size_is_zero,
|
||||
join_return_block,
|
||||
write_immutable_data_block,
|
||||
)?;
|
||||
|
||||
context.set_basic_block(write_immutable_data_block);
|
||||
let immutable_data_pointer = context
|
||||
.get_global(revive_runtime_api::immutable_data::GLOBAL_IMMUTABLE_DATA_POINTER)?
|
||||
.value
|
||||
.as_pointer_value();
|
||||
context.build_runtime_call(
|
||||
revive_runtime_api::polkavm_imports::SET_IMMUTABLE_DATA,
|
||||
&[
|
||||
context
|
||||
.builder()
|
||||
.build_ptr_to_int(
|
||||
immutable_data_pointer,
|
||||
context.xlen_type(),
|
||||
"immutable_data_pointer_to_xlen",
|
||||
)?
|
||||
.into(),
|
||||
immutable_data_size,
|
||||
],
|
||||
);
|
||||
context.build_unconditional_branch(join_return_block);
|
||||
|
||||
context.set_basic_block(join_return_block);
|
||||
}
|
||||
Some(CodeType::Runtime) => {}
|
||||
}
|
||||
|
||||
context.build_exit(
|
||||
|
||||
@@ -1,10 +1,7 @@
|
||||
//! Translates the return data instructions.
|
||||
|
||||
use inkwell::values::BasicValue;
|
||||
|
||||
use crate::polkavm::context::Context;
|
||||
use crate::polkavm::Dependency;
|
||||
use crate::polkavm_const::runtime_api;
|
||||
|
||||
/// Translates the return data size.
|
||||
pub fn size<'ctx, D>(
|
||||
@@ -13,13 +10,17 @@ pub fn size<'ctx, D>(
|
||||
where
|
||||
D: Dependency + Clone,
|
||||
{
|
||||
let value = context
|
||||
.get_global_value(crate::polkavm::GLOBAL_RETURN_DATA_SIZE)?
|
||||
.into_int_value();
|
||||
Ok(context
|
||||
.builder()
|
||||
.build_int_z_extend(value, context.word_type(), "calldatasize_extended")?
|
||||
.as_basic_value_enum())
|
||||
let output_pointer = context.build_alloca_at_entry(context.word_type(), "return_data_size");
|
||||
let output_pointer_parameter = context.builder().build_ptr_to_int(
|
||||
output_pointer.value,
|
||||
context.xlen_type(),
|
||||
"return_data_copy_output_pointer",
|
||||
)?;
|
||||
context.build_runtime_call(
|
||||
revive_runtime_api::polkavm_imports::RETURNDATASIZE,
|
||||
&[output_pointer_parameter.into()],
|
||||
);
|
||||
context.build_load(output_pointer, "return_data_size_load")
|
||||
}
|
||||
|
||||
/// Translates the return data copy, trapping if
|
||||
@@ -39,16 +40,49 @@ where
|
||||
let destination_offset = context.safe_truncate_int_to_xlen(destination_offset)?;
|
||||
let size = context.safe_truncate_int_to_xlen(size)?;
|
||||
|
||||
let destination_offset = context.builder().build_ptr_to_int(
|
||||
let output_pointer = context.builder().build_ptr_to_int(
|
||||
context.build_heap_gep(destination_offset, size)?.value,
|
||||
context.xlen_type(),
|
||||
"destination_offset",
|
||||
"return_data_copy_output_pointer",
|
||||
)?;
|
||||
|
||||
let output_length_pointer = context.build_alloca_at_entry(
|
||||
context.xlen_type(),
|
||||
"return_data_copy_output_length_pointer",
|
||||
);
|
||||
context.build_store(output_length_pointer, size)?;
|
||||
let output_length_pointer_int = context.builder().build_ptr_to_int(
|
||||
output_length_pointer.value,
|
||||
context.xlen_type(),
|
||||
"return_data_copy_output_length_pointer_int",
|
||||
)?;
|
||||
|
||||
context.build_runtime_call(
|
||||
runtime_api::imports::RETURNDATACOPY,
|
||||
&[destination_offset.into(), source_offset.into(), size.into()],
|
||||
revive_runtime_api::polkavm_imports::RETURNDATACOPY,
|
||||
&[
|
||||
output_pointer.into(),
|
||||
output_length_pointer_int.into(),
|
||||
source_offset.into(),
|
||||
],
|
||||
);
|
||||
|
||||
// Trap on OOB (will be different in EOF code)
|
||||
let overflow_block = context.append_basic_block("return_data_overflow");
|
||||
let non_overflow_block = context.append_basic_block("return_data_non_overflow");
|
||||
let is_overflow = context.builder().build_int_compare(
|
||||
inkwell::IntPredicate::UGT,
|
||||
size,
|
||||
context
|
||||
.build_load(output_length_pointer, "bytes_written")?
|
||||
.into_int_value(),
|
||||
"is_overflow",
|
||||
)?;
|
||||
context.build_conditional_branch(is_overflow, overflow_block, non_overflow_block)?;
|
||||
|
||||
context.set_basic_block(overflow_block);
|
||||
context.build_call(context.intrinsics().trap, &[], "invalid_trap");
|
||||
context.build_unreachable();
|
||||
|
||||
context.set_basic_block(non_overflow_block);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
//! Translates the storage operations.
|
||||
|
||||
use crate::polkavm::context::address_space::AddressSpace;
|
||||
use crate::polkavm::context::pointer::Pointer;
|
||||
use crate::polkavm::context::Context;
|
||||
use crate::polkavm::Dependency;
|
||||
|
||||
@@ -13,14 +12,10 @@ pub fn load<'ctx, D>(
|
||||
where
|
||||
D: Dependency + Clone,
|
||||
{
|
||||
let position_pointer = Pointer::new_with_offset(
|
||||
context,
|
||||
AddressSpace::Storage,
|
||||
context.word_type(),
|
||||
position,
|
||||
"storage_load_position_pointer",
|
||||
);
|
||||
context.build_load(position_pointer, "storage_load_value")
|
||||
let mut slot_ptr = context.build_alloca_at_entry(context.word_type(), "slot_pointer");
|
||||
slot_ptr.address_space = AddressSpace::Storage;
|
||||
context.builder().build_store(slot_ptr.value, position)?;
|
||||
context.build_load(slot_ptr, "storage_load_value")
|
||||
}
|
||||
|
||||
/// Translates the storage store.
|
||||
@@ -32,14 +27,10 @@ pub fn store<'ctx, D>(
|
||||
where
|
||||
D: Dependency + Clone,
|
||||
{
|
||||
let position_pointer = Pointer::new_with_offset(
|
||||
context,
|
||||
AddressSpace::Storage,
|
||||
context.word_type(),
|
||||
position,
|
||||
"storage_store_position_pointer",
|
||||
);
|
||||
context.build_store(position_pointer, value)?;
|
||||
let mut slot_ptr = context.build_alloca_at_entry(context.word_type(), "slot_pointer");
|
||||
slot_ptr.address_space = AddressSpace::Storage;
|
||||
context.builder().build_store(slot_ptr.value, position)?;
|
||||
context.build_store(slot_ptr, value)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -51,14 +42,10 @@ pub fn transient_load<'ctx, D>(
|
||||
where
|
||||
D: Dependency + Clone,
|
||||
{
|
||||
let position_pointer = Pointer::new_with_offset(
|
||||
context,
|
||||
AddressSpace::TransientStorage,
|
||||
context.word_type(),
|
||||
position,
|
||||
"transient_storage_load_position_pointer",
|
||||
);
|
||||
context.build_load(position_pointer, "transient_storage_load_value")
|
||||
let mut slot_ptr = context.build_alloca_at_entry(context.word_type(), "slot_pointer");
|
||||
slot_ptr.address_space = AddressSpace::TransientStorage;
|
||||
context.builder().build_store(slot_ptr.value, position)?;
|
||||
context.build_load(slot_ptr, "transient_storage_load_value")
|
||||
}
|
||||
|
||||
/// Translates the transient storage store.
|
||||
@@ -70,13 +57,9 @@ pub fn transient_store<'ctx, D>(
|
||||
where
|
||||
D: Dependency + Clone,
|
||||
{
|
||||
let position_pointer = Pointer::new_with_offset(
|
||||
context,
|
||||
AddressSpace::TransientStorage,
|
||||
context.word_type(),
|
||||
position,
|
||||
"transient_storage_store_position_pointer",
|
||||
);
|
||||
context.build_store(position_pointer, value)?;
|
||||
let mut slot_ptr = context.build_alloca_at_entry(context.word_type(), "slot_pointer");
|
||||
slot_ptr.address_space = AddressSpace::TransientStorage;
|
||||
context.builder().build_store(slot_ptr.value, position)?;
|
||||
context.build_store(slot_ptr, value)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -97,7 +97,6 @@ pub trait Dependency {
|
||||
dependency: Self,
|
||||
path: &str,
|
||||
optimizer_settings: OptimizerSettings,
|
||||
is_system_mode: bool,
|
||||
include_metadata_hash: bool,
|
||||
debug_config: Option<DebugConfig>,
|
||||
) -> anyhow::Result<String>;
|
||||
@@ -118,7 +117,6 @@ impl Dependency for DummyDependency {
|
||||
_dependency: Self,
|
||||
_path: &str,
|
||||
_optimizer_settings: OptimizerSettings,
|
||||
_is_system_mode: bool,
|
||||
_include_metadata_hash: bool,
|
||||
_debug_config: Option<DebugConfig>,
|
||||
) -> anyhow::Result<String> {
|
||||
|
||||
@@ -39,9 +39,9 @@ impl TargetMachine {
|
||||
|
||||
/// LLVM target features.
|
||||
#[cfg(feature = "riscv-zbb")]
|
||||
pub const VM_FEATURES: &'static str = "+zbb,+e,+m";
|
||||
pub const VM_FEATURES: &'static str = "+zbb,+a,+e,+m,+c,+fast-unaligned-access,+xtheadcondmov";
|
||||
#[cfg(not(feature = "riscv-zbb"))]
|
||||
pub const VM_FEATURES: &'static str = "+e,+m";
|
||||
pub const VM_FEATURES: &'static str = "+a,+e,+m,+c,+fast-unaligned-access,+xtheadcondmov";
|
||||
|
||||
/// A shortcut constructor.
|
||||
/// A separate instance for every optimization level is created.
|
||||
|
||||
Reference in New Issue
Block a user