@amritk/nish-x86_64-linux 0.18.0 → 0.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (104) hide show
  1. package/INSTALL.md +58 -28
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +2759 -0
  4. package/package.json +3 -2
  5. package/runtime/browser.d.ts +14 -0
  6. package/runtime/browser.mjs +88 -0
  7. package/runtime/core/arrays.d.ts +18 -0
  8. package/runtime/core/arrays.mjs +125 -0
  9. package/runtime/core/checks.mjs +20 -0
  10. package/runtime/core/host.d.ts +70 -0
  11. package/runtime/core/host.mjs +311 -0
  12. package/runtime/core/numbers.d.ts +53 -0
  13. package/runtime/core/numbers.mjs +316 -0
  14. package/runtime/core/result.d.ts +41 -0
  15. package/runtime/core/result.mjs +127 -0
  16. package/runtime/core/strings.d.ts +14 -0
  17. package/runtime/core/strings.mjs +169 -0
  18. package/runtime/core.d.ts +9 -0
  19. package/runtime/core.mjs +55 -0
  20. package/runtime/host.d.ts +6 -0
  21. package/runtime/nish.d.ts +99 -5
  22. package/runtime/nish.h +289 -19
  23. package/runtime/nish.mjs +39 -3
  24. package/runtime/node.mjs +383 -0
  25. package/runtime/runtime-aes.c +490 -0
  26. package/runtime/runtime-bytes.c +401 -0
  27. package/runtime/runtime-cross.c +366 -0
  28. package/runtime/runtime-host.c +8 -7
  29. package/runtime/runtime-net.c +93 -34
  30. package/runtime/runtime-os.c +214 -16
  31. package/runtime/runtime-parallel.c +590 -107
  32. package/runtime/runtime-simd.c +235 -11
  33. package/runtime/runtime-term.c +504 -0
  34. package/runtime/runtime-wasm.c +5 -14
  35. package/runtime/runtime.c +257 -17
  36. package/runtime/shim.mjs +6 -915
  37. package/runtime/sqlite/LICENSE.md +85 -0
  38. package/runtime/sqlite/README.md +45 -0
  39. package/runtime/sqlite/nish-sqlite.c +359 -0
  40. package/runtime/sqlite/sqlite3.c +271131 -0
  41. package/runtime/sqlite/sqlite3.h +14447 -0
  42. package/scripts/build.sh +437 -28
  43. package/std/LICENSE-fast-float +23 -0
  44. package/std/LICENSE-fdlibm +8 -0
  45. package/std/README.md +108 -58
  46. package/std/base64.ts +116 -0
  47. package/std/bytes.ts +419 -0
  48. package/std/cli.ts +328 -0
  49. package/std/collections.ts +6 -5
  50. package/std/crypto/LICENSE-crypt_blowfish +26 -0
  51. package/std/crypto/LICENSE-libsecp256k1 +19 -0
  52. package/std/crypto/aes.ts +238 -76
  53. package/std/crypto/base64url.ts +125 -0
  54. package/std/crypto/bcrypt.ts +760 -0
  55. package/std/crypto/chacha20poly1305.ts +43 -9
  56. package/std/crypto/ed25519.ts +840 -0
  57. package/std/crypto/hkdf.ts +304 -12
  58. package/std/crypto/hmac.ts +382 -54
  59. package/std/crypto/p256.ts +932 -61
  60. package/std/crypto/x25519.ts +55 -37
  61. package/std/crypto/x509-random.ts +71 -0
  62. package/std/crypto/x509.ts +20 -131
  63. package/std/json-tokenizer.ts +4237 -0
  64. package/std/json-typed.ts +1014 -0
  65. package/std/json-value.ts +1182 -0
  66. package/std/json.ts +1202 -34
  67. package/std/math.ts +608 -0
  68. package/std/net/buffer-pool.ts +151 -0
  69. package/std/net/hpack.ts +416 -112
  70. package/std/net/http-fields.ts +93 -0
  71. package/std/net/http1-server.ts +501 -62
  72. package/std/net/http1.ts +31 -6
  73. package/std/net/http2-tls.ts +112 -24
  74. package/std/net/http2.ts +333 -75
  75. package/std/net/http3-server.ts +261 -43
  76. package/std/net/http3.ts +187 -28
  77. package/std/net/postgres.ts +1875 -0
  78. package/std/net/prefork.ts +412 -0
  79. package/std/net/quic-listener.ts +108 -11
  80. package/std/net/quic-packet.ts +243 -82
  81. package/std/net/quic-recovery.ts +361 -30
  82. package/std/net/quic-stream.ts +424 -24
  83. package/std/net/quic.ts +519 -167
  84. package/std/net/router.ts +676 -0
  85. package/std/net/tls/codec.ts +426 -27
  86. package/std/net/tls/record-server.ts +478 -132
  87. package/std/net/tls/record.ts +133 -42
  88. package/std/net/tls/schedule.ts +464 -55
  89. package/std/net/tls/ticket.ts +610 -0
  90. package/std/net/tls-tcp.ts +196 -53
  91. package/std/net/tls.ts +1018 -260
  92. package/std/net/url.ts +571 -0
  93. package/std/random.ts +94 -0
  94. package/std/regex.ts +4274 -0
  95. package/std/sqlite.ts +391 -0
  96. package/std/term-io.ts +978 -0
  97. package/std/term-pixels.ts +874 -0
  98. package/std/term-probe.ts +459 -0
  99. package/std/term-rate.ts +993 -0
  100. package/std/term.ts +2453 -0
  101. package/std/text.ts +90 -7
  102. package/std/threads.ts +438 -14
  103. package/std/yaml-lines.ts +141 -0
  104. package/std/yaml.ts +4675 -0
package/scripts/build.sh CHANGED
@@ -3,18 +3,23 @@
3
3
  #
4
4
  # scripts/build.sh <module.ll> [more .ll/.c files...] -o <out> [--profile debug|speed|size|wasm|wasi|napi]
