numbl 0.2.0 → 0.3.3

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 (127) hide show
  1. package/binding.gyp +24 -36
  2. package/dist-cli/cli.js +16096 -18209
  3. package/dist-lib/graphics/types.d.ts +22 -0
  4. package/dist-lib/lib.d.ts +1 -0
  5. package/dist-lib/lib.js +61324 -62782
  6. package/dist-lib/numbl-core/executeCode.d.ts +11 -21
  7. package/dist-lib/numbl-core/executors/cJit/builtins.d.ts +30 -0
  8. package/dist-lib/numbl-core/executors/cJit/chainCodegen.d.ts +59 -0
  9. package/dist-lib/numbl-core/executors/cJit/chainExecutor.d.ts +27 -0
  10. package/dist-lib/numbl-core/executors/cJit/chainPass.d.ts +42 -0
  11. package/dist-lib/numbl-core/executors/cJit/codegen.d.ts +44 -0
  12. package/dist-lib/numbl-core/executors/cJit/compile.d.ts +45 -0
  13. package/dist-lib/numbl-core/executors/cJit/elemwiseCodegen.d.ts +23 -0
  14. package/dist-lib/numbl-core/executors/cJit/elemwiseStructural.d.ts +33 -0
  15. package/dist-lib/numbl-core/executors/cJit/fuseAnalyze.d.ts +39 -0
  16. package/dist-lib/numbl-core/executors/cJit/fuseCodegen.d.ts +16 -0
  17. package/dist-lib/numbl-core/executors/cJit/fuseExecutor.d.ts +28 -0
  18. package/dist-lib/numbl-core/executors/cJit/loopExecutor.d.ts +32 -0
  19. package/dist-lib/numbl-core/executors/cJit/register.d.ts +10 -0
  20. package/dist-lib/numbl-core/executors/cJit/whitelist.d.ts +15 -0
  21. package/dist-lib/numbl-core/executors/cache.d.ts +26 -0
  22. package/dist-lib/numbl-core/executors/context.d.ts +76 -0
  23. package/dist-lib/numbl-core/executors/index.d.ts +17 -0
  24. package/dist-lib/numbl-core/executors/jsJit/callExecutor.d.ts +25 -0
  25. package/dist-lib/numbl-core/{jit/js → executors/jsJit/codegen}/jitCodegen.d.ts +2 -2
  26. package/dist-lib/numbl-core/{jit/js → executors/jsJit/codegen}/jitCodegenHoist.d.ts +1 -1
  27. package/dist-lib/numbl-core/executors/jsJit/codegen/jsMultiReduction.d.ts +67 -0
  28. package/dist-lib/numbl-core/executors/jsJit/helpers/alloc.d.ts +12 -0
  29. package/dist-lib/numbl-core/{jit/js → executors/jsJit/helpers}/jitHelpers.d.ts +2 -2
  30. package/dist-lib/numbl-core/{jit/js → executors/jsJit/helpers}/jitHelpersComplex.d.ts +1 -1
  31. package/dist-lib/numbl-core/executors/jsJit/helpers/jitHelpersIndex.d.ts +33 -0
  32. package/dist-lib/numbl-core/{jit/js → executors/jsJit/helpers}/jitHelpersTensor.d.ts +7 -7
  33. package/dist-lib/numbl-core/executors/jsJit/jitCall.d.ts +59 -0
  34. package/dist-lib/numbl-core/executors/jsJit/jitLoop.d.ts +53 -0
  35. package/dist-lib/numbl-core/executors/jsJit/jitTopLevel.d.ts +44 -0
  36. package/dist-lib/numbl-core/executors/jsJit/loopExecutor.d.ts +15 -0
  37. package/dist-lib/numbl-core/{jit/jitLoopAnalysis.d.ts → executors/jsJit/lower/blockAnalysis.d.ts} +5 -5
  38. package/dist-lib/numbl-core/{jit → executors/jsJit/lower}/jitBailSafety.d.ts +1 -1
  39. package/dist-lib/numbl-core/{jit → executors/jsJit/lower}/jitLower.d.ts +18 -4
  40. package/dist-lib/numbl-core/{jit → executors/jsJit/lower}/jitLowerExpr.d.ts +11 -2
  41. package/dist-lib/numbl-core/{jit → executors/jsJit/lower}/jitLowerStmt.d.ts +2 -2
  42. package/dist-lib/numbl-core/{jit → executors/jsJit/lower}/jitLowerTypes.d.ts +2 -2
  43. package/dist-lib/numbl-core/{jit → executors/jsJit/lower}/scalarEmit.d.ts +2 -2
  44. package/dist-lib/numbl-core/executors/jsJit/shared.d.ts +120 -0
  45. package/dist-lib/numbl-core/executors/jsJit/topLevelExecutor.d.ts +17 -0
  46. package/dist-lib/numbl-core/executors/lowering.d.ts +166 -0
  47. package/dist-lib/numbl-core/executors/plugins.d.ts +39 -0
  48. package/dist-lib/numbl-core/executors/registry.d.ts +148 -0
  49. package/dist-lib/numbl-core/executors/types.d.ts +103 -0
  50. package/dist-lib/numbl-core/fileIOAdapter.d.ts +2 -0
  51. package/dist-lib/numbl-core/functionResolve.d.ts +7 -0
  52. package/dist-lib/numbl-core/helpers/check-helpers.d.ts +4 -5
  53. package/dist-lib/numbl-core/helpers/linsolve.d.ts +2 -3
  54. package/dist-lib/numbl-core/helpers/prng.d.ts +1 -2
  55. package/dist-lib/numbl-core/interpreter/builtins/datetime.d.ts +2 -1
  56. package/dist-lib/numbl-core/interpreter/builtins/misc.d.ts +4 -1
  57. package/dist-lib/numbl-core/interpreter/builtins/types.d.ts +4 -91
  58. package/dist-lib/numbl-core/interpreter/interpreter.d.ts +34 -44
  59. package/dist-lib/numbl-core/interpreter/interpreterSpecialBuiltins.d.ts +6 -3
  60. package/dist-lib/numbl-core/interpreter/types.d.ts +27 -12
  61. package/dist-lib/numbl-core/{jit/jitTypes.d.ts → jitTypes.d.ts} +15 -1
  62. package/dist-lib/numbl-core/jsUserFunctions.d.ts +8 -0
  63. package/dist-lib/numbl-core/lowering/loweringContext.d.ts +24 -0
  64. package/dist-lib/numbl-core/native/lapack-bridge.d.ts +3 -3
  65. package/dist-lib/numbl-core/parser/types.d.ts +20 -0
  66. package/dist-lib/numbl-core/runtime/constructors.d.ts +6 -6
  67. package/dist-lib/numbl-core/runtime/cow.d.ts +33 -0
  68. package/dist-lib/numbl-core/runtime/index.d.ts +3 -2
  69. package/dist-lib/numbl-core/runtime/indexing.d.ts +6 -1
  70. package/dist-lib/numbl-core/runtime/plotBuiltinDispatch.d.ts +86 -0
  71. package/dist-lib/numbl-core/runtime/plotUtils.d.ts +17 -2
  72. package/dist-lib/numbl-core/runtime/refcount.d.ts +85 -0
  73. package/dist-lib/numbl-core/runtime/runtime.d.ts +27 -66
  74. package/dist-lib/numbl-core/runtime/runtimeDispatch.d.ts +2 -2
  75. package/dist-lib/numbl-core/runtime/runtimeIndexing.d.ts +2 -2
  76. package/dist-lib/numbl-core/runtime/runtimeMemberAccess.d.ts +1 -1
  77. package/dist-lib/numbl-core/runtime/runtimePlot.d.ts +1 -0
  78. package/dist-lib/numbl-core/runtime/struct-access.d.ts +2 -1
  79. package/dist-lib/numbl-core/runtime/types.d.ts +104 -62
  80. package/dist-lib/numbl-core/runtime/utils.d.ts +2 -8
  81. package/dist-lib/numbl-core/version.d.ts +1 -1
  82. package/dist-plot-viewer/assets/index-COAM8o1E.js +4426 -0
  83. package/dist-plot-viewer/index.html +1 -1
  84. package/native/lapack_linsolve.cpp +1 -1
  85. package/native/numbl_addon.cpp +9 -0
  86. package/native/numbl_addon_common.h +2 -2
  87. package/native/ops/comparison.c +1 -1
  88. package/package.json +3 -8
  89. package/dist-lib/numbl-core/jit/c/abi.d.ts +0 -90
  90. package/dist-lib/numbl-core/jit/c/assemble.d.ts +0 -56
  91. package/dist-lib/numbl-core/jit/c/classify.d.ts +0 -70
  92. package/dist-lib/numbl-core/jit/c/compile.d.ts +0 -37
  93. package/dist-lib/numbl-core/jit/c/context.d.ts +0 -152
  94. package/dist-lib/numbl-core/jit/c/emit/assign.d.ts +0 -20
  95. package/dist-lib/numbl-core/jit/c/emit/complexScalar.d.ts +0 -18
  96. package/dist-lib/numbl-core/jit/c/emit/fused.d.ts +0 -42
  97. package/dist-lib/numbl-core/jit/c/emit/helpers.d.ts +0 -40
  98. package/dist-lib/numbl-core/jit/c/emit/index.d.ts +0 -14
  99. package/dist-lib/numbl-core/jit/c/emit/scalar.d.ts +0 -23
  100. package/dist-lib/numbl-core/jit/c/emit/stmt.d.ts +0 -25
  101. package/dist-lib/numbl-core/jit/c/emit/tensor.d.ts +0 -127
  102. package/dist-lib/numbl-core/jit/c/emit/userCall.d.ts +0 -58
  103. package/dist-lib/numbl-core/jit/c/epilogue.d.ts +0 -26
  104. package/dist-lib/numbl-core/jit/c/feasibility.d.ts +0 -44
  105. package/dist-lib/numbl-core/jit/c/hybrid.d.ts +0 -42
  106. package/dist-lib/numbl-core/jit/c/install.d.ts +0 -15
  107. package/dist-lib/numbl-core/jit/c/parityError.d.ts +0 -26
  108. package/dist-lib/numbl-core/jit/c/prelude.d.ts +0 -37
  109. package/dist-lib/numbl-core/jit/c/registry.d.ts +0 -51
  110. package/dist-lib/numbl-core/jit/c/visit.d.ts +0 -63
  111. package/dist-lib/numbl-core/jit/e1/install.d.ts +0 -13
  112. package/dist-lib/numbl-core/jit/e1/kernelEmit.d.ts +0 -54
  113. package/dist-lib/numbl-core/jit/e1/openmpFlag.d.ts +0 -13
  114. package/dist-lib/numbl-core/jit/e1/scalarFnKernel.d.ts +0 -44
  115. package/dist-lib/numbl-core/jit/fusedChainHelpers.d.ts +0 -65
  116. package/dist-lib/numbl-core/jit/fusedScalarEmit.d.ts +0 -61
  117. package/dist-lib/numbl-core/jit/fusion.d.ts +0 -71
  118. package/dist-lib/numbl-core/jit/fusionOps.d.ts +0 -25
  119. package/dist-lib/numbl-core/jit/index.d.ts +0 -7
  120. package/dist-lib/numbl-core/jit/jitLoop.d.ts +0 -25
  121. package/dist-lib/numbl-core/jit/jitTopLevel.d.ts +0 -22
  122. package/dist-lib/numbl-core/jit/js/jitHelpersIndex.d.ts +0 -33
  123. package/dist-lib/numbl-core/jit/js/jsFusedCodegen.d.ts +0 -17
  124. package/dist-lib/numbl-core/runtime/alloc.d.ts +0 -23
  125. package/dist-plot-viewer/assets/index-GiUNnMQg.js +0 -4426
  126. package/native/jit_runtime/jit_runtime.c +0 -261
  127. package/native/jit_runtime/jit_runtime.h +0 -204
