Skip to content

Commit 935e630

Browse files
authored
feat: Basic IR codegen (#11)
* feat(codegen): add LLVM IR generation and JIT execution support * feat(codegen): enhance LLVM IR generation with type system and JIT execution * feat(codegen): implement bitwise operations and loop constructs * feat(codegen): add variable type tracking for proper type-aware loads * feat(codegen): add comprehensive test suite and module declarations Add comprehensive_test.rs and test.rs files with extensive test coverage. Declare test modules in codegen.rs to organize test structure. * feat(codegen): add modulo operator and type conversion support Add modulo operator implementation for both integer and floating-point types. Support mixed int/float operations through automatic type conversion. Improve return type handling for boolean values. * feat(codegen): add boolean-to-float conversion for arithmetic operations * style: format code * refactor: Improve IR generation error handling and code structure This commit standardizes error handling in by refactoring long, chained error maps into more readable blocks. It also removes internal test modules to simplify the build structure. * **IR Generation:** Refactors complex LLVM builder calls and conversions (like float/int/bool promotions, bitwise operations) to use dedicated code blocks, improving readability and maintainability of error messages. * **Code Structure:** Removes and modules from , aligning with the typical Rust practice of placing tests directly within the module being tested or in a dedicated directory. * **Minor Cleanup:** Removes unused variable assignment in . * refactor(tests): extract common test helpers to shared module * refactor(parser): rename parse_program to parse_gml and mark visitors as WIP * refactor(codegen): modularize IR generator implementation * feat(codegen): add terminator-aware control flow handling Add early termination checks in statement processing to skip remaining statements when a block already has a terminator. Improve control flow handling in if statements and blocks by checking for existing terminators before adding branches. * refactor(codegen): reorganize module structure and enhance type mapping * chore(ci): delete rust beta * chore(ci): add LLVM 18 installation to CI workflow * chore(ci): switch test to beta channel * test: update codegen test to use std::f64::consts::TAU * chore(ci): add LLVM 18 installation to GitHub workflows * chore(ci): add libpolly-18-dev to LLVM dependencies * Fix(CI): Allow LLVM v18 downgrade on Windows build The 'windows-latest' runner pre-installs a newer LLVM version (e.g., v20), causing the 'choco install llvm --version=18.1.8' step to fail. Added '--allow-downgrade' to the Chocolatey command to force the installation of the required LLVM v18.1.8. * ci: set explicit clang path for macos build * feat: Implement macOS Universal 2 Binary build in CI
1 parent fa728a2 commit 935e630

35 files changed

Lines changed: 4598 additions & 171 deletions

.github/workflows/ci.yml

Lines changed: 67 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,32 @@ jobs:
2222
- nightly
2323
steps:
2424
- uses: actions/checkout@v5
25+
26+
- name: Install LLVM 18
27+
run: |
28+
wget https://apt.llvm.org/llvm.sh
29+
chmod +x llvm.sh
30+
sudo ./llvm.sh 18
31+
sudo apt-get install -y llvm-18-dev libclang-18-dev libpolly-18-dev
32+
echo "LLVM_SYS_180_PREFIX=/usr/lib/llvm-18" >> $GITHUB_ENV
2533
2634
- name: Install Rust
2735
uses: dtolnay/rust-toolchain@master
2836
with:
2937
toolchain: ${{ matrix.rust }}
30-
38+
3139
- name: Cache cargo registry
3240
uses: actions/cache@v4
3341
with:
3442
path: ~/.cargo/registry
3543
key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}
36-
44+
3745
- name: Cache cargo index
3846
uses: actions/cache@v4
3947
with:
4048
path: ~/.cargo/git
4149
key: ${{ runner.os }}-cargo-index-${{ hashFiles('**/Cargo.lock') }}
42-
50+
4351
- name: Cache cargo build
4452
uses: actions/cache@v4
4553
with:
@@ -49,18 +57,19 @@ jobs:
4957
- name: Check formatting
5058
run: cargo fmt --all -- --check
5159
if: matrix.rust == 'stable'
52-
60+
5361
- name: Run clippy
5462
run: cargo clippy --all-targets --all-features
55-
# -- -D warnings
5663
if: matrix.rust == 'stable'
57-
64+
5865
- name: Build
5966
run: cargo build --verbose
60-
67+
6168
- name: Run tests
6269
run: cargo test --verbose
6370

