scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | README | LICENSE

cc1.h (9709B)


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