Skip to content

Commit ba3fcf8

Browse files
committed
Introduce bgen
1 parent be73162 commit ba3fcf8

7 files changed

Lines changed: 66 additions & 27 deletions

File tree

Makefile

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,7 @@ RSS=\
2727
$(SRC)/time.rs \
2828
$(SRC)/jim.rs \
2929
$(SRC)/jimp.rs \
30-
$(SRC)/codegen/gas_aarch64.rs \
31-
$(SRC)/codegen/gas_x86_64.rs \
32-
$(SRC)/codegen/mos6502.rs \
33-
$(SRC)/codegen/uxn.rs \
34-
$(SRC)/codegen/mod.rs \
30+
3531

3632
POSIX_OBJS=\
3733
$(BUILD)/nob.posix.o \
@@ -65,21 +61,32 @@ test: $(BUILD)/b $(BUILD)/btest
6561
.PHONY: mingw32-all
6662
mingw32-all: $(BUILD)/b.exe $(BUILD)/btest.exe
6763

68-
$(BUILD)/b: $(RSS) $(POSIX_OBJS) | $(BUILD)
69-
rustc $(CRUST_FLAGS) -C link-args="$(POSIX_OBJS) $(LDFLAGS)" $(SRC)/b.rs -o $(BUILD)/b
64+
$(BUILD)/b: $(RSS) $(POSIX_OBJS) $(SRC)/codegen/.INDEX.rs | $(BUILD)
65+
rustc $(CRUST_FLAGS) -L $(BUILD) -C link-args="$(POSIX_OBJS) $(LDFLAGS)" $(SRC)/b.rs -o $(BUILD)/b
7066

71-
$(BUILD)/btest: $(SRC)/btest.rs $(RSS) $(POSIX_OBJS) | $(BUILD)
67+
$(BUILD)/btest: $(SRC)/btest.rs $(RSS) $(POSIX_OBJS) $(SRC)/codegen/.INDEX.rs | $(BUILD)
7268
rustc $(CRUST_FLAGS) -C link-args="$(POSIX_OBJS) $(LDFLAGS)" $(SRC)/btest.rs -o $(BUILD)/btest
7369

70+
ifneq ($(OS),Windows_NT)
71+
$(SRC)/codegen/.INDEX.rs: $(BUILD)/bgen
72+
$(BUILD)/bgen
73+
else
74+
$(SRC)/codegen/.INDEX.rs: $(BUILD)/bgen.exe
75+
$(BUILD)/bgen.exe
76+
endif
77+
78+
$(BUILD)/bgen: $(SRC)/bgen.rs $(RSS) $(POSIX_OBJS) | $(BUILD)
79+
rustc $(CRUST_FLAGS) -C link-args="$(POSIX_OBJS) $(LDFLAGS)" $(SRC)/bgen.rs -o $(BUILD)/bgen
80+
7481
$(BUILD)/%.posix.o: ./thirdparty/%.c | $(BUILD)
7582
$(CC) -fPIC -g -c $< -o $@ $(LDFLAGS)
7683

7784
# Cross-compilation on POSIX to Windows using mingw32-w64
7885
# Invoked on demand by `make ./build/b.exe`
79-
$(BUILD)/b.exe: $(RSS) $(MINGW32_OBJS) | $(BUILD)
86+
$(BUILD)/b.exe: $(RSS) $(MINGW32_OBJS) $(SRC)/codegen/.INDEX.rs | $(BUILD)
8087
rustc $(CRUST_FLAGS) --target x86_64-pc-windows-gnu -C link-args="$(MINGW32_OBJS) -lmingwex -lmsvcrt -lkernel32" $(SRC)/b.rs -o $(BUILD)/b.exe
8188

82-
$(BUILD)/btest.exe: $(SRC)/btest.rs $(RSS) $(MINGW32_OBJS) | $(BUILD)
89+
$(BUILD)/btest.exe: $(SRC)/btest.rs $(RSS) $(MINGW32_OBJS) $(SRC)/codegen/.INDEX.rs | $(BUILD)
8390
rustc $(CRUST_FLAGS) --target x86_64-pc-windows-gnu -C link-args="$(MINGW32_OBJS) -lmingwex -lmsvcrt -lkernel32" $(SRC)/btest.rs -o $(BUILD)/btest.exe
8491

8592
$(BUILD)/%.mingw32.o: ./thirdparty/%.c | $(BUILD)