71+
---
72+
6473
build:
6574
name: Build
6675
needs: test
@@ -79,50 +88,90 @@ jobs:
7988
asset_name: col-windows-x86_64.exe
8089
- os: macos-latest
8190
target: x86_64-apple-darwin
91+
arm_target: aarch64-apple-darwin
8292
artifact_name: col
83-
asset_name: col-macos-x86_64
93+
asset_name: col-macos-universal
8494

8595
steps:
8696
- uses: actions/checkout@v5
97+
98+
- name: Install LLVM 18 (Ubuntu)
99+
if: matrix.os == 'ubuntu-latest'
100+
run: |
101+
wget https://apt.llvm.org/llvm.sh
102+
chmod +x llvm.sh
103+
sudo ./llvm.sh 18
104+
sudo apt-get install -y llvm-18-dev libclang-18-dev libpolly-18-dev
105+
echo "LLVM_SYS_180_PREFIX=/usr/lib/llvm-18" >> $GITHUB_ENV
106+
107+
- name: Install LLVM 18 & Rust Targets (macOS)
108+
if: matrix.os == 'macos-latest'
109+
run: |
110+
brew install llvm@18
111+
LLVM_PATH=$(brew --prefix llvm@18)
112+
echo "LLVM_SYS_180_PREFIX=$LLVM_PATH" >> $GITHUB_ENV
113+
rustup target add x86_64-apple-darwin aarch64-apple-darwin
114+
echo "CC=$LLVM_PATH/bin/clang" >> $GITHUB_ENV
115+
echo "CXX=$LLVM_PATH/bin/clang++" >> $GITHUB_ENV
116+
117+
- name: Install LLVM 18 (Windows)
118+
if: matrix.os == 'windows-latest'
119+
run: |
120+
choco install llvm --version=18.1.8 --allow-downgrade
121+
echo "LLVM_SYS_180_PREFIX=C:\Program Files\LLVM" >> $env:GITHUB_ENV
87122
88123
- name: Install Rust
89124
uses: dtolnay/rust-toolchain@stable
90125
with:
91-
targets: ${{ matrix.target }}
92-
126+
targets: ${{ matrix.os != 'macos-latest' && matrix.target || '' }}
127+
93128
- name: Cache cargo registry
94129
uses: actions/cache@v4
95130
with:
96131
path: ~/.cargo/registry
97132
key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}
98-
133+
99134
- name: Cache cargo index
100135
uses: actions/cache@v4
101136
with:
102137
path: ~/.cargo/git
103138
key: ${{ runner.os }}-cargo-index-${{ hashFiles('**/Cargo.lock') }}
104-
139+
105140
- name: Cache cargo build
106141
uses: actions/cache@v4
107142
with:
108143
path: target
109144
key: ${{ runner.os }}-cargo-build-target-${{ hashFiles('**/Cargo.lock') }}
110145

111-
- name: Build release
146+
- name: Build release (Linux/Windows)
147+
if: matrix.os != 'macos-latest'
112148
run: cargo build --release --target ${{ matrix.target }}
113-
149+
150+
- name: Build and Combine (macOS Universal Binary)
151+
if: matrix.os == 'macos-latest'
152+
run: |
153+
cargo build --release --target ${{ matrix.target }}
154+
cargo build --release --target ${{ matrix.arm_target }}
155+
lipo -create \
156+
target/${{ matrix.target }}/release/${{ matrix.artifact_name }} \
157+
target/${{ matrix.arm_target }}/release/${{ matrix.artifact_name }} \
158+
-output target/release/${{ matrix.asset_name }}
159+
echo "FINAL_ARTIFACT_PATH=target/release/${{ matrix.asset_name }}" >> $GITHUB_ENV
160+
echo "FINAL_ARTIFACT_NAME=${{ matrix.asset_name }}" >> $GITHUB_ENV
161+
114162
- name: Upload artifact
115163
uses: actions/upload-artifact@v4
116164
with:
117-
name: ${{ matrix.asset_name }}
118-
path: target/${{ matrix.target }}/release/${{ matrix.artifact_name }}
165+
name: ${{ env.FINAL_ARTIFACT_NAME || matrix.asset_name }}
166+
path: ${{ env.FINAL_ARTIFACT_PATH || format('target/{0}/release/{1}', matrix.target, matrix.artifact_name) }}
167+
168+
---
119169

