aruu

[MIRROR] Actual replacement for Busybox | suckless
git clone https://git.daat.foo/aruu.git
Log | Files | Refs | README | LICENSE

type.c (10399B)


      1 #include <assert.h>
      2 #include <stdbool.h>
      3 #include <stddef.h>
      4 #include <string.h>
      5 #include "util.h"
      6 #include "cc.h"
      7 
      8 #define INTTYPE(k, n, s, p)                                                                        \
      9   {                                                                                                \
     10       .kind    = k,                                                                                \
     11       .size    = n,                                                                                \
     12       .align   = n,                                                                                \
     13       .u.arith = {.issigned = s, .width = n * 8},                                                  \
     14       .prop    = PROPSCALAR | PROPARITH | PROPREAL | PROPINT | p,                                  \
     15   }
     16 #define FLTTYPE(k, n)                                                                              \
     17   {                                                                                                \
     18       .kind  = k,                                                                                  \
     19       .size  = n,                                                                                  \
     20       .align = n,                                                                                  \
     21       .prop  = PROPSCALAR | PROPARITH | PROPREAL | PROPFLOAT,                                      \
     22   }
     23 
     24 struct type typevoid = {.kind = TYPEVOID, .incomplete = true};
     25 
     26 struct type typebool = INTTYPE(TYPEBOOL, 1, false, 0);
     27 
     28 struct type typechar  = INTTYPE(TYPECHAR, 1, true, PROPCHAR);
     29 struct type typeschar = INTTYPE(TYPECHAR, 1, true, PROPCHAR);
     30 struct type typeuchar = INTTYPE(TYPECHAR, 1, false, PROPCHAR);
     31 
     32 struct type typeshort  = INTTYPE(TYPESHORT, 2, true, 0);
     33 struct type typeushort = INTTYPE(TYPESHORT, 2, false, 0);
     34 
     35 struct type typeint  = INTTYPE(TYPEINT, 4, true, 0);
     36 struct type typeuint = INTTYPE(TYPEINT, 4, false, 0);
     37 
     38 struct type typelong  = INTTYPE(TYPELONG, 8, true, 0);
     39 struct type typeulong = INTTYPE(TYPELONG, 8, false, 0);
     40 
     41 struct type typellong  = INTTYPE(TYPELLONG, 8, true, 0);
     42 struct type typeullong = INTTYPE(TYPELLONG, 8, false, 0);
     43 
     44 /* unlike every other arithmetic type, this one never lives in a
     45  * single qbe scalar register: qbe has no 128-bit type at all, so a
     46  * value of this type always lives in memory instead, the same way a
     47  * struct does (funcload/funcstore route TYPEINT128 through the same
     48  * address-is-the-value path as TYPESTRUCT/TYPEUNION, see qbe.c). that
     49  * also means it inherits struct/union parameter and return-value
     50  * passing for free, without needing any abi work of its own */
     51 struct type typeint128  = INTTYPE(TYPEINT128, 16, true, 0);
     52 struct type typeuint128 = INTTYPE(TYPEINT128, 16, false, 0);
     53 
     54 struct type typefloat   = FLTTYPE(TYPEFLOAT, 4);
     55 struct type typedouble  = FLTTYPE(TYPEDOUBLE, 8);
     56 struct type typeldouble = FLTTYPE(TYPELDOUBLE, 16);
     57 
     58 /* a complex value is its real and imaginary parts back to back, and
     59  * like __int128 always lives in memory rather than a qbe register of
     60  * its own (funcload/funcstore route it the same way, see qbe.c). not
     61  * an arithmetic type in this compiler's own sense (no PROPARITH): it
     62  * never reaches the usual arithmetic conversions expr.c's commonreal
     63  * applies to real operands, +, -, and unary - between two operands of
     64  * the same complex type are special-cased directly instead */
     65 struct type typefloatcomplex  = {.kind = TYPECOMPLEX, .size = 8, .align = 4, .base = &typefloat};
     66 struct type typedoublecomplex = {.kind = TYPECOMPLEX, .size = 16, .align = 8, .base = &typedouble};
     67 
     68 struct type typenullptr = {.kind = TYPENULLPTR, .size = 8, .align = 8, .prop = PROPSCALAR};
     69 
     70 struct type *typeadjvalist;
     71 
     72 struct type *
     73 mktype(enum typekind kind, enum typeprop prop)
     74 {
     75   struct type *t;
     76 
     77   t             = xmalloc(sizeof(*t));
     78   t->kind       = kind;
     79   t->prop       = prop;
     80   t->value      = NULL;
     81   t->incomplete = false;
     82 
     83   return t;
     84 }
     85 
     86 struct type *
     87 mkpointertype(struct type *base, enum typequal qual)
     88 {
     89   struct type *t;
     90 
     91   t        = mktype(TYPEPOINTER, PROPSCALAR);
     92   t->base  = base;
     93   t->qual  = qual;
     94   t->size  = 8;
     95   t->align = 8;
     96   if (base)
     97     t->prop |= base->prop & PROPVM;
     98 
     99   return t;
    100 }
    101 
    102 struct type *
    103 mkarraytype(struct type *base, enum typequal qual, unsigned long long len)
    104 {
    105   struct type *t;
    106 
    107   t                  = mktype(TYPEARRAY, 0);
    108   t->base            = base;
    109   t->qual            = qual;
    110   t->u.array.length  = NULL;
    111   t->u.array.ptrqual = QUALNONE;
    112   t->incomplete      = !len;
    113   if (t->base) {
    114     t->align = t->base->align;
    115     t->size  = t->base->size * len; /* XXX: overflow? */
    116   }
    117 
    118   return t;
    119 }
    120 
    121 struct type *
    122 mkbitinttype(int width, bool sign)
    123 {
    124   struct type *t;
    125 
    126   t                   = mktype(TYPEBITINT, PROPSCALAR | PROPARITH | PROPREAL | PROPINT);
    127   t->u.arith.issigned = sign;
    128   t->u.arith.width    = width;
    129   if (width < 1 + sign || width > 64)
    130     error(&tok.loc, "invalid %s _BitInt width %d", sign ? "signed" : "unsigned", width);
    131 
    132   /* calculate byte size */
    133   t->size = 1;
    134   while (t->size * 8 < (unsigned long long)width)
    135     t->size <<= 1;
    136   t->align = t->size;
    137 
    138   return t;
    139 }
    140 
    141 /*
    142  * we define type rank using the number of bits in the type shifted
    143  * left by 4. the least significant 4 bits are used to establish a
    144  * ranking between integer types with the same width (such as long and
    145  * long long on 64-bit platforms)
    146  */
    147 static int
    148 typerank(struct type *t)
    149 {
    150   if (t->kind == TYPEENUM)
    151     t = t->base;
    152   assert(t->prop & PROPINT);
    153   switch (t->kind) {
    154     case TYPEBOOL:
    155       return 0;
    156     case TYPECHAR:
    157       return 0x081;
    158     case TYPESHORT:
    159       return 0x101;
    160     case TYPEINT:
    161       return 0x201;
    162     case TYPELONG:
    163       return t->size << (3 + 4) | 0x1;
    164     case TYPELLONG:
    165       return 0x402;
    166     case TYPEBITINT:
    167       return t->u.arith.width << 4;
    168     case TYPEINT128:
    169       return 128 << 4;
    170     default:
    171       break;
    172   }
    173   fatal("internal error; unhandled integer type");
    174   return -1;
    175 }
    176 
    177 bool
    178 typecompatible(struct type *t1, struct type *t2)
    179 {
    180   struct decl *p1, *p2;
    181   struct expr *e1, *e2;
    182 
    183   if (t1 == t2)
    184     return true;
    185   if (t1->kind != t2->kind) {
    186     /*
    187      * enum types are compatible with their underlying
    188      * type, but not with each other (unless they are the
    189      * same type)
    190      */
    191     return (t1->kind == TYPEENUM && t2 == t1->base) || (t2->kind == TYPEENUM && t1 == t2->base);
    192   }
    193   switch (t1->kind) {
    194     case TYPEBITINT:
    195       return t1->u.arith.width == t2->u.arith.width && t1->u.arith.issigned == t2->u.arith.issigned;
    196     case TYPEPOINTER:
    197       goto derived;
    198     case TYPEARRAY:
    199       if (t1->incomplete || t2->incomplete)
    200         goto derived;
    201       e1 = t1->u.array.length;
    202       e2 = t2->u.array.length;
    203       if (e1 && e2 && e1->kind == EXPRCONST && e2->kind == EXPRCONST
    204           && e1->u.constant.u != e2->u.constant.u)
    205         return false;
    206       goto derived;
    207     case TYPEFUNC:
    208       if (t1->u.func.isvararg != t2->u.func.isvararg)
    209         return false;
    210       for (p1 = t1->u.func.params, p2 = t2->u.func.params; p1 && p2; p1 = p1->next, p2 = p2->next) {
    211         if (!typecompatible(p1->type, p2->type))
    212           return false;
    213       }
    214       if (p1 || p2)
    215         return false;
    216       goto derived;
    217     derived:
    218       return t1->qual == t2->qual && typecompatible(t1->base, t2->base);
    219     default:
    220       break;
    221   }
    222   return false;
    223 }
    224 
    225 bool
    226 typesame(struct type *t1, struct type *t2)
    227 {
    228   /* XXX: implement */
    229   return typecompatible(t1, t2);
    230 }
    231 
    232 struct type *
    233 typecomposite(struct type *t1, struct type *t2)
    234 {
    235   (void)t2;
    236   /* XXX: implement 6.2.7 */
    237   /* XXX: merge with typecompatible? */
    238   return t1;
    239 }
    240 
    241 struct type *
    242 typepromote(struct type *t, unsigned width)
    243 {
    244   if (t == &typefloat)
    245     return &typedouble;
    246   if (t->kind == TYPEBITINT)
    247     return t;
    248   if (t->prop & PROPINT && (typerank(t) <= typerank(&typeint) || width <= typeint.size * 8)) {
    249     if (width == -1u)
    250       width = t->size * 8;
    251     return width - t->u.arith.issigned < typeint.size * 8 ? &typeint : &typeuint;
    252   }
    253   return t;
    254 }
    255 
    256 struct type *
    257 typecommonreal(struct type *t1, unsigned w1, struct type *t2, unsigned w2)
    258 {
    259   struct type *tmp;
    260 
    261   assert(t1->prop & PROPREAL && t2->prop & PROPREAL);
    262   if (t1 == &typeldouble || t2 == &typeldouble)
    263     return &typeldouble;
    264   if (t1 == &typedouble || t2 == &typedouble)
    265     return &typedouble;
    266   if (t1 == &typefloat || t2 == &typefloat)
    267     return &typefloat;
    268   t1 = typepromote(t1, w1);
    269   t2 = typepromote(t2, w2);
    270   if (t1 == t2)
    271     return t1;
    272   if (t1->u.arith.issigned == t2->u.arith.issigned)
    273     return typerank(t1) > typerank(t2) ? t1 : t2;
    274   if (t1->u.arith.issigned) {
    275     tmp = t1;
    276     t1  = t2;
    277     t2  = tmp;
    278   }
    279   /* t1 is unsigned and t2 is signed */
    280   if (typerank(t1) >= typerank(t2))
    281     return t1;
    282   /* t1 has a lower rank than t2 */
    283   if (t1->u.arith.width < t2->u.arith.width)
    284     return t2;
    285   switch (t2->kind) {
    286     case TYPEINT:
    287       return &typeuint;
    288     case TYPELONG:
    289       return &typeulong;
    290     case TYPELLONG:
    291       return &typellong;
    292     default:
    293       break;
    294   }
    295   fatal("internal error; could not find common real type");
    296   return NULL;
    297 }
    298 
    299 /* function parameter type adjustment (C11 6.7.6.3p7) */
    300 struct type *
    301 typeadjust(struct type *t, enum typequal *tq)
    302 {
    303   enum typequal ptrqual;
    304 
    305   switch (t->kind) {
    306     case TYPEARRAY:
    307       ptrqual = t->u.array.ptrqual;
    308       t       = mkpointertype(t->base, *tq | t->qual);
    309       *tq     = ptrqual;
    310       break;
    311     case TYPEFUNC:
    312       assert(*tq == QUALNONE);
    313       t = mkpointertype(t, QUALNONE);
    314       break;
    315     default:
    316       break;
    317   }
    318 
    319   return t;
    320 }
    321 
    322 struct member *
    323 typemember(struct type *t, const char *name, unsigned long long *offset)
    324 {
    325   struct member *m, *sub;
    326 
    327   assert(t->kind == TYPESTRUCT || t->kind == TYPEUNION);
    328   for (m = t->u.structunion.members; m; m = m->next) {
    329     if (m->name) {
    330       if (strcmp(m->name, name) == 0) {
    331         *offset += m->offset;
    332         return m;
    333       }
    334     } else {
    335       sub = typemember(m->type, name, offset);
    336       if (sub) {
    337         *offset += m->offset;
    338         return sub;
    339       }
    340     }
    341   }
    342   return NULL;
    343 }
    344 
    345 bool
    346 typehasint(struct type *t, unsigned long long i, bool sign)
    347 {
    348   assert(t->prop & PROPINT);
    349   if (sign && i >= -1ull << 63)
    350     return t->u.arith.issigned && i >= -1ull << ((t->size << 3) - 1);
    351   return i <= 0xffffffffffffffffull >> (((8 - t->size) << 3) + t->u.arith.issigned);
    352 }