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
|
/* table.c - table handler for bcc */
/* Copyright (C) 1992 Bruce Evans */
/* Can I now get rid of EXPRLEVEL? Yes, except expression symbols must
* usually be set to some level different from OFFKLUDGELEVEL.
*/
#include "bcc.h"
#include "align.h"
#include "gencode.h"
#include "os.h"
#include "parse.h"
#include "reg.h"
#include "sc.h"
#include "scan.h"
#include "sizes.h"
#include "type.h"
#undef EXTERN
#define EXTERN
#include "table.h"
#define GOLDEN 157 /* GOLDEN/HASHTABSIZE approx golden ratio */
#define HASHTABSIZE 256
#define MARKER ((unsigned) 0x18C396A5L) /* lint everywhere it is used */
#ifdef VERY_SMALL_MEMORY
#define MAXEXPR 125
#else
#define MAXEXPR 500
#endif
#define MAXLOCAL 100
#define NKEYWORDS 36
#ifdef NOFLOAT
#define NSCALTYPES 10
#else
#define NSCALTYPES 12
#endif
#define STACKSPACE 256 /* punt for normal recursions - switch extra */
struct keywordstruct
{
char *kwname;
sym_t kwcode;
};
#ifdef HOLDSTRINGS
struct string
{
struct string *snext;
char *sptr;
char *stop;
label_no slabel;
};
#endif
struct typedatastruct
{
char *tdname;
bool_t tdkeyscalar;
scalar_t tdscalar;
smalin_t tdsize;
struct typestruct **tdtypeptr;
};
PRIVATE struct symstruct exprsyms[MAXEXPR];
PRIVATE struct symstruct *hashtab[HASHTABSIZE];
/* depends on zero init */
#ifdef HOLDSTRINGS
PRIVATE struct string *laststring; /* last in list of strings */
/* depends on NULL init */
#endif
PUBLIC struct symstruct locsyms[MAXLOCAL];
PRIVATE struct symstruct constemplate =
{
CONSTANT, /* storage */
0, /* indcount */
0, /* flags */
EXPRLEVEL, /* level */
NULL, /* next */
NULL, /* prev */
NULL, /* type, init later to itype */
/* offset is a union, set every time constemplate is used */
/* name is a union, namea member is constant "\0" */
};
PRIVATE struct keywordstruct keywords[NKEYWORDS] =
{
{ "enum", ENUMDECL, },
{ "struct", STRUCTDECL, },
{ "union", UNIONDECL, },
{ "unsigned", UNSIGNDECL, },
{ "signed", SIGNDECL, },
{ "auto", AUTODECL, },
{ "extern", EXTERNDECL, },
{ "register", REGDECL, },
{ "static", STATICDECL, },
{ "typedef", TYPEDEFDECL, },
{ "asm", ASMSYM, },
{ "break", BREAKSYM, },
{ "case", CASESYM, },
{ "continue", CONTSYM, },
{ "default", DEFAULTSYM, },
{ "do", DOSYM, },
{ "else", ELSESYM, },
{ "for", FORSYM, },
{ "goto", GOTOSYM, },
{ "if", IFSYM, },
{ "return", RETURNSYM, },
{ "sizeof", SIZEOFSYM, },
{ "switch", SWITCHSYM, },
{ "while", WHILESYM, },
{ "#asm", ASMCNTL, },
{ "#define", DEFINECNTL, },
{ "#else", ELSECNTL, },
{ "#endasm", ENDASMCNTL, },
{ "#endif", ENDIFCNTL, },
{ "#if", IFCNTL, },
{ "#ifdef", IFDEFCNTL, },
{ "#include", INCLUDECNTL, },
{ "#ifndef", IFNDEFCNTL, },
{ "#line", LINECNTL, },
{ "#undef", UNDEFCNTL, },
{ "defined", DEFINEDSYM, }, /* should be deactivated except in #if's */
};
PRIVATE struct typedatastruct scaltypes[NSCALTYPES] =
{
{ "void", TRUE, 0, 0, &vtype, }, /* first - addkeyword uses vtype */
{ "char", TRUE, CHAR, 1, &ctype, },
{ "char", FALSE, UNSIGNED | CHAR, 1, &uctype, },
{ "char", FALSE, CHAR, 1, &sctype, },
{ "short", TRUE, SHORT, 2, &stype, },
{ "short", FALSE, UNSIGNED | SHORT, 2, &ustype, },
{ "int", TRUE, INT, 2, &itype, },
{ "int", FALSE, UNSIGNED | INT, 2, &uitype, },
{ "long", TRUE, LONG | DLONG, 4, <ype, },
{ "long", FALSE, UNSIGNED | LONG | DLONG, 4, &ultype, },
#ifndef NOFLOAT
{ "float", TRUE, FLOAT, 4, &fltype, },
{ "double", TRUE, DOUBLE, 8, &dtype, },
#endif
};
FORWARD struct symstruct *addkeyword P((char *name, sym_pt code));
FORWARD void heapcorrupterror P((void));
PUBLIC struct symstruct *addglb(name, type)
char *name;
struct typestruct *type;
{
struct symstruct **hashptr;
struct symstruct *oldsymptr = NULL; /* for -Wall */
register struct symstruct *symptr;
hashptr = gethashptr(name);
symptr = *hashptr;
while (symptr != NULL)
{
oldsymptr = symptr;
symptr = symptr->next;
}
symptr = qmalloc(sizeof (struct symstruct) + strlen(name));
#ifdef TS
++ts_n_global;
ts_size_global += sizeof (struct symstruct) + strlen(name);
#endif
addsym(name, type, symptr);
symptr->storage = GLOBAL;
symptr->level = GLBLEVEL;
if (*hashptr == NULL)
{
*hashptr = symptr;
symptr->prev = hashptr;
}
else
{
oldsymptr->next = symptr;
symptr->prev = &oldsymptr->next;
}
return symptr;
}
PRIVATE struct symstruct *addkeyword(name, code)
char *name;
sym_pt code;
{
struct symstruct *symptr;
(symptr = addglb(name, vtype))->flags = KEYWORD;
symptr->offset.offsym = code;
return symptr;
}
PUBLIC struct symstruct *addloc(name, type)
char *name;
struct typestruct *type;
{
struct symstruct **hashptr;
register struct symstruct *symptr;
hashptr = gethashptr(name);
symptr = *hashptr;
while (symptr != NULL)
symptr = symptr->next;
symptr = locptr;
locptr = (struct symstruct *) align(&symptr->name.namea[strlen(name) + 1]);
if (locptr >= &locsyms[MAXLOCAL])
limiterror("too many local symbols (101)");
addsym(name, type, symptr);
if (type->constructor == FUNCTION)
symptr->storage = GLOBAL;
else
symptr->storage = LOCAL;
symptr->level = level;
if (*hashptr != NULL)
{
symptr->next = *hashptr;
symptr->next->prev = &symptr->next;
}
*hashptr = symptr;
symptr->prev = hashptr;
return symptr;
}
PUBLIC struct symstruct *addlorg(name, type)
char *name;
struct typestruct *type;
{
if (level != GLBLEVEL)
return addloc(name, type);
return addglb(name, type);
}
PUBLIC void addsym(name, type, symptr)
char *name;
struct typestruct *type;
register struct symstruct *symptr;
{
if (type->constructor & (ARRAY | FUNCTION))
symptr->indcount = 0;
else
symptr->indcount = 1;
symptr->flags = 0;
symptr->next = NULL;
symptr->type = type;
symptr->offset.offi = 0;
strcpy(symptr->name.namea, name);
}
PUBLIC struct symstruct *constsym(longconst)
value_t longconst;
{
register struct symstruct *symptr;
symptr = exprsym(&constemplate);
symptr->offset.offv = longconst;
return symptr;
}
PUBLIC void delsym(symptr)
register struct symstruct *symptr;
{
if ((*(symptr->prev) = symptr->next) != NULL)
symptr->next->prev = symptr->prev;
}
/* dumpglbs() - define locations of globals and reserve space for them */
PUBLIC void dumpglbs()
{
int i;
register struct symstruct *symptr;
register struct typestruct *type;
#ifdef TS
extern char *brksize;
struct ts
{
char *what;
uvalue_t *where;
};
static struct ts ts[] =
{
"ts_n_newtypelist ", &ts_n_newtypelist,
"ts_s_newtypelist ", &ts_s_newtypelist,
"ts_n_filename_term ", &ts_n_filename_term,
"ts_s_filename_term ", &ts_s_filename_term,
"ts_n_filename ", &ts_n_filename,
"ts_s_filename ", &ts_s_filename,
"ts_s_filename_tot ", &ts_s_filename_tot,
"ts_n_pathname ", &ts_n_pathname,
"ts_s_pathname ", &ts_s_pathname,
"ts_s_pathname_tot ", &ts_s_pathname_tot,
"ts_n_inputbuf ", &ts_n_inputbuf,
"ts_s_inputbuf ", &ts_s_inputbuf,
"ts_s_inputbuf_tot ", &ts_s_inputbuf_tot,
"ts_n_includelist ", &ts_n_includelist,
"ts_s_includelist ", &ts_s_includelist,
"ts_s_outputbuf ", &ts_s_outputbuf,
"ts_n_macstring_ident ", &ts_n_macstring_ident,
"ts_n_macstring_ordinary ", &ts_n_macstring_ordinary,
"ts_n_macstring_param ", &ts_n_macstring_param,
"ts_n_macstring_quoted ", &ts_n_macstring_quoted,
"ts_n_macstring_term ", &ts_n_macstring_term,
"ts_s_macstring ", &ts_s_macstring,
"ts_n_defines ", &ts_n_defines,
"ts_s_defines ", &ts_s_defines,
"ts_n_macparam ", &ts_n_macparam,
"ts_s_macparam ", &ts_s_macparam,
"ts_s_macparam_tot ", &ts_s_macparam_tot,
"ts_n_macparam_string_ordinary ", &ts_n_macparam_string_ordinary,
"ts_n_macparam_string_quoted ", &ts_n_macparam_string_quoted,
"ts_n_macparam_string_term ", &ts_n_macparam_string_term,
"ts_s_macparam_string ", &ts_s_macparam_string,
"ts_s_macparam_string_tot ", &ts_s_macparam_string_tot,
"ts_s_macparam_string_alloced ", &ts_s_macparam_string_alloced,
"ts_s_macparam_string_alloced_tot ", &ts_s_macparam_string_alloced_tot,
"ts_s_fakeline ", &ts_s_fakeline,
"ts_s_fakeline_tot ", &ts_s_fakeline_tot,
"ts_n_string ", &ts_n_string,
"ts_n_case ", &ts_n_case,
"ts_n_case_realloc ", &ts_n_case_realloc,
"ts_s_case ", &ts_s_case,
"ts_s_case_tot ", &ts_s_case_tot,
"ts_n_structname ", &ts_n_structname,
"ts_s_structname ", &ts_s_structname,
"ts_n_type ", &ts_n_type,
"ts_s_type ", &ts_s_type,
"ts_n_global ", &ts_n_global,
"ts_size_global ", &ts_size_global,
"ts_n_holdstr ", &ts_n_holdstr,
"ts_size_holdstr ", &ts_size_holdstr,
"ts_n_growobj ", &ts_n_growobj,
"ts_size_growobj_wasted ", &ts_size_growobj_wasted,
"ts_n_growheap ", &ts_n_growheap,
"ts_s_growheap ", &ts_s_growheap,
};
struct ts *tp;
for (tp = &ts[0]; tp < &ts[sizeof ts / sizeof ts[0]]; ++tp)
{
comment();
outstr(tp->what);
outstr("");
outuvalue(*tp->where);
outnl();
}
comment();
outstr("brksize ");
outhex((uvalue_t) brksize);
outnl();
#endif
#ifndef DIRECTPAGE
bssseg();
#endif
for (i = 0; i < HASHTABSIZE; ++i)
for (symptr = hashtab[i]; symptr != NULL; symptr = symptr->next)
if (symptr->storage == GLOBAL && !(symptr->flags & EXTERNAL)
&& *symptr->name.namea >= 'A' && symptr->flags <= MAXDUMPFLAG
/* Don't access type unless flags <= MAXDUMPFLAG, because
* type is punned to a symbol pointer for the label chain
* and might be null.
*/
&& symptr->type->constructor != FUNCTION)
{
#ifdef DIRECTPAGE
if (symptr->flags & DIRECTPAGE)
dpseg();
else
dseg();
#endif
type = symptr->type;
if (symptr->flags & STATIC)
lcommon(symptr->name.namea);
else
common(symptr->name.namea);
outnhex(type->typesize);
}
if (printf_fp)
globl("__xfpcvt");
if (scanf_fp)
globl("__xfptvc");
}
/* dumplocs() - define offsets of current locals */
PUBLIC void dumplocs()
{
register struct symstruct *symptr;
int i;
for (i = 0; i < HASHTABSIZE; ++i)
for (symptr = hashtab[i]; symptr != NULL; symptr = symptr->next)
if (symptr->storage == LOCAL)
set(symptr->name.namea, symptr->offset.offi - sp);
}
#ifdef HOLDSTRINGS
/* dumpstrings() - dump held-up strings */
PUBLIC void dumpstrings()
{
struct string *stringp;
dseg();
for (stringp = laststring; stringp != NULL; stringp = stringp->snext)
{
outnlabel(stringp->slabel);
defstr(stringp->sptr, stringp->stop, TRUE);
dataoffset += stringp->stop - stringp->sptr + 1;
}
}
#endif
PUBLIC struct symstruct *exprsym(symptr)
struct symstruct *symptr;
{
register struct symstruct *newsymptr;
newsymptr = exprptr++;
if (exprptr >= &exprsyms[MAXEXPR])
#if MAXEXPR == 500
limiterror("expression too complex (501 symbols)");
#else
limiterror("expression too complex (MAXEXPR)");
#endif
*newsymptr = *symptr;
newsymptr->level = EXPRLEVEL;
newsymptr->name.namep = symptr->name.namea;
return newsymptr;
}
PUBLIC struct symstruct *findlorg(name)
char *name;
{
struct symstruct *symptr;
symptr = *gethashptr(name);
while (symptr != NULL && (strcmp(symptr->name.namea, name) != 0 ||
symptr->flags == STRUCTNAME))
symptr = symptr->next;
return symptr;
}
PUBLIC struct symstruct *findstruct(name)
char *name;
{
struct symstruct *symptr;
symptr = *gethashptr(name);
while (symptr != NULL && (symptr->flags != STRUCTNAME ||
strcmp(symptr->name.namea, name) != 0))
symptr = symptr->next;
return symptr;
}
/* convert name to a hash table ptr */
PUBLIC struct symstruct **gethashptr(sname)
char *sname;
{
register int hashval;
register char *rsname;
hashval = 0;
rsname = sname;
#if 1
while (*rsname)
hashval = hashval * 2 + *rsname++;
#else
hashval = rsname[0];
if (rsname[1] != 0)
hashval = (rsname[2] << 2) ^ rsname[1] ^ (rsname[0] << 1);
else
hashval = rsname[0];
#endif
return hashtab + ((hashval * GOLDEN) & (HASHTABSIZE - 1));
}
PRIVATE void heapcorrupterror()
{
outofmemoryerror(" (heap corrupt - stack overflow?)");
}
#ifdef HOLDSTRINGS
/* hold string for dumping at end, to avoid mixing it with other data */
PUBLIC label_no holdstr(sptr, stop)
char *sptr;
char *stop;
{
register struct string *stringp;
stringp = qmalloc(sizeof *stringp);
#ifdef TS
++ts_n_holdstr;
ts_size_holdstr += sizeof *stringp;
#endif
stringp->snext = laststring;
stringp->sptr = sptr;
stringp->stop = stop;
laststring = stringp;
return stringp->slabel = getlabel();
}
#endif /* HOLDSTRINGS */
PUBLIC void newlevel()
{
if (*(unsigned *) chartop != MARKER)
heapcorrupterror();
if (level >= MAXLEVEL)
limiterror("compound statements nested too deeply (126 levels)");
else
++level;
}
PUBLIC void oldlevel()
{
register struct symstruct *symptr;
if (*(unsigned *) chartop != MARKER)
heapcorrupterror();
if (level == 0)
bugerror("not in a compound statement");
else
--level;
for (symptr = &locsyms[0]; symptr < locptr;
symptr = (struct symstruct *)
align(&symptr->name.namea[strlen(symptr->name.namea) + 1]))
if (symptr->level > level)
delsym(symptr);
}
PUBLIC void ourfree(ptr)
void *ptr;
{
free(ptr);
}
PUBLIC void *ourmalloc(nbytes)
unsigned nbytes;
{
void *ptr;
if ((ptr = malloc(nbytes)) == NULL)
outofmemoryerror("");
return ptr;
}
PUBLIC void outofmemoryerror(message)
char *message;
{
error2error("compiler out of memory", message);
#if defined(DEBUG) && 0
{
unsigned size;
char *ptr;
for (size = 0x1000; size != 0; --size)
if ((ptr = malloc(size)) != NULL)
{
outstr("found free memory at ");
outuvalue((uvalue_t) ptr);
outstr(" size ");
outuvalue((uvalue_t) size);
outnl();
}
}
#endif
finishup();
}
PUBLIC void *growobject(object, extra)
void *object;
unsigned extra;
{
/* size_t */ unsigned oblength;
/* It would be nice to realloc the previous memory if it can be left in
* the same place. Since a realloc that moves the memory can almost be
* relied on to mess up pointers before *obptr, and since malloc(extra)
* would be unlikely to produce memory adjoining chartop, it is not
* clear how to do this.
*/
#ifdef TS
ts_size_growobj_wasted += chartop - (char *) object;
#endif
oblength = (/* size_t */ unsigned) (charptr - (char *) object);
growheap(oblength + extra);
#ifdef TS
++ts_n_growobj;
#endif
memcpy(charptr, object, oblength);
object = charptr;
charptr += oblength;
return object;
}
#define ALLOC_UNIT ((unsigned) 0x400)
#ifdef S_ALIGNMENT
#define ALLOC_OVERHEAD (S_ALIGNMENT - 1 + sizeof (unsigned))
#else
#define ALLOC_OVERHEAD (sizeof (unsigned))
#endif
PUBLIC void growheap(size)
unsigned size;
{
register char *newptr;
if ((newptr = malloc(size += ALLOC_UNIT + ALLOC_OVERHEAD)) == NULL
&& (newptr = malloc(size -= ALLOC_UNIT)) == NULL)
outofmemoryerror("");
#ifdef TS
++ts_n_growheap;
ts_s_growheap += size;
#endif
charptr = newptr;
newptr = (char *) align(newptr + size - ALLOC_OVERHEAD);
chartop = newptr;
char1top = newptr - 1;
char3top = newptr - 3;
*(unsigned *) newptr = MARKER;
}
PUBLIC void *qmalloc(size)
unsigned size;
{
register char *ptr;
if ((charptr = (char *) align(charptr)) + size > chartop)
growheap(size);
ptr = charptr;
charptr += size;
return ptr;
}
PUBLIC void swapsym(sym1, sym2)
struct symstruct *sym1;
struct symstruct *sym2;
{
struct symstruct swaptemp;
swaptemp = *sym1;
*sym1 = *sym2;
*sym2 = swaptemp;
}
PUBLIC void syminit()
{
struct keywordstruct *kwptr;
struct typedatastruct *tdptr;
struct typestruct *type;
exprptr = exprsyms;
locptr = locsyms;
for (tdptr = scaltypes; tdptr < scaltypes + NSCALTYPES; ++tdptr)
{
(*tdptr->tdtypeptr = type = newtype())->scalar = tdptr->tdscalar;
type->alignmask = ~((type->typesize = tdptr->tdsize) - 1) |
alignmask;
type->tname = tdptr->tdname;
if (tdptr->tdkeyscalar)
addkeyword(tdptr->tdname, TYPEDECL)->type = type;
}
for (kwptr = keywords; kwptr < keywords + NKEYWORDS; ++kwptr)
addkeyword(kwptr->kwname, kwptr->kwcode);
constemplate.type = itype;
}
|