@oseo/backend-c 0.0.0-stage → 0.1.0-canary.370
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/LICENSE +674 -0
- package/MIT-LICENSE.txt +21 -0
- package/README.md +58 -2
- package/dist/bounded-body.d.ts +28 -0
- package/dist/bounded-body.d.ts.map +1 -0
- package/dist/bounded-body.js +198 -0
- package/dist/bounded-body.js.map +1 -0
- package/dist/emitted-c.d.ts +676 -0
- package/dist/emitted-c.d.ts.map +1 -0
- package/dist/emitted-c.js +1331 -0
- package/dist/emitted-c.js.map +1 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2282 -0
- package/dist/index.js.map +1 -0
- package/dist/regexp-program.d.ts +64 -0
- package/dist/regexp-program.d.ts.map +1 -0
- package/dist/regexp-program.js +357 -0
- package/dist/regexp-program.js.map +1 -0
- package/package.json +36 -3
package/dist/index.js
ADDED
|
@@ -0,0 +1,2282 @@
|
|
|
1
|
+
import { BodyUnits, bodyUnit, boundBody, replaceSyntax } from "./bounded-body.js";
|
|
2
|
+
import { emittedC as emittedC$1, emittedCNotice } from "./emitted-c.js";
|
|
3
|
+
import { encodeRegExpProgram } from "./regexp-program.js";
|
|
4
|
+
import { lowerFragmentPhases, scriptFragmentAbi } from "@oseo/compiler";
|
|
5
|
+
|
|
6
|
+
//#region src/index.ts
|
|
7
|
+
function includePropertiesWhen(properties) {
|
|
8
|
+
return properties() ?? {};
|
|
9
|
+
}
|
|
10
|
+
function normalizeCCatalog(catalog) {
|
|
11
|
+
const normalized = {};
|
|
12
|
+
for (const [groupName, group] of Object.entries(catalog)) {
|
|
13
|
+
const normalizedGroup = {};
|
|
14
|
+
for (const [fragmentName, fragment] of Object.entries(group)) normalizedGroup[fragmentName] = fragment.map((segment) => segment.join(""));
|
|
15
|
+
normalized[groupName] = normalizedGroup;
|
|
16
|
+
}
|
|
17
|
+
return normalized;
|
|
18
|
+
}
|
|
19
|
+
const emittedC = normalizeCCatalog(emittedC$1);
|
|
20
|
+
function renderC(segments, ...values) {
|
|
21
|
+
if (segments.length !== values.length + 1) throw new Error(`C fragment needs ${segments.length - 1} values, got ${values.length}.`);
|
|
22
|
+
if (values.length === 0) return segments[0] ?? "";
|
|
23
|
+
let result = "";
|
|
24
|
+
for (let index = 0; index < values.length; index += 1) {
|
|
25
|
+
result += segments[index] ?? "";
|
|
26
|
+
result += String(values[index]);
|
|
27
|
+
}
|
|
28
|
+
result += segments.at(-1) ?? "";
|
|
29
|
+
return result;
|
|
30
|
+
}
|
|
31
|
+
/** Leave a generated function with a normal completion. */
|
|
32
|
+
function emitNormalReturn(state, value, indent = "") {
|
|
33
|
+
line(state, renderC(emittedC.normalReturn.resultAssignOseoResultOseoStatusNormal, indent, value));
|
|
34
|
+
if (!state.generator) line(state, renderC(emittedC.normalReturn.releaseRoots, indent));
|
|
35
|
+
line(state, renderC(emittedC.normalReturn.returnResult, indent));
|
|
36
|
+
}
|
|
37
|
+
function escapeCString(value) {
|
|
38
|
+
let result = "";
|
|
39
|
+
for (const character of value) {
|
|
40
|
+
const codePoint = character.codePointAt(0);
|
|
41
|
+
if (character === "\\") result += renderC(emittedC.cString.backslashEscape);
|
|
42
|
+
else if (character === "\"") result += renderC(emittedC.cString.quoteEscape);
|
|
43
|
+
else if (character === "?") result += renderC(emittedC.cString.questionMarkEscape);
|
|
44
|
+
else if (character === "\n") result += renderC(emittedC.cString.newlineEscape);
|
|
45
|
+
else if (codePoint != null && codePoint >= 32 && codePoint <= 126) result += character;
|
|
46
|
+
else if (codePoint != null) {
|
|
47
|
+
const bytes = new TextEncoder().encode(character);
|
|
48
|
+
for (const byte of bytes) result += renderC(emittedC.cString.octalEscape, byte.toString(8).padStart(3, renderC(emittedC.cString.octalPadding)));
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return result;
|
|
52
|
+
}
|
|
53
|
+
function line(state, source) {
|
|
54
|
+
state.lines.push(renderC(emittedC.line.indentLine, source));
|
|
55
|
+
}
|
|
56
|
+
function location(state, range) {
|
|
57
|
+
if (state.fragmentUnit != null) {
|
|
58
|
+
line(state, `oseo_fragment_location(context, ${state.fragmentUnit === "harness" ? 0 : 1}u, ${range.start.line}u, ${range.start.column}u);`);
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
if (range.sourceId != null) {
|
|
62
|
+
const sourceId = escapeCString(range.sourceId);
|
|
63
|
+
const length = new TextEncoder().encode(range.sourceId).length;
|
|
64
|
+
line(state, renderC(emittedC.location.sourceLocationPrefix, sourceId, length) + renderC(emittedC.common.sourcePositionSuffix, range.start.line, range.start.column));
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
line(state, renderC(emittedC.location.locationPrefix, range.start.line) + renderC(emittedC.common.positionSuffix, range.start.column));
|
|
68
|
+
}
|
|
69
|
+
function operationArgument(operation, index) {
|
|
70
|
+
const value = operation.arguments[index];
|
|
71
|
+
if (value == null) throw new Error(`MIR ${operation.kind} operation %${operation.id} is missing argument ${index}.`);
|
|
72
|
+
return value;
|
|
73
|
+
}
|
|
74
|
+
function operatorHelper(operator) {
|
|
75
|
+
return {
|
|
76
|
+
"!=": renderC(emittedC.operatorHelper.oseoNotLooseEqual),
|
|
77
|
+
"!==": renderC(emittedC.operatorHelper.oseoNotStrictEqual),
|
|
78
|
+
"%": renderC(emittedC.operatorHelper.oseoRemainder),
|
|
79
|
+
"&": renderC(emittedC.operatorHelper.oseoBitwiseAnd),
|
|
80
|
+
"*": renderC(emittedC.operatorHelper.oseoMultiply),
|
|
81
|
+
"**": renderC(emittedC.operatorHelper.oseoExponentiate),
|
|
82
|
+
"+": renderC(emittedC.operatorHelper.oseoAdd),
|
|
83
|
+
"-": renderC(emittedC.operatorHelper.oseoSubtract),
|
|
84
|
+
"/": renderC(emittedC.operatorHelper.oseoDivide),
|
|
85
|
+
"<": renderC(emittedC.operatorHelper.oseoLessThan),
|
|
86
|
+
"<<": renderC(emittedC.operatorHelper.oseoShiftLeft),
|
|
87
|
+
"<=": renderC(emittedC.operatorHelper.oseoLessEqual),
|
|
88
|
+
"==": renderC(emittedC.operatorHelper.oseoLooseEqual),
|
|
89
|
+
"===": renderC(emittedC.operatorHelper.oseoStrictEqual),
|
|
90
|
+
">": renderC(emittedC.operatorHelper.oseoGreaterThan),
|
|
91
|
+
">=": renderC(emittedC.operatorHelper.oseoGreaterEqual),
|
|
92
|
+
">>": renderC(emittedC.operatorHelper.oseoShiftRight),
|
|
93
|
+
">>>": renderC(emittedC.operatorHelper.oseoShiftRightUnsigned),
|
|
94
|
+
"^": renderC(emittedC.operatorHelper.oseoBitwiseXor),
|
|
95
|
+
in: renderC(emittedC.operatorHelper.oseoHasProperty),
|
|
96
|
+
instanceof: renderC(emittedC.operatorHelper.oseoInstanceof),
|
|
97
|
+
"|": renderC(emittedC.operatorHelper.oseoBitwiseOr)
|
|
98
|
+
}[operator];
|
|
99
|
+
}
|
|
100
|
+
function numberLiteral(value) {
|
|
101
|
+
if (Number.isNaN(value)) return renderC(emittedC.numberLiteral.nan);
|
|
102
|
+
if (value === Infinity) return renderC(emittedC.numberLiteral.positiveInfinity);
|
|
103
|
+
if (value === -Infinity) return renderC(emittedC.numberLiteral.negativeInfinity);
|
|
104
|
+
if (Object.is(value, -0)) return renderC(emittedC.numberLiteral.negativeZero);
|
|
105
|
+
const text = value.toString();
|
|
106
|
+
if (Number.isInteger(value) && !text.includes("e")) return renderC(emittedC.numberLiteral.integerAsDouble, text);
|
|
107
|
+
return text;
|
|
108
|
+
}
|
|
109
|
+
function utf16Units(value) {
|
|
110
|
+
const units = [];
|
|
111
|
+
for (let index = 0; index < value.length; index += 1) units.push(value.charCodeAt(index));
|
|
112
|
+
return units;
|
|
113
|
+
}
|
|
114
|
+
function emitStringConstant(state, operation, value) {
|
|
115
|
+
const units = utf16Units(value);
|
|
116
|
+
let input = renderC(emittedC.stringConstant.nullU);
|
|
117
|
+
if (units.length > 0) {
|
|
118
|
+
const name = renderC(emittedC.stringConstant.stringUnits, operation.id);
|
|
119
|
+
line(state, renderC(emittedC.common.staticConstUint16TAssignStatement, name, units.join(renderC(emittedC.common.commaSpace))));
|
|
120
|
+
input = renderC(emittedC.stringConstant.unitsWithLength, name, units.length);
|
|
121
|
+
}
|
|
122
|
+
location(state, operation.range);
|
|
123
|
+
state.usesAbrupt = true;
|
|
124
|
+
line(state, renderC(emittedC.stringConstant.resultAssignOseoStringFromUnitsContext, input));
|
|
125
|
+
line(state, renderC(emittedC.common.rootAssignResultValue, operation.id));
|
|
126
|
+
}
|
|
127
|
+
function emitConstant(state, operation, constant) {
|
|
128
|
+
if (constant.kind === "string") emitStringConstant(state, operation, constant.value);
|
|
129
|
+
else if (constant.kind === "bigint") {
|
|
130
|
+
if (!{
|
|
131
|
+
2: /^[01]+$/u,
|
|
132
|
+
8: /^[0-7]+$/u,
|
|
133
|
+
10: /^[0-9]+$/u,
|
|
134
|
+
16: /^[0-9a-f]+$/u
|
|
135
|
+
}[constant.radix].test(constant.digits)) throw new Error(`MIR constant %${operation.id} has invalid BigInt digits.`);
|
|
136
|
+
location(state, operation.range);
|
|
137
|
+
state.usesAbrupt = true;
|
|
138
|
+
line(state, renderC(emittedC.constant.resultAssignBigIntLiteral, constant.digits, constant.radix));
|
|
139
|
+
line(state, renderC(emittedC.common.rootAssignResultValue, operation.id));
|
|
140
|
+
} else if (constant.kind === "undefined") line(state, renderC(emittedC.common.rootsAssignOseoUndefinedStatement, operation.id));
|
|
141
|
+
else if (constant.kind === "null") line(state, renderC(emittedC.constant.rootsAssignOseoNullStatement, operation.id));
|
|
142
|
+
else if (constant.kind === "boolean") {
|
|
143
|
+
const value = constant.value ? renderC(emittedC.common.trueValue) : renderC(emittedC.common.falseValue);
|
|
144
|
+
line(state, renderC(emittedC.common.rootsAssignOseoBooleanStatement, operation.id, value));
|
|
145
|
+
} else {
|
|
146
|
+
const value = numberLiteral(constant.value);
|
|
147
|
+
line(state, renderC(emittedC.constant.rootsAssignOseoNumberStatement, operation.id, value));
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Emit one ahead-of-time compiled regular expression literal.
|
|
152
|
+
*
|
|
153
|
+
* Every array here is generated data the build already produced, so
|
|
154
|
+
* evaluating the operation parses no pattern and compiles no program: it
|
|
155
|
+
* allocates a fresh object over the realm's one immutable artifact for
|
|
156
|
+
* this descriptor, which is what keeps repeated evaluation from sharing
|
|
157
|
+
* `lastIndex`.
|
|
158
|
+
*/
|
|
159
|
+
function emitRegExpLiteral(state, operation) {
|
|
160
|
+
const program = operation.regexpProgram;
|
|
161
|
+
if (program == null) throw new Error(`MIR regexp-literal %${operation.id} has no artifact.`);
|
|
162
|
+
const encoded = encodeRegExpProgram(program);
|
|
163
|
+
const suffix = String(operation.id);
|
|
164
|
+
const comma = renderC(emittedC.common.commaSpace);
|
|
165
|
+
const nothing = renderC(emittedC.common.nullPointer);
|
|
166
|
+
const count = (value) => renderC(emittedC.regexpLiteral.unsignedValue, value);
|
|
167
|
+
const declare = (fragment, name, values) => {
|
|
168
|
+
if (values.length === 0) return nothing;
|
|
169
|
+
line(state, renderC(fragment, name, values.join(comma)));
|
|
170
|
+
return name;
|
|
171
|
+
};
|
|
172
|
+
const sourceUnits = declare(emittedC.regexpLiteral.uint16ArrayDeclaration, `regexp_source_units_${suffix}`, encoded.sourceUnits.map(count));
|
|
173
|
+
const flagUnits = declare(emittedC.regexpLiteral.uint16ArrayDeclaration, `regexp_flag_units_${suffix}`, encoded.flagUnits.map(count));
|
|
174
|
+
const instructions = declare(emittedC.regexpLiteral.instructionArrayDeclaration, `regexp_instructions_${suffix}`, encoded.instructions.map((instruction) => renderC(emittedC.regexpLiteral.instructionValue, instruction.opcode, instruction.modifiers, instruction.operands[0], instruction.operands[1], instruction.operands[2], instruction.operands[3])));
|
|
175
|
+
const boundaries = encoded.setBoundaries.length === 0 && encoded.setOffsets.length > 1 ? [0] : encoded.setBoundaries;
|
|
176
|
+
const setBoundaries = declare(emittedC.regexpLiteral.uint32ArrayDeclaration, `regexp_set_boundaries_${suffix}`, boundaries.map(count));
|
|
177
|
+
const setOffsets = declare(emittedC.regexpLiteral.uint32ArrayDeclaration, `regexp_set_offsets_${suffix}`, encoded.setOffsets.map(count));
|
|
178
|
+
const repeats = declare(emittedC.regexpLiteral.repeatArrayDeclaration, `regexp_repeats_${suffix}`, encoded.repeats.map((repeat) => renderC(emittedC.regexpLiteral.repeatValue, renderC(emittedC.regexpLiteral.unsignedLongValue, repeat.minimum), repeat.maximum == null ? renderC(emittedC.regexpLiteral.unboundedValue) : renderC(emittedC.regexpLiteral.unsignedLongValue, repeat.maximum), repeat.clearFrom, repeat.clearTo)));
|
|
179
|
+
const nameUnits = declare(emittedC.regexpLiteral.uint16ArrayDeclaration, `regexp_name_units_${suffix}`, encoded.nameUnits.map(count));
|
|
180
|
+
const captures = declare(emittedC.regexpLiteral.captureArrayDeclaration, `regexp_captures_${suffix}`, encoded.captures.map((capture) => renderC(emittedC.regexpLiteral.captureValue, capture.nameOffset, capture.nameLength, renderC(capture.named ? emittedC.common.trueValue : emittedC.common.falseValue))));
|
|
181
|
+
const canonicalCharacters = declare(emittedC.regexpLiteral.uint32ArrayDeclaration, `regexp_canonical_characters_${suffix}`, encoded.canonicalCharacters.map(count));
|
|
182
|
+
const canonicalValues = declare(emittedC.regexpLiteral.uint32ArrayDeclaration, `regexp_canonical_values_${suffix}`, encoded.canonicalValues.map(count));
|
|
183
|
+
const flag = (value) => renderC(value ? emittedC.common.trueValue : emittedC.common.falseValue);
|
|
184
|
+
const programName = `regexp_program_${suffix}`;
|
|
185
|
+
line(state, renderC(emittedC.regexpLiteral.programDeclaration, programName, [
|
|
186
|
+
instructions,
|
|
187
|
+
count(encoded.instructions.length),
|
|
188
|
+
setBoundaries,
|
|
189
|
+
setOffsets,
|
|
190
|
+
count(Math.max(encoded.setOffsets.length - 1, 0)),
|
|
191
|
+
repeats,
|
|
192
|
+
count(encoded.repeats.length),
|
|
193
|
+
nameUnits,
|
|
194
|
+
count(encoded.nameUnits.length),
|
|
195
|
+
captures,
|
|
196
|
+
count(encoded.captures.length),
|
|
197
|
+
count(encoded.registers),
|
|
198
|
+
canonicalCharacters,
|
|
199
|
+
canonicalValues,
|
|
200
|
+
count(encoded.canonicalCharacters.length),
|
|
201
|
+
flag(encoded.unicodeMode),
|
|
202
|
+
flag(encoded.ignoreCase),
|
|
203
|
+
flag(encoded.hasGroupNames)
|
|
204
|
+
].join(comma)));
|
|
205
|
+
const literalName = `regexp_literal_${suffix}`;
|
|
206
|
+
line(state, renderC(emittedC.regexpLiteral.literalDeclaration, literalName, [
|
|
207
|
+
sourceUnits,
|
|
208
|
+
count(encoded.sourceUnits.length),
|
|
209
|
+
flagUnits,
|
|
210
|
+
count(encoded.flagUnits.length),
|
|
211
|
+
renderC(emittedC.regexpLiteral.programAddress, programName),
|
|
212
|
+
count(encoded.flagMask)
|
|
213
|
+
].join(comma)));
|
|
214
|
+
location(state, operation.range);
|
|
215
|
+
state.usesAbrupt = true;
|
|
216
|
+
line(state, renderC(emittedC.regexpLiteral.resultAssign, literalName));
|
|
217
|
+
line(state, renderC(emittedC.common.rootAssignResultValue, operation.id));
|
|
218
|
+
}
|
|
219
|
+
function emitTemplateObject(state, operation) {
|
|
220
|
+
const cooked = operation.templateCooked;
|
|
221
|
+
const raw = operation.templateRaw;
|
|
222
|
+
if (cooked == null || raw == null || cooked.length === 0 || cooked.length !== raw.length) throw new Error(`MIR template-object %${operation.id} has invalid strings.`);
|
|
223
|
+
const suffix = String(operation.id);
|
|
224
|
+
const site = `template_site_${suffix}`;
|
|
225
|
+
const cookedPointers = `template_cooked_${suffix}`;
|
|
226
|
+
const cookedLengths = `template_cooked_lengths_${suffix}`;
|
|
227
|
+
const cookedDefined = `template_cooked_defined_${suffix}`;
|
|
228
|
+
const rawPointers = `template_raw_${suffix}`;
|
|
229
|
+
const rawLengths = `template_raw_lengths_${suffix}`;
|
|
230
|
+
const cookedInputs = [];
|
|
231
|
+
const rawInputs = [];
|
|
232
|
+
const emitUnits = (kind, value, index) => {
|
|
233
|
+
const units = utf16Units(value);
|
|
234
|
+
if (units.length === 0) return renderC(emittedC.common.nullPointer);
|
|
235
|
+
const name = `template_${kind}_units_${suffix}_${index}`;
|
|
236
|
+
line(state, renderC(emittedC.common.staticConstUint16TAssignStatement, name, units.join(renderC(emittedC.common.commaSpace))));
|
|
237
|
+
return name;
|
|
238
|
+
};
|
|
239
|
+
for (let index = 0; index < cooked.length; index += 1) {
|
|
240
|
+
const cookedPiece = cooked[index];
|
|
241
|
+
cookedInputs.push(cookedPiece == null ? renderC(emittedC.common.nullPointer) : emitUnits("cooked", cookedPiece, index));
|
|
242
|
+
rawInputs.push(emitUnits("raw", raw[index], index));
|
|
243
|
+
}
|
|
244
|
+
const comma = renderC(emittedC.common.commaSpace);
|
|
245
|
+
line(state, renderC(emittedC.templateObject.siteDeclaration, site));
|
|
246
|
+
line(state, renderC(emittedC.templateObject.pointerArrayDeclaration, cookedPointers, cookedInputs.join(comma)));
|
|
247
|
+
line(state, renderC(emittedC.templateObject.sizeArrayDeclaration, cookedLengths, cooked.map((piece) => `${piece?.length ?? 0}u`).join(comma)));
|
|
248
|
+
line(state, renderC(emittedC.templateObject.boolArrayDeclaration, cookedDefined, cooked.map((piece) => piece == null ? renderC(emittedC.common.falseValue) : renderC(emittedC.common.trueValue)).join(comma)));
|
|
249
|
+
line(state, renderC(emittedC.templateObject.pointerArrayDeclaration, rawPointers, rawInputs.join(comma)));
|
|
250
|
+
line(state, renderC(emittedC.templateObject.sizeArrayDeclaration, rawLengths, raw.map((piece) => `${piece.length}u`).join(comma)));
|
|
251
|
+
location(state, operation.range);
|
|
252
|
+
state.usesAbrupt = true;
|
|
253
|
+
line(state, renderC(emittedC.templateObject.resultAssign, site, cooked.length, cookedPointers, cookedLengths, cookedDefined, rawPointers, rawLengths));
|
|
254
|
+
line(state, renderC(emittedC.common.rootAssignResultValue, operation.id));
|
|
255
|
+
}
|
|
256
|
+
function emitArguments(state, operation) {
|
|
257
|
+
if (operation.forwardArguments === true) return {
|
|
258
|
+
count: renderC(emittedC.arguments.incomingCount),
|
|
259
|
+
name: renderC(emittedC.arguments.incomingValues)
|
|
260
|
+
};
|
|
261
|
+
if (operation.argumentListId != null) {
|
|
262
|
+
const count = renderC(emittedC.arguments.argumentCount, operation.id);
|
|
263
|
+
const name = renderC(emittedC.arguments.argumentValues, operation.id);
|
|
264
|
+
line(state, renderC(emittedC.arguments.sizeTAssignUStatement, count));
|
|
265
|
+
line(state, renderC(emittedC.arguments.constOseoValuePointerAssignNullStatement, name));
|
|
266
|
+
line(state, renderC(emittedC.arguments.resultAssignOseoArgumentListViewContext) + renderC(emittedC.arguments.rootsAddressAddressStatement, operation.argumentListId, count, name));
|
|
267
|
+
line(state, renderC(emittedC.common.gotoAbruptUnlessNormal));
|
|
268
|
+
return {
|
|
269
|
+
count,
|
|
270
|
+
name
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
if (operation.arguments.length === 0) return {
|
|
274
|
+
count: renderC(emittedC.arguments.zeroUnsigned),
|
|
275
|
+
name: renderC(emittedC.common.nullPointer)
|
|
276
|
+
};
|
|
277
|
+
for (let index = 0; index < operation.arguments.length; index += 1) {
|
|
278
|
+
const value = operationArgument(operation, index);
|
|
279
|
+
line(state, renderC(emittedC.common.rootAssignRoot, state.argumentSlotStart + index, value));
|
|
280
|
+
}
|
|
281
|
+
return {
|
|
282
|
+
count: renderC(emittedC.arguments.unsignedCount, operation.arguments.length),
|
|
283
|
+
name: renderC(emittedC.arguments.addressOfRoot, state.argumentSlotStart)
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
function emittedArgument(operation, emitted, index) {
|
|
287
|
+
if (operation.argumentListId != null) return renderC(emittedC.argument.boundedArgumentCount, emitted.count, index) + renderC(emittedC.argument.oseoUndefined, emitted.name, index);
|
|
288
|
+
const value = operation.arguments[index];
|
|
289
|
+
return value == null ? renderC(emittedC.common.undefinedValue) : renderC(emittedC.common.root, value);
|
|
290
|
+
}
|
|
291
|
+
/** Pass one static call-site record to the reusable native composition. */
|
|
292
|
+
function generatedOperation(state, operation, first, second) {
|
|
293
|
+
const name = `generated_operation_${operation.id}`;
|
|
294
|
+
const range = operation.range;
|
|
295
|
+
const source = range.sourceId == null ? "NULL" : `"${escapeCString(range.sourceId)}"`;
|
|
296
|
+
const length = range.sourceId == null ? 0 : new TextEncoder().encode(range.sourceId).length;
|
|
297
|
+
line(state, `static const OseoGeneratedOperation ${name} = {${operation.id}u, ${first}u, ${second}u, ${source}, ${length}u, ${range.start.line}u, ${range.start.column}u};`);
|
|
298
|
+
return name;
|
|
299
|
+
}
|
|
300
|
+
function emitRead(state, operation) {
|
|
301
|
+
const bindingId = operation.bindingId;
|
|
302
|
+
if (bindingId == null) throw new Error(`MIR read %${operation.id} has no bound value.`);
|
|
303
|
+
if (state.fragmentUnit == null) {
|
|
304
|
+
state.usesAbrupt = true;
|
|
305
|
+
line(state, `if (!oseo_generated_binding_read_at(context, roots, &${generatedOperation(state, operation, state.environmentSlot, bindingId)}, &result)) goto abrupt;`);
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
location(state, operation.range);
|
|
309
|
+
state.usesAbrupt = true;
|
|
310
|
+
line(state, `if (!oseo_generated_binding_read(context, roots[${state.environmentSlot}], ${bindingId}u, &result, &roots[${operation.id}])) goto abrupt;`);
|
|
311
|
+
}
|
|
312
|
+
function emitWrite(state, operation) {
|
|
313
|
+
const bindingId = operation.bindingId;
|
|
314
|
+
if (bindingId == null) throw new Error(`MIR write %${operation.id} has no binding identity.`);
|
|
315
|
+
const value = operationArgument(operation, 0);
|
|
316
|
+
line(state, renderC(emittedC.common.rootAssignRoot, operation.id, value));
|
|
317
|
+
location(state, operation.range);
|
|
318
|
+
state.usesAbrupt = true;
|
|
319
|
+
if (operation.mutable === false) {
|
|
320
|
+
if (operation.functionNameBinding === true && !state.strict) {
|
|
321
|
+
line(state, renderC(emittedC.write.resultAssignOseoResultOseoStatusNormal, value));
|
|
322
|
+
return;
|
|
323
|
+
}
|
|
324
|
+
if (operation.importedBinding === true) {
|
|
325
|
+
line(state, renderC(emittedC.write.resultAssignOseoWriteImmutableBinding));
|
|
326
|
+
return;
|
|
327
|
+
}
|
|
328
|
+
line(state, renderC(emittedC.common.resultAssignOseoEnvironmentGetContext) + renderC(emittedC.common.rootsUStatement, state.environmentSlot, bindingId));
|
|
329
|
+
line(state, renderC(emittedC.common.statusNormalOpen));
|
|
330
|
+
line(state, renderC(emittedC.write.resultAssignOseoCellGetContextResultValue));
|
|
331
|
+
line(state, renderC(emittedC.common.closeBlock));
|
|
332
|
+
line(state, renderC(emittedC.common.statusNormalOpen));
|
|
333
|
+
line(state, renderC(emittedC.write.indentedWriteImmutableBinding));
|
|
334
|
+
line(state, renderC(emittedC.common.closeBlock));
|
|
335
|
+
return;
|
|
336
|
+
}
|
|
337
|
+
line(state, renderC(emittedC.common.resultAssignOseoEnvironmentGetContext) + renderC(emittedC.common.rootsUStatement, state.environmentSlot, bindingId));
|
|
338
|
+
line(state, renderC(emittedC.common.gotoAbruptUnlessNormal));
|
|
339
|
+
if (state.globalObjectBindingIds.has(bindingId)) {
|
|
340
|
+
line(state, renderC(emittedC.write.resultAssignOseoGlobalBindingSet, value, state.strict ? renderC(emittedC.common.trueValue) : renderC(emittedC.common.falseValue)));
|
|
341
|
+
return;
|
|
342
|
+
}
|
|
343
|
+
line(state, renderC(emittedC.write.resultAssignOseoCellSetContextResultValue, value));
|
|
344
|
+
}
|
|
345
|
+
function emitInitialize(state, operation) {
|
|
346
|
+
const bindingId = operation.bindingId;
|
|
347
|
+
if (bindingId == null) throw new Error(`MIR initialize %${operation.id} has no binding identity.`);
|
|
348
|
+
const value = operationArgument(operation, 0);
|
|
349
|
+
line(state, renderC(emittedC.common.rootAssignRoot, operation.id, value));
|
|
350
|
+
location(state, operation.range);
|
|
351
|
+
state.usesAbrupt = true;
|
|
352
|
+
line(state, renderC(emittedC.common.resultAssignOseoEnvironmentGetContext) + renderC(emittedC.common.rootsUStatement, state.environmentSlot, bindingId));
|
|
353
|
+
line(state, renderC(emittedC.common.gotoAbruptUnlessNormal));
|
|
354
|
+
if (state.globalObjectBindingIds.has(bindingId)) line(state, `result = ${state.globalFunctionBindingIds.has(bindingId) ? "oseo_global_function_initialize" : "oseo_global_binding_initialize"}(context, result.value, roots[${value}]);`);
|
|
355
|
+
else line(state, renderC(emittedC.initialize.resultAssignOseoCellInitializeContext, value));
|
|
356
|
+
line(state, renderC(emittedC.common.gotoAbruptUnlessNormal));
|
|
357
|
+
}
|
|
358
|
+
function emitBindingReset(state, operation) {
|
|
359
|
+
const bindingId = operation.bindingId;
|
|
360
|
+
if (bindingId == null) throw new Error(`MIR binding-reset %${operation.id} has no identity.`);
|
|
361
|
+
location(state, operation.range);
|
|
362
|
+
state.usesAbrupt = true;
|
|
363
|
+
line(state, renderC(emittedC.common.resultAssignOseoCellCreateContextOseo));
|
|
364
|
+
line(state, renderC(emittedC.common.rootAssignResultValue, operation.id));
|
|
365
|
+
line(state, renderC(emittedC.common.statusNormalOpen));
|
|
366
|
+
line(state, renderC(emittedC.bindingReset.resultAssignOseoEnvironmentSetContext) + renderC(emittedC.common.rootsU, state.environmentSlot, bindingId) + renderC(emittedC.common.rootCallSuffix, operation.id));
|
|
367
|
+
line(state, renderC(emittedC.common.closeBlock));
|
|
368
|
+
}
|
|
369
|
+
function emitUnary(state, operation) {
|
|
370
|
+
const argument = operationArgument(operation, 0);
|
|
371
|
+
if (operation.operator === "!") {
|
|
372
|
+
line(state, renderC(emittedC.common.rootsAssign, operation.id) + renderC(emittedC.unary.oseoBooleanOseoToBooleanRootsStatement, argument));
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
if (operation.operator === "void") {
|
|
376
|
+
line(state, renderC(emittedC.common.rootsAssignOseoUndefinedStatement, operation.id));
|
|
377
|
+
return;
|
|
378
|
+
}
|
|
379
|
+
const helpers = {
|
|
380
|
+
"+": renderC(emittedC.common.unaryToNumber),
|
|
381
|
+
"-": renderC(emittedC.common.unaryNegate),
|
|
382
|
+
"numeric-one": renderC(emittedC.common.unaryNumericOne),
|
|
383
|
+
"to-numeric": renderC(emittedC.common.unaryToNumeric),
|
|
384
|
+
"to-string": renderC(emittedC.common.unaryToString),
|
|
385
|
+
typeof: renderC(emittedC.common.unaryTypeof),
|
|
386
|
+
"~": renderC(emittedC.common.unaryBitwiseNot)
|
|
387
|
+
};
|
|
388
|
+
const operator = operation.operator;
|
|
389
|
+
if (operator == null || !(operator in helpers)) throw new Error(`MIR unary %${operation.id} has no valid operator.`);
|
|
390
|
+
const helper = helpers[operator];
|
|
391
|
+
location(state, operation.range);
|
|
392
|
+
state.usesAbrupt = true;
|
|
393
|
+
line(state, renderC(emittedC.unary.resultAssignContextRootsStatement, helper, argument));
|
|
394
|
+
line(state, renderC(emittedC.common.rootAssignResultValue, operation.id));
|
|
395
|
+
}
|
|
396
|
+
function emitBinary(state, operation) {
|
|
397
|
+
const operator = operation.operator;
|
|
398
|
+
if (operator == null || operator === "!" || operator === "numeric-one" || operator === "to-numeric" || operator === "to-string" || operator === "typeof" || operator === "void" || operator === "~" || (operator === "-" || operator === "+") && operation.arguments.length !== 2) throw new Error(`MIR binary %${operation.id} has no valid operator.`);
|
|
399
|
+
const left = operationArgument(operation, 0);
|
|
400
|
+
const right = operationArgument(operation, 1);
|
|
401
|
+
location(state, operation.range);
|
|
402
|
+
state.usesAbrupt = true;
|
|
403
|
+
line(state, renderC(emittedC.binary.resultAssign, operatorHelper(operator)) + renderC(emittedC.binary.contextRootsRootsStatement, left, right));
|
|
404
|
+
line(state, renderC(emittedC.common.rootAssignResultValue, operation.id));
|
|
405
|
+
}
|
|
406
|
+
function emitGuardSmi(state, operation) {
|
|
407
|
+
const argument = operationArgument(operation, 0);
|
|
408
|
+
state.scalarKinds.set(operation.id, "boolean");
|
|
409
|
+
line(state, renderC(emittedC.guardSmi.boolFastAssignOseoValueIsSmiRootsStatement, operation.id, argument));
|
|
410
|
+
}
|
|
411
|
+
function emitUnboxSmi(state, operation) {
|
|
412
|
+
const argument = operationArgument(operation, 0);
|
|
413
|
+
state.scalarKinds.set(operation.id, "smi");
|
|
414
|
+
line(state, renderC(emittedC.unboxSmi.int64TFastAssign, operation.id) + renderC(emittedC.unboxSmi.oseoValueUnboxSmiRootsStatement, argument));
|
|
415
|
+
}
|
|
416
|
+
function emitCheckedAdd(state, operation) {
|
|
417
|
+
const left = operationArgument(operation, 0);
|
|
418
|
+
const right = operationArgument(operation, 1);
|
|
419
|
+
const result = operation.checkedResult;
|
|
420
|
+
if (result == null || state.scalarKinds.get(left) !== "smi" || state.scalarKinds.get(right) !== "smi") throw new Error(`MIR checked add %${operation.id} has invalid inputs.`);
|
|
421
|
+
state.scalarKinds.set(operation.id, "boolean");
|
|
422
|
+
state.scalarKinds.set(result, "smi");
|
|
423
|
+
line(state, renderC(emittedC.checkedAdd.int64TFastStatement, result));
|
|
424
|
+
line(state, renderC(emittedC.checkedAdd.boolFastAssignOseoSmiTryAdd, operation.id) + renderC(emittedC.checkedAdd.fastFastAddressFastStatement, left, right, result));
|
|
425
|
+
}
|
|
426
|
+
function emitBoxSmi(state, operation) {
|
|
427
|
+
const argument = operationArgument(operation, 0);
|
|
428
|
+
if (state.scalarKinds.get(argument) !== "smi") throw new Error(`MIR box %${operation.id} has no small integer input.`);
|
|
429
|
+
line(state, renderC(emittedC.boxSmi.rootsAssignOseoValueBoxSmiFastStatement, operation.id, argument));
|
|
430
|
+
}
|
|
431
|
+
/**
|
|
432
|
+
* The C expression reading one iterator's done flag.
|
|
433
|
+
*
|
|
434
|
+
* A generator body may suspend while an iterator operation is still in
|
|
435
|
+
* progress, and every resumption runs in a fresh C invocation, so an
|
|
436
|
+
* automatic local would read indeterminate state. Such a body keeps the
|
|
437
|
+
* flag in the root slot the MIR reserved for it instead.
|
|
438
|
+
*/
|
|
439
|
+
function iteratorDoneRead(state, doneState) {
|
|
440
|
+
return state.generator ? renderC(emittedC.common.oseoToBooleanRoots, doneState) : renderC(emittedC.common.iteratorDone, doneState);
|
|
441
|
+
}
|
|
442
|
+
/**
|
|
443
|
+
* Emit one for-in enumeration operation.
|
|
444
|
+
*
|
|
445
|
+
* `EnumerateObjectProperties` is not the iterator protocol: neither
|
|
446
|
+
* entry point runs user code, reads a `next` method, or is ever closed,
|
|
447
|
+
* so both report exhaustion through a local flag and leave the produced
|
|
448
|
+
* record or key in a root slot that survives collection and suspension.
|
|
449
|
+
*/
|
|
450
|
+
function emitEnumerateOperation(state, operation) {
|
|
451
|
+
const get = operation.kind === "enumerate-get";
|
|
452
|
+
const slot = get ? operation.enumerateRecordResult : operation.enumerateKeyResult;
|
|
453
|
+
if (slot == null) throw new Error(`MIR ${operation.kind} %${operation.id} has no result.`);
|
|
454
|
+
state.scalarKinds.set(operation.id, "boolean");
|
|
455
|
+
line(state, renderC(emittedC.enumerateOperation.boolEnumerateDoneAssignTrueStatement, operation.id));
|
|
456
|
+
line(state, renderC(get ? emittedC.enumerateOperation.resultAssignOseoEnumerateGetContext : emittedC.enumerateOperation.resultAssignOseoEnumerateNextContext, operationArgument(operation, 0), slot, operation.id));
|
|
457
|
+
line(state, renderC(emittedC.enumerateOperation.boolFastAssignEnumerateDoneStatement, operation.id, operation.id));
|
|
458
|
+
line(state, renderC(emittedC.common.voidFastStatement, operation.id));
|
|
459
|
+
}
|
|
460
|
+
function emitIteratorOperation(state, operation) {
|
|
461
|
+
location(state, operation.range);
|
|
462
|
+
state.usesAbrupt = true;
|
|
463
|
+
const asynchronous = operation.iteratorAsync === true ? renderC(emittedC.iteratorOperation.asyncPrefix) : renderC(emittedC.common.empty);
|
|
464
|
+
if (operation.kind === "iterator-close-start") {
|
|
465
|
+
const promise = operation.iteratorValueResult;
|
|
466
|
+
const ignoreResult = operation.iteratorValueOnlyResult;
|
|
467
|
+
const skipValidation = operation.iteratorCloseResultMode;
|
|
468
|
+
const slot = operation.completionSlot;
|
|
469
|
+
if (promise == null || ignoreResult == null || skipValidation == null || slot == null) throw new Error(`MIR iterator close start %${operation.id} is incomplete.`);
|
|
470
|
+
state.usesCompletion = true;
|
|
471
|
+
state.scalarKinds.set(operation.id, "boolean");
|
|
472
|
+
line(state, renderC(emittedC.iteratorOperation.rootsAssignCompletionKindThrowStatement, ignoreResult, slot));
|
|
473
|
+
line(state, renderC(emittedC.iteratorOperation.boolIteratorCloseAwaitAssignFalseStatement, operation.id));
|
|
474
|
+
line(state, renderC(emittedC.iteratorOperation.resultAssignOseoAsyncIteratorCloseStartContext, operationArgument(operation, 0), slot, skipValidation, operation.id));
|
|
475
|
+
line(state, renderC(emittedC.common.rootAssignResultValue, promise));
|
|
476
|
+
line(state, renderC(emittedC.iteratorOperation.boolFastAssignIteratorCloseAwaitStatement, operation.id, operation.id));
|
|
477
|
+
line(state, renderC(emittedC.common.voidFastStatement, operation.id));
|
|
478
|
+
return;
|
|
479
|
+
}
|
|
480
|
+
if (operation.kind === "iterator-close-result") {
|
|
481
|
+
line(state, renderC(emittedC.iteratorOperation.resultAssignOseoAsyncIteratorCloseResultContext, operationArgument(operation, 0), operationArgument(operation, 1), operationArgument(operation, 2)));
|
|
482
|
+
line(state, renderC(emittedC.common.rootAssignResultValue, operation.id));
|
|
483
|
+
return;
|
|
484
|
+
}
|
|
485
|
+
if (operation.kind === "iterator-await-start") {
|
|
486
|
+
const stepKind = operation.iteratorStepKind;
|
|
487
|
+
const valueOnly = operation.iteratorValueOnlyResult;
|
|
488
|
+
if (stepKind == null || valueOnly == null) throw new Error(`MIR iterator await start %${operation.id} is incomplete.`);
|
|
489
|
+
line(state, renderC(emittedC.iteratorOperation.rootsAssignOseoBooleanFalseStatement, valueOnly));
|
|
490
|
+
if (stepKind === "next") line(state, renderC(emittedC.iteratorOperation.resultAssignOseoAsyncIteratorNextStartContext, operationArgument(operation, 0), operationArgument(operation, 1)));
|
|
491
|
+
else {
|
|
492
|
+
const step = stepKind === "delegate-next" ? renderC(emittedC.common.iteratorDelegateNext) : stepKind === "delegate-return" ? renderC(emittedC.common.iteratorDelegateReturn) : renderC(emittedC.common.iteratorDelegateThrow);
|
|
493
|
+
const iterator = operationArgument(operation, 0);
|
|
494
|
+
const trailing = stepKind === "delegate-next" ? renderC(emittedC.iteratorOperation.rootsRootsAddressRootsStatementSuffix, operationArgument(operation, 1), operationArgument(operation, 2), valueOnly) : renderC(emittedC.iteratorOperation.rootsAddressRootsStatementSuffix, operationArgument(operation, 1), valueOnly);
|
|
495
|
+
line(state, renderC(emittedC.iteratorOperation.resultAssignOseoAsyncIteratorDelegateStartContext, step, iterator) + trailing);
|
|
496
|
+
}
|
|
497
|
+
line(state, renderC(emittedC.common.rootAssignResultValue, operation.id));
|
|
498
|
+
return;
|
|
499
|
+
}
|
|
500
|
+
if (operation.kind === "iterator-await-result") {
|
|
501
|
+
const value = operation.iteratorValueResult;
|
|
502
|
+
if (value == null) throw new Error(`MIR iterator await result %${operation.id} has no value.`);
|
|
503
|
+
state.scalarKinds.set(operation.id, "boolean");
|
|
504
|
+
line(state, renderC(emittedC.iteratorOperation.boolIteratorDoneAssignTrueStatement, operation.id));
|
|
505
|
+
line(state, renderC(emittedC.iteratorOperation.resultAssignOseoAsyncIteratorResultContext, operationArgument(operation, 0), operationArgument(operation, 1), operation.iteratorValueWhenDone === true, operationArgument(operation, 2), operation.iteratorStepKind === "delegate-throw", value, operation.id));
|
|
506
|
+
line(state, renderC(emittedC.iteratorOperation.boolFastAssignIteratorDoneStatement, operation.id, operation.id));
|
|
507
|
+
line(state, renderC(emittedC.common.voidFastStatement, operation.id));
|
|
508
|
+
return;
|
|
509
|
+
}
|
|
510
|
+
if (operation.kind === "iterator-get") {
|
|
511
|
+
const iterable = operationArgument(operation, 0);
|
|
512
|
+
const nextMethod = operation.iteratorNextMethodResult;
|
|
513
|
+
if (nextMethod == null) throw new Error(`MIR iterator get %${operation.id} has no next method.`);
|
|
514
|
+
line(state, renderC(emittedC.iteratorOperation.resultAssignOseoIteratorGetContext, asynchronous) + renderC(emittedC.iteratorOperation.rootsAddressRootsStatement, iterable, nextMethod));
|
|
515
|
+
if (operation.iteratorDoneState != null) {
|
|
516
|
+
const doneState = operation.iteratorDoneState;
|
|
517
|
+
line(state, state.generator ? renderC(emittedC.iteratorOperation.rootsAssignOseoBooleanFalseStatement, doneState) : renderC(emittedC.iteratorOperation.boolIteratorDoneAssignFalseStatement, doneState));
|
|
518
|
+
}
|
|
519
|
+
line(state, renderC(emittedC.common.rootAssignResultValue, operation.id));
|
|
520
|
+
return;
|
|
521
|
+
}
|
|
522
|
+
if (operation.kind === "iterator-delegate-next" || operation.kind === "iterator-delegate-return" || operation.kind === "iterator-delegate-throw") {
|
|
523
|
+
const delegatingNext = operation.kind === "iterator-delegate-next";
|
|
524
|
+
const step = delegatingNext ? renderC(emittedC.common.iteratorDelegateNext) : operation.kind === "iterator-delegate-return" ? renderC(emittedC.common.iteratorDelegateReturn) : renderC(emittedC.common.iteratorDelegateThrow);
|
|
525
|
+
const value = operation.iteratorValueResult;
|
|
526
|
+
if (value == null) throw new Error(`MIR iterator delegation %${operation.id} has no value.`);
|
|
527
|
+
state.scalarKinds.set(operation.id, "boolean");
|
|
528
|
+
const done = renderC(emittedC.common.iteratorDone, operation.id);
|
|
529
|
+
const inner = delegatingNext ? renderC(emittedC.common.rootWithComma, operationArgument(operation, 1)) + renderC(emittedC.common.root, operationArgument(operation, 2)) : renderC(emittedC.common.root, operationArgument(operation, 1));
|
|
530
|
+
line(state, renderC(emittedC.iteratorOperation.boolAssignTrueStatement, done));
|
|
531
|
+
line(state, renderC(emittedC.iteratorOperation.resultAssignOseoIteratorDelegateContext, asynchronous, step) + renderC(emittedC.common.twoRootsWithComma, operationArgument(operation, 0), inner) + renderC(emittedC.iteratorOperation.addressRootsAddressStatement, value, done));
|
|
532
|
+
line(state, renderC(emittedC.common.boolFastAssignStatement, operation.id, done));
|
|
533
|
+
line(state, renderC(emittedC.common.voidFastStatement, operation.id));
|
|
534
|
+
return;
|
|
535
|
+
}
|
|
536
|
+
if (operation.kind === "iterator-next") {
|
|
537
|
+
const iterator = operationArgument(operation, 0);
|
|
538
|
+
const nextMethod = operationArgument(operation, 1);
|
|
539
|
+
const value = operation.iteratorValueResult;
|
|
540
|
+
if (value == null) throw new Error(`MIR iterator next %${operation.id} has no value.`);
|
|
541
|
+
state.scalarKinds.set(operation.id, "boolean");
|
|
542
|
+
const doneState = operation.iteratorDoneState;
|
|
543
|
+
if (doneState == null) {
|
|
544
|
+
line(state, renderC(emittedC.iteratorOperation.boolIteratorDoneAssignTrueStatement, operation.id));
|
|
545
|
+
line(state, renderC(emittedC.iteratorOperation.resultAssignOseoIteratorNextContext, asynchronous) + renderC(emittedC.common.rootsRootsAddressRoots, iterator, nextMethod, value) + renderC(emittedC.iteratorOperation.addressIteratorDoneStatement, operation.id));
|
|
546
|
+
line(state, renderC(emittedC.iteratorOperation.boolFastAssignIteratorDoneStatement, operation.id, operation.id));
|
|
547
|
+
line(state, renderC(emittedC.common.voidFastStatement, operation.id));
|
|
548
|
+
return;
|
|
549
|
+
}
|
|
550
|
+
const step = state.generator ? renderC(emittedC.iteratorOperation.iteratorStepDone, operation.id) : renderC(emittedC.common.iteratorDone, doneState);
|
|
551
|
+
if (state.generator) line(state, renderC(emittedC.iteratorOperation.boolAssignStatement, step, iteratorDoneRead(state, doneState)));
|
|
552
|
+
line(state, renderC(emittedC.common.ifOpen, step));
|
|
553
|
+
line(state, renderC(emittedC.common.resultAssignOseoResultOseoStatusNormalOseo));
|
|
554
|
+
line(state, renderC(emittedC.iteratorOperation.rootsAssignOseoUndefinedStatement, value));
|
|
555
|
+
line(state, renderC(emittedC.common.elseOpen));
|
|
556
|
+
line(state, renderC(emittedC.iteratorOperation.indentedIteratorNext, asynchronous) + renderC(emittedC.common.rootsRootsAddressRoots, iterator, nextMethod, value) + renderC(emittedC.common.addressStatement, step));
|
|
557
|
+
line(state, renderC(emittedC.common.closeBlock));
|
|
558
|
+
if (state.generator) line(state, renderC(emittedC.common.rootsAssignOseoBooleanStatement, doneState, step));
|
|
559
|
+
line(state, renderC(emittedC.common.boolFastAssignStatement, operation.id, step));
|
|
560
|
+
line(state, renderC(emittedC.common.voidFastStatement, operation.id));
|
|
561
|
+
return;
|
|
562
|
+
}
|
|
563
|
+
const iterator = operationArgument(operation, 0);
|
|
564
|
+
const slot = operation.completionSlot;
|
|
565
|
+
if (slot == null) throw new Error(`MIR iterator close %${operation.id} has no completion.`);
|
|
566
|
+
state.usesCompletion = true;
|
|
567
|
+
if (operation.iteratorDoneState == null) line(state, renderC(emittedC.iteratorOperation.resultAssignOseoIteratorCloseContext, asynchronous) + renderC(emittedC.common.rootsCompletionUKindEqualStatement, iterator, slot));
|
|
568
|
+
else {
|
|
569
|
+
const done = iteratorDoneRead(state, operation.iteratorDoneState);
|
|
570
|
+
line(state, renderC(emittedC.common.ifOpen, done));
|
|
571
|
+
line(state, renderC(emittedC.common.resultAssignOseoResultOseoStatusNormalOseo));
|
|
572
|
+
line(state, renderC(emittedC.common.elseOpen));
|
|
573
|
+
line(state, renderC(emittedC.iteratorOperation.indentedIteratorClose, asynchronous) + renderC(emittedC.common.rootsCompletionUKindEqualStatement, iterator, slot));
|
|
574
|
+
line(state, renderC(emittedC.common.closeBlock));
|
|
575
|
+
}
|
|
576
|
+
line(state, renderC(emittedC.common.rootAssignResultValue, operation.id));
|
|
577
|
+
}
|
|
578
|
+
function emitDisposalOperation(state, operation) {
|
|
579
|
+
location(state, operation.range);
|
|
580
|
+
state.usesAbrupt = true;
|
|
581
|
+
const flag = (value) => renderC(value ? emittedC.common.trueValue : emittedC.common.falseValue);
|
|
582
|
+
const scope = () => operationArgument(operation, 0);
|
|
583
|
+
if (operation.kind === "dispose-scope-create") line(state, renderC(emittedC.disposalOperation.resultAssignScopeCreate, flag(operation.disposalAsync === true)));
|
|
584
|
+
else if (operation.kind === "dispose-scope-add") line(state, renderC(emittedC.disposalOperation.resultAssignScopeAdd, scope(), operationArgument(operation, 1), flag(operation.disposalAsync === true)));
|
|
585
|
+
else if (operation.kind === "dispose-scope-dispose" || operation.kind === "dispose-scope-begin") {
|
|
586
|
+
const slot = operation.completionSlot;
|
|
587
|
+
if (slot == null) throw new Error(`MIR ${operation.kind} %${operation.id} has no slot.`);
|
|
588
|
+
state.usesCompletion = true;
|
|
589
|
+
line(state, renderC(emittedC.disposalOperation.resultAssignScopeCompletion, operation.kind === "dispose-scope-dispose" ? "dispose" : "begin", scope(), slot, state.completionSlotStart + slot));
|
|
590
|
+
} else if (operation.kind === "dispose-scope-continue") line(state, renderC(emittedC.disposalOperation.resultAssignScopeContinue, scope(), flag(operation.disposalRejected !== true), operationArgument(operation, 1)));
|
|
591
|
+
else {
|
|
592
|
+
const names = {
|
|
593
|
+
"dispose-scope-await-value": "await_value",
|
|
594
|
+
"dispose-scope-awaiting": "awaiting",
|
|
595
|
+
"dispose-scope-result": "result"
|
|
596
|
+
};
|
|
597
|
+
if (operation.kind !== "dispose-scope-await-value" && operation.kind !== "dispose-scope-awaiting" && operation.kind !== "dispose-scope-result") throw new Error(`MIR ${operation.kind} is not a using scope call.`);
|
|
598
|
+
line(state, renderC(emittedC.disposalOperation.resultAssignScopeQuery, names[operation.kind], scope()));
|
|
599
|
+
}
|
|
600
|
+
line(state, renderC(emittedC.common.rootAssignResultValue, operation.id));
|
|
601
|
+
}
|
|
602
|
+
function emitCall(state, operation) {
|
|
603
|
+
const target = operation.target;
|
|
604
|
+
if (target == null) throw new Error(`MIR call %${operation.id} has no target.`);
|
|
605
|
+
const dynamic = target.kind === "dynamic";
|
|
606
|
+
const constructing = operation.kind === "construct";
|
|
607
|
+
const callArguments = dynamic ? {
|
|
608
|
+
...operation,
|
|
609
|
+
arguments: operation.arguments.slice(2)
|
|
610
|
+
} : target.kind === "super" ? {
|
|
611
|
+
...operation,
|
|
612
|
+
arguments: operation.arguments.slice(1)
|
|
613
|
+
} : operation;
|
|
614
|
+
location(state, operation.range);
|
|
615
|
+
state.usesAbrupt = true;
|
|
616
|
+
const argumentsValue = emitArguments(state, callArguments);
|
|
617
|
+
if (operation.tail === true) emitTailCall(state, operation, argumentsValue);
|
|
618
|
+
else if (target.kind === "console-log") line(state, renderC(emittedC.call.resultAssignOseoConsoleLogContext, argumentsValue.count) + renderC(emittedC.common.callSuffix, argumentsValue.name));
|
|
619
|
+
else if (target.kind === "builtin-export") line(state, `result = oseo_builtin_export(context, ${argumentsValue.count}, ${argumentsValue.name});`);
|
|
620
|
+
else if (target.kind === "module-evaluate") line(state, renderC(emittedC.call.resultAssignOseoModuleGraphEvaluateContext, argumentsValue.count) + renderC(emittedC.common.callSuffix, argumentsValue.name));
|
|
621
|
+
else if (target.kind === "module-import") line(state, renderC(emittedC.call.resultAssignOseoModuleImportContext, argumentsValue.count) + renderC(emittedC.common.callSuffix, argumentsValue.name));
|
|
622
|
+
else if (target.kind === "promise-intrinsic") if (target.method === "asyncCall") {
|
|
623
|
+
const execution = emittedArgument(callArguments, argumentsValue, 0);
|
|
624
|
+
line(state, renderC(emittedC.call.resultAssignOseoPromiseAsyncCallContext, execution));
|
|
625
|
+
} else if (target.method === "awaitThen") {
|
|
626
|
+
const promise = emittedArgument(callArguments, argumentsValue, 0);
|
|
627
|
+
const onFulfilled = emittedArgument(callArguments, argumentsValue, 1);
|
|
628
|
+
line(state, renderC(emittedC.call.resultAssignOseoPromiseAwaitThenContext) + renderC(emittedC.common.twoValuesCallSuffix, promise, onFulfilled));
|
|
629
|
+
} else if (target.method === "then") {
|
|
630
|
+
const promise = emittedArgument(callArguments, argumentsValue, 0);
|
|
631
|
+
const onFulfilled = emittedArgument(callArguments, argumentsValue, 1);
|
|
632
|
+
const onRejected = emittedArgument(callArguments, argumentsValue, 2);
|
|
633
|
+
line(state, renderC(emittedC.call.resultAssignOseoPromiseThenContext) + renderC(emittedC.call.threeValuesCallSuffix, promise, onFulfilled, onRejected));
|
|
634
|
+
} else {
|
|
635
|
+
const names = {
|
|
636
|
+
all: renderC(emittedC.call.oseoPromiseAll),
|
|
637
|
+
resolve: renderC(emittedC.call.oseoPromiseResolve)
|
|
638
|
+
};
|
|
639
|
+
const value = emittedArgument(callArguments, argumentsValue, 0);
|
|
640
|
+
line(state, renderC(emittedC.call.resultAssignContextStatement, names[target.method], value));
|
|
641
|
+
}
|
|
642
|
+
else if (target.kind === "timer-intrinsic") if (target.method === "setTimeout") line(state, renderC(emittedC.call.resultAssignOseoSetTimeoutContext, argumentsValue.count) + renderC(emittedC.common.callSuffix, argumentsValue.name));
|
|
643
|
+
else {
|
|
644
|
+
const handle = emittedArgument(callArguments, argumentsValue, 0);
|
|
645
|
+
line(state, renderC(emittedC.call.resultAssignOseoClearTimeoutContext, handle));
|
|
646
|
+
}
|
|
647
|
+
else if (target.kind === "dynamic") {
|
|
648
|
+
const callee = operationArgument(operation, 0);
|
|
649
|
+
const receiver = operationArgument(operation, 1);
|
|
650
|
+
line(state, renderC(emittedC.call.callFunctionWithDynamicReceiverPrefix, callee) + renderC(emittedC.common.twoRootsWithComma, receiver, argumentsValue.count) + renderC(emittedC.common.valueWithComma, argumentsValue.name) + (constructing ? renderC(emittedC.common.rootCallSuffix, callee) : renderC(emittedC.common.undefinedFinalArgumentCallSuffix)));
|
|
651
|
+
if (constructing) {
|
|
652
|
+
line(state, renderC(emittedC.common.statusNormalOpen));
|
|
653
|
+
line(state, renderC(emittedC.call.resultAssignOseoConstructorResult) + renderC(emittedC.call.contextResultValueRootsStatement, receiver));
|
|
654
|
+
line(state, renderC(emittedC.common.closeBlock));
|
|
655
|
+
}
|
|
656
|
+
} else if (target.kind === "super") {
|
|
657
|
+
const parent = operationArgument(operation, 0);
|
|
658
|
+
const newTarget = renderC(emittedC.functionCreate.newTarget);
|
|
659
|
+
line(state, renderC(emittedC.superConstructor.resultAssignOseoSuperConstructorCheckContextRoots, parent));
|
|
660
|
+
line(state, renderC(emittedC.common.statusNormalOpen));
|
|
661
|
+
line(state, renderC(emittedC.superConstructor.resultAssignOseoSuperConstructorReceiverContextRoots, parent, newTarget));
|
|
662
|
+
line(state, renderC(emittedC.common.rootsAssignResultValueIfStatusNormal, operation.id));
|
|
663
|
+
line(state, renderC(emittedC.common.statusNormalOpen));
|
|
664
|
+
line(state, renderC(emittedC.call.callFunctionWithRootReceiverPrefix, parent, operation.id) + renderC(emittedC.call.newTargetStatement, argumentsValue.count, argumentsValue.name));
|
|
665
|
+
line(state, renderC(emittedC.common.statusNormalOpen));
|
|
666
|
+
line(state, renderC(emittedC.call.resultAssignOseoConstructorResultContextValueRoot, operation.id));
|
|
667
|
+
line(state, renderC(emittedC.common.closeBlock));
|
|
668
|
+
line(state, renderC(emittedC.common.closeBlock));
|
|
669
|
+
line(state, renderC(emittedC.common.closeBlock));
|
|
670
|
+
} else {
|
|
671
|
+
const targetRootCount = state.functionFrameCosts.get(target.functionId);
|
|
672
|
+
if (targetRootCount == null) throw new Error(`MIR call %${operation.id} targets unknown function '${target.functionId}'.`);
|
|
673
|
+
let functionName = renderC(emittedC.call.oseoFunction, target.functionId);
|
|
674
|
+
if (target.functionId === state.functionId) {
|
|
675
|
+
functionName = renderC(emittedC.call.recursiveTarget, state.nextRecursiveTarget);
|
|
676
|
+
state.nextRecursiveTarget += 1;
|
|
677
|
+
line(state, renderC(emittedC.call.oseoFunctionEntryVolatileAssign, functionName) + renderC(emittedC.call.oseoFunctionStatement, target.functionId));
|
|
678
|
+
}
|
|
679
|
+
line(state, renderC(emittedC.call.resultAssignOseoCallEnterContextStatement));
|
|
680
|
+
line(state, renderC(emittedC.common.statusNormalOpen));
|
|
681
|
+
line(state, renderC(emittedC.call.resultAssignOseoFrameEnterContextU, targetRootCount));
|
|
682
|
+
line(state, renderC(emittedC.call.ifResultStatusEqualOseoStatusNormalOpen));
|
|
683
|
+
line(state, renderC(emittedC.call.resultAssign, functionName) + renderC(emittedC.call.contextOseoUndefinedOseoUndefined) + renderC(emittedC.common.twoValuesWithComma, argumentsValue.count, argumentsValue.name) + renderC(emittedC.common.undefinedFinalArgumentCallSuffix));
|
|
684
|
+
line(state, renderC(emittedC.call.oseoFrameLeaveContextUStatement, targetRootCount));
|
|
685
|
+
line(state, renderC(emittedC.common.indentedCloseBlock));
|
|
686
|
+
line(state, renderC(emittedC.call.oseoCallLeaveContextStatement));
|
|
687
|
+
line(state, renderC(emittedC.call.callCompleteStatement));
|
|
688
|
+
line(state, renderC(emittedC.common.closeBlock));
|
|
689
|
+
}
|
|
690
|
+
line(state, renderC(emittedC.common.rootAssignResultValue, operation.id));
|
|
691
|
+
}
|
|
692
|
+
/**
|
|
693
|
+
* Emits a call MIR marks as being in tail position. The runtime records
|
|
694
|
+
* it and returns OSEO_STATUS_TAIL_CALL, so the following status check
|
|
695
|
+
* leaves through the body's ordinary abrupt exit, which releases the
|
|
696
|
+
* frame's roots and returns that result to the trampoline. A tail call
|
|
697
|
+
* never has a handler in its own body, so that exit is the body's
|
|
698
|
+
* return. A derived class constructor passes its `this` binding cell
|
|
699
|
+
* along for the result rules the frame can no longer apply.
|
|
700
|
+
*/
|
|
701
|
+
function emitTailCall(state, operation, argumentsValue) {
|
|
702
|
+
const target = operation.target;
|
|
703
|
+
const thisBindingId = operation.tailThisBindingId;
|
|
704
|
+
if (thisBindingId != null) {
|
|
705
|
+
line(state, renderC(emittedC.common.resultAssignOseoEnvironmentGetContext) + renderC(emittedC.common.rootsUStatement, state.environmentSlot, thisBindingId));
|
|
706
|
+
line(state, renderC(emittedC.common.gotoAbruptUnlessNormal));
|
|
707
|
+
}
|
|
708
|
+
const derived = thisBindingId != null;
|
|
709
|
+
if (target?.kind === "dynamic") {
|
|
710
|
+
const callee = operationArgument(operation, 0);
|
|
711
|
+
const receiver = operationArgument(operation, 1);
|
|
712
|
+
line(state, renderC(derived ? emittedC.call.tailCallDerivedPrefix : emittedC.call.tailCallPrefix, callee) + renderC(emittedC.common.twoRootsWithComma, receiver, argumentsValue.count) + (derived ? renderC(emittedC.common.valueWithComma, argumentsValue.name) + renderC(emittedC.derivedReturn.resultValueStatement) : renderC(emittedC.common.callSuffix, argumentsValue.name)));
|
|
713
|
+
return;
|
|
714
|
+
}
|
|
715
|
+
let host;
|
|
716
|
+
if (target?.kind === "console-log") host = renderC(emittedC.call.tailHostConsoleLog);
|
|
717
|
+
else if (target?.kind === "timer-intrinsic") host = renderC(target.method === "setTimeout" ? emittedC.call.tailHostSetTimeout : emittedC.call.tailHostClearTimeout);
|
|
718
|
+
else throw new Error(`MIR tail call %${operation.id} has a target with no tail call ABI.`);
|
|
719
|
+
line(state, renderC(derived ? emittedC.call.tailHostCallDerivedPrefix : emittedC.call.tailHostCallPrefix, host, argumentsValue.count) + (derived ? renderC(emittedC.common.valueWithComma, argumentsValue.name) + renderC(emittedC.derivedReturn.resultValueStatement) : renderC(emittedC.common.callSuffix, argumentsValue.name)));
|
|
720
|
+
}
|
|
721
|
+
function emitObjectOperation(state, operation) {
|
|
722
|
+
if (state.fragmentUnit == null && (operation.kind === "property-key" || operation.kind === "property-get" && operation.superReference !== true)) {
|
|
723
|
+
state.usesAbrupt = true;
|
|
724
|
+
const name = generatedOperation(state, operation, operationArgument(operation, 0), operation.kind === "property-get" ? operationArgument(operation, 1) : 0);
|
|
725
|
+
line(state, `oseo_generated_property_${operation.kind === "property-key" ? "key" : "get"}_at(context, roots, &${name}, &result);`);
|
|
726
|
+
return;
|
|
727
|
+
}
|
|
728
|
+
location(state, operation.range);
|
|
729
|
+
state.usesAbrupt = true;
|
|
730
|
+
if (operation.kind === "array-create") {
|
|
731
|
+
if (operation.arrayLength == null) throw new Error(`MIR array-create %${operation.id} has no length.`);
|
|
732
|
+
line(state, renderC(emittedC.objectOperation.resultAssignOseoArrayCreateContextU, operation.arrayLength));
|
|
733
|
+
} else if (operation.kind === "array-append") {
|
|
734
|
+
const array = operationArgument(operation, 0);
|
|
735
|
+
const value = operationArgument(operation, 1);
|
|
736
|
+
line(state, renderC(emittedC.objectOperation.resultAssignOseoArrayAppendContextRoots, array) + renderC(emittedC.common.rootCallSuffix, value));
|
|
737
|
+
} else if (operation.kind === "array-append-hole") {
|
|
738
|
+
const array = operationArgument(operation, 0);
|
|
739
|
+
line(state, renderC(emittedC.objectOperation.resultAssignOseoArrayAppendHoleContext, array));
|
|
740
|
+
} else if (operation.kind === "object-create") line(state, renderC(emittedC.objectOperation.resultAssignOseoObjectLiteralCreateContext));
|
|
741
|
+
else if (operation.kind === "to-object" || operation.kind === "object-coercible" || operation.kind === "delete-object-coercible") {
|
|
742
|
+
const input = operationArgument(operation, 0);
|
|
743
|
+
line(state, renderC(operation.kind === "to-object" ? emittedC.objectOperation.resultAssignOseoToObject : operation.kind === "delete-object-coercible" ? emittedC.objectOperation.resultAssignOseoRequireDeleteObjectCoercible : emittedC.objectOperation.resultAssignOseoRequireObjectCoercible, input));
|
|
744
|
+
} else if (operation.kind === "object-rest") {
|
|
745
|
+
line(state, "{");
|
|
746
|
+
const object = operationArgument(operation, 0);
|
|
747
|
+
const excluded = operation.arguments.slice(1);
|
|
748
|
+
const excludedName = renderC(emittedC.objectOperation.objectRestExcluded, operation.id);
|
|
749
|
+
if (excluded.length > 0) line(state, renderC(emittedC.objectOperation.oseoValueUAssignOpen, excludedName, excluded.length) + excluded.map((id) => renderC(emittedC.common.root, id)).join(renderC(emittedC.common.commaSpace)) + renderC(emittedC.objectOperation.initializerSuffix));
|
|
750
|
+
line(state, renderC(emittedC.objectOperation.resultAssignOseoObjectRestContextRoots, object) + renderC(emittedC.common.unsignedWithComma, excluded.length) + (excluded.length === 0 ? renderC(emittedC.objectOperation.nullStatement) : renderC(emittedC.common.callSuffix, excludedName)));
|
|
751
|
+
line(state, "}");
|
|
752
|
+
} else if (operation.kind === "object-spread") {
|
|
753
|
+
const object = operationArgument(operation, 0);
|
|
754
|
+
const source = operationArgument(operation, 1);
|
|
755
|
+
line(state, renderC(emittedC.objectOperation.resultAssignOseoObjectSpreadContextRoots, object) + renderC(emittedC.common.rootCallSuffix, source));
|
|
756
|
+
} else if (operation.kind === "object-set-prototype") {
|
|
757
|
+
const object = operationArgument(operation, 0);
|
|
758
|
+
const prototypeValue = operationArgument(operation, 1);
|
|
759
|
+
line(state, renderC(emittedC.objectOperation.resultAssignOseoObjectLiteralSetPrototypeContextRoots, object) + renderC(emittedC.common.rootCallSuffix, prototypeValue));
|
|
760
|
+
} else if (operation.kind === "property-key") {
|
|
761
|
+
line(state, `oseo_generated_property_key(context, roots[${operationArgument(operation, 0)}], &result, &roots[${operation.id}]);`);
|
|
762
|
+
return;
|
|
763
|
+
} else if (operation.kind === "property-define-data") {
|
|
764
|
+
const object = operationArgument(operation, 0);
|
|
765
|
+
const key = operationArgument(operation, 1);
|
|
766
|
+
const value = operationArgument(operation, 2);
|
|
767
|
+
line(state, renderC(emittedC.common.resultAssignOseoObjectDefineContextRoots, object) + renderC(emittedC.common.rootsRoots, key, value) + renderC(emittedC.objectOperation.oseoPropertyAttributesTrueTrueTrueFalse));
|
|
768
|
+
} else if (operation.kind === "property-define-method") {
|
|
769
|
+
const object = operationArgument(operation, 0);
|
|
770
|
+
const key = operationArgument(operation, 1);
|
|
771
|
+
const value = operationArgument(operation, 2);
|
|
772
|
+
line(state, renderC(emittedC.common.resultAssignOseoObjectDefineContextRoots, object) + renderC(emittedC.common.rootsRoots, key, value) + renderC(emittedC.objectOperation.oseoPropertyAttributesTrueFalseTrueFalse));
|
|
773
|
+
} else if (operation.kind === "property-define-accessor") {
|
|
774
|
+
const object = operationArgument(operation, 0);
|
|
775
|
+
const key = operationArgument(operation, 1);
|
|
776
|
+
const value = operationArgument(operation, 2);
|
|
777
|
+
const isSetter = operation.accessorKind === "set";
|
|
778
|
+
const enumerable = operation.enumerable !== false;
|
|
779
|
+
line(state, renderC(emittedC.objectOperation.resultAssignOseoObjectDefineAccessor, object) + renderC(emittedC.common.rootWithComma, key) + renderC(emittedC.common.valueWithComma, isSetter ? renderC(emittedC.common.undefinedValue) : renderC(emittedC.common.root, value)) + renderC(emittedC.common.valueWithComma, isSetter ? renderC(emittedC.common.root, value) : renderC(emittedC.common.undefinedValue)) + renderC(emittedC.common.twoValuesWithComma, isSetter ? renderC(emittedC.common.falseValue) : renderC(emittedC.common.trueValue), isSetter ? renderC(emittedC.common.trueValue) : renderC(emittedC.common.falseValue)) + renderC(emittedC.objectOperation.oseoPropertyAttributesTrueFalseTrue, enumerable));
|
|
780
|
+
} else {
|
|
781
|
+
const object = operationArgument(operation, 0);
|
|
782
|
+
const key = operationArgument(operation, 1);
|
|
783
|
+
const strict = state.strict || operation.strict === true;
|
|
784
|
+
const superReference = operation.superReference === true;
|
|
785
|
+
if (operation.kind === "property-get") if (superReference) {
|
|
786
|
+
const receiver = operationArgument(operation, 2);
|
|
787
|
+
line(state, renderC(emittedC.objectOperation.resultAssignOseoSuperGetContextRoots, object) + renderC(emittedC.common.rootsRootsStatement, key, receiver));
|
|
788
|
+
} else {
|
|
789
|
+
line(state, `oseo_generated_property_get(context, roots[${object}], roots[${key}], &result, &roots[${operation.id}]);`);
|
|
790
|
+
return;
|
|
791
|
+
}
|
|
792
|
+
else if (operation.kind === "property-delete") line(state, renderC(emittedC.objectOperation.resultAssignOseoObjectDeleteContextRoots, object) + renderC(emittedC.common.rootsStatement, key, strict ? renderC(emittedC.common.trueValue) : renderC(emittedC.common.falseValue)));
|
|
793
|
+
else {
|
|
794
|
+
const value = operationArgument(operation, 2);
|
|
795
|
+
if (superReference) {
|
|
796
|
+
const receiver = operationArgument(operation, 3);
|
|
797
|
+
line(state, renderC(emittedC.objectOperation.resultAssignOseoSuperSetContextRoots, object) + renderC(emittedC.objectOperation.rootsRootsRoots, key, value, receiver) + renderC(emittedC.common.callSuffix, strict ? renderC(emittedC.common.trueValue) : renderC(emittedC.common.falseValue)));
|
|
798
|
+
} else line(state, renderC(emittedC.objectOperation.resultAssignOseoObjectSetContextRoots, object) + renderC(emittedC.common.rootsRoots, key, value) + renderC(emittedC.common.callSuffix, strict ? renderC(emittedC.common.trueValue) : renderC(emittedC.common.falseValue)));
|
|
799
|
+
}
|
|
800
|
+
}
|
|
801
|
+
line(state, renderC(emittedC.common.rootAssignResultValue, operation.id));
|
|
802
|
+
}
|
|
803
|
+
function emitArgumentListOperation(state, operation) {
|
|
804
|
+
location(state, operation.range);
|
|
805
|
+
state.usesAbrupt = true;
|
|
806
|
+
if (operation.kind === "argument-list-create") line(state, renderC(emittedC.argumentListOperation.resultAssignOseoArgumentListCreateContext));
|
|
807
|
+
else {
|
|
808
|
+
const list = operationArgument(operation, 0);
|
|
809
|
+
const value = operationArgument(operation, 1);
|
|
810
|
+
line(state, renderC(emittedC.argumentListOperation.resultAssignOseoArgumentListAppendContext, list) + renderC(emittedC.common.rootCallSuffix, value));
|
|
811
|
+
}
|
|
812
|
+
line(state, renderC(emittedC.common.rootAssignResultValue, operation.id));
|
|
813
|
+
}
|
|
814
|
+
function propertyCacheName(operation) {
|
|
815
|
+
if (operation.cacheId == null) throw new Error(`MIR ${operation.kind} %${operation.id} has no cache.`);
|
|
816
|
+
return renderC(emittedC.propertyCacheName.propertyCache, operation.cacheId);
|
|
817
|
+
}
|
|
818
|
+
function emitObjectTest(state, operation) {
|
|
819
|
+
const object = operationArgument(operation, 0);
|
|
820
|
+
state.scalarKinds.set(operation.id, "boolean");
|
|
821
|
+
line(state, renderC(emittedC.guardObject.boolFastAssignOseoValueIsObjectRoots, operation.id, object));
|
|
822
|
+
}
|
|
823
|
+
function emitPropertyCacheGuard(state, operation) {
|
|
824
|
+
const object = operationArgument(operation, 0);
|
|
825
|
+
const cache = propertyCacheName(operation);
|
|
826
|
+
state.scalarKinds.set(operation.id, "boolean");
|
|
827
|
+
line(state, renderC(emittedC.propertyCacheGuard.staticOseoPropertyCacheAssignUUStatement, cache));
|
|
828
|
+
line(state, renderC(emittedC.propertyCacheGuard.boolFastAssignOseoPropertyCacheMatches, operation.id) + renderC(emittedC.propertyCacheGuard.rootsAddressStatement, object, cache));
|
|
829
|
+
}
|
|
830
|
+
function emitLoadFixedSlot(state, operation) {
|
|
831
|
+
const object = operationArgument(operation, 0);
|
|
832
|
+
const cache = propertyCacheName(operation);
|
|
833
|
+
line(state, renderC(emittedC.common.rootsAssign, operation.id) + renderC(emittedC.loadFixedSlot.oseoPropertyCacheLoadRootsAddressStatement, object, cache));
|
|
834
|
+
}
|
|
835
|
+
function emitUpdatePropertyCache(state, operation) {
|
|
836
|
+
const object = operationArgument(operation, 0);
|
|
837
|
+
const key = operationArgument(operation, 1);
|
|
838
|
+
const cache = propertyCacheName(operation);
|
|
839
|
+
line(state, renderC(emittedC.updatePropertyCache.oseoPropertyCacheUpdateRootsRoots, object, key) + renderC(emittedC.common.addressStatement, cache));
|
|
840
|
+
}
|
|
841
|
+
function emitAgentFunctionSource(state, id, split) {
|
|
842
|
+
const entries = [];
|
|
843
|
+
for (const [index, run] of split.runs.entries()) {
|
|
844
|
+
const units = utf16Units(run);
|
|
845
|
+
line(state, renderC(emittedC.agent.sourceRunUnits, id, index, units.length === 0 ? "0" : units.join(renderC(emittedC.common.commaSpace))));
|
|
846
|
+
entries.push(renderC(emittedC.agent.sourceRunEntry, id, index, units.length));
|
|
847
|
+
}
|
|
848
|
+
line(state, renderC(emittedC.agent.sourceRuns, id, entries.join(renderC(emittedC.common.commaSpace))));
|
|
849
|
+
line(state, renderC(emittedC.agent.sourceHoles, id, split.holes.map((hole) => renderC(emittedC.agent.holeIndex, hole)).join(renderC(emittedC.common.commaSpace))));
|
|
850
|
+
line(state, renderC(emittedC.common.statusNormalOpen));
|
|
851
|
+
line(state, renderC(emittedC.agent.setSource, id, id, split.runs.length, id));
|
|
852
|
+
line(state, renderC(emittedC.common.rootAssignResultValue, id));
|
|
853
|
+
line(state, renderC(emittedC.common.closeBlock));
|
|
854
|
+
}
|
|
855
|
+
function emitFunctionCreate(state, operation) {
|
|
856
|
+
if (operation.functionId == null) throw new Error(`MIR function-create %${operation.id} has no code id.`);
|
|
857
|
+
if (operation.functionName == null || operation.functionLength == null) throw new Error(`MIR function-create %${operation.id} has no function metadata.`);
|
|
858
|
+
if (operation.functionKind == null) throw new Error(`MIR function-create %${operation.id} has no callable kind.`);
|
|
859
|
+
const functionKinds = {
|
|
860
|
+
arrow: renderC(emittedC.functionCreate.oseoFunctionArrow),
|
|
861
|
+
async: renderC(emittedC.functionCreate.oseoFunctionAsync),
|
|
862
|
+
"async-arrow": renderC(emittedC.functionCreate.oseoFunctionAsyncArrow),
|
|
863
|
+
"async-generator": renderC(emittedC.functionCreate.oseoFunctionAsyncGenerator),
|
|
864
|
+
class: renderC(emittedC.functionCreate.oseoFunctionClass),
|
|
865
|
+
generator: renderC(emittedC.functionCreate.oseoFunctionGenerator),
|
|
866
|
+
method: renderC(emittedC.functionCreate.oseoFunctionMethod),
|
|
867
|
+
ordinary: renderC(emittedC.functionCreate.oseoFunctionOrdinary)
|
|
868
|
+
};
|
|
869
|
+
const units = utf16Units(operation.functionName);
|
|
870
|
+
let nameInput = renderC(emittedC.common.nullPointer);
|
|
871
|
+
if (units.length > 0) {
|
|
872
|
+
const name = renderC(emittedC.functionCreate.functionNameUnits, operation.id);
|
|
873
|
+
line(state, renderC(emittedC.common.staticConstUint16TAssignStatement, name, units.join(renderC(emittedC.common.commaSpace))));
|
|
874
|
+
nameInput = name;
|
|
875
|
+
}
|
|
876
|
+
const inferredName = operation.arguments[0] == null ? renderC(emittedC.common.undefinedValue) : renderC(emittedC.common.root, operation.arguments[0]);
|
|
877
|
+
const namePrefixes = {
|
|
878
|
+
get: renderC(emittedC.functionCreate.oseoFunctionNamePrefixGet),
|
|
879
|
+
set: renderC(emittedC.functionCreate.oseoFunctionNamePrefixSet)
|
|
880
|
+
};
|
|
881
|
+
const namePrefix = operation.accessorKind == null ? renderC(emittedC.functionCreate.oseoFunctionNamePrefixNone) : namePrefixes[operation.accessorKind];
|
|
882
|
+
location(state, operation.range);
|
|
883
|
+
state.usesAbrupt = true;
|
|
884
|
+
line(state, renderC(state.nonStrictFunctionIds.has(operation.functionId) ? emittedC.functionCreate.resultAssignOseoFunctionCreateNonStrictContextU : emittedC.functionCreate.resultAssignOseoFunctionCreateContextU, operation.functionId) + renderC(emittedC.functionCreate.rootsU, state.environmentSlot, nameInput, units.length) + renderC(emittedC.common.unsignedWithComma, operation.functionLength) + renderC(emittedC.functionCreate.receiver, functionKinds[operation.functionKind], inferredName) + renderC(emittedC.common.callSuffix, namePrefix));
|
|
885
|
+
line(state, renderC(emittedC.common.rootAssignResultValue, operation.id));
|
|
886
|
+
if (operation.functionSourceHoles != null) emitAgentFunctionSource(state, operation.id, operation.functionSourceHoles);
|
|
887
|
+
else if (operation.functionSource != null) {
|
|
888
|
+
const sourceUnits = utf16Units(operation.functionSource);
|
|
889
|
+
const sourceName = renderC(emittedC.functionCreate.functionSourceUnits, operation.id);
|
|
890
|
+
line(state, renderC(emittedC.common.staticConstUint16TAssignStatement, sourceName, sourceUnits.join(renderC(emittedC.common.commaSpace))));
|
|
891
|
+
line(state, renderC(emittedC.common.statusNormalOpen));
|
|
892
|
+
line(state, renderC(emittedC.functionCreate.resultAssignOseoFunctionSetSource, operation.id, sourceName, sourceUnits.length));
|
|
893
|
+
line(state, renderC(emittedC.common.rootAssignResultValue, operation.id));
|
|
894
|
+
line(state, renderC(emittedC.common.closeBlock));
|
|
895
|
+
}
|
|
896
|
+
if (operation.functionKind === "arrow" || operation.functionKind === "async-arrow") {
|
|
897
|
+
const newTarget = state.generator ? renderC(emittedC.common.undefinedValue) : renderC(emittedC.functionCreate.newTarget);
|
|
898
|
+
line(state, renderC(emittedC.functionCreate.bindArrowContext, operation.id, newTarget));
|
|
899
|
+
}
|
|
900
|
+
}
|
|
901
|
+
function emitErrorIntrinsic(state, operation) {
|
|
902
|
+
const errorKinds = {
|
|
903
|
+
AggregateError: renderC(emittedC.errorIntrinsic.oseoErrorAggregate),
|
|
904
|
+
Error: renderC(emittedC.errorIntrinsic.oseoErrorError),
|
|
905
|
+
EvalError: renderC(emittedC.errorIntrinsic.oseoErrorEval),
|
|
906
|
+
RangeError: renderC(emittedC.errorIntrinsic.oseoErrorRange),
|
|
907
|
+
ReferenceError: renderC(emittedC.errorIntrinsic.oseoErrorReference),
|
|
908
|
+
SyntaxError: renderC(emittedC.errorIntrinsic.oseoErrorSyntax),
|
|
909
|
+
TypeError: renderC(emittedC.errorIntrinsic.oseoErrorType),
|
|
910
|
+
URIError: renderC(emittedC.errorIntrinsic.oseoErrorUri)
|
|
911
|
+
};
|
|
912
|
+
if (operation.errorName == null) throw new Error(`MIR error-intrinsic %${operation.id} has no name.`);
|
|
913
|
+
location(state, operation.range);
|
|
914
|
+
state.usesAbrupt = true;
|
|
915
|
+
line(state, renderC(emittedC.errorIntrinsic.resultAssignOseoErrorIntrinsicContext) + renderC(emittedC.common.callSuffix, errorKinds[operation.errorName]));
|
|
916
|
+
line(state, renderC(emittedC.common.rootAssignResultValue, operation.id));
|
|
917
|
+
}
|
|
918
|
+
function emitConstructReceiver(state, operation) {
|
|
919
|
+
const callee = operationArgument(operation, 0);
|
|
920
|
+
location(state, operation.range);
|
|
921
|
+
state.usesAbrupt = true;
|
|
922
|
+
line(state, renderC(emittedC.common.resultAssignOseoFunctionPrototypeContext, callee));
|
|
923
|
+
line(state, renderC(emittedC.common.statusNormalOpen));
|
|
924
|
+
line(state, renderC(emittedC.constructReceiver.resultAssignOseoConstructorReceiverContext));
|
|
925
|
+
line(state, renderC(emittedC.common.closeBlock));
|
|
926
|
+
line(state, renderC(emittedC.common.rootAssignResultValue, operation.id));
|
|
927
|
+
}
|
|
928
|
+
/**
|
|
929
|
+
* Links a derived class to its heritage: the constructor inherits from
|
|
930
|
+
* the parent constructor and the class prototype object from the
|
|
931
|
+
* parent's `prototype`, so both static and instance lookups walk into
|
|
932
|
+
* the parent.
|
|
933
|
+
*/
|
|
934
|
+
function emitClassHeritage(state, operation) {
|
|
935
|
+
const constructorValue = operationArgument(operation, 0);
|
|
936
|
+
const heritage = operationArgument(operation, 1);
|
|
937
|
+
location(state, operation.range);
|
|
938
|
+
state.usesAbrupt = true;
|
|
939
|
+
line(state, renderC(emittedC.classHeritage.resultAssignOseoClassHeritageContextRoots, constructorValue) + renderC(emittedC.common.rootCallSuffix, heritage));
|
|
940
|
+
line(state, renderC(emittedC.common.rootAssignResultValue, operation.id));
|
|
941
|
+
}
|
|
942
|
+
/**
|
|
943
|
+
* Records one instance field on the constructor: the key evaluated by
|
|
944
|
+
* the class body and the closure that produces the value, or
|
|
945
|
+
* `undefined` for a field declared without an initializer.
|
|
946
|
+
*/
|
|
947
|
+
function emitClassFieldDefine(state, operation) {
|
|
948
|
+
const constructorValue = operationArgument(operation, 0);
|
|
949
|
+
const key = operationArgument(operation, 1);
|
|
950
|
+
const initializer = operationArgument(operation, 2);
|
|
951
|
+
location(state, operation.range);
|
|
952
|
+
state.usesAbrupt = true;
|
|
953
|
+
line(state, renderC(emittedC.classFieldDefine.resultAssignOseoClassFieldDefineContext) + renderC(emittedC.common.rootsRootsRootsStatement, constructorValue, key, initializer));
|
|
954
|
+
line(state, renderC(emittedC.common.rootAssignResultValue, operation.id));
|
|
955
|
+
}
|
|
956
|
+
/**
|
|
957
|
+
* Runs one `static` field initializer against the constructor and
|
|
958
|
+
* defines the result on it. The public form creates an own writable,
|
|
959
|
+
* enumerable, configurable data property, and the private form adds a
|
|
960
|
+
* private element the constructor carries.
|
|
961
|
+
*/
|
|
962
|
+
function emitClassStaticFieldDefine(state, operation, privateElement) {
|
|
963
|
+
const constructorValue = operationArgument(operation, 0);
|
|
964
|
+
const key = operationArgument(operation, 1);
|
|
965
|
+
const initializer = operationArgument(operation, 2);
|
|
966
|
+
const entryPoint = privateElement ? renderC(emittedC.classStaticFieldDefine.oseoClassStaticPrivateFieldDefine) : renderC(emittedC.classStaticFieldDefine.oseoClassStaticFieldDefine);
|
|
967
|
+
location(state, operation.range);
|
|
968
|
+
state.usesAbrupt = true;
|
|
969
|
+
line(state, renderC(emittedC.common.resultAssignContext, entryPoint) + renderC(emittedC.common.rootsRootsRootsStatement, constructorValue, key, initializer));
|
|
970
|
+
line(state, renderC(emittedC.common.rootAssignResultValue, operation.id));
|
|
971
|
+
}
|
|
972
|
+
/**
|
|
973
|
+
* Creates one private name: the identity a class evaluation gives one
|
|
974
|
+
* declared `#name`, and initializes its fresh lexical cell in one runtime
|
|
975
|
+
* call. The runtime owns intermediate roots and C temporaries, so they do
|
|
976
|
+
* not accumulate in the enclosing script's frame across private names.
|
|
977
|
+
*/
|
|
978
|
+
function emitPrivateNameCreate(state, operation) {
|
|
979
|
+
if (operation.bindingId == null) throw new Error(`MIR private name %${operation.id} has no binding.`);
|
|
980
|
+
location(state, operation.range);
|
|
981
|
+
state.usesAbrupt = true;
|
|
982
|
+
line(state, `result = oseo_private_name_bind(context, roots[${state.environmentSlot}], ${operation.bindingId}u);`);
|
|
983
|
+
line(state, renderC(emittedC.common.rootAssignResultValue, operation.id));
|
|
984
|
+
}
|
|
985
|
+
/**
|
|
986
|
+
* Records one private field on the constructor: the private name the
|
|
987
|
+
* class evaluation created and the closure that produces the value, or
|
|
988
|
+
* `undefined` for a field declared without an initializer.
|
|
989
|
+
*/
|
|
990
|
+
function emitClassPrivateFieldDefine(state, operation) {
|
|
991
|
+
const constructorValue = operationArgument(operation, 0);
|
|
992
|
+
const privateName = operationArgument(operation, 1);
|
|
993
|
+
const initializer = operationArgument(operation, 2);
|
|
994
|
+
location(state, operation.range);
|
|
995
|
+
state.usesAbrupt = true;
|
|
996
|
+
line(state, renderC(emittedC.classPrivateFieldDefine.resultAssignOseoClassPrivateFieldDefine) + renderC(emittedC.common.rootsRoots, constructorValue, privateName) + renderC(emittedC.common.rootCallSuffix, initializer));
|
|
997
|
+
line(state, renderC(emittedC.common.rootAssignResultValue, operation.id));
|
|
998
|
+
}
|
|
999
|
+
/**
|
|
1000
|
+
* Records one instance private method or accessor half, or installs one
|
|
1001
|
+
* static half directly on the constructor. A getter and a setter under
|
|
1002
|
+
* one private name merge into the single accessor element that name
|
|
1003
|
+
* reaches.
|
|
1004
|
+
*/
|
|
1005
|
+
function emitClassPrivateMethodDefine(state, operation) {
|
|
1006
|
+
const constructorValue = operationArgument(operation, 0);
|
|
1007
|
+
const privateName = operationArgument(operation, 1);
|
|
1008
|
+
const value = operationArgument(operation, 2);
|
|
1009
|
+
const kind = operation.accessorKind === "get" ? renderC(emittedC.common.privateGetterKind) : operation.accessorKind === "set" ? renderC(emittedC.common.privateSetterKind) : renderC(emittedC.common.privateMethodKind);
|
|
1010
|
+
location(state, operation.range);
|
|
1011
|
+
state.usesAbrupt = true;
|
|
1012
|
+
line(state, renderC(operation.kind === "class-static-private-method-define" ? emittedC.classPrivateMethodDefine.resultAssignOseoClassStaticPrivateMethodDefine : emittedC.classPrivateMethodDefine.resultAssignOseoClassPrivateMethodDefine) + renderC(emittedC.common.rootsRoots, constructorValue, privateName) + renderC(emittedC.common.rootsStatement, value, kind));
|
|
1013
|
+
line(state, renderC(emittedC.common.rootAssignResultValue, operation.id));
|
|
1014
|
+
}
|
|
1015
|
+
/** PrivateGet: the element the object carries under one private name. */
|
|
1016
|
+
function emitPrivateGet(state, operation) {
|
|
1017
|
+
const object = operationArgument(operation, 0);
|
|
1018
|
+
const privateName = operationArgument(operation, 1);
|
|
1019
|
+
location(state, operation.range);
|
|
1020
|
+
state.usesAbrupt = true;
|
|
1021
|
+
line(state, renderC(emittedC.privateGet.resultAssignOseoPrivateGetContextRoots, object) + renderC(emittedC.common.rootCallSuffix, privateName));
|
|
1022
|
+
line(state, renderC(emittedC.common.rootAssignResultValue, operation.id));
|
|
1023
|
+
}
|
|
1024
|
+
/** PrivateBrandCheck: whether one object carries one private name. */
|
|
1025
|
+
function emitPrivateIn(state, operation) {
|
|
1026
|
+
const object = operationArgument(operation, 0);
|
|
1027
|
+
const privateName = operationArgument(operation, 1);
|
|
1028
|
+
location(state, operation.range);
|
|
1029
|
+
state.usesAbrupt = true;
|
|
1030
|
+
line(state, renderC(emittedC.privateIn.resultAssignOseoPrivateInContextRoots, object) + renderC(emittedC.common.rootCallSuffix, privateName));
|
|
1031
|
+
line(state, renderC(emittedC.common.rootAssignResultValue, operation.id));
|
|
1032
|
+
}
|
|
1033
|
+
/** PrivateSet: replaces the value one private field element holds. */
|
|
1034
|
+
function emitPrivateSet(state, operation) {
|
|
1035
|
+
const object = operationArgument(operation, 0);
|
|
1036
|
+
const privateName = operationArgument(operation, 1);
|
|
1037
|
+
const value = operationArgument(operation, 2);
|
|
1038
|
+
location(state, operation.range);
|
|
1039
|
+
state.usesAbrupt = true;
|
|
1040
|
+
line(state, renderC(emittedC.privateSet.resultAssignOseoPrivateSetContextRoots, object) + renderC(emittedC.common.rootsRootsStatement, privateName, value));
|
|
1041
|
+
line(state, renderC(emittedC.common.rootAssignResultValue, operation.id));
|
|
1042
|
+
}
|
|
1043
|
+
/**
|
|
1044
|
+
* Runs the running constructor's instance element records against one
|
|
1045
|
+
* instance: it installs the private methods and accessors, then runs
|
|
1046
|
+
* the field initializers. The constructor is the callee, so a base
|
|
1047
|
+
* constructor reaches its own class's records and a derived one reaches
|
|
1048
|
+
* only the records its own class declared.
|
|
1049
|
+
*/
|
|
1050
|
+
function emitInstanceElementsInit(state, operation) {
|
|
1051
|
+
const instance = operationArgument(operation, 0);
|
|
1052
|
+
location(state, operation.range);
|
|
1053
|
+
state.usesAbrupt = true;
|
|
1054
|
+
line(state, renderC(emittedC.instanceElementsInit.resultAssignOseoInitializeInstanceElements) + renderC(emittedC.common.rootCallSuffix, instance));
|
|
1055
|
+
line(state, renderC(emittedC.common.rootAssignResultValue, operation.id));
|
|
1056
|
+
}
|
|
1057
|
+
/**
|
|
1058
|
+
* Records the object a class element's `super.x` references look
|
|
1059
|
+
* through. The binding is the only way the element's function object
|
|
1060
|
+
* reaches its class again, because a method value can be called through
|
|
1061
|
+
* any receiver.
|
|
1062
|
+
*/
|
|
1063
|
+
function emitHomeObjectBind(state, operation) {
|
|
1064
|
+
const functionValue = operationArgument(operation, 0);
|
|
1065
|
+
const home = operationArgument(operation, 1);
|
|
1066
|
+
location(state, operation.range);
|
|
1067
|
+
line(state, renderC(emittedC.homeObjectBind.oseoBindHomeObjectRootsRootsStatement, functionValue, home));
|
|
1068
|
+
}
|
|
1069
|
+
/**
|
|
1070
|
+
* Reads the [[Prototype]] of the running function's home object, which
|
|
1071
|
+
* is where a `super` property reference starts its lookup. It reads
|
|
1072
|
+
* runtime state that a class definition already established, so it runs
|
|
1073
|
+
* no user code and reports no abrupt completion; a home object whose
|
|
1074
|
+
* chain ends leaves the nullish value for the reference itself to
|
|
1075
|
+
* reject.
|
|
1076
|
+
*/
|
|
1077
|
+
function emitSuperBase(state, operation) {
|
|
1078
|
+
location(state, operation.range);
|
|
1079
|
+
line(state, renderC(emittedC.superBase.rootsAssignOseoSuperBaseCalleeStatement, operation.id));
|
|
1080
|
+
}
|
|
1081
|
+
/**
|
|
1082
|
+
* Rejects an evaluated `super` property reference used as a `delete`
|
|
1083
|
+
* operand. The receiver and the key have already been evaluated, so the
|
|
1084
|
+
* only remaining step is the ReferenceError the delete evaluation
|
|
1085
|
+
* raises for a super reference.
|
|
1086
|
+
*/
|
|
1087
|
+
function emitSuperPropertyDelete(state, operation) {
|
|
1088
|
+
location(state, operation.range);
|
|
1089
|
+
state.usesAbrupt = true;
|
|
1090
|
+
line(state, renderC(emittedC.superPropertyDelete.resultAssignOseoSuperPropertyDeleteContext));
|
|
1091
|
+
line(state, renderC(emittedC.common.rootAssignResultValue, operation.id));
|
|
1092
|
+
}
|
|
1093
|
+
/**
|
|
1094
|
+
* Reads the running constructor's own [[Prototype]], which is the
|
|
1095
|
+
* constructor `super()` invokes.
|
|
1096
|
+
*/
|
|
1097
|
+
function emitSuperConstructor(state, operation) {
|
|
1098
|
+
location(state, operation.range);
|
|
1099
|
+
state.usesAbrupt = true;
|
|
1100
|
+
line(state, renderC(emittedC.superConstructor.resultAssignOseoSuperConstructorContext));
|
|
1101
|
+
line(state, renderC(emittedC.common.rootAssignResultValue, operation.id));
|
|
1102
|
+
}
|
|
1103
|
+
/** Binds what `super()` produced to the derived constructor's `this`. */
|
|
1104
|
+
function emitThisBind(state, operation) {
|
|
1105
|
+
const bindingId = operation.bindingId;
|
|
1106
|
+
if (bindingId == null) throw new Error(`MIR this-bind %${operation.id} has no binding identity.`);
|
|
1107
|
+
const value = operationArgument(operation, 0);
|
|
1108
|
+
location(state, operation.range);
|
|
1109
|
+
state.usesAbrupt = true;
|
|
1110
|
+
line(state, renderC(emittedC.common.resultAssignOseoEnvironmentGetContext) + renderC(emittedC.common.rootsUStatement, state.environmentSlot, bindingId));
|
|
1111
|
+
line(state, renderC(emittedC.common.gotoAbruptUnlessNormal));
|
|
1112
|
+
line(state, renderC(emittedC.thisBind.resultAssignOseoBindThisContextResultValue, value));
|
|
1113
|
+
line(state, renderC(emittedC.common.rootAssignResultValue, operation.id));
|
|
1114
|
+
}
|
|
1115
|
+
/**
|
|
1116
|
+
* Resolves a derived constructor's completion value: an object stands as
|
|
1117
|
+
* written, `undefined` becomes the bound `this`, and any other value is
|
|
1118
|
+
* a `TypeError`.
|
|
1119
|
+
*/
|
|
1120
|
+
function emitDerivedReturn(state, operation) {
|
|
1121
|
+
const bindingId = operation.bindingId;
|
|
1122
|
+
if (bindingId == null) throw new Error(`MIR derived-return %${operation.id} has no binding identity.`);
|
|
1123
|
+
const value = operationArgument(operation, 0);
|
|
1124
|
+
location(state, operation.range);
|
|
1125
|
+
state.usesAbrupt = true;
|
|
1126
|
+
line(state, renderC(emittedC.common.resultAssignOseoEnvironmentGetContext) + renderC(emittedC.common.rootsUStatement, state.environmentSlot, bindingId));
|
|
1127
|
+
line(state, renderC(emittedC.common.gotoAbruptUnlessNormal));
|
|
1128
|
+
line(state, renderC(emittedC.derivedReturn.resultAssignOseoDerivedConstructorResult, value) + renderC(emittedC.derivedReturn.resultValueStatement));
|
|
1129
|
+
line(state, renderC(emittedC.common.gotoAbruptUnlessNormal));
|
|
1130
|
+
line(state, renderC(emittedC.common.rootAssignResultValue, operation.id));
|
|
1131
|
+
}
|
|
1132
|
+
function emitClassPrototype(state, operation) {
|
|
1133
|
+
const constructorValue = operationArgument(operation, 0);
|
|
1134
|
+
location(state, operation.range);
|
|
1135
|
+
state.usesAbrupt = true;
|
|
1136
|
+
line(state, renderC(emittedC.common.resultAssignOseoFunctionPrototypeContext, constructorValue));
|
|
1137
|
+
line(state, renderC(emittedC.common.rootAssignResultValue, operation.id));
|
|
1138
|
+
}
|
|
1139
|
+
function emitModuleNamespace(state, operation) {
|
|
1140
|
+
const names = operation.namespaceNames;
|
|
1141
|
+
const namespaceBindingIds = operation.namespaceBindingIds;
|
|
1142
|
+
if (names == null || namespaceBindingIds == null || names.length !== namespaceBindingIds.length) throw new Error(`MIR namespace %${operation.id} has invalid entries.`);
|
|
1143
|
+
if (names.length === 0) {
|
|
1144
|
+
location(state, operation.range);
|
|
1145
|
+
state.usesAbrupt = true;
|
|
1146
|
+
line(state, renderC(emittedC.common.resultAssignOseoModuleNamespaceCreate) + renderC(emittedC.moduleNamespace.rootsUNullNullNullStatement, state.environmentSlot));
|
|
1147
|
+
line(state, renderC(emittedC.common.rootAssignResultValue, operation.id));
|
|
1148
|
+
return;
|
|
1149
|
+
}
|
|
1150
|
+
const pointers = [];
|
|
1151
|
+
const lengths = [];
|
|
1152
|
+
for (const [index, name] of names.entries()) {
|
|
1153
|
+
const units = utf16Units(name);
|
|
1154
|
+
lengths.push(units.length);
|
|
1155
|
+
if (units.length === 0) {
|
|
1156
|
+
pointers.push(renderC(emittedC.common.nullPointer));
|
|
1157
|
+
continue;
|
|
1158
|
+
}
|
|
1159
|
+
const constantName = renderC(emittedC.moduleNamespace.namespaceUnits, operation.id, index);
|
|
1160
|
+
line(state, renderC(emittedC.common.staticConstUint16TAssignStatement, constantName, units.join(renderC(emittedC.common.commaSpace))));
|
|
1161
|
+
pointers.push(constantName);
|
|
1162
|
+
}
|
|
1163
|
+
const prefix = renderC(emittedC.moduleNamespace.namespace, operation.id);
|
|
1164
|
+
line(state, renderC(emittedC.moduleNamespace.staticConstUint16TPointerConstNamesAssign, prefix) + renderC(emittedC.common.bracedInitializer, pointers.join(renderC(emittedC.common.commaSpace))));
|
|
1165
|
+
line(state, renderC(emittedC.moduleNamespace.staticConstSizeTLengthsAssignStatement, prefix, lengths.join(renderC(emittedC.common.commaSpace))));
|
|
1166
|
+
line(state, renderC(emittedC.moduleNamespace.staticConstSizeTBindingsAssign, prefix) + renderC(emittedC.common.bracedInitializer, namespaceBindingIds.join(renderC(emittedC.common.commaSpace))));
|
|
1167
|
+
location(state, operation.range);
|
|
1168
|
+
state.usesAbrupt = true;
|
|
1169
|
+
line(state, renderC(emittedC.common.resultAssignOseoModuleNamespaceCreate) + renderC(emittedC.common.rootsU, state.environmentSlot, names.length) + renderC(emittedC.moduleNamespace.namesLengthsBindingsStatement, prefix, prefix, prefix));
|
|
1170
|
+
line(state, renderC(emittedC.common.rootAssignResultValue, operation.id));
|
|
1171
|
+
}
|
|
1172
|
+
function emitOperation(state, operation) {
|
|
1173
|
+
if (operation.kind === "constant") {
|
|
1174
|
+
if (operation.constant == null) throw new Error(`MIR constant %${operation.id} has no value.`);
|
|
1175
|
+
emitConstant(state, operation, operation.constant);
|
|
1176
|
+
} else if (operation.kind === "read") emitRead(state, operation);
|
|
1177
|
+
else if (operation.kind === "write") emitWrite(state, operation);
|
|
1178
|
+
else if (operation.kind === "initialize") emitInitialize(state, operation);
|
|
1179
|
+
else if (operation.kind === "binding-reset") emitBindingReset(state, operation);
|
|
1180
|
+
else if (operation.kind === "function-create") emitFunctionCreate(state, operation);
|
|
1181
|
+
else if (operation.kind === "construct-receiver") emitConstructReceiver(state, operation);
|
|
1182
|
+
else if (operation.kind === "class-prototype") emitClassPrototype(state, operation);
|
|
1183
|
+
else if (operation.kind === "class-heritage") emitClassHeritage(state, operation);
|
|
1184
|
+
else if (operation.kind === "class-field-define") emitClassFieldDefine(state, operation);
|
|
1185
|
+
else if (operation.kind === "private-name-create") emitPrivateNameCreate(state, operation);
|
|
1186
|
+
else if (operation.kind === "class-private-field-define") emitClassPrivateFieldDefine(state, operation);
|
|
1187
|
+
else if (operation.kind === "class-private-method-define") emitClassPrivateMethodDefine(state, operation);
|
|
1188
|
+
else if (operation.kind === "class-static-private-method-define") emitClassPrivateMethodDefine(state, operation);
|
|
1189
|
+
else if (operation.kind === "class-static-field-define") emitClassStaticFieldDefine(state, operation, false);
|
|
1190
|
+
else if (operation.kind === "class-static-private-field-define") emitClassStaticFieldDefine(state, operation, true);
|
|
1191
|
+
else if (operation.kind === "private-get") emitPrivateGet(state, operation);
|
|
1192
|
+
else if (operation.kind === "private-in") emitPrivateIn(state, operation);
|
|
1193
|
+
else if (operation.kind === "private-set") emitPrivateSet(state, operation);
|
|
1194
|
+
else if (operation.kind === "instance-elements-init") emitInstanceElementsInit(state, operation);
|
|
1195
|
+
else if (operation.kind === "home-object-bind") emitHomeObjectBind(state, operation);
|
|
1196
|
+
else if (operation.kind === "super-base") emitSuperBase(state, operation);
|
|
1197
|
+
else if (operation.kind === "super-property-delete") emitSuperPropertyDelete(state, operation);
|
|
1198
|
+
else if (operation.kind === "super-constructor") emitSuperConstructor(state, operation);
|
|
1199
|
+
else if (operation.kind === "this-bind") emitThisBind(state, operation);
|
|
1200
|
+
else if (operation.kind === "derived-return") emitDerivedReturn(state, operation);
|
|
1201
|
+
else if (operation.kind === "error-intrinsic") emitErrorIntrinsic(state, operation);
|
|
1202
|
+
else if (operation.kind === "symbol-intrinsic") {
|
|
1203
|
+
location(state, operation.range);
|
|
1204
|
+
state.usesAbrupt = true;
|
|
1205
|
+
line(state, renderC(emittedC.operation.resultAssignOseoSymbolIntrinsicContext));
|
|
1206
|
+
line(state, renderC(emittedC.common.rootAssignResultValue, operation.id));
|
|
1207
|
+
} else if (operation.kind === "function-intrinsic") {
|
|
1208
|
+
location(state, operation.range);
|
|
1209
|
+
state.usesAbrupt = true;
|
|
1210
|
+
line(state, renderC(emittedC.functionIntrinsic.resultAssign));
|
|
1211
|
+
line(state, renderC(emittedC.common.rootAssignResultValue, operation.id));
|
|
1212
|
+
} else if (operation.kind === "agent-hole") {
|
|
1213
|
+
const hole = operation.agentHole;
|
|
1214
|
+
if (hole == null) throw new Error(`MIR agent-hole %${operation.id} has no hole index.`);
|
|
1215
|
+
location(state, operation.range);
|
|
1216
|
+
state.usesAbrupt = true;
|
|
1217
|
+
line(state, renderC(emittedC.agent.holeStatement, hole));
|
|
1218
|
+
line(state, renderC(emittedC.common.rootAssignResultValue, operation.id));
|
|
1219
|
+
} else if (operation.kind === "iterator-intrinsic") {
|
|
1220
|
+
location(state, operation.range);
|
|
1221
|
+
state.usesAbrupt = true;
|
|
1222
|
+
line(state, renderC(emittedC.iteratorIntrinsic.resultAssign));
|
|
1223
|
+
line(state, renderC(emittedC.common.rootAssignResultValue, operation.id));
|
|
1224
|
+
} else if (operation.kind === "module-namespace-create") emitModuleNamespace(state, operation);
|
|
1225
|
+
else if (operation.kind === "receiver") line(state, renderC(emittedC.operation.rootsAssignReceiverStatement, operation.id));
|
|
1226
|
+
else if (operation.kind === "global-environment") {
|
|
1227
|
+
location(state, operation.range);
|
|
1228
|
+
state.usesAbrupt = true;
|
|
1229
|
+
line(state, `result = oseo_global_environment(context, roots[${operation.arguments[0]}]);`);
|
|
1230
|
+
line(state, renderC(emittedC.common.rootAssignResultValue, operation.id));
|
|
1231
|
+
} else if (operation.kind === "global-this") {
|
|
1232
|
+
location(state, operation.range);
|
|
1233
|
+
state.usesAbrupt = true;
|
|
1234
|
+
line(state, renderC(emittedC.operation.resultAssignOseoThisValueContextReceiver));
|
|
1235
|
+
line(state, renderC(emittedC.common.rootAssignResultValue, operation.id));
|
|
1236
|
+
} else if (operation.kind === "new-target") line(state, renderC(emittedC.common.rootsAssign, operation.id) + (state.generator ? renderC(emittedC.operation.undefinedExpressionStatement) : renderC(emittedC.operation.newTargetStatement)));
|
|
1237
|
+
else if (operation.kind === "caught") {
|
|
1238
|
+
state.usesCompletion = true;
|
|
1239
|
+
const slot = operation.completionSlot;
|
|
1240
|
+
if (slot == null) throw new Error(`MIR caught %${operation.id} has no completion slot.`);
|
|
1241
|
+
line(state, renderC(emittedC.common.oseoContextClearLanguageErrorContext));
|
|
1242
|
+
line(state, renderC(emittedC.operation.rootsAssignRootsUStatement, operation.id, state.completionSlotStart + slot));
|
|
1243
|
+
line(state, renderC(emittedC.common.resultAssignOseoResultOseoStatusNormal) + renderC(emittedC.common.rootUnsignedInitializerSuffix, state.completionSlotStart + slot));
|
|
1244
|
+
} else if (operation.kind === "completion-set") {
|
|
1245
|
+
state.usesCompletion = true;
|
|
1246
|
+
const kinds = {
|
|
1247
|
+
jump: 3,
|
|
1248
|
+
normal: 0,
|
|
1249
|
+
return: 1,
|
|
1250
|
+
throw: 2
|
|
1251
|
+
};
|
|
1252
|
+
const kind = operation.completionKind;
|
|
1253
|
+
if (kind == null) throw new Error(`MIR completion-set %${operation.id} has no kind.`);
|
|
1254
|
+
const slot = operation.completionSlot;
|
|
1255
|
+
if (slot == null) throw new Error(`MIR completion-set %${operation.id} has no completion slot.`);
|
|
1256
|
+
line(state, renderC(emittedC.operation.completionUKindAssignStatement, slot, kinds[kind]));
|
|
1257
|
+
if (kind === "throw") {
|
|
1258
|
+
location(state, operation.range);
|
|
1259
|
+
line(state, renderC(emittedC.common.oseoContextClearLanguageErrorContext));
|
|
1260
|
+
line(state, renderC(emittedC.operation.completionULineAssignContextMemberLine, slot));
|
|
1261
|
+
line(state, renderC(emittedC.operation.completionUColumnAssignContextMemberColumn, slot));
|
|
1262
|
+
line(state, renderC(emittedC.operation.completionUSourceIdAssignContextMember, slot));
|
|
1263
|
+
line(state, renderC(emittedC.operation.completionUSourceIdLengthAssign, slot) + renderC(emittedC.common.contextMemberSourceIdLengthStatement));
|
|
1264
|
+
line(state, renderC(emittedC.operation.completionUErrorCodeAssignContextMember, slot));
|
|
1265
|
+
line(state, renderC(emittedC.operation.completionUErrorMessageAssignContextMember, slot));
|
|
1266
|
+
}
|
|
1267
|
+
if (operation.arguments[0] != null) line(state, renderC(emittedC.operation.rootsUAssign, state.completionSlotStart + slot) + renderC(emittedC.operation.rootsStatement, operation.arguments[0]));
|
|
1268
|
+
if (operation.completionTarget != null) {
|
|
1269
|
+
line(state, renderC(emittedC.operation.completionUTargetAssign, slot) + renderC(emittedC.common.uStatement, operation.completionTarget.blockId));
|
|
1270
|
+
line(state, renderC(emittedC.operation.completionUDepthAssign, slot) + renderC(emittedC.common.uStatement, operation.completionTarget.cleanupDepth));
|
|
1271
|
+
}
|
|
1272
|
+
} else if (operation.kind === "unary") emitUnary(state, operation);
|
|
1273
|
+
else if (operation.kind === "binary") emitBinary(state, operation);
|
|
1274
|
+
else if (operation.kind === "guard-object" || operation.kind === "is-object") emitObjectTest(state, operation);
|
|
1275
|
+
else if (operation.kind === "guard-shape") emitPropertyCacheGuard(state, operation);
|
|
1276
|
+
else if (operation.kind === "guard-smi") emitGuardSmi(state, operation);
|
|
1277
|
+
else if (operation.kind === "unbox-smi") emitUnboxSmi(state, operation);
|
|
1278
|
+
else if (operation.kind === "add-smi-checked") emitCheckedAdd(state, operation);
|
|
1279
|
+
else if (operation.kind === "box-smi") emitBoxSmi(state, operation);
|
|
1280
|
+
else if (operation.kind === "enumerate-get" || operation.kind === "enumerate-next") emitEnumerateOperation(state, operation);
|
|
1281
|
+
else if (operation.kind === "iterator-await-result" || operation.kind === "iterator-await-start" || operation.kind === "iterator-get" || operation.kind === "iterator-next" || operation.kind === "iterator-delegate-next" || operation.kind === "iterator-delegate-return" || operation.kind === "iterator-delegate-throw" || operation.kind === "iterator-close" || operation.kind === "iterator-close-result" || operation.kind === "iterator-close-start") emitIteratorOperation(state, operation);
|
|
1282
|
+
else if (operation.kind.startsWith("dispose-scope-")) emitDisposalOperation(state, operation);
|
|
1283
|
+
else if (operation.kind === "load-fixed-slot") emitLoadFixedSlot(state, operation);
|
|
1284
|
+
else if (operation.kind === "update-property-cache") emitUpdatePropertyCache(state, operation);
|
|
1285
|
+
else if (operation.kind === "count-guard-hit") {
|
|
1286
|
+
if (state.observeSpecialization) line(state, renderC(emittedC.operation.contextMemberGuardHitsPlusAssignUStatement));
|
|
1287
|
+
} else if (operation.kind === "count-guard-miss") {
|
|
1288
|
+
if (state.observeSpecialization) line(state, renderC(emittedC.operation.contextMemberGuardMissesPlusAssignU));
|
|
1289
|
+
} else if (operation.kind === "count-overflow-miss") {
|
|
1290
|
+
if (state.observeSpecialization) line(state, renderC(emittedC.operation.contextMemberOverflowMissesPlusAssignU));
|
|
1291
|
+
} else if (operation.kind === "call" || operation.kind === "construct") emitCall(state, operation);
|
|
1292
|
+
else if (operation.kind === "argument-list-append" || operation.kind === "argument-list-create") emitArgumentListOperation(state, operation);
|
|
1293
|
+
else if (operation.kind === "template-object") emitTemplateObject(state, operation);
|
|
1294
|
+
else if (operation.kind === "regexp-literal") emitRegExpLiteral(state, operation);
|
|
1295
|
+
else if (operation.kind === "array-append" || operation.kind === "array-append-hole" || operation.kind === "array-create" || operation.kind === "delete-object-coercible" || operation.kind === "to-object" || operation.kind === "object-coercible" || operation.kind === "object-create" || operation.kind === "object-rest" || operation.kind === "object-set-prototype" || operation.kind === "object-spread" || operation.kind === "property-key" || operation.kind === "property-define-accessor" || operation.kind === "property-define-data" || operation.kind === "property-define-method" || operation.kind === "property-delete" || operation.kind === "property-get" || operation.kind === "property-set") emitObjectOperation(state, operation);
|
|
1296
|
+
else if (operation.kind === "check-status") if (operation.abruptTarget == null) line(state, renderC(emittedC.common.gotoAbruptUnlessNormal));
|
|
1297
|
+
else {
|
|
1298
|
+
const target = operation.abruptTarget.blockId;
|
|
1299
|
+
state.usesCompletion = true;
|
|
1300
|
+
line(state, renderC(emittedC.operation.ifResultStatusNotEqualOseoStatusNormalOpen));
|
|
1301
|
+
line(state, renderC(emittedC.operation.ifContextMemberHasDiagnosticGotoAbrupt));
|
|
1302
|
+
line(state, renderC(emittedC.operation.setThrowKind, target));
|
|
1303
|
+
line(state, renderC(emittedC.common.rootsUAssign, state.completionSlotStart + target) + renderC(emittedC.operation.resultValueStatement));
|
|
1304
|
+
line(state, renderC(emittedC.operation.setThrowLine, target));
|
|
1305
|
+
line(state, renderC(emittedC.operation.setThrowColumn, target));
|
|
1306
|
+
line(state, renderC(emittedC.operation.setThrowSourceId, target));
|
|
1307
|
+
line(state, renderC(emittedC.operation.setThrowSourceIdLength, target) + renderC(emittedC.common.contextMemberSourceIdLengthStatement));
|
|
1308
|
+
line(state, renderC(emittedC.operation.setThrowErrorCode, target));
|
|
1309
|
+
line(state, renderC(emittedC.operation.completionUErrorMessageAssign, target) + renderC(emittedC.operation.contextMemberErrorMessageStatement));
|
|
1310
|
+
line(state, renderC(emittedC.common.gotoBbStatement, target));
|
|
1311
|
+
line(state, renderC(emittedC.common.closeBlock));
|
|
1312
|
+
}
|
|
1313
|
+
}
|
|
1314
|
+
function emitCompletionCopy(state, source, target) {
|
|
1315
|
+
line(state, renderC(emittedC.completionCopy.completionUAssignCompletionUStatement, target, source));
|
|
1316
|
+
line(state, renderC(emittedC.common.rootsUAssign, state.completionSlotStart + target) + renderC(emittedC.completionCopy.rootsUStatement, state.completionSlotStart + source));
|
|
1317
|
+
}
|
|
1318
|
+
function emitTerminator(state, terminator) {
|
|
1319
|
+
if (terminator.kind === "return") emitNormalReturn(state, renderC(emittedC.common.root, terminator.value));
|
|
1320
|
+
else if (terminator.kind === "generator-yield") {
|
|
1321
|
+
const resultObject = terminator.resultObject === true ? renderC(emittedC.common.trueValue) : renderC(emittedC.common.falseValue);
|
|
1322
|
+
const reason = terminator.awaited === true ? renderC(emittedC.terminator.oseoGeneratorSuspendAwait) : renderC(emittedC.terminator.oseoGeneratorSuspendYield);
|
|
1323
|
+
line(state, renderC(emittedC.terminator.oseoGeneratorSuspendContextGeneratorU, terminator.resume) + renderC(emittedC.common.twoValuesCallSuffix, resultObject, reason));
|
|
1324
|
+
line(state, renderC(emittedC.common.resultAssignOseoResultOseoStatusNormal) + renderC(emittedC.terminator.rootsStatement, terminator.value));
|
|
1325
|
+
line(state, renderC(emittedC.common.returnResult));
|
|
1326
|
+
} else if (terminator.kind === "jump") {
|
|
1327
|
+
if (!state.generator && state.generatorBodyStart === terminator.target) {
|
|
1328
|
+
emitNormalReturn(state, renderC(emittedC.function.frameSlotsZero));
|
|
1329
|
+
return;
|
|
1330
|
+
}
|
|
1331
|
+
const parameters = state.blockParameters.get(terminator.target) ?? [];
|
|
1332
|
+
const values = terminator.values ?? [];
|
|
1333
|
+
if (parameters.length !== values.length) throw new Error(`MIR jump to bb${terminator.target} has ${values.length} values for ${parameters.length} parameters.`);
|
|
1334
|
+
for (let index = 0; index < parameters.length; index += 1) line(state, renderC(emittedC.common.rootAssignRoot, parameters[index], values[index]));
|
|
1335
|
+
line(state, renderC(emittedC.common.gotoBlock, terminator.target));
|
|
1336
|
+
} else if (terminator.kind === "branch") {
|
|
1337
|
+
const test = state.scalarKinds.get(terminator.test) === "boolean" ? renderC(emittedC.terminator.fast, terminator.test) : renderC(emittedC.common.oseoToBooleanRoots, terminator.test);
|
|
1338
|
+
line(state, renderC(emittedC.terminator.ifGotoBbStatement, test, terminator.whenTrue));
|
|
1339
|
+
line(state, renderC(emittedC.common.gotoBlock, terminator.whenFalse));
|
|
1340
|
+
} else if (terminator.kind === "resume-completion") {
|
|
1341
|
+
state.usesCompletion = true;
|
|
1342
|
+
state.usesAbrupt = true;
|
|
1343
|
+
const slot = terminator.completionSlot;
|
|
1344
|
+
if (terminator.outerAbrupt != null) {
|
|
1345
|
+
const target = terminator.outerAbrupt.blockId;
|
|
1346
|
+
line(state, renderC(emittedC.common.ifCompletionKindThrowOpen, slot));
|
|
1347
|
+
emitCompletionCopy(state, slot, target);
|
|
1348
|
+
line(state, renderC(emittedC.common.gotoBbStatement, target));
|
|
1349
|
+
line(state, renderC(emittedC.common.closeBlock));
|
|
1350
|
+
}
|
|
1351
|
+
if (terminator.outerFinalizer != null) {
|
|
1352
|
+
const target = terminator.outerFinalizer;
|
|
1353
|
+
line(state, renderC(emittedC.terminator.ifCompletionUKindNotEqualAddressAddress, slot) + renderC(emittedC.terminator.completionUDepthAssignUOpen, slot, target.cleanupDepth));
|
|
1354
|
+
emitCompletionCopy(state, slot, target.blockId);
|
|
1355
|
+
line(state, renderC(emittedC.common.gotoBbStatement, target.blockId));
|
|
1356
|
+
line(state, renderC(emittedC.common.closeBlock));
|
|
1357
|
+
}
|
|
1358
|
+
line(state, renderC(emittedC.terminator.ifCompletionKindReturnOpen, slot));
|
|
1359
|
+
if (state.derivedThisBindingId != null) {
|
|
1360
|
+
line(state, renderC(emittedC.common.resultAssignOseoEnvironmentGetContext) + renderC(emittedC.common.rootsUStatement, state.environmentSlot, state.derivedThisBindingId));
|
|
1361
|
+
line(state, renderC(emittedC.common.gotoAbruptUnlessNormal));
|
|
1362
|
+
line(state, renderC(emittedC.derivedReturn.resultAssignOseoDerivedConstructorResult, state.completionSlotStart + slot) + renderC(emittedC.derivedReturn.resultValueStatement));
|
|
1363
|
+
line(state, renderC(emittedC.common.gotoAbruptUnlessNormal));
|
|
1364
|
+
emitNormalReturn(state, "result.value", renderC(emittedC.common.indent));
|
|
1365
|
+
} else emitNormalReturn(state, renderC(emittedC.common.rootUnsigned, state.completionSlotStart + slot), renderC(emittedC.common.indent));
|
|
1366
|
+
line(state, renderC(emittedC.common.closeBlock));
|
|
1367
|
+
line(state, renderC(emittedC.common.ifCompletionKindThrowOpen, slot));
|
|
1368
|
+
line(state, renderC(emittedC.terminator.contextMemberLineAssignCompletionULine, slot));
|
|
1369
|
+
line(state, renderC(emittedC.terminator.contextMemberColumnAssignCompletionUColumn, slot));
|
|
1370
|
+
line(state, renderC(emittedC.terminator.contextMemberSourceIdAssignCompletionU, slot));
|
|
1371
|
+
line(state, renderC(emittedC.terminator.contextMemberSourceIdLengthAssign) + renderC(emittedC.terminator.completionUSourceIdLengthStatement, slot));
|
|
1372
|
+
line(state, renderC(emittedC.terminator.contextMemberErrorCodeAssignCompletionU, slot));
|
|
1373
|
+
line(state, renderC(emittedC.terminator.contextMemberErrorMessageAssignCompletionU, slot));
|
|
1374
|
+
line(state, renderC(emittedC.terminator.contextMemberHasDiagnosticAssignFalse));
|
|
1375
|
+
line(state, renderC(emittedC.terminator.resultAssignOseoResultOseoStatusThrow) + renderC(emittedC.common.rootUnsignedInitializerSuffix, state.completionSlotStart + slot));
|
|
1376
|
+
line(state, renderC(emittedC.terminator.gotoAbruptStatement));
|
|
1377
|
+
line(state, renderC(emittedC.common.closeBlock));
|
|
1378
|
+
line(state, renderC(emittedC.terminator.switchCompletionUTargetOpen, slot));
|
|
1379
|
+
for (const target of state.blockParameters.keys()) {
|
|
1380
|
+
if (target === 0 || !state.completionTargets.has(target)) continue;
|
|
1381
|
+
line(state, renderC(emittedC.common.caseUGotoBbStatement, target, target));
|
|
1382
|
+
}
|
|
1383
|
+
line(state, renderC(emittedC.common.defaultAbortStatement));
|
|
1384
|
+
line(state, renderC(emittedC.common.closeBlock));
|
|
1385
|
+
} else line(state, renderC(emittedC.terminator.abortStatement));
|
|
1386
|
+
}
|
|
1387
|
+
/** MIR annotations describe roots and checks but do not produce values. */
|
|
1388
|
+
function producesRootValue(operation) {
|
|
1389
|
+
return operation.kind !== "safepoint" && operation.kind !== "root-store" && operation.kind !== "check-status";
|
|
1390
|
+
}
|
|
1391
|
+
const auxiliaryValueFields = [
|
|
1392
|
+
"argumentListId",
|
|
1393
|
+
"checkedResult",
|
|
1394
|
+
"iteratorNextMethodResult",
|
|
1395
|
+
"iteratorDoneState",
|
|
1396
|
+
"iteratorCloseResultMode",
|
|
1397
|
+
"iteratorValueResult",
|
|
1398
|
+
"iteratorValueOnlyResult",
|
|
1399
|
+
"enumerateRecordResult",
|
|
1400
|
+
"enumerateKeyResult"
|
|
1401
|
+
];
|
|
1402
|
+
/** Values requiring storage, including hidden iterator and suspension state. */
|
|
1403
|
+
function rootValues(block) {
|
|
1404
|
+
const values = [...block.parameters ?? []];
|
|
1405
|
+
for (const operation of block.operations) {
|
|
1406
|
+
if (producesRootValue(operation)) values.push(operation.id);
|
|
1407
|
+
for (const argument of operation.arguments) values.push(argument);
|
|
1408
|
+
for (const field of auxiliaryValueFields) {
|
|
1409
|
+
const value = operation[field];
|
|
1410
|
+
if (value != null) values.push(value);
|
|
1411
|
+
}
|
|
1412
|
+
}
|
|
1413
|
+
const terminator = block.terminator;
|
|
1414
|
+
if (terminator.kind === "return") values.push(terminator.value);
|
|
1415
|
+
if (terminator.kind === "branch") values.push(terminator.test);
|
|
1416
|
+
if (terminator.kind === "jump") for (const value of terminator.values ?? []) values.push(value);
|
|
1417
|
+
if (terminator.kind === "generator-yield") values.push(terminator.sent, terminator.value);
|
|
1418
|
+
return values;
|
|
1419
|
+
}
|
|
1420
|
+
/**
|
|
1421
|
+
* Assign dense C storage identities without changing the public SSA MIR.
|
|
1422
|
+
* Each value still owns a distinct slot for the entire frame lifetime;
|
|
1423
|
+
* only annotation IDs cease to consume storage. In particular, this does
|
|
1424
|
+
* not reuse live roots or change iterator state across a suspension.
|
|
1425
|
+
*/
|
|
1426
|
+
function compactRootIds(functionValue) {
|
|
1427
|
+
const blocks = reachableBlocks(functionValue);
|
|
1428
|
+
const values = [...new Set(blocks.flatMap(rootValues))].toSorted((left, right) => left - right);
|
|
1429
|
+
const slots = new Map(values.map((value, slot) => [value, slot]));
|
|
1430
|
+
const slot = (value) => {
|
|
1431
|
+
const result = slots.get(value);
|
|
1432
|
+
if (result == null) throw new Error(`MIR value %${value} has no root slot.`);
|
|
1433
|
+
return result;
|
|
1434
|
+
};
|
|
1435
|
+
let originalMaximum = values.at(-1) ?? -1;
|
|
1436
|
+
for (const block of blocks) for (const operation of block.operations) originalMaximum = Math.max(originalMaximum, operation.id);
|
|
1437
|
+
const originalCount = originalMaximum + 1;
|
|
1438
|
+
const reserved = Math.max(0, functionValue.rootSlotCount - originalCount);
|
|
1439
|
+
return {
|
|
1440
|
+
...functionValue,
|
|
1441
|
+
rootSlotCount: values.length + reserved,
|
|
1442
|
+
blocks: blocks.map((block) => {
|
|
1443
|
+
const terminator = block.terminator;
|
|
1444
|
+
let mappedTerminator = terminator;
|
|
1445
|
+
if (terminator.kind === "return") mappedTerminator = {
|
|
1446
|
+
...terminator,
|
|
1447
|
+
value: slot(terminator.value)
|
|
1448
|
+
};
|
|
1449
|
+
else if (terminator.kind === "branch") mappedTerminator = {
|
|
1450
|
+
...terminator,
|
|
1451
|
+
test: slot(terminator.test)
|
|
1452
|
+
};
|
|
1453
|
+
else if (terminator.kind === "jump" && terminator.values != null) mappedTerminator = {
|
|
1454
|
+
...terminator,
|
|
1455
|
+
values: terminator.values.map(slot)
|
|
1456
|
+
};
|
|
1457
|
+
else if (terminator.kind === "generator-yield") mappedTerminator = {
|
|
1458
|
+
...terminator,
|
|
1459
|
+
sent: slot(terminator.sent),
|
|
1460
|
+
value: slot(terminator.value)
|
|
1461
|
+
};
|
|
1462
|
+
return {
|
|
1463
|
+
...block,
|
|
1464
|
+
...includePropertiesWhen(() => block.parameters == null ? void 0 : { parameters: block.parameters.map(slot) }),
|
|
1465
|
+
terminator: mappedTerminator,
|
|
1466
|
+
operations: block.operations.map((operation) => {
|
|
1467
|
+
const auxiliary = {};
|
|
1468
|
+
for (const field of auxiliaryValueFields) {
|
|
1469
|
+
const value = operation[field];
|
|
1470
|
+
if (value != null) auxiliary[field] = slot(value);
|
|
1471
|
+
}
|
|
1472
|
+
return {
|
|
1473
|
+
...operation,
|
|
1474
|
+
...auxiliary,
|
|
1475
|
+
id: producesRootValue(operation) ? slot(operation.id) : operation.id,
|
|
1476
|
+
arguments: operation.arguments.map(slot)
|
|
1477
|
+
};
|
|
1478
|
+
})
|
|
1479
|
+
};
|
|
1480
|
+
})
|
|
1481
|
+
};
|
|
1482
|
+
}
|
|
1483
|
+
function maximumValueId(blocks) {
|
|
1484
|
+
let maximum = -1;
|
|
1485
|
+
for (const block of blocks) for (const value of rootValues(block)) maximum = Math.max(maximum, value);
|
|
1486
|
+
return maximum;
|
|
1487
|
+
}
|
|
1488
|
+
function bindingIds(functionValue, blocks, globalBindings) {
|
|
1489
|
+
const ids = new Set(functionValue.parameters.map((parameter) => parameter.bindingId));
|
|
1490
|
+
for (const block of blocks) for (const operation of block.operations) if (operation.bindingId != null) ids.add(operation.bindingId);
|
|
1491
|
+
return [...ids].filter((id) => !globalBindings.has(id)).toSorted((left, right) => left - right);
|
|
1492
|
+
}
|
|
1493
|
+
function maximumArgumentCount(blocks) {
|
|
1494
|
+
let maximum = 0;
|
|
1495
|
+
for (const block of blocks) for (const operation of block.operations) if (operation.kind === "call" || operation.kind === "construct") {
|
|
1496
|
+
if (operation.argumentListId != null) continue;
|
|
1497
|
+
maximum = Math.max(maximum, operation.arguments.length);
|
|
1498
|
+
}
|
|
1499
|
+
return maximum;
|
|
1500
|
+
}
|
|
1501
|
+
function completionSlotCount(blocks) {
|
|
1502
|
+
if (!blocks.some((block) => block.terminator.kind === "resume-completion" || block.operations.some((operation) => operation.kind === "caught" || operation.kind === "completion-set" || operation.kind === "iterator-close" && operation.completionSlot != null || operation.kind === "iterator-close-start" && operation.completionSlot != null || (operation.kind === "dispose-scope-dispose" || operation.kind === "dispose-scope-begin") && operation.completionSlot != null || operation.kind === "check-status" && operation.abruptTarget != null))) return 0;
|
|
1503
|
+
let count = 1;
|
|
1504
|
+
for (const block of blocks) count = Math.max(count, block.id + 1);
|
|
1505
|
+
return count;
|
|
1506
|
+
}
|
|
1507
|
+
function rootCount(functionValue) {
|
|
1508
|
+
const blocks = reachableBlocks(functionValue);
|
|
1509
|
+
const valueSlotCount = maximumValueId(blocks) + 1;
|
|
1510
|
+
return Math.max(functionValue.rootSlotCount, valueSlotCount + 2, 32) + maximumArgumentCount(blocks) + completionSlotCount(blocks) + (needsThisBinding(functionValue) && !functionValue.generator ? 1 : 0);
|
|
1511
|
+
}
|
|
1512
|
+
/** Script this is separate; lexical receivers arrive captured by [[Call]]. */
|
|
1513
|
+
function needsThisBinding(functionValue) {
|
|
1514
|
+
return functionValue.id >= 0 && functionValue.strict !== true;
|
|
1515
|
+
}
|
|
1516
|
+
/**
|
|
1517
|
+
* Keep a script's native-stack charge separate from its heap root layout.
|
|
1518
|
+
* Binding operations emit two result-returning calls, for cell lookup or
|
|
1519
|
+
* creation and the binding access. Retain two extra units per access for their
|
|
1520
|
+
* C expression temporaries, restoring the logical declaration charge without
|
|
1521
|
+
* charging array-literal annotations or sparse SSA gaps as native storage.
|
|
1522
|
+
*/
|
|
1523
|
+
function scriptFrameCost(functionValue) {
|
|
1524
|
+
const compact = compactRootIds(functionValue);
|
|
1525
|
+
let bindingAccesses = 0;
|
|
1526
|
+
for (const block of reachableBlocks(functionValue)) for (const operation of block.operations) if (operation.kind === "initialize" || operation.kind === "read" || operation.kind === "write" || operation.kind === "binding-reset") bindingAccesses += 1;
|
|
1527
|
+
return rootCount(compact) + 2 * bindingAccesses;
|
|
1528
|
+
}
|
|
1529
|
+
/**
|
|
1530
|
+
* Blocks of `functionValue` that C control can enter from `start`,
|
|
1531
|
+
* without crossing `excluded`.
|
|
1532
|
+
*
|
|
1533
|
+
* A completion target is entered only through a `resume-completion`
|
|
1534
|
+
* terminator, whose switch names every completion target emitted for the
|
|
1535
|
+
* same body.
|
|
1536
|
+
* Targets are therefore deferred until such a terminator is reachable;
|
|
1537
|
+
* targets found before or after it join the fixed point together. When
|
|
1538
|
+
* every `finally` in the body leaves abruptly, no dispatch exists, and
|
|
1539
|
+
* emitting the targets would leave labels that no `goto` names.
|
|
1540
|
+
*
|
|
1541
|
+
* Block 0 as a target is the sentinel that a `return`, `throw`, or normal
|
|
1542
|
+
* completion without a jump destination saves (`enterFinalizer`); the
|
|
1543
|
+
* dispatch switch never names it. It is not deferred, so a generator body
|
|
1544
|
+
* that starts after the call-time parameter prologue does not take in the
|
|
1545
|
+
* prologue's entry block.
|
|
1546
|
+
*/
|
|
1547
|
+
function reachableBlocksFrom(functionValue, start, excluded = /* @__PURE__ */ new Set()) {
|
|
1548
|
+
const blocks = new Map(functionValue.blocks.map((block) => [block.id, block]));
|
|
1549
|
+
const pending = [start];
|
|
1550
|
+
const reachable = /* @__PURE__ */ new Set();
|
|
1551
|
+
const completionTargets = [];
|
|
1552
|
+
let resumesCompletion = false;
|
|
1553
|
+
while (pending.length > 0 || resumesCompletion && completionTargets.length > 0) {
|
|
1554
|
+
if (pending.length === 0) pending.push(...completionTargets.splice(0));
|
|
1555
|
+
const id = pending.pop();
|
|
1556
|
+
if (id == null || reachable.has(id) || excluded.has(id)) continue;
|
|
1557
|
+
const block = blocks.get(id);
|
|
1558
|
+
if (block == null) throw new Error(`MIR function '${functionValue.name}' has no block bb${id}.`);
|
|
1559
|
+
reachable.add(id);
|
|
1560
|
+
if (block.terminator.kind === "jump") pending.push(block.terminator.target);
|
|
1561
|
+
else if (block.terminator.kind === "generator-yield") {
|
|
1562
|
+
pending.push(block.terminator.resume);
|
|
1563
|
+
if (block.terminator.returnResume != null) pending.push(block.terminator.returnResume);
|
|
1564
|
+
if (block.terminator.throwResume != null) pending.push(block.terminator.throwResume);
|
|
1565
|
+
} else if (block.terminator.kind === "branch") pending.push(block.terminator.whenFalse, block.terminator.whenTrue);
|
|
1566
|
+
else if (block.terminator.kind === "resume-completion") {
|
|
1567
|
+
resumesCompletion = true;
|
|
1568
|
+
if (block.terminator.outerAbrupt != null) pending.push(block.terminator.outerAbrupt.blockId);
|
|
1569
|
+
if (block.terminator.outerFinalizer != null) pending.push(block.terminator.outerFinalizer.blockId);
|
|
1570
|
+
}
|
|
1571
|
+
for (const operation of block.operations) {
|
|
1572
|
+
if (operation.abruptTarget != null) pending.push(operation.abruptTarget.blockId);
|
|
1573
|
+
if (operation.completionTarget != null && operation.completionTarget.blockId !== 0) completionTargets.push(operation.completionTarget.blockId);
|
|
1574
|
+
}
|
|
1575
|
+
}
|
|
1576
|
+
return functionValue.blocks.filter((block) => reachable.has(block.id));
|
|
1577
|
+
}
|
|
1578
|
+
/** Every block a `completion-set` of `functionValue` names as its target. */
|
|
1579
|
+
function savedCompletionTargets(functionValue) {
|
|
1580
|
+
const targets = /* @__PURE__ */ new Set();
|
|
1581
|
+
for (const block of functionValue.blocks) for (const operation of block.operations) if (operation.completionTarget != null) targets.add(operation.completionTarget.blockId);
|
|
1582
|
+
return targets;
|
|
1583
|
+
}
|
|
1584
|
+
function reachableBlocks(functionValue) {
|
|
1585
|
+
return reachableBlocksFrom(functionValue, 0);
|
|
1586
|
+
}
|
|
1587
|
+
function calledFunctionIds(functionValue) {
|
|
1588
|
+
return reachableBlocks(functionValue).flatMap((block) => block.operations.flatMap((operation) => operation.kind === "function-create" && operation.functionId != null ? [operation.functionId] : operation.kind === "call" && operation.target?.kind === "function" ? [operation.target.functionId] : []));
|
|
1589
|
+
}
|
|
1590
|
+
function reachableFunctions(input) {
|
|
1591
|
+
const functions = new Map(input.functions.map((functionValue) => [functionValue.id, functionValue]));
|
|
1592
|
+
const pending = [...calledFunctionIds(input.script), ...(input.test262Host?.scripts ?? []).flatMap((entry) => entry.functionId == null ? [] : [entry.functionId])];
|
|
1593
|
+
const reachable = /* @__PURE__ */ new Set();
|
|
1594
|
+
while (pending.length > 0) {
|
|
1595
|
+
const id = pending.pop();
|
|
1596
|
+
if (id == null || reachable.has(id)) continue;
|
|
1597
|
+
const functionValue = functions.get(id);
|
|
1598
|
+
if (functionValue == null) throw new Error(`MIR has no declared function @f${id}.`);
|
|
1599
|
+
reachable.add(id);
|
|
1600
|
+
pending.push(...calledFunctionIds(functionValue));
|
|
1601
|
+
}
|
|
1602
|
+
return input.functions.filter((functionValue) => reachable.has(functionValue.id));
|
|
1603
|
+
}
|
|
1604
|
+
function hasSelfCall(functionValue) {
|
|
1605
|
+
return calledFunctionIds(functionValue).includes(functionValue.id);
|
|
1606
|
+
}
|
|
1607
|
+
/** Resume block identifiers paired with their resumption continuations. */
|
|
1608
|
+
function yieldResumePoints(blocks) {
|
|
1609
|
+
const points = /* @__PURE__ */ new Map();
|
|
1610
|
+
for (const block of blocks) {
|
|
1611
|
+
if (block.terminator.kind !== "generator-yield") continue;
|
|
1612
|
+
const terminator = block.terminator;
|
|
1613
|
+
points.set(terminator.resume, {
|
|
1614
|
+
...includePropertiesWhen(() => {
|
|
1615
|
+
if (terminator.returnResume == null) return void 0;
|
|
1616
|
+
return { returnResume: terminator.returnResume };
|
|
1617
|
+
}),
|
|
1618
|
+
sent: terminator.sent,
|
|
1619
|
+
...includePropertiesWhen(() => {
|
|
1620
|
+
if (terminator.throwResume == null) return void 0;
|
|
1621
|
+
return { throwResume: terminator.throwResume };
|
|
1622
|
+
})
|
|
1623
|
+
});
|
|
1624
|
+
}
|
|
1625
|
+
return points;
|
|
1626
|
+
}
|
|
1627
|
+
/** Emit one static UTF-16 name table used by the global record prologue. */
|
|
1628
|
+
function emitGlobalNameTable(state, prefix, entries) {
|
|
1629
|
+
if (entries.length === 0) {
|
|
1630
|
+
const nullPointer = renderC(emittedC.common.nullPointer);
|
|
1631
|
+
return {
|
|
1632
|
+
columns: nullPointer,
|
|
1633
|
+
lengths: nullPointer,
|
|
1634
|
+
lines: nullPointer,
|
|
1635
|
+
names: nullPointer
|
|
1636
|
+
};
|
|
1637
|
+
}
|
|
1638
|
+
const pointers = [];
|
|
1639
|
+
const lengths = [];
|
|
1640
|
+
for (const [index, entry] of entries.entries()) {
|
|
1641
|
+
const name = entry.name;
|
|
1642
|
+
const units = utf16Units(name);
|
|
1643
|
+
lengths.push(units.length);
|
|
1644
|
+
if (units.length === 0) {
|
|
1645
|
+
pointers.push(renderC(emittedC.common.nullPointer));
|
|
1646
|
+
continue;
|
|
1647
|
+
}
|
|
1648
|
+
const constantName = `${prefix}_units_${index}`;
|
|
1649
|
+
line(state, renderC(emittedC.common.staticConstUint16TAssignStatement, constantName, units.join(renderC(emittedC.common.commaSpace))));
|
|
1650
|
+
pointers.push(constantName);
|
|
1651
|
+
}
|
|
1652
|
+
line(state, renderC(emittedC.moduleNamespace.staticConstUint16TPointerConstNamesAssign, prefix) + renderC(emittedC.common.bracedInitializer, pointers.join(renderC(emittedC.common.commaSpace))));
|
|
1653
|
+
line(state, renderC(emittedC.moduleNamespace.staticConstSizeTLengthsAssignStatement, prefix, lengths.join(renderC(emittedC.common.commaSpace))));
|
|
1654
|
+
line(state, `static const size_t ${prefix}_lines[] = {${entries.map((entry) => entry.range.start.line).join(", ")}};`);
|
|
1655
|
+
line(state, `static const size_t ${prefix}_columns[] = {${entries.map((entry) => entry.range.start.column).join(", ")}};`);
|
|
1656
|
+
return {
|
|
1657
|
+
columns: `${prefix}_columns`,
|
|
1658
|
+
lengths: `${prefix}_lengths`,
|
|
1659
|
+
lines: `${prefix}_lines`,
|
|
1660
|
+
names: `${prefix}_names`
|
|
1661
|
+
};
|
|
1662
|
+
}
|
|
1663
|
+
/**
|
|
1664
|
+
* Validate and instantiate one Script's global lexical and var-scoped names.
|
|
1665
|
+
*/
|
|
1666
|
+
function emitGlobalObject(state, lexicalNames, bindings, hostScript = false) {
|
|
1667
|
+
if (lexicalNames.length === 0 && bindings.length === 0) return;
|
|
1668
|
+
const lexical = emitGlobalNameTable(state, "global_lexical", lexicalNames);
|
|
1669
|
+
const bindingNames = emitGlobalNameTable(state, renderC(emittedC.globalObject.prefix), bindings);
|
|
1670
|
+
const nullPointer = renderC(emittedC.common.nullPointer);
|
|
1671
|
+
let globalBindingIds = nullPointer;
|
|
1672
|
+
let functionDeclarations = nullPointer;
|
|
1673
|
+
let webLegacyFunctions = nullPointer;
|
|
1674
|
+
if (bindings.length > 0) {
|
|
1675
|
+
const prefix = renderC(emittedC.globalObject.prefix);
|
|
1676
|
+
globalBindingIds = `${prefix}_bindings`;
|
|
1677
|
+
functionDeclarations = `${prefix}_functions`;
|
|
1678
|
+
line(state, renderC(emittedC.moduleNamespace.staticConstSizeTBindingsAssign, prefix) + renderC(emittedC.common.bracedInitializer, bindings.map((binding) => binding.id).join(renderC(emittedC.common.commaSpace))));
|
|
1679
|
+
line(state, `static const bool ${functionDeclarations}[] = ` + renderC(emittedC.common.bracedInitializer, bindings.map((binding) => binding.declaration === "function" ? "true" : "false").join(renderC(emittedC.common.commaSpace))));
|
|
1680
|
+
if (bindings.some((binding) => binding.declaration === "annexb")) {
|
|
1681
|
+
webLegacyFunctions = `${prefix}_web_legacy`;
|
|
1682
|
+
line(state, `static const bool ${webLegacyFunctions}[] = ` + renderC(emittedC.common.bracedInitializer, bindings.map((binding) => binding.declaration === "annexb" ? "true" : "false").join(renderC(emittedC.common.commaSpace))));
|
|
1683
|
+
}
|
|
1684
|
+
}
|
|
1685
|
+
line(state, `result = oseo_global_object_create(context, roots[${state.environmentSlot}], ${lexicalNames.length}u, ${lexical.names}, ${lexical.lengths}, ${lexical.lines}, ${lexical.columns}, ${bindings.length}u, ${bindingNames.names}, ${bindingNames.lengths}, ${bindingNames.lines}, ${bindingNames.columns}, ${globalBindingIds}, ${functionDeclarations}, ${webLegacyFunctions});`);
|
|
1686
|
+
line(state, renderC(emittedC.common.gotoAbruptUnlessNormal));
|
|
1687
|
+
if (hostScript && lexicalNames.length > 0) {
|
|
1688
|
+
line(state, "static const size_t lexical_ids[] = {" + lexicalNames.map((entry) => `${entry.id}u`).join(", ") + "};");
|
|
1689
|
+
line(state, "static const bool lexical_mutable[] = {" + lexicalNames.map((entry) => String(entry.mutable)).join(", ") + "};");
|
|
1690
|
+
line(state, `result = oseo_script_lexicals_install(context, roots[${state.environmentSlot}], ${lexicalNames.length}u, ${lexical.names}, ${lexical.lengths}, lexical_ids, lexical_mutable);`);
|
|
1691
|
+
line(state, renderC(emittedC.common.gotoAbruptUnlessNormal));
|
|
1692
|
+
}
|
|
1693
|
+
}
|
|
1694
|
+
/**
|
|
1695
|
+
* The mapped arguments exotic object aliases each supplied index that is
|
|
1696
|
+
* the rightmost formal parameter of its name to that parameter's own
|
|
1697
|
+
* binding cell, so a later mutation through either the index or the
|
|
1698
|
+
* parameter observes the other. A duplicate name's earlier occurrence,
|
|
1699
|
+
* having no index of its own left to alias, keeps the ordinary snapshot
|
|
1700
|
+
* value the runtime otherwise gives every unmapped or excess index.
|
|
1701
|
+
*/
|
|
1702
|
+
function emitMappedArgumentsCreate(state, functionValue, environmentSlot) {
|
|
1703
|
+
const parameters = functionValue.parameters;
|
|
1704
|
+
const mapped = parameters.map((parameter, index) => ({
|
|
1705
|
+
bindingId: parameter.bindingId,
|
|
1706
|
+
index
|
|
1707
|
+
})).filter(({ bindingId }, position) => parameters.findLastIndex((p) => p.bindingId === bindingId) === position);
|
|
1708
|
+
let indicesExpression = renderC(emittedC.common.nullPointer);
|
|
1709
|
+
let bindingIdsExpression = renderC(emittedC.common.nullPointer);
|
|
1710
|
+
if (mapped.length > 0) {
|
|
1711
|
+
indicesExpression = renderC(emittedC.mappedArguments.indicesName, functionValue.id);
|
|
1712
|
+
bindingIdsExpression = renderC(emittedC.mappedArguments.bindingIdsName, functionValue.id);
|
|
1713
|
+
line(state, renderC(emittedC.common.staticConstSizeTArrayAssignStatement, indicesExpression, mapped.map(({ index }) => `${index}u`).join(renderC(emittedC.common.commaSpace))));
|
|
1714
|
+
line(state, renderC(emittedC.common.staticConstSizeTArrayAssignStatement, bindingIdsExpression, mapped.map(({ bindingId }) => `${bindingId}u`).join(renderC(emittedC.common.commaSpace))));
|
|
1715
|
+
}
|
|
1716
|
+
line(state, renderC(emittedC.prologue.resultAssignOseoMappedArgumentsCreate, environmentSlot, indicesExpression, bindingIdsExpression, mapped.length));
|
|
1717
|
+
}
|
|
1718
|
+
/**
|
|
1719
|
+
* The ReferenceError message one unresolvable global reference reports.
|
|
1720
|
+
* The runtime renders a diagnostic message through an ASCII string, so a
|
|
1721
|
+
* name outside that range keeps the general wording rather than a
|
|
1722
|
+
* mis-decoded identifier.
|
|
1723
|
+
*/
|
|
1724
|
+
function unresolvableReferenceMessage(name) {
|
|
1725
|
+
return /^[\u0020-\u007e]+$/u.test(name) ? `${name} is not defined.` : "Reference is not defined.";
|
|
1726
|
+
}
|
|
1727
|
+
function emitPrologue(state, functionValue, bindingIdValues, totalBindingCount, temporarySlot, unresolvableGlobals, globalLexicalNames, globalObjectBindings) {
|
|
1728
|
+
const environmentSlot = state.environmentSlot;
|
|
1729
|
+
if (functionValue.id < 0 && state.fragmentUnit != null) {
|
|
1730
|
+
line(state, `roots[${environmentSlot}] = callee;`);
|
|
1731
|
+
return;
|
|
1732
|
+
}
|
|
1733
|
+
if (functionValue.id < 0 || functionValue.hostScript != null) line(state, renderC(emittedC.prologue.resultAssignOseoEnvironmentCreateContextU, totalBindingCount));
|
|
1734
|
+
else {
|
|
1735
|
+
line(state, renderC(emittedC.prologue.resultAssignOseoFunctionEnvironmentContext));
|
|
1736
|
+
line(state, renderC(emittedC.common.gotoAbruptUnlessNormal));
|
|
1737
|
+
line(state, renderC(emittedC.prologue.resultAssignOseoEnvironmentCloneContext));
|
|
1738
|
+
}
|
|
1739
|
+
line(state, renderC(emittedC.common.rootAssignResultValue, environmentSlot));
|
|
1740
|
+
line(state, renderC(emittedC.common.gotoAbruptUnlessNormal));
|
|
1741
|
+
for (const bindingId of bindingIdValues) {
|
|
1742
|
+
const unresolvable = unresolvableGlobals.get(bindingId);
|
|
1743
|
+
line(state, unresolvable == null ? renderC(emittedC.common.resultAssignOseoCellCreateContextOseo) : renderC(emittedC.prologue.resultAssignOseoUnresolvableCellCreate, escapeCString(unresolvableReferenceMessage(unresolvable))));
|
|
1744
|
+
line(state, renderC(emittedC.common.rootAssignResultValue, temporarySlot));
|
|
1745
|
+
line(state, renderC(emittedC.common.gotoAbruptUnlessNormal));
|
|
1746
|
+
line(state, renderC(emittedC.prologue.resultAssignOseoEnvironmentSetContextRoots, environmentSlot) + renderC(emittedC.prologue.uRootsStatement, bindingId, temporarySlot));
|
|
1747
|
+
line(state, renderC(emittedC.common.gotoAbruptUnlessNormal));
|
|
1748
|
+
}
|
|
1749
|
+
if (functionValue.id < 0 || functionValue.hostScript != null) emitGlobalObject(state, functionValue.hostScript?.lexical ?? globalLexicalNames, functionValue.hostScript?.objects ?? globalObjectBindings, functionValue.hostScript != null);
|
|
1750
|
+
if (functionValue.selfBindingId != null) {
|
|
1751
|
+
line(state, renderC(emittedC.common.resultAssignOseoEnvironmentGetContextRoots, environmentSlot) + renderC(emittedC.common.positionSuffix, functionValue.selfBindingId));
|
|
1752
|
+
line(state, renderC(emittedC.common.gotoAbruptUnlessNormal));
|
|
1753
|
+
line(state, renderC(emittedC.prologue.resultAssignOseoCellInitializeContext));
|
|
1754
|
+
line(state, renderC(emittedC.common.gotoAbruptUnlessNormal));
|
|
1755
|
+
}
|
|
1756
|
+
if (functionValue.argumentsBindingId != null) {
|
|
1757
|
+
line(state, renderC(emittedC.common.resultAssignOseoEnvironmentGetContextRoots, environmentSlot) + renderC(emittedC.common.positionSuffix, functionValue.argumentsBindingId));
|
|
1758
|
+
line(state, renderC(emittedC.common.gotoAbruptUnlessNormal));
|
|
1759
|
+
line(state, renderC(emittedC.common.rootAssignResultValue, temporarySlot));
|
|
1760
|
+
if (functionValue.argumentsMapped === true) emitMappedArgumentsCreate(state, functionValue, environmentSlot);
|
|
1761
|
+
else line(state, renderC(emittedC.prologue.resultAssignOseoArgumentsCreate));
|
|
1762
|
+
line(state, renderC(emittedC.common.gotoAbruptUnlessNormal));
|
|
1763
|
+
line(state, renderC(emittedC.prologue.resultAssignContextRootsResultValue, renderC(emittedC.prologue.oseoCellInitialize), temporarySlot));
|
|
1764
|
+
line(state, renderC(emittedC.common.gotoAbruptUnlessNormal));
|
|
1765
|
+
}
|
|
1766
|
+
const parameters = functionValue.parameters;
|
|
1767
|
+
const initializedParameters = /* @__PURE__ */ new Set();
|
|
1768
|
+
for (let index = 0; index < parameters.length; index += 1) {
|
|
1769
|
+
const parameter = parameters[index];
|
|
1770
|
+
if (parameter == null) continue;
|
|
1771
|
+
line(state, renderC(emittedC.common.resultAssignOseoEnvironmentGetContextRoots, environmentSlot) + renderC(emittedC.common.positionSuffix, parameter.bindingId));
|
|
1772
|
+
line(state, renderC(emittedC.common.gotoAbruptUnlessNormal));
|
|
1773
|
+
const setter = initializedParameters.has(parameter.bindingId) ? renderC(emittedC.prologue.oseoCellSet) : renderC(emittedC.prologue.oseoCellInitialize);
|
|
1774
|
+
if (parameter.rest === true) {
|
|
1775
|
+
line(state, renderC(emittedC.common.rootAssignResultValue, temporarySlot));
|
|
1776
|
+
line(state, renderC(emittedC.prologue.resultAssignOseoArrayCreateContextU));
|
|
1777
|
+
line(state, renderC(emittedC.common.gotoAbruptUnlessNormal));
|
|
1778
|
+
line(state, renderC(emittedC.prologue.resultAssignContextRootsResultValue, setter, temporarySlot));
|
|
1779
|
+
line(state, renderC(emittedC.common.gotoAbruptUnlessNormal));
|
|
1780
|
+
line(state, renderC(emittedC.prologue.forSizeTRestIndexAssignU, index, index) + renderC(emittedC.prologue.restIndexArgumentCountRestIndexPlusAssignU, index, index));
|
|
1781
|
+
line(state, renderC(emittedC.prologue.resultAssignOseoArrayAppendContextResult) + renderC(emittedC.prologue.argumentsRestIndexStatement, index));
|
|
1782
|
+
line(state, renderC(emittedC.prologue.ifResultStatusNotEqualOseoStatusNormalGoto));
|
|
1783
|
+
line(state, renderC(emittedC.common.closeBlock));
|
|
1784
|
+
initializedParameters.add(parameter.bindingId);
|
|
1785
|
+
continue;
|
|
1786
|
+
}
|
|
1787
|
+
line(state, renderC(emittedC.prologue.resultAssignContextResultValue, setter) + renderC(emittedC.prologue.argumentCountUArguments, index, index) + renderC(emittedC.prologue.undefinedDefaultArgumentCallSuffix));
|
|
1788
|
+
line(state, renderC(emittedC.common.gotoAbruptUnlessNormal));
|
|
1789
|
+
initializedParameters.add(parameter.bindingId);
|
|
1790
|
+
}
|
|
1791
|
+
}
|
|
1792
|
+
function emitBlocks(state, blocks, resumePoints) {
|
|
1793
|
+
for (const block of blocks) {
|
|
1794
|
+
if (block.id !== 0 || state.generator) line(state, renderC(emittedC.blocks.bbStatement, block.id));
|
|
1795
|
+
const resume = resumePoints.get(block.id);
|
|
1796
|
+
if (resume != null) {
|
|
1797
|
+
line(state, renderC(emittedC.blocks.rootsAssignOseoGeneratorSentGenerator, resume.sent));
|
|
1798
|
+
if (resume.returnResume != null) line(state, renderC(emittedC.common.ifOseoGeneratorResumeKindGeneratorEqual) + renderC(emittedC.blocks.oseoGeneratorResumeReturnGotoBbStatement, resume.returnResume));
|
|
1799
|
+
if (resume.throwResume != null) line(state, renderC(emittedC.common.ifOseoGeneratorResumeKindGeneratorEqual) + renderC(emittedC.blocks.oseoGeneratorResumeThrowGotoBbStatement, resume.throwResume));
|
|
1800
|
+
}
|
|
1801
|
+
for (const operation of block.operations) emitOperation(state, operation);
|
|
1802
|
+
emitTerminator(state, block.terminator);
|
|
1803
|
+
}
|
|
1804
|
+
}
|
|
1805
|
+
function completionDeclaration(state, completionSlots) {
|
|
1806
|
+
if (!state.usesCompletion) return renderC(emittedC.common.empty);
|
|
1807
|
+
if (state.generator) return renderC(emittedC.completionDeclaration.oseoCompletionRecordPointerCompletion) + renderC(emittedC.completionDeclaration.oseoGeneratorCompletionsGeneratorLine) + renderC(emittedC.common.voidCompletionLine);
|
|
1808
|
+
return renderC(emittedC.completionDeclaration.oseoCompletionRecordCompletionUAssign, completionSlots) + renderC(emittedC.completionDeclaration.uUUUNullUNullNullLine) + renderC(emittedC.common.voidCompletionLine);
|
|
1809
|
+
}
|
|
1810
|
+
/** The C identifier holding one MIR function's generated body code. */
|
|
1811
|
+
function generatorBodyName(functionValue) {
|
|
1812
|
+
return renderC(emittedC.generatorBodyName.oseoGeneratorBody, functionValue.id);
|
|
1813
|
+
}
|
|
1814
|
+
/** Discard impossible bodies before unoptimized sanitizer IR construction. */
|
|
1815
|
+
function nativeFrameGuard(frameCost, unusedParameters) {
|
|
1816
|
+
return `#if ${frameCost} > OSEO_MAX_ACTIVE_FRAME_SLOTS
|
|
1817
|
+
${unusedParameters.map((name) => ` (void)${name};`).join("\n")}
|
|
1818
|
+
return oseo_frame_enter(context, OSEO_MAX_ACTIVE_FRAME_SLOTS + 1u);
|
|
1819
|
+
#else
|
|
1820
|
+
`;
|
|
1821
|
+
}
|
|
1822
|
+
function emitGeneratorBody(functionValue, blocks, completionSlots, base, frameCost, units) {
|
|
1823
|
+
const state = {
|
|
1824
|
+
...base,
|
|
1825
|
+
blockParameters: new Map(blocks.map((block) => [block.id, block.parameters ?? []])),
|
|
1826
|
+
generator: true,
|
|
1827
|
+
lines: [],
|
|
1828
|
+
scalarKinds: /* @__PURE__ */ new Map()
|
|
1829
|
+
};
|
|
1830
|
+
const resumePoints = yieldResumePoints(blocks);
|
|
1831
|
+
line(state, renderC(emittedC.generatorBody.switchOseoGeneratorResumePointGenerator));
|
|
1832
|
+
const bodyStart = functionValue.generatorBodyStart ?? 0;
|
|
1833
|
+
line(state, renderC(emittedC.common.caseUGotoBbStatement, 0, bodyStart));
|
|
1834
|
+
for (const resume of resumePoints.keys()) line(state, renderC(emittedC.common.caseUGotoBbStatement, resume, resume));
|
|
1835
|
+
line(state, renderC(emittedC.common.defaultAbortStatement));
|
|
1836
|
+
line(state, renderC(emittedC.common.closeBlock));
|
|
1837
|
+
emitBlocks(state, blocks, resumePoints);
|
|
1838
|
+
if (state.usesAbrupt) {
|
|
1839
|
+
line(state, renderC(emittedC.common.abruptLabel));
|
|
1840
|
+
line(state, renderC(emittedC.common.returnResult));
|
|
1841
|
+
}
|
|
1842
|
+
const ownerUnit = units?.assign(4096) ?? 0;
|
|
1843
|
+
const bounded = boundBody(state.lines, generatorBodyName(functionValue), frameCost, true, state.usesCompletion, units, ownerUnit);
|
|
1844
|
+
const body = renderC(emittedC.generatorBody.staticOseoResultLine, generatorBodyName(functionValue)) + renderC(emittedC.common.oseoContextPointerContextLine) + renderC(emittedC.generatorBody.oseoValueGeneratorLine) + renderC(emittedC.common.functionBodyOpenLine) + nativeFrameGuard(frameCost, ["generator"]) + renderC(emittedC.generatorBody.oseoValuePointerRootsAssignOseoGenerator) + renderC(emittedC.generatorBody.oseoValueCalleeAssignOseoGeneratorCallee) + renderC(emittedC.generatorBody.oseoValueReceiverAssignOseoGenerator) + renderC(emittedC.generatorBody.oseoResultResultAssignOseoStatusNormalOseo) + completionDeclaration(state, completionSlots) + renderC(emittedC.generatorBody.voidContextLine) + renderC(emittedC.common.voidCalleeLine) + renderC(emittedC.common.voidReceiverLine) + renderC(emittedC.common.valueThenNewline, bounded?.execution ?? state.lines.join(renderC(emittedC.common.newline))) + "#endif\n" + renderC(emittedC.common.closeBlockLine);
|
|
1845
|
+
return (bounded?.declarations ?? "") + (units == null ? body : bodyUnit(body, ownerUnit));
|
|
1846
|
+
}
|
|
1847
|
+
function emitFunction(functionValue, functionFrameCosts, totalBindingCount, observeSpecialization, unresolvableGlobals, globalLexicalNames, globalObjectBindings, fragmentUnit, scriptName = "script", units, nonStrictIds = /* @__PURE__ */ new Set()) {
|
|
1848
|
+
if (functionValue.blocks.length === 0) throw new Error(`MIR function '${functionValue.name}' has no blocks.`);
|
|
1849
|
+
const blocks = reachableBlocks(functionValue);
|
|
1850
|
+
const completionSlots = completionSlotCount(blocks);
|
|
1851
|
+
const valueSlotCount = maximumValueId(blocks) + 1;
|
|
1852
|
+
const bindingIdValues = functionValue.localBindingIds ?? bindingIds(functionValue, blocks, /* @__PURE__ */ new Map());
|
|
1853
|
+
const environmentSlot = valueSlotCount;
|
|
1854
|
+
const temporarySlot = valueSlotCount + 1;
|
|
1855
|
+
const baseRootCount = Math.max(functionValue.rootSlotCount, valueSlotCount + 2, 32);
|
|
1856
|
+
const argumentSlots = maximumArgumentCount(blocks);
|
|
1857
|
+
const functionRootCount = rootCount(functionValue);
|
|
1858
|
+
if (!functionFrameCosts.has(functionValue.id)) throw new Error(`MIR function '${functionValue.name}' has no root frame layout.`);
|
|
1859
|
+
const generator = functionValue.generator === true;
|
|
1860
|
+
const generatorBodyStart = functionValue.generatorBodyStart;
|
|
1861
|
+
const entryBlocks = generatorBodyStart == null ? blocks : reachableBlocksFrom(functionValue, 0, /* @__PURE__ */ new Set([generatorBodyStart]));
|
|
1862
|
+
const bodyBlocks = generatorBodyStart == null ? blocks : reachableBlocksFrom(functionValue, generatorBodyStart);
|
|
1863
|
+
const base = {
|
|
1864
|
+
...includePropertiesWhen(() => fragmentUnit == null ? void 0 : { fragmentUnit }),
|
|
1865
|
+
argumentSlotStart: baseRootCount,
|
|
1866
|
+
blockParameters: new Map(blocks.map((block) => [block.id, block.parameters ?? []])),
|
|
1867
|
+
completionSlotStart: baseRootCount + argumentSlots,
|
|
1868
|
+
completionTargets: savedCompletionTargets(functionValue),
|
|
1869
|
+
...includePropertiesWhen(() => {
|
|
1870
|
+
if (functionValue.derivedThisBindingId == null) return void 0;
|
|
1871
|
+
return { derivedThisBindingId: functionValue.derivedThisBindingId };
|
|
1872
|
+
}),
|
|
1873
|
+
functionFrameCosts,
|
|
1874
|
+
nonStrictFunctionIds: nonStrictIds,
|
|
1875
|
+
environmentSlot,
|
|
1876
|
+
globalObjectBindingIds: new Set(globalObjectBindings.map((binding) => binding.id)),
|
|
1877
|
+
globalFunctionBindingIds: new Set(globalObjectBindings.filter((binding) => binding.declaration === "function").map((binding) => binding.id)),
|
|
1878
|
+
nextRecursiveTarget: 0,
|
|
1879
|
+
observeSpecialization,
|
|
1880
|
+
scalarKinds: /* @__PURE__ */ new Map(),
|
|
1881
|
+
strict: functionValue.strict === true,
|
|
1882
|
+
usesAbrupt: false,
|
|
1883
|
+
usesCompletion: false,
|
|
1884
|
+
functionId: functionValue.id
|
|
1885
|
+
};
|
|
1886
|
+
const state = {
|
|
1887
|
+
...base,
|
|
1888
|
+
blockParameters: new Map((generatorBodyStart == null ? blocks : entryBlocks).map((block) => [block.id, block.parameters ?? []])),
|
|
1889
|
+
generator: false,
|
|
1890
|
+
...includePropertiesWhen(() => {
|
|
1891
|
+
if (generatorBodyStart == null) return void 0;
|
|
1892
|
+
return { generatorBodyStart };
|
|
1893
|
+
}),
|
|
1894
|
+
lines: []
|
|
1895
|
+
};
|
|
1896
|
+
state.usesAbrupt = fragmentUnit == null || functionValue.id >= 0;
|
|
1897
|
+
if (needsThisBinding(functionValue)) {
|
|
1898
|
+
location(state, functionValue.range);
|
|
1899
|
+
line(state, "result = oseo_this_value(context, receiver);");
|
|
1900
|
+
line(state, renderC(emittedC.common.gotoAbruptUnlessNormal));
|
|
1901
|
+
line(state, `roots[${generator ? 0 : functionRootCount - 1}] = result.value;`);
|
|
1902
|
+
line(state, "receiver = result.value;");
|
|
1903
|
+
}
|
|
1904
|
+
if (generator) {
|
|
1905
|
+
line(state, renderC(emittedC.function.resultAssignOseoGeneratorCreateContext) + renderC(emittedC.common.sourcePositionSuffix, functionRootCount, completionSlots));
|
|
1906
|
+
line(state, renderC(emittedC.function.frameSlotsAssignResultValueStatement));
|
|
1907
|
+
line(state, renderC(emittedC.common.gotoAbruptUnlessNormal));
|
|
1908
|
+
line(state, renderC(emittedC.function.rootsAssignOseoGeneratorSlotsFrameSlots));
|
|
1909
|
+
}
|
|
1910
|
+
emitPrologue(state, functionValue, bindingIdValues, totalBindingCount, temporarySlot, unresolvableGlobals, globalLexicalNames, globalObjectBindings);
|
|
1911
|
+
if (generator) if (generatorBodyStart == null) line(state, renderC(emittedC.function.resultAssignOseoResultOseoStatusNormal));
|
|
1912
|
+
else emitBlocks(state, entryBlocks, /* @__PURE__ */ new Map());
|
|
1913
|
+
else emitBlocks(state, blocks, /* @__PURE__ */ new Map());
|
|
1914
|
+
if (state.usesAbrupt) {
|
|
1915
|
+
line(state, renderC(emittedC.common.abruptLabel));
|
|
1916
|
+
line(state, renderC(emittedC.function.oseoRootsReleaseContextAddressFrame));
|
|
1917
|
+
line(state, renderC(emittedC.common.returnResult));
|
|
1918
|
+
}
|
|
1919
|
+
const id = functionValue.id < 0 ? scriptName : String(functionValue.id);
|
|
1920
|
+
const charge = functionFrameCosts.get(functionValue.id);
|
|
1921
|
+
const frameCost = functionValue.id < 0 ? `${charge}u` : `OSEO_CALLABLE_FRAME_COST(${charge}u)`;
|
|
1922
|
+
const frameGuard = nativeFrameGuard(frameCost, [
|
|
1923
|
+
"callee",
|
|
1924
|
+
"receiver",
|
|
1925
|
+
"argument_count",
|
|
1926
|
+
"arguments",
|
|
1927
|
+
"new_target"
|
|
1928
|
+
]);
|
|
1929
|
+
const ownerUnit = units?.assign(4096) ?? 0;
|
|
1930
|
+
const bounded = boundBody(state.lines, `oseo_body_${id}`, frameCost, false, state.usesCompletion, units, ownerUnit);
|
|
1931
|
+
const entrySource = renderC(emittedC.function.staticOseoResultOseoFunctionLine, id) + renderC(emittedC.common.oseoContextPointerContextLine) + renderC(emittedC.function.oseoValueCalleeLine) + renderC(emittedC.function.oseoValueReceiverLine) + renderC(emittedC.function.sizeTArgumentCountLine) + renderC(emittedC.function.constOseoValuePointerArgumentsLine) + renderC(emittedC.function.oseoValueNewTargetLine) + renderC(emittedC.common.functionBodyOpenLine) + frameGuard + renderC(emittedC.function.oseoRootFrameFrameAssignNullNullULine) + renderC(emittedC.function.oseoValuePointerRootsLine) + renderC(emittedC.common.oseoResultResultLine) + completionDeclaration(state, completionSlots) + renderC(emittedC.common.voidCalleeLine) + renderC(emittedC.common.voidReceiverLine) + renderC(emittedC.function.voidArgumentCountLine) + renderC(emittedC.function.voidArgumentsLine) + renderC(emittedC.function.voidNewTargetLine) + renderC(emittedC.function.resultAssignOseoRootsAllocate) + renderC(emittedC.function.contextAddressFrameULine, generator ? 1 : functionRootCount) + renderC(emittedC.function.ifResultStatusNotEqualOseoStatusNormal) + renderC(emittedC.function.rootsAssignFrameSlotsLine) + renderC(emittedC.common.valueThenNewline, bounded?.execution ?? state.lines.join(renderC(emittedC.common.newline))) + "#endif\n" + renderC(emittedC.common.closeBlockLine);
|
|
1932
|
+
const entry = (bounded?.declarations ?? "") + (units == null ? entrySource : bodyUnit(entrySource, ownerUnit));
|
|
1933
|
+
if (!generator) return entry;
|
|
1934
|
+
return renderC(emittedC.function.newline, entry, emitGeneratorBody(functionValue, bodyBlocks, completionSlots, base, frameCost, units));
|
|
1935
|
+
}
|
|
1936
|
+
function nonStrictFunctionIds(functions) {
|
|
1937
|
+
return new Set(functions.filter((fn) => fn.strict !== true).map((fn) => fn.id));
|
|
1938
|
+
}
|
|
1939
|
+
function prototype(functionValue) {
|
|
1940
|
+
const id = functionValue.id < 0 ? renderC(emittedC.common.script) : String(functionValue.id);
|
|
1941
|
+
const entry = renderC(emittedC.prototype.staticOseoResultOseoFunction, id) + renderC(emittedC.prototype.oseoContextPointerOseoValueOseoValueSizeT) + renderC(emittedC.prototype.constOseoValuePointerOseoValueStatement);
|
|
1942
|
+
return functionValue.generator === true ? renderC(emittedC.prototype.staticOseoResult, entry, generatorBodyName(functionValue)) + renderC(emittedC.prototype.oseoContextPointerOseoValueStatement) : entry;
|
|
1943
|
+
}
|
|
1944
|
+
/** Charge active continuation bodies, independently of their heap roots. */
|
|
1945
|
+
function generatorDispatchCases(functions, functionFrameCosts) {
|
|
1946
|
+
return functions.filter((fn) => fn.generator === true).map((fn) => {
|
|
1947
|
+
const count = functionFrameCosts.get(fn.id);
|
|
1948
|
+
if (count == null) throw new Error(`MIR function '${fn.name}' has no root frame layout.`);
|
|
1949
|
+
return renderC(emittedC.generatorDispatcher.caseClause, fn.id, count, generatorBodyName(fn), count);
|
|
1950
|
+
}).join(renderC(emittedC.common.newline));
|
|
1951
|
+
}
|
|
1952
|
+
/** Route a resumed generator to the body code its function identity owns. */
|
|
1953
|
+
function emitGeneratorDispatcher(functions, functionFrameCosts) {
|
|
1954
|
+
const cases = generatorDispatchCases(functions, functionFrameCosts);
|
|
1955
|
+
if (cases === "") return void 0;
|
|
1956
|
+
return renderC(emittedC.generatorDispatcher.source, cases);
|
|
1957
|
+
}
|
|
1958
|
+
function emitFunctionDispatcher(functions, functionFrameCosts) {
|
|
1959
|
+
const cases = [];
|
|
1960
|
+
for (const functionValue of functions) {
|
|
1961
|
+
const count = functionFrameCosts.get(functionValue.id);
|
|
1962
|
+
if (count == null) throw new Error(`MIR function '${functionValue.name}' has no root frame layout.`);
|
|
1963
|
+
cases.push(renderC(emittedC.functionDispatcher.caseClause, functionValue.id, count, functionValue.id, count));
|
|
1964
|
+
}
|
|
1965
|
+
const casesSection = cases.length === 0 ? renderC(emittedC.common.empty) : renderC(emittedC.common.valueThenNewline, cases.join(renderC(emittedC.common.newline)));
|
|
1966
|
+
return renderC(emittedC.functionDispatcher.source, casesSection);
|
|
1967
|
+
}
|
|
1968
|
+
/** Deterministic C11 lowering whose only semantic input is MIR. */
|
|
1969
|
+
function emitAgentTables(agents) {
|
|
1970
|
+
let text = renderC(emittedC.common.newline);
|
|
1971
|
+
const entries = [];
|
|
1972
|
+
for (const [index, agent] of agents.entries()) {
|
|
1973
|
+
text += renderC(emittedC.agent.entryPrototype, index);
|
|
1974
|
+
const segments = [];
|
|
1975
|
+
for (const [position, segment] of agent.segments.entries()) {
|
|
1976
|
+
const units = [];
|
|
1977
|
+
for (let unit = 0; unit < segment.length; unit += 1) units.push(segment.charCodeAt(unit));
|
|
1978
|
+
text += renderC(emittedC.agent.segmentUnits, index, position, units.length === 0 ? "0" : units.join(", "));
|
|
1979
|
+
segments.push(renderC(emittedC.agent.segmentEntry, index, position, units.length));
|
|
1980
|
+
}
|
|
1981
|
+
text += renderC(emittedC.agent.segmentTable, index, segments.join(", "));
|
|
1982
|
+
entries.push(renderC(emittedC.agent.programEntry, index, agent.segments.length, index, escapeCString(agent.sourceId), new TextEncoder().encode(agent.sourceId).length));
|
|
1983
|
+
}
|
|
1984
|
+
if (entries.length > 0) text += renderC(emittedC.agent.programTable, entries.join(", "));
|
|
1985
|
+
return text;
|
|
1986
|
+
}
|
|
1987
|
+
/** Keep standalone C emission while selecting bounded native build units. */
|
|
1988
|
+
function nativeSource(source, sourceName, units) {
|
|
1989
|
+
source = emittedCNotice + source;
|
|
1990
|
+
if (units == null) return {
|
|
1991
|
+
source,
|
|
1992
|
+
sourceName
|
|
1993
|
+
};
|
|
1994
|
+
source = replaceSyntax(source, /\bstatic\s+(?=OseoResult\s+oseo_(?:function|generator_body)_)/gu, () => "");
|
|
1995
|
+
const prefix = sourceName.replace(/[^a-zA-Z0-9]/gu, "_");
|
|
1996
|
+
source = replaceSyntax(source, /* @__PURE__ */ new RegExp("\\b(oseo_(?:function_(?:\\d+|script|instantiate|evaluate)|generator_body_\\d+[a-zA-Z0-9_]*|body_[a-zA-Z0-9_]+))\\b", "gu"), (identifier) => `${prefix}_${identifier}`);
|
|
1997
|
+
source = source.replace(new RegExp(String.raw`\n(int main\(int argc, char \*\*argv\)|` + String.raw`OseoResult oseo_agent_program_\d+\()`, "u"), "\n#if !defined(OSEO_C_BODY_UNIT) || OSEO_C_BODY_UNIT == 0\n$1") + "\n#endif\n";
|
|
1998
|
+
const compilationUnits = Array.from({ length: units.count + 1 }, (_, unit) => ({
|
|
1999
|
+
source: emittedCNotice + `#define OSEO_C_BODY_UNIT ${unit}\n#include "${sourceName}"\n`,
|
|
2000
|
+
sourceName: `${sourceName.slice(0, -2)}-unit-${unit}.c`
|
|
2001
|
+
}));
|
|
2002
|
+
return {
|
|
2003
|
+
source,
|
|
2004
|
+
sourceName,
|
|
2005
|
+
compilationUnits
|
|
2006
|
+
};
|
|
2007
|
+
}
|
|
2008
|
+
/** Immutable source table; callbacks share the complete function dispatcher. */
|
|
2009
|
+
function emitHostScriptTables(scripts, costs) {
|
|
2010
|
+
if (scripts.length === 0) return "";
|
|
2011
|
+
const lines = [];
|
|
2012
|
+
const entries = scripts.map((script, index) => {
|
|
2013
|
+
const units = utf16Units(script.source);
|
|
2014
|
+
lines.push(`static const uint16_t host_source_${index}[] = {` + (units.length === 0 ? "0" : units.join(", ")) + "};");
|
|
2015
|
+
return `{host_source_${index}, ${units.length}u, ` + (script.functionId == null ? "NULL, 0u, " : `oseo_function_${script.functionId}, OSEO_CALLABLE_FRAME_COST(${costs.get(script.functionId)}u), `) + `${script.error === "syntax" ? "true" : "false"}, ${script.message == null ? "NULL" : `"${escapeCString(script.message)}"`}}`;
|
|
2016
|
+
});
|
|
2017
|
+
lines.push("static const OseoHostScript oseo_host_scripts[] = {" + entries.join(",\n") + "};\n");
|
|
2018
|
+
return lines.join("\n");
|
|
2019
|
+
}
|
|
2020
|
+
/**
|
|
2021
|
+
* The generated setup line that selects the observe rejection policy, or
|
|
2022
|
+
* nothing. Ordinary programs carry no policy field, so their emitted C is
|
|
2023
|
+
* unchanged and their context keeps the runtime's fatal default.
|
|
2024
|
+
*/
|
|
2025
|
+
function unhandledRejectionPolicySetup(program) {
|
|
2026
|
+
return program.unhandledRejectionPolicy === "observe" ? " context.unhandled_rejection_policy =\n OSEO_UNHANDLED_REJECTION_OBSERVE;\n" : "";
|
|
2027
|
+
}
|
|
2028
|
+
const cBackend = { emit(input) {
|
|
2029
|
+
const originalFunctions = reachableFunctions(input);
|
|
2030
|
+
const declaredFunctions = originalFunctions.map(compactRootIds);
|
|
2031
|
+
const script = compactRootIds(input.test262Host == null ? input.script : {
|
|
2032
|
+
...input.script,
|
|
2033
|
+
hostScript: {
|
|
2034
|
+
lexical: input.globalLexicalNames ?? [],
|
|
2035
|
+
objects: input.globalObjectBindings
|
|
2036
|
+
}
|
|
2037
|
+
});
|
|
2038
|
+
const functions = [...declaredFunctions, script];
|
|
2039
|
+
const units = functions.reduce((count, fn) => count + fn.blocks.reduce((sum, block) => sum + block.operations.length, 0), 0) > 2048 ? new BodyUnits() : void 0;
|
|
2040
|
+
const globalLexicalNames = input.globalLexicalNames ?? [];
|
|
2041
|
+
const globalObjectBindings = [...input.globalObjectBindings, ...input.hostObjectBindings ?? []];
|
|
2042
|
+
const functionFrameCosts = new Map([...originalFunctions.map((fn) => [fn.id, rootCount(fn)]), [input.script.id, scriptFrameCost(input.script)]]);
|
|
2043
|
+
const scriptRootCount = functionFrameCosts.get(input.script.id);
|
|
2044
|
+
if (scriptRootCount == null) throw new Error("MIR script has no root frame layout.");
|
|
2045
|
+
let totalBindingCount = 0;
|
|
2046
|
+
for (const functionValue of functions) {
|
|
2047
|
+
for (const bindingId of functionValue.localBindingIds ?? []) totalBindingCount = Math.max(totalBindingCount, bindingId + 1);
|
|
2048
|
+
for (const parameter of functionValue.parameters) totalBindingCount = Math.max(totalBindingCount, parameter.bindingId + 1);
|
|
2049
|
+
for (const block of functionValue.blocks) for (const operation of block.operations) {
|
|
2050
|
+
if (operation.bindingId == null) continue;
|
|
2051
|
+
totalBindingCount = Math.max(totalBindingCount, operation.bindingId + 1);
|
|
2052
|
+
}
|
|
2053
|
+
}
|
|
2054
|
+
for (const binding of input.globalBindings) totalBindingCount = Math.max(totalBindingCount, binding.id + 1);
|
|
2055
|
+
for (const binding of globalObjectBindings) totalBindingCount = Math.max(totalBindingCount, binding.id + 1);
|
|
2056
|
+
const unresolvableGlobals = new Map(input.globalBindings.filter((binding) => binding.unresolvableName != null).map((binding) => [binding.id, binding.unresolvableName ?? ""]));
|
|
2057
|
+
const declarations = functions.map(prototype).join(renderC(emittedC.common.newline));
|
|
2058
|
+
const dispatcherSource = emitFunctionDispatcher(declaredFunctions, functionFrameCosts);
|
|
2059
|
+
const dispatcher = units == null ? dispatcherSource : bodyUnit(dispatcherSource, 0);
|
|
2060
|
+
const generatorDispatcher = emitGeneratorDispatcher(declaredFunctions, functionFrameCosts);
|
|
2061
|
+
const functionReferences = declaredFunctions.map((functionValue) => renderC(emittedC.program.voidOseoFunctionStatement, functionValue.id)).join(renderC(emittedC.common.newline));
|
|
2062
|
+
const definitions = functions.map((functionValue) => emitFunction(functionValue, functionFrameCosts, totalBindingCount, input.observeSpecialization === true, unresolvableGlobals, globalLexicalNames, globalObjectBindings, void 0, "script", units, nonStrictFunctionIds(originalFunctions))).join(renderC(emittedC.common.newline));
|
|
2063
|
+
const sourceId = escapeCString(input.sourceId);
|
|
2064
|
+
const sourceIdByteLength = new TextEncoder().encode(input.sourceId).length;
|
|
2065
|
+
const empty = renderC(emittedC.common.empty);
|
|
2066
|
+
const functionEntryType = declaredFunctions.some(hasSelfCall) ? renderC(emittedC.program.typedefOseoResultPointerOseoFunctionEntry) + renderC(emittedC.program.oseoContextPointerOseoValueOseoValueSizeT) + renderC(emittedC.program.constOseoValuePointerOseoValueLine) : empty;
|
|
2067
|
+
const generatorDispatcherSection = generatorDispatcher == null ? empty : renderC(emittedC.common.valueThenBlankLine, units == null ? generatorDispatcher : bodyUnit(generatorDispatcher, 0));
|
|
2068
|
+
const generatorRegistration = generatorDispatcher == null ? empty : renderC(emittedC.program.oseoContextSetGeneratorDispatcherLine) + renderC(emittedC.program.addressContextOseoDispatchGeneratorLine);
|
|
2069
|
+
const specializationObservation = input.observeSpecialization === true ? renderC(emittedC.program.contextObserveSpecializationAssignTrueLine) : empty;
|
|
2070
|
+
const observations = input.observeSpecialization === true ? renderC(emittedC.program.oseoContextPrintObservationsAddressContext) : empty;
|
|
2071
|
+
if (input.agentProgram != null) {
|
|
2072
|
+
const agentGeneratorRegistration = generatorDispatcher == null ? empty : renderC(emittedC.agent.generatorRegistrationLine);
|
|
2073
|
+
return nativeSource(renderC(emittedC.agent.source, functionEntryType, declarations, dispatcher, generatorDispatcherSection, definitions, input.agentProgram, functionReferences, functionReferences === empty ? empty : renderC(emittedC.common.newline), agentGeneratorRegistration, scriptRootCount, scriptRootCount), `agent-${input.agentProgram}.c`, units);
|
|
2074
|
+
}
|
|
2075
|
+
const host = input.test262Host;
|
|
2076
|
+
const scriptTables = host == null ? empty : emitHostScriptTables(host.scripts ?? [], functionFrameCosts);
|
|
2077
|
+
const hostTables = host == null ? empty : emitAgentTables(host.agents) + scriptTables;
|
|
2078
|
+
const scriptInstall = host == null ? empty : ` context.host_scripts = ${host.scripts?.length ? "oseo_host_scripts" : "NULL"};\n context.host_script_count = ${host.scripts?.length ?? 0}u;\n`;
|
|
2079
|
+
const rejectionPolicy = unhandledRejectionPolicySetup(input);
|
|
2080
|
+
const hostInstall = host == null ? empty : ` context.agent_can_block = ${host.mainAgentCanBlock !== false};\n` + renderC(emittedC.agent.installLine, host.agents.length === 0 ? renderC(emittedC.agent.noProgramsValue) : renderC(emittedC.agent.programsValue), host.agents.length);
|
|
2081
|
+
return nativeSource(renderC(emittedC.program.source, functionEntryType, declarations, dispatcher, generatorDispatcherSection, definitions + (units == null ? hostTables : bodyUnit(hostTables, 0)), functionReferences, functionReferences === empty ? empty : renderC(emittedC.common.newline), sourceId, sourceIdByteLength, generatorRegistration, specializationObservation + rejectionPolicy + scriptInstall + hostInstall, scriptRootCount, scriptRootCount, observations), "generated.c", units);
|
|
2082
|
+
} };
|
|
2083
|
+
const fragmentIncludes = `#include "oseo_runtime.h"
|
|
2084
|
+
#include <math.h>
|
|
2085
|
+
#include <stddef.h>
|
|
2086
|
+
#include <stdint.h>
|
|
2087
|
+
#include <stdlib.h>
|
|
2088
|
+
void oseo_fragment_location(OseoContext *, unsigned, size_t, size_t);
|
|
2089
|
+
`;
|
|
2090
|
+
const fragmentCallParameters = `OseoContext *context, OseoValue callee,
|
|
2091
|
+
OseoValue receiver, size_t argument_count,
|
|
2092
|
+
const OseoValue *arguments, OseoValue new_target`;
|
|
2093
|
+
const fragmentCallArguments = `context, callee, receiver, argument_count,
|
|
2094
|
+
arguments, new_target`;
|
|
2095
|
+
function emitFragmentUnit(fragment, unit, globalObjects) {
|
|
2096
|
+
const phases = lowerFragmentPhases(fragment);
|
|
2097
|
+
const functions = fragment.mir.functions.map(compactRootIds);
|
|
2098
|
+
const counts = new Map(fragment.mir.functions.map((fn) => [fn.id, rootCount(fn)]));
|
|
2099
|
+
const nonStrict = nonStrictFunctionIds(fragment.mir.functions);
|
|
2100
|
+
const definitions = functions.map((fn) => emitFunction(fn, counts, fragment.nextBindingId, fragment.mir.observeSpecialization, /* @__PURE__ */ new Map(), [], globalObjects, unit, void 0, void 0, nonStrict));
|
|
2101
|
+
const entries = phases.map((phase, index) => {
|
|
2102
|
+
const name = index === 0 ? "instantiate" : "evaluate";
|
|
2103
|
+
const script = compactRootIds(phase.script);
|
|
2104
|
+
const count = scriptFrameCost(phase.script);
|
|
2105
|
+
return emitFunction(script, new Map([...counts, [-1, count]]), fragment.nextBindingId, fragment.mir.observeSpecialization, /* @__PURE__ */ new Map(), [], globalObjects, unit, name, void 0, nonStrict) + `
|
|
2106
|
+
OseoResult oseo_${unit}_${name}(OseoContext *context,
|
|
2107
|
+
OseoValue environment) {
|
|
2108
|
+
OseoResult result = oseo_frame_enter(context, ${count}u);
|
|
2109
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
2110
|
+
result = oseo_function_${name}(context, environment,
|
|
2111
|
+
oseo_undefined(), 0u, NULL, oseo_undefined());
|
|
2112
|
+
oseo_frame_leave(context, ${count}u);
|
|
2113
|
+
return result;
|
|
2114
|
+
}
|
|
2115
|
+
`;
|
|
2116
|
+
});
|
|
2117
|
+
const cases = functions.map((fn) => renderC(emittedC.functionDispatcher.caseClause, fn.id, counts.get(fn.id), fn.id, counts.get(fn.id))).join("\n");
|
|
2118
|
+
const generatorCases = generatorDispatchCases(functions, counts);
|
|
2119
|
+
return emittedCNotice + fragmentIncludes + `
|
|
2120
|
+
typedef OseoResult (*OseoFunctionEntry)(OseoContext *, OseoValue,
|
|
2121
|
+
OseoValue, size_t, const OseoValue *, OseoValue);
|
|
2122
|
+
${functions.map(prototype).join("\n")}
|
|
2123
|
+
${definitions.join("\n")}
|
|
2124
|
+
${entries.join("\n")}
|
|
2125
|
+
OseoResult oseo_${unit}_dispatch(size_t code_id,
|
|
2126
|
+
${fragmentCallParameters}) {
|
|
2127
|
+
OseoResult result;
|
|
2128
|
+
(void)result; (void)callee; (void)receiver;
|
|
2129
|
+
(void)argument_count; (void)arguments; (void)new_target;
|
|
2130
|
+
switch (code_id) {
|
|
2131
|
+
${cases}
|
|
2132
|
+
default: return oseo_unknown_function(context, code_id);
|
|
2133
|
+
}
|
|
2134
|
+
}
|
|
2135
|
+
OseoResult oseo_${unit}_resume(size_t code_id,
|
|
2136
|
+
OseoContext *context, OseoValue generator) {
|
|
2137
|
+
OseoResult result;
|
|
2138
|
+
(void)result; (void)generator;
|
|
2139
|
+
switch (code_id) {
|
|
2140
|
+
${generatorCases}
|
|
2141
|
+
default: return oseo_unknown_function(context, code_id);
|
|
2142
|
+
}
|
|
2143
|
+
}
|
|
2144
|
+
`;
|
|
2145
|
+
}
|
|
2146
|
+
/**
|
|
2147
|
+
* Emit the opt-in fragment ABI. The caller must retain whole-Script fallback
|
|
2148
|
+
* and provide offsets for the exact source assembly it admitted.
|
|
2149
|
+
*/
|
|
2150
|
+
function emitScriptFragments(harness, body, mapping) {
|
|
2151
|
+
if (harness.abi !== scriptFragmentAbi || ![
|
|
2152
|
+
harness.nextBindingId,
|
|
2153
|
+
body.nextBindingId,
|
|
2154
|
+
harness.nextFunctionId,
|
|
2155
|
+
body.nextFunctionId
|
|
2156
|
+
].every((value) => Number.isSafeInteger(value) && value >= 0 && value <= 4294967295) || body.nextBindingId < harness.nextBindingId || body.nextFunctionId < harness.nextFunctionId || body.mir.functions.some((fn) => fn.id < harness.nextFunctionId) || ![mapping.harnessLineOffset, mapping.bodyLineOffset].every(Number.isSafeInteger)) throw new Error("Invalid fragment ABI limits or source mapping.");
|
|
2157
|
+
const functions = [...harness.mir.functions, ...body.mir.functions];
|
|
2158
|
+
const globals = [...harness.mir.globalObjectBindings, ...body.mir.globalObjectBindings];
|
|
2159
|
+
const remap = (range, offset) => ({
|
|
2160
|
+
...range,
|
|
2161
|
+
sourceId: mapping.sourceId,
|
|
2162
|
+
start: {
|
|
2163
|
+
...range.start,
|
|
2164
|
+
line: range.start.line + offset
|
|
2165
|
+
},
|
|
2166
|
+
end: {
|
|
2167
|
+
...range.end,
|
|
2168
|
+
line: range.end.line + offset
|
|
2169
|
+
}
|
|
2170
|
+
});
|
|
2171
|
+
const fragments = [harness, body];
|
|
2172
|
+
const offsets = [mapping.harnessLineOffset, mapping.bodyLineOffset];
|
|
2173
|
+
const lexical = fragments.flatMap((fragment, index) => (fragment.mir.globalLexicalNames ?? []).map((entry) => ({
|
|
2174
|
+
name: entry.name,
|
|
2175
|
+
range: remap(entry.range, offsets[index])
|
|
2176
|
+
})));
|
|
2177
|
+
const objectBindings = [
|
|
2178
|
+
"annexb",
|
|
2179
|
+
"function",
|
|
2180
|
+
"var"
|
|
2181
|
+
].flatMap((kind) => fragments.flatMap((fragment, index) => fragment.mir.globalObjectBindings.filter((entry) => entry.declaration === kind).map((entry) => ({
|
|
2182
|
+
id: entry.id,
|
|
2183
|
+
name: entry.name,
|
|
2184
|
+
declaration: entry.declaration,
|
|
2185
|
+
range: remap(entry.range, offsets[index])
|
|
2186
|
+
}))));
|
|
2187
|
+
const lines = [];
|
|
2188
|
+
emitGlobalObject({
|
|
2189
|
+
lines,
|
|
2190
|
+
environmentSlot: 0
|
|
2191
|
+
}, lexical, objectBindings);
|
|
2192
|
+
const cells = [...new Set(fragments.flatMap((fragment) => fragment.mir.script.localBindingIds ?? []))];
|
|
2193
|
+
const unresolvableGlobals = new Map(fragments.flatMap((fragment) => fragment.mir.globalBindings.flatMap((binding) => binding.unresolvableName == null ? [] : [[binding.id, binding.unresolvableName]])));
|
|
2194
|
+
const declarations = ["harness", "case"].map((unit) => `
|
|
2195
|
+
OseoResult oseo_${unit}_instantiate(OseoContext *, OseoValue);
|
|
2196
|
+
OseoResult oseo_${unit}_evaluate(OseoContext *, OseoValue);
|
|
2197
|
+
OseoResult oseo_${unit}_dispatch(size_t, ${fragmentCallParameters});
|
|
2198
|
+
OseoResult oseo_${unit}_resume(size_t, OseoContext *, OseoValue);
|
|
2199
|
+
`).join("\n");
|
|
2200
|
+
const dispatcher = `
|
|
2201
|
+
static OseoResult oseo_dispatch_function(${fragmentCallParameters}) {
|
|
2202
|
+
size_t code_id = 0u;
|
|
2203
|
+
OseoResult result = oseo_function_code_id(context, callee, &code_id);
|
|
2204
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
2205
|
+
${harness.nextFunctionId === 0 ? `return oseo_case_dispatch(code_id, ${fragmentCallArguments});` : `return code_id < ${harness.nextFunctionId}u
|
|
2206
|
+
? oseo_harness_dispatch(code_id, ${fragmentCallArguments})
|
|
2207
|
+
: oseo_case_dispatch(code_id, ${fragmentCallArguments});`}
|
|
2208
|
+
}
|
|
2209
|
+
static OseoResult oseo_dispatch_generator(OseoContext *context,
|
|
2210
|
+
OseoValue generator) {
|
|
2211
|
+
size_t code_id = 0u;
|
|
2212
|
+
OseoResult result = oseo_function_code_id(context,
|
|
2213
|
+
oseo_generator_callee(generator), &code_id);
|
|
2214
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
2215
|
+
${harness.nextFunctionId === 0 ? "return oseo_case_resume(code_id, context, generator);" : `return code_id < ${harness.nextFunctionId}u
|
|
2216
|
+
? oseo_harness_resume(code_id, context, generator)
|
|
2217
|
+
: oseo_case_resume(code_id, context, generator);`}
|
|
2218
|
+
}
|
|
2219
|
+
`;
|
|
2220
|
+
const sourceId = escapeCString(mapping.sourceId);
|
|
2221
|
+
const sourceLength = new TextEncoder().encode(mapping.sourceId).length;
|
|
2222
|
+
const script = `
|
|
2223
|
+
const size_t oseo_fragment_binding_count = ${body.nextBindingId}u;
|
|
2224
|
+
void oseo_fragment_location(OseoContext *context, unsigned unit,
|
|
2225
|
+
size_t line, size_t column) {
|
|
2226
|
+
ptrdiff_t offset = unit == 0u ? ${mapping.harnessLineOffset}
|
|
2227
|
+
: ${mapping.bodyLineOffset};
|
|
2228
|
+
oseo_context_source_location(context, "${sourceId}", ${sourceLength}u,
|
|
2229
|
+
(size_t)((ptrdiff_t)line + offset), column);
|
|
2230
|
+
}
|
|
2231
|
+
static OseoResult oseo_function_script(${fragmentCallParameters}) {
|
|
2232
|
+
OseoRootFrame frame = {NULL, NULL, 0u};
|
|
2233
|
+
OseoResult result = oseo_roots_allocate(context, &frame, 2u);
|
|
2234
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
2235
|
+
OseoValue *roots = frame.slots;
|
|
2236
|
+
(void)callee; (void)receiver; (void)argument_count;
|
|
2237
|
+
(void)arguments; (void)new_target;
|
|
2238
|
+
result = oseo_environment_create(context, oseo_fragment_binding_count);
|
|
2239
|
+
roots[0] = result.value;
|
|
2240
|
+
if (result.status != OSEO_STATUS_NORMAL) goto abrupt;
|
|
2241
|
+
${cells.map((id) => {
|
|
2242
|
+
const unresolvable = unresolvableGlobals.get(id);
|
|
2243
|
+
return ` ${unresolvable == null ? renderC(emittedC.common.resultAssignOseoCellCreateContextOseo) : renderC(emittedC.prologue.resultAssignOseoUnresolvableCellCreate, escapeCString(unresolvableReferenceMessage(unresolvable)))}
|
|
2244
|
+
roots[1] = result.value;
|
|
2245
|
+
if (result.status != OSEO_STATUS_NORMAL) goto abrupt;
|
|
2246
|
+
result = oseo_environment_set(context, roots[0], ${id}u, roots[1]);
|
|
2247
|
+
if (result.status != OSEO_STATUS_NORMAL) goto abrupt;`;
|
|
2248
|
+
}).join("\n")}
|
|
2249
|
+
${lines.join("\n")}
|
|
2250
|
+
${[
|
|
2251
|
+
"harness_instantiate",
|
|
2252
|
+
"case_instantiate",
|
|
2253
|
+
"harness_evaluate",
|
|
2254
|
+
"case_evaluate"
|
|
2255
|
+
].map((entry) => ` result = oseo_${entry}(context, roots[0]);
|
|
2256
|
+
if (result.status != OSEO_STATUS_NORMAL) goto abrupt;`).join("\n")}
|
|
2257
|
+
abrupt:
|
|
2258
|
+
oseo_roots_release(context, &frame);
|
|
2259
|
+
return result;
|
|
2260
|
+
}
|
|
2261
|
+
`;
|
|
2262
|
+
const launcher = renderC(emittedC.program.source, "", declarations, dispatcher, "", script, "", "", sourceId, sourceLength, " oseo_context_set_generator_dispatcher(\n &context, oseo_dispatch_generator);\n", (harness.mir.observeSpecialization ? " context.observe_specialization = true;\n" : "") + unhandledRejectionPolicySetup(harness.mir), 2, 2, harness.mir.observeSpecialization ? " oseo_context_print_observations(&context);\n" : "");
|
|
2263
|
+
if (functions.some((fn, index) => functions.some((other, otherIndex) => index !== otherIndex && fn.id === other.id))) throw new Error("Fragment function IDs overlap.");
|
|
2264
|
+
return {
|
|
2265
|
+
harness: {
|
|
2266
|
+
source: emitFragmentUnit(harness, "harness", harness.mir.globalObjectBindings),
|
|
2267
|
+
sourceName: "harness.c"
|
|
2268
|
+
},
|
|
2269
|
+
body: {
|
|
2270
|
+
source: emitFragmentUnit(body, "case", globals),
|
|
2271
|
+
sourceName: "case.c"
|
|
2272
|
+
},
|
|
2273
|
+
launcher: {
|
|
2274
|
+
source: emittedCNotice + launcher,
|
|
2275
|
+
sourceName: "launcher.c"
|
|
2276
|
+
}
|
|
2277
|
+
};
|
|
2278
|
+
}
|
|
2279
|
+
|
|
2280
|
+
//#endregion
|
|
2281
|
+
export { cBackend, emitScriptFragments };
|
|
2282
|
+
//# sourceMappingURL=index.js.map
|