numbl 0.1.6 → 0.2.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 (105) hide show
  1. package/binding.gyp +53 -2
  2. package/dist-cli/cli.js +38743 -24679
  3. package/dist-lib/lib.js +43424 -30466
  4. package/dist-lib/numbl-core/executeCode.d.ts +22 -0
  5. package/dist-lib/numbl-core/helpers/bessel.d.ts +9 -0
  6. package/dist-lib/numbl-core/helpers/reduction-helpers.d.ts +7 -2
  7. package/dist-lib/numbl-core/interpreter/builtins/datetime.d.ts +39 -0
  8. package/dist-lib/numbl-core/interpreter/builtins/index.d.ts +1 -0
  9. package/dist-lib/numbl-core/interpreter/builtins/time-system.d.ts +1 -0
  10. package/dist-lib/numbl-core/interpreter/builtins/types.d.ts +100 -5
  11. package/dist-lib/numbl-core/interpreter/interpreter.d.ts +41 -3
  12. package/dist-lib/numbl-core/interpreter/interpreterSpecialBuiltins.d.ts +2 -0
  13. package/dist-lib/numbl-core/interpreter/types.d.ts +16 -7
  14. package/dist-lib/numbl-core/jit/c/abi.d.ts +90 -0
  15. package/dist-lib/numbl-core/jit/c/assemble.d.ts +56 -0
  16. package/dist-lib/numbl-core/jit/c/classify.d.ts +70 -0
  17. package/dist-lib/numbl-core/jit/c/compile.d.ts +37 -0
  18. package/dist-lib/numbl-core/jit/c/context.d.ts +152 -0
  19. package/dist-lib/numbl-core/jit/c/emit/assign.d.ts +20 -0
  20. package/dist-lib/numbl-core/jit/c/emit/complexScalar.d.ts +18 -0
  21. package/dist-lib/numbl-core/jit/c/emit/fused.d.ts +42 -0
  22. package/dist-lib/numbl-core/jit/c/emit/helpers.d.ts +40 -0
  23. package/dist-lib/numbl-core/jit/c/emit/index.d.ts +14 -0
  24. package/dist-lib/numbl-core/jit/c/emit/scalar.d.ts +23 -0
  25. package/dist-lib/numbl-core/jit/c/emit/stmt.d.ts +25 -0
  26. package/dist-lib/numbl-core/jit/c/emit/tensor.d.ts +127 -0
  27. package/dist-lib/numbl-core/jit/c/emit/userCall.d.ts +58 -0
  28. package/dist-lib/numbl-core/jit/c/epilogue.d.ts +26 -0
  29. package/dist-lib/numbl-core/jit/c/feasibility.d.ts +44 -0
  30. package/dist-lib/numbl-core/jit/c/hybrid.d.ts +42 -0
  31. package/dist-lib/numbl-core/jit/c/install.d.ts +15 -0
  32. package/dist-lib/numbl-core/jit/c/parityError.d.ts +26 -0
  33. package/dist-lib/numbl-core/jit/c/prelude.d.ts +37 -0
  34. package/dist-lib/numbl-core/jit/c/registry.d.ts +51 -0
  35. package/dist-lib/numbl-core/jit/c/visit.d.ts +63 -0
  36. package/dist-lib/numbl-core/jit/e1/install.d.ts +13 -0
  37. package/dist-lib/numbl-core/jit/e1/kernelEmit.d.ts +54 -0
  38. package/dist-lib/numbl-core/jit/e1/openmpFlag.d.ts +13 -0
  39. package/dist-lib/numbl-core/jit/e1/scalarFnKernel.d.ts +44 -0
  40. package/dist-lib/numbl-core/jit/fusedChainHelpers.d.ts +65 -0
  41. package/dist-lib/numbl-core/jit/fusedScalarEmit.d.ts +61 -0
  42. package/dist-lib/numbl-core/jit/fusion.d.ts +71 -0
  43. package/dist-lib/numbl-core/jit/fusionOps.d.ts +25 -0
  44. package/dist-lib/numbl-core/{interpreter/jit → jit}/index.d.ts +2 -2
  45. package/dist-lib/numbl-core/jit/jitBailSafety.d.ts +41 -0
  46. package/dist-lib/numbl-core/{interpreter/jit → jit}/jitLoop.d.ts +2 -2
  47. package/dist-lib/numbl-core/{interpreter/jit → jit}/jitLoopAnalysis.d.ts +13 -1
  48. package/dist-lib/numbl-core/jit/jitLower.d.ts +122 -0
  49. package/dist-lib/numbl-core/jit/jitLowerExpr.d.ts +27 -0
  50. package/dist-lib/numbl-core/jit/jitLowerStmt.d.ts +9 -0
  51. package/dist-lib/numbl-core/jit/jitLowerTypes.d.ts +29 -0
  52. package/dist-lib/numbl-core/jit/jitTopLevel.d.ts +22 -0
  53. package/dist-lib/numbl-core/jit/jitTypes.d.ts +394 -0
  54. package/dist-lib/numbl-core/jit/js/jitCodegen.d.ts +7 -0
  55. package/dist-lib/numbl-core/jit/js/jitCodegenHoist.d.ts +70 -0
  56. package/dist-lib/numbl-core/jit/js/jitHelpers.d.ts +34 -0
  57. package/dist-lib/numbl-core/jit/js/jitHelpersComplex.d.ts +21 -0
  58. package/dist-lib/numbl-core/jit/js/jitHelpersIndex.d.ts +33 -0
  59. package/dist-lib/numbl-core/jit/js/jitHelpersTensor.d.ts +34 -0
  60. package/dist-lib/numbl-core/jit/js/jsFusedCodegen.d.ts +17 -0
  61. package/dist-lib/numbl-core/jit/scalarEmit.d.ts +58 -0
  62. package/dist-lib/numbl-core/lexer/types.d.ts +2 -1
  63. package/dist-lib/numbl-core/native/lapack-bridge.d.ts +46 -1
  64. package/dist-lib/numbl-core/ops/bessel.d.ts +18 -0
  65. package/dist-lib/numbl-core/ops/comparison.d.ts +11 -0
  66. package/dist-lib/numbl-core/ops/complexBinaryElemwise.d.ts +10 -0
  67. package/dist-lib/numbl-core/ops/complexUnaryElemwise.d.ts +8 -0
  68. package/dist-lib/numbl-core/ops/dispatch.d.ts +26 -0
  69. package/dist-lib/numbl-core/ops/index.d.ts +8 -0
  70. package/dist-lib/numbl-core/ops/opCodes.d.ts +70 -0
  71. package/dist-lib/numbl-core/ops/realBinaryElemwise.d.ts +8 -0
  72. package/dist-lib/numbl-core/ops/realUnaryElemwise.d.ts +5 -0
  73. package/dist-lib/numbl-core/ops/reduce.d.ts +6 -0
  74. package/dist-lib/numbl-core/parser/types.d.ts +6 -0
  75. package/dist-lib/numbl-core/runtime/alloc.d.ts +23 -0
  76. package/dist-lib/numbl-core/runtime/constructors.d.ts +2 -1
  77. package/dist-lib/numbl-core/runtime/error.d.ts +3 -0
  78. package/dist-lib/numbl-core/runtime/index.d.ts +1 -1
  79. package/dist-lib/numbl-core/runtime/runtime.d.ts +15 -2
  80. package/dist-lib/numbl-core/runtime/runtimePlot.d.ts +11 -0
  81. package/dist-lib/numbl-core/runtime/types.d.ts +16 -1
  82. package/dist-lib/numbl-core/runtime/utils.d.ts +3 -1
  83. package/dist-lib/numbl-core/version.d.ts +1 -1
  84. package/dist-plot-viewer/assets/{index-vtrJ8bml.js → index-GiUNnMQg.js} +1 -1
  85. package/dist-plot-viewer/index.html +1 -1
  86. package/native/elemwise.cpp +134 -0
  87. package/native/jit_runtime/jit_runtime.c +261 -0
  88. package/native/jit_runtime/jit_runtime.h +204 -0
  89. package/native/numbl_addon.cpp +55 -1
  90. package/native/numbl_addon_common.h +1 -0
  91. package/native/ops/bessel.c +572 -0
  92. package/native/ops/comparison.c +150 -0
  93. package/native/ops/complex_binary_elemwise.c +192 -0
  94. package/native/ops/complex_unary_elemwise.c +152 -0
  95. package/native/ops/numbl_ops.c +66 -0
  96. package/native/ops/numbl_ops.h +262 -0
  97. package/native/ops/real_binary_elemwise.c +85 -0
  98. package/native/ops/real_unary_elemwise.c +104 -0
  99. package/native/ops/reduce.c +162 -0
  100. package/native/ops_napi.cpp +320 -0
  101. package/package.json +11 -10
  102. package/dist-lib/numbl-core/interpreter/jit/jitCodegen.d.ts +0 -5
  103. package/dist-lib/numbl-core/interpreter/jit/jitHelpers.d.ts +0 -14
  104. package/dist-lib/numbl-core/interpreter/jit/jitLower.d.ts +0 -20
  105. package/dist-lib/numbl-core/interpreter/jit/jitTypes.d.ts +0 -168
