Repository navigation
Expand file tree
/
Copy pathopcodes.ts
More file actions
288 lines (254 loc) · 6.61 KB
/
Copy pathopcodes.ts
File metadata and controls
288 lines (254 loc) · 6.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import type { DiagnosticKind } from "./codemode.js"
import type { ErrorType } from "./runtime/intrinsics.js"
/** Uninitialized lexical binding sentinel (temporal dead zone). */
export const HOLE: unique symbol = Symbol("codemode.hole")
/** Mutable box for a binding captured by a closure. */
export class Cell {
constructor(public v: unknown) {}
}
const initCell = new Cell(0)
initCell.v = 1.5
initCell.v = ""
initCell.v = true
initCell.v = null
initCell.v = undefined
initCell.v = HOLE
const makeEmptyCells = (): ReadonlyArray<Cell> => {
const arr: Array<unknown> = []
arr.push(initCell)
arr.length = 0
return arr as Array<Cell>
}
export const EMPTY_CELLS: ReadonlyArray<Cell> = makeEmptyCells()
/** Raised when the VM compiler or runtime encounters a feature scheduled for a later phase. */
export class NotYetImplemented extends Error {
constructor(readonly feature: string) {
super(`Not yet implemented in VM: ${feature}`)
this.name = "NotYetImplemented"
}
}
/**
* Bytecode opcodes (1 word per instruction: `op:8 | a:8 | bx:16`, widened by `EXTRA` prefixes for `a > 0xff` or `bx > 0xffff`).
*/
export const Op = {
// Stack and constants (10)
PUSH_UNDEF: 0,
PUSH_NULL: 1,
PUSH_BOOL: 2,
PUSH_INT: 3,
PUSH_CONST: 4,
POP: 5,
POPN: 6,
DUP: 7,
PICK: 8,
ROT: 9,
// Variables (15)
GET_LOCAL: 10,
PUT_LOCAL: 11,
HOLE_LOCAL: 12,
NEW_CELL: 13,
COPY_CELL: 14,
GET_CELL: 15,
PUT_CELL: 16,
GET_UPVAL: 17,
PUT_UPVAL: 18,
GET_GLOBAL: 19,
GET_INTRINSIC: 20,
ARGS: 21,
CLOSURE: 22,
SET_NAME: 23,
IMPORT: 24,
// Properties (10)
GET_PROP: 25,
GET_ELEM: 26,
PUT_PROP: 27,
PUT_ELEM: 28,
DELETE_PROP: 29,
DELETE_ELEM: 30,
CHECK_BASE: 31,
TO_KEY: 32,
IN: 33,
INSTANCEOF: 34,
// Literals (8)
NEW_OBJECT: 35,
NEW_ARRAY: 36,
ARRAY_APPEND: 37,
DEFINE_FIELD: 38,
COPY_DATA_PROPS: 39,
COPY_REST: 40,
TEMPLATE: 41,
REGEXP: 42,
// Operators (16)
ADD: 43,
SUB: 44,
MUL: 45,
LT: 46,
LE: 47,
GT: 48,
GE: 49,
SEQ: 50,
SNE: 51,
BINOP: 52,
UNARY: 53,
NOT: 54,
TO_NUMERIC: 55,
INC: 56,
TO_STRING: 57,
CONCAT: 58,
// Control (11)
JUMP: 59,
LOOP: 60,
JUMP_IF_FALSE: 61,
JUMP_IF_TRUE: 62,
JUMP_COND: 63,
THROW: 64,
THROW_ERROR: 65,
END_FINALLY: 66,
RETURN: 67,
NOP: 68,
EXTRA: 69,
// Calls (4)
CALL: 70,
CALL_SPREAD: 71,
NEW: 72,
NEW_SPREAD: 73,
// Iteration (7)
ITER_OPEN: 74,
ITER_INIT: 75,
ITER_STEP: 76,
ITER_RESULT: 77,
ITER_CLOSE: 78,
ITER_APPEND_ALL: 79,
FOR_IN: 80,
// Generators and async (6)
GENERATOR_START: 81,
YIELD: 82,
RESUME: 83,
AWAIT: 84,
YSTAR: 85,
YSTAR_RESULT: 86,
// Program (1)
END_PROGRAM: 87,
} as const
export type OpCode = (typeof Op)[keyof typeof Op]
export const OpNames: ReadonlyArray<string> = Object.entries(Op).reduce<Array<string>>((acc, [name, code]) => {
acc[code] = name
return acc
}, [])
/** Sub-ops for `Op.BINOP` (`a` operand). */
export const BinOp = {
DIV: 0,
MOD: 1,
EXP: 2,
BAND: 3,
BOR: 4,
BXOR: 5,
SHL: 6,
SAR: 7,
SHR: 8,
EQ: 9,
NE: 10,
} as const
export const BinOpSymbols: ReadonlyArray<string> = ["/", "%", "**", "&", "|", "^", "<<", ">>", ">>>", "==", "!="]
/** Sub-ops for `Op.UNARY` (`a` operand). */
export const UnaryOp = {
NEG: 0,
PLUS: 1,
BNOT: 2,
TYPEOF: 3,
VOID: 4,
} as const
export const UnaryOpSymbols: ReadonlyArray<string> = ["-", "+", "~", "typeof", "void"]
/** Sub-ops for `Op.JUMP_COND` (`a` operand). */
export const CondKind = {
/** If top is truthy, keep it and jump; else pop and fall through (`||`, `||=`). */
TRUTHY_KEEP: 0,
/** If top is falsy, keep it and jump; else pop and fall through (`&&`, `&&=`). */
FALSY_KEEP: 1,
/** If top is not null/undefined, keep it and jump; else pop and fall through (`??`, `??=`). */
NOT_NULLISH_KEEP: 2,
/** If top !== undefined, keep it and jump; else pop and fall through (defaults). */
DEFINED: 3,
/** If top == null, replace top with undefined and jump; else keep top and fall through (`?.`). */
NULLISH: 4,
/** For `[thisValue, callable]`: if callable == null, pop callable, set top to undefined, and jump (`o.m?.()`). */
NULLISH_METHOD: 5,
} as const
/** Sub-ops for `Op.PUT_LOCAL`, `Op.PUT_CELL`, `Op.PUT_UPVAL` (`a` operand). */
export const PutKind = {
/** Normal assignment: checks TDZ (`HOLE`). */
ASSIGN: 0,
/** Declaration initialization or internal slot write: skips TDZ check. */
INIT: 1,
/** Assignment to a `const` or read-only binding: checks TDZ first, then throws `TypeError`. */
CONST: 2,
} as const
/** Sub-ops for `Op.CHECK_BASE` (`a` operand). */
export const CheckBaseKind = {
/** `o[k]`: checks `o` is not null/undefined before converting `k` (`[o, k] -> [o, k]`). */
READ: 0,
/** `o.x = ...`: checks `o.x` is a writable reference (`[o] -> [o]`, `bx = k`). */
WRITE_PROP: 1,
/** `o[k] = ...`: checks `o[k]` is a writable reference after `TO_KEY` (`[o, key] -> [o, key]`). */
WRITE_ELEM: 2,
/** Destructuring target check: throws if top is null or undefined (`[v] -> [v]`). */
DESTRUCTURE: 3,
} as const
/** Sub-ops for `Op.ITER_OPEN` and `Op.ITER_INIT` (`a` operand). */
export const IterContext = {
FOR_OF: 0,
DESTRUCTURE: 1,
SPREAD: 2,
DELEGATE: 3,
FOR_AWAIT: 4,
ASYNC_DELEGATE: 5,
CHECK_OBJECT: 6,
} as const
/** Sub-ops for `Op.IMPORT` (`a` operand). */
export const ImportKind = {
DEFAULT: 0,
NAMESPACE: 1,
NAMED: 2,
SIDE_EFFECT: 3,
DYNAMIC: 4,
} as const
/** Bit flags on `Code.flags`. */
export const CodeFlags = {
ARROW: 1 << 0,
ASYNC: 1 << 1,
GENERATOR: 1 << 2,
SIMPLE_PARAMS: 1 << 3,
USES_THIS: 1 << 4,
USES_ARGUMENTS: 1 << 5,
INTERNAL: 1 << 6,
PROGRAM: 1 << 7,
NEEDS_ARGS: 1 << 9,
} as const
/** Constant pool descriptors. */
export type TemplateSite = {
readonly cooked: ReadonlyArray<string | undefined>
readonly raw: ReadonlyArray<string>
readonly nodeIndex: number
}
export type RegExpSite = {
readonly pattern: string
readonly flags: string
}
export type ErrorSite = {
readonly type: ErrorType
readonly message: string
readonly kind?: DiagnosticKind
readonly suggestions?: ReadonlyArray<string>
}
export type ImportSite = {
readonly specifier: string
readonly imported?: string
}
export type ConstValue = string | number | TemplateSite | RegExpSite | ErrorSite | ImportSite
/** Packs one instruction word: `op:8 | a:8 | bx:16`. Throws if any field does not fit its width. */
export const packWord = (op: OpCode, a = 0, bx = 0): number => {
if ((op & 0xff) !== op || (a & 0xff) !== a || (bx & 0xffff) !== bx) {
throw new RangeError(`Instruction operand out of single-word range: op=${op} a=${a} bx=${bx}`)
}
return op | (a << 8) | (bx << 16)
}