@@ -0,0 +1,25 @@
1
+ /**
2
+ * js-jit-call — JS codegen executor for the call shape.
3
+ *
4
+ * Lowering is the dispatcher's job. The call executor receives the
5
+ * lowered IR (with pre-computed feasibility flags) as the first arg
6
+ * to propose, decides whether to commit, and produces a compiled JS
7
+ * artifact on commit.
8
+ *
9
+ * - `propose()` filters on `lowered.kind === "call"`, applies
10
+ * IO+bail-risk feasibility check. Returns null to decline.
11
+ *
12
+ * - `compile()` calls `generateCallJS` against the lowered IR.
13
+ * Cached by the registry under the classification's cacheKey.
14
+ *
15
+ * - `run()` calls `runCallCompiled`. JitBailToInterpreter →
16
+ * transient bail.
17
+ */
18
+ import type { Executor } from "../types.js";
19
+ import { type CallLowered, type CallCompiled } from "./jitCall.js";
20
+ interface JsJitCallData {
21
+ readonly lowered: CallLowered;
22
+ readonly args: readonly unknown[];
23
+ }
24
+ export declare const jsJitCallExecutor: Executor<JsJitCallData, CallCompiled | null>;
25
+ export {};
@@ -3,5 +3,5 @@
3
3
  *
