fixing the cli

This commit is contained in:
Jakub Doka 2024-09-28 16:28:05 +02:00
parent 4291ebc25e
commit 6d805dc2ec
No known key found for this signature in database
GPG key ID: C6E9A89936B8C143
14 changed files with 71 additions and 118 deletions

View file

@ -11,10 +11,10 @@ fn main() -> std::io::Result<()> {
} }
hblang::run_compiler( hblang::run_compiler(
args.get(1).copied().unwrap_or("main.hb"), args.iter().filter(|a| !a.starts_with('-')).nth(1).copied().unwrap_or("main.hb"),
hblang::Options { hblang::Options {
fmt: args.contains(&"--fmt"), fmt: args.contains(&"--fmt"),
fmt_current: args.contains(&"--fmt-current"), fmt_current: args.contains(&"--fmt-stdout"),
dump_asm: args.contains(&"--dump-asm"), dump_asm: args.contains(&"--dump-asm"),
extra_threads: args extra_threads: args
.iter() .iter()

View file

@ -1994,18 +1994,39 @@ impl Codegen {
ret_rel.apply_jump(&mut self.ci.code, end as _, 0); ret_rel.apply_jump(&mut self.ci.code, end as _, 0);
} }
let mut stripped_prelude_size = 0;
'_close_function: { '_close_function: {
let pushed = (saved as i64 + 1) * 8; let pushed =
(saved as i64 + (std::mem::take(&mut self.ci.call_count) != 0) as i64) * 8;
let stack = std::mem::take(&mut self.ci.stack_size) as i64; let stack = std::mem::take(&mut self.ci.stack_size) as i64;
match (pushed, stack) {
(0, 0) => {
stripped_prelude_size =
instrs::addi64(0, 0, 0).0 + instrs::st(0, 0, 0, 0).0;
self.ci.code.drain(0..stripped_prelude_size);
break '_close_function;
}
(0, stack) => {
write_reloc(&mut self.ci.code, 3, -stack, 8);
stripped_prelude_size = instrs::addi64(0, 0, 0).0;
let end = stripped_prelude_size + instrs::st(0, 0, 0, 0).0;
self.ci.code.drain(stripped_prelude_size..end);
self.ci.emit(instrs::addi64(reg::STACK_PTR, reg::STACK_PTR, stack as _));
break '_close_function;
}
_ => {}
}
write_reloc(&mut self.ci.code, 3, -(pushed + stack), 8); write_reloc(&mut self.ci.code, 3, -(pushed + stack), 8);
write_reloc(&mut self.ci.code, 3 + 8 + 3, stack, 8); write_reloc(&mut self.ci.code, 3 + 8 + 3, stack, 8);
write_reloc(&mut self.ci.code, 3 + 8 + 3 + 8, pushed, 2); write_reloc(&mut self.ci.code, 3 + 8 + 3 + 8, pushed, 2);
self.ci.emit(instrs::ld(reg::RET_ADDR, reg::STACK_PTR, stack as _, pushed as _)); self.ci.emit(instrs::ld(reg::RET_ADDR, reg::STACK_PTR, stack as _, pushed as _));
self.ci.emit(instrs::addi64(reg::STACK_PTR, reg::STACK_PTR, (pushed + stack) as _)); self.ci.emit(instrs::addi64(reg::STACK_PTR, reg::STACK_PTR, (pushed + stack) as _));
self.ci.emit(instrs::jala(reg::ZERO, reg::RET_ADDR, 0));
} }
self.ci.relocs.iter_mut().for_each(|r| r.reloc.offset -= stripped_prelude_size as u32);
self.ci.emit(instrs::jala(reg::ZERO, reg::RET_ADDR, 0));
} }
self.tys.funcs[id as usize].code.append(&mut self.ci.code); self.tys.funcs[id as usize].code.append(&mut self.ci.code);

View file

