forked from arendst/Tasmota
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbe_vm.c
1419 lines (1361 loc) · 48.5 KB
/
be_vm.c
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
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/********************************************************************
** Copyright (c) 2018-2020 Guan Wenliang
** This file is part of the Berry default interpreter.
** [email protected], https://github.com/Skiars/berry
** See Copyright Notice in the LICENSE file or at
** https://github.com/Skiars/berry/blob/master/LICENSE
********************************************************************/
#include "be_vm.h"
#include "be_decoder.h"
#include "be_string.h"
#include "be_strlib.h"
#include "be_class.h"
#include "be_func.h"
#include "be_vector.h"
#include "be_list.h"
#include "be_map.h"
#include "be_module.h"
#include "be_mem.h"
#include "be_var.h"
#include "be_gc.h"
#include "be_exec.h"
#include "be_debug.h"
#include "be_libs.h"
#include <string.h>
#include <math.h>
#define NOT_METHOD BE_NONE
#define vm_error(vm, except, ...) \
be_raise(vm, except, be_pushfstring(vm, __VA_ARGS__))
#define RA() (reg + IGET_RA(ins)) /* Get value of register A */
#define RKB() ((isKB(ins) ? ktab : reg) + KR2idx(IGET_RKB(ins))) /* Get value of register or constant B */
#define RKC() ((isKC(ins) ? ktab : reg) + KR2idx(IGET_RKC(ins))) /* Get value of register or constant C */
#define var2cl(_v) cast(bclosure*, var_toobj(_v)) /* cast var to closure */
#define var2real(_v) (var_isreal(_v) ? (_v)->v.r : (breal)(_v)->v.i) /* get var as real or convert to real if integer */
#define val2bool(v) ((v) ? btrue : bfalse) /* get var as bool (trur if non zero) */
#define ibinop(op, a, b) ((a)->v.i op (b)->v.i) /* apply binary operator to both arguments as integers */
#if BE_USE_DEBUG_HOOK
#define DEBUG_HOOK() \
if (vm->hookmask & BE_HOOK_LINE) { \
do_linehook(vm); \
reg = vm->reg; \
}
#else
#define DEBUG_HOOK()
#endif
#if BE_USE_PERF_COUNTERS
#define COUNTER_HOOK() \
vm->counter_ins++;
#else
#define COUNTER_HOOK()
#endif
#if BE_USE_PERF_COUNTERS
#define VM_HEARTBEAT() \
if ((vm->counter_ins & ((1<<(BE_VM_OBSERVABILITY_SAMPLING - 1))-1) ) == 0) { /* call every 2^BE_VM_OBSERVABILITY_SAMPLING instructions */ \
if (vm->obshook != NULL) \
(*vm->obshook)(vm, BE_OBS_VM_HEARTBEAT, vm->counter_ins); \
}
#else
#define VM_HEARTBEAT()
#endif
#define vm_exec_loop() \
loop: \
DEBUG_HOOK(); \
COUNTER_HOOK(); \
VM_HEARTBEAT(); \
switch (IGET_OP(ins = *vm->ip++))
#if BE_USE_SINGLE_FLOAT
#define mathfunc(func) func##f
#else
#define mathfunc(func) func
#endif
#define opcase(opcode) case OP_##opcode
#define dispatch() goto loop
#define equal_rule(op, iseq) \
bbool res; \
be_assert(!var_isstatic(a)); \
be_assert(!var_isstatic(b)); \
if (var_isint(a) && var_isint(b)) { \
res = ibinop(op, a, b); \
} else if (var_isnumber(a) && var_isnumber(b)) { \
res = var2real(a) op var2real(b); \
} else if (var_isinstance(a) && !var_isnil(b)) { \
res = object_eqop(vm, #op, iseq, a, b); \
} else if (var_primetype(a) == var_primetype(b)) { /* same types */ \
if (var_isnil(a)) { /* nil op nil */ \
res = 1 op 1; \
} else if (var_isbool(a)) { /* bool op bool */ \
res = var_tobool(a) op var_tobool(b); \
} else if (var_isstr(a)) { /* string op string */ \
res = 1 op be_eqstr(a->v.s, b->v.s); \
} else if (var_isclass(a) || var_isfunction(a) || var_iscomptr(a)) { \
res = var_toobj(a) op var_toobj(b); \
} else { \
binop_error(vm, #op, a, b); \
res = bfalse; /* will not be executed */ \
} \
} else { /* different types */ \
res = 1 op 0; \
} \
return res
/* when running on ESP32 in IRAM, there is a bug in early chip revision */
#ifdef ESP32
#define relop_rule(op) \
bbool res; \
if (var_isint(a) && var_isint(b)) { \
res = ibinop(op, a, b); \
} else if (var_isnumber(a) && var_isnumber(b)) { \
/* res = var2real(a) op var2real(b); */ \
union bvaldata x, y; /* TASMOTA workaround for ESP32 rev0 bug */ \
x.i = a->v.i;\
if (var_isint(a)) { x.r = (breal) x.i; }\
y.i = b->v.i;\
if (var_isint(b)) { y.r = (breal) y.i; }\
res = x.r op y.r; \
} else if (var_isstr(a) && var_isstr(b)) { \
bstring *s1 = var_tostr(a), *s2 = var_tostr(b); \
res = be_strcmp(s1, s2) op 0; \
} else if (var_isinstance(a)) { \
binstance *obj = var_toobj(a); \
object_binop(vm, #op, *a, *b); \
check_bool(vm, obj, #op); \
res = var_tobool(vm->top); \
} else { \
binop_error(vm, #op, a, b); \
res = bfalse; /* will not be executed */ \
} \
return res
#else // ESP32
#define relop_rule(op) \
bbool res; \
if (var_isint(a) && var_isint(b)) { \
res = ibinop(op, a, b); \
} else if (var_isnumber(a) && var_isnumber(b)) { \
res = var2real(a) op var2real(b); \
} else if (var_isstr(a) && var_isstr(b)) { \
bstring *s1 = var_tostr(a), *s2 = var_tostr(b); \
res = be_strcmp(s1, s2) op 0; \
} else if (var_isinstance(a)) { \
binstance *obj = var_toobj(a); \
object_binop(vm, #op, *a, *b); \
check_bool(vm, obj, #op); \
res = var_tobool(vm->top); \
} else { \
binop_error(vm, #op, a, b); \
res = bfalse; /* will not be executed */ \
} \
return res
#endif // ESP32
#define bitwise_block(op) \
bvalue *dst = RA(), *a = RKB(), *b = RKC(); \
if (var_isint(a) && var_isint(b)) { \
var_setint(dst, ibinop(op, a, b)); \
} else if (var_isinstance(a)) { \
ins_binop(vm, #op, ins); \
} else { \
binop_error(vm, #op, a, b); \
}
#define push_native(_vm, _f, _ns, _t) { \
precall(_vm, _f, _ns, _t); \
_vm->cf->status = PRIM_FUNC; \
}
static void prep_closure(bvm *vm, int pos, int argc, int mode);
static void attribute_error(bvm *vm, const char *t, bvalue *b, bvalue *c)
{
const char *attr = var_isstr(c) ? str(var_tostr(c)) : be_vtype2str(c);
vm_error(vm, "attribute_error",
"'%s' value has no %s '%s'", be_vtype2str(b), t, attr);
}
static void binop_error(bvm *vm, const char *op, bvalue *a, bvalue *b)
{
vm_error(vm, "type_error",
"unsupported operand type(s) for %s: '%s' and '%s'",
op, be_vtype2str(a), be_vtype2str(b));
}
static void unop_error(bvm *vm, const char *op, bvalue *a)
{
vm_error(vm, "type_error",
"unsupported operand type(s) for %s: '%s'",
op, be_vtype2str(a));
}
static void call_error(bvm *vm, bvalue *v)
{
vm_error(vm, "type_error",
"'%s' value is not callable", be_vtype2str(v));
}
/* Check that the return value is bool or raise an exception */
/* `obj` and `method` are only passed for error reporting */
static void check_bool(bvm *vm, binstance *obj, const char *method)
{
if (!var_isbool(vm->top)) {
const char *name = str(be_instance_name(obj));
vm_error(vm, "type_error",
"`%s::%s` return value error, the expected type is 'bool'",
strlen(name) ? name : "<anonymous>", method);
}
}
#if BE_USE_DEBUG_HOOK
static void do_linehook(bvm *vm)
{
bcallframe *cf = vm->cf;
bclosure *cl = var_toobj(cf->func);
int pc = cast_int(vm->ip - cl->proto->code);
if (!pc || pc > cf->lineinfo->endpc) {
while (pc > cf->lineinfo->endpc)
cf->lineinfo++;
be_callhook(vm, BE_HOOK_LINE);
} else {
blineinfo *linfo = cf->lineinfo;
blineinfo *base = cl->proto->lineinfo;
while (linfo > base && pc <= linfo[-1].endpc)
linfo--;
if (cf->lineinfo != linfo) {
cf->lineinfo = linfo;
be_callhook(vm, BE_HOOK_LINE);
}
}
}
#endif
/* Prepare the stack for the function/method call */
/* `func` is a pointer to the function/method on the stack, it contains the closure before call and the result after the call */
/* `nstackˋ is the stack depth used by the function (determined by compiler), we add BE_STACK_FREE_MIN as a safety margin */
static void precall(bvm *vm, bvalue *func, int nstack, int mode)
{
bcallframe *cf;
int expan = nstack + BE_STACK_FREE_MIN; /* `expan` is the minimum required space on the stack */
if (vm->stacktop < func + expan) { /* do we have too little space left on the stack? */
size_t fpos = func - vm->stack; /* compute offset of `func` from base stack, in case stack is reallocated and base address changes */
be_stack_expansion(vm, expan); /* expand stack (vector object), warning stack address changes */
func = vm->stack + fpos; /* recompute `func` address with new stack address */
}
be_stack_push(vm, &vm->callstack, NULL); /* push a NULL value on callstack */
cf = be_stack_top(&vm->callstack); /* get address of new callframe at top of callstack */
cf->func = func - mode;
cf->top = vm->top; /* save previous stack top */
cf->reg = vm->reg; /* save previous stack base */
vm->reg = func + 1; /* new stack base is right after function */
vm->top = vm->reg + nstack; /* new stack top is above the registers used by the function, so we don´t mess with them */
vm->cf = cf; /* set new current callframe */
}
/* Prepare call of closure, setting the instruction pointer (ip) */
static void push_closure(bvm *vm, bvalue *func, int nstack, int mode)
{
bclosure *cl = var_toobj(func);
precall(vm, func, nstack, mode);
vm->cf->ip = vm->ip;
vm->cf->status = NONE_FLAG;
vm->ip = cl->proto->code;
#if BE_USE_DEBUG_HOOK
vm->cf->lineinfo = cl->proto->lineinfo;
be_callhook(vm, BE_HOOK_CALL);
#endif
}
static void ret_native(bvm *vm)
{
bcallframe *_cf = vm->cf;
vm->reg = _cf->reg;
vm->top = _cf->top;
be_stack_pop(&vm->callstack);
vm->cf = be_stack_top(&vm->callstack);
}
static bbool obj2bool(bvm *vm, bvalue *var)
{
binstance *obj = var_toobj(var);
bstring *tobool = str_literal(vm, "tobool");
/* get operator method */
int type = be_instance_member(vm, obj, tobool, vm->top);
if (type != BE_NONE && type != BE_NIL) {
vm->top[1] = *var; /* move self to argv[0] */
be_dofunc(vm, vm->top, 1); /* call method 'tobool' */
/* check the return value */
check_bool(vm, obj, "tobool");
return var_tobool(vm->top);
}
return btrue;
}
bbool be_value2bool(bvm *vm, bvalue *v)
{
switch (var_basetype(v)) {
case BE_NIL:
return bfalse;
case BE_BOOL:
return var_tobool(v);
case BE_INT:
return val2bool(v->v.i);
case BE_REAL:
return val2bool(v->v.r);
case BE_STRING:
return str_len(var_tostr(v)) != 0;
case BE_COMPTR:
return var_toobj(v) != NULL;
case BE_COMOBJ:
return ((bcommomobj*)var_toobj(v))->data != NULL;
case BE_INSTANCE:
return obj2bool(vm, v);
default:
return btrue;
}
}
static void obj_method(bvm *vm, bvalue *o, bstring *attr, bvalue *dst)
{
binstance *obj = var_toobj(o);
int type = be_instance_member_simple(vm, obj, attr, dst);
if (basetype(type) != BE_FUNCTION) {
vm_error(vm, "attribute_error",
"the '%s' object has no method '%s'",
str(be_instance_name(obj)), str(attr));
}
}
static int obj_attribute(bvm *vm, bvalue *o, bstring *attr, bvalue *dst)
{
binstance *obj = var_toobj(o);
int type = be_instance_member(vm, obj, attr, dst);
if (type == BE_NONE) {
vm_error(vm, "attribute_error",
"the '%s' object has no attribute '%s'",
str(be_instance_name(obj)), str(attr));
}
return type;
}
static int class_attribute(bvm *vm, bvalue *o, bvalue *c, bvalue *dst)
{
bstring *attr = var_tostr(c);
bclass *obj = var_toobj(o);
int type = be_class_member(vm, obj, attr, dst);
if (type == BE_NONE || type == BE_INDEX) {
vm_error(vm, "attribute_error",
"the '%s' class has no static attribute '%s'",
str(obj->name), str(attr));
}
return type;
}
static int module_attribute(bvm *vm, bvalue *o, bvalue *c, bvalue *dst)
{
bstring *attr = var_tostr(c);
bmodule *module = var_toobj(o);
int type = be_module_attr(vm, module, attr, dst);
if (type == BE_NONE) {
vm_error(vm, "attribute_error",
"module '%s' has no member '%s'",
be_module_name(module), str(attr));
}
return type;
}
static bbool object_eqop(bvm *vm,
const char *op, bbool iseq, bvalue *a, bvalue *b)
{
binstance *o = var_toobj(a);
bvalue self = *a, other = *b;
bbool isself = var_isinstance(b) && o == var_toobj(b);
/* first, try to call the overloaded operator of the object */
int type = be_instance_member(vm, o, be_newstr(vm, op), vm->top);
if (basetype(type) == BE_FUNCTION) { /* call method */
bvalue *top = vm->top;
top[1] = self; /* move self to argv[0] */
top[2] = other; /* move other to argv[1] */
be_incrtop(vm); /* prevent collection results */
be_dofunc(vm, top, 2); /* call method 'item' */
be_stackpop(vm, 1);
check_bool(vm, o, op); /* check return value */
return var_tobool(vm->top); /* copy result to dst */
}
/* the default equal operation rule */
return iseq == isself; /* check object self */
}
static void object_binop(bvm *vm, const char *op, bvalue self, bvalue other)
{
bvalue *top = vm->top;
/* get operator method (possible GC) */
obj_method(vm, &self, be_newstr(vm, op), vm->top);
top[1] = self; /* move self to argv[0] */
top[2] = other; /* move other to argv[1] */
be_incrtop(vm); /* prevent collection results */
be_dofunc(vm, top, 2); /* call method 'item' */
be_stackpop(vm, 1);
}
#define ins_binop(vm, op, ins) { \
object_binop(vm, op, *RKB(), *RKC()); \
reg = vm->reg; \
*RA() = *vm->top; /* copy result to dst */ \
}
static void ins_unop(bvm *vm, const char *op, bvalue self)
{
bvalue *top = vm->top;
/* get operator method (possible GC) */
obj_method(vm, &self, be_newstr(vm, op), vm->top);
top[1] = self; /* move self to argv[0] */
be_dofunc(vm, top, 1); /* call method 'item' */
}
bbool be_vm_iseq(bvm *vm, bvalue *a, bvalue *b)
{
equal_rule(==, btrue);
}
bbool be_vm_isneq(bvm *vm, bvalue *a, bvalue *b)
{
equal_rule(!=, bfalse);
}
bbool be_vm_islt(bvm *vm, bvalue *a, bvalue *b)
{
relop_rule(<);
}
bbool be_vm_isle(bvm *vm, bvalue *a, bvalue *b)
{
relop_rule(<=);
}
bbool be_vm_isgt(bvm *vm, bvalue *a, bvalue *b)
{
relop_rule(>);
}
bbool be_vm_isge(bvm *vm, bvalue *a, bvalue *b)
{
relop_rule(>=);
}
static void make_range(bvm *vm, bvalue lower, bvalue upper)
{
/* get method 'item' (possible GC) */
int idx = be_builtin_find(vm, str_literal(vm, "range"));
bvalue *top = vm->top;
top[0] = *be_global_var(vm, idx);
top[1] = lower; /* move lower to argv[0] */
top[2] = upper; /* move upper to argv[1] */
vm->top += 3; /* prevent collection results */
be_dofunc(vm, top, 2); /* call method 'item' */
vm->top -= 3;
}
static void connect_str(bvm *vm, bstring *a, bvalue *b)
{
bstring *s;
if (var_isstr(b)) {
s = be_strcat(vm, a, var_tostr(b));
var_setstr(vm->top, s);
} else {
*vm->top++ = *b;
be_val2str(vm, -1);
b = vm->top - 1;
s = be_strcat(vm, a, var_tostr(b));
var_setstr(b, s);
vm->top -= 1;
}
}
BERRY_API bvm* be_vm_new(void)
{
bvm *vm = be_os_malloc(sizeof(bvm));
be_assert(vm != NULL);
memset(vm, 0, sizeof(bvm)); /* clear all members */
be_gc_init(vm);
be_string_init(vm);
be_stack_init(vm, &vm->callstack, sizeof(bcallframe));
be_stack_init(vm, &vm->refstack, sizeof(binstance*));
be_stack_init(vm, &vm->exceptstack, sizeof(struct bexecptframe));
be_stack_init(vm, &vm->tracestack, sizeof(bcallsnapshot));
vm->stack = be_malloc(vm, sizeof(bvalue) * BE_STACK_START);
vm->stacktop = vm->stack + BE_STACK_START;
vm->reg = vm->stack;
vm->top = vm->reg;
be_globalvar_init(vm);
be_gc_setpause(vm, 1);
be_loadlibs(vm);
vm->compopt = 0;
vm->bytesmaxsize = BE_BYTES_MAX_SIZE;
vm->obshook = NULL;
vm->ctypefunc = NULL;
#if BE_USE_PERF_COUNTERS
vm->counter_ins = 0;
vm->counter_enter = 0;
vm->counter_call = 0;
vm->counter_get = 0;
vm->counter_set = 0;
vm->counter_get_global = 0;
vm->counter_try = 0;
vm->counter_exc = 0;
vm->counter_gc_kept = 0;
vm->counter_gc_freed = 0;
vm->counter_mem_alloc = 0;
vm->counter_mem_free = 0;
vm->counter_mem_realloc = 0;
#endif
return vm;
}
BERRY_API void be_vm_delete(bvm *vm)
{
be_gc_deleteall(vm);
be_string_deleteall(vm);
be_stack_delete(vm, &vm->callstack);
be_stack_delete(vm, &vm->refstack);
be_stack_delete(vm, &vm->exceptstack);
be_stack_delete(vm, &vm->tracestack);
be_free(vm, vm->stack, (vm->stacktop - vm->stack) * sizeof(bvalue));
be_globalvar_deinit(vm);
be_gc_free_memory_pools(vm);
#if BE_USE_DEBUG_HOOK
/* free native hook */
if (var_istype(&vm->hook, BE_COMPTR))
be_free(vm, var_toobj(&vm->hook), sizeof(struct bhookblock));
#endif
/* free VM structure */
be_os_free(vm);
}
static void vm_exec(bvm *vm)
{
bclosure *clos;
bvalue *ktab, *reg;
binstruction ins;
vm->cf->status |= BASE_FRAME;
newframe: /* a new call frame */
be_assert(var_isclosure(vm->cf->func));
clos = var_toobj(vm->cf->func); /* `clos` is the current function/closure */
ktab = clos->proto->ktab; /* `ktab` is the current constant table */
reg = vm->reg; /* `reg` is the current stack base for the callframe */
#if BE_USE_PERF_COUNTERS
vm->counter_enter++;
#endif
vm_exec_loop() {
opcase(LDNIL): {
var_setnil(RA());
dispatch();
}
opcase(LDBOOL): {
bvalue *v = RA();
var_setbool(v, IGET_RKB(ins));
if (IGET_RKC(ins)) { /* skip next instruction */
vm->ip += 1;
}
dispatch();
}
opcase(LDINT): {
bvalue *v = RA();
var_setint(v, IGET_sBx(ins));
dispatch();
}
opcase(LDCONST): {
bvalue *dst = RA();
*dst = ktab[IGET_Bx(ins)];
dispatch();
}
opcase(GETGBL): {
bvalue *v = RA();
int idx = IGET_Bx(ins);
*v = *be_global_var(vm, idx);
dispatch();
}
opcase(GETNGBL): { /* get Global by name */
#if BE_USE_PERF_COUNTERS
vm->counter_get_global++;
#endif
bvalue *v = RA();
bvalue *b = RKB();
if (var_isstr(b)) {
bstring *name = var_tostr(b);
int idx = be_global_find(vm, name);
if (idx >= 0) {
*v = *be_global_var(vm, idx);
} else {
vm_error(vm, "attribute_error", "'%s' undeclared", str(name));
}
} else {
vm_error(vm, "internal_error", "global name must be a string");
}
dispatch();
}
opcase(SETNGBL): { /* set Global by name */
bvalue *v = RA();
bvalue *b = RKB();
if (var_isstr(b)) {
bstring *name = var_tostr(b);
int idx = be_global_new(vm, name);
*be_global_var(vm, idx) = *v;
} else {
vm_error(vm, "internal_error", "global name must be a string");
}
dispatch();
}
opcase(SETGBL): {
bvalue *v = RA();
int idx = IGET_Bx(ins);
*be_global_var(vm, idx) = *v;
dispatch();
}
opcase(GETUPV): {
bvalue *v = RA();
int idx = IGET_Bx(ins);
be_assert(*clos->upvals != NULL);
*v = *clos->upvals[idx]->value;
dispatch();
}
opcase(SETUPV): {
bvalue *v = RA();
int idx = IGET_Bx(ins);
be_assert(*clos->upvals != NULL);
*clos->upvals[idx]->value = *v;
dispatch();
}
opcase(MOVE): {
bvalue *dst = RA();
*dst = *RKB();
dispatch();
}
opcase(ADD): {
bvalue *dst = RA(), *a = RKB(), *b = RKC();
if (var_isint(a) && var_isint(b)) {
var_setint(dst, ibinop(+, a, b));
} else if (var_isnumber(a) && var_isnumber(b)) {
#ifdef ESP32 /* when running on ESP32 in IRAM, there is a bug in early chip revision */
union bvaldata x, y; // TASMOTA workaround for ESP32 rev0 bug
x.i = a->v.i;
if (var_isint(a)) { x.r = (breal) x.i; }
y.i = b->v.i;
if (var_isint(b)) { y.r = (breal) y.i; }
var_setreal(dst, x.r + y.r);
#else // ESP32
breal x = var2real(a), y = var2real(b);
var_setreal(dst, x + y);
#endif // ESP32
} else if (var_isstr(a) && var_isstr(b)) { /* strcat */
bstring *s = be_strcat(vm, var_tostr(a), var_tostr(b));
reg = vm->reg;
dst = RA();
var_setstr(dst, s);
} else if (var_isinstance(a)) {
ins_binop(vm, "+", ins);
} else {
binop_error(vm, "+", a, b);
}
dispatch();
}
opcase(SUB): {
bvalue *dst = RA(), *a = RKB(), *b = RKC();
if (var_isint(a) && var_isint(b)) {
var_setint(dst, ibinop(-, a, b));
} else if (var_isnumber(a) && var_isnumber(b)) {
#if CONFIG_IDF_TARGET_ESP32 /* when running on ESP32 in IRAM, there is a bug in early chip revision */
union bvaldata x, y; // TASMOTA workaround for ESP32 rev0 bug
x.i = a->v.i;
if (var_isint(a)) { x.r = (breal) x.i; }
y.i = b->v.i;
if (var_isint(b)) { y.r = (breal) y.i; }
var_setreal(dst, x.r - y.r);
#else // CONFIG_IDF_TARGET_ESP32
breal x = var2real(a), y = var2real(b);
var_setreal(dst, x - y);
#endif // CONFIG_IDF_TARGET_ESP32
} else if (var_isinstance(a)) {
ins_binop(vm, "-", ins);
} else {
binop_error(vm, "-", a, b);
}
dispatch();
}
opcase(MUL): {
bvalue *dst = RA(), *a = RKB(), *b = RKC();
if (var_isint(a) && var_isint(b)) {
var_setint(dst, ibinop(*, a, b));
} else if (var_isnumber(a) && var_isnumber(b)) {
#if CONFIG_IDF_TARGET_ESP32 /* when running on ESP32 in IRAM, there is a bug in early chip revision */
union bvaldata x, y; // TASMOTA workaround for ESP32 rev0 bug
x.i = a->v.i;
if (var_isint(a)) { x.r = (breal) x.i; }
y.i = b->v.i;
if (var_isint(b)) { y.r = (breal) y.i; }
var_setreal(dst, x.r * y.r);
#else // CONFIG_IDF_TARGET_ESP32
breal x = var2real(a), y = var2real(b);
var_setreal(dst, x * y);
#endif // CONFIG_IDF_TARGET_ESP32
} else if (var_isinstance(a)) {
ins_binop(vm, "*", ins);
} else {
binop_error(vm, "*", a, b);
}
dispatch();
}
opcase(DIV): {
bvalue *dst = RA(), *a = RKB(), *b = RKC();
if (var_isint(a) && var_isint(b)) {
bint x = var_toint(a), y = var_toint(b);
if (y == 0) {
vm_error(vm, "divzero_error", "division by zero");
} else {
var_setint(dst, x / y);
}
} else if (var_isnumber(a) && var_isnumber(b)) {
#if CONFIG_IDF_TARGET_ESP32 /* when running on ESP32 in IRAM, there is a bug in early chip revision */
union bvaldata x0, y0; // TASMOTA workaround for ESP32 rev0 bug
x0.i = a->v.i;
if (var_isint(a)) { x0.r = (breal) x0.i; }
y0.i = b->v.i;
if (var_isint(b)) { y0.r = (breal) y0.i; }
breal x = x0.r, y = y0.r;
#else // CONFIG_IDF_TARGET_ESP32
breal x = var2real(a), y = var2real(b);
#endif // CONFIG_IDF_TARGET_ESP32
if (y == cast(breal, 0)) {
vm_error(vm, "divzero_error", "division by zero");
} else {
var_setreal(dst, x / y);
}
} else if (var_isinstance(a)) {
ins_binop(vm, "/", ins);
} else {
binop_error(vm, "/", a, b);
}
dispatch();
}
opcase(MOD): {
bvalue *dst = RA(), *a = RKB(), *b = RKC();
if (var_isint(a) && var_isint(b)) {
bint x = var_toint(a), y = var_toint(b);
if (y == 0) {
vm_error(vm, "divzero_error", "division by zero");
} else {
var_setint(dst, x % y);
}
} else if (var_isnumber(a) && var_isnumber(b)) {
#if CONFIG_IDF_TARGET_ESP32 /* when running on ESP32 in IRAM, there is a bug in early chip revision */
union bvaldata x0, y0; // TASMOTA workaround for ESP32 rev0 bug
x0.i = a->v.i;
if (var_isint(a)) { x0.r = (breal) x0.i; }
y0.i = b->v.i;
if (var_isint(b)) { y0.r = (breal) y0.i; }
breal x = x0.r, y = y0.r;
#else
breal x = var2real(a), y = var2real(b);
#endif
if (y == cast(breal, 0)) {
vm_error(vm, "divzero_error", "division by zero");
} else {
var_setreal(dst, mathfunc(fmod)(x, y));
}
} else if (var_isinstance(a)) {
ins_binop(vm, "%", ins);
} else {
binop_error(vm, "%", a, b);
}
dispatch();
}
opcase(LT): {
bbool res = be_vm_islt(vm, RKB(), RKC());
bvalue *dst;
reg = vm->reg;
dst = RA();
var_setbool(dst, res);
dispatch();
}
opcase(LE): {
bbool res = be_vm_isle(vm, RKB(), RKC());
bvalue *dst;
reg = vm->reg;
dst = RA();
var_setbool(dst, res);
dispatch();
}
opcase(EQ): {
bbool res = be_vm_iseq(vm, RKB(), RKC());
bvalue *dst;
reg = vm->reg;
dst = RA();
var_setbool(dst, res);
dispatch();
}
opcase(NE): {
bbool res = be_vm_isneq(vm, RKB(), RKC());
bvalue *dst;
reg = vm->reg;
dst = RA();
var_setbool(dst, res);
dispatch();
}
opcase(GT): {
bbool res = be_vm_isgt(vm, RKB(), RKC());
bvalue *dst;
reg = vm->reg;
dst = RA();
var_setbool(dst, res);
dispatch();
}
opcase(GE): {
bbool res = be_vm_isge(vm, RKB(), RKC());
bvalue *dst;
reg = vm->reg;
dst = RA();
var_setbool(dst, res);
dispatch();
}
opcase(CONNECT): {
bvalue *a = RKB(), *b = RKC();
if (var_isint(a) && var_isint(b)) {
make_range(vm, *RKB(), *RKC());
} else if (var_isstr(a)) {
connect_str(vm, var_tostr(a), b);
} else if (var_isinstance(a)) {
object_binop(vm, "..", *RKB(), *RKC());
} else {
binop_error(vm, "..", RKB(), RKC());
}
reg = vm->reg;
*RA() = *vm->top; /* copy result to R(A) */
dispatch();
}
opcase(AND): {
bitwise_block(&);
dispatch();
}
opcase(OR): {
bitwise_block(|);
dispatch();
}
opcase(XOR): {
bitwise_block(^);
dispatch();
}
opcase(SHL): {
bitwise_block(<<);
dispatch();
}
opcase(SHR): {
bitwise_block(>>);
dispatch();
}
opcase(NEG): {
bvalue *dst = RA(), *a = RKB();
if (var_isint(a)) {
var_setint(dst, -a->v.i);
} else if (var_isreal(a)) {
var_setreal(dst, -a->v.r);
} else if (var_isinstance(a)) {
ins_unop(vm, "-*", *RKB());
reg = vm->reg;
*RA() = *vm->top; /* copy result to dst */
} else {
unop_error(vm, "-", a);
}
dispatch();
}
opcase(FLIP): {
bvalue *dst = RA(), *a = RKB();
if (var_isint(a)) {
var_setint(dst, ~a->v.i);
} else if (var_isinstance(a)) {
ins_unop(vm, "~", *RKB());
reg = vm->reg;
*RA() = *vm->top; /* copy result to dst */
} else {
unop_error(vm, "~", a);
}
dispatch();
}
opcase(JMP): {
vm->ip += IGET_sBx(ins);
dispatch();
}
opcase(JMPT): {
if (be_value2bool(vm, RA())) {
vm->ip += IGET_sBx(ins);
}
dispatch();
}
opcase(JMPF): {
if (!be_value2bool(vm, RA())) {
vm->ip += IGET_sBx(ins);
}
dispatch();
}
opcase(CLOSURE): {
bvalue *dst;
bproto *p = clos->proto->ptab[IGET_Bx(ins)];
bclosure *cl = be_newclosure(vm, p->nupvals);
cl->proto = p;
reg = vm->reg;
dst = RA();
var_setclosure(dst, cl);
be_initupvals(vm, cl);
dispatch();
}
opcase(CLASS): {
bclass *c = var_toobj(ktab + IGET_Bx(ins));
be_class_upvalue_init(vm, c);
dispatch();
}
opcase(GETMBR): {
#if BE_USE_PERF_COUNTERS
vm->counter_get++;
#endif
bvalue result; /* copy result to a temp variable because the stack may be relocated in virtual member calls */
bvalue *b = RKB(), *c = RKC();
if (var_isinstance(b) && var_isstr(c)) {
obj_attribute(vm, b, var_tostr(c), &result);
reg = vm->reg;
} else if (var_isclass(b) && var_isstr(c)) {
class_attribute(vm, b, c, &result);
reg = vm->reg;
} else if (var_ismodule(b) && var_isstr(c)) {
module_attribute(vm, b, c, &result);
reg = vm->reg;
} else {
attribute_error(vm, "attribute", b, c);
result = *RA(); /* avoid gcc warning for uninitialized variable result, this code is never reached */
}
bvalue *a = RA();
*a = result; /* assign the resul to the specified register on the updated stack */
dispatch();
}
opcase(GETMET): {
#if BE_USE_PERF_COUNTERS
vm->counter_get++;
#endif
bvalue result; /* copy result to a temp variable because the stack may be relocated in virtual member calls */
bvalue *b = RKB(), *c = RKC();
if (var_isinstance(b) && var_isstr(c)) {
binstance *obj = var_toobj(b);
int type = obj_attribute(vm, b, var_tostr(c), &result);
reg = vm->reg;
bvalue *a = RA();
*a = result;
if (var_basetype(a) == BE_FUNCTION) {
if ((type & BE_STATIC) || (type == BE_INDEX)) { /* if instance variable then we consider it's non-method */
/* static method, don't bother with the instance */
a[1] = result;
var_settype(a, NOT_METHOD);
} else {
/* this is a real method (i.e. non-static) */
/* check if the object is a superinstance, if so get the lowest possible subclass */
while (obj->sub) {
obj = obj->sub;
}
var_setinstance(&a[1], obj); /* replace superinstance by lowest subinstance */
}
} else if (var_isclass(a)) {
/* in this case we have a class in a static or non-static member */
/* it's always treated like a static function */
a[1] = result;
var_settype(a, NOT_METHOD);
} else {
vm_error(vm, "attribute_error",
"class '%s' has no method '%s'",
str(be_instance_name(obj)), str(var_tostr(c)));
}
} else if (var_isclass(b) && var_isstr(c)) {
class_attribute(vm, b, c, &result);
reg = vm->reg;
bvalue *a = RA();
a[1] = result;
var_settype(a, NOT_METHOD);
} else if (var_ismodule(b) && var_isstr(c)) {
module_attribute(vm, b, c, &result);
reg = vm->reg;
bvalue *a = RA();
a[1] = result;
var_settype(a, NOT_METHOD);
} else {
attribute_error(vm, "method", b, c);
}
dispatch();
}
opcase(SETMBR): {
#if BE_USE_PERF_COUNTERS
vm->counter_set++;
#endif