scc

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

cc1.h (9825B)


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