5
5
  #
6
- # The C runtime is six translation units and is named as one: an input
6
+ # The C runtime is ten translation units and is named as one: an input
7
7
  # <dir>/runtime.c also compiles <dir>/runtime-os.c, the half that wraps the
8
8
  # system calls (files, directories, subprocesses, the environment, the clock),
9
9
  # <dir>/runtime-parallel.c, the half that divides a range of work across
10
- # threads, <dir>/runtime-host.c, the wall clock, entropy, file times and
11
- # signals, <dir>/runtime-net.c, the sockets of `nish:net`, and
12
- # <dir>/runtime-simd.c, the byte searches that read a vector at a time. Each of
13
- # those files says why they are compiled and measured apart.
10
+ # threads, <dir>/runtime-cross.c, the copies that carry a string or an array
11
+ # from one thread's arena to another's, <dir>/runtime-host.c, the wall clock, entropy, file times and
12
+ # signals, <dir>/runtime-net.c, the sockets of `nish:net`,
13
+ # <dir>/runtime-simd.c, the byte searches that read a vector at a time,
14
+ # <dir>/runtime-aes.c, AES-GCM on the CPU's AES instructions,
15
+ # <dir>/runtime-term.c, stdin and the terminal of `nish:term`, and
16
+ # <dir>/runtime-bytes.c, the byte-class runs of `nish/bytes`. Each of those
17
+ # files says why they are compiled and measured apart.
14
18
  #
15
19
  # Profiles:
16
20
  # debug clang defaults: no optimisation, symbols kept. The "before" number.
17
- # speed -O3 + LTO + section GC + strip. Rust `--release` equivalent.
21
+ # speed -O3 + LTO + section GC + strip. Rust `--release` equivalent. With
22
+ # lld, LTO code generation runs on every core.
18
23
  # size -Oz + LTO + section GC + strip + no unwind tables. Rust
19
24
  # `opt-level="z"`, `panic="abort"`, `strip=true` equivalent.
20
25
  # wasm wasm32 freestanding module exporting every non-internal function;
@@ -57,6 +62,49 @@
57
62
  # put in the .ll (line table, variables) reaches the binary. `nish
58
63
  # --link -g` passes it through automatically.
59
64
  #
65
+ # CPU level (WP38 S2): `--cpu x86-64-v3` compiles and links every input --
66
+ # the module, the runtime and any other C -- with -march=x86-64-v3
67
+ # -ffp-contract=off, so the code generator may use AVX2, BMI2 and the other
68
+ # v3 instructions and still forms no fused multiply-add the IR did not write.
69
+ # It is the one level taken; any other value is refused with exit 2, and so are
70
+ # the wasm and wasi profiles, which are not x86-64. A binary built with it
71
+ # stops with SIGILL on a CPU without AVX2. `nish --link --cpu x86-64-v3`
72
+ # passes it through.
73
+ #
74
+ # SQLite: an input .ll that declares an `sqlite3_*` or `nishsqlite_*` function
75
+ # also compiles and links every runtime/sqlite/*.c, the vendored amalgamation
76
+ # (runtime/sqlite/README.md). Any other program compiles none of it. sqlite3.c
77
+ # is compiled to native code at the profile's level, even where the profile
78
+ # compiles to LTO bitcode. The options are most of SQLite's recommended set
79
+ # (see the defaults below), with THREADSAFE=0 (=2 under --threads).
80
+ # NISH_SQLITE_CFLAGS is appended to them: its first line, split on whitespace
81
+ # with no quoting, and a relative -I in it names a directory below
82
+ # runtime/sqlite/, where each unit is compiled. A -D or -U in it for one of
83
+ # their names replaces it. The napi profile compiles SQLite -fPIC and
84
+ # -fvisibility=hidden, so an addon does not export its symbols. Every profile
85
+ # but wasm links it; wasm has no libc to build it on and is refused with exit 2.
86
+ # On wasi, SQLite locks with dot files and has no WAL shared memory.
87
+ #
88
+ # The runtime compiled once (the debug, speed and size profiles only; `nish
89
+ # --link` and `nish run` use these through their runtime cache, src/compile.ts):
90
+ # --runtime-objects <dir> compile the runtime.c input and its seven siblings,
91
+ # one $CC -c each and all at once, into
92
+ # <dir>/<unit>.o with the profile's compile flags,
93
+ # and link nothing. speed and size store LTO bitcode,
94
+ # so the link can still inline the runtime into the
95
+ # program. No -o; prints nothing on success.
96
+ # --runtime-from <dir> link against <dir>/<unit>.o in place of a
97
+ # runtime.c input, after compiling every other input
98
+ # with its own $CC -c, all at once. The link line is
99
+ # the one the profile always uses.
100
+ # --sqlite-objects <dir> compile runtime/sqlite/*.c alone, into
101
+ # <dir>/<unit>.o, as a link of a program that calls
102
+ # SQLite would, and link nothing. Takes no inputs and
103
+ # no -o, and prints nothing on success.
104
+ # --sqlite-from <dir> link <dir>/<unit>.o in place of compiling
105
+ # runtime/sqlite/, when the IR calls SQLite.
106
+ # All four refuse the wasm, wasi and napi profiles and --pgo-*, with exit 2.
107
+ #
60
108
  # Works on Linux (clang + lld preferred, GNU ld tolerated) and macOS (Apple ld64
61
109
  # or Homebrew llvm). Set CC to pick a compiler (default: clang on PATH).
62
110
  set -euo pipefail
@@ -67,28 +115,90 @@ inputs=()
67
115
  pgo=() # -fprofile-generate / -fprofile-use=<file>
68
116
  debug=0 # -g: keep DWARF (nish -g emits it in the IR; runtime.c gets it here)
69
117
  threads=0 # --threads: -DNISH_THREADS, the thread-local arena (WP20 T0)