4
4
  * IR walkers for hoist-pass data collection are in jitCodegenHoist.ts.
5
5
  */
6
- import { type JitStmt } from "../jitTypes.js";
7
- export declare function generateJS(body: JitStmt[], params: string[], outputs: string[], nargout: number, localVars: Set<string>, fileName?: string, fuse?: boolean, experimental?: string): string;
6
+ import { type JitStmt } from "../../../jitTypes.js";
7
+ export declare function generateJS(body: JitStmt[], params: string[], outputs: string[], nargout: number, localVars: Set<string>, fileName?: string): string;
@@ -5,7 +5,7 @@
5
5
  * for hoisting tensor aliases, struct field reads, and struct array
6
6
  * element reads to the top of the generated function.
7
7
  */
8
- import type { JitStmt } from "../jitTypes.js";
8
+ import type { JitStmt } from "../../../jitTypes.js";
9
9
  /**
10
10
  * Hoisted aliases for a tensor variable read/written in the loop body.
11
11
  * Maps the original variable name to local JS identifiers for its
@@ -0,0 +1,67 @@
1
+ /**
2
+ * JS-side multi-reduction block emission.
3
+ *
4
+ * Detection + dispatch for scalar Assigns of the form
5
+ *
6
+ * acc = <expr tree with >=2 reductions over a single tensor Var>
7
+ *
8
+ * On match, the JS codegen emits a block that runs a single-pass
9
+ * inline JS reduction loop, stashes each reduction result in a local,
10
+ * and then emits the Assign's RHS with each reduction Call substituted
11
+ * by the local that holds its value. The RHS-substitution contract
12
+ * lives in `jitCodegen._multiReductionSubst`, which this module
13
+ * installs and clears around the `emitExpr` call the caller provides.
14
+ *
15
+ * Limited to:
16
+ * - sum / prod / max / min / mean (short-circuit any/all excluded)
17
+ * - a single tensor Var argument to every reduction
18
+ * - all reductions read the same tensor name
19
+ * - the tensor is real-typed
20
+ * - at least two reductions in the RHS
21
+ *
22
+ * NaN semantics for max/min are matched to MATLAB omit-NaN behaviour:
23
+ * IEEE unordered compare already skips NaN, and an `any_non_nan` flag
24
+ * drives the all-NaN → NaN fallback.
25
+ */
26
+ import type { JitExpr, JitStmt } from "../../../jitTypes.js";
27
+ type MultiReduceOp = "sum" | "prod" | "max" | "min" | "mean";
28
+ export interface MultiReductionMatch {
29
+ /** The scalar Assign being emitted (target + original RHS). */
30
+ stmt: JitStmt & {
31
+ tag: "Assign";
32
+ };
33
+ /** The tensor variable name all reductions read. */
34
+ tensorName: string;
35
+ /** Distinct ops that actually appear, in first-occurrence order. Used
36
+ * to size the kernel's output buffer and decide which accumulators
37
+ * to declare on the JS fallback path. */
38
+ ops: MultiReduceOp[];
39
+ /** Every reduction Call node found in the RHS, paired with its op.
40
+ * The JS emitter builds a substitution map from each Call to the
41
+ * local that holds its result. */
42
+ sites: {
43
+ call: JitExpr & {
44
+ tag: "Call";
45
+ };
46
+ op: MultiReduceOp;
47
+ }[];
48
+ }
49
+ /** Try to match the multi-reduction pattern on a single statement. */
50
+ export declare function tryMatchMultiReduction(stmt: JitStmt): MultiReductionMatch | null;
51
+ /**
52
+ * Emit the multi-reduction block. Writes into `lines`.
53
+ *
54
+ * The block:
55
+ * 1. Aliases `<tensorName>.data` to a local and reads its length.
56
+ * 2. Emits a single-pass JS loop that updates each accumulator.
57
+ * 3. Post-loop: `mean = sum / n`, and a NaN fixup for max/min when
58
+ * every input element was NaN.
59
+ * 4. Installs `_multiReductionSubst` pointing each reduction Call at
60
+ * its local, emits the Assign's RHS via the caller-provided
61
+ * `emitExpr`, and writes the final `<target> = <rhs>;`.
62
+ */
63
+ export declare function emitMultiReductionBlock(lines: string[], indent: string, match: MultiReductionMatch, mangleName: (n: string) => string, emitExprWithSubst: (expr: JitExpr, subst: Map<JitExpr, string>) => string): void;
64
+ /** Reset per-function counter so generated names stay stable between
65
+ * compiles of the same IR. Called from `generateJS`. */
66
+ export declare function resetMultiReductionState(): void;
67
+ export {};
@@ -0,0 +1,12 @@
1
+ /** Allocate a Float64Array. Thin wrapper around `new Float64Array(x)`
2
+ * kept as a single allocation site so future strategies (pooling,
3
+ * COW-aware allocation, etc.) can be reintroduced without touching
4
+ * every callsite. */
5
+ export declare function allocFloat64Array(x: number | number[] | Float64Array): Float64Array;
6
+ /** No-op. Kept as a placeholder for future explicit-release strategies
7
+ * (e.g. pool release, arena reset). With pure JS GC, scratch buffers
8
+ * are reclaimed when the wrapper goes out of scope. */
9
+ export declare function releaseFloat64Array(_buf: Float64Array): void;
10
+ /** Pass-through. Kept as a placeholder for future scratch-arena
11
+ * strategies. Today it just runs `fn` and returns its result. */
12
+ export declare function withScratch<T>(fn: () => T): T;
@@ -12,7 +12,7 @@
12
12
  */
