scc

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

cc1.h (9781B)


      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 of 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 	TYPE       = 129,
    152 	IDEN,
    153 	SCLASS,
    154 	CONSTANT,
    155 	STRING,
    156 	SIZEOF,
    157 	INDIR,
    158 	INC,
    159 	DEC,
    160 	SHL,
    161 	SHR,
    162 	LE,
    163 	GE,
    164 	EQ,
    165 	NE,
    166 	AND,
    167 	OR,
    168 	MUL_EQ,
    169 	DIV_EQ,
    170 	MOD_EQ,
    171 	ADD_EQ,
    172 	SUB_EQ,
    173 	AND_EQ,
    174 	XOR_EQ,
    175 	OR_EQ,
    176 	SHL_EQ,
    177 	SHR_EQ,
    178 	ELLIPSIS,
    179 	CASE,
    180 	DEFAULT,
    181 	IF,
    182 	ELSE,
    183 	SWITCH,
    184 	WHILE,
    185 	DO,
    186 	FOR,
    187 	GOTO,
    188 	VOID,
    189 	FLOAT,
    190 	INT,
    191 	BOOL,
    192 	VA_LIST,
    193 	STRUCT,
    194 	UNION,
    195 	CHAR,
    196 	DOUBLE,
    197 	SHORT,
    198 	LONG,
    199 	LLONG,
    200 	COMPLEX,
    201 	TYPEDEF,
    202 	EXTERN,
    203 	STATIC,
    204 	AUTO,
    205 	REGISTER,
    206 	ENUM,
    207 	TYPEIDEN,
    208 	UNSIGNED,
    209 	SIGNED,
    210 	CONTINUE,
    211 	BREAK,
    212 	RETURN,
    213 	DEFINE,
    214 	DEFINED,
    215 	INCLUDE,
    216 	LINE,
    217 	PRAGMA,
    218 	ERROR,
    219 	IFDEF,
    220 	ELIF,
    221 	IFNDEF,
    222 	UNDEF,
    223 	ENDIF,
    224 	BUILTIN,
    225 	EOFTOK
    226 };
    227 
    228 /* operations */
    229 enum op {
    230 	OADD,
    231 	OMUL,
    232 	OSUB,
    233 	OINC,
    234 	ODEC,
    235 	ODIV,
    236 	OMOD,
    237 	OSHL,
    238 	OSHR,
    239 	OBAND,
    240 	OBXOR,
    241 	OBOR,
    242 	OSNEG,
    243 	ONEG,
    244 	OCPL,
    245 	OAND,
    246 	OOR,
    247 	OEQ,
    248 	ONE,
    249 	OLT,
    250 	OGE,
    251 	OLE,
    252 	OGT,
    253 	OASSIGN,
    254 	OA_MUL,
    255 	OA_DIV,
    256 	OA_MOD,
    257 	OA_ADD,
    258 	OA_SUB,
    259 	OA_SHL,
    260 	OA_SHR,
    261 	OA_AND,
    262 	OA_XOR,
    263 	OA_OR,
    264 	OADDR,
    265 	OCOMMA,
    266 	OCAST,
    267 	OPTR,
    268 	OSYM,
    269 	OASK,
    270 	OCOLON,
    271 	OFIELD,
    272 	OLABEL,
    273 	ODEFAULT,
    274 	OCASE,
    275 	OJUMP,
    276 	OBRANCH,
    277 	OEXPR,
    278 	OEFUN,
    279 	OELOOP,
    280 	OBLOOP,
    281 	OFUN,
    282 	OPAR,
    283 	OCALL,
    284 	OCALLE,
    285 	ORET,
    286 	ODECL,
    287 	OBSWITCH,
    288 	OESWITCH,
    289 	OINIT,
    290 	OBUILTIN,
    291 	OTYP,
    292 };
    293 
    294 /*
    295  * Definition of structures
    296  */
    297 typedef struct type Type;
    298 typedef struct symbol Symbol;
    299 typedef struct swtch Switch;
    300 typedef struct node Node;
    301 typedef struct macro Macro;
    302 typedef struct input Input;
    303 typedef struct arch Arch;
    304 typedef uint32_t Rune;
    305 
    306 struct limits {
    307 	union {
    308 		unsigned long long i;
    309 		double f;
    310 	} max;
    311 	union {
    312 		unsigned long long i;
    313 		double f;
    314 	} min;
    315 };
    316 
    317 struct builtin {
    318 	char *str;
    319 	Node *(*fun)(Symbol *);
    320 };
    321 
    322 struct type {
    323 	unsigned char op;           /* type builder operator */
    324 	int ns;                     /* namespace for struct members */
    325 	short id;                   /* type id, used in dcls */
    326 	char letter;                /* letter of the type */
    327 	unsigned char prop;         /* type properties */
    328 	unsigned char align;        /* align of the type */
    329 	unsigned long size;         /* sizeof the type */
    330 	Type *type;                 /* base type */
    331 	Symbol *tag;                /* symbol of the strug tag */
    332 	union {
    333 		Type **pars;            /* Function type parameters */
    334 		Symbol **fields;        /* fields of aggregate type */
    335 	} p;
    336 	union {
    337 		unsigned char rank;     /* conversion rank */
    338 		long long elem;         /* number of type parameters */
    339 	} n;
    340 	Type *next;                 /* local list pointer */
    341 	Type *h_next;               /* hash collision list */
    342 };
    343 
    344 struct symbol {
    345 	unsigned char ctx;
    346 	unsigned char hide;
    347 	int ns;
    348 	unsigned short id;
    349 	unsigned short flags;
    350 	char *name;
    351 	Type *type;
    352 	unsigned char token;
    353 	union {
    354 		long long i;
    355 		unsigned long long u;
    356 		float f;
    357 		double d;
    358 		long double ld;
    359 		char *s;
    360 		Rune *rs;
    361 		unsigned char token;
    362 		Node **init;
    363 		Symbol **pars;
    364 		Node *(*fun)(Symbol *);
    365 	} u;
    366 	struct symbol *next;
    367 	struct symbol *hash;
    368 };
    369 
    370 struct node {
    371 	unsigned char op;
    372 	unsigned char flags;
    373 	Type *type;
    374 	Symbol *sym;
    375 	struct node *left, *right;
    376 };
    377 
    378 struct swtch {
    379 	short nr;
    380 	char hasdef;
    381 };
    382 
    383 struct arch {
    384 	Type voidtype;
    385 	Type pvoidtype;
    386 	Type booltype;
    387 	Type schartype;
    388 	Type uchartype;
    389 	Type chartype;
    390 	Type ushorttype;
    391 	Type shorttype;
    392 	Type uinttype;
    393 	Type inttype;
    394 	Type longtype;
    395 	Type ulongtype;
    396 	Type ullongtype;
    397 	Type llongtype;
    398 	Type floattype;
    399 	Type doubletype;
    400 	Type ldoubletype;
    401 	Type sizettype;
    402 	Type pdifftype;
    403 	Type ellipsistype;
    404 	Type va_type;
    405 	Type va_list_type;
    406 	Type wchartype;
    407 
    408 	int (*valid_va_list)(Type *tp);
    409 };
    410 
    411 struct yystype {
    412 	Symbol *sym;
    413 	unsigned char token;
    414 };
    415 
    416 #ifdef NR_MACROARG
    417 struct macro {
    418 	Symbol *sym;
    419 	char *fname;
    420 	char **arglist;
    421 	char *buffer;
    422 	char *def;
    423 	char *arg;
    424 	int bufsiz;
    425 	int argsiz;
    426 	int npars;
    427 	Symbol *hideset[NR_MACROARG];
    428 };
    429 #endif
    430 
    431 #ifdef stdin
    432 struct input {
    433 	char flags;
    434 	unsigned lineno;
    435 	char *filenam;
    436 	FILE *fp;
    437 	Macro *macro;
    438 	char *line, *begin, *p;
    439 	struct input *next;
    440 };
    441 #endif
    442 
    443 /* error.c */
    444 void error(char *, ...);
    445 void warn(char *, ...);
    446 void unexpected(void);
    447 void errorp(char *, ...);
    448 void cpperror(char *, ...);
    449 Type *deftype(Type *);
    450 
    451 /* types.c */
    452 int eqtype(Type *, Type *, int);
    453 Type *ctype(int, int, int);
    454 Type *mktype(Type *, int, long long, Type *[]);
    455 Type *duptype(Type *);
    456 struct limits *getlimits(Type *);
    457 void typesize(Type *);
    458 void flushtypes(void);
    459 
    460 /* symbol.c */
    461 void dumpstab(Symbol **, char *);
    462 Symbol *lookup(int, char *, int);
    463 Symbol *nextsym(Symbol *, int);
    464 Symbol *install(int, Symbol *);
    465 Symbol *newsym(int, char *);
    466 void pushctx(void), popctx(void);
    467 void killsym(Symbol *);
    468 Symbol *newlabel(void);
    469 void builtins(struct builtin *);
    470 Symbol *newstring(char *, size_t);
    471 Symbol *newrstring(Rune *, size_t);
    472 unsigned newid(void);
    473 void isyms(void);
    474 
    475 /* stmt.c */
    476 void compound(Symbol *, Symbol *, Switch *);
    477 
    478 /* decl.c */
    479 Type *typename(void);
    480 void decl(void);
    481 
    482 /* lex.c */
    483 int ahead(void);
    484 int next(void);
    485 void expect(int);
    486 void discard(void);
    487 int moreinput(void);
    488 int addinput(int, void *, int);
    489 void delinput(void);
    490 void setsafe(int);
    491 void setloc(char *, unsigned);
    492 #define accept(t) ((yytoken == (t)) ? next() : 0)
    493 
    494 /* code.c */
    495 Node *prtree(char *, Node *);
    496 void emit(int, void *);
    497 Node *node(int, Type *, Node *, Node *);
    498 Node *varnode(Symbol *);
    499 Node *constnode(Symbol *);
    500 Node *sizeofnode(Type *);
    501 Node *offsetnode(Symbol *,  Type *);
    502 Node *addrnode(unsigned long long);
    503 void freetree(Node *);
    504 void icode(void);
    505 Node *zeronode(Type *);
    506 
    507 /* fold.c */
    508 Node *simplify(Node *);
    509 unsigned long long ones(int);
    510 
    511 /* expr.c */
    512 Node *decay(Node *), *negate(Node *np), *assign(void);
    513 Node *convert(Node *, Type *, int);
    514 Node *iconstexpr(void), *condexpr(int), *expr(void);
    515 int isnodecmp(int);
    516 int negop(int);
    517 int cmpnode(Node *, unsigned long long);
    518 int power2node(Node *, int *);
    519 
    520 /* init.c */
    521 void initializer(Symbol *);
    522 
    523 /* cpp.c */
    524 void icpp(void);
    525 int cpp(void);
    526 int expand(Symbol *);
    527 void incdir(char *);
    528 void outcpp(void);
    529 void defdefine(char *, char *, char *);
    530 void undefmacro(char *);
    531 void ppragmaln(void);
    532 void delmacro(Macro *);
    533 Macro *newmacro(Symbol *);
    534 Node *defined(void);
    535 
    536 
    537 /* builtin.c */
    538 void ibuilts(void);
    539 
    540 /* arch.c */
    541 void iarch(void);
    542 int valid_va_list(Type *);
    543 
    544 /* architectures */
    545 Arch *amd64_sysv(void);
    546 Arch *z80_scc(void);
    547 Arch *arm64_sysv(void);
    548 Arch *riscv64_sysv(void);
    549 Arch *i386_sysv(void);
    550 
    551 /*
    552  * Definition of global variables
    553  */
    554 extern struct yystype yylval;
    555 extern char yytext[];
    556 extern int yytoken;
    557 extern unsigned short yylen;
    558 extern int disexpand, disstring;
    559 extern unsigned cppctx;
    560 extern Input *input;
    561 extern int lexmode, namespace;
    562 extern int onlycpp, onlyheader;
    563 extern unsigned curctx;
    564 extern Symbol *curfun, *zero, *one;
    565 extern unsigned lineno;
    566 extern char filenam[];
    567 extern char *architecture;
    568 
    569 extern Type *voidtype, *pvoidtype, *booltype,
    570             *uchartype,   *chartype, *schartype,
    571             *uinttype,    *inttype,
    572             *sizettype, *pdifftype,
    573             *ushorttype,   *shorttype,
    574             *longtype,    *ulongtype,
    575             *ullongtype,  *llongtype,
    576             *floattype,   *doubletype,  *ldoubletype,
    577             *ellipsistype, *va_list_type, *va_type,
    578             *wchartype;