From f2efa657b787e4d985c035876b10b50a922bd940 Mon Sep 17 00:00:00 2001 From: dzaima Date: Sat, 14 Sep 2024 18:28:51 +0300 Subject: [PATCH] start of cc.bqn/precompiled format refactor --- build/cc.bqn | 109 ++++++++++++++++++++++++-------------------- build/genRuntime | 17 +++---- build/genRuntimeSrc | 2 +- src/core/tyarr.c | 6 ++- src/load.c | 33 ++++++-------- 5 files changed, 88 insertions(+), 79 deletions(-) diff --git a/build/cc.bqn b/build/cc.bqn index ef36632d..73b4ed9f 100755 --- a/build/cc.bqn +++ b/build/cc.bqn @@ -1,56 +1,63 @@ #!/usr/bin/env bqn # Modified version of https://github.com/mlochbaum/BQN/blob/master/src/cjs.bqn -args←•args -"Usage: ./cc.bqn path/to/mlochbaum/BQN [-i] <(r|r0|r1|r1x|c|cc|f|e|p) or an expression>"!2≤≠args -return ← 1≡⊑args -args↓˜↩ return -path←(⊑args)∾"/src/" -args↓˜↩1 -Import ← {𝕨•Import path∾𝕩} -FChars ← {𝕨•FChars path∾𝕩} -FLines ← {𝕨•FLines path∾𝕩} +args ← •args +"Usage: ./cc.bqn path/to/mlochbaum/BQN [-i] [-n name] <(r|r0|r1|r1x|c|cc|f|e|p) or an expression>"!2≤≠args +NextArg ← {𝕊: (args↓˜↩1) ⊢ ⊑args} +TryArg ← {𝕩≡⊑args? 1 ⊣ NextArg@; 0} +return ← TryArg 1 +bqnpath ← (NextArg@) •file.At "src" +srcmap ← TryArg "-i" +name ← TryArg◶"(precompiled)"‿NextArg "-n" -F ← •Repr # Format number -# Escape the special characters that appear in BQN sources. -Esc←{ - in ← (@+0‿9‿10‿13)∾"'""" # Null, Tab, LF, CR, and quotes - out ← "0tnr" # Whitespace characters changed to letters - i ← in⊐𝕩 - 𝕩 ↩ i ⊏⟜out⌾((i<≠out)⊸/) 𝕩 # Replace - ∾(i<≠in) /⟜"\"⊸∾¨ 𝕩 # Insert \ +Import ← {𝕨 •Import bqnpath •file.At 𝕩} +FChars ← {𝕨 •FChars bqnpath •file.At 𝕩} +FLines ← {𝕨 •FLines bqnpath •file.At 𝕩} + +F ← •Repr # Format number +Join ← {(-≠𝕨)↓ ∾∾⟜𝕨¨ 𝕩} + +FmtComp ← { + # Escape the special characters that appear in BQN sources. + Esc ← { + in ← (@+0‿9‿10‿13)∾"'""" # Null, Tab, LF, CR, and quotes + out ← "0tnr" # Whitespace characters changed to letters + i ← in⊐𝕩 + 𝕩 ↩ i ⊏⟜out⌾((i<≠out)⊸/) 𝕩 # Replace + ∾(i<≠in) /⟜"\"⊸∾¨ 𝕩 # Insert \ + } + + U32Len ← (≠ - ·+´ ≥⟜55296 ∧ ≤⟜56319)-⟜@ # for dzaima/BQN; shouldn't break things in proper BQN impls, as surrogate values don't map to valid codepoints + + CStr ← {"""" (∾∾⊣) Esc 𝕩} + + OStr ← { # A BQN string + a←∧´𝕩<128+@ + ∾⟨a⊑"m_c32vec"‿"m_c8vec","(",a¬⊸⥊"U","",CStr𝕩,",",•Repr U32Len 𝕩,")"⟩ + } + OChr ← {"m_c32(U'"∾(Esc⥊𝕩)∾"')"} # A BQN character + ONum ← {s←"-"/˜𝕩<0 ⋄ ∞⊸=∘|◶⟨"m_f64("∾")"∾˜s∾F∘| ⋄ "m_f64("∾s∾"1.0/0.0)"⟩𝕩} # Format number + OAny ← ≡◶⟨@⊸≤◶ONum‿OChr, OStr, ⊑⟩ + + List ← (0<≠)◶⟨"",1↓·∾","⊸∾¨⟩ + ShortList ← {"m_lv"∾𝕨∾"_"∾(F≠𝕩)∾"("∾(List𝕩)∾")"} + LongList ← {"m_ca"∾𝕨∾"("∾(F≠𝕩)∾",("∾𝕨∾"[]){"∾(List𝕩)∾"})"} + ConstList ← {"m_ca"∾𝕨∾"("∾(F≠𝕩)∾",({static "∾𝕨∾" l_[]={"∾(List𝕩)∾"};l_;}))"} + _makeList ← {𝕨 (≠∘⊢>4˙)◶⟨ShortList,𝕗⊑LongList‿ConstList⟩ 𝕩} + Li ← {"i32" 1 _makeList F¨𝕩} + _Lo ← {"B" 0 _makeList 𝔽¨𝕩} + + a ← ((≠𝕩)↑⟨Li ⋄ OAny¨ ⋄ =◶⟨ONum,Li¨⟩¨¨ ⋄ (Li 2⊸↑)¨ ⋄ Li¨ ⋄ OStr⟩) {𝕎𝕩}¨ 𝕩 + a {2=•Type⊑𝕩? 𝕩; 𝕊_Lo 𝕩}¨↩ + ∾⟨ + "load_importBlock", srcmap/"_src", "(", CStr name, ",", ⥊@+10, + (","∾@+10) Join a + ⥊@+10 + ");" + ⟩ } -U32Len ← (≠ - ·+´ ≥⟜55296 ∧ ≤⟜56319)-⟜@ # for dzaima/BQN; shouldn't break things in proper BQN impls, as surrogate values don't map to valid codepoints -OStr ← { # A BQN string - a←∧´𝕩<128+@ - ∾⟨a⊑"m_c32vec"‿"m_c8vec","(",a¬⊸⥊"U","""",Esc𝕩,""",",•Repr U32Len 𝕩,")"⟩ -} -OChr ← {"m_c32(U'"∾(Esc⥊𝕩)∾"')"} # A BQN character -ONum ← {s←"-"/˜𝕩<0 ⋄ ∞⊸=∘|◶⟨"m_f64("∾")"∾˜s∾F∘| ⋄ "m_f64("∾s∾"1.0/0.0)"⟩𝕩} # Format number -OAny ← ≡◶⟨@⊸≤◶ONum‿OChr, OStr, ⊑⟩ - -List ← (0<≠)◶⟨"",1↓·∾","⊸∾¨⟩ -ShortList ← {"m_lv"∾𝕨∾"_"∾(F≠𝕩)∾"("∾(List𝕩)∾")"} -LongList ← {"m_ca"∾𝕨∾"("∾(F≠𝕩)∾",("∾𝕨∾"[]){"∾(List𝕩)∾"})"} -ConstList ← {"m_ca"∾𝕨∾"("∾(F≠𝕩)∾",({static "∾𝕨∾" l_[]={"∾(List𝕩)∾"};l_;}))"} -_makeList ← {𝕨 (≠∘⊢>4˙)◶⟨ShortList,𝕗⊑LongList‿ConstList⟩ 𝕩} -Li ← {"i32" 1 _makeList F¨𝕩} -_Lo ← {"B" 0 _makeList 𝔽¨𝕩} - - -glyphs ← Import "glyphs.bqn" -_getComp ← { (4+2×useInd)↑ <∘⊢∾˜ 5↑ (𝕗 Import "c.bqn"){𝔽} } -useInd ← "-i"≡⊑args ⋄ args↓˜↩useInd -Comp ← ((<"incG(runtime["∾F∾"])"˙)¨↕≠∾glyphs) glyphs _getComp ⊢ -J ← ∾∾⟜(@+10)¨ - -Fout ← {((≠𝕩)↑⟨Li ⋄ OAny _Lo ⋄ =◶⟨ONum,Li _Lo⟩ _Lo _Lo ⋄ (Li 2⊸↑) _Lo ⋄ Li _Lo ⋄ OStr⟩) {𝕎𝕩}¨ 𝕩} -Frun ← 1⊸Fout -Long ← {¯2↓∾𝕩∾¨<","∾@+10} -LFC ← Long∘Fout∘Comp - primN ← ⟨ "add", "sub", "mul", "div", "pow", "root", "floor", "ceil", "stile", "not" "and", "or", "lt", "gt", "ne", "eq", "le", "ge", "feq", "fne" @@ -67,16 +74,21 @@ fullC ← isFull/primC extraC ← pre (¬∘∊˜/⊢) fullC # •Out List "bi_"⊸∾¨ (primC∊extraC) / primN +glyphs ← Import "glyphs.bqn" +_getComp ← { (4+2×srcmap)↑ <∘⊢∾˜ 5↑ (𝕗 Import "c.bqn"){𝔽} } +Comp ← ((<"incG(runtime["∾F∾"])"˙)¨↕≠∾glyphs) glyphs _getComp ⊢ RT ← { ext ← 2=≠𝕩 𝕩 ↩ ⊑𝕩 src‿need‿inputs←((ext⊑𝕩‿1) ∾ ext/⟨fullC, extraC⟩) Import "pr.bqn" pr←"runtime_0"‿"provide"{(∾𝕨<⊸(<∘{∾"incG("‿𝕨‿"["‿𝕩‿"])"}⟜F¨)⟜(↕≠)¨𝕩)⊏˜(∾𝕩)⊐∾need}○((-1+1=𝕩)⊸↑)inputs - Long Fout pr need _getComp src + FmtComp pr need _getComp src } -CArg ← {J (¯5⊸↓∾𝕩˙)⌾⊑ FLines "c.bqn"} +CArg ← {(@+10) Join (¯5⊸↓∾𝕩˙)⌾⊑ FLines "c.bqn"} SVG ← {∾⟨"Modify←GetHighlights←⊢⋄"⟩∾ FChars∘∾⟜".bqn"¨ "../svg"‿𝕩} +LFC ← FmtComp∘Comp + (@+10)∾˜ •Out⍟(¬return) (⊑"r"‿"r0"‿"r1"‿"r1x"‿"c"‿"cc"‿"f"‿"e"‿"eu"‿"p"⊐⊏)◶⟨ RT∘2, RT∘0, RT∘1, RT∘1‿'x' {𝕩⋄LFC CArg "⟨"∾"⟩"«∾","⊸∾¨'"'(⊣∾∾˜)¨glyphs} @@ -86,5 +98,4 @@ SVG ← {∾⟨"Modify←GetHighlights←⊢⋄"⟩∾ FChars∘∾⟜".bqn"¨ " {𝕩⋄LFC FChars "eu.bqn"} {𝕩⋄LFC SVG "p"} ∾LFC¨ - # ¯1 ↓ · J L∘Fout∘Comp¨ ⟩ args \ No newline at end of file diff --git a/build/genRuntime b/build/genRuntime index ac5d213a..5a721e83 100755 --- a/build/genRuntime +++ b/build/genRuntime @@ -5,11 +5,12 @@ bqnSrc‿outputPath ← •wdpath⊸•file.At¨ 2↑•args rtSrc ← ⊑(<"src")∊2↓•args -CC ← {𝕨 •FChars (⟨1,bqnSrc⟩∾(rtSrc/⟨"-i"⟩)∾⟨𝕩⟩) •Import "cc.bqn"} -(outputPath•file.At "gen/src" )•FChars "#define RT_SRC "∾('0'+rtSrc)∾@+10 -(outputPath•file.At "gen/compiles" )CC"cc" -(outputPath•file.At "gen/runtime0" )CC"r0" -(outputPath•file.At "gen/runtime1" )CC"r1" -(outputPath•file.At "gen/runtime1x")CC"r1x" -(outputPath•file.At "gen/formatter")CC"f" -(outputPath•file.At "gen/explain" )CC"eu" +CC ← { dst‿type‿name: + (outputPath•file.At dst) •FChars (⟨1, bqnSrc⟩∾(rtSrc/⟨"-i"⟩)∾⟨"-n", name, type⟩) •Import "cc.bqn" +} +CC ⟨"gen/compiles", "cc", "(compiler)"⟩ +CC ⟨"gen/runtime0", "r0", "(self-hosted runtime0)"⟩ +CC ⟨"gen/runtime1", "r1", "(self-hosted runtime1)"⟩ +CC ⟨"gen/runtime1x", "r1x", "(self-hosted runtime1x)"⟩ +CC ⟨"gen/formatter", "f", "(formatter)"⟩ +CC ⟨"gen/explain", "eu", "(explain)"⟩ diff --git a/build/genRuntimeSrc b/build/genRuntimeSrc index 6f15032c..5dc22d5c 100755 --- a/build/genRuntimeSrc +++ b/build/genRuntimeSrc @@ -1,3 +1,3 @@ -#!./BQN +#!/usr/bin/env bqn # a version of genRuntime that also generates source maps, so that errors in the runtime or compiler get error messages ({𝕩∾<"src"}⍟(2≤≠) •args) •Import "genRuntime" \ No newline at end of file diff --git a/src/core/tyarr.c b/src/core/tyarr.c index d4242c0e..d637a418 100644 --- a/src/core/tyarr.c +++ b/src/core/tyarr.c @@ -97,12 +97,14 @@ static void bitarr_init(void) { TIi(t_bitarr,canStore) = bitarr_canStore; } +INIT_GLOBAL Arr* staticSliceRoot; void tyarr_init(void) { i8arr_init(); i16arr_init(); i32arr_init(); bitarr_init(); c8arr_init(); c16arr_init(); c32arr_init(); f64arr_init(); - { u64* tmp; bi_emptyIVec = m_bitarrv(&tmp, 0); gc_add(bi_emptyIVec); } - { u8* tmp; bi_emptyCVec = m_c8arrv (&tmp, 0); gc_add(bi_emptyCVec); } + { u64* tmp; gc_add(bi_emptyIVec = m_bitarrv(&tmp, 0)); } + { u8* tmp; gc_add(bi_emptyCVec = m_c8arrv (&tmp, 0)); } + { u8* tmp; gc_add(taga(arr_shVec(staticSliceRoot = m_c8arrp(&tmp, 0)))); } Arr* emptySVec = arr_shVec(m_fillarrpEmpty(emptyCVec())); bi_emptySVec = taga(emptySVec); gc_add(bi_emptySVec); diff --git a/src/load.c b/src/load.c index 2f27c513..9623b351 100644 --- a/src/load.c +++ b/src/load.c @@ -23,6 +23,8 @@ FOR_INIT(F) GLOBAL u64 mm_heapMax = HEAP_MAX; GLOBAL u64 mm_heapAlloc; +extern Arr* staticSliceRoot; + // compiler result: // [ // [...bytecode], @@ -142,19 +144,12 @@ Block* load_buildBlock(B x, B src, B path, B name, Scope* sc, i32 nsResult) { // return r; } -#if !(ONLY_NATIVE_COMP && !FORMATTER && NO_RT && NO_EXPLAIN) -#include PRECOMPILED_FILE(src) -#endif - -#if RT_SRC -Block* load_importBlock(char* name, B bc, B objs, B blocks, B bodies, B inds, B src) { // consumes all +static NOINLINE Block* load_importBlock_src(char* name, B bc, B objs, B blocks, B bodies, B inds, B src) { // consumes all return compileAll(bc, objs, blocks, bodies, inds, bi_N, src, m_c8vec_0(name), NULL, 0); } -#else -Block* load_importBlock(char* name, B bc, B objs, B blocks, B bodies) { // consumes all +static NOINLINE Block* load_importBlock(char* name, B bc, B objs, B blocks, B bodies) { // consumes all return compileAll(bc, objs, blocks, bodies, bi_N, bi_N, bi_N, m_c8vec_0(name), NULL, 0); } -#endif GLOBAL B load_compgen; GLOBAL B def_re; @@ -465,20 +460,20 @@ void load_init() { // very last init function #if !ALL_R0 B runtime_0[] = {bi_floor,bi_ceil,bi_stile,bi_lt,bi_gt,bi_ne,bi_ge,bi_rtack,bi_ltack,bi_join,bi_pair,bi_take,bi_drop,bi_select,bi_const,bi_swap,bi_each,bi_fold,bi_atop,bi_over,bi_before,bi_after,bi_cond,bi_repeat}; #else - Block* runtime0_b = load_importBlock("(self-hosted runtime0)", + Block* runtime0_b = ({ #include PRECOMPILED_FILE(runtime0) - ); + }); HArr* r0r = toHArr(evalFunBlockConsume(runtime0_b)); B* runtime_0 = r0r->a; #endif - Block* runtime_b = load_importBlock("(self-hosted runtime1)", + Block* runtime_b = ({ #if ALL_R0 || ALL_R1 || NO_EXTENDED_PROVIDE || RT_VERIFY #include PRECOMPILED_FILE(runtime1) #else #include PRECOMPILED_FILE(runtime1x) #endif - ); + }); #if ALL_R0 ptr_dec(r0r); @@ -579,9 +574,9 @@ void load_init() { // very last init function #else B prevAsrt = runtime[n_asrt]; runtime[n_asrt] = bi_casrt; // horrible but GC is off so it's fiiiiiine - Block* comp_b = load_importBlock("(compiler)", + Block* comp_b = ({ #include PRECOMPILED_FILE(compiles) - ); + }); runtime[n_asrt] = prevAsrt; gc_add(load_compgen = evalFunBlockConsume(comp_b)); @@ -601,9 +596,9 @@ void load_init() { // very last init function gc_add(def_re = ps.b); #if FORMATTER - Block* fmt_b = load_importBlock("(formatter)", + Block* fmt_b = ({ #include PRECOMPILED_FILE(formatter) - ); + }); B fmtM = evalFunBlockConsume(fmt_b); B fmtR = c1(fmtM, m_caB(4, (B[]){incG(bi_type), incG(bi_decp), incG(bi_glyph), incG(bi_repr)})); decG(fmtM); SGet(fmtR) @@ -647,9 +642,9 @@ B bqn_explain(B str) { B* o = harr_ptr(def_re); if (load_explain.u==0) { B* runtime = harr_ptr(o[re_rt]); - Block* expl_b = load_importBlock("(explain)", + Block* expl_b = ({ #include PRECOMPILED_FILE(explain) - ); + }); gc_add(load_explain = evalFunBlockConsume(expl_b)); }