forked from xythian/wp-lambdamoo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
execute.c
3348 lines (2994 loc) · 86.4 KB
/
execute.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) 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 "config.h"
#include "db.h"
#include "db_io.h"
#include "decompile.h"
#include "eval_env.h"
#include "eval_vm.h"
#include "exceptions.h"
#include "execute.h"
#include "functions.h"
#include "list.h"
#include "log.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 "waif.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;
#ifdef WAIF_DICT
/*
* Jay Carlson's WAIF DICT patch. These static moo-strings are needed for
* the new call_verb2 interface which assumes verb is a moo-string rather
* than a (non reference-counted) C-string.
*/
static char *waif_index_verb;
static char *waif_indexset_verb;
#endif /* WAIF_DICT */
/* 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 = 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 ");
stream_printf(str, "#%d:%s", activ_stack[t].vloc,
activ_stack[t].verbname);
if (activ_stack[t].vloc != activ_stack[t].this)
stream_printf(str, " (this == #%d)", activ_stack[t].this);
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);
}
void
output_to_log(const char *line)
{
oklog("%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, top_activ_stack + 1);
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 int
unwind_stack(Finally_Reason why, Var value, enum outcome *outcome)
{
/* Returns true iff the entire stack was unwound and the interpreter
* should stop, in which case *outcome is the correct outcome to return. */
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 BI_RETURN:
*(a->top_rt_stack++) = p.u.ret;
return 0;
case 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 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 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 BI_KILL:
return unwind_stack(FIN_ABORT, zero, outcome);
}
} 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 BI_RETURN:
free_var(p.u.ret);
break;
case BI_RAISE:
free_var(p.u.raise.code);
free_str(p.u.raise.msg);
free_var(p.u.raise.value);
break;
case BI_SUSPEND:
case BI_KILL:
break;
case 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 == 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)
{
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] = var_ref(stack[i].THIS);
v.v.list[2].type = TYPE_STR;
v.v.list[2].v.str = str_ref(stack[i].verb);
v.v.list[3].type = TYPE_OBJ;
v.v.list[3].v.obj = stack[i].progr;
v.v.list[4].type = TYPE_OBJ;
v.v.list[4].v.obj = stack[i].vloc;
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 = 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;
}
static int
raise_error(package p, enum outcome *outcome)
{
/* ASSERT: p.kind == 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);
if (why == FIN_UNCAUGHT) {
save_handler_info("handle_uncaught_error", value);
value = zero;
}
return unwind_stack(why, value, outcome);
}
static void
abort_task(int is_ticks)
{
Var value;
const char *msg = (is_ticks ? "Task ran out of ticks"
: "Task ran out of seconds");
value = new_list(3);
value.v.list[1].type = TYPE_STR;
value.v.list[1].v.str = str_dup(is_ticks ? "ticks" : "seconds");
value.v.list[2] = make_stack_list(activ_stack, 0, top_activ_stack, 1,
root_activ_vector, 1);
value.v.list[3] = error_backtrace_list(msg);
save_handler_info("handle_task_timeout", value);
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->THIS);
free_var(ap->temp);
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 this, 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 this, const char *vname_in, Var args, int do_pass)
{
const char *vname = str_dup(vname_in);
enum error result;
Var THIS;
THIS.type = TYPE_OBJ;
THIS.v.obj = this;
result = call_verb2(this, vname, THIS, args, do_pass);
/* call_verb2 got any refs it wanted */
free_str(vname);
return result;
}
enum error
call_verb2(Objid this, 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 */
Objid where;
db_verb_handle h;
Program *program;
Var *env;
Var v;
if (do_pass)
if (!valid(RUN_ACTIV.vloc))
return E_INVIND;
else
where = db_object_parent(RUN_ACTIV.vloc);
else
where = this;
if (!valid(where))
return E_INVIND;
h = db_find_callable_verb(where, vname);
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 = this;
RUN_ACTIV.THIS = var_ref(THIS);
RUN_ACTIV.progr = db_verb_owner(h);
RUN_ACTIV.vloc = 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(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;
if (vname[0] == WAIF_VERB_PREFIX)
v.v.str = str_dup(vname + 1);
else
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;
}
static int
rangeset_check(int end, int from, int to)
{
if (from > end + 1 || to < 0)
return 1;
return 0;
}
#ifdef IGNORE_PROP_PROTECTED
#define bi_prop_protected(prop, progr) (0)
#else
static int
bi_prop_protected(enum bi_prop prop, Objid progr)
{
const char *pname = 0; /* silence warning */
if (is_wizard(progr))
return 0;
switch (prop) {
case BP_NAME:
pname = "protect_name";
break;
case BP_OWNER:
pname = "protect_owner";
break;
case BP_PROGRAMMER:
pname = "protect_programmer";
break;
case BP_WIZARD:
pname = "protect_wizard";
break;
case BP_R:
pname = "protect_r";
break;
case BP_W:
pname = "protect_w";
break;
case BP_F:
pname = "protect_f";
break;
case BP_LOCATION:
pname = "protect_location";
break;
case BP_CONTENTS:
pname = "protect_contents";
break;
default:
panic("Can't happen in BI_PROP_PROTECTED!");
}
return server_flag_option(pname);
}
#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 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 = *bv++;
if (COUNT_TICK(op)) {
if (--ticks_remaining <= 0) {
STORE_STATE_VARIABLES();
abort_task(1);
return OUTCOME_ABORTED;
}
if (task_timed_out) {
STORE_STATE_VARIABLES();
abort_task(0);
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_LIST:
{
unsigned id = READ_BYTES(bv, bc.numbytes_var_name);
unsigned lab = READ_BYTES(bv, bc.numbytes_label);
Var count, list;
count = TOP_RT_VALUE; /* will be a integer */
list = NEXT_TOP_RT_VALUE; /* should be a list */
if (list.type != TYPE_LIST) {
RAISE_ERROR(E_TYPE);
free_var(POP());
free_var(POP());
JUMP(lab);
} else if (count.v.num > list.v.list[0].v.num /* size */ ) {
free_var(POP());
free_var(POP());
JUMP(lab);
} else {
free_var(RUN_ACTIV.rt_env[id]);
RUN_ACTIV.rt_env[id] = var_ref(list.v.list[count.v.num]);
count.v.num++; /* increment count */
TOP_RT_VALUE = count;
}
}
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)
from.v.num++;
else
from.v.obj++;
NEXT_TOP_RT_VALUE = from;
}
}
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_MAKE_EMPTY_LIST:
{
Var list;
list = new_list(0);
PUSH(list);
}
break;
case OP_LIST_ADD_TAIL:
{
Var 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
PUSH(listappend(list, tail));
}
break;
case OP_LIST_APPEND:
{
Var tail, list;
tail = POP(); /* second, should be list */
list = POP(); /* first, should be list */
if (tail.type != TYPE_LIST || list.type != TYPE_LIST) {
free_var(tail);
free_var(list);
PUSH_ERROR(E_TYPE);
} else
PUSH(listconcat(list, tail));
}
break;
case OP_INDEXSET:
{
Var value, index, list;
value = POP(); /* rhs value */
index = POP(); /* index, should be integer */
list = POP(); /* lhs except last index, should be list or str */
/* whole thing should mean list[index] = value */
#ifdef WAIF_DICT
if (list.type == TYPE_WAIF) {
Objid class;
Var args;
enum error err = E_NONE;
args = new_list(2);
args.v.list[1] = var_ref(index);
args.v.list[2] = var_ref(value);
class = list.v.waif->class;
if (!valid(class)) {
err = E_INVIND;
} else if (!is_wizard(db_object_owner(class))) {
err = E_TYPE;
} else {
STORE_STATE_VARIABLES();
err = call_verb2(class, waif_indexset_verb, list, args, 0);
if (err == E_VERBNF) {
err = E_TYPE;
}
LOAD_STATE_VARIABLES();
}
free_var(index);