Compare commits

..

1 Commits

Author SHA1 Message Date
Cyrill Leutwiler 1a36630900 release resolc v0.3.0
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
2025-06-28 12:16:23 +02:00
5 changed files with 72 additions and 180 deletions
@@ -2,13 +2,13 @@
use inkwell::values::BasicValue; use inkwell::values::BasicValue;
//use crate::polkavm::context::runtime::RuntimeFunction; use crate::polkavm::context::runtime::RuntimeFunction;
use crate::polkavm::context::Context; use crate::polkavm::context::Context;
use crate::polkavm::Dependency; use crate::polkavm::Dependency;
//use crate::PolkaVMDivisionFunction; use crate::PolkaVMDivisionFunction;
//use crate::PolkaVMRemainderFunction; use crate::PolkaVMRemainderFunction;
//use crate::PolkaVMSignedDivisionFunction; use crate::PolkaVMSignedDivisionFunction;
//use crate::PolkaVMSignedRemainderFunction; use crate::PolkaVMSignedRemainderFunction;
/// Translates the arithmetic addition. /// Translates the arithmetic addition.
pub fn addition<'ctx, D>( pub fn addition<'ctx, D>(
@@ -64,21 +64,11 @@ pub fn division<'ctx, D>(
where where
D: Dependency + Clone, D: Dependency + Clone,
{ {
let result_pointer = context.build_alloca_at_entry(context.word_type(), "div_result_pointer"); let name = <PolkaVMDivisionFunction as RuntimeFunction<D>>::NAME;
let operand_1_pointer = context.build_alloca_at_entry(context.word_type(), "operand_1_pointer"); let declaration = <PolkaVMDivisionFunction as RuntimeFunction<D>>::declaration(context);
let operand_2_pointer = context.build_alloca_at_entry(context.word_type(), "operand_2_pointer"); Ok(context
.build_call(declaration, &[operand_1.into(), operand_2.into()], "div")
context.build_store(operand_1_pointer, operand_1)?; .unwrap_or_else(|| panic!("revive runtime function {name} should return a value",)))
context.build_store(operand_2_pointer, operand_2)?;
let arguments = &[
result_pointer.to_int(context).into(),
operand_1_pointer.to_int(context).into(),
operand_2_pointer.to_int(context).into(),
];
context.build_runtime_call(revive_runtime_api::polkavm_imports::DIV, arguments);
context.build_load(result_pointer, "div_result")
} }
/// Translates the arithmetic remainder. /// Translates the arithmetic remainder.
@@ -90,21 +80,11 @@ pub fn remainder<'ctx, D>(
where where
D: Dependency + Clone, D: Dependency + Clone,
{ {
let result_pointer = context.build_alloca_at_entry(context.word_type(), "rem_result_pointer"); let name = <PolkaVMRemainderFunction as RuntimeFunction<D>>::NAME;
let operand_1_pointer = context.build_alloca_at_entry(context.word_type(), "operand_1_pointer"); let declaration = <PolkaVMRemainderFunction as RuntimeFunction<D>>::declaration(context);
let operand_2_pointer = context.build_alloca_at_entry(context.word_type(), "operand_2_pointer"); Ok(context
.build_call(declaration, &[operand_1.into(), operand_2.into()], "rem")
context.build_store(operand_1_pointer, operand_1)?; .unwrap_or_else(|| panic!("revive runtime function {name} should return a value",)))
context.build_store(operand_2_pointer, operand_2)?;
let arguments = &[
result_pointer.to_int(context).into(),
operand_1_pointer.to_int(context).into(),
operand_2_pointer.to_int(context).into(),
];
context.build_runtime_call(revive_runtime_api::polkavm_imports::MOD, arguments);
context.build_load(result_pointer, "rem_result")
} }
/// Translates the signed arithmetic division. /// Translates the signed arithmetic division.
@@ -119,21 +99,11 @@ pub fn division_signed<'ctx, D>(
where where
D: Dependency + Clone, D: Dependency + Clone,
{ {
let result_pointer = context.build_alloca_at_entry(context.word_type(), "sdiv_result_pointer"); let name = <PolkaVMSignedDivisionFunction as RuntimeFunction<D>>::NAME;
let operand_1_pointer = context.build_alloca_at_entry(context.word_type(), "operand_1_pointer"); let declaration = <PolkaVMSignedDivisionFunction as RuntimeFunction<D>>::declaration(context);
let operand_2_pointer = context.build_alloca_at_entry(context.word_type(), "operand_2_pointer"); Ok(context
.build_call(declaration, &[operand_1.into(), operand_2.into()], "sdiv")
context.build_store(operand_1_pointer, operand_1)?; .unwrap_or_else(|| panic!("revive runtime function {name} should return a value",)))
context.build_store(operand_2_pointer, operand_2)?;
let arguments = &[
result_pointer.to_int(context).into(),
operand_1_pointer.to_int(context).into(),
operand_2_pointer.to_int(context).into(),
];
context.build_runtime_call(revive_runtime_api::polkavm_imports::SDIV, arguments);
context.build_load(result_pointer, "sdiv_result")
} }
/// Translates the signed arithmetic remainder. /// Translates the signed arithmetic remainder.
@@ -145,19 +115,9 @@ pub fn remainder_signed<'ctx, D>(
where where
D: Dependency + Clone, D: Dependency + Clone,
{ {
let result_pointer = context.build_alloca_at_entry(context.word_type(), "srem_result_pointer"); let name = <PolkaVMSignedRemainderFunction as RuntimeFunction<D>>::NAME;
let operand_1_pointer = context.build_alloca_at_entry(context.word_type(), "operand_1_pointer"); let declaration = <PolkaVMSignedRemainderFunction as RuntimeFunction<D>>::declaration(context);
let operand_2_pointer = context.build_alloca_at_entry(context.word_type(), "operand_2_pointer"); Ok(context
.build_call(declaration, &[operand_1.into(), operand_2.into()], "srem")
context.build_store(operand_1_pointer, operand_1)?; .unwrap_or_else(|| panic!("revive runtime function {name} should return a value",)))
context.build_store(operand_2_pointer, operand_2)?;
let arguments = &[
result_pointer.to_int(context).into(),
operand_1_pointer.to_int(context).into(),
operand_2_pointer.to_int(context).into(),
];
context.build_runtime_call(revive_runtime_api::polkavm_imports::SMOD, arguments);
context.build_load(result_pointer, "rsem_result")
} }
+38 -72
View File
@@ -1,5 +1,7 @@
//! Translates the mathematical operations. //! Translates the mathematical operations.
use inkwell::values::BasicValue;
use crate::polkavm::context::Context; use crate::polkavm::context::Context;
use crate::polkavm::Dependency; use crate::polkavm::Dependency;
@@ -13,26 +15,17 @@ pub fn add_mod<'ctx, D>(
where where
D: Dependency + Clone, D: Dependency + Clone,
{ {
let result_pointer = Ok(context
context.build_alloca_at_entry(context.word_type(), "addmod_result_pointer"); .build_call(
let operand_1_pointer = context.build_alloca_at_entry(context.word_type(), "addmod_operand_1"); context.llvm_runtime().add_mod,
let operand_2_pointer = context.build_alloca_at_entry(context.word_type(), "addmod_operand_2"); &[
let modulo_pointer = operand_1.as_basic_value_enum(),
context.build_alloca_at_entry(context.word_type(), "addmod_modulo_operand"); operand_2.as_basic_value_enum(),
modulo.as_basic_value_enum(),
context.build_store(operand_2_pointer, operand_2)?; ],
context.build_store(operand_1_pointer, operand_1)?; "add_mod_call",
context.build_store(modulo_pointer, modulo)?; )
.expect("Always exists"))
let arguments = &[
result_pointer.to_int(context).into(),
operand_1_pointer.to_int(context).into(),
operand_2_pointer.to_int(context).into(),
modulo_pointer.to_int(context).into(),
];
context.build_runtime_call(revive_runtime_api::polkavm_imports::ADDMOD, arguments);
context.build_load(result_pointer, "addmod_result")
} }
/// Translates the `mulmod` instruction. /// Translates the `mulmod` instruction.
@@ -45,26 +38,17 @@ pub fn mul_mod<'ctx, D>(
where where
D: Dependency + Clone, D: Dependency + Clone,
{ {
let result_pointer = Ok(context
context.build_alloca_at_entry(context.word_type(), "mulmod_result_pointer"); .build_call(
let operand_1_pointer = context.build_alloca_at_entry(context.word_type(), "mulmod_operand_1"); context.llvm_runtime().mul_mod,
let operand_2_pointer = context.build_alloca_at_entry(context.word_type(), "mulmod_operand_2"); &[
let modulo_pointer = operand_1.as_basic_value_enum(),
context.build_alloca_at_entry(context.word_type(), "mulmod_modulo_operand"); operand_2.as_basic_value_enum(),
modulo.as_basic_value_enum(),
context.build_store(operand_2_pointer, operand_2)?; ],
context.build_store(operand_1_pointer, operand_1)?; "mul_mod_call",
context.build_store(modulo_pointer, modulo)?; )
.expect("Always exists"))
let arguments = &[
result_pointer.to_int(context).into(),
operand_1_pointer.to_int(context).into(),
operand_2_pointer.to_int(context).into(),
modulo_pointer.to_int(context).into(),
];
context.build_runtime_call(revive_runtime_api::polkavm_imports::MULMOD, arguments);
context.build_load(result_pointer, "addmod_result")
} }
/// Translates the `exp` instruction. /// Translates the `exp` instruction.
@@ -76,22 +60,13 @@ pub fn exponent<'ctx, D>(
where where
D: Dependency + Clone, D: Dependency + Clone,
{ {
let result_pointer = context.build_alloca_at_entry(context.word_type(), "exp_result_pointer"); Ok(context
let value_pointer = context.build_alloca_at_entry(context.word_type(), "exp_value_pointer"); .build_call(
let exponent_pointer = context.llvm_runtime().exp,
context.build_alloca_at_entry(context.word_type(), "exp_exponent_pointer"); &[value.as_basic_value_enum(), exponent.as_basic_value_enum()],
"exp_call",
context.build_store(value_pointer, value)?; )
context.build_store(exponent_pointer, exponent)?; .expect("Always exists"))
let arguments = &[
result_pointer.to_int(context).into(),
value_pointer.to_int(context).into(),
exponent_pointer.to_int(context).into(),
];
context.build_runtime_call(revive_runtime_api::polkavm_imports::EXP, arguments);
context.build_load(result_pointer, "exponent_result")
} }
/// Translates the `signextend` instruction. /// Translates the `signextend` instruction.
@@ -103,20 +78,11 @@ pub fn sign_extend<'ctx, D>(
where where
D: Dependency + Clone, D: Dependency + Clone,
{ {
let result_pointer = Ok(context
context.build_alloca_at_entry(context.word_type(), "signext_result_pointer"); .build_call(
let bytes_pointer = context.build_alloca_at_entry(context.word_type(), "bytes_pointer"); context.llvm_runtime().sign_extend,
let value_pointer = context.build_alloca_at_entry(context.word_type(), "signext_value_pointer"); &[bytes.as_basic_value_enum(), value.as_basic_value_enum()],
"sign_extend_call",
context.build_store(bytes_pointer, bytes)?; )
context.build_store(value_pointer, value)?; .expect("Always exists"))
let arguments = &[
result_pointer.to_int(context).into(),
bytes_pointer.to_int(context).into(),
value_pointer.to_int(context).into(),
];
context.build_runtime_call(revive_runtime_api::polkavm_imports::EXP, arguments);
context.build_load(result_pointer, "signext_mod_result")
} }
-16
View File
@@ -104,19 +104,3 @@ POLKAVM_IMPORT(uint64_t, set_storage, uint32_t, uint32_t, uint32_t, uint32_t, ui
POLKAVM_IMPORT(void, value_transferred, uint32_t) POLKAVM_IMPORT(void, value_transferred, uint32_t)
POLKAVM_IMPORT(void, weight_to_fee, uint64_t, uint64_t, uint32_t); POLKAVM_IMPORT(void, weight_to_fee, uint64_t, uint64_t, uint32_t);
POLKAVM_IMPORT(void, div, uint32_t, uint32_t, uint32_t);
POLKAVM_IMPORT(void, sdiv, uint32_t, uint32_t, uint32_t);
POLKAVM_IMPORT(void, addmod, uint32_t, uint32_t, uint32_t, uint32_t);
POLKAVM_IMPORT(void, mulmod, uint32_t, uint32_t, uint32_t, uint32_t);
POLKAVM_IMPORT(void, mod, uint32_t, uint32_t, uint32_t);
POLKAVM_IMPORT(void, smod, uint32_t, uint32_t, uint32_t);
POLKAVM_IMPORT(void, exp, uint32_t, uint32_t, uint32_t);
POLKAVM_IMPORT(void, signext, uint32_t, uint32_t, uint32_t);
+1 -19
View File
@@ -68,27 +68,9 @@ pub static VALUE_TRANSFERRED: &str = "value_transferred";
pub static WEIGHT_TO_FEE: &str = "weight_to_fee"; pub static WEIGHT_TO_FEE: &str = "weight_to_fee";
pub static DIV: &str = "div";
pub static SDIV: &str = "sdiv";
pub static ADDMOD: &str = "addmod";
pub static MULMOD: &str = "mulmod";
pub static MOD: &str = "mod";
pub static SMOD: &str = "smod";
pub static EXP: &str = "exp";
pub static SIGNEXT: &str = "signext";
/// All imported runtime API symbols. /// All imported runtime API symbols.
/// Useful for configuring common attributes and linkage. /// Useful for configuring common attributes and linkage.
pub static IMPORTS: [&str; 41] = [ pub static IMPORTS: [&str; 33] = [
DIV,
SDIV,
ADDMOD,
MULMOD,
MOD,
SMOD,
EXP,
SIGNEXT,
//
ADDRESS, ADDRESS,
BALANCE, BALANCE,
BALANCE_OF, BALANCE_OF,
+8 -8
View File
@@ -204,10 +204,10 @@ where
revive_llvm_context::PolkaVMEventLogFunction::<3>.declare(context)?; revive_llvm_context::PolkaVMEventLogFunction::<3>.declare(context)?;
revive_llvm_context::PolkaVMEventLogFunction::<4>.declare(context)?; revive_llvm_context::PolkaVMEventLogFunction::<4>.declare(context)?;
//revive_llvm_context::PolkaVMDivisionFunction.declare(context)?; revive_llvm_context::PolkaVMDivisionFunction.declare(context)?;
//revive_llvm_context::PolkaVMSignedDivisionFunction.declare(context)?; revive_llvm_context::PolkaVMSignedDivisionFunction.declare(context)?;
//revive_llvm_context::PolkaVMRemainderFunction.declare(context)?; revive_llvm_context::PolkaVMRemainderFunction.declare(context)?;
//revive_llvm_context::PolkaVMSignedRemainderFunction.declare(context)?; revive_llvm_context::PolkaVMSignedRemainderFunction.declare(context)?;
revive_llvm_context::PolkaVMSbrkFunction.declare(context)?; revive_llvm_context::PolkaVMSbrkFunction.declare(context)?;
@@ -258,10 +258,10 @@ where
revive_llvm_context::PolkaVMEventLogFunction::<3>.into_llvm(context)?; revive_llvm_context::PolkaVMEventLogFunction::<3>.into_llvm(context)?;
revive_llvm_context::PolkaVMEventLogFunction::<4>.into_llvm(context)?; revive_llvm_context::PolkaVMEventLogFunction::<4>.into_llvm(context)?;
//revive_llvm_context::PolkaVMDivisionFunction.into_llvm(context)?; revive_llvm_context::PolkaVMDivisionFunction.into_llvm(context)?;
//revive_llvm_context::PolkaVMSignedDivisionFunction.into_llvm(context)?; revive_llvm_context::PolkaVMSignedDivisionFunction.into_llvm(context)?;
//revive_llvm_context::PolkaVMRemainderFunction.into_llvm(context)?; revive_llvm_context::PolkaVMRemainderFunction.into_llvm(context)?;
//revive_llvm_context::PolkaVMSignedRemainderFunction.into_llvm(context)?; revive_llvm_context::PolkaVMSignedRemainderFunction.into_llvm(context)?;
revive_llvm_context::PolkaVMSbrkFunction.into_llvm(context)?; revive_llvm_context::PolkaVMSbrkFunction.into_llvm(context)?;