cc.h (11731B)
1 #include <stdbool.h> 2 #include <stddef.h> 3 #include <stdio.h> 4 5 #ifndef noreturn 6 #if __STDC_VERSION__ >= 201112L 7 #define noreturn _Noreturn 8 /* C99 has no word for it: the compilers that build us have their own */ 9 #elif defined(__GNUC__) 10 #define noreturn __attribute__((noreturn)) 11 #else 12 #define noreturn 13 #endif 14 #endif 15 16 struct func; 17 18 enum tokenkind { 19 #define TOKEN(t, s) t, 20 #include "tokens.h" 21 #undef TOKEN 22 23 TPPIDENT = TALIGNAS, 24 TIDENT = TDEFINE 25 }; 26 27 struct location { 28 const char *file; 29 size_t line, col; 30 }; 31 32 struct token { 33 enum tokenkind kind; 34 /* whether or not the token is ineligible for expansion */ 35 bool hide; 36 /* whether or not the token was preceeded by a space */ 37 bool space; 38 struct location loc; 39 char *lit; 40 }; 41 42 enum typequal { 43 QUALNONE, 44 45 QUALCONST = 1 << 1, 46 QUALRESTRICT = 1 << 2, 47 QUALVOLATILE = 1 << 3, 48 QUALATOMIC = 1 << 4 49 }; 50 51 enum typekind { 52 TYPENONE, 53 54 TYPEVOID, 55 TYPEBOOL, 56 TYPECHAR, 57 TYPESHORT, 58 TYPEINT, 59 TYPEENUM, 60 TYPELONG, 61 TYPELLONG, 62 TYPEFLOAT, 63 TYPEDOUBLE, 64 TYPELDOUBLE, 65 TYPEPOINTER, 66 TYPEARRAY, 67 TYPEFUNC, 68 TYPESTRUCT, 69 TYPEUNION, 70 TYPENULLPTR, 71 TYPEBITINT, 72 TYPEINT128, 73 TYPECOMPLEX, 74 }; 75 76 enum typeprop { 77 PROPNONE, 78 79 PROPCHAR = 1 << 0, 80 PROPINT = 1 << 1, 81 PROPREAL = 1 << 2, 82 PROPARITH = 1 << 3, 83 PROPSCALAR = 1 << 4, 84 PROPFLOAT = 1 << 5, 85 PROPVM = 1 << 6, /* variably-modified type */ 86 PROPFLEX = 1 << 7, /* struct with flexible array member */ 87 }; 88 89 struct bitfield { 90 short before; /* number of bits in the storage unit before the bit-field 91 */ 92 short after; /* number of bits in the storage unit after the bit-field 93 */ 94 }; 95 96 struct member { 97 char *name; 98 struct type *type; 99 enum typequal qual; 100 unsigned long long offset; 101 struct bitfield bits; 102 struct member *next; 103 }; 104 105 struct type { 106 enum typekind kind; 107 enum typeprop prop; 108 int align; 109 unsigned long long size; 110 struct value *value; /* used by the backend */ 111 struct type *base; 112 struct list link; /* used only during construction of type */ 113 /* qualifiers of the base type */ 114 enum typequal qual; 115 bool incomplete; 116 union { 117 struct { 118 bool issigned, iscomplex; 119 int width; 120 } arith; 121 struct { 122 struct expr *length; 123 enum typequal ptrqual; 124 struct value *size; 125 } array; 126 struct { 127 bool isvararg; 128 struct decl *params; 129 size_t nparam; 130 } func; 131 struct { 132 char *tag; 133 struct member *members; 134 } structunion; 135 } u; 136 }; 137 138 enum declkind { 139 DECLTYPE, 140 DECLOBJECT, 141 DECLFUNC, 142 DECLCONST, 143 DECLBUILTIN, 144 }; 145 146 enum linkage { 147 LINKNONE, 148 LINKINTERN, 149 LINKEXTERN, 150 }; 151 152 enum storageduration { 153 SDSTATIC, 154 SDTHREAD, 155 SDAUTO, 156 }; 157 158 enum builtinkind { 159 BUILTINALLOCA, 160 BUILTINCONSTANTP, 161 BUILTINEXPECT, 162 BUILTININFF, 163 BUILTINNANF, 164 BUILTINOFFSETOF, 165 BUILTINTYPESCOMPATIBLEP, 166 BUILTINUNREACHABLE, 167 BUILTINVAARG, 168 BUILTINVACOPY, 169 BUILTINVAEND, 170 BUILTINVALIST, 171 BUILTINVASTART, 172 BUILTINATOMICLOAD, 173 BUILTINATOMICSTORE, 174 BUILTINATOMICEXCHANGE, 175 BUILTINATOMICCOMPAREEXCHANGE, 176 BUILTINATOMICFETCHADD, 177 BUILTINATOMICFETCHSUB, 178 BUILTINATOMICFETCHAND, 179 BUILTINATOMICFETCHOR, 180 BUILTINATOMICFETCHXOR, 181 }; 182 183 struct decl { 184 char *name; 185 enum declkind kind; 186 enum linkage linkage; 187 struct type *type; 188 enum typequal qual; 189 struct value *value; 190 char *asmname; 191 bool defined; 192 bool tentative; 193 struct decl *next; 194 195 union { 196 struct { 197 /* alignment of object storage (may be stricter than 198 * type requires) */ 199 int align; 200 enum storageduration storage; 201 } obj; 202 struct { 203 /* the function might have an "inline definition" 204 * (C11 6.7.4p7) */ 205 bool inlinedefn; 206 bool isnoreturn; 207 } func; 208 unsigned long long enumconst; 209 enum builtinkind builtin; 210 } u; 211 }; 212 213 struct scope { 214 struct map tags; 215 struct map decls; 216 struct block *breaklabel; 217 struct block *continuelabel; 218 struct switchcases *switchcases; 219 struct scope *parent; 220 }; 221 222 enum exprkind { 223 /* primary expression */ 224 EXPRIDENT, 225 EXPRCONST, 226 EXPRSTRING, 227 228 /* postfix expression */ 229 EXPRCALL, 230 /* member E.M gets transformed to *(typeof(e.m) *)((char *)e + 231 * offsetof(typeof(e), m)) */ 232 EXPRBITFIELD, 233 EXPRINCDEC, 234 EXPRCOMPOUND, 235 /* subscript E1[E2] gets transformed to *((E1)+(E2)) */ 236 237 EXPRUNARY, 238 EXPRCAST, 239 EXPRBINARY, 240 EXPRCOND, 241 EXPRASSIGN, 242 EXPRCOMMA, 243 244 EXPRBUILTIN, 245 EXPRTEMP, 246 EXPRSIZEOF, 247 }; 248 249 struct stringlit { 250 size_t size; 251 void *data; 252 }; 253 254 struct expr { 255 enum exprkind kind; 256 /* whether this expression is an lvalue */ 257 bool lvalue; 258 /* whether this expression is a pointer decayed from an array or 259 * function designator */ 260 bool decayed; 261 /* the unqualified type of the expression */ 262 struct type *type; 263 /* the type qualifiers of the object this expression refers to (ignored 264 * for non-lvalues) */ 265 enum typequal qual; 266 enum tokenkind op; 267 struct expr *base; 268 struct expr *next; 269 struct expr *toeval; 270 union { 271 struct { 272 struct decl *decl; 273 } ident; 274 union { 275 unsigned long long u; 276 long long i; 277 double f; 278 } constant; 279 struct stringlit string; 280 struct { 281 struct expr *args; 282 size_t nargs; 283 } call; 284 struct { 285 struct bitfield bits; 286 } bitfield; 287 struct { 288 struct decl *decl; 289 struct init *init; 290 } compound; 291 struct { 292 bool post; 293 } incdec; 294 struct { 295 struct expr *l, *r; 296 } binary; 297 struct { 298 struct expr *t, *f; 299 } cond; 300 struct { 301 struct expr *l, *r; 302 } assign; 303 struct { 304 enum builtinkind kind; 305 struct expr *val, *val2; 306 } builtin; 307 struct { 308 struct type *type; 309 } szof; 310 struct value *temp; 311 } u; 312 }; 313 314 struct init { 315 unsigned long long start, end; 316 struct expr *expr; 317 struct bitfield bits; 318 struct init *next; 319 }; 320 321 /* token */ 322 323 extern struct token tok; 324 325 void tokeninit(void); 326 int tokenget(const void *, size_t); 327 void tokenprint(const struct token *, FILE *); 328 char *tokenstr(enum tokenkind); 329 char *tokencheck(const struct token *, enum tokenkind, const char *); 330 void diagloc(const struct location *); 331 noreturn void error(const struct location *, const char *, ...); 332 333 /* scan */ 334 335 void scanfrom(const char *, FILE *); 336 void scanopen(void); 337 void scansetloc(struct location loc); 338 void scan(struct token *); 339 char *scaninclude(bool *); 340 341 /* preprocessor */ 342 343 enum ppflags { 344 /* preserve newlines in preprocessor output */ 345 PPNEWLINE = 1 << 0, 346 }; 347 348 extern enum ppflags ppflags; 349 350 void ppinit(void); 351 void addincdir(const char *); 352 353 void next(void); 354 bool peek(enum tokenkind); 355 char *expect(enum tokenkind, const char *); 356 bool consume(enum tokenkind); 357 358 /* type */ 359 360 struct type *mktype(enum typekind, enum typeprop); 361 struct type *mkpointertype(struct type *, enum typequal); 362 struct type *mkarraytype(struct type *, enum typequal, unsigned long long); 363 struct type *mkbitinttype(int width, bool sign); 364 365 bool typecompatible(struct type *, struct type *); 366 bool typesame(struct type *, struct type *); 367 struct type *typecomposite(struct type *, struct type *); 368 struct type *typeunqual(struct type *, enum typequal *); 369 struct type *typecommonreal(struct type *, unsigned, struct type *, unsigned); 370 struct type *typepromote(struct type *, unsigned); 371 struct type *typeadjust(struct type *, enum typequal *); 372 enum typeprop typeprop(struct type *); 373 struct member *typemember(struct type *, const char *, unsigned long long *); 374 bool typehasint(struct type *, unsigned long long, bool); 375 376 extern struct type typevoid; 377 extern struct type typebool; 378 extern struct type typechar, typeschar, typeuchar; 379 extern struct type typeshort, typeushort; 380 extern struct type typeint, typeuint; 381 extern struct type typelong, typeulong; 382 extern struct type typellong, typeullong; 383 extern struct type typeint128, typeuint128; 384 extern struct type typefloat, typedouble, typeldouble; 385 extern struct type typefloatcomplex, typedoublecomplex; 386 extern struct type typenullptr; 387 extern struct type *typeadjvalist; 388 389 /* targ */ 390 391 struct target { 392 const char *name; 393 struct type *typevalist; 394 struct type *typewchar; 395 int signedchar; 396 }; 397 398 extern const struct target *targ; 399 400 void targinit(const char *); 401 402 /* the macros that name the system, from the ospredef file tagged for it */ 403 extern const char ospredef[]; 404 405 /* attr */ 406 407 enum attrkind { 408 ATTRNORETURN = 1 << 0, 409 410 /* GNU attributes */ 411 ATTRALIGNED = 1 << 2, 412 ATTRCONSTRUCTOR = 1 << 3, 413 ATTRDESTRUCTOR = 1 << 4, 414 ATTRPACKED = 1 << 5, 415 ATTRSECTION = 1 << 6, 416 }; 417 418 struct attr { 419 enum attrkind kind; 420 int align; 421 char *section; 422 }; 423 424 bool attr(struct attr *, enum attrkind); 425 bool gnuattr(struct attr *, enum attrkind); 426 427 /* decl */ 428 429 struct decl *mkdecl(char *name, enum declkind, struct type *, enum typequal, enum linkage); 430 bool decl(struct scope *, struct func *); 431 struct type *typename(struct scope *, enum typequal *, struct expr **); 432 433 struct decl *stringdecl(struct expr *); 434 435 void emittentativedefns(void); 436 437 /* scope */ 438 439 void scopeinit(void); 440 struct scope *mkscope(struct scope *); 441 struct scope *delscope(struct scope *); 442 443 void scopeputdecl(struct scope *, struct decl *); 444 struct decl *scopegetdecl(struct scope *, const char *, bool); 445 446 void scopeputtag(struct scope *, const char *, struct type *); 447 struct type *scopegettag(struct scope *, const char *, bool); 448 449 extern struct scope filescope; 450 451 /* expr */ 452 453 struct type *stringconcat(struct stringlit *, bool); 454 455 struct expr *expr(struct scope *); 456 struct expr *assignexpr(struct scope *); 457 struct expr *condexpr(struct scope *); 458 unsigned long long intconstexpr(struct scope *, bool); 459 void delexpr(struct expr *); 460 461 struct expr *exprassign(struct expr *, struct type *); 462 struct expr *exprpromote(struct expr *); 463 464 /* eval */ 465 466 struct expr *eval(struct expr *); 467 468 /* init */ 469 470 struct init *mkinit(unsigned long long, unsigned long long, struct bitfield, struct expr *); 471 struct init *parseinit(struct scope *, struct type *); 472 473 /* stmt */ 474 475 void stmt(struct func *, struct scope *); 476 477 /* backend */ 478 479 struct gotolabel { 480 struct block *label; 481 bool defined; 482 }; 483 484 struct switchcases { 485 void *root; 486 struct type *type; 487 struct block *defaultlabel; 488 }; 489 490 void switchcase(struct switchcases *, unsigned long long, struct block *); 491 492 struct block *mkblock(char *); 493 494 struct value *mkglobal(struct decl *); 495 496 struct value *mkintconst(unsigned long long); 497 498 struct func *mkfunc(struct decl *, char *, struct type *, struct scope *); 499 void delfunc(struct func *); 500 struct type *functype(struct func *); 501 void funclabel(struct func *, struct block *); 502 struct value *funcbranch(struct func *, struct expr *, struct block *, struct block *); 503 struct value *funcexpr(struct func *, struct expr *); 504 void funcjmp(struct func *, struct block *); 505 void funcjnz(struct func *, struct value *, struct type *, struct block *, struct block *); 506 void funcret(struct func *, struct value *); 507 void funchlt(struct func *); 508 struct gotolabel *funcgoto(struct func *, char *); 509 void funcswitch(struct func *, struct value *, struct switchcases *, struct block *); 510 void funcinit(struct func *, struct decl *, struct init *, bool); 511 512 void emitfunc(struct func *, bool); 513 void emitdata(struct decl *, struct init *);