@ -1,10 +1,6 @@
main: main:
ADDI64 r254, r254, -8d
ST r31, r254, 0a, 8h
LI64 r1, 0d LI64 r1, 0d
LD r31, r254, 0a, 8h
ADDI64 r254, r254, 8d
JALA r0, r31, 0a JALA r0, r31, 0a
code size: 77 code size: 29
ret: 0 ret: 0
status: Ok(()) status: Ok(())

View file

@ -1,6 +1,4 @@
main: main:
ADDI64 r254, r254, -8d
ST r31, r254, 0a, 8h
LI64 r1, 1d LI64 r1, 1d
JNE r2, r1, :0 JNE r2, r1, :0
JMP :1 JMP :1
@ -9,9 +7,7 @@ main:
LI64 r1, 2d LI64 r1, 2d
JMP :1 JMP :1
2: LI64 r1, 3d 2: LI64 r1, 3d
1: LD r31, r254, 0a, 8h 1: JALA r0, r31, 0a
ADDI64 r254, r254, 8d code size: 79
JALA r0, r31, 0a
code size: 127
ret: 2 ret: 2
status: Ok(()) status: Ok(())

View file

@ -1,8 +1,4 @@
foo: foo:
ADDI64 r254, r254, -8d
ST r31, r254, 0a, 8h
LD r31, r254, 0a, 8h
ADDI64 r254, r254, 8d
JALA r0, r31, 0a JALA r0, r31, 0a
main: main:
ADDI64 r254, r254, -8d ADDI64 r254, r254, -8d
@ -12,6 +8,6 @@ main:
LD r31, r254, 0a, 8h LD r31, r254, 0a, 8h
ADDI64 r254, r254, 8d ADDI64 r254, r254, 8d
JALA r0, r31, 0a JALA r0, r31, 0a
code size: 143 code size: 95
ret: 0 ret: 0
status: Ok(()) status: Ok(())

View file

@ -1,10 +1,6 @@
main: main:
ADDI64 r254, r254, -8d
ST r31, r254, 0a, 8h
LI64 r1, 0d LI64 r1, 0d
LD r31, r254, 0a, 8h
ADDI64 r254, r254, 8d
JALA r0, r31, 0a JALA r0, r31, 0a
code size: 77 code size: 29
ret: 0 ret: 0
status: Ok(()) status: Ok(())

View file

@ -1,24 +1,20 @@
continue_and_state_change: continue_and_state_change:
ADDI64 r254, r254, -40d LI64 r7, 3d
ST r31, r254, 0a, 40h LI64 r8, 4d
LI64 r32, 3d LI64 r9, 2d
LI64 r33, 4d LI64 r10, 10d
LI64 r34, 2d 6: JLTU r2, r10, :0
LI64 r35, 10d
6: JLTU r2, r35, :0
CP r1, r2 CP r1, r2
JMP :1 JMP :1
0: JNE r2, r34, :2 0: JNE r2, r9, :2
CP r2, r33 CP r2, r8
JMP :3 JMP :3
2: JNE r2, r32, :4 2: JNE r2, r7, :4
LI64 r1, 0d LI64 r1, 0d
1: JMP :5 1: JMP :5
4: ADDI64 r2, r2, 1d 4: ADDI64 r2, r2, 1d
3: JMP :6 3: JMP :6
5: LD r31, r254, 0a, 40h 5: JALA r0, r31, 0a
ADDI64 r254, r254, 40d
JALA r0, r31, 0a
main: main:
ADDI64 r254, r254, -80d ADDI64 r254, r254, -80d
ST r31, r254, 0a, 80h ST r31, r254, 0a, 80h
@ -70,37 +66,29 @@ main:
ADDI64 r254, r254, 80d ADDI64 r254, r254, 80d
JALA r0, r31, 0a JALA r0, r31, 0a
multiple_breaks: multiple_breaks:
ADDI64 r254, r254, -24d LI64 r6, 3d
ST r31, r254, 0a, 24h LI64 r5, 10d
LI64 r32, 3d 4: JLTU r2, r5, :0
LI64 r33, 10d
4: JLTU r2, r33, :0
CP r1, r2 CP r1, r2
JMP :1 JMP :1
0: ADDI64 r1, r2, 1d 0: ADDI64 r1, r2, 1d
JNE r1, r32, :2 JNE r1, r6, :2
1: JMP :3 1: JMP :3
2: CP r2, r1 2: CP r2, r1
JMP :4 JMP :4
3: LD r31, r254, 0a, 24h 3: JALA r0, r31, 0a
ADDI64 r254, r254, 24d
JALA r0, r31, 0a
state_change_in_break: state_change_in_break:
ADDI64 r254, r254, -24d LI64 r5, 3d
ST r31, r254, 0a, 24h LI64 r6, 10d
LI64 r32, 3d 4: JLTU r2, r6, :0
LI64 r33, 10d
4: JLTU r2, r33, :0
CP r1, r2 CP r1, r2
JMP :1 JMP :1
0: JNE r2, r32, :2 0: JNE r2, r5, :2
LI64 r1, 0d LI64 r1, 0d
1: JMP :3 1: JMP :3
2: ADDI64 r2, r2, 1d 2: ADDI64 r2, r2, 1d
JMP :4 JMP :4
3: LD r31, r254, 0a, 24h 3: JALA r0, r31, 0a
ADDI64 r254, r254, 24d code size: 569
JALA r0, r31, 0a
code size: 713
ret: 0 ret: 0
status: Ok(()) status: Ok(())

