From a5fbd545c530bb26a14f0d372d80f7f1ecd39fa3 Mon Sep 17 00:00:00 2001 From: xermicus Date: Wed, 3 Sep 2025 20:17:51 +0200 Subject: [PATCH] rul Signed-off-by: xermicus --- Cargo.lock | 84 ++++++++++++++++++++++-- Cargo.toml | 1 + crates/rul-runtime-library/Cargo.toml | 9 +++ crates/rul-runtime-library/src/config.rs | 1 + crates/rul-runtime-library/src/lib.rs | 17 +++++ crates/rul/Cargo.toml | 14 ++++ crates/rul/src/builder.rs | 80 ++++++++++++++++++++++ crates/rul/src/lib.rs | 5 ++ crates/rul/src/yul_library.rs | 1 + 9 files changed, 207 insertions(+), 5 deletions(-) create mode 100644 crates/rul-runtime-library/Cargo.toml create mode 100644 crates/rul-runtime-library/src/config.rs create mode 100644 crates/rul-runtime-library/src/lib.rs create mode 100644 crates/rul/Cargo.toml create mode 100644 crates/rul/src/builder.rs create mode 100644 crates/rul/src/lib.rs create mode 100644 crates/rul/src/yul_library.rs diff --git a/Cargo.lock b/Cargo.lock index d7522f0..9432051 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6505,8 +6505,8 @@ dependencies = [ "num-integer", "num-traits", "pallet-revive-fixtures", - "pallet-revive-proc-macro", - "pallet-revive-uapi", + "pallet-revive-proc-macro 0.3.0", + "pallet-revive-uapi 0.4.0", "pallet-transaction-payment", "parity-scale-codec", "paste", @@ -6539,7 +6539,7 @@ checksum = "dc1df19ca809f036d6ddf1632039e9db312f92dbe8f9390e6722ad808cd95377" dependencies = [ "anyhow", "cargo_metadata", - "pallet-revive-uapi", + "pallet-revive-uapi 0.4.0", "polkavm-linker 0.21.0", "sp-core", "sp-io", @@ -6558,7 +6558,7 @@ dependencies = [ "pallet-balances", "pallet-message-queue", "pallet-revive", - "pallet-revive-uapi", + "pallet-revive-uapi 0.4.0", "pallet-timestamp", "pallet-xcm", "parity-scale-codec", @@ -6587,6 +6587,17 @@ dependencies = [ "syn 2.0.101", ] +[[package]] +name = "pallet-revive-proc-macro" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "feb9c42c125790dd4bb0132312bb1a9d3a890b4720c7696d636194311f948e36" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.101", +] + [[package]] name = "pallet-revive-uapi" version = "0.4.0" @@ -6594,12 +6605,25 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0cb8f45102c6279f59f55e0051fc6c26b996619d7842800dfaf3a2583459a1c7" dependencies = [ "bitflags 1.3.2", - "pallet-revive-proc-macro", + "pallet-revive-proc-macro 0.3.0", "parity-scale-codec", "polkavm-derive 0.21.0", "scale-info", ] +[[package]] +name = "pallet-revive-uapi" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e340813d94f380bc531d4cd5f28685065a14dbbff87ab23507f72c7d2792b82c" +dependencies = [ + "bitflags 1.3.2", + "pallet-revive-proc-macro 0.4.0", + "parity-scale-codec", + "polkavm-derive 0.27.0", + "scale-info", +] + [[package]] name = "pallet-root-offences" version = "37.0.0" @@ -7927,6 +7951,12 @@ dependencies = [ "polkavm-assembler 0.24.0", ] +[[package]] +name = "polkavm-common" +version = "0.27.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a19805789e7bf778ac5855f6fe9350353f6a1697c2aab9bfb6fc7c831be54fad" + [[package]] name = "polkavm-derive" version = "0.18.0" @@ -7945,6 +7975,15 @@ dependencies = [ "polkavm-derive-impl-macro 0.21.0", ] +[[package]] +name = "polkavm-derive" +version = "0.27.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4eea46a17d87cbf3c0f3f6156f6300f60cec67cf9eaca296c770e0873f8389d6" +dependencies = [ + "polkavm-derive-impl-macro 0.27.0", +] + [[package]] name = "polkavm-derive-impl" version = "0.18.1" @@ -7969,6 +8008,18 @@ dependencies = [ "syn 2.0.101", ] +[[package]] +name = "polkavm-derive-impl" +version = "0.27.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8abdd1210d96b1dda9ac21199ec469448fd628cea102e2ff0e0df1667c4c3b5f" +dependencies = [ + "polkavm-common 0.27.0", + "proc-macro2", + "quote", + "syn 2.0.101", +] + [[package]] name = "polkavm-derive-impl-macro" version = "0.18.0" @@ -7989,6 +8040,16 @@ dependencies = [ "syn 2.0.101", ] +[[package]] +name = "polkavm-derive-impl-macro" +version = "0.27.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a45173d70138aa1879892c50777ed0d8b0c8556f7678372f09fa1d89bbbddb4" +dependencies = [ + "polkavm-derive-impl 0.27.0", + "syn 2.0.101", +] + [[package]] name = "polkavm-disassembler" version = "0.24.0" @@ -8697,6 +8758,19 @@ dependencies = [ "sha3", ] +[[package]] +name = "revive-rul" +version = "0.1.0" +dependencies = [ + "anyhow", + "pallet-revive-uapi 0.7.0", + "polkavm-linker 0.24.0", +] + +[[package]] +name = "revive-rul-runtime-library" +version = "0.1.0" + [[package]] name = "revive-runner" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 5877089..ef509e6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -71,6 +71,7 @@ tar = "0.4" toml = "0.8" assert_cmd = "2.0" assert_fs = "1.1" +pallet-revive-uapi = "0.7.0" # polkadot-sdk and friends codec = { version = "3.7.5", default-features = false, package = "parity-scale-codec" } diff --git a/crates/rul-runtime-library/Cargo.toml b/crates/rul-runtime-library/Cargo.toml new file mode 100644 index 0000000..3385af6 --- /dev/null +++ b/crates/rul-runtime-library/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "revive-rul-runtime-library" +version.workspace = true +license.workspace = true +edition.workspace = true +repository.workspace = true +authors.workspace = true +description = "revive compiler rust backend runtime library" + diff --git a/crates/rul-runtime-library/src/config.rs b/crates/rul-runtime-library/src/config.rs new file mode 100644 index 0000000..bf11569 --- /dev/null +++ b/crates/rul-runtime-library/src/config.rs @@ -0,0 +1 @@ +//! The revive Rust backend contract runtime library configuration. diff --git a/crates/rul-runtime-library/src/lib.rs b/crates/rul-runtime-library/src/lib.rs new file mode 100644 index 0000000..03b8e19 --- /dev/null +++ b/crates/rul-runtime-library/src/lib.rs @@ -0,0 +1,17 @@ +//! The revive Rust backend contract runtime library. + +#![no_std] + +pub const EVM_WORD_SIZE_BYTES: usize = 32; + +/// The emulated linear EVM heap memory size. +pub const MEMORY_SIZE: usize = 1024 * 64; + +/// The emulated linear EVM heap memory size. +pub const MEMORY: [u8; MEMORY_SIZE] = [0; MEMORY_SIZE]; + +pub struct Function { + pub variables: [u8; VARIABLES], +} + +impl Function {} diff --git a/crates/rul/Cargo.toml b/crates/rul/Cargo.toml new file mode 100644 index 0000000..34df0f6 --- /dev/null +++ b/crates/rul/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "revive-rul" +version.workspace = true +license.workspace = true +edition.workspace = true +repository.workspace = true +authors.workspace = true +description = "revive compiler rust backend" + +[dependencies] +anyhow = { workspace = true } +pallet-revive-uapi = { workspace = true } +polkavm-linker = { workspace = true } + diff --git a/crates/rul/src/builder.rs b/crates/rul/src/builder.rs new file mode 100644 index 0000000..5a247a9 --- /dev/null +++ b/crates/rul/src/builder.rs @@ -0,0 +1,80 @@ +//! The revive rust backend builder module. + +const CARGO_TOML: &str = r#" +[package] +name = "contracts" +publish = false +version = "1.0.0" +edition = "2021" + +# Make sure this is not included into the workspace +[workspace] + +# Binary targets are injected dynamically by the build script. +[[bin]] + +# All paths are injected dynamically by the build script. +[dependencies] +uapi = { version = "0.7.0", package = 'pallet-revive-uapi', features = ["unstable-hostfn"], default-features = false } +hex-literal = { version = "0.4.1", default-features = false } +polkavm-derive = { version = "0.27.0" } + +[profile.release] +opt-level = 3 +lto = true +codegen-units = 1 +"#; + +const HEADER: &str = r#" +#![no_std] +#![no_main] +include!("../panic_handler.rs"); + +use uapi::{HostFn, HostFnImpl as api, ReturnFlags}; + +#[panic_handler] +fn panic(_info: &core::panic::PanicInfo) -> ! { + // SAFETY: The unimp instruction is guaranteed to trap + unsafe { + core::arch::asm!("unimp"); + core::hint::unreachable_unchecked(); + } +} + +/// The emulated linear EVM heap memory size. +pub const MEMORY_SIZE: usize = 1024 * 64; + +/// The emulated linear EVM heap memory size. +pub const MEMORY: [u8; MEMORY_SIZE] = [0; MEMORY_SIZE]; +"#; + +const EXPORT_FUNCTION: &str = r#" +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn +"#; + +fn emit(constructor_code: &str, runtime_code: &str) -> String { + let mut buffer = String::from(HEADER); + buffer.reserve( + constructor_code.len() + runtime_code.len() + HEADER.len() + EXPORT_FUNCTION.len() * 2, + ); + + buffer.push_str(EXPORT_FUNCTION); + buffer.push_str("deploy() {"); + buffer.push_str(constructor_code); + buffer.push_str("\n}"); + + buffer.push_str(EXPORT_FUNCTION); + buffer.push_str("call() {"); + buffer.push_str(runtime_code); + buffer.push_str("\n}"); + + buffer +} + +/// Build a PVM blob. +pub fn build(constructor_code: &str, runtime_code: &str) -> Vec { + let code = emit(constructor_code, runtime_code); + todo!(); +} diff --git a/crates/rul/src/lib.rs b/crates/rul/src/lib.rs new file mode 100644 index 0000000..489ea7d --- /dev/null +++ b/crates/rul/src/lib.rs @@ -0,0 +1,5 @@ +//! The revive rust backend library. + +pub mod builder; +pub mod yul_library; + diff --git a/crates/rul/src/yul_library.rs b/crates/rul/src/yul_library.rs new file mode 100644 index 0000000..976c1b0 --- /dev/null +++ b/crates/rul/src/yul_library.rs @@ -0,0 +1 @@ +//! The revive rust backend YUL auxilliary library functions module.