euler8, euler12

This commit is contained in:
2025-06-01 19:02:25 +02:00
parent ad8c61002b
commit e84419f0cf
4 changed files with 101 additions and 8 deletions

View File

@@ -102,6 +102,7 @@ extern sprintf
extern strlen
extern strcmp
extern puts
extern system
print equ puts
; generated with clang
@@ -131,6 +132,41 @@ strrev:
pop rbx
pop r14
ret
isqrt:
xor rax, rax
mov rcx, 1
mov rbx, rdi
shl rcx, 62
.LBB0_3:
cmp rcx, 0
je .LBB0_5
cmp rcx, rbx
jbe .LBB0_4
shr rcx, 2
jmp .LBB0_3
.LBB0_4:
cmp rcx, 0
je .LBB0_5
mov rdx, rax
add rdx, rcx
cmp rbx, rdx
jb .LBB0_7
sub rbx, rdx
shr rax, 1
add rax, rcx
jmp .LBB0_6
.LBB0_7:
shr rax, 1
.LBB0_6:
shr rcx, 2
jmp .LBB0_4
.LBB0_5:
ret
nth:
movzx rax, byte [rdi + rsi]
ret
",
);
Ok(())
@@ -381,7 +417,7 @@ strrev:
} => {
let callee = match *callee {
Expr::Variable(name) => name.lexeme,
_ => todo!(),
_ => return error!(&paren.loc, "tried to call a non-constant expression"),
};
for (i, arg) in args.iter().enumerate() {

View File

@@ -35,20 +35,29 @@ fn compile_file(path: String) -> Result<(), ZernError> {
}
codegen.emit_epilogue()?;
// TODO: handle error
fs::write("out.s", codegen.get_output()).unwrap();
if fs::write("out.s", codegen.get_output()).is_err() {
eprintln!("\x1b[91mERROR\x1b[0m: failed to write to out.s");
process::exit(1);
}
// TODO: stop on nasm/gcc error
Command::new("nasm")
if !Command::new("nasm")
.args(["-f", "elf64", "-o", "out.o", "out.s"])
.status()
.unwrap();
.unwrap()
.success()
{
process::exit(1);
}
// TODO: drop libc entirely
Command::new("./musl-1.2.4/root/bin/musl-gcc")
if !Command::new("./musl-1.2.4/root/bin/musl-gcc")
.args(["-static", "-o", "out", "out.o"])
.status()
.unwrap();
.unwrap()
.success()
{
process::exit(1);
}
Ok(())
}