From 46045af4fa2243356a895dc6ed09c036ba3bb2c7 Mon Sep 17 00:00:00 2001 From: Toni Date: Tue, 11 Nov 2025 13:33:40 +0100 Subject: [PATCH] raylib example --- examples/raylib.zr | 20 ++++++++++++++++++++ src/codegen_x86_64.rs | 25 ++++++++++++++++++++----- src/main.rs | 19 ++++++++++++++----- 3 files changed, 54 insertions(+), 10 deletions(-) create mode 100644 examples/raylib.zr diff --git a/examples/raylib.zr b/examples/raylib.zr new file mode 100644 index 0000000..b06737f --- /dev/null +++ b/examples/raylib.zr @@ -0,0 +1,20 @@ +// musl doesnt like dlopen, needs to be compiled with -m + +func main[] : I64 + let rl: Ptr = c.dlopen("libraylib.so", 2) + + let rl.InitWindow: Ptr = c.dlsym(rl, "InitWindow") + let rl.WindowShouldClose: Ptr = c.dlsym(rl, "WindowShouldClose") + let rl.BeginDrawing: Ptr = c.dlsym(rl, "BeginDrawing") + let rl.EndDrawing: Ptr = c.dlsym(rl, "EndDrawing") + let rl.ClearBackground: Ptr = c.dlsym(rl, "ClearBackground") + let rl.CloseWindow: Ptr = c.dlsym(rl, "CloseWindow") + + rl.InitWindow(800, 600, "Hello, World!") + + while !rl.WindowShouldClose() + rl.BeginDrawing() + rl.ClearBackground(4278190335) // 0xff0000ff + rl.EndDrawing() + + rl.CloseWindow() \ No newline at end of file diff --git a/src/codegen_x86_64.rs b/src/codegen_x86_64.rs index acb5f4a..57249ec 100644 --- a/src/codegen_x86_64.rs +++ b/src/codegen_x86_64.rs @@ -38,9 +38,12 @@ impl Env { pub fn define_var(&mut self, name: String, _var_type: String) -> usize { let offset = self.next_offset; self.next_offset += 8; - self.scopes.last_mut().unwrap().insert(name, Var { - stack_offset: offset, - }); + self.scopes.last_mut().unwrap().insert( + name, + Var { + stack_offset: offset, + }, + ); offset } @@ -103,7 +106,7 @@ section .text ); // take that rustfmt - for name in "malloc,calloc,realloc,free,puts,putchar,printf,sprintf,snprintf,strtol,strlen,strcmp,strncmp,strcat,strcpy,strdup,strncpy,syscall,fopen,fseek,ftell,fread,fwrite,fclose,rewind,system,opendir,readdir,closedir,exit,gettimeofday,connect,socket,send,write,read,close,bind,listen,accept,getchar,gethostbyname".split(",") + for name in "malloc,calloc,realloc,free,puts,putchar,printf,sprintf,snprintf,strtol,strlen,strcmp,strncmp,strcat,strcpy,strdup,strncpy,syscall,fopen,fseek,ftell,fread,fwrite,fclose,rewind,system,opendir,readdir,closedir,exit,gettimeofday,connect,socket,send,write,read,close,bind,listen,accept,getchar,gethostbyname,dlopen,dlsym,dlerror".split(",") { emit!(&mut self.output, "extern {}", name); emit!(&mut self.output, "c.{} equ {}", name, name); @@ -471,7 +474,19 @@ _builtin_set64: emit!(&mut self.output, " pop {}", reg); } - emit!(&mut self.output, " call {}", callee); + match env.get_var(&callee) { + Some(var) => { + emit!( + &mut self.output, + " mov rax, QWORD [rbp-{}]", + var.stack_offset, + ); + emit!(&mut self.output, " call rax"); + } + None => { + emit!(&mut self.output, " call {}", callee); + } + }; } Expr::ArrayLiteral(exprs) => { emit!(&mut self.output, " call array.new"); diff --git a/src/main.rs b/src/main.rs index d33a759..017691c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -77,11 +77,17 @@ fn compile_file(args: Args) -> Result<(), ZernError> { run_command(format!("nasm -f elf64 -o {}.o {}.s", args.out, args.out)); - // TODO: drop libc entirely - run_command(format!( - "./musl-1.2.4/root/bin/musl-gcc -static -o {} {}.o -flto -Wl,--gc-sections {}", - args.out, args.out, args.cflags - )); + if args.no_musl { + run_command(format!( + "gcc -no-pie -o {} {}.o -flto -Wl,--gc-sections {}", + args.out, args.out, args.cflags + )); + } else { + run_command(format!( + "./musl-1.2.4/root/bin/musl-gcc -static -o {} {}.o -flto -Wl,--gc-sections {}", + args.out, args.out, args.cflags + )); + } if args.run_exe { run_command( @@ -112,6 +118,9 @@ struct Args { #[arg(short = 'r', help = "Run the compiled executable")] run_exe: bool, + #[arg(short = 'm', help = "Don't use musl")] + no_musl: bool, + #[arg(short = 'C', default_value = "", help = "Extra flags to pass to gcc")] cflags: String, }