13
13
  export { re, im, mkc, cAdd, cSub, cMul, cDiv, cNeg, cConj, cAngle, cTruthy, } from "./jitHelpersComplex.js";
14
14
  export { bce, idx1, idx2, idxN, idx1r, idx2r, idx3r, idx1r_h, idx2r_h, idx3r_h, set1r_h, set2r_h, set3r_h, setRange1r_h, setCol2r_h, subarrayCopy1r, subarrayCopy1rRow, } from "./jitHelpersIndex.js";
15
- export { makeTensor, tensorBinaryOp, tensorCompareOp, tensorUnary, tensorNeg, vconcatGrow1r, unshare, shareTensor, asTensor, tDouble, tSum, tAdd, tSub, tMul, tDiv, tPow, tEq, tNeq, tLt, tLe, tGt, tGe, } from "./jitHelpersTensor.js";
15
+ export { makeTensor, tensorBinaryOp, tensorCompareOp, tensorUnary, tensorNeg, vconcatGrow1r, unshare, asTensor, tDouble, tSum, tAdd, tSub, tMul, tDiv, tPow, tEq, tNeq, tLt, tLe, tGt, tGe, } from "./jitHelpersTensor.js";
16
16
  export { JitBailToInterpreter } from "./jitHelpersIndex.js";
17
17
  /**
18
18
  * Thrown when a function handle called from JIT code returns a type
@@ -26,7 +26,7 @@ export declare class JitFuncHandleBailError extends Error {
26
26
  constructor(fnName: string, expectedType: string, actualType: string);
27
27
  }
28
28
  export declare const jitHelpers: Record<string, unknown>;
29
- import type { IBuiltin } from "../../interpreter/builtins/index.js";
29
+ import type { IBuiltin } from "../../../interpreter/builtins/index.js";
30
30
  /**
31
31
  * Build a per-runtime jitHelpers snapshot. Uses a single spread expression
32
32
  * for a fresh V8 hidden class (critical for inline-cache performance).
@@ -2,7 +2,7 @@
2
2
  * Complex number arithmetic helpers for JIT-compiled code.
3
3
  * These are pure functions used by the $h helpers object.
4
4
  */
5
- import type { RuntimeComplexNumber } from "../../runtime/types.js";
5
+ import { RuntimeComplexNumber } from "../../../runtime/types.js";
6
6
  export declare function re(v: unknown): number;
7
7
  export declare function im(v: unknown): number;
