diff --git a/PudgeWithMoML/bin/run.t b/PudgeWithMoML/bin/run.t index 277502d8..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) @@ -174,6 +182,8 @@ .globl _start _start: mv fp, sp + mv a0, sp + call init_GC addi sp, sp, -16 addi sp, sp, -16 la t5, f_1 @@ -187,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) @@ -262,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) @@ -271,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) @@ -292,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 @@ -311,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) @@ -326,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) @@ -362,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) @@ -374,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) @@ -387,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 @@ -406,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) @@ -421,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) @@ -465,6 +487,8 @@ .globl _start _start: mv fp, sp + mv a0, sp + call init_GC addi sp, sp, -16 addi sp, sp, -16 la t5, f_2 @@ -478,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) @@ -565,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) @@ -611,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 @@ -624,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 @@ -664,6 +724,8 @@ .globl _start _start: mv fp, sp + mv a0, sp + call init_GC addi sp, sp, -160 # Apply wrap__0 with 11 args addi sp, sp, -16 @@ -679,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 @@ -701,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 @@ -716,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 @@ -731,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 @@ -746,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 @@ -761,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 @@ -776,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 @@ -791,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 @@ -806,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 @@ -821,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 @@ -836,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 @@ -870,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 @@ -892,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 @@ -907,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 @@ -922,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 @@ -936,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 7b1b2702..652842c1 100644 --- a/PudgeWithMoML/bin/run_after_ll.t +++ b/PudgeWithMoML/bin/run_after_ll.t @@ -50,7 +50,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 +63,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 +84,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 +97,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 +110,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 +129,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) @@ -156,11 +164,13 @@ .globl _start _start: mv fp, sp + 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 - li t0, 4 + li t0, 9 sd t0, 0(sp) addi sp, sp, -16 la t5, id__0 @@ -185,7 +195,7 @@ 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 @@ -253,7 +263,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) @@ -262,7 +276,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) @@ -283,8 +297,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 @@ -298,7 +316,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) @@ -319,7 +337,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) @@ -334,7 +352,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) @@ -355,7 +373,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) @@ -367,7 +385,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) @@ -380,8 +398,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 @@ -395,7 +417,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) @@ -439,11 +461,13 @@ .globl _start _start: mv fp, sp + mv a0, sp + call init_GC addi sp, sp, -24 # 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 @@ -470,7 +494,7 @@ 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 diff --git a/PudgeWithMoML/bin/run_simplyfied.t b/PudgeWithMoML/bin/run_simplyfied.t index b09316d0..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) @@ -68,11 +76,13 @@ .globl _start _start: mv fp, sp + mv a0, sp + call init_GC addi sp, sp, -8 # 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 @@ -121,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) @@ -130,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 @@ -147,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 @@ -165,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) @@ -174,11 +196,13 @@ .globl _start _start: mv fp, sp + mv a0, sp + call init_GC addi sp, sp, -8 # 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 @@ -246,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 @@ -254,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: @@ -271,29 +295,31 @@ .globl _start _start: mv fp, sp + 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 @@ -309,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 @@ -326,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 @@ -349,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 @@ -368,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 @@ -405,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 @@ -422,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 @@ -445,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/frontend/inferencer.ml b/PudgeWithMoML/lib/frontend/inferencer.ml index 595b590a..04fadb45 100644 --- a/PudgeWithMoML/lib/frontend/inferencer.ml +++ b/PudgeWithMoML/lib/frontend/inferencer.ml @@ -329,6 +329,11 @@ 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 + 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 @@ -347,6 +352,11 @@ end = struct ; "not", bool_not ; "~-", uminus_s ; "print_int", print_int_s + ; "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 54e71446..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 @@ -304,6 +343,14 @@ 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 "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 @@ -492,6 +539,7 @@ let gather pr : instr list t = @ functions_code @ [ directive ".globl _start"; label "_start" ] @ [ mv fp Sp ] + @ [ mv (A 0) Sp; call "init_GC" ] @ [ addi Sp Sp (-frame) ] @ main_code @ [ call "flush"; li (A 0) 0; li (A 7) 94; ecall ] 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/.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 8ea5782a..c7759d45 100644 --- a/PudgeWithMoML/lib/runtime/runtime.c +++ b/PudgeWithMoML/lib/runtime/runtime.c @@ -1,41 +1,425 @@ #include #include +#include #include #include #include #include +#if false +#define LOG(fmt, ...) \ + { \ + printf(fmt, ##__VA_ARGS__); \ + fflush(stdout); \ + } +#define LOGF(fun) fun +#else +#define LOG(fmt, ...) ((void)0) +#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(int n) { printf("%d\n", n); } +void print_int(size_t n) { + n >>= 1; + 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) + +// 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 allocated_bytes_count; + size_t collect_count; +} GC_state; + +static GC_state gc; +void *first_new_space; // for stable CI + 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 +static 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", 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); + 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]; + + 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: ", addr + i + 1, offset); + printf("[data: 0x%x]\n", gc.new_space[offset]); + offset++; + } + } + + printf("=== GC status ===\n"); + + void *current_sp = NULL; + asm volatile("mv %0, sp" : "=r"(current_sp)); + LOGF(print_stack(current_sp)); + + 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); + first_new_space = gc.new_space; + 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); + + // 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) { +#define X(i, reg, offset) \ + case i: \ + asm volatile("mv " #reg ", %0" ::"r"(val)); + RISCV_REG_LIST +#undef X + default: + break; + } +} + +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; + 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 +// 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 +static void _gc_collect(void *current_sp) { + if (gc.alloc_offset == 0) { + return; + } + + 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; + 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); + + // 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 - 1; + 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 + 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]; + } + 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 - 1; + if (*byte == cur_pointer) { + LOG("Change stack cell 0x%x. 0x%x -> 0x%x\n", byte, *byte, new_pointer); + *byte = new_pointer; + } + } + } + + cur_offset += cur_size + 1; + } + LOGF(print_stack(current_sp)); + + 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 +static void *my_malloc(size_t size) { + LOG("[DEBUG] %s(size: %ld)\n", __func__, 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) { + LOG("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); + } + } + + gc.new_space[gc.alloc_offset++] = (void *)words; + void **result = gc.new_space + gc.alloc_offset; + + 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) { + void **result = (void **)(((uintptr_t)gc.new_space) << 1 + 1); + LOG(" -> 0x%x\n", result); + return result; + } + + void **addr = (void **)0x1000; + addr += gc.new_space == first_new_space ? 0 : GC_SPACE_INITIAL_SIZE; + + void **result = (void **)(((uintptr_t)addr) << 1 + 1); + 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) { + void **result = (void **)(((uintptr_t)(gc.new_space + gc.space_capacity)) << 1 + 1); + 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; + + void **result = (void **)(((uintptr_t)addr) << 1 + 1); + LOG(" -> 0x%x", result); + return result; +} + void *alloc_closure(INT8, void *f, uint8_t argc) { - closure *clos = malloc(sizeof(closure) + sizeof(void *) * 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; clos->argc = 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) { +static 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]; } @@ -43,32 +427,54 @@ 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); + argc = argc >> 1; + 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] %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]); + } + 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(stderr, "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]; } - va_end(list); + free(args); // 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); } diff --git a/PudgeWithMoML/lib/runtime/test b/PudgeWithMoML/lib/runtime/test new file mode 100755 index 00000000..f4c7c9fa Binary files /dev/null and b/PudgeWithMoML/lib/runtime/test differ diff --git a/PudgeWithMoML/test/GC.t b/PudgeWithMoML/test/GC.t new file mode 100644 index 00000000..1601d099 --- /dev/null +++ b/PudgeWithMoML/test/GC.t @@ -0,0 +1,829 @@ +( 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: 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 -> + 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) + 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 + 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, 3 + sd t0, 8(sp) + li t0, 11 + 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, 3 + 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 + 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 + > 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 + === 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 -> + 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, 3 + 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, 3 + 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, 3 + sd t0, 8(sp) + li t0, 11 + 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 + mv a0, sp + call init_GC + addi sp, sp, 0 + # Apply homka__5 with 3 args + # Load args on stack + addi sp, sp, -32 + li t0, 5 + 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 = gc_collect () + > let _9 = print_gc_status () + > 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: 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' + > 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 + === 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 -> + 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) + 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 + 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, 3 + sd t0, 8(sp) + li t0, 11 + 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, 3 + sd t0, 8(sp) + li t0, 7 + 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, 3 + 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 + 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] + +( 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 (end - start) + > 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 === diff --git a/PudgeWithMoML/test/codegen.t b/PudgeWithMoML/test/codegen.t index ce72fc4b..9d699e9a 100644 --- a/PudgeWithMoML/test/codegen.t +++ b/PudgeWithMoML/test/codegen.t @@ -13,9 +13,11 @@ .globl _start _start: mv fp, sp + mv a0, sp + 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 @@ -54,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 @@ -62,13 +68,15 @@ .globl _start _start: mv fp, sp + mv a0, sp + call init_GC addi sp, sp, -8 # 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 @@ -135,33 +143,35 @@ .globl _start _start: mv fp, sp + mv a0, sp + call init_GC addi sp, sp, -8 # 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 @@ -217,13 +227,15 @@ .globl _start _start: mv fp, sp + mv a0, sp + call init_GC addi sp, sp, -8 # 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 @@ -284,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) @@ -305,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 @@ -314,6 +330,8 @@ .globl _start _start: mv fp, sp + mv a0, sp + call init_GC addi sp, sp, -8 # Apply app__0 with 2 args # Load args on stack @@ -327,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 @@ -363,10 +381,12 @@ .globl _start _start: mv fp, sp + 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) @@ -418,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 @@ -426,6 +450,8 @@ .globl _start _start: mv fp, sp + mv a0, sp + call init_GC addi sp, sp, -32 # Partial application add__0 with 1 args # Load args on stack @@ -439,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 @@ -460,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 @@ -518,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 @@ -526,6 +556,8 @@ .globl _start _start: mv fp, sp + mv a0, sp + call init_GC addi sp, sp, -56 # Partial application add__0 with 1 args # Load args on stack @@ -539,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 @@ -560,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 @@ -585,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 @@ -634,15 +666,17 @@ .globl _start _start: mv fp, sp + 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 @@ -688,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 @@ -696,6 +734,8 @@ .globl _start _start: mv fp, sp + mv a0, sp + call init_GC addi sp, sp, -16 # Partial application add__0 with 1 args # Load args on stack @@ -709,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 @@ -730,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 @@ -804,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 @@ -812,6 +856,8 @@ .globl _start _start: mv fp, sp + mv a0, sp + call init_GC addi sp, sp, -32 # Partial application add__0 with 1 args # Load args on stack @@ -825,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 @@ -850,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 @@ -863,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 @@ -890,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 @@ -958,11 +1004,13 @@ .globl _start _start: mv fp, sp + 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) @@ -1024,13 +1072,15 @@ .globl _start _start: mv fp, sp + mv a0, sp + call init_GC 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) @@ -1134,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 @@ -1160,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) @@ -1185,6 +1239,8 @@ .globl _start _start: mv fp, sp + mv a0, sp + call init_GC addi sp, sp, 0 call flush li a0, 0 @@ -1225,6 +1281,7 @@ 3 5 + ( IT MUST BE AT THE END OF THE CRAM TEST ) $ cat results.txt 5 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/*)))