internalize register size

Signed-off-by: xermicus <cyrill@parity.io>
This commit is contained in:
xermicus
2024-05-06 14:30:32 +02:00
parent 518c03d045
commit b1a3a452ac
6 changed files with 37 additions and 26 deletions
@@ -50,7 +50,7 @@ impl Entry {
context context
.get_global(crate::polkavm::GLOBAL_HEAP_MEMORY_POINTER)? .get_global(crate::polkavm::GLOBAL_HEAP_MEMORY_POINTER)?
.into(), .into(),
context.build_sbrk(context.integer_const(32, 0))?, context.build_sbrk(context.integer_const(crate::polkavm::XLEN, 0))?,
)?; )?;
context.set_global( context.set_global(
@@ -112,7 +112,7 @@ impl Entry {
context.build_store( context.build_store(
length_pointer, length_pointer,
context.integer_const(32, Self::MAX_CALLDATA_SIZE as u64), context.integer_const(crate::polkavm::XLEN, Self::MAX_CALLDATA_SIZE as u64),
)?; )?;
context.build_runtime_call( context.build_runtime_call(
runtime_api::INPUT, runtime_api::INPUT,
+16 -13
View File
@@ -677,14 +677,17 @@ where
.build_store(storage_key_pointer.value, storage_key_value)?; .build_store(storage_key_pointer.value, storage_key_value)?;
self.builder().build_store( self.builder().build_store(
storage_value_length_pointer.value, storage_value_length_pointer.value,
self.integer_const(32, revive_common::BIT_LENGTH_FIELD as u64), self.integer_const(
crate::polkavm::XLEN,
revive_common::BIT_LENGTH_FIELD as u64,
),
)?; )?;
self.build_runtime_call( self.build_runtime_call(
runtime_api::GET_STORAGE, runtime_api::GET_STORAGE,
&[ &[
storage_key_pointer_casted.into(), storage_key_pointer_casted.into(),
self.integer_const(32, 32).into(), self.integer_const(crate::polkavm::XLEN, 32).into(),
storage_value_pointer_casted.into(), storage_value_pointer_casted.into(),
storage_value_length_pointer_casted.into(), storage_value_length_pointer_casted.into(),
], ],
@@ -788,9 +791,9 @@ where
runtime_api::SET_STORAGE, runtime_api::SET_STORAGE,
&[ &[
storage_key_pointer_casted.into(), storage_key_pointer_casted.into(),
self.integer_const(32, 32).into(), self.integer_const(crate::polkavm::XLEN, 32).into(),
storage_value_pointer_casted.into(), storage_value_pointer_casted.into(),
self.integer_const(32, 32).into(), self.integer_const(crate::polkavm::XLEN, 32).into(),
], ],
); );
} }
@@ -1124,11 +1127,11 @@ where
// zkevm_opcode_defs::RetForwardPageType::UseHeap // zkevm_opcode_defs::RetForwardPageType::UseHeap
//}; //};
let offset_truncated = self.safe_truncate_int_to_i32(offset)?; let offset_truncated = self.safe_truncate_int_to_xlen(offset)?;
let length_truncated = self.safe_truncate_int_to_i32(length)?; let length_truncated = self.safe_truncate_int_to_xlen(length)?;
let offset_into_heap = self.build_heap_gep(offset_truncated, length_truncated)?; let offset_into_heap = self.build_heap_gep(offset_truncated, length_truncated)?;
let length_pointer = self.safe_truncate_int_to_i32(length)?; let length_pointer = self.safe_truncate_int_to_xlen(length)?;
let offset_pointer = self.builder().build_ptr_to_int( let offset_pointer = self.builder().build_ptr_to_int(
offset_into_heap.value, offset_into_heap.value,
self.xlen_type(), self.xlen_type(),
@@ -1144,11 +1147,11 @@ where
Ok(()) Ok(())
} }
/// Truncate a memory offset into 32 bits, trapping if it doesn't fit. /// Truncate a memory offset to register size, trapping if it doesn't fit.
/// Pointers are represented as opaque 256 bit integer values in EVM. /// Pointers are represented as opaque 256 bit integer values in EVM.
/// In practice, they should never exceed a 32 bit value. However, we /// In practice, they should never exceed a register sized bit value.
/// still protect against this possibility here. /// However, we still protect against this possibility here.
pub fn safe_truncate_int_to_i32( pub fn safe_truncate_int_to_xlen(
&self, &self,
value: inkwell::values::IntValue<'ctx>, value: inkwell::values::IntValue<'ctx>,
) -> anyhow::Result<inkwell::values::IntValue<'ctx>> { ) -> anyhow::Result<inkwell::values::IntValue<'ctx>> {
@@ -1230,7 +1233,7 @@ where
/// Returns a pointer to `offset` into the heap, allocating /// Returns a pointer to `offset` into the heap, allocating
/// enough memory if `offset + length` would be out of bounds. /// enough memory if `offset + length` would be out of bounds.
/// # Panics /// # Panics
/// Assumes `offset` and `length` to be an i32 value. /// Assumes `offset` and `length` to be a register sized value.
pub fn build_heap_gep( pub fn build_heap_gep(
&self, &self,
offset: inkwell::values::IntValue<'ctx>, offset: inkwell::values::IntValue<'ctx>,
@@ -1243,7 +1246,7 @@ where
.get_global(crate::polkavm::GLOBAL_HEAP_MEMORY_POINTER)? .get_global(crate::polkavm::GLOBAL_HEAP_MEMORY_POINTER)?
.value .value
.as_pointer_value(); .as_pointer_value();
let heap_end = self.build_sbrk(self.integer_const(32, 0))?; let heap_end = self.build_sbrk(self.integer_const(crate::polkavm::XLEN, 0))?;
let value_end = self.build_gep( let value_end = self.build_gep(
Pointer::new(self.byte_type(), AddressSpace::Stack, heap_start), Pointer::new(self.byte_type(), AddressSpace::Stack, heap_start),
&[self.builder().build_int_nuw_add(offset, length, "end")?], &[self.builder().build_int_nuw_add(offset, length, "end")?],
@@ -68,7 +68,7 @@ impl<'ctx> Pointer<'ctx> {
"Stack pointers cannot be addressed" "Stack pointers cannot be addressed"
); );
let offset = context.safe_truncate_int_to_i32(offset).unwrap(); let offset = context.safe_truncate_int_to_xlen(offset).unwrap();
let value = context let value = context
.builder .builder
.build_int_to_ptr(offset, context.llvm().ptr_type(address_space.into()), name) .build_int_to_ptr(offset, context.llvm().ptr_type(address_space.into()), name)
@@ -51,8 +51,8 @@ pub fn copy<'ctx, D>(
where where
D: Dependency + Clone, D: Dependency + Clone,
{ {
let offset = context.safe_truncate_int_to_i32(destination_offset)?; let offset = context.safe_truncate_int_to_xlen(destination_offset)?;
let size = context.safe_truncate_int_to_i32(size)?; let size = context.safe_truncate_int_to_xlen(size)?;
let destination = context.build_heap_gep(offset, size)?; let destination = context.build_heap_gep(offset, size)?;
let calldata_pointer = context let calldata_pointer = context
@@ -61,7 +61,7 @@ where
.as_pointer_value(); .as_pointer_value();
let source = context.build_gep( let source = context.build_gep(
Pointer::new(context.byte_type(), AddressSpace::Stack, calldata_pointer), Pointer::new(context.byte_type(), AddressSpace::Stack, calldata_pointer),
&[context.safe_truncate_int_to_i32(source_offset)?], &[context.safe_truncate_int_to_xlen(source_offset)?],
context.byte_type(), context.byte_type(),
"calldata_pointer_with_offset", "calldata_pointer_with_offset",
); );
@@ -13,8 +13,8 @@ pub fn sha3<'ctx, D>(
where where
D: Dependency + Clone, D: Dependency + Clone,
{ {
let offset_casted = context.safe_truncate_int_to_i32(offset)?; let offset_casted = context.safe_truncate_int_to_xlen(offset)?;
let length_casted = context.safe_truncate_int_to_i32(length)?; let length_casted = context.safe_truncate_int_to_xlen(length)?;
let input_pointer = context.build_heap_gep(offset_casted, length_casted)?; let input_pointer = context.build_heap_gep(offset_casted, length_casted)?;
let input_pointer_casted = context.builder().build_ptr_to_int( let input_pointer_casted = context.builder().build_ptr_to_int(
input_pointer.value, input_pointer.value,
+13 -5
View File
@@ -61,13 +61,17 @@ where
)?; )?;
context.build_exit( context.build_exit(
context.integer_const(32, 0), context.integer_const(crate::polkavm::XLEN, 0),
context.field_const(crate::polkavm::HEAP_AUX_OFFSET_CONSTRUCTOR_RETURN_DATA), context.field_const(crate::polkavm::HEAP_AUX_OFFSET_CONSTRUCTOR_RETURN_DATA),
return_data_length, return_data_length,
)?; )?;
} }
Some(CodeType::Runtime) => { Some(CodeType::Runtime) => {
context.build_exit(context.integer_const(32, 0), offset, length)?; context.build_exit(
context.integer_const(crate::polkavm::XLEN, 0),
offset,
length,
)?;
} }
} }
@@ -83,7 +87,11 @@ pub fn revert<'ctx, D>(
where where
D: Dependency + Clone, D: Dependency + Clone,
{ {
context.build_exit(context.integer_const(32, 1), offset, length) context.build_exit(
context.integer_const(crate::polkavm::XLEN, 1),
offset,
length,
)
} }
/// Translates the `stop` instruction. /// Translates the `stop` instruction.
@@ -94,8 +102,8 @@ where
{ {
r#return( r#return(
context, context,
context.integer_const(32, 0), context.integer_const(crate::polkavm::XLEN, 0),
context.integer_const(32, 0), context.integer_const(crate::polkavm::XLEN, 0),
) )
} }