/[projet1]/public/pc/tools/osdk/main/compiler/includes/c.h
Defence Force logotype

Contents of /public/pc/tools/osdk/main/compiler/includes/c.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1133 - (show annotations)
Sun May 11 10:05:08 2014 UTC (5 years, 10 months ago) by Jylam
File MIME type: text/plain
File size: 17567 byte(s)
Defined POSIX in the Makefile and modified c.h accordingly

1 /* C compiler: definitions */
2 #ifdef POSIX
3 #include <unistd.h>
4 #endif
5
6 /* default sizes */
7 #define MAXLINE 512 /* maximum output line length */
8 #define MAXTOKEN 32 /* maximum token length */
9 #define BUFSIZE 4096 /* input buffer size */
10 #define HASHSIZE 128 /* default hash table size */
11 #define MEMINCR 10 /* blocks (1kb) allocated per arena */
12
13
14 #include "ops.h"
15
16 #include <stdio.h>
17 #include <stdarg.h>
18 #include <string.h>
19
20 #define va_init va_start
21
22 typedef enum tokencode {
23 #define xx(a,b,c,d,e,f,g) a=b,
24 #define yy(a,b,c,d,e,f,g)
25 #include "token.h"
26 NTOKENS
27 } Typeop; /* type operators are a subset of tokens */
28
29 #define dclproto(func,args) func args
30 typedef void *Generic;
31
32 typedef struct list { /* lists: */
33 Generic x; /* element */
34 struct list *link; /* next node */
35 } *List;
36 typedef struct symbol *Symbol; /* symbol table entries */
37 typedef struct table *Table; /* symbol tables */
38 typedef struct tynode *Type; /* type nodes */
39 typedef struct node *Node; /* dag nodes */
40 typedef struct tree *Tree; /* tree nodes */
41 typedef struct field *Field; /* struct/union fields */
42 typedef struct swtch *Swtch; /* switch data */
43
44 typedef union value
45 { /* constant values: */
46 char sc; /* signed */
47 short ss; /* signed */
48 int i; /* signed */
49 unsigned char uc;
50 unsigned short us;
51 unsigned int u;
52 float f;
53 double d;
54 char *p; /* pointer to anything */
55 } Value;
56
57 typedef struct coord { /* source coordinates: */
58 char *file; /* file name */
59 unsigned short x, y; /* x,y position in file */
60 } Coordinate;
61
62 void address(Symbol, Symbol, int);
63 void asmcode(char *, Symbol []);
64 void defaddress(Symbol);
65 void defconst(int, Value);
66 void defstring(int, char *);
67 void defsymbol(Symbol);
68 void emit(Node);
69 void export(Symbol);
70 void function(Symbol, Symbol [], Symbol [], int);
71 Node gen(Node);
72 void global(Symbol);
73 void import(Symbol);
74 void local(Symbol);
75 void progbeg(int, char **);
76 void progend(void);
77 void segment(int);
78 void space(int);
79 /* symbol table */
80 void stabblock(int, int, Symbol*);
81 void stabend(Coordinate *, Symbol, Coordinate **, Symbol *, Symbol *);
82 void stabfend(Symbol, int);
83 void stabinit(char *, int, char *[]);
84 void stabline(Coordinate *);
85 void stabsym(Symbol);
86 void stabtype(Symbol);
87
88 #include "config.h"
89 #ifndef MAXKIDS
90 #define MAXKIDS 2
91 #endif
92 #ifndef MAXSYMS
93 #define MAXSYMS 3
94 #endif
95 #ifndef blockbeg
96 void blockbeg(Env *);
97 #endif
98 #ifndef blockend
99 void blockend(Env *);
100 #endif
101 #ifndef JUMP_ON_RETURN
102 #define JUMP_ON_RETURN 0
103 #endif
104
105 /* limits */
106 #ifdef __LCC__
107 #include <limits.h>
108 #include <float.h>
109 #else
110 /*
111 * The magnitudes of the values below are greater than or equal to the minimum
112 * permitted by the standard (see Appendix D) and are typical for byte-addressed
113 * machines with 32-bit integers. These values are suitable for bootstrapping.
114 */
115 #define CHAR_BIT 8
116 #define MB_LEN_MAX 1
117
118 #define UCHAR_MAX 0xff
119 #define USHRT_MAX 0xffff
120 #define UINT_MAX 0xffffffff
121 #define ULONG_MAX 0xffffffffL
122
123 #define CHAR_MAX SCHAR_MAX
124 #define SCHAR_MAX 0x7f
125 #define SHRT_MAX 0x7fff
126 #define INT_MAX 0x7fffffff
127 #define LONG_MAX 0x7fffffffL
128
129 #define CHAR_MIN SCHAR_MIN
130 #define SCHAR_MIN (-SCHAR_MAX-1)
131 #define SHRT_MIN (-SHRT_MAX-1)
132 #define INT_MIN (-INT_MAX-1)
133 #define LONG_MIN (-LONG_MAX-1)
134
135 #define FLT_MAX 1e37
136 #define DBL_MAX 1e37
137 #endif
138
139 /* data structures */
140
141 struct symbol { /* symbol structures: */
142 Xsymbol x; /* back-end's type extension */
143 char *name; /* name */
144 unsigned short scope; /* scope level */
145 unsigned char sclass; /* storage class */
146 unsigned defined:1; /* 1 if defined */
147 unsigned temporary:1; /* 1 if a temporary */
148 unsigned generated:1; /* 1 if a generated identifier */
149 unsigned computed:1; /* 1 if an address computation identifier */
150 unsigned addressed:1; /* 1 if its address is taken */
151 unsigned initialized:1; /* 1 if local is initialized */
152 unsigned structarg:1; /* 1 if parameter is a struct */
153 int ref; /* weighted # of references */
154 Type type; /* data type */
155 Coordinate src; /* definition coordinate */
156 Coordinate **uses; /* array of Coordinate *'s for uses (omit) */
157 Symbol up; /* next symbol in this or outer scope */
158 union {
159 struct { /* labels: */
160 int label; /* label number */
161 Symbol equatedto; /* equivalent label */
162 } l;
163 struct { /* struct/union types: */
164 unsigned cfields:1; /* 1 if >= 1 const fields */
165 unsigned vfields:1; /* 1 if >= 1 volatile fields */
166 Table ftab; /* if xref != 0, table of field names */
167 Field flist; /* field list */
168 } s;
169 int value; /* enumeration identifiers: value */
170 Symbol *idlist; /* enumeration types: identifiers */
171 struct { /* constants: */
172 Value v; /* value */
173 Symbol loc; /* out-of-line location */
174 } c;
175 struct { /* functions: */
176 Coordinate pt[3];/* source code coordinates */
177 int label; /* exit label */
178 int ncalls; /* # calls in this function */
179 Symbol *callee; /* parameter symbols */
180 } f;
181 int seg; /* globals, statics: definition segment */
182 } u;
183 #ifdef Ysymbol /* (omit) */
184 Ysymbol y; /* (omit) */
185 #endif /* (omit) */
186 };
187
188
189 typedef struct {
190 unsigned printed:1;
191 unsigned marked:1;
192 unsigned short typeno;
193 } Xtype;
194
195 enum { CODE=1, BSS, DATA, LIT, SYM }; /* logical segments */
196 enum { CONSTANTS=1, LABELS, GLOBAL, PARAM, LOCAL };
197
198
199 /* misc. macros */
200 #define roundup(x,n) (((x)+((n)-1))&(~((n)-1)))
201 #define utod(x) (2.*(int)((unsigned)(x)>>1)+(int)((x)&1))
202 #ifdef NDEBUG
203 #define assert(c)
204 #else
205 #define assert(c) ((c) || fatal(__FILE__,"assertion failure at line %d\n",\
206 __LINE__))
207 #endif
208
209 #ifndef strtod
210 dclproto(extern double strtod,(char *, char **));
211 #endif
212
213
214 /* C library */
215 #ifndef _POSIX_VERSION
216 int atoi(char *);
217 int close(int);
218 int creat(char *, int);
219 void exit(int);
220 Generic malloc(unsigned);
221 int read(int, char *, int);
222 int open(char *, int);
223 long strtol(char *, char **, int);
224 int sprintf(char *, const char *, ...);
225 char *strchr(const char *, int);
226 int strcmp(const char *, const char *);
227 unsigned strlen(const char *);
228 //char *strncmp(const char *, const char *, unsigned);
229 //char *strncpy(char *, const char *, unsigned);
230 int write(int, char *, int);
231 #else
232 #define _strdup strdup
233 #endif
234
235
236 struct node { /* dag nodes: */
237 Opcode op; /* operator */
238 short count; /* reference count */
239 Symbol syms[MAXSYMS]; /* symbols */
240 Node kids[MAXKIDS]; /* operands */
241 Node link; /* next dag in the forest */
242 Xnode x; /* back-end's type extension */
243 };
244 #define islabel(p) ((p) && (p)->op == LABEL+V && (p)->syms[0])
245 struct tree { /* tree nodes: */
246 Opcode op; /* operator */
247 Type type; /* type of result */
248 Tree kids[2]; /* operands */
249 Node node; /* associated dag node */
250 union {
251 Symbol sym; /* associated symbol */
252 Value v; /* associated value */
253 Field field; /* associated struct/union bit field */
254 } u;
255 };
256 typedef struct arena *Arena;
257 struct arena { /* storage allocation arena: */
258 int m; /* size of current allocation request */
259 char *avail; /* next available location */
260 char *limit; /* address 1 past end of arena */
261 Arena first; /* pointer to first arena */
262 Arena next; /* link to next arena */
263 };
264 #define yyalloc(n,ap) (ap->m = roundup(n,sizeof(double)), \
265 ap->avail + ap->m >= ap->limit ? allocate(ap->m, &ap) : \
266 (ap->avail += ap->m, ap->avail - ap->m))
267 #define alloc(n) yyalloc(n, permanent)
268 #define talloc(n) yyalloc(n, transient)
269 #define BZERO(p,t) \
270 { unsigned *q1 = (unsigned *)(p), *q2 = q1 + ((sizeof (t)/sizeof (unsigned))&~(8-1)); \
271 for ( ; q1 < q2; q1 += 8) \
272 q1[0] = q1[1] = q1[2] = q1[3] = q1[4] = q1[5] = q1[6] = q1[7] = 0; \
273 sizeof (t)/sizeof (unsigned)%8 >= 1 ? q1[0] = 0 : 0; \
274 sizeof (t)/sizeof (unsigned)%8 >= 2 ? q1[1] = 0 : 0; \
275 sizeof (t)/sizeof (unsigned)%8 >= 3 ? q1[2] = 0 : 0; \
276 sizeof (t)/sizeof (unsigned)%8 >= 4 ? q1[3] = 0 : 0; \
277 sizeof (t)/sizeof (unsigned)%8 >= 5 ? q1[4] = 0 : 0; \
278 sizeof (t)/sizeof (unsigned)%8 >= 6 ? q1[5] = 0 : 0; \
279 sizeof (t)/sizeof (unsigned)%8 >= 7 ? q1[6] = 0 : 0; \
280 sizeof (t)%sizeof (unsigned) >= 1 ? ((char *)(q1 + sizeof (t)/sizeof (unsigned)%8))[0] = 0 : 0; \
281 sizeof (t)%sizeof (unsigned) >= 2 ? ((char *)(q1 + sizeof (t)/sizeof (unsigned)%8))[1] = 0 : 0; \
282 sizeof (t)%sizeof (unsigned) >= 3 ? ((char *)(q1 + sizeof (t)/sizeof (unsigned)%8))[2] = 0 : 0; \
283 }
284 typedef struct code *Code;
285 struct code { /* code list entries: */
286
287 enum {
288 Blockbeg, Blockend, Local, Address, Defpoint,
289 Label, Start, Asm, Gen, Jump, Switch } kind;
290
291 Code prev; /* previous code node */
292 Code next; /* next code node */
293 union {
294 struct { /* Asm: assembly language */
295 char *code; /* assembly code */
296 Symbol *argv; /* %name arguments */
297 } acode;
298 struct { /* Blockbeg: */
299 Code prev; /* previous Blockbeg */
300 short bnumber; /* block number */
301 short level; /* block level */
302 Symbol *locals; /* locals */
303 Table identifiers, types;/* symbol tables; used for -g */
304 Env x; /* value filled in by blockbeg() */
305 } block;
306 Symbol var; /* Local: local variable */
307 struct { /* Address: */
308 Symbol sym; /* created symbol */
309 Symbol base; /* local or parameter */
310 int offset; /* offset from sym */
311 } addr;
312 struct { /* Defpoint: execution point */
313 Coordinate src; /* source location */
314 int point; /* execution point number */
315 } point;
316 Node node; /* Label, Gen, Jump: a dag node */
317 struct swselect { /* Switch: swselect data */
318 Symbol sym; /* temporary holding value */
319 Symbol table; /* branch table */
320 Symbol deflab; /* default label */
321 int size; /* size of value & label arrays */
322 int *values; /* value, label pairs */
323 Symbol *labels;
324 } swtch;
325 } u;
326 };
327 struct tynode { /* type nodes: */
328 Typeop op; /* operator */
329 short align; /* alignment in storage units */
330 int size; /* size in storage units */
331 Type type; /* operand */
332 union {
333 Symbol sym; /* associated symbol */
334 Type *proto; /* function prototype */
335 Generic ptr;
336 } u;
337 Xtype x; /* symbol table information */
338 #ifdef Ytype
339 Ytype y;
340 #endif
341 };
342 struct field { /* struct/union fields: */
343 char *name; /* field name */
344 Type type; /* data type */
345 int offset; /* field offset */
346 short from, to; /* bit fields: bits from..to */
347 Field link; /* next field in this type */
348 };
349 #define fieldsize(p) ((p)->to - (p)->from)
350 #ifdef LITTLE_ENDIAN
351 #define fieldright(p) (p)->from
352 #else
353 #define fieldright(p) (8*(p)->type->size - (p)->to)
354 #endif
355 #define fieldmask(p) (~(~(unsigned)0<<fieldsize(p)))
356 #define fieldleft(p) (8*(p)->type->size - fieldsize(p) - fieldright(p))
357 /*
358 * type-checking macros.
359 * the operator codes are defined in token.h
360 * to permit the range tests below; don't change them.
361 */
362 #define isqual(t) ((t)->op >= CONST)
363 #define isvolatile(t) ((t)->op == VOLATILE || (t)->op == CONST+VOLATILE)
364 #define isconst(t) ((t)->op == CONST || (t)->op == CONST+VOLATILE)
365 #define unqual(t) (isqual(t) ? (t)->type : t)
366 #define isarray(t) (unqual(t)->op == ARRAY)
367 #define isstruct(t) (unqual(t)->op == STRUCT || unqual(t)->op == UNION)
368 #define isunion(t) (unqual(t)->op == UNION)
369 #define isfunc(t) (unqual(t)->op == FUNCTION)
370 #define isptr(t) (unqual(t)->op == POINTER)
371 #define ischar(t) (unqual(t)->op == CHAR)
372 #define isint(t) (unqual(t)->op >= CHAR && unqual(t)->op <= UNSIGNED)
373 #define isfloat(t) (unqual(t)->op <= DOUBLE)
374 #define isarith(t) (unqual(t)->op <= UNSIGNED)
375 #define isunsigned(t) (unqual(t)->op == UNSIGNED)
376 #define isdouble(t) (unqual(t)->op == DOUBLE)
377 #define isscalar(t) (unqual(t)->op <= POINTER || unqual(t)->op == ENUM)
378 #define isenum(t) (unqual(t)->op == ENUM)
379 #define widen(t) (isint(t) || isenum(t) ? INT : ttob(t))
380
381
382 void addlocal(Symbol);
383 Code code(int);
384 void emitcode(void);
385 void gencode(Symbol [], Symbol []);
386 Node listnodes(Tree, int, int);
387 Node jump(int);
388 Node newnode(int, Node, Node, Symbol);
389 Node node(int, Node, Node, Symbol);
390 void printdag(Node, int);
391 void walk(Tree, int, int);
392 extern struct code codehead;
393 extern Code codelist;
394 extern int nodecount;
395 void compound(int, Swtch, int);
396 void finalize(void);
397 void program(void);
398 Type typename(void);
399 extern Symbol cfunc;
400 extern char *fname;
401 extern Symbol retv;
402 int genconst(Tree, int);
403 int hascall(Tree);
404 int nodeid(Tree);
405 char *opname(int);
406 int *printed(int);
407 void printtree(Tree, int);
408 Tree retype(Tree, Type);
409 Tree root(Tree);
410 Tree texpr(Tree (*)(int), int);
411 void tfree(void);
412 Tree tree(int, Type, Tree, Tree);
413 extern int ntree;
414 Tree addrof(Tree);
415 Tree asgn(Symbol, Tree);
416 Type assign(Type, Tree);
417 Tree cast(Tree, Type);
418 Tree cond(Tree);
419 Tree conditional(int);
420 Tree constexpr(int);
421 Tree expr0(int);
422 Tree expr(int);
423 Tree expr1(int);
424 Tree field(Tree, char *);
425 char *funcname(Tree);
426 Tree idnode(Symbol);
427 Tree incr(int, Tree, Tree);
428 int intexpr(int, int);
429 Tree lvalue(Tree);
430 Tree pointer(Tree);
431 Type promote(Type);
432 Tree right(Tree, Tree);
433 Tree rvalue(Tree);
434 Tree cvtconst(Tree);
435 void defglobal(Symbol, int);
436 void defpointer(Symbol);
437 void doconst(Symbol, Generic);
438 void initglobal(Symbol, int);
439 Type initializer(Type, int);
440 Tree structexp(Type, Symbol);
441 void swtoseg(int);
442 void inputInit(int);
443 void inputstring(char *);
444 void fillbuf(void);
445 void nextline(void);
446 extern unsigned char *cp;
447 extern char *file;
448 extern char *firstfile;
449 extern unsigned char *limit;
450 extern char *line;
451 extern int lineno;
452 int getchr(void);
453 int gettok(void);
454 extern char kind[];
455 extern Coordinate src;
456 extern enum tokencode t;
457 extern char *token;
458 extern Symbol tsym;
459 int main(int, char **);
460 Symbol mkstr(char *);
461 Symbol mksymbol(int, char *,Type);
462 extern int Aflag;
463 extern int Pflag;
464 extern Symbol YYnull;
465 extern int glevel;
466 extern int xref;
467 void bbinit(char *);
468 extern int ncalled;
469 extern int npoints;
470 void traceinit(char *);
471
472 typedef struct
473 {
474 List entry;
475 List exit;
476 List returns;
477 List points;
478 List calls;
479 List end;
480 } Events;
481
482 extern Events events;
483 typedef void (*Apply)(Generic, Generic, Generic);
484 void attach(Apply, Generic, List *);
485 void apply(List, Generic, Generic);
486 void fprint(int, char *, ...);
487 void print(char *, ...);
488
489 char *stringf(char *, ...);
490
491
492 void outflush(void);
493 void outs(char *);
494 void vfprint(int, char *, va_list);
495 void vprint(char *, va_list);
496 extern char *bp;
497 void error(char *, ...);
498 int fatal(char *, char *, int);
499 void warning(char *, ...);
500 int expect(int);
501 void skipto(int, char *);
502 void test(int, char *);
503 extern int errcnt;
504 extern int errlimit;
505 extern int wflag;
506 int process(char *);
507 int findfunc(char *, char *);
508 int findcount(char *, int, int);
509 Tree asgnnode(int, Tree, Tree);
510 Tree bitnode(int, Tree, Tree);
511 Tree callnode(Tree, Type, Tree);
512 Tree condnode(Tree, Tree, Tree);
513 Tree constnode(unsigned int, Type);
514 Tree eqnode(int, Tree, Tree);
515 Tree shnode(int, Tree, Tree);
516 void typeerror(int, Tree, Tree);
517 Tree (*opnode[])(int, Tree, Tree);
518 Tree simplify(int, Type, Tree, Tree);
519 int ispow2(unsigned u);
520 char *vtoa(Type, Value);
521 extern int needconst;
522 void definelab(int);
523 Code definept(Coordinate *);
524 void equatelab(Symbol, Symbol);
525 void flushequ(void);
526 void retcode(Tree, int);
527 void statement(int, Swtch, int);
528 extern float density;
529 extern int refinc;
530 char *allocate(int, Arena *);
531 void deallocate(Arena *);
532 extern Arena permanent;
533 extern Arena transient;
534 List append(Generic, List);
535 int length(List);
536 Generic *ltoa(List, Generic []);
537 char *string(char *);
538 char *stringd(int);
539 char *stringn(char *, int);
540 Symbol constant(Type, Value);
541 void enterscope(void);
542 void exitscope(void);
543 void fielduses(Symbol, Generic);
544 Symbol findlabel(int);
545 Symbol findtype(Type);
546 void foreach(Table, int, void (*)(Symbol, Generic), Generic);
547 Symbol genident(int, Type, int);
548 int genlabel(int);
549 Symbol install(char *, Table *, int);
550 Symbol intconst(int);
551 void locus(Table, Coordinate *);
552 Symbol lookup(char *, Table);
553 Symbol newconst(Value v,int tc);
554 Symbol newtemp(int, int);
555 void rmtemps(int, int);
556 void release(Symbol);
557 void setuses(Table);
558 Table table(Table, int);
559 Symbol temporary(int, Type);
560 void use(Symbol, Coordinate);
561 extern int bnumber;
562 extern Table constants;
563 extern Table externals;
564 extern Table globals;
565 extern Table identifiers;
566 extern Table labels[2];
567 extern Table types;
568 extern int level;
569 extern List symbols;
570 void typeInit(void);
571 Type array(Type, int, int);
572 Type atop(Type);
573 void checkfields(Type);
574 Type composite(Type, Type);
575 Symbol deftype(char *, Type, Coordinate *);
576 Type deref(Type);
577 int eqtype(Type, Type, int);
578 Field extends(Type, Type);
579 Field fieldlist(Type);
580 Field fieldref(char *, Type);
581 Type freturn(Type);
582 Type func(Type, Type *);
583 int hasproto(Type);
584 Field newfield(char *, Type, Type);
585 Type newstruct(int, char *);
586 void outtype(Type);
587 void printdecl(Symbol, Type);
588 void printproto(Symbol, Symbol *);
589 void printtype(Type, int);
590 Type ptr(Type);
591 Type qual(int, Type);
592 void rmtypes(void);
593 int ttob(Type);
594 char *typestring(Type, char *);
595 int variadic(Type);
596 extern Type chartype;
597 extern Type doubletype;
598 extern Type floattype;
599 extern Type inttype;
600 extern Type longdouble;
601 extern Type longtype;
602 extern Type shorttype;
603 extern Type signedchar;
604 extern Type unsignedchar;
605 extern Type unsignedlong;
606 extern Type unsignedshort;
607 extern Type unsignedtype;
608 extern Type voidptype;
609 extern Type voidtype;

  ViewVC Help
Powered by ViewVC 1.1.26