8
8
  export declare function mkc(r: number, i: number): number | RuntimeComplexNumber;
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Tensor indexing helpers for JIT-compiled code.
3
+ *
4
+ * Three tiers of helpers, from most general to most specialized:
5
+ * 1. Generic (idx1, idx2, idxN) — handle any base type, real or complex
6
+ * 2. Real-tensor fast path (idx1r, idx2r, idx3r) — skip type/imag checks
7
+ * 3. Hoisted-base (idx*r_h, set*r_h) — take pre-extracted data/len/shape
8
+ *
9
+ * All helpers use 1-based MATLAB indexing and include per-dimension bounds
10
+ * checks to ensure consistency with the interpreter.
11
+ */
12
+ import { type RuntimeTensor } from "../../../runtime/types.js";
13
+ export declare class JitBailToInterpreter extends Error {
14
+ readonly reason: string;
15
+ constructor(reason: string);
16
+ }
17
+ export declare function bce(): never;
18
+ export declare function idx1(base: unknown, i: number): unknown;
19
+ export declare function idx2(base: unknown, ri: number, ci: number): unknown;
20
+ export declare function idxN(base: unknown, indices: number[]): unknown;
21
+ export declare function idx1r(base: RuntimeTensor, i: number): number;
22
+ export declare function idx2r(base: RuntimeTensor, ri: number, ci: number): number;
23
+ export declare function idx3r(base: RuntimeTensor, i1: number, i2: number, i3: number): number;
24
+ export declare function idx1r_h(data: Float64Array, len: number, i: number): number;
25
+ export declare function idx2r_h(data: Float64Array, len: number, rows: number, ri: number, ci: number): number;
26
+ export declare function idx3r_h(data: Float64Array, len: number, d0: number, d1: number, i1: number, i2: number, i3: number): number;
27
+ export declare function set1r_h(data: Float64Array, len: number, i: number, v: number): void;
28
+ export declare function set2r_h(data: Float64Array, len: number, rows: number, ri: number, ci: number, v: number): void;
29
+ export declare function set3r_h(data: Float64Array, len: number, d0: number, d1: number, i1: number, i2: number, i3: number, v: number): void;
30
+ export declare function setRange1r_h(dstData: Float64Array, dstLen: number, dstStart: number, dstEnd: number, srcData: Float64Array, srcLen: number, srcStart: number, srcEnd: number): void;
31
+ export declare function subarrayCopy1r(srcData: Float64Array, srcLen: number, start: number, end: number): RuntimeTensor;
32
+ export declare function subarrayCopy1rRow(srcData: Float64Array, srcLen: number, start: number, end: number): RuntimeTensor;
33
+ export declare function setCol2r_h(dstData: Float64Array, dstRows: number, dstLen: number, col: number, srcData: Float64Array, srcLen: number): void;
@@ -3,13 +3,9 @@
3
3
  * Handles element-wise binary/unary ops, comparison ops, concat, and
4
4
  * tensor coercion for struct-array field access.
5
5
  */
6
- import { type FloatXArrayType, type RuntimeTensor } from "../../runtime/types.js";
7
- import type { RuntimeComplexNumber } from "../../runtime/types.js";
8
- export declare function makeTensor(data: FloatXArrayType, imag: FloatXArrayType | undefined, shape: number[]): RuntimeTensor;
9
- /** Bump rc on a tensor (no-op for non-tensor values). Used for var-to-var
10
- * JIT assignments so the aliased tensor is no longer eligible for
11
- * in-place buffer reuse. */
12
- export declare function shareTensor(v: unknown): unknown;
6
+ import { RuntimeTensor } from "../../../runtime/types.js";
7
+ import type { RuntimeComplexNumber } from "../../../runtime/types.js";
8
+ export declare function makeTensor(data: Float64Array, imag: Float64Array | undefined, shape: number[]): RuntimeTensor;
13
9
  type ScalarVal = number | RuntimeComplexNumber;
14
10
  export declare function tensorBinaryOp(a: unknown, b: unknown, realOp: (x: number, y: number) => number, complexOp: (a: ScalarVal, b: ScalarVal) => ScalarVal): RuntimeTensor | ScalarVal;
15
11
  export declare function tensorCompareOp(a: unknown, b: unknown, cmp: (x: number, y: number) => boolean): RuntimeTensor;
@@ -18,6 +14,10 @@ export declare function tensorNeg(dest: unknown, a: RuntimeTensor): RuntimeTenso
18
14
  export declare function tDouble(v: unknown): unknown;
19
15
  export declare function tSum(v: unknown): unknown;
20
16
  export declare function vconcatGrow1r(base: unknown, v: number): RuntimeTensor;
17
+ /** Unconditionally allocate a fresh wrapper + buffer for `t`. The caller
18
+ * can mutate the result without affecting any other alias of the input.
19
+ * Used by the JIT path on entry to a write-target hoisted alias; the
20
+ * runtime indexed-store path uses the sweep instead. */
21
21
  export declare function unshare(t: unknown): RuntimeTensor;
22
22
  export declare function asTensor(v: unknown): RuntimeTensor;
23
23
  export declare const tAdd: (dest: unknown, a: unknown, b: unknown) => RuntimeTensor | ScalarVal;