@@ -0,0 +1,122 @@
1
+ /**
2
+ * AST -> JIT IR lowering with type propagation. Orchestrator that owns
3
+ * the shared context types (LowerCtx, SliceAlias), the public result
4
+ * types (GeneratedFn, LoweringResult), and the `lowerFunction` entry
5
+ * point. Actual statement and expression lowering lives in
6
+ * jitLowerStmt.ts and jitLowerExpr.ts; type-level helpers are in
7
+ * jitLowerTypes.ts.
8
+ *
9
+ * Returns null if any unsupported construct is encountered, causing the
10
+ * entire function to fall back to interpretation.
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";
15
+ import { type TypeEnv } from "./jitLowerTypes.js";
16
+ /**
17
+ * Lowered IR for a user function reached during a top-level lowering.
18
+ *
19
+ * JS-JIT caches the callee as generated JS source in `generatedFns` and
20
+ * is done. C-JIT needs to re-analyze the callee's IR (for recursive
21
+ * feasibility + emitting a static C function per callee), so we also
22
+ * cache the raw IR here. One entry per unique `jitName`; matches the
23
+ * JS-JIT specialization key.
24
+ */
25
+ export interface GeneratedFn {
26
+ fn: FunctionDef;
27
+ argTypes: JitType[];
28
+ outputNames: string[];
29
+ outputTypes: JitType[];
30
+ body: JitStmt[];
31
+ localVars: Set<string>;
32
+ nargout: number;
33
+ }
34
+ export interface LoweringResult {
35
+ body: JitStmt[];
36
+ outputNames: string[];
37
+ localVars: Set<string>;
38
+ hasTensorOps: boolean;
39
+ /** Generated JS code for called user functions: jitName → code */
40
+ generatedFns: Map<string, string>;
41
+ /** Lowered IR for called user functions: jitName → IR + metadata.
42
+ * Populated alongside `generatedFns` by `lowerUserFuncCall`; the
43
+ * C-JIT uses it for recursive feasibility and per-callee C emission. */
44
+ generatedIRBodies: Map<string, GeneratedFn>;
45
+ /** Type of the first output variable after lowering */
46
+ outputType: JitType | null;
47
+ /** Types of all output variables in outputNames order. Mirrors the
48
+ * shape of JS-JIT's `return [out0, out1, ...]`. */
49
+ outputTypes: JitType[];
50
+ /** Final typed environment after lowering the body. The hybrid
51
+ * loop-extraction pass uses this to look up live-in/live-out var
52
+ * types without re-running type inference. */
53
+ endEnv: TypeEnv;
54
+ }
55
+ /**
56
+ * A "slice alias" records that a MATLAB local was bound to a colon-slice of
57
+ * a real tensor (e.g. `pt = pts(:, i)`). Rather than materializing the slice
58
+ * as a RuntimeTensor per iteration, we remember the base tensor and a
59
+ * per-dim "template" of indices — scalar expressions captured at bind time
60
+ * for the non-colon dims, and `"colon"` placeholders for the colon dims.
61
+ * Subsequent reads like `pt(k)` substitute `k` into the colon positions and
62
+ * emit a direct scalar read on the base tensor, which compiles cleanly
63
+ * through the existing hoisted `idx{1,2,3}r_h` fast path.
64
+ */
65
+ export interface SliceAlias {
66
+ baseName: string;
67
+ baseType: JitType;
68
+ /**
69
+ * One entry per index of the original bind expression, in source order.
70
+ * A `"colon"` slot expects to be filled by the read-site's colon indices.
71
+ * An `"expr"` slot carries a JitExpr that will be substituted as-is.
72
+ */
73
+ template: ({
74
+ kind: "colon";
75
+ } | {
76
+ kind: "expr";
77
+ expr: JitExpr;
78
+ })[];
79
+ /** Sizes of the slice's colon dimensions, in source order. */
80
+ sliceShape: number[];
81
+ /** Indices into `template` where colon slots live, in source order. */
82
+ colonPositions: number[];
83
+ }
84
+ export interface LowerCtx {
85
+ env: TypeEnv;
86
+ localVars: Set<string>;
87
+ params: Set<string>;
88
+ /** First bail reason encountered during lowering; lets callers surface
89
+ * a hint for why JS-JIT declined a function (purely diagnostic, only
90
+ * consulted by the `NUMBL_LOG_CJIT_MISSES` tally). Writes should use
91
+ * `setBailReason()` which is idempotent — only the first reason sticks. */
92
+ bailReason?: string;
93
+ bailLine?: number;
94
+ /** Type of the deepest expr whose lowering was attempted — used as a
95
+ * last-resort bail reason hint when no more specific reason was set. */
96
+ lastExprType?: string;
97
+ lastExprLine?: number;
98
+ /** Variables that are actually assigned in the function body. */
99
+ assignedVars: Set<string>;
100
+ /**
101
+ * Map from a MATLAB local name to its slice alias, if any. A name is
102
+ * present here iff the most recent assignment to it was a whole-tensor
103
+ * colon slice. Reads of the name as a plain Ident bail; reads of
104
+ * `name(...)` substitute through the template and emit a direct scalar
105
+ * read of the base tensor. See `tryLowerAsSliceBind`.
106
+ */
107
+ sliceAliases: Map<string, SliceAlias>;
108
+ _hasTensorOps?: boolean;
109
+ interp?: Interpreter;
110
+ /** The nargout this specialization was requested with — inlined wherever
111
+ * the body reads the `nargout` identifier, since the JIT specializes per
112
+ * nargout already. */
113
+ nargout?: number;
114
+ generatedFns: Map<string, string>;
115
+ generatedIRBodies: Map<string, GeneratedFn>;
116
+ loweringInProgress: Set<string>;
117
+ /** Pre-built line break table for offset→line lookup. */
118
+ lineTable?: number[];
119
+ }
120
+ /** Idempotent bail-reason setter. Only the first reason per function sticks. */
121
+ 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;
@@ -0,0 +1,27 @@
1
+ /**
2
+ * Expression lowering for the JIT. Paired with jitLowerStmt.ts; the two
3
+ * sides are mutually recursive (stmt bodies contain exprs; function-call
4
+ * exprs may trigger nested lowering of a callee's body). jitLower.ts is
5
+ * the orchestrator that ties them together via LowerCtx + lowerFunction.
6
+ */
7
+ import type { Expr } from "../parser/types.js";
8
+ import { type JitExpr } from "./jitTypes.js";
9
+ import type { LowerCtx } from "./jitLower.js";
10
+ export declare function lowerExpr(ctx: LowerCtx, expr: Expr): JitExpr | null;
11
+ /**
12
+ * Stage 21: lower `src(a:b)` on a real-tensor base into a
13
+ * `RangeSliceRead` IR node producing a fresh column-vector tensor.
14
+ *
15
+ * Accepts `Range` with no step (default step 1). `start` and `end`
16
+ * must lower to numeric/boolean scalar exprs. The result is a real
17
+ * tensor with shape `[?, 1]` — the exact length is runtime-dependent.
18
+ *
19
+ * Caller responsibility: match the parent expression shape
20
+ * `Index(Ident(src), [Range])` or `FuncCall(src, [Range])` before
21
+ * calling. Returns null if the source isn't a real tensor or the
22
+ * range isn't the expected shape.
23
+ */
24
+ export declare function tryLowerRangeSliceRead(ctx: LowerCtx, baseName: string, rangeExpr: Expr): JitExpr | null;
25
+ export declare function lowerIBuiltinCall(ctx: LowerCtx, expr: Expr & {
26
+ type: "FuncCall";
27
+ }): JitExpr | null;
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Statement lowering for the JIT. Paired with jitLowerExpr.ts; the two
3
+ * sides are mutually recursive. jitLower.ts is the orchestrator that ties
4
+ * them together via LowerCtx + lowerFunction.
5
+ */
6
+ import type { Stmt } from "../parser/types.js";
7
+ import { type JitStmt } from "./jitTypes.js";
8
+ import type { LowerCtx } from "./jitLower.js";
9
+ export declare function lowerStmts(ctx: LowerCtx, stmts: Stmt[]): JitStmt[] | null;
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Type-level helpers for the JIT lowering pass.
3
+ *
4
+ * Pure functions for:
5
+ * - Sign algebra (how signs combine through arithmetic)
6
+ * - Binary/unary operation result type inference
7
+ * - Type environment management (clone, merge, equality)
8
+ * - Known MATLAB constants
9
+ *
10
+ * These have no dependency on LowerCtx or the lowering state.
11
+ */
12
+ import { BinaryOperation, UnaryOperation } from "../parser/types.js";
13
+ import { type JitExpr, type JitType, type SignCategory } from "./jitTypes.js";
14
+ export declare const KNOWN_CONSTANTS: Record<string, number>;
15
+ export type TypeEnv = Map<string, JitType>;
16
+ export declare function cloneEnv(env: TypeEnv): TypeEnv;
17
+ /** Merge two type environments at a join point. Returns null if any type becomes unknown. */
18
+ export declare function mergeEnvs(a: TypeEnv, b: TypeEnv): TypeEnv | null;
19
+ /** Check if two type environments are identical (by JSON comparison). */
20
+ export declare function envsEqual(a: TypeEnv, b: TypeEnv): boolean;
21
+ export declare function addSigns(a: SignCategory, b: SignCategory): SignCategory | undefined;
22
+ export declare function mulSigns(a: SignCategory, b: SignCategory): SignCategory | undefined;
23
+ export declare function combineSigns(a: SignCategory | undefined, b: SignCategory | undefined, op: BinaryOperation): SignCategory | undefined;
24
+ export declare function binaryResultType(op: BinaryOperation, left: JitType, right: JitType,
25
+ /** Lowered operand exprs. Optional because tests / older callers pass
26
+ * only types. When provided, enables structural nonneg detection for
27
+ * patterns like `x .* x` that the type-only path can't see. */
28
+ leftExpr?: JitExpr, rightExpr?: JitExpr): JitType | null;
29
+ export declare function unaryResultType(op: UnaryOperation, operand: JitType): JitType | null;
@@ -0,0 +1,22 @@
1
+ /**
2
+ * JIT compilation for the top-level script body (the main workspace).
3
+ *
4
+ * Wraps the list of non-function, non-classdef statements of a script as
5
+ * a synthetic `FunctionDef` whose parameters are the live-in env vars
6
+ * and whose outputs are every variable assigned in the script. On
7
+ * success the compiled code runs once and all output values are written
8
+ * back to the interpreter's workspace env.
9
+ *
10
+ * Mirrors `tryJitLoop` in jitLoop.ts — same lowering, same JS/C backend
11
+ * pipeline, same progressive type widening. The differences:
12
+ * - the synthetic body is the list of stmts directly, not a single For/While
13
+ * - every assigned variable is live-out (the whole workspace is live)
14
+ * - cache key is per-Interpreter (a single script AST per interp run)
15
+ */
16
+ import type { Interpreter } from "../interpreter/interpreter.js";
17
+ import type { Stmt } from "../parser/types.js";
18
+ /**
19
+ * Attempt to JIT-compile and execute the top-level script body.
20
+ * Returns true if JIT succeeded, false to fall back to interpretation.
21
+ */
22
+ export declare function tryJitTopLevel(interp: Interpreter, stmts: Stmt[]): boolean;
@@ -0,0 +1,394 @@
1
+ /**
2
+ * JIT type system and IR node definitions.
3
+ */
4
+ import type { BinaryOperation, UnaryOperation } from "../parser/types.js";
5
+ export type SignCategory = "positive" | "nonneg" | "nonpositive" | "negative";
6
+ export type JitType = {
7
+ kind: "number";
8
+ exact?: number;
9
+ sign?: SignCategory;
10
+ isInteger?: boolean;
11
+ } | {
12
+ kind: "boolean";
13
+ value?: boolean;
14
+ } | {
15
+ kind: "complex_or_number";
16
+ pureImaginary?: boolean;
17
+ } | {
18
+ kind: "tensor";
19
+ isComplex: boolean;
20
+ shape?: number[];
21
+ ndim?: number;
22
+ isLogical?: boolean;
23
+ nonneg?: boolean;
24
+ } | {
25
+ kind: "string";
26
+ value?: string;
27
+ } | {
28
+ kind: "char";
29
+ value?: string;
30
+ } | {
31
+ kind: "struct";
32
+ fields?: Record<string, JitType>;
33
+ } | {
34
+ /**
35
+ * A 1-D homogeneous array of structs (matlab struct array form
36
+ * `T.nodes(i)`). `elemFields` records the per-element field types
37
+ * unified across all elements. `length` is optional and carries
38
+ * the statically-known element count when available.
39
+ */
40
+ kind: "struct_array";
41
+ elemFields?: Record<string, JitType>;
42
+ length?: number;
43
+ } | {
44
+ kind: "class_instance";
45
+ className: string;
46
+ isHandleClass?: boolean;
47
+ fields?: Record<string, JitType>;
48
+ } | {
49
+ kind: "sparse_matrix";
50
+ isComplex: boolean;
51
+ m?: number;
52
+ n?: number;
53
+ } | {
54
+ kind: "cell";
55
+ shape?: number[];
56
+ } | {
57
+ kind: "dictionary";
58
+ } | {
59
+ kind: "function_handle";
60
+ } | {
61
+ kind: "unknown";
62
+ };
63
+ export declare function signFromNumber(v: number): SignCategory | undefined;
64
+ export declare function isNonneg(t: JitType): boolean;
65
+ export declare function flipSign(s?: SignCategory): SignCategory | undefined;
66
+ export declare function unifySign(a?: SignCategory, b?: SignCategory): SignCategory | undefined;
67
+ export declare function jitTypeKey(t: JitType): string;
68
+ export declare function computeJitCacheKey(nargout: number, argTypes: JitType[]): string;
69
+ /** Compute a unique JS function name for a JIT'd specialization. */
70
+ export declare function computeJitFnName(identity: string, funcName: string): string;
71
+ /** Widen/unify two types at control-flow join points. */
72
+ export declare function unifyJitTypes(a: JitType, b: JitType): JitType;
73
+ export declare function isScalarType(t: JitType): boolean;
74
+ export declare function isNumericScalarType(t: JitType): boolean;
75
+ export declare function isTensorType(t: JitType): boolean;
76
+ export declare function isComplexType(t: JitType): boolean;
77
+ /** True when the type guarantees an integer value at runtime (safe for |0). */
78
+ export declare function isKnownInteger(t: JitType): boolean;
79
+ /** Types that support arithmetic binary operations in the JIT. */
80
+ export declare function isArithmeticType(t: JitType): boolean;
81
+ export declare function shapeAfterReduction(shape: number[], dim?: number): {
82
+ scalar: true;
83
+ } | {
84
+ scalar: false;
85
+ shape: number[];
86
+ };
87
+ export type JitExpr = {
88
+ tag: "NumberLiteral";
89
+ value: number;
90
+ jitType: JitType;
91
+ } | {
92
+ tag: "ImagLiteral";
93
+ jitType: JitType;
94
+ } | {
95
+ tag: "Var";
96
+ name: string;
97
+ jitType: JitType;
98
+ } | {
99
+ tag: "Binary";
100
+ op: BinaryOperation;
101
+ left: JitExpr;
102
+ right: JitExpr;
103
+ jitType: JitType;
104
+ } | {
105
+ tag: "Unary";
106
+ op: UnaryOperation;
107
+ operand: JitExpr;
108
+ jitType: JitType;
109
+ } | {
110
+ tag: "StringLiteral";
111
+ value: string;
112
+ isChar: boolean;
113
+ jitType: JitType;
114
+ } | {
115
+ tag: "Call";
116
+ name: string;
117
+ args: JitExpr[];
118
+ jitType: JitType;
119
+ } | {
120
+ tag: "UserCall";
121
+ jitName: string;
122
+ name: string;
123
+ args: JitExpr[];
124
+ jitType: JitType;
125
+ } | {
126
+ tag: "Index";
127
+ base: JitExpr;
128
+ indices: JitExpr[];
129
+ jitType: JitType;
130
+ } | {
131
+ /**
132
+ * Range slice read on a real tensor: `src(a:b)` producing a fresh
133
+ * column-vector tensor of length `b - a + 1`. Stage 21 — unblocks
134
+ * chunkie's chunk_nearparam Newton loop which splits a Legendre
135
+ * expansion vector via `r0 = all0(1:dim)`.
136
+ *
137
+ * Codegen emits `$h.subarrayCopy1r(srcData, srcLen, start, end)`
138
+ * using the hoisted data/length aliases. Per-iter allocation is
139
+ * unavoidable without a range-alias extension to stage 5; small
140
+ * slices are cheap in V8 young-gen.
141
+ */
142
+ tag: "RangeSliceRead";
143
+ baseName: string;
144
+ start: JitExpr;
145
+ /** `null` when the range endpoint is the `end` keyword — codegen
146
+ * substitutes the hoisted `.data.length` alias. */
147
+ end: JitExpr | null;
148
+ /** True when the source is a statically-known row vector
149
+ * (shape `[1, n]`); the slice then preserves row orientation.
150
+ * Otherwise the slice is a column vector (MATLAB semantics). */
151
+ isRow: boolean;
152
+ jitType: JitType;
153
+ } | {
154
+ tag: "TensorLiteral";
155
+ rows: JitExpr[][];
156
+ nRows: number;
157
+ nCols: number;
158
+ jitType: JitType;
159
+ } | {
160
+ /**
161
+ * Vertical concat growth: `[base; value]` where `base` is a real
162
+ * column-vector (or empty) tensor and `value` is a numeric scalar.
163
+ * Lowered from the chunkie grow-a-list pattern `it = [it; i]`.
164
+ * Codegens to `$h.vconcatGrow1r(base, value)` which allocates a
165
+ * fresh `(k+1, 1)` tensor and copies.
166
+ */
167
+ tag: "VConcatGrow";
168
+ base: JitExpr;
169
+ value: JitExpr;
170
+ jitType: JitType;
171
+ } | {
172
+ /**
173
+ * Scalar read of a struct field: `s.f` where `s` has a struct type
174
+ * with a statically-known scalar field `f`. Lowered from `Member`
175
+ * exprs with an `Ident` base. The codegen hoists each unique
176
+ * `(baseName, fieldName)` pair to a local alias at function entry
177
+ * so per-iter reads are bare local loads (no `Map.get` per use).
178
+ * Mirrors the chunkie `opts.rho`, `chnkr.k`, `chnkr.nch` pattern.
179
+ */
180
+ tag: "MemberRead";
181
+ baseName: string;
182
+ fieldName: string;
183
+ jitType: JitType;
184
+ } | {
185
+ /**
186
+ * Chained struct array member read: `T.nodes(i).leaf` where
187
+ * `T.nodes` is a struct array whose elements all share a field
188
+ * `leaf`. Lowered from the parser shape
189
+ * `Member(MethodCall(Ident(T), "nodes", [i]), "leaf")`.
190
+ *
191
+ * Codegen hoists `var $T_nodes_elements = T.fields.get("nodes").elements`
192
+ * at function entry (one alias per unique (structVarName,
193
+ * structArrayFieldName) pair) and emits
194
+ * `$T_nodes_elements[Math.round(i) - 1].fields.get("leaf")` per use.
195
+ *
196
+ * `leafFieldJitType` records the result type: either a scalar
197
+ * numeric type (number/boolean/complex_or_number) or a real tensor.
198
+ * Tensor-valued leaves work because the existing per-Assign hoist
199
+ * refresh picks up the newly-read tensor when the result flows
200
+ * into `chld = T.nodes(i).chld` and the callers then use
201
+ * `chld(k)` through the hoisted alias path.
202
+ */
203
+ tag: "StructArrayMemberRead";
204
+ structVarName: string;
205
+ structArrayFieldName: string;
206
+ indexExpr: JitExpr;
207
+ leafFieldName: string;
208
+ jitType: JitType;
209
+ } | {
210
+ /**
211
+ * Indirect call through a function handle variable: `f(a, b)` where
212
+ * `f` has JitType `function_handle`. The codegen emits
213
+ * `$h.callFuncHandle($rt, f, arg1, arg2, ...)` which invokes the
214
+ * handle via its `jsFn` closure (or falls back to `rt.dispatch`).
215
+ * The `returnType` is determined at lowering time by probing the
216
+ * function handle's runtime value.
217
+ */
218
+ tag: "FuncHandleCall";
219
+ name: string;
220
+ args: JitExpr[];
221
+ jitType: JitType;
222
+ } | {
223
+ /**
224
+ * User function call that bailed recursive lowering (stage 24 —
225
+ * soft-bail). Instead of bailing the containing loop, emit a
226
+ * runtime dispatch through the interpreter. Return type is
227
+ * determined at JIT compile time by probing (actually calling
228
+ * the function once with representative args). The helper
229
+ * `callUserFunc` verifies the actual return type on every call
230
+ * and throws `JitFuncHandleBailError` on mismatch so the loop
231
+ * runner can invalidate the cache entry and fall back to
232
+ * interpretation.
233
+ *
234
+ * Use case: `oneintp(...)` / `lege.exev(...)` in chunkie's adap
235
+ * inner loop — the callee body has tensor arithmetic the JIT
236
+ * can't inline, but the outer loop (scalar stack + col-slice
237
+ * writes + scalar compares) is a perfect JIT fit.
238
+ */
239
+ tag: "UserDispatchCall";
240
+ name: string;
241
+ args: JitExpr[];
242
+ jitType: JitType;
243
+ };
244
+ export type JitStmt = {
245
+ tag: "Assign";
246
+ name: string;
247
+ expr: JitExpr;
248
+ } | {
249
+ tag: "AssignIndex";
250
+ /** Name of the tensor variable being written to (must be an Ident-based lvalue). */
251
+ baseName: string;
252
+ /** Scalar index expressions (1..3). */
253
+ indices: JitExpr[];
254
+ /** RHS scalar value to store. */
255
+ value: JitExpr;
256
+ /** Type of the base tensor (for codegen to choose the right helper). */
257
+ baseType: JitType;
258
+ } | {
259
+ /**
260
+ * Range-slice write of the form `dst(a:b) = src(c:d)` or
261
+ * `dst(a:b) = src` (whole-tensor source). Both `dst` and `src` must
262
+ * be real tensors, and the LHS must use exactly one (linear) range
263
+ * index. Used by the loop JIT for the chunkie grow-and-copy pattern.
264
+ *
265
+ * When `srcStart` and `srcEnd` are both `null`, the source is used
266
+ * in its entirety — codegen substitutes `1` and `srcLen` (the
267
+ * source's hoisted length alias). This is stage 9's whole-tensor
268
+ * RHS form: `isp(1:nn) = itemp` where `itemp` is a plain Var.
269
+ */
270
+ tag: "AssignIndexRange";
271
+ baseName: string;
272
+ baseType: JitType;
273
+ dstStart: JitExpr;
274
+ dstEnd: JitExpr;
275
+ srcBaseName: string;
276
+ srcType: JitType;
277
+ srcStart: JitExpr | null;
278
+ srcEnd: JitExpr | null;
279
+ } | {
280
+ /**
281
+ * Multi-dim column slice write `dst(:, j) = src` where both `dst`
282
+ * and `src` are real tensors. LHS must be `Index(Ident(dst),
283
+ * [Colon, scalar_j])` with `dst` having statically 2-D shape.
284
+ * RHS must be a plain Ident referencing a real-tensor var whose
285
+ * total element count matches `dst.shape[0]` at runtime.
286
+ *
287
+ * Used by the loop JIT for chunkie's adapgausskerneval column
288
+ * stack write: `vals(:, jj+1) = v2`. Also covers
289
+ * chunkerinterior's `rss(:, jj) = rval(:, k)` after a slice
290
+ * alias binds the RHS.
291
+ */
292
+ tag: "AssignIndexCol";
293
+ baseName: string;
294
+ baseType: JitType;
295
+ colIndex: JitExpr;
296
+ srcBaseName: string;
297
+ srcType: JitType;
298
+ } | {
299
+ /**
300
+ * Page-slice write `dst(:, :, k) = rhs` where `dst` is a 3-D tensor
301
+ * and `rhs` is a 2-D tensor. Real-base + complex-rhs upgrades the
302
+ * base to complex in place. Drives chunkie helm2d.green's
303
+ * `grad(:,:,k) = ...` / `hess(:,:,k) = ...` assignments.
304
+ */
305
+ tag: "AssignIndexPage3d";
306
+ baseName: string;
307
+ /** Base tensor type AFTER the write (may be complex even if the
308
+ * initializer was real — the lowerer promotes in ctx.env). */
309
+ baseType: JitType;
310
+ /** Page index (1-based). */
311
+ pageIndex: JitExpr;
312
+ /** 2-D tensor RHS. */
313
+ value: JitExpr;
314
+ } | {
315
+ /**
316
+ * Struct field assign lvalue `s.f = v`. When `needsPromote` is
317
+ * true, the base is (re)initialized as a fresh empty struct
318
+ * before the field is set — mirrors MATLAB's
319
+ * `s = []; s.f = v` idiom that promotes the empty matrix to a
320
+ * struct on first field write. `valueType` is the JIT type of
321
+ * `value` so stage 12 can hoist subsequent reads through a
322
+ * typed alias.
323
+ */
324
+ tag: "AssignMember";
325
+ baseName: string;
326
+ fieldName: string;
327
+ value: JitExpr;
328
+ needsPromote: boolean;
329
+ } | {
330
+ tag: "If";
331
+ cond: JitExpr;
332
+ thenBody: JitStmt[];
333
+ elseifBlocks: {
334
+ cond: JitExpr;
335
+ body: JitStmt[];
336
+ }[];
337
+ elseBody: JitStmt[] | null;
338
+ } | {
339
+ tag: "For";
340
+ varName: string;
341
+ start: JitExpr;
342
+ step: JitExpr | null;
343
+ end: JitExpr;
344
+ body: JitStmt[];
345
+ } | {
346
+ tag: "While";
347
+ cond: JitExpr;
348
+ body: JitStmt[];
349
+ } | {
350
+ tag: "Break";
351
+ } | {
352
+ tag: "Continue";
353
+ } | {
354
+ tag: "Return";
355
+ } | {
356
+ tag: "ExprStmt";
357
+ expr: JitExpr;
358
+ } | {
359
+ tag: "MultiAssign";
360
+ names: (string | null)[];
361
+ callName: string;
362
+ args: JitExpr[];
363
+ outputTypes: JitType[];
364
+ } | {
365
+ /**
366
+ * Call a synthetic specialization (installed as a forwarder on $h)
367
+ * and write back multiple outputs into outer-scope vars. Only
368
+ * produced by the hybrid-loop pass in hybrid.ts, which
369
+ * replaces a For/While JitStmt with this call form when the
370
+ * extracted loop specialization compiled to native C.
371
+ *
372
+ * Semantics (0 / 1 / N outputs):
373
+ * nargout==0 → emit `$h.callUser(...)` as a stmt (no writeback)
374
+ * nargout==1 → emit `out = $h.callUser(...)`
375
+ * nargout>=2 → emit `var $r = $h.callUser(...); out0=$r[0]; ...`
376
+ */
377
+ tag: "UserCallWriteback";
378
+ outputs: string[];
379
+ jitName: string;
380
+ name: string;
381
+ args: JitExpr[];
382
+ outputTypes: JitType[];
383
+ } | {
384
+ tag: "SetLoc";
385
+ line: number;
386
+ } | {
387
+ /** C-JIT assertion: if this JitStmt survives to JS codegen at --opt 2,
388
+ * emit a runtime throw. The C-JIT codegen elides it. */
389
+ tag: "AssertCJit";
390
+ };
391
+ export interface JitCacheEntry {
392
+ fn: (...args: unknown[]) => unknown;
393
+ source: string;
394
+ }
@@ -0,0 +1,7 @@
1
+ /**
2
+ * JIT IR -> JavaScript code generation.
3
+ *
4
+ * IR walkers for hoist-pass data collection are in jitCodegenHoist.ts.
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;
@@ -0,0 +1,70 @@
1
+ /**
2
+ * IR walkers for the JIT codegen hoist pass.
3
+ *
4
+ * These pure functions walk the JIT IR to collect information needed
5
+ * for hoisting tensor aliases, struct field reads, and struct array
6
+ * element reads to the top of the generated function.
7
+ */
8
+ import type { JitStmt } from "../jitTypes.js";
9
+ /**
10
+ * Hoisted aliases for a tensor variable read/written in the loop body.
11
+ * Maps the original variable name to local JS identifiers for its
12
+ * .data, .data.length, .shape[0], and .shape[1].
13
+ */
14
+ export interface HoistedAlias {
15
+ data: string;
16
+ len: string;
17
+ d0: string;
18
+ d1: string;
19
+ maxDim: number;
20
+ isWriteTarget: boolean;
21
+ isParam: boolean;
22
+ }
23
+ export declare function structFieldKey(baseName: string, fieldName: string): string;
24
+ export declare function structArrayElementsKey(structVarName: string, structArrayFieldName: string): string;
25
+ export interface TensorUsage {
26
+ maxReadDim: number;
27
+ maxWriteDim: number;
28
+ isReal: boolean;
29
+ }
30
+ /**
31
+ * Walk the JIT IR collecting, for every variable that appears as the base
32
+ * of an Index read, AssignIndex write, or AssignIndexRange write, the
33
+ * maximum indexing arity and whether all uses are on a real tensor.
34
+ */
35
+ export declare function collectTensorUsage(body: JitStmt[]): Map<string, TensorUsage>;
36
+ /**
37
+ * Walk the JIT IR collecting every base name that is the target of an
38
+ * `AssignMember` stmt (stage 22 struct field write). Used by the
39
+ * codegen to emit a one-time `$h.structUnshare_h(s)` at function entry
40
+ * for any such name that is also a function parameter — this preserves
41
+ * MATLAB value semantics when structs are passed by value and the
42
+ * callee mutates a field.
43
+ */
44
+ export declare function collectStructMemberWrites(body: JitStmt[]): Set<string>;
45
+ /**
46
+ * Walk the JIT IR collecting every name that is the target of any
47
+ * plain `Assign` stmt. Used by the stage-12 struct-field-read hoist to
48
+ * decide whether a struct-typed param is safe to hoist: if the body
49
+ * ever reassigns the name (e.g. `s = [];` promoted to struct, or `s =
50
+ * struct()` inside the loop), the pre-loop hoisted field aliases would
51
+ * be stale and must be disabled.
52
+ */
53
+ export declare function collectPlainAssignTargets(body: JitStmt[]): Set<string>;
54
+ /**
55
+ * Walk the JIT IR collecting all unique (baseName, fieldName) pairs
56
+ * referenced by MemberRead nodes. The codegen hoists each pair as a
57
+ * local alias at function entry.
58
+ */
59
+ export declare function collectStructFieldReads(body: JitStmt[]): Map<string, {
60
+ baseName: string;
61
+ fieldName: string;
62
+ }>;
63
+ /**
64
+ * Walk the JIT IR collecting all unique (structVarName, structArrayFieldName)
65
+ * pairs referenced by StructArrayMemberRead nodes.
66
+ */
67
+ export declare function collectStructArrayElementReads(body: JitStmt[]): Map<string, {
68
+ structVarName: string;
69
+ structArrayFieldName: string;
70
+ }>;