src/bgen.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#![no_main]
2+
#![no_std]
3+
#![allow(non_upper_case_globals)]
4+
#![allow(non_camel_case_types)]
5+
#![allow(unused_macros)]
6+
7+
#[macro_use]
8+
pub mod crust;
9+
pub mod nob;
10+
11+
use core::ffi::*;
12+
use core::mem::zeroed;
13+
use nob::*;
14+
use crust::libc::*;
15+
use crust::*;
16+
17+
pub unsafe fn main(mut _argc: i32, mut _argv: *mut*mut c_char) -> Option<()> {
18+
let parent = c!("./src/codegen");
19+
let mut children: File_Paths = zeroed();
20+
if !read_entire_dir(parent, &mut children) {todo!();}
21+
qsort(children.items as *mut c_void, children.count, size_of::<*const c_char>(), compar_cstr);
22+
let mut sb: String_Builder = zeroed();
23+
sb_appendf(&mut sb, c!("codegens! {\n"));
24+
for i in 0..children.count {
25+
let child = *children.items.add(i);
26+
if *child == '.' as c_char { continue; }
27+
if strcmp(child, c!("mod.rs")) == 0 { continue; }
28+
let child = temp_strip_suffix(child, c!(".rs")).unwrap_or(child);
29+
sb_appendf(&mut sb, c!(" %s,\n"), child);
30+
}
31+
sb_appendf(&mut sb, c!("}\n"));
32+
let output_path = temp_sprintf(c!("%s/.INDEX.rs"), parent);
33+
write_entire_file(output_path, sb.items as _, sb.count)?;
34+
log(Log_Level::INFO, c!("Generated %s"), output_path);
35+
Some(())
36+
}

src/btest.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use flag::*;
3232
use glob::*;
3333
use jim::*;
3434
use jimp::*;
35+
use crust::compar_cstr;
3536

3637
const GARBAGE_FOLDER: *const c_char = c!("./build/tests/");
3738

@@ -197,10 +198,6 @@ pub unsafe fn usage() {
197198
flag_print_options(stderr());
198199
}
199200

200-
pub unsafe extern "C" fn compar_cstr(a: *const c_void, b: *const c_void) -> c_int {
201-
strcmp(*(a as *const *const c_char), *(b as *const *const c_char))
202-
}
203-
204201
#[derive(Clone, Copy)]
205202
pub struct ReportStats {
206203
entries: [usize; REPORT_STATUS_ORDER.len()]

src/codegen/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.INDEX.rs

src/codegen/mod.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,6 @@ macro_rules! codegens {
2424
};
2525
}
2626

27-
codegens! {
28-
// TODO: maybe instead of gas_ the prefix should be gnu_, 'cause that makes more sense.
29-
gas_x86_64,
30-
gas_aarch64,
31-
uxn,
32-
mos6502,
33-
ilasm_mono,
34-
noop,
35-
}
27+
// TODO: maybe instead of gas_ the prefix should be gnu_, 'cause that makes more sense.
28+
29+
include!(".INDEX.rs"); // Generated by bgen

src/crust.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ use crate::crust::libc::*;
33
use core::panic::PanicInfo;
44
use core::ffi::*;
55

6-
#[macro_export]
6+
// #[macro_export]
77
macro_rules! c {
88
($l:expr) => {
99
concat!($l, "\0").as_ptr() as *const c_char
1010
}
1111
}
1212

13-
#[macro_export]
13+
// #[macro_export]
1414
macro_rules! enum_with_order {
1515
(
1616
#[derive($($traits:tt)*)]
@@ -134,6 +134,10 @@ pub mod libc {
134134
}
135135
}
136136

137+
pub unsafe extern "C" fn compar_cstr(a: *const c_void, b: *const c_void) -> c_int {
138+
strcmp(*(a as *const *const c_char), *(b as *const *const c_char))
139+
}
140+
137141
#[panic_handler]
138142
pub unsafe fn panic_handler(info: &PanicInfo) -> ! {
139143
// TODO: What's the best way to implement the panic handler within the Crust spirit

src/nob.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ pub unsafe fn da_append_many<T: Clone + Copy>(xs: *mut Array<T>, items: *const [
6262
}
6363
}
6464

65-
#[macro_export]
65+
// #[macro_export]
6666
macro_rules! shift {
6767
($ptr:ident, $len:ident) => {{
6868
let result = *$ptr;
@@ -74,7 +74,7 @@ macro_rules! shift {
7474

7575
pub type Cmd = Array<*const c_char>;
7676

77-
#[macro_export]
77+
// #[macro_export]
7878
macro_rules! cmd_append {
7979
($cmd:expr, $($arg:expr),+ $(,)?) => {
8080
$(da_append($cmd, $arg);)+

0 commit comments

Comments
 (0)