diff --git a/Cargo.lock b/Cargo.lock index 47cdc93..4a35a17 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -300,6 +300,10 @@ dependencies = [ "os_str_bytes", ] +[[package]] +name = "compiler-builtins" +version = "0.1.0" + [[package]] name = "core-foundation-sys" version = "0.8.6" @@ -546,16 +550,6 @@ dependencies = [ "hashbrown 0.14.3", ] -[[package]] -name = "ir-tac" -version = "0.1.0" -dependencies = [ - "evmil", - "indexmap 2.1.0", - "petgraph", - "primitive-types", -] - [[package]] name = "itertools" version = "0.10.5" @@ -567,9 +561,9 @@ dependencies = [ [[package]] name = "itoa" -version = "1.0.9" +version = "1.0.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38" +checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c" [[package]] name = "js-sys" @@ -604,6 +598,14 @@ version = "0.5.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f" +[[package]] +name = "lld-sys" +version = "0.1.0" +dependencies = [ + "cc", + "libc", +] + [[package]] name = "lock_api" version = "0.4.11" @@ -694,9 +696,9 @@ dependencies = [ [[package]] name = "once_cell" -version = "1.18.0" +version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" +checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" [[package]] name = "ordered-float" @@ -808,10 +810,11 @@ dependencies = [ [[package]] name = "proc-macro-crate" -version = "2.0.0" +version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7e8366a6159044a37876a2b9817124296703c586a5c92e2c53751fa06d8d43e8" +checksum = "97dc5fea232fc28d2f597b37c4876b348a40e33f3b02cc975c8d006d78d94b1a" dependencies = [ + "toml_datetime", "toml_edit", ] @@ -934,14 +937,32 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" [[package]] -name = "revive" +name = "revive-cli" version = "0.1.0" dependencies = [ "evmil", "hex", - "ir-tac", + "revive-ir", ] +[[package]] +name = "revive-environment" +version = "0.1.0" + +[[package]] +name = "revive-ir" +version = "0.1.0" +dependencies = [ + "evmil", + "indexmap 2.1.0", + "petgraph", + "primitive-types", +] + +[[package]] +name = "revive-polkavm" +version = "0.1.0" + [[package]] name = "rlp" version = "0.5.2" @@ -1197,15 +1218,15 @@ dependencies = [ [[package]] name = "toml_datetime" -version = "0.6.5" +version = "0.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3550f4e9685620ac18a50ed434eb3aec30db8ba93b0287467bca5826ea25baf1" +checksum = "7cda73e2f1397b1262d6dfdcef8aafae14d1de7748d66822d3bfeeb6d03e5e4b" [[package]] name = "toml_edit" -version = "0.20.7" +version = "0.20.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70f427fce4d84c72b5b732388bf4a9f4531b53f74e2887e3ecb2481f68f66d81" +checksum = "396e4d48bbb2b7554c944bde63101b5ae446cff6ec4a24227428f15eb72ef338" dependencies = [ "indexmap 2.1.0", "toml_datetime", @@ -1437,9 +1458,9 @@ checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" [[package]] name = "winnow" -version = "0.5.19" +version = "0.5.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "829846f3e3db426d4cee4510841b71a8e58aa2a76b1132579487ae430ccd9c7b" +checksum = "b67b5f0a4e7a27a64c651977932b9dc5667ca7fc31ac44b03ed37a0cf42fdfff" dependencies = [ "memchr", ] diff --git a/Cargo.toml b/Cargo.toml index 56ca25e..bf27474 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,14 +1,13 @@ [workspace] resolver = "2" - -members = [ - "crates/ir-tac", - "crates/cli", -] +members = ["crates/*"] [workspace.dependencies] evmil = "0.4" hex = "0.4" petgraph = "0.6" primitive-types = "0.12" -indexmap = "2.1.0" \ No newline at end of file +indexmap = "2.1.0" +inkwell = { version = "0.2.0", features = ["target-riscv", "no-libffi-linking", "llvm16-0"] } +cc = "1.0" +libc = "0.2" diff --git a/crates/builtins/Cargo.toml b/crates/builtins/Cargo.toml new file mode 100644 index 0000000..605b68e --- /dev/null +++ b/crates/builtins/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "compiler-builtins" +version = "0.1.0" +edition = "2021" +build = "build.rs" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/crates/builtins/build.rs b/crates/builtins/build.rs new file mode 100644 index 0000000..d05bfe4 --- /dev/null +++ b/crates/builtins/build.rs @@ -0,0 +1,31 @@ +use std::{env, fs, io::Read, path::Path, process::Command}; + +fn main() { + let lib = "libclang_rt.builtins-riscv32.a"; + let mut llvm_lib_dir = String::new(); + + Command::new("llvm-config") + .args(["--libdir"]) + .output() + .expect("llvm-config should be able to provide LD path") + .stdout + .as_slice() + .read_to_string(&mut llvm_lib_dir) + .expect("llvm-config output should be utf8"); + + let lib_path = std::path::PathBuf::from(llvm_lib_dir.trim()) + .join("linux") + .join(lib); + let archive = fs::read(lib_path).expect("clang builtins for riscv32 not fonud"); + + let out_dir = env::var_os("OUT_DIR").expect("has OUT_DIR"); + let archive_path = Path::new(&out_dir).join(lib); + let len = archive.len(); + std::fs::write(archive_path, &archive).expect("can write to OUT_DIR"); + + let src_path = Path::new(&out_dir).join("compiler_rt.rs"); + let src = format!("pub static COMPILER_RT: &[u8; {len}] = include_bytes!(\"{lib}\");"); + fs::write(src_path, src).expect("can write to OUT_DIR"); + + println!("cargo:rerun-if-changed=build.rs"); +} diff --git a/crates/builtins/src/lib.rs b/crates/builtins/src/lib.rs new file mode 100644 index 0000000..f7b811e --- /dev/null +++ b/crates/builtins/src/lib.rs @@ -0,0 +1 @@ +include!(concat!(env!("OUT_DIR"), "/compiler_rt.rs")); diff --git a/crates/cli/Cargo.toml b/crates/cli/Cargo.toml index 980e2f6..24830a6 100644 --- a/crates/cli/Cargo.toml +++ b/crates/cli/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "revive" +name = "revive-cli" version = "0.1.0" edition = "2021" @@ -8,4 +8,4 @@ edition = "2021" [dependencies] hex = { workspace = true } evmil = { workspace = true } -ir-tac = { path = "../ir-tac" } \ No newline at end of file +revive-ir = { path = "../ir" } \ No newline at end of file diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index 5dc5cab..a1af99f 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -1,5 +1,5 @@ use evmil::bytecode::Disassemble; -use ir_tac::cfg::{BasicBlockFormatOption, Program}; +use revive_ir::cfg::{BasicBlockFormatOption, Program}; fn main() { let hexcode = std::fs::read_to_string(std::env::args().nth(1).unwrap()).unwrap(); diff --git a/crates/environment/Cargo.toml b/crates/environment/Cargo.toml new file mode 100644 index 0000000..5fbb07e --- /dev/null +++ b/crates/environment/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "revive-environment" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/crates/environment/src/lib.rs b/crates/environment/src/lib.rs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/crates/environment/src/lib.rs @@ -0,0 +1 @@ + diff --git a/crates/ir-tac/Cargo.toml b/crates/ir/Cargo.toml similarity index 93% rename from crates/ir-tac/Cargo.toml rename to crates/ir/Cargo.toml index 7e54269..924b0c1 100644 --- a/crates/ir-tac/Cargo.toml +++ b/crates/ir/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "ir-tac" +name = "revive-ir" version = "0.1.0" edition = "2021" diff --git a/crates/ir-tac/src/address.rs b/crates/ir/src/address.rs similarity index 100% rename from crates/ir-tac/src/address.rs rename to crates/ir/src/address.rs diff --git a/crates/ir-tac/src/cfg.rs b/crates/ir/src/cfg.rs similarity index 100% rename from crates/ir-tac/src/cfg.rs rename to crates/ir/src/cfg.rs diff --git a/crates/ir-tac/src/instruction.rs b/crates/ir/src/instruction.rs similarity index 100% rename from crates/ir-tac/src/instruction.rs rename to crates/ir/src/instruction.rs diff --git a/crates/ir-tac/src/lib.rs b/crates/ir/src/lib.rs similarity index 100% rename from crates/ir-tac/src/lib.rs rename to crates/ir/src/lib.rs diff --git a/crates/ir-tac/src/symbol.rs b/crates/ir/src/symbol.rs similarity index 100% rename from crates/ir-tac/src/symbol.rs rename to crates/ir/src/symbol.rs diff --git a/crates/lld-sys/Cargo.toml b/crates/lld-sys/Cargo.toml new file mode 100644 index 0000000..6a742ec --- /dev/null +++ b/crates/lld-sys/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "lld-sys" +version = "0.1.0" +edition = "2021" +build = "build.rs" + + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +libc = { workspace = true } + +[build-dependencies] +cc = { workspace = true } \ No newline at end of file diff --git a/crates/lld-sys/build.rs b/crates/lld-sys/build.rs new file mode 100644 index 0000000..ddd234b --- /dev/null +++ b/crates/lld-sys/build.rs @@ -0,0 +1,25 @@ +// SPDX-License-Identifier: Apache-2.0 + +use std::{io::Read, process::Command}; + +fn main() { + let mut flags = String::new(); + Command::new("llvm-config") + .args(["--cxxflags"]) + .output() + .expect("llvm-config should be able to provide CXX flags") + .stdout + .as_slice() + .read_to_string(&mut flags) + .expect("llvm-config output should be utf8"); + + let mut builder = cc::Build::new(); + flags + .split_whitespace() + .fold(&mut builder, |builder, flag| builder.flag(flag)) + .cpp(true) + .file("src/linker.cpp") + .compile("liblinker.a"); + + println!("cargo:rerun-if-changed=build.rs"); +} diff --git a/crates/lld-sys/src/lib.rs b/crates/lld-sys/src/lib.rs new file mode 100644 index 0000000..0418e19 --- /dev/null +++ b/crates/lld-sys/src/lib.rs @@ -0,0 +1,3 @@ +extern "C" { + pub fn LLDELFLink(args: *const *const libc::c_char, size: libc::size_t) -> libc::c_int; +} diff --git a/crates/lld-sys/src/linker.cpp b/crates/lld-sys/src/linker.cpp new file mode 100644 index 0000000..05a5f5a --- /dev/null +++ b/crates/lld-sys/src/linker.cpp @@ -0,0 +1,22 @@ +// SPDX-License-Identifier: Apache-2.0 + +#include "lld/Common/Driver.h" +#include "lld/Common/CommonLinkerContext.h" +#include "llvm/Support/CrashRecoveryContext.h" + +extern "C" bool LLDELFLink(const char *argv[], size_t length) +{ + bool canRunAgain; + + { + llvm::ArrayRef args(argv, length); + llvm::CrashRecoveryContext crc; + if (!crc.RunSafely([&]() + { canRunAgain = lld::elf::link(args, llvm::outs(), llvm::errs(), false, false); })) + return false; + } + + llvm::CrashRecoveryContext crc; + return canRunAgain && crc.RunSafely([&]() + { lld::CommonLinkerContext::destroy(); }); +} \ No newline at end of file diff --git a/crates/polkavm/Cargo.toml b/crates/polkavm/Cargo.toml new file mode 100644 index 0000000..185470b --- /dev/null +++ b/crates/polkavm/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "revive-polkavm" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/crates/polkavm/src/lib.rs b/crates/polkavm/src/lib.rs new file mode 100644 index 0000000..7d12d9a --- /dev/null +++ b/crates/polkavm/src/lib.rs @@ -0,0 +1,14 @@ +pub fn add(left: usize, right: usize) -> usize { + left + right +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn it_works() { + let result = add(2, 2); + assert_eq!(result, 4); + } +} diff --git a/testdata/unbalanced.bin b/testdata/unbalanced.bin new file mode 100644 index 0000000..bf965a1 --- /dev/null +++ b/testdata/unbalanced.bin @@ -0,0 +1 @@ +602060006000376020600060003561001b5760ff602052602060205bf3