118
+ objects_to="" # --runtime-objects <dir>: compile the runtime alone, into <dir>
119
+ objects_from="" # --runtime-from <dir>: link against the runtime compiled there
120
+ cpu="" # --cpu x86-64-v3: the one CPU level above the baseline (WP38 S2)
121
+ sqlite_to="" # --sqlite-objects <dir>: compile runtime/sqlite/ alone, into <dir>
122
+ sqlite_from="" # --sqlite-from <dir>: link runtime/sqlite/ compiled there
70
123
  while [ $# -gt 0 ]; do
71
124
  case "$1" in
72
125
  -o) out="$2"; shift 2 ;;
73
126
  --profile) profile="$2"; shift 2 ;;
74
127
  -g) debug=1; shift ;;
75
128
  --threads) threads=1; shift ;;
129
+ --cpu)
130
+ [ -n "${2-}" ] || { echo "error: --cpu needs a level; the one taken is x86-64-v3" >&2; exit 2; }
131
+ cpu="$2"; shift 2 ;;
132
+ --runtime-objects) objects_to="$2"; shift 2 ;;
133
+ --runtime-from) objects_from="$2"; shift 2 ;;
134
+ --sqlite-objects) sqlite_to="$2"; shift 2 ;;
135
+ --sqlite-from) sqlite_from="$2"; shift 2 ;;
76
136
  --pgo-generate) pgo=(-fprofile-generate); shift ;;
77
137
  --pgo-use)
78
138
  [ -f "$2" ] || { echo "error: --pgo-use: profile '$2' not found (run the instrumented binary, then llvm-profdata merge)" >&2; exit 2; }
79
139
  pgo=("-fprofile-use=$2"); shift 2 ;;
80
- -h|--help) sed -n '2,42p' "$0"; exit 0 ;;
140
+ -h|--help) sed -n '2,/^set -euo pipefail$/{/^set -euo pipefail$/!p;}' "$0"; exit 0 ;; # the header, to the first command
81
141
  *) inputs+=("$1"); shift ;;
82
142
  esac
83
143
  done