@@ -0,0 +1,59 @@
1
+ /**
2
+ * User-function call — phased pipeline.
3
+ *
4
+ * 1. `classifyCall` — cheap classification + cacheKey synthesis.
5
+ * Type-infer args, prune `exact` for numeric scalars, type
6
+ * widening using fn._lastJitArgTypes. Returns null on
7
+ * type-unknown args or `~` placeholder params.
8
+ *
9
+ * 2. `lowerCall` — IR lowering.
10
+ *
11
+ * 3. `generateCallJS` — JS codegen + `new Function`.
12
+ *
13
+ * 4. `runCallCompiled` — push call frame + cleanup scope, invoke
14
+ * compiled fn, pop frame. Translates JitBailToInterpreter into
15
+ * a transient bail.
16
+ */
17
+ import type { Interpreter } from "../../interpreter/interpreter.js";
18
+ import type { FunctionDef } from "../../interpreter/types.js";
19
+ import { type JitType } from "../../jitTypes.js";
20
+ import { type LoweringResult } from "./lower/jitLower.js";
21
+ /** Cheap pre-lowering data. */
22
+ export interface CallClassification {
23
+ readonly fn: FunctionDef;
24
+ readonly nargout: number;
25
+ readonly argTypes: readonly JitType[];
26
+ readonly cacheKey: string;
27
+ /**
28
+ * Effective JS parameter names for this specialization. Mirrors
29
+ * `fn.params` for non-varargin functions. For varargin functions,
30
+ * the trailing `varargin` is replaced with one synthetic name per
31
+ * variadic arg (`$va_0`, `$va_1`, …) so each variadic arg becomes
32
+ * a regular JS function parameter. argTypes is one-to-one with
33
+ * effectiveParams.
34
+ */
35
+ readonly effectiveParams: readonly string[];
36
+ /** Number of variadic args (0 when fn has no varargin). */
37
+ readonly nVarargin: number;
38
+ }
39
+ /** Lowered IR plus the classification it came from. */
40
+ export interface CallLowered {
41
+ readonly classification: CallClassification;
42
+ readonly result: LoweringResult;
43
+ }
44
+ /** A compiled JS artifact ready to run. */
45
+ export interface CallCompiled {
46
+ readonly fn: (...args: unknown[]) => unknown;
47
+ readonly source: string;
48
+ }
49
+ export declare function classifyCall(fn: FunctionDef, args: unknown[], nargout: number, prevArgTypes: readonly JitType[] | undefined): CallClassification | null;
50
+ export declare function lowerCall(interp: Interpreter, classification: CallClassification): CallLowered | null;
51
+ export declare function generateCallJS(interp: Interpreter, lowered: CallLowered): CallCompiled | null;
52
+ export type CallRunOutcome = {
53
+ ok: true;
54
+ result: unknown;
55
+ } | {
56
+ ok: false;
57
+ transient: boolean;
58
+ };
59
+ export declare function runCallCompiled(interp: Interpreter, fn: FunctionDef, compiled: CallCompiled, args: unknown[]): CallRunOutcome;
@@ -0,0 +1,53 @@
1
+ /**
2
+ * For/while loop body — phased pipeline.
3
+ *
4
+ * 1. `classifyLoop` — analyze loop, gather typed env inputs,
5
+ * live-out filter on outputs (using post-siblings), type
6
+ * widening, cacheKey synthesis.
7
+ *
8
+ * 2. `lowerLoop` — wrap as a synthetic `FunctionDef` and
9
+ * lower via `lowerSyntheticFn`.
10
+ *
11
+ * 3. `generateLoopJS` — `generateSyntheticFnJS` + diagnostics.
12
+ *
13
+ * 4. `runLoopCompiled` — `runSyntheticFnAgainstEnv`.
14
+ *
15
+ * Shared with top-level via `shared.ts`. What's loop-specific is
16
+ * the analysis (For/While discrimination), the live-out filter
17
+ * against post-siblings, the cacheKey location (per loop AST
18
+ * span), and the `$loop_for` / `$loop_while` synthetic name.
19
+ */
20
+ import type { Interpreter } from "../../interpreter/interpreter.js";
21
+ import type { Stmt } from "../../parser/types.js";
22
+ import type { JitType } from "../../jitTypes.js";
23
+ import type { LoweringResult } from "./lower/jitLower.js";
24
+ import { type SyntheticFnRunResult } from "./shared.js";
25
+ export type LoopKind = "for" | "while";
26
+ export interface LoopClassification {
27
+ readonly stmt: Stmt & {
28
+ type: "For" | "While";
29
+ };
30
+ readonly kind: LoopKind;
31
+ readonly inputs: readonly string[];
32
+ readonly inputTypes: readonly JitType[];
33
+ readonly outputs: readonly string[];
34
+ readonly hasReturn: boolean;
35
+ readonly cacheKey: string;
36
+ }
37
+ export interface LoopLowered {
38
+ readonly classification: LoopClassification;
39
+ readonly result: LoweringResult;
40
+ }
41
+ export interface LoopCompiled {
42
+ readonly fn: (...args: unknown[]) => unknown;
43
+ readonly source: string;
44
+ }
45
+ export type LoopRunResult = SyntheticFnRunResult;
46
+ /** `postSiblings` / `postIdx` describe the stmt list this loop sits
47
+ * in; used to compute the live-out filter on outputs. */
48
+ export declare function classifyLoop(interp: Interpreter, stmt: Stmt & {
49
+ type: "For" | "While";
50
+ }, postSiblings: readonly Stmt[], postIdx: number, prevInputTypes: readonly JitType[] | undefined): LoopClassification | null;
51
+ export declare function lowerLoop(interp: Interpreter, classification: LoopClassification): LoopLowered | null;
52
+ export declare function generateLoopJS(interp: Interpreter, lowered: LoopLowered): LoopCompiled | null;
53
+ export declare function runLoopCompiled(interp: Interpreter, compiled: LoopCompiled, classification: LoopClassification): LoopRunResult;
@@ -0,0 +1,44 @@
1
+ /**
2
+ * Top-level script body — phased pipeline.
3
+ *
4
+ * 1. `classifyTopLevel` — analyze script body, gather typed env
5
+ * inputs, type widening, cacheKey synthesis.
6
+ *
7
+ * 2. `lowerTopLevel` — wrap as a synthetic `FunctionDef` and
8
+ * lower via `lowerSyntheticFn`.
9
+ *
10
+ * 3. `generateTopLevelJS` — `generateSyntheticFnJS` + diagnostics.
11
+ *
12
+ * 4. `runTopLevelCompiled` — `runSyntheticFnAgainstEnv`.
13
+ *
14
+ * The shared mechanics live in `shared.ts`; this module spells out
15
+ * what's actually top-level-specific (whole-script body, no
16
+ * post-loop liveness filter, `$top` synthetic name).
17
+ */
18
+ import type { Interpreter } from "../../interpreter/interpreter.js";
19
+ import type { Stmt } from "../../parser/types.js";
20
+ import type { JitType } from "../../jitTypes.js";
21
+ import type { LoweringResult } from "./lower/jitLower.js";
22
+ import { type SyntheticFnRunResult } from "./shared.js";
23
+ export interface TopLevelClassification {
24
+ readonly stmts: readonly Stmt[];
25
+ readonly inputs: readonly string[];
26
+ readonly inputTypes: readonly JitType[];
27
+ readonly outputs: readonly string[];
28
+ readonly currentFile: string;
29
+ readonly hasReturn: boolean;
30
+ readonly cacheKey: string;
31
+ }
32
+ export interface TopLevelLowered {
33
+ readonly classification: TopLevelClassification;
34
+ readonly result: LoweringResult;
35
+ }
36
+ export interface TopLevelCompiled {
37
+ readonly fn: (...args: unknown[]) => unknown;
38
+ readonly source: string;
39
+ }
40
+ export type TopLevelRunResult = SyntheticFnRunResult;
41
+ export declare function classifyTopLevel(interp: Interpreter, stmts: readonly Stmt[], prevInputTypes: readonly JitType[] | undefined): TopLevelClassification | null;
42
+ export declare function lowerTopLevel(interp: Interpreter, classification: TopLevelClassification): TopLevelLowered | null;
43
+ export declare function generateTopLevelJS(interp: Interpreter, lowered: TopLevelLowered): TopLevelCompiled | null;
44
+ export declare function runTopLevelCompiled(interp: Interpreter, compiled: TopLevelCompiled, classification: TopLevelClassification): TopLevelRunResult;
@@ -0,0 +1,15 @@
1
+ /**
2
+ * js-jit-loop — JS codegen executor for the loop shape.
3
+ *
4
+ * - `propose()` filters on `lowered.kind === "loop"`, applies
5
+ * JIT-feasibility checks (hasReturn, IO+bail-risk).
6
+ *
7
+ * - `compile()` calls `generateLoopJS`. Cached by the registry
8
+ * under the classification's cacheKey.
9
+ *
10
+ * - `run()` calls `runLoopCompiled`. JitBailToInterpreter →
11
+ * transient bail; JitFuncHandleBailError → permanent bail.
12
+ */
13
+ import type { Executor } from "../types.js";
14
+ import { type LoopLowered, type LoopCompiled } from "./jitLoop.js";
15
+ export declare const jsJitLoopExecutor: Executor<LoopLowered, LoopCompiled | null>;
@@ -6,8 +6,8 @@
6
6
  * - referenced: variables read inside the loop
