mirror of
https://github.com/pezkuwichain/revive.git
synced 2026-06-12 17:31:02 +00:00
wip evm builtins
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
This commit is contained in:
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user