View file

@ -42,20 +42,12 @@ main:
ADDI64 r254, r254, 64d ADDI64 r254, r254, 64d
JALA r0, r31, 0a JALA r0, r31, 0a
set_pixel: set_pixel:
ADDI64 r254, r254, -16d MUL64 r8, r4, r3
ST r31, r254, 0a, 16h ADD64 r1, r8, r2
MUL64 r32, r4, r3
ADD64 r1, r32, r2
LD r31, r254, 0a, 16h
ADDI64 r254, r254, 16d
JALA r0, r31, 0a JALA r0, r31, 0a
x86_fb_ptr: x86_fb_ptr:
ADDI64 r254, r254, -8d
ST r31, r254, 0a, 8h
LI64 r1, 100d LI64 r1, 100d
LD r31, r254, 0a, 8h
ADDI64 r254, r254, 8d
JALA r0, r31, 0a JALA r0, r31, 0a
code size: 426 code size: 330
ret: 3000 ret: 3000
status: Ok(()) status: Ok(())

View file

@ -1,16 +1,8 @@
add_one: add_one:
ADDI64 r254, r254, -8d
ST r31, r254, 0a, 8h
ADDI64 r1, r2, 1d ADDI64 r1, r2, 1d
LD r31, r254, 0a, 8h
ADDI64 r254, r254, 8d
JALA r0, r31, 0a JALA r0, r31, 0a
add_two: add_two:
ADDI64 r254, r254, -8d
ST r31, r254, 0a, 8h
ADDI64 r1, r2, 2d ADDI64 r1, r2, 2d
LD r31, r254, 0a, 8h
ADDI64 r254, r254, 8d
JALA r0, r31, 0a JALA r0, r31, 0a
main: main:
ADDI64 r254, r254, -16d ADDI64 r254, r254, -16d
@ -24,6 +16,6 @@ main:
LD r31, r254, 0a, 16h LD r31, r254, 0a, 16h
ADDI64 r254, r254, 16d ADDI64 r254, r254, 16d
JALA r0, r31, 0a JALA r0, r31, 0a
code size: 248 code size: 152
ret: 33 ret: 33
status: Ok(()) status: Ok(())

View file

