scc

simple C compiler
git clone git://git.2f30.org/scc
Log | Files | Refs | README | LICENSE

cc1.h (9011B)


      1 #define INPUTSIZ LINESIZ
      2 
      3 #define GLOBALCTX 0
      4 #define PARAMCTX  1
      5 
      6 #define NR_USWITCHES 20
      7 
      8 /*
      9  * Definition of enumerations
     10  */
     11 enum {
     12 	NOALLOC,
     13 	ALLOC
     14 };
     15 
     16 enum typeprops {
     17 	TDEFINED = 1 << 0,    /* type defined */
     18 	TSIGNED  = 1 << 1,    /* signedness of the type */
     19 	TINTEGER = 1 << 2,    /* the type is INT of enum */
     20 	TARITH   = 1 << 3,    /* the type is INT, ENUM or FLOAT */
     21 	TAGGREG  = 1 << 4,    /* the type is struct or union */
     22 	TK_R     = 1 << 5,    /* this is a K&R-function */
     23 	TELLIPSIS= 1 << 6,    /* this function has an ellipsis par */
     24 	TFUNDEF  = 1 << 7,    /* function definition */
     25 };
     26 
     27 enum inputtype {
     28 	IMACRO = 1 << 0,      /* macro expansion type */
     29 	IFILE  = 1 << 1,      /* input file type */
     30 	ISTDIN = 1 << 2,      /* stdin type */
     31 	IEOF   = 1 << 3,      /* EOF mark */
     32 	ITYPE  = IMACRO | IFILE | ISTDIN,
     33 };
     34 
     35 /* data type letters */
     36 enum ns {
     37 	L_INT8      = 'C',
     38 	L_INT16     = 'I',
     39 	L_INT32     = 'W',
     40 	L_INT64     = 'Q',
     41 	L_UINT8     = 'K',
     42 	L_UINT16    = 'N',
     43 	L_UINT32    = 'Z',
     44 	L_UINT64    = 'O',
     45 	L_BOOL      = 'B',
     46 
     47 	L_FLOAT     = 'J',
     48 	L_DOUBLE    = 'D',
     49 	L_LDOUBLE   = 'H',
     50 
     51 	L_ELLIPSIS  = 'E',
     52 	L_VOID      = '0',
     53 	L_POINTER   = 'P',
     54 	L_FUNCTION  = 'F',
     55 	L_ARRAY     = 'V',
     56 	L_UNION     = 'U',
     57 	L_STRUCT    = 'S',
     58 	L_VA_ARG    = '1',
     59 };
     60 
     61 /* recovery points */
     62 enum {
     63 	END_DECL,
     64 	END_LDECL,
     65 	END_COMP,
     66 	END_COND
     67 };
     68 
     69 /* type constructors */
     70 enum typeop {
     71 	FTN = 1,
     72 	PTR,
     73 	ARY,
     74 	KRFTN
     75 };
     76 
     77 /* namespaces */
     78 enum namespaces {
     79 	NS_DUMMY,
     80 	NS_IDEN,
     81 	NS_TAG,
     82 	NS_LABEL,
     83 	NS_CPP,
     84 	NS_KEYWORD,
     85 	NS_CPPCLAUSES,
     86 	NS_STRUCTS
     87 };
     88 
     89 /* symbol flags */
     90 enum {
     91 	SAUTO     = 1 << 0,
     92 	SREGISTER = 1 << 1,
     93 	SDECLARED = 1 << 2,
     94 	SFIELD    = 1 << 3,
     95 	SEXTERN   = 1 << 4,
     96 	SUSED     = 1 << 5,
     97 	SCONSTANT = 1 << 6,
     98 	SGLOBAL   = 1 << 7,
     99 	SPRIVATE  = 1 << 8,
    100 	SLOCAL    = 1 << 9,
    101 	SEMITTED  = 1 << 10,
    102 	SDEFINED  = 1 << 11,
    103 	SSTRING   = 1 << 12,
    104 	STYPEDEF  = 1 << 13,
    105 	SINITLST  = 1 << 14,
    106 	SHASINIT  = 1 << 15
    107 };
    108 
    109 /* node flags */
    110 enum {
    111 	NLVAL   = 1 << 0,
    112 	NCONST  = 1 << 1,
    113 	NEFFECT = 1 << 2
    114 };
    115 
    116 /* lexer mode, compiler or preprocessor directive */
    117 enum {
    118 	CCMODE,
    119 	CPPMODE
    120 };
    121 
    122 /* input tokens */
    123 enum tokens {
    124 	CONST      = 1 << 0,      /* type qualifier tokens are used as flags */
    125 	RESTRICT   = 1 << 1,
    126 	VOLATILE   = 1 << 2,
    127 	INLINE     = 1 << 3,
    128 	TQUALIFIER = 1 << 7,      /* this value is picked outside of ASCII range */
    129 	TYPE,
    130 	IDEN,
    131 	SCLASS,
    132 	CONSTANT,
    133 	STRING,
    134 	SIZEOF,
    135 	INDIR,
    136 	INC,
    137 	DEC,
    138 	SHL,
    139 	SHR,
    140 	LE,
    141 	GE,
    142 	EQ,
    143 	NE,
    144 	AND,
    145 	OR,
    146 	MUL_EQ,
    147 	DIV_EQ,
    148 	MOD_EQ,
    149 	ADD_EQ,
    150 	SUB_EQ,
    151 	AND_EQ,
    152 	XOR_EQ,
    153 	OR_EQ,
    154 	SHL_EQ,
    155 	SHR_EQ,
    156 	ELLIPSIS,
    157 	CASE,
    158 	DEFAULT,
    159 	IF,
    160 	ELSE,
    161 	SWITCH,
    162 	WHILE,
    163 	DO,
    164 	FOR,
    165 	GOTO,
    166 	VOID,
    167 	FLOAT,
    168 	INT,
    169 	BOOL,
    170 	VA_LIST,
    171 	STRUCT,
    172 	UNION,
    173 	CHAR,
    174 	DOUBLE,
    175 	SHORT,
    176 	LONG,
    177 	LLONG,
    178 	COMPLEX,
    179 	TYPEDEF,
    180 	EXTERN,
    181 	STATIC,
    182 	AUTO,
    183 	REGISTER,
    184 	ENUM,
    185 	TYPEIDEN,
    186 	UNSIGNED,
    187 	SIGNED,
    188 	CONTINUE,
    189 	BREAK,
    190 	RETURN,
    191 	DEFINE,
    192 	INCLUDE,
    193 	LINE,
    194 	PRAGMA,
    195 	ERROR,
    196 	IFDEF,
    197 	ELIF,
    198 	IFNDEF,
    199 	UNDEF,
    200 	ENDIF,
    201 	BUILTIN,
    202 	EOFTOK
    203 };
    204 
    205 /* operations */
    206 enum op {
    207 	OADD,
    208 	OMUL,
    209 	OSUB,
    210 	OINC,
    211 	ODEC,
    212 	ODIV,
    213 	OMOD,
    214 	OSHL,
    215 	OSHR,
    216 	OBAND,
    217 	OBXOR,
    218 	OBOR,
    219 	OSNEG,
    220 	ONEG,
    221 	OCPL,
    222 	OAND,
    223 	OOR,
    224 	OEQ,
    225 	ONE,
    226 	OLT,
    227 	OGE,
    228 	OLE,
    229 	OGT,
    230 	OASSIGN,
    231 	OA_MUL,
    232 	OA_DIV,
    233 	OA_MOD,
    234 	OA_ADD,
    235 	OA_SUB,
    236 	OA_SHL,
    237 	OA_SHR,
    238 	OA_AND,
    239 	OA_XOR,
    240 	OA_OR,
    241 	OADDR,
    242 	OCOMMA,
    243 	OCAST,
    244 	OPTR,
    245 	OSYM,
    246 	OASK,
    247 	OCOLON,
    248 	OFIELD,
    249 	OLABEL,
    250 	ODEFAULT,
    251 	OCASE,
    252 	OJUMP,
    253 	OBRANCH,
    254 	OEXPR,
    255 	OEFUN,
    256 	OELOOP,
    257 	OBLOOP,
    258 	OFUN,
    259 	OPAR,
    260 	OCALL,
    261 	OCALLE,
    262 	ORET,
    263 	ODECL,
    264 	OBSWITCH,
    265 	OESWITCH,
    266 	OINIT,
    267 	OBUILTIN,
    268 	OTYP,
    269 };
    270 
    271 /*
    272  * Definition of structures
    273  */
    274 typedef struct type Type;
    275 typedef struct symbol Symbol;
    276 typedef struct swtch Switch;
    277 typedef struct node Node;
    278 typedef struct input Input;
    279 
    280 struct limits {
    281 	union {
    282 		TUINT i;
    283 		TFLOAT f;
    284 	} max;
    285 	union {
    286 		TUINT i;
    287 		TFLOAT f;
    288 	} min;
    289 };
    290 
    291 struct builtin {
    292 	char *str;
    293 	Node *(*fun)(Symbol *);
    294 };
    295 
    296 struct keyword {
    297 	char *str;
    298 	unsigned char token, value;
    299 };
    300 
    301 struct type {
    302 	unsigned char op;           /* type builder operator */
    303 	unsigned char ns;           /* namespace for struct members */
    304 	short id;                   /* type id, used in dcls */
    305 	char letter;                /* letter of the type */
    306 	unsigned char prop;         /* type properties */
    307 	unsigned char align;        /* align of the type */
    308 	unsigned long size;         /* sizeof the type */
    309 	Type *type;                 /* base type */
    310 	Symbol *tag;                /* symbol of the strug tag */
    311 	union {
    312 		Type **pars;            /* Function type parameters */
    313 		Symbol **fields;        /* fields of aggregate type */
    314 	} p;
    315 	union {
    316 		unsigned char rank;     /* convertion rank */
    317 		TINT elem;              /* number of type parameters */
    318 	} n;
    319 	Type *next;                 /* local list pointer */
    320 	Type *h_next;               /* hash collision list */
    321 };
    322 
    323 struct symbol {
    324 	unsigned char ctx;
    325 	unsigned char hide;
    326 	char ns;
    327 	unsigned short id;
    328 	unsigned short flags;
    329 	char *name;
    330 	Type *type;
    331 	unsigned char token;
    332 	union {
    333 		TINT i;
    334 		TUINT u;
    335 		TFLOAT f;
    336 		char *s;
    337 		unsigned char token;
    338 		Node **init;
    339 		Symbol **pars;
    340 		Node *(*fun)(Symbol *);
    341 	} u;
    342 	struct symbol *next;
    343 	struct symbol *hash;
    344 };
    345 
    346 struct node {
    347 	unsigned char op;
    348 	unsigned char flags;
    349 	Type *type;
    350 	Symbol *sym;
    351 	struct node *left, *right;
    352 };
    353 
    354 struct swtch {
    355 	short nr;
    356 	char hasdef;
    357 };
    358 
    359 struct yystype {
    360 	Symbol *sym;
    361 	unsigned char token;
    362 };
    363 
    364 #ifdef stdin
    365 struct input {
    366 	char flags;
    367 	unsigned lineno;
    368 	char *filenam;
    369 	FILE *fp;
    370 	Symbol *hide;
    371 	char *line, *begin, *p;
    372 	struct input *next;
    373 };
    374 #endif
    375 
    376 /* error.c */
    377 extern void error(char *fmt, ...);
    378 extern void warn(char *fmt, ...);
    379 extern void unexpected(void);
    380 extern void errorp(char *fmt, ...);
    381 extern void cpperror(char *fmt, ...);
    382 extern Type *deftype(Type *tp);
    383 
    384 /* types.c */
    385 extern int eqtype(Type *tp1, Type *tp2, int eqflag);
    386 extern Type *ctype(int type, int sign, int size);
    387 extern Type *mktype(Type *tp, int op, TINT nelem, Type *data[]);
    388 extern Type *duptype(Type *base);
    389 extern struct limits *getlimits(Type *tp);
    390 extern void typesize(Type *tp);
    391 extern void flushtypes(void);
    392 
    393 /* symbol.c */
    394 extern void dumpstab(Symbol **tbl, char *msg);
    395 extern Symbol *lookup(int ns, char *name, int alloc);
    396 extern Symbol *nextsym(Symbol *sym, int ns);
    397 extern Symbol *install(int ns, Symbol *sym);
    398 extern Symbol *newsym(int ns, char *name);
    399 extern void pushctx(void), popctx(void);
    400 extern void killsym(Symbol *sym);
    401 extern Symbol *newlabel(void);
    402 extern void keywords(struct keyword *key, int ns);
    403 extern void builtins(struct builtin *builts);
    404 extern Symbol *newstring(char *s, size_t len);
    405 extern unsigned newid(void);
    406 
    407 /* stmt.c */
    408 extern void compound(Symbol *lbreak, Symbol *lcont, Switch *sw);
    409 
    410 /* decl.c */
    411 extern Type *typename(void);
    412 extern void decl(void);
    413 
    414 /* lex.c */
    415 extern int ahead(void);
    416 extern int next(void);
    417 extern void expect(int tok);
    418 extern void discard(void);
    419 extern int addinput(char *fname, Symbol *hide, char *buffer);
    420 extern void delinput(void);
    421 extern void setsafe(int type);
    422 extern void ilex(void);
    423 extern int setloc(char *fname, unsigned line);
    424 #define accept(t) ((yytoken == (t)) ? next() : 0)
    425 
    426 /* code.c */
    427 extern void prtree(Node *np);
    428 extern void emit(int, void *);
    429 extern Node *node(int op, Type *tp, Node *left, Node *rigth);
    430 extern Node *varnode(Symbol *sym);
    431 extern Node *constnode(Symbol *sym);
    432 extern Node *sizeofnode(Type *tp);
    433 extern void freetree(Node *np);
    434 extern void icode(void);
    435 #define BTYPE(np) ((np)->type->op)
    436 
    437 /* fold.c */
    438 extern Node *simplify(Node *np);
    439 extern TUINT ones(int nbytes);
    440 
    441 /* expr.c */
    442 extern Node *decay(Node *), *negate(Node *np), *assign(void);
    443 extern Node *convert(Node *np, Type *tp1, int iscast);
    444 extern Node *constexpr(void), *condexpr(int neg), *expr(void);
    445 extern int isnodecmp(int op);
    446 extern int negop(int op);
    447 extern int cmpnode(Node *np, TUINT val);
    448 
    449 /* init.c */
    450 extern void initializer(Symbol *sym, Type *tp);
    451 extern Node *initlist(Type *tp);
    452 
    453 /* cpp.c */
    454 extern void icpp(void);
    455 extern int cpp(void);
    456 extern int expand(char *begin, Symbol *sym);
    457 extern void incdir(char *dir);
    458 extern void outcpp(void);
    459 extern void defdefine(char *macro, char *val, char *source);
    460 extern void undefmacro(char *s);
    461 extern void ppragmaln(void);
    462 
    463 /* builtin.c */
    464 extern void ibuilts(void);
    465 
    466 /* arch.c */
    467 extern void iarch(void);
    468 extern int valid_va_list(Type *tp);
    469 
    470 /*
    471  * Definition of global variables
    472  */
    473 extern struct yystype yylval;
    474 extern char yytext[];
    475 extern int yytoken;
    476 extern unsigned short yylen;
    477 extern int disexpand;
    478 extern unsigned cppctx;
    479 extern Input *input;
    480 extern int lexmode, namespace;
    481 extern int onlycpp, onlyheader;
    482 extern unsigned curctx;
    483 extern Symbol *curfun, *zero, *one;
    484 extern char *infile;
    485 extern unsigned lineno;
    486 extern char filenam[];
    487 
    488 extern Type *voidtype, *pvoidtype, *booltype,
    489             *uchartype,   *chartype, *schartype,
    490             *uinttype,    *inttype,
    491             *sizettype, *pdifftype,
    492             *ushortype,   *shortype,
    493             *longtype,    *ulongtype,
    494             *ullongtype,  *llongtype,
    495             *floattype,   *doubletype,  *ldoubletype,
    496             *ellipsistype, *va_list_type, *va_type;