84
- [ ${#inputs[@]} -gt 0 ] || { echo "error: no input files" >&2; exit 2; }
85
- [ -n "$out" ] || { echo "error: -o <out> is required" >&2; exit 2; }
144
+ [ ${#inputs[@]} -gt 0 ] || [ -n "$sqlite_to" ] || { echo "error: no input files" >&2; exit 2; }
145
+ if [ -n "$cpu" ]; then
146
+ [ "$cpu" = x86-64-v3 ] || { echo "error: --cpu takes x86-64-v3, not '$cpu'" >&2; exit 2; }
147
+ case "$profile" in
148
+ wasm|wasi) echo "error: --cpu x86-64-v3 is an x86-64 level, so the $profile profile does not take it" >&2; exit 2 ;;
149
+ esac
150
+ fi
151
+ # wasm-ld exports a wasm module's linear memory as `memory`, so under the wasm
152
+ # profile's `--export-all` a function of that name makes a module Node refuses
153
+ # to compile ("Duplicate export name 'memory'", #643). The wasi module exports
154
+ # only `memory` and `_start`, but the name is reserved there too, so a program
155
+ # that links for wasi also links for wasm. The compiler refuses one (NL3038)
156
+ # when it knows the build is wasm, but IR compiled for the host with `-o` alone
157
+ # reaches these profiles without telling it, so the IR is read here too.
158
+ case "$profile" in
159
+ wasm|wasi)
160
+ for i in ${inputs[@]+"${inputs[@]}"}; do
161
+ case "$i" in
162
+ *.ll)
163
+ # An `internal` or `private` definition is not exported, so it is left alone.
164
+ if grep -E '^define [^@]*@memory\(' "$i" | grep -Evq '^define (internal|private) '; then
165
+ echo "error: $i defines a function named \`memory\`, the name the $profile profile's module exports its linear memory under; rename the function" >&2
166
+ exit 2
167
+ fi ;;
168
+ esac
169
+ done ;;
170
+ esac
171
+ if [ -n "$objects_to" ] || [ -n "$objects_from" ]; then
172
+ # A cached runtime is the profile's ordinary compile, so it exists only for
173
+ # the profiles that compile the runtime with nothing but these flags.
174
+ case "$profile" in
175
+ debug|speed|size) ;;
176
+ *) echo "error: --runtime-objects and --runtime-from take the debug, speed and size profiles, not '$profile'" >&2; exit 2 ;;
177
+ esac
178
+ [ ${#pgo[@]} -eq 0 ] || { echo "error: --runtime-objects and --runtime-from do not take --pgo-*" >&2; exit 2; }
179
+ [ -z "$objects_to" ] || [ -z "$objects_from" ] || { echo "error: --runtime-objects and --runtime-from are two different builds" >&2; exit 2; }
180
+ fi
181
+ if [ -n "$sqlite_to" ] || [ -n "$sqlite_from" ]; then
182
+ # The cached SQLite objects sit beside the cached runtime and take what it takes.
183
+ case "$profile" in
184
+ debug|speed|size) ;;
185
+ *) echo "error: --sqlite-objects and --sqlite-from take the debug, speed and size profiles, not '$profile'" >&2; exit 2 ;;
186
+ esac
187
+ [ ${#pgo[@]} -eq 0 ] || { echo "error: --sqlite-objects and --sqlite-from do not take --pgo-*" >&2; exit 2; }
188
+ [ -z "$sqlite_to" ] || [ -z "$sqlite_from" ] || { echo "error: --sqlite-objects and --sqlite-from are two different builds" >&2; exit 2; }
189
+ [ -z "$sqlite_to" ] || [ -z "$objects_to$objects_from" ] || { echo "error: --sqlite-objects compiles runtime/sqlite/ alone, without --runtime-objects or --runtime-from" >&2; exit 2; }
190
+ fi
191
+ [ -n "$out" ] || [ -n "$objects_to" ] || [ -n "$sqlite_to" ] || { echo "error: -o <out> is required" >&2; exit 2; }
86
192
 
87
- # The runtime is six translation units, and a caller names one: whoever passes
193
+ # The runtime is ten translation units, and a caller names one: whoever passes
88
194
  # <dir>/runtime.c gets <dir>/runtime-os.c, <dir>/runtime-parallel.c,
89
- # <dir>/runtime-host.c, <dir>/runtime-net.c and <dir>/runtime-simd.c compiled
90
- # beside it, on every profile, wasi included: runtime-simd.c takes its scalar
91
- # path there unless the build adds -msimd128. They were one file until the
195
+ # <dir>/runtime-cross.c, <dir>/runtime-host.c, <dir>/runtime-net.c, <dir>/runtime-simd.c,
196
+ # <dir>/runtime-aes.c, <dir>/runtime-term.c and <dir>/runtime-bytes.c compiled beside it, on every profile,
197
+ # wasi included: runtime-simd.c and runtime-bytes.c take their scalar paths there
198
+ # unless the build adds -msimd128,
199
+ # runtime-aes.c compiles to an answer of 0 and stubs that nothing calls,
200
+ # because the compiler emits no call to its kernels for wasm32, and
201
+ # runtime-term.c reads stdin and answers "not a terminal". They were one file until the
92
202
  # operating-system half was split out for its own size budget, and the parallel
93
203
  # half followed for the same reason (each file's header comment says why), and
94
204
  # a link line is where those splits
@@ -98,10 +208,11 @@ done
98
208
  # here keeps every one of those correct, and keeps "the runtime" one thing to
99
209
  # name from the outside. A caller that names one itself is left alone, because
100
210
  # naming one object twice is a duplicate-symbol error.
211
+ halves="runtime-os.c runtime-parallel.c runtime-cross.c runtime-host.c runtime-net.c runtime-simd.c runtime-aes.c runtime-term.c runtime-bytes.c"
101
212
  for i in ${inputs[@]+"${inputs[@]}"}; do
102
213
  case "$i" in
103
214
  */runtime.c|runtime.c)
104
- for half in runtime-os.c runtime-parallel.c runtime-host.c runtime-net.c runtime-simd.c; do
215
+ for half in $halves; do
105
216
  side="${i%runtime.c}$half"
106
217
  have=0
107
218
  for j in "${inputs[@]}"; do
@@ -112,9 +223,49 @@ for i in ${inputs[@]+"${inputs[@]}"}; do
112
223
  esac
113
224
  done
114
225
 
226
+ # SQLite (runtime/sqlite/) is an optional unit, and the IR selects it. A module
227
+ # that declares a function of SQLite's C API (`sqlite3_*`) or of the adapter
228
+ # beside it (`nishsqlite_*`) gets every runtime/sqlite/*.c compiled and
229
+ # linked. Any other program compiles none of it, so its link line and binary
230
+ # stay as they were. The test reads `declare` lines, and only before the first
231
+ # `"` on one, rather than any `@sqlite3_` in the file, because a string
232
+ # constant or a quoted name may hold one. It is one rule with `callsSqlite` in
233
+ # src/run-cache.ts, which decides whether a link asks the runtime cache for
234
+ # SQLite's objects; change both or neither. A caller
235
+ # that names a runtime/sqlite/*.c input itself is left alone, as `halves`
236
+ # leaves a runtime half: naming one object twice is a duplicate-symbol error.
237
+ # A tree without the directory still links every program that does not call it.
238
+ sqlite_dir="$(cd "$(dirname "$0")/../runtime/sqlite" 2>/dev/null && pwd -P)" || sqlite_dir=""
239
+ sqlite_wanted=0
240
+ if [ -n "$sqlite_to" ]; then
241
+ if [ ${#inputs[@]} -gt 0 ]; then
242
+ echo "error: --sqlite-objects compiles runtime/sqlite/ alone, not ${inputs[0]}" >&2; exit 2
243
+ fi
244
+ sqlite_wanted=1
245
+ fi
246
+ for i in ${inputs[@]+"${inputs[@]}"}; do
247
+ case "$i" in
248
+ *.ll) if grep -Eq '^declare [^"]*@(sqlite3|nishsqlite)_' "$i"; then sqlite_wanted=1; fi ;;
249
+ esac
250
+ done
251
+ for i in ${inputs[@]+"${inputs[@]}"}; do
252
+ case "$i" in
253
+ *.c) if [ -n "$sqlite_dir" ] && [ "$(cd "$(dirname -- "$i")" && pwd -P)" = "$sqlite_dir" ]; then sqlite_wanted=0; break; fi ;;
254
+ esac
255
+ done
256
+ if [ "$sqlite_wanted" = 1 ] && [ -z "$sqlite_dir" ]; then
257
+ echo "error: the module calls SQLite, and $(dirname "$0")/../runtime/sqlite/ is not there" >&2
258
+ exit 2
259
+ fi
260
+ if [ "$sqlite_wanted" = 1 ] && [ "$profile" = wasm ]; then
261
+ echo "error: the module calls SQLite, which the wasm profile cannot link: it has no libc for runtime/sqlite/sqlite3.c to build on; use --profile wasi" >&2
262
+ exit 2
263
+ fi
264
+
115
265
  CC=${CC:-clang}
116
266
  common=(-Wno-override-module) # our IR is target-neutral; clang fills the triple in
117
267
  elf=() # flags that only make sense for ELF targets
268
+ lld=0 # 1 when the link goes through ld.lld
118
269
 
119
270
  # Platform-specific dead-stripping, symbol stripping and LTO linker selection.
120
271
  case "$(uname -s)" in
@@ -146,7 +297,7 @@ case "$(uname -s)" in
146
297
  gc=(-Wl,--gc-sections -Wl,--as-needed -Wl,-O2 -Wl,--build-id=none); strip_flag=(-s)
147
298
  elf=(-fno-plt)
148
299
  # GNU ld needs the gold plugin for LTO; prefer lld when clang can find it.
149
- if command -v ld.lld >/dev/null 2>&1; then common+=(-fuse-ld=lld); fi ;;
300
+ if command -v ld.lld >/dev/null 2>&1; then common+=(-fuse-ld=lld); lld=1; fi ;;
150
301
  esac
151
302
 
152
303
  # -g: compile everything with debug info and never strip, whatever the profile,
@@ -177,30 +328,248 @@ if [ "$debug" = 1 ]; then common+=(-g); strip_flag=(); fi
177
328
  # command lines, which have no threads to link against, and where
178
329
  # runtime-parallel.c compiles to its sequential fallback because it tests
179
330
  # __wasi__ and __wasm__ as well as the macro.
331
+ # --cpu: every unit at the one level, so the module, the runtime and the link
332
+ # (LTO generates code there) agree. -ffp-contract=off keeps the meaning fixed:
333
+ # v3 has FMA, and contracting a*b+c into one rounding would print a different
334
+ # float than the baseline build (docs/LANGUAGE.md, "The CPU level").
335
+ if [ -n "$cpu" ]; then common+=(-march=x86-64-v3 -ffp-contract=off); fi
336
+
180
337
  tls=()
181
338
  if [ "$threads" = 1 ]; then
182
339
  common+=(-DNISH_THREADS=1 -ftls-model=initial-exec -pthread)
183
340
  tls=(-DNISH_THREADS=1 -ftls-model=initial-exec)
184
341
  fi
185
342
 
343
+ # What the speed and size profiles compile every input with, beside `common`.
344
+ # One list per profile, because the runtime cache below compiles with exactly
345
+ # what the one-command link does, and two copies of a flag set drift.
346
+ optimise=()
347
+ case "$profile" in
348
+ speed)
349
+ optimise=(-O3 -flto -DNDEBUG ${pgo[@]+"${pgo[@]}"}
350
+ -ffunction-sections -fdata-sections -fomit-frame-pointer
351
+ -fno-asynchronous-unwind-tables -fno-unwind-tables ${elf[@]+"${elf[@]}"}) ;;
352
+ size)
353
+ optimise=(-Oz -flto -DNDEBUG ${pgo[@]+"${pgo[@]}"}
354
+ -ffunction-sections -fdata-sections -fomit-frame-pointer
355
+ -fno-asynchronous-unwind-tables -fno-unwind-tables ${elf[@]+"${elf[@]}"}
356
+ -fno-stack-protector -fvisibility=hidden) ;;
357
+ napi)
358
+ optimise=(-O3 -flto -DNDEBUG ${pgo[@]+"${pgo[@]}"}
359
+ -ffunction-sections -fdata-sections -fomit-frame-pointer
360
+ -fno-asynchronous-unwind-tables -fno-unwind-tables ${elf[@]+"${elf[@]}"}) ;;
361
+ esac
362
+
363
+ # The speed link splits full LTO's code generation into one partition per core,
364
+ # run in parallel: lld's --lto-partitions, after the whole program is optimised
365
+ # as one module, so inlining and the instructions a program runs do not change
366
+ # (bench/instructions.json agrees to a few instructions in hundreds of
367
+ # millions). The bytes depend on the count, so one machine links one input to
368
+ # one output, as scripts/bootstrap.sh needs. ld64 and GNU ld have no such flag
369
+ # and link as before, and so does every other profile. The count comes
370
+ # from nproc (Linux), else getconf (both), else sysctl (macOS).
371
+ lto_parts=()
372
+ if [ "$profile" = speed ] && [ "$lld" = 1 ]; then
373
+ cores=$(nproc 2>/dev/null || getconf _NPROCESSORS_ONLN 2>/dev/null ||
374
+ sysctl -n hw.ncpu 2>/dev/null || echo 1)
375
+ # shellcheck disable=SC2054 # -Wl,<flag> is one argument: the comma is the linker's
376
+ lto_parts=(-Wl,--lto-partitions="$cores")
377
+ fi
378
+
379
+ # --runtime-objects and --runtime-from: the one-command link split in two, with
380
+ # every compile it held run at once. A `$CC -c` takes `common` and `optimise`,
381
+ # which is what the one command compiles each input with -- less
382
+ # -fuse-ld=lld, which only names the linker -- so a runtime unit compiles to the
383
+ # same object either way: native code for debug, -flto bitcode for speed and
384
+ # size, which the link still optimises together with the program. The link
385
+ # takes the same line the profile always uses, with objects for sources.
386
+ compile=()
387
+ for flag in "${common[@]}"; do
388
+ case "$flag" in -fuse-ld=*) ;; *) compile+=("$flag") ;; esac
389
+ done
390
+ compile+=(${optimise[@]+"${optimise[@]}"})
391
+ # Waits for every job, not just the last, and fails when any of them did.
392
+ # bash 3.2 has no `wait -n`, so the jobs are waited for in order.
393
+ pids=()
394
+ join_jobs() {
395
+ local failed=0 pid
396
+ for pid in ${pids[@]+"${pids[@]}"}; do
397
+ wait "$pid" || failed=1
398
+ done
399
+ pids=()
400
+ return "$failed"
401
+ }
402
+ # The compiles are this script's children, so a signal that stops it stops
403
+ # them too, before anything they write into is removed. A finished script
404
+ # has waited for every one of them, so there is nothing left to stop.
405
+ stop_jobs() {
406
+ if [ ${#pids[@]} -gt 0 ]; then
407
+ kill "${pids[@]}" 2>/dev/null || true
408
+ wait "${pids[@]}" 2>/dev/null || true
409
+ pids=()
410
+ fi
411
+ }
412
+ work=""
413
+ trap 'stop_jobs; [ -z "$work" ] || rm -rf -- "$work"' EXIT
414
+ trap 'exit 130' INT
415
+ trap 'exit 143' TERM
416
+ # A link's own objects go in a private directory of its own and go with it.
417
+ make_work() {
418
+ [ -n "$work" ] || work=$(mktemp -d "${TMPDIR:-/tmp}/nish-link.XXXXXX")
419
+ }
420
+ # A CC named by a relative path is named from here, not from a unit's directory.
421
+ case "$CC" in /*) ;; */*) CC="$(pwd)/$CC" ;; esac
422
+
423
+ # SQLite's compile-time options (https://sqlite.org/compile.html): the set the
424
+ # SQLite project recommends for a program that needs none of the legacy
425
+ # behaviours, less two of it. SQLITE_OMIT_DECLTYPE is left out because it
426
+ # removes sqlite3_column_decltype, which a binding reads to tell a column's
427
+ # declared type. SQLITE_OMIT_AUTOINIT is left out because it makes a call
428
+ # before sqlite3_initialize() undefined behaviour, and the check it saves is
429
+ # a branch in a few entry points. Double-quoted strings are identifiers only. There are no memory
430
+ # statistics, no progress callback, no shared cache and no loadable extensions.
431
+ # Expression depth is bounded by the stack, not a counter. A program has one
432
+ # thread unless it was built with --threads. SQLite takes no mutex for one
433
+ # thread (THREADSAFE=0). Under --threads it is multi-thread mode (=2): no mutex
434
+ # guards a connection, so a program must not share one across threads. wasi
435
+ # has no threads, so it gets 0 whatever the module says. NISH_SQLITE_CFLAGS is
436
+ # appended: `read` takes its first line and splits it on whitespace, so a
437
+ # word cannot hold a space and a quote is an ordinary character. A relative
438
+ # path in it (-Iinclude) resolves against runtime/sqlite/, the directory every
439
+ # unit is compiled from. A -D or -U in it for one of these names replaces the
440
+ # default, so the compile does not warn about a macro defined twice.
441
+ if [ "$threads" = 1 ] && [ "$profile" != wasi ]; then sqlite_threadsafe=2; else sqlite_threadsafe=0; fi
442
+ sqlite_extra=()
443
+ read -r -a sqlite_extra <<< "${NISH_SQLITE_CFLAGS:-}"
444
+ sqlite_defs=()
445
+ for d in -DSQLITE_DQS=0 -DSQLITE_DEFAULT_MEMSTATUS=0 -DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1 \
446
+ -DSQLITE_LIKE_DOESNT_MATCH_BLOBS -DSQLITE_MAX_EXPR_DEPTH=0 -DSQLITE_OMIT_DEPRECATED \
447
+ -DSQLITE_OMIT_PROGRESS_CALLBACK -DSQLITE_OMIT_SHARED_CACHE -DSQLITE_USE_ALLOCA \
448
+ -DSQLITE_OMIT_LOAD_EXTENSION "-DSQLITE_THREADSAFE=$sqlite_threadsafe"; do
449
+ name=${d#-D}; name=${name%%=*}
450
+ keep=1
451
+ for e in ${sqlite_extra[@]+"${sqlite_extra[@]}"}; do
452
+ case "$e" in "-D$name"|"-D$name="*|"-U$name") keep=0 ;; esac
453
+ done
454
+ if [ "$keep" = 1 ]; then sqlite_defs+=("$d"); fi
455
+ done
456
+ sqlite_defs+=(${sqlite_extra[@]+"${sqlite_extra[@]}"})
457
+
458
+ # Compiles every runtime/sqlite/*.c with the flags given, all at once, into
459
+ # $1/<unit>.o. Each unit is compiled from inside runtime/sqlite/ by its bare
460
+ # name, as the runtime is, so -g's DWARF names no directory the link was
461
+ # started in. sqlite3.c, the 9.6 MB amalgamation, is compiled to native code
462
+ # even where the profile compiles to LTO bitcode. As bitcode, every link of a
463
+ # program that uses it would generate its code again: measured on a program
464
+ # that opens a database and runs a query, that is 10.6 s a link at -O3,
465
+ # against 0.04 s for the native object. The adapter beside it stays LTO, and
466
+ # it is what inlines into the program. So `-flto` comes off for that one unit
467
+ # alone.
468
+ sqlite_units() {
469
+ local to=$1 unit flag
470
+ local -a flags
471
+ shift
472
+ for unit in "$sqlite_dir"/*.c; do
473
+ unit=${unit##*/}
474
+ flags=()
475
+ for flag in "$@"; do
476
+ case "$unit:$flag" in sqlite3.c:-flto) ;; *) flags+=("$flag") ;; esac
477
+ done
478
+ (cd -- "$sqlite_dir" && exec "$CC" "${flags[@]}" "${sqlite_defs[@]}" -I. -c "$unit" -o "$to/${unit%.c}.o") &
479
+ pids+=("$!")
480
+ done
481
+ join_jobs || { echo "error: runtime/sqlite/ did not compile" >&2; exit 1; }
482
+ }
483
+ # Adds the objects of runtime/sqlite/ to this link, in the order of the units.
484
+ # They are compiled into the link's own directory with the flags given, or
485
+ # taken from --sqlite-from. Nothing is added for a module that does not call
486
+ # SQLite.
487
+ sqlite_objects() {
488
+ local from=$sqlite_from unit
489
+ [ "$sqlite_wanted" = 1 ] || return 0
490
+ if [ -z "$from" ]; then
491
+ make_work
492
+ from="$work/sqlite"
493
+ mkdir -p -- "$from"
494
+ sqlite_units "$from" "$@"
495
+ fi
496
+ for unit in "$sqlite_dir"/*.c; do
497
+ unit=${unit##*/}
498
+ [ -f "$from/${unit%.c}.o" ] || { echo "error: --sqlite-from: $from/${unit%.c}.o is missing" >&2; exit 2; }
499
+ inputs+=("$from/${unit%.c}.o")
500
+ done
501
+ }
502
+
503
+ if [ -n "$sqlite_to" ]; then
504
+ mkdir -p -- "$sqlite_to"
505
+ sqlite_to=$(cd -- "$sqlite_to" && pwd)
506
+ sqlite_units "$sqlite_to" "${compile[@]}"
507
+ exit 0
508
+ fi
509
+
510
+ if [ -n "$objects_to" ]; then
511
+ runtime=""
512
+ for i in "${inputs[@]}"; do
513
+ case "$i" in
514
+ */runtime.c|runtime.c) runtime="$i" ;;
515
+ */runtime-*.c|runtime-*.c) ;; # its siblings, added above
516
+ *) echo "error: --runtime-objects compiles the runtime alone, not $i" >&2; exit 2 ;;
517
+ esac
518
+ done
519
+ [ -n "$runtime" ] || { echo "error: --runtime-objects needs runtime/runtime.c" >&2; exit 2; }
520
+ mkdir -p -- "$objects_to"
521
+ objects_to=$(cd -- "$objects_to" && pwd)
522
+ # Each unit is compiled from inside its own directory, by its bare name, so
523
+ # an object holds no trace of where the compile was started: -g's DWARF names
524
+ # the file `runtime.c` in the runtime's own directory, whichever directory
525
+ # the first link that compiled it was run in.
526
+ runtime_dir=$(dirname -- "$runtime")
527
+ for unit in runtime.c $halves; do
528
+ (cd -- "$runtime_dir" && exec "$CC" "${compile[@]}" -c "$unit" -o "$objects_to/${unit%.c}.o") &
529
+ pids+=("$!")
530
+ done
531
+ join_jobs || { echo "error: the runtime did not compile" >&2; exit 1; }
532
+ exit 0
533
+ fi
534
+
535
+ if [ -n "$objects_from" ]; then
536
+ for unit in runtime.c $halves; do
537
+ [ -f "$objects_from/${unit%.c}.o" ] || { echo "error: --runtime-from: $objects_from/${unit%.c}.o is missing" >&2; exit 2; }
538
+ done
539
+ make_work
540
+ objects=()
541
+ n=0
542
+ for i in "${inputs[@]}"; do
543
+ case "$i" in */runtime.c|runtime.c) echo "error: --runtime-from links the runtime in $objects_from, so $i is not an input" >&2; exit 2 ;; esac
544
+ n=$((n + 1))
545
+ "$CC" "${compile[@]}" -c "$i" -o "$work/$n.o" &
546
+ pids+=("$!")
547
+ objects+=("$work/$n.o")
548
+ done
549
+ join_jobs || { echo "error: a module did not compile" >&2; exit 1; }
550
+ # In the order the one-command link names them: the inputs, then runtime.c
551
+ # and its siblings.
552
+ for unit in runtime.c $halves; do
553
+ objects+=("$objects_from/${unit%.c}.o")
554
+ done
555
+ inputs=("${objects[@]}")
556
+ # clang runs dsymutil itself when one command compiles and links with -g on
557
+ # macOS; a link of objects does not, so it is run here, while the program's
558
+ # objects the debug map points at are still there.
559
+ if [ "$debug" = 1 ] && [ "$(uname -s)" = Darwin ]; then dsym=1; else dsym=0; fi
560
+ fi
561
+
186
562
  # The ${arr[@]+"${arr[@]}"} spelling below is not a style tic: macOS ships bash
