From e3baa868f73eebbf5eca10fea2f0c3d943bd33fe Mon Sep 17 00:00:00 2001 From: dzaima Date: Wed, 6 Apr 2022 22:38:51 +0300 Subject: [PATCH] put in a bit more effort printing messages while things are really bad --- makefile | 2 +- src/load.c | 2 ++ src/vm.c | 44 ++++++++++++++++++++++++++++---------------- 3 files changed, 31 insertions(+), 17 deletions(-) diff --git a/makefile b/makefile index 41e5e3e1..78b8af88 100644 --- a/makefile +++ b/makefile @@ -29,7 +29,7 @@ heapverifyn-singeli: rtverifyn-singeli: @${MAKE} singeli=1 t=rtverifyn_si f="-O3 -DRT_VERIFY -DEEQUAL_NEGZERO -march=native" c wasi-o3: - @${MAKE} singeli=0 t=wasi_o3 f="-DWASM -O3 -DCATCH_ERRORS=0 -D_WASI_EMULATED_MMAN --target=wasm32-wasi" LDFLAGS="-lwasi-emulated-mman --target=wasm32-wasi" LD_LIBS= PIE= c + @${MAKE} singeli=0 t=wasi_o3 f="-DWASM -DWASI -O3 -DCATCH_ERRORS=0 -D_WASI_EMULATED_MMAN --target=wasm32-wasi" LDFLAGS="-lwasi-emulated-mman --target=wasm32-wasi" LD_LIBS= PIE= c emcc-o3: @${MAKE} singeli=0 t=emcc_o3 f='-DWASM -DEMCC -O3' LDFLAGS='-s EXPORTED_FUNCTIONS=_main,_cbqn_runLine -s EXPORTED_RUNTIME_METHODS=ccall,cwrap -s ALLOW_MEMORY_GROWTH=1' CC=emcc c diff --git a/src/load.c b/src/load.c index 0d03fd15..7e64805f 100644 --- a/src/load.c +++ b/src/load.c @@ -667,9 +667,11 @@ void base_init() { // very first init function #undef FD } +bool cbqn_initialized; void cbqn_init() { #define F(X) X##_init(); FOR_INIT(F) #undef F + cbqn_initialized = true; } #undef FOR_INIT diff --git a/src/vm.c b/src/vm.c index c2a6047c..a9e02e3b 100644 --- a/src/vm.c +++ b/src/vm.c @@ -1144,6 +1144,7 @@ NOINLINE B vm_fmtPoint(B src, B prepend, B path, usz cs, usz ce) { // consumes p return s; } +extern bool cbqn_initialized; NOINLINE void vm_printPos(Comp* comp, i32 bcPos, i64 pos) { B src = comp->src; if (!q_N(src) && !q_N(comp->indices)) { @@ -1151,25 +1152,33 @@ NOINLINE void vm_printPos(Comp* comp, i32 bcPos, i64 pos) { B inde = IGetU(comp->indices, 1); usz ce = o2s(IGetU(inde,bcPos))+1; // printf(" bcPos=%d\n", bcPos); // in case the pretty error generator is broken // printf(" inds:%d…%d\n", cs, ce); - if (CATCH) { // want to try really hard to print errors - freeThrown(); - int start = fprintf(stderr, "at "); - usz srcL = a(src)->ia; - SGetU(src) - usz srcS = cs; while (srcS>0 && o2cu(GetU(src,srcS-1))!='\n') srcS--; - usz srcE = srcS; while (srcEsrcE) ce = srcE; - cs-= srcS; ce-= srcS; - fputc('\n', stderr); - for (i32 i = 0; i < cs+start; i++) fputc(' ', stderr); - for (i32 i = cs; i < ce; i++) fputc('^', stderr); - fputc('\n', stderr); - return; - } + + + // want to try really hard to print errors + if (!cbqn_initialized) goto native_print; + if (CATCH) goto native_print; + B s = emptyCVec(); fprintRaw(stderr, vm_fmtPoint(src, s, comp->path, cs, ce)); fputc('\n', stderr); popCatch(); + return; + +native_print: + freeThrown(); + int start = fprintf(stderr, "at "); + usz srcL = a(src)->ia; + SGetU(src) + usz srcS = cs; while (srcS>0 && o2cu(GetU(src,srcS-1))!='\n') srcS--; + usz srcE = srcS; while (srcEsrcE) ce = srcE; + cs-= srcS; ce-= srcS; + fputc('\n', stderr); + for (i32 i = 0; i < cs+start; i++) fputc(' ', stderr); + for (i32 i = cs; i < ce; i++) fputc('^', stderr); + fputc('\n', stderr); + return; + //print_BCStream((u32*)i32arr_ptr(comp->bc)+bcPos); } else { #ifdef DEBUG @@ -1278,4 +1287,7 @@ NOINLINE void freeThrown() { NOINLINE NORETURN void thrM(char* s) { thr(fromUTF8(s, strlen(s))); } -NOINLINE NORETURN void thrOOM() { thr(inc(oomMessage)); } +NOINLINE NORETURN void thrOOM() { + if (oomMessage.u==0) err("out-of-memory encountered before out-of-memory error message object was initialized"); + thr(inc(oomMessage)); +}