raylib example
This commit is contained in:
20
examples/raylib.zr
Normal file
20
examples/raylib.zr
Normal file
@@ -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()
|
||||||
@@ -38,9 +38,12 @@ impl Env {
|
|||||||
pub fn define_var(&mut self, name: String, _var_type: String) -> usize {
|
pub fn define_var(&mut self, name: String, _var_type: String) -> usize {
|
||||||
let offset = self.next_offset;
|
let offset = self.next_offset;
|
||||||
self.next_offset += 8;
|
self.next_offset += 8;
|
||||||
self.scopes.last_mut().unwrap().insert(name, Var {
|
self.scopes.last_mut().unwrap().insert(
|
||||||
|
name,
|
||||||
|
Var {
|
||||||
stack_offset: offset,
|
stack_offset: offset,
|
||||||
});
|
},
|
||||||
|
);
|
||||||
offset
|
offset
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -103,7 +106,7 @@ section .text
|
|||||||
);
|
);
|
||||||
|
|
||||||
// take that rustfmt
|
// 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, "extern {}", name);
|
||||||
emit!(&mut self.output, "c.{} equ {}", name, name);
|
emit!(&mut self.output, "c.{} equ {}", name, name);
|
||||||
@@ -471,8 +474,20 @@ _builtin_set64:
|
|||||||
emit!(&mut self.output, " pop {}", reg);
|
emit!(&mut self.output, " pop {}", reg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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);
|
emit!(&mut self.output, " call {}", callee);
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
Expr::ArrayLiteral(exprs) => {
|
Expr::ArrayLiteral(exprs) => {
|
||||||
emit!(&mut self.output, " call array.new");
|
emit!(&mut self.output, " call array.new");
|
||||||
emit!(&mut self.output, " push rax");
|
emit!(&mut self.output, " push rax");
|
||||||
|
|||||||
11
src/main.rs
11
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));
|
run_command(format!("nasm -f elf64 -o {}.o {}.s", args.out, args.out));
|
||||||
|
|
||||||
// TODO: drop libc entirely
|
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!(
|
run_command(format!(
|
||||||
"./musl-1.2.4/root/bin/musl-gcc -static -o {} {}.o -flto -Wl,--gc-sections {}",
|
"./musl-1.2.4/root/bin/musl-gcc -static -o {} {}.o -flto -Wl,--gc-sections {}",
|
||||||
args.out, args.out, args.cflags
|
args.out, args.out, args.cflags
|
||||||
));
|
));
|
||||||
|
}
|
||||||
|
|
||||||
if args.run_exe {
|
if args.run_exe {
|
||||||
run_command(
|
run_command(
|
||||||
@@ -112,6 +118,9 @@ struct Args {
|
|||||||
#[arg(short = 'r', help = "Run the compiled executable")]
|
#[arg(short = 'r', help = "Run the compiled executable")]
|
||||||
run_exe: bool,
|
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")]
|
#[arg(short = 'C', default_value = "", help = "Extra flags to pass to gcc")]
|
||||||
cflags: String,
|
cflags: String,
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user