7
7
  * - hasReturn: whether the loop body contains a return statement
8
8
  */
9
- import type { Stmt } from "../parser/types.js";
10
- export interface LoopVarInfo {
9
+ import type { Stmt } from "../../../parser/types.js";
10
+ export interface BlockVarInfo {
11
11
  /** Variables referenced in the loop that must come from enclosing scope */
12
12
  inputs: string[];
13
13
  /** Variables assigned in the loop body (written back after JIT execution) */
@@ -18,7 +18,7 @@ export interface LoopVarInfo {
18
18
  /** Analyze a for loop statement for JIT compilation. */
19
19
  export declare function analyzeForLoop(stmt: Stmt & {
20
20
  type: "For";
21
- }): LoopVarInfo;
21
+ }): BlockVarInfo;
22
22
  /**
23
23
  * Collect the names of all variables read in a sibling-tail starting at
24
24
  * `startIdx` of the given stmt list. Used by the loop JIT to filter the
@@ -30,8 +30,8 @@ export declare function collectReadsFromSiblings(stmts: Stmt[], startIdx: number
30
30
  * Analyze a top-level script body (list of statements) for JIT compilation.
31
31
  * Used by `tryJitTopLevel` to wrap the whole main script as a synthetic fn.
32
32
  */
33
- export declare function analyzeTopLevel(stmts: Stmt[]): LoopVarInfo;
33
+ export declare function analyzeTopLevel(stmts: Stmt[]): BlockVarInfo;
34
34
  /** Analyze a while loop statement for JIT compilation. */
35
35
  export declare function analyzeWhileLoop(stmt: Stmt & {
36
36
  type: "While";
37
- }): LoopVarInfo;
37
+ }): BlockVarInfo;
@@ -15,7 +15,7 @@
15
15
  * of `UserCall`-ed functions (via `generatedIRBodies`), since a bail
16
16
  * inside a callee still forces the caller to re-run.
17
17
  */
18
- import type { JitStmt } from "./jitTypes.js";
18
+ import type { JitStmt } from "../../../jitTypes.js";
19
19
  import type { GeneratedFn } from "./jitLower.js";
20
20
  /**
21
21
  * Names of I/O-emitting builtins that the JIT is willing to lower as
@@ -9,9 +9,9 @@
9
9
  * Returns null if any unsupported construct is encountered, causing the
10
10
  * entire function to fall back to interpretation.
11
11
  */
12
- import type { FunctionDef } from "../interpreter/types.js";
13
- import type { Interpreter } from "../interpreter/interpreter.js";
14
- import type { JitType, JitExpr, JitStmt } from "./jitTypes.js";
12
+ import type { FunctionDef } from "../../../interpreter/types.js";
13
+ import type { Interpreter } from "../../../interpreter/interpreter.js";
14
+ import type { JitType, JitExpr, JitStmt } from "../../../jitTypes.js";
15
15
  import { type TypeEnv } from "./jitLowerTypes.js";
16
16
  /**
17
17
  * Lowered IR for a user function reached during a top-level lowering.
@@ -111,6 +111,17 @@ export interface LowerCtx {
111
111
  * the body reads the `nargout` identifier, since the JIT specializes per
112
112
  * nargout already. */
