scc

Simple C99 Compiler
Log | Files | Refs | README | LICENSE

cc1.h (8034B)


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