xbintsc 0.1.3 → 0.1.8
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.
- package/README.md +14 -13
- package/dist/src/ast/common.d.ts +33 -0
- package/dist/src/ast/common.js +7 -0
- package/dist/src/ast/common.js.map +1 -0
- package/dist/src/ast/declarations.d.ts +110 -0
- package/dist/src/ast/declarations.js +5 -0
- package/dist/src/ast/declarations.js.map +1 -0
- package/dist/src/ast/expressions.d.ts +219 -0
- package/dist/src/ast/expressions.js +6 -0
- package/dist/src/ast/expressions.js.map +1 -0
- package/dist/src/ast/kinds.d.ts +160 -0
- package/dist/src/ast/kinds.js +171 -0
- package/dist/src/ast/kinds.js.map +1 -0
- package/dist/src/ast/modules.d.ts +61 -0
- package/dist/src/ast/modules.js +5 -0
- package/dist/src/ast/modules.js.map +1 -0
- package/dist/src/ast/nodes.d.ts +11 -839
- package/dist/src/ast/nodes.js +11 -237
- package/dist/src/ast/nodes.js.map +1 -1
- package/dist/src/ast/operators.d.ts +71 -0
- package/dist/src/ast/operators.js +77 -0
- package/dist/src/ast/operators.js.map +1 -0
- package/dist/src/ast/statements.d.ts +120 -0
- package/dist/src/ast/statements.js +5 -0
- package/dist/src/ast/statements.js.map +1 -0
- package/dist/src/ast/types.d.ts +123 -0
- package/dist/src/ast/types.js +6 -0
- package/dist/src/ast/types.js.map +1 -0
- package/dist/src/codegen/generator/calls.d.ts +22 -0
- package/dist/src/codegen/generator/calls.js +301 -0
- package/dist/src/codegen/generator/calls.js.map +1 -0
- package/dist/src/codegen/generator/context.d.ts +45 -0
- package/dist/src/codegen/generator/context.js +128 -0
- package/dist/src/codegen/generator/context.js.map +1 -0
- package/dist/src/codegen/generator/expressions.d.ts +22 -0
- package/dist/src/codegen/generator/expressions.js +383 -0
- package/dist/src/codegen/generator/expressions.js.map +1 -0
- package/dist/src/codegen/generator/generator.d.ts +22 -0
- package/dist/src/codegen/generator/generator.js +21 -0
- package/dist/src/codegen/generator/generator.js.map +1 -0
- package/dist/src/codegen/generator/module.d.ts +15 -0
- package/dist/src/codegen/generator/module.js +114 -0
- package/dist/src/codegen/generator/module.js.map +1 -0
- package/dist/src/codegen/generator/state.d.ts +40 -0
- package/dist/src/codegen/generator/state.js +84 -0
- package/dist/src/codegen/generator/state.js.map +1 -0
- package/dist/src/codegen/generator/statements.d.ts +22 -0
- package/dist/src/codegen/generator/statements.js +425 -0
- package/dist/src/codegen/generator/statements.js.map +1 -0
- package/dist/src/codegen/generator/tables.d.ts +19 -0
- package/dist/src/codegen/generator/tables.js +153 -0
- package/dist/src/codegen/generator/tables.js.map +1 -0
- package/dist/src/codegen/llvm.d.ts +4 -13
- package/dist/src/codegen/llvm.js +4 -1159
- package/dist/src/codegen/llvm.js.map +1 -1
- package/dist/src/driver/compiler.js +13 -4
- package/dist/src/driver/compiler.js.map +1 -1
- package/dist/src/lexer/char.d.ts +78 -0
- package/dist/src/lexer/char.js +106 -0
- package/dist/src/lexer/char.js.map +1 -0
- package/dist/src/lexer/scanner.js +1 -98
- package/dist/src/lexer/scanner.js.map +1 -1
- package/dist/src/parser/context.d.ts +58 -0
- package/dist/src/parser/context.js +174 -0
- package/dist/src/parser/context.js.map +1 -0
- package/dist/src/parser/declarations.d.ts +26 -0
- package/dist/src/parser/declarations.js +461 -0
- package/dist/src/parser/declarations.js.map +1 -0
- package/dist/src/parser/expressions.d.ts +27 -0
- package/dist/src/parser/expressions.js +437 -0
- package/dist/src/parser/expressions.js.map +1 -0
- package/dist/src/parser/helpers.d.ts +7 -0
- package/dist/src/parser/helpers.js +36 -0
- package/dist/src/parser/helpers.js.map +1 -0
- package/dist/src/parser/literals.d.ts +16 -0
- package/dist/src/parser/literals.js +193 -0
- package/dist/src/parser/literals.js.map +1 -0
- package/dist/src/parser/modules.d.ts +12 -0
- package/dist/src/parser/modules.js +175 -0
- package/dist/src/parser/modules.js.map +1 -0
- package/dist/src/parser/operators.d.ts +12 -0
- package/dist/src/parser/operators.js +172 -0
- package/dist/src/parser/operators.js.map +1 -0
- package/dist/src/parser/parser.d.ts +17 -122
- package/dist/src/parser/parser.js +18 -2307
- package/dist/src/parser/parser.js.map +1 -1
- package/dist/src/parser/speculation.d.ts +6 -0
- package/dist/src/parser/speculation.js +10 -0
- package/dist/src/parser/speculation.js.map +1 -0
- package/dist/src/parser/statements.d.ts +28 -0
- package/dist/src/parser/statements.js +402 -0
- package/dist/src/parser/statements.js.map +1 -0
- package/dist/src/parser/types.d.ts +21 -0
- package/dist/src/parser/types.js +338 -0
- package/dist/src/parser/types.js.map +1 -0
- package/dist/tests/e2e/compile.test.js +122 -2
- package/dist/tests/e2e/compile.test.js.map +1 -1
- package/dist/tests/parser/parser.test.js +9 -1
- package/dist/tests/parser/parser.test.js.map +1 -1
- package/package.json +1 -1
- package/runtime/rt.h +32 -0
- package/runtime/rt_internal.h +106 -0
- package/runtime/xt_alloc.c +38 -0
- package/runtime/xt_builtins.c +192 -0
- package/runtime/xt_containers.c +225 -0
- package/runtime/xt_io.c +133 -0
- package/runtime/xt_stdlib.c +502 -0
- package/runtime/xt_values.c +291 -0
- package/scripts/build-runtime.mjs +8 -1
- package/src/ast/common.ts +210 -0
- package/src/ast/declarations.ts +130 -0
- package/src/ast/expressions.ts +275 -0
- package/src/ast/kinds.ts +176 -0
- package/src/ast/modules.ts +73 -0
- package/src/ast/nodes.ts +11 -1101
- package/src/ast/operators.ts +76 -0
- package/src/ast/statements.ts +148 -0
- package/src/ast/types.ts +173 -0
- package/src/codegen/generator/calls.ts +355 -0
- package/src/codegen/generator/context.ts +156 -0
- package/src/codegen/generator/expressions.ts +431 -0
- package/src/codegen/generator/generator.ts +35 -0
- package/src/codegen/generator/module.ts +139 -0
- package/src/codegen/generator/state.ts +126 -0
- package/src/codegen/generator/statements.ts +474 -0
- package/src/codegen/generator/tables.ts +171 -0
- package/src/codegen/llvm.ts +4 -1305
- package/src/driver/compiler.ts +17 -5
- package/src/lexer/char.ts +116 -0
- package/src/lexer/scanner.ts +9 -109
- package/src/parser/context.ts +196 -0
- package/src/parser/declarations.ts +502 -0
- package/src/parser/expressions.ts +465 -0
- package/src/parser/helpers.ts +42 -0
- package/src/parser/literals.ts +207 -0
- package/src/parser/modules.ts +194 -0
- package/src/parser/operators.ts +181 -0
- package/src/parser/parser.ts +35 -2369
- package/src/parser/speculation.ts +9 -0
- package/src/parser/statements.ts +457 -0
- package/src/parser/types.ts +365 -0
- package/runtime/xt_runtime.c +0 -682
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Calls, member access, optional chaining, object/array literals and closures.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
SyntaxKind,
|
|
7
|
+
type ArrowFunction,
|
|
8
|
+
type ArrayLiteralExpression,
|
|
9
|
+
type CallExpression,
|
|
10
|
+
type DeleteExpression,
|
|
11
|
+
type ElementAccessExpression,
|
|
12
|
+
type Expression,
|
|
13
|
+
type FunctionExpression,
|
|
14
|
+
type Identifier,
|
|
15
|
+
type ObjectLiteralExpression,
|
|
16
|
+
type PropertyAccessExpression,
|
|
17
|
+
} from "../../ast/nodes.js";
|
|
18
|
+
import { SymbolKind } from "../../binder/binder.js";
|
|
19
|
+
import { i64, numberLiteral, XT_TRUE, XT_UNDEFINED } from "../values.js";
|
|
20
|
+
import {
|
|
21
|
+
BUILTIN_METHODS,
|
|
22
|
+
CONSOLE_METHODS,
|
|
23
|
+
GLOBAL_FUNCTIONS,
|
|
24
|
+
MATH_CONSTANTS,
|
|
25
|
+
MATH_FUNCTIONS,
|
|
26
|
+
propertyNameText,
|
|
27
|
+
} from "./tables.js";
|
|
28
|
+
import type { Generator } from "./generator.js";
|
|
29
|
+
|
|
30
|
+
export interface CallMethods {
|
|
31
|
+
emitCall(this: Generator, node: CallExpression): string;
|
|
32
|
+
tryEmitBuiltinCall(this: Generator, node: CallExpression, callee: PropertyAccessExpression): string | undefined;
|
|
33
|
+
runtimeCall(this: Generator, name: string, args: readonly string[]): string;
|
|
34
|
+
emitArguments(this: Generator, args: readonly Expression[]): { argc: number; ptr: string };
|
|
35
|
+
emitDelete(this: Generator, node: DeleteExpression): string;
|
|
36
|
+
emitPropertyAccess(this: Generator, node: PropertyAccessExpression): string;
|
|
37
|
+
emitElementAccess(this: Generator, node: ElementAccessExpression): string;
|
|
38
|
+
emitOptional(this: Generator, objectValue: string, compute: () => string): string;
|
|
39
|
+
emitArrayLiteral(this: Generator, node: ArrayLiteralExpression): string;
|
|
40
|
+
emitObjectLiteral(this: Generator, node: ObjectLiteralExpression): string;
|
|
41
|
+
emitClosure(this: Generator, node: ArrowFunction | FunctionExpression): string;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export const callMethods: CallMethods = {
|
|
45
|
+
emitCall(node: CallExpression): string {
|
|
46
|
+
const callee = node.expression;
|
|
47
|
+
if (node.optional) {
|
|
48
|
+
const calleeValue = this.emitExpression(callee);
|
|
49
|
+
return this.emitOptional(calleeValue, () => {
|
|
50
|
+
const args = this.emitArguments(node.arguments);
|
|
51
|
+
const result = this.reg();
|
|
52
|
+
this.emit(` ${result} = call i64 @xt_closure_call(i64 ${calleeValue}, i32 ${args.argc}, i64* ${args.ptr})`);
|
|
53
|
+
return result;
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
if (
|
|
57
|
+
(callee.kind === SyntaxKind.PropertyAccessExpression || callee.kind === SyntaxKind.ElementAccessExpression) &&
|
|
58
|
+
(callee as PropertyAccessExpression | ElementAccessExpression).optional
|
|
59
|
+
) {
|
|
60
|
+
// `obj?.method(args)` / `obj?.[key](args)`: guard the receiver, then
|
|
61
|
+
// dispatch through the runtime, which understands object methods as well
|
|
62
|
+
// as the built-in array/string methods.
|
|
63
|
+
const access = callee as PropertyAccessExpression | ElementAccessExpression;
|
|
64
|
+
const object = this.emitExpression(access.expression);
|
|
65
|
+
return this.emitOptional(object, () => {
|
|
66
|
+
const name =
|
|
67
|
+
access.kind === SyntaxKind.PropertyAccessExpression
|
|
68
|
+
? this.stringValue((access as PropertyAccessExpression).name.text)
|
|
69
|
+
: this.emitExpression((access as ElementAccessExpression).argumentExpression);
|
|
70
|
+
const args = this.emitArguments(node.arguments);
|
|
71
|
+
const result = this.reg();
|
|
72
|
+
this.emit(
|
|
73
|
+
` ${result} = call i64 @xt_call_method(i64 ${object}, i64 ${name}, i32 ${args.argc}, i64* ${args.ptr})`,
|
|
74
|
+
);
|
|
75
|
+
return result;
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
if (callee.kind === SyntaxKind.PropertyAccessExpression) {
|
|
79
|
+
const special = this.tryEmitBuiltinCall(node, callee as PropertyAccessExpression);
|
|
80
|
+
if (special) return special;
|
|
81
|
+
}
|
|
82
|
+
if (callee.kind === SyntaxKind.Identifier) {
|
|
83
|
+
const symbol = this.binding.symbolOfIdentifier.get(callee as Identifier);
|
|
84
|
+
if (symbol && symbol.kind === SymbolKind.Function) {
|
|
85
|
+
const declaration = symbol.declarations[0];
|
|
86
|
+
const fn = declaration ? this.binding.functionOfNode.get(declaration) : undefined;
|
|
87
|
+
if (fn) {
|
|
88
|
+
const args = this.emitArguments(node.arguments);
|
|
89
|
+
const result = this.reg();
|
|
90
|
+
this.emit(
|
|
91
|
+
` ${result} = call i64 @${this.functionName(fn)}(i64 ${i64(XT_UNDEFINED)}, i32 ${args.argc}, i64* ${args.ptr})`,
|
|
92
|
+
);
|
|
93
|
+
return result;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
const globalFunction = !symbol ? GLOBAL_FUNCTIONS[(callee as Identifier).text] : undefined;
|
|
97
|
+
if (globalFunction) {
|
|
98
|
+
const args = this.emitArguments(node.arguments);
|
|
99
|
+
const result = this.reg();
|
|
100
|
+
this.emit(` ${result} = call i64 @${globalFunction}(i32 ${args.argc}, i64* ${args.ptr})`);
|
|
101
|
+
return result;
|
|
102
|
+
}
|
|
103
|
+
const builtin = this.builtins[(callee as Identifier).text];
|
|
104
|
+
if (!symbol && builtin) {
|
|
105
|
+
this.extraDeclarations.add(
|
|
106
|
+
builtin.returnVoid ? `declare void @${builtin.symbol}(i32, i64*)` : `declare i64 @${builtin.symbol}(i32, i64*)`,
|
|
107
|
+
);
|
|
108
|
+
const args = this.emitArguments(node.arguments);
|
|
109
|
+
if (builtin.returnVoid) {
|
|
110
|
+
this.emit(` call void @${builtin.symbol}(i32 ${args.argc}, i64* ${args.ptr})`);
|
|
111
|
+
return i64(XT_UNDEFINED);
|
|
112
|
+
}
|
|
113
|
+
const result = this.reg();
|
|
114
|
+
this.emit(` ${result} = call i64 @${builtin.symbol}(i32 ${args.argc}, i64* ${args.ptr})`);
|
|
115
|
+
return result;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
const calleeValue = this.emitExpression(callee);
|
|
119
|
+
const args = this.emitArguments(node.arguments);
|
|
120
|
+
const result = this.reg();
|
|
121
|
+
this.emit(` ${result} = call i64 @xt_closure_call(i64 ${calleeValue}, i32 ${args.argc}, i64* ${args.ptr})`);
|
|
122
|
+
return result;
|
|
123
|
+
},
|
|
124
|
+
|
|
125
|
+
tryEmitBuiltinCall(node: CallExpression, callee: PropertyAccessExpression): string | undefined {
|
|
126
|
+
const target = callee.expression;
|
|
127
|
+
const method = callee.name.text;
|
|
128
|
+
const targetIdentifier = target.kind === SyntaxKind.Identifier ? (target as Identifier) : undefined;
|
|
129
|
+
const targetSymbol = targetIdentifier ? this.binding.symbolOfIdentifier.get(targetIdentifier) : undefined;
|
|
130
|
+
|
|
131
|
+
if (targetIdentifier && targetIdentifier.text === "console" && !targetSymbol) {
|
|
132
|
+
const consoleFn = CONSOLE_METHODS[method];
|
|
133
|
+
if (consoleFn) {
|
|
134
|
+
const args = this.emitArguments(node.arguments);
|
|
135
|
+
this.emit(` call void @${consoleFn}(i32 ${args.argc}, i64* ${args.ptr})`);
|
|
136
|
+
return i64(XT_UNDEFINED);
|
|
137
|
+
}
|
|
138
|
+
return undefined;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (targetIdentifier && targetIdentifier.text === "Math" && !targetSymbol) {
|
|
142
|
+
if (!MATH_FUNCTIONS.has(method)) return undefined;
|
|
143
|
+
const name = this.stringValue(method);
|
|
144
|
+
const args = this.emitArguments(node.arguments);
|
|
145
|
+
const result = this.reg();
|
|
146
|
+
this.emit(` ${result} = call i64 @xt_math_call(i64 ${name}, i32 ${args.argc}, i64* ${args.ptr})`);
|
|
147
|
+
return result;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if (targetIdentifier && targetIdentifier.text === "Object" && !targetSymbol) {
|
|
151
|
+
if (method === "keys" || method === "values" || method === "entries") {
|
|
152
|
+
const argument = node.arguments.length > 0 ? this.emitExpression(node.arguments[0]!) : i64(XT_UNDEFINED);
|
|
153
|
+
const fn = method === "keys" ? "xt_object_keys" : method === "values" ? "xt_object_values" : "xt_object_entries";
|
|
154
|
+
return this.runtimeCall(fn, [argument]);
|
|
155
|
+
}
|
|
156
|
+
if (method === "assign") {
|
|
157
|
+
const args = this.emitArguments(node.arguments);
|
|
158
|
+
const result = this.reg();
|
|
159
|
+
this.emit(` ${result} = call i64 @xt_object_assign(i32 ${args.argc}, i64* ${args.ptr})`);
|
|
160
|
+
return result;
|
|
161
|
+
}
|
|
162
|
+
return undefined;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (BUILTIN_METHODS.has(method)) {
|
|
166
|
+
const object = this.emitExpression(target);
|
|
167
|
+
const name = this.stringValue(method);
|
|
168
|
+
const args = this.emitArguments(node.arguments);
|
|
169
|
+
const result = this.reg();
|
|
170
|
+
this.emit(
|
|
171
|
+
` ${result} = call i64 @xt_call_method(i64 ${object}, i64 ${name}, i32 ${args.argc}, i64* ${args.ptr})`,
|
|
172
|
+
);
|
|
173
|
+
return result;
|
|
174
|
+
}
|
|
175
|
+
return undefined;
|
|
176
|
+
},
|
|
177
|
+
|
|
178
|
+
runtimeCall(name: string, args: readonly string[]): string {
|
|
179
|
+
const result = this.reg();
|
|
180
|
+
this.emit(` ${result} = call i64 @${name}(${args.map((a) => `i64 ${a}`).join(", ")})`);
|
|
181
|
+
return result;
|
|
182
|
+
},
|
|
183
|
+
|
|
184
|
+
emitArguments(args: readonly Expression[]): { argc: number; ptr: string } {
|
|
185
|
+
if (args.length === 0) return { argc: 0, ptr: "null" };
|
|
186
|
+
const ptr = `%args${this.current.allocas.length}`;
|
|
187
|
+
this.current.allocas.push(`${ptr} = alloca i64, i32 ${args.length}`);
|
|
188
|
+
for (let index = 0; index < args.length; index++) {
|
|
189
|
+
const value = this.emitExpression(args[index]!);
|
|
190
|
+
const slot = this.reg();
|
|
191
|
+
this.emit(` ${slot} = getelementptr i64, i64* ${ptr}, i32 ${index}`);
|
|
192
|
+
this.emit(` store i64 ${value}, i64* ${slot}`);
|
|
193
|
+
}
|
|
194
|
+
return { argc: args.length, ptr };
|
|
195
|
+
},
|
|
196
|
+
|
|
197
|
+
emitDelete(node: DeleteExpression): string {
|
|
198
|
+
const target = node.expression;
|
|
199
|
+
if (target.kind === SyntaxKind.PropertyAccessExpression) {
|
|
200
|
+
const access = target as PropertyAccessExpression;
|
|
201
|
+
const object = this.emitExpression(access.expression);
|
|
202
|
+
const key = this.stringValue(access.name.text);
|
|
203
|
+
return this.runtimeCall("xt_delete", [object, key]);
|
|
204
|
+
}
|
|
205
|
+
if (target.kind === SyntaxKind.ElementAccessExpression) {
|
|
206
|
+
const access = target as ElementAccessExpression;
|
|
207
|
+
const object = this.emitExpression(access.expression);
|
|
208
|
+
const key = this.emitExpression(access.argumentExpression);
|
|
209
|
+
return this.runtimeCall("xt_delete", [object, key]);
|
|
210
|
+
}
|
|
211
|
+
return i64(XT_TRUE);
|
|
212
|
+
},
|
|
213
|
+
|
|
214
|
+
emitPropertyAccess(node: PropertyAccessExpression): string {
|
|
215
|
+
if (
|
|
216
|
+
node.name.text in MATH_CONSTANTS &&
|
|
217
|
+
node.expression.kind === SyntaxKind.Identifier &&
|
|
218
|
+
(node.expression as Identifier).text === "Math" &&
|
|
219
|
+
!this.binding.symbolOfIdentifier.get(node.expression as Identifier)
|
|
220
|
+
) {
|
|
221
|
+
return numberLiteral(MATH_CONSTANTS[node.name.text]!);
|
|
222
|
+
}
|
|
223
|
+
const object = this.emitExpression(node.expression);
|
|
224
|
+
const access = (): string => {
|
|
225
|
+
if (node.name.text === "length") {
|
|
226
|
+
const result = this.reg();
|
|
227
|
+
this.emit(` ${result} = call i64 @xt_array_length(i64 ${object})`);
|
|
228
|
+
return result;
|
|
229
|
+
}
|
|
230
|
+
const key = this.stringValue(node.name.text);
|
|
231
|
+
const result = this.reg();
|
|
232
|
+
this.emit(` ${result} = call i64 @xt_get(i64 ${object}, i64 ${key})`);
|
|
233
|
+
return result;
|
|
234
|
+
};
|
|
235
|
+
return node.optional ? this.emitOptional(object, access) : access();
|
|
236
|
+
},
|
|
237
|
+
|
|
238
|
+
emitElementAccess(node: ElementAccessExpression): string {
|
|
239
|
+
const object = this.emitExpression(node.expression);
|
|
240
|
+
const access = (): string => {
|
|
241
|
+
const key = this.emitExpression(node.argumentExpression);
|
|
242
|
+
const result = this.reg();
|
|
243
|
+
this.emit(` ${result} = call i64 @xt_get(i64 ${object}, i64 ${key})`);
|
|
244
|
+
return result;
|
|
245
|
+
};
|
|
246
|
+
return node.optional ? this.emitOptional(object, access) : access();
|
|
247
|
+
},
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Evaluate `object` once and, when it is neither `null` nor `undefined`, run
|
|
251
|
+
* `compute` to produce the value; otherwise short-circuit to `undefined`.
|
|
252
|
+
*/
|
|
253
|
+
emitOptional(objectValue: string, compute: () => string): string {
|
|
254
|
+
const result = this.alloca();
|
|
255
|
+
this.emit(` store i64 ${i64(XT_UNDEFINED)}, i64* ${result}`);
|
|
256
|
+
const nullish = this.reg();
|
|
257
|
+
this.emit(` ${nullish} = call i64 @xt_is_nullish(i64 ${objectValue})`);
|
|
258
|
+
const truthy = this.reg();
|
|
259
|
+
this.emit(` ${truthy} = call i32 @xt_truthy(i64 ${nullish})`);
|
|
260
|
+
const condition = this.reg();
|
|
261
|
+
this.emit(` ${condition} = icmp ne i32 ${truthy}, 0`);
|
|
262
|
+
const someLabel = this.label("opt.some");
|
|
263
|
+
const endLabel = this.label("opt.end");
|
|
264
|
+
this.terminate(`br i1 ${condition}, label %${endLabel}, label %${someLabel}`);
|
|
265
|
+
this.startBlock(someLabel);
|
|
266
|
+
const value = compute();
|
|
267
|
+
this.emit(` store i64 ${value}, i64* ${result}`);
|
|
268
|
+
this.terminate(`br label %${endLabel}`);
|
|
269
|
+
this.startBlock(endLabel);
|
|
270
|
+
const merged = this.reg();
|
|
271
|
+
this.emit(` ${merged} = load i64, i64* ${result}`);
|
|
272
|
+
return merged;
|
|
273
|
+
},
|
|
274
|
+
|
|
275
|
+
emitArrayLiteral(node: ArrayLiteralExpression): string {
|
|
276
|
+
const hasSpread = node.elements.some((element) => element.kind === SyntaxKind.SpreadElement);
|
|
277
|
+
if (!hasSpread) {
|
|
278
|
+
const args = this.emitArguments(node.elements);
|
|
279
|
+
const result = this.reg();
|
|
280
|
+
this.emit(` ${result} = call i64 @xt_array_new(i32 ${args.argc}, i64* ${args.ptr})`);
|
|
281
|
+
return result;
|
|
282
|
+
}
|
|
283
|
+
let array = this.runtimeCall("xt_array_new", [`0`, `null`]);
|
|
284
|
+
for (const element of node.elements) {
|
|
285
|
+
if (element.kind === SyntaxKind.SpreadElement) {
|
|
286
|
+
const spread = this.emitExpression((element as { expression: Expression }).expression);
|
|
287
|
+
array = this.runtimeCall("xt_array_spread", [array, spread]);
|
|
288
|
+
} else {
|
|
289
|
+
const value = this.emitExpression(element);
|
|
290
|
+
array = this.runtimeCall("xt_array_push", [array, value]);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
return array;
|
|
294
|
+
},
|
|
295
|
+
|
|
296
|
+
emitObjectLiteral(node: ObjectLiteralExpression): string {
|
|
297
|
+
const object = this.reg();
|
|
298
|
+
this.emit(` ${object} = call i64 @xt_object_new()`);
|
|
299
|
+
for (const property of node.properties) {
|
|
300
|
+
if (property.kind === SyntaxKind.PropertyAssignment) {
|
|
301
|
+
const name = propertyNameText(property.name);
|
|
302
|
+
const key = this.stringValue(name);
|
|
303
|
+
const value = this.emitExpression(property.initializer);
|
|
304
|
+
this.emit(` call i64 @xt_set(i64 ${object}, i64 ${key}, i64 ${value})`);
|
|
305
|
+
} else if (property.kind === SyntaxKind.ShorthandPropertyAssignment) {
|
|
306
|
+
const identifier = property.name;
|
|
307
|
+
const key = this.stringValue(identifier.text);
|
|
308
|
+
const value = this.emitIdentifier(identifier);
|
|
309
|
+
this.emit(` call i64 @xt_set(i64 ${object}, i64 ${key}, i64 ${value})`);
|
|
310
|
+
} else if (property.kind === SyntaxKind.SpreadElement) {
|
|
311
|
+
const spread = this.emitExpression((property as { expression: Expression }).expression);
|
|
312
|
+
this.emit(` call i64 @xt_object_spread(i64 ${object}, i64 ${spread})`);
|
|
313
|
+
} else {
|
|
314
|
+
this.unsupported(property, "object spread");
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
return object;
|
|
318
|
+
},
|
|
319
|
+
|
|
320
|
+
emitClosure(node: ArrowFunction | FunctionExpression): string {
|
|
321
|
+
const fn = this.binding.functionOfNode.get(node);
|
|
322
|
+
if (!fn) {
|
|
323
|
+
this.unsupported(node, "closure");
|
|
324
|
+
return i64(XT_UNDEFINED);
|
|
325
|
+
}
|
|
326
|
+
let envPtr = "null";
|
|
327
|
+
if (fn.captures.length > 0) {
|
|
328
|
+
envPtr = `%env${this.current.allocas.length}`;
|
|
329
|
+
this.current.allocas.push(`${envPtr} = alloca i64, i32 ${fn.captures.length}`);
|
|
330
|
+
for (let index = 0; index < fn.captures.length; index++) {
|
|
331
|
+
const symbol = fn.captures[index]!;
|
|
332
|
+
// The current function must be able to read the capture: either from
|
|
333
|
+
// its own slot (local) or from one of its own capture slots.
|
|
334
|
+
const slot = this.current.slots.get(symbol.id);
|
|
335
|
+
let raw: string;
|
|
336
|
+
if (slot) {
|
|
337
|
+
raw = this.reg();
|
|
338
|
+
this.emit(` ${raw} = load i64, i64* ${slot.ptr}`);
|
|
339
|
+
} else {
|
|
340
|
+
const outer = this.binding.functionOfNode.get(node);
|
|
341
|
+
void outer;
|
|
342
|
+
raw = i64(XT_UNDEFINED);
|
|
343
|
+
}
|
|
344
|
+
const target = this.reg();
|
|
345
|
+
this.emit(` ${target} = getelementptr i64, i64* ${envPtr}, i32 ${index}`);
|
|
346
|
+
this.emit(` store i64 ${raw}, i64* ${target}`);
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
const cast = this.reg();
|
|
350
|
+
this.emit(` ${cast} = bitcast i64 (i64, i32, i64*)* @${this.functionName(fn)} to i8*`);
|
|
351
|
+
const closure = this.reg();
|
|
352
|
+
this.emit(` ${closure} = call i64 @xt_closure_new(i8* ${cast}, i32 ${fn.captures.length}, i64* ${envPtr})`);
|
|
353
|
+
return closure;
|
|
354
|
+
},
|
|
355
|
+
};
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared generator state plus the low-level emission primitives.
|
|
3
|
+
*
|
|
4
|
+
* The generator's method groups are installed onto {@link Generator} via
|
|
5
|
+
* `Object.assign`; this context holds the fields they all mutate and the
|
|
6
|
+
* helpers every group relies on.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { bind, type BindResult, type SymbolInfo } from "../../binder/binder.js";
|
|
10
|
+
import type { DiagnosticBag } from "../../diagnostics/diagnostic.js";
|
|
11
|
+
import { DiagnosticCode } from "../../diagnostics/diagnostic.js";
|
|
12
|
+
import type { SourceFileNode, Node } from "../../ast/nodes.js";
|
|
13
|
+
import { i64, XT_UNDEFINED } from "../values.js";
|
|
14
|
+
import type { BuiltinFunction } from "../../extensions/registry.js";
|
|
15
|
+
import type { CodegenOptions, FunctionState } from "./state.js";
|
|
16
|
+
import { escapeBytes, kindName, utf8Bytes } from "./tables.js";
|
|
17
|
+
|
|
18
|
+
export class GeneratorContext {
|
|
19
|
+
readonly binding: BindResult;
|
|
20
|
+
readonly sourceFile: SourceFileNode;
|
|
21
|
+
readonly diagnostics: DiagnosticBag;
|
|
22
|
+
readonly builtins: Readonly<Record<string, BuiltinFunction>>;
|
|
23
|
+
readonly globals: string[] = [];
|
|
24
|
+
readonly functions: string[] = [];
|
|
25
|
+
readonly strings = new Map<string, { label: string; length: number }>();
|
|
26
|
+
readonly extraDeclarations = new Set<string>();
|
|
27
|
+
stringCounter = 0;
|
|
28
|
+
current!: FunctionState;
|
|
29
|
+
|
|
30
|
+
constructor(sourceFile: SourceFileNode, diagnostics: DiagnosticBag, options: CodegenOptions = {}) {
|
|
31
|
+
this.sourceFile = sourceFile;
|
|
32
|
+
this.diagnostics = diagnostics;
|
|
33
|
+
this.binding = bind(sourceFile);
|
|
34
|
+
this.builtins = options.builtins ?? {};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// -- emission primitives -------------------------------------------------
|
|
38
|
+
|
|
39
|
+
emit(line: string): void {
|
|
40
|
+
this.current.buffer.push(line);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
terminate(line: string): void {
|
|
44
|
+
this.current.buffer.push(` ${line}`);
|
|
45
|
+
this.current.terminated = true;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
reg(): string {
|
|
49
|
+
return `%r${this.current.reg++}`;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
label(prefix: string): string {
|
|
53
|
+
return `${prefix}.${this.current.label++}`;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
startBlock(label: string): void {
|
|
57
|
+
this.current.buffer.push(`${label}:`);
|
|
58
|
+
this.current.terminated = false;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
alloca(): string {
|
|
62
|
+
const ptr = `%slot${this.current.allocas.length}`;
|
|
63
|
+
this.current.allocas.push(`${ptr} = alloca i64`);
|
|
64
|
+
this.current.escapePointers.push(ptr);
|
|
65
|
+
return ptr;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const(hex: bigint): string {
|
|
69
|
+
return i64(hex);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// -- variables -----------------------------------------------------------
|
|
73
|
+
|
|
74
|
+
declareSlot(symbol: SymbolInfo, initial: string): void {
|
|
75
|
+
const ptr = this.alloca();
|
|
76
|
+
if (symbol.boxed) {
|
|
77
|
+
const box = this.reg();
|
|
78
|
+
this.emit(` ${box} = call i64 @xt_box_new(i64 ${initial})`);
|
|
79
|
+
this.emit(` store i64 ${box}, i64* ${ptr}`);
|
|
80
|
+
} else {
|
|
81
|
+
this.emit(` store i64 ${initial}, i64* ${ptr}`);
|
|
82
|
+
}
|
|
83
|
+
this.current.slots.set(symbol.id, { ptr, boxed: symbol.boxed });
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
defineCaptureSlot(symbol: SymbolInfo, box: string): void {
|
|
87
|
+
const ptr = this.alloca();
|
|
88
|
+
this.emit(` store i64 ${box}, i64* ${ptr}`);
|
|
89
|
+
// A captured symbol is always boxed; the environment holds the box.
|
|
90
|
+
this.current.slots.set(symbol.id, { ptr, boxed: true });
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
readSlot(symbol: SymbolInfo): string {
|
|
94
|
+
const slot = this.current.slots.get(symbol.id);
|
|
95
|
+
if (!slot) {
|
|
96
|
+
// Referenced from an inner function without a capture slot: this should
|
|
97
|
+
// be impossible once the binder has run, but stay defensive.
|
|
98
|
+
this.diagnostics.error(
|
|
99
|
+
DiagnosticCode.CodegenError,
|
|
100
|
+
`Internal: no slot for '${symbol.name}' in ${this.current.fn.name}`,
|
|
101
|
+
);
|
|
102
|
+
return i64(XT_UNDEFINED);
|
|
103
|
+
}
|
|
104
|
+
const value = this.reg();
|
|
105
|
+
this.emit(` ${value} = load i64, i64* ${slot.ptr}`);
|
|
106
|
+
if (!slot.boxed) return value;
|
|
107
|
+
const unboxed = this.reg();
|
|
108
|
+
this.emit(` ${unboxed} = call i64 @xt_box_get(i64 ${value})`);
|
|
109
|
+
return unboxed;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
writeSlot(symbol: SymbolInfo, value: string): void {
|
|
113
|
+
const slot = this.current.slots.get(symbol.id);
|
|
114
|
+
if (!slot) {
|
|
115
|
+
this.diagnostics.error(DiagnosticCode.CodegenError, `Internal: no slot for '${symbol.name}'`);
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
if (!slot.boxed) {
|
|
119
|
+
this.emit(` store i64 ${value}, i64* ${slot.ptr}`);
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
const box = this.reg();
|
|
123
|
+
this.emit(` ${box} = load i64, i64* ${slot.ptr}`);
|
|
124
|
+
this.emit(` call i64 @xt_box_set(i64 ${box}, i64 ${value})`);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// -- string pool ---------------------------------------------------------
|
|
128
|
+
|
|
129
|
+
stringValue(text: string): string {
|
|
130
|
+
const entry = this.internString(text);
|
|
131
|
+
const result = this.reg();
|
|
132
|
+
this.emit(` ${result} = call i64 @xt_string_new(i8* ${entry.label}, i64 ${entry.length})`);
|
|
133
|
+
return result;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
internString(text: string): { label: string; length: number } {
|
|
137
|
+
const existing = this.strings.get(text);
|
|
138
|
+
if (existing) return existing;
|
|
139
|
+
const label = `@.str.${this.stringCounter++}`;
|
|
140
|
+
const bytes = utf8Bytes(text);
|
|
141
|
+
const entry = { label, length: bytes.length };
|
|
142
|
+
this.strings.set(text, entry);
|
|
143
|
+
this.globals.push(`${label} = private unnamed_addr constant [${bytes.length + 1} x i8] c"${escapeBytes(bytes)}\\00"`);
|
|
144
|
+
return entry;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
unsupported(node: Node, what: string): void {
|
|
148
|
+
const label = kindName(node.kind);
|
|
149
|
+
this.diagnostics.error(
|
|
150
|
+
DiagnosticCode.UnsupportedFeature,
|
|
151
|
+
`xbintsc does not yet support this ${what} (${label})`,
|
|
152
|
+
node,
|
|
153
|
+
this.sourceFile.fileName,
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
}
|