113
113
  nargout?: number;
114
+ /** The nargin this specialization was requested with. Set whenever the
115
+ * function is varargin (the call shape pins down nargin per spec).
116
+ * Reads of bare `nargin` get inlined to this constant; `nargin > K`
117
+ * guards then constant-fold so dead branches don't have to lower. */
118
+ nargin?: number;
119
+ /** Number of variadic args bound for this specialization (0 for
120
+ * non-varargin callees). Used by `varargin{i}` lowering to redirect to
121
+ * the synthetic `$va_*` param holding the i-th variadic arg. The
122
+ * varargin lowering only needs the count — the synthetic name is
123
+ * derived from the literal index alone. */
124
+ nVarargin?: number;
114
125
  generatedFns: Map<string, string>;
115
126
  generatedIRBodies: Map<string, GeneratedFn>;
116
127
  loweringInProgress: Set<string>;
@@ -119,4 +130,7 @@ export interface LowerCtx {
119
130
  }
120
131
  /** Idempotent bail-reason setter. Only the first reason per function sticks. */
121
132
  export declare function setBailReason(ctx: LowerCtx, reason: string, line?: number): void;
122
- export declare function lowerFunction(fn: FunctionDef, argTypes: JitType[], nargout: number, interp?: Interpreter, generatedFns?: Map<string, string>, loweringInProgress?: Set<string>, generatedIRBodies?: Map<string, GeneratedFn>): LoweringResult | null;
133
+ export declare function lowerFunction(fn: FunctionDef, argTypes: JitType[], nargout: number, interp?: Interpreter, generatedFns?: Map<string, string>, loweringInProgress?: Set<string>, generatedIRBodies?: Map<string, GeneratedFn>, varargs?: {
134
+ effectiveParams: string[];
135
+ nVarargin: number;
136
+ }): LoweringResult | null;
@@ -4,8 +4,9 @@
4
4
  * exprs may trigger nested lowering of a callee's body). jitLower.ts is
5
5
  * the orchestrator that ties them together via LowerCtx + lowerFunction.
6
6
  */
7
- import type { Expr } from "../parser/types.js";
8
- import { type JitExpr } from "./jitTypes.js";
7
+ import type { Expr } from "../../../parser/types.js";
8
+ import type { Interpreter } from "../../../interpreter/interpreter.js";
9
+ import { type JitType, type JitExpr } from "../../../jitTypes.js";
9
10
  import type { LowerCtx } from "./jitLower.js";
10
11
  export declare function lowerExpr(ctx: LowerCtx, expr: Expr): JitExpr | null;
11
12
  /**
@@ -25,3 +26,11 @@ export declare function tryLowerRangeSliceRead(ctx: LowerCtx, baseName: string,
25
26
  export declare function lowerIBuiltinCall(ctx: LowerCtx, expr: Expr & {
26
27
  type: "FuncCall";
27
28
  }): JitExpr | null;
29
+ /**
30
+ * Multi-output variant of probeFuncHandleReturnType. Calls the handle
31
+ * with the requested nargout and returns one JitType per output. Bails
32
+ * (returns null) on the same conditions as the single-output probe
33
+ * (handle without jsFn, args we can't synthesize, output containing
34
+ * unknown), or when the result isn't an array of the expected length.
35
+ */
36
+ export declare function probeFuncHandleMultiReturnTypes(interp: Interpreter, fnName: string, loweredArgs: JitExpr[], nargout: number): JitType[] | null;
@@ -3,7 +3,7 @@
3
3
  * sides are mutually recursive. jitLower.ts is the orchestrator that ties
4
4
  * them together via LowerCtx + lowerFunction.
5
5
  */
6
- import type { Stmt } from "../parser/types.js";
7
- import { type JitStmt } from "./jitTypes.js";
6
+ import type { Stmt } from "../../../parser/types.js";
7
+ import { type JitStmt } from "../../../jitTypes.js";
8
8
  import type { LowerCtx } from "./jitLower.js";
9
9
  export declare function lowerStmts(ctx: LowerCtx, stmts: Stmt[]): JitStmt[] | null;
@@ -9,8 +9,8 @@
9
9
  *
10
10
  * These have no dependency on LowerCtx or the lowering state.
11
11
  */
12
- import { BinaryOperation, UnaryOperation } from "../parser/types.js";
13
- import { type JitExpr, type JitType, type SignCategory } from "./jitTypes.js";
12
+ import { BinaryOperation, UnaryOperation } from "../../../parser/types.js";
13
+ import { type JitExpr, type JitType, type SignCategory } from "../../../jitTypes.js";
14
14
  export declare const KNOWN_CONSTANTS: Record<string, number>;
15
15
  export type TypeEnv = Map<string, JitType>;
16
16
  export declare function cloneEnv(env: TypeEnv): TypeEnv;
@@ -11,8 +11,8 @@
11
11
  * Complex-scalar and tensor ops are *not* covered here — those remain
12
12
  * backend-specific (JS uses `$h.cAdd` etc.; C-JIT bails on complex).
13
13
  */
14
- import { BinaryOperation, UnaryOperation } from "../parser/types.js";
15
- import type { JitExpr } from "./jitTypes.js";
14
+ import { BinaryOperation, UnaryOperation } from "../../../parser/types.js";
15
+ import type { JitExpr } from "../../../jitTypes.js";
16
16
  export interface ScalarOpTarget {
17
17
  binAdd(l: string, r: string): string;
18
18
  binSub(l: string, r: string): string;