From dc775f35b046a8fd2127580b16813db525a85186 Mon Sep 17 00:00:00 2001 From: homka122 Date: Sun, 9 Nov 2025 10:40:28 +0300 Subject: [PATCH 01/19] Feat: add my malloc function --- PudgeWithMoML/bin/run.t | 6 + PudgeWithMoML/bin/run_after_ll.t | 4 + PudgeWithMoML/bin/run_simplyfied.t | 6 + PudgeWithMoML/lib/frontend/inferencer.ml | 2 + PudgeWithMoML/lib/riscv/codegen.ml | 3 + PudgeWithMoML/lib/runtime/runtime.c | 161 ++++++++++++- PudgeWithMoML/test/codegen.t | 279 +++++++++++++++++++++++ 7 files changed, 457 insertions(+), 4 deletions(-) diff --git a/PudgeWithMoML/bin/run.t b/PudgeWithMoML/bin/run.t index 277502d8..4785218a 100644 --- a/PudgeWithMoML/bin/run.t +++ b/PudgeWithMoML/bin/run.t @@ -175,6 +175,8 @@ _start: mv fp, sp addi sp, sp, -16 + mv a0, sp + call init_GC addi sp, sp, -16 la t5, f_1 li t6, 1 @@ -466,6 +468,8 @@ _start: mv fp, sp addi sp, sp, -16 + mv a0, sp + call init_GC addi sp, sp, -16 la t5, f_2 li t6, 1 @@ -665,6 +669,8 @@ _start: mv fp, sp addi sp, sp, -160 + mv a0, sp + call init_GC # Apply wrap__0 with 11 args addi sp, sp, -16 la t5, wrap__0 diff --git a/PudgeWithMoML/bin/run_after_ll.t b/PudgeWithMoML/bin/run_after_ll.t index 7b1b2702..99c0f6ca 100644 --- a/PudgeWithMoML/bin/run_after_ll.t +++ b/PudgeWithMoML/bin/run_after_ll.t @@ -157,6 +157,8 @@ _start: mv fp, sp addi sp, sp, -16 + mv a0, sp + call init_GC # Apply fac_cps__6 with 2 args # Load args on stack addi sp, sp, -16 @@ -440,6 +442,8 @@ _start: mv fp, sp addi sp, sp, -24 + mv a0, sp + call init_GC # Apply fib__11 with 2 args # Load args on stack addi sp, sp, -16 diff --git a/PudgeWithMoML/bin/run_simplyfied.t b/PudgeWithMoML/bin/run_simplyfied.t index b09316d0..9b8fc1ae 100644 --- a/PudgeWithMoML/bin/run_simplyfied.t +++ b/PudgeWithMoML/bin/run_simplyfied.t @@ -69,6 +69,8 @@ _start: mv fp, sp addi sp, sp, -8 + mv a0, sp + call init_GC # Apply fac__0 with 1 args # Load args on stack addi sp, sp, -16 @@ -175,6 +177,8 @@ _start: mv fp, sp addi sp, sp, -8 + mv a0, sp + call init_GC # Apply fib__0 with 1 args # Load args on stack addi sp, sp, -16 @@ -272,6 +276,8 @@ _start: mv fp, sp addi sp, sp, -136 + mv a0, sp + call init_GC li t0, 0 li t1, 1 sub t0, t0, t1 diff --git a/PudgeWithMoML/lib/frontend/inferencer.ml b/PudgeWithMoML/lib/frontend/inferencer.ml index 595b590a..f45c43c1 100644 --- a/PudgeWithMoML/lib/frontend/inferencer.ml +++ b/PudgeWithMoML/lib/frontend/inferencer.ml @@ -329,6 +329,7 @@ end = struct let uminus_s = Scheme (VarSet.empty, arrow_t [ int_typ ] int_typ) in let bool_not = Scheme (VarSet.empty, arrow_t [ bool_typ ] bool_typ) in let print_int_s = Scheme (VarSet.empty, arrow_t [ int_typ ] unit_typ) in + let print_gs_status_s = Scheme (VarSet.empty, arrow_t [ unit_typ ] unit_typ) in ( Map.of_alist_exn (module String) [ "=", eq_s @@ -347,6 +348,7 @@ end = struct ; "not", bool_not ; "~-", uminus_s ; "print_int", print_int_s + ; "print_gc_status", print_gs_status_s ] , fresh tv ) ;; diff --git a/PudgeWithMoML/lib/riscv/codegen.ml b/PudgeWithMoML/lib/riscv/codegen.ml index 54e71446..6b12fde9 100644 --- a/PudgeWithMoML/lib/riscv/codegen.ml +++ b/PudgeWithMoML/lib/riscv/codegen.ml @@ -304,6 +304,8 @@ let rec gen_cexpr (var_arity : string -> int) dst = function let+ arg_c = gen_imm (A 0) arg in (arg_c @ [ call "print_int" ] @ if dst = A 0 then [] else [ mv dst (A 0) ]) |> comment_wrap "Apply print_int" + | CApp (ImmVar "print_gc_status", ImmConst Unit_lt, []) -> + [ call "print_gc_status" ] |> return | CApp (ImmVar f, arg, args) (* it is full application *) when let arity = var_arity f in @@ -493,6 +495,7 @@ let gather pr : instr list t = @ [ directive ".globl _start"; label "_start" ] @ [ mv fp Sp ] @ [ addi Sp Sp (-frame) ] + @ [ mv (A 0) Sp; call "init_GC" ] @ main_code @ [ call "flush"; li (A 0) 0; li (A 7) 94; ecall ] ;; diff --git a/PudgeWithMoML/lib/runtime/runtime.c b/PudgeWithMoML/lib/runtime/runtime.c index 8ea5782a..8c4022f7 100644 --- a/PudgeWithMoML/lib/runtime/runtime.c +++ b/PudgeWithMoML/lib/runtime/runtime.c @@ -11,18 +11,167 @@ void print_int(int n) { printf("%d\n", n); } void flush() { fflush(stdout); } +// size in words +#define GC_SPACE_INITIAL_SIZE (1600) + +// HEAP structure +// word: value +// +// 0: [N_0 = size in words] +// 1: [data] +// ... [...data] +// N_0: [data] +// N_0 + 1: [N_2 = size in words] +// N_0 + 2: [data] +// ... .... [..data] +// N_0 + (N_2 - 1): [data] + +typedef struct { + void *base_sp; + size_t space_capacity; // dynamic size in words + void **new_space; + size_t alloc_offset; // first free word offset in new space + void **old_space; + size_t alloc_count; + size_t collect_count; +} GC_state; + +static GC_state gc; + typedef struct { void *code; - uint8_t argc; - uint8_t argc_recived; + size_t argc; + size_t argc_recived; void *args[]; } closure; #define ZERO8 0, 0, 0, 0, 0, 0, 0, 0 #define INT8 int, int, int, int, int, int, int, int +// Print stats about Garbage Collector work +void print_gc_status() { + printf("=== GC status ===\n"); + printf("Base stack pointer: %x\n", gc.base_sp); + printf("Current space capacity: %ld\n", gc.space_capacity); + printf("Allocated words in new space: %ld\n", gc.alloc_offset); + + printf("Current new space:\n"); + size_t offset = 0; + while (1) { + size_t size = (size_t)gc.new_space[offset]; + printf("\t0x%x: [size: %ld]\n", offset, size); + offset++; + for (size_t i = 0; i < size; i++) { + printf("\t0x%x: ", offset); + printf("[data: 0x%x]\n", gc.new_space[offset]); + offset++; + } + + if (offset >= gc.alloc_offset) { + break; + } + } + + printf("=== GC status ===\n"); + + return; +} + +// Alloc space for GC, init initial state +void init_GC(INT8, void *base_sp) { + // I have problems with modify global variables in direct way + gc.base_sp = base_sp; + gc.space_capacity = GC_SPACE_INITIAL_SIZE; + gc.new_space = malloc(sizeof(void *) * GC_SPACE_INITIAL_SIZE); + gc.alloc_offset = 0; + gc.old_space = malloc(sizeof(void *) * GC_SPACE_INITIAL_SIZE); + + return; +} + +// if caller function wants to save some regs that my collect_riscv_state +// function may destroy (for ex. during creating regs[26]) then caller puts +// their values on stack. so we don't lose any address that points to object in +// heap +// TODO: riscv calling conventions +static void **collect_riscv_state() { + // t0-t6 (7), a0-a7 (8), s1-s11 (11) + size_t *regs = malloc(sizeof(size_t) * 26); + + asm volatile("sd t0, 0(%0)\n\t" + "sd t1, 8(%0)\n\t" + "sd t2, 16(%0)\n\t" + "sd t3, 24(%0)\n\t" + "sd t4, 32(%0)\n\t" + "sd t5, 40(%0)\n\t" + "sd t6, 48(%0)\n\t" + "sd a0, 56(%0)\n\t" + "sd a1, 64(%0)\n\t" + "sd a2, 72(%0)\n\t" + "sd a3, 80(%0)\n\t" + "sd a4, 88(%0)\n\t" + "sd a5, 96(%0)\n\t" + "sd a6, 104(%0)\n\t" + "sd a7, 112(%0)\n\t" + "sd s1, 120(%0)\n\t" + "sd s2, 128(%0)\n\t" + "sd s3, 136(%0)\n\t" + "sd s4, 144(%0)\n\t" + "sd s5, 152(%0)\n\t" + "sd s6, 160(%0)\n\t" + "sd s7, 168(%0)\n\t" + "sd s8, 176(%0)\n\t" + "sd s9, 184(%0)\n\t" + "sd s10, 192(%0)\n\t" + "sd s11, 200(%0)\n\t" + : + : "r"(regs) + : "memory"); + + return (void **)regs; +} + +// alloc size bytes in gc.memory +void *my_malloc(size_t size) { + // printf("MY MALLOC: %ld\n", size); + if (size == 0) { + return NULL; + } + + // 8 byte rounding + if (size % 8 != 0) { + size = size + (8 - (size % 8)); + } + + size_t words = size / 8; + + // no free space left after alloc size bytes + header + if (gc.alloc_offset + (words + 1) >= gc.space_capacity) { + // gc_collect(); + + // after collecting we still don't have space + if (gc.alloc_offset + (words + 1) >= gc.space_capacity) { + size_t mult = 1; + + while (gc.alloc_offset + (words + 1) >= (gc.space_capacity * mult)) { + mult *= 2; + } + + gc.space_capacity *= mult; + gc.new_space = realloc(gc.new_space, sizeof(void *) * gc.space_capacity); + gc.old_space = realloc(gc.old_space, sizeof(void *) * gc.space_capacity); + } + } + + gc.new_space[gc.alloc_offset++] = (void *)words; + void **result = gc.new_space + gc.alloc_offset; + + gc.alloc_offset += words; + return result; +} + void *alloc_closure(INT8, void *f, uint8_t argc) { - closure *clos = malloc(sizeof(closure) + sizeof(void *) * argc); + closure *clos = my_malloc(sizeof(closure) + sizeof(void *) * argc); clos->code = f; clos->argc = argc; @@ -48,11 +197,15 @@ void *copy_closure(closure *old_clos) { // get closure and apply [argc] arguments to closure void *apply_closure(INT8, closure *old_clos, uint8_t argc, ...) { closure *clos = copy_closure(old_clos); + // printf("CLOS: 0x%x\n", clos); + // print_gc_status(); + // fflush(stdout); va_list list; va_start(list, argc); if (clos->argc_recived + argc > clos->argc) { - fprintf(stderr, "Runtime error: function accept more arguments than expect\n"); + fprintf(stderr, + "Runtime error: function accept more arguments than expect\n"); exit(122); } diff --git a/PudgeWithMoML/test/codegen.t b/PudgeWithMoML/test/codegen.t index ce72fc4b..cc9cf760 100644 --- a/PudgeWithMoML/test/codegen.t +++ b/PudgeWithMoML/test/codegen.t @@ -14,6 +14,8 @@ _start: mv fp, sp addi sp, sp, 0 + mv a0, sp + call init_GC # Apply print_int li a0, 5 call print_int @@ -63,6 +65,8 @@ _start: mv fp, sp addi sp, sp, -8 + mv a0, sp + call init_GC # Apply add__0 with 2 args # Load args on stack addi sp, sp, -16 @@ -136,6 +140,8 @@ _start: mv fp, sp addi sp, sp, -8 + mv a0, sp + call init_GC # Apply homka__0 with 12 args # Load args on stack addi sp, sp, -96 @@ -218,6 +224,8 @@ _start: mv fp, sp addi sp, sp, -8 + mv a0, sp + call init_GC # Apply id__0 with 2 args # Load args on stack addi sp, sp, -16 @@ -315,6 +323,8 @@ _start: mv fp, sp addi sp, sp, -8 + mv a0, sp + call init_GC # Apply app__0 with 2 args # Load args on stack addi sp, sp, -16 @@ -364,6 +374,8 @@ _start: mv fp, sp addi sp, sp, -32 + mv a0, sp + call init_GC li t0, 10 sd t0, -8(fp) li t0, 20 @@ -427,6 +439,8 @@ _start: mv fp, sp addi sp, sp, -32 + mv a0, sp + call init_GC # Partial application add__0 with 1 args # Load args on stack addi sp, sp, -32 @@ -527,6 +541,8 @@ _start: mv fp, sp addi sp, sp, -56 + mv a0, sp + call init_GC # Partial application add__0 with 1 args # Load args on stack addi sp, sp, -32 @@ -635,6 +651,8 @@ _start: mv fp, sp addi sp, sp, 0 + mv a0, sp + call init_GC li t0, 4 la t1, x__0 sd t0, 0(t1) @@ -697,6 +715,8 @@ _start: mv fp, sp addi sp, sp, -16 + mv a0, sp + call init_GC # Partial application add__0 with 1 args # Load args on stack addi sp, sp, -32 @@ -813,6 +833,8 @@ _start: mv fp, sp addi sp, sp, -32 + mv a0, sp + call init_GC # Partial application add__0 with 1 args # Load args on stack addi sp, sp, -32 @@ -959,6 +981,8 @@ _start: mv fp, sp addi sp, sp, -8 + mv a0, sp + call init_GC li t0, 5 la t1, x__0 sd t0, 0(t1) @@ -1025,6 +1049,8 @@ _start: mv fp, sp addi sp, sp, 0 + mv a0, sp + call init_GC li t0, 1 beq t0, zero, L0 li t0, 1 @@ -1186,6 +1212,8 @@ _start: mv fp, sp addi sp, sp, 0 + mv a0, sp + call init_GC call flush li a0, 0 li a7, 94 @@ -1225,6 +1253,221 @@ 3 5 +( alloc inner closure ) + $ make compile opts=-gen_mid --no-print-directory -C .. << 'EOF' + > let wrap f x = f x + > let id x = x + > let homka useless wrap id = + > let my_id = wrap id in + > my_id 5 + > let homs = homka 2 wrap id + > let _ = print_gc_status () + > let main = print_int homs + > EOF + $ qemu-riscv64 -L /usr/riscv64-linux-gnu -cpu rv64 ../main.exe | tee -a results.txt && echo "-----" >> results.txt + === GC status === + Base stack pointer: 1 + Current space capacity: 1600 + Allocated words in new space: 28 + Current new space: + 0x0: [size: 5] + 0x1: [data: 0x106c0] + 0x2: [data: 0x2] + 0x3: [data: 0x0] + 0x4: [data: 0x0] + 0x5: [data: 0x0] + 0x6: [size: 4] + 0x7: [data: 0x106f0] + 0x8: [data: 0x1] + 0x9: [data: 0x0] + 0xa: [data: 0x0] + 0xb: [size: 5] + 0xc: [data: 0x106c0] + 0xd: [data: 0x2] + 0xe: [data: 0x1] + 0xf: [data: 0x142d8] + 0x10: [data: 0x0] + 0x11: [size: 5] + 0x12: [data: 0x106c0] + 0x13: [data: 0x2] + 0x14: [data: 0x2] + 0x15: [data: 0x142d8] + 0x16: [data: 0x5] + 0x17: [size: 4] + 0x18: [data: 0x106f0] + 0x19: [data: 0x1] + 0x1a: [data: 0x1] + 0x1b: [data: 0x5] + === GC status === + 5 + $ cat ../main.anf + let wrap__0 = fun f__1 -> + fun x__2 -> + f__1 x__2 + + + let id__3 = fun x__4 -> + x__4 + + + let homka__5 = fun useless__6 -> + fun wrap__7 -> + fun id__8 -> + let anf_t4 = wrap__7 id__8 in + anf_t4 5 + + + let homs__10 = homka__5 2 wrap__0 id__3 + + + let _ = print_gc_status () + + + let main__11 = print_int homs__10 + $ cat ../main.s + .text + .globl wrap__0 + wrap__0: + addi sp, sp, -24 + sd ra, 16(sp) + sd fp, 8(sp) + addi fp, sp, 24 + # Apply f__1 with 1 args + ld t0, 0(fp) + sd t0, -24(fp) + # Load args on stack + addi sp, sp, -32 + ld t0, -24(fp) + sd t0, 0(sp) + li t0, 1 + sd t0, 8(sp) + ld t0, 8(fp) + sd t0, 16(sp) + # End loading args on stack + call apply_closure + # Free args on stack + addi sp, sp, 32 + # End free args on stack + # End Apply f__1 with 1 args + ld ra, 16(sp) + ld fp, 8(sp) + addi sp, sp, 24 + ret + .globl id__3 + id__3: + addi sp, sp, -16 + sd ra, 8(sp) + sd fp, 0(sp) + addi fp, sp, 16 + ld a0, 0(fp) + ld ra, 8(sp) + ld fp, 0(sp) + addi sp, sp, 16 + ret + .globl homka__5 + homka__5: + addi sp, sp, -40 + sd ra, 32(sp) + sd fp, 24(sp) + addi fp, sp, 40 + # Apply wrap__7 with 1 args + ld t0, 8(fp) + sd t0, -24(fp) + # Load args on stack + addi sp, sp, -32 + ld t0, -24(fp) + sd t0, 0(sp) + li t0, 1 + sd t0, 8(sp) + ld t0, 16(fp) + sd t0, 16(sp) + # End loading args on stack + call apply_closure + # Free args on stack + addi sp, sp, 32 + # End free args on stack + mv t0, a0 + # End Apply wrap__7 with 1 args + sd t0, -32(fp) + # Apply anf_t4 with 1 args + ld t0, -32(fp) + sd t0, -40(fp) + # Load args on stack + addi sp, sp, -32 + ld t0, -40(fp) + sd t0, 0(sp) + li t0, 1 + sd t0, 8(sp) + li t0, 5 + sd t0, 16(sp) + # End loading args on stack + call apply_closure + # Free args on stack + addi sp, sp, 32 + # End free args on stack + # End Apply anf_t4 with 1 args + ld ra, 32(sp) + ld fp, 24(sp) + addi sp, sp, 40 + ret + .globl _start + _start: + mv fp, sp + addi sp, sp, 0 + mv a0, sp + call init_GC + # Apply homka__5 with 3 args + # Load args on stack + addi sp, sp, -32 + li t0, 2 + sd t0, 0(sp) + addi sp, sp, -16 + la t5, wrap__0 + li t6, 2 + sd t5, 0(sp) + sd t6, 8(sp) + call alloc_closure + mv t0, a0 + addi sp, sp, 16 + sd t0, 8(sp) + addi sp, sp, -16 + la t5, id__3 + li t6, 1 + sd t5, 0(sp) + sd t6, 8(sp) + call alloc_closure + mv t0, a0 + addi sp, sp, 16 + sd t0, 16(sp) + # End loading args on stack + call homka__5 + # Free args on stack + addi sp, sp, 32 + # End free args on stack + mv t0, a0 + # End Apply homka__5 with 3 args + la t1, homs__10 + sd t0, 0(t1) + call print_gc_status + la t1, _ + sd t0, 0(t1) + # Apply print_int + la t5, homs__10 + ld a0, 0(t5) + call print_int + mv t0, a0 + # End Apply print_int + la t1, main__11 + sd t0, 0(t1) + call flush + li a0, 0 + li a7, 94 + ecall + .data + main__11: .dword 0 + homs__10: .dword 0 + _: .dword 0 + ( IT MUST BE AT THE END OF THE CRAM TEST ) $ cat results.txt 5 @@ -1263,3 +1506,39 @@ 3 5 ----- + === GC status === + Base stack pointer: 1 + Current space capacity: 1600 + Allocated words in new space: 28 + Current new space: + 0x0: [size: 5] + 0x1: [data: 0x106c0] + 0x2: [data: 0x2] + 0x3: [data: 0x0] + 0x4: [data: 0x0] + 0x5: [data: 0x0] + 0x6: [size: 4] + 0x7: [data: 0x106f0] + 0x8: [data: 0x1] + 0x9: [data: 0x0] + 0xa: [data: 0x0] + 0xb: [size: 5] + 0xc: [data: 0x106c0] + 0xd: [data: 0x2] + 0xe: [data: 0x1] + 0xf: [data: 0x142d8] + 0x10: [data: 0x0] + 0x11: [size: 5] + 0x12: [data: 0x106c0] + 0x13: [data: 0x2] + 0x14: [data: 0x2] + 0x15: [data: 0x142d8] + 0x16: [data: 0x5] + 0x17: [size: 4] + 0x18: [data: 0x106f0] + 0x19: [data: 0x1] + 0x1a: [data: 0x1] + 0x1b: [data: 0x5] + === GC status === + 5 + ----- From ab9efa1c25cc956ba372329c7d318b0b53714212 Mon Sep 17 00:00:00 2001 From: homka122 Date: Sun, 9 Nov 2025 13:31:17 +0300 Subject: [PATCH 02/19] Feat: add collect method and test for runtime --- PudgeWithMoML/lib/runtime/runtime.c | 191 ++++++++++++- PudgeWithMoML/lib/runtime/runtime_test.c | 336 +++++++++++++++++++++++ 2 files changed, 523 insertions(+), 4 deletions(-) create mode 100644 PudgeWithMoML/lib/runtime/runtime_test.c diff --git a/PudgeWithMoML/lib/runtime/runtime.c b/PudgeWithMoML/lib/runtime/runtime.c index 8c4022f7..643d9b9f 100644 --- a/PudgeWithMoML/lib/runtime/runtime.c +++ b/PudgeWithMoML/lib/runtime/runtime.c @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -7,12 +8,12 @@ extern void *call_closure(void *code, uint64_t argc, void **argv); -void print_int(int n) { printf("%d\n", n); } +void print_int(size_t n) { printf("%d\n", n); } void flush() { fflush(stdout); } // size in words -#define GC_SPACE_INITIAL_SIZE (1600) +#define GC_SPACE_INITIAL_SIZE (256) // HEAP structure // word: value @@ -52,6 +53,7 @@ typedef struct { void print_gc_status() { printf("=== GC status ===\n"); printf("Base stack pointer: %x\n", gc.base_sp); + printf("Start address of new space: %x\n", gc.new_space); printf("Current space capacity: %ld\n", gc.space_capacity); printf("Allocated words in new space: %ld\n", gc.alloc_offset); @@ -78,7 +80,7 @@ void print_gc_status() { } // Alloc space for GC, init initial state -void init_GC(INT8, void *base_sp) { +void init_GC(void *base_sp) { // I have problems with modify global variables in direct way gc.base_sp = base_sp; gc.space_capacity = GC_SPACE_INITIAL_SIZE; @@ -131,6 +133,183 @@ static void **collect_riscv_state() { return (void **)regs; } +static void set_riscv_reg(int idx, void *val) { + switch (idx) { + case 0: + asm volatile("mv t0, %0" ::"r"(val)); + break; + case 1: + asm volatile("mv t1, %0" ::"r"(val)); + break; + case 2: + asm volatile("mv t2, %0" ::"r"(val)); + break; + case 3: + asm volatile("mv t3, %0" ::"r"(val)); + break; + case 4: + asm volatile("mv t4, %0" ::"r"(val)); + break; + case 5: + asm volatile("mv t5, %0" ::"r"(val)); + break; + case 6: + asm volatile("mv t6, %0" ::"r"(val)); + break; + case 7: + asm volatile("mv a0, %0" ::"r"(val)); + break; + case 8: + asm volatile("mv a1, %0" ::"r"(val)); + break; + case 9: + asm volatile("mv a2, %0" ::"r"(val)); + break; + case 10: + asm volatile("mv a3, %0" ::"r"(val)); + break; + case 11: + asm volatile("mv a4, %0" ::"r"(val)); + break; + case 12: + asm volatile("mv a5, %0" ::"r"(val)); + break; + case 13: + asm volatile("mv a6, %0" ::"r"(val)); + break; + case 14: + asm volatile("mv a7, %0" ::"r"(val)); + break; + case 15: + asm volatile("mv s1, %0" ::"r"(val)); + break; + case 16: + asm volatile("mv s2, %0" ::"r"(val)); + break; + case 17: + asm volatile("mv s3, %0" ::"r"(val)); + break; + case 18: + asm volatile("mv s4, %0" ::"r"(val)); + break; + case 19: + asm volatile("mv s5, %0" ::"r"(val)); + break; + case 20: + asm volatile("mv s6, %0" ::"r"(val)); + break; + case 21: + asm volatile("mv s7, %0" ::"r"(val)); + break; + case 22: + asm volatile("mv s8, %0" ::"r"(val)); + break; + case 23: + asm volatile("mv s9, %0" ::"r"(val)); + break; + case 24: + asm volatile("mv s10, %0" ::"r"(val)); + break; + case 25: + asm volatile("mv s11, %0" ::"r"(val)); + break; + default: + break; + } +} + +// When we exec gc_collect we have on a heap objects: +// [size 3] [data 0] [data 1] [data 2] [size 1] [data 0] [size 2] ... +// We iterate through heap and try to find poiters to "data 0" on stack\regs +// If we find it in first time: +// 1) move size bytes to the old_space +// 2) save new pointer to old_space +// 3) iterate through stack\regs and replace all pointer to the new +// pointer +void gc_collect() { + if (gc.alloc_offset == 0) { + return; + } + + void **regs = collect_riscv_state(); + void *current_sp = NULL; + asm volatile("mv %0, sp" : "=r"(current_sp)); + + size_t stack_size = (gc.base_sp - current_sp) / 8; + + // printf("STACK: base_sp %x, current_sp %x\n", gc.base_sp, current_sp); + // printf("STACK SIZE: %ld\n", stack_size); + // fflush(stdout); + + void *new_pointer = NULL; + size_t cur_offset = 0; + size_t old_space_offset = 0; + while (cur_offset < gc.alloc_offset) { + size_t cur_size = (size_t)gc.new_space[cur_offset]; + if (cur_size == 0) { + print_gc_status(); + exit(122); + } + void *cur_pointer = gc.new_space + cur_offset + 1; + + // try to find in regs and stack at least one pointer + { + bool found = false; + // regs + for (size_t i = 0; i < 25; i++) { + if (regs[i] == cur_pointer) { + found = true; + } + } + + // stack + for (size_t i = 0; i < stack_size; i++) { + void **byte = (void **)gc.base_sp - i; + if (*byte == cur_pointer) { + found = true; + } + } + + if (!found) { + cur_offset += cur_size; + continue; + } + + // copy to old space + gc.old_space[old_space_offset++] = (void *)cur_size; + new_pointer = gc.old_space + old_space_offset; + for (size_t j = 0; j < cur_size; j++) { + gc.old_space[old_space_offset++] = gc.new_space[cur_offset + 1 + j]; + } + } + + // change all occurences + { + // regs + for (size_t i = 0; i < 25; i++) { + if (regs[i] == cur_pointer) { + set_riscv_reg(i, new_pointer); + } + } + + // stack + for (size_t i = 0; i < stack_size; i++) { + void **byte = (void **)gc.base_sp - i; + if (*byte == cur_pointer) { + *byte = new_pointer; + } + } + } + + cur_offset += cur_size; + } + + void *temp = gc.new_space; + gc.new_space = gc.old_space; + gc.old_space = gc.new_space; + gc.alloc_offset = old_space_offset; +} + // alloc size bytes in gc.memory void *my_malloc(size_t size) { // printf("MY MALLOC: %ld\n", size); @@ -147,10 +326,13 @@ void *my_malloc(size_t size) { // no free space left after alloc size bytes + header if (gc.alloc_offset + (words + 1) >= gc.space_capacity) { - // gc_collect(); + gc_collect(); // after collecting we still don't have space if (gc.alloc_offset + (words + 1) >= gc.space_capacity) { + fprintf(stderr, "panic! overflow memory limits\n"); + fflush(stderr); + exit(122); size_t mult = 1; while (gc.alloc_offset + (words + 1) >= (gc.space_capacity * mult)) { @@ -171,6 +353,7 @@ void *my_malloc(size_t size) { } void *alloc_closure(INT8, void *f, uint8_t argc) { + gc_collect(); closure *clos = my_malloc(sizeof(closure) + sizeof(void *) * argc); clos->code = f; diff --git a/PudgeWithMoML/lib/runtime/runtime_test.c b/PudgeWithMoML/lib/runtime/runtime_test.c new file mode 100644 index 00000000..75c86472 --- /dev/null +++ b/PudgeWithMoML/lib/runtime/runtime_test.c @@ -0,0 +1,336 @@ +#include +#include +#include +#include +#include +#include +#include + +extern void *call_closure(void *code, uint64_t argc, void **argv); + +void print_int(size_t n) { printf("%d\n", n); } + +void flush() { fflush(stdout); } + +// size in words +#define GC_SPACE_INITIAL_SIZE (256) + +// HEAP structure +// word: value +// +// 0: [N_0 = size in words] +// 1: [data] +// ... [...data] +// N_0: [data] +// N_0 + 1: [N_2 = size in words] +// N_0 + 2: [data] +// ... .... [..data] +// N_0 + (N_2 - 1): [data] + +typedef struct { + void *base_sp; + size_t space_capacity; // dynamic size in words + void **new_space; + size_t alloc_offset; // first free word offset in new space + void **old_space; + size_t alloc_count; + size_t collect_count; +} GC_state; + +static GC_state gc; + +// mocked stack and regs +void **my_stack; +void **regs; +void *current_sp; + +typedef struct { + void *code; + size_t argc; + size_t argc_recived; + void *args[]; +} closure; + +#define ZERO8 0, 0, 0, 0, 0, 0, 0, 0 +#define INT8 int, int, int, int, int, int, int, int + +// Print stats about Garbage Collector work +void print_gc_status() { + printf("=== GC status ===\n"); + printf("Base stack pointer: %x\n", gc.base_sp); + printf("Start address of new space: %x\n", gc.new_space); + printf("Current space capacity: %ld\n", gc.space_capacity); + printf("Allocated words in new space: %ld\n", gc.alloc_offset); + + printf("Current new space:\n"); + size_t offset = 0; + while (1) { + size_t size = (size_t)gc.new_space[offset]; + printf("\t0x%x: [size: %ld]\n", offset, size); + offset++; + for (size_t i = 0; i < size; i++) { + printf("\t0x%x: ", offset); + printf("[data: 0x%x]\n", gc.new_space[offset]); + offset++; + } + + if (offset >= gc.alloc_offset) { + break; + } + } + + printf("=== GC status ===\n"); + + return; +} + +void print_stack() { + printf("=== STACK status ===\n"); + size_t stack_size = (gc.base_sp - current_sp) / 8; + printf("STACK SIZE: %ld\n", stack_size); + + for (size_t i = 0; i < stack_size; i++) { + uint64_t *byte = (uint64_t *)gc.base_sp - i; + printf("\t0x%x: 0x%x\n", byte, *byte); + } + + printf("=== STACK status ===\n"); + + return; +} + +// Alloc space for GC, init initial state +void init_GC(void *base_sp) { + // I have problems with modify global variables in direct way + gc.base_sp = base_sp; + gc.space_capacity = GC_SPACE_INITIAL_SIZE; + gc.new_space = malloc(sizeof(void *) * GC_SPACE_INITIAL_SIZE); + gc.alloc_offset = 0; + gc.old_space = malloc(sizeof(void *) * GC_SPACE_INITIAL_SIZE); + + return; +} + +static void **collect_riscv_state() { + void **_regs = malloc(sizeof(void *) * 26); + + regs = _regs; + + return _regs; +} + +static void set_riscv_reg(int idx, void *val) { regs[idx] = val; } + +// When we exec gc_collect we have on a heap objects: +// [size 3] [data 0] [data 1] [data 2] [size 1] [data 0] [size 2] ... +// We iterate through heap and try to find poiters to "data 0" on stack\regs +// If we find it in first time: +// 1) move size bytes to the old_space +// 2) save new pointer to old_space +// 3) iterate through stack\regs and replace all pointer to the new +// pointer +void gc_collect() { + if (gc.alloc_offset == 0) { + return; + } + + size_t stack_size = (gc.base_sp - current_sp) / 8; + + // printf("STACK: base_sp %x, current_sp %x\n", gc.base_sp, current_sp); + // printf("STACK SIZE: %ld\n", stack_size); + // fflush(stdout); + + void *new_pointer = NULL; + size_t cur_offset = 0; + size_t old_space_offset = 0; + while (cur_offset < gc.alloc_offset) { + size_t cur_size = (size_t)gc.new_space[cur_offset]; + if (cur_size == 0) { + print_gc_status(); + exit(122); + } + void *cur_pointer = gc.new_space + cur_offset + 1; + + // try to find in regs and stack at least one pointer + { + bool found = false; + // regs + for (size_t i = 0; i < 25; i++) { + if (regs[i] == cur_pointer) { + found = true; + } + } + + // stack + for (size_t i = 0; i < stack_size; i++) { + void **byte = (void **)gc.base_sp - i; + if (*byte == cur_pointer) { + found = true; + } + } + + if (!found) { + cur_offset += cur_size + 1; + continue; + } + + // copy to old space + gc.old_space[old_space_offset++] = (void *)cur_size; + new_pointer = gc.old_space + old_space_offset; + for (size_t j = 0; j < cur_size; j++) { + gc.old_space[old_space_offset++] = gc.new_space[cur_offset + 1 + j]; + } + } + + // change all occurences + { + // regs + for (size_t i = 0; i < 25; i++) { + if (regs[i] == cur_pointer) { + set_riscv_reg(i, new_pointer); + } + } + + // stack + for (size_t i = 0; i < stack_size; i++) { + void **byte = gc.base_sp - i; + if (*byte == cur_pointer) { + *byte = new_pointer; + } + } + } + + cur_offset += cur_size + 1; + } + + void *temp = gc.new_space; + gc.new_space = gc.old_space; + gc.old_space = gc.new_space; + gc.alloc_offset = old_space_offset; +} + +// alloc size bytes in gc.memory +void *my_malloc(size_t size) { + // printf("MY MALLOC: %ld\n", size); + if (size == 0) { + return NULL; + } + + // 8 byte rounding + if (size % 8 != 0) { + size = size + (8 - (size % 8)); + } + + size_t words = size / 8; + + // no free space left after alloc size bytes + header + if (gc.alloc_offset + (words + 1) >= gc.space_capacity) { + gc_collect(); + + // after collecting we still don't have space + if (gc.alloc_offset + (words + 1) >= gc.space_capacity) { + fprintf(stderr, "panic! overflow memory limits\n"); + fflush(stderr); + exit(122); + size_t mult = 1; + + while (gc.alloc_offset + (words + 1) >= (gc.space_capacity * mult)) { + mult *= 2; + } + + gc.space_capacity *= mult; + gc.new_space = realloc(gc.new_space, sizeof(void *) * gc.space_capacity); + gc.old_space = realloc(gc.old_space, sizeof(void *) * gc.space_capacity); + } + } + + gc.new_space[gc.alloc_offset++] = (void *)words; + void **result = gc.new_space + gc.alloc_offset; + + gc.alloc_offset += words; + return result; +} + +void *alloc_closure(INT8, void *f, uint8_t argc) { + // gc_collect(); + closure *clos = my_malloc(sizeof(closure) + sizeof(void *) * argc); + + clos->code = f; + clos->argc = argc; + clos->argc_recived = 0; + memset(clos->args, 0, sizeof(void *) * argc); + + return clos; +} + +void *copy_closure(closure *old_clos) { + closure *clos = old_clos; + closure *new = alloc_closure(ZERO8, clos->code, clos->argc); + + for (size_t i = 0; i < clos->argc_recived; i++) { + new->args[new->argc_recived++] = clos->args[i]; + } + + return new; +} + +#define WORD_SIZE (8) + +int main(int argc, char **argv) { + my_stack = (void **)malloc(sizeof(void *) * 32); + init_GC(my_stack + 32); + current_sp = my_stack + 32 - 8; + regs = malloc(sizeof(void *) * 26); + + if (0) { + print_gc_status(); + alloc_closure(ZERO8, (void *)0xFF, 2); + alloc_closure(ZERO8, (void *)0xFFF, 3); + alloc_closure(ZERO8, (void *)0xFFFF, 4); + print_gc_status(); + gc_collect(); + // must be empty + print_gc_status(); + } + + if (0) { + print_gc_status(); + void *clos = alloc_closure(ZERO8, (void *)0xFF, 2); + alloc_closure(ZERO8, (void *)0xFFF, 3); + alloc_closure(ZERO8, (void *)0xFFFF, 4); + print_gc_status(); + regs[12] = clos; + gc_collect(); + // must has first closure + print_gc_status(); + } + + if (0) { + print_gc_status(); + alloc_closure(ZERO8, (void *)0xFF, 2); + alloc_closure(ZERO8, (void *)0xFFF, 3); + void *clos = alloc_closure(ZERO8, (void *)0xFFFF, 4); + print_gc_status(); + regs[12] = clos; + gc_collect(); + // must has third closure + print_gc_status(); + } + + if (1) { + print_gc_status(); + void *clos1 = alloc_closure(ZERO8, (void *)0xFF, 2); + alloc_closure(ZERO8, (void *)0xFFF, 3); + void *clos2 = alloc_closure(ZERO8, (void *)0xFFFF, 4); + print_gc_status(); + regs[12] = clos1; + my_stack[32 - 2] = clos2; + print_stack(); + gc_collect(); + // must has first and third closure + print_gc_status(); + } + + printf("Done\n"); + return 0; +} \ No newline at end of file From 764ca9bc8030cf2c3e815d25ac246770ea3ffbdf Mon Sep 17 00:00:00 2001 From: homka122 Date: Sun, 9 Nov 2025 13:35:24 +0300 Subject: [PATCH 03/19] Fix: runtime wrong offset adding --- PudgeWithMoML/lib/runtime/runtime.c | 4 ++-- PudgeWithMoML/lib/runtime/runtime_test.c | 12 +++++++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/PudgeWithMoML/lib/runtime/runtime.c b/PudgeWithMoML/lib/runtime/runtime.c index 643d9b9f..1b4fbacb 100644 --- a/PudgeWithMoML/lib/runtime/runtime.c +++ b/PudgeWithMoML/lib/runtime/runtime.c @@ -271,7 +271,7 @@ void gc_collect() { } if (!found) { - cur_offset += cur_size; + cur_offset += cur_size + 1; continue; } @@ -301,7 +301,7 @@ void gc_collect() { } } - cur_offset += cur_size; + cur_offset += cur_size + 1; } void *temp = gc.new_space; diff --git a/PudgeWithMoML/lib/runtime/runtime_test.c b/PudgeWithMoML/lib/runtime/runtime_test.c index 75c86472..348773f3 100644 --- a/PudgeWithMoML/lib/runtime/runtime_test.c +++ b/PudgeWithMoML/lib/runtime/runtime_test.c @@ -317,7 +317,7 @@ int main(int argc, char **argv) { print_gc_status(); } - if (1) { + if (0) { print_gc_status(); void *clos1 = alloc_closure(ZERO8, (void *)0xFF, 2); alloc_closure(ZERO8, (void *)0xFFF, 3); @@ -331,6 +331,16 @@ int main(int argc, char **argv) { print_gc_status(); } + if (1) { + print_gc_status(); + void *clos1 = alloc_closure(ZERO8, (void *)0xFF, 2); + print_gc_status(); + regs[12] = clos1; + print_stack(); + gc_collect(); + print_gc_status(); + } + printf("Done\n"); return 0; } \ No newline at end of file From 543496e9660910e2e14d65b8c985b7fd8b9b428b Mon Sep 17 00:00:00 2001 From: homka122 Date: Sun, 9 Nov 2025 22:03:36 +0300 Subject: [PATCH 04/19] Feat: add collect to garbage collector --- PudgeWithMoML/lib/frontend/inferencer.ml | 4 + PudgeWithMoML/lib/riscv/codegen.ml | 2 + PudgeWithMoML/lib/runtime/runtime.c | 137 +- PudgeWithMoML/lib/runtime/runtime_test.c | 2 +- PudgeWithMoML/test/GC.t | 1539 ++++++++++++++++++++++ PudgeWithMoML/test/codegen.t | 1158 ++++++++++++---- 6 files changed, 2574 insertions(+), 268 deletions(-) create mode 100644 PudgeWithMoML/test/GC.t diff --git a/PudgeWithMoML/lib/frontend/inferencer.ml b/PudgeWithMoML/lib/frontend/inferencer.ml index f45c43c1..9c618f02 100644 --- a/PudgeWithMoML/lib/frontend/inferencer.ml +++ b/PudgeWithMoML/lib/frontend/inferencer.ml @@ -330,6 +330,8 @@ end = struct let bool_not = Scheme (VarSet.empty, arrow_t [ bool_typ ] bool_typ) in let print_int_s = Scheme (VarSet.empty, arrow_t [ int_typ ] unit_typ) in let print_gs_status_s = Scheme (VarSet.empty, arrow_t [ unit_typ ] unit_typ) in + let gc_collect_s = Scheme (VarSet.empty, arrow_t [ unit_typ ] unit_typ) in + let clear_regs_s = Scheme (VarSet.empty, arrow_t [ unit_typ ] unit_typ) in ( Map.of_alist_exn (module String) [ "=", eq_s @@ -349,6 +351,8 @@ end = struct ; "~-", uminus_s ; "print_int", print_int_s ; "print_gc_status", print_gs_status_s + ; "gc_collect", gc_collect_s + ; "clear_regs", clear_regs_s ] , fresh tv ) ;; diff --git a/PudgeWithMoML/lib/riscv/codegen.ml b/PudgeWithMoML/lib/riscv/codegen.ml index 6b12fde9..21372ec2 100644 --- a/PudgeWithMoML/lib/riscv/codegen.ml +++ b/PudgeWithMoML/lib/riscv/codegen.ml @@ -306,6 +306,8 @@ let rec gen_cexpr (var_arity : string -> int) dst = function |> comment_wrap "Apply print_int" | CApp (ImmVar "print_gc_status", ImmConst Unit_lt, []) -> [ call "print_gc_status" ] |> return + | CApp (ImmVar "gc_collect", ImmConst Unit_lt, []) -> [ call "gc_collect" ] |> return + | CApp (ImmVar "clear_regs", ImmConst Unit_lt, []) -> [ call "clear_regs" ] |> return | CApp (ImmVar f, arg, args) (* it is full application *) when let arity = var_arity f in diff --git a/PudgeWithMoML/lib/runtime/runtime.c b/PudgeWithMoML/lib/runtime/runtime.c index 1b4fbacb..5674d402 100644 --- a/PudgeWithMoML/lib/runtime/runtime.c +++ b/PudgeWithMoML/lib/runtime/runtime.c @@ -6,6 +6,14 @@ #include #include +#if true +#define LOG(fmt, ...) printf(fmt, ##__VA_ARGS__) +#define LOGF(fun) fun +#else +#define LOG(fmt, ...) ((void)0) +#define LOGF(fun) ((void)0) +#endif + extern void *call_closure(void *code, uint64_t argc, void **argv); void print_int(size_t n) { printf("%d\n", n); } @@ -14,6 +22,7 @@ void flush() { fflush(stdout); } // size in words #define GC_SPACE_INITIAL_SIZE (256) +#define WORD_SIZE (8) // HEAP structure // word: value @@ -34,6 +43,7 @@ typedef struct { size_t alloc_offset; // first free word offset in new space void **old_space; size_t alloc_count; + size_t allocated_bytes_count; size_t collect_count; } GC_state; @@ -55,23 +65,24 @@ void print_gc_status() { printf("Base stack pointer: %x\n", gc.base_sp); printf("Start address of new space: %x\n", gc.new_space); printf("Current space capacity: %ld\n", gc.space_capacity); + printf("Allocate count: %ld\n", gc.alloc_count); + printf("Allocated memory count: %ld bytes\n", gc.allocated_bytes_count); + printf("Collect count: %ld\n", gc.collect_count); printf("Allocated words in new space: %ld\n", gc.alloc_offset); printf("Current new space:\n"); size_t offset = 0; - while (1) { + while (offset < gc.alloc_offset) { size_t size = (size_t)gc.new_space[offset]; + printf("\t0x%x: [size: %ld]\n", offset, size); offset++; + for (size_t i = 0; i < size; i++) { printf("\t0x%x: ", offset); printf("[data: 0x%x]\n", gc.new_space[offset]); offset++; } - - if (offset >= gc.alloc_offset) { - break; - } } printf("=== GC status ===\n"); @@ -91,10 +102,44 @@ void init_GC(void *base_sp) { return; } +// clear all registers +void clear_regs() { + // t0-t6 (7), a0-a7 (8), s1-s11 (11) + + asm volatile("li t0, 0\n\t" + "li t1, 0\n\t" + "li t2, 0\n\t" + "li t3, 0\n\t" + "li t4, 0\n\t" + "li t5, 0\n\t" + "li t6, 0\n\t" + "li a0, 0\n\t" + "li a1, 0\n\t" + "li a2, 0\n\t" + "li a3, 0\n\t" + "li a4, 0\n\t" + "li a5, 0\n\t" + "li a6, 0\n\t" + "li a7, 0\n\t" + "li s1, 0\n\t" + "li s2, 0\n\t" + "li s3, 0\n\t" + "li s4, 0\n\t" + "li s5, 0\n\t" + "li s6, 0\n\t" + "li s7, 0\n\t" + "li s8, 0\n\t" + "li s9, 0\n\t" + "li s10, 0\n\t" + "li s11, 0\n\t"); + + return; +} + // if caller function wants to save some regs that my collect_riscv_state // function may destroy (for ex. during creating regs[26]) then caller puts -// their values on stack. so we don't lose any address that points to object in -// heap +// their values on stack. so we don't lose any address that points to object +// in heap // TODO: riscv calling conventions static void **collect_riscv_state() { // t0-t6 (7), a0-a7 (8), s1-s11 (11) @@ -218,6 +263,22 @@ static void set_riscv_reg(int idx, void *val) { } } +void print_stack(void *current_sp) { + printf("=== STACK status ===\n"); + printf("BASE_SP: 0x%x, CURRENT_SP: 0x%x\n", gc.base_sp, current_sp); + size_t stack_size = (gc.base_sp - current_sp) / 8; + printf("STACK SIZE: %ld\n", stack_size); + + for (size_t i = 0; i < stack_size; i++) { + uint64_t *byte = (uint64_t *)gc.base_sp - i; + printf("\t0x%x: 0x%x\n", byte, *byte); + } + + printf("=== STACK status ===\n"); + + return; +} + // When we exec gc_collect we have on a heap objects: // [size 3] [data 0] [data 1] [data 2] [size 1] [data 0] [size 2] ... // We iterate through heap and try to find poiters to "data 0" on stack\regs @@ -226,39 +287,42 @@ static void set_riscv_reg(int idx, void *val) { // 2) save new pointer to old_space // 3) iterate through stack\regs and replace all pointer to the new // pointer -void gc_collect() { +void _gc_collect(void *current_sp) { if (gc.alloc_offset == 0) { return; } void **regs = collect_riscv_state(); - void *current_sp = NULL; - asm volatile("mv %0, sp" : "=r"(current_sp)); size_t stack_size = (gc.base_sp - current_sp) / 8; - - // printf("STACK: base_sp %x, current_sp %x\n", gc.base_sp, current_sp); - // printf("STACK SIZE: %ld\n", stack_size); - // fflush(stdout); - - void *new_pointer = NULL; size_t cur_offset = 0; size_t old_space_offset = 0; while (cur_offset < gc.alloc_offset) { + void *new_pointer = NULL; size_t cur_size = (size_t)gc.new_space[cur_offset]; + void *cur_pointer = gc.new_space + cur_offset + 1; + if (cur_size == 0) { + fprintf( + stderr, + "You have object on heap with zero size\nBug in malloc function!\n"); print_gc_status(); exit(122); } - void *cur_pointer = gc.new_space + cur_offset + 1; + + LOG("Try to find stack cell with 0x%x value on 0x%ld offset\n", cur_pointer, + cur_offset); // try to find in regs and stack at least one pointer { bool found = false; + // regs for (size_t i = 0; i < 25; i++) { if (regs[i] == cur_pointer) { + LOG("FOUND AT REG: %ld\n", i); found = true; + break; } } @@ -266,7 +330,11 @@ void gc_collect() { for (size_t i = 0; i < stack_size; i++) { void **byte = (void **)gc.base_sp - i; if (*byte == cur_pointer) { + LOG("FOUND AT STACK: %ld. CUR_OFFSET: %x, CUR_POINTER: %x, byte: " + "%x, *byte: %x\n", + i, cur_offset, cur_pointer, byte, *byte); found = true; + break; } } @@ -276,13 +344,16 @@ void gc_collect() { } // copy to old space - gc.old_space[old_space_offset++] = (void *)cur_size; new_pointer = gc.old_space + old_space_offset; + gc.old_space[old_space_offset++] = (void *)cur_size; for (size_t j = 0; j < cur_size; j++) { gc.old_space[old_space_offset++] = gc.new_space[cur_offset + 1 + j]; } + LOG("NEW POINTER: 0x%x\n", new_pointer); } + LOG("RUN CHANGING\n"); + LOGF(print_stack(current_sp)); // change all occurences { // regs @@ -296,18 +367,33 @@ void gc_collect() { for (size_t i = 0; i < stack_size; i++) { void **byte = (void **)gc.base_sp - i; if (*byte == cur_pointer) { + LOG("Change stack cell 0x%x. 0x%x -> 0x%x\n", byte, *byte, + new_pointer); *byte = new_pointer; } } } + LOGF(print_stack(current_sp)); cur_offset += cur_size + 1; } void *temp = gc.new_space; gc.new_space = gc.old_space; - gc.old_space = gc.new_space; + gc.old_space = temp; gc.alloc_offset = old_space_offset; + + gc.collect_count++; +} + +// WARNING: if you read stack pointer in _gc_collect function than when you go +// through stack you can change local variables of _gc_collect fuction +// So we write wrapper only for reading stack pointer **before** _gc_collect +// function It took 4 hours for debug this chaos 🐣🐣🐤🐤🐔🐔🦆🦆🐹🐹🐹🐹 +void gc_collect() { + void *current_sp = NULL; + asm volatile("mv %0, sp" : "=r"(current_sp)); + _gc_collect(current_sp); } // alloc size bytes in gc.memory @@ -349,11 +435,12 @@ void *my_malloc(size_t size) { void **result = gc.new_space + gc.alloc_offset; gc.alloc_offset += words; + gc.alloc_count++; + gc.allocated_bytes_count += size + WORD_SIZE; return result; } void *alloc_closure(INT8, void *f, uint8_t argc) { - gc_collect(); closure *clos = my_malloc(sizeof(closure) + sizeof(void *) * argc); clos->code = f; @@ -375,19 +462,17 @@ void *copy_closure(closure *old_clos) { return new; } -#define WORD_SIZE (8) - // get closure and apply [argc] arguments to closure void *apply_closure(INT8, closure *old_clos, uint8_t argc, ...) { closure *clos = copy_closure(old_clos); - // printf("CLOS: 0x%x\n", clos); - // print_gc_status(); - // fflush(stdout); + printf("CLOS: 0x%x\n", clos); + print_gc_status(); + fflush(stdout); va_list list; va_start(list, argc); if (clos->argc_recived + argc > clos->argc) { - fprintf(stderr, + fprintf(stdout, "Runtime error: function accept more arguments than expect\n"); exit(122); } diff --git a/PudgeWithMoML/lib/runtime/runtime_test.c b/PudgeWithMoML/lib/runtime/runtime_test.c index 348773f3..e507c489 100644 --- a/PudgeWithMoML/lib/runtime/runtime_test.c +++ b/PudgeWithMoML/lib/runtime/runtime_test.c @@ -13,7 +13,7 @@ void print_int(size_t n) { printf("%d\n", n); } void flush() { fflush(stdout); } // size in words -#define GC_SPACE_INITIAL_SIZE (256) +#define GC_SPACE_INITIAL_SIZE (160) // HEAP structure // word: value diff --git a/PudgeWithMoML/test/GC.t b/PudgeWithMoML/test/GC.t new file mode 100644 index 00000000..40ff8ea3 --- /dev/null +++ b/PudgeWithMoML/test/GC.t @@ -0,0 +1,1539 @@ +( alloc inner closure ) + $ make compile opts=-gen_mid --no-print-directory -C .. << 'EOF' + > let wrap f x = f x + > let id x = x + > let homka useless wrap id = + > let my_id = wrap id in + > my_id 5 + > let homs = homka 2 wrap id + > let _ = print_gc_status () + > let _ = gc_collect () + > let _ = print_gc_status () + > let main = print_int homs + > EOF + $ qemu-riscv64 -L /usr/riscv64-linux-gnu -cpu rv64 ../main.exe + CLOS: 0x14300 + === GC status === + Base stack pointer: bb0d0e40 + Start address of new space: 142a0 + Current space capacity: 256 + Allocate count: 3 + Allocated memory count: 136 bytes + Collect count: 0 + Allocated words in new space: 17 + Current new space: + 0x0: [size: 5] + 0x1: [data: 0x10670] + 0x2: [data: 0x2] + 0x3: [data: 0x0] + 0x4: [data: 0x0] + 0x5: [data: 0x0] + 0x6: [size: 4] + 0x7: [data: 0x106a0] + 0x8: [data: 0x1] + 0x9: [data: 0x0] + 0xa: [data: 0x0] + 0xb: [size: 5] + 0xc: [data: 0x10670] + 0xd: [data: 0x2] + 0xe: [data: 0x0] + 0xf: [data: 0x0] + 0x10: [data: 0x0] + === GC status === + CLOS: 0x14330 + === GC status === + Base stack pointer: bb0d0e40 + Start address of new space: 142a0 + Current space capacity: 256 + Allocate count: 4 + Allocated memory count: 184 bytes + Collect count: 0 + Allocated words in new space: 23 + Current new space: + 0x0: [size: 5] + 0x1: [data: 0x10670] + 0x2: [data: 0x2] + 0x3: [data: 0x0] + 0x4: [data: 0x0] + 0x5: [data: 0x0] + 0x6: [size: 4] + 0x7: [data: 0x106a0] + 0x8: [data: 0x1] + 0x9: [data: 0x0] + 0xa: [data: 0x0] + 0xb: [size: 5] + 0xc: [data: 0x10670] + 0xd: [data: 0x2] + 0xe: [data: 0x1] + 0xf: [data: 0x142d8] + 0x10: [data: 0x0] + 0x11: [size: 5] + 0x12: [data: 0x10670] + 0x13: [data: 0x2] + 0x14: [data: 0x1] + 0x15: [data: 0x142d8] + 0x16: [data: 0x0] + === GC status === + CLOS: 0x14360 + === GC status === + Base stack pointer: bb0d0e40 + Start address of new space: 142a0 + Current space capacity: 256 + Allocate count: 5 + Allocated memory count: 224 bytes + Collect count: 0 + Allocated words in new space: 28 + Current new space: + 0x0: [size: 5] + 0x1: [data: 0x10670] + 0x2: [data: 0x2] + 0x3: [data: 0x0] + 0x4: [data: 0x0] + 0x5: [data: 0x0] + 0x6: [size: 4] + 0x7: [data: 0x106a0] + 0x8: [data: 0x1] + 0x9: [data: 0x0] + 0xa: [data: 0x0] + 0xb: [size: 5] + 0xc: [data: 0x10670] + 0xd: [data: 0x2] + 0xe: [data: 0x1] + 0xf: [data: 0x142d8] + 0x10: [data: 0x0] + 0x11: [size: 5] + 0x12: [data: 0x10670] + 0x13: [data: 0x2] + 0x14: [data: 0x2] + 0x15: [data: 0x142d8] + 0x16: [data: 0x5] + 0x17: [size: 4] + 0x18: [data: 0x106a0] + 0x19: [data: 0x1] + 0x1a: [data: 0x0] + 0x1b: [data: 0x0] + === GC status === + === GC status === + Base stack pointer: bb0d0e40 + Start address of new space: 142a0 + Current space capacity: 256 + Allocate count: 5 + Allocated memory count: 224 bytes + Collect count: 0 + Allocated words in new space: 28 + Current new space: + 0x0: [size: 5] + 0x1: [data: 0x10670] + 0x2: [data: 0x2] + 0x3: [data: 0x0] + 0x4: [data: 0x0] + 0x5: [data: 0x0] + 0x6: [size: 4] + 0x7: [data: 0x106a0] + 0x8: [data: 0x1] + 0x9: [data: 0x0] + 0xa: [data: 0x0] + 0xb: [size: 5] + 0xc: [data: 0x10670] + 0xd: [data: 0x2] + 0xe: [data: 0x1] + 0xf: [data: 0x142d8] + 0x10: [data: 0x0] + 0x11: [size: 5] + 0x12: [data: 0x10670] + 0x13: [data: 0x2] + 0x14: [data: 0x2] + 0x15: [data: 0x142d8] + 0x16: [data: 0x5] + 0x17: [size: 4] + 0x18: [data: 0x106a0] + 0x19: [data: 0x1] + 0x1a: [data: 0x1] + 0x1b: [data: 0x5] + === GC status === + Try to find stack cell with 0x142a8 value on 0x0 offset + Try to find stack cell with 0x142d8 value on 0x6 offset + Try to find stack cell with 0x14300 value on 0x11 offset + Try to find stack cell with 0x14330 value on 0x17 offset + Try to find stack cell with 0x14360 value on 0x23 offset + === GC status === + Base stack pointer: bb0d0e40 + Start address of new space: 14ab0 + Current space capacity: 256 + Allocate count: 5 + Allocated memory count: 224 bytes + Collect count: 1 + Allocated words in new space: 0 + Current new space: + === GC status === + 5 + $ cat ../main.anf + let wrap__0 = fun f__1 -> + fun x__2 -> + f__1 x__2 + + + let id__3 = fun x__4 -> + x__4 + + + let homka__5 = fun useless__6 -> + fun wrap__7 -> + fun id__8 -> + let anf_t6 = wrap__7 id__8 in + anf_t6 5 + + + let homs__10 = homka__5 2 wrap__0 id__3 + + + let _ = print_gc_status () + + + let _ = gc_collect () + + + let _ = print_gc_status () + + + let main__11 = print_int homs__10 + $ cat ../main.s + .text + .globl wrap__0 + wrap__0: + addi sp, sp, -24 + sd ra, 16(sp) + sd fp, 8(sp) + addi fp, sp, 24 + # Apply f__1 with 1 args + ld t0, 0(fp) + sd t0, -24(fp) + # Load args on stack + addi sp, sp, -32 + ld t0, -24(fp) + sd t0, 0(sp) + li t0, 1 + sd t0, 8(sp) + ld t0, 8(fp) + sd t0, 16(sp) + # End loading args on stack + call apply_closure + # Free args on stack + addi sp, sp, 32 + # End free args on stack + # End Apply f__1 with 1 args + ld ra, 16(sp) + ld fp, 8(sp) + addi sp, sp, 24 + ret + .globl id__3 + id__3: + addi sp, sp, -16 + sd ra, 8(sp) + sd fp, 0(sp) + addi fp, sp, 16 + ld a0, 0(fp) + ld ra, 8(sp) + ld fp, 0(sp) + addi sp, sp, 16 + ret + .globl homka__5 + homka__5: + addi sp, sp, -40 + sd ra, 32(sp) + sd fp, 24(sp) + addi fp, sp, 40 + # Apply wrap__7 with 1 args + ld t0, 8(fp) + sd t0, -24(fp) + # Load args on stack + addi sp, sp, -32 + ld t0, -24(fp) + sd t0, 0(sp) + li t0, 1 + sd t0, 8(sp) + ld t0, 16(fp) + sd t0, 16(sp) + # End loading args on stack + call apply_closure + # Free args on stack + addi sp, sp, 32 + # End free args on stack + mv t0, a0 + # End Apply wrap__7 with 1 args + sd t0, -32(fp) + # Apply anf_t6 with 1 args + ld t0, -32(fp) + sd t0, -40(fp) + # Load args on stack + addi sp, sp, -32 + ld t0, -40(fp) + sd t0, 0(sp) + li t0, 1 + sd t0, 8(sp) + li t0, 5 + sd t0, 16(sp) + # End loading args on stack + call apply_closure + # Free args on stack + addi sp, sp, 32 + # End free args on stack + # End Apply anf_t6 with 1 args + ld ra, 32(sp) + ld fp, 24(sp) + addi sp, sp, 40 + ret + .globl _start + _start: + mv fp, sp + addi sp, sp, 0 + mv a0, sp + call init_GC + # Apply homka__5 with 3 args + # Load args on stack + addi sp, sp, -32 + li t0, 2 + sd t0, 0(sp) + addi sp, sp, -16 + la t5, wrap__0 + li t6, 2 + sd t5, 0(sp) + sd t6, 8(sp) + call alloc_closure + mv t0, a0 + addi sp, sp, 16 + sd t0, 8(sp) + addi sp, sp, -16 + la t5, id__3 + li t6, 1 + sd t5, 0(sp) + sd t6, 8(sp) + call alloc_closure + mv t0, a0 + addi sp, sp, 16 + sd t0, 16(sp) + # End loading args on stack + call homka__5 + # Free args on stack + addi sp, sp, 32 + # End free args on stack + mv t0, a0 + # End Apply homka__5 with 3 args + la t1, homs__10 + sd t0, 0(t1) + call print_gc_status + la t1, _ + sd t0, 0(t1) + call gc_collect + la t1, _ + sd t0, 0(t1) + call print_gc_status + la t1, _ + sd t0, 0(t1) + # Apply print_int + la t5, homs__10 + ld a0, 0(t5) + call print_int + mv t0, a0 + # End Apply print_int + la t1, main__11 + sd t0, 0(t1) + call flush + li a0, 0 + li a7, 94 + ecall + .data + main__11: .dword 0 + homs__10: .dword 0 + _: .dword 0 + +( a lot of collector ) + $ make compile opts=-gen_mid --no-print-directory -C .. << 'EOF' + > let gleb a b = a + b + > let homs = gleb 7 + > let _1 = print_gc_status () + > let _2 = gc_collect () + > let _3 = print_gc_status () + > let _4 = gleb 6 + > let _5 = print_gc_status () + > let _6 = gc_collect () + > let _7 = print_gc_status () + > let _8 = clear_regs () + > let _9 = gc_collect () + > let _10 = print_gc_status () + > let main = print_int 5 + > EOF + $ qemu-riscv64 -L /usr/riscv64-linux-gnu -cpu rv64 ../main.exe + CLOS: 0x142d8 + === GC status === + Base stack pointer: 128cee40 + Start address of new space: 142a0 + Current space capacity: 256 + Allocate count: 2 + Allocated memory count: 96 bytes + Collect count: 0 + Allocated words in new space: 12 + Current new space: + 0x0: [size: 5] + 0x1: [data: 0x10670] + 0x2: [data: 0x2] + 0x3: [data: 0x0] + 0x4: [data: 0x0] + 0x5: [data: 0x0] + 0x6: [size: 5] + 0x7: [data: 0x10670] + 0x8: [data: 0x2] + 0x9: [data: 0x0] + 0xa: [data: 0x0] + 0xb: [data: 0x0] + === GC status === + === GC status === + Base stack pointer: 128cee40 + Start address of new space: 142a0 + Current space capacity: 256 + Allocate count: 2 + Allocated memory count: 96 bytes + Collect count: 0 + Allocated words in new space: 12 + Current new space: + 0x0: [size: 5] + 0x1: [data: 0x10670] + 0x2: [data: 0x2] + 0x3: [data: 0x0] + 0x4: [data: 0x0] + 0x5: [data: 0x0] + 0x6: [size: 5] + 0x7: [data: 0x10670] + 0x8: [data: 0x2] + 0x9: [data: 0x1] + 0xa: [data: 0x7] + 0xb: [data: 0x0] + === GC status === + Try to find stack cell with 0x142a8 value on 0x0 offset + Try to find stack cell with 0x142d8 value on 0x6 offset + === GC status === + Base stack pointer: 128cee40 + Start address of new space: 14ab0 + Current space capacity: 256 + Allocate count: 2 + Allocated memory count: 96 bytes + Collect count: 1 + Allocated words in new space: 0 + Current new space: + === GC status === + CLOS: 0x14ae8 + === GC status === + Base stack pointer: 128cee40 + Start address of new space: 14ab0 + Current space capacity: 256 + Allocate count: 4 + Allocated memory count: 192 bytes + Collect count: 1 + Allocated words in new space: 12 + Current new space: + 0x0: [size: 5] + 0x1: [data: 0x10670] + 0x2: [data: 0x2] + 0x3: [data: 0x0] + 0x4: [data: 0x0] + 0x5: [data: 0x0] + 0x6: [size: 5] + 0x7: [data: 0x10670] + 0x8: [data: 0x2] + 0x9: [data: 0x0] + 0xa: [data: 0x0] + 0xb: [data: 0x0] + === GC status === + === GC status === + Base stack pointer: 128cee40 + Start address of new space: 14ab0 + Current space capacity: 256 + Allocate count: 4 + Allocated memory count: 192 bytes + Collect count: 1 + Allocated words in new space: 12 + Current new space: + 0x0: [size: 5] + 0x1: [data: 0x10670] + 0x2: [data: 0x2] + 0x3: [data: 0x0] + 0x4: [data: 0x0] + 0x5: [data: 0x0] + 0x6: [size: 5] + 0x7: [data: 0x10670] + 0x8: [data: 0x2] + 0x9: [data: 0x1] + 0xa: [data: 0x6] + 0xb: [data: 0x0] + === GC status === + Try to find stack cell with 0x14ab8 value on 0x0 offset + Try to find stack cell with 0x14ae8 value on 0x6 offset + === GC status === + Base stack pointer: 128cee40 + Start address of new space: 142a0 + Current space capacity: 256 + Allocate count: 4 + Allocated memory count: 192 bytes + Collect count: 2 + Allocated words in new space: 0 + Current new space: + === GC status === + === GC status === + Base stack pointer: 128cee40 + Start address of new space: 142a0 + Current space capacity: 256 + Allocate count: 4 + Allocated memory count: 192 bytes + Collect count: 2 + Allocated words in new space: 0 + Current new space: + === GC status === + 5 + +( overflow heap, but collect help ) + $ make compile opts=-gen_mid --no-print-directory -C .. << 'EOF' + > let homka h1 h2 h3 h4 h5 h6 h7 h8 h9 h10 h11 h12 h13 h14 h15 = h1 + > let _ = + > let _ = homka 1 in + > let _ = homka 1 in + > let _ = homka 1 in + > let _ = homka 1 in + > let _ = homka 1 in + > let _ = homka 1 in + > let _ = homka 1 in + > 5 + > let _ = print_gc_status () + > let _ = clear_regs () + > let _ = gc_collect () + > let _ = print_gc_status () + > let main = print_int 5 + > EOF + $ qemu-riscv64 -L /usr/riscv64-linux-gnu -cpu rv64 ../main.exe + CLOS: 0x14340 + === GC status === + Base stack pointer: aa2f5e08 + Start address of new space: 142a0 + Current space capacity: 256 + Allocate count: 2 + Allocated memory count: 304 bytes + Collect count: 0 + Allocated words in new space: 38 + Current new space: + 0x0: [size: 18] + 0x1: [data: 0x10670] + 0x2: [data: 0xf] + 0x3: [data: 0x0] + 0x4: [data: 0x0] + 0x5: [data: 0x0] + 0x6: [data: 0x0] + 0x7: [data: 0x0] + 0x8: [data: 0x0] + 0x9: [data: 0x0] + 0xa: [data: 0x0] + 0xb: [data: 0x0] + 0xc: [data: 0x0] + 0xd: [data: 0x0] + 0xe: [data: 0x0] + 0xf: [data: 0x0] + 0x10: [data: 0x0] + 0x11: [data: 0x0] + 0x12: [data: 0x0] + 0x13: [size: 18] + 0x14: [data: 0x10670] + 0x15: [data: 0xf] + 0x16: [data: 0x0] + 0x17: [data: 0x0] + 0x18: [data: 0x0] + 0x19: [data: 0x0] + 0x1a: [data: 0x0] + 0x1b: [data: 0x0] + 0x1c: [data: 0x0] + 0x1d: [data: 0x0] + 0x1e: [data: 0x0] + 0x1f: [data: 0x0] + 0x20: [data: 0x0] + 0x21: [data: 0x0] + 0x22: [data: 0x0] + 0x23: [data: 0x0] + 0x24: [data: 0x0] + 0x25: [data: 0x0] + === GC status === + CLOS: 0x14470 + === GC status === + Base stack pointer: aa2f5e08 + Start address of new space: 142a0 + Current space capacity: 256 + Allocate count: 4 + Allocated memory count: 608 bytes + Collect count: 0 + Allocated words in new space: 76 + Current new space: + 0x0: [size: 18] + 0x1: [data: 0x10670] + 0x2: [data: 0xf] + 0x3: [data: 0x0] + 0x4: [data: 0x0] + 0x5: [data: 0x0] + 0x6: [data: 0x0] + 0x7: [data: 0x0] + 0x8: [data: 0x0] + 0x9: [data: 0x0] + 0xa: [data: 0x0] + 0xb: [data: 0x0] + 0xc: [data: 0x0] + 0xd: [data: 0x0] + 0xe: [data: 0x0] + 0xf: [data: 0x0] + 0x10: [data: 0x0] + 0x11: [data: 0x0] + 0x12: [data: 0x0] + 0x13: [size: 18] + 0x14: [data: 0x10670] + 0x15: [data: 0xf] + 0x16: [data: 0x1] + 0x17: [data: 0x1] + 0x18: [data: 0x0] + 0x19: [data: 0x0] + 0x1a: [data: 0x0] + 0x1b: [data: 0x0] + 0x1c: [data: 0x0] + 0x1d: [data: 0x0] + 0x1e: [data: 0x0] + 0x1f: [data: 0x0] + 0x20: [data: 0x0] + 0x21: [data: 0x0] + 0x22: [data: 0x0] + 0x23: [data: 0x0] + 0x24: [data: 0x0] + 0x25: [data: 0x0] + 0x26: [size: 18] + 0x27: [data: 0x10670] + 0x28: [data: 0xf] + 0x29: [data: 0x0] + 0x2a: [data: 0x0] + 0x2b: [data: 0x0] + 0x2c: [data: 0x0] + 0x2d: [data: 0x0] + 0x2e: [data: 0x0] + 0x2f: [data: 0x0] + 0x30: [data: 0x0] + 0x31: [data: 0x0] + 0x32: [data: 0x0] + 0x33: [data: 0x0] + 0x34: [data: 0x0] + 0x35: [data: 0x0] + 0x36: [data: 0x0] + 0x37: [data: 0x0] + 0x38: [data: 0x0] + 0x39: [size: 18] + 0x3a: [data: 0x10670] + 0x3b: [data: 0xf] + 0x3c: [data: 0x0] + 0x3d: [data: 0x0] + 0x3e: [data: 0x0] + 0x3f: [data: 0x0] + 0x40: [data: 0x0] + 0x41: [data: 0x0] + 0x42: [data: 0x0] + 0x43: [data: 0x0] + 0x44: [data: 0x0] + 0x45: [data: 0x0] + 0x46: [data: 0x0] + 0x47: [data: 0x0] + 0x48: [data: 0x0] + 0x49: [data: 0x0] + 0x4a: [data: 0x0] + 0x4b: [data: 0x0] + === GC status === + CLOS: 0x145a0 + === GC status === + Base stack pointer: aa2f5e08 + Start address of new space: 142a0 + Current space capacity: 256 + Allocate count: 6 + Allocated memory count: 912 bytes + Collect count: 0 + Allocated words in new space: 114 + Current new space: + 0x0: [size: 18] + 0x1: [data: 0x10670] + 0x2: [data: 0xf] + 0x3: [data: 0x0] + 0x4: [data: 0x0] + 0x5: [data: 0x0] + 0x6: [data: 0x0] + 0x7: [data: 0x0] + 0x8: [data: 0x0] + 0x9: [data: 0x0] + 0xa: [data: 0x0] + 0xb: [data: 0x0] + 0xc: [data: 0x0] + 0xd: [data: 0x0] + 0xe: [data: 0x0] + 0xf: [data: 0x0] + 0x10: [data: 0x0] + 0x11: [data: 0x0] + 0x12: [data: 0x0] + 0x13: [size: 18] + 0x14: [data: 0x10670] + 0x15: [data: 0xf] + 0x16: [data: 0x1] + 0x17: [data: 0x1] + 0x18: [data: 0x0] + 0x19: [data: 0x0] + 0x1a: [data: 0x0] + 0x1b: [data: 0x0] + 0x1c: [data: 0x0] + 0x1d: [data: 0x0] + 0x1e: [data: 0x0] + 0x1f: [data: 0x0] + 0x20: [data: 0x0] + 0x21: [data: 0x0] + 0x22: [data: 0x0] + 0x23: [data: 0x0] + 0x24: [data: 0x0] + 0x25: [data: 0x0] + 0x26: [size: 18] + 0x27: [data: 0x10670] + 0x28: [data: 0xf] + 0x29: [data: 0x0] + 0x2a: [data: 0x0] + 0x2b: [data: 0x0] + 0x2c: [data: 0x0] + 0x2d: [data: 0x0] + 0x2e: [data: 0x0] + 0x2f: [data: 0x0] + 0x30: [data: 0x0] + 0x31: [data: 0x0] + 0x32: [data: 0x0] + 0x33: [data: 0x0] + 0x34: [data: 0x0] + 0x35: [data: 0x0] + 0x36: [data: 0x0] + 0x37: [data: 0x0] + 0x38: [data: 0x0] + 0x39: [size: 18] + 0x3a: [data: 0x10670] + 0x3b: [data: 0xf] + 0x3c: [data: 0x1] + 0x3d: [data: 0x1] + 0x3e: [data: 0x0] + 0x3f: [data: 0x0] + 0x40: [data: 0x0] + 0x41: [data: 0x0] + 0x42: [data: 0x0] + 0x43: [data: 0x0] + 0x44: [data: 0x0] + 0x45: [data: 0x0] + 0x46: [data: 0x0] + 0x47: [data: 0x0] + 0x48: [data: 0x0] + 0x49: [data: 0x0] + 0x4a: [data: 0x0] + 0x4b: [data: 0x0] + 0x4c: [size: 18] + 0x4d: [data: 0x10670] + 0x4e: [data: 0xf] + 0x4f: [data: 0x0] + 0x50: [data: 0x0] + 0x51: [data: 0x0] + 0x52: [data: 0x0] + 0x53: [data: 0x0] + 0x54: [data: 0x0] + 0x55: [data: 0x0] + 0x56: [data: 0x0] + 0x57: [data: 0x0] + 0x58: [data: 0x0] + 0x59: [data: 0x0] + 0x5a: [data: 0x0] + 0x5b: [data: 0x0] + 0x5c: [data: 0x0] + 0x5d: [data: 0x0] + 0x5e: [data: 0x0] + 0x5f: [size: 18] + 0x60: [data: 0x10670] + 0x61: [data: 0xf] + 0x62: [data: 0x0] + 0x63: [data: 0x0] + 0x64: [data: 0x0] + 0x65: [data: 0x0] + 0x66: [data: 0x0] + 0x67: [data: 0x0] + 0x68: [data: 0x0] + 0x69: [data: 0x0] + 0x6a: [data: 0x0] + 0x6b: [data: 0x0] + 0x6c: [data: 0x0] + 0x6d: [data: 0x0] + 0x6e: [data: 0x0] + 0x6f: [data: 0x0] + 0x70: [data: 0x0] + 0x71: [data: 0x0] + === GC status === + CLOS: 0x146d0 + === GC status === + Base stack pointer: aa2f5e08 + Start address of new space: 142a0 + Current space capacity: 256 + Allocate count: 8 + Allocated memory count: 1216 bytes + Collect count: 0 + Allocated words in new space: 152 + Current new space: + 0x0: [size: 18] + 0x1: [data: 0x10670] + 0x2: [data: 0xf] + 0x3: [data: 0x0] + 0x4: [data: 0x0] + 0x5: [data: 0x0] + 0x6: [data: 0x0] + 0x7: [data: 0x0] + 0x8: [data: 0x0] + 0x9: [data: 0x0] + 0xa: [data: 0x0] + 0xb: [data: 0x0] + 0xc: [data: 0x0] + 0xd: [data: 0x0] + 0xe: [data: 0x0] + 0xf: [data: 0x0] + 0x10: [data: 0x0] + 0x11: [data: 0x0] + 0x12: [data: 0x0] + 0x13: [size: 18] + 0x14: [data: 0x10670] + 0x15: [data: 0xf] + 0x16: [data: 0x1] + 0x17: [data: 0x1] + 0x18: [data: 0x0] + 0x19: [data: 0x0] + 0x1a: [data: 0x0] + 0x1b: [data: 0x0] + 0x1c: [data: 0x0] + 0x1d: [data: 0x0] + 0x1e: [data: 0x0] + 0x1f: [data: 0x0] + 0x20: [data: 0x0] + 0x21: [data: 0x0] + 0x22: [data: 0x0] + 0x23: [data: 0x0] + 0x24: [data: 0x0] + 0x25: [data: 0x0] + 0x26: [size: 18] + 0x27: [data: 0x10670] + 0x28: [data: 0xf] + 0x29: [data: 0x0] + 0x2a: [data: 0x0] + 0x2b: [data: 0x0] + 0x2c: [data: 0x0] + 0x2d: [data: 0x0] + 0x2e: [data: 0x0] + 0x2f: [data: 0x0] + 0x30: [data: 0x0] + 0x31: [data: 0x0] + 0x32: [data: 0x0] + 0x33: [data: 0x0] + 0x34: [data: 0x0] + 0x35: [data: 0x0] + 0x36: [data: 0x0] + 0x37: [data: 0x0] + 0x38: [data: 0x0] + 0x39: [size: 18] + 0x3a: [data: 0x10670] + 0x3b: [data: 0xf] + 0x3c: [data: 0x1] + 0x3d: [data: 0x1] + 0x3e: [data: 0x0] + 0x3f: [data: 0x0] + 0x40: [data: 0x0] + 0x41: [data: 0x0] + 0x42: [data: 0x0] + 0x43: [data: 0x0] + 0x44: [data: 0x0] + 0x45: [data: 0x0] + 0x46: [data: 0x0] + 0x47: [data: 0x0] + 0x48: [data: 0x0] + 0x49: [data: 0x0] + 0x4a: [data: 0x0] + 0x4b: [data: 0x0] + 0x4c: [size: 18] + 0x4d: [data: 0x10670] + 0x4e: [data: 0xf] + 0x4f: [data: 0x0] + 0x50: [data: 0x0] + 0x51: [data: 0x0] + 0x52: [data: 0x0] + 0x53: [data: 0x0] + 0x54: [data: 0x0] + 0x55: [data: 0x0] + 0x56: [data: 0x0] + 0x57: [data: 0x0] + 0x58: [data: 0x0] + 0x59: [data: 0x0] + 0x5a: [data: 0x0] + 0x5b: [data: 0x0] + 0x5c: [data: 0x0] + 0x5d: [data: 0x0] + 0x5e: [data: 0x0] + 0x5f: [size: 18] + 0x60: [data: 0x10670] + 0x61: [data: 0xf] + 0x62: [data: 0x1] + 0x63: [data: 0x1] + 0x64: [data: 0x0] + 0x65: [data: 0x0] + 0x66: [data: 0x0] + 0x67: [data: 0x0] + 0x68: [data: 0x0] + 0x69: [data: 0x0] + 0x6a: [data: 0x0] + 0x6b: [data: 0x0] + 0x6c: [data: 0x0] + 0x6d: [data: 0x0] + 0x6e: [data: 0x0] + 0x6f: [data: 0x0] + 0x70: [data: 0x0] + 0x71: [data: 0x0] + 0x72: [size: 18] + 0x73: [data: 0x10670] + 0x74: [data: 0xf] + 0x75: [data: 0x0] + 0x76: [data: 0x0] + 0x77: [data: 0x0] + 0x78: [data: 0x0] + 0x79: [data: 0x0] + 0x7a: [data: 0x0] + 0x7b: [data: 0x0] + 0x7c: [data: 0x0] + 0x7d: [data: 0x0] + 0x7e: [data: 0x0] + 0x7f: [data: 0x0] + 0x80: [data: 0x0] + 0x81: [data: 0x0] + 0x82: [data: 0x0] + 0x83: [data: 0x0] + 0x84: [data: 0x0] + 0x85: [size: 18] + 0x86: [data: 0x10670] + 0x87: [data: 0xf] + 0x88: [data: 0x0] + 0x89: [data: 0x0] + 0x8a: [data: 0x0] + 0x8b: [data: 0x0] + 0x8c: [data: 0x0] + 0x8d: [data: 0x0] + 0x8e: [data: 0x0] + 0x8f: [data: 0x0] + 0x90: [data: 0x0] + 0x91: [data: 0x0] + 0x92: [data: 0x0] + 0x93: [data: 0x0] + 0x94: [data: 0x0] + 0x95: [data: 0x0] + 0x96: [data: 0x0] + 0x97: [data: 0x0] + === GC status === + CLOS: 0x14800 + === GC status === + Base stack pointer: aa2f5e08 + Start address of new space: 142a0 + Current space capacity: 256 + Allocate count: 10 + Allocated memory count: 1520 bytes + Collect count: 0 + Allocated words in new space: 190 + Current new space: + 0x0: [size: 18] + 0x1: [data: 0x10670] + 0x2: [data: 0xf] + 0x3: [data: 0x0] + 0x4: [data: 0x0] + 0x5: [data: 0x0] + 0x6: [data: 0x0] + 0x7: [data: 0x0] + 0x8: [data: 0x0] + 0x9: [data: 0x0] + 0xa: [data: 0x0] + 0xb: [data: 0x0] + 0xc: [data: 0x0] + 0xd: [data: 0x0] + 0xe: [data: 0x0] + 0xf: [data: 0x0] + 0x10: [data: 0x0] + 0x11: [data: 0x0] + 0x12: [data: 0x0] + 0x13: [size: 18] + 0x14: [data: 0x10670] + 0x15: [data: 0xf] + 0x16: [data: 0x1] + 0x17: [data: 0x1] + 0x18: [data: 0x0] + 0x19: [data: 0x0] + 0x1a: [data: 0x0] + 0x1b: [data: 0x0] + 0x1c: [data: 0x0] + 0x1d: [data: 0x0] + 0x1e: [data: 0x0] + 0x1f: [data: 0x0] + 0x20: [data: 0x0] + 0x21: [data: 0x0] + 0x22: [data: 0x0] + 0x23: [data: 0x0] + 0x24: [data: 0x0] + 0x25: [data: 0x0] + 0x26: [size: 18] + 0x27: [data: 0x10670] + 0x28: [data: 0xf] + 0x29: [data: 0x0] + 0x2a: [data: 0x0] + 0x2b: [data: 0x0] + 0x2c: [data: 0x0] + 0x2d: [data: 0x0] + 0x2e: [data: 0x0] + 0x2f: [data: 0x0] + 0x30: [data: 0x0] + 0x31: [data: 0x0] + 0x32: [data: 0x0] + 0x33: [data: 0x0] + 0x34: [data: 0x0] + 0x35: [data: 0x0] + 0x36: [data: 0x0] + 0x37: [data: 0x0] + 0x38: [data: 0x0] + 0x39: [size: 18] + 0x3a: [data: 0x10670] + 0x3b: [data: 0xf] + 0x3c: [data: 0x1] + 0x3d: [data: 0x1] + 0x3e: [data: 0x0] + 0x3f: [data: 0x0] + 0x40: [data: 0x0] + 0x41: [data: 0x0] + 0x42: [data: 0x0] + 0x43: [data: 0x0] + 0x44: [data: 0x0] + 0x45: [data: 0x0] + 0x46: [data: 0x0] + 0x47: [data: 0x0] + 0x48: [data: 0x0] + 0x49: [data: 0x0] + 0x4a: [data: 0x0] + 0x4b: [data: 0x0] + 0x4c: [size: 18] + 0x4d: [data: 0x10670] + 0x4e: [data: 0xf] + 0x4f: [data: 0x0] + 0x50: [data: 0x0] + 0x51: [data: 0x0] + 0x52: [data: 0x0] + 0x53: [data: 0x0] + 0x54: [data: 0x0] + 0x55: [data: 0x0] + 0x56: [data: 0x0] + 0x57: [data: 0x0] + 0x58: [data: 0x0] + 0x59: [data: 0x0] + 0x5a: [data: 0x0] + 0x5b: [data: 0x0] + 0x5c: [data: 0x0] + 0x5d: [data: 0x0] + 0x5e: [data: 0x0] + 0x5f: [size: 18] + 0x60: [data: 0x10670] + 0x61: [data: 0xf] + 0x62: [data: 0x1] + 0x63: [data: 0x1] + 0x64: [data: 0x0] + 0x65: [data: 0x0] + 0x66: [data: 0x0] + 0x67: [data: 0x0] + 0x68: [data: 0x0] + 0x69: [data: 0x0] + 0x6a: [data: 0x0] + 0x6b: [data: 0x0] + 0x6c: [data: 0x0] + 0x6d: [data: 0x0] + 0x6e: [data: 0x0] + 0x6f: [data: 0x0] + 0x70: [data: 0x0] + 0x71: [data: 0x0] + 0x72: [size: 18] + 0x73: [data: 0x10670] + 0x74: [data: 0xf] + 0x75: [data: 0x0] + 0x76: [data: 0x0] + 0x77: [data: 0x0] + 0x78: [data: 0x0] + 0x79: [data: 0x0] + 0x7a: [data: 0x0] + 0x7b: [data: 0x0] + 0x7c: [data: 0x0] + 0x7d: [data: 0x0] + 0x7e: [data: 0x0] + 0x7f: [data: 0x0] + 0x80: [data: 0x0] + 0x81: [data: 0x0] + 0x82: [data: 0x0] + 0x83: [data: 0x0] + 0x84: [data: 0x0] + 0x85: [size: 18] + 0x86: [data: 0x10670] + 0x87: [data: 0xf] + 0x88: [data: 0x1] + 0x89: [data: 0x1] + 0x8a: [data: 0x0] + 0x8b: [data: 0x0] + 0x8c: [data: 0x0] + 0x8d: [data: 0x0] + 0x8e: [data: 0x0] + 0x8f: [data: 0x0] + 0x90: [data: 0x0] + 0x91: [data: 0x0] + 0x92: [data: 0x0] + 0x93: [data: 0x0] + 0x94: [data: 0x0] + 0x95: [data: 0x0] + 0x96: [data: 0x0] + 0x97: [data: 0x0] + 0x98: [size: 18] + 0x99: [data: 0x10670] + 0x9a: [data: 0xf] + 0x9b: [data: 0x0] + 0x9c: [data: 0x0] + 0x9d: [data: 0x0] + 0x9e: [data: 0x0] + 0x9f: [data: 0x0] + 0xa0: [data: 0x0] + 0xa1: [data: 0x0] + 0xa2: [data: 0x0] + 0xa3: [data: 0x0] + 0xa4: [data: 0x0] + 0xa5: [data: 0x0] + 0xa6: [data: 0x0] + 0xa7: [data: 0x0] + 0xa8: [data: 0x0] + 0xa9: [data: 0x0] + 0xaa: [data: 0x0] + 0xab: [size: 18] + 0xac: [data: 0x10670] + 0xad: [data: 0xf] + 0xae: [data: 0x0] + 0xaf: [data: 0x0] + 0xb0: [data: 0x0] + 0xb1: [data: 0x0] + 0xb2: [data: 0x0] + 0xb3: [data: 0x0] + 0xb4: [data: 0x0] + 0xb5: [data: 0x0] + 0xb6: [data: 0x0] + 0xb7: [data: 0x0] + 0xb8: [data: 0x0] + 0xb9: [data: 0x0] + 0xba: [data: 0x0] + 0xbb: [data: 0x0] + 0xbc: [data: 0x0] + 0xbd: [data: 0x0] + === GC status === + CLOS: 0x14930 + === GC status === + Base stack pointer: aa2f5e08 + Start address of new space: 142a0 + Current space capacity: 256 + Allocate count: 12 + Allocated memory count: 1824 bytes + Collect count: 0 + Allocated words in new space: 228 + Current new space: + 0x0: [size: 18] + 0x1: [data: 0x10670] + 0x2: [data: 0xf] + 0x3: [data: 0x0] + 0x4: [data: 0x0] + 0x5: [data: 0x0] + 0x6: [data: 0x0] + 0x7: [data: 0x0] + 0x8: [data: 0x0] + 0x9: [data: 0x0] + 0xa: [data: 0x0] + 0xb: [data: 0x0] + 0xc: [data: 0x0] + 0xd: [data: 0x0] + 0xe: [data: 0x0] + 0xf: [data: 0x0] + 0x10: [data: 0x0] + 0x11: [data: 0x0] + 0x12: [data: 0x0] + 0x13: [size: 18] + 0x14: [data: 0x10670] + 0x15: [data: 0xf] + 0x16: [data: 0x1] + 0x17: [data: 0x1] + 0x18: [data: 0x0] + 0x19: [data: 0x0] + 0x1a: [data: 0x0] + 0x1b: [data: 0x0] + 0x1c: [data: 0x0] + 0x1d: [data: 0x0] + 0x1e: [data: 0x0] + 0x1f: [data: 0x0] + 0x20: [data: 0x0] + 0x21: [data: 0x0] + 0x22: [data: 0x0] + 0x23: [data: 0x0] + 0x24: [data: 0x0] + 0x25: [data: 0x0] + 0x26: [size: 18] + 0x27: [data: 0x10670] + 0x28: [data: 0xf] + 0x29: [data: 0x0] + 0x2a: [data: 0x0] + 0x2b: [data: 0x0] + 0x2c: [data: 0x0] + 0x2d: [data: 0x0] + 0x2e: [data: 0x0] + 0x2f: [data: 0x0] + 0x30: [data: 0x0] + 0x31: [data: 0x0] + 0x32: [data: 0x0] + 0x33: [data: 0x0] + 0x34: [data: 0x0] + 0x35: [data: 0x0] + 0x36: [data: 0x0] + 0x37: [data: 0x0] + 0x38: [data: 0x0] + 0x39: [size: 18] + 0x3a: [data: 0x10670] + 0x3b: [data: 0xf] + 0x3c: [data: 0x1] + 0x3d: [data: 0x1] + 0x3e: [data: 0x0] + 0x3f: [data: 0x0] + 0x40: [data: 0x0] + 0x41: [data: 0x0] + 0x42: [data: 0x0] + 0x43: [data: 0x0] + 0x44: [data: 0x0] + 0x45: [data: 0x0] + 0x46: [data: 0x0] + 0x47: [data: 0x0] + 0x48: [data: 0x0] + 0x49: [data: 0x0] + 0x4a: [data: 0x0] + 0x4b: [data: 0x0] + 0x4c: [size: 18] + 0x4d: [data: 0x10670] + 0x4e: [data: 0xf] + 0x4f: [data: 0x0] + 0x50: [data: 0x0] + 0x51: [data: 0x0] + 0x52: [data: 0x0] + 0x53: [data: 0x0] + 0x54: [data: 0x0] + 0x55: [data: 0x0] + 0x56: [data: 0x0] + 0x57: [data: 0x0] + 0x58: [data: 0x0] + 0x59: [data: 0x0] + 0x5a: [data: 0x0] + 0x5b: [data: 0x0] + 0x5c: [data: 0x0] + 0x5d: [data: 0x0] + 0x5e: [data: 0x0] + 0x5f: [size: 18] + 0x60: [data: 0x10670] + 0x61: [data: 0xf] + 0x62: [data: 0x1] + 0x63: [data: 0x1] + 0x64: [data: 0x0] + 0x65: [data: 0x0] + 0x66: [data: 0x0] + 0x67: [data: 0x0] + 0x68: [data: 0x0] + 0x69: [data: 0x0] + 0x6a: [data: 0x0] + 0x6b: [data: 0x0] + 0x6c: [data: 0x0] + 0x6d: [data: 0x0] + 0x6e: [data: 0x0] + 0x6f: [data: 0x0] + 0x70: [data: 0x0] + 0x71: [data: 0x0] + 0x72: [size: 18] + 0x73: [data: 0x10670] + 0x74: [data: 0xf] + 0x75: [data: 0x0] + 0x76: [data: 0x0] + 0x77: [data: 0x0] + 0x78: [data: 0x0] + 0x79: [data: 0x0] + 0x7a: [data: 0x0] + 0x7b: [data: 0x0] + 0x7c: [data: 0x0] + 0x7d: [data: 0x0] + 0x7e: [data: 0x0] + 0x7f: [data: 0x0] + 0x80: [data: 0x0] + 0x81: [data: 0x0] + 0x82: [data: 0x0] + 0x83: [data: 0x0] + 0x84: [data: 0x0] + 0x85: [size: 18] + 0x86: [data: 0x10670] + 0x87: [data: 0xf] + 0x88: [data: 0x1] + 0x89: [data: 0x1] + 0x8a: [data: 0x0] + 0x8b: [data: 0x0] + 0x8c: [data: 0x0] + 0x8d: [data: 0x0] + 0x8e: [data: 0x0] + 0x8f: [data: 0x0] + 0x90: [data: 0x0] + 0x91: [data: 0x0] + 0x92: [data: 0x0] + 0x93: [data: 0x0] + 0x94: [data: 0x0] + 0x95: [data: 0x0] + 0x96: [data: 0x0] + 0x97: [data: 0x0] + 0x98: [size: 18] + 0x99: [data: 0x10670] + 0x9a: [data: 0xf] + 0x9b: [data: 0x0] + 0x9c: [data: 0x0] + 0x9d: [data: 0x0] + 0x9e: [data: 0x0] + 0x9f: [data: 0x0] + 0xa0: [data: 0x0] + 0xa1: [data: 0x0] + 0xa2: [data: 0x0] + 0xa3: [data: 0x0] + 0xa4: [data: 0x0] + 0xa5: [data: 0x0] + 0xa6: [data: 0x0] + 0xa7: [data: 0x0] + 0xa8: [data: 0x0] + 0xa9: [data: 0x0] + 0xaa: [data: 0x0] + 0xab: [size: 18] + 0xac: [data: 0x10670] + 0xad: [data: 0xf] + 0xae: [data: 0x1] + 0xaf: [data: 0x1] + 0xb0: [data: 0x0] + 0xb1: [data: 0x0] + 0xb2: [data: 0x0] + 0xb3: [data: 0x0] + 0xb4: [data: 0x0] + 0xb5: [data: 0x0] + 0xb6: [data: 0x0] + 0xb7: [data: 0x0] + 0xb8: [data: 0x0] + 0xb9: [data: 0x0] + 0xba: [data: 0x0] + 0xbb: [data: 0x0] + 0xbc: [data: 0x0] + 0xbd: [data: 0x0] + 0xbe: [size: 18] + 0xbf: [data: 0x10670] + 0xc0: [data: 0xf] + 0xc1: [data: 0x0] + 0xc2: [data: 0x0] + 0xc3: [data: 0x0] + 0xc4: [data: 0x0] + 0xc5: [data: 0x0] + 0xc6: [data: 0x0] + 0xc7: [data: 0x0] + 0xc8: [data: 0x0] + 0xc9: [data: 0x0] + 0xca: [data: 0x0] + 0xcb: [data: 0x0] + 0xcc: [data: 0x0] + 0xcd: [data: 0x0] + 0xce: [data: 0x0] + 0xcf: [data: 0x0] + 0xd0: [data: 0x0] + 0xd1: [size: 18] + 0xd2: [data: 0x10670] + 0xd3: [data: 0xf] + 0xd4: [data: 0x0] + 0xd5: [data: 0x0] + 0xd6: [data: 0x0] + 0xd7: [data: 0x0] + 0xd8: [data: 0x0] + 0xd9: [data: 0x0] + 0xda: [data: 0x0] + 0xdb: [data: 0x0] + 0xdc: [data: 0x0] + 0xdd: [data: 0x0] + 0xde: [data: 0x0] + 0xdf: [data: 0x0] + 0xe0: [data: 0x0] + 0xe1: [data: 0x0] + 0xe2: [data: 0x0] + 0xe3: [data: 0x0] + === GC status === + Try to find stack cell with 0x142a8 value on 0x0 offset + Try to find stack cell with 0x14340 value on 0x19 offset + Try to find stack cell with 0x143d8 value on 0x38 offset + Try to find stack cell with 0x14470 value on 0x57 offset + Try to find stack cell with 0x14508 value on 0x76 offset + Try to find stack cell with 0x145a0 value on 0x95 offset + Try to find stack cell with 0x14638 value on 0x114 offset + Try to find stack cell with 0x146d0 value on 0x133 offset + Try to find stack cell with 0x14768 value on 0x152 offset + Try to find stack cell with 0x14800 value on 0x171 offset + Try to find stack cell with 0x14898 value on 0x190 offset + Try to find stack cell with 0x14930 value on 0x209 offset + Try to find stack cell with 0x149c8 value on 0x228 offset + FOUND AT STACK: 4. CUR_OFFSET: e4, CUR_POINTER: 149c8, byte: aa2f5de8, *byte: 149c8 + NEW POINTER: 0x14ab0 + RUN CHANGING + === STACK status === + BASE_SP: 0xaa2f5e08, CURRENT_SP: 0xaa2f5cb8 + STACK SIZE: 42 + 0xaa2f5e08: 0x0 + 0xaa2f5e00: 0x1068e + 0xaa2f5df8: 0x1 + 0xaa2f5df0: 0x1 + 0xaa2f5de8: 0x149c8 + 0xaa2f5de0: 0x107d6 + 0xaa2f5dd8: 0xaa2f5e40 + 0xaa2f5dd0: 0x107c4 + 0xaa2f5dc8: 0xaa2f5e40 + 0xaa2f5dc0: 0x149c8 + 0xaa2f5db8: 0xaa2f5e00 + 0xaa2f5db0: 0x0 + 0xaa2f5da8: 0x14a58 + 0xaa2f5da0: 0x149c8 + 0xaa2f5d98: 0x149e0 + 0xaa2f5d90: 0x11574 + 0xaa2f5d88: 0xaa2f5de8 + 0xaa2f5d80: 0x149c8 + 0xaa2f5d78: 0x149c8 + 0xaa2f5d70: 0x90 + 0xaa2f5d68: 0xa999b400 + 0xaa2f5d60: 0x149c8 + 0xaa2f5d58: 0xaa2f5d98 + 0xaa2f5d50: 0xf + 0xaa2f5d48: 0x10670 + 0xaa2f5d40: 0x114c8 + 0xaa2f5d38: 0xaa2f5d98 + 0xaa2f5d30: 0xa98ad480 + 0xaa2f5d28: 0x209 + 0xaa2f5d20: 0x0 + 0xaa2f5d18: 0x0 + 0xaa2f5d10: 0x0 + 0xaa2f5d08: 0x0 + 0xaa2f5d00: 0x11448 + 0xaa2f5cf8: 0xaa2f5d48 + 0xaa2f5cf0: 0xa99991f8 + 0xaa2f5ce8: 0x12 + 0xaa2f5ce0: 0x90 + 0xaa2f5cd8: 0x0 + 0xaa2f5cd0: 0x1131e + 0xaa2f5cc8: 0xaa2f5d08 + 0xaa2f5cc0: 0xaa2f5cb8 + === STACK status === + Change stack cell 0xaa2f5de8. 0x149c8 -> 0x14ab0 + Change stack cell 0xaa2f5dc0. 0x149c8 -> 0x14ab0 + Change stack cell 0xaa2f5d80. 0x149c8 -> 0x14ab0 + Change stack cell 0xaa2f5d78. 0x149c8 -> 0x14ab0 + Change stack cell 0xaa2f5d60. 0x149c8 -> 0x14ab0 + === STACK status === + BASE_SP: 0xaa2f5e08, CURRENT_SP: 0xaa2f5cb8 + STACK SIZE: 42 + 0xaa2f5e08: 0x0 + 0xaa2f5e00: 0x1068e + 0xaa2f5df8: 0x1 + 0xaa2f5df0: 0x1 + 0xaa2f5de8: 0x14ab0 + 0xaa2f5de0: 0x107d6 + 0xaa2f5dd8: 0xaa2f5e40 + 0xaa2f5dd0: 0x107c4 + 0xaa2f5dc8: 0xaa2f5e40 + 0xaa2f5dc0: 0x14ab0 + 0xaa2f5db8: 0xaa2f5e00 + 0xaa2f5db0: 0x0 + 0xaa2f5da8: 0x14a58 + 0xaa2f5da0: 0x149c8 + 0xaa2f5d98: 0x149e0 + 0xaa2f5d90: 0x11574 + 0xaa2f5d88: 0xaa2f5de8 + 0xaa2f5d80: 0x14ab0 + 0xaa2f5d78: 0x14ab0 + 0xaa2f5d70: 0x90 + 0xaa2f5d68: 0xa999b400 + 0xaa2f5d60: 0x14ab0 + 0xaa2f5d58: 0xaa2f5d98 + 0xaa2f5d50: 0xf + 0xaa2f5d48: 0x10670 + 0xaa2f5d40: 0x114c8 + 0xaa2f5d38: 0xaa2f5d98 + 0xaa2f5d30: 0xa98ad480 + 0xaa2f5d28: 0x209 + 0xaa2f5d20: 0x0 + 0xaa2f5d18: 0x0 + 0xaa2f5d10: 0x0 + 0xaa2f5d08: 0x0 + 0xaa2f5d00: 0x11448 + 0xaa2f5cf8: 0xaa2f5d48 + 0xaa2f5cf0: 0xa99991f8 + 0xaa2f5ce8: 0x12 + 0xaa2f5ce0: 0x90 + 0xaa2f5cd8: 0x0 + 0xaa2f5cd0: 0x1131e + 0xaa2f5cc8: 0xaa2f5d08 + 0xaa2f5cc0: 0xaa2f5cb8 + === STACK status === + CLOS: 0x14b50 + === GC status === + Base stack pointer: aa2f5e08 + Start address of new space: 14ab0 + Current space capacity: 256 + Allocate count: 14 + Allocated memory count: 2128 bytes + Collect count: 1 + Allocated words in new space: 38 + Current new space: + 0x0: [size: 18] + 0x1: [data: 0x10670] + 0x2: [data: 0xf] + 0x3: [data: 0x0] + 0x4: [data: 0x0] + 0x5: [data: 0x0] + 0x6: [data: 0x0] + 0x7: [data: 0x0] + 0x8: [data: 0x0] + 0x9: [data: 0x0] + 0xa: [data: 0x0] + 0xb: [data: 0x0] + 0xc: [data: 0x0] + 0xd: [data: 0x0] + 0xe: [data: 0x0] + 0xf: [data: 0x0] + 0x10: [data: 0x0] + 0x11: [data: 0x0] + 0x12: [data: 0x0] + 0x13: [size: 18] + 0x14: [data: 0x10670] + 0x15: [data: 0xf] + 0x16: [data: 0xf] + 0x17: [data: 0x0] + 0x18: [data: 0x0] + 0x19: [data: 0x0] + 0x1a: [data: 0x0] + 0x1b: [data: 0x0] + 0x1c: [data: 0x0] + 0x1d: [data: 0x0] + 0x1e: [data: 0x0] + 0x1f: [data: 0x0] + 0x20: [data: 0x0] + 0x21: [data: 0x0] + 0x22: [data: 0x0] + 0x23: [data: 0x0] + 0x24: [data: 0x0] + 0x25: [data: 0x0] + === GC status === + Runtime error: function accept more arguments than expect + [122] diff --git a/PudgeWithMoML/test/codegen.t b/PudgeWithMoML/test/codegen.t index cc9cf760..3112b7b1 100644 --- a/PudgeWithMoML/test/codegen.t +++ b/PudgeWithMoML/test/codegen.t @@ -263,6 +263,27 @@ > EOF $ qemu-riscv64 -L /usr/riscv64-linux-gnu -cpu rv64 ../main.exe | tee -a results.txt && echo "-----" >> results.txt + CLOS: 0x142d0 + === GC status === + Base stack pointer: dd28ce28 + Start address of new space: 142a0 + Current space capacity: 256 + Allocate count: 2 + Allocated memory count: 80 bytes + Collect count: 0 + Allocated words in new space: 10 + Current new space: + 0x0: [size: 4] + 0x1: [data: 0x106a0] + 0x2: [data: 0x1] + 0x3: [data: 0x0] + 0x4: [data: 0x0] + 0x5: [size: 4] + 0x6: [data: 0x106a0] + 0x7: [data: 0x1] + 0x8: [data: 0x0] + 0x9: [data: 0x0] + === GC status === 6 $ cat ../main.anf let app__0 = fun f__1 -> @@ -409,6 +430,58 @@ > EOF $ qemu-riscv64 -L /usr/riscv64-linux-gnu -cpu rv64 ../main.exe | tee -a results.txt && echo "-----" >> results.txt + CLOS: 0x142d8 + === GC status === + Base stack pointer: a3c3be10 + Start address of new space: 142a0 + Current space capacity: 256 + Allocate count: 2 + Allocated memory count: 96 bytes + Collect count: 0 + Allocated words in new space: 12 + Current new space: + 0x0: [size: 5] + 0x1: [data: 0x10670] + 0x2: [data: 0x2] + 0x3: [data: 0x0] + 0x4: [data: 0x0] + 0x5: [data: 0x0] + 0x6: [size: 5] + 0x7: [data: 0x10670] + 0x8: [data: 0x2] + 0x9: [data: 0x0] + 0xa: [data: 0x0] + 0xb: [data: 0x0] + === GC status === + CLOS: 0x14308 + === GC status === + Base stack pointer: a3c3be10 + Start address of new space: 142a0 + Current space capacity: 256 + Allocate count: 3 + Allocated memory count: 144 bytes + Collect count: 0 + Allocated words in new space: 18 + Current new space: + 0x0: [size: 5] + 0x1: [data: 0x10670] + 0x2: [data: 0x2] + 0x3: [data: 0x0] + 0x4: [data: 0x0] + 0x5: [data: 0x0] + 0x6: [size: 5] + 0x7: [data: 0x10670] + 0x8: [data: 0x2] + 0x9: [data: 0x1] + 0xa: [data: 0x1] + 0xb: [data: 0x0] + 0xc: [size: 5] + 0xd: [data: 0x10670] + 0xe: [data: 0x2] + 0xf: [data: 0x1] + 0x10: [data: 0x1] + 0x11: [data: 0x0] + === GC status === 122 $ cat ../main.anf let add__0 = fun x__1 -> @@ -507,7 +580,94 @@ > EOF $ qemu-riscv64 -L /usr/riscv64-linux-gnu -cpu rv64 ../main.exe | tee -a results.txt && echo "-----" >> results.txt + CLOS: 0x142d8 + === GC status === + Base stack pointer: 98de0df8 + Start address of new space: 142a0 + Current space capacity: 256 + Allocate count: 2 + Allocated memory count: 96 bytes + Collect count: 0 + Allocated words in new space: 12 + Current new space: + 0x0: [size: 5] + 0x1: [data: 0x10670] + 0x2: [data: 0x2] + 0x3: [data: 0x0] + 0x4: [data: 0x0] + 0x5: [data: 0x0] + 0x6: [size: 5] + 0x7: [data: 0x10670] + 0x8: [data: 0x2] + 0x9: [data: 0x0] + 0xa: [data: 0x0] + 0xb: [data: 0x0] + === GC status === + CLOS: 0x14308 + === GC status === + Base stack pointer: 98de0df8 + Start address of new space: 142a0 + Current space capacity: 256 + Allocate count: 3 + Allocated memory count: 144 bytes + Collect count: 0 + Allocated words in new space: 18 + Current new space: + 0x0: [size: 5] + 0x1: [data: 0x10670] + 0x2: [data: 0x2] + 0x3: [data: 0x0] + 0x4: [data: 0x0] + 0x5: [data: 0x0] + 0x6: [size: 5] + 0x7: [data: 0x10670] + 0x8: [data: 0x2] + 0x9: [data: 0x1] + 0xa: [data: 0x1] + 0xb: [data: 0x0] + 0xc: [size: 5] + 0xd: [data: 0x10670] + 0xe: [data: 0x2] + 0xf: [data: 0x1] + 0x10: [data: 0x1] + 0x11: [data: 0x0] + === GC status === 122 + CLOS: 0x14338 + === GC status === + Base stack pointer: 98de0df8 + Start address of new space: 142a0 + Current space capacity: 256 + Allocate count: 4 + Allocated memory count: 192 bytes + Collect count: 0 + Allocated words in new space: 24 + Current new space: + 0x0: [size: 5] + 0x1: [data: 0x10670] + 0x2: [data: 0x2] + 0x3: [data: 0x0] + 0x4: [data: 0x0] + 0x5: [data: 0x0] + 0x6: [size: 5] + 0x7: [data: 0x10670] + 0x8: [data: 0x2] + 0x9: [data: 0x1] + 0xa: [data: 0x1] + 0xb: [data: 0x0] + 0xc: [size: 5] + 0xd: [data: 0x10670] + 0xe: [data: 0x2] + 0xf: [data: 0x2] + 0x10: [data: 0x1] + 0x11: [data: 0x79] + 0x12: [size: 5] + 0x13: [data: 0x10670] + 0x14: [data: 0x2] + 0x15: [data: 0x1] + 0x16: [data: 0x1] + 0x17: [data: 0x0] + === GC status === 123 $ cat ../main.anf let add__0 = fun x__1 -> @@ -683,6 +843,58 @@ > EOF $ qemu-riscv64 -L /usr/riscv64-linux-gnu -cpu rv64 ../main.exe | tee -a results.txt && echo "-----" >> results.txt + CLOS: 0x142d8 + === GC status === + Base stack pointer: ec7ce20 + Start address of new space: 142a0 + Current space capacity: 256 + Allocate count: 2 + Allocated memory count: 96 bytes + Collect count: 0 + Allocated words in new space: 12 + Current new space: + 0x0: [size: 5] + 0x1: [data: 0x10670] + 0x2: [data: 0x2] + 0x3: [data: 0x0] + 0x4: [data: 0x0] + 0x5: [data: 0x0] + 0x6: [size: 5] + 0x7: [data: 0x10670] + 0x8: [data: 0x2] + 0x9: [data: 0x0] + 0xa: [data: 0x0] + 0xb: [data: 0x0] + === GC status === + CLOS: 0x14308 + === GC status === + Base stack pointer: ec7ce20 + Start address of new space: 142a0 + Current space capacity: 256 + Allocate count: 3 + Allocated memory count: 144 bytes + Collect count: 0 + Allocated words in new space: 18 + Current new space: + 0x0: [size: 5] + 0x1: [data: 0x10670] + 0x2: [data: 0x2] + 0x3: [data: 0x0] + 0x4: [data: 0x0] + 0x5: [data: 0x0] + 0x6: [size: 5] + 0x7: [data: 0x10670] + 0x8: [data: 0x2] + 0x9: [data: 0x1] + 0xa: [data: 0x5] + 0xb: [data: 0x0] + 0xc: [size: 5] + 0xd: [data: 0x10670] + 0xe: [data: 0x2] + 0xf: [data: 0x1] + 0x10: [data: 0x5] + 0x11: [data: 0x0] + === GC status === 122 $ cat ../main.anf let add__0 = fun x__1 -> @@ -788,6 +1000,105 @@ > EOF $ qemu-riscv64 -L /usr/riscv64-linux-gnu -cpu rv64 ../main.exe | tee -a results.txt && echo "-----" >> results.txt + CLOS: 0x142d8 + === GC status === + Base stack pointer: 2b23be10 + Start address of new space: 142a0 + Current space capacity: 256 + Allocate count: 2 + Allocated memory count: 96 bytes + Collect count: 0 + Allocated words in new space: 12 + Current new space: + 0x0: [size: 5] + 0x1: [data: 0x10670] + 0x2: [data: 0x2] + 0x3: [data: 0x0] + 0x4: [data: 0x0] + 0x5: [data: 0x0] + 0x6: [size: 5] + 0x7: [data: 0x10670] + 0x8: [data: 0x2] + 0x9: [data: 0x0] + 0xa: [data: 0x0] + 0xb: [data: 0x0] + === GC status === + CLOS: 0x14338 + === GC status === + Base stack pointer: 2b23be10 + Start address of new space: 142a0 + Current space capacity: 256 + Allocate count: 4 + Allocated memory count: 192 bytes + Collect count: 0 + Allocated words in new space: 24 + Current new space: + 0x0: [size: 5] + 0x1: [data: 0x10670] + 0x2: [data: 0x2] + 0x3: [data: 0x0] + 0x4: [data: 0x0] + 0x5: [data: 0x0] + 0x6: [size: 5] + 0x7: [data: 0x10670] + 0x8: [data: 0x2] + 0x9: [data: 0x1] + 0xa: [data: 0x5] + 0xb: [data: 0x0] + 0xc: [size: 5] + 0xd: [data: 0x10670] + 0xe: [data: 0x2] + 0xf: [data: 0x0] + 0x10: [data: 0x0] + 0x11: [data: 0x0] + 0x12: [size: 5] + 0x13: [data: 0x10670] + 0x14: [data: 0x2] + 0x15: [data: 0x0] + 0x16: [data: 0x0] + 0x17: [data: 0x0] + === GC status === + CLOS: 0x14368 + === GC status === + Base stack pointer: 2b23be10 + Start address of new space: 142a0 + Current space capacity: 256 + Allocate count: 5 + Allocated memory count: 240 bytes + Collect count: 0 + Allocated words in new space: 30 + Current new space: + 0x0: [size: 5] + 0x1: [data: 0x10670] + 0x2: [data: 0x2] + 0x3: [data: 0x0] + 0x4: [data: 0x0] + 0x5: [data: 0x0] + 0x6: [size: 5] + 0x7: [data: 0x10670] + 0x8: [data: 0x2] + 0x9: [data: 0x1] + 0xa: [data: 0x5] + 0xb: [data: 0x0] + 0xc: [size: 5] + 0xd: [data: 0x10670] + 0xe: [data: 0x2] + 0xf: [data: 0x0] + 0x10: [data: 0x0] + 0x11: [data: 0x0] + 0x12: [size: 5] + 0x13: [data: 0x10670] + 0x14: [data: 0x2] + 0x15: [data: 0x1] + 0x16: [data: 0x1] + 0x17: [data: 0x0] + 0x18: [size: 5] + 0x19: [data: 0x10670] + 0x1a: [data: 0x2] + 0x1b: [data: 0x1] + 0x1c: [data: 0x5] + 0x1d: [data: 0x0] + === GC status === 115 122 17 @@ -1224,6 +1535,158 @@ > let _ = print_int (f 5) > EOF $ qemu-riscv64 -L /usr/riscv64-linux-gnu -cpu rv64 ../main.exe | tee -a results.txt && echo "-----" >> results.txt + CLOS: 0x14308 + === GC status === + Base stack pointer: d7ffde10 + Start address of new space: 142a0 + Current space capacity: 256 + Allocate count: 3 + Allocated memory count: 144 bytes + Collect count: 0 + Allocated words in new space: 18 + Current new space: + 0x0: [size: 5] + 0x1: [data: 0x10670] + 0x2: [data: 0x2] + 0x3: [data: 0x0] + 0x4: [data: 0x0] + 0x5: [data: 0x0] + 0x6: [size: 5] + 0x7: [data: 0x1068c] + 0x8: [data: 0x2] + 0x9: [data: 0x0] + 0xa: [data: 0x0] + 0xb: [data: 0x0] + 0xc: [size: 5] + 0xd: [data: 0x1068c] + 0xe: [data: 0x2] + 0xf: [data: 0x0] + 0x10: [data: 0x0] + 0x11: [data: 0x0] + === GC status === + CLOS: 0x14338 + === GC status === + Base stack pointer: d7ffde10 + Start address of new space: 142a0 + Current space capacity: 256 + Allocate count: 4 + Allocated memory count: 192 bytes + Collect count: 0 + Allocated words in new space: 24 + Current new space: + 0x0: [size: 5] + 0x1: [data: 0x10670] + 0x2: [data: 0x2] + 0x3: [data: 0x0] + 0x4: [data: 0x0] + 0x5: [data: 0x0] + 0x6: [size: 5] + 0x7: [data: 0x1068c] + 0x8: [data: 0x2] + 0x9: [data: 0x0] + 0xa: [data: 0x0] + 0xb: [data: 0x0] + 0xc: [size: 5] + 0xd: [data: 0x1068c] + 0xe: [data: 0x2] + 0xf: [data: 0x1] + 0x10: [data: 0x142a8] + 0x11: [data: 0x0] + 0x12: [size: 5] + 0x13: [data: 0x1068c] + 0x14: [data: 0x2] + 0x15: [data: 0x1] + 0x16: [data: 0x142a8] + 0x17: [data: 0x0] + === GC status === + CLOS: 0x14368 + === GC status === + Base stack pointer: d7ffde10 + Start address of new space: 142a0 + Current space capacity: 256 + Allocate count: 5 + Allocated memory count: 240 bytes + Collect count: 0 + Allocated words in new space: 30 + Current new space: + 0x0: [size: 5] + 0x1: [data: 0x10670] + 0x2: [data: 0x2] + 0x3: [data: 0x0] + 0x4: [data: 0x0] + 0x5: [data: 0x0] + 0x6: [size: 5] + 0x7: [data: 0x1068c] + 0x8: [data: 0x2] + 0x9: [data: 0x0] + 0xa: [data: 0x0] + 0xb: [data: 0x0] + 0xc: [size: 5] + 0xd: [data: 0x1068c] + 0xe: [data: 0x2] + 0xf: [data: 0x1] + 0x10: [data: 0x142a8] + 0x11: [data: 0x0] + 0x12: [size: 5] + 0x13: [data: 0x1068c] + 0x14: [data: 0x2] + 0x15: [data: 0x2] + 0x16: [data: 0x142a8] + 0x17: [data: 0x5] + 0x18: [size: 5] + 0x19: [data: 0x10670] + 0x1a: [data: 0x2] + 0x1b: [data: 0x0] + 0x1c: [data: 0x0] + 0x1d: [data: 0x0] + === GC status === + CLOS: 0x14398 + === GC status === + Base stack pointer: d7ffde10 + Start address of new space: 142a0 + Current space capacity: 256 + Allocate count: 6 + Allocated memory count: 288 bytes + Collect count: 0 + Allocated words in new space: 36 + Current new space: + 0x0: [size: 5] + 0x1: [data: 0x10670] + 0x2: [data: 0x2] + 0x3: [data: 0x0] + 0x4: [data: 0x0] + 0x5: [data: 0x0] + 0x6: [size: 5] + 0x7: [data: 0x1068c] + 0x8: [data: 0x2] + 0x9: [data: 0x0] + 0xa: [data: 0x0] + 0xb: [data: 0x0] + 0xc: [size: 5] + 0xd: [data: 0x1068c] + 0xe: [data: 0x2] + 0xf: [data: 0x1] + 0x10: [data: 0x142a8] + 0x11: [data: 0x0] + 0x12: [size: 5] + 0x13: [data: 0x1068c] + 0x14: [data: 0x2] + 0x15: [data: 0x2] + 0x16: [data: 0x142a8] + 0x17: [data: 0x5] + 0x18: [size: 5] + 0x19: [data: 0x10670] + 0x1a: [data: 0x2] + 0x1b: [data: 0x1] + 0x1c: [data: 0x5] + 0x1d: [data: 0x0] + 0x1e: [size: 5] + 0x1f: [data: 0x10670] + 0x20: [data: 0x2] + 0x21: [data: 0x1] + 0x22: [data: 0x5] + 0x23: [data: 0x0] + === GC status === 8 $ cat ../main.anf let f_0 = fun x__1 -> @@ -1253,220 +1716,6 @@ 3 5 -( alloc inner closure ) - $ make compile opts=-gen_mid --no-print-directory -C .. << 'EOF' - > let wrap f x = f x - > let id x = x - > let homka useless wrap id = - > let my_id = wrap id in - > my_id 5 - > let homs = homka 2 wrap id - > let _ = print_gc_status () - > let main = print_int homs - > EOF - $ qemu-riscv64 -L /usr/riscv64-linux-gnu -cpu rv64 ../main.exe | tee -a results.txt && echo "-----" >> results.txt - === GC status === - Base stack pointer: 1 - Current space capacity: 1600 - Allocated words in new space: 28 - Current new space: - 0x0: [size: 5] - 0x1: [data: 0x106c0] - 0x2: [data: 0x2] - 0x3: [data: 0x0] - 0x4: [data: 0x0] - 0x5: [data: 0x0] - 0x6: [size: 4] - 0x7: [data: 0x106f0] - 0x8: [data: 0x1] - 0x9: [data: 0x0] - 0xa: [data: 0x0] - 0xb: [size: 5] - 0xc: [data: 0x106c0] - 0xd: [data: 0x2] - 0xe: [data: 0x1] - 0xf: [data: 0x142d8] - 0x10: [data: 0x0] - 0x11: [size: 5] - 0x12: [data: 0x106c0] - 0x13: [data: 0x2] - 0x14: [data: 0x2] - 0x15: [data: 0x142d8] - 0x16: [data: 0x5] - 0x17: [size: 4] - 0x18: [data: 0x106f0] - 0x19: [data: 0x1] - 0x1a: [data: 0x1] - 0x1b: [data: 0x5] - === GC status === - 5 - $ cat ../main.anf - let wrap__0 = fun f__1 -> - fun x__2 -> - f__1 x__2 - - - let id__3 = fun x__4 -> - x__4 - - - let homka__5 = fun useless__6 -> - fun wrap__7 -> - fun id__8 -> - let anf_t4 = wrap__7 id__8 in - anf_t4 5 - - - let homs__10 = homka__5 2 wrap__0 id__3 - - - let _ = print_gc_status () - - - let main__11 = print_int homs__10 - $ cat ../main.s - .text - .globl wrap__0 - wrap__0: - addi sp, sp, -24 - sd ra, 16(sp) - sd fp, 8(sp) - addi fp, sp, 24 - # Apply f__1 with 1 args - ld t0, 0(fp) - sd t0, -24(fp) - # Load args on stack - addi sp, sp, -32 - ld t0, -24(fp) - sd t0, 0(sp) - li t0, 1 - sd t0, 8(sp) - ld t0, 8(fp) - sd t0, 16(sp) - # End loading args on stack - call apply_closure - # Free args on stack - addi sp, sp, 32 - # End free args on stack - # End Apply f__1 with 1 args - ld ra, 16(sp) - ld fp, 8(sp) - addi sp, sp, 24 - ret - .globl id__3 - id__3: - addi sp, sp, -16 - sd ra, 8(sp) - sd fp, 0(sp) - addi fp, sp, 16 - ld a0, 0(fp) - ld ra, 8(sp) - ld fp, 0(sp) - addi sp, sp, 16 - ret - .globl homka__5 - homka__5: - addi sp, sp, -40 - sd ra, 32(sp) - sd fp, 24(sp) - addi fp, sp, 40 - # Apply wrap__7 with 1 args - ld t0, 8(fp) - sd t0, -24(fp) - # Load args on stack - addi sp, sp, -32 - ld t0, -24(fp) - sd t0, 0(sp) - li t0, 1 - sd t0, 8(sp) - ld t0, 16(fp) - sd t0, 16(sp) - # End loading args on stack - call apply_closure - # Free args on stack - addi sp, sp, 32 - # End free args on stack - mv t0, a0 - # End Apply wrap__7 with 1 args - sd t0, -32(fp) - # Apply anf_t4 with 1 args - ld t0, -32(fp) - sd t0, -40(fp) - # Load args on stack - addi sp, sp, -32 - ld t0, -40(fp) - sd t0, 0(sp) - li t0, 1 - sd t0, 8(sp) - li t0, 5 - sd t0, 16(sp) - # End loading args on stack - call apply_closure - # Free args on stack - addi sp, sp, 32 - # End free args on stack - # End Apply anf_t4 with 1 args - ld ra, 32(sp) - ld fp, 24(sp) - addi sp, sp, 40 - ret - .globl _start - _start: - mv fp, sp - addi sp, sp, 0 - mv a0, sp - call init_GC - # Apply homka__5 with 3 args - # Load args on stack - addi sp, sp, -32 - li t0, 2 - sd t0, 0(sp) - addi sp, sp, -16 - la t5, wrap__0 - li t6, 2 - sd t5, 0(sp) - sd t6, 8(sp) - call alloc_closure - mv t0, a0 - addi sp, sp, 16 - sd t0, 8(sp) - addi sp, sp, -16 - la t5, id__3 - li t6, 1 - sd t5, 0(sp) - sd t6, 8(sp) - call alloc_closure - mv t0, a0 - addi sp, sp, 16 - sd t0, 16(sp) - # End loading args on stack - call homka__5 - # Free args on stack - addi sp, sp, 32 - # End free args on stack - mv t0, a0 - # End Apply homka__5 with 3 args - la t1, homs__10 - sd t0, 0(t1) - call print_gc_status - la t1, _ - sd t0, 0(t1) - # Apply print_int - la t5, homs__10 - ld a0, 0(t5) - call print_int - mv t0, a0 - # End Apply print_int - la t1, main__11 - sd t0, 0(t1) - call flush - li a0, 0 - li a7, 94 - ecall - .data - main__11: .dword 0 - homs__10: .dword 0 - _: .dword 0 ( IT MUST BE AT THE END OF THE CRAM TEST ) $ cat results.txt @@ -1478,20 +1727,331 @@ ----- 5 ----- + CLOS: 0x142d0 + === GC status === + Base stack pointer: dd28ce28 + Start address of new space: 142a0 + Current space capacity: 256 + Allocate count: 2 + Allocated memory count: 80 bytes + Collect count: 0 + Allocated words in new space: 10 + Current new space: + 0x0: [size: 4] + 0x1: [data: 0x106a0] + 0x2: [data: 0x1] + 0x3: [data: 0x0] + 0x4: [data: 0x0] + 0x5: [size: 4] + 0x6: [data: 0x106a0] + 0x7: [data: 0x1] + 0x8: [data: 0x0] + 0x9: [data: 0x0] + === GC status === 6 ----- 20 10 ----- + CLOS: 0x142d8 + === GC status === + Base stack pointer: a3c3be10 + Start address of new space: 142a0 + Current space capacity: 256 + Allocate count: 2 + Allocated memory count: 96 bytes + Collect count: 0 + Allocated words in new space: 12 + Current new space: + 0x0: [size: 5] + 0x1: [data: 0x10670] + 0x2: [data: 0x2] + 0x3: [data: 0x0] + 0x4: [data: 0x0] + 0x5: [data: 0x0] + 0x6: [size: 5] + 0x7: [data: 0x10670] + 0x8: [data: 0x2] + 0x9: [data: 0x0] + 0xa: [data: 0x0] + 0xb: [data: 0x0] + === GC status === + CLOS: 0x14308 + === GC status === + Base stack pointer: a3c3be10 + Start address of new space: 142a0 + Current space capacity: 256 + Allocate count: 3 + Allocated memory count: 144 bytes + Collect count: 0 + Allocated words in new space: 18 + Current new space: + 0x0: [size: 5] + 0x1: [data: 0x10670] + 0x2: [data: 0x2] + 0x3: [data: 0x0] + 0x4: [data: 0x0] + 0x5: [data: 0x0] + 0x6: [size: 5] + 0x7: [data: 0x10670] + 0x8: [data: 0x2] + 0x9: [data: 0x1] + 0xa: [data: 0x1] + 0xb: [data: 0x0] + 0xc: [size: 5] + 0xd: [data: 0x10670] + 0xe: [data: 0x2] + 0xf: [data: 0x1] + 0x10: [data: 0x1] + 0x11: [data: 0x0] + === GC status === 122 ----- + CLOS: 0x142d8 + === GC status === + Base stack pointer: 98de0df8 + Start address of new space: 142a0 + Current space capacity: 256 + Allocate count: 2 + Allocated memory count: 96 bytes + Collect count: 0 + Allocated words in new space: 12 + Current new space: + 0x0: [size: 5] + 0x1: [data: 0x10670] + 0x2: [data: 0x2] + 0x3: [data: 0x0] + 0x4: [data: 0x0] + 0x5: [data: 0x0] + 0x6: [size: 5] + 0x7: [data: 0x10670] + 0x8: [data: 0x2] + 0x9: [data: 0x0] + 0xa: [data: 0x0] + 0xb: [data: 0x0] + === GC status === + CLOS: 0x14308 + === GC status === + Base stack pointer: 98de0df8 + Start address of new space: 142a0 + Current space capacity: 256 + Allocate count: 3 + Allocated memory count: 144 bytes + Collect count: 0 + Allocated words in new space: 18 + Current new space: + 0x0: [size: 5] + 0x1: [data: 0x10670] + 0x2: [data: 0x2] + 0x3: [data: 0x0] + 0x4: [data: 0x0] + 0x5: [data: 0x0] + 0x6: [size: 5] + 0x7: [data: 0x10670] + 0x8: [data: 0x2] + 0x9: [data: 0x1] + 0xa: [data: 0x1] + 0xb: [data: 0x0] + 0xc: [size: 5] + 0xd: [data: 0x10670] + 0xe: [data: 0x2] + 0xf: [data: 0x1] + 0x10: [data: 0x1] + 0x11: [data: 0x0] + === GC status === 122 + CLOS: 0x14338 + === GC status === + Base stack pointer: 98de0df8 + Start address of new space: 142a0 + Current space capacity: 256 + Allocate count: 4 + Allocated memory count: 192 bytes + Collect count: 0 + Allocated words in new space: 24 + Current new space: + 0x0: [size: 5] + 0x1: [data: 0x10670] + 0x2: [data: 0x2] + 0x3: [data: 0x0] + 0x4: [data: 0x0] + 0x5: [data: 0x0] + 0x6: [size: 5] + 0x7: [data: 0x10670] + 0x8: [data: 0x2] + 0x9: [data: 0x1] + 0xa: [data: 0x1] + 0xb: [data: 0x0] + 0xc: [size: 5] + 0xd: [data: 0x10670] + 0xe: [data: 0x2] + 0xf: [data: 0x2] + 0x10: [data: 0x1] + 0x11: [data: 0x79] + 0x12: [size: 5] + 0x13: [data: 0x10670] + 0x14: [data: 0x2] + 0x15: [data: 0x1] + 0x16: [data: 0x1] + 0x17: [data: 0x0] + === GC status === 123 ----- 5 ----- + CLOS: 0x142d8 + === GC status === + Base stack pointer: ec7ce20 + Start address of new space: 142a0 + Current space capacity: 256 + Allocate count: 2 + Allocated memory count: 96 bytes + Collect count: 0 + Allocated words in new space: 12 + Current new space: + 0x0: [size: 5] + 0x1: [data: 0x10670] + 0x2: [data: 0x2] + 0x3: [data: 0x0] + 0x4: [data: 0x0] + 0x5: [data: 0x0] + 0x6: [size: 5] + 0x7: [data: 0x10670] + 0x8: [data: 0x2] + 0x9: [data: 0x0] + 0xa: [data: 0x0] + 0xb: [data: 0x0] + === GC status === + CLOS: 0x14308 + === GC status === + Base stack pointer: ec7ce20 + Start address of new space: 142a0 + Current space capacity: 256 + Allocate count: 3 + Allocated memory count: 144 bytes + Collect count: 0 + Allocated words in new space: 18 + Current new space: + 0x0: [size: 5] + 0x1: [data: 0x10670] + 0x2: [data: 0x2] + 0x3: [data: 0x0] + 0x4: [data: 0x0] + 0x5: [data: 0x0] + 0x6: [size: 5] + 0x7: [data: 0x10670] + 0x8: [data: 0x2] + 0x9: [data: 0x1] + 0xa: [data: 0x5] + 0xb: [data: 0x0] + 0xc: [size: 5] + 0xd: [data: 0x10670] + 0xe: [data: 0x2] + 0xf: [data: 0x1] + 0x10: [data: 0x5] + 0x11: [data: 0x0] + === GC status === 122 ----- + CLOS: 0x142d8 + === GC status === + Base stack pointer: 2b23be10 + Start address of new space: 142a0 + Current space capacity: 256 + Allocate count: 2 + Allocated memory count: 96 bytes + Collect count: 0 + Allocated words in new space: 12 + Current new space: + 0x0: [size: 5] + 0x1: [data: 0x10670] + 0x2: [data: 0x2] + 0x3: [data: 0x0] + 0x4: [data: 0x0] + 0x5: [data: 0x0] + 0x6: [size: 5] + 0x7: [data: 0x10670] + 0x8: [data: 0x2] + 0x9: [data: 0x0] + 0xa: [data: 0x0] + 0xb: [data: 0x0] + === GC status === + CLOS: 0x14338 + === GC status === + Base stack pointer: 2b23be10 + Start address of new space: 142a0 + Current space capacity: 256 + Allocate count: 4 + Allocated memory count: 192 bytes + Collect count: 0 + Allocated words in new space: 24 + Current new space: + 0x0: [size: 5] + 0x1: [data: 0x10670] + 0x2: [data: 0x2] + 0x3: [data: 0x0] + 0x4: [data: 0x0] + 0x5: [data: 0x0] + 0x6: [size: 5] + 0x7: [data: 0x10670] + 0x8: [data: 0x2] + 0x9: [data: 0x1] + 0xa: [data: 0x5] + 0xb: [data: 0x0] + 0xc: [size: 5] + 0xd: [data: 0x10670] + 0xe: [data: 0x2] + 0xf: [data: 0x0] + 0x10: [data: 0x0] + 0x11: [data: 0x0] + 0x12: [size: 5] + 0x13: [data: 0x10670] + 0x14: [data: 0x2] + 0x15: [data: 0x0] + 0x16: [data: 0x0] + 0x17: [data: 0x0] + === GC status === + CLOS: 0x14368 + === GC status === + Base stack pointer: 2b23be10 + Start address of new space: 142a0 + Current space capacity: 256 + Allocate count: 5 + Allocated memory count: 240 bytes + Collect count: 0 + Allocated words in new space: 30 + Current new space: + 0x0: [size: 5] + 0x1: [data: 0x10670] + 0x2: [data: 0x2] + 0x3: [data: 0x0] + 0x4: [data: 0x0] + 0x5: [data: 0x0] + 0x6: [size: 5] + 0x7: [data: 0x10670] + 0x8: [data: 0x2] + 0x9: [data: 0x1] + 0xa: [data: 0x5] + 0xb: [data: 0x0] + 0xc: [size: 5] + 0xd: [data: 0x10670] + 0xe: [data: 0x2] + 0xf: [data: 0x0] + 0x10: [data: 0x0] + 0x11: [data: 0x0] + 0x12: [size: 5] + 0x13: [data: 0x10670] + 0x14: [data: 0x2] + 0x15: [data: 0x1] + 0x16: [data: 0x1] + 0x17: [data: 0x0] + 0x18: [size: 5] + 0x19: [data: 0x10670] + 0x1a: [data: 0x2] + 0x1b: [data: 0x1] + 0x1c: [data: 0x5] + 0x1d: [data: 0x0] + === GC status === 115 122 17 @@ -1501,44 +2061,160 @@ ----- 1 ----- - 8 - ----- - 3 - 5 - ----- + CLOS: 0x14308 === GC status === - Base stack pointer: 1 - Current space capacity: 1600 - Allocated words in new space: 28 + Base stack pointer: d7ffde10 + Start address of new space: 142a0 + Current space capacity: 256 + Allocate count: 3 + Allocated memory count: 144 bytes + Collect count: 0 + Allocated words in new space: 18 Current new space: 0x0: [size: 5] - 0x1: [data: 0x106c0] + 0x1: [data: 0x10670] 0x2: [data: 0x2] 0x3: [data: 0x0] 0x4: [data: 0x0] 0x5: [data: 0x0] - 0x6: [size: 4] - 0x7: [data: 0x106f0] - 0x8: [data: 0x1] + 0x6: [size: 5] + 0x7: [data: 0x1068c] + 0x8: [data: 0x2] 0x9: [data: 0x0] 0xa: [data: 0x0] - 0xb: [size: 5] - 0xc: [data: 0x106c0] - 0xd: [data: 0x2] - 0xe: [data: 0x1] - 0xf: [data: 0x142d8] + 0xb: [data: 0x0] + 0xc: [size: 5] + 0xd: [data: 0x1068c] + 0xe: [data: 0x2] + 0xf: [data: 0x0] 0x10: [data: 0x0] - 0x11: [size: 5] - 0x12: [data: 0x106c0] - 0x13: [data: 0x2] + 0x11: [data: 0x0] + === GC status === + CLOS: 0x14338 + === GC status === + Base stack pointer: d7ffde10 + Start address of new space: 142a0 + Current space capacity: 256 + Allocate count: 4 + Allocated memory count: 192 bytes + Collect count: 0 + Allocated words in new space: 24 + Current new space: + 0x0: [size: 5] + 0x1: [data: 0x10670] + 0x2: [data: 0x2] + 0x3: [data: 0x0] + 0x4: [data: 0x0] + 0x5: [data: 0x0] + 0x6: [size: 5] + 0x7: [data: 0x1068c] + 0x8: [data: 0x2] + 0x9: [data: 0x0] + 0xa: [data: 0x0] + 0xb: [data: 0x0] + 0xc: [size: 5] + 0xd: [data: 0x1068c] + 0xe: [data: 0x2] + 0xf: [data: 0x1] + 0x10: [data: 0x142a8] + 0x11: [data: 0x0] + 0x12: [size: 5] + 0x13: [data: 0x1068c] + 0x14: [data: 0x2] + 0x15: [data: 0x1] + 0x16: [data: 0x142a8] + 0x17: [data: 0x0] + === GC status === + CLOS: 0x14368 + === GC status === + Base stack pointer: d7ffde10 + Start address of new space: 142a0 + Current space capacity: 256 + Allocate count: 5 + Allocated memory count: 240 bytes + Collect count: 0 + Allocated words in new space: 30 + Current new space: + 0x0: [size: 5] + 0x1: [data: 0x10670] + 0x2: [data: 0x2] + 0x3: [data: 0x0] + 0x4: [data: 0x0] + 0x5: [data: 0x0] + 0x6: [size: 5] + 0x7: [data: 0x1068c] + 0x8: [data: 0x2] + 0x9: [data: 0x0] + 0xa: [data: 0x0] + 0xb: [data: 0x0] + 0xc: [size: 5] + 0xd: [data: 0x1068c] + 0xe: [data: 0x2] + 0xf: [data: 0x1] + 0x10: [data: 0x142a8] + 0x11: [data: 0x0] + 0x12: [size: 5] + 0x13: [data: 0x1068c] + 0x14: [data: 0x2] + 0x15: [data: 0x2] + 0x16: [data: 0x142a8] + 0x17: [data: 0x5] + 0x18: [size: 5] + 0x19: [data: 0x10670] + 0x1a: [data: 0x2] + 0x1b: [data: 0x0] + 0x1c: [data: 0x0] + 0x1d: [data: 0x0] + === GC status === + CLOS: 0x14398 + === GC status === + Base stack pointer: d7ffde10 + Start address of new space: 142a0 + Current space capacity: 256 + Allocate count: 6 + Allocated memory count: 288 bytes + Collect count: 0 + Allocated words in new space: 36 + Current new space: + 0x0: [size: 5] + 0x1: [data: 0x10670] + 0x2: [data: 0x2] + 0x3: [data: 0x0] + 0x4: [data: 0x0] + 0x5: [data: 0x0] + 0x6: [size: 5] + 0x7: [data: 0x1068c] + 0x8: [data: 0x2] + 0x9: [data: 0x0] + 0xa: [data: 0x0] + 0xb: [data: 0x0] + 0xc: [size: 5] + 0xd: [data: 0x1068c] + 0xe: [data: 0x2] + 0xf: [data: 0x1] + 0x10: [data: 0x142a8] + 0x11: [data: 0x0] + 0x12: [size: 5] + 0x13: [data: 0x1068c] 0x14: [data: 0x2] - 0x15: [data: 0x142d8] - 0x16: [data: 0x5] - 0x17: [size: 4] - 0x18: [data: 0x106f0] - 0x19: [data: 0x1] - 0x1a: [data: 0x1] - 0x1b: [data: 0x5] + 0x15: [data: 0x2] + 0x16: [data: 0x142a8] + 0x17: [data: 0x5] + 0x18: [size: 5] + 0x19: [data: 0x10670] + 0x1a: [data: 0x2] + 0x1b: [data: 0x1] + 0x1c: [data: 0x5] + 0x1d: [data: 0x0] + 0x1e: [size: 5] + 0x1f: [data: 0x10670] + 0x20: [data: 0x2] + 0x21: [data: 0x1] + 0x22: [data: 0x5] + 0x23: [data: 0x0] === GC status === + 8 + ----- + 3 5 ----- From 4ae99d4ae433cfff3e11b5527e59026ce25dbe0d Mon Sep 17 00:00:00 2001 From: homka122 Date: Mon, 10 Nov 2025 01:41:20 +0300 Subject: [PATCH 05/19] Fix: fix bug of the my year (init sp before init GC) --- PudgeWithMoML/lib/riscv/codegen.ml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PudgeWithMoML/lib/riscv/codegen.ml b/PudgeWithMoML/lib/riscv/codegen.ml index 21372ec2..049bf439 100644 --- a/PudgeWithMoML/lib/riscv/codegen.ml +++ b/PudgeWithMoML/lib/riscv/codegen.ml @@ -496,8 +496,8 @@ let gather pr : instr list t = @ functions_code @ [ directive ".globl _start"; label "_start" ] @ [ mv fp Sp ] - @ [ addi Sp Sp (-frame) ] @ [ mv (A 0) Sp; call "init_GC" ] + @ [ addi Sp Sp (-frame) ] @ main_code @ [ call "flush"; li (A 0) 0; li (A 7) 94; ecall ] ;; From 77af20d06c6fdc8d2b2c68fcd45d11e7d49177b7 Mon Sep 17 00:00:00 2001 From: homka122 Date: Mon, 10 Nov 2025 05:31:28 +0300 Subject: [PATCH 06/19] Fix: fix second bug of my life (inc after set new pointer) --- PudgeWithMoML/lib/runtime/runtime.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PudgeWithMoML/lib/runtime/runtime.c b/PudgeWithMoML/lib/runtime/runtime.c index 5674d402..41ff962b 100644 --- a/PudgeWithMoML/lib/runtime/runtime.c +++ b/PudgeWithMoML/lib/runtime/runtime.c @@ -344,8 +344,8 @@ void _gc_collect(void *current_sp) { } // copy to old space - new_pointer = gc.old_space + old_space_offset; gc.old_space[old_space_offset++] = (void *)cur_size; + new_pointer = gc.old_space + old_space_offset; for (size_t j = 0; j < cur_size; j++) { gc.old_space[old_space_offset++] = gc.new_space[cur_offset + 1 + j]; } From 206987eb5769660fbe8a880222f7c6061321d477 Mon Sep 17 00:00:00 2001 From: homka122 Date: Mon, 10 Nov 2025 05:32:27 +0300 Subject: [PATCH 07/19] Refactor: improove print functions in GC runtime --- PudgeWithMoML/lib/runtime/runtime.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/PudgeWithMoML/lib/runtime/runtime.c b/PudgeWithMoML/lib/runtime/runtime.c index 41ff962b..9a3f7ecf 100644 --- a/PudgeWithMoML/lib/runtime/runtime.c +++ b/PudgeWithMoML/lib/runtime/runtime.c @@ -6,7 +6,7 @@ #include #include -#if true +#if false #define LOG(fmt, ...) printf(fmt, ##__VA_ARGS__) #define LOGF(fun) fun #else @@ -59,27 +59,30 @@ typedef struct { #define ZERO8 0, 0, 0, 0, 0, 0, 0, 0 #define INT8 int, int, int, int, int, int, int, int +void print_stack(void *current_sp); + // Print stats about Garbage Collector work void print_gc_status() { printf("=== GC status ===\n"); printf("Base stack pointer: %x\n", gc.base_sp); printf("Start address of new space: %x\n", gc.new_space); - printf("Current space capacity: %ld\n", gc.space_capacity); - printf("Allocate count: %ld\n", gc.alloc_count); - printf("Allocated memory count: %ld bytes\n", gc.allocated_bytes_count); - printf("Collect count: %ld\n", gc.collect_count); - printf("Allocated words in new space: %ld\n", gc.alloc_offset); + printf("Allocate count: %ld times\n", gc.alloc_count); + printf("Collect count: %ld times\n", gc.collect_count); + printf("Current space capacity: %ld words\n", gc.space_capacity); + printf("Total allocated memory: %ld words\n", + gc.allocated_bytes_count / WORD_SIZE); + printf("Allocated words in new space: %ld words\n", gc.alloc_offset); printf("Current new space:\n"); size_t offset = 0; while (offset < gc.alloc_offset) { size_t size = (size_t)gc.new_space[offset]; - printf("\t0x%x: [size: %ld]\n", offset, size); + printf("\t(0x%x) 0x%x: [size: %ld]\n", gc.new_space + offset, offset, size); offset++; for (size_t i = 0; i < size; i++) { - printf("\t0x%x: ", offset); + printf("\t(0x%x) 0x%x: ", gc.new_space + offset, offset); printf("[data: 0x%x]\n", gc.new_space[offset]); offset++; } @@ -87,6 +90,10 @@ void print_gc_status() { printf("=== GC status ===\n"); + void *current_sp = NULL; + asm volatile("mv %0, sp" : "=r"(current_sp)); + LOGF(print_stack(current_sp)); + return; } From 5655a976629f0402c8f1e9c04d41e6737758ad8e Mon Sep 17 00:00:00 2001 From: homka122 Date: Mon, 10 Nov 2025 05:33:45 +0300 Subject: [PATCH 08/19] Refactor: debug messages in GC runtime Signed-off-by: homka122 --- PudgeWithMoML/lib/runtime/runtime.c | 34 +++++++++++++---------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/PudgeWithMoML/lib/runtime/runtime.c b/PudgeWithMoML/lib/runtime/runtime.c index 9a3f7ecf..bc073f8b 100644 --- a/PudgeWithMoML/lib/runtime/runtime.c +++ b/PudgeWithMoML/lib/runtime/runtime.c @@ -300,6 +300,7 @@ void _gc_collect(void *current_sp) { } void **regs = collect_riscv_state(); + LOGF(print_stack(current_sp)); size_t stack_size = (gc.base_sp - current_sp) / 8; size_t cur_offset = 0; @@ -318,14 +319,14 @@ void _gc_collect(void *current_sp) { } LOG("Try to find stack cell with 0x%x value on 0x%ld offset\n", cur_pointer, - cur_offset); + cur_offset + 1); // try to find in regs and stack at least one pointer { bool found = false; // regs - for (size_t i = 0; i < 25; i++) { + for (size_t i = 0; i < 26; i++) { if (regs[i] == cur_pointer) { LOG("FOUND AT REG: %ld\n", i); found = true; @@ -335,7 +336,7 @@ void _gc_collect(void *current_sp) { // stack for (size_t i = 0; i < stack_size; i++) { - void **byte = (void **)gc.base_sp - i; + void **byte = (void **)gc.base_sp - i - 1; if (*byte == cur_pointer) { LOG("FOUND AT STACK: %ld. CUR_OFFSET: %x, CUR_POINTER: %x, byte: " "%x, *byte: %x\n", @@ -360,11 +361,10 @@ void _gc_collect(void *current_sp) { } LOG("RUN CHANGING\n"); - LOGF(print_stack(current_sp)); // change all occurences { // regs - for (size_t i = 0; i < 25; i++) { + for (size_t i = 0; i < 26; i++) { if (regs[i] == cur_pointer) { set_riscv_reg(i, new_pointer); } @@ -372,7 +372,7 @@ void _gc_collect(void *current_sp) { // stack for (size_t i = 0; i < stack_size; i++) { - void **byte = (void **)gc.base_sp - i; + void **byte = (void **)gc.base_sp - i - 1; if (*byte == cur_pointer) { LOG("Change stack cell 0x%x. 0x%x -> 0x%x\n", byte, *byte, new_pointer); @@ -380,10 +380,10 @@ void _gc_collect(void *current_sp) { } } } - LOGF(print_stack(current_sp)); cur_offset += cur_size + 1; } + LOGF(print_stack(current_sp)); void *temp = gc.new_space; gc.new_space = gc.old_space; @@ -405,7 +405,7 @@ void gc_collect() { // alloc size bytes in gc.memory void *my_malloc(size_t size) { - // printf("MY MALLOC: %ld\n", size); + LOG("MY MALLOC: %ld\n", size); if (size == 0) { return NULL; } @@ -419,6 +419,7 @@ void *my_malloc(size_t size) { // no free space left after alloc size bytes + header if (gc.alloc_offset + (words + 1) >= gc.space_capacity) { + LOG("no free space\n"); gc_collect(); // after collecting we still don't have space @@ -426,15 +427,6 @@ void *my_malloc(size_t size) { fprintf(stderr, "panic! overflow memory limits\n"); fflush(stderr); exit(122); - size_t mult = 1; - - while (gc.alloc_offset + (words + 1) >= (gc.space_capacity * mult)) { - mult *= 2; - } - - gc.space_capacity *= mult; - gc.new_space = realloc(gc.new_space, sizeof(void *) * gc.space_capacity); - gc.old_space = realloc(gc.old_space, sizeof(void *) * gc.space_capacity); } } @@ -448,6 +440,7 @@ void *my_malloc(size_t size) { } void *alloc_closure(INT8, void *f, uint8_t argc) { + LOG("alloc_closure(0x%x, %d)\n", f, argc); closure *clos = my_malloc(sizeof(closure) + sizeof(void *) * argc); clos->code = f; @@ -462,6 +455,9 @@ void *copy_closure(closure *old_clos) { closure *clos = old_clos; closure *new = alloc_closure(ZERO8, clos->code, clos->argc); + LOG("old clos: 0x%x, new clos: 0x%x\n", clos, new); + LOG("clos.code: 0x%x, clos argc: 0x%d\n", clos->code, clos->argc); + for (size_t i = 0; i < clos->argc_recived; i++) { new->args[new->argc_recived++] = clos->args[i]; } @@ -472,8 +468,8 @@ void *copy_closure(closure *old_clos) { // get closure and apply [argc] arguments to closure void *apply_closure(INT8, closure *old_clos, uint8_t argc, ...) { closure *clos = copy_closure(old_clos); - printf("CLOS: 0x%x\n", clos); - print_gc_status(); + LOG("CLOS: 0x%x\n", clos); + LOGF(print_gc_status()); fflush(stdout); va_list list; va_start(list, argc); From a1760223ffd40ef9602902c225bf173f278244ff Mon Sep 17 00:00:00 2001 From: homka122 Date: Mon, 10 Nov 2025 06:22:57 +0300 Subject: [PATCH 09/19] Feat: add CI_STABLE flag for tests in CI Signed-off-by: homka122 --- PudgeWithMoML/bin/run.t | 6 +- PudgeWithMoML/bin/run_after_ll.t | 4 +- PudgeWithMoML/bin/run_simplyfied.t | 6 +- PudgeWithMoML/lib/runtime/runtime.c | 29 +- PudgeWithMoML/lib/runtime/runtime_test.c | 62 +- PudgeWithMoML/test/GC.t | 1834 +++++++--------------- PudgeWithMoML/test/codegen.t | 954 +---------- PudgeWithMoML/test/dune | 8 + 8 files changed, 632 insertions(+), 2271 deletions(-) diff --git a/PudgeWithMoML/bin/run.t b/PudgeWithMoML/bin/run.t index 4785218a..1a98beaa 100644 --- a/PudgeWithMoML/bin/run.t +++ b/PudgeWithMoML/bin/run.t @@ -174,10 +174,10 @@ .globl _start _start: mv fp, sp - addi sp, sp, -16 mv a0, sp call init_GC addi sp, sp, -16 + addi sp, sp, -16 la t5, f_1 li t6, 1 sd t5, 0(sp) @@ -467,10 +467,10 @@ .globl _start _start: mv fp, sp - addi sp, sp, -16 mv a0, sp call init_GC addi sp, sp, -16 + addi sp, sp, -16 la t5, f_2 li t6, 1 sd t5, 0(sp) @@ -668,9 +668,9 @@ .globl _start _start: mv fp, sp - addi sp, sp, -160 mv a0, sp call init_GC + addi sp, sp, -160 # Apply wrap__0 with 11 args addi sp, sp, -16 la t5, wrap__0 diff --git a/PudgeWithMoML/bin/run_after_ll.t b/PudgeWithMoML/bin/run_after_ll.t index 99c0f6ca..c04efef3 100644 --- a/PudgeWithMoML/bin/run_after_ll.t +++ b/PudgeWithMoML/bin/run_after_ll.t @@ -156,9 +156,9 @@ .globl _start _start: mv fp, sp - addi sp, sp, -16 mv a0, sp call init_GC + addi sp, sp, -16 # Apply fac_cps__6 with 2 args # Load args on stack addi sp, sp, -16 @@ -441,9 +441,9 @@ .globl _start _start: mv fp, sp - addi sp, sp, -24 mv a0, sp call init_GC + addi sp, sp, -24 # Apply fib__11 with 2 args # Load args on stack addi sp, sp, -16 diff --git a/PudgeWithMoML/bin/run_simplyfied.t b/PudgeWithMoML/bin/run_simplyfied.t index 9b8fc1ae..cda0176a 100644 --- a/PudgeWithMoML/bin/run_simplyfied.t +++ b/PudgeWithMoML/bin/run_simplyfied.t @@ -68,9 +68,9 @@ .globl _start _start: mv fp, sp - addi sp, sp, -8 mv a0, sp call init_GC + addi sp, sp, -8 # Apply fac__0 with 1 args # Load args on stack addi sp, sp, -16 @@ -176,9 +176,9 @@ .globl _start _start: mv fp, sp - addi sp, sp, -8 mv a0, sp call init_GC + addi sp, sp, -8 # Apply fib__0 with 1 args # Load args on stack addi sp, sp, -16 @@ -275,9 +275,9 @@ .globl _start _start: mv fp, sp - addi sp, sp, -136 mv a0, sp call init_GC + addi sp, sp, -136 li t0, 0 li t1, 1 sub t0, t0, t1 diff --git a/PudgeWithMoML/lib/runtime/runtime.c b/PudgeWithMoML/lib/runtime/runtime.c index bc073f8b..725acaef 100644 --- a/PudgeWithMoML/lib/runtime/runtime.c +++ b/PudgeWithMoML/lib/runtime/runtime.c @@ -14,6 +14,10 @@ #define LOGF(fun) ((void)0) #endif +// each run of tests change, for example, initial sp address or address of heap +// so when we want stable ci tests -- we mock that value +#define STABLE_CI true + extern void *call_closure(void *code, uint64_t argc, void **argv); void print_int(size_t n) { printf("%d\n", n); } @@ -21,7 +25,7 @@ void print_int(size_t n) { printf("%d\n", n); } void flush() { fflush(stdout); } // size in words -#define GC_SPACE_INITIAL_SIZE (256) +#define GC_SPACE_INITIAL_SIZE (8192) #define WORD_SIZE (8) // HEAP structure @@ -48,6 +52,7 @@ typedef struct { } GC_state; static GC_state gc; +void *first_new_space; // for stable CI typedef struct { void *code; @@ -64,8 +69,12 @@ void print_stack(void *current_sp); // Print stats about Garbage Collector work void print_gc_status() { printf("=== GC status ===\n"); - printf("Base stack pointer: %x\n", gc.base_sp); - printf("Start address of new space: %x\n", gc.new_space); + // printf("Base stack pointer: %x\n", STABLE_CI ? (void *)0x122 : gc.base_sp); + printf("Start address of new space: %x\n", + STABLE_CI ? (gc.new_space == first_new_space + ? (void *)0x1000 + : (void *)(0x1000 + GC_SPACE_INITIAL_SIZE * 8)) + : gc.new_space); printf("Allocate count: %ld times\n", gc.alloc_count); printf("Collect count: %ld times\n", gc.collect_count); printf("Current space capacity: %ld words\n", gc.space_capacity); @@ -78,11 +87,20 @@ void print_gc_status() { while (offset < gc.alloc_offset) { size_t size = (size_t)gc.new_space[offset]; - printf("\t(0x%x) 0x%x: [size: %ld]\n", gc.new_space + offset, offset, size); + void **addr = gc.new_space + offset; + if (STABLE_CI) { + if (gc.new_space == first_new_space) { + addr = ((void **)0x1000) + offset; + } else { + addr = ((void **)0x1000) + GC_SPACE_INITIAL_SIZE + offset; + } + } + + printf("\t(0x%x) 0x%x: [size: %ld]\n", addr, offset, size); offset++; for (size_t i = 0; i < size; i++) { - printf("\t(0x%x) 0x%x: ", gc.new_space + offset, offset); + printf("\t(0x%x) 0x%x: ", addr + i + 1, offset); printf("[data: 0x%x]\n", gc.new_space[offset]); offset++; } @@ -103,6 +121,7 @@ void init_GC(void *base_sp) { gc.base_sp = base_sp; gc.space_capacity = GC_SPACE_INITIAL_SIZE; gc.new_space = malloc(sizeof(void *) * GC_SPACE_INITIAL_SIZE); + first_new_space = gc.new_space; gc.alloc_offset = 0; gc.old_space = malloc(sizeof(void *) * GC_SPACE_INITIAL_SIZE); diff --git a/PudgeWithMoML/lib/runtime/runtime_test.c b/PudgeWithMoML/lib/runtime/runtime_test.c index e507c489..d4c36617 100644 --- a/PudgeWithMoML/lib/runtime/runtime_test.c +++ b/PudgeWithMoML/lib/runtime/runtime_test.c @@ -129,35 +129,41 @@ static void set_riscv_reg(int idx, void *val) { regs[idx] = val; } // 2) save new pointer to old_space // 3) iterate through stack\regs and replace all pointer to the new // pointer -void gc_collect() { +void _gc_collect(void *current_sp) { if (gc.alloc_offset == 0) { return; } size_t stack_size = (gc.base_sp - current_sp) / 8; - - // printf("STACK: base_sp %x, current_sp %x\n", gc.base_sp, current_sp); - // printf("STACK SIZE: %ld\n", stack_size); - // fflush(stdout); - - void *new_pointer = NULL; size_t cur_offset = 0; size_t old_space_offset = 0; while (cur_offset < gc.alloc_offset) { + void *new_pointer = NULL; size_t cur_size = (size_t)gc.new_space[cur_offset]; + void *cur_pointer = gc.new_space + cur_offset + 1; + if (cur_size == 0) { + fprintf( + stderr, + "You have object on heap with zero size\nBug in malloc function!\n"); print_gc_status(); exit(122); } - void *cur_pointer = gc.new_space + cur_offset + 1; + LOG("Try to find stack cell with 0x%x value on 0x%ld offset\n", cur_pointer, + cur_offset + 1); + + LOGF(print_stack()); // try to find in regs and stack at least one pointer { bool found = false; + // regs - for (size_t i = 0; i < 25; i++) { + for (size_t i = 0; i < 26; i++) { if (regs[i] == cur_pointer) { + LOG("FOUND AT REG: %ld\n", i); found = true; + break; } } @@ -165,7 +171,11 @@ void gc_collect() { for (size_t i = 0; i < stack_size; i++) { void **byte = (void **)gc.base_sp - i; if (*byte == cur_pointer) { + LOG("FOUND AT STACK: %ld. CUR_OFFSET: %x, CUR_POINTER: %x, byte: " + "%x, *byte: %x\n", + i, cur_offset, cur_pointer, byte, *byte); found = true; + break; } } @@ -175,17 +185,19 @@ void gc_collect() { } // copy to old space - gc.old_space[old_space_offset++] = (void *)cur_size; new_pointer = gc.old_space + old_space_offset; + gc.old_space[old_space_offset++] = (void *)cur_size; for (size_t j = 0; j < cur_size; j++) { gc.old_space[old_space_offset++] = gc.new_space[cur_offset + 1 + j]; } + LOG("NEW POINTER: 0x%x\n", new_pointer); } + LOG("RUN CHANGING\n"); // change all occurences { // regs - for (size_t i = 0; i < 25; i++) { + for (size_t i = 0; i < 26; i++) { if (regs[i] == cur_pointer) { set_riscv_reg(i, new_pointer); } @@ -193,25 +205,40 @@ void gc_collect() { // stack for (size_t i = 0; i < stack_size; i++) { - void **byte = gc.base_sp - i; + void **byte = (void **)gc.base_sp - i; if (*byte == cur_pointer) { + LOG("Change stack cell 0x%x. 0x%x -> 0x%x\n", byte, *byte, + new_pointer); *byte = new_pointer; } } } + LOGF(print_stack()); cur_offset += cur_size + 1; } void *temp = gc.new_space; gc.new_space = gc.old_space; - gc.old_space = gc.new_space; + gc.old_space = temp; gc.alloc_offset = old_space_offset; + + gc.collect_count++; +} + +// WARNING: if you read stack pointer in _gc_collect function than when you go +// through stack you can change local variables of _gc_collect fuction +// So we write wrapper only for reading stack pointer **before** _gc_collect +// function It took 4 hours for debug this chaos 🐣🐣🐤🐤🐔🐔🦆🦆🐹🐹🐹🐹 +void gc_collect() { + void *current_sp = NULL; + asm volatile("mv %0, sp" : "=r"(current_sp)); + _gc_collect(current_sp); } // alloc size bytes in gc.memory void *my_malloc(size_t size) { - // printf("MY MALLOC: %ld\n", size); + printf("MY MALLOC: %ld\n", size); if (size == 0) { return NULL; } @@ -225,6 +252,7 @@ void *my_malloc(size_t size) { // no free space left after alloc size bytes + header if (gc.alloc_offset + (words + 1) >= gc.space_capacity) { + printf("no free space\n"); gc_collect(); // after collecting we still don't have space @@ -244,6 +272,7 @@ void *my_malloc(size_t size) { } } + printf("words: %ld\n", words); gc.new_space[gc.alloc_offset++] = (void *)words; void **result = gc.new_space + gc.alloc_offset; @@ -252,7 +281,7 @@ void *my_malloc(size_t size) { } void *alloc_closure(INT8, void *f, uint8_t argc) { - // gc_collect(); + LOG("alloc_closure(0x%x, %d)\n", f, argc); closure *clos = my_malloc(sizeof(closure) + sizeof(void *) * argc); clos->code = f; @@ -267,6 +296,9 @@ void *copy_closure(closure *old_clos) { closure *clos = old_clos; closure *new = alloc_closure(ZERO8, clos->code, clos->argc); + printf("old clos: 0x%x, new clos: 0x%x\n", clos, new); + printf("clos.code: 0x%x, clos argc: 0x%d\n", clos->code, clos->argc); + for (size_t i = 0; i < clos->argc_recived; i++) { new->args[new->argc_recived++] = clos->args[i]; } diff --git a/PudgeWithMoML/test/GC.t b/PudgeWithMoML/test/GC.t index 40ff8ea3..99dda222 100644 --- a/PudgeWithMoML/test/GC.t +++ b/PudgeWithMoML/test/GC.t @@ -1,3 +1,191 @@ +( simple example ) + $ make compile opts=-gen_mid --no-print-directory -C .. << 'EOF' + > let add a b = a + b + > let main = + > let homka1 = add 5 in + > let homka2 = print_gc_status () in + > let homka3 = gc_collect () in + > let homka4 = print_gc_status () in + > let lol = (homka1 2) in + > let homka5 = print_gc_status () in + > print_int lol + > EOF + $ qemu-riscv64 -L /usr/riscv64-linux-gnu -cpu rv64 ../main.exe + === GC status === + Start address of new space: 1000 + Allocate count: 2 times + Collect count: 0 times + Current space capacity: 8192 words + Total allocated memory: 12 words + Allocated words in new space: 12 words + Current new space: + (0x1000) 0x0: [size: 5] + (0x1008) 0x1: [data: 0x10670] + (0x1010) 0x2: [data: 0x2] + (0x1018) 0x3: [data: 0x0] + (0x1020) 0x4: [data: 0x0] + (0x1028) 0x5: [data: 0x0] + (0x1030) 0x6: [size: 5] + (0x1038) 0x7: [data: 0x10670] + (0x1040) 0x8: [data: 0x2] + (0x1048) 0x9: [data: 0x1] + (0x1050) 0xa: [data: 0x5] + (0x1058) 0xb: [data: 0x0] + === GC status === + === GC status === + Start address of new space: 11000 + Allocate count: 2 times + Collect count: 1 times + Current space capacity: 8192 words + Total allocated memory: 12 words + Allocated words in new space: 6 words + Current new space: + (0x11000) 0x0: [size: 5] + (0x11008) 0x1: [data: 0x10670] + (0x11010) 0x2: [data: 0x2] + (0x11018) 0x3: [data: 0x1] + (0x11020) 0x4: [data: 0x5] + (0x11028) 0x5: [data: 0x0] + === GC status === + === GC status === + Start address of new space: 11000 + Allocate count: 3 times + Collect count: 1 times + Current space capacity: 8192 words + Total allocated memory: 18 words + Allocated words in new space: 12 words + Current new space: + (0x11000) 0x0: [size: 5] + (0x11008) 0x1: [data: 0x10670] + (0x11010) 0x2: [data: 0x2] + (0x11018) 0x3: [data: 0x1] + (0x11020) 0x4: [data: 0x5] + (0x11028) 0x5: [data: 0x0] + (0x11030) 0x6: [size: 5] + (0x11038) 0x7: [data: 0x10670] + (0x11040) 0x8: [data: 0x2] + (0x11048) 0x9: [data: 0x2] + (0x11050) 0xa: [data: 0x5] + (0x11058) 0xb: [data: 0x2] + === GC status === + 7 + $ cat ../main.anf + let add__0 = fun a__1 -> + fun b__2 -> + a__1 + b__2 + + + let main__3 = let anf_t6 = add__0 5 in + let homka1__4 = anf_t6 in + let anf_t5 = print_gc_status () in + let homka2__5 = anf_t5 in + let anf_t4 = gc_collect () in + let homka3__6 = anf_t4 in + let anf_t3 = print_gc_status () in + let homka4__7 = anf_t3 in + let anf_t2 = homka1__4 2 in + let lol__8 = anf_t2 in + let anf_t1 = print_gc_status () in + let homka5__9 = anf_t1 in + print_int lol__8 + $ cat ../main.s + .text + .globl add__0 + add__0: + addi sp, sp, -16 + sd ra, 8(sp) + sd fp, 0(sp) + addi fp, sp, 16 + ld t0, 0(fp) + ld t1, 8(fp) + add a0, t0, t1 + ld ra, 8(sp) + ld fp, 0(sp) + addi sp, sp, 16 + ret + .globl _start + _start: + mv fp, sp + mv a0, sp + call init_GC + addi sp, sp, -104 + # Partial application add__0 with 1 args + # Load args on stack + addi sp, sp, -32 + addi sp, sp, -16 + la t5, add__0 + li t6, 2 + sd t5, 0(sp) + sd t6, 8(sp) + call alloc_closure + mv t0, a0 + addi sp, sp, 16 + sd t0, 0(sp) + li t0, 1 + sd t0, 8(sp) + li t0, 5 + sd t0, 16(sp) + # End loading args on stack + call apply_closure + mv t0, a0 + # Free args on stack + addi sp, sp, 32 + # End free args on stack + # End Partial application add__0 with 1 args + sd t0, -8(fp) + ld t0, -8(fp) + sd t0, -16(fp) + call print_gc_status + sd t0, -24(fp) + ld t0, -24(fp) + sd t0, -32(fp) + call gc_collect + sd t0, -40(fp) + ld t0, -40(fp) + sd t0, -48(fp) + call print_gc_status + sd t0, -56(fp) + ld t0, -56(fp) + sd t0, -64(fp) + # Apply homka1__4 with 1 args + ld t0, -16(fp) + sd t0, -72(fp) + # Load args on stack + addi sp, sp, -32 + ld t0, -72(fp) + sd t0, 0(sp) + li t0, 1 + sd t0, 8(sp) + li t0, 2 + sd t0, 16(sp) + # End loading args on stack + call apply_closure + # Free args on stack + addi sp, sp, 32 + # End free args on stack + mv t0, a0 + # End Apply homka1__4 with 1 args + sd t0, -80(fp) + ld t0, -80(fp) + sd t0, -88(fp) + call print_gc_status + sd t0, -96(fp) + ld t0, -96(fp) + sd t0, -104(fp) + # Apply print_int + ld a0, -88(fp) + call print_int + mv t0, a0 + # End Apply print_int + la t1, main__3 + sd t0, 0(t1) + call flush + li a0, 0 + li a7, 94 + ecall + .data + main__3: .dword 0 + ( alloc inner closure ) $ make compile opts=-gen_mid --no-print-directory -C .. << 'EOF' > let wrap f x = f x @@ -12,158 +200,50 @@ > let main = print_int homs > EOF $ qemu-riscv64 -L /usr/riscv64-linux-gnu -cpu rv64 ../main.exe - CLOS: 0x14300 - === GC status === - Base stack pointer: bb0d0e40 - Start address of new space: 142a0 - Current space capacity: 256 - Allocate count: 3 - Allocated memory count: 136 bytes - Collect count: 0 - Allocated words in new space: 17 - Current new space: - 0x0: [size: 5] - 0x1: [data: 0x10670] - 0x2: [data: 0x2] - 0x3: [data: 0x0] - 0x4: [data: 0x0] - 0x5: [data: 0x0] - 0x6: [size: 4] - 0x7: [data: 0x106a0] - 0x8: [data: 0x1] - 0x9: [data: 0x0] - 0xa: [data: 0x0] - 0xb: [size: 5] - 0xc: [data: 0x10670] - 0xd: [data: 0x2] - 0xe: [data: 0x0] - 0xf: [data: 0x0] - 0x10: [data: 0x0] - === GC status === - CLOS: 0x14330 === GC status === - Base stack pointer: bb0d0e40 - Start address of new space: 142a0 - Current space capacity: 256 - Allocate count: 4 - Allocated memory count: 184 bytes - Collect count: 0 - Allocated words in new space: 23 + Start address of new space: 1000 + Allocate count: 5 times + Collect count: 0 times + Current space capacity: 8192 words + Total allocated memory: 28 words + Allocated words in new space: 28 words Current new space: - 0x0: [size: 5] - 0x1: [data: 0x10670] - 0x2: [data: 0x2] - 0x3: [data: 0x0] - 0x4: [data: 0x0] - 0x5: [data: 0x0] - 0x6: [size: 4] - 0x7: [data: 0x106a0] - 0x8: [data: 0x1] - 0x9: [data: 0x0] - 0xa: [data: 0x0] - 0xb: [size: 5] - 0xc: [data: 0x10670] - 0xd: [data: 0x2] - 0xe: [data: 0x1] - 0xf: [data: 0x142d8] - 0x10: [data: 0x0] - 0x11: [size: 5] - 0x12: [data: 0x10670] - 0x13: [data: 0x2] - 0x14: [data: 0x1] - 0x15: [data: 0x142d8] - 0x16: [data: 0x0] - === GC status === - CLOS: 0x14360 - === GC status === - Base stack pointer: bb0d0e40 - Start address of new space: 142a0 - Current space capacity: 256 - Allocate count: 5 - Allocated memory count: 224 bytes - Collect count: 0 - Allocated words in new space: 28 - Current new space: - 0x0: [size: 5] - 0x1: [data: 0x10670] - 0x2: [data: 0x2] - 0x3: [data: 0x0] - 0x4: [data: 0x0] - 0x5: [data: 0x0] - 0x6: [size: 4] - 0x7: [data: 0x106a0] - 0x8: [data: 0x1] - 0x9: [data: 0x0] - 0xa: [data: 0x0] - 0xb: [size: 5] - 0xc: [data: 0x10670] - 0xd: [data: 0x2] - 0xe: [data: 0x1] - 0xf: [data: 0x142d8] - 0x10: [data: 0x0] - 0x11: [size: 5] - 0x12: [data: 0x10670] - 0x13: [data: 0x2] - 0x14: [data: 0x2] - 0x15: [data: 0x142d8] - 0x16: [data: 0x5] - 0x17: [size: 4] - 0x18: [data: 0x106a0] - 0x19: [data: 0x1] - 0x1a: [data: 0x0] - 0x1b: [data: 0x0] - === GC status === - === GC status === - Base stack pointer: bb0d0e40 - Start address of new space: 142a0 - Current space capacity: 256 - Allocate count: 5 - Allocated memory count: 224 bytes - Collect count: 0 - Allocated words in new space: 28 - Current new space: - 0x0: [size: 5] - 0x1: [data: 0x10670] - 0x2: [data: 0x2] - 0x3: [data: 0x0] - 0x4: [data: 0x0] - 0x5: [data: 0x0] - 0x6: [size: 4] - 0x7: [data: 0x106a0] - 0x8: [data: 0x1] - 0x9: [data: 0x0] - 0xa: [data: 0x0] - 0xb: [size: 5] - 0xc: [data: 0x10670] - 0xd: [data: 0x2] - 0xe: [data: 0x1] - 0xf: [data: 0x142d8] - 0x10: [data: 0x0] - 0x11: [size: 5] - 0x12: [data: 0x10670] - 0x13: [data: 0x2] - 0x14: [data: 0x2] - 0x15: [data: 0x142d8] - 0x16: [data: 0x5] - 0x17: [size: 4] - 0x18: [data: 0x106a0] - 0x19: [data: 0x1] - 0x1a: [data: 0x1] - 0x1b: [data: 0x5] - === GC status === - Try to find stack cell with 0x142a8 value on 0x0 offset - Try to find stack cell with 0x142d8 value on 0x6 offset - Try to find stack cell with 0x14300 value on 0x11 offset - Try to find stack cell with 0x14330 value on 0x17 offset - Try to find stack cell with 0x14360 value on 0x23 offset - === GC status === - Base stack pointer: bb0d0e40 - Start address of new space: 14ab0 - Current space capacity: 256 - Allocate count: 5 - Allocated memory count: 224 bytes - Collect count: 1 - Allocated words in new space: 0 + (0x1000) 0x0: [size: 5] + (0x1008) 0x1: [data: 0x10670] + (0x1010) 0x2: [data: 0x2] + (0x1018) 0x3: [data: 0x0] + (0x1020) 0x4: [data: 0x0] + (0x1028) 0x5: [data: 0x0] + (0x1030) 0x6: [size: 4] + (0x1038) 0x7: [data: 0x106a0] + (0x1040) 0x8: [data: 0x1] + (0x1048) 0x9: [data: 0x0] + (0x1050) 0xa: [data: 0x0] + (0x1058) 0xb: [size: 5] + (0x1060) 0xc: [data: 0x10670] + (0x1068) 0xd: [data: 0x2] + (0x1070) 0xe: [data: 0x1] + (0x1078) 0xf: [data: 0x142d8] + (0x1080) 0x10: [data: 0x0] + (0x1088) 0x11: [size: 5] + (0x1090) 0x12: [data: 0x10670] + (0x1098) 0x13: [data: 0x2] + (0x10a0) 0x14: [data: 0x2] + (0x10a8) 0x15: [data: 0x142d8] + (0x10b0) 0x16: [data: 0x5] + (0x10b8) 0x17: [size: 4] + (0x10c0) 0x18: [data: 0x106a0] + (0x10c8) 0x19: [data: 0x1] + (0x10d0) 0x1a: [data: 0x1] + (0x10d8) 0x1b: [data: 0x5] + === GC status === + === GC status === + Start address of new space: 11000 + Allocate count: 5 times + Collect count: 1 times + Current space capacity: 8192 words + Total allocated memory: 28 words + Allocated words in new space: 0 words Current new space: === GC status === 5 @@ -286,9 +366,9 @@ .globl _start _start: mv fp, sp - addi sp, sp, 0 mv a0, sp call init_GC + addi sp, sp, 0 # Apply homka__5 with 3 args # Load args on stack addi sp, sp, -32 @@ -364,1176 +444,324 @@ > let main = print_int 5 > EOF $ qemu-riscv64 -L /usr/riscv64-linux-gnu -cpu rv64 ../main.exe - CLOS: 0x142d8 === GC status === - Base stack pointer: 128cee40 - Start address of new space: 142a0 - Current space capacity: 256 - Allocate count: 2 - Allocated memory count: 96 bytes - Collect count: 0 - Allocated words in new space: 12 + Start address of new space: 1000 + Allocate count: 2 times + Collect count: 0 times + Current space capacity: 8192 words + Total allocated memory: 12 words + Allocated words in new space: 12 words Current new space: - 0x0: [size: 5] - 0x1: [data: 0x10670] - 0x2: [data: 0x2] - 0x3: [data: 0x0] - 0x4: [data: 0x0] - 0x5: [data: 0x0] - 0x6: [size: 5] - 0x7: [data: 0x10670] - 0x8: [data: 0x2] - 0x9: [data: 0x0] - 0xa: [data: 0x0] - 0xb: [data: 0x0] - === GC status === - === GC status === - Base stack pointer: 128cee40 - Start address of new space: 142a0 - Current space capacity: 256 - Allocate count: 2 - Allocated memory count: 96 bytes - Collect count: 0 - Allocated words in new space: 12 + (0x1000) 0x0: [size: 5] + (0x1008) 0x1: [data: 0x10670] + (0x1010) 0x2: [data: 0x2] + (0x1018) 0x3: [data: 0x0] + (0x1020) 0x4: [data: 0x0] + (0x1028) 0x5: [data: 0x0] + (0x1030) 0x6: [size: 5] + (0x1038) 0x7: [data: 0x10670] + (0x1040) 0x8: [data: 0x2] + (0x1048) 0x9: [data: 0x1] + (0x1050) 0xa: [data: 0x7] + (0x1058) 0xb: [data: 0x0] + === GC status === + === GC status === + Start address of new space: 11000 + Allocate count: 2 times + Collect count: 1 times + Current space capacity: 8192 words + Total allocated memory: 12 words + Allocated words in new space: 0 words Current new space: - 0x0: [size: 5] - 0x1: [data: 0x10670] - 0x2: [data: 0x2] - 0x3: [data: 0x0] - 0x4: [data: 0x0] - 0x5: [data: 0x0] - 0x6: [size: 5] - 0x7: [data: 0x10670] - 0x8: [data: 0x2] - 0x9: [data: 0x1] - 0xa: [data: 0x7] - 0xb: [data: 0x0] === GC status === - Try to find stack cell with 0x142a8 value on 0x0 offset - Try to find stack cell with 0x142d8 value on 0x6 offset === GC status === - Base stack pointer: 128cee40 - Start address of new space: 14ab0 - Current space capacity: 256 - Allocate count: 2 - Allocated memory count: 96 bytes - Collect count: 1 - Allocated words in new space: 0 + Start address of new space: 11000 + Allocate count: 4 times + Collect count: 1 times + Current space capacity: 8192 words + Total allocated memory: 24 words + Allocated words in new space: 12 words Current new space: - === GC status === - CLOS: 0x14ae8 - === GC status === - Base stack pointer: 128cee40 - Start address of new space: 14ab0 - Current space capacity: 256 - Allocate count: 4 - Allocated memory count: 192 bytes - Collect count: 1 - Allocated words in new space: 12 + (0x11000) 0x0: [size: 5] + (0x11008) 0x1: [data: 0x10670] + (0x11010) 0x2: [data: 0x2] + (0x11018) 0x3: [data: 0x0] + (0x11020) 0x4: [data: 0x0] + (0x11028) 0x5: [data: 0x0] + (0x11030) 0x6: [size: 5] + (0x11038) 0x7: [data: 0x10670] + (0x11040) 0x8: [data: 0x2] + (0x11048) 0x9: [data: 0x1] + (0x11050) 0xa: [data: 0x6] + (0x11058) 0xb: [data: 0x0] + === GC status === + === GC status === + Start address of new space: 1000 + Allocate count: 4 times + Collect count: 2 times + Current space capacity: 8192 words + Total allocated memory: 24 words + Allocated words in new space: 0 words Current new space: - 0x0: [size: 5] - 0x1: [data: 0x10670] - 0x2: [data: 0x2] - 0x3: [data: 0x0] - 0x4: [data: 0x0] - 0x5: [data: 0x0] - 0x6: [size: 5] - 0x7: [data: 0x10670] - 0x8: [data: 0x2] - 0x9: [data: 0x0] - 0xa: [data: 0x0] - 0xb: [data: 0x0] === GC status === === GC status === - Base stack pointer: 128cee40 - Start address of new space: 14ab0 - Current space capacity: 256 - Allocate count: 4 - Allocated memory count: 192 bytes - Collect count: 1 - Allocated words in new space: 12 - Current new space: - 0x0: [size: 5] - 0x1: [data: 0x10670] - 0x2: [data: 0x2] - 0x3: [data: 0x0] - 0x4: [data: 0x0] - 0x5: [data: 0x0] - 0x6: [size: 5] - 0x7: [data: 0x10670] - 0x8: [data: 0x2] - 0x9: [data: 0x1] - 0xa: [data: 0x6] - 0xb: [data: 0x0] - === GC status === - Try to find stack cell with 0x14ab8 value on 0x0 offset - Try to find stack cell with 0x14ae8 value on 0x6 offset - === GC status === - Base stack pointer: 128cee40 - Start address of new space: 142a0 - Current space capacity: 256 - Allocate count: 4 - Allocated memory count: 192 bytes - Collect count: 2 - Allocated words in new space: 0 - Current new space: - === GC status === - === GC status === - Base stack pointer: 128cee40 - Start address of new space: 142a0 - Current space capacity: 256 - Allocate count: 4 - Allocated memory count: 192 bytes - Collect count: 2 - Allocated words in new space: 0 + Start address of new space: 1000 + Allocate count: 4 times + Collect count: 2 times + Current space capacity: 8192 words + Total allocated memory: 24 words + Allocated words in new space: 0 words Current new space: === GC status === 5 -( overflow heap, but collect help ) +( move multiple objects to old_space ) $ make compile opts=-gen_mid --no-print-directory -C .. << 'EOF' - > let homka h1 h2 h3 h4 h5 h6 h7 h8 h9 h10 h11 h12 h13 h14 h15 = h1 - > let _ = - > let _ = homka 1 in - > let _ = homka 1 in - > let _ = homka 1 in - > let _ = homka 1 in - > let _ = homka 1 in - > let _ = homka 1 in - > let _ = homka 1 in - > 5 - > let _ = print_gc_status () - > let _ = clear_regs () - > let _ = gc_collect () - > let _ = print_gc_status () - > let main = print_int 5 + > let add a b = a + b + > let main = + > let homka1 = add 5 in + > let homka2 = add 3 in + > let homka2 = print_gc_status () in + > let homka3 = gc_collect () in + > let homka4 = print_gc_status () in + > let lol = (homka1 2) in + > let homka5 = print_gc_status () in + > print_int lol > EOF $ qemu-riscv64 -L /usr/riscv64-linux-gnu -cpu rv64 ../main.exe - CLOS: 0x14340 - === GC status === - Base stack pointer: aa2f5e08 - Start address of new space: 142a0 - Current space capacity: 256 - Allocate count: 2 - Allocated memory count: 304 bytes - Collect count: 0 - Allocated words in new space: 38 - Current new space: - 0x0: [size: 18] - 0x1: [data: 0x10670] - 0x2: [data: 0xf] - 0x3: [data: 0x0] - 0x4: [data: 0x0] - 0x5: [data: 0x0] - 0x6: [data: 0x0] - 0x7: [data: 0x0] - 0x8: [data: 0x0] - 0x9: [data: 0x0] - 0xa: [data: 0x0] - 0xb: [data: 0x0] - 0xc: [data: 0x0] - 0xd: [data: 0x0] - 0xe: [data: 0x0] - 0xf: [data: 0x0] - 0x10: [data: 0x0] - 0x11: [data: 0x0] - 0x12: [data: 0x0] - 0x13: [size: 18] - 0x14: [data: 0x10670] - 0x15: [data: 0xf] - 0x16: [data: 0x0] - 0x17: [data: 0x0] - 0x18: [data: 0x0] - 0x19: [data: 0x0] - 0x1a: [data: 0x0] - 0x1b: [data: 0x0] - 0x1c: [data: 0x0] - 0x1d: [data: 0x0] - 0x1e: [data: 0x0] - 0x1f: [data: 0x0] - 0x20: [data: 0x0] - 0x21: [data: 0x0] - 0x22: [data: 0x0] - 0x23: [data: 0x0] - 0x24: [data: 0x0] - 0x25: [data: 0x0] - === GC status === - CLOS: 0x14470 === GC status === - Base stack pointer: aa2f5e08 - Start address of new space: 142a0 - Current space capacity: 256 - Allocate count: 4 - Allocated memory count: 608 bytes - Collect count: 0 - Allocated words in new space: 76 + Start address of new space: 1000 + Allocate count: 4 times + Collect count: 0 times + Current space capacity: 8192 words + Total allocated memory: 24 words + Allocated words in new space: 24 words Current new space: - 0x0: [size: 18] - 0x1: [data: 0x10670] - 0x2: [data: 0xf] - 0x3: [data: 0x0] - 0x4: [data: 0x0] - 0x5: [data: 0x0] - 0x6: [data: 0x0] - 0x7: [data: 0x0] - 0x8: [data: 0x0] - 0x9: [data: 0x0] - 0xa: [data: 0x0] - 0xb: [data: 0x0] - 0xc: [data: 0x0] - 0xd: [data: 0x0] - 0xe: [data: 0x0] - 0xf: [data: 0x0] - 0x10: [data: 0x0] - 0x11: [data: 0x0] - 0x12: [data: 0x0] - 0x13: [size: 18] - 0x14: [data: 0x10670] - 0x15: [data: 0xf] - 0x16: [data: 0x1] - 0x17: [data: 0x1] - 0x18: [data: 0x0] - 0x19: [data: 0x0] - 0x1a: [data: 0x0] - 0x1b: [data: 0x0] - 0x1c: [data: 0x0] - 0x1d: [data: 0x0] - 0x1e: [data: 0x0] - 0x1f: [data: 0x0] - 0x20: [data: 0x0] - 0x21: [data: 0x0] - 0x22: [data: 0x0] - 0x23: [data: 0x0] - 0x24: [data: 0x0] - 0x25: [data: 0x0] - 0x26: [size: 18] - 0x27: [data: 0x10670] - 0x28: [data: 0xf] - 0x29: [data: 0x0] - 0x2a: [data: 0x0] - 0x2b: [data: 0x0] - 0x2c: [data: 0x0] - 0x2d: [data: 0x0] - 0x2e: [data: 0x0] - 0x2f: [data: 0x0] - 0x30: [data: 0x0] - 0x31: [data: 0x0] - 0x32: [data: 0x0] - 0x33: [data: 0x0] - 0x34: [data: 0x0] - 0x35: [data: 0x0] - 0x36: [data: 0x0] - 0x37: [data: 0x0] - 0x38: [data: 0x0] - 0x39: [size: 18] - 0x3a: [data: 0x10670] - 0x3b: [data: 0xf] - 0x3c: [data: 0x0] - 0x3d: [data: 0x0] - 0x3e: [data: 0x0] - 0x3f: [data: 0x0] - 0x40: [data: 0x0] - 0x41: [data: 0x0] - 0x42: [data: 0x0] - 0x43: [data: 0x0] - 0x44: [data: 0x0] - 0x45: [data: 0x0] - 0x46: [data: 0x0] - 0x47: [data: 0x0] - 0x48: [data: 0x0] - 0x49: [data: 0x0] - 0x4a: [data: 0x0] - 0x4b: [data: 0x0] - === GC status === - CLOS: 0x145a0 - === GC status === - Base stack pointer: aa2f5e08 - Start address of new space: 142a0 - Current space capacity: 256 - Allocate count: 6 - Allocated memory count: 912 bytes - Collect count: 0 - Allocated words in new space: 114 - Current new space: - 0x0: [size: 18] - 0x1: [data: 0x10670] - 0x2: [data: 0xf] - 0x3: [data: 0x0] - 0x4: [data: 0x0] - 0x5: [data: 0x0] - 0x6: [data: 0x0] - 0x7: [data: 0x0] - 0x8: [data: 0x0] - 0x9: [data: 0x0] - 0xa: [data: 0x0] - 0xb: [data: 0x0] - 0xc: [data: 0x0] - 0xd: [data: 0x0] - 0xe: [data: 0x0] - 0xf: [data: 0x0] - 0x10: [data: 0x0] - 0x11: [data: 0x0] - 0x12: [data: 0x0] - 0x13: [size: 18] - 0x14: [data: 0x10670] - 0x15: [data: 0xf] - 0x16: [data: 0x1] - 0x17: [data: 0x1] - 0x18: [data: 0x0] - 0x19: [data: 0x0] - 0x1a: [data: 0x0] - 0x1b: [data: 0x0] - 0x1c: [data: 0x0] - 0x1d: [data: 0x0] - 0x1e: [data: 0x0] - 0x1f: [data: 0x0] - 0x20: [data: 0x0] - 0x21: [data: 0x0] - 0x22: [data: 0x0] - 0x23: [data: 0x0] - 0x24: [data: 0x0] - 0x25: [data: 0x0] - 0x26: [size: 18] - 0x27: [data: 0x10670] - 0x28: [data: 0xf] - 0x29: [data: 0x0] - 0x2a: [data: 0x0] - 0x2b: [data: 0x0] - 0x2c: [data: 0x0] - 0x2d: [data: 0x0] - 0x2e: [data: 0x0] - 0x2f: [data: 0x0] - 0x30: [data: 0x0] - 0x31: [data: 0x0] - 0x32: [data: 0x0] - 0x33: [data: 0x0] - 0x34: [data: 0x0] - 0x35: [data: 0x0] - 0x36: [data: 0x0] - 0x37: [data: 0x0] - 0x38: [data: 0x0] - 0x39: [size: 18] - 0x3a: [data: 0x10670] - 0x3b: [data: 0xf] - 0x3c: [data: 0x1] - 0x3d: [data: 0x1] - 0x3e: [data: 0x0] - 0x3f: [data: 0x0] - 0x40: [data: 0x0] - 0x41: [data: 0x0] - 0x42: [data: 0x0] - 0x43: [data: 0x0] - 0x44: [data: 0x0] - 0x45: [data: 0x0] - 0x46: [data: 0x0] - 0x47: [data: 0x0] - 0x48: [data: 0x0] - 0x49: [data: 0x0] - 0x4a: [data: 0x0] - 0x4b: [data: 0x0] - 0x4c: [size: 18] - 0x4d: [data: 0x10670] - 0x4e: [data: 0xf] - 0x4f: [data: 0x0] - 0x50: [data: 0x0] - 0x51: [data: 0x0] - 0x52: [data: 0x0] - 0x53: [data: 0x0] - 0x54: [data: 0x0] - 0x55: [data: 0x0] - 0x56: [data: 0x0] - 0x57: [data: 0x0] - 0x58: [data: 0x0] - 0x59: [data: 0x0] - 0x5a: [data: 0x0] - 0x5b: [data: 0x0] - 0x5c: [data: 0x0] - 0x5d: [data: 0x0] - 0x5e: [data: 0x0] - 0x5f: [size: 18] - 0x60: [data: 0x10670] - 0x61: [data: 0xf] - 0x62: [data: 0x0] - 0x63: [data: 0x0] - 0x64: [data: 0x0] - 0x65: [data: 0x0] - 0x66: [data: 0x0] - 0x67: [data: 0x0] - 0x68: [data: 0x0] - 0x69: [data: 0x0] - 0x6a: [data: 0x0] - 0x6b: [data: 0x0] - 0x6c: [data: 0x0] - 0x6d: [data: 0x0] - 0x6e: [data: 0x0] - 0x6f: [data: 0x0] - 0x70: [data: 0x0] - 0x71: [data: 0x0] - === GC status === - CLOS: 0x146d0 - === GC status === - Base stack pointer: aa2f5e08 - Start address of new space: 142a0 - Current space capacity: 256 - Allocate count: 8 - Allocated memory count: 1216 bytes - Collect count: 0 - Allocated words in new space: 152 - Current new space: - 0x0: [size: 18] - 0x1: [data: 0x10670] - 0x2: [data: 0xf] - 0x3: [data: 0x0] - 0x4: [data: 0x0] - 0x5: [data: 0x0] - 0x6: [data: 0x0] - 0x7: [data: 0x0] - 0x8: [data: 0x0] - 0x9: [data: 0x0] - 0xa: [data: 0x0] - 0xb: [data: 0x0] - 0xc: [data: 0x0] - 0xd: [data: 0x0] - 0xe: [data: 0x0] - 0xf: [data: 0x0] - 0x10: [data: 0x0] - 0x11: [data: 0x0] - 0x12: [data: 0x0] - 0x13: [size: 18] - 0x14: [data: 0x10670] - 0x15: [data: 0xf] - 0x16: [data: 0x1] - 0x17: [data: 0x1] - 0x18: [data: 0x0] - 0x19: [data: 0x0] - 0x1a: [data: 0x0] - 0x1b: [data: 0x0] - 0x1c: [data: 0x0] - 0x1d: [data: 0x0] - 0x1e: [data: 0x0] - 0x1f: [data: 0x0] - 0x20: [data: 0x0] - 0x21: [data: 0x0] - 0x22: [data: 0x0] - 0x23: [data: 0x0] - 0x24: [data: 0x0] - 0x25: [data: 0x0] - 0x26: [size: 18] - 0x27: [data: 0x10670] - 0x28: [data: 0xf] - 0x29: [data: 0x0] - 0x2a: [data: 0x0] - 0x2b: [data: 0x0] - 0x2c: [data: 0x0] - 0x2d: [data: 0x0] - 0x2e: [data: 0x0] - 0x2f: [data: 0x0] - 0x30: [data: 0x0] - 0x31: [data: 0x0] - 0x32: [data: 0x0] - 0x33: [data: 0x0] - 0x34: [data: 0x0] - 0x35: [data: 0x0] - 0x36: [data: 0x0] - 0x37: [data: 0x0] - 0x38: [data: 0x0] - 0x39: [size: 18] - 0x3a: [data: 0x10670] - 0x3b: [data: 0xf] - 0x3c: [data: 0x1] - 0x3d: [data: 0x1] - 0x3e: [data: 0x0] - 0x3f: [data: 0x0] - 0x40: [data: 0x0] - 0x41: [data: 0x0] - 0x42: [data: 0x0] - 0x43: [data: 0x0] - 0x44: [data: 0x0] - 0x45: [data: 0x0] - 0x46: [data: 0x0] - 0x47: [data: 0x0] - 0x48: [data: 0x0] - 0x49: [data: 0x0] - 0x4a: [data: 0x0] - 0x4b: [data: 0x0] - 0x4c: [size: 18] - 0x4d: [data: 0x10670] - 0x4e: [data: 0xf] - 0x4f: [data: 0x0] - 0x50: [data: 0x0] - 0x51: [data: 0x0] - 0x52: [data: 0x0] - 0x53: [data: 0x0] - 0x54: [data: 0x0] - 0x55: [data: 0x0] - 0x56: [data: 0x0] - 0x57: [data: 0x0] - 0x58: [data: 0x0] - 0x59: [data: 0x0] - 0x5a: [data: 0x0] - 0x5b: [data: 0x0] - 0x5c: [data: 0x0] - 0x5d: [data: 0x0] - 0x5e: [data: 0x0] - 0x5f: [size: 18] - 0x60: [data: 0x10670] - 0x61: [data: 0xf] - 0x62: [data: 0x1] - 0x63: [data: 0x1] - 0x64: [data: 0x0] - 0x65: [data: 0x0] - 0x66: [data: 0x0] - 0x67: [data: 0x0] - 0x68: [data: 0x0] - 0x69: [data: 0x0] - 0x6a: [data: 0x0] - 0x6b: [data: 0x0] - 0x6c: [data: 0x0] - 0x6d: [data: 0x0] - 0x6e: [data: 0x0] - 0x6f: [data: 0x0] - 0x70: [data: 0x0] - 0x71: [data: 0x0] - 0x72: [size: 18] - 0x73: [data: 0x10670] - 0x74: [data: 0xf] - 0x75: [data: 0x0] - 0x76: [data: 0x0] - 0x77: [data: 0x0] - 0x78: [data: 0x0] - 0x79: [data: 0x0] - 0x7a: [data: 0x0] - 0x7b: [data: 0x0] - 0x7c: [data: 0x0] - 0x7d: [data: 0x0] - 0x7e: [data: 0x0] - 0x7f: [data: 0x0] - 0x80: [data: 0x0] - 0x81: [data: 0x0] - 0x82: [data: 0x0] - 0x83: [data: 0x0] - 0x84: [data: 0x0] - 0x85: [size: 18] - 0x86: [data: 0x10670] - 0x87: [data: 0xf] - 0x88: [data: 0x0] - 0x89: [data: 0x0] - 0x8a: [data: 0x0] - 0x8b: [data: 0x0] - 0x8c: [data: 0x0] - 0x8d: [data: 0x0] - 0x8e: [data: 0x0] - 0x8f: [data: 0x0] - 0x90: [data: 0x0] - 0x91: [data: 0x0] - 0x92: [data: 0x0] - 0x93: [data: 0x0] - 0x94: [data: 0x0] - 0x95: [data: 0x0] - 0x96: [data: 0x0] - 0x97: [data: 0x0] - === GC status === - CLOS: 0x14800 - === GC status === - Base stack pointer: aa2f5e08 - Start address of new space: 142a0 - Current space capacity: 256 - Allocate count: 10 - Allocated memory count: 1520 bytes - Collect count: 0 - Allocated words in new space: 190 + (0x1000) 0x0: [size: 5] + (0x1008) 0x1: [data: 0x10670] + (0x1010) 0x2: [data: 0x2] + (0x1018) 0x3: [data: 0x0] + (0x1020) 0x4: [data: 0x0] + (0x1028) 0x5: [data: 0x0] + (0x1030) 0x6: [size: 5] + (0x1038) 0x7: [data: 0x10670] + (0x1040) 0x8: [data: 0x2] + (0x1048) 0x9: [data: 0x1] + (0x1050) 0xa: [data: 0x5] + (0x1058) 0xb: [data: 0x0] + (0x1060) 0xc: [size: 5] + (0x1068) 0xd: [data: 0x10670] + (0x1070) 0xe: [data: 0x2] + (0x1078) 0xf: [data: 0x0] + (0x1080) 0x10: [data: 0x0] + (0x1088) 0x11: [data: 0x0] + (0x1090) 0x12: [size: 5] + (0x1098) 0x13: [data: 0x10670] + (0x10a0) 0x14: [data: 0x2] + (0x10a8) 0x15: [data: 0x1] + (0x10b0) 0x16: [data: 0x3] + (0x10b8) 0x17: [data: 0x0] + === GC status === + === GC status === + Start address of new space: 11000 + Allocate count: 4 times + Collect count: 1 times + Current space capacity: 8192 words + Total allocated memory: 24 words + Allocated words in new space: 12 words Current new space: - 0x0: [size: 18] - 0x1: [data: 0x10670] - 0x2: [data: 0xf] - 0x3: [data: 0x0] - 0x4: [data: 0x0] - 0x5: [data: 0x0] - 0x6: [data: 0x0] - 0x7: [data: 0x0] - 0x8: [data: 0x0] - 0x9: [data: 0x0] - 0xa: [data: 0x0] - 0xb: [data: 0x0] - 0xc: [data: 0x0] - 0xd: [data: 0x0] - 0xe: [data: 0x0] - 0xf: [data: 0x0] - 0x10: [data: 0x0] - 0x11: [data: 0x0] - 0x12: [data: 0x0] - 0x13: [size: 18] - 0x14: [data: 0x10670] - 0x15: [data: 0xf] - 0x16: [data: 0x1] - 0x17: [data: 0x1] - 0x18: [data: 0x0] - 0x19: [data: 0x0] - 0x1a: [data: 0x0] - 0x1b: [data: 0x0] - 0x1c: [data: 0x0] - 0x1d: [data: 0x0] - 0x1e: [data: 0x0] - 0x1f: [data: 0x0] - 0x20: [data: 0x0] - 0x21: [data: 0x0] - 0x22: [data: 0x0] - 0x23: [data: 0x0] - 0x24: [data: 0x0] - 0x25: [data: 0x0] - 0x26: [size: 18] - 0x27: [data: 0x10670] - 0x28: [data: 0xf] - 0x29: [data: 0x0] - 0x2a: [data: 0x0] - 0x2b: [data: 0x0] - 0x2c: [data: 0x0] - 0x2d: [data: 0x0] - 0x2e: [data: 0x0] - 0x2f: [data: 0x0] - 0x30: [data: 0x0] - 0x31: [data: 0x0] - 0x32: [data: 0x0] - 0x33: [data: 0x0] - 0x34: [data: 0x0] - 0x35: [data: 0x0] - 0x36: [data: 0x0] - 0x37: [data: 0x0] - 0x38: [data: 0x0] - 0x39: [size: 18] - 0x3a: [data: 0x10670] - 0x3b: [data: 0xf] - 0x3c: [data: 0x1] - 0x3d: [data: 0x1] - 0x3e: [data: 0x0] - 0x3f: [data: 0x0] - 0x40: [data: 0x0] - 0x41: [data: 0x0] - 0x42: [data: 0x0] - 0x43: [data: 0x0] - 0x44: [data: 0x0] - 0x45: [data: 0x0] - 0x46: [data: 0x0] - 0x47: [data: 0x0] - 0x48: [data: 0x0] - 0x49: [data: 0x0] - 0x4a: [data: 0x0] - 0x4b: [data: 0x0] - 0x4c: [size: 18] - 0x4d: [data: 0x10670] - 0x4e: [data: 0xf] - 0x4f: [data: 0x0] - 0x50: [data: 0x0] - 0x51: [data: 0x0] - 0x52: [data: 0x0] - 0x53: [data: 0x0] - 0x54: [data: 0x0] - 0x55: [data: 0x0] - 0x56: [data: 0x0] - 0x57: [data: 0x0] - 0x58: [data: 0x0] - 0x59: [data: 0x0] - 0x5a: [data: 0x0] - 0x5b: [data: 0x0] - 0x5c: [data: 0x0] - 0x5d: [data: 0x0] - 0x5e: [data: 0x0] - 0x5f: [size: 18] - 0x60: [data: 0x10670] - 0x61: [data: 0xf] - 0x62: [data: 0x1] - 0x63: [data: 0x1] - 0x64: [data: 0x0] - 0x65: [data: 0x0] - 0x66: [data: 0x0] - 0x67: [data: 0x0] - 0x68: [data: 0x0] - 0x69: [data: 0x0] - 0x6a: [data: 0x0] - 0x6b: [data: 0x0] - 0x6c: [data: 0x0] - 0x6d: [data: 0x0] - 0x6e: [data: 0x0] - 0x6f: [data: 0x0] - 0x70: [data: 0x0] - 0x71: [data: 0x0] - 0x72: [size: 18] - 0x73: [data: 0x10670] - 0x74: [data: 0xf] - 0x75: [data: 0x0] - 0x76: [data: 0x0] - 0x77: [data: 0x0] - 0x78: [data: 0x0] - 0x79: [data: 0x0] - 0x7a: [data: 0x0] - 0x7b: [data: 0x0] - 0x7c: [data: 0x0] - 0x7d: [data: 0x0] - 0x7e: [data: 0x0] - 0x7f: [data: 0x0] - 0x80: [data: 0x0] - 0x81: [data: 0x0] - 0x82: [data: 0x0] - 0x83: [data: 0x0] - 0x84: [data: 0x0] - 0x85: [size: 18] - 0x86: [data: 0x10670] - 0x87: [data: 0xf] - 0x88: [data: 0x1] - 0x89: [data: 0x1] - 0x8a: [data: 0x0] - 0x8b: [data: 0x0] - 0x8c: [data: 0x0] - 0x8d: [data: 0x0] - 0x8e: [data: 0x0] - 0x8f: [data: 0x0] - 0x90: [data: 0x0] - 0x91: [data: 0x0] - 0x92: [data: 0x0] - 0x93: [data: 0x0] - 0x94: [data: 0x0] - 0x95: [data: 0x0] - 0x96: [data: 0x0] - 0x97: [data: 0x0] - 0x98: [size: 18] - 0x99: [data: 0x10670] - 0x9a: [data: 0xf] - 0x9b: [data: 0x0] - 0x9c: [data: 0x0] - 0x9d: [data: 0x0] - 0x9e: [data: 0x0] - 0x9f: [data: 0x0] - 0xa0: [data: 0x0] - 0xa1: [data: 0x0] - 0xa2: [data: 0x0] - 0xa3: [data: 0x0] - 0xa4: [data: 0x0] - 0xa5: [data: 0x0] - 0xa6: [data: 0x0] - 0xa7: [data: 0x0] - 0xa8: [data: 0x0] - 0xa9: [data: 0x0] - 0xaa: [data: 0x0] - 0xab: [size: 18] - 0xac: [data: 0x10670] - 0xad: [data: 0xf] - 0xae: [data: 0x0] - 0xaf: [data: 0x0] - 0xb0: [data: 0x0] - 0xb1: [data: 0x0] - 0xb2: [data: 0x0] - 0xb3: [data: 0x0] - 0xb4: [data: 0x0] - 0xb5: [data: 0x0] - 0xb6: [data: 0x0] - 0xb7: [data: 0x0] - 0xb8: [data: 0x0] - 0xb9: [data: 0x0] - 0xba: [data: 0x0] - 0xbb: [data: 0x0] - 0xbc: [data: 0x0] - 0xbd: [data: 0x0] - === GC status === - CLOS: 0x14930 - === GC status === - Base stack pointer: aa2f5e08 - Start address of new space: 142a0 - Current space capacity: 256 - Allocate count: 12 - Allocated memory count: 1824 bytes - Collect count: 0 - Allocated words in new space: 228 + (0x11000) 0x0: [size: 5] + (0x11008) 0x1: [data: 0x10670] + (0x11010) 0x2: [data: 0x2] + (0x11018) 0x3: [data: 0x1] + (0x11020) 0x4: [data: 0x5] + (0x11028) 0x5: [data: 0x0] + (0x11030) 0x6: [size: 5] + (0x11038) 0x7: [data: 0x10670] + (0x11040) 0x8: [data: 0x2] + (0x11048) 0x9: [data: 0x1] + (0x11050) 0xa: [data: 0x3] + (0x11058) 0xb: [data: 0x0] + === GC status === + === GC status === + Start address of new space: 11000 + Allocate count: 5 times + Collect count: 1 times + Current space capacity: 8192 words + Total allocated memory: 30 words + Allocated words in new space: 18 words Current new space: - 0x0: [size: 18] - 0x1: [data: 0x10670] - 0x2: [data: 0xf] - 0x3: [data: 0x0] - 0x4: [data: 0x0] - 0x5: [data: 0x0] - 0x6: [data: 0x0] - 0x7: [data: 0x0] - 0x8: [data: 0x0] - 0x9: [data: 0x0] - 0xa: [data: 0x0] - 0xb: [data: 0x0] - 0xc: [data: 0x0] - 0xd: [data: 0x0] - 0xe: [data: 0x0] - 0xf: [data: 0x0] - 0x10: [data: 0x0] - 0x11: [data: 0x0] - 0x12: [data: 0x0] - 0x13: [size: 18] - 0x14: [data: 0x10670] - 0x15: [data: 0xf] - 0x16: [data: 0x1] - 0x17: [data: 0x1] - 0x18: [data: 0x0] - 0x19: [data: 0x0] - 0x1a: [data: 0x0] - 0x1b: [data: 0x0] - 0x1c: [data: 0x0] - 0x1d: [data: 0x0] - 0x1e: [data: 0x0] - 0x1f: [data: 0x0] - 0x20: [data: 0x0] - 0x21: [data: 0x0] - 0x22: [data: 0x0] - 0x23: [data: 0x0] - 0x24: [data: 0x0] - 0x25: [data: 0x0] - 0x26: [size: 18] - 0x27: [data: 0x10670] - 0x28: [data: 0xf] - 0x29: [data: 0x0] - 0x2a: [data: 0x0] - 0x2b: [data: 0x0] - 0x2c: [data: 0x0] - 0x2d: [data: 0x0] - 0x2e: [data: 0x0] - 0x2f: [data: 0x0] - 0x30: [data: 0x0] - 0x31: [data: 0x0] - 0x32: [data: 0x0] - 0x33: [data: 0x0] - 0x34: [data: 0x0] - 0x35: [data: 0x0] - 0x36: [data: 0x0] - 0x37: [data: 0x0] - 0x38: [data: 0x0] - 0x39: [size: 18] - 0x3a: [data: 0x10670] - 0x3b: [data: 0xf] - 0x3c: [data: 0x1] - 0x3d: [data: 0x1] - 0x3e: [data: 0x0] - 0x3f: [data: 0x0] - 0x40: [data: 0x0] - 0x41: [data: 0x0] - 0x42: [data: 0x0] - 0x43: [data: 0x0] - 0x44: [data: 0x0] - 0x45: [data: 0x0] - 0x46: [data: 0x0] - 0x47: [data: 0x0] - 0x48: [data: 0x0] - 0x49: [data: 0x0] - 0x4a: [data: 0x0] - 0x4b: [data: 0x0] - 0x4c: [size: 18] - 0x4d: [data: 0x10670] - 0x4e: [data: 0xf] - 0x4f: [data: 0x0] - 0x50: [data: 0x0] - 0x51: [data: 0x0] - 0x52: [data: 0x0] - 0x53: [data: 0x0] - 0x54: [data: 0x0] - 0x55: [data: 0x0] - 0x56: [data: 0x0] - 0x57: [data: 0x0] - 0x58: [data: 0x0] - 0x59: [data: 0x0] - 0x5a: [data: 0x0] - 0x5b: [data: 0x0] - 0x5c: [data: 0x0] - 0x5d: [data: 0x0] - 0x5e: [data: 0x0] - 0x5f: [size: 18] - 0x60: [data: 0x10670] - 0x61: [data: 0xf] - 0x62: [data: 0x1] - 0x63: [data: 0x1] - 0x64: [data: 0x0] - 0x65: [data: 0x0] - 0x66: [data: 0x0] - 0x67: [data: 0x0] - 0x68: [data: 0x0] - 0x69: [data: 0x0] - 0x6a: [data: 0x0] - 0x6b: [data: 0x0] - 0x6c: [data: 0x0] - 0x6d: [data: 0x0] - 0x6e: [data: 0x0] - 0x6f: [data: 0x0] - 0x70: [data: 0x0] - 0x71: [data: 0x0] - 0x72: [size: 18] - 0x73: [data: 0x10670] - 0x74: [data: 0xf] - 0x75: [data: 0x0] - 0x76: [data: 0x0] - 0x77: [data: 0x0] - 0x78: [data: 0x0] - 0x79: [data: 0x0] - 0x7a: [data: 0x0] - 0x7b: [data: 0x0] - 0x7c: [data: 0x0] - 0x7d: [data: 0x0] - 0x7e: [data: 0x0] - 0x7f: [data: 0x0] - 0x80: [data: 0x0] - 0x81: [data: 0x0] - 0x82: [data: 0x0] - 0x83: [data: 0x0] - 0x84: [data: 0x0] - 0x85: [size: 18] - 0x86: [data: 0x10670] - 0x87: [data: 0xf] - 0x88: [data: 0x1] - 0x89: [data: 0x1] - 0x8a: [data: 0x0] - 0x8b: [data: 0x0] - 0x8c: [data: 0x0] - 0x8d: [data: 0x0] - 0x8e: [data: 0x0] - 0x8f: [data: 0x0] - 0x90: [data: 0x0] - 0x91: [data: 0x0] - 0x92: [data: 0x0] - 0x93: [data: 0x0] - 0x94: [data: 0x0] - 0x95: [data: 0x0] - 0x96: [data: 0x0] - 0x97: [data: 0x0] - 0x98: [size: 18] - 0x99: [data: 0x10670] - 0x9a: [data: 0xf] - 0x9b: [data: 0x0] - 0x9c: [data: 0x0] - 0x9d: [data: 0x0] - 0x9e: [data: 0x0] - 0x9f: [data: 0x0] - 0xa0: [data: 0x0] - 0xa1: [data: 0x0] - 0xa2: [data: 0x0] - 0xa3: [data: 0x0] - 0xa4: [data: 0x0] - 0xa5: [data: 0x0] - 0xa6: [data: 0x0] - 0xa7: [data: 0x0] - 0xa8: [data: 0x0] - 0xa9: [data: 0x0] - 0xaa: [data: 0x0] - 0xab: [size: 18] - 0xac: [data: 0x10670] - 0xad: [data: 0xf] - 0xae: [data: 0x1] - 0xaf: [data: 0x1] - 0xb0: [data: 0x0] - 0xb1: [data: 0x0] - 0xb2: [data: 0x0] - 0xb3: [data: 0x0] - 0xb4: [data: 0x0] - 0xb5: [data: 0x0] - 0xb6: [data: 0x0] - 0xb7: [data: 0x0] - 0xb8: [data: 0x0] - 0xb9: [data: 0x0] - 0xba: [data: 0x0] - 0xbb: [data: 0x0] - 0xbc: [data: 0x0] - 0xbd: [data: 0x0] - 0xbe: [size: 18] - 0xbf: [data: 0x10670] - 0xc0: [data: 0xf] - 0xc1: [data: 0x0] - 0xc2: [data: 0x0] - 0xc3: [data: 0x0] - 0xc4: [data: 0x0] - 0xc5: [data: 0x0] - 0xc6: [data: 0x0] - 0xc7: [data: 0x0] - 0xc8: [data: 0x0] - 0xc9: [data: 0x0] - 0xca: [data: 0x0] - 0xcb: [data: 0x0] - 0xcc: [data: 0x0] - 0xcd: [data: 0x0] - 0xce: [data: 0x0] - 0xcf: [data: 0x0] - 0xd0: [data: 0x0] - 0xd1: [size: 18] - 0xd2: [data: 0x10670] - 0xd3: [data: 0xf] - 0xd4: [data: 0x0] - 0xd5: [data: 0x0] - 0xd6: [data: 0x0] - 0xd7: [data: 0x0] - 0xd8: [data: 0x0] - 0xd9: [data: 0x0] - 0xda: [data: 0x0] - 0xdb: [data: 0x0] - 0xdc: [data: 0x0] - 0xdd: [data: 0x0] - 0xde: [data: 0x0] - 0xdf: [data: 0x0] - 0xe0: [data: 0x0] - 0xe1: [data: 0x0] - 0xe2: [data: 0x0] - 0xe3: [data: 0x0] - === GC status === - Try to find stack cell with 0x142a8 value on 0x0 offset - Try to find stack cell with 0x14340 value on 0x19 offset - Try to find stack cell with 0x143d8 value on 0x38 offset - Try to find stack cell with 0x14470 value on 0x57 offset - Try to find stack cell with 0x14508 value on 0x76 offset - Try to find stack cell with 0x145a0 value on 0x95 offset - Try to find stack cell with 0x14638 value on 0x114 offset - Try to find stack cell with 0x146d0 value on 0x133 offset - Try to find stack cell with 0x14768 value on 0x152 offset - Try to find stack cell with 0x14800 value on 0x171 offset - Try to find stack cell with 0x14898 value on 0x190 offset - Try to find stack cell with 0x14930 value on 0x209 offset - Try to find stack cell with 0x149c8 value on 0x228 offset - FOUND AT STACK: 4. CUR_OFFSET: e4, CUR_POINTER: 149c8, byte: aa2f5de8, *byte: 149c8 - NEW POINTER: 0x14ab0 - RUN CHANGING - === STACK status === - BASE_SP: 0xaa2f5e08, CURRENT_SP: 0xaa2f5cb8 - STACK SIZE: 42 - 0xaa2f5e08: 0x0 - 0xaa2f5e00: 0x1068e - 0xaa2f5df8: 0x1 - 0xaa2f5df0: 0x1 - 0xaa2f5de8: 0x149c8 - 0xaa2f5de0: 0x107d6 - 0xaa2f5dd8: 0xaa2f5e40 - 0xaa2f5dd0: 0x107c4 - 0xaa2f5dc8: 0xaa2f5e40 - 0xaa2f5dc0: 0x149c8 - 0xaa2f5db8: 0xaa2f5e00 - 0xaa2f5db0: 0x0 - 0xaa2f5da8: 0x14a58 - 0xaa2f5da0: 0x149c8 - 0xaa2f5d98: 0x149e0 - 0xaa2f5d90: 0x11574 - 0xaa2f5d88: 0xaa2f5de8 - 0xaa2f5d80: 0x149c8 - 0xaa2f5d78: 0x149c8 - 0xaa2f5d70: 0x90 - 0xaa2f5d68: 0xa999b400 - 0xaa2f5d60: 0x149c8 - 0xaa2f5d58: 0xaa2f5d98 - 0xaa2f5d50: 0xf - 0xaa2f5d48: 0x10670 - 0xaa2f5d40: 0x114c8 - 0xaa2f5d38: 0xaa2f5d98 - 0xaa2f5d30: 0xa98ad480 - 0xaa2f5d28: 0x209 - 0xaa2f5d20: 0x0 - 0xaa2f5d18: 0x0 - 0xaa2f5d10: 0x0 - 0xaa2f5d08: 0x0 - 0xaa2f5d00: 0x11448 - 0xaa2f5cf8: 0xaa2f5d48 - 0xaa2f5cf0: 0xa99991f8 - 0xaa2f5ce8: 0x12 - 0xaa2f5ce0: 0x90 - 0xaa2f5cd8: 0x0 - 0xaa2f5cd0: 0x1131e - 0xaa2f5cc8: 0xaa2f5d08 - 0xaa2f5cc0: 0xaa2f5cb8 - === STACK status === - Change stack cell 0xaa2f5de8. 0x149c8 -> 0x14ab0 - Change stack cell 0xaa2f5dc0. 0x149c8 -> 0x14ab0 - Change stack cell 0xaa2f5d80. 0x149c8 -> 0x14ab0 - Change stack cell 0xaa2f5d78. 0x149c8 -> 0x14ab0 - Change stack cell 0xaa2f5d60. 0x149c8 -> 0x14ab0 - === STACK status === - BASE_SP: 0xaa2f5e08, CURRENT_SP: 0xaa2f5cb8 - STACK SIZE: 42 - 0xaa2f5e08: 0x0 - 0xaa2f5e00: 0x1068e - 0xaa2f5df8: 0x1 - 0xaa2f5df0: 0x1 - 0xaa2f5de8: 0x14ab0 - 0xaa2f5de0: 0x107d6 - 0xaa2f5dd8: 0xaa2f5e40 - 0xaa2f5dd0: 0x107c4 - 0xaa2f5dc8: 0xaa2f5e40 - 0xaa2f5dc0: 0x14ab0 - 0xaa2f5db8: 0xaa2f5e00 - 0xaa2f5db0: 0x0 - 0xaa2f5da8: 0x14a58 - 0xaa2f5da0: 0x149c8 - 0xaa2f5d98: 0x149e0 - 0xaa2f5d90: 0x11574 - 0xaa2f5d88: 0xaa2f5de8 - 0xaa2f5d80: 0x14ab0 - 0xaa2f5d78: 0x14ab0 - 0xaa2f5d70: 0x90 - 0xaa2f5d68: 0xa999b400 - 0xaa2f5d60: 0x14ab0 - 0xaa2f5d58: 0xaa2f5d98 - 0xaa2f5d50: 0xf - 0xaa2f5d48: 0x10670 - 0xaa2f5d40: 0x114c8 - 0xaa2f5d38: 0xaa2f5d98 - 0xaa2f5d30: 0xa98ad480 - 0xaa2f5d28: 0x209 - 0xaa2f5d20: 0x0 - 0xaa2f5d18: 0x0 - 0xaa2f5d10: 0x0 - 0xaa2f5d08: 0x0 - 0xaa2f5d00: 0x11448 - 0xaa2f5cf8: 0xaa2f5d48 - 0xaa2f5cf0: 0xa99991f8 - 0xaa2f5ce8: 0x12 - 0xaa2f5ce0: 0x90 - 0xaa2f5cd8: 0x0 - 0xaa2f5cd0: 0x1131e - 0xaa2f5cc8: 0xaa2f5d08 - 0xaa2f5cc0: 0xaa2f5cb8 - === STACK status === - CLOS: 0x14b50 - === GC status === - Base stack pointer: aa2f5e08 - Start address of new space: 14ab0 - Current space capacity: 256 - Allocate count: 14 - Allocated memory count: 2128 bytes - Collect count: 1 - Allocated words in new space: 38 - Current new space: - 0x0: [size: 18] - 0x1: [data: 0x10670] - 0x2: [data: 0xf] - 0x3: [data: 0x0] - 0x4: [data: 0x0] - 0x5: [data: 0x0] - 0x6: [data: 0x0] - 0x7: [data: 0x0] - 0x8: [data: 0x0] - 0x9: [data: 0x0] - 0xa: [data: 0x0] - 0xb: [data: 0x0] - 0xc: [data: 0x0] - 0xd: [data: 0x0] - 0xe: [data: 0x0] - 0xf: [data: 0x0] - 0x10: [data: 0x0] - 0x11: [data: 0x0] - 0x12: [data: 0x0] - 0x13: [size: 18] - 0x14: [data: 0x10670] - 0x15: [data: 0xf] - 0x16: [data: 0xf] - 0x17: [data: 0x0] - 0x18: [data: 0x0] - 0x19: [data: 0x0] - 0x1a: [data: 0x0] - 0x1b: [data: 0x0] - 0x1c: [data: 0x0] - 0x1d: [data: 0x0] - 0x1e: [data: 0x0] - 0x1f: [data: 0x0] - 0x20: [data: 0x0] - 0x21: [data: 0x0] - 0x22: [data: 0x0] - 0x23: [data: 0x0] - 0x24: [data: 0x0] - 0x25: [data: 0x0] - === GC status === - Runtime error: function accept more arguments than expect + (0x11000) 0x0: [size: 5] + (0x11008) 0x1: [data: 0x10670] + (0x11010) 0x2: [data: 0x2] + (0x11018) 0x3: [data: 0x1] + (0x11020) 0x4: [data: 0x5] + (0x11028) 0x5: [data: 0x0] + (0x11030) 0x6: [size: 5] + (0x11038) 0x7: [data: 0x10670] + (0x11040) 0x8: [data: 0x2] + (0x11048) 0x9: [data: 0x1] + (0x11050) 0xa: [data: 0x3] + (0x11058) 0xb: [data: 0x0] + (0x11060) 0xc: [size: 5] + (0x11068) 0xd: [data: 0x10670] + (0x11070) 0xe: [data: 0x2] + (0x11078) 0xf: [data: 0x2] + (0x11080) 0x10: [data: 0x5] + (0x11088) 0x11: [data: 0x2] + === GC status === + 7 + $ cat ../main.anf + let add__0 = fun a__1 -> + fun b__2 -> + a__1 + b__2 + + + let main__3 = let anf_t7 = add__0 5 in + let homka1__4 = anf_t7 in + let anf_t6 = add__0 3 in + let homka2__5 = anf_t6 in + let anf_t5 = print_gc_status () in + let homka2__6 = anf_t5 in + let anf_t4 = gc_collect () in + let homka3__7 = anf_t4 in + let anf_t3 = print_gc_status () in + let homka4__8 = anf_t3 in + let anf_t2 = homka1__4 2 in + let lol__9 = anf_t2 in + let anf_t1 = print_gc_status () in + let homka5__10 = anf_t1 in + print_int lol__9 + $ cat ../main.s + .text + .globl add__0 + add__0: + addi sp, sp, -16 + sd ra, 8(sp) + sd fp, 0(sp) + addi fp, sp, 16 + ld t0, 0(fp) + ld t1, 8(fp) + add a0, t0, t1 + ld ra, 8(sp) + ld fp, 0(sp) + addi sp, sp, 16 + ret + .globl _start + _start: + mv fp, sp + mv a0, sp + call init_GC + addi sp, sp, -120 + # Partial application add__0 with 1 args + # Load args on stack + addi sp, sp, -32 + addi sp, sp, -16 + la t5, add__0 + li t6, 2 + sd t5, 0(sp) + sd t6, 8(sp) + call alloc_closure + mv t0, a0 + addi sp, sp, 16 + sd t0, 0(sp) + li t0, 1 + sd t0, 8(sp) + li t0, 5 + sd t0, 16(sp) + # End loading args on stack + call apply_closure + mv t0, a0 + # Free args on stack + addi sp, sp, 32 + # End free args on stack + # End Partial application add__0 with 1 args + sd t0, -8(fp) + ld t0, -8(fp) + sd t0, -16(fp) + # Partial application add__0 with 1 args + # Load args on stack + addi sp, sp, -32 + addi sp, sp, -16 + la t5, add__0 + li t6, 2 + sd t5, 0(sp) + sd t6, 8(sp) + call alloc_closure + mv t0, a0 + addi sp, sp, 16 + sd t0, 0(sp) + li t0, 1 + sd t0, 8(sp) + li t0, 3 + sd t0, 16(sp) + # End loading args on stack + call apply_closure + mv t0, a0 + # Free args on stack + addi sp, sp, 32 + # End free args on stack + # End Partial application add__0 with 1 args + sd t0, -24(fp) + ld t0, -24(fp) + sd t0, -32(fp) + call print_gc_status + sd t0, -40(fp) + ld t0, -40(fp) + sd t0, -48(fp) + call gc_collect + sd t0, -56(fp) + ld t0, -56(fp) + sd t0, -64(fp) + call print_gc_status + sd t0, -72(fp) + ld t0, -72(fp) + sd t0, -80(fp) + # Apply homka1__4 with 1 args + ld t0, -16(fp) + sd t0, -88(fp) + # Load args on stack + addi sp, sp, -32 + ld t0, -88(fp) + sd t0, 0(sp) + li t0, 1 + sd t0, 8(sp) + li t0, 2 + sd t0, 16(sp) + # End loading args on stack + call apply_closure + # Free args on stack + addi sp, sp, 32 + # End free args on stack + mv t0, a0 + # End Apply homka1__4 with 1 args + sd t0, -96(fp) + ld t0, -96(fp) + sd t0, -104(fp) + call print_gc_status + sd t0, -112(fp) + ld t0, -112(fp) + sd t0, -120(fp) + # Apply print_int + ld a0, -104(fp) + call print_int + mv t0, a0 + # End Apply print_int + la t1, main__3 + sd t0, 0(t1) + call flush + li a0, 0 + li a7, 94 + ecall + .data + main__3: .dword 0 + + +( overflow heap, but nobody can help you ) + $ make compile opts=-gen_mid --no-print-directory -C .. << 'EOF' + > let rec fib n k = if n < 2 then k n else fib (n - 1) (fun a -> fib (n - 2) (fun b -> k (a + b))) + > let main = print_int (fib 15 (fun x -> x)) + > EOF + $ qemu-riscv64 -L /usr/riscv64-linux-gnu -cpu rv64 ../main.exe + panic! overflow memory limits [122] diff --git a/PudgeWithMoML/test/codegen.t b/PudgeWithMoML/test/codegen.t index 3112b7b1..af475369 100644 --- a/PudgeWithMoML/test/codegen.t +++ b/PudgeWithMoML/test/codegen.t @@ -13,9 +13,9 @@ .globl _start _start: mv fp, sp - addi sp, sp, 0 mv a0, sp call init_GC + addi sp, sp, 0 # Apply print_int li a0, 5 call print_int @@ -64,9 +64,9 @@ .globl _start _start: mv fp, sp - addi sp, sp, -8 mv a0, sp call init_GC + addi sp, sp, -8 # Apply add__0 with 2 args # Load args on stack addi sp, sp, -16 @@ -139,9 +139,9 @@ .globl _start _start: mv fp, sp - addi sp, sp, -8 mv a0, sp call init_GC + addi sp, sp, -8 # Apply homka__0 with 12 args # Load args on stack addi sp, sp, -96 @@ -223,9 +223,9 @@ .globl _start _start: mv fp, sp - addi sp, sp, -8 mv a0, sp call init_GC + addi sp, sp, -8 # Apply id__0 with 2 args # Load args on stack addi sp, sp, -16 @@ -263,27 +263,6 @@ > EOF $ qemu-riscv64 -L /usr/riscv64-linux-gnu -cpu rv64 ../main.exe | tee -a results.txt && echo "-----" >> results.txt - CLOS: 0x142d0 - === GC status === - Base stack pointer: dd28ce28 - Start address of new space: 142a0 - Current space capacity: 256 - Allocate count: 2 - Allocated memory count: 80 bytes - Collect count: 0 - Allocated words in new space: 10 - Current new space: - 0x0: [size: 4] - 0x1: [data: 0x106a0] - 0x2: [data: 0x1] - 0x3: [data: 0x0] - 0x4: [data: 0x0] - 0x5: [size: 4] - 0x6: [data: 0x106a0] - 0x7: [data: 0x1] - 0x8: [data: 0x0] - 0x9: [data: 0x0] - === GC status === 6 $ cat ../main.anf let app__0 = fun f__1 -> @@ -343,9 +322,9 @@ .globl _start _start: mv fp, sp - addi sp, sp, -8 mv a0, sp call init_GC + addi sp, sp, -8 # Apply app__0 with 2 args # Load args on stack addi sp, sp, -16 @@ -394,9 +373,9 @@ .globl _start _start: mv fp, sp - addi sp, sp, -32 mv a0, sp call init_GC + addi sp, sp, -32 li t0, 10 sd t0, -8(fp) li t0, 20 @@ -430,58 +409,6 @@ > EOF $ qemu-riscv64 -L /usr/riscv64-linux-gnu -cpu rv64 ../main.exe | tee -a results.txt && echo "-----" >> results.txt - CLOS: 0x142d8 - === GC status === - Base stack pointer: a3c3be10 - Start address of new space: 142a0 - Current space capacity: 256 - Allocate count: 2 - Allocated memory count: 96 bytes - Collect count: 0 - Allocated words in new space: 12 - Current new space: - 0x0: [size: 5] - 0x1: [data: 0x10670] - 0x2: [data: 0x2] - 0x3: [data: 0x0] - 0x4: [data: 0x0] - 0x5: [data: 0x0] - 0x6: [size: 5] - 0x7: [data: 0x10670] - 0x8: [data: 0x2] - 0x9: [data: 0x0] - 0xa: [data: 0x0] - 0xb: [data: 0x0] - === GC status === - CLOS: 0x14308 - === GC status === - Base stack pointer: a3c3be10 - Start address of new space: 142a0 - Current space capacity: 256 - Allocate count: 3 - Allocated memory count: 144 bytes - Collect count: 0 - Allocated words in new space: 18 - Current new space: - 0x0: [size: 5] - 0x1: [data: 0x10670] - 0x2: [data: 0x2] - 0x3: [data: 0x0] - 0x4: [data: 0x0] - 0x5: [data: 0x0] - 0x6: [size: 5] - 0x7: [data: 0x10670] - 0x8: [data: 0x2] - 0x9: [data: 0x1] - 0xa: [data: 0x1] - 0xb: [data: 0x0] - 0xc: [size: 5] - 0xd: [data: 0x10670] - 0xe: [data: 0x2] - 0xf: [data: 0x1] - 0x10: [data: 0x1] - 0x11: [data: 0x0] - === GC status === 122 $ cat ../main.anf let add__0 = fun x__1 -> @@ -511,9 +438,9 @@ .globl _start _start: mv fp, sp - addi sp, sp, -32 mv a0, sp call init_GC + addi sp, sp, -32 # Partial application add__0 with 1 args # Load args on stack addi sp, sp, -32 @@ -580,94 +507,7 @@ > EOF $ qemu-riscv64 -L /usr/riscv64-linux-gnu -cpu rv64 ../main.exe | tee -a results.txt && echo "-----" >> results.txt - CLOS: 0x142d8 - === GC status === - Base stack pointer: 98de0df8 - Start address of new space: 142a0 - Current space capacity: 256 - Allocate count: 2 - Allocated memory count: 96 bytes - Collect count: 0 - Allocated words in new space: 12 - Current new space: - 0x0: [size: 5] - 0x1: [data: 0x10670] - 0x2: [data: 0x2] - 0x3: [data: 0x0] - 0x4: [data: 0x0] - 0x5: [data: 0x0] - 0x6: [size: 5] - 0x7: [data: 0x10670] - 0x8: [data: 0x2] - 0x9: [data: 0x0] - 0xa: [data: 0x0] - 0xb: [data: 0x0] - === GC status === - CLOS: 0x14308 - === GC status === - Base stack pointer: 98de0df8 - Start address of new space: 142a0 - Current space capacity: 256 - Allocate count: 3 - Allocated memory count: 144 bytes - Collect count: 0 - Allocated words in new space: 18 - Current new space: - 0x0: [size: 5] - 0x1: [data: 0x10670] - 0x2: [data: 0x2] - 0x3: [data: 0x0] - 0x4: [data: 0x0] - 0x5: [data: 0x0] - 0x6: [size: 5] - 0x7: [data: 0x10670] - 0x8: [data: 0x2] - 0x9: [data: 0x1] - 0xa: [data: 0x1] - 0xb: [data: 0x0] - 0xc: [size: 5] - 0xd: [data: 0x10670] - 0xe: [data: 0x2] - 0xf: [data: 0x1] - 0x10: [data: 0x1] - 0x11: [data: 0x0] - === GC status === 122 - CLOS: 0x14338 - === GC status === - Base stack pointer: 98de0df8 - Start address of new space: 142a0 - Current space capacity: 256 - Allocate count: 4 - Allocated memory count: 192 bytes - Collect count: 0 - Allocated words in new space: 24 - Current new space: - 0x0: [size: 5] - 0x1: [data: 0x10670] - 0x2: [data: 0x2] - 0x3: [data: 0x0] - 0x4: [data: 0x0] - 0x5: [data: 0x0] - 0x6: [size: 5] - 0x7: [data: 0x10670] - 0x8: [data: 0x2] - 0x9: [data: 0x1] - 0xa: [data: 0x1] - 0xb: [data: 0x0] - 0xc: [size: 5] - 0xd: [data: 0x10670] - 0xe: [data: 0x2] - 0xf: [data: 0x2] - 0x10: [data: 0x1] - 0x11: [data: 0x79] - 0x12: [size: 5] - 0x13: [data: 0x10670] - 0x14: [data: 0x2] - 0x15: [data: 0x1] - 0x16: [data: 0x1] - 0x17: [data: 0x0] - === GC status === 123 $ cat ../main.anf let add__0 = fun x__1 -> @@ -700,9 +540,9 @@ .globl _start _start: mv fp, sp - addi sp, sp, -56 mv a0, sp call init_GC + addi sp, sp, -56 # Partial application add__0 with 1 args # Load args on stack addi sp, sp, -32 @@ -810,9 +650,9 @@ .globl _start _start: mv fp, sp - addi sp, sp, 0 mv a0, sp call init_GC + addi sp, sp, 0 li t0, 4 la t1, x__0 sd t0, 0(t1) @@ -843,58 +683,6 @@ > EOF $ qemu-riscv64 -L /usr/riscv64-linux-gnu -cpu rv64 ../main.exe | tee -a results.txt && echo "-----" >> results.txt - CLOS: 0x142d8 - === GC status === - Base stack pointer: ec7ce20 - Start address of new space: 142a0 - Current space capacity: 256 - Allocate count: 2 - Allocated memory count: 96 bytes - Collect count: 0 - Allocated words in new space: 12 - Current new space: - 0x0: [size: 5] - 0x1: [data: 0x10670] - 0x2: [data: 0x2] - 0x3: [data: 0x0] - 0x4: [data: 0x0] - 0x5: [data: 0x0] - 0x6: [size: 5] - 0x7: [data: 0x10670] - 0x8: [data: 0x2] - 0x9: [data: 0x0] - 0xa: [data: 0x0] - 0xb: [data: 0x0] - === GC status === - CLOS: 0x14308 - === GC status === - Base stack pointer: ec7ce20 - Start address of new space: 142a0 - Current space capacity: 256 - Allocate count: 3 - Allocated memory count: 144 bytes - Collect count: 0 - Allocated words in new space: 18 - Current new space: - 0x0: [size: 5] - 0x1: [data: 0x10670] - 0x2: [data: 0x2] - 0x3: [data: 0x0] - 0x4: [data: 0x0] - 0x5: [data: 0x0] - 0x6: [size: 5] - 0x7: [data: 0x10670] - 0x8: [data: 0x2] - 0x9: [data: 0x1] - 0xa: [data: 0x5] - 0xb: [data: 0x0] - 0xc: [size: 5] - 0xd: [data: 0x10670] - 0xe: [data: 0x2] - 0xf: [data: 0x1] - 0x10: [data: 0x5] - 0x11: [data: 0x0] - === GC status === 122 $ cat ../main.anf let add__0 = fun x__1 -> @@ -926,9 +714,9 @@ .globl _start _start: mv fp, sp - addi sp, sp, -16 mv a0, sp call init_GC + addi sp, sp, -16 # Partial application add__0 with 1 args # Load args on stack addi sp, sp, -32 @@ -1000,105 +788,6 @@ > EOF $ qemu-riscv64 -L /usr/riscv64-linux-gnu -cpu rv64 ../main.exe | tee -a results.txt && echo "-----" >> results.txt - CLOS: 0x142d8 - === GC status === - Base stack pointer: 2b23be10 - Start address of new space: 142a0 - Current space capacity: 256 - Allocate count: 2 - Allocated memory count: 96 bytes - Collect count: 0 - Allocated words in new space: 12 - Current new space: - 0x0: [size: 5] - 0x1: [data: 0x10670] - 0x2: [data: 0x2] - 0x3: [data: 0x0] - 0x4: [data: 0x0] - 0x5: [data: 0x0] - 0x6: [size: 5] - 0x7: [data: 0x10670] - 0x8: [data: 0x2] - 0x9: [data: 0x0] - 0xa: [data: 0x0] - 0xb: [data: 0x0] - === GC status === - CLOS: 0x14338 - === GC status === - Base stack pointer: 2b23be10 - Start address of new space: 142a0 - Current space capacity: 256 - Allocate count: 4 - Allocated memory count: 192 bytes - Collect count: 0 - Allocated words in new space: 24 - Current new space: - 0x0: [size: 5] - 0x1: [data: 0x10670] - 0x2: [data: 0x2] - 0x3: [data: 0x0] - 0x4: [data: 0x0] - 0x5: [data: 0x0] - 0x6: [size: 5] - 0x7: [data: 0x10670] - 0x8: [data: 0x2] - 0x9: [data: 0x1] - 0xa: [data: 0x5] - 0xb: [data: 0x0] - 0xc: [size: 5] - 0xd: [data: 0x10670] - 0xe: [data: 0x2] - 0xf: [data: 0x0] - 0x10: [data: 0x0] - 0x11: [data: 0x0] - 0x12: [size: 5] - 0x13: [data: 0x10670] - 0x14: [data: 0x2] - 0x15: [data: 0x0] - 0x16: [data: 0x0] - 0x17: [data: 0x0] - === GC status === - CLOS: 0x14368 - === GC status === - Base stack pointer: 2b23be10 - Start address of new space: 142a0 - Current space capacity: 256 - Allocate count: 5 - Allocated memory count: 240 bytes - Collect count: 0 - Allocated words in new space: 30 - Current new space: - 0x0: [size: 5] - 0x1: [data: 0x10670] - 0x2: [data: 0x2] - 0x3: [data: 0x0] - 0x4: [data: 0x0] - 0x5: [data: 0x0] - 0x6: [size: 5] - 0x7: [data: 0x10670] - 0x8: [data: 0x2] - 0x9: [data: 0x1] - 0xa: [data: 0x5] - 0xb: [data: 0x0] - 0xc: [size: 5] - 0xd: [data: 0x10670] - 0xe: [data: 0x2] - 0xf: [data: 0x0] - 0x10: [data: 0x0] - 0x11: [data: 0x0] - 0x12: [size: 5] - 0x13: [data: 0x10670] - 0x14: [data: 0x2] - 0x15: [data: 0x1] - 0x16: [data: 0x1] - 0x17: [data: 0x0] - 0x18: [size: 5] - 0x19: [data: 0x10670] - 0x1a: [data: 0x2] - 0x1b: [data: 0x1] - 0x1c: [data: 0x5] - 0x1d: [data: 0x0] - === GC status === 115 122 17 @@ -1143,9 +832,9 @@ .globl _start _start: mv fp, sp - addi sp, sp, -32 mv a0, sp call init_GC + addi sp, sp, -32 # Partial application add__0 with 1 args # Load args on stack addi sp, sp, -32 @@ -1291,9 +980,9 @@ .globl _start _start: mv fp, sp - addi sp, sp, -8 mv a0, sp call init_GC + addi sp, sp, -8 li t0, 5 la t1, x__0 sd t0, 0(t1) @@ -1359,9 +1048,9 @@ .globl _start _start: mv fp, sp - addi sp, sp, 0 mv a0, sp call init_GC + addi sp, sp, 0 li t0, 1 beq t0, zero, L0 li t0, 1 @@ -1522,9 +1211,9 @@ .globl _start _start: mv fp, sp - addi sp, sp, 0 mv a0, sp call init_GC + addi sp, sp, 0 call flush li a0, 0 li a7, 94 @@ -1535,158 +1224,6 @@ > let _ = print_int (f 5) > EOF $ qemu-riscv64 -L /usr/riscv64-linux-gnu -cpu rv64 ../main.exe | tee -a results.txt && echo "-----" >> results.txt - CLOS: 0x14308 - === GC status === - Base stack pointer: d7ffde10 - Start address of new space: 142a0 - Current space capacity: 256 - Allocate count: 3 - Allocated memory count: 144 bytes - Collect count: 0 - Allocated words in new space: 18 - Current new space: - 0x0: [size: 5] - 0x1: [data: 0x10670] - 0x2: [data: 0x2] - 0x3: [data: 0x0] - 0x4: [data: 0x0] - 0x5: [data: 0x0] - 0x6: [size: 5] - 0x7: [data: 0x1068c] - 0x8: [data: 0x2] - 0x9: [data: 0x0] - 0xa: [data: 0x0] - 0xb: [data: 0x0] - 0xc: [size: 5] - 0xd: [data: 0x1068c] - 0xe: [data: 0x2] - 0xf: [data: 0x0] - 0x10: [data: 0x0] - 0x11: [data: 0x0] - === GC status === - CLOS: 0x14338 - === GC status === - Base stack pointer: d7ffde10 - Start address of new space: 142a0 - Current space capacity: 256 - Allocate count: 4 - Allocated memory count: 192 bytes - Collect count: 0 - Allocated words in new space: 24 - Current new space: - 0x0: [size: 5] - 0x1: [data: 0x10670] - 0x2: [data: 0x2] - 0x3: [data: 0x0] - 0x4: [data: 0x0] - 0x5: [data: 0x0] - 0x6: [size: 5] - 0x7: [data: 0x1068c] - 0x8: [data: 0x2] - 0x9: [data: 0x0] - 0xa: [data: 0x0] - 0xb: [data: 0x0] - 0xc: [size: 5] - 0xd: [data: 0x1068c] - 0xe: [data: 0x2] - 0xf: [data: 0x1] - 0x10: [data: 0x142a8] - 0x11: [data: 0x0] - 0x12: [size: 5] - 0x13: [data: 0x1068c] - 0x14: [data: 0x2] - 0x15: [data: 0x1] - 0x16: [data: 0x142a8] - 0x17: [data: 0x0] - === GC status === - CLOS: 0x14368 - === GC status === - Base stack pointer: d7ffde10 - Start address of new space: 142a0 - Current space capacity: 256 - Allocate count: 5 - Allocated memory count: 240 bytes - Collect count: 0 - Allocated words in new space: 30 - Current new space: - 0x0: [size: 5] - 0x1: [data: 0x10670] - 0x2: [data: 0x2] - 0x3: [data: 0x0] - 0x4: [data: 0x0] - 0x5: [data: 0x0] - 0x6: [size: 5] - 0x7: [data: 0x1068c] - 0x8: [data: 0x2] - 0x9: [data: 0x0] - 0xa: [data: 0x0] - 0xb: [data: 0x0] - 0xc: [size: 5] - 0xd: [data: 0x1068c] - 0xe: [data: 0x2] - 0xf: [data: 0x1] - 0x10: [data: 0x142a8] - 0x11: [data: 0x0] - 0x12: [size: 5] - 0x13: [data: 0x1068c] - 0x14: [data: 0x2] - 0x15: [data: 0x2] - 0x16: [data: 0x142a8] - 0x17: [data: 0x5] - 0x18: [size: 5] - 0x19: [data: 0x10670] - 0x1a: [data: 0x2] - 0x1b: [data: 0x0] - 0x1c: [data: 0x0] - 0x1d: [data: 0x0] - === GC status === - CLOS: 0x14398 - === GC status === - Base stack pointer: d7ffde10 - Start address of new space: 142a0 - Current space capacity: 256 - Allocate count: 6 - Allocated memory count: 288 bytes - Collect count: 0 - Allocated words in new space: 36 - Current new space: - 0x0: [size: 5] - 0x1: [data: 0x10670] - 0x2: [data: 0x2] - 0x3: [data: 0x0] - 0x4: [data: 0x0] - 0x5: [data: 0x0] - 0x6: [size: 5] - 0x7: [data: 0x1068c] - 0x8: [data: 0x2] - 0x9: [data: 0x0] - 0xa: [data: 0x0] - 0xb: [data: 0x0] - 0xc: [size: 5] - 0xd: [data: 0x1068c] - 0xe: [data: 0x2] - 0xf: [data: 0x1] - 0x10: [data: 0x142a8] - 0x11: [data: 0x0] - 0x12: [size: 5] - 0x13: [data: 0x1068c] - 0x14: [data: 0x2] - 0x15: [data: 0x2] - 0x16: [data: 0x142a8] - 0x17: [data: 0x5] - 0x18: [size: 5] - 0x19: [data: 0x10670] - 0x1a: [data: 0x2] - 0x1b: [data: 0x1] - 0x1c: [data: 0x5] - 0x1d: [data: 0x0] - 0x1e: [size: 5] - 0x1f: [data: 0x10670] - 0x20: [data: 0x2] - 0x21: [data: 0x1] - 0x22: [data: 0x5] - 0x23: [data: 0x0] - === GC status === 8 $ cat ../main.anf let f_0 = fun x__1 -> @@ -1727,331 +1264,20 @@ ----- 5 ----- - CLOS: 0x142d0 - === GC status === - Base stack pointer: dd28ce28 - Start address of new space: 142a0 - Current space capacity: 256 - Allocate count: 2 - Allocated memory count: 80 bytes - Collect count: 0 - Allocated words in new space: 10 - Current new space: - 0x0: [size: 4] - 0x1: [data: 0x106a0] - 0x2: [data: 0x1] - 0x3: [data: 0x0] - 0x4: [data: 0x0] - 0x5: [size: 4] - 0x6: [data: 0x106a0] - 0x7: [data: 0x1] - 0x8: [data: 0x0] - 0x9: [data: 0x0] - === GC status === 6 ----- 20 10 ----- - CLOS: 0x142d8 - === GC status === - Base stack pointer: a3c3be10 - Start address of new space: 142a0 - Current space capacity: 256 - Allocate count: 2 - Allocated memory count: 96 bytes - Collect count: 0 - Allocated words in new space: 12 - Current new space: - 0x0: [size: 5] - 0x1: [data: 0x10670] - 0x2: [data: 0x2] - 0x3: [data: 0x0] - 0x4: [data: 0x0] - 0x5: [data: 0x0] - 0x6: [size: 5] - 0x7: [data: 0x10670] - 0x8: [data: 0x2] - 0x9: [data: 0x0] - 0xa: [data: 0x0] - 0xb: [data: 0x0] - === GC status === - CLOS: 0x14308 - === GC status === - Base stack pointer: a3c3be10 - Start address of new space: 142a0 - Current space capacity: 256 - Allocate count: 3 - Allocated memory count: 144 bytes - Collect count: 0 - Allocated words in new space: 18 - Current new space: - 0x0: [size: 5] - 0x1: [data: 0x10670] - 0x2: [data: 0x2] - 0x3: [data: 0x0] - 0x4: [data: 0x0] - 0x5: [data: 0x0] - 0x6: [size: 5] - 0x7: [data: 0x10670] - 0x8: [data: 0x2] - 0x9: [data: 0x1] - 0xa: [data: 0x1] - 0xb: [data: 0x0] - 0xc: [size: 5] - 0xd: [data: 0x10670] - 0xe: [data: 0x2] - 0xf: [data: 0x1] - 0x10: [data: 0x1] - 0x11: [data: 0x0] - === GC status === 122 ----- - CLOS: 0x142d8 - === GC status === - Base stack pointer: 98de0df8 - Start address of new space: 142a0 - Current space capacity: 256 - Allocate count: 2 - Allocated memory count: 96 bytes - Collect count: 0 - Allocated words in new space: 12 - Current new space: - 0x0: [size: 5] - 0x1: [data: 0x10670] - 0x2: [data: 0x2] - 0x3: [data: 0x0] - 0x4: [data: 0x0] - 0x5: [data: 0x0] - 0x6: [size: 5] - 0x7: [data: 0x10670] - 0x8: [data: 0x2] - 0x9: [data: 0x0] - 0xa: [data: 0x0] - 0xb: [data: 0x0] - === GC status === - CLOS: 0x14308 - === GC status === - Base stack pointer: 98de0df8 - Start address of new space: 142a0 - Current space capacity: 256 - Allocate count: 3 - Allocated memory count: 144 bytes - Collect count: 0 - Allocated words in new space: 18 - Current new space: - 0x0: [size: 5] - 0x1: [data: 0x10670] - 0x2: [data: 0x2] - 0x3: [data: 0x0] - 0x4: [data: 0x0] - 0x5: [data: 0x0] - 0x6: [size: 5] - 0x7: [data: 0x10670] - 0x8: [data: 0x2] - 0x9: [data: 0x1] - 0xa: [data: 0x1] - 0xb: [data: 0x0] - 0xc: [size: 5] - 0xd: [data: 0x10670] - 0xe: [data: 0x2] - 0xf: [data: 0x1] - 0x10: [data: 0x1] - 0x11: [data: 0x0] - === GC status === 122 - CLOS: 0x14338 - === GC status === - Base stack pointer: 98de0df8 - Start address of new space: 142a0 - Current space capacity: 256 - Allocate count: 4 - Allocated memory count: 192 bytes - Collect count: 0 - Allocated words in new space: 24 - Current new space: - 0x0: [size: 5] - 0x1: [data: 0x10670] - 0x2: [data: 0x2] - 0x3: [data: 0x0] - 0x4: [data: 0x0] - 0x5: [data: 0x0] - 0x6: [size: 5] - 0x7: [data: 0x10670] - 0x8: [data: 0x2] - 0x9: [data: 0x1] - 0xa: [data: 0x1] - 0xb: [data: 0x0] - 0xc: [size: 5] - 0xd: [data: 0x10670] - 0xe: [data: 0x2] - 0xf: [data: 0x2] - 0x10: [data: 0x1] - 0x11: [data: 0x79] - 0x12: [size: 5] - 0x13: [data: 0x10670] - 0x14: [data: 0x2] - 0x15: [data: 0x1] - 0x16: [data: 0x1] - 0x17: [data: 0x0] - === GC status === 123 ----- 5 ----- - CLOS: 0x142d8 - === GC status === - Base stack pointer: ec7ce20 - Start address of new space: 142a0 - Current space capacity: 256 - Allocate count: 2 - Allocated memory count: 96 bytes - Collect count: 0 - Allocated words in new space: 12 - Current new space: - 0x0: [size: 5] - 0x1: [data: 0x10670] - 0x2: [data: 0x2] - 0x3: [data: 0x0] - 0x4: [data: 0x0] - 0x5: [data: 0x0] - 0x6: [size: 5] - 0x7: [data: 0x10670] - 0x8: [data: 0x2] - 0x9: [data: 0x0] - 0xa: [data: 0x0] - 0xb: [data: 0x0] - === GC status === - CLOS: 0x14308 - === GC status === - Base stack pointer: ec7ce20 - Start address of new space: 142a0 - Current space capacity: 256 - Allocate count: 3 - Allocated memory count: 144 bytes - Collect count: 0 - Allocated words in new space: 18 - Current new space: - 0x0: [size: 5] - 0x1: [data: 0x10670] - 0x2: [data: 0x2] - 0x3: [data: 0x0] - 0x4: [data: 0x0] - 0x5: [data: 0x0] - 0x6: [size: 5] - 0x7: [data: 0x10670] - 0x8: [data: 0x2] - 0x9: [data: 0x1] - 0xa: [data: 0x5] - 0xb: [data: 0x0] - 0xc: [size: 5] - 0xd: [data: 0x10670] - 0xe: [data: 0x2] - 0xf: [data: 0x1] - 0x10: [data: 0x5] - 0x11: [data: 0x0] - === GC status === 122 ----- - CLOS: 0x142d8 - === GC status === - Base stack pointer: 2b23be10 - Start address of new space: 142a0 - Current space capacity: 256 - Allocate count: 2 - Allocated memory count: 96 bytes - Collect count: 0 - Allocated words in new space: 12 - Current new space: - 0x0: [size: 5] - 0x1: [data: 0x10670] - 0x2: [data: 0x2] - 0x3: [data: 0x0] - 0x4: [data: 0x0] - 0x5: [data: 0x0] - 0x6: [size: 5] - 0x7: [data: 0x10670] - 0x8: [data: 0x2] - 0x9: [data: 0x0] - 0xa: [data: 0x0] - 0xb: [data: 0x0] - === GC status === - CLOS: 0x14338 - === GC status === - Base stack pointer: 2b23be10 - Start address of new space: 142a0 - Current space capacity: 256 - Allocate count: 4 - Allocated memory count: 192 bytes - Collect count: 0 - Allocated words in new space: 24 - Current new space: - 0x0: [size: 5] - 0x1: [data: 0x10670] - 0x2: [data: 0x2] - 0x3: [data: 0x0] - 0x4: [data: 0x0] - 0x5: [data: 0x0] - 0x6: [size: 5] - 0x7: [data: 0x10670] - 0x8: [data: 0x2] - 0x9: [data: 0x1] - 0xa: [data: 0x5] - 0xb: [data: 0x0] - 0xc: [size: 5] - 0xd: [data: 0x10670] - 0xe: [data: 0x2] - 0xf: [data: 0x0] - 0x10: [data: 0x0] - 0x11: [data: 0x0] - 0x12: [size: 5] - 0x13: [data: 0x10670] - 0x14: [data: 0x2] - 0x15: [data: 0x0] - 0x16: [data: 0x0] - 0x17: [data: 0x0] - === GC status === - CLOS: 0x14368 - === GC status === - Base stack pointer: 2b23be10 - Start address of new space: 142a0 - Current space capacity: 256 - Allocate count: 5 - Allocated memory count: 240 bytes - Collect count: 0 - Allocated words in new space: 30 - Current new space: - 0x0: [size: 5] - 0x1: [data: 0x10670] - 0x2: [data: 0x2] - 0x3: [data: 0x0] - 0x4: [data: 0x0] - 0x5: [data: 0x0] - 0x6: [size: 5] - 0x7: [data: 0x10670] - 0x8: [data: 0x2] - 0x9: [data: 0x1] - 0xa: [data: 0x5] - 0xb: [data: 0x0] - 0xc: [size: 5] - 0xd: [data: 0x10670] - 0xe: [data: 0x2] - 0xf: [data: 0x0] - 0x10: [data: 0x0] - 0x11: [data: 0x0] - 0x12: [size: 5] - 0x13: [data: 0x10670] - 0x14: [data: 0x2] - 0x15: [data: 0x1] - 0x16: [data: 0x1] - 0x17: [data: 0x0] - 0x18: [size: 5] - 0x19: [data: 0x10670] - 0x1a: [data: 0x2] - 0x1b: [data: 0x1] - 0x1c: [data: 0x5] - 0x1d: [data: 0x0] - === GC status === 115 122 17 @@ -2061,158 +1287,6 @@ ----- 1 ----- - CLOS: 0x14308 - === GC status === - Base stack pointer: d7ffde10 - Start address of new space: 142a0 - Current space capacity: 256 - Allocate count: 3 - Allocated memory count: 144 bytes - Collect count: 0 - Allocated words in new space: 18 - Current new space: - 0x0: [size: 5] - 0x1: [data: 0x10670] - 0x2: [data: 0x2] - 0x3: [data: 0x0] - 0x4: [data: 0x0] - 0x5: [data: 0x0] - 0x6: [size: 5] - 0x7: [data: 0x1068c] - 0x8: [data: 0x2] - 0x9: [data: 0x0] - 0xa: [data: 0x0] - 0xb: [data: 0x0] - 0xc: [size: 5] - 0xd: [data: 0x1068c] - 0xe: [data: 0x2] - 0xf: [data: 0x0] - 0x10: [data: 0x0] - 0x11: [data: 0x0] - === GC status === - CLOS: 0x14338 - === GC status === - Base stack pointer: d7ffde10 - Start address of new space: 142a0 - Current space capacity: 256 - Allocate count: 4 - Allocated memory count: 192 bytes - Collect count: 0 - Allocated words in new space: 24 - Current new space: - 0x0: [size: 5] - 0x1: [data: 0x10670] - 0x2: [data: 0x2] - 0x3: [data: 0x0] - 0x4: [data: 0x0] - 0x5: [data: 0x0] - 0x6: [size: 5] - 0x7: [data: 0x1068c] - 0x8: [data: 0x2] - 0x9: [data: 0x0] - 0xa: [data: 0x0] - 0xb: [data: 0x0] - 0xc: [size: 5] - 0xd: [data: 0x1068c] - 0xe: [data: 0x2] - 0xf: [data: 0x1] - 0x10: [data: 0x142a8] - 0x11: [data: 0x0] - 0x12: [size: 5] - 0x13: [data: 0x1068c] - 0x14: [data: 0x2] - 0x15: [data: 0x1] - 0x16: [data: 0x142a8] - 0x17: [data: 0x0] - === GC status === - CLOS: 0x14368 - === GC status === - Base stack pointer: d7ffde10 - Start address of new space: 142a0 - Current space capacity: 256 - Allocate count: 5 - Allocated memory count: 240 bytes - Collect count: 0 - Allocated words in new space: 30 - Current new space: - 0x0: [size: 5] - 0x1: [data: 0x10670] - 0x2: [data: 0x2] - 0x3: [data: 0x0] - 0x4: [data: 0x0] - 0x5: [data: 0x0] - 0x6: [size: 5] - 0x7: [data: 0x1068c] - 0x8: [data: 0x2] - 0x9: [data: 0x0] - 0xa: [data: 0x0] - 0xb: [data: 0x0] - 0xc: [size: 5] - 0xd: [data: 0x1068c] - 0xe: [data: 0x2] - 0xf: [data: 0x1] - 0x10: [data: 0x142a8] - 0x11: [data: 0x0] - 0x12: [size: 5] - 0x13: [data: 0x1068c] - 0x14: [data: 0x2] - 0x15: [data: 0x2] - 0x16: [data: 0x142a8] - 0x17: [data: 0x5] - 0x18: [size: 5] - 0x19: [data: 0x10670] - 0x1a: [data: 0x2] - 0x1b: [data: 0x0] - 0x1c: [data: 0x0] - 0x1d: [data: 0x0] - === GC status === - CLOS: 0x14398 - === GC status === - Base stack pointer: d7ffde10 - Start address of new space: 142a0 - Current space capacity: 256 - Allocate count: 6 - Allocated memory count: 288 bytes - Collect count: 0 - Allocated words in new space: 36 - Current new space: - 0x0: [size: 5] - 0x1: [data: 0x10670] - 0x2: [data: 0x2] - 0x3: [data: 0x0] - 0x4: [data: 0x0] - 0x5: [data: 0x0] - 0x6: [size: 5] - 0x7: [data: 0x1068c] - 0x8: [data: 0x2] - 0x9: [data: 0x0] - 0xa: [data: 0x0] - 0xb: [data: 0x0] - 0xc: [size: 5] - 0xd: [data: 0x1068c] - 0xe: [data: 0x2] - 0xf: [data: 0x1] - 0x10: [data: 0x142a8] - 0x11: [data: 0x0] - 0x12: [size: 5] - 0x13: [data: 0x1068c] - 0x14: [data: 0x2] - 0x15: [data: 0x2] - 0x16: [data: 0x142a8] - 0x17: [data: 0x5] - 0x18: [size: 5] - 0x19: [data: 0x10670] - 0x1a: [data: 0x2] - 0x1b: [data: 0x1] - 0x1c: [data: 0x5] - 0x1d: [data: 0x0] - 0x1e: [size: 5] - 0x1f: [data: 0x10670] - 0x20: [data: 0x2] - 0x21: [data: 0x1] - 0x22: [data: 0x5] - 0x23: [data: 0x0] - === GC status === 8 ----- 3 diff --git a/PudgeWithMoML/test/dune b/PudgeWithMoML/test/dune index 7a592453..acf0aefe 100644 --- a/PudgeWithMoML/test/dune +++ b/PudgeWithMoML/test/dune @@ -55,3 +55,11 @@ ../bin/compiler.exe ../Makefile (glob_files ../lib/runtime/*))) + + +(cram + (applies_to GC) + (deps + ../bin/compiler.exe + ../Makefile + (glob_files ../lib/runtime/*))) From 2bb43e9a741607815911643a93dd95c693acb847 Mon Sep 17 00:00:00 2001 From: homka122 Date: Mon, 10 Nov 2025 06:23:30 +0300 Subject: [PATCH 10/19] Proj: delete runtime test Signed-off-by: homka122 --- PudgeWithMoML/lib/runtime/runtime_test.c | 378 ----------------------- 1 file changed, 378 deletions(-) delete mode 100644 PudgeWithMoML/lib/runtime/runtime_test.c diff --git a/PudgeWithMoML/lib/runtime/runtime_test.c b/PudgeWithMoML/lib/runtime/runtime_test.c deleted file mode 100644 index d4c36617..00000000 --- a/PudgeWithMoML/lib/runtime/runtime_test.c +++ /dev/null @@ -1,378 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - -extern void *call_closure(void *code, uint64_t argc, void **argv); - -void print_int(size_t n) { printf("%d\n", n); } - -void flush() { fflush(stdout); } - -// size in words -#define GC_SPACE_INITIAL_SIZE (160) - -// HEAP structure -// word: value -// -// 0: [N_0 = size in words] -// 1: [data] -// ... [...data] -// N_0: [data] -// N_0 + 1: [N_2 = size in words] -// N_0 + 2: [data] -// ... .... [..data] -// N_0 + (N_2 - 1): [data] - -typedef struct { - void *base_sp; - size_t space_capacity; // dynamic size in words - void **new_space; - size_t alloc_offset; // first free word offset in new space - void **old_space; - size_t alloc_count; - size_t collect_count; -} GC_state; - -static GC_state gc; - -// mocked stack and regs -void **my_stack; -void **regs; -void *current_sp; - -typedef struct { - void *code; - size_t argc; - size_t argc_recived; - void *args[]; -} closure; - -#define ZERO8 0, 0, 0, 0, 0, 0, 0, 0 -#define INT8 int, int, int, int, int, int, int, int - -// Print stats about Garbage Collector work -void print_gc_status() { - printf("=== GC status ===\n"); - printf("Base stack pointer: %x\n", gc.base_sp); - printf("Start address of new space: %x\n", gc.new_space); - printf("Current space capacity: %ld\n", gc.space_capacity); - printf("Allocated words in new space: %ld\n", gc.alloc_offset); - - printf("Current new space:\n"); - size_t offset = 0; - while (1) { - size_t size = (size_t)gc.new_space[offset]; - printf("\t0x%x: [size: %ld]\n", offset, size); - offset++; - for (size_t i = 0; i < size; i++) { - printf("\t0x%x: ", offset); - printf("[data: 0x%x]\n", gc.new_space[offset]); - offset++; - } - - if (offset >= gc.alloc_offset) { - break; - } - } - - printf("=== GC status ===\n"); - - return; -} - -void print_stack() { - printf("=== STACK status ===\n"); - size_t stack_size = (gc.base_sp - current_sp) / 8; - printf("STACK SIZE: %ld\n", stack_size); - - for (size_t i = 0; i < stack_size; i++) { - uint64_t *byte = (uint64_t *)gc.base_sp - i; - printf("\t0x%x: 0x%x\n", byte, *byte); - } - - printf("=== STACK status ===\n"); - - return; -} - -// Alloc space for GC, init initial state -void init_GC(void *base_sp) { - // I have problems with modify global variables in direct way - gc.base_sp = base_sp; - gc.space_capacity = GC_SPACE_INITIAL_SIZE; - gc.new_space = malloc(sizeof(void *) * GC_SPACE_INITIAL_SIZE); - gc.alloc_offset = 0; - gc.old_space = malloc(sizeof(void *) * GC_SPACE_INITIAL_SIZE); - - return; -} - -static void **collect_riscv_state() { - void **_regs = malloc(sizeof(void *) * 26); - - regs = _regs; - - return _regs; -} - -static void set_riscv_reg(int idx, void *val) { regs[idx] = val; } - -// When we exec gc_collect we have on a heap objects: -// [size 3] [data 0] [data 1] [data 2] [size 1] [data 0] [size 2] ... -// We iterate through heap and try to find poiters to "data 0" on stack\regs -// If we find it in first time: -// 1) move size bytes to the old_space -// 2) save new pointer to old_space -// 3) iterate through stack\regs and replace all pointer to the new -// pointer -void _gc_collect(void *current_sp) { - if (gc.alloc_offset == 0) { - return; - } - - size_t stack_size = (gc.base_sp - current_sp) / 8; - size_t cur_offset = 0; - size_t old_space_offset = 0; - while (cur_offset < gc.alloc_offset) { - void *new_pointer = NULL; - size_t cur_size = (size_t)gc.new_space[cur_offset]; - void *cur_pointer = gc.new_space + cur_offset + 1; - - if (cur_size == 0) { - fprintf( - stderr, - "You have object on heap with zero size\nBug in malloc function!\n"); - print_gc_status(); - exit(122); - } - - LOG("Try to find stack cell with 0x%x value on 0x%ld offset\n", cur_pointer, - cur_offset + 1); - - LOGF(print_stack()); - // try to find in regs and stack at least one pointer - { - bool found = false; - - // regs - for (size_t i = 0; i < 26; i++) { - if (regs[i] == cur_pointer) { - LOG("FOUND AT REG: %ld\n", i); - found = true; - break; - } - } - - // stack - for (size_t i = 0; i < stack_size; i++) { - void **byte = (void **)gc.base_sp - i; - if (*byte == cur_pointer) { - LOG("FOUND AT STACK: %ld. CUR_OFFSET: %x, CUR_POINTER: %x, byte: " - "%x, *byte: %x\n", - i, cur_offset, cur_pointer, byte, *byte); - found = true; - break; - } - } - - if (!found) { - cur_offset += cur_size + 1; - continue; - } - - // copy to old space - new_pointer = gc.old_space + old_space_offset; - gc.old_space[old_space_offset++] = (void *)cur_size; - for (size_t j = 0; j < cur_size; j++) { - gc.old_space[old_space_offset++] = gc.new_space[cur_offset + 1 + j]; - } - LOG("NEW POINTER: 0x%x\n", new_pointer); - } - - LOG("RUN CHANGING\n"); - // change all occurences - { - // regs - for (size_t i = 0; i < 26; i++) { - if (regs[i] == cur_pointer) { - set_riscv_reg(i, new_pointer); - } - } - - // stack - for (size_t i = 0; i < stack_size; i++) { - void **byte = (void **)gc.base_sp - i; - if (*byte == cur_pointer) { - LOG("Change stack cell 0x%x. 0x%x -> 0x%x\n", byte, *byte, - new_pointer); - *byte = new_pointer; - } - } - } - LOGF(print_stack()); - - cur_offset += cur_size + 1; - } - - void *temp = gc.new_space; - gc.new_space = gc.old_space; - gc.old_space = temp; - gc.alloc_offset = old_space_offset; - - gc.collect_count++; -} - -// WARNING: if you read stack pointer in _gc_collect function than when you go -// through stack you can change local variables of _gc_collect fuction -// So we write wrapper only for reading stack pointer **before** _gc_collect -// function It took 4 hours for debug this chaos 🐣🐣🐤🐤🐔🐔🦆🦆🐹🐹🐹🐹 -void gc_collect() { - void *current_sp = NULL; - asm volatile("mv %0, sp" : "=r"(current_sp)); - _gc_collect(current_sp); -} - -// alloc size bytes in gc.memory -void *my_malloc(size_t size) { - printf("MY MALLOC: %ld\n", size); - if (size == 0) { - return NULL; - } - - // 8 byte rounding - if (size % 8 != 0) { - size = size + (8 - (size % 8)); - } - - size_t words = size / 8; - - // no free space left after alloc size bytes + header - if (gc.alloc_offset + (words + 1) >= gc.space_capacity) { - printf("no free space\n"); - gc_collect(); - - // after collecting we still don't have space - if (gc.alloc_offset + (words + 1) >= gc.space_capacity) { - fprintf(stderr, "panic! overflow memory limits\n"); - fflush(stderr); - exit(122); - size_t mult = 1; - - while (gc.alloc_offset + (words + 1) >= (gc.space_capacity * mult)) { - mult *= 2; - } - - gc.space_capacity *= mult; - gc.new_space = realloc(gc.new_space, sizeof(void *) * gc.space_capacity); - gc.old_space = realloc(gc.old_space, sizeof(void *) * gc.space_capacity); - } - } - - printf("words: %ld\n", words); - gc.new_space[gc.alloc_offset++] = (void *)words; - void **result = gc.new_space + gc.alloc_offset; - - gc.alloc_offset += words; - return result; -} - -void *alloc_closure(INT8, void *f, uint8_t argc) { - LOG("alloc_closure(0x%x, %d)\n", f, argc); - closure *clos = my_malloc(sizeof(closure) + sizeof(void *) * argc); - - clos->code = f; - clos->argc = argc; - clos->argc_recived = 0; - memset(clos->args, 0, sizeof(void *) * argc); - - return clos; -} - -void *copy_closure(closure *old_clos) { - closure *clos = old_clos; - closure *new = alloc_closure(ZERO8, clos->code, clos->argc); - - printf("old clos: 0x%x, new clos: 0x%x\n", clos, new); - printf("clos.code: 0x%x, clos argc: 0x%d\n", clos->code, clos->argc); - - for (size_t i = 0; i < clos->argc_recived; i++) { - new->args[new->argc_recived++] = clos->args[i]; - } - - return new; -} - -#define WORD_SIZE (8) - -int main(int argc, char **argv) { - my_stack = (void **)malloc(sizeof(void *) * 32); - init_GC(my_stack + 32); - current_sp = my_stack + 32 - 8; - regs = malloc(sizeof(void *) * 26); - - if (0) { - print_gc_status(); - alloc_closure(ZERO8, (void *)0xFF, 2); - alloc_closure(ZERO8, (void *)0xFFF, 3); - alloc_closure(ZERO8, (void *)0xFFFF, 4); - print_gc_status(); - gc_collect(); - // must be empty - print_gc_status(); - } - - if (0) { - print_gc_status(); - void *clos = alloc_closure(ZERO8, (void *)0xFF, 2); - alloc_closure(ZERO8, (void *)0xFFF, 3); - alloc_closure(ZERO8, (void *)0xFFFF, 4); - print_gc_status(); - regs[12] = clos; - gc_collect(); - // must has first closure - print_gc_status(); - } - - if (0) { - print_gc_status(); - alloc_closure(ZERO8, (void *)0xFF, 2); - alloc_closure(ZERO8, (void *)0xFFF, 3); - void *clos = alloc_closure(ZERO8, (void *)0xFFFF, 4); - print_gc_status(); - regs[12] = clos; - gc_collect(); - // must has third closure - print_gc_status(); - } - - if (0) { - print_gc_status(); - void *clos1 = alloc_closure(ZERO8, (void *)0xFF, 2); - alloc_closure(ZERO8, (void *)0xFFF, 3); - void *clos2 = alloc_closure(ZERO8, (void *)0xFFFF, 4); - print_gc_status(); - regs[12] = clos1; - my_stack[32 - 2] = clos2; - print_stack(); - gc_collect(); - // must has first and third closure - print_gc_status(); - } - - if (1) { - print_gc_status(); - void *clos1 = alloc_closure(ZERO8, (void *)0xFF, 2); - print_gc_status(); - regs[12] = clos1; - print_stack(); - gc_collect(); - print_gc_status(); - } - - printf("Done\n"); - return 0; -} \ No newline at end of file From c0bf924b7e45fd2e793c325dff28d1d57fe22ffc Mon Sep 17 00:00:00 2001 From: homka122 Date: Mon, 10 Nov 2025 06:37:39 +0300 Subject: [PATCH 11/19] Feat: add get_heap_start and get_heap_end function Signed-off-by: homka122 --- PudgeWithMoML/lib/frontend/inferencer.ml | 4 ++++ PudgeWithMoML/lib/riscv/codegen.ml | 4 ++++ PudgeWithMoML/lib/runtime/runtime.c | 23 +++++++++++++++++++++++ PudgeWithMoML/test/GC.t | 9 +++++++++ 4 files changed, 40 insertions(+) diff --git a/PudgeWithMoML/lib/frontend/inferencer.ml b/PudgeWithMoML/lib/frontend/inferencer.ml index 9c618f02..04fadb45 100644 --- a/PudgeWithMoML/lib/frontend/inferencer.ml +++ b/PudgeWithMoML/lib/frontend/inferencer.ml @@ -332,6 +332,8 @@ end = struct let print_gs_status_s = Scheme (VarSet.empty, arrow_t [ unit_typ ] unit_typ) in let gc_collect_s = Scheme (VarSet.empty, arrow_t [ unit_typ ] unit_typ) in let clear_regs_s = Scheme (VarSet.empty, arrow_t [ unit_typ ] unit_typ) in + let get_heap_start_s = Scheme (VarSet.empty, arrow_t [ unit_typ ] int_typ) in + let get_heap_fin_s = Scheme (VarSet.empty, arrow_t [ unit_typ ] int_typ) in ( Map.of_alist_exn (module String) [ "=", eq_s @@ -353,6 +355,8 @@ end = struct ; "print_gc_status", print_gs_status_s ; "gc_collect", gc_collect_s ; "clear_regs", clear_regs_s + ; "get_heap_start", get_heap_start_s + ; "get_heap_fin", get_heap_fin_s ] , fresh tv ) ;; diff --git a/PudgeWithMoML/lib/riscv/codegen.ml b/PudgeWithMoML/lib/riscv/codegen.ml index 049bf439..c0604f57 100644 --- a/PudgeWithMoML/lib/riscv/codegen.ml +++ b/PudgeWithMoML/lib/riscv/codegen.ml @@ -308,6 +308,10 @@ let rec gen_cexpr (var_arity : string -> int) dst = function [ call "print_gc_status" ] |> return | CApp (ImmVar "gc_collect", ImmConst Unit_lt, []) -> [ call "gc_collect" ] |> return | CApp (ImmVar "clear_regs", ImmConst Unit_lt, []) -> [ call "clear_regs" ] |> return + | CApp (ImmVar "get_heap_start", ImmConst Unit_lt, []) -> + ([ call "get_heap_start" ] @ if dst = A 0 then [] else [ mv dst (A 0) ]) |> return + | CApp (ImmVar "get_heap_fin", ImmConst Unit_lt, []) -> + ([ call "get_heap_fin" ] @ if dst = A 0 then [] else [ mv dst (A 0) ]) |> return | CApp (ImmVar f, arg, args) (* it is full application *) when let arity = var_arity f in diff --git a/PudgeWithMoML/lib/runtime/runtime.c b/PudgeWithMoML/lib/runtime/runtime.c index 725acaef..af3a6205 100644 --- a/PudgeWithMoML/lib/runtime/runtime.c +++ b/PudgeWithMoML/lib/runtime/runtime.c @@ -458,6 +458,29 @@ void *my_malloc(size_t size) { return result; } +// get heap start of current new_space +void **get_heap_start() { + if (!STABLE_CI) { + return gc.new_space; + } + + return gc.new_space == first_new_space + ? (void **)0x1000 + : ((void **)0x1000) + GC_SPACE_INITIAL_SIZE; +} + +// get heap end of current new_space +void **get_heap_fin() { + if (!STABLE_CI) { + return gc.new_space + gc.space_capacity; + } + + return (gc.new_space == first_new_space + ? (void **)0x1000 + : ((void **)0x1000) + GC_SPACE_INITIAL_SIZE) + + gc.space_capacity; +} + void *alloc_closure(INT8, void *f, uint8_t argc) { LOG("alloc_closure(0x%x, %d)\n", f, argc); closure *clos = my_malloc(sizeof(closure) + sizeof(void *) * argc); diff --git a/PudgeWithMoML/test/GC.t b/PudgeWithMoML/test/GC.t index 99dda222..668000bb 100644 --- a/PudgeWithMoML/test/GC.t +++ b/PudgeWithMoML/test/GC.t @@ -765,3 +765,12 @@ $ qemu-riscv64 -L /usr/riscv64-linux-gnu -cpu rv64 ../main.exe panic! overflow memory limits [122] + +( get current capacity of heap ) + $ make compile opts=-gen_mid --no-print-directory -C .. << 'EOF' + > let start = get_heap_start () + > let end = get_heap_fin () + > let main = print_int (start) + > EOF + $ qemu-riscv64 -L /usr/riscv64-linux-gnu -cpu rv64 ../main.exe + 4096 From cffd6a9c3a8201e317b8e8ab6ab3ef56b4e6a40c Mon Sep 17 00:00:00 2001 From: homka122 Date: Mon, 10 Nov 2025 06:59:46 +0300 Subject: [PATCH 12/19] Refactor: add X macro for regs https://en.wikipedia.org/wiki/X_macro Signed-off-by: homka122 --- PudgeWithMoML/lib/runtime/runtime.c | 184 ++++++++-------------------- 1 file changed, 51 insertions(+), 133 deletions(-) diff --git a/PudgeWithMoML/lib/runtime/runtime.c b/PudgeWithMoML/lib/runtime/runtime.c index af3a6205..59cd538d 100644 --- a/PudgeWithMoML/lib/runtime/runtime.c +++ b/PudgeWithMoML/lib/runtime/runtime.c @@ -24,6 +24,34 @@ void print_int(size_t n) { printf("%d\n", n); } void flush() { fflush(stdout); } +#define RISCV_REG_LIST \ + X(0, t0, 0) \ + X(1, t1, 8) \ + X(2, t2, 16) \ + X(3, t3, 24) \ + X(4, t4, 32) \ + X(5, t5, 40) \ + X(6, t6, 48) \ + X(7, a0, 56) \ + X(8, a1, 64) \ + X(9, a2, 72) \ + X(10, a3, 80) \ + X(11, a4, 88) \ + X(12, a5, 96) \ + X(13, a6, 104) \ + X(14, a7, 112) \ + X(15, s1, 120) \ + X(16, s2, 128) \ + X(17, s3, 136) \ + X(18, s4, 144) \ + X(19, s5, 152) \ + X(20, s6, 160) \ + X(21, s7, 168) \ + X(22, s8, 176) \ + X(23, s9, 184) \ + X(24, s10, 192) \ + X(25, s11, 200) + // size in words #define GC_SPACE_INITIAL_SIZE (8192) #define WORD_SIZE (8) @@ -132,32 +160,13 @@ void init_GC(void *base_sp) { void clear_regs() { // t0-t6 (7), a0-a7 (8), s1-s11 (11) - asm volatile("li t0, 0\n\t" - "li t1, 0\n\t" - "li t2, 0\n\t" - "li t3, 0\n\t" - "li t4, 0\n\t" - "li t5, 0\n\t" - "li t6, 0\n\t" - "li a0, 0\n\t" - "li a1, 0\n\t" - "li a2, 0\n\t" - "li a3, 0\n\t" - "li a4, 0\n\t" - "li a5, 0\n\t" - "li a6, 0\n\t" - "li a7, 0\n\t" - "li s1, 0\n\t" - "li s2, 0\n\t" - "li s3, 0\n\t" - "li s4, 0\n\t" - "li s5, 0\n\t" - "li s6, 0\n\t" - "li s7, 0\n\t" - "li s8, 0\n\t" - "li s9, 0\n\t" - "li s10, 0\n\t" - "li s11, 0\n\t"); +// li t0, 0\n\t +// li t1, 0\n\t +// li t2, 0\n\t +// ... +#define X(i, reg, offset) "li " #reg ", 0\n\t" + asm volatile(RISCV_REG_LIST); +#undef X return; } @@ -171,119 +180,28 @@ static void **collect_riscv_state() { // t0-t6 (7), a0-a7 (8), s1-s11 (11) size_t *regs = malloc(sizeof(size_t) * 26); - asm volatile("sd t0, 0(%0)\n\t" - "sd t1, 8(%0)\n\t" - "sd t2, 16(%0)\n\t" - "sd t3, 24(%0)\n\t" - "sd t4, 32(%0)\n\t" - "sd t5, 40(%0)\n\t" - "sd t6, 48(%0)\n\t" - "sd a0, 56(%0)\n\t" - "sd a1, 64(%0)\n\t" - "sd a2, 72(%0)\n\t" - "sd a3, 80(%0)\n\t" - "sd a4, 88(%0)\n\t" - "sd a5, 96(%0)\n\t" - "sd a6, 104(%0)\n\t" - "sd a7, 112(%0)\n\t" - "sd s1, 120(%0)\n\t" - "sd s2, 128(%0)\n\t" - "sd s3, 136(%0)\n\t" - "sd s4, 144(%0)\n\t" - "sd s5, 152(%0)\n\t" - "sd s6, 160(%0)\n\t" - "sd s7, 168(%0)\n\t" - "sd s8, 176(%0)\n\t" - "sd s9, 184(%0)\n\t" - "sd s10, 192(%0)\n\t" - "sd s11, 200(%0)\n\t" - : - : "r"(regs) - : "memory"); + // sd t0, 0(%0)\n\t + // sd t1, 8(%0)\n\t + // sd t2, 16(%0)\n\t + // ... +#define X(i, reg, offset) "sd " #reg ", " #offset "(%0)\n\t" + asm volatile(RISCV_REG_LIST : : "r"(regs) : "memory"); +#undef X return (void **)regs; } +// case 0: mv t0, %0 :: "r"(val) +// case 1: mv t1, %0 :: "r"(val) +// case 2: mv t2, %0 :: "r"(val) +// ... static void set_riscv_reg(int idx, void *val) { switch (idx) { - case 0: - asm volatile("mv t0, %0" ::"r"(val)); - break; - case 1: - asm volatile("mv t1, %0" ::"r"(val)); - break; - case 2: - asm volatile("mv t2, %0" ::"r"(val)); - break; - case 3: - asm volatile("mv t3, %0" ::"r"(val)); - break; - case 4: - asm volatile("mv t4, %0" ::"r"(val)); - break; - case 5: - asm volatile("mv t5, %0" ::"r"(val)); - break; - case 6: - asm volatile("mv t6, %0" ::"r"(val)); - break; - case 7: - asm volatile("mv a0, %0" ::"r"(val)); - break; - case 8: - asm volatile("mv a1, %0" ::"r"(val)); - break; - case 9: - asm volatile("mv a2, %0" ::"r"(val)); - break; - case 10: - asm volatile("mv a3, %0" ::"r"(val)); - break; - case 11: - asm volatile("mv a4, %0" ::"r"(val)); - break; - case 12: - asm volatile("mv a5, %0" ::"r"(val)); - break; - case 13: - asm volatile("mv a6, %0" ::"r"(val)); - break; - case 14: - asm volatile("mv a7, %0" ::"r"(val)); - break; - case 15: - asm volatile("mv s1, %0" ::"r"(val)); - break; - case 16: - asm volatile("mv s2, %0" ::"r"(val)); - break; - case 17: - asm volatile("mv s3, %0" ::"r"(val)); - break; - case 18: - asm volatile("mv s4, %0" ::"r"(val)); - break; - case 19: - asm volatile("mv s5, %0" ::"r"(val)); - break; - case 20: - asm volatile("mv s6, %0" ::"r"(val)); - break; - case 21: - asm volatile("mv s7, %0" ::"r"(val)); - break; - case 22: - asm volatile("mv s8, %0" ::"r"(val)); - break; - case 23: - asm volatile("mv s9, %0" ::"r"(val)); - break; - case 24: - asm volatile("mv s10, %0" ::"r"(val)); - break; - case 25: - asm volatile("mv s11, %0" ::"r"(val)); - break; +#define X(i, reg, offset) \ + case i: \ + asm volatile("mv " #reg ", %0" ::"r"(val)); + RISCV_REG_LIST +#undef X default: break; } From 350faf4c8a78587273b23c2bbebddd87ef9aefdb Mon Sep 17 00:00:00 2001 From: homka122 Date: Mon, 10 Nov 2025 16:44:36 +0300 Subject: [PATCH 13/19] Refactor: add clang-format to runtime code Signed-off-by: homka122 --- PudgeWithMoML/lib/runtime/.clang-format | 288 ++++++++++++++++++ PudgeWithMoML/lib/runtime/runtime.c | 131 ++++---- PudgeWithMoML/test/GC.t | 377 +++++++----------------- 3 files changed, 463 insertions(+), 333 deletions(-) create mode 100644 PudgeWithMoML/lib/runtime/.clang-format diff --git a/PudgeWithMoML/lib/runtime/.clang-format b/PudgeWithMoML/lib/runtime/.clang-format new file mode 100644 index 00000000..c5f1ae95 --- /dev/null +++ b/PudgeWithMoML/lib/runtime/.clang-format @@ -0,0 +1,288 @@ +--- +Language: Cpp +AccessModifierOffset: -2 +AlignAfterOpenBracket: Align +AlignArrayOfStructures: None +AlignConsecutiveAssignments: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + AlignFunctionDeclarations: false + AlignFunctionPointers: false + PadOperators: true +AlignConsecutiveBitFields: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + AlignFunctionDeclarations: false + AlignFunctionPointers: false + PadOperators: false +AlignConsecutiveDeclarations: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + AlignFunctionDeclarations: true + AlignFunctionPointers: false + PadOperators: false +AlignConsecutiveMacros: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + AlignFunctionDeclarations: false + AlignFunctionPointers: false + PadOperators: false +AlignConsecutiveShortCaseStatements: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCaseArrows: false + AlignCaseColons: false +AlignConsecutiveTableGenBreakingDAGArgColons: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + AlignFunctionDeclarations: false + AlignFunctionPointers: false + PadOperators: false +AlignConsecutiveTableGenCondOperatorColons: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + AlignFunctionDeclarations: false + AlignFunctionPointers: false + PadOperators: false +AlignConsecutiveTableGenDefinitionColons: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + AlignFunctionDeclarations: false + AlignFunctionPointers: false + PadOperators: false +AlignEscapedNewlines: Right +AlignOperands: Align +AlignTrailingComments: + Kind: Always + OverEmptyLines: 0 +AllowAllArgumentsOnNextLine: true +AllowAllParametersOfDeclarationOnNextLine: true +AllowBreakBeforeNoexceptSpecifier: Never +AllowShortBlocksOnASingleLine: Never +AllowShortCaseExpressionOnASingleLine: true +AllowShortCaseLabelsOnASingleLine: false +AllowShortCompoundRequirementOnASingleLine: true +AllowShortEnumsOnASingleLine: true +AllowShortFunctionsOnASingleLine: All +AllowShortIfStatementsOnASingleLine: Never +AllowShortLambdasOnASingleLine: All +AllowShortLoopsOnASingleLine: false +AllowShortNamespacesOnASingleLine: false +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakBeforeMultilineStrings: false +AttributeMacros: + - __capability +BinPackArguments: true +BinPackParameters: BinPack +BitFieldColonSpacing: Both +BraceWrapping: + AfterCaseLabel: false + AfterClass: false + AfterControlStatement: Never + AfterEnum: false + AfterExternBlock: false + AfterFunction: false + AfterNamespace: false + AfterObjCDeclaration: false + AfterStruct: false + AfterUnion: false + BeforeCatch: false + BeforeElse: false + BeforeLambdaBody: false + BeforeWhile: false + IndentBraces: false + SplitEmptyFunction: true + SplitEmptyRecord: true + SplitEmptyNamespace: true +BreakAdjacentStringLiterals: true +BreakAfterAttributes: Leave +BreakAfterJavaFieldAnnotations: false +BreakAfterReturnType: None +BreakArrays: true +BreakBeforeBinaryOperators: None +BreakBeforeConceptDeclarations: Always +BreakBeforeBraces: Attach +BreakBeforeInlineASMColon: OnlyMultiline +BreakBeforeTernaryOperators: true +BreakBinaryOperations: Never +BreakConstructorInitializers: BeforeColon +BreakFunctionDefinitionParameters: false +BreakInheritanceList: BeforeColon +BreakStringLiterals: true +BreakTemplateDeclarations: MultiLine +ColumnLimit: 120 +CommentPragmas: '^ IWYU pragma:' +CompactNamespaces: false +ConstructorInitializerIndentWidth: 4 +ContinuationIndentWidth: 4 +Cpp11BracedListStyle: true +DerivePointerAlignment: false +DisableFormat: false +EmptyLineAfterAccessModifier: Never +EmptyLineBeforeAccessModifier: LogicalBlock +ExperimentalAutoDetectBinPacking: false +FixNamespaceComments: true +ForEachMacros: + - foreach + - Q_FOREACH + - BOOST_FOREACH +IfMacros: + - KJ_IF_MAYBE +IncludeBlocks: Preserve +IncludeCategories: + - Regex: '^"(llvm|llvm-c|clang|clang-c)/' + Priority: 2 + SortPriority: 0 + CaseSensitive: false + - Regex: '^(<|"(gtest|gmock|isl|json)/)' + Priority: 3 + SortPriority: 0 + CaseSensitive: false + - Regex: '.*' + Priority: 1 + SortPriority: 0 + CaseSensitive: false +IncludeIsMainRegex: '(Test)?$' +IncludeIsMainSourceRegex: '' +IndentAccessModifiers: false +IndentCaseBlocks: false +IndentCaseLabels: false +IndentExportBlock: true +IndentExternBlock: AfterExternBlock +IndentGotoLabels: true +IndentPPDirectives: None +IndentRequiresClause: true +IndentWidth: 2 +IndentWrappedFunctionNames: false +InsertBraces: false +InsertNewlineAtEOF: false +InsertTrailingCommas: None +IntegerLiteralSeparator: + Binary: 0 + BinaryMinDigits: 0 + Decimal: 0 + DecimalMinDigits: 0 + Hex: 0 + HexMinDigits: 0 +JavaScriptQuotes: Leave +JavaScriptWrapImports: true +KeepEmptyLines: + AtEndOfFile: false + AtStartOfBlock: true + AtStartOfFile: true +KeepFormFeed: false +LambdaBodyIndentation: Signature +LineEnding: DeriveLF +MacroBlockBegin: '' +MacroBlockEnd: '' +MainIncludeChar: Quote +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: None +ObjCBinPackProtocolList: Auto +ObjCBlockIndentWidth: 2 +ObjCBreakBeforeNestedBlockParam: true +ObjCSpaceAfterProperty: false +ObjCSpaceBeforeProtocolList: true +PackConstructorInitializers: BinPack +PenaltyBreakAssignment: 2 +PenaltyBreakBeforeFirstCallParameter: 19 +PenaltyBreakBeforeMemberAccess: 150 +PenaltyBreakComment: 300 +PenaltyBreakFirstLessLess: 120 +PenaltyBreakOpenParenthesis: 0 +PenaltyBreakScopeResolution: 500 +PenaltyBreakString: 1000 +PenaltyBreakTemplateDeclaration: 10 +PenaltyExcessCharacter: 1000000 +PenaltyIndentedWhitespace: 0 +PenaltyReturnTypeOnItsOwnLine: 60 +PointerAlignment: Right +PPIndentWidth: -1 +QualifierAlignment: Leave +ReferenceAlignment: Pointer +ReflowComments: Always +RemoveBracesLLVM: false +RemoveEmptyLinesInUnwrappedLines: false +RemoveParentheses: Leave +RemoveSemicolon: false +RequiresClausePosition: OwnLine +RequiresExpressionIndentation: OuterScope +SeparateDefinitionBlocks: Leave +ShortNamespaceLines: 1 +SkipMacroDefinitionBody: false +SortIncludes: CaseSensitive +SortJavaStaticImport: Before +SortUsingDeclarations: LexicographicNumeric +SpaceAfterCStyleCast: false +SpaceAfterLogicalNot: false +SpaceAfterTemplateKeyword: true +SpaceAroundPointerQualifiers: Default +SpaceBeforeAssignmentOperators: true +SpaceBeforeCaseColon: false +SpaceBeforeCpp11BracedList: false +SpaceBeforeCtorInitializerColon: true +SpaceBeforeInheritanceColon: true +SpaceBeforeJsonColon: false +SpaceBeforeParens: ControlStatements +SpaceBeforeParensOptions: + AfterControlStatements: true + AfterForeachMacros: true + AfterFunctionDefinitionName: false + AfterFunctionDeclarationName: false + AfterIfMacros: true + AfterOverloadedOperator: false + AfterPlacementOperator: true + AfterRequiresInClause: false + AfterRequiresInExpression: false + BeforeNonEmptyParentheses: false +SpaceBeforeRangeBasedForLoopColon: true +SpaceBeforeSquareBrackets: false +SpaceInEmptyBlock: false +SpacesBeforeTrailingComments: 1 +SpacesInAngles: Never +SpacesInContainerLiterals: true +SpacesInLineCommentPrefix: + Minimum: 1 + Maximum: -1 +SpacesInParens: Never +SpacesInParensOptions: + ExceptDoubleParentheses: false + InCStyleCasts: false + InConditionalStatements: false + InEmptyParentheses: false + Other: false +SpacesInSquareBrackets: false +Standard: Latest +StatementAttributeLikeMacros: + - Q_EMIT +StatementMacros: + - Q_UNUSED + - QT_REQUIRE_VERSION +TableGenBreakInsideDAGArg: DontBreak +TabWidth: 8 +UseTab: Never +VerilogBreakBetweenInstancePorts: true +WhitespaceSensitiveMacros: + - BOOST_PP_STRINGIZE + - CF_SWIFT_NAME + - NS_SWIFT_NAME + - PP_STRINGIZE + - STRINGIZE +WrapNamespaceBodyWithEmptyLines: Leave +... + diff --git a/PudgeWithMoML/lib/runtime/runtime.c b/PudgeWithMoML/lib/runtime/runtime.c index 59cd538d..629d0506 100644 --- a/PudgeWithMoML/lib/runtime/runtime.c +++ b/PudgeWithMoML/lib/runtime/runtime.c @@ -6,8 +6,12 @@ #include #include -#if false -#define LOG(fmt, ...) printf(fmt, ##__VA_ARGS__) +#if true +#define LOG(fmt, ...) \ + { \ + printf(fmt, ##__VA_ARGS__); \ + fflush(stdout); \ + } #define LOGF(fun) fun #else #define LOG(fmt, ...) ((void)0) @@ -24,32 +28,32 @@ void print_int(size_t n) { printf("%d\n", n); } void flush() { fflush(stdout); } -#define RISCV_REG_LIST \ - X(0, t0, 0) \ - X(1, t1, 8) \ - X(2, t2, 16) \ - X(3, t3, 24) \ - X(4, t4, 32) \ - X(5, t5, 40) \ - X(6, t6, 48) \ - X(7, a0, 56) \ - X(8, a1, 64) \ - X(9, a2, 72) \ - X(10, a3, 80) \ - X(11, a4, 88) \ - X(12, a5, 96) \ - X(13, a6, 104) \ - X(14, a7, 112) \ - X(15, s1, 120) \ - X(16, s2, 128) \ - X(17, s3, 136) \ - X(18, s4, 144) \ - X(19, s5, 152) \ - X(20, s6, 160) \ - X(21, s7, 168) \ - X(22, s8, 176) \ - X(23, s9, 184) \ - X(24, s10, 192) \ +#define RISCV_REG_LIST \ + X(0, t0, 0) \ + X(1, t1, 8) \ + X(2, t2, 16) \ + X(3, t3, 24) \ + X(4, t4, 32) \ + X(5, t5, 40) \ + X(6, t6, 48) \ + X(7, a0, 56) \ + X(8, a1, 64) \ + X(9, a2, 72) \ + X(10, a3, 80) \ + X(11, a4, 88) \ + X(12, a5, 96) \ + X(13, a6, 104) \ + X(14, a7, 112) \ + X(15, s1, 120) \ + X(16, s2, 128) \ + X(17, s3, 136) \ + X(18, s4, 144) \ + X(19, s5, 152) \ + X(20, s6, 160) \ + X(21, s7, 168) \ + X(22, s8, 176) \ + X(23, s9, 184) \ + X(24, s10, 192) \ X(25, s11, 200) // size in words @@ -99,15 +103,12 @@ void print_gc_status() { printf("=== GC status ===\n"); // printf("Base stack pointer: %x\n", STABLE_CI ? (void *)0x122 : gc.base_sp); printf("Start address of new space: %x\n", - STABLE_CI ? (gc.new_space == first_new_space - ? (void *)0x1000 - : (void *)(0x1000 + GC_SPACE_INITIAL_SIZE * 8)) + STABLE_CI ? (gc.new_space == first_new_space ? (void *)0x1000 : (void *)(0x1000 + GC_SPACE_INITIAL_SIZE * 8)) : gc.new_space); printf("Allocate count: %ld times\n", gc.alloc_count); printf("Collect count: %ld times\n", gc.collect_count); printf("Current space capacity: %ld words\n", gc.space_capacity); - printf("Total allocated memory: %ld words\n", - gc.allocated_bytes_count / WORD_SIZE); + printf("Total allocated memory: %ld words\n", gc.allocated_bytes_count / WORD_SIZE); printf("Allocated words in new space: %ld words\n", gc.alloc_offset); printf("Current new space:\n"); @@ -197,8 +198,8 @@ static void **collect_riscv_state() { // ... static void set_riscv_reg(int idx, void *val) { switch (idx) { -#define X(i, reg, offset) \ - case i: \ +#define X(i, reg, offset) \ + case i: \ asm volatile("mv " #reg ", %0" ::"r"(val)); RISCV_REG_LIST #undef X @@ -248,15 +249,12 @@ void _gc_collect(void *current_sp) { void *cur_pointer = gc.new_space + cur_offset + 1; if (cur_size == 0) { - fprintf( - stderr, - "You have object on heap with zero size\nBug in malloc function!\n"); + fprintf(stderr, "You have object on heap with zero size\nBug in malloc function!\n"); print_gc_status(); exit(122); } - LOG("Try to find stack cell with 0x%x value on 0x%ld offset\n", cur_pointer, - cur_offset + 1); + LOG("Try to find stack cell with 0x%x value on 0x%ld offset\n", cur_pointer, cur_offset + 1); // try to find in regs and stack at least one pointer { @@ -311,8 +309,7 @@ void _gc_collect(void *current_sp) { for (size_t i = 0; i < stack_size; i++) { void **byte = (void **)gc.base_sp - i - 1; if (*byte == cur_pointer) { - LOG("Change stack cell 0x%x. 0x%x -> 0x%x\n", byte, *byte, - new_pointer); + LOG("Change stack cell 0x%x. 0x%x -> 0x%x\n", byte, *byte, new_pointer); *byte = new_pointer; } } @@ -382,9 +379,10 @@ void **get_heap_start() { return gc.new_space; } - return gc.new_space == first_new_space - ? (void **)0x1000 - : ((void **)0x1000) + GC_SPACE_INITIAL_SIZE; + void **addr = (void **)0x1000; + addr += gc.new_space == first_new_space ? 0 : GC_SPACE_INITIAL_SIZE; + + return addr; } // get heap end of current new_space @@ -393,10 +391,11 @@ void **get_heap_fin() { return gc.new_space + gc.space_capacity; } - return (gc.new_space == first_new_space - ? (void **)0x1000 - : ((void **)0x1000) + GC_SPACE_INITIAL_SIZE) + - gc.space_capacity; + void **addr = (void **)0x1000; + addr += gc.new_space == first_new_space ? 0 : GC_SPACE_INITIAL_SIZE; + addr += gc.space_capacity; + + return addr; } void *alloc_closure(INT8, void *f, uint8_t argc) { @@ -427,24 +426,40 @@ void *copy_closure(closure *old_clos) { // get closure and apply [argc] arguments to closure void *apply_closure(INT8, closure *old_clos, uint8_t argc, ...) { - closure *clos = copy_closure(old_clos); - LOG("CLOS: 0x%x\n", clos); - LOGF(print_gc_status()); - fflush(stdout); + void **args = malloc(sizeof(void *) * argc); + va_list list; va_start(list, argc); + for (size_t i = 0; i < argc; i++) { + void *arg = va_arg(list, void *); + args[i] = arg; + } + va_end(list); + + LOG("[Debug] apply_closure(old_clos = {\n\tcode: 0x%x,\n\targc: %d\n\targc_recived: %d\n\targs = [", old_clos->code, + old_clos->argc, old_clos->argc_recived); + for (size_t i = 0; i < old_clos->argc_recived; i++) { + LOG(i == 0 ? "0x%x" : ", 0x%x", old_clos->args[i]); + } + LOG("]\n}, argc: %d, args: [", argc); + for (size_t i = 0; i < argc; i++) { + LOG(i == 0 ? "0x%x" : ", 0x%x", args[i]); + } + LOG("])\n"); + fflush(stdout); + + closure *clos = copy_closure(old_clos); + // LOGF(print_gc_status()); if (clos->argc_recived + argc > clos->argc) { - fprintf(stdout, - "Runtime error: function accept more arguments than expect\n"); + fprintf(stdout, "Runtime error: function accept more arguments than expect\n"); exit(122); } for (size_t i = 0; i < argc; i++) { - void *arg = va_arg(list, void *); - clos->args[clos->argc_recived++] = arg; + clos->args[clos->argc_recived++] = args[i]; + free(args); } - va_end(list); // if application is partial if (clos->argc_recived < clos->argc) { diff --git a/PudgeWithMoML/test/GC.t b/PudgeWithMoML/test/GC.t index 668000bb..69e42267 100644 --- a/PudgeWithMoML/test/GC.t +++ b/PudgeWithMoML/test/GC.t @@ -11,64 +11,20 @@ > print_int lol > EOF $ qemu-riscv64 -L /usr/riscv64-linux-gnu -cpu rv64 ../main.exe - === GC status === - Start address of new space: 1000 - Allocate count: 2 times - Collect count: 0 times - Current space capacity: 8192 words - Total allocated memory: 12 words - Allocated words in new space: 12 words - Current new space: - (0x1000) 0x0: [size: 5] - (0x1008) 0x1: [data: 0x10670] - (0x1010) 0x2: [data: 0x2] - (0x1018) 0x3: [data: 0x0] - (0x1020) 0x4: [data: 0x0] - (0x1028) 0x5: [data: 0x0] - (0x1030) 0x6: [size: 5] - (0x1038) 0x7: [data: 0x10670] - (0x1040) 0x8: [data: 0x2] - (0x1048) 0x9: [data: 0x1] - (0x1050) 0xa: [data: 0x5] - (0x1058) 0xb: [data: 0x0] - === GC status === - === GC status === - Start address of new space: 11000 - Allocate count: 2 times - Collect count: 1 times - Current space capacity: 8192 words - Total allocated memory: 12 words - Allocated words in new space: 6 words - Current new space: - (0x11000) 0x0: [size: 5] - (0x11008) 0x1: [data: 0x10670] - (0x11010) 0x2: [data: 0x2] - (0x11018) 0x3: [data: 0x1] - (0x11020) 0x4: [data: 0x5] - (0x11028) 0x5: [data: 0x0] - === GC status === - === GC status === - Start address of new space: 11000 - Allocate count: 3 times - Collect count: 1 times - Current space capacity: 8192 words - Total allocated memory: 18 words - Allocated words in new space: 12 words - Current new space: - (0x11000) 0x0: [size: 5] - (0x11008) 0x1: [data: 0x10670] - (0x11010) 0x2: [data: 0x2] - (0x11018) 0x3: [data: 0x1] - (0x11020) 0x4: [data: 0x5] - (0x11028) 0x5: [data: 0x0] - (0x11030) 0x6: [size: 5] - (0x11038) 0x7: [data: 0x10670] - (0x11040) 0x8: [data: 0x2] - (0x11048) 0x9: [data: 0x2] - (0x11050) 0xa: [data: 0x5] - (0x11058) 0xb: [data: 0x2] - === GC status === - 7 + alloc_closure(0x106c0, 2) + MY MALLOC: 40 + [Debug] apply_closure(old_clos = { + code: 0x106c0, + argc: 2 + argc_recived: 0 + args = [] + }, argc: 3, args: [0xb, 0xfff, 0x10000]) + alloc_closure(0x106c0, 2) + MY MALLOC: 40 + old clos: 0x152a8, new clos: 0x152d8 + clos.code: 0x106c0, clos argc: 0x2 + Runtime error: function accept more arguments than expect + [122] $ cat ../main.anf let add__0 = fun a__1 -> fun b__2 -> @@ -98,7 +54,11 @@ addi fp, sp, 16 ld t0, 0(fp) ld t1, 8(fp) + srai t0, t0, 1 + srai t1, t1, 1 add a0, t0, t1 + slli a0, a0, 1 + ori a0, a0, 1 ld ra, 8(sp) ld fp, 0(sp) addi sp, sp, 16 @@ -121,9 +81,9 @@ mv t0, a0 addi sp, sp, 16 sd t0, 0(sp) - li t0, 1 + li t0, 3 sd t0, 8(sp) - li t0, 5 + li t0, 11 sd t0, 16(sp) # End loading args on stack call apply_closure @@ -154,9 +114,9 @@ addi sp, sp, -32 ld t0, -72(fp) sd t0, 0(sp) - li t0, 1 + li t0, 3 sd t0, 8(sp) - li t0, 2 + li t0, 5 sd t0, 16(sp) # End loading args on stack call apply_closure @@ -174,6 +134,7 @@ sd t0, -104(fp) # Apply print_int ld a0, -88(fp) + srai a0, a0, 1 call print_int mv t0, a0 # End Apply print_int @@ -200,53 +161,22 @@ > let main = print_int homs > EOF $ qemu-riscv64 -L /usr/riscv64-linux-gnu -cpu rv64 ../main.exe - === GC status === - Start address of new space: 1000 - Allocate count: 5 times - Collect count: 0 times - Current space capacity: 8192 words - Total allocated memory: 28 words - Allocated words in new space: 28 words - Current new space: - (0x1000) 0x0: [size: 5] - (0x1008) 0x1: [data: 0x10670] - (0x1010) 0x2: [data: 0x2] - (0x1018) 0x3: [data: 0x0] - (0x1020) 0x4: [data: 0x0] - (0x1028) 0x5: [data: 0x0] - (0x1030) 0x6: [size: 4] - (0x1038) 0x7: [data: 0x106a0] - (0x1040) 0x8: [data: 0x1] - (0x1048) 0x9: [data: 0x0] - (0x1050) 0xa: [data: 0x0] - (0x1058) 0xb: [size: 5] - (0x1060) 0xc: [data: 0x10670] - (0x1068) 0xd: [data: 0x2] - (0x1070) 0xe: [data: 0x1] - (0x1078) 0xf: [data: 0x142d8] - (0x1080) 0x10: [data: 0x0] - (0x1088) 0x11: [size: 5] - (0x1090) 0x12: [data: 0x10670] - (0x1098) 0x13: [data: 0x2] - (0x10a0) 0x14: [data: 0x2] - (0x10a8) 0x15: [data: 0x142d8] - (0x10b0) 0x16: [data: 0x5] - (0x10b8) 0x17: [size: 4] - (0x10c0) 0x18: [data: 0x106a0] - (0x10c8) 0x19: [data: 0x1] - (0x10d0) 0x1a: [data: 0x1] - (0x10d8) 0x1b: [data: 0x5] - === GC status === - === GC status === - Start address of new space: 11000 - Allocate count: 5 times - Collect count: 1 times - Current space capacity: 8192 words - Total allocated memory: 28 words - Allocated words in new space: 0 words - Current new space: - === GC status === - 5 + alloc_closure(0x106c0, 2) + MY MALLOC: 40 + alloc_closure(0x106f0, 1) + MY MALLOC: 32 + [Debug] apply_closure(old_clos = { + code: 0x106c0, + argc: 2 + argc_recived: 0 + args = [] + }, argc: 3, args: [0x152d8, 0x0, 0x152d8]) + alloc_closure(0x106c0, 2) + MY MALLOC: 40 + old clos: 0x152a8, new clos: 0x15300 + clos.code: 0x106c0, clos argc: 0x2 + Runtime error: function accept more arguments than expect + [122] $ cat ../main.anf let wrap__0 = fun f__1 -> fun x__2 -> @@ -292,7 +222,7 @@ addi sp, sp, -32 ld t0, -24(fp) sd t0, 0(sp) - li t0, 1 + li t0, 3 sd t0, 8(sp) ld t0, 8(fp) sd t0, 16(sp) @@ -330,7 +260,7 @@ addi sp, sp, -32 ld t0, -24(fp) sd t0, 0(sp) - li t0, 1 + li t0, 3 sd t0, 8(sp) ld t0, 16(fp) sd t0, 16(sp) @@ -349,9 +279,9 @@ addi sp, sp, -32 ld t0, -40(fp) sd t0, 0(sp) - li t0, 1 + li t0, 3 sd t0, 8(sp) - li t0, 5 + li t0, 11 sd t0, 16(sp) # End loading args on stack call apply_closure @@ -372,7 +302,7 @@ # Apply homka__5 with 3 args # Load args on stack addi sp, sp, -32 - li t0, 2 + li t0, 5 sd t0, 0(sp) addi sp, sp, -16 la t5, wrap__0 @@ -413,6 +343,7 @@ # Apply print_int la t5, homs__10 ld a0, 0(t5) + srai a0, a0, 1 call print_int mv t0, a0 # End Apply print_int @@ -444,76 +375,20 @@ > let main = print_int 5 > EOF $ qemu-riscv64 -L /usr/riscv64-linux-gnu -cpu rv64 ../main.exe - === GC status === - Start address of new space: 1000 - Allocate count: 2 times - Collect count: 0 times - Current space capacity: 8192 words - Total allocated memory: 12 words - Allocated words in new space: 12 words - Current new space: - (0x1000) 0x0: [size: 5] - (0x1008) 0x1: [data: 0x10670] - (0x1010) 0x2: [data: 0x2] - (0x1018) 0x3: [data: 0x0] - (0x1020) 0x4: [data: 0x0] - (0x1028) 0x5: [data: 0x0] - (0x1030) 0x6: [size: 5] - (0x1038) 0x7: [data: 0x10670] - (0x1040) 0x8: [data: 0x2] - (0x1048) 0x9: [data: 0x1] - (0x1050) 0xa: [data: 0x7] - (0x1058) 0xb: [data: 0x0] - === GC status === - === GC status === - Start address of new space: 11000 - Allocate count: 2 times - Collect count: 1 times - Current space capacity: 8192 words - Total allocated memory: 12 words - Allocated words in new space: 0 words - Current new space: - === GC status === - === GC status === - Start address of new space: 11000 - Allocate count: 4 times - Collect count: 1 times - Current space capacity: 8192 words - Total allocated memory: 24 words - Allocated words in new space: 12 words - Current new space: - (0x11000) 0x0: [size: 5] - (0x11008) 0x1: [data: 0x10670] - (0x11010) 0x2: [data: 0x2] - (0x11018) 0x3: [data: 0x0] - (0x11020) 0x4: [data: 0x0] - (0x11028) 0x5: [data: 0x0] - (0x11030) 0x6: [size: 5] - (0x11038) 0x7: [data: 0x10670] - (0x11040) 0x8: [data: 0x2] - (0x11048) 0x9: [data: 0x1] - (0x11050) 0xa: [data: 0x6] - (0x11058) 0xb: [data: 0x0] - === GC status === - === GC status === - Start address of new space: 1000 - Allocate count: 4 times - Collect count: 2 times - Current space capacity: 8192 words - Total allocated memory: 24 words - Allocated words in new space: 0 words - Current new space: - === GC status === - === GC status === - Start address of new space: 1000 - Allocate count: 4 times - Collect count: 2 times - Current space capacity: 8192 words - Total allocated memory: 24 words - Allocated words in new space: 0 words - Current new space: - === GC status === - 5 + alloc_closure(0x106c0, 2) + MY MALLOC: 40 + [Debug] apply_closure(old_clos = { + code: 0x106c0, + argc: 2 + argc_recived: 0 + args = [] + }, argc: 3, args: [0xf, 0x106f2, 0x1]) + alloc_closure(0x106c0, 2) + MY MALLOC: 40 + old clos: 0x152a8, new clos: 0x152d8 + clos.code: 0x106c0, clos argc: 0x2 + Runtime error: function accept more arguments than expect + [122] ( move multiple objects to old_space ) $ make compile opts=-gen_mid --no-print-directory -C .. << 'EOF' @@ -529,88 +404,20 @@ > print_int lol > EOF $ qemu-riscv64 -L /usr/riscv64-linux-gnu -cpu rv64 ../main.exe - === GC status === - Start address of new space: 1000 - Allocate count: 4 times - Collect count: 0 times - Current space capacity: 8192 words - Total allocated memory: 24 words - Allocated words in new space: 24 words - Current new space: - (0x1000) 0x0: [size: 5] - (0x1008) 0x1: [data: 0x10670] - (0x1010) 0x2: [data: 0x2] - (0x1018) 0x3: [data: 0x0] - (0x1020) 0x4: [data: 0x0] - (0x1028) 0x5: [data: 0x0] - (0x1030) 0x6: [size: 5] - (0x1038) 0x7: [data: 0x10670] - (0x1040) 0x8: [data: 0x2] - (0x1048) 0x9: [data: 0x1] - (0x1050) 0xa: [data: 0x5] - (0x1058) 0xb: [data: 0x0] - (0x1060) 0xc: [size: 5] - (0x1068) 0xd: [data: 0x10670] - (0x1070) 0xe: [data: 0x2] - (0x1078) 0xf: [data: 0x0] - (0x1080) 0x10: [data: 0x0] - (0x1088) 0x11: [data: 0x0] - (0x1090) 0x12: [size: 5] - (0x1098) 0x13: [data: 0x10670] - (0x10a0) 0x14: [data: 0x2] - (0x10a8) 0x15: [data: 0x1] - (0x10b0) 0x16: [data: 0x3] - (0x10b8) 0x17: [data: 0x0] - === GC status === - === GC status === - Start address of new space: 11000 - Allocate count: 4 times - Collect count: 1 times - Current space capacity: 8192 words - Total allocated memory: 24 words - Allocated words in new space: 12 words - Current new space: - (0x11000) 0x0: [size: 5] - (0x11008) 0x1: [data: 0x10670] - (0x11010) 0x2: [data: 0x2] - (0x11018) 0x3: [data: 0x1] - (0x11020) 0x4: [data: 0x5] - (0x11028) 0x5: [data: 0x0] - (0x11030) 0x6: [size: 5] - (0x11038) 0x7: [data: 0x10670] - (0x11040) 0x8: [data: 0x2] - (0x11048) 0x9: [data: 0x1] - (0x11050) 0xa: [data: 0x3] - (0x11058) 0xb: [data: 0x0] - === GC status === - === GC status === - Start address of new space: 11000 - Allocate count: 5 times - Collect count: 1 times - Current space capacity: 8192 words - Total allocated memory: 30 words - Allocated words in new space: 18 words - Current new space: - (0x11000) 0x0: [size: 5] - (0x11008) 0x1: [data: 0x10670] - (0x11010) 0x2: [data: 0x2] - (0x11018) 0x3: [data: 0x1] - (0x11020) 0x4: [data: 0x5] - (0x11028) 0x5: [data: 0x0] - (0x11030) 0x6: [size: 5] - (0x11038) 0x7: [data: 0x10670] - (0x11040) 0x8: [data: 0x2] - (0x11048) 0x9: [data: 0x1] - (0x11050) 0xa: [data: 0x3] - (0x11058) 0xb: [data: 0x0] - (0x11060) 0xc: [size: 5] - (0x11068) 0xd: [data: 0x10670] - (0x11070) 0xe: [data: 0x2] - (0x11078) 0xf: [data: 0x2] - (0x11080) 0x10: [data: 0x5] - (0x11088) 0x11: [data: 0x2] - === GC status === - 7 + alloc_closure(0x106c0, 2) + MY MALLOC: 40 + [Debug] apply_closure(old_clos = { + code: 0x106c0, + argc: 2 + argc_recived: 0 + args = [] + }, argc: 3, args: [0xb, 0x0, 0x273aa408]) + alloc_closure(0x106c0, 2) + MY MALLOC: 40 + old clos: 0x152a8, new clos: 0x152d8 + clos.code: 0x106c0, clos argc: 0x2 + Runtime error: function accept more arguments than expect + [122] $ cat ../main.anf let add__0 = fun a__1 -> fun b__2 -> @@ -642,7 +449,11 @@ addi fp, sp, 16 ld t0, 0(fp) ld t1, 8(fp) + srai t0, t0, 1 + srai t1, t1, 1 add a0, t0, t1 + slli a0, a0, 1 + ori a0, a0, 1 ld ra, 8(sp) ld fp, 0(sp) addi sp, sp, 16 @@ -665,9 +476,9 @@ mv t0, a0 addi sp, sp, 16 sd t0, 0(sp) - li t0, 1 + li t0, 3 sd t0, 8(sp) - li t0, 5 + li t0, 11 sd t0, 16(sp) # End loading args on stack call apply_closure @@ -691,9 +502,9 @@ mv t0, a0 addi sp, sp, 16 sd t0, 0(sp) - li t0, 1 - sd t0, 8(sp) li t0, 3 + sd t0, 8(sp) + li t0, 7 sd t0, 16(sp) # End loading args on stack call apply_closure @@ -724,9 +535,9 @@ addi sp, sp, -32 ld t0, -88(fp) sd t0, 0(sp) - li t0, 1 + li t0, 3 sd t0, 8(sp) - li t0, 2 + li t0, 5 sd t0, 16(sp) # End loading args on stack call apply_closure @@ -744,6 +555,7 @@ sd t0, -120(fp) # Apply print_int ld a0, -104(fp) + srai a0, a0, 1 call print_int mv t0, a0 # End Apply print_int @@ -763,8 +575,23 @@ > let main = print_int (fib 15 (fun x -> x)) > EOF $ qemu-riscv64 -L /usr/riscv64-linux-gnu -cpu rv64 ../main.exe - panic! overflow memory limits - [122] + alloc_closure(0x1088c, 1) + MY MALLOC: 32 + alloc_closure(0x1070c, 3) + MY MALLOC: 48 + [Debug] apply_closure(old_clos = { + code: 0x1070c, + argc: 3 + argc_recived: 0 + args = [] + }, argc: 3, args: [0x152a8, 0x3, 0x0]) + alloc_closure(0x1070c, 3) + MY MALLOC: 48 + old clos: 0x152d0, new clos: 0x15308 + clos.code: 0x1070c, clos argc: 0x3 + free(): double free detected in tcache 2 + 86489 Aborted (core dumped) qemu-riscv64 -L /usr/riscv64-linux-gnu -cpu rv64 ../main.exe + [134] ( get current capacity of heap ) $ make compile opts=-gen_mid --no-print-directory -C .. << 'EOF' @@ -773,4 +600,4 @@ > let main = print_int (start) > EOF $ qemu-riscv64 -L /usr/riscv64-linux-gnu -cpu rv64 ../main.exe - 4096 + 2048 From 4c75035e08696aa9b00decd6065300d052e4f2a1 Mon Sep 17 00:00:00 2001 From: homka122 Date: Mon, 10 Nov 2025 17:46:28 +0300 Subject: [PATCH 14/19] Refactor: improve debug messages in runtime Signed-off-by: homka122 --- PudgeWithMoML/bin/run_after_ll.t | 148 +++++++++++++++++++++++----- PudgeWithMoML/lib/runtime/runtime.c | 40 +++++--- 2 files changed, 154 insertions(+), 34 deletions(-) diff --git a/PudgeWithMoML/bin/run_after_ll.t b/PudgeWithMoML/bin/run_after_ll.t index c04efef3..871c5f13 100644 --- a/PudgeWithMoML/bin/run_after_ll.t +++ b/PudgeWithMoML/bin/run_after_ll.t @@ -4,7 +4,25 @@ $ make compile opts=-gen_mid input=test/manytests/typed/010faccps_ll.ml --no-print-directory -C .. $ qemu-riscv64 -L /usr/riscv64-linux-gnu -cpu rv64 ../main.exe | tee -a results.txt && echo "-----" >> results.txt - 24 + [DEBUG] alloc_closure(f: 0x106c0, argc: 1) + [DEBUG] my_malloc(size: 32) + -> 0x152a8 + -> 0x152a8 + [DEBUG] alloc_closure(f: 0x106d2, argc: 3) + [DEBUG] my_malloc(size: 48) + -> 0x152d0 + -> 0x152d0 + [Debug] apply_closure(old_clos = { + code: 0x106d2, + argc: 3 + argc_recived: 0 + args = [] + }, argc: 5, args: [0x9, 0x152a8, 0x152a8, 0x7, 0xa1f2b3b0]) + [DEBUG] alloc_closure(f: 0x106d2, argc: 3) + [DEBUG] my_malloc(size: 48) + -> 0x15308 + -> 0x15308 + Runtime error: function accept more arguments than expect $ cat ../main.anf let id__0 = fun x__1 -> x__1 @@ -50,7 +68,11 @@ addi fp, sp, 32 ld t0, 16(fp) ld t1, 0(fp) + srai t0, t0, 1 + srai t1, t1, 1 mul t0, t0, t1 + slli t0, t0, 1 + ori t0, t0, 1 sd t0, -24(fp) # Apply k__4 with 1 args ld t0, 8(fp) @@ -59,7 +81,7 @@ addi sp, sp, -32 ld t0, -32(fp) sd t0, 0(sp) - li t0, 1 + li t0, 3 sd t0, 8(sp) ld t0, -24(fp) sd t0, 16(sp) @@ -80,7 +102,7 @@ sd fp, 32(sp) addi fp, sp, 48 ld t0, 0(fp) - li t1, 1 + li t1, 3 sub t0, t0, t1 seqz t0, t0 sd t0, -24(fp) @@ -93,9 +115,9 @@ addi sp, sp, -32 ld t0, -32(fp) sd t0, 0(sp) - li t0, 1 + li t0, 3 sd t0, 8(sp) - li t0, 1 + li t0, 3 sd t0, 16(sp) # End loading args on stack call apply_closure @@ -106,8 +128,12 @@ j L3 L2: ld t0, 0(fp) - li t1, 1 + li t1, 3 + srai t0, t0, 1 + srai t1, t1, 1 sub t0, t0, t1 + slli t0, t0, 1 + ori t0, t0, 1 sd t0, -40(fp) # Partial application fresh_1__2 with 2 args # Load args on stack @@ -121,7 +147,7 @@ mv t0, a0 addi sp, sp, 16 sd t0, 0(sp) - li t0, 2 + li t0, 5 sd t0, 8(sp) ld t0, 0(fp) sd t0, 16(sp) @@ -162,7 +188,7 @@ # Apply fac_cps__6 with 2 args # Load args on stack addi sp, sp, -16 - li t0, 4 + li t0, 9 sd t0, 0(sp) addi sp, sp, -16 la t5, id__0 @@ -183,11 +209,12 @@ sd t0, -8(fp) # Apply print_int ld a0, -8(fp) + srai a0, a0, 1 call print_int mv t0, a0 # End Apply print_int sd t0, -16(fp) - li t0, 0 + li t0, 1 la t1, main__9 sd t0, 0(t1) call flush @@ -199,7 +226,29 @@ $ make compile opts=-gen_mid input=test/manytests/typed/010fibcps_ll.ml --no-print-directory -C .. $ qemu-riscv64 -L /usr/riscv64-linux-gnu -cpu rv64 ../main.exe | tee -a results.txt && echo "-----" >> results.txt - 8 + [DEBUG] alloc_closure(f: 0x106c0, argc: 1) + [DEBUG] my_malloc(size: 32) + -> 0x152a8 + -> 0x152a8 + [DEBUG] alloc_closure(f: 0x1071e, argc: 4) + [DEBUG] my_malloc(size: 56) + -> 0x152d0 + -> 0x152d0 + [DEBUG] alloc_closure(f: 0x107c0, argc: 2) + [DEBUG] my_malloc(size: 40) + -> 0x15310 + -> 0x15310 + [Debug] apply_closure(old_clos = { + code: 0x1071e, + argc: 4 + argc_recived: 0 + args = [] + }, argc: 7, args: [0xd, 0x152a8, 0x15310, 0x10000, 0x152a8, 0xb, 0xcab1d3b0]) + [DEBUG] alloc_closure(f: 0x1071e, argc: 4) + [DEBUG] my_malloc(size: 56) + -> 0x15340 + -> 0x15340 + Runtime error: function accept more arguments than expect $ cat ../main.anf let id__0 = fun x__1 -> x__1 @@ -255,7 +304,11 @@ addi fp, sp, 32 ld t0, 0(fp) ld t1, 16(fp) + srai t0, t0, 1 + srai t1, t1, 1 add t0, t0, t1 + slli t0, t0, 1 + ori t0, t0, 1 sd t0, -24(fp) # Apply k__4 with 1 args ld t0, 8(fp) @@ -264,7 +317,7 @@ addi sp, sp, -32 ld t0, -32(fp) sd t0, 0(sp) - li t0, 1 + li t0, 3 sd t0, 8(sp) ld t0, -24(fp) sd t0, 16(sp) @@ -285,8 +338,12 @@ sd fp, 32(sp) addi fp, sp, 48 ld t0, 0(fp) - li t1, 2 + li t1, 5 + srai t0, t0, 1 + srai t1, t1, 1 sub t0, t0, t1 + slli t0, t0, 1 + ori t0, t0, 1 sd t0, -24(fp) # Partial application fresh_2__2 with 2 args # Load args on stack @@ -300,7 +357,7 @@ mv t0, a0 addi sp, sp, 16 sd t0, 0(sp) - li t0, 2 + li t0, 5 sd t0, 8(sp) ld t0, 24(fp) sd t0, 16(sp) @@ -321,7 +378,7 @@ addi sp, sp, -32 ld t0, -40(fp) sd t0, 0(sp) - li t0, 1 + li t0, 3 sd t0, 8(sp) ld t0, -24(fp) sd t0, 16(sp) @@ -336,7 +393,7 @@ addi sp, sp, -32 ld t0, -48(fp) sd t0, 0(sp) - li t0, 1 + li t0, 3 sd t0, 8(sp) ld t0, -32(fp) sd t0, 16(sp) @@ -357,7 +414,7 @@ sd fp, 32(sp) addi fp, sp, 48 ld t0, 0(fp) - li t1, 2 + li t1, 5 slt t0, t0, t1 sd t0, -24(fp) ld t0, -24(fp) @@ -369,7 +426,7 @@ addi sp, sp, -32 ld t0, -32(fp) sd t0, 0(sp) - li t0, 1 + li t0, 3 sd t0, 8(sp) ld t0, 0(fp) sd t0, 16(sp) @@ -382,8 +439,12 @@ j L5 L4: ld t0, 0(fp) - li t1, 1 + li t1, 3 + srai t0, t0, 1 + srai t1, t1, 1 sub t0, t0, t1 + slli t0, t0, 1 + ori t0, t0, 1 sd t0, -40(fp) # Partial application fresh_1__6 with 3 args # Load args on stack @@ -397,7 +458,7 @@ mv t0, a0 addi sp, sp, 16 sd t0, 0(sp) - li t0, 3 + li t0, 7 sd t0, 8(sp) ld t0, 0(fp) sd t0, 16(sp) @@ -447,7 +508,7 @@ # Apply fib__11 with 2 args # Load args on stack addi sp, sp, -16 - li t0, 6 + li t0, 13 sd t0, 0(sp) addi sp, sp, -16 la t5, id__0 @@ -468,13 +529,14 @@ sd t0, -8(fp) # Apply print_int ld a0, -8(fp) + srai a0, a0, 1 call print_int mv t0, a0 # End Apply print_int sd t0, -16(fp) ld t0, -16(fp) sd t0, -24(fp) - li t0, 0 + li t0, 1 la t1, main__14 sd t0, 0(t1) call flush @@ -487,7 +549,47 @@ ( IT MUST BE AT THE END OF THE CRAM TEST ) $ cat results.txt - 24 + [DEBUG] alloc_closure(f: 0x106c0, argc: 1) + [DEBUG] my_malloc(size: 32) + -> 0x152a8 + -> 0x152a8 + [DEBUG] alloc_closure(f: 0x106d2, argc: 3) + [DEBUG] my_malloc(size: 48) + -> 0x152d0 + -> 0x152d0 + [Debug] apply_closure(old_clos = { + code: 0x106d2, + argc: 3 + argc_recived: 0 + args = [] + }, argc: 5, args: [0x9, 0x152a8, 0x152a8, 0x7, 0xa1f2b3b0]) + [DEBUG] alloc_closure(f: 0x106d2, argc: 3) + [DEBUG] my_malloc(size: 48) + -> 0x15308 + -> 0x15308 + Runtime error: function accept more arguments than expect ----- - 8 + [DEBUG] alloc_closure(f: 0x106c0, argc: 1) + [DEBUG] my_malloc(size: 32) + -> 0x152a8 + -> 0x152a8 + [DEBUG] alloc_closure(f: 0x1071e, argc: 4) + [DEBUG] my_malloc(size: 56) + -> 0x152d0 + -> 0x152d0 + [DEBUG] alloc_closure(f: 0x107c0, argc: 2) + [DEBUG] my_malloc(size: 40) + -> 0x15310 + -> 0x15310 + [Debug] apply_closure(old_clos = { + code: 0x1071e, + argc: 4 + argc_recived: 0 + args = [] + }, argc: 7, args: [0xd, 0x152a8, 0x15310, 0x10000, 0x152a8, 0xb, 0xcab1d3b0]) + [DEBUG] alloc_closure(f: 0x1071e, argc: 4) + [DEBUG] my_malloc(size: 56) + -> 0x15340 + -> 0x15340 + Runtime error: function accept more arguments than expect ----- diff --git a/PudgeWithMoML/lib/runtime/runtime.c b/PudgeWithMoML/lib/runtime/runtime.c index 629d0506..3da6aa16 100644 --- a/PudgeWithMoML/lib/runtime/runtime.c +++ b/PudgeWithMoML/lib/runtime/runtime.c @@ -339,7 +339,7 @@ void gc_collect() { // alloc size bytes in gc.memory void *my_malloc(size_t size) { - LOG("MY MALLOC: %ld\n", size); + LOG("[DEBUG] %s(size: %ld)\n", __func__, size); if (size == 0) { return NULL; } @@ -370,36 +370,49 @@ void *my_malloc(size_t size) { gc.alloc_offset += words; gc.alloc_count++; gc.allocated_bytes_count += size + WORD_SIZE; + + LOG(" -> 0x%x\n", result); return result; } // get heap start of current new_space void **get_heap_start() { + LOG("[DEBUG] %s()\n", __func__); if (!STABLE_CI) { - return gc.new_space; + void **result = gc.new_space; + LOG(" -> 0x%x\n", result); + return result; } void **addr = (void **)0x1000; addr += gc.new_space == first_new_space ? 0 : GC_SPACE_INITIAL_SIZE; - return addr; + void **result = addr; + LOG(" -> 0x%x\n", result); + return result; } // get heap end of current new_space void **get_heap_fin() { + LOG("[DEBUG] %s()\n", __func__); + if (!STABLE_CI) { - return gc.new_space + gc.space_capacity; + void **result = gc.new_space + gc.space_capacity; + LOG(" -> 0x%x", result); + return result; } void **addr = (void **)0x1000; addr += gc.new_space == first_new_space ? 0 : GC_SPACE_INITIAL_SIZE; addr += gc.space_capacity; - return addr; + void **result = addr; + LOG(" -> 0x%x", result); + return result; } void *alloc_closure(INT8, void *f, uint8_t argc) { - LOG("alloc_closure(0x%x, %d)\n", f, argc); + LOG("[DEBUG] %s(f: 0x%x, argc: %d)\n", __func__, f, argc); closure *clos = my_malloc(sizeof(closure) + sizeof(void *) * argc); clos->code = f; @@ -407,15 +420,17 @@ void *alloc_closure(INT8, void *f, uint8_t argc) { clos->argc_recived = 0; memset(clos->args, 0, sizeof(void *) * argc); - return clos; + void *result = clos; + LOG(" -> 0x%x\n", result); + return result; } void *copy_closure(closure *old_clos) { closure *clos = old_clos; closure *new = alloc_closure(ZERO8, clos->code, clos->argc); - LOG("old clos: 0x%x, new clos: 0x%x\n", clos, new); - LOG("clos.code: 0x%x, clos argc: 0x%d\n", clos->code, clos->argc); + // LOG("old clos: 0x%x, new clos: 0x%x\n", clos, new); + // LOG("clos.code: 0x%x, clos argc: 0x%d\n", clos->code, clos->argc); for (size_t i = 0; i < clos->argc_recived; i++) { new->args[new->argc_recived++] = clos->args[i]; @@ -436,7 +451,7 @@ void *apply_closure(INT8, closure *old_clos, uint8_t argc, ...) { } va_end(list); - LOG("[Debug] apply_closure(old_clos = {\n\tcode: 0x%x,\n\targc: %d\n\targc_recived: %d\n\targs = [", old_clos->code, + LOG("[Debug] %s(old_clos = {\n\tcode: 0x%x,\n\targc: %d\n\targc_recived: %d\n\targs = [", __func__, old_clos->code, old_clos->argc, old_clos->argc_recived); for (size_t i = 0; i < old_clos->argc_recived; i++) { LOG(i == 0 ? "0x%x" : ", 0x%x", old_clos->args[i]); @@ -463,11 +478,14 @@ void *apply_closure(INT8, closure *old_clos, uint8_t argc, ...) { // if application is partial if (clos->argc_recived < clos->argc) { - return clos; + void *result = clos; + LOG(" -> 0x%x\n", result) + return result; } // full application (we need pass all arguments to stack and exec function) assert(clos->argc_recived == clos->argc); + LOG(" -> *exec 0x%x*\n", old_clos->code); return call_closure(clos->code, clos->argc, clos->args); } From ec64a8b06746cd08349f1a42ce2023dda47276b2 Mon Sep 17 00:00:00 2001 From: homka122 Date: Mon, 10 Nov 2025 17:51:57 +0300 Subject: [PATCH 15/19] Feat: add static keyword to runtime functions Signed-off-by: homka122 --- PudgeWithMoML/lib/runtime/runtime.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/PudgeWithMoML/lib/runtime/runtime.c b/PudgeWithMoML/lib/runtime/runtime.c index 3da6aa16..3956096f 100644 --- a/PudgeWithMoML/lib/runtime/runtime.c +++ b/PudgeWithMoML/lib/runtime/runtime.c @@ -96,7 +96,7 @@ typedef struct { #define ZERO8 0, 0, 0, 0, 0, 0, 0, 0 #define INT8 int, int, int, int, int, int, int, int -void print_stack(void *current_sp); +static void print_stack(void *current_sp); // Print stats about Garbage Collector work void print_gc_status() { @@ -208,7 +208,7 @@ static void set_riscv_reg(int idx, void *val) { } } -void print_stack(void *current_sp) { +static void print_stack(void *current_sp) { printf("=== STACK status ===\n"); printf("BASE_SP: 0x%x, CURRENT_SP: 0x%x\n", gc.base_sp, current_sp); size_t stack_size = (gc.base_sp - current_sp) / 8; @@ -232,7 +232,7 @@ void print_stack(void *current_sp) { // 2) save new pointer to old_space // 3) iterate through stack\regs and replace all pointer to the new // pointer -void _gc_collect(void *current_sp) { +static void _gc_collect(void *current_sp) { if (gc.alloc_offset == 0) { return; } @@ -338,7 +338,7 @@ void gc_collect() { } // alloc size bytes in gc.memory -void *my_malloc(size_t size) { +static void *my_malloc(size_t size) { LOG("[DEBUG] %s(size: %ld)\n", __func__, size); if (size == 0) { return NULL; @@ -425,7 +425,7 @@ void *alloc_closure(INT8, void *f, uint8_t argc) { return result; } -void *copy_closure(closure *old_clos) { +static void *copy_closure(closure *old_clos) { closure *clos = old_clos; closure *new = alloc_closure(ZERO8, clos->code, clos->argc); From c98f2c3517ec816b78669bdf52a1fd48e0eaf484 Mon Sep 17 00:00:00 2001 From: homka122 Date: Mon, 10 Nov 2025 18:34:59 +0300 Subject: [PATCH 16/19] Feat: add tagged numbers Signed-off-by: homka122 --- PudgeWithMoML/bin/run.t | 156 +++++++++---- PudgeWithMoML/bin/run_after_ll.t | 90 +------- PudgeWithMoML/bin/run_simplyfied.t | 88 ++++--- PudgeWithMoML/lib/riscv/codegen.ml | 63 ++++- PudgeWithMoML/lib/riscv/machine.ml | 20 ++ PudgeWithMoML/lib/runtime/runtime.c | 12 +- PudgeWithMoML/lib/runtime/test | Bin 0 -> 19544 bytes PudgeWithMoML/test/GC.t | 341 +++++++++++++++++++++------- PudgeWithMoML/test/codegen.t | 134 ++++++----- 9 files changed, 585 insertions(+), 319 deletions(-) create mode 100755 PudgeWithMoML/lib/runtime/test diff --git a/PudgeWithMoML/bin/run.t b/PudgeWithMoML/bin/run.t index 1a98beaa..a65f392b 100644 --- a/PudgeWithMoML/bin/run.t +++ b/PudgeWithMoML/bin/run.t @@ -41,7 +41,11 @@ addi fp, sp, 32 ld t0, 16(fp) ld t1, 8(fp) + srai t0, t0, 1 + srai t1, t1, 1 mul t0, t0, t1 + slli t0, t0, 1 + ori t0, t0, 1 sd t0, -24(fp) # Apply k__2__new with 1 args ld t0, 0(fp) @@ -50,7 +54,7 @@ addi sp, sp, -32 ld t0, -32(fp) sd t0, 0(sp) - li t0, 1 + li t0, 3 sd t0, 8(sp) ld t0, -24(fp) sd t0, 16(sp) @@ -71,7 +75,7 @@ sd fp, 56(sp) addi fp, sp, 72 ld t0, 0(fp) - li t1, 2 + li t1, 5 slt t0, t0, t1 sd t0, -24(fp) ld t0, -24(fp) @@ -83,9 +87,9 @@ addi sp, sp, -32 ld t0, -32(fp) sd t0, 0(sp) - li t0, 1 + li t0, 3 sd t0, 8(sp) - li t0, 1 + li t0, 3 sd t0, 16(sp) # End loading args on stack call apply_closure @@ -96,8 +100,12 @@ j L5 L4: ld t0, 0(fp) - li t1, 1 + li t1, 3 + srai t0, t0, 1 + srai t1, t1, 1 sub t0, t0, t1 + slli t0, t0, 1 + ori t0, t0, 1 sd t0, -40(fp) addi sp, sp, -16 la t5, f_0 @@ -115,7 +123,7 @@ addi sp, sp, -32 ld t0, -56(fp) sd t0, 0(sp) - li t0, 1 + li t0, 3 sd t0, 8(sp) ld t0, 8(fp) sd t0, 16(sp) @@ -130,7 +138,7 @@ addi sp, sp, -32 ld t0, -64(fp) sd t0, 0(sp) - li t0, 1 + li t0, 3 sd t0, 8(sp) ld t0, 0(fp) sd t0, 16(sp) @@ -189,7 +197,7 @@ # Apply fac__0 with 2 args # Load args on stack addi sp, sp, -16 - li t0, 6 + li t0, 13 sd t0, 0(sp) ld t0, -8(fp) sd t0, 8(sp) @@ -264,7 +272,11 @@ addi fp, sp, 32 ld t0, 0(fp) ld t1, 16(fp) + srai t0, t0, 1 + srai t1, t1, 1 add t0, t0, t1 + slli t0, t0, 1 + ori t0, t0, 1 sd t0, -24(fp) # Apply k__2__new with 1 args ld t0, 8(fp) @@ -273,7 +285,7 @@ addi sp, sp, -32 ld t0, -32(fp) sd t0, 0(sp) - li t0, 1 + li t0, 3 sd t0, 8(sp) ld t0, -24(fp) sd t0, 16(sp) @@ -294,8 +306,12 @@ sd fp, 40(sp) addi fp, sp, 56 ld t0, 8(fp) - li t1, 2 + li t1, 5 + srai t0, t0, 1 + srai t1, t1, 1 sub t0, t0, t1 + slli t0, t0, 1 + ori t0, t0, 1 sd t0, -24(fp) addi sp, sp, -16 la t5, f_0 @@ -313,7 +329,7 @@ addi sp, sp, -32 ld t0, -40(fp) sd t0, 0(sp) - li t0, 1 + li t0, 3 sd t0, 8(sp) ld t0, 16(fp) sd t0, 16(sp) @@ -328,7 +344,7 @@ addi sp, sp, -32 ld t0, -48(fp) sd t0, 0(sp) - li t0, 1 + li t0, 3 sd t0, 8(sp) ld t0, 0(fp) sd t0, 16(sp) @@ -364,7 +380,7 @@ sd fp, 56(sp) addi fp, sp, 72 ld t0, 0(fp) - li t1, 2 + li t1, 5 slt t0, t0, t1 sd t0, -24(fp) ld t0, -24(fp) @@ -376,7 +392,7 @@ addi sp, sp, -32 ld t0, -32(fp) sd t0, 0(sp) - li t0, 1 + li t0, 3 sd t0, 8(sp) ld t0, 0(fp) sd t0, 16(sp) @@ -389,8 +405,12 @@ j L7 L6: ld t0, 0(fp) - li t1, 1 + li t1, 3 + srai t0, t0, 1 + srai t1, t1, 1 sub t0, t0, t1 + slli t0, t0, 1 + ori t0, t0, 1 sd t0, -40(fp) addi sp, sp, -16 la t5, f_1 @@ -408,7 +428,7 @@ addi sp, sp, -32 ld t0, -56(fp) sd t0, 0(sp) - li t0, 1 + li t0, 3 sd t0, 8(sp) ld t0, 8(fp) sd t0, 16(sp) @@ -423,7 +443,7 @@ addi sp, sp, -32 ld t0, -64(fp) sd t0, 0(sp) - li t0, 1 + li t0, 3 sd t0, 8(sp) ld t0, 0(fp) sd t0, 16(sp) @@ -482,7 +502,7 @@ # Apply fib__0 with 2 args # Load args on stack addi sp, sp, -16 - li t0, 6 + li t0, 13 sd t0, 0(sp) ld t0, -8(fp) sd t0, 8(sp) @@ -569,8 +589,8 @@ sd ra, 16(sp) sd fp, 8(sp) addi fp, sp, 24 - li t0, 1 - li t1, 1 + li t0, 3 + li t1, 3 sub t0, t0, t1 seqz t0, t0 sd t0, -24(fp) @@ -615,7 +635,7 @@ sd t0, -56(fp) ld t0, -56(fp) sd t0, -64(fp) - li a0, 0 + li a0, 1 ld ra, 56(sp) ld fp, 48(sp) addi sp, sp, 64 @@ -628,39 +648,75 @@ addi fp, sp, 80 ld t0, 0(fp) ld t1, 8(fp) + srai t0, t0, 1 + srai t1, t1, 1 add t0, t0, t1 + slli t0, t0, 1 + ori t0, t0, 1 sd t0, -24(fp) ld t0, -24(fp) ld t1, 16(fp) + srai t0, t0, 1 + srai t1, t1, 1 add t0, t0, t1 + slli t0, t0, 1 + ori t0, t0, 1 sd t0, -32(fp) ld t0, -32(fp) ld t1, 24(fp) + srai t0, t0, 1 + srai t1, t1, 1 add t0, t0, t1 + slli t0, t0, 1 + ori t0, t0, 1 sd t0, -40(fp) ld t0, -40(fp) ld t1, 32(fp) + srai t0, t0, 1 + srai t1, t1, 1 add t0, t0, t1 + slli t0, t0, 1 + ori t0, t0, 1 sd t0, -48(fp) ld t0, -48(fp) ld t1, 40(fp) + srai t0, t0, 1 + srai t1, t1, 1 add t0, t0, t1 + slli t0, t0, 1 + ori t0, t0, 1 sd t0, -56(fp) ld t0, -56(fp) ld t1, 48(fp) + srai t0, t0, 1 + srai t1, t1, 1 add t0, t0, t1 + slli t0, t0, 1 + ori t0, t0, 1 sd t0, -64(fp) ld t0, -64(fp) ld t1, 56(fp) + srai t0, t0, 1 + srai t1, t1, 1 add t0, t0, t1 + slli t0, t0, 1 + ori t0, t0, 1 sd t0, -72(fp) ld t0, -72(fp) ld t1, 64(fp) + srai t0, t0, 1 + srai t1, t1, 1 add t0, t0, t1 + slli t0, t0, 1 + ori t0, t0, 1 sd t0, -80(fp) ld t0, -80(fp) ld t1, 72(fp) + srai t0, t0, 1 + srai t1, t1, 1 add a0, t0, t1 + slli a0, a0, 1 + ori a0, a0, 1 ld ra, 72(sp) ld fp, 64(sp) addi sp, sp, 80 @@ -685,7 +741,7 @@ addi sp, sp, -32 ld t0, -8(fp) sd t0, 0(sp) - li t0, 1 + li t0, 3 sd t0, 8(sp) addi sp, sp, -16 la t5, test10__9 @@ -707,9 +763,9 @@ addi sp, sp, -32 ld t0, -16(fp) sd t0, 0(sp) - li t0, 1 + li t0, 3 sd t0, 8(sp) - li t0, 1 + li t0, 3 sd t0, 16(sp) # End loading args on stack call apply_closure @@ -722,9 +778,9 @@ addi sp, sp, -32 ld t0, -24(fp) sd t0, 0(sp) - li t0, 1 + li t0, 3 sd t0, 8(sp) - li t0, 10 + li t0, 21 sd t0, 16(sp) # End loading args on stack call apply_closure @@ -737,9 +793,9 @@ addi sp, sp, -32 ld t0, -32(fp) sd t0, 0(sp) - li t0, 1 + li t0, 3 sd t0, 8(sp) - li t0, 100 + li t0, 201 sd t0, 16(sp) # End loading args on stack call apply_closure @@ -752,9 +808,9 @@ addi sp, sp, -32 ld t0, -40(fp) sd t0, 0(sp) - li t0, 1 + li t0, 3 sd t0, 8(sp) - li t0, 1000 + li t0, 2001 sd t0, 16(sp) # End loading args on stack call apply_closure @@ -767,9 +823,9 @@ addi sp, sp, -32 ld t0, -48(fp) sd t0, 0(sp) - li t0, 1 + li t0, 3 sd t0, 8(sp) - li t0, 10000 + li t0, 20001 sd t0, 16(sp) # End loading args on stack call apply_closure @@ -782,9 +838,9 @@ addi sp, sp, -32 ld t0, -56(fp) sd t0, 0(sp) - li t0, 1 + li t0, 3 sd t0, 8(sp) - li t0, 100000 + li t0, 200001 sd t0, 16(sp) # End loading args on stack call apply_closure @@ -797,9 +853,9 @@ addi sp, sp, -32 ld t0, -64(fp) sd t0, 0(sp) - li t0, 1 + li t0, 3 sd t0, 8(sp) - li t0, 1000000 + li t0, 2000001 sd t0, 16(sp) # End loading args on stack call apply_closure @@ -812,9 +868,9 @@ addi sp, sp, -32 ld t0, -72(fp) sd t0, 0(sp) - li t0, 1 + li t0, 3 sd t0, 8(sp) - li t0, 10000000 + li t0, 20000001 sd t0, 16(sp) # End loading args on stack call apply_closure @@ -827,9 +883,9 @@ addi sp, sp, -32 ld t0, -80(fp) sd t0, 0(sp) - li t0, 1 + li t0, 3 sd t0, 8(sp) - li t0, 100000000 + li t0, 200000001 sd t0, 16(sp) # End loading args on stack call apply_closure @@ -842,9 +898,9 @@ addi sp, sp, -32 ld t0, -88(fp) sd t0, 0(sp) - li t0, 1 + li t0, 3 sd t0, 8(sp) - li t0, 1000000000 + li t0, 2000000001 sd t0, 16(sp) # End loading args on stack call apply_closure @@ -876,7 +932,7 @@ addi sp, sp, -32 ld t0, -120(fp) sd t0, 0(sp) - li t0, 1 + li t0, 3 sd t0, 8(sp) addi sp, sp, -16 la t5, test3__2 @@ -898,9 +954,9 @@ addi sp, sp, -32 ld t0, -128(fp) sd t0, 0(sp) - li t0, 1 + li t0, 3 sd t0, 8(sp) - li t0, 1 + li t0, 3 sd t0, 16(sp) # End loading args on stack call apply_closure @@ -913,9 +969,9 @@ addi sp, sp, -32 ld t0, -136(fp) sd t0, 0(sp) - li t0, 1 + li t0, 3 sd t0, 8(sp) - li t0, 10 + li t0, 21 sd t0, 16(sp) # End loading args on stack call apply_closure @@ -928,9 +984,9 @@ addi sp, sp, -32 ld t0, -144(fp) sd t0, 0(sp) - li t0, 1 + li t0, 3 sd t0, 8(sp) - li t0, 100 + li t0, 201 sd t0, 16(sp) # End loading args on stack call apply_closure @@ -942,7 +998,7 @@ sd t0, -152(fp) ld t0, -152(fp) sd t0, -160(fp) - li t0, 0 + li t0, 1 la t1, main__20 sd t0, 0(t1) call flush diff --git a/PudgeWithMoML/bin/run_after_ll.t b/PudgeWithMoML/bin/run_after_ll.t index 871c5f13..652842c1 100644 --- a/PudgeWithMoML/bin/run_after_ll.t +++ b/PudgeWithMoML/bin/run_after_ll.t @@ -4,25 +4,7 @@ $ make compile opts=-gen_mid input=test/manytests/typed/010faccps_ll.ml --no-print-directory -C .. $ qemu-riscv64 -L /usr/riscv64-linux-gnu -cpu rv64 ../main.exe | tee -a results.txt && echo "-----" >> results.txt - [DEBUG] alloc_closure(f: 0x106c0, argc: 1) - [DEBUG] my_malloc(size: 32) - -> 0x152a8 - -> 0x152a8 - [DEBUG] alloc_closure(f: 0x106d2, argc: 3) - [DEBUG] my_malloc(size: 48) - -> 0x152d0 - -> 0x152d0 - [Debug] apply_closure(old_clos = { - code: 0x106d2, - argc: 3 - argc_recived: 0 - args = [] - }, argc: 5, args: [0x9, 0x152a8, 0x152a8, 0x7, 0xa1f2b3b0]) - [DEBUG] alloc_closure(f: 0x106d2, argc: 3) - [DEBUG] my_malloc(size: 48) - -> 0x15308 - -> 0x15308 - Runtime error: function accept more arguments than expect + 24 $ cat ../main.anf let id__0 = fun x__1 -> x__1 @@ -209,7 +191,6 @@ sd t0, -8(fp) # Apply print_int ld a0, -8(fp) - srai a0, a0, 1 call print_int mv t0, a0 # End Apply print_int @@ -226,29 +207,7 @@ $ make compile opts=-gen_mid input=test/manytests/typed/010fibcps_ll.ml --no-print-directory -C .. $ qemu-riscv64 -L /usr/riscv64-linux-gnu -cpu rv64 ../main.exe | tee -a results.txt && echo "-----" >> results.txt - [DEBUG] alloc_closure(f: 0x106c0, argc: 1) - [DEBUG] my_malloc(size: 32) - -> 0x152a8 - -> 0x152a8 - [DEBUG] alloc_closure(f: 0x1071e, argc: 4) - [DEBUG] my_malloc(size: 56) - -> 0x152d0 - -> 0x152d0 - [DEBUG] alloc_closure(f: 0x107c0, argc: 2) - [DEBUG] my_malloc(size: 40) - -> 0x15310 - -> 0x15310 - [Debug] apply_closure(old_clos = { - code: 0x1071e, - argc: 4 - argc_recived: 0 - args = [] - }, argc: 7, args: [0xd, 0x152a8, 0x15310, 0x10000, 0x152a8, 0xb, 0xcab1d3b0]) - [DEBUG] alloc_closure(f: 0x1071e, argc: 4) - [DEBUG] my_malloc(size: 56) - -> 0x15340 - -> 0x15340 - Runtime error: function accept more arguments than expect + 8 $ cat ../main.anf let id__0 = fun x__1 -> x__1 @@ -529,7 +488,6 @@ sd t0, -8(fp) # Apply print_int ld a0, -8(fp) - srai a0, a0, 1 call print_int mv t0, a0 # End Apply print_int @@ -549,47 +507,7 @@ ( IT MUST BE AT THE END OF THE CRAM TEST ) $ cat results.txt - [DEBUG] alloc_closure(f: 0x106c0, argc: 1) - [DEBUG] my_malloc(size: 32) - -> 0x152a8 - -> 0x152a8 - [DEBUG] alloc_closure(f: 0x106d2, argc: 3) - [DEBUG] my_malloc(size: 48) - -> 0x152d0 - -> 0x152d0 - [Debug] apply_closure(old_clos = { - code: 0x106d2, - argc: 3 - argc_recived: 0 - args = [] - }, argc: 5, args: [0x9, 0x152a8, 0x152a8, 0x7, 0xa1f2b3b0]) - [DEBUG] alloc_closure(f: 0x106d2, argc: 3) - [DEBUG] my_malloc(size: 48) - -> 0x15308 - -> 0x15308 - Runtime error: function accept more arguments than expect + 24 ----- - [DEBUG] alloc_closure(f: 0x106c0, argc: 1) - [DEBUG] my_malloc(size: 32) - -> 0x152a8 - -> 0x152a8 - [DEBUG] alloc_closure(f: 0x1071e, argc: 4) - [DEBUG] my_malloc(size: 56) - -> 0x152d0 - -> 0x152d0 - [DEBUG] alloc_closure(f: 0x107c0, argc: 2) - [DEBUG] my_malloc(size: 40) - -> 0x15310 - -> 0x15310 - [Debug] apply_closure(old_clos = { - code: 0x1071e, - argc: 4 - argc_recived: 0 - args = [] - }, argc: 7, args: [0xd, 0x152a8, 0x15310, 0x10000, 0x152a8, 0xb, 0xcab1d3b0]) - [DEBUG] alloc_closure(f: 0x1071e, argc: 4) - [DEBUG] my_malloc(size: 56) - -> 0x15340 - -> 0x15340 - Runtime error: function accept more arguments than expect + 8 ----- diff --git a/PudgeWithMoML/bin/run_simplyfied.t b/PudgeWithMoML/bin/run_simplyfied.t index cda0176a..a7132b8a 100644 --- a/PudgeWithMoML/bin/run_simplyfied.t +++ b/PudgeWithMoML/bin/run_simplyfied.t @@ -27,18 +27,22 @@ sd fp, 40(sp) addi fp, sp, 56 ld t0, 0(fp) - li t1, 1 + li t1, 3 slt t0, t1, t0 xori t0, t0, 1 sd t0, -24(fp) ld t0, -24(fp) beq t0, zero, L0 - li a0, 1 + li a0, 3 j L1 L0: ld t0, 0(fp) - li t1, 1 + li t1, 3 + srai t0, t0, 1 + srai t1, t1, 1 sub t0, t0, t1 + slli t0, t0, 1 + ori t0, t0, 1 sd t0, -32(fp) ld t0, -32(fp) sd t0, -40(fp) @@ -59,7 +63,11 @@ sd t0, -56(fp) ld t0, 0(fp) ld t1, -56(fp) + srai t0, t0, 1 + srai t1, t1, 1 mul a0, t0, t1 + slli a0, a0, 1 + ori a0, a0, 1 L1: ld ra, 48(sp) ld fp, 40(sp) @@ -74,7 +82,7 @@ # Apply fac__0 with 1 args # Load args on stack addi sp, sp, -16 - li t0, 4 + li t0, 9 sd t0, 0(sp) # End loading args on stack call fac__0 @@ -123,7 +131,7 @@ sd fp, 40(sp) addi fp, sp, 56 ld t0, 0(fp) - li t1, 2 + li t1, 5 slt t0, t0, t1 sd t0, -24(fp) ld t0, -24(fp) @@ -132,8 +140,12 @@ j L1 L0: ld t0, 0(fp) - li t1, 1 + li t1, 3 + srai t0, t0, 1 + srai t1, t1, 1 sub t0, t0, t1 + slli t0, t0, 1 + ori t0, t0, 1 sd t0, -32(fp) # Apply fib__0 with 1 args # Load args on stack @@ -149,8 +161,12 @@ # End Apply fib__0 with 1 args sd t0, -40(fp) ld t0, 0(fp) - li t1, 2 + li t1, 5 + srai t0, t0, 1 + srai t1, t1, 1 sub t0, t0, t1 + slli t0, t0, 1 + ori t0, t0, 1 sd t0, -48(fp) # Apply fib__0 with 1 args # Load args on stack @@ -167,7 +183,11 @@ sd t0, -56(fp) ld t0, -40(fp) ld t1, -56(fp) + srai t0, t0, 1 + srai t1, t1, 1 add a0, t0, t1 + slli a0, a0, 1 + ori a0, a0, 1 L1: ld ra, 48(sp) ld fp, 40(sp) @@ -182,7 +202,7 @@ # Apply fib__0 with 1 args # Load args on stack addi sp, sp, -16 - li t0, 10 + li t0, 21 sd t0, 0(sp) # End loading args on stack call fib__0 @@ -250,7 +270,7 @@ sd ra, 16(sp) sd fp, 8(sp) addi fp, sp, 24 - li t0, 0 + li t0, 1 ld t1, 0(fp) sub t0, t0, t1 snez t0, t0 @@ -258,13 +278,13 @@ ld t0, -24(fp) beq t0, zero, L0 # Apply print_int - li a0, 0 + li a0, 1 call print_int # End Apply print_int j L1 L0: # Apply print_int - li a0, 1 + li a0, 3 call print_int # End Apply print_int L1: @@ -278,28 +298,28 @@ mv a0, sp call init_GC addi sp, sp, -136 - li t0, 0 - li t1, 1 + li t0, 1 + li t1, 3 sub t0, t0, t1 seqz t0, t0 sd t0, -8(fp) ld t0, -8(fp) beq t0, zero, L14 - li t0, 0 - li t1, 1 + li t0, 1 + li t1, 3 sub t0, t0, t1 seqz t0, t0 sd t0, -16(fp) ld t0, -16(fp) beq t0, zero, L6 - li t0, 0 - li t1, 1 + li t0, 1 + li t1, 3 sub t0, t0, t1 seqz t0, t0 sd t0, -24(fp) ld t0, -24(fp) beq t0, zero, L2 - li t0, 0 + li t0, 1 sd t0, -32(fp) # Apply large__0 with 1 args # Load args on stack @@ -315,7 +335,7 @@ # End Apply large__0 with 1 args j L3 L2: - li t0, 1 + li t0, 3 sd t0, -40(fp) # Apply large__0 with 1 args # Load args on stack @@ -332,14 +352,14 @@ L3: j L7 L6: - li t0, 1 - li t1, 1 + li t0, 3 + li t1, 3 sub t0, t0, t1 seqz t0, t0 sd t0, -48(fp) ld t0, -48(fp) beq t0, zero, L4 - li t0, 0 + li t0, 1 sd t0, -56(fp) # Apply large__0 with 1 args # Load args on stack @@ -355,7 +375,7 @@ # End Apply large__0 with 1 args j L5 L4: - li t0, 1 + li t0, 3 sd t0, -64(fp) # Apply large__0 with 1 args # Load args on stack @@ -374,28 +394,28 @@ j L15 L14: # Apply print_int - li a0, 42 + li a0, 85 call print_int mv t0, a0 # End Apply print_int sd t0, -72(fp) ld t0, -72(fp) sd t0, -80(fp) - li t0, 1 - li t1, 1 + li t0, 3 + li t1, 3 sub t0, t0, t1 seqz t0, t0 sd t0, -88(fp) ld t0, -88(fp) beq t0, zero, L12 - li t0, 0 - li t1, 1 + li t0, 1 + li t1, 3 sub t0, t0, t1 seqz t0, t0 sd t0, -96(fp) ld t0, -96(fp) beq t0, zero, L8 - li t0, 0 + li t0, 1 sd t0, -104(fp) # Apply large__0 with 1 args # Load args on stack @@ -411,7 +431,7 @@ # End Apply large__0 with 1 args j L9 L8: - li t0, 1 + li t0, 3 sd t0, -112(fp) # Apply large__0 with 1 args # Load args on stack @@ -428,14 +448,14 @@ L9: j L13 L12: - li t0, 1 - li t1, 1 + li t0, 3 + li t1, 3 sub t0, t0, t1 seqz t0, t0 sd t0, -120(fp) ld t0, -120(fp) beq t0, zero, L10 - li t0, 0 + li t0, 1 sd t0, -128(fp) # Apply large__0 with 1 args # Load args on stack @@ -451,7 +471,7 @@ # End Apply large__0 with 1 args j L11 L10: - li t0, 1 + li t0, 3 sd t0, -136(fp) # Apply large__0 with 1 args # Load args on stack diff --git a/PudgeWithMoML/lib/riscv/codegen.ml b/PudgeWithMoML/lib/riscv/codegen.ml index c0604f57..8202c557 100644 --- a/PudgeWithMoML/lib/riscv/codegen.ml +++ b/PudgeWithMoML/lib/riscv/codegen.ml @@ -87,6 +87,9 @@ let imm_of_literal : literal -> int = function (* Generate code that puts imm value to dst reg *) (* Note: gen_imm overwrite **regs t5 and t6** for internal work *) let gen_imm dst = function + | ImmConst (Int_lt _ as lt) -> + let imm = imm_of_literal lt in + M.return [ li dst (Int.shift_left imm 1 + 1) ] | ImmConst lt -> let imm = imm_of_literal lt in M.return [ li dst imm ] @@ -156,13 +159,13 @@ let%expect_test "even args" = {| # Load args on stack addi sp, sp, -32 - li t0, 5 + li t0, 11 sd t0, 0(sp) - li t0, 2 + li t0, 5 sd t0, 8(sp) - li t0, 1 + li t0, 3 sd t0, 16(sp) - li t0, 4 + li t0, 9 sd t0, 24(sp) # End loading args on stack |}] @@ -180,11 +183,11 @@ let%expect_test "not even args" = {| # Load args on stack addi sp, sp, -32 - li t0, 4 + li t0, 9 sd t0, 0(sp) - li t0, 2 + li t0, 5 sd t0, 8(sp) - li t0, 1 + li t0, 3 sd t0, 16(sp) # End loading args on stack |}] @@ -258,7 +261,7 @@ let%expect_test "alloc_closure_test" = mv t0, a0 addi sp, sp, 16 sd t0, 0(sp) - li t0, 5 + li t0, 11 sd t0, 8(sp) # End loading args on stack call alloc_closure @@ -291,10 +294,46 @@ let rec gen_cexpr (var_arity : string -> int) dst = function | "<" -> c1 @ c2 @ [ slt dst (T 0) (T 1) ] |> return | ">=" -> c1 @ c2 @ [ slt dst (T 0) (T 1); xori dst dst 1 ] |> return | ">" -> c1 @ c2 @ [ slt dst (T 1) (T 0) ] |> return - | "+" -> c1 @ c2 @ [ add dst (T 0) (T 1) ] |> return - | "-" -> c1 @ c2 @ [ sub dst (T 0) (T 1) ] |> return - | "*" -> c1 @ c2 @ [ mul dst (T 0) (T 1) ] |> return - | "/" -> c1 @ c2 @ [ div dst (T 0) (T 1) ] |> return + | "+" -> + c1 + @ c2 + @ [ srai (T 0) (T 0) 1 + ; srai (T 1) (T 1) 1 + ; add dst (T 0) (T 1) + ; slli dst dst 1 + ; ori dst dst 1 + ] + |> return + | "-" -> + c1 + @ c2 + @ [ srai (T 0) (T 0) 1 + ; srai (T 1) (T 1) 1 + ; sub dst (T 0) (T 1) + ; slli dst dst 1 + ; ori dst dst 1 + ] + |> return + | "*" -> + c1 + @ c2 + @ [ srai (T 0) (T 0) 1 + ; srai (T 1) (T 1) 1 + ; mul dst (T 0) (T 1) + ; slli dst dst 1 + ; ori dst dst 1 + ] + |> return + | "/" -> + c1 + @ c2 + @ [ srai (T 0) (T 0) 1 + ; srai (T 1) (T 1) 1 + ; div dst (T 0) (T 1) + ; slli dst dst 1 + ; ori dst dst 1 + ] + |> return | "<>" -> c1 @ c2 @ [ sub dst (T 0) (T 1); snez dst dst ] |> return | "=" -> c1 @ c2 @ [ sub dst (T 0) (T 1); seqz dst dst ] |> return | "&&" -> c1 @ c2 @ [ and_ dst (T 0) (T 1) ] |> return diff --git a/PudgeWithMoML/lib/riscv/machine.ml b/PudgeWithMoML/lib/riscv/machine.ml index b68e7edb..35bbaa22 100644 --- a/PudgeWithMoML/lib/riscv/machine.ml +++ b/PudgeWithMoML/lib/riscv/machine.ml @@ -50,6 +50,10 @@ type instr = | And of reg * reg * reg | Ori of reg * reg * int | Or of reg * reg * reg + | Slli of reg * reg * offset + | Srli of reg * reg * offset + | Slai of reg * reg * offset + | Srai of reg * reg * offset | Beq of reg * reg * string | Ble of reg * reg * string | J of string @@ -85,6 +89,10 @@ let pp_instr fmt = | And (rd, rs1, rs2) -> fprintf fmt "and %a, %a, %a" pp_reg rd pp_reg rs1 pp_reg rs2 | Ori (rd, rs, n) -> fprintf fmt "ori %a, %a, %d" pp_reg rd pp_reg rs n | Or (rd, rs1, rs2) -> fprintf fmt "or %a, %a, %a" pp_reg rd pp_reg rs1 pp_reg rs2 + | Slli (rd, rs1, imm) -> fprintf fmt "slli %a, %a, %d" pp_reg rd pp_reg rs1 imm + | Srli (rd, rs1, imm) -> fprintf fmt "srli %a, %a, %d" pp_reg rd pp_reg rs1 imm + | Slai (rd, rs1, imm) -> fprintf fmt "slai %a, %a, %d" pp_reg rd pp_reg rs1 imm + | Srai (rd, rs1, imm) -> fprintf fmt "srai %a, %a, %d" pp_reg rd pp_reg rs1 imm | Beq (rs1, rs2, label) -> fprintf fmt "beq %a, %a, %s" pp_reg rs1 pp_reg rs2 label | Ble (rs1, rs2, label) -> fprintf fmt "ble %a, %a, %s" pp_reg rs1 pp_reg rs2 label | J label -> fprintf fmt "j %s" label @@ -117,6 +125,10 @@ let andi r1 r2 n = Andi (r1, r2, n) let and_ r1 r2 r3 = And (r1, r2, r3) let ori r1 r2 n = Ori (r1, r2, n) let or_ r1 r2 r3 = Or (r1, r2, r3) +let slli rd rs1 imm = Slli (rd, rs1, imm) +let srli rd rs1 imm = Srli (rd, rs1, imm) +let slai rd rs1 imm = Slai (rd, rs1, imm) +let srai rd rs1 imm = Srai (rd, rs1, imm) let beq r1 r2 label = Beq (r1, r2, label) let ble r1 r2 label = Ble (r1, r2, label) let j label = J label @@ -154,6 +166,10 @@ let%expect_test "Print all regs and instructions" = ; and_ (A 0) (A 1) (A 2) ; ori (A 3) (A 4) 9 ; or_ (S 0) (S 1) (A 0) + ; slai (S 0) (S 1) 4 + ; srai (S 0) (S 1) 4 + ; slli (S 0) (S 1) 4 + ; srli (S 0) (S 1) 4 ; beq (A 0) (A 1) "label1" ; ble (A 2) (A 3) "label2" ; j "main" @@ -189,6 +205,10 @@ let%expect_test "Print all regs and instructions" = and a0, a1, a2 ori a3, a4, 9 or fp, s1, a0 + slai fp, s1, 4 + srai fp, s1, 4 + slli fp, s1, 4 + srli fp, s1, 4 beq a0, a1, label1 ble a2, a3, label2 j main diff --git a/PudgeWithMoML/lib/runtime/runtime.c b/PudgeWithMoML/lib/runtime/runtime.c index 3956096f..05841318 100644 --- a/PudgeWithMoML/lib/runtime/runtime.c +++ b/PudgeWithMoML/lib/runtime/runtime.c @@ -6,7 +6,7 @@ #include #include -#if true +#if false #define LOG(fmt, ...) \ { \ printf(fmt, ##__VA_ARGS__); \ @@ -24,7 +24,10 @@ extern void *call_closure(void *code, uint64_t argc, void **argv); -void print_int(size_t n) { printf("%d\n", n); } +void print_int(size_t n) { + n >>= 1; + printf("%d\n", n); +} void flush() { fflush(stdout); } @@ -441,6 +444,7 @@ static void *copy_closure(closure *old_clos) { // get closure and apply [argc] arguments to closure void *apply_closure(INT8, closure *old_clos, uint8_t argc, ...) { + argc = argc >> 1; void **args = malloc(sizeof(void *) * argc); va_list list; @@ -473,13 +477,13 @@ void *apply_closure(INT8, closure *old_clos, uint8_t argc, ...) { for (size_t i = 0; i < argc; i++) { clos->args[clos->argc_recived++] = args[i]; - free(args); } + free(args); // if application is partial if (clos->argc_recived < clos->argc) { void *result = clos; - LOG(" -> 0x%x\n", result) + LOG(" -> 0x%x\n", result); return result; } diff --git a/PudgeWithMoML/lib/runtime/test b/PudgeWithMoML/lib/runtime/test new file mode 100755 index 0000000000000000000000000000000000000000..f4c7c9fa37c34bcebdea3803bdd013318768991b GIT binary patch literal 19544 zcmeHv3v^V+nP%N?mDEz}(RzWzvw1&^S`Qu;0_+w-r~wfxJYul1n^t#AY9nUMKUzBDLE@3=`n{QINY+L;4DSfP)A6RE1m>yg-IC7 zlc$wH~3QXPYC$na$-E6%DVb25G4C zLNU@+MhOzoodOpMj4=&KmI^_-#-G?3w33)w-_r_1SBt|Dj;cd!D`J9>cw0t{XMNJBWX6bOL2jFX80&+7*ExCY++WHX`sS;S|QT=LG%`;gptZ z!vb$5oF0j5g95*ga7xv+qrfS~>fdeY|N93${lE3}y?(Q0S7Y@^^~;`1m;Ndj;gtUD z4#xi2<|>#gGfwj7C(*&v?kYF)Y@oYYd>U3oGqcYUVPdpx@O;~(FtzUoLjF^43DAGj zbMCFXJ?F-)9^<0t@835I6Be?Rg>-%Le{{o-5EP3S@AmY+F?JZOxX51s#dzTSE&fRU z`sYXxJ>l^5KQitaXdL(SUp(t6b~E8_?8N&M6N4|G7eWJkD=c~XFO7YNs1HbL-wo&4 zX2_2{03y2cGeYL#*#{Fgc1gw~ig7T(SSK0#6yrpKF<&xvD#qyqBO8pfzXm$?_Wyvl zOO4|qtpnUiq653fJ?DOlgkA9rAkM}yP;kjMy_GO>fmHh%#|`M;B>n46TF8P&N;dpS z5*px_m)~ml;@O;p*ujLYzQ&sd%-r;>YaEOIEsbkZUE4SwuKZksR~8 zzoK-ATu?q7&>tiHyAt}}BYlw$%GA2Xo6#3!RF8o%8EVo1;ZH3?gqQe}0QAK>%kWAZ zJ5F}``QwuJdE%i+-p3{Hb08*JJbMKKW4i?pu0=<27wMxa2VMUofARFk(P};o^nu-f z@%Vo#`}idYArqFwIJ|HaQclwT^hm+6C-EBa43v&9CBXlgz->}`8pGf@3NkAQ6 zqm&!fG8nRJ8&AYDAZVaJMsa&&w5;bfC|wmwl~7tLm97dU-M5b-6}VFMuhM9|ivBzu zy;SggS1WSA+y{LKzI&Ay8oABugItIGNtZvSE; zdxjb5eR8{k<)S^HA-zOmoX^EFVKd5g&Fi=@b%5Fey^ z|G6_X<@y1AQO~x}i|N|97}8O}y)VM=$e|)Te_v`)JM>q91{^O?p?NVn=kQuYIZ zz|QmUQj_)jhhjkITm|JStRvTSxori{Uy)U|qvav?5Uc+_>DP3!`VLHg*R- z0hrz(EOeauZe2&_sVm(M&v6er4IiuitqwrAz(dq`4ykK?1)+n{H)8ShH@{@lZ)%Fd zLE|s_0vS=CXnawSF}yKH&(e!|>`(ullEQzwKy=f8A;5`2AMu6nQA7Fjf;fU0A^3eD z@~wPUY7T=iR)Wrc$kH(ZnSWDT@20MhZ5g%8U>Jd{{or!2cr|sO${2u31&t8K~$Ch2sY+4%V)zShLC% zi@0h;gtQRJ!9}mrOrlW5a9D_D$F*$sW3$%&>wsRSgOXaDK#NjI0wXh1(cQP9ue}XD z0=g4ddMO8CD*F`kRCH?FRP?6X&`GwCl`~rBP_cN(8-=9Wj-ElL+*GWM*Pg{MMwC(G1C_{yCg(j>a>OX5`}(UF)$p}ZxRMS&c)VZ#PzQ-c$I zXvSksU~Ho=79`w%z}X!^2L_{c&P4|)4?C%&oxVUI8jQu9kv3;I_^>n9?ehnvL_<6p z4Teoga{7JXgv>+Wb;1!!J&hWl84Nfdjzj}7XDFOvfuyvFNy{v|>foY-bJ8P*=!|<*-eWaF7`@t?Q1W4F$Mvyfra#0kHG!iHVbdCjn_8v*YH(#0|iQ z0dq0Xjsva$y!`u#iDtmqzfMea0{#&2F~A#urvOX;Ffs8e;7{IxKHwNW$GPaLPJF>v z0M5WCyBSbzKQZI*4rUy57&CLTY=e-e=YU>jml4-uK(;B%L9{AduY;z?_CjLea)Ndg zkmWlz=a+3Qus>`&%I==AZu!cE^NCEhI&nRSb}b@8eZJ!}mWI5{{cs4B_>bZ8gFg*0 z!T;za{}jl3!C!~?5`60#gCt1*Ra_?W^EZGAzU66QfcQ6XO@O~6g}*()&xPG;Y?5B{&wFXe!f{E|fckAeR4KTb>> zOSW%0l8EmV__gTIZ>R8|P4Hg@|04K5PT{{S10)(s1}^GD>Ob1gvm9k3M5Bi%%)tLI85qfMi% zMNR#z%J$_+oYq%V^zWcEC7M_V%}_XVxkZiU0aWzd`knVDXz9kB4mW|sZ@L|Y63-M= zY_}-BUOW6dMb~SFGKH5asDWN1jHz{r{!Txm1oZq*uL0E0pKP$mEhIF(O5r;0{Yqc2 z1+;(c7K!#SC6Aplf$O|t`X%r;Rr{2g|7$@1?wszhNA>S#6ntF4rxko&!HWw1LP4_A z)X-4pT#DJyGH3N_tU#-sHB~jMt7@xiolAFMW#aLficwL$jMaavZdG@6&DvFL$H(fT z@o+d4Zg;l({pq@Fl4 zxs6QqTu&uas(2bsC8MAF9jd>llG&H%Chshu;8ZeB$wG#LV{~Y^j*J zlIrw!vYaT7QvF?sTD~-s>ei=lgTY-OIjNqxhNxx5w40Op7Z|Y4EICB=3fwZ@m`=v# z{T}qIyv9a{4h&jj@l_&VW>Aaxo!MUnuo+(>q*fpf!$6byT7i@q|3pZgK%7PstnqaM zS!(=?{T z-=Gf)Zz}l=kPc&rkS!wW?)iUB*0u_yY}jfP6WVMWfZ)T%Kak*d`=TsK%9MfWI-rIzij_!MVUP-vKK#tNf=> zTJR=`t;o-}0a`x^zLm%YPvJ&Ul~Ol?ND<`!h-$-B`#5gaZ_$4&8Gj;Vf#5iewWPC9aF!a+60%4jRm@rsxlB4GW-PY81CZ73XG;qofWd6* zaFrWen7)bI9X9fkZ9S@qZ-Tf&5g*mWBQQ~sMMkr&gNiu4ta6;1@d(|jR5P9>q*^uO z+l15z#9_Qf$jWR|DPsj>Dhs;+3q`Us9|UTwvc5>n&*Em=kWUgTAt4NB(<7fPWEXuM zv1I?zlDC}1L^)Pcc8?~NJ%4fb;k(G#O~HzY`cn zx_h7E*rz-mQ_{vsS0?&Ozxe+4zkGPFqq z&vdjMY}S85V_qZCh8(wuLF|E{W}N&YA}RSgv|lTe;qS3L0>-EqeU_p9mbbtg7Y`1a z#$Q$j?i6MiTXGqujqCZOi|P88fF0}4atlms#Gz)Kywirbo`9i#Vb(hI%n{4i4aWXj z3{vaR@Da<8pgE|Vc355nb0`sl71)oJ=!qlz4P~Fslu)tk2LJQ96oPeV@Q9@v=m{|Z ztV53;$#dDaG@|)DpZ=f2} z1BT^34Yd8gX1W2epDyks`Wi$_=uO^UB5ilD+~?3k_pVo>f0GcsD;rj6^z79mf)6Mm z{rn*l`}E_-zo3{j>!2bM@j!tvGep_9(usoHcVMy3;BM3z6PwzA#>rvwwh7gZdNeMl zRj7rYkxgQdS%&uTb4u@iu&5w_rb0Pd_5;1+B}!9X=JgCWFnrR)oy@Oz5REkKnRNd`{6q6miUdc#~cEH!pgHH?up01MXGVU(dQOwI+QckYi zWuGiJAf6{ZQ#BtS_WbquViXw0mSUnfv~kg%yUWRMA?%A5acUz`itGjn7i-}JqeP1e zbrLO|hoCEwmuXTs3wfEBzoq!@a`HR9*s$AWYv*N?$ubqWNa&LCEye3a%;gC(TuqW^ zOtw?elN&k{9z`k>;mw+qM-*9PC|O~4p|L2x*gk03hm1_?+?jKU>A+fl4tfwxn5()E zkA!ov=!<#9hBY585aCk%^OJ`|p8WvFd~C9VF(06GFt#YQJ)bp@4N_l>jIkvmQ))=z zE#QI==|OkoVIjOMnUUI(<#uB?;=aSh#5hpd)NRECDB&L+rLBIFrVgj^_;9QhTTwR* zo4i{8{9J9 zq}$tc<1<*cML%NJ9D~m~!z*ojc>Z17v6VYt=VjK4_1xO`q^(cJj-g| z!>tR4xiOEIRKYr*@g!eh^6Dpej?Kxf+jxQPK5p&h3oChr$qP<#9_LHQn(Z_%ljf{n z6Y8_8dH##M^jo~>TiijMCwOr@39bb++|FXoTyuJJm(Ok4CE54V{9Of%cZa>3w5Axz>p4H0>D|xPM zD=%ypZMJy$EE#0+Cx|=0lJp%&N}T7KBy|VamL@G_{|3)7dC4}OZL8eJ7b2uHe0eWl zx|L7gz~=+)jguKyCHI)T_H}OW<%>;TdWP5b^5yIKG8nV2)5eOQP727b0-&C9J#HWx27dA4=^6TAf3MTl{_Bfs4Gy(5>oVSSZ3`J8H=`6ADa zKSdeDNXY5^G|&Fee7eb(pCS_9%kB5`a+9w(MFSvfukGbGU?A@aUS#sQqIVbd@&#LY z=>}@M)8si@Iq%`csQi+8tEP%qSmu{dR`Or`7N7P4x4uBh$?v5hlJg45EUDtTFYr72 zcu_AeuxuzHPEjRy_VTJTe4(v~S5lHL@j0?z#2A9^L!>*ehEIQk=Mv`$ZjJL2lUv*P zoIaj^ndij0O)|K36`GELZmBKi*?W0D4a&WHx%GZ}B8;id7B!*?DVzA5}t_V z^~a-Lf21?sg-8*BKN1M~dtfKh8AwDH4MMB+5YDx99ik&E{sWBI;xq&^gI(Q1waXVh z5Ja;@d(jO4fp|AH+uIfm262jlTF1zpI%&bMa!@wNvp6^-5=SzWD|x2IOU)8Cohhv+ zotUpDC==$zhTQ?xz#8W5LNk~;z(a1dOVMBHO-Qse5rxQ1cQn|ex)2PquX=d%mgYv) zi)&VaCi39bM?e^LAlk){)y|;b^hQH5e-HUY{%~@Lk`&V&9Enq(ZHz=ZS)y_98D$s| zN)gFd%@_SkF@^#`FC7Mjp2qai;~U)0~RK^}E;Rkst=VNDOq&QJ_(M;oify@teMIQB))E@hhzbV+*{#FQsFh+vBP zG#+EvD>p-3c(j8t+Gwuqh;#+%cEDFjAy&rl7*w{z1MR_kLuSYJ$oA$m45!5^l@V4M zk3}m(VSi^FnyomKBPk?;&34*E_pVum4eCIL&y+_3E2~#)g*AmpeY>LPhUd5 zs_5w}(_eu;mHmGK-N3I8qto1|rIxAWe-9CZl_vfUQfs^?Dg{jc{hy}i(d3j(XZrU$ zOyov+lpf7Gv3R)AiFNYnRO)+l=VdT_m> zGH%&vIxOf~Y7!18`Sf-9=RtSC{`xfgUnY4APyFtxEe$I>>C0$Z$rov%BzRuQr*9Gc z7<9^~-Z<6m{n>5o&{j0b>pfL1Pd_41{OS35A9QC@9J-o^2f5Ia&)mgsO%iYh(OEvL zRrCzST?RV!uU=Z{)%!|8Pcb2o&9|Y`28;voUro#NgCuXsU=H=%VVNh&yNI9sj#}LU zUo6S2N7>QiM3Gsq@SAbVhU{k*J$;kq^NOCnH%dGBQ}yHD-G+Vv^g=Ap)8n~9@|a7m zSMlpOe?jud!+w|vhFl`V`+q|QGd!x7UBb&Tl=H0aSBlX+2 zVD{?8jKbUK@k*{|(+=isYQAS@MF?pw}qyY=a8 zoXD25#RP@UYs>k$o`ur^@f3DqS{)RVF_ExaCI@nbkfN6~Hxn~!d4fDeBza;drg}8j zh+?XkdTbnB8;f{5eBnSRCS{ncpb??4mu5*xlV>^AOR+DfeJLb#GA2cVPE3&G$z?IU z3HiO&a>U3nrth2-__+TNhMm$Im4bSXm26+6_0IW3?W zm9RtC#1Vq_aNN}z58*E(LILIyW?W|Qpvhb`a*2ZHpqsf!uEU1|{jR{FFl{fu2Z$%V(O=P-`5MP&6>u z8gGYjU$}h|4Takx307+>RyP{MUzP-Q9VymTBBRd}cr@HjbLE(RMg1Q^I*)|CSj=@~ zDO2yiGVF#?Stvo`uD>&y;6eqjewL{S`u(|H>Fe`OT0UL>c$)rx<$u4@KdKcKbfiH# z4v_3$e}6`m{wJ|no?NuPrq!X6tgp{a?8}o93TSbB;ZlnmDCz6`F$Ymllp}qfOA@85 zt`qd&!qNMP)^{p>r_x`lg!OkvLq!(9S73uS;a{Hv^C*2M1&2aupsLoV?N%!H;i6P& z|N2~6MCULEQ9SnMRX?=bEPyc^3`v0BLuMw@M9xwJs=u(QczW%$ftF!<{ z(Q*>I)&HNsNY>ZuxG|;Qq7@a?{x!5fIayz?D{7rmAwAzDn_TFq4B680>v{2dr<6L8 zER4D$C#Z7$DgS09K}1FC)Au2{X#W~cLxt*9$wH_<8kYKvP` zUz{f?o3x-3NY4lTtePewB^OAL9#^{T*4yY;DgEE-fusDV>woMv`b8B|fz~C-C0##o z8~xu_N-a8glU&mEyOn-=JD*r7wRl>>w7!OPB#r9n`d_W?QoM_ub_Nb7uEPZs@3 zt;Fb^n_SXm2~SIr5I?Q-_oxLbt%K?8vwm*Mgp~ZQq@_r39Ct~9^o_K1|MZ!gO8-x8 MssEKUg*24?Ps?gs#sB~S literal 0 HcmV?d00001 diff --git a/PudgeWithMoML/test/GC.t b/PudgeWithMoML/test/GC.t index 69e42267..bcce0e13 100644 --- a/PudgeWithMoML/test/GC.t +++ b/PudgeWithMoML/test/GC.t @@ -11,20 +11,64 @@ > print_int lol > EOF $ qemu-riscv64 -L /usr/riscv64-linux-gnu -cpu rv64 ../main.exe - alloc_closure(0x106c0, 2) - MY MALLOC: 40 - [Debug] apply_closure(old_clos = { - code: 0x106c0, - argc: 2 - argc_recived: 0 - args = [] - }, argc: 3, args: [0xb, 0xfff, 0x10000]) - alloc_closure(0x106c0, 2) - MY MALLOC: 40 - old clos: 0x152a8, new clos: 0x152d8 - clos.code: 0x106c0, clos argc: 0x2 - Runtime error: function accept more arguments than expect - [122] + === GC status === + Start address of new space: 1000 + Allocate count: 2 times + Collect count: 0 times + Current space capacity: 8192 words + Total allocated memory: 12 words + Allocated words in new space: 12 words + Current new space: + (0x1000) 0x0: [size: 5] + (0x1008) 0x1: [data: 0x106c0] + (0x1010) 0x2: [data: 0x2] + (0x1018) 0x3: [data: 0x0] + (0x1020) 0x4: [data: 0x0] + (0x1028) 0x5: [data: 0x0] + (0x1030) 0x6: [size: 5] + (0x1038) 0x7: [data: 0x106c0] + (0x1040) 0x8: [data: 0x2] + (0x1048) 0x9: [data: 0x1] + (0x1050) 0xa: [data: 0xb] + (0x1058) 0xb: [data: 0x0] + === GC status === + === GC status === + Start address of new space: 11000 + Allocate count: 2 times + Collect count: 1 times + Current space capacity: 8192 words + Total allocated memory: 12 words + Allocated words in new space: 6 words + Current new space: + (0x11000) 0x0: [size: 5] + (0x11008) 0x1: [data: 0x106c0] + (0x11010) 0x2: [data: 0x2] + (0x11018) 0x3: [data: 0x1] + (0x11020) 0x4: [data: 0xb] + (0x11028) 0x5: [data: 0x0] + === GC status === + === GC status === + Start address of new space: 11000 + Allocate count: 3 times + Collect count: 1 times + Current space capacity: 8192 words + Total allocated memory: 18 words + Allocated words in new space: 12 words + Current new space: + (0x11000) 0x0: [size: 5] + (0x11008) 0x1: [data: 0x106c0] + (0x11010) 0x2: [data: 0x2] + (0x11018) 0x3: [data: 0x1] + (0x11020) 0x4: [data: 0xb] + (0x11028) 0x5: [data: 0x0] + (0x11030) 0x6: [size: 5] + (0x11038) 0x7: [data: 0x106c0] + (0x11040) 0x8: [data: 0x2] + (0x11048) 0x9: [data: 0x2] + (0x11050) 0xa: [data: 0xb] + (0x11058) 0xb: [data: 0x5] + === GC status === + 7 $ cat ../main.anf let add__0 = fun a__1 -> fun b__2 -> @@ -134,7 +178,6 @@ sd t0, -104(fp) # Apply print_int ld a0, -88(fp) - srai a0, a0, 1 call print_int mv t0, a0 # End Apply print_int @@ -161,22 +204,53 @@ > let main = print_int homs > EOF $ qemu-riscv64 -L /usr/riscv64-linux-gnu -cpu rv64 ../main.exe - alloc_closure(0x106c0, 2) - MY MALLOC: 40 - alloc_closure(0x106f0, 1) - MY MALLOC: 32 - [Debug] apply_closure(old_clos = { - code: 0x106c0, - argc: 2 - argc_recived: 0 - args = [] - }, argc: 3, args: [0x152d8, 0x0, 0x152d8]) - alloc_closure(0x106c0, 2) - MY MALLOC: 40 - old clos: 0x152a8, new clos: 0x15300 - clos.code: 0x106c0, clos argc: 0x2 - Runtime error: function accept more arguments than expect - [122] + === GC status === + Start address of new space: 1000 + Allocate count: 5 times + Collect count: 0 times + Current space capacity: 8192 words + Total allocated memory: 28 words + Allocated words in new space: 28 words + Current new space: + (0x1000) 0x0: [size: 5] + (0x1008) 0x1: [data: 0x106c0] + (0x1010) 0x2: [data: 0x2] + (0x1018) 0x3: [data: 0x0] + (0x1020) 0x4: [data: 0x0] + (0x1028) 0x5: [data: 0x0] + (0x1030) 0x6: [size: 4] + (0x1038) 0x7: [data: 0x106f0] + (0x1040) 0x8: [data: 0x1] + (0x1048) 0x9: [data: 0x0] + (0x1050) 0xa: [data: 0x0] + (0x1058) 0xb: [size: 5] + (0x1060) 0xc: [data: 0x106c0] + (0x1068) 0xd: [data: 0x2] + (0x1070) 0xe: [data: 0x1] + (0x1078) 0xf: [data: 0x142d8] + (0x1080) 0x10: [data: 0x0] + (0x1088) 0x11: [size: 5] + (0x1090) 0x12: [data: 0x106c0] + (0x1098) 0x13: [data: 0x2] + (0x10a0) 0x14: [data: 0x2] + (0x10a8) 0x15: [data: 0x142d8] + (0x10b0) 0x16: [data: 0xb] + (0x10b8) 0x17: [size: 4] + (0x10c0) 0x18: [data: 0x106f0] + (0x10c8) 0x19: [data: 0x1] + (0x10d0) 0x1a: [data: 0x1] + (0x10d8) 0x1b: [data: 0xb] + === GC status === + === GC status === + Start address of new space: 11000 + Allocate count: 5 times + Collect count: 1 times + Current space capacity: 8192 words + Total allocated memory: 28 words + Allocated words in new space: 0 words + Current new space: + === GC status === + 5 $ cat ../main.anf let wrap__0 = fun f__1 -> fun x__2 -> @@ -343,7 +417,6 @@ # Apply print_int la t5, homs__10 ld a0, 0(t5) - srai a0, a0, 1 call print_int mv t0, a0 # End Apply print_int @@ -375,20 +448,76 @@ > let main = print_int 5 > EOF $ qemu-riscv64 -L /usr/riscv64-linux-gnu -cpu rv64 ../main.exe - alloc_closure(0x106c0, 2) - MY MALLOC: 40 - [Debug] apply_closure(old_clos = { - code: 0x106c0, - argc: 2 - argc_recived: 0 - args = [] - }, argc: 3, args: [0xf, 0x106f2, 0x1]) - alloc_closure(0x106c0, 2) - MY MALLOC: 40 - old clos: 0x152a8, new clos: 0x152d8 - clos.code: 0x106c0, clos argc: 0x2 - Runtime error: function accept more arguments than expect - [122] + === GC status === + Start address of new space: 1000 + Allocate count: 2 times + Collect count: 0 times + Current space capacity: 8192 words + Total allocated memory: 12 words + Allocated words in new space: 12 words + Current new space: + (0x1000) 0x0: [size: 5] + (0x1008) 0x1: [data: 0x106c0] + (0x1010) 0x2: [data: 0x2] + (0x1018) 0x3: [data: 0x0] + (0x1020) 0x4: [data: 0x0] + (0x1028) 0x5: [data: 0x0] + (0x1030) 0x6: [size: 5] + (0x1038) 0x7: [data: 0x106c0] + (0x1040) 0x8: [data: 0x2] + (0x1048) 0x9: [data: 0x1] + (0x1050) 0xa: [data: 0xf] + (0x1058) 0xb: [data: 0x0] + === GC status === + === GC status === + Start address of new space: 11000 + Allocate count: 2 times + Collect count: 1 times + Current space capacity: 8192 words + Total allocated memory: 12 words + Allocated words in new space: 0 words + Current new space: + === GC status === + === GC status === + Start address of new space: 11000 + Allocate count: 4 times + Collect count: 1 times + Current space capacity: 8192 words + Total allocated memory: 24 words + Allocated words in new space: 12 words + Current new space: + (0x11000) 0x0: [size: 5] + (0x11008) 0x1: [data: 0x106c0] + (0x11010) 0x2: [data: 0x2] + (0x11018) 0x3: [data: 0x0] + (0x11020) 0x4: [data: 0x0] + (0x11028) 0x5: [data: 0x0] + (0x11030) 0x6: [size: 5] + (0x11038) 0x7: [data: 0x106c0] + (0x11040) 0x8: [data: 0x2] + (0x11048) 0x9: [data: 0x1] + (0x11050) 0xa: [data: 0xd] + (0x11058) 0xb: [data: 0x0] + === GC status === + === GC status === + Start address of new space: 1000 + Allocate count: 4 times + Collect count: 2 times + Current space capacity: 8192 words + Total allocated memory: 24 words + Allocated words in new space: 0 words + Current new space: + === GC status === + === GC status === + Start address of new space: 1000 + Allocate count: 4 times + Collect count: 2 times + Current space capacity: 8192 words + Total allocated memory: 24 words + Allocated words in new space: 0 words + Current new space: + === GC status === + 5 ( move multiple objects to old_space ) $ make compile opts=-gen_mid --no-print-directory -C .. << 'EOF' @@ -404,20 +533,88 @@ > print_int lol > EOF $ qemu-riscv64 -L /usr/riscv64-linux-gnu -cpu rv64 ../main.exe - alloc_closure(0x106c0, 2) - MY MALLOC: 40 - [Debug] apply_closure(old_clos = { - code: 0x106c0, - argc: 2 - argc_recived: 0 - args = [] - }, argc: 3, args: [0xb, 0x0, 0x273aa408]) - alloc_closure(0x106c0, 2) - MY MALLOC: 40 - old clos: 0x152a8, new clos: 0x152d8 - clos.code: 0x106c0, clos argc: 0x2 - Runtime error: function accept more arguments than expect - [122] + === GC status === + Start address of new space: 1000 + Allocate count: 4 times + Collect count: 0 times + Current space capacity: 8192 words + Total allocated memory: 24 words + Allocated words in new space: 24 words + Current new space: + (0x1000) 0x0: [size: 5] + (0x1008) 0x1: [data: 0x106c0] + (0x1010) 0x2: [data: 0x2] + (0x1018) 0x3: [data: 0x0] + (0x1020) 0x4: [data: 0x0] + (0x1028) 0x5: [data: 0x0] + (0x1030) 0x6: [size: 5] + (0x1038) 0x7: [data: 0x106c0] + (0x1040) 0x8: [data: 0x2] + (0x1048) 0x9: [data: 0x1] + (0x1050) 0xa: [data: 0xb] + (0x1058) 0xb: [data: 0x0] + (0x1060) 0xc: [size: 5] + (0x1068) 0xd: [data: 0x106c0] + (0x1070) 0xe: [data: 0x2] + (0x1078) 0xf: [data: 0x0] + (0x1080) 0x10: [data: 0x0] + (0x1088) 0x11: [data: 0x0] + (0x1090) 0x12: [size: 5] + (0x1098) 0x13: [data: 0x106c0] + (0x10a0) 0x14: [data: 0x2] + (0x10a8) 0x15: [data: 0x1] + (0x10b0) 0x16: [data: 0x7] + (0x10b8) 0x17: [data: 0x0] + === GC status === + === GC status === + Start address of new space: 11000 + Allocate count: 4 times + Collect count: 1 times + Current space capacity: 8192 words + Total allocated memory: 24 words + Allocated words in new space: 12 words + Current new space: + (0x11000) 0x0: [size: 5] + (0x11008) 0x1: [data: 0x106c0] + (0x11010) 0x2: [data: 0x2] + (0x11018) 0x3: [data: 0x1] + (0x11020) 0x4: [data: 0xb] + (0x11028) 0x5: [data: 0x0] + (0x11030) 0x6: [size: 5] + (0x11038) 0x7: [data: 0x106c0] + (0x11040) 0x8: [data: 0x2] + (0x11048) 0x9: [data: 0x1] + (0x11050) 0xa: [data: 0x7] + (0x11058) 0xb: [data: 0x0] + === GC status === + === GC status === + Start address of new space: 11000 + Allocate count: 5 times + Collect count: 1 times + Current space capacity: 8192 words + Total allocated memory: 30 words + Allocated words in new space: 18 words + Current new space: + (0x11000) 0x0: [size: 5] + (0x11008) 0x1: [data: 0x106c0] + (0x11010) 0x2: [data: 0x2] + (0x11018) 0x3: [data: 0x1] + (0x11020) 0x4: [data: 0xb] + (0x11028) 0x5: [data: 0x0] + (0x11030) 0x6: [size: 5] + (0x11038) 0x7: [data: 0x106c0] + (0x11040) 0x8: [data: 0x2] + (0x11048) 0x9: [data: 0x1] + (0x11050) 0xa: [data: 0x7] + (0x11058) 0xb: [data: 0x0] + (0x11060) 0xc: [size: 5] + (0x11068) 0xd: [data: 0x106c0] + (0x11070) 0xe: [data: 0x2] + (0x11078) 0xf: [data: 0x2] + (0x11080) 0x10: [data: 0xb] + (0x11088) 0x11: [data: 0x5] + === GC status === + 7 $ cat ../main.anf let add__0 = fun a__1 -> fun b__2 -> @@ -555,7 +752,6 @@ sd t0, -120(fp) # Apply print_int ld a0, -104(fp) - srai a0, a0, 1 call print_int mv t0, a0 # End Apply print_int @@ -575,29 +771,14 @@ > let main = print_int (fib 15 (fun x -> x)) > EOF $ qemu-riscv64 -L /usr/riscv64-linux-gnu -cpu rv64 ../main.exe - alloc_closure(0x1088c, 1) - MY MALLOC: 32 - alloc_closure(0x1070c, 3) - MY MALLOC: 48 - [Debug] apply_closure(old_clos = { - code: 0x1070c, - argc: 3 - argc_recived: 0 - args = [] - }, argc: 3, args: [0x152a8, 0x3, 0x0]) - alloc_closure(0x1070c, 3) - MY MALLOC: 48 - old clos: 0x152d0, new clos: 0x15308 - clos.code: 0x1070c, clos argc: 0x3 - free(): double free detected in tcache 2 - 86489 Aborted (core dumped) qemu-riscv64 -L /usr/riscv64-linux-gnu -cpu rv64 ../main.exe - [134] + panic! overflow memory limits + [122] ( get current capacity of heap ) $ make compile opts=-gen_mid --no-print-directory -C .. << 'EOF' > let start = get_heap_start () > let end = get_heap_fin () - > let main = print_int (start) + > let main = print_int (end - start) > EOF $ qemu-riscv64 -L /usr/riscv64-linux-gnu -cpu rv64 ../main.exe - 2048 + 32768 diff --git a/PudgeWithMoML/test/codegen.t b/PudgeWithMoML/test/codegen.t index af475369..9d699e9a 100644 --- a/PudgeWithMoML/test/codegen.t +++ b/PudgeWithMoML/test/codegen.t @@ -17,7 +17,7 @@ call init_GC addi sp, sp, 0 # Apply print_int - li a0, 5 + li a0, 11 call print_int mv t0, a0 # End Apply print_int @@ -56,7 +56,11 @@ addi fp, sp, 16 ld t0, 0(fp) ld t1, 8(fp) + srai t0, t0, 1 + srai t1, t1, 1 add a0, t0, t1 + slli a0, a0, 1 + ori a0, a0, 1 ld ra, 8(sp) ld fp, 0(sp) addi sp, sp, 16 @@ -70,9 +74,9 @@ # Apply add__0 with 2 args # Load args on stack addi sp, sp, -16 - li t0, 5 + li t0, 11 sd t0, 0(sp) - li t0, 2 + li t0, 5 sd t0, 8(sp) # End loading args on stack call add__0 @@ -145,29 +149,29 @@ # Apply homka__0 with 12 args # Load args on stack addi sp, sp, -96 - li t0, 122 + li t0, 245 sd t0, 0(sp) - li t0, 1 + li t0, 3 sd t0, 8(sp) - li t0, 2 + li t0, 5 sd t0, 16(sp) - li t0, 3 + li t0, 7 sd t0, 24(sp) - li t0, 4 + li t0, 9 sd t0, 32(sp) - li t0, 5 + li t0, 11 sd t0, 40(sp) - li t0, 6 + li t0, 13 sd t0, 48(sp) - li t0, 7 + li t0, 15 sd t0, 56(sp) - li t0, 8 + li t0, 17 sd t0, 64(sp) - li t0, 9 + li t0, 19 sd t0, 72(sp) - li t0, 10 + li t0, 21 sd t0, 80(sp) - li t0, 11 + li t0, 23 sd t0, 88(sp) # End loading args on stack call homka__0 @@ -229,9 +233,9 @@ # Apply id__0 with 2 args # Load args on stack addi sp, sp, -16 - li t0, 5 + li t0, 11 sd t0, 0(sp) - li t0, 5 + li t0, 11 sd t0, 8(sp) # End loading args on stack call id__0 @@ -292,7 +296,7 @@ addi sp, sp, -32 ld t0, -24(fp) sd t0, 0(sp) - li t0, 1 + li t0, 3 sd t0, 8(sp) ld t0, 8(fp) sd t0, 16(sp) @@ -313,8 +317,12 @@ sd fp, 0(sp) addi fp, sp, 16 ld t0, 0(fp) - li t1, 1 + li t1, 3 + srai t0, t0, 1 + srai t1, t1, 1 add a0, t0, t1 + slli a0, a0, 1 + ori a0, a0, 1 ld ra, 8(sp) ld fp, 0(sp) addi sp, sp, 16 @@ -337,7 +345,7 @@ mv t0, a0 addi sp, sp, 16 sd t0, 0(sp) - li t0, 5 + li t0, 11 sd t0, 8(sp) # End loading args on stack call app__0 @@ -376,9 +384,9 @@ mv a0, sp call init_GC addi sp, sp, -32 - li t0, 10 + li t0, 21 sd t0, -8(fp) - li t0, 20 + li t0, 41 sd t0, -16(fp) # Apply print_int ld a0, -16(fp) @@ -430,7 +438,11 @@ addi fp, sp, 16 ld t0, 0(fp) ld t1, 8(fp) + srai t0, t0, 1 + srai t1, t1, 1 add a0, t0, t1 + slli a0, a0, 1 + ori a0, a0, 1 ld ra, 8(sp) ld fp, 0(sp) addi sp, sp, 16 @@ -453,9 +465,9 @@ mv t0, a0 addi sp, sp, 16 sd t0, 0(sp) - li t0, 1 + li t0, 3 sd t0, 8(sp) - li t0, 1 + li t0, 3 sd t0, 16(sp) # End loading args on stack call apply_closure @@ -474,9 +486,9 @@ addi sp, sp, -32 ld t0, -24(fp) sd t0, 0(sp) - li t0, 1 + li t0, 3 sd t0, 8(sp) - li t0, 121 + li t0, 243 sd t0, 16(sp) # End loading args on stack call apply_closure @@ -532,7 +544,11 @@ addi fp, sp, 16 ld t0, 0(fp) ld t1, 8(fp) + srai t0, t0, 1 + srai t1, t1, 1 add a0, t0, t1 + slli a0, a0, 1 + ori a0, a0, 1 ld ra, 8(sp) ld fp, 0(sp) addi sp, sp, 16 @@ -555,9 +571,9 @@ mv t0, a0 addi sp, sp, 16 sd t0, 0(sp) - li t0, 1 + li t0, 3 sd t0, 8(sp) - li t0, 1 + li t0, 3 sd t0, 16(sp) # End loading args on stack call apply_closure @@ -576,9 +592,9 @@ addi sp, sp, -32 ld t0, -24(fp) sd t0, 0(sp) - li t0, 1 + li t0, 3 sd t0, 8(sp) - li t0, 121 + li t0, 243 sd t0, 16(sp) # End loading args on stack call apply_closure @@ -601,9 +617,9 @@ addi sp, sp, -32 ld t0, -48(fp) sd t0, 0(sp) - li t0, 1 + li t0, 3 sd t0, 8(sp) - li t0, 122 + li t0, 245 sd t0, 16(sp) # End loading args on stack call apply_closure @@ -653,14 +669,14 @@ mv a0, sp call init_GC addi sp, sp, 0 - li t0, 4 + li t0, 9 la t1, x__0 sd t0, 0(t1) - li t0, 5 + li t0, 11 la t1, x__1 sd t0, 0(t1) # Apply print_int - li a0, 5 + li a0, 11 call print_int mv t0, a0 # End Apply print_int @@ -706,7 +722,11 @@ addi fp, sp, 16 ld t0, 0(fp) ld t1, 8(fp) + srai t0, t0, 1 + srai t1, t1, 1 add a0, t0, t1 + slli a0, a0, 1 + ori a0, a0, 1 ld ra, 8(sp) ld fp, 0(sp) addi sp, sp, 16 @@ -729,9 +749,9 @@ mv t0, a0 addi sp, sp, 16 sd t0, 0(sp) - li t0, 1 + li t0, 3 sd t0, 8(sp) - li t0, 5 + li t0, 11 sd t0, 16(sp) # End loading args on stack call apply_closure @@ -750,9 +770,9 @@ addi sp, sp, -32 ld t0, -8(fp) sd t0, 0(sp) - li t0, 1 + li t0, 3 sd t0, 8(sp) - li t0, 117 + li t0, 235 sd t0, 16(sp) # End loading args on stack call apply_closure @@ -824,7 +844,11 @@ addi fp, sp, 16 ld t0, 0(fp) ld t1, 8(fp) + srai t0, t0, 1 + srai t1, t1, 1 add a0, t0, t1 + slli a0, a0, 1 + ori a0, a0, 1 ld ra, 8(sp) ld fp, 0(sp) addi sp, sp, 16 @@ -847,9 +871,9 @@ mv t0, a0 addi sp, sp, 16 sd t0, 0(sp) - li t0, 1 + li t0, 3 sd t0, 8(sp) - li t0, 5 + li t0, 11 sd t0, 16(sp) # End loading args on stack call apply_closure @@ -872,9 +896,9 @@ mv t0, a0 addi sp, sp, 16 sd t0, 0(sp) - li t0, 1 + li t0, 3 sd t0, 8(sp) - li t0, 1 + li t0, 3 sd t0, 16(sp) # End loading args on stack call apply_closure @@ -885,15 +909,15 @@ # End Partial application add__0 with 1 args la t1, inc__4 sd t0, 0(t1) - li t0, 17 + li t0, 35 la t1, homka__5 sd t0, 0(t1) # Apply add__0 with 2 args # Load args on stack addi sp, sp, -16 - li t0, 120 + li t0, 241 sd t0, 0(sp) - li t0, 2 + li t0, 5 sd t0, 8(sp) # End loading args on stack call add__0 @@ -912,9 +936,9 @@ addi sp, sp, -32 ld t0, -8(fp) sd t0, 0(sp) - li t0, 1 + li t0, 3 sd t0, 8(sp) - li t0, 110 + li t0, 221 sd t0, 16(sp) # End loading args on stack call apply_closure @@ -983,10 +1007,10 @@ mv a0, sp call init_GC addi sp, sp, -8 - li t0, 5 + li t0, 11 la t1, x__0 sd t0, 0(t1) - li t0, 2 + li t0, 5 sd t0, -8(fp) # Apply print_int ld a0, -8(fp) @@ -1053,10 +1077,10 @@ addi sp, sp, 0 li t0, 1 beq t0, zero, L0 - li t0, 1 + li t0, 3 j L1 L0: - li t0, 2 + li t0, 5 L1: la t1, t__0 sd t0, 0(t1) @@ -1160,7 +1184,7 @@ sd ra, 8(sp) sd fp, 0(sp) addi fp, sp, 16 - li a0, 5 + li a0, 11 ld ra, 8(sp) ld fp, 0(sp) addi sp, sp, 16 @@ -1186,7 +1210,11 @@ sd t0, -48(fp) ld t0, 0(fp) ld t1, 0(fp) + srai t0, t0, 1 + srai t1, t1, 1 div t0, t0, t1 + slli t0, t0, 1 + ori t0, t0, 1 sd t0, -56(fp) ld t0, -56(fp) sd t0, -64(fp) From a54728fff53dd3e935ef4c68c58e3568a1389e61 Mon Sep 17 00:00:00 2001 From: homka122 Date: Mon, 10 Nov 2025 18:39:34 +0300 Subject: [PATCH 17/19] Refactor: delete unused function clear_regs Signed-off-by: homka122 --- PudgeWithMoML/lib/runtime/runtime.c | 15 --------------- PudgeWithMoML/test/GC.t | 5 ++--- 2 files changed, 2 insertions(+), 18 deletions(-) diff --git a/PudgeWithMoML/lib/runtime/runtime.c b/PudgeWithMoML/lib/runtime/runtime.c index 05841318..8ccfc9c1 100644 --- a/PudgeWithMoML/lib/runtime/runtime.c +++ b/PudgeWithMoML/lib/runtime/runtime.c @@ -160,21 +160,6 @@ void init_GC(void *base_sp) { return; } -// clear all registers -void clear_regs() { - // t0-t6 (7), a0-a7 (8), s1-s11 (11) - -// li t0, 0\n\t -// li t1, 0\n\t -// li t2, 0\n\t -// ... -#define X(i, reg, offset) "li " #reg ", 0\n\t" - asm volatile(RISCV_REG_LIST); -#undef X - - return; -} - // if caller function wants to save some regs that my collect_riscv_state // function may destroy (for ex. during creating regs[26]) then caller puts // their values on stack. so we don't lose any address that points to object diff --git a/PudgeWithMoML/test/GC.t b/PudgeWithMoML/test/GC.t index bcce0e13..305af409 100644 --- a/PudgeWithMoML/test/GC.t +++ b/PudgeWithMoML/test/GC.t @@ -442,9 +442,8 @@ > let _5 = print_gc_status () > let _6 = gc_collect () > let _7 = print_gc_status () - > let _8 = clear_regs () - > let _9 = gc_collect () - > let _10 = print_gc_status () + > let _8 = gc_collect () + > let _9 = print_gc_status () > let main = print_int 5 > EOF $ qemu-riscv64 -L /usr/riscv64-linux-gnu -cpu rv64 ../main.exe From bdad8c33eeabf91c371a99ff83835c13b30bf910 Mon Sep 17 00:00:00 2001 From: homka122 Date: Mon, 10 Nov 2025 18:52:18 +0300 Subject: [PATCH 18/19] Fix: get_heap_start\fin functions must return tagged numbers Signed-off-by: homka122 --- PudgeWithMoML/lib/runtime/runtime.c | 8 ++++---- PudgeWithMoML/test/GC.t | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/PudgeWithMoML/lib/runtime/runtime.c b/PudgeWithMoML/lib/runtime/runtime.c index 8ccfc9c1..c7759d45 100644 --- a/PudgeWithMoML/lib/runtime/runtime.c +++ b/PudgeWithMoML/lib/runtime/runtime.c @@ -367,7 +367,7 @@ static void *my_malloc(size_t size) { void **get_heap_start() { LOG("[DEBUG] %s()\n", __func__); if (!STABLE_CI) { - void **result = gc.new_space; + void **result = (void **)(((uintptr_t)gc.new_space) << 1 + 1); LOG(" -> 0x%x\n", result); return result; } @@ -375,7 +375,7 @@ void **get_heap_start() { void **addr = (void **)0x1000; addr += gc.new_space == first_new_space ? 0 : GC_SPACE_INITIAL_SIZE; - void **result = addr; + void **result = (void **)(((uintptr_t)addr) << 1 + 1); LOG(" -> 0x%x\n", result); return result; } @@ -385,7 +385,7 @@ void **get_heap_fin() { LOG("[DEBUG] %s()\n", __func__); if (!STABLE_CI) { - void **result = gc.new_space + gc.space_capacity; + void **result = (void **)(((uintptr_t)(gc.new_space + gc.space_capacity)) << 1 + 1); LOG(" -> 0x%x", result); return result; } @@ -394,7 +394,7 @@ void **get_heap_fin() { addr += gc.new_space == first_new_space ? 0 : GC_SPACE_INITIAL_SIZE; addr += gc.space_capacity; - void **result = addr; + void **result = (void **)(((uintptr_t)addr) << 1 + 1); LOG(" -> 0x%x", result); return result; } diff --git a/PudgeWithMoML/test/GC.t b/PudgeWithMoML/test/GC.t index 305af409..074442c4 100644 --- a/PudgeWithMoML/test/GC.t +++ b/PudgeWithMoML/test/GC.t @@ -780,4 +780,4 @@ > let main = print_int (end - start) > EOF $ qemu-riscv64 -L /usr/riscv64-linux-gnu -cpu rv64 ../main.exe - 32768 + 131072 From e0ff8bc88038b6f38a824a1b8ca9a86d098d2b57 Mon Sep 17 00:00:00 2001 From: homka122 Date: Mon, 10 Nov 2025 18:58:18 +0300 Subject: [PATCH 19/19] Test: add tests for tagged numbers equality to address Signed-off-by: homka122 --- PudgeWithMoML/test/GC.t | 46 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/PudgeWithMoML/test/GC.t b/PudgeWithMoML/test/GC.t index 074442c4..1601d099 100644 --- a/PudgeWithMoML/test/GC.t +++ b/PudgeWithMoML/test/GC.t @@ -781,3 +781,49 @@ > EOF $ qemu-riscv64 -L /usr/riscv64-linux-gnu -cpu rv64 ../main.exe 131072 + +( numbers can't be equal existings addresses on heap ) + $ make compile opts=-gen_mid --no-print-directory -C .. << 'EOF' + > let add x y = x + y + > let homka = add 122 + > let _ = print_gc_status () + > let homka122 = + > let start = get_heap_start () in + > let _ = print_int start in + > let _ = gc_collect () in + > let _ = print_gc_status () in + > 5 + > let main = 5 + > EOF + $ qemu-riscv64 -L /usr/riscv64-linux-gnu -cpu rv64 ../main.exe + === GC status === + Start address of new space: 1000 + Allocate count: 2 times + Collect count: 0 times + Current space capacity: 8192 words + Total allocated memory: 12 words + Allocated words in new space: 12 words + Current new space: + (0x1000) 0x0: [size: 5] + (0x1008) 0x1: [data: 0x106c0] + (0x1010) 0x2: [data: 0x2] + (0x1018) 0x3: [data: 0x0] + (0x1020) 0x4: [data: 0x0] + (0x1028) 0x5: [data: 0x0] + (0x1030) 0x6: [size: 5] + (0x1038) 0x7: [data: 0x106c0] + (0x1040) 0x8: [data: 0x2] + (0x1048) 0x9: [data: 0x1] + (0x1050) 0xa: [data: 0xf5] + (0x1058) 0xb: [data: 0x0] + === GC status === + 8192 + === GC status === + Start address of new space: 11000 + Allocate count: 2 times + Collect count: 1 times + Current space capacity: 8192 words + Total allocated memory: 12 words + Allocated words in new space: 0 words + Current new space: + === GC status ===