187
563
  # 3.2 (Apple will not ship GPLv3), where expanding an empty array as "${arr[@]}"
188
564
  # under `set -u` is a fatal "unbound variable" -- bash 4.4 made it legal, which is
189
565
  # why Linux never noticed. `pgo`, `elf`, `strip_flag` and `libs` are all empty on
190
566
  # ordinary builds, so please do not simplify these back.
567
+ case "$profile" in debug|speed|size) sqlite_objects "${compile[@]}" ;; esac
191
568
  case "$profile" in
192
569
  debug)
193
570
  "$CC" "${common[@]}" "${inputs[@]}" -lm -o "$out" ;;
194
- speed)
195
- "$CC" "${common[@]}" -O3 -flto -DNDEBUG ${pgo[@]+"${pgo[@]}"} \
196
- -ffunction-sections -fdata-sections -fomit-frame-pointer \
197
- -fno-asynchronous-unwind-tables -fno-unwind-tables ${elf[@]+"${elf[@]}"} \
198
- "${gc[@]}" ${strip_flag[@]+"${strip_flag[@]}"} "${inputs[@]}" -lm -o "$out" ;;
199
- size)
200
- "$CC" "${common[@]}" -Oz -flto -DNDEBUG ${pgo[@]+"${pgo[@]}"} \
201
- -ffunction-sections -fdata-sections -fomit-frame-pointer \
202
- -fno-asynchronous-unwind-tables -fno-unwind-tables ${elf[@]+"${elf[@]}"} \
203
- -fno-stack-protector -fvisibility=hidden \
571
+ speed|size)
572
+ "$CC" "${common[@]}" "${optimise[@]}" ${lto_parts[@]+"${lto_parts[@]}"} \
204
573
  "${gc[@]}" ${strip_flag[@]+"${strip_flag[@]}"} "${inputs[@]}" -lm -o "$out" ;;
