implement the code size opcodes (#107)

This commit is contained in:
Cyrill Leutwiler
2024-10-31 11:46:47 +01:00
committed by GitHub
parent 68564f9866
commit 43d2ef3ce9
6 changed files with 484 additions and 418 deletions
+14 -30
View File
@@ -1,7 +1,5 @@
//! Translates the external code operations.
use inkwell::values::BasicValue;
use crate::polkavm::context::Context;
use crate::polkavm::Dependency;
@@ -14,37 +12,23 @@ 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(
revive_runtime_api::polkavm_imports::CODE_SIZE,
&[address_pointer_casted.into()],
)
.unwrap_or_else(|| {
panic!(
"{} should return a value",
revive_runtime_api::polkavm_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.