forked from cotillion/cd-gamedriver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interpret.c
8380 lines (7471 loc) · 176 KB
/
interpret.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
/* vim: set ts=8 : */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/time.h>
#include <time.h>
#include <sys/types.h> /* sys/types.h and netinet/in.h are here to enable include of comm.h below */
#include <sys/stat.h>
/* #include <netinet/in.h> Included in comm.h below */
#include <memory.h>
#include <limits.h>
#include <math.h>
#include <float.h>
#include <errno.h>
#include <unistd.h>
#include "config.h"
#include "lint.h"
#include "mstring.h"
#include "lang.h"
#include "exec.h"
#include "interpret.h"
#include "object.h"
#include "instrs.h"
#include "comm.h"
#include "mapping.h"
#include "mudstat.h"
#include "lex.h"
#include "comm1.h"
#include "simulate.h"
#include "main.h"
#include "sent.h"
#include "json.h"
#include "random.h"
#include "inline_eqs.h"
#include "inline_svalue.h"
#include "efun_table.h"
#define FLOATASGOP(lhs, op, rhs) { lhs op (double)rhs; }
struct fkntab
{
char *name;
unsigned short inherit_index;
unsigned short function_index;
};
extern char *crypt(const char *key, const char *salt);
extern struct object *master_ob;
extern struct vector *inherit_list (struct object *);
struct svalue *sapply (char *, struct object *, int, int);
#ifdef TRACE_CODE
static void do_trace (char *, char *, char *);
static int strpref (char *, char *);
#endif
static int apply_low (char *, struct object *, int, int);
extern int do_rename (char *, char *);
static int inter_sscanf (int);
extern int wildmat(char *, char *);
static struct closure *alloc_objclosure(int ftype, int funinh, int funno, struct object *ob, char *refstr, int usecache);
static struct closure *alloc_objclosurestr(int ftype, char *funstr, struct object *ob, char *refstr, int usecache);
INLINE int search_for_function(char *name, struct program *prog);
int s_f_f(char *name, struct program *prog);
extern struct object *previous_ob;
extern char *last_verb, *trig_verb;
struct program *current_prog;
extern int s_flag, unlimited;
extern struct object *current_interactive;
int variable_index_found;
int variable_inherit_found;
int variable_type_mod_found;
unsigned long long cache_tries = 0, cache_hits = 0;
int num_closures, total_closure_size;
#ifdef CACHE_STATS
long long call_cache_saves = 0;
long long global_cache_saves = 0;
long long searches_needed = 0;
long long searches_done = 0;
long long global_first_saves = 0;
long long call_first_saves = 0;
#endif
#ifdef PROFILE_LPC
int trace_calls;
FILE *trace_calls_file;
double last_execution;
#endif
static int tracedepth;
#define TRACE_CALL 1
#define TRACE_CALL_OTHER 2
#define TRACE_RETURN 4
#define TRACE_ARGS 8
#define TRACE_EXEC 16
#define TRACE_TOS 32
#define TRACE_APPLY 64
#define TRACE_OBJNAME 128
#define TRACE_HEART_BEAT 512
#define TRACETST(b) (current_interactive->interactive->trace_level & (b))
#define TRACEP(b) \
(current_interactive && current_interactive->interactive && TRACETST(b) && \
(current_interactive->interactive->trace_prefix == 0 || \
(current_object && strpref(current_interactive->interactive->trace_prefix, \
current_object->name))) )
/*
* Inheritance:
* An object X can inherit from another object Y. This is done with
* the statement inherit "file";
* The inherit statement will clone a copy of that file, call reset
* in it, and set a pointer to Y from X.
* Y has to be removed from the linked list of all objects.
* All variables declared by Y will be copied to X, so that X has access
* to them.
*
* If Y isnt loaded when it is needed, X will be discarded, and Y will be
* loaded separetly. X will then be reloaded again.
*/
extern int d_flag;
extern int current_line, eval_cost;
/*
* These are the registers used at runtime.
* The control stack saves registers to be restored when a function
* will return. That means that control_stack[0] will have almost no
* interesting values, as it will terminate execution.
*/
char *pc; /* Program pointer. */
static struct svalue *fp; /* Pointer to first argument. */
struct svalue *sp; /* Points to value of last push. */
int inh_offset; /* Needed for inheritance */
struct svalue start_of_stack[EVALUATOR_STACK_SIZE];
struct svalue catch_value; /* Used to throw an error to a catch */
static struct control_stack control_stack[MAX_TRACE];
struct control_stack *csp; /* Points to last element pushed */
#ifdef COUNT_CALLS /* Temporary */
static int num_call_self, num_call_down, num_call_other;
#endif
/* These are set by search_for_function if it is successful
* function_inherit_found == num_inherit if in top program
* function_prog_found == Implied by inherit_found
*/
int function_inherit_found = -1;
struct program *function_prog_found = 0;
int function_index_found = -1;
unsigned short function_type_mod_found = 0;
/*
* Information about assignments of values:
*
* There are three types of l-values: Local variables, global variables
* and vector elements.
*
* The local variables are allocated on the stack together with the arguments.
* the register 'frame_pointer' points to the first argument.
*
* The global variables must keep their values between executions, and
* have space allocated at the creation of the object.
*
* Elements in vectors are similar to global variables. There is a reference
* count to the whole vector, that states when to deallocate the vector.
* The elements consists of 'struct svalue's, and will thus have to be freed
* immediately when over written.
*/
/*
* Probe to see if the stack can be safely extended.
*/
static INLINE void
probe_stack(int n)
{
if (sp + n >= &start_of_stack[EVALUATOR_STACK_SIZE])
error("stack overflow\n");
}
/*
* Extend the stack and check for overflow.
*/
static INLINE void
extend_stack(void)
{
probe_stack(1);
++sp;
}
/*
* Push an object pointer on the stack. Note that the reference count is
* incremented.
* A destructed object must never be pushed onto the stack.
*/
INLINE void
push_object(struct object *ob)
{
extend_stack();
if (ob != NULL && !(ob->flags & O_DESTRUCTED))
{
sp->type = T_OBJECT;
sp->u.ob = ob;
add_ref(ob, "push_object");
}
else
{
*sp = const0;
}
}
/*
* Push a number on the value stack.
*/
INLINE void
push_number(long long n)
{
extend_stack();
sp->type = T_NUMBER;
sp->u.number = n;
}
INLINE void
push_float(double f)
{
extend_stack();
sp->type = T_FLOAT;
sp->u.real = f;
}
INLINE void
push_function(struct closure *func, bool_t reference)
{
extend_stack();
sp->type = T_FUNCTION;
sp->u.func = func;
if (reference)
INCREF(func->ref);
}
/*
* Push a string on the value stack.
*/
INLINE void
push_string(char *p, int type)
{
extend_stack();
sp->type = T_STRING;
sp->string_type = type;
switch(type) {
case STRING_MSTRING:
sp->u.string = make_mstring(p);
break;
case STRING_SSTRING:
sp->u.string = make_sstring(p);
break;
case STRING_CSTRING:
sp->u.string = p;
break;
}
}
/*
* Get address to a valid global variable.
*/
static INLINE struct svalue *
find_value(int inh, int rnum)
{
int num;
if ((inh == 255 && rnum == 255))
{
error("Referencing undefined variable (inh == %d, var_num == %d, variables are %s).\n",
inh, rnum, current_object->variables?"defined":"undefined");
}
if (inh == 255)
inh = 0;
else
inh -= current_prog->num_inherited - 1;
#ifdef DEBUG
if (inh > 0)
fatal("Illegal variable access, %d(off %d). See trace above.\n",
inh, current_prog->num_inherited);
if (rnum > current_object->prog->inherit[inh_offset + inh].prog->num_variables - 1)
fatal("Illegal variable access, variable %d(off %d, in %d). See trace above.\n",
rnum, current_object->prog->inherit[inh_offset + inh].prog->num_variables,
inh_offset + inh);
#endif
num = current_object->prog->
inherit[inh_offset + inh].variable_index_offset + rnum;
return ¤t_object->variables[num];
}
/*
* Free the data that an svalue is pointing to. Not the svalue
* itself.
*/
#if defined(PROFILE)
void
free_svalue(struct svalue *v)
{
if (v->type & T_LVALUE)
return;
switch (v->type) {
case T_NUMBER:
case T_FLOAT:
break;
case T_STRING:
switch (v->string_type) {
case STRING_MSTRING:
free_mstring(v->u.string);
break;
case STRING_SSTRING:
free_sstring(v->u.string);
break;
case STRING_CSTRING:
break;
}
break;
case T_OBJECT:
free_object(v->u.ob, "free_svalue");
break;
case T_POINTER:
free_vector(v->u.vec);
break;
case T_MAPPING:
free_mapping(v->u.map);
break;
case T_FUNCTION:
free_closure(v->u.func);
break;
default:
fatal("Invalid value of variable!\n");
break;
}
*v = const0; /* marion - clear this value all away */
}
int
equal_svalue(const struct svalue *sval1, const struct svalue *sval2)
{
if (sval1->type == T_NUMBER && sval1->u.number == 0 &&
sval2->type == T_OBJECT && sval2->u.ob->flags & O_DESTRUCTED)
return 1;
else if (sval2->type == T_NUMBER && sval2->u.number == 0 &&
sval1->type == T_OBJECT && sval1->u.ob->flags & O_DESTRUCTED)
return 1;
else if (sval2->type == T_NUMBER && sval2->u.number == 0 &&
sval1->type == T_FUNCTION && !legal_closure(sval1->u.func))
return 1;
else if (sval2->type == T_NUMBER && sval2->u.number == 0 &&
sval1->type == T_FUNCTION && !legal_closure(sval1->u.func))
return 1;
else if (sval1->type != sval2->type)
return 0;
else switch (sval1->type) {
case T_NUMBER:
return sval1->u.number == sval2->u.number;
case T_POINTER:
return sval1->u.vec == sval2->u.vec;
case T_MAPPING:
return sval1->u.map == sval2->u.map;
case T_STRING:
return sval1->u.string == sval2->u.string ||
strcmp(sval1->u.string, sval2->u.string) == 0;
case T_OBJECT:
return ((sval1->u.ob->flags & O_DESTRUCTED) && (sval2->u.ob->flags & O_DESTRUCTED)) ||
sval1->u.ob == sval2->u.ob;
case T_FLOAT:
return sval1->u.real == sval2->u.real;
case T_FUNCTION:
return sval1->u.func == sval2->u.func;
}
return 0;
}
#endif /* !PROFILE */
/*
* Prepend a slash in front of a string.
*/
char *
add_slash(char *cp)
{
size_t len;
char *xp;
len = strlen(cp) + 1;
xp = allocate_mstring(len);
xp[0] = '/';
(void)memcpy(&xp[1], cp, len);
return xp;
}
/*
* Assign to a svalue.
* This is done either when element in vector, or when to an identifier
* (as all identifiers are kept in a vector pointed to by the object).
*/
INLINE void
assign_svalue_no_free(struct svalue *to, struct svalue *from)
{
if (to == from)
return;
#ifdef DEBUG
if (from == 0)
fatal("Null pointer to assign_svalue().\n");
#endif
*to = *from;
switch(from->type) {
case T_STRING:
switch(from->string_type) {
case STRING_MSTRING:
(void)reference_mstring(from->u.string);
break;
case STRING_SSTRING:
(void)reference_sstring(from->u.string);
break;
case STRING_CSTRING:
break;
default:
fatal("Bad string type %d\n", from->string_type);
}
break;
case T_OBJECT:
add_ref(to->u.ob, "ass to var");
break;
case T_POINTER:
INCREF(to->u.vec->ref);
break;
case T_MAPPING:
INCREF(to->u.map->ref);
break;
case T_FUNCTION:
INCREF(to->u.func->ref);
break;
}
}
INLINE void
assign_svalue(struct svalue *dest, struct svalue *v)
{
if (dest == v)
return;
/* First deallocate the previous value. */
free_svalue(dest);
assign_svalue_no_free(dest, v);
}
#if 0
/* This function has been replaced with a macro to save speed. Macro is defined
* in interpret.h
*/
void
push_svalue(struct svalue *v)
{
extend_stack();
assign_svalue_no_free(sp, v);
}
#endif
/*
* Pop the top-most value of the stack.
* Don't do this if it is a value that will be used afterwards, as the
* data may be sent to free(), and destroyed.
*/
INLINE
void pop_stack()
{
#ifdef DEBUG
if (sp < start_of_stack)
fatal("Stack underflow.\n");
#endif
free_svalue(sp--);
}
/*
* Compute the address of an array element.
*/
INLINE static void
push_indexed_lvalue(int needlval)
{
struct svalue *i, *vec, *item;
long long ind = 0; /* = 0 to make Wall quiet */
long long org_ind = 0;
i = sp;
vec = sp - 1;
if (vec->type != T_MAPPING)
{
if (i->type != T_NUMBER)
error("Illegal index.\n");
org_ind = ind = i->u.number;
}
switch(vec->type) {
case T_STRING: {
static struct svalue one_character;
/* marion says: this is a crude part of code */
pop_stack();
one_character.type = T_NUMBER;
if (ind < 0)
ind = strlen(vec->u.string) + ind;
if (ind > strlen(vec->u.string) || ind < 0)
one_character.u.number = 0;
else
one_character.u.number = vec->u.string[ind];
free_svalue(sp);
sp->type = T_LVALUE;
sp->u.lvalue = &one_character;
break;}
case T_POINTER:
pop_stack();
if (ind < 0)
ind = vec->u.vec->size + ind;
if (ind >= vec->u.vec->size || ind < 0)
error("Index out of bounds. Vector size: %d, index: %lld\n",
vec->u.vec->size, org_ind);
item = &vec->u.vec->item[ind];
if (vec->u.vec->ref == 1) {
static struct svalue quickfix = { T_NUMBER };
/* marion says: but this is crude too */
/* marion blushes. */
assign_svalue (&quickfix, item);
item = &quickfix;
}
free_svalue(sp); /* This will make 'vec' invalid to use */
sp->type = T_LVALUE;
sp->u.lvalue = item;
break;
case T_MAPPING:
item = get_map_lvalue(vec->u.map, i, needlval);
pop_stack();
if (vec->u.map->ref == 1) {
static struct svalue quickfix = { T_NUMBER };
assign_svalue (&quickfix, item);
item = &quickfix;
}
free_svalue(sp); /* This will make 'vec' invalid to use */
sp->type = T_LVALUE;
sp->u.lvalue = item;
break;
default:
error("Indexing on illegal type.\n");
break;
}
}
#ifdef OPCPROF
#define MAXOPC 512
static int opcount[MAXOPC];
#endif
/*
* Deallocate 'n' values from the stack.
*/
INLINE
void pop_n_elems(int n)
{
#ifdef DEBUG
if (n < 0)
fatal("pop_n_elems: %d elements.\n", n);
#endif
for (; n > 0; n--)
pop_stack();
}
char *
get_typename(int type)
{
switch(type)
{
case T_NUMBER:
return "Integer";
case T_STRING:
return "String";
case T_POINTER:
return "Array";
case T_OBJECT:
return "Object";
case T_MAPPING:
return "Mapping";
case T_FLOAT:
return "Float";
case T_FUNCTION:
return "Function";
}
return "Unknown";
}
void
bad_arg(int arg, int instr, struct svalue *sv)
{
error("Bad argument %d to %s(), received type was %s \n", arg, get_f_name(instr), get_typename(sv->type));
}
void
bad_arg_op(int instr, struct svalue *arg1, struct svalue *arg2)
{
error("Bad arguments to %s(), received types were %s and %s\n",
get_f_name(instr), get_typename(arg1->type), get_typename(arg2->type));
}
#if defined(PROFILE_LPC)
/* profile_exp_mtimebase should equal (1.0 - exp(-1.0/profile_timebase)) */
static long double profile_timebase = 60.0l, profile_exp_mtimebase = 0x8.766dfa92ba7052ep-9l;
long double
get_profile_timebase() {
return profile_timebase;
}
void
set_profile_timebase(long double timebase) {
if (timebase > 0.0) {
profile_timebase = timebase;
profile_exp_mtimebase = -expm1l(-1.0l / timebase);
}
}
#define _UPDATE_AV_C(delta_time) (expl(-(delta_time) / (profile_timebase)))
#define _UPDATE_AV(avg, C, amt) \
((avg) = (avg) * (C) + \
(amt) * (profile_exp_mtimebase))
#define UPDATE_AV(avg, delta_time, amt) (_UPDATE_AV((avg), _UPDATE_AV_C((delta_time)), (amt)))
double
current_cpu(void)
{
static struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ts.tv_sec + (ts.tv_nsec / 1e9);
}
void
update_prog_profile(struct program *prog, double now, double delta, double tot_delta)
{
UPDATE_AV(prog->cpu_avg, (now - prog->last_avg_update), delta);
prog->last_avg_update = now;
prog->cpu += delta;
}
void
update_func_profile(struct function *funp, double now, double delta, double tot_delta, int calls)
{
long double C = _UPDATE_AV_C((now - funp->last_call_update));
funp->last_call_update = now;
_UPDATE_AV(funp->avg_time, C, delta);
funp->time_spent += delta;
_UPDATE_AV(funp->avg_tot_time, C, tot_delta);
funp->tot_time_spent += tot_delta;
_UPDATE_AV(funp->avg_calls, C, calls);
funp->num_calls += calls;
}
#endif
void
save_control_context(struct control_stack *csp1)
{
#ifdef DEBUG
if (current_prog && pc - current_prog->program >= current_prog->program_size)
fatal("Invalid offset during context save\n");
#endif
csp1->ob = current_object;
csp1->prev_ob = previous_ob;
csp1->fp = fp;
csp1->funp = 0;
csp1->prog = current_prog;
csp1->extern_call = 0;
if (current_prog)
csp1->pc = pc - current_prog->program;
#ifdef DEBUG
else
csp1->pc = 0xdeadbeef;
#endif
csp1->inh_offset = inh_offset;
}
void
push_control_stack(struct object *ob, struct program *prog, struct function *funp)
{
if (csp >= &control_stack[MAX_TRACE-1])
error("Too deep recursion.\n");
csp++;
save_control_context(csp);
csp->funp = funp; /* Only used for tracebacks */
#if defined(PROFILE_LPC)
{
double now = current_cpu();
if (trace_calls) {
if (csp == control_stack)
fprintf(trace_calls_file, "%.3f %.6f\n",
(now - last_execution) * 1000.0, now);
fprintf(trace_calls_file, "%*s%s %s %s()\n",
(int)(csp - control_stack) * 4, "",
ob->name, prog->name, funp ? funp->name : "???");
}
if (csp != control_stack) {
csp[-1].frame_cpu += now - csp[-1].startcpu;
}
csp->frame_start = csp->startcpu = now;
csp->frame_cpu = 0.0;
}
#endif
}
/*
* Pop the control stack one element, and restore registers.
* extern_call must not be modified here, as it is used imediately after pop.
*/
void
restore_control_context(struct control_stack *csp1)
{
current_object = csp1->ob;
current_prog = csp1->prog;
if(current_prog)
{
pc = current_prog->program + csp1->pc;
}
previous_ob = csp1->prev_ob;
fp = csp1->fp;
inh_offset = csp1->inh_offset;
}
void
pop_control_stack()
{
#ifdef DEBUG
if (csp == control_stack - 1)
fatal("Popped out of the control stack\n");
#endif
#if defined(PROFILE_LPC)
{
double now = current_cpu();
double delta = csp->frame_cpu + (now - csp->startcpu);
double tot_delta = now - csp->frame_start;
if (current_prog)
update_prog_profile(current_prog, now, delta, tot_delta);
if (csp->funp)
update_func_profile(csp->funp, now, delta, tot_delta, 1);
if (csp != control_stack) {
csp[-1].startcpu = now;
} else
last_execution = now;
if (trace_calls) {
fprintf(trace_calls_file, "%*s--- %.3f / %.3f\n",
(int)(csp - control_stack) * 4, "",
delta * 1000.0,
tot_delta * 1000.0);
if (csp == control_stack)
putc('\n', trace_calls_file);
}
}
#endif
restore_control_context(csp);
csp--;
}
/*
* Push a pointer to a vector on the stack. Note that the reference count
* is incremented. Newly created vectors normally have a reference count
* initialized to 1.
*/
INLINE void
push_vector(struct vector *v, bool_t reference)
{
extend_stack();
if (reference)
INCREF(v->ref);
sp->type = T_POINTER;
sp->u.vec = v;
}
INLINE void
push_mapping(struct mapping *v, bool_t reference)
{
extend_stack();
if (reference)
INCREF(v->ref);
sp->type = T_MAPPING;
sp->u.map = v;
}
/*
* Push a string on the stack that is already allocated.
*/
INLINE void
push_mstring(char *p)
{
extend_stack();
sp->type = T_STRING;
sp->string_type = STRING_MSTRING;
sp->u.string = p;
}
extern char *string_print_formatted (int , char *, int, struct svalue *);
#ifdef TRACE_CODE
static void
do_trace_call(struct function *funp)
{
do_trace("Call direct ", funp->name, " ");
if (TRACETST(TRACE_ARGS))
{
int i;
char buff[1024];
(void)sprintf(buff, " with %d arguments: ", funp->num_arg);
write_socket(buff, command_giver);
for(i = 0; i < funp->num_arg; i++)
write_socket(string_print_formatted(0, "%O ", 1, &fp[i]),
command_giver);
}
write_socket("\n", command_giver);
}
#endif
static unsigned int previous_ob_access_time;
/*
* Argument is the function to execute.
* The function is located in current_prog.
* There is a number of arguments on the stack. Normalize them and initialize
* local variables, so that the called function is pleased.
*/
char *
setup_new_frame(struct function *funp)
{
int called_args;
unsigned short int npc;
char *off;
/* Remove excessive arguments, or put them in argv if applicable */
previous_ob_access_time = current_object->time_of_ref;
if (funp->type_flags & TYPE_MOD_TRUE_VARARGS)
{
if (csp->num_local_variables >= funp->num_arg)
{
struct vector *v;
int i, narg;
v = allocate_array(narg = csp->num_local_variables -
(funp->num_arg - 1));
for (i = narg - 1; i >= 0; i--)
assign_svalue_no_free(&v->item[narg - 1 - i], &sp[-i]);
pop_n_elems(narg);
push_vector(v, FALSE);
csp->num_local_variables -= narg - 1;
}
}
else
while(csp->num_local_variables > funp->num_arg)
{
pop_stack();
csp->num_local_variables--;
}
/* Correct number of arguments and local variables */
called_args = csp->num_local_variables;
while(csp->num_local_variables < funp->num_arg + (int)funp->num_local)
{
push_number(0);
csp->num_local_variables++;
}
#ifdef DEBUG
if (called_args > funp->num_arg)
fatal("Error in seting up call frame!\n");
#endif
if (called_args == funp->num_arg)
npc = funp->offset + funp->num_arg * 2;
else
{
off = current_prog->program + funp->offset + called_args * 2;
((char *)&npc)[0] = off[0];
((char *)&npc)[1] = off[1];
}
tracedepth++;
fp = sp - csp->num_local_variables + 1;
#ifdef TRACE_CODE
if (TRACEP(TRACE_CALL)) {
do_trace_call(funp);
}
#endif
return current_prog->program + npc;
}
/* marion
* maintain a small and inefficient stack of error recovery context
* data structures.
* This routine is called in three different ways:
* push=-1 Pop the stack.
* push=1 push the stack.
* push=0 No error occured, so the pushed value does not have to be
* restored. The pushed value can simply be popped into the void.
*
* The stack is implemented as a linked list of stack-objects, allocated
* from the heap, and deallocated when popped.
*/
void
push_pop_error_context (int push)
{
static struct error_context_stack
{
struct gdexception *exception;
struct control_stack *save_csp;
struct object *save_command_giver;
struct svalue *save_sp;
struct error_context_stack *next;
struct control_stack cstack;
} *ecsp = 0, *p;
if (push == 1) {
/*
* Save some global variables that must be restored separately
* after a longjmp. The stack will have to be manually popped all
* the way.
*/
p = (struct error_context_stack *)xalloc (sizeof *p);
save_control_context(&(p->cstack));
p->save_sp = sp;
p->save_csp = csp;
p->save_command_giver = command_giver;
p->exception = exception;
p->next = ecsp;
ecsp = p;
} else {
p = ecsp;
if (p == 0)
fatal("Catch: error context stack underflow\n");
if (push == 0) {
#ifdef DEBUG
#if 0
if (csp != p->save_csp-1)
fatal("Catch: Lost track of csp\n");
#endif
#endif
;
} else {
/* push == -1 !
* They did a throw() or error. That means that the control
* stack must be restored manually here.
*/
#ifdef PROFILE_LPC
double now = current_cpu();
struct program *prog = current_prog;
csp->frame_cpu += (now - csp->startcpu);
for (;csp != p->save_csp; (prog = csp->prog), csp--) {
double frame_tot_cpu = now - csp->frame_start;
if (prog)
update_prog_profile(prog, now, csp->frame_cpu, frame_tot_cpu);
if (csp->funp)
update_func_profile(csp->funp, now, csp->frame_cpu, frame_tot_cpu, 1);
if (trace_calls) {
fprintf(trace_calls_file, "%*s--- %.3f / %.3f\n",
(int)(csp - control_stack) * 4, "",
csp->frame_cpu * 1000.0,
frame_tot_cpu * 1000.0);
}
}
csp->startcpu = now;
#else
csp = p->save_csp;
#endif
pop_n_elems (sp - p->save_sp);
command_giver = p->save_command_giver;
}
exception = p->exception;
ecsp = p->next;
restore_control_context(&(p->cstack));
free ((char *)p);
}
}
/*
* May current_object shadow object ob ? We rely heavily on the fact that
* function names are pointers to shared strings, which means that equality
* can be tested simply through pointer comparison.
*/