205
574
  wasm)
206
575
  # clang resolves wasm-ld next to its own binary first, then on PATH; ask it
@@ -210,6 +579,38 @@ case "$profile" in
210
579
  echo "error: the wasm profile needs wasm-ld (install lld; on macOS: brew install llvm@18)" >&2
211
580
  exit 2
212
581
  fi
582
+ # IR compiled for the host rather than with `--target wasm32` calls std/crypto/aes.ts's
583
+ # AES kernels behind `nish_aes_hw` (src/kernels.ts). runtime-aes.c's wasm32 build
584
+ # answers 0 there, so the bitsliced code runs, and it is added only for such IR:
585
+ # every other module links the bytes it always did.
586
+ # A caller that names runtime-aes.c itself is left alone, as `halves` leaves it:
587
+ # one object named twice is a duplicate-symbol error.
588
+ aes_named=0
589
+ aes_wanted=0
590
+ for i in ${inputs[@]+"${inputs[@]}"}; do
591
+ case "$i" in
592
+ */runtime-aes.c|runtime-aes.c) aes_named=1 ;;
593
+ *.ll) if grep -q '@nish_aes_hw(' "$i"; then aes_wanted=1; fi ;;
594
+ esac
595
+ done
596
+ if [ "$aes_wanted" = 1 ] && [ "$aes_named" = 0 ]; then
597
+ inputs+=("$(cd "$(dirname "$0")/../runtime" && pwd)/runtime-aes.c")
598
+ fi
599
+ # IR of either kind that imports std/json or calls nish/bytes's byte-class runs
600
+ # calls runtime-bytes.c's kernels (src/kernels.ts), which compile freestanding:
601
+ # the scalar path, or the 128-bit one under -msimd128. It is added the same way,
602
+ # only for such IR and only when the caller has not named it.
603
+ bytes_named=0
604
+ bytes_wanted=0
605
+ for i in ${inputs[@]+"${inputs[@]}"}; do
606
+ case "$i" in
607
+ */runtime-bytes.c|runtime-bytes.c) bytes_named=1 ;;
608
+ *.ll) if grep -q '@nish_bytes_skip_' "$i"; then bytes_wanted=1; fi ;;
609
+ esac
610
+ done
611
+ if [ "$bytes_wanted" = 1 ] && [ "$bytes_named" = 0 ]; then
612
+ inputs+=("$(cd "$(dirname "$0")/../runtime" && pwd)/runtime-bytes.c")
613
+ fi
213
614
  # -mbulk-memory lowers llvm.memset/memcpy (`new Array<T>(n)`, `push` growth) to the
