Skip to content

Commit fbdea63

Browse files
committed
add targte selection and update CI to other OS
1 parent 43c5f69 commit fbdea63

10 files changed

Lines changed: 588 additions & 460 deletions

File tree

.github/workflows/ci.yml

Lines changed: 88 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,63 +8,120 @@ on:
88

99
jobs:
1010
build:
11-
runs-on: windows-latest
11+
strategy:
12+
matrix:
13+
os: [windows-latest, ubuntu-latest, macos-latest]
14+
fail-fast: false # Don't cancel other jobs if one fails
15+
runs-on: ${{ matrix.os }}
16+
continue-on-error: ${{ matrix.os != 'windows-latest' }} # Allow Linux/macOS to fail
1217

1318
steps:
1419
- uses: actions/checkout@v4
1520

16-
- name: Install Rust 1.88.0
17-
uses: dtolnay/rust-toolchain@stable
18-
with:
19-
toolchain: "1.88.0"
20-
2121
- name: Build project
2222
run: cargo build --verbose
2323

2424
test:
25-
runs-on: windows-latest
25+
strategy:
26+
matrix:
27+
os: [windows-latest, ubuntu-latest, macos-latest]
28+
fail-fast: false # Don't cancel other jobs if one fails
29+
runs-on: ${{ matrix.os }}
30+
continue-on-error: ${{ matrix.os != 'windows-latest' }} # Allow Linux/macOS to fail
2631
needs: build
2732

2833
steps:
2934
- uses: actions/checkout@v4
3035

31-
- name: Install Rust 1.88.0
32-
uses: dtolnay/rust-toolchain@stable
33-
with:
34-
toolchain: "1.88.0"
35-
3636
- name: Run tests
3737
run: cargo test --verbose
3838

3939
run-and-execute:
40-
runs-on: windows-latest
41-
40+
strategy:
41+
matrix:
42+
include:
43+
- os: windows-latest
44+
target: windows-x64
45+
nasm_format: win64
46+
executable_ext: .exe
47+
continue_on_error: false
48+
- os: ubuntu-latest
49+
target: linux-x64
50+
nasm_format: elf64
51+
executable_ext: ""
52+
continue_on_error: true
53+
- os: macos-latest
54+
target: macos-x64
55+
nasm_format: macho64
56+
executable_ext: ""
57+
continue_on_error: true
58+
fail-fast: false # Don't cancel other jobs if one fails
59+
runs-on: ${{ matrix.os }}
60+
continue-on-error: ${{ matrix.continue_on_error }} # Allow Linux/macOS to fail
61+
needs: test
62+
4263
steps:
4364
- uses: actions/checkout@v4
44-
45-
- name: Install Rust 1.88.0
46-
uses: dtolnay/rust-toolchain@stable
47-
with:
48-
toolchain: "1.88.0"
49-
50-
- name: Install NASM
65+
66+
# Windows-specific dependencies
67+
- name: Install NASM (Windows)
68+
if: matrix.os == 'windows-latest'
5169
run: |
5270
choco install nasm
5371
& "C:\Program Files\NASM\nasm.exe" -v
5472
55-
- name: Install GCC (MinGW)
73+
# Linux-specific dependencies
74+
- name: Install NASM and GCC (Linux)
75+
if: matrix.os == 'ubuntu-latest'
5676
run: |
57-
choco install mingw
77+
sudo apt-get update
78+
sudo apt-get install -y nasm gcc
79+
nasm -v
5880
gcc --version
5981
60-
- name: Run compiler to generate ASM
61-
run: cargo run
82+
# macOS-specific dependencies
83+
- name: Install NASM and GCC (macOS)
84+
if: matrix.os == 'macos-latest'
85+
run: |
86+
brew install nasm gcc
87+
nasm -v
88+
gcc --version
89+
90+
- name: Run compiler to generate ASM for target
91+
run: cargo run -- --target ${{ matrix.target }}
92+
93+
# Windows assembly and linking
94+
- name: Compile ASM to object file (Windows)
95+
if: matrix.os == 'windows-latest'
96+
run: '& "C:\Program Files\NASM\nasm.exe" -f ${{ matrix.nasm_format }} build/output.asm -o build/output.obj'
97+
98+
- name: Link and create executable (Windows)
99+
if: matrix.os == 'windows-latest'
100+
run: gcc -o build/output${{ matrix.executable_ext }} build/output.obj -lmsvcrt
101+
102+
# Linux assembly and linking
103+
- name: Compile ASM to object file (Linux)
104+
if: matrix.os == 'ubuntu-latest'
105+
run: nasm -f ${{ matrix.nasm_format }} build/output.asm -o build/output.o
106+
107+
- name: Link and create executable (Linux)
108+
if: matrix.os == 'ubuntu-latest'
109+
run: gcc -o build/output${{ matrix.executable_ext }} build/output.o -no-pie
110+
111+
# macOS assembly and linking
112+
- name: Compile ASM to object file (macOS)
113+
if: matrix.os == 'macos-latest'
114+
run: nasm -f ${{ matrix.nasm_format }} build/output.asm -o build/output.o
62115

