scc

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

cc1.h (9953B)


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