120170
security:
121171
name: Security Audit
122172
runs-on: ubuntu-latest
123173
if: github.event.pull_request.draft == false
124174
steps:
125-
- uses: actions/checkout@v5
126175
- uses: rustsec/audit-check@v1.4.1
127176
with:
128-
token: ${{ secrets.GITHUB_TOKEN }}
177+
token: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/coverage.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ jobs:
1717
steps:
1818
- uses: actions/checkout@v5
1919

20+
- name: Install LLVM 18
21+
run: |
22+
wget https://apt.llvm.org/llvm.sh
23+
chmod +x llvm.sh
24+
sudo ./llvm.sh 18
25+
sudo apt-get install -y llvm-18-dev libclang-18-dev libpolly-18-dev
26+
echo "LLVM_SYS_180_PREFIX=/usr/lib/llvm-18" >> $GITHUB_ENV
27+
2028
- name: Install Rust
2129
uses: dtolnay/rust-toolchain@stable
2230
with:

.github/workflows/release.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,28 @@ jobs:
6868
steps:
6969
- uses: actions/checkout@v5
7070

71+
- name: Install LLVM 18 (Ubuntu)
72+
if: matrix.os == 'ubuntu-latest'
73+
run: |
74+
wget https://apt.llvm.org/llvm.sh
75+
chmod +x llvm.sh
76+
sudo ./llvm.sh 18
77+
sudo apt-get install -y llvm-18-dev libclang-18-dev libpolly-18-dev
78+
echo "LLVM_SYS_180_PREFIX=/usr/lib/llvm-18" >> $GITHUB_ENV
79+
80+
- name: Install LLVM 18 (macOS)
81+
if: matrix.os == 'macos-latest'
82+
run: |
83+
brew install llvm@18
84+
echo "/opt/homebrew/opt/llvm@18/bin" >> $GITHUB_PATH
85+
echo "LLVM_SYS_180_PREFIX=/opt/homebrew/opt/llvm@18" >> $GITHUB_ENV
86+
87+
- name: Install LLVM 18 (Windows)
88+
if: matrix.os == 'windows-latest'
89+
run: |
90+
choco install llvm --version=18.1.8
91+
echo "LLVM_SYS_180_PREFIX=C:\Program Files\LLVM" >> $env:GITHUB_ENV
92+
7193
- name: Install Rust
7294
uses: dtolnay/rust-toolchain@stable
7395
with:
@@ -110,6 +132,14 @@ jobs:
110132
steps:
111133
- uses: actions/checkout@v5
112134

135+
- name: Install LLVM 18
136+
run: |
137+
wget https://apt.llvm.org/llvm.sh
138+
chmod +x llvm.sh
139+
sudo ./llvm.sh 18
140+
sudo apt-get install -y llvm-18-dev libclang-18-dev libpolly-18-dev
141+
echo "LLVM_SYS_180_PREFIX=/usr/lib/llvm-18" >> $GITHUB_ENV
142+
113143
- name: Install Rust
114144
uses: dtolnay/rust-toolchain@stable
115145

.github/workflows/update-deps.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ jobs:
1515
with:
1616
token: ${{ secrets.GITHUB_TOKEN }}
1717

18+
- name: Install LLVM 18
19+
run: |
20+
wget https://apt.llvm.org/llvm.sh
21+
chmod +x llvm.sh
22+
sudo ./llvm.sh 18
23+
sudo apt-get install -y llvm-18-dev libclang-18-dev libpolly-18-dev
24+
echo "LLVM_SYS_180_PREFIX=/usr/lib/llvm-18" >> $GITHUB_ENV
25+
1826
- name: Install Rust
1927
uses: dtolnay/rust-toolchain@stable
2028

Cargo.lock

Lines changed: 84 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ ariadne = "0.5.1"
88
chumsky = "0.11.1"
99
logos = "0.15.1"
1010
owo-colors = "4.2.3"
11+
12+
inkwell = { version = "0.6.0", features = ["llvm18-1"] }

0 commit comments

Comments
 (0)