From 9d2f9e154bf9b565941ae6f7ea942a2e1b8ec392 Mon Sep 17 00:00:00 2001 From: Enrico Zandomeni Borba Date: Mon, 3 Jun 2024 07:13:34 +0200 Subject: [PATCH 1/4] recursive pretty print --- src/hvm.c | 137 +++++++++++++++++++++++----------------------------- src/hvm.cu | 139 +++++++++++++++++++++++------------------------------ 2 files changed, 121 insertions(+), 155 deletions(-) diff --git a/src/hvm.c b/src/hvm.c index c5ced9ff..b8fa7882 100644 --- a/src/hvm.c +++ b/src/hvm.c @@ -1991,84 +1991,69 @@ void pretty_print_numb(Numb word) { } void pretty_print_port(Net* net, Book* book, Port port) { - Port stack[256]; - stack[0] = port; - u32 len = 1; - u32 num = 0; - while (len > 0) { - Port cur = stack[--len]; - switch (get_tag(cur)) { - case CON: { - Pair node = node_load(net,get_val(cur)); - Port p2 = get_snd(node); - Port p1 = get_fst(node); - printf("("); - stack[len++] = new_port(ERA, (u32)(')')); - stack[len++] = p2; - stack[len++] = new_port(ERA, (u32)(' ')); - stack[len++] = p1; - break; - } - case ERA: { - if (get_val(cur) != 0) { - printf("%c", (char)get_val(cur)); - } else { - printf("*"); - } - break; - } - case VAR: { - Port got = vars_load(net, get_val(cur)); - if (got != NONE) { - stack[len++] = got; - } else { - printf("x%x", get_val(cur)); - } - break; - } - case NUM: { - pretty_print_numb(get_val(cur)); - break; - } - case DUP: { - Pair node = node_load(net,get_val(cur)); - Port p2 = get_snd(node); - Port p1 = get_fst(node); - printf("{"); - stack[len++] = new_port(ERA, (u32)('}')); - stack[len++] = p2; - stack[len++] = new_port(ERA, (u32)(' ')); - stack[len++] = p1; - break; - } - case OPR: { - Pair node = node_load(net,get_val(cur)); - Port p2 = get_snd(node); - Port p1 = get_fst(node); - printf("$("); - stack[len++] = new_port(ERA, (u32)(')')); - stack[len++] = p2; - stack[len++] = new_port(ERA, (u32)(' ')); - stack[len++] = p1; - break; - } - case SWI: { - Pair node = node_load(net,get_val(cur)); - Port p2 = get_snd(node); - Port p1 = get_fst(node); - printf("?("); - stack[len++] = new_port(ERA, (u32)(')')); - stack[len++] = p2; - stack[len++] = new_port(ERA, (u32)(' ')); - stack[len++] = p1; - break; + switch (get_tag(port)) { + case CON: { + Pair node = node_load(net, get_val(port)); + printf("("); + pretty_print_port(net, book, get_fst(node)); + printf(" "); + pretty_print_port(net, book, get_snd(node)); + printf(")"); + return; + } + case ERA: { + if (get_val(port) != 0) { + printf("%c", (char) get_val(port)); + } else { + printf("*"); } - case REF: { - u32 fid = get_val(cur) & 0xFFFFFFF; - Def* def = &book->defs_buf[fid]; - printf("@%s", def->name); - break; + return; + } + case VAR: { + Port val = vars_load(net, get_val(port)); + if (val != NONE) { + pretty_print_port(net, book, val); + } else { + printf("x%x", get_val(port)); } + return; + } + case NUM: { + pretty_print_numb(get_val(port)); + return; + } + case DUP: { + Pair node = node_load(net, get_val(port)); + printf("{"); + pretty_print_port(net, book, get_fst(node)); + printf(" "); + pretty_print_port(net, book, get_snd(node)); + printf("}"); + return; + } + case OPR: { + Pair node = node_load(net, get_val(port)); + printf("$("); + pretty_print_port(net, book, get_fst(node)); + printf(" "); + pretty_print_port(net, book, get_snd(node)); + printf(")"); + return; + } + case SWI: { + Pair node = node_load(net, get_val(port)); + printf("?("); + pretty_print_port(net, book, get_fst(node)); + printf(" "); + pretty_print_port(net, book, get_snd(node)); + printf(")"); + return; + } + case REF: { + u32 fid = get_val(port) & 0xFFFFFFF; + Def* def = &book->defs_buf[fid]; + printf("@%s", def->name); + return; } } } diff --git a/src/hvm.cu b/src/hvm.cu index af9f405b..d2659b28 100644 --- a/src/hvm.cu +++ b/src/hvm.cu @@ -2477,88 +2477,69 @@ __device__ void pretty_print_numb(Numb word) { } __device__ void pretty_print_port(Net* net, Port port) { - Port stack[256]; - stack[0] = port; - u32 len = 1; - while (len > 0) { - if (len > 256) { - printf("ERROR: result too deep to print. This will be fixed soon(TM)"); - --len; - continue; + switch (get_tag(port)) { + case CON: { + Pair node = node_load(net, get_val(port)); + printf("("); + pretty_print_port(net, book, get_fst(node)); + printf(" "); + pretty_print_port(net, book, get_snd(node)); + printf(")"); + return; } - Port cur = stack[--len]; - switch (get_tag(cur)) { - case CON: { - Pair node = node_load(net,get_val(cur)); - Port p2 = get_snd(node); - Port p1 = get_fst(node); - printf("("); - stack[len++] = new_port(ERA, (u32)(')')); - stack[len++] = p2; - stack[len++] = new_port(ERA, (u32)(' ')); - stack[len++] = p1; - break; - } - case ERA: { - if (get_val(cur) != 0) { - printf("%c", (char)get_val(cur)); - } else { - printf("*"); - } - break; - } - case VAR: { - Port got = vars_load(net, get_val(cur)); - if (got != NONE) { - stack[len++] = got; - } else { - printf("x%x", get_val(cur)); - } - break; - } - case NUM: { - pretty_print_numb(get_val(cur)); - break; - } - case DUP: { - Pair node = node_load(net,get_val(cur)); - Port p2 = get_snd(node); - Port p1 = get_fst(node); - printf("{"); - stack[len++] = new_port(ERA, (u32)('}')); - stack[len++] = p2; - stack[len++] = new_port(ERA, (u32)(' ')); - stack[len++] = p1; - break; - } - case OPR: { - Pair node = node_load(net,get_val(cur)); - Port p2 = get_snd(node); - Port p1 = get_fst(node); - printf("$("); - stack[len++] = new_port(ERA, (u32)(')')); - stack[len++] = p2; - stack[len++] = new_port(ERA, (u32)(' ')); - stack[len++] = p1; - break; - } - case SWI: { - Pair node = node_load(net,get_val(cur)); - Port p2 = get_snd(node); - Port p1 = get_fst(node); - printf("?("); - stack[len++] = new_port(ERA, (u32)(')')); - stack[len++] = p2; - stack[len++] = new_port(ERA, (u32)(' ')); - stack[len++] = p1; - break; + case ERA: { + if (get_val(port) != 0) { + printf("%c", (char) get_val(port)); + } else { + printf("*"); } - case REF: { - u32 fid = get_val(cur) & 0xFFFFFFF; - Def* def = &BOOK.defs_buf[fid]; - printf("@%s", def->name); - break; + return; + } + case VAR: { + Port val = vars_load(net, get_val(port)); + if (val != NONE) { + pretty_print_port(net, book, val); + } else { + printf("x%x", get_val(port)); } + return; + } + case NUM: { + pretty_print_numb(get_val(port)); + return; + } + case DUP: { + Pair node = node_load(net, get_val(port)); + printf("{"); + pretty_print_port(net, book, get_fst(node)); + printf(" "); + pretty_print_port(net, book, get_snd(node)); + printf("}"); + return; + } + case OPR: { + Pair node = node_load(net, get_val(port)); + printf("$("); + pretty_print_port(net, book, get_fst(node)); + printf(" "); + pretty_print_port(net, book, get_snd(node)); + printf(")"); + return; + } + case SWI: { + Pair node = node_load(net, get_val(port)); + printf("?("); + pretty_print_port(net, book, get_fst(node)); + printf(" "); + pretty_print_port(net, book, get_snd(node)); + printf(")"); + return; + } + case REF: { + u32 fid = get_val(port) & 0xFFFFFFF; + Def* def = &book->defs_buf[fid]; + printf("@%s", def->name); + return; } } } From d84ef0ed6b7c72c34b4c4d1d3c72c2b0c9cf5e4d Mon Sep 17 00:00:00 2001 From: Enrico Zandomeni Borba Date: Mon, 3 Jun 2024 07:19:21 +0200 Subject: [PATCH 2/4] fix bench --- .github/workflows/bench.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/bench.yml b/.github/workflows/bench.yml index c0933053..ee13e347 100644 --- a/.github/workflows/bench.yml +++ b/.github/workflows/bench.yml @@ -13,7 +13,7 @@ jobs: timeout-minutes: 10 steps: - uses: actions/checkout@v3 - - uses: dsherret/rust-toolchain-file@v1 + - uses: dtolnay/rust-toolchain@nightly - name: compare perf run: | git fetch origin main From 5def50ab5c545e2fdea059fd992ac055c327d4b0 Mon Sep 17 00:00:00 2001 From: Enrico Zandomeni Borba Date: Mon, 3 Jun 2024 07:22:15 +0200 Subject: [PATCH 3/4] fix cu --- src/hvm.cu | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/hvm.cu b/src/hvm.cu index d2659b28..ecf46feb 100644 --- a/src/hvm.cu +++ b/src/hvm.cu @@ -2481,9 +2481,9 @@ __device__ void pretty_print_port(Net* net, Port port) { case CON: { Pair node = node_load(net, get_val(port)); printf("("); - pretty_print_port(net, book, get_fst(node)); + pretty_print_port(net, get_fst(node)); printf(" "); - pretty_print_port(net, book, get_snd(node)); + pretty_print_port(net, get_snd(node)); printf(")"); return; } @@ -2498,7 +2498,7 @@ __device__ void pretty_print_port(Net* net, Port port) { case VAR: { Port val = vars_load(net, get_val(port)); if (val != NONE) { - pretty_print_port(net, book, val); + pretty_print_port(net, val); } else { printf("x%x", get_val(port)); } @@ -2511,33 +2511,33 @@ __device__ void pretty_print_port(Net* net, Port port) { case DUP: { Pair node = node_load(net, get_val(port)); printf("{"); - pretty_print_port(net, book, get_fst(node)); + pretty_print_port(net, get_fst(node)); printf(" "); - pretty_print_port(net, book, get_snd(node)); + pretty_print_port(net, get_snd(node)); printf("}"); return; } case OPR: { Pair node = node_load(net, get_val(port)); printf("$("); - pretty_print_port(net, book, get_fst(node)); + pretty_print_port(net, get_fst(node)); printf(" "); - pretty_print_port(net, book, get_snd(node)); + pretty_print_port(net, get_snd(node)); printf(")"); return; } case SWI: { Pair node = node_load(net, get_val(port)); printf("?("); - pretty_print_port(net, book, get_fst(node)); + pretty_print_port(net, get_fst(node)); printf(" "); - pretty_print_port(net, book, get_snd(node)); + pretty_print_port(net, get_snd(node)); printf(")"); return; } case REF: { u32 fid = get_val(port) & 0xFFFFFFF; - Def* def = &book->defs_buf[fid]; + Def* def = &Book.defs_buf[fid]; printf("@%s", def->name); return; } From a846e5acf19682f5a1b6038c5f6dfd7efac46dbd Mon Sep 17 00:00:00 2001 From: Enrico Zandomeni Borba Date: Mon, 3 Jun 2024 07:23:57 +0200 Subject: [PATCH 4/4] fix cu --- src/hvm.cu | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hvm.cu b/src/hvm.cu index ecf46feb..12b6a88d 100644 --- a/src/hvm.cu +++ b/src/hvm.cu @@ -2537,7 +2537,7 @@ __device__ void pretty_print_port(Net* net, Port port) { } case REF: { u32 fid = get_val(port) & 0xFFFFFFF; - Def* def = &Book.defs_buf[fid]; + Def* def = &BOOK.defs_buf[fid]; printf("@%s", def->name); return; }