@ -1,10 +1,6 @@
main: main:
ADDI64 r254, r254, -8d
ST r31, r254, 0a, 8h
LI64 r1, 0d LI64 r1, 0d
LD r31, r254, 0a, 8h
ADDI64 r254, r254, 8d
JALA r0, r31, 0a JALA r0, r31, 0a
code size: 77 code size: 29
ret: 0 ret: 0
status: Ok(()) status: Ok(())

View file

@ -1,19 +1,15 @@
fib: fib:
ADDI64 r254, r254, -32d LI64 r7, 1d
ST r31, r254, 0a, 32h LI64 r4, 0d
LI64 r32, 1d CP r1, r4
LI64 r33, 0d 2: JNE r2, r4, :0
CP r1, r33
2: JNE r2, r33, :0
JMP :1 JMP :1
0: ADD64 r34, r32, r1 0: ADD64 r3, r7, r1
ADDI64 r2, r2, -1d ADDI64 r2, r2, -1d
CP r1, r32 CP r1, r7
CP r32, r34 CP r7, r3
JMP :2 JMP :2
1: LD r31, r254, 0a, 32h 1: JALA r0, r31, 0a
ADDI64 r254, r254, 32d
JALA r0, r31, 0a
main: main:
ADDI64 r254, r254, -8d ADDI64 r254, r254, -8d
ST r31, r254, 0a, 8h ST r31, r254, 0a, 8h
@ -22,6 +18,6 @@ main:
LD r31, r254, 0a, 8h LD r31, r254, 0a, 8h
ADDI64 r254, r254, 8d ADDI64 r254, r254, 8d
JALA r0, r31, 0a JALA r0, r31, 0a
code size: 202 code size: 154
ret: 55 ret: 55
status: Ok(()) status: Ok(())

View file

@ -1,10 +1,6 @@
clobber: clobber:
ADDI64 r254, r254, -16d LI64 r3, 4d
ST r31, r254, 0a, 16h ST r3, r2, 0a, 8h
LI64 r32, 4d
ST r32, r2, 0a, 8h
LD r31, r254, 0a, 16h
ADDI64 r254, r254, 16d
JALA r0, r31, 0a JALA r0, r31, 0a
main: main:
ADDI64 r254, r254, -32d ADDI64 r254, r254, -32d
@ -18,6 +14,6 @@ main:
LD r31, r254, 8a, 24h LD r31, r254, 8a, 24h
ADDI64 r254, r254, 32d ADDI64 r254, r254, 32d
JALA r0, r31, 0a JALA r0, r31, 0a
code size: 214 code size: 166
ret: 0 ret: 0
status: Ok(()) status: Ok(())

View file

@ -1,8 +1,4 @@
drop: drop:
ADDI64 r254, r254, -8d
ST r31, r254, 0a, 8h
LD r31, r254, 0a, 8h
ADDI64 r254, r254, 8d
JALA r0, r31, 0a JALA r0, r31, 0a
main: main:
ADDI64 r254, r254, -32d ADDI64 r254, r254, -32d
@ -19,13 +15,9 @@ main:
ADDI64 r254, r254, 32d ADDI64 r254, r254, 32d
JALA r0, r31, 0a JALA r0, r31, 0a
modify: modify:
ADDI64 r254, r254, -16d LI64 r3, 2d
ST r31, r254, 0a, 16h ST r3, r2, 0a, 8h
LI64 r32, 2d
ST r32, r2, 0a, 8h
LD r31, r254, 0a, 16h
ADDI64 r254, r254, 16d
JALA r0, r31, 0a JALA r0, r31, 0a
code size: 283 code size: 187
ret: 0 ret: 0
status: Ok(()) status: Ok(())

View file

@ -1,10 +1,6 @@
main: main:
ADDI64 r254, r254, -8d
ST r31, r254, 0a, 8h
LI64 r1, 0d LI64 r1, 0d
LD r31, r254, 0a, 8h
ADDI64 r254, r254, 8d
JALA r0, r31, 0a JALA r0, r31, 0a
code size: 77 code size: 29
ret: 0 ret: 0
status: Ok(()) status: Ok(())