-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmisc.c
1609 lines (1471 loc) · 38.9 KB
/
misc.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
/*
* miscellaneous Jekyllries
*/
#define E_NEEDS_FILE
#define E_NEEDS_STAT
#define E_NEEDS_STRING
#define E_NEEDS_NTOH
#include "system.h"
#include "vm_exp.h"
#include "iset.h"
#include "iisc.h"
#include "ooisc.h"
#include "oisc.h"
#include "trace.h"
#include "types.h"
#include "misc.h"
#include "assert.h"
#include "gaggle.h"
#include "locate.h"
typedef int (*ccallFunction)();
typedef struct CCallDescriptor {
ccallFunction ccFunction;
char* ccName;
char* ccArgTemplate;
} CCallDescriptor;
#include "globals.h"
#include "read.h"
#define OPOIDTABLESIZE 101
extern void die(void);
int stackSize = STACKSIZE;
void *extraRoots[10];
int extraRootsSP;
static char *OpNames = NULL;
static char **OpIDsToNames;
extern int debug(State *, char *);
#undef isalnum
#undef isxdigit
#undef isdigit
#undef isspace
#undef islower
static int isalnum(int c)
{
return (('0' <= c && c <= '9') || ('a' <= c && c <= 'z') ||
('A' <= c && c <= 'Z'));
}
static int isxdigit(int c)
{
return (('0' <= c && c <= '9') || ('a' <= c && c <= 'f') ||
('A' <= c && c <= 'F'));
}
static int isdigit(int c)
{
return ('0' <= c && c <= '9');
}
static int islower(int c)
{
return ('a' <= c && c <= 'z');
}
static int isspace(int c)
{
return (c == ' ' || c == '\t' || c == '\n' || c == '\r');
}
#define DIGIT(x) (isdigit(x) ? (x) - '0' : \
islower(x) ? (x) + 10 - 'a' : (x) + 10 - 'A')
#define MBASE ('z' - 'a' + 1 + 10)
int mstrtol(const char *str, char **ptr, int base)
{
register int val;
register int c;
int xx, neg = 0;
if (ptr != (char **)0)
*ptr = (char *)str; /* in case no number is formed */
if (base < 0 || base > MBASE)
return (0); /* base is invalid -- should be a fatal error */
if (!isalnum(c = *str)) {
while (isspace(c))
c = *++str;
switch (c) {
case '-':
neg++;
case '+': /* fall-through */
c = *++str;
}
}
if (base == 0) {
if (c != '0')
base = 10;
else if (str[1] == 'x' || str[1] == 'X')
base = 16;
else
base = 8;
}
/*
* for any base > 10, the digits incrementally following
* 9 are assumed to be "abc...z" or "ABC...Z"
*/
if (!isalnum(c) || (xx = DIGIT(c)) >= base)
return (0); /* no number formed */
if (base == 16 && c == '0' && isxdigit(str[2]) &&
(str[1] == 'x' || str[1] == 'X'))
c = *(str += 2); /* skip over leading "0x" or "0X" */
for (val = -DIGIT(c); isalnum(c = *++str) && (xx = DIGIT(c)) < base; )
/* accumulate neg avoids surprises near MAXLONG */
val = base * val - xx;
if (ptr != (char **)0)
*ptr = (char *)str;
return (neg ? val : -val);
}
static char blanks[] = " ";
extern int arDepth(u32, u32);
void obsolete(char *name, State *state)
{
ftrace("Attempt to use obsolete bytecode %s", name);
debug(state, "Obsolete bytecode");
}
void docall(int op, int sp, int fp, ConcreteType ct, Object oop, int sb)
{
printf("%.*sCall of %.*s.%s o = %#x, a = <%#x, %#x> sp=%#x\n",
2 * arDepth(fp, sb),
blanks,
ct->d.name->d.items,
ct->d.name->d.data,
OperationName(op), (Bits32)oop, *(int *)(sp - 8),
*(int *)(sp - 4),
sp);
}
void docallct(OpVectorElement ove, int sp, int fp, ConcreteType ct, Object oop, int sb)
{
printf("%.*sCall of %.*s.%.*s o = %#x, a = <%#x, %#x> sp=%#x\n",
2 * arDepth(fp, sb),
blanks,
ct->d.name->d.items,
(char *)ct->d.name->d.data,
ove->d.name->d.items, (char *)ove->d.name->d.data,
(Bits32)oop, *(int *)(sp - 8),
*(int *)(sp - 4),
sp);
}
void doret(int fp, int sb, int pc, ConcreteType ct)
{
int opid = -3;
unsigned char *pcc = (unsigned char *)pc;
char *name = "";
int namelen = 99;
/* Find the opid. There are 2 call instructions and padding (with ':') so */
/* we have to do some work here to figure out how we were called. */
if (pc == -1 || pc == -2) {
opid = pc;
} else {
if (pcc[-3] == CALLOIDS || (pcc[-3] == ':' && pcc[-4] == CALLOIDS)) {
opid = ((unsigned short *)pc)[-1];
} else if (pcc[-5] == CALLOID ||
(pcc[-5] == ':' && pcc[-6] == CALLOID) ||
(pcc[-5] == ':' && pcc[-6] == ':' && pcc[-7] == CALLOID) ||
(pcc[-5] == ':' && pcc[-6] == ':' && pcc[-7] == ':' && pcc[-8] == CALLOID)) {
opid = ((unsigned int *)pc)[-1];
} else if (pcc[-1] == CALLSTAR) {
name = ".* invoke";
} else if (pcc[-2] == CALLCTB) {
int which = pcc[-1];
opid = -3;
name = (char *)ct->d.opVector->d.data[which]->d.name->d.data;
namelen = ct->d.opVector->d.data[which]->d.name->d.items;
} else if (pcc[-3] == CALLS || (pcc[-3] == ':' && pcc[-4] == CALLS)) {
opid = -3;
name = "some calls";
} else {
printf("%.*sCan't figure out the call instruction\n", 2 * arDepth(fp, sb), blanks);
opid = 0;
}
}
printf("%.*sReturn %.*s\n", 2 * arDepth(fp, sb), blanks,
opid == -3 ? namelen : 99,
opid == -3 ? name : OperationName(opid));
}
#ifdef WIN32
#define stat _stat
#define fstat _fstat
#define read _read
#define open _open
#define close _close
#endif
void OpoidsInit(void)
{
char OpoidFile[1024];
char *ReadHead, *root;
int fd, i;
int NumberOfOps;
struct stat buf;
if (OpNames == NULL) {
root = getenv("EMERALDROOT");
if (!root)
root = EMERALDROOT;
strcpy(OpoidFile, root);
strcat(OpoidFile, "/lib/opoid");
fd = open(OpoidFile, O_RDONLY | O_BINARY, 0);
if (fstat(fd, &buf) < 0)
FatalError("OpoidsInit :");
OpNames = (char *) vmMalloc(buf.st_size + 1);
read(fd, OpNames, buf.st_size);
NumberOfOps = 0;
for (ReadHead = OpNames; ReadHead < OpNames + buf.st_size; ReadHead++) {
NumberOfOps++;
while (*ReadHead != '\n' && ReadHead < OpNames + buf.st_size)
ReadHead++;
if( ReadHead < OpNames + buf.st_size) *ReadHead = '\0';
}
OpIDsToNames = vmCalloc(NumberOfOps, sizeof(char *));
ReadHead = OpNames;
for (i = 0; i < NumberOfOps; i++) {
OpIDsToNames[i] = ReadHead;
while (*ReadHead)
ReadHead++;
ReadHead++;
}
close(fd);
}
return;
}
char *OperationName(int OpID)
{
if (OpID == -1) {
return "initially";
} else if (OpID == -2) {
return "process";
} else if (OpID == -3) {
return "recovery";
} else if (OpID < 2000) {
if (OpNames == NULL) OpoidsInit();
return OpIDsToNames[OpID];
} else {
return "*unknown*";
}
}
#ifdef PROFILEINVOKES
/*
* This stuff is wrong - it uses the pre-merger types. However, since
* Mark didn't change it in his version, I'm leaving it the way it is,
* at least for now. - david carlton
*/
typedef struct ProfileEntry {
unsigned int ncalls;
unsigned int nrecursivecalls;
unsigned int nbytecodes;
unsigned int childbytecodes;
IISc callers, callees;
OpVectorElement ove;
unsigned marked:1;
unsigned onStack:1;
unsigned isCycle:1;
unsigned number:29;
unsigned lowLink;
struct ProfileEntry *cycle;
} ProfileEntry;
IISc invokeprofile, /* ove -> ProfileEntry */
opvectorentry2ct; /* ove -> ct */
unsigned int *currentOPECount;
unsigned int *opestack[800];
int opesp = 0;
void profileBump(pc, ove, ct)
unsigned int pc;
OpVectorElement ove;
ConcreteType ct;
{
ProfileEntry *p = (ProfileEntry *)IIScLookup(invokeprofile, ove);
if ((int)p == IIScNIL) {
p = (ProfileEntry *)vmMalloc(sizeof(ProfileEntry));
p->marked = p->onStack = p->isCycle = 0;
p->ncalls = 0;
p->nrecursivecalls = 0;
p->nbytecodes = 0;
p->childbytecodes = 0;
p->callers = IIScCreate();
p->callees = IIScCreate();
p->ove = ove;
p->cycle = NULL;
IIScInsert(invokeprofile, (int)ove, (int)p);
IIScInsert(opvectorentry2ct,(int)ove,(int)ct);
}
p->ncalls ++;
opestack[opesp++] = currentOPECount;
currentOPECount = &p->nbytecodes;
IIScBump(p->callers, pc);
}
profileRet()
{
currentOPECount = opestack[--opesp];
}
ProfileEntry **pes;
static int comparepeaddr(a, b)
ProfileEntry **a, **b;
{
return (*a)->ove->d.code->d.data - (*b)->ove->d.code->d.data;
}
static int comparepeweight(a, b)
ProfileEntry **a, **b;
{
return ((*b)->nbytecodes +(*b)->childbytecodes) -
((*a)->nbytecodes + (*a)->childbytecodes);
}
static int comparepeself(a, b)
ProfileEntry **a, **b;
{
return ((*b)->nbytecodes) - ((*a)->nbytecodes);
}
buildCodeVector()
{
OpVectorElement ove;
ConcreteType ct;
int i = 0;
pes = (ProfileEntry **)
vmMalloc(IIScSize(opvectorentry2ct) * sizeof(ProfileEntry *) + 30);
IIScForEach(opvectorentry2ct, ove, ct) {
pes[i++] = (ProfileEntry *)IIScLookup(invokeprofile, ove);
} IIScNext();
qsort(pes, i, sizeof(ProfileEntry *), comparepeaddr);
}
OpVectorElement findCaller(pc)
unsigned char *pc;
{
int low = 0, hi = IIScSize(opvectorentry2ct), mid;
OpVectorElement ove;
while (low < hi) {
mid = (hi + low) / 2;
ove = pes[mid]->ove;
if (pc > ove->d.code->d.data) {
if (pc <= &ove->d.code->d.data[ove->d.code->d.items]) {
low = mid;
break;
} else {
low = mid + 1;
}
} else {
hi = mid - 1;
}
}
ove = pes[low]->ove;
if (ove->d.code->d.data <= pc && pc <= &ove->d.code->d.data[ove->d.code->d.items]) {
return ove;
} else {
return NULL;
}
}
static void pad(n)
{
while (n-- > 0) putchar(' ');
}
printove(ove, width)
OpVectorElement ove;
int width;
{
ConcreteType ct;
ct = (ConcreteType) IIScLookup(opvectorentry2ct, ove);
printf("%.*s.%.*s", ct->d.name->d.items, ct->d.name->d.data,
width == 0 ? ove->d.name->d.items :
max(0, min(width - ct->d.name->d.items - 1, ove->d.name->d.items)), ove->d.name->d.data);
pad(width - ct->d.name->d.items - ove->d.name->d.items - 1);
}
printpe(p, width)
ProfileEntry *p;
int width;
{
if (p->isCycle) {
printf("Cycle %3d", (int)p->ove);
pad(width - 9);
} else {
if (p->cycle != NULL) {
printf("<%2d> ", p->cycle->ove);
width -= 5;
}
printove(p->ove, width);
}
}
IISc roots;
buildProfileGraph()
{
IISc old;
OpVectorElement e, caller;
ProfileEntry *p, *callerp;
unsigned int fromwhere, count, howmany;
roots = IIScCreate();
IIScForEach(invokeprofile, e, p) {
old = p->callers;
p->callers = IIScCreate();
IIScForEach(old, fromwhere, howmany) {
caller = findCaller(fromwhere);
if (caller != NULL) {
callerp = (ProfileEntry *)IIScLookup(invokeprofile, caller);
assert(callerp != (ProfileEntry *)IIScNIL);
count = IIScLookup(p->callers, callerp);
if (count == IIScNIL) count = 0;
if (callerp == p) {
p->nrecursivecalls += howmany;
p->ncalls -= howmany;
} else {
IIScInsert(p->callers, callerp, howmany + count);
IIScInsert(callerp->callees, p, howmany + count);
}
}
} IIScNext();
IIScDestroy(old);
if (IIScSize(p->callers) == 0) {
IIScInsert(roots, p, 1);
}
} IIScNext();
}
static int scccount = 0;
static ProfileEntry **sccstack;
static int sccsp;
/* static NodePtr SCComponent = NULL; */
void propagate1(p)
ProfileEntry *p;
{
ProfileEntry *calleep;
unsigned int howmany;
double fraction;
IIScForEach(p->callees, calleep, howmany) {
fraction = (double)howmany / (double) calleep->ncalls;
p->childbytecodes +=
fraction * (calleep->nbytecodes + calleep->childbytecodes) + 0.5;
if (calleep->cycle != NULL) {
fraction = (double)howmany / (double) calleep->cycle->ncalls;
p->childbytecodes +=
fraction * (calleep->cycle->nbytecodes) + 0.5;
}
} IIScNext();
}
void propagateN(start, end)
int start, end;
{
int i;
ProfileEntry *p, *calleep, *cycle;
unsigned int howmany, count;
double fraction;
static int serial = 0;
cycle = (ProfileEntry *)vmMalloc(sizeof(ProfileEntry));
cycle->ncalls = 0;
cycle->nrecursivecalls = 0;
cycle->childbytecodes = 0;
cycle->nbytecodes = 0;
cycle->callers = IIScCreate();
cycle->callees = IIScCreate();
cycle->isCycle = 1;
cycle->ove = (OpVectorElement) ++serial;
cycle->onStack = 1;
cycle->marked = 1;
cycle->cycle = NULL;
pes[IIScSize(invokeprofile)] = cycle;
IIScInsert(invokeprofile, cycle->ove, cycle);
/* Propagate the costs for the non-cycle callees to their parent nodes */
for (i = start; i <= end; i++) {
p = sccstack[i];
p->cycle = cycle;
IIScForEach(p->callees, calleep, howmany) {
fraction = (double)howmany / (double) calleep->ncalls;
if (calleep->onStack) {
/* Something in this same cycle */
} else {
p->childbytecodes +=
fraction * (calleep->nbytecodes + calleep->childbytecodes) + 0.5;
if (calleep->cycle != NULL) {
fraction = (double)howmany / (double) calleep->cycle->ncalls;
p->childbytecodes +=
fraction * (calleep->cycle->nbytecodes) + 0.5;
}
}
} IIScNext();
}
/* Propagate the cycle costs to the cycle node */
for (i = start; i <= end; i++) {
p = sccstack[i];
cycle->ncalls += p->ncalls;
IIScForEach(p->callees, calleep, howmany) {
fraction = (double)howmany / (double) calleep->ncalls;
if (calleep->onStack) {
cycle->nbytecodes +=
fraction * (calleep->nbytecodes + calleep->childbytecodes) + 0.5;
count = IIScLookup(cycle->callees, calleep);
if (count == IIScNIL) count = 0;
IIScInsert(cycle->callees, calleep, count + howmany);
cycle->ncalls -= howmany;
cycle->nrecursivecalls += howmany;
}
} IIScNext();
}
}
void doSCs(v)
register ProfileEntry *v;
{
unsigned int howmany;
ProfileEntry *w, *x;
v->marked = 1;
v->number = scccount++;
v->lowLink = v->number;
sccstack[++sccsp] = v;
v->onStack = 1;
IIScForEach(v->callees, w, howmany) {
if (! w->marked) {
doSCs(w);
v->lowLink = min(v->lowLink, w->lowLink);
} else {
if (w->number < v->number && w->onStack) {
v->lowLink = min(w->number, v->lowLink);
}
}
} IIScNext();
if (v->lowLink == v->number) {
int start, end;
start = end = sccsp;
while (start >= 0) {
x = sccstack[start];
assert(x->onStack);
if (x == v) break;
start--;
}
if (start == end) {
propagate1(sccstack[start]);
} else {
propagateN(start, end);
}
while (start <= end) {
x = sccstack[end--];
x->onStack = 0;
}
sccsp = start - 1;
}
}
propagateProfileInfo()
{
int x;
ProfileEntry *p;
sccstack = (ProfileEntry **)
vmMalloc(IIScSize(invokeprofile) * sizeof(ProfileEntry *));
sccsp = -1;
IIScForEach(roots, p, x) {
assert(!p->marked);
doSCs(p);
} IIScNext();
gc_free(sccstack);
}
printOneGuy(indent, p, n, c, d, q, newline, base)
int indent, newline;
ProfileEntry *p, *q, *base;
unsigned int n, d;
char c;
{
int i;
double fraction;
pad(indent);
printpe(p, 28-indent);
if (n && d && n != d) {
fraction = (double)n / d;
} else {
fraction = 1.0;
}
if (n) printf("%6d", n); else pad(6);
if (d) printf("%c%-6d" , c, d); else pad(7);
if (c == '+') {
printf("%9d %9d", q->nbytecodes, q->childbytecodes);
} else {
printf("%9d ", (int)(fraction * q->nbytecodes + 0.5));
{
double children = fraction * q->childbytecodes;
if (!base->isCycle && q->cycle != NULL && q->cycle != base->cycle) {
fraction = (double)n / q->cycle->ncalls;
children += fraction * q->cycle->nbytecodes;
}
printf("%9d", (int)(children + 0.5));
}
}
if (newline) putchar('\n');
}
outputProfileInfo()
{
int i, limit = IIScSize(invokeprofile);
static char *separator = "----------------------------------------\n";
ProfileEntry *p, *q;
unsigned int howmany;
extern int totalbytecodes;
int mytotalbytecodes = 0;
qsort(pes, limit, sizeof(ProfileEntry *), comparepeself);
for (i = 0; i < limit; i++) {
p = pes[i];
if (!p->isCycle) mytotalbytecodes += p->nbytecodes;
}
printf("Accounted for byte codes = %d\n", mytotalbytecodes);
printf(separator);
printf("Flat profile\n");
printf(separator);
for (i = 0; i < limit; i++) {
p = pes[i];
if (p->isCycle) continue;
printf("%5.1f ", (double)p->nbytecodes / totalbytecodes * 100);
printpe(p, 28);
printf(" %10d %13d\n", p->ncalls, p->nbytecodes);
}
qsort(pes, limit, sizeof(ProfileEntry *), comparepeweight);
printf(separator);
printf("Propagated profile\n");
printf(separator);
for (i = 0; i < limit; i++) {
p = pes[i];
if (IIScSize(p->callers) == 0 && !p->isCycle) {
pad(10);
printf("<spontaneous>\n");
} else {
IIScForEach(p->callers, q, howmany) {
pad(6); printOneGuy(4, q, howmany, '/', p->ncalls, p, 1, p);
} IIScNext();
}
printf("%5.1f ", (double)(p->nbytecodes + p->childbytecodes) / totalbytecodes * 100);
printOneGuy(0, p, p->ncalls, '+', p->nrecursivecalls, p, 0, p);
printf(" = %9d\n", p->nbytecodes + p->childbytecodes);
IIScForEach(p->callees, q, howmany) {
pad(6); printOneGuy(4, q, howmany, '/', q->ncalls, q, 1, p);
} IIScNext();
printf(separator);
}
}
outputInvokeProfile()
{
OpVectorElement e;
ConcreteType ct;
unsigned int howmany;
ProfileEntry *p, *callerp;
printf("Jekyll Invocation Profile:\n");
buildCodeVector();
buildProfileGraph();
propagateProfileInfo();
outputProfileInfo();
}
#endif /* PROFILEINVOKES */
void setBits(Vector v, int off, int len, u32 val)
{
u32 m = -1L, temp;
int wordoff = off >> 5;
off = off & 31;
temp = ((int *)v->d.data)[wordoff];
m = m << (32 - len);
m = m >> (off & 31);
temp = (temp & ~m) | (m & (val << (32 - (off & 31) - len)));
((int *)v->d.data)[wordoff] = temp;
}
void ntohBits(Vector v, int off, int len)
{
int wordoff;
if (len == 32) {
u32 temp;
wordoff = off >> 5;
temp = *((u32 *)v->d.data + wordoff);
temp = ntohl(temp);
*((u32 *)v->d.data + wordoff) = temp;
} else if (len == 16) {
u16 temp;
wordoff = off >> 4;
temp = *((u16 *)v->d.data + wordoff);
temp = ntohs(temp);
*((u16 *)v->d.data + wordoff) = temp;
}
}
String timeToDate(int secs)
{
#ifdef STRFTIME
char buf[30];
struct tm *tm;
int len;
tm = localtime(&secs);
len = strftime(buf, 30, "%c", tm);
#else
char *buf;
buf = ctime((time_t *)&secs);
if (buf[strlen(buf) - 1] == '\n') buf[strlen(buf) - 1] = '\0';
#endif
return CreateString(buf);
}
void loadNGo(String s)
{
char *name;
name = vmMalloc(s->d.items + 1);
strncpy(name, (char*)s->d.data, s->d.items);
name[s->d.items] = '\0';
DoCheckpointFromFile(name);
vmFree(name);
}
/*
* attempt a New C Call. If an error occurs (probably caused by a
* bogus argument string) call debug with an appropriate string.
*
* If the ccall needs to block, return 1.
*/
int doNCCall(state)
State *state;
{
u32 moduleindex, funcindex;
int sl, i, fail = 0, block = 0;
s32 args[CCALL_MAXARGS];
f32 f;
/* the return value is in args[0], arguments in rest */
CCallDescriptor ccd;
char buf[80];
extern CCallDescriptor *ccalltable[];
#define pc state->pc
#define sp state->sp
#define fp state->fp
#define op state->op
#define sb state->sb /* Stack base */
#define cp state->cp /* Concrete type */
IFETCH1(moduleindex);
IFETCH1(funcindex);
if (!ccalltable[moduleindex]) {
sprintf(buf, "Call of unselected ccall module %d", moduleindex);
return debug(state, buf);
}
ccd = ccalltable[moduleindex][funcindex];
sl = strlen(ccd.ccArgTemplate);
if (sl <1 || sl > CCALL_MAXARGS) {
sprintf(buf, "Invalid number of args to ccall function %s", ccd.ccName);
return debug(state, buf);
}
/* for each of the argument description letters, pop
an argument and convert it to C style. */
for (i=sl-1; i>0; i--) {
switch (ccd.ccArgTemplate[i]) {
case 'x': /* exception flag */
args[i] = (s32) &fail;
break;
case 'X': /* blocking flag */
block = (int)state;
args[i] = (s32) █
assert(ccd.ccArgTemplate[0] == 'v');
break;
case 'b' :
case 'i' :
case 'p' :
case 'c' :
POP(s32, args[i]);
break;
case 'S':
case 's':
/* manufacture a C string from an Emerald one */
{
String string;
char* c_string;
POP(String, string);
c_string = vmMalloc(string->d.items + 1);
strncpy(c_string,(char*)string->d.data,string->d.items);
c_string[string->d.items] = '\0';
args[i] = (s32)c_string;
}
break;
case 'f':
POP(f32, f);
*(float *)&args[i] = f;
break;
default :
sprintf(buf, "Unknown argtype to ccall function %s", ccd.ccName);
return debug(state, buf);
}
}
/* Tracing! */
IFTRACE(ccalls, 1) {
printf("CCall: %s(", ccd.ccName);
for (i=1; i<sl; i++) {
if (i>1) printf(", ");
switch (ccd.ccArgTemplate[i]) {
case 'b' :
printf("%s", args[i] ? "true" : "false");
break;
case 'i' :
printf("%d", args[i]);
break;
case 'p' :
printf("0x%x", args[i]);
break;
case 'c':
printf( "0x%02X", args[i] );
case 's' :
case 'S':
printf("\"%s\"", (char *)args[i]);
break;
case 'f':
printf("\"%g\"", *(float*)&args[i]);
break;
case 'x':
printf( "(fail)" );
break;
case 'X':
printf( "(block)" );
break;
}
}
printf(")\n");
}
/*
* Call the function with the appropriate number of arguments of course,
* pushing extras doesn't hurt. Note though, that the call needs to pass
* CCALL_MAXARGS-1 arguments.
*/
args[0] = ccd.ccFunction(args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9], args[10]);
/* Tracing! */
IFTRACE(ccalls, 1) {
printf("CCall: %s() returned ", ccd.ccName);
switch (ccd.ccArgTemplate[0])
{
case 'v': break;
case 'b': printf("%s", args[0] ? "true" : "false"); break;
case 'c': printf( "0x%02X", args[0] ); break;
case 'i': printf("%d", args[0]); break;
case 'p': printf("0x%x", args[0]); break;
case 'f': printf("%g", *(float *)&args[0]); break;
case 's':
case 'S': printf("\"%s\"", (char *)args[0]); break;
}
printf("\n");
}
/* free the arg strings */
for (i=sl-1; i>0; i--) {
if (ccd.ccArgTemplate[i] == 'S')
vmFree( (void*)args[i] );
}
/*
* If we blocked, we don't have a result.
*/
if (block) return 1;
/* store the result */
switch (ccd.ccArgTemplate[0]) {
case 'v':
break;
case 'c':
case 'b':
case 'i':
case 'p':
PUSH( s32, ( fail ? JNIL : args[0] ) );
break;
case 's': case 'S':
if( ! fail ) {
if (args[0]) {
PUSH( String, CreateString((char*)args[0]));
if( ccd.ccArgTemplate[0] == 'S' ) vmFree( (void*)args[0] );
} else {
PUSH( s32, JNIL );
}
} else { PUSH( s32, JNIL ); }
break;
case 'f':
PUSH( f32, ( fail ? JNIL : *(float *)&args[0] ) );
break;
default:
sprintf(buf, "Unknown return type from CCALL %s", ccd.ccName);
return debug(state, buf);
}
if( fail ) {
sprintf( buf, "Exception raised in CCALL %s", ccd.ccName );
return debug( state, buf );
}
return 0;
#undef pc
#undef sp
#undef fp
#undef op
#undef sb
#undef cp
}
extern void move_pointers(int n, Object *p);
extern void move_variables(int n, u32 *p);
extern void move_variable(Object *data, ConcreteType *ct);
static void findLocals(Template t, u32 base, u32 sp,
void (*pointers_f)(int, Object *), void (*variables_f)(int, u32 *))
{
char *brands;
int *p = (int *)base, *limit = (int *)sp;
int count, nelements, size, totalsize, vector;
char c;
if (!ISNIL(t)) {
brands = (char *)t->d.data;
while(*brands != '\0') {
vector = 0;
switch (*brands++) {
case '%':
count = 0;
nelements = 1;
while (isdigit(c = *brands++)) {
count = count * 10 + c-'0';
}
if (!count) count = 1;
if (c == '*') {
vector = 1;
nelements = *p;
p++;
c = *brands++;
}
while (count--) {
switch(c) {
case 'm':
/* do nothing, no object pointers here */
size = 8;
break;
case 'X':
case 'x':
pointers_f(nelements, (Object *)&p[0]);
size = 4;
break;
case 'D':
case 'd':
size = 4;
break;
case 'F':
case 'f':
size = 4;
break;
case 'C':