Skip to content

Commit

Permalink
chore: init repo
Browse files Browse the repository at this point in the history
  • Loading branch information
0xLucqs committed Jul 17, 2024
0 parents commit e67c1bd
Show file tree
Hide file tree
Showing 8 changed files with 245 additions and 0 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
on: [push, pull_request]

name: Test

env:
jobs:
test:
name: Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install LLVM 18
run: |
wget https://github.com/llvm/llvm-project/releases/download/llvmorg-18.1.8/clang+llvm-18.1.8-x86_64-linux-gnu-ubuntu-18.04.tar.xz
tar -xvf clang+llvm-18.1.8-x86_64-linux-gnu-ubuntu-18.04.tar.xz
mv clang+llvm-18.1.8-x86_64-linux-gnu-ubuntu-18.04 /llvm-18
- name: ls llvm de merde
run: ls /llvm-18
- name: Install toolchain
run: rustup show

- name: Run cargo fmt
run: cargo fmt -- --check

- name: Run cargo clippy
run: >
LLVM_SYS_180_PREFIX="/llvm-18"
cargo clippy --all-features -- -D warnings

2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target
*.ll
150 changes: 150 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "llvm-to-cairo"
version = "0.1.0"
edition = "2021"

[dependencies]
inkwell = {git = "https://github.com/TheDan64/inkwell", features = ["llvm18-0"]}
4 changes: 4 additions & 0 deletions examples/add/add.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[no_mangle]
pub fn add(left: usize, right: usize) -> usize {
left + right
}
4 changes: 4 additions & 0 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[toolchain]
channel = "1.79.0"
components = ["rustfmt", "clippy"]
profile = "minimal"
23 changes: 23 additions & 0 deletions scripts/generate_llvm.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash

# Check if a file is provided
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <file_name>"
exit 1
fi

# Get the Rust file from the first argument
rust_file="examples/$1/$1.rs"
out_file="examples/$1/$1.ll"

# Check if the file exists
if [ ! -f "$rust_file" ]; then
echo "File not found: $rust_file"
exit 1
fi


# Compile the Rust file to LLVM IR
rustc --crate-type=lib -C opt-level=3 --emit=llvm-ir "$rust_file" -o $out_file

echo "LLVM IR generated and saved to $out_file"
25 changes: 25 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use std::path::Path;

use inkwell::{context::Context, memory_buffer::MemoryBuffer};

pub fn compile(path: &str) {
// Initialize LLVM context
let context = Context::create();
// Parse the LLVM IR
let module = context
.create_module_from_ir(
MemoryBuffer::create_from_file(Path::new(path)).expect("Failed to load llvm file"),
)
.expect("Failed to parse LLVM IR");
println!("Compiling LLVM IR {}", module.to_string());
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_compiles() {
compile("examples/add/add.ll");
}
}

0 comments on commit e67c1bd

Please sign in to comment.