-
Notifications
You must be signed in to change notification settings - Fork 17
/
execute.cc
3532 lines (3147 loc) · 88 KB
/
execute.cc
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) 1992, 1995, 1996 Xerox Corporation. All rights reserved.
Portions of this code were written by Stephen White, aka ghond.
Use and copying of this software and preparation of derivative works based
upon this software are permitted. Any distribution of this software or
derivative works must comply with all applicable United States export
control laws. This software is made available AS IS, and Xerox Corporation
makes no warranty about the software, its performance or its conformity to
any specification. Any person obtaining a copy of this software is requested
to send their name and post office or electronic mail address to:
Pavel Curtis
Xerox PARC
3333 Coyote Hill Rd.
Palo Alto, CA 94304
Pavel@Xerox.Com
*****************************************************************************/
#include "my-string.h"
#include "collection.h"
#include "config.h"
#include "db.h"
#include "db_io.h"
#include "decompile.h"
#include "eval_env.h"
#include "eval_vm.h"
#include "execute.h"
#include "functions.h"
#include "list.h"
#include "log.h"
#include "map.h"
#include "numbers.h"
#include "opcode.h"
#include "options.h"
#include "parse_cmd.h"
#include "server.h"
#include "storage.h"
#include "streams.h"
#include "structures.h"
#include "sym_table.h"
#include "tasks.h"
#include "timers.h"
#include "utils.h"
#include "version.h"
/* the following globals are the guts of the virtual machine: */
static activation *activ_stack = 0;
static int max_stack_size = 0;
static unsigned top_activ_stack; /* points to top-of-stack
(last-occupied-slot),
not next-empty-slot */
static int root_activ_vector; /* root_activ_vector == MAIN_VECTOR
iff root activation is main
vector */
/* these globals are not part of the vm because they get re-initialized
after a suspend */
static int ticks_remaining;
int task_timed_out;
static int interpreter_is_running = 0;
static Timer_ID task_alarm_id;
static const char *handler_verb_name; /* For in-DB traceback handling */
static Var handler_verb_args;
/* used when loading the database to hold values that may reference yet
unloaded anonymous objects */
static Var temp_vars = new_list(0);
/* macros to ease indexing into activation stack */
#define RUN_ACTIV activ_stack[top_activ_stack]
#define CALLER_ACTIV activ_stack[top_activ_stack - 1]
/**** error handling ****/
typedef enum { /* Reasons for executing a FINALLY handler */
/* These constants are stored in the DB, so don't change the order... */
FIN_FALL_THRU, FIN_RAISE, FIN_UNCAUGHT, FIN_RETURN,
FIN_ABORT, /* This doesn't actually get you into a FINALLY... */
FIN_EXIT
} Finally_Reason;
/*
* Keep a pool of the common size rt_stacks around to avoid beating up on
* malloc. This doesn't really need tuning. Most rt_stacks will be less
* than size 10. I rounded up to a size which won't waste a lot of space
* with a powers-of-two malloc (while leaving some room for mymalloc
* overhead, if any).
*/
static Var *rt_stack_quick;
#define RT_STACK_QUICKSIZE 15
static void
alloc_rt_stack(activation * a, int size)
{
Var *res;
if (size <= RT_STACK_QUICKSIZE && rt_stack_quick) {
res = rt_stack_quick;
rt_stack_quick = rt_stack_quick[0].v.list;
} else {
res = (Var *)mymalloc(MAX(size, RT_STACK_QUICKSIZE) * sizeof(Var), M_RT_STACK);
}
a->base_rt_stack = a->top_rt_stack = res;
a->rt_stack_size = size;
}
static void
free_rt_stack(activation * a)
{
Var *stack = a->base_rt_stack;
if (a->rt_stack_size <= RT_STACK_QUICKSIZE) {
stack[0].v.list = rt_stack_quick;
rt_stack_quick = stack;
} else
myfree(stack, M_RT_STACK);
}
void
print_error_backtrace(const char *msg, void (*output) (const char *))
{
int t;
Stream *str;
if (!interpreter_is_running)
return;
str = new_stream(100);
for (t = top_activ_stack; t >= 0; t--) {
if (t != top_activ_stack)
stream_printf(str, "... called from ");
if (TYPE_OBJ == activ_stack[t].vloc.type)
stream_printf(str, "#%d:%s", activ_stack[t].vloc.v.obj,
activ_stack[t].verbname);
else
stream_printf(str, "*anonymous*:%s",
activ_stack[t].verbname);
if (equality(activ_stack[t].vloc, activ_stack[t]._this, 0)) {
stream_add_string(str, " (this == ");
unparse_value(str, activ_stack[t]._this);
stream_add_string(str, ")");
}
stream_printf(str, ", line %d",
find_line_number(activ_stack[t].prog,
(t == 0 ? root_activ_vector
: MAIN_VECTOR),
activ_stack[t].error_pc));
if (t == top_activ_stack)
stream_printf(str, ": %s", msg);
output(reset_stream(str));
if (t > 0 && activ_stack[t].bi_func_pc) {
stream_printf(str, "... called from built-in function %s()",
name_func_by_num(activ_stack[t].bi_func_id));
output(reset_stream(str));
}
}
output("(End of traceback)");
free_stream(str);
}
static void
output_to_log(const char *line)
{
applog(LOG_INFO2, "%s\n", line);
}
static Var backtrace_list;
static void
output_to_list(const char *line)
{
Var str;
str.type = TYPE_STR;
str.v.str = str_dup(line);
backtrace_list = listappend(backtrace_list, str);
}
static Var
error_backtrace_list(const char *msg)
{
backtrace_list = new_list(0);
print_error_backtrace(msg, output_to_list);
return backtrace_list;
}
static enum error
suspend_task(package p)
{
vm the_vm = new_vm(current_task_id, var_ref(current_local), top_activ_stack + 1);
unsigned int i;
enum error e;
the_vm->max_stack_size = max_stack_size;
the_vm->top_activ_stack = top_activ_stack;
the_vm->root_activ_vector = root_activ_vector;
the_vm->func_id = 0; /* shouldn't need func_id; */
for (i = 0; i <= top_activ_stack; i++)
the_vm->activ_stack[i] = activ_stack[i];
e = (*p.u.susp.proc) (the_vm, p.u.susp.data);
if (e != E_NONE)
free_vm(the_vm, 0);
return e;
}
static int raise_error(package p, enum outcome *outcome);
static void abort_task(enum abort_reason reason);
static int
unwind_stack(Finally_Reason why, Var value, enum outcome *outcome)
{
/* Returns true iff the interpreter should stop,
* in which case *outcome is set to the correct outcome to return.
* Interpreter stops either because it was blocked (OUTCOME_BLOCKED)
* or the entire stack was unwound (OUTCOME_DONE/OUTCOME_ABORTED)
*
* why==FIN_EXIT always returns false
* why==FIN_ABORT always returns true/OUTCOME_ABORTED
*/
Var code = (why == FIN_RAISE ? value.v.list[1] : zero);
for (;;) { /* loop over activations */
activation *a = &(activ_stack[top_activ_stack]);
void *bi_func_data = 0;
int bi_func_pc;
unsigned bi_func_id = 0;
Objid player;
Var v, *goal = a->base_rt_stack;
if (why == FIN_EXIT)
goal += value.v.list[1].v.num;
while (a->top_rt_stack > goal) { /* loop over rt stack */
a->top_rt_stack--;
v = *(a->top_rt_stack);
if (why != FIN_ABORT && v.type == TYPE_FINALLY) {
/* FINALLY handler */
a->pc = v.v.num;
v.type = TYPE_INT;
v.v.num = why;
*(a->top_rt_stack++) = v;
*(a->top_rt_stack++) = value;
return 0;
} else if (why == FIN_RAISE && v.type == TYPE_CATCH) {
/* TRY-EXCEPT or `expr ! ...' handler */
Var *new_top = a->top_rt_stack - 2 * v.v.num;
Var *vv;
int found = 0;
for (vv = new_top; vv < a->top_rt_stack; vv += 2) {
if (!found && (vv->type != TYPE_LIST
|| ismember(code, *vv, 0))) {
found = 1;
v = *(vv + 1);
if (v.type != TYPE_INT)
panic("Non-numeric PC value on stack!");
a->pc = v.v.num;
}
free_var(*vv);
}
a->top_rt_stack = new_top;
if (found) {
*(a->top_rt_stack++) = value;
return 0;
}
} else
free_var(v);
}
if (why == FIN_EXIT) {
a->pc = value.v.list[2].v.num;
free_var(value);
return 0;
}
bi_func_pc = a->bi_func_pc;
if (bi_func_pc) {
bi_func_id = a->bi_func_id;
bi_func_data = a->bi_func_data;
}
player = a->player;
free_activation(a, 0); /* 0 == don't free bi_func_data */
if (top_activ_stack == 0) { /* done */
if (outcome)
*outcome = (why == FIN_RETURN
? OUTCOME_DONE
: OUTCOME_ABORTED);
return 1;
}
top_activ_stack--;
if (bi_func_pc != 0) { /* Must unwind through a built-in function */
package p;
if (why == FIN_RETURN) {
a = &(activ_stack[top_activ_stack]);
p = call_bi_func(bi_func_id, value, bi_func_pc, a->progr,
bi_func_data);
switch (p.kind) {
case package::BI_RETURN:
*(a->top_rt_stack++) = p.u.ret;
return 0;
case package::BI_RAISE:
if (a->debug)
return raise_error(p, outcome);
else {
*(a->top_rt_stack++) = p.u.raise.code;
free_str(p.u.raise.msg);
free_var(p.u.raise.value);
return 0;
}
case package::BI_SUSPEND:
{
enum error e = suspend_task(p);
if (e == E_NONE) {
if (outcome)
*outcome = OUTCOME_BLOCKED;
return 1;
} else {
value.type = TYPE_ERR;
value.v.err = e;
return unwind_stack(FIN_RAISE, value, outcome);
}
}
case package::BI_CALL:
a = &(activ_stack[top_activ_stack]); /* TOS has changed */
a->bi_func_id = bi_func_id;
a->bi_func_pc = p.u.call.pc;
a->bi_func_data = p.u.call.data;
return 0;
case package::BI_KILL:
abort_task((abort_reason)p.u.ret.v.num);
if (outcome)
*outcome = OUTCOME_ABORTED;
return 1;
}
} else {
/* Built-in functions receive zero as a `returned value' on
* errors and aborts, all further calls they make are short-
* circuited with an immediate return of zero, and any errors
* they raise are squelched. This is compatible with older,
* pre-error-handling versions of the server, and thus
* acceptible for the existing built-ins. It is conceivable
* that this model will have to be revisited at some point in
* the future.
*/
do {
p = call_bi_func(bi_func_id, zero, bi_func_pc, a->progr,
bi_func_data);
switch (p.kind) {
case package::BI_RETURN:
free_var(p.u.ret);
break;
case package::BI_RAISE:
free_var(p.u.raise.code);
free_str(p.u.raise.msg);
free_var(p.u.raise.value);
break;
case package::BI_SUSPEND:
case package::BI_KILL:
break;
case package::BI_CALL:
free_activation(&activ_stack[top_activ_stack--], 0);
bi_func_pc = p.u.call.pc;
bi_func_data = p.u.call.data;
break;
}
} while (p.kind == package::BI_CALL && bi_func_pc != 0); /* !tailcall */
}
} else if (why == FIN_RETURN) { /* Push the value on the stack & go */
a = &(activ_stack[top_activ_stack]);
*(a->top_rt_stack++) = value;
return 0;
}
}
}
static int
find_handler_activ(Var code)
{
/* Returns the index of the hottest activation with an active exception
* handler for the given code.
*/
int frame;
for (frame = top_activ_stack; frame >= 0; frame--) {
activation *a = &(activ_stack[frame]);
Var *v, *vv;
for (v = a->top_rt_stack - 1; v >= a->base_rt_stack; v--)
if (v->type == TYPE_CATCH) {
for (vv = v - 2 * v->v.num; vv < v; vv += 2)
if (vv->type != TYPE_LIST || ismember(code, *vv, 0))
return frame;
v -= 2 * v->v.num;
}
}
return -1;
}
static Var
make_stack_list(activation * stack, int start, int end, int include_end,
int root_vector, int line_numbers_too, Objid progr)
{
Var r;
int count = 0, i, j;
for (i = end; i >= start; i--) {
if (include_end || i != end)
count++;
if (i != start && stack[i].bi_func_pc)
count++;
}
r = new_list(count);
j = 1;
for (i = end; i >= start; i--) {
Var v;
if (include_end || i != end) {
v = r.v.list[j++] = new_list(line_numbers_too ? 6 : 5);
v.v.list[1] = anonymizing_var_ref(stack[i]._this, progr);
v.v.list[2] = str_ref_to_var(stack[i].verb);
v.v.list[3] = Var::new_obj(stack[i].progr);
v.v.list[4] = anonymizing_var_ref(stack[i].vloc, progr);
v.v.list[5] = Var::new_obj(stack[i].player);
if (line_numbers_too) {
v.v.list[6].type = TYPE_INT;
v.v.list[6].v.num = find_line_number(stack[i].prog,
(i == 0 ? root_vector
: MAIN_VECTOR),
stack[i].error_pc);
}
}
if (i != start && stack[i].bi_func_pc) {
v = r.v.list[j++] = new_list(line_numbers_too ? 6 : 5);
v.v.list[1].type = TYPE_OBJ;
v.v.list[1].v.obj = NOTHING;
v.v.list[2].type = TYPE_STR;
v.v.list[2].v.str = str_dup(name_func_by_num(stack[i].bi_func_id));
v.v.list[3].type = TYPE_OBJ;
v.v.list[3].v.obj = NOTHING;
v.v.list[4].type = TYPE_OBJ;
v.v.list[4].v.obj = NOTHING;
v.v.list[5].type = TYPE_OBJ;
v.v.list[5].v.obj = stack[i].player;
if (line_numbers_too) {
v.v.list[6].type = TYPE_INT;
v.v.list[6].v.num = stack[i].bi_func_pc;
}
}
}
return r;
}
static void
save_handler_info(const char *vname, Var args)
{
handler_verb_name = vname;
free_var(handler_verb_args);
handler_verb_args = args;
}
/* TODO: both `raise_error()' and `abort_task()' should create a stack
* list from the point of view of the programmer _catching the error_
* (not the point of view of the programmer who's verb generated the
* error) however, this is hard, so for now everyone gets invalid
* anonymous objects!
*/
static int
raise_error(package p, enum outcome *outcome)
{
/* ASSERT: p.kind == package::BI_RAISE */
int handler_activ = find_handler_activ(p.u.raise.code);
Finally_Reason why;
Var value;
if (handler_activ >= 0) { /* handler found */
why = FIN_RAISE;
value = new_list(4);
} else { /* uncaught exception */
why = FIN_UNCAUGHT;
value = new_list(5);
value.v.list[5] = error_backtrace_list(p.u.raise.msg);
handler_activ = 0; /* get entire stack in list */
}
value.v.list[1] = p.u.raise.code;
value.v.list[2].type = TYPE_STR;
value.v.list[2].v.str = p.u.raise.msg;
value.v.list[3] = p.u.raise.value;
value.v.list[4] = make_stack_list(activ_stack, handler_activ,
top_activ_stack, 1,
root_activ_vector, 1,
NOTHING);
if (why == FIN_UNCAUGHT) {
save_handler_info("handle_uncaught_error", value);
value = zero;
}
return unwind_stack(why, value, outcome);
}
static void
abort_task(enum abort_reason reason)
{
Var value;
const char *msg;
const char *htag;
switch(reason) {
default:
panic("Bad abort_reason");
/*NOTREACHED*/
case ABORT_TICKS:
msg = "Task ran out of ticks";
htag = "ticks";
goto save_hinfo;
case ABORT_SECONDS:
msg = "Task ran out of seconds";
htag = "seconds";
save_hinfo:
value = new_list(3);
value.v.list[1].type = TYPE_STR;
value.v.list[1].v.str = str_dup(htag);
value.v.list[2] = make_stack_list(activ_stack, 0, top_activ_stack, 1,
root_activ_vector, 1,
NOTHING);
value.v.list[3] = error_backtrace_list(msg);
save_handler_info("handle_task_timeout", value);
/* fall through */
case ABORT_KILL:
(void) unwind_stack(FIN_ABORT, zero, 0);
}
}
/**** activation manipulation ****/
static int
push_activation(void)
{
if (top_activ_stack < max_stack_size - 1) {
top_activ_stack++;
return 1;
} else
return 0;
}
void
free_activation(activation * ap, char data_too)
{
Var *i;
free_rt_env(ap->rt_env, ap->prog->num_var_names);
for (i = ap->base_rt_stack; i < ap->top_rt_stack; i++)
free_var(*i);
free_rt_stack(ap);
free_var(ap->temp);
free_var(ap->_this);
free_var(ap->vloc);
free_str(ap->verb);
free_str(ap->verbname);
free_program(ap->prog);
if (data_too && ap->bi_func_pc && ap->bi_func_data)
free_data(ap->bi_func_data);
/* else bi_func_state will be later freed by bi_function */
}
/** Set up another activation for calling a verb
does not change the vm in case of any error **/
enum error call_verb2(Objid recv, const char *vname, Var _this, Var args, int do_pass);
/*
* Historical interface for things which want to call with vname not
* already in a moo-str.
*/
enum error
call_verb(Objid recv, const char *vname_in, Var _this, Var args, int do_pass)
{
const char *vname = str_dup(vname_in);
enum error result;
result = call_verb2(recv, vname, _this, args, do_pass);
/* call_verb2 got any refs it wanted */
free_str(vname);
return result;
}
enum error
call_verb2(Objid recv, const char *vname, Var _this, Var args, int do_pass)
{
/* if call succeeds, args will be consumed. If call fails, args
will NOT be consumed -- it must therefore be freed by caller */
/* vname will never be consumed */
/* vname *must* already be a MOO-string (as in str_ref-able) */
/* `_this' will never be consumed */
/* will only return E_MAXREC, E_INVIND, E_VERBNF, or E_NONE */
/* returns an error if there is one, and does not change the vm in that
case, else sets up the activ_stack for the verb call and then returns
E_NONE */
db_verb_handle h = { };
Program *program;
Var *env;
Var v;
if (do_pass) {
Objid where;
if (!is_valid(RUN_ACTIV.vloc))
return E_INVIND;
Var parents = db_object_parents2(RUN_ACTIV.vloc);
if (TYPE_LIST == parents.type) {
if (listlength(parents) == 0)
return E_INVIND;
/* Loop over each parent, looking for the first parent
* that defines a suitable verb that we can pass to.
*/
Var parent;
int i, c;
FOR_EACH(parent, parents, i, c) {
where = parent.v.obj;
h = db_find_callable_verb(Var::new_obj(where), vname);
if (h.ptr)
break;
}
}
else if (TYPE_OBJ == parents.type) {
/* Look for a suitable verb on the parent, if the parent
* is valid.
*/
where = parents.v.obj;
if (!valid(where))
return E_INVIND;
h = db_find_callable_verb(Var::new_obj(where), vname);
}
else {
return E_VERBNF;
}
}
else {
if (TYPE_ANON == _this.type && is_valid(_this))
h = db_find_callable_verb(_this, vname);
else if (valid(recv))
h = db_find_callable_verb(Var::new_obj(recv), vname);
else
return E_INVIND;
}
if (!h.ptr)
return E_VERBNF;
else if (!push_activation())
return E_MAXREC;
program = db_verb_program(h);
RUN_ACTIV.prog = program_ref(program);
RUN_ACTIV._this = var_ref(_this);
RUN_ACTIV.progr = db_verb_owner(h);
RUN_ACTIV.recv = recv;
RUN_ACTIV.vloc = var_ref(db_verb_definer(h));
RUN_ACTIV.verb = str_ref(vname);
RUN_ACTIV.verbname = str_ref(db_verb_names(h));
RUN_ACTIV.debug = (db_verb_flags(h) & VF_DEBUG);
alloc_rt_stack(&RUN_ACTIV, program->main_vector.max_stack);
RUN_ACTIV.pc = 0;
RUN_ACTIV.error_pc = 0;
RUN_ACTIV.bi_func_pc = 0;
RUN_ACTIV.temp.type = TYPE_NONE;
RUN_ACTIV.rt_env = env = new_rt_env(RUN_ACTIV.prog->num_var_names);
fill_in_rt_consts(env, program->version);
set_rt_env_var(env, SLOT_THIS, var_ref(RUN_ACTIV._this));
set_rt_env_var(env, SLOT_CALLER, var_ref(CALLER_ACTIV._this));
#define ENV_COPY(slot) \
set_rt_env_var(env, slot, var_ref(CALLER_ACTIV.rt_env[slot]))
ENV_COPY(SLOT_ARGSTR);
ENV_COPY(SLOT_DOBJ);
ENV_COPY(SLOT_DOBJSTR);
ENV_COPY(SLOT_PREPSTR);
ENV_COPY(SLOT_IOBJ);
ENV_COPY(SLOT_IOBJSTR);
if (is_wizard(CALLER_ACTIV.progr) &&
(CALLER_ACTIV.rt_env[SLOT_PLAYER].type == TYPE_OBJ))
ENV_COPY(SLOT_PLAYER);
else
set_rt_env_obj(env, SLOT_PLAYER, CALLER_ACTIV.player);
RUN_ACTIV.player = env[SLOT_PLAYER].v.obj;
#undef ENV_COPY
v.type = TYPE_STR;
v.v.str = str_ref(vname);
set_rt_env_var(env, SLOT_VERB, v); /* no var_dup */
set_rt_env_var(env, SLOT_ARGS, args); /* no var_dup */
return E_NONE;
}
#ifdef IGNORE_PROP_PROTECTED
#define bi_prop_protected(prop, progr) (0)
#else
#define bi_prop_protected(prop, progr) ((!is_wizard(progr)) && server_flag_option_cached(prop))
#endif /* IGNORE_PROP_PROTECTED */
/**
the main interpreter -- run()
everything is just an entry point to it
**/
static enum outcome
run(char raise, enum error resumption_error, Var * result)
{ /* runs the_vm */
/* If the returned value is OUTCOME_DONE and RESULT is non-NULL, then
* *RESULT is the value returned by the top frame.
*/
/* bc, bv, rts are distinguished as the state variables of run()
their value capture the state of the running between OP_ cases */
Bytecodes bc;
Byte *bv, *error_bv;
Var *rts; /* next empty slot */
enum Opcode op;
Var error_var;
enum outcome outcome;
/** a bunch of macros that work *ONLY* inside run() **/
/* helping macros about the runtime_stack. */
#define POP() (*(--rts))
#define PUSH(v) (*(rts++) = v)
#define PUSH_REF(v) PUSH(var_ref(v))
#define TOP_RT_VALUE (*(rts - 1))
#define NEXT_TOP_RT_VALUE (*(rts - 2))
#define READ_BYTES(bv, nb) \
( bv += nb, \
(nb == 1 \
? bv[-1] \
: (nb == 2 \
? ((unsigned) bv[-2] << 8) + bv[-1] \
: (((unsigned) bv[-4] << 24) \
+ ((unsigned) bv[-3] << 16) \
+ ((unsigned) bv[-2] << 8) \
+ bv[-1]))))
#define SKIP_BYTES(bv, nb) ((void)(bv += nb))
#define LOAD_STATE_VARIABLES() \
do { \
bc = ( (top_activ_stack != 0 || root_activ_vector == MAIN_VECTOR) \
? RUN_ACTIV.prog->main_vector \
: RUN_ACTIV.prog->fork_vectors[root_activ_vector]); \
bv = bc.vector + RUN_ACTIV.pc; \
error_bv = bc.vector + RUN_ACTIV.error_pc; \
rts = RUN_ACTIV.top_rt_stack; /* next empty slot */ \
} while (0)
#define STORE_STATE_VARIABLES() \
do { \
RUN_ACTIV.pc = bv - bc.vector; \
RUN_ACTIV.error_pc = error_bv - bc.vector; \
RUN_ACTIV.top_rt_stack = rts; \
} while (0)
#define RAISE_ERROR(the_err) \
do { \
if (RUN_ACTIV.debug) { \
STORE_STATE_VARIABLES(); \
if (raise_error(make_error_pack(the_err), 0)) \
return OUTCOME_ABORTED; \
else { \
LOAD_STATE_VARIABLES(); \
goto next_opcode; \
} \
} \
} while (0)
#define PUSH_ERROR(the_err) \
do { \
RAISE_ERROR(the_err); /* may not return!! */ \
error_var.type = TYPE_ERR; \
error_var.v.err = the_err; \
PUSH(error_var); \
} while (0)
#define PUSH_ERROR_UNLESS_QUOTA(the_err) \
do { \
if (E_QUOTA == (the_err) && \
!server_flag_option_cached(SVO_MAX_CONCAT_CATCHABLE)) \
{ \
/* simulate out-of-seconds abort resulting */ \
/* from monster malloc+copy taking too long */ \
STORE_STATE_VARIABLES(); \
abort_task(ABORT_SECONDS); \
return OUTCOME_ABORTED; \
} \
else \
PUSH_ERROR(the_err); \
} while (0)
#define JUMP(label) (bv = bc.vector + label)
/* end of major run() macros */
LOAD_STATE_VARIABLES();
if (raise) {
error_bv = bv;
PUSH_ERROR(resumption_error);
}
for (;;) {
next_opcode:
error_bv = bv;
op = (Opcode)(*bv++);
if (COUNT_TICK(op)) {
if (--ticks_remaining <= 0) {
STORE_STATE_VARIABLES();
abort_task(ABORT_TICKS);
return OUTCOME_ABORTED;
}
if (task_timed_out) {
STORE_STATE_VARIABLES();
abort_task(ABORT_SECONDS);
return OUTCOME_ABORTED;
}
}
switch (op) {
case OP_IF_QUES:
case OP_IF:
case OP_WHILE:
case OP_EIF:
do_test:
{
Var cond;
cond = POP();
if (!is_true(cond)) { /* jump if false */
unsigned lab = READ_BYTES(bv, bc.numbytes_label);
JUMP(lab);
}
else {
SKIP_BYTES(bv, bc.numbytes_label);
}
free_var(cond);
}
break;
case OP_JUMP:
{
unsigned lab = READ_BYTES(bv, bc.numbytes_label);
JUMP(lab);
}
break;
case OP_FOR_RANGE:
{
unsigned id = READ_BYTES(bv, bc.numbytes_var_name);
unsigned lab = READ_BYTES(bv, bc.numbytes_label);
Var from, to;
to = TOP_RT_VALUE;
from = NEXT_TOP_RT_VALUE;
if ((to.type != TYPE_INT && to.type != TYPE_OBJ)
|| to.type != from.type) {
RAISE_ERROR(E_TYPE);
free_var(POP());
free_var(POP());
JUMP(lab);
} else if (to.type == TYPE_INT
? from.v.num > to.v.num
: from.v.obj > to.v.obj) {
free_var(POP());
free_var(POP());
JUMP(lab);
} else {
free_var(RUN_ACTIV.rt_env[id]);
RUN_ACTIV.rt_env[id] = var_ref(from);
if (to.type == TYPE_INT) {
if (from.v.num < MAXINT) {
from.v.num++;
NEXT_TOP_RT_VALUE = from;
} else {
to.v.num--;
TOP_RT_VALUE = to;
}
} else {
if (from.v.obj < MAXOBJ) {
from.v.obj++;
NEXT_TOP_RT_VALUE = from;
} else {
to.v.obj--;
TOP_RT_VALUE = to;
}
}
}
}
break;
case OP_POP:
free_var(POP());
break;
case OP_IMM:
{
int slot;
/* If we'd just throw it away anyway (eg verbdocs),
skip both OPs. This accounts for most executions
of OP_IMM in my tests.
*/
if (bv[bc.numbytes_literal] == OP_POP) {
bv += bc.numbytes_literal + 1;
break;
}
slot = READ_BYTES(bv, bc.numbytes_literal);
PUSH_REF(RUN_ACTIV.prog->literals[slot]);
}
break;
case OP_MAP_CREATE:
{
Var map;
map = new_map();
PUSH(map);
}
break;
case OP_MAP_INSERT:
{
Var r, map, key, value;
enum error e = E_NONE;
key = POP(); /* any except list or map */
value = POP(); /* any */
map = POP(); /* should be map */
if (map.type != TYPE_MAP || key.is_collection()) {
free_var(key);
free_var(value);
free_var(map);
PUSH_ERROR(E_TYPE);
} else {
r = mapinsert(map, key, value);
if (value_bytes(r) <= server_int_option_cached(SVO_MAX_MAP_VALUE_BYTES))
PUSH(r);
else {
free_var(r);
PUSH_ERROR_UNLESS_QUOTA(E_QUOTA);
}
}
}
break;
case OP_MAKE_EMPTY_LIST:
{
Var list;
list = new_list(0);
PUSH(list);
}
break;
case OP_LIST_ADD_TAIL:
{
Var r, tail, list;
tail = POP(); /* whatever */
list = POP(); /* should be list */
if (list.type != TYPE_LIST) {
free_var(list);
free_var(tail);
PUSH_ERROR(E_TYPE);
} else {
r = listappend(list, tail);
if (value_bytes(r) <= server_int_option_cached(SVO_MAX_LIST_VALUE_BYTES))