wip evm builtins

Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
This commit is contained in:
Cyrill Leutwiler
2025-07-03 15:36:11 +02:00
parent dfe56f9306
commit bb3b6ddb41
5 changed files with 91 additions and 15 deletions
+1
View File
@@ -19,6 +19,7 @@ pub use self::polkavm::context::function::declaration::Declaration as PolkaVMFun
pub use self::polkavm::context::function::intrinsics::Intrinsics as PolkaVMIntrinsicFunction;
pub use self::polkavm::context::function::llvm_runtime::LLVMRuntime as PolkaVMLLVMRuntime;
pub use self::polkavm::context::function::r#return::Return as PolkaVMFunctionReturn;
pub use self::polkavm::context::function::runtime::arithmetics::Addition as PolkaVMAdditionFunction;
pub use self::polkavm::context::function::runtime::arithmetics::Division as PolkaVMDivisionFunction;
pub use self::polkavm::context::function::runtime::arithmetics::Remainder as PolkaVMRemainderFunction;
pub use self::polkavm::context::function::runtime::arithmetics::SignedDivision as PolkaVMSignedDivisionFunction;
@@ -7,6 +7,65 @@ use crate::polkavm::context::Context;
use crate::polkavm::Dependency;
use crate::polkavm::WriteLLVM;
/// Implements the division operator according to the EVM specification.
pub struct Addition;
impl<D> RuntimeFunction<D> for Addition
where
D: Dependency + Clone,
{
const NAME: &'static str = "__revive_addition";
fn r#type<'ctx>(context: &Context<'ctx, D>) -> inkwell::types::FunctionType<'ctx> {
context.void_type().fn_type(
&[
context.llvm().ptr_type(Default::default()).into(),
context.llvm().ptr_type(Default::default()).into(),
context.llvm().ptr_type(Default::default()).into(),
],
false,
)
}
fn emit_body<'ctx>(
&self,
context: &mut Context<'ctx, D>,
) -> anyhow::Result<Option<inkwell::values::BasicValueEnum<'ctx>>> {
let result_pointer = Self::paramater(context, 0).into_pointer_value();
let operand_1 = Self::paramater(context, 1).into_pointer_value();
let operand_2 = Self::paramater(context, 2).into_pointer_value();
let operand_1 = context
.builder()
.build_load(context.word_type(), operand_1, "operand_1")?
.into_int_value();
let operand_2 = context
.builder()
.build_load(context.word_type(), operand_2, "operand_2")?
.into_int_value();
let result = context
.builder()
.build_int_add(operand_1, operand_2, "addition_result")?;
context.builder().build_store(result_pointer, result)?;
Ok(None)
}
}
impl<D> WriteLLVM<D> for Addition
where
D: Dependency + Clone,
{
fn declare(&mut self, context: &mut Context<D>) -> anyhow::Result<()> {
<Self as RuntimeFunction<_>>::declare(self, context)
}
fn into_llvm(self, context: &mut Context<D>) -> anyhow::Result<()> {
<Self as RuntimeFunction<_>>::emit(&self, context)
}
}
/// Implements the division operator according to the EVM specification.
pub struct Division;
@@ -5,6 +5,7 @@ use inkwell::values::BasicValue;
use crate::polkavm::context::runtime::RuntimeFunction;
use crate::polkavm::context::Context;
use crate::polkavm::Dependency;
use crate::PolkaVMAdditionFunction;
use crate::PolkaVMDivisionFunction;
use crate::PolkaVMRemainderFunction;
use crate::PolkaVMSignedDivisionFunction;
@@ -13,16 +14,20 @@ use crate::PolkaVMSignedRemainderFunction;
/// Translates the arithmetic addition.
pub fn addition<'ctx, D>(
context: &mut Context<'ctx, D>,
operand_1: inkwell::values::IntValue<'ctx>,
operand_2: inkwell::values::IntValue<'ctx>,
) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>>
binding: inkwell::values::PointerValue<'ctx>,
operand_1: inkwell::values::PointerValue<'ctx>,
operand_2: inkwell::values::PointerValue<'ctx>,
) -> anyhow::Result<()>
where
D: Dependency + Clone,
{
Ok(context
.builder()
.build_int_add(operand_1, operand_2, "addition_result")?
.as_basic_value_enum())
let declaration = <PolkaVMAdditionFunction as RuntimeFunction<D>>::declaration(context);
context.build_call(
declaration,
&[binding.into(), operand_1.into(), operand_2.into()],
"add",
);
Ok(())
}
/// Translates the arithmetic subtraction.
@@ -176,10 +176,11 @@ impl FunctionCall {
let arguments = self.pop_arguments_llvm::<D, 2>(context)?;
revive_llvm_context::polkavm_evm_arithmetic::addition(
context,
arguments[0].into_int_value(),
arguments[1].into_int_value(),
)
.map(Some)
bindings,
arguments[0].into_pointer_value(),
arguments[1].into_pointer_value(),
)?;
Ok(())
}
Name::Sub => {
let arguments = self.pop_arguments_llvm::<D, 2>(context)?;
@@ -998,10 +999,12 @@ impl FunctionCall {
D: revive_llvm_context::PolkaVMDependency + Clone,
{
let mut arguments = Vec::with_capacity(N);
for expression in self.arguments.drain(0..N).rev() {
for (index, expression) in self.arguments.drain(0..N).rev().enumerate() {
let name = format!("arg_{index}");
let pointer = context.build_alloca(context.word_type(), &name);
arguments.push(
expression
.into_llvm(context)?
.into_llvm(&[(name, pointer)], context)?
.expect("Always exists")
.access(context)?,
);
@@ -1020,8 +1023,14 @@ impl FunctionCall {
D: revive_llvm_context::PolkaVMDependency + Clone,
{
let mut arguments = Vec::with_capacity(N);
for expression in self.arguments.drain(0..N).rev() {
arguments.push(expression.into_llvm(context)?.expect("Always exists"));
for (index, expression) in self.arguments.drain(0..N).rev().enumerate() {
let name = format!("arg_{index}");
let pointer = context.build_alloca(context.word_type(), &name);
arguments.push(
expression
.into_llvm(&[(name, pointer)], context)?
.expect("Always exists"),
);
}
arguments.reverse();
@@ -204,6 +204,7 @@ where
revive_llvm_context::PolkaVMEventLogFunction::<3>.declare(context)?;
revive_llvm_context::PolkaVMEventLogFunction::<4>.declare(context)?;
revive_llvm_context::PolkaVMAdditionFunction.declare(context)?;
revive_llvm_context::PolkaVMDivisionFunction.declare(context)?;
revive_llvm_context::PolkaVMSignedDivisionFunction.declare(context)?;
revive_llvm_context::PolkaVMRemainderFunction.declare(context)?;
@@ -258,6 +259,7 @@ where
revive_llvm_context::PolkaVMEventLogFunction::<3>.into_llvm(context)?;
revive_llvm_context::PolkaVMEventLogFunction::<4>.into_llvm(context)?;
revive_llvm_context::PolkaVMAdditionFunction.into_llvm(context)?;
revive_llvm_context::PolkaVMDivisionFunction.into_llvm(context)?;
revive_llvm_context::PolkaVMSignedDivisionFunction.into_llvm(context)?;
revive_llvm_context::PolkaVMRemainderFunction.into_llvm(context)?;