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(
args.get(1).copied().unwrap_or("main.hb"),
args.iter().filter(|a| !a.starts_with('-')).nth(1).copied().unwrap_or("main.hb"),
hblang::Options {
fmt: args.contains(&"--fmt"),
fmt_current: args.contains(&"--fmt-current"),
fmt_current: args.contains(&"--fmt-stdout"),
dump_asm: args.contains(&"--dump-asm"),
extra_threads: args
.iter()

View file

@ -1994,18 +1994,39 @@ impl Codegen {
ret_rel.apply_jump(&mut self.ci.code, end as _, 0);
}
let mut stripped_prelude_size = 0;
'_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;
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 + 8 + 3, stack, 8);
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::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);

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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