63-
- name: Compile ASM to object file
64-
run: '& "C:\Program Files\NASM\nasm.exe" -f win64 build/output.asm -o output.obj'
116+
- name: Link and create executable (macOS)
117+
if: matrix.os == 'macos-latest'
118+
run: gcc -o build/output${{ matrix.executable_ext }} build/output.o
65119

66-
- name: Link and create executable
67-
run: gcc -o build/output.exe build/output.obj -lmsvcrt
120+
# Execute the binary (all platforms)
121+
- name: Execute the binary (Windows)
122+
if: matrix.os == 'windows-latest'
123+
run: .\build\output${{ matrix.executable_ext }}
68124

69-
- name: Execute the binary
70-
run: .\build\output.exe
125+
- name: Execute the binary (Linux/macOS)
126+
if: matrix.os != 'windows-latest'
127+
run: ./build/output${{ matrix.executable_ext }}

examples/target_demo.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use compiler_minic::codegen::{Codegen, TargetPlatform};
2+
use compiler_minic::ir::{IrProgram, IrFunction, IrInstruction, IrValue, IrType};
3+
4+
fn main() {
5+
// Create a simple IR program
6+
let program = IrProgram {
7+
functions: vec![
8+
IrFunction {
9+
name: "main".to_string(),
10+
return_type: IrType::Int,
11+
parameters: vec![],
12+
local_vars: vec![],
13+
instructions: vec![
14+
IrInstruction::Print {
15+
format_string: IrValue::StringConstant("hello_msg".to_string()),
16+
args: vec![],
17+
},
18+
IrInstruction::Return {
19+
value: Some(IrValue::IntConstant(0)),
20+
var_type: IrType::Int,
21+
},
22+
],
23+
}
24+
],
25+
global_strings: vec![
26+
("hello_msg".to_string(), "Hello, World!".to_string()),
27+
],
28+
};
29+
30+
println!("=== WINDOWS X64 TARGET ===");
31+
let windows_codegen = Codegen::new_with_target(TargetPlatform::WindowsX64);
32+
let windows_asm = windows_codegen.generate(&program);
33+
println!("{}", windows_asm);
34+
35+
println!("\n=== LINUX X64 TARGET ===");
36+
let linux_codegen = Codegen::new_with_target(TargetPlatform::LinuxX64);
37+
let linux_asm = linux_codegen.generate(&program);
38+
println!("{}", linux_asm);
39+
40+
println!("\n=== MACOS X64 TARGET ===");
41+
let macos_codegen = Codegen::new_with_target(TargetPlatform::MacOSX64);
42+
let macos_asm = macos_codegen.generate(&program);
43+
println!("{}", macos_asm);
44+
}

src/codegen/core/instruction.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ pub enum Instruction {
1313

1414
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
1515
pub enum Register {
16-
Rax, Rbp, Rsp, Rcx, Rdx, R8, R9,
17-
Eax, Edx, R8d, R9d,
16+
Rax, Rbp, Rsp, Rcx, Rdx, R8, R9, Rdi, Rsi,
17+
Eax, Edx, R8d, R9d, Edi, Esi,
1818
Al,
1919
Xmm0, Xmm1, Xmm2, Xmm3,
2020
}
@@ -85,10 +85,14 @@ impl Register {
8585
Register::Rdx => "rdx",
8686
Register::R8 => "r8",
8787
Register::R9 => "r9",
88+
Register::Rdi => "rdi",
89+
Register::Rsi => "rsi",
8890
Register::Eax => "eax",
8991
Register::Edx => "edx",
9092
Register::R8d => "r8d",
9193
Register::R9d => "r9d",
94+
Register::Edi => "edi",
95+
Register::Esi => "esi",
9296
Register::Al => "al",
9397
Register::Xmm0 => "xmm0",
9498
Register::Xmm1 => "xmm1",

src/codegen/core/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
33
mod emitter;
44
mod instruction;
5-
mod target;
5+
pub mod targets;
66

77
pub use emitter::{Emitter, CodeEmitter, CodeEmitterWithComment};
88
pub use instruction::{Instruction, Register, Operand, Size};
9-
pub use target::{
9+
pub use targets::{
1010
Target, TargetPlatform, CallingConvention,
1111
WindowsX64Target, LinuxX64Target, MacOSX64Target,
1212
create_target, parse_target_platform

0 commit comments

Comments
 (0)