aboutsummaryrefslogtreecommitdiff
path: root/src/njs_vmcode.h
blob: 83507f636812204cf06334dcbd11a41a45f1f6ca (plain)
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/*
 * Copyright (C) Igor Sysoev
 * Copyright (C) NGINX, Inc.
 */

#ifndef _NJS_VMCODE_H_INCLUDED_
#define _NJS_VMCODE_H_INCLUDED_


/*
 * Negative return values handled by nJSVM interpreter as special events.
 * The values must be in range from -1 to -11, because -12 is minimal jump
 * offset on 32-bit platforms.
 *    0  (NJS_OK)   :  njs_vmcode_stop() has stopped execution,
 *                          execution successfully finished
 *    -1 (NJS_ERROR):  error or exception;
 *    -2 .. -11:                 not used.
 */

/* The last return value which preempts execution. */
#define NJS_PREEMPT                     (-11)


typedef intptr_t                        njs_jump_off_t;
typedef uint8_t                         njs_vmcode_t;


enum {
    NJS_VMCODE_PUT_ARG = 0,
    NJS_VMCODE_STOP,
    NJS_VMCODE_JUMP,
    NJS_VMCODE_PROPERTY_ATOM_SET,
    NJS_VMCODE_PROPERTY_SET,
    NJS_VMCODE_PROPERTY_ACCESSOR,
    NJS_VMCODE_IF_TRUE_JUMP,
    NJS_VMCODE_IF_FALSE_JUMP,
    NJS_VMCODE_IF_EQUAL_JUMP,
    NJS_VMCODE_PROPERTY_INIT,
    NJS_VMCODE_RETURN,
    NJS_VMCODE_FUNCTION_COPY,
    NJS_VMCODE_FUNCTION_FRAME,
    NJS_VMCODE_METHOD_FRAME,
    NJS_VMCODE_FUNCTION_CALL,
    NJS_VMCODE_PROPERTY_NEXT,
    NJS_VMCODE_ARGUMENTS,
    NJS_VMCODE_PROTO_INIT,
    NJS_VMCODE_TO_PROPERTY_KEY,
    NJS_VMCODE_TO_PROPERTY_KEY_CHK,
    NJS_VMCODE_SET_FUNCTION_NAME,
    NJS_VMCODE_IMPORT,
    NJS_VMCODE_AWAIT,
    NJS_VMCODE_TRY_START,
    NJS_VMCODE_THROW,
    NJS_VMCODE_TRY_BREAK,
    NJS_VMCODE_TRY_CONTINUE,
    NJS_VMCODE_TRY_END,
    NJS_VMCODE_CATCH,
    NJS_VMCODE_FINALLY,
    NJS_VMCODE_LET,
    NJS_VMCODE_LET_UPDATE,
    NJS_VMCODE_INITIALIZATION_TEST,
    NJS_VMCODE_NOT_INITIALIZED,
    NJS_VMCODE_ASSIGNMENT_ERROR,
    NJS_VMCODE_ERROR,
    NJS_VMCODE_MOVE,
    NJS_VMCODE_PROPERTY_ATOM_GET,
    NJS_VMCODE_PROPERTY_GET,
    NJS_VMCODE_INCREMENT,
    NJS_VMCODE_POST_INCREMENT,
    NJS_VMCODE_DECREMENT,
    NJS_VMCODE_POST_DECREMENT,
    NJS_VMCODE_TRY_RETURN,
    NJS_VMCODE_GLOBAL_GET,
    NJS_VMCODE_LESS,
    NJS_VMCODE_GREATER,
    NJS_VMCODE_LESS_OR_EQUAL,
    NJS_VMCODE_GREATER_OR_EQUAL,
    NJS_VMCODE_ADDITION,
    NJS_VMCODE_EQUAL,
    NJS_VMCODE_NOT_EQUAL,
    NJS_VMCODE_SUBTRACTION,
    NJS_VMCODE_MULTIPLICATION,
    NJS_VMCODE_EXPONENTIATION,
    NJS_VMCODE_DIVISION,
    NJS_VMCODE_REMAINDER,
    NJS_VMCODE_BITWISE_AND,
    NJS_VMCODE_BITWISE_OR,
    NJS_VMCODE_BITWISE_XOR,
    NJS_VMCODE_LEFT_SHIFT,
    NJS_VMCODE_RIGHT_SHIFT,
    NJS_VMCODE_UNSIGNED_RIGHT_SHIFT,
    NJS_VMCODE_TEMPLATE_LITERAL,
    NJS_VMCODE_PROPERTY_IN,
    NJS_VMCODE_PROPERTY_DELETE,
    NJS_VMCODE_PROPERTY_FOREACH,
    NJS_VMCODE_STRICT_EQUAL,
    NJS_VMCODE_STRICT_NOT_EQUAL,
    NJS_VMCODE_TEST_IF_TRUE,
    NJS_VMCODE_TEST_IF_FALSE,
    NJS_VMCODE_COALESCE,
    NJS_VMCODE_UNARY_PLUS,
    NJS_VMCODE_UNARY_NEGATION,
    NJS_VMCODE_BITWISE_NOT,
    NJS_VMCODE_LOGICAL_NOT,
    NJS_VMCODE_OBJECT,
    NJS_VMCODE_ARRAY,
    NJS_VMCODE_FUNCTION,
    NJS_VMCODE_REGEXP,
    NJS_VMCODE_INSTANCE_OF,
    NJS_VMCODE_TYPEOF,
    NJS_VMCODE_VOID,
    NJS_VMCODE_DELETE,
    NJS_VMCODE_DEBUGGER,
    NJS_VMCODES
};


typedef struct {
    njs_vmcode_t               code;
    njs_index_t                operand1;
    njs_index_t                operand2;
    njs_index_t                operand3;
} njs_vmcode_generic_t;


typedef struct {
    njs_vmcode_t               code;
    njs_index_t                index;
} njs_vmcode_1addr_t;


typedef struct {
    njs_vmcode_t               code;
    njs_index_t                dst;
    njs_index_t                src;
} njs_vmcode_2addr_t;


typedef struct {
    njs_vmcode_t               code;
    njs_index_t                dst;
    njs_index_t                src1;
    njs_index_t                src2;
} njs_vmcode_3addr_t;


typedef struct {
    njs_vmcode_t               code;
    njs_index_t                dst;
    njs_index_t                src;
} njs_vmcode_move_t;


typedef struct {
    njs_vmcode_t               code;
    njs_index_t                retval;
} njs_vmcode_object_t;


typedef struct {
     njs_vmcode_t              code;
     njs_index_t               dst;
} njs_vmcode_this_t;


typedef struct {
    njs_vmcode_t               code;
    njs_index_t                dst;
} njs_vmcode_arguments_t;


typedef struct {
    njs_vmcode_t               code;
    njs_index_t                retval;
    uintptr_t                  length;
    uint8_t                    ctor;       /* 1 bit  */
} njs_vmcode_array_t;


typedef struct {
     njs_vmcode_t              code;
     njs_index_t               retval;
} njs_vmcode_template_literal_t;


typedef struct {
    njs_vmcode_t               code;
    njs_index_t                retval;
    njs_function_lambda_t      *lambda;
    njs_bool_t                 async;
} njs_vmcode_function_t;


typedef struct {
    njs_vmcode_t               code;
    njs_index_t                retval;
    njs_regexp_pattern_t       *pattern;
} njs_vmcode_regexp_t;


typedef struct {
    njs_vmcode_t               code;
    njs_index_t                retval;
    njs_index_t                object;
} njs_vmcode_object_copy_t;


typedef struct {
    njs_vmcode_t               code;
    njs_jump_off_t             offset;
} njs_vmcode_jump_t;


typedef struct {
    njs_vmcode_t               code;
    njs_jump_off_t             offset;
    njs_index_t                cond;
} njs_vmcode_cond_jump_t;


typedef struct {
    njs_vmcode_t               code;
    njs_jump_off_t             offset;
    njs_index_t                value1;
    njs_index_t                value2;
} njs_vmcode_equal_jump_t;


typedef struct {
    njs_vmcode_t               code;
    njs_index_t                retval;
    njs_index_t                value;
    njs_jump_off_t             offset;
} njs_vmcode_test_jump_t;


typedef struct {
    njs_vmcode_t               code;
    njs_index_t                value;
    njs_index_t                object;
    njs_index_t                property;
} njs_vmcode_prop_get_t;


typedef struct {
    njs_vmcode_t               code;
    njs_index_t                value;
    njs_index_t                object;
    njs_index_t                property;
} njs_vmcode_prop_set_t;


typedef struct {
    njs_vmcode_t               code;
    njs_index_t                value;
    njs_index_t                object;
    njs_index_t                property;
    uint8_t                    type;
} njs_vmcode_prop_accessor_t;


typedef struct {
    njs_vmcode_t               code;
    njs_index_t                next;
    njs_index_t                object;
    njs_jump_off_t             offset;
} njs_vmcode_prop_foreach_t;


typedef struct {
    njs_vmcode_t               code;
    njs_index_t                retval;
    njs_index_t                object;
    njs_index_t                next;
    njs_jump_off_t             offset;
} njs_vmcode_prop_next_t;


typedef struct {
    njs_vmcode_t               code;
    njs_index_t                value;
    njs_index_t                constructor;
    njs_index_t                object;
} njs_vmcode_instance_of_t;


typedef struct {
    njs_vmcode_t               code;
    njs_index_t                nargs;
    njs_index_t                name;
    uint8_t                    ctor;       /* 1 bit  */
} njs_vmcode_function_frame_t;


typedef struct {
    njs_vmcode_t               code;
    njs_index_t                nargs;
    njs_index_t                object;
    njs_index_t                method;
    uint8_t                    ctor;       /* 1 bit  */
} njs_vmcode_method_frame_t;


typedef struct {
    njs_vmcode_t               code;
    njs_index_t                retval;
} njs_vmcode_function_call_t;


typedef struct {
    njs_vmcode_t               code;
    njs_index_t                retval;
} njs_vmcode_return_t;


typedef struct {
    njs_vmcode_t               code;
    njs_index_t                retval;
} njs_vmcode_stop_t;


typedef struct {
    njs_vmcode_t               code;
    njs_jump_off_t             offset;
    njs_index_t                exception_value;
    njs_index_t                exit_value;
} njs_vmcode_try_start_t;


typedef struct {
    njs_vmcode_t               code;
    njs_jump_off_t             offset;
    njs_index_t                exit_value;
} njs_vmcode_try_trampoline_t;


typedef struct {
    njs_vmcode_t               code;
    njs_jump_off_t             offset;
    njs_index_t                exception;
} njs_vmcode_catch_t;


typedef struct {
    njs_vmcode_t               code;
    njs_index_t                retval;
} njs_vmcode_throw_t;


typedef struct {
    njs_vmcode_t               code;
    njs_jump_off_t             offset;
} njs_vmcode_try_end_t;


typedef struct {
    njs_vmcode_t               code;
    njs_index_t                save;
    njs_index_t                retval;
    njs_jump_off_t             offset;
} njs_vmcode_try_return_t;


typedef struct {
    njs_vmcode_t               code;
    njs_index_t                retval;
    njs_index_t                exit_value;
    njs_jump_off_t             continue_offset;
    njs_jump_off_t             break_offset;
} njs_vmcode_finally_t;


typedef struct {
    njs_vmcode_t               code;
    njs_object_type_t          type;
    union {
        njs_str_t              name;
        njs_str_t              message;
    } u;
} njs_vmcode_error_t;


typedef struct {
    njs_vmcode_t               code;
    njs_value_t                *function;
    njs_index_t                retval;
} njs_vmcode_function_copy_t;


typedef struct {
    njs_vmcode_t               code;
    njs_index_t                retval;
    njs_mod_t                  *module;
} njs_vmcode_import_t;


typedef struct {
    njs_vmcode_t               code;
    njs_index_t                dst;
} njs_vmcode_variable_t;


typedef struct {
    njs_vmcode_t               code;
    njs_index_t                retval;
} njs_vmcode_debugger_t;


typedef struct {
    njs_vmcode_t               code;
    njs_index_t                retval;
} njs_vmcode_await_t;


njs_int_t njs_vmcode_interpreter(njs_vm_t *vm, u_char *pc, njs_value_t *retval,
    void *promise_cap, void *async_ctx);

njs_object_t *njs_function_new_object(njs_vm_t *vm, njs_value_t *constructor);

#ifdef NJS_DEBUG_OPCODE
#define njs_vmcode_debug(vm, pc, prefix) {                                    \
        if (vm->options.opcode_debug) do {                                    \
            njs_vm_code_t  *code;                                             \
                                                                              \
            code = njs_lookup_code(vm, pc);                                   \
                                                                              \
            njs_printf("%s %V\n", prefix,                                     \
                       (code != NULL) ? &code->name : &njs_entry_unknown);    \
        } while (0);                                                          \
    }

#define njs_vmcode_debug_opcode()                                             \
    if (vm->options.opcode_debug) {                                           \
        njs_disassemble(pc, NULL, 1, NULL);                                   \
    }
#else
#define njs_vmcode_debug(vm, pc, prefix)
#define njs_vmcode_debug_opcode()
#endif

#endif /* _NJS_VMCODE_H_INCLUDED_ */