switch to pallet_revive runtime (#40)

Signed-off-by: xermicus <cyrill@parity.io>
This commit is contained in:
Cyrill Leutwiler
2024-09-03 17:18:22 +02:00
committed by GitHub
parent 41c8d4e955
commit d2f76b645f
66 changed files with 3023 additions and 2911 deletions
+1 -1
View File
@@ -34,7 +34,7 @@ polkavm-disassembler = { workspace = true }
polkavm-common = { workspace = true }
revive-common = { workspace = true }
pallet-contracts-pvm-llapi = { workspace = true }
revive-runtime-api = { workspace = true }
revive-linker = { workspace = true }
revive-builtins = { workspace = true }
revive-stdlib = { workspace = true }
@@ -19,7 +19,7 @@ pub mod imports {
pub static BLOCK_NUMBER: &str = "block_number";
pub static CALL: &str = "seal_call";
pub static CALL: &str = "call";
pub static CALLER: &str = "caller";
@@ -233,11 +233,12 @@ 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)?;
for symbol in runtime_api::exports::EXPORTS {
context.declare_extern_function(symbol)?;
}
context.add_function(
runtime::FUNCTION_ENTRY,
entry_function_type,
0,
Some(inkwell::module::Linkage::External),
)?;
Ok(())
}
@@ -258,26 +259,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());
+34 -18
View File
@@ -124,27 +124,17 @@ 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");
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),
);
}
.expect("the PolkaVM imports module should be linkable");
for import in runtime_api::imports::IMPORTS {
module
@@ -154,6 +144,24 @@ 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
)
})
}
/// Configure the PolkaVM minimum stack size.
fn set_polkavm_stack_size(
llvm: &'ctx inkwell::context::Context,
@@ -161,7 +169,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 +199,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 +238,8 @@ where
) -> anyhow::Result<Build> {
let module_clone = self.module.clone();
self.link_polkavm_exports(contract_path)?;
let target_machine = TargetMachine::new(Target::PVM, self.optimizer.settings())?;
target_machine.set_target_data(self.module());
@@ -688,8 +698,14 @@ where
self.builder()
.build_store(storage_key_pointer.value, storage_key_value)?;
let (storage_value_pointer, storage_value_length_pointer) = self
.build_stack_parameter(revive_common::BIT_LENGTH_WORD, "storage_value_pointer");
let storage_value_pointer =
self.build_alloca(self.word_type(), "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;
+2 -2
View File
@@ -57,9 +57,9 @@ where
let output_length_pointer = context.get_global(crate::polkavm::GLOBAL_RETURN_DATA_SIZE)?;
context.build_store(output_length_pointer.into(), output_length)?;
let argument_pointer = pallet_contracts_pvm_llapi::calling_convention::Spill::new(
let argument_pointer = revive_runtime_api::calling_convention::Spill::new(
context.builder(),
pallet_contracts_pvm_llapi::calling_convention::call(context.llvm()),
revive_runtime_api::calling_convention::call(context.llvm()),
"call_arguments",
)?
.next(context.xlen_type().const_int(flags as u64, false))?
@@ -38,12 +38,12 @@ where
/// 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!()
Ok(context.word_const(0).as_basic_value_enum())
}
/// Translates the `block_number` instruction.
@@ -58,9 +58,9 @@ where
context.build_stack_parameter(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_pointer = revive_runtime_api::calling_convention::Spill::new(
context.builder(),
pallet_contracts_pvm_llapi::calling_convention::instantiate(context.llvm()),
revive_runtime_api::calling_convention::instantiate(context.llvm()),
"create2_arguments",
)?
.next(code_hash_pointer.value)?