214
615
  # memory.fill/memory.copy instructions instead of libc calls the freestanding link lacks.
215
616
  "$CC" -Wno-override-module --target=wasm32-unknown-unknown -Oz -nostdlib -mbulk-memory \
@@ -251,6 +652,10 @@ case "$profile" in
251
652
  exit 2
252
653
  fi
253
654
  fi
655
+ # SQLite builds on wasi-libc as it is: the amalgamation sees __wasi__ and
656
+ # takes dot-file locking, no mmap and no WAL shared memory.
657
+ sqlite_objects --target=wasm32-wasi --sysroot="$sysroot" -Oz -DNDEBUG \
658
+ ${tls[@]+"${tls[@]}"} -ffunction-sections -fdata-sections
254
659
  "$CC" -Wno-override-module --target=wasm32-wasi --sysroot="$sysroot" -Oz -DNDEBUG \
255
660
  ${tls[@]+"${tls[@]}"} \
256
661
  -ffunction-sections -fdata-sections -Wl,--gc-sections -Wl,--strip-all \
@@ -277,12 +682,16 @@ case "$profile" in
277
682
  case "$(uname -s)" in Darwin) shared+=(-Wl,-undefined,dynamic_lookup) ;; esac
278
683
  # runtime/nish.h is the public ABI header the generated shim includes.
