From 6a6356307955fd3d8229f7a853c965952b532df0 Mon Sep 17 00:00:00 2001 From: Matthew Wilding Date: Fri, 3 Oct 2025 19:08:50 +0800 Subject: [PATCH] Crust compiler --- Makefile | 2 +- README.md | 20 +++++++++++++++++++- main.rs | 36 ++++++++++++++++++++---------------- 3 files changed, 40 insertions(+), 18 deletions(-) diff --git a/Makefile b/Makefile index d90236b..9f63b80 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ else endif $(OUT): main.rs - rustc --edition 2021 -g -C opt-level=z -C link-args=$(ARGS) -C panic="abort" main.rs -o $(OUT) + /home/anon/dev/github/rust/build/x86_64-unknown-linux-gnu/stage1/bin/rustc --edition 2021 -g -C opt-level=z -C link-args=$(ARGS) -C panic="abort" main.rs -o $(OUT) clean: rm -f main diff --git a/README.md b/README.md index 1d5d3b5..e3d0839 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@

-1. Every function is unsafe. +1. Use custom build of rustc 1. No references, only pointers. 1. No cargo, build with rustc directly. 1. No std, but libc is allowed. @@ -15,3 +15,21 @@ *The list of rules may change. The goal is to make programming in Rust fun.* Currently used in the [B Compiler Project](https://github.com/tsoding/b). + +## Crust Compiler + +1. Unsafe checks removed +1. Variables are mutable by default (`let` rather than `let mut`) +1. Mutable pointers by default (Can use pointers like `*` instead of `*mut` or `**` rather than `*mut *mut`) + +> TODO: public without specifying `pub` + +## Setup + +```bash +git clone git@github.com:mbwilding/rust.git --depth=1 +cd rust +./x.py build +``` + +Then update the path in the `Makefile` in this repo to match your clone location diff --git a/main.rs b/main.rs index 701821d..ac4f67f 100644 --- a/main.rs +++ b/main.rs @@ -6,8 +6,12 @@ use core::panic::PanicInfo; use core::mem::zeroed; use libc::*; +// NOTE: Haven't figured out how to not need this yet +#[no_mangle] +pub extern "C" fn rust_eh_personality() {} + #[panic_handler] -unsafe fn panic(info: &PanicInfo) -> ! { +fn panic(info: &PanicInfo) -> ! { // TODO: What's the best way to implement the panic handler within the Crust spirit // PanicInfo must be passed by reference. if let Some(location) = info.location() { @@ -75,9 +79,9 @@ pub mod libc { pub type FILE = c_void; extern "C" { - pub static stdin: *mut FILE; - pub static stdout: *mut FILE; - pub static stderr: *mut FILE; + pub static stdin: *FILE; + pub static stdout: *FILE; + pub static stderr: *FILE; } #[macro_export] @@ -86,7 +90,7 @@ pub mod libc { use core::ffi::c_int; extern "C" { #[link_name = "fprintf"] - pub fn fprintf_raw(stream: *mut libc::FILE, fmt: *const c_char, ...) -> c_int; + pub fn fprintf_raw(stream: *libc::FILE, fmt: *const c_char, ...) -> c_int; } fprintf_raw($stream, $fmt.as_ptr() $($args)*) }}; @@ -108,20 +112,20 @@ pub mod libc { pub fn abort() -> !; } - pub unsafe fn realloc_items(ptr: *mut T, count: usize) -> *mut T { + pub fn realloc_items(ptr: *T, count: usize) -> *T { extern "C" { #[link_name = "realloc"] - fn realloc_raw(ptr: *mut c_void, size: usize) -> *mut c_void; + fn realloc_raw(ptr: *c_void, size: usize) -> *mut c_void; } - realloc_raw(ptr as *mut c_void, size_of::()*count) as *mut T + realloc_raw(ptr as *c_void, size_of::()*count) as *T } - pub unsafe fn free(ptr: *mut T) { + pub fn free(ptr: *T) { extern "C" { #[link_name = "free"] - fn free_raw(ptr: *mut c_void); + fn free_raw(ptr: *c_void); } - free_raw(ptr as *mut c_void); + free_raw(ptr as *c_void); } } @@ -132,12 +136,12 @@ pub mod da { // Dynamic Arrays in Crust #[repr(C)] #[derive(Clone, Copy)] pub struct Array { - pub items: *mut T, + pub items: *T, pub count: usize, pub capacity: usize, } - pub unsafe fn da_append(da: *mut Array, item: T) { + pub fn da_append(da: *Array, item: T) { if (*da).count >= (*da).capacity { if (*da).capacity == 0 { (*da).capacity = 256; @@ -150,7 +154,7 @@ pub mod da { // Dynamic Arrays in Crust (*da).count += 1; } - pub unsafe fn da_destroy(da: *mut Array) { + pub fn da_destroy(da: *Array) { libc::free((*da).items); (*da).items = ptr::null_mut(); (*da).count = 0; @@ -166,7 +170,7 @@ pub struct Rect { } #[no_mangle] -pub unsafe extern "C" fn main(_argc: i32, _argv: *mut *mut u8) -> i32 { +pub extern "C" fn main(_argc: i32, _argv: **u8) -> i32 { use core::ffi::c_float; use raylib::*; use raymath::*; @@ -175,7 +179,7 @@ pub unsafe extern "C" fn main(_argc: i32, _argv: *mut *mut u8) -> i32 { const BACKGROUND: Color = Color {r: 0x18, g: 0x18, b: 0x18, a: 255}; const RECT_SIZE: Vector2 = Vector2 { x: 100.0, y: 100.0 }; - let mut rects: Array = zeroed(); + let rects: Array = zeroed(); da_append(&mut rects, Rect { position: Vector2 { x: 0.0, y: 0.0 }, velocity: Vector2 { x: 100.0, y: 100.0 },