From 7357820f19dc3524e41d5b689b5060d8e2faf9b7 Mon Sep 17 00:00:00 2001 From: dzaima Date: Sat, 25 Nov 2023 23:21:23 +0200 Subject: [PATCH] start of compiler state refactoring --- build/src/clangd.bqn | 2 +- src/builtins/sysfn.c | 31 +++++++------- src/core/stuff.h | 5 --- src/load.c | 99 +++++++++++++++++++------------------------ src/load.h | 16 +++++++ src/vm.c | 3 +- test/cases/system.bqn | 8 ++++ 7 files changed, 86 insertions(+), 78 deletions(-) create mode 100644 src/load.h diff --git a/build/src/clangd.bqn b/build/src/clangd.bqn index 56f65ce0..c213478c 100644 --- a/build/src/clangd.bqn +++ b/build/src/clangd.bqn @@ -46,7 +46,7 @@ ModifyCBQNFlags ← { # non-'utils/' headers & optional .c files {𝕩 WantsIncludes ⟨"core.h"⟩}¨ ⟨ - "ns.h", "vm.h", "builtins.h" + "ns.h", "vm.h", "builtins.h", "load.h" "opt/mm_malloc.c", "opt/mm_buddy.c", "opt/mm_2buddy.c", "opt/mm_buddyTemplate.c", "opt/mm_buddyTemplate.h", "opt/gc.c", "opt/gc.h" "builtins/radix.h", "singeli/c/arithdDispatch.h" "jit/nvm.h" diff --git a/src/builtins/sysfn.c b/src/builtins/sysfn.c index 4ebef478..876d2e78 100644 --- a/src/builtins/sysfn.c +++ b/src/builtins/sysfn.c @@ -6,6 +6,7 @@ #include "../builtins.h" #include "../ns.h" #include "../nfns.h" +#include "../load.h" #include #if defined(_WIN32) || defined(_WIN64) @@ -224,21 +225,21 @@ B casrt_c2(B t, B w, B x) { B s = IGet(w,1); AFMT("\n"); usz pos = o2s(w0); - s = vm_fmtPoint(comp_currSrc, s, comp_currPath, pos, pos+1); + s = vm_fmtPoint(COMPS_CREF(src), s, COMPS_CREF(path), pos, pos+1); dec(w); thr(s); } if (isArr(w0) && RNK(w0)==1 && IA(w0)>=1) { B s = IGet(w,1); AFMT("\n"); usz pos = o2s(IGetU(w0,0)); - s = vm_fmtPoint(comp_currSrc, s, comp_currPath, pos, pos+1); + s = vm_fmtPoint(COMPS_CREF(src), s, COMPS_CREF(path), pos, pos+1); dec(w); thr(s); } if (isArr(w0) && RNK(w0)==2 && IA(w0)>=2) { B s = IGet(w,1); AFMT("\n"); SGetU(w0) - s = vm_fmtPoint(comp_currSrc, s, comp_currPath, o2s(GetU(w0,0)), o2s(GetU(w0,1))+1); + s = vm_fmtPoint(COMPS_CREF(src), s, COMPS_CREF(path), o2s(GetU(w0,0)), o2s(GetU(w0,1))+1); dec(w); thr(s); } @@ -1591,8 +1592,6 @@ B getBitNS(void) { B getInternalNS(void); B getMathNS(void); -B getPrimitives(void); -void getSysvals(B* res); static Body* file_nsGen; @@ -1760,7 +1759,7 @@ static B getPlatformNS(void) { B sys_c1(B t, B x) { assert(isArr(x)); - B tmp[2]; getSysvals(tmp); + B tmp[2]; comps_getSysvals(tmp); B curr_ns = tmp[0]; B curr_vs = tmp[1]; SGetU(curr_vs) B idxs = C2(indexOf, incG(curr_ns), incG(x)); SGetU(idxs) @@ -1769,8 +1768,8 @@ B sys_c1(B t, B x) { B path = m_f64(0); B name = m_f64(0); B wdpath = m_f64(0); - #define REQ_PATH ({ if(!path.u) path = q_N(comp_currPath)? bi_N : path_abs(path_parent(inc(comp_currPath))); path; }) - #define REQ_NAME ({ if(!name.u) name = path_name(inc(comp_currPath)); name; }) + #define REQ_PATH ({ if(!path.u) path = q_N(COMPS_CREF(path))? bi_N : path_abs(path_parent(inc(COMPS_CREF(path)))); path; }) + #define REQ_NAME ({ if(!name.u) name = path_name(inc(COMPS_CREF(path))); name; }) M_HARR(r, IA(x)) for (usz i = 0; i < IA(x); i++) { @@ -1785,15 +1784,15 @@ B sys_c1(B t, B x) { case 1: cr = getRandNS(); break; // •rand case 2: cr = getTermNS(); break; // •term case 3: cr = getBitNS(); break; // •bit - case 4: cr = getPrimitives(); break; // •primitives + case 4: cr = comps_getPrimitives(); break; // •primitives case 5: cr = getInternalNS(); break; // •internal case 6: initFileNS(); cr = m_nfn(fCharsDesc, inc(REQ_PATH)); break; // •FChars case 7: initFileNS(); cr = m_nfn(fBytesDesc, inc(REQ_PATH)); break; // •FBytes case 8: initFileNS(); cr = m_nfn(fLinesDesc, inc(REQ_PATH)); break; // •FLines case 9: initFileNS(); cr = m_nfn(importDesc, inc(REQ_PATH)); break; // •Import case 10: initFileNS(); cr = m_nfn(ffiloadDesc, inc(REQ_PATH)); break; // •FFI - case 11: if (q_N(comp_currPath)) thrM("No path present for •name"); cr = inc(REQ_NAME); break; // •name - case 12: if (q_N(comp_currPath)) thrM("No path present for •path"); cr = inc(REQ_PATH); break; // •path + case 11: if (q_N(COMPS_CREF(path))) thrM("No path present for •name"); cr = inc(REQ_NAME); break; // •name + case 12: if (q_N(COMPS_CREF(path))) thrM("No path present for •path"); cr = inc(REQ_PATH); break; // •path case 13: { // •wdpath if (!wdpath.u) wdpath = path_abs(inc(cdPath)); cr = inc(wdpath); @@ -1811,14 +1810,14 @@ B sys_c1(B t, B x) { break; } case 15: { // •state - if (q_N(comp_currArgs)) thrM("No arguments present for •state"); - if (q_N(comp_currPath)) thrM("No path present for •state"); - cr = m_hvec3(inc(REQ_PATH), inc(REQ_NAME), inc(comp_currArgs)); + if (q_N(COMPS_CREF(args))) thrM("No arguments present for •state"); + if (q_N(COMPS_CREF(path))) thrM("No path present for •state"); + cr = m_hvec3(inc(REQ_PATH), inc(REQ_NAME), inc(COMPS_CREF(args))); break; } case 16: { // •args - if (q_N(comp_currArgs)) thrM("No arguments present for •args"); - cr = inc(comp_currArgs); + if (q_N(COMPS_CREF(args))) thrM("No arguments present for •args"); + cr = inc(COMPS_CREF(args)); break; } case 17: cr = incG(curr_ns); break; // •listsys diff --git a/src/core/stuff.h b/src/core/stuff.h index 33c768ca..2df6f6d5 100644 --- a/src/core/stuff.h +++ b/src/core/stuff.h @@ -384,8 +384,3 @@ FORCE_INLINE void preFree(Value* x, bool mmx) { #endif // x->refc = 0x61616161; } - -extern i64 comp_currEnvPos; -extern B comp_currPath; -extern B comp_currArgs; -extern B comp_currSrc; diff --git a/src/load.c b/src/load.c index b526f3f0..82ce61e6 100644 --- a/src/load.c +++ b/src/load.c @@ -3,6 +3,7 @@ #include "vm.h" #include "ns.h" #include "builtins.h" +#include "load.h" #ifndef FAKE_RUNTIME #define FAKE_RUNTIME 0 @@ -102,12 +103,7 @@ B rtWrap_wrap(B x, bool nnbi); // consumes void rtWrap_print(void); -// comp_currEnvPos/comp_currPath/comp_currArgs/comp_currSrc are only valid while evaluating through bqn_comp*; comp_currRe is valid at all times -i64 comp_currEnvPos; -B comp_currPath; -B comp_currArgs; -B comp_currSrc; -B comp_currRe; // ⟨REPL mode ⋄ scope ⋄ compiler ⋄ runtime ⋄ glyphs ⋄ sysval names ⋄ sysval values⟩ +HArr* comps_curr; B rt_undo, rt_select, rt_slash, rt_insert, rt_depth, rt_group, rt_under, rt_find; @@ -178,33 +174,38 @@ B bqn_repr(B x) { } #endif -#define POP_COMP ({ \ - comp_currPath = prevPath; \ - comp_currArgs = prevArgs; \ - comp_currSrc = prevSrc; \ - comp_currEnvPos = prevEnvPos; }) +NOINLINE HArr* m_comps(B path, B args, B src, B re, i64 envPos) { + HArr* r = m_harr0v(comps_max).c; + COMPS_REF(r, path) = inc(path); + COMPS_REF(r, args) = inc(args); + COMPS_REF(r, src) = inc(src); + COMPS_REF(r, re) = inc(re); + COMPS_REF(r, envPos) = m_f64(envPos); + return r; +} -#define PUSH_COMP \ - B prevPath = comp_currPath ; comp_currPath = path; \ - B prevArgs = comp_currArgs ; comp_currArgs = args; \ - B prevSrc = comp_currSrc ; comp_currSrc = str; \ - i64 prevEnvPos = comp_currEnvPos; comp_currEnvPos = envCurr-envStart; \ - if (CATCH) { POP_COMP; rethrow(); } +#define COMPS_PUSH(PATH, ARGS, STR, RE) \ + HArr* compsP = comps_curr; \ + HArr* compsN = m_comps(PATH, ARGS, STR, RE, envCurr-envStart); \ + comps_curr = compsN; \ + if (CATCH) { COMPS_POP; rethrow(); } -static NOINLINE Block* bqn_compc(B str, B path, B args, B comp, B compArg) { // consumes str,path,args +#define COMPS_POP ({ comps_curr = compsP; ptr_dec(compsN); }) + +static NOINLINE Block* bqn_compc(B str, B path, B args, B re, B comp, B compArg) { // consumes str,path,args str = chr_squeeze(str); - PUSH_COMP; + COMPS_PUSH(path, args, str, re); Block* r = load_compObj(c2G(comp, incG(compArg), inc(str)), str, path, NULL, 0); dec(path); dec(args); - POP_COMP; popCatch(); + COMPS_POP; popCatch(); return r; } Block* bqn_comp(B str, B path, B args) { // consumes all - return bqn_compc(str, path, args, load_comp, load_compArg); + return bqn_compc(str, path, args, bi_N, load_comp, load_compArg); } -Block* bqn_compScc(B str, B path, B args, Scope* sc, B comp, B rt, bool loose, bool noNS) { // consumes str,path,args +Block* bqn_compScc(B str, B path, B args, B re, B comp, B rt, Scope* sc, bool loose, bool noNS) { // consumes str,path,args str = chr_squeeze(str); - PUSH_COMP; + COMPS_PUSH(path, args, str, re); B vName = emptyHVec(); B vDepth = emptyIVec(); if (loose && (!sc || sc->psc)) thrM("VM compiler: REPL mode must be used at top level scope"); @@ -220,11 +221,11 @@ Block* bqn_compScc(B str, B path, B args, Scope* sc, B comp, B rt, bool loose, b } Block* r = load_compObj(c2G(comp, m_hvec4(incG(rt), incG(bi_sys), vName, vDepth), inc(str)), str, path, sc, sc!=NULL? (noNS? -1 : 1) : 0); dec(path); dec(args); - POP_COMP; popCatch(); + COMPS_POP; popCatch(); return r; } NOINLINE Block* bqn_compSc(B str, B path, B args, Scope* sc, bool repl) { // consumes str,path,args - return bqn_compScc(str, path, args, sc, load_comp, load_rtObj, repl, false); + return bqn_compScc(str, path, args, bi_N, load_comp, load_rtObj, sc, repl, false); } B bqn_exec(B str, B path, B args) { // consumes all @@ -306,10 +307,10 @@ void init_comp(B* set, B prim, B sys) { } } } -B getPrimitives(void) { +B comps_getPrimitives(void) { B g, r; - if (q_N(comp_currRe)) { g=load_glyphs; r=load_rtObj; } - else { B* o = harr_ptr(comp_currRe); g=o[4]; r=o[3]; } + if (q_N(COMPS_CREF(re))) { g=load_glyphs; r=load_rtObj; } + else { B* o = harr_ptr(COMPS_CREF(re)); g=o[4]; r=o[3]; } B* pr = harr_ptr(r); B* gg = harr_ptr(g); M_HARR(ph, IA(r)); @@ -324,39 +325,30 @@ B getPrimitives(void) { return HARR_FV(ph); } -extern B dsv_ns, dsv_vs; -void getSysvals(B* res) { - if (q_N(comp_currRe)) { res[0]=dsv_ns; res[1]=dsv_vs; } - else { B* o=harr_ptr(comp_currRe); res[0]=o[5]; res[1]=o[6]; } +void comps_getSysvals(B* res) { + B re = COMPS_CREF(re); + if (q_N(re)) { res[0]=dsv_ns; res[1]=dsv_vs; } + else { B* o=harr_ptr(re); res[0]=o[5]; res[1]=o[6]; } } -B rebqn_exec(B str, B path, B args, B o) { - B prevRe = comp_currRe; - if (CATCH) { comp_currRe = prevRe; rethrow(); } - comp_currRe = inc(o); - - B* op = harr_ptr(o); +B rebqn_exec(B str, B path, B args, B re) { + B* op = harr_ptr(re); i32 replMode = o2iG(op[0]); Scope* sc = c(Scope, op[1]); B res; Block* block; if (replMode>0) { - block = bqn_compScc(str, path, args, sc, op[2], op[3], replMode==2, true); - comp_currRe = prevRe; + block = bqn_compScc(str, path, args, re, op[2], op[3], sc, replMode==2, true); ptr_dec(sc->body); sc->body = ptr_inc(block->bodies[0]); res = execBlockInplace(block, sc); } else { B rtsys = m_hvec2(inc(op[3]), incG(bi_sys)); - block = bqn_compc(str, path, args, op[2], rtsys); + block = bqn_compc(str, path, args, re, op[2], rtsys); decG(rtsys); - comp_currRe = prevRe; res = evalFunBlock(block, 0); } ptr_dec(block); - decG(o); - - popCatch(); return res; } @@ -372,15 +364,12 @@ static NOINLINE B m_lvi32_3(i32 a, i32 b, i32 c ) { i32* rp; B r = m_i32ar static NOINLINE B m_lvi32_4(i32 a, i32 b, i32 c, i32 d) { i32* rp; B r = m_i32arrv(&rp,4); rp[0]=a; rp[1]=b; rp[2]=c; rp[3]=d; return r; } B invalidFn_c1(B t, B x); +void comps_gcFn() { + mm_visitP(comps_curr); +} void load_init() { // very last init function - comp_currPath = bi_N; - comp_currArgs = bi_N; - comp_currSrc = bi_N; - comp_currRe = bi_N; - gc_add_ref(&comp_currPath); - gc_add_ref(&comp_currArgs); - gc_add_ref(&comp_currSrc); - gc_add_ref(&comp_currRe); + comps_curr = m_comps(bi_N, bi_N, bi_N, bi_N, 0); + gc_addFn(comps_gcFn); gc_add_ref(&rt_invFnReg); gc_add_ref(&rt_invFnSwap); B fruntime[] = { @@ -594,9 +583,9 @@ B bqn_explain(B str, B path) { } B args = bi_N; - PUSH_COMP; + COMPS_PUSH(path, args, str, bi_N); B c = c2(load_comp, incG(load_compArg), inc(str)); - POP_COMP; + COMPS_POP; B ret = c2(load_explain, c, str); return ret; #endif diff --git a/src/load.h b/src/load.h new file mode 100644 index 00000000..2840c328 --- /dev/null +++ b/src/load.h @@ -0,0 +1,16 @@ +#pragma once +extern HArr* comps_curr; // global-ish state, valid during compilation; comps_max elements +enum { + comps_path, + comps_args, + comps_src, + comps_re, // HArr of ⟨REPL mode ⋄ scope ⋄ compiler ⋄ runtime ⋄ glyphs ⋄ sysval names ⋄ sysval values⟩ + comps_envPos, + comps_max +}; +#define COMPS_REF(O,N) O->a[comps_##N] +#define COMPS_CREF(N) COMPS_REF(comps_curr, N) + +extern B dsv_ns, dsv_vs; +B comps_getPrimitives(void); +void comps_getSysvals(B* res); diff --git a/src/vm.c b/src/vm.c index 925639e6..69830a1f 100644 --- a/src/vm.c +++ b/src/vm.c @@ -9,6 +9,7 @@ #include "utils/utf.h" #include "utils/talloc.h" #include "utils/interrupt.h" +#include "load.h" #include #ifndef UNWIND_COMPILER // whether to hide stackframes of the compiler in compiling errors @@ -1662,7 +1663,7 @@ void unwindEnv(Env* envNew) { } void unwindCompiler() { #if UNWIND_COMPILER - unwindEnv(envStart+comp_currEnvPos); + unwindEnv(envStart+o2i64(COMPS_CREF(envPos))); #endif } diff --git a/test/cases/system.bqn b/test/cases/system.bqn index 99fffb39..7ea1df22 100644 --- a/test/cases/system.bqn +++ b/test/cases/system.bqn @@ -27,6 +27,14 @@ f ← •ReBQN{repl⇐"none"} ⋄ !6≡•Type F "⇐" !"Cannot construct a namespace for a REPL result" % f ← •ReBQN{repl⇐"strict"} ⋄ F "a⇐1" !"Cannot construct a namespace for a REPL result" % f ← •ReBQN{repl⇐"strict"} ⋄ F "⇐" +# f←•ReBQN {primitives⇐⋈'÷'‿-} ⋄ ! ¯5 ≡ F"•BQN""÷5""" ⋄ b←F"•BQN" ⋄ ! ¯5 ≡ B "÷5" ⋄ ! 0.2 ≡ •BQN "÷5" +# f←•ReBQN {primitives⇐⋈'÷'‿-} ⋄ a1 ← •bqn ⋄ r1 ← F"•BQN" ⋄ a2 ← A1"•BQN" ⋄ r2 ← R1"•BQN" ⋄ a3 ← A1"•ReBQN{⇐}" ⋄ r3 ← R1"•ReBQN{⇐}" ⋄ {𝕏"÷"}¨ ⟨a1,r1,a2,r2,a3,r3⟩ %% ⟨÷,-,÷,-,÷,-⟩ +f←•ReBQN {repl⇐"none" ⋄ primitives⇐⋈'÷'‿÷} ⋄ ! "Unknown character: '-'" ≡ F⎊{𝕊: •CurrentError@} "-5" ⋄ ! 0.2 ≡ •BQN "÷5" +f←•ReBQN {repl⇐"loose" ⋄ primitives⇐⋈'÷'‿÷} ⋄ ! "Unknown character: '-'" ≡ F⎊{𝕊: •CurrentError@} "-5" ⋄ ! 0.2 ≡ •BQN "÷5" +ps ← ⟨'÷'‿÷⟩ ⋄ f←•ReBQN {repl⇐"none" ⋄ primitives⇐ps} ⋄ ! ps ≡ F "•primitives" ⋄ ! •primitives ≡ •BQN "•primitives" ⋄ ! ps ≡ F "•primitives" +ps ← ⟨'÷'‿÷⟩ ⋄ f←•ReBQN {repl⇐"loose" ⋄ primitives⇐ps} ⋄ ! ps ≡ F "•primitives" ⋄ ! •primitives ≡ •BQN "•primitives" ⋄ ! ps ≡ F "•primitives" + + # •primitives !∧´2=•Type¨ ⊑¨•primitives !¨ (•Type¨1⊑¨•primitives)∊3‿4‿5