279
684
  runtime_inc="$(cd "$(dirname "$0")/../runtime" && pwd)"
280
- "$CC" "${common[@]}" -O3 -flto -DNDEBUG ${pgo[@]+"${pgo[@]}"} -I"$node_inc" -I"$runtime_inc" \
281
- -ffunction-sections -fdata-sections -fomit-frame-pointer \
282
- -fno-asynchronous-unwind-tables -fno-unwind-tables ${elf[@]+"${elf[@]}"} \
685
+ # SQLite's API stays inside the addon: a Node linked against a system
686
+ # libsqlite3 exports the same names, and a call bound to that copy would
687
+ # run another version with other options.
688
+ sqlite_objects "${compile[@]}" -fPIC -fvisibility=hidden
689
+ "$CC" "${common[@]}" "${optimise[@]}" -I"$node_inc" -I"$runtime_inc" \
283
690
  "${shared[@]}" "${gc[@]}" ${strip_flag[@]+"${strip_flag[@]}"} "${inputs[@]}" -lm -o "$out" ;;
284
691
  *) echo "error: unknown profile '$profile'" >&2; exit 2 ;;
285
692
  esac
286
693
 
694
+ if [ "${dsym:-0}" = 1 ]; then "$("$CC" -print-prog-name=dsymutil)" "$out"; fi
695
+
287
696
  # `wc -c` pads with spaces on macOS; strip them so the number is clean.
288
697
  printf '%s: %s bytes (%s)\n' "$out" "$(wc -c < "$out" | tr -d ' ')" "$profile"
@@ -0,0 +1,23 @@
1
+ Boost Software License - Version 1.0 - August 17th, 2003
2
+
3
+ Permission is hereby granted, free of charge, to any person or organization
4
+ obtaining a copy of the software and accompanying documentation covered by
5
+ this license (the "Software") to use, reproduce, display, distribute,
6
+ execute, and transmit the Software, and to prepare derivative works of the
7
+ Software, and to permit third-parties to whom the Software is furnished to
8
+ do so, all subject to the following:
9
+
10
+ The copyright notices in the Software and this entire statement, including
11
+ the above license grant, this restriction and the following disclaimer,
12
+ must be included in all copies of the Software, in whole or in part, and
13
+ all derivative works of the Software, unless such copies or derivative
14
+ works are solely in the form of machine-executable object code generated by
15
+ a source language processor.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
20
+ SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
21
+ FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
22
+ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23
+ DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,8 @@
1
+ ====================================================
2
+ Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
3
+
4
+ Developed at SunPro, a Sun Microsystems, Inc. business.
5
+ Permission to use, copy, modify, and distribute this
6
+ software is freely granted, provided that this notice
7
+ is preserved.
8
+ ====================================================