forked from microsoft/devicescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvm_ops.c
More file actions
762 lines (644 loc) · 24.7 KB
/
vm_ops.c
File metadata and controls
762 lines (644 loc) · 24.7 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
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
#include "devs_internal.h"
#include "devs_vm_internal.h"
#include <limits.h>
#include <math.h>
static void stmt1_return(devs_activation_t *frame, devs_ctx_t *ctx) {
devs_fiber_t *f = ctx->curr_fiber;
f->ret_val = devs_vm_pop_arg(ctx);
devs_fiber_return_from_call(f, frame);
if (ctx->dbg_en && ctx->step_fn == frame && (ctx->step_flags & DEVS_CTX_STEP_OUT)) {
devs_vm_suspend(ctx, JD_DEVS_DBG_SUSPENSION_TYPE_STEP);
}
}
static void stmt0_debugger(devs_activation_t *frame, devs_ctx_t *ctx) {
devs_vm_suspend(ctx, JD_DEVS_DBG_SUSPENSION_TYPE_DEBUGGER_STMT);
}
static void set_alloc(devs_activation_t *frame, devs_ctx_t *ctx, void *p, unsigned sz) {
if (p == NULL)
devs_oom(ctx, sz);
ctx->curr_fiber->ret_val = devs_value_from_gc_obj(ctx, p);
}
static void stmt0_alloc_map(devs_activation_t *frame, devs_ctx_t *ctx) {
set_alloc(
frame, ctx,
devs_map_try_alloc(ctx, devs_get_builtin_object(ctx, DEVS_BUILTIN_OBJECT_OBJECT_PROTOTYPE)),
sizeof(devs_map_t));
}
static void stmt1_alloc_array(devs_activation_t *frame, devs_ctx_t *ctx) {
uint32_t sz = devs_vm_pop_arg_u32(ctx);
devs_array_t *arr = devs_array_try_alloc(ctx, sz);
set_alloc(frame, ctx, arr, sizeof(devs_array_t) + sz * sizeof(value_t));
if (arr)
arr->length = 0; // size is only suggestion
}
static void stmt1_alloc_buffer(devs_activation_t *frame, devs_ctx_t *ctx) {
uint32_t sz = devs_vm_pop_arg_u32(ctx);
devs_buffer_t *obj = devs_buffer_try_alloc(ctx, sz);
// DMESG("buf=%p %p", obj, (void*)obj->gc.header);
set_alloc(frame, ctx, obj, sizeof(devs_buffer_t) + sz);
}
static void stmt3_index_set(devs_activation_t *frame, devs_ctx_t *ctx) {
value_t v = devs_vm_pop_arg(ctx);
value_t idx = devs_vm_pop_arg(ctx);
value_t obj = devs_vm_pop_arg(ctx);
devs_any_set(ctx, obj, idx, v);
}
static void stmt2_index_delete(devs_activation_t *frame, devs_ctx_t *ctx) {
value_t idx = devs_vm_pop_arg(ctx);
value_t obj = devs_vm_pop_arg(ctx);
ctx->curr_fiber->ret_val = devs_false;
ctx->diag_field = idx;
devs_map_t *map = devs_object_get_attached_rw(ctx, obj);
if (!map)
return;
bool str = devs_is_string(ctx, idx);
if (!str) {
idx = devs_value_to_string(ctx, idx);
devs_value_pin(ctx, idx);
}
if (devs_map_delete(ctx, map, idx) == 0)
ctx->curr_fiber->ret_val = devs_true;
if (!str)
devs_value_unpin(ctx, idx);
}
static void stmt_callN(devs_activation_t *frame, devs_ctx_t *ctx, unsigned N) {
JD_ASSERT(ctx->stack_top == N + 1);
ctx->stack_top = 0;
#if 0
devs_log_value(ctx, "fn", ctx->the_stack[0]);
devs_log_value(ctx, "a0", ctx->the_stack[1]);
#endif
devs_fiber_call_function(ctx->curr_fiber, N, NULL);
}
#define STMT_CALL(n, k) \
static void n(devs_activation_t *frame, devs_ctx_t *ctx) { \
stmt_callN(frame, ctx, k); \
}
STMT_CALL(stmt1_call0, 0)
STMT_CALL(stmt2_call1, 1)
STMT_CALL(stmt3_call2, 2)
STMT_CALL(stmt4_call3, 3)
STMT_CALL(stmt5_call4, 4)
STMT_CALL(stmt6_call5, 5)
STMT_CALL(stmt7_call6, 6)
STMT_CALL(stmt8_call7, 7)
STMT_CALL(stmt9_call8, 8)
static void stmt2_call_array(devs_activation_t *frame, devs_ctx_t *ctx) {
value_t args = devs_vm_pop_arg(ctx);
value_t fn = devs_vm_pop_arg(ctx);
(void)fn;
if (!devs_is_array(ctx, args))
devs_throw_expecting_error(ctx, DEVS_BUILTIN_STRING_ARRAY, args);
else {
JD_ASSERT(ctx->stack_top == 0); // fn needs to be at stack top
ctx->stack_top_for_gc = 1;
devs_fiber_call_function(ctx->curr_fiber, 0, devs_value_to_gc_obj(ctx, args));
}
}
static int get_pc(devs_activation_t *frame, devs_ctx_t *ctx) {
int32_t off = ctx->literal_int;
int pc = ctx->jmp_pc + off;
if ((int)frame->func->start <= pc && pc < frame->maxpc) {
return pc;
} else {
devs_invalid_program(ctx, 60105);
return 0;
}
}
static void stmtx_jmp(devs_activation_t *frame, devs_ctx_t *ctx) {
int pc = get_pc(frame, ctx);
if (pc)
frame->pc = pc;
}
static void stmtx1_jmp_z(devs_activation_t *frame, devs_ctx_t *ctx) {
int cond = devs_value_to_bool(ctx, devs_vm_pop_arg(ctx));
int pc = get_pc(frame, ctx);
if (pc && !cond)
frame->pc = pc;
}
static void stmtx_jmp_ret_val_z(devs_activation_t *frame, devs_ctx_t *ctx) {
int pc = get_pc(frame, ctx);
if (pc && devs_is_null_or_undefined(ctx->curr_fiber->ret_val)) {
ctx->curr_fiber->ret_val = devs_undefined; // null => undefined
frame->pc = pc;
}
}
static void stmtx_try(devs_activation_t *frame, devs_ctx_t *ctx) {
int pc = get_pc(frame, ctx);
devs_push_tryframe(frame, ctx, pc);
}
static void stmtx_end_try(devs_activation_t *frame, devs_ctx_t *ctx) {
int pc = get_pc(frame, ctx);
if (pc)
frame->pc = pc;
if (devs_pop_tryframe(frame, ctx) == 0)
devs_invalid_program(ctx, 60106);
}
static void stmt0_catch(devs_activation_t *frame, devs_ctx_t *ctx) {
// no regular execution of catch()
devs_invalid_program(ctx, 60107);
}
static void stmt0_finally(devs_activation_t *frame, devs_ctx_t *ctx) {
// regular execution of finally() clears the exception value
// and pops the top of the try stack, that has to match the location of the finally()
int pc = devs_pop_tryframe(frame, ctx);
if (pc == frame->pc - 1)
ctx->curr_fiber->ret_val = devs_undefined;
else
devs_invalid_program(ctx, 60109);
}
static void stmt1_throw(devs_activation_t *frame, devs_ctx_t *ctx) {
value_t exn = devs_vm_pop_arg(ctx);
if (devs_is_null_or_undefined(exn)) {
devs_throw_type_error(ctx, "throwing null/undefined");
} else {
devs_throw(ctx, exn, 0);
}
}
static void stmtx1_throw_jmp(devs_activation_t *frame, devs_ctx_t *ctx) {
unsigned lev = devs_vm_pop_arg_i32(ctx);
if (lev > DEVS_SPECIAL_THROW_JMP_LEVEL_MAX) {
devs_invalid_program(ctx, 60110);
return;
}
int pc = get_pc(frame, ctx);
if (pc) {
value_t exn = devs_value_encode_throw_jmp_pc(pc, lev);
devs_throw(ctx, exn, DEVS_THROW_NO_STACK);
}
}
static void stmt1_re_throw(devs_activation_t *frame, devs_ctx_t *ctx) {
value_t exn = devs_vm_pop_arg(ctx);
if (devs_is_undefined(exn)) {
// no-op
} else {
devs_throw(ctx, exn, DEVS_THROW_NO_STACK);
}
}
static void stmtx1_store_local(devs_activation_t *frame, devs_ctx_t *ctx) {
value_t v = devs_vm_pop_arg(ctx);
unsigned off = ctx->literal_int;
// DMESG("store L%d := %d st=%d", off, devs_value_to_int(v), frame->func->start);
if (off >= frame->func->num_slots)
devs_invalid_program(ctx, 60111);
else
frame->slots[off] = v;
}
static void stmt1_store_ret_val(devs_activation_t *frame, devs_ctx_t *ctx) {
value_t v = devs_vm_pop_arg(ctx);
ctx->curr_fiber->ret_val = v;
}
static value_t *lookup_clo_val(devs_activation_t *frame, devs_ctx_t *ctx) {
int level = devs_vm_pop_arg_i32(ctx);
unsigned off = ctx->literal_int;
if (level <= 0)
return NULL;
devs_activation_t *closure = frame;
while (closure && level-- > 0)
closure = closure->closure;
if (closure && off < closure->func->num_slots)
return &closure->slots[off];
return NULL;
}
static void stmtx2_store_closure(devs_activation_t *frame, devs_ctx_t *ctx) {
value_t v = devs_vm_pop_arg(ctx);
value_t *dst = lookup_clo_val(frame, ctx);
if (dst == NULL)
devs_invalid_program(ctx, 60112);
else
*dst = v;
}
static void stmtx1_store_global(devs_activation_t *frame, devs_ctx_t *ctx) {
value_t v = devs_vm_pop_arg(ctx);
unsigned off = ctx->literal_int;
if (off >= ctx->img.header->num_globals)
devs_invalid_program(ctx, 60113);
else
ctx->globals[off] = v;
}
static void stmt4_store_buffer(devs_activation_t *frame, devs_ctx_t *ctx) {
value_t val = devs_vm_pop_arg(ctx);
uint32_t offset = devs_vm_pop_arg_u32(ctx);
uint32_t fmt0 = devs_vm_pop_arg_u32(ctx);
value_t buffer = devs_vm_pop_arg_buffer(ctx, DEVS_BUFFER_RW);
if (!devs_is_undefined(buffer))
devs_buffer_op(ctx, fmt0, offset, buffer, &val);
}
static value_t expr_invalid(devs_activation_t *frame, devs_ctx_t *ctx) {
return devs_invalid_program(ctx, 60114);
}
static value_t exprx_load_local(devs_activation_t *frame, devs_ctx_t *ctx) {
unsigned off = ctx->literal_int;
if (off < frame->func->num_slots)
return frame->slots[off];
return devs_invalid_program(ctx, 60115);
}
static value_t exprx1_load_closure(devs_activation_t *frame, devs_ctx_t *ctx) {
value_t *src = lookup_clo_val(frame, ctx);
if (src == NULL) {
return devs_invalid_program(ctx, 60116);
} else {
return *src;
}
}
static value_t exprx_make_closure(devs_activation_t *frame, devs_ctx_t *ctx) {
unsigned fidx = ctx->literal_int;
if (fidx >= devs_img_num_functions(ctx->img)) {
return devs_invalid_program(ctx, 60117);
} else {
return devs_make_closure(ctx, frame, fidx);
}
}
static value_t exprx_load_global(devs_activation_t *frame, devs_ctx_t *ctx) {
unsigned off = ctx->literal_int;
if (off < ctx->img.header->num_globals)
return ctx->globals[off];
return devs_invalid_program(ctx, 60118);
}
static value_t expr3_load_buffer(devs_activation_t *frame, devs_ctx_t *ctx) {
uint32_t offset = devs_vm_pop_arg_u32(ctx);
uint32_t fmt0 = devs_vm_pop_arg_u32(ctx);
value_t buf = devs_vm_pop_arg_buffer(ctx, DEVS_BUFFER_STRING_OK);
if (devs_is_undefined(buf))
return devs_undefined;
return devs_buffer_op(ctx, fmt0, offset, buf, NULL);
}
static value_t exprx_literal(devs_activation_t *frame, devs_ctx_t *ctx) {
int32_t v = ctx->literal_int;
return devs_value_from_int(v);
}
static value_t exprx_literal_f64(devs_activation_t *frame, devs_ctx_t *ctx) {
unsigned off = ctx->literal_int;
if (off < devs_img_num_floats(ctx->img))
return devs_img_get_float(ctx->img, off);
return devs_invalid_program(ctx, 60119);
}
static value_t expr0_ret_val(devs_activation_t *frame, devs_ctx_t *ctx) {
return ctx->curr_fiber->ret_val;
}
static value_t expr1_new(devs_activation_t *frame, devs_ctx_t *ctx) {
value_t func = devs_vm_pop_arg(ctx);
value_t th;
devs_activation_t *clo;
int fidx = devs_get_fnidx(ctx, func, &th, &clo);
if (fidx < 0)
return devs_throw_type_error(ctx, "new unsupported on this expression");
int bltin = fidx - DEVS_FIRST_BUILTIN_FUNCTION;
if (bltin >= 0) {
JD_ASSERT(bltin < devs_num_builtin_functions);
const devs_builtin_function_t *h = &devs_builtin_functions[bltin];
if (!(h->flags & DEVS_BUILTIN_FLAG_IS_CTOR))
return devs_throw_type_error(ctx, "builtin function is not a ctor");
} else {
JD_ASSERT(fidx < (int)devs_img_num_functions(ctx->img));
const devs_function_desc_t *func = devs_img_get_function(ctx->img, fidx);
if (!(func->flags & DEVS_FUNCTIONFLAG_IS_CTOR))
return devs_throw_type_error(ctx, "function is not a ctor");
}
return func;
// return devs_function_bind(ctx, devs_new, func);
}
static value_t expr2_bind(devs_activation_t *frame, devs_ctx_t *ctx) {
value_t obj = devs_vm_pop_arg(ctx);
value_t func = devs_vm_pop_arg(ctx);
// special case for built-in constructors
if (devs_handle_type(func) == DEVS_HANDLE_TYPE_SPECIAL &&
devs_handle_is_builtin(devs_handle_value(func))) {
func = devs_object_get_built_in_field(ctx, func, DEVS_BUILTIN_STRING___FUNC__);
}
return devs_function_bind(ctx, obj, func);
}
static value_t exprx_static_function(devs_activation_t *frame, devs_ctx_t *ctx) {
unsigned fidx = ctx->literal_int;
if (fidx >= devs_img_num_functions(ctx->img)) {
return devs_invalid_program(ctx, 60120);
} else {
return devs_value_from_handle(DEVS_HANDLE_TYPE_STATIC_FUNCTION, fidx);
}
}
static value_t exprx_static_spec(devs_activation_t *frame, devs_ctx_t *ctx) {
unsigned fidx = ctx->literal_int;
if (fidx >= ctx->img.header->num_service_specs) {
return devs_invalid_program(ctx, 60128);
} else {
return devs_value_from_service_spec_idx(ctx, fidx);
}
}
static value_t exprx_static_spec_proto(devs_activation_t *frame, devs_ctx_t *ctx) {
unsigned fidx = ctx->literal_int;
if (fidx >= ctx->img.header->num_service_specs) {
return devs_invalid_program(ctx, 60131);
} else {
return devs_value_from_gc_obj(ctx, devs_get_spec_proto(ctx, fidx));
}
}
value_t devs_value_bufferish(devs_ctx_t *ctx, unsigned tp, uint32_t lit) {
uint32_t v = (tp << DEVS_STRIDX__SHIFT) | lit;
if (!devs_img_stridx_ok(ctx->img, v))
return devs_undefined;
return devs_value_from_handle(DEVS_HANDLE_TYPE_IMG_BUFFERISH, v);
}
static value_t static_something(devs_ctx_t *ctx, unsigned tp) {
value_t r = devs_value_bufferish(ctx, tp, ctx->literal_int);
if (devs_is_undefined(r))
devs_invalid_program(ctx, 60122);
return r;
}
static inline value_t get_field_ex(devs_ctx_t *ctx, unsigned tp, value_t obj) {
value_t fld = static_something(ctx, tp);
if (devs_is_undefined(fld))
return devs_undefined;
else
return devs_object_get(ctx, obj, fld);
}
static inline value_t get_field(devs_ctx_t *ctx, unsigned tp) {
return get_field_ex(ctx, tp, devs_vm_pop_arg(ctx));
}
static value_t exprx_static_buffer(devs_activation_t *frame, devs_ctx_t *ctx) {
return static_something(ctx, DEVS_STRIDX_BUFFER);
}
static value_t exprx_static_ascii_string(devs_activation_t *frame, devs_ctx_t *ctx) {
return static_something(ctx, DEVS_STRIDX_ASCII);
}
static value_t exprx_static_utf8_string(devs_activation_t *frame, devs_ctx_t *ctx) {
return static_something(ctx, DEVS_STRIDX_UTF8);
}
static value_t exprx_static_builtin_string(devs_activation_t *frame, devs_ctx_t *ctx) {
return static_something(ctx, DEVS_STRIDX_BUILTIN);
}
static value_t exprx1_builtin_field(devs_activation_t *frame, devs_ctx_t *ctx) {
return get_field(ctx, DEVS_STRIDX_BUILTIN);
}
static value_t exprx1_ascii_field(devs_activation_t *frame, devs_ctx_t *ctx) {
return get_field(ctx, DEVS_STRIDX_ASCII);
}
static value_t exprx1_utf8_field(devs_activation_t *frame, devs_ctx_t *ctx) {
return get_field(ctx, DEVS_STRIDX_UTF8);
}
static value_t get_builtin_field(devs_ctx_t *ctx, unsigned obj) {
value_t fld = static_something(ctx, DEVS_STRIDX_BUILTIN);
return devs_maplike_get_no_bind(ctx, devs_get_builtin_object(ctx, obj), fld);
}
static value_t exprx_math_field(devs_activation_t *frame, devs_ctx_t *ctx) {
return get_builtin_field(ctx, DEVS_BUILTIN_OBJECT_MATH);
}
static value_t exprx_object_field(devs_activation_t *frame, devs_ctx_t *ctx) {
return get_builtin_field(ctx, DEVS_BUILTIN_OBJECT_OBJECT);
}
static value_t exprx_ds_field(devs_activation_t *frame, devs_ctx_t *ctx) {
return get_builtin_field(ctx, DEVS_BUILTIN_OBJECT_DEVICESCRIPT);
}
static value_t exprx_builtin_object(devs_activation_t *frame, devs_ctx_t *ctx) {
return devs_builtin_object_value(ctx, ctx->literal_int);
}
static value_t expr2_index(devs_activation_t *frame, devs_ctx_t *ctx) {
value_t idx = devs_vm_pop_arg(ctx);
value_t arr = devs_vm_pop_arg(ctx);
return devs_any_get(ctx, arr, idx);
}
static value_t expr1_typeof(devs_activation_t *frame, devs_ctx_t *ctx) {
value_t obj = devs_vm_pop_arg(ctx);
return devs_value_from_int(devs_value_typeof(ctx, obj));
}
static const uint8_t typeof_map[] = {
[DEVS_OBJECT_TYPE_UNDEFINED] = DEVS_BUILTIN_STRING_UNDEFINED,
[DEVS_OBJECT_TYPE_NULL] = DEVS_BUILTIN_STRING_OBJECT,
[DEVS_OBJECT_TYPE_NUMBER] = DEVS_BUILTIN_STRING_NUMBER,
[DEVS_OBJECT_TYPE_MAP] = DEVS_BUILTIN_STRING_OBJECT,
[DEVS_OBJECT_TYPE_ARRAY] = DEVS_BUILTIN_STRING_OBJECT,
[DEVS_OBJECT_TYPE_BUFFER] = DEVS_BUILTIN_STRING_OBJECT,
[DEVS_OBJECT_TYPE_IMAGE] = DEVS_BUILTIN_STRING_OBJECT,
[DEVS_OBJECT_TYPE_ROLE] = DEVS_BUILTIN_STRING_OBJECT,
[DEVS_OBJECT_TYPE_BOOL] = DEVS_BUILTIN_STRING_BOOLEAN,
[DEVS_OBJECT_TYPE_FIBER] = DEVS_BUILTIN_STRING_OBJECT,
[DEVS_OBJECT_TYPE_FUNCTION] = DEVS_BUILTIN_STRING_FUNCTION,
[DEVS_OBJECT_TYPE_STRING] = DEVS_BUILTIN_STRING_STRING,
[DEVS_OBJECT_TYPE_PACKET] = DEVS_BUILTIN_STRING_OBJECT,
[DEVS_OBJECT_TYPE_EXOTIC] = DEVS_BUILTIN_STRING_OBJECT,
};
static value_t expr1_typeof_str(devs_activation_t *frame, devs_ctx_t *ctx) {
value_t obj = devs_vm_pop_arg(ctx);
unsigned idx = devs_value_typeof(ctx, obj);
if (idx < sizeof(typeof_map))
idx = typeof_map[idx];
else
idx = 0;
if (idx == 0)
return devs_undefined;
else {
ctx->literal_int = idx;
return static_something(ctx, DEVS_STRIDX_BUILTIN);
}
}
static value_t expr0_null(devs_activation_t *frame, devs_ctx_t *ctx) {
return devs_null;
}
static value_t expr0_undefined(devs_activation_t *frame, devs_ctx_t *ctx) {
return devs_undefined;
}
static value_t expr1_is_undefined(devs_activation_t *frame, devs_ctx_t *ctx) {
value_t obj = devs_vm_pop_arg(ctx);
if (devs_is_undefined(obj))
return devs_true;
else
return devs_false;
}
static value_t expr1_is_nullish(devs_activation_t *frame, devs_ctx_t *ctx) {
value_t obj = devs_vm_pop_arg(ctx);
if (devs_is_null_or_undefined(obj))
return devs_true;
else
return devs_false;
}
static value_t expr2_instance_of(devs_activation_t *frame, devs_ctx_t *ctx) {
value_t cls = devs_vm_pop_arg(ctx);
value_t obj = devs_vm_pop_arg(ctx);
return devs_value_from_bool(devs_instance_of(ctx, obj, devs_get_prototype_field(ctx, cls)));
}
static value_t expr0_true(devs_activation_t *frame, devs_ctx_t *ctx) {
return devs_true;
}
static value_t expr0_false(devs_activation_t *frame, devs_ctx_t *ctx) {
return devs_false;
}
//
// Math stuff
//
static value_t expr0_nan(devs_activation_t *frame, devs_ctx_t *ctx) {
return devs_nan;
}
static value_t expr0_inf(devs_activation_t *frame, devs_ctx_t *ctx) {
return devs_inf;
}
static value_t expr1_abs(devs_activation_t *frame, devs_ctx_t *ctx) {
value_t v = devs_vm_pop_arg(ctx);
if (!devs_is_tagged_int(v)) {
double f = devs_value_to_double(ctx, v);
return f < 0 ? devs_value_from_double(-f) : v;
}
int q = v.val_int32;
if (q < 0) {
if (q == INT_MIN)
return devs_max_int_1;
else
return devs_value_from_int(-q);
} else
return v;
}
static value_t expr1_bit_not(devs_activation_t *frame, devs_ctx_t *ctx) {
return devs_value_from_int(~devs_vm_pop_arg_i32(ctx));
}
static value_t expr1_to_int(devs_activation_t *frame, devs_ctx_t *ctx) {
return devs_value_from_int(devs_vm_pop_arg_i32(ctx));
}
static value_t expr1_is_nan(devs_activation_t *frame, devs_ctx_t *ctx) {
value_t v = devs_vm_pop_arg(ctx);
return devs_value_from_bool(devs_is_nan(v));
}
static value_t expr1_neg(devs_activation_t *frame, devs_ctx_t *ctx) {
value_t v = devs_vm_pop_arg(ctx);
if (!devs_is_tagged_int(v))
return devs_value_from_double(-devs_value_to_double(ctx, v));
if (v.val_int32 == INT_MIN)
return devs_max_int_1;
else
return devs_value_from_int(-v.val_int32);
}
static value_t expr1_uplus(devs_activation_t *frame, devs_ctx_t *ctx) {
value_t v = devs_vm_pop_arg(ctx);
if (devs_is_number(v) || devs_is_nan(v))
return v;
return devs_value_from_double(devs_value_to_double(ctx, v));
}
static value_t expr1_not(devs_activation_t *frame, devs_ctx_t *ctx) {
value_t v = devs_vm_pop_arg(ctx);
return devs_value_from_bool(!devs_value_to_bool(ctx, v));
}
static value_t expr1_to_bool(devs_activation_t *frame, devs_ctx_t *ctx) {
value_t v = devs_vm_pop_arg(ctx);
return devs_value_from_bool(devs_value_to_bool(ctx, v));
}
static int exec2_and_check_int(devs_activation_t *frame, devs_ctx_t *ctx) {
ctx->binop[1] = devs_vm_pop_arg(ctx);
ctx->binop[0] = devs_vm_pop_arg(ctx);
return devs_is_tagged_int(ctx->binop[0]) && devs_is_tagged_int(ctx->binop[1]);
}
#define aa ctx->binop[0].val_int32
#define bb ctx->binop[1].val_int32
#define af ctx->binop_f[0]
#define bf ctx->binop_f[1]
static bool either_is_string(devs_ctx_t *ctx) {
return devs_is_string(ctx, ctx->binop[0]) || devs_is_string(ctx, ctx->binop[1]);
}
static void force_double(devs_ctx_t *ctx) {
af = devs_value_to_double(ctx, ctx->binop[0]);
bf = devs_value_to_double(ctx, ctx->binop[1]);
}
static void exec2_and_force_int(devs_activation_t *frame, devs_ctx_t *ctx) {
bb = devs_vm_pop_arg_i32(ctx);
aa = devs_vm_pop_arg_i32(ctx);
}
static int exec2_and_check_int_or_force_double(devs_activation_t *frame, devs_ctx_t *ctx) {
if (exec2_and_check_int(frame, ctx))
return 1;
force_double(ctx);
return 0;
}
static value_t expr2_add(devs_activation_t *frame, devs_ctx_t *ctx) {
if (exec2_and_check_int(frame, ctx)) {
int r;
if (!__builtin_sadd_overflow(aa, bb, &r))
return devs_value_from_int(r);
}
if (either_is_string(ctx))
return devs_string_concat(ctx, ctx->binop[0], ctx->binop[1]);
force_double(ctx);
return devs_value_from_double(af + bf);
}
static value_t expr2_sub(devs_activation_t *frame, devs_ctx_t *ctx) {
if (exec2_and_check_int(frame, ctx)) {
int r;
if (!__builtin_ssub_overflow(aa, bb, &r))
return devs_value_from_int(r);
}
force_double(ctx);
return devs_value_from_double(af - bf);
}
static value_t expr2_mul(devs_activation_t *frame, devs_ctx_t *ctx) {
if (exec2_and_check_int(frame, ctx)) {
int r;
if (!__builtin_smul_overflow(aa, bb, &r))
return devs_value_from_int(r);
}
force_double(ctx);
return devs_value_from_double(af * bf);
}
static value_t expr2_div(devs_activation_t *frame, devs_ctx_t *ctx) {
if (exec2_and_check_int(frame, ctx)) {
int r;
// not sure this is worth it on M0+; it definitely is on M4
if (bb != 0 && (bb != -1 || aa != INT_MIN) && ((r = aa / bb)) * bb == aa)
return devs_value_from_int(r);
}
force_double(ctx);
return devs_value_from_double(af / bf);
}
static value_t expr2_bit_and(devs_activation_t *frame, devs_ctx_t *ctx) {
exec2_and_force_int(frame, ctx);
return devs_value_from_int(aa & bb);
}
static value_t expr2_bit_or(devs_activation_t *frame, devs_ctx_t *ctx) {
exec2_and_force_int(frame, ctx);
return devs_value_from_int(aa | bb);
}
static value_t expr2_bit_xor(devs_activation_t *frame, devs_ctx_t *ctx) {
exec2_and_force_int(frame, ctx);
return devs_value_from_int(aa ^ bb);
}
static value_t expr2_shift_left(devs_activation_t *frame, devs_ctx_t *ctx) {
exec2_and_force_int(frame, ctx);
return devs_value_from_int(aa << (31 & bb));
}
static value_t expr2_shift_right(devs_activation_t *frame, devs_ctx_t *ctx) {
exec2_and_force_int(frame, ctx);
return devs_value_from_int(aa >> (31 & bb));
}
static value_t expr2_shift_right_unsigned(devs_activation_t *frame, devs_ctx_t *ctx) {
exec2_and_force_int(frame, ctx);
uint32_t tmp = (uint32_t)aa >> (31 & bb);
if (tmp >> 31)
return devs_value_from_double(tmp);
else
return devs_value_from_int(tmp);
}
static value_t expr2_eq(devs_activation_t *frame, devs_ctx_t *ctx) {
if (exec2_and_check_int(frame, ctx))
return devs_value_from_bool(aa == bb);
return devs_value_from_bool(devs_value_ieee_eq(ctx, ctx->binop[0], ctx->binop[1]));
}
static value_t expr2_ne(devs_activation_t *frame, devs_ctx_t *ctx) {
if (exec2_and_check_int(frame, ctx))
return devs_value_from_bool(aa != bb);
return devs_value_from_bool(!devs_value_ieee_eq(ctx, ctx->binop[0], ctx->binop[1]));
}
static value_t expr2_approx_eq(devs_activation_t *frame, devs_ctx_t *ctx) {
if (exec2_and_check_int(frame, ctx))
return devs_value_from_bool(aa == bb);
return devs_value_from_bool(devs_value_approx_eq(ctx, ctx->binop[0], ctx->binop[1]));
}
static value_t expr2_approx_ne(devs_activation_t *frame, devs_ctx_t *ctx) {
if (exec2_and_check_int(frame, ctx))
return devs_value_from_bool(aa != bb);
return devs_value_from_bool(!devs_value_approx_eq(ctx, ctx->binop[0], ctx->binop[1]));
}
static value_t expr2_le(devs_activation_t *frame, devs_ctx_t *ctx) {
if (exec2_and_check_int_or_force_double(frame, ctx))
return devs_value_from_bool(aa <= bb);
return devs_value_from_bool(af <= bf);
}
static value_t expr2_lt(devs_activation_t *frame, devs_ctx_t *ctx) {
if (exec2_and_check_int_or_force_double(frame, ctx))
return devs_value_from_bool(aa < bb);
return devs_value_from_bool(af < bf);
}
const void *const devs_vm_op_handlers[DEVS_OP_PAST_LAST + 1] = {DEVS_OP_HANDLERS};