aruu

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

stmt.c (7948B)


      1 #include <assert.h>
      2 #include <stdbool.h>
      3 #include <stdio.h>
      4 #include <stdlib.h>
      5 #include <string.h>
      6 #include "util.h"
      7 #include "cc.h"
      8 
      9 /* 6.8.1 labeled statements */
     10 static bool
     11 label(struct func *f, struct scope *s)
     12 {
     13   char              *name;
     14   struct gotolabel  *g;
     15   struct block      *b;
     16   unsigned long long i;
     17 
     18   attr(NULL, 0);
     19   switch (tok.kind) {
     20     case TCASE:
     21       next();
     22       if (!s->switchcases)
     23         error(&tok.loc, "'case' label must be in switch");
     24       b = mkblock("switch_case");
     25       funclabel(f, b);
     26       i = intconstexpr(s, true);
     27       switchcase(s->switchcases, i, b);
     28       expect(TCOLON, "after case expression");
     29       break;
     30     case TDEFAULT:
     31       next();
     32       if (!s->switchcases)
     33         error(&tok.loc, "'default' label must be in switch");
     34       if (s->switchcases->defaultlabel)
     35         error(&tok.loc, "multiple 'default' labels");
     36       expect(TCOLON, "after 'default'");
     37       s->switchcases->defaultlabel = mkblock("switch_default");
     38       funclabel(f, s->switchcases->defaultlabel);
     39       break;
     40     default:
     41       if (tok.kind < TIDENT)
     42         return false;
     43       name = tokenstr(tok.kind);
     44       if (!peek(TCOLON))
     45         return false;
     46       g          = funcgoto(f, name);
     47       g->defined = true;
     48       funclabel(f, g->label);
     49       break;
     50   }
     51   return true;
     52 }
     53 
     54 static void
     55 labelstmt(struct func *f, struct scope *s)
     56 {
     57   while (label(f, s))
     58     ;
     59   stmt(f, s);
     60 }
     61 
     62 /* 6.8 statements and blocks */
     63 void
     64 stmt(struct func *f, struct scope *s)
     65 {
     66   char              *name;
     67   struct expr       *e;
     68   struct type       *t;
     69   struct value      *v;
     70   struct block      *b[4];
     71   struct switchcases swtch;
     72 
     73   attr(NULL, 0);
     74   switch (tok.kind) {
     75     /* 6.8.2 compound statement */
     76     case TLBRACE:
     77       next();
     78       s = mkscope(s);
     79       while (tok.kind != TRBRACE) {
     80         if (!label(f, s) && !decl(s, f))
     81           stmt(f, s);
     82       }
     83       s = delscope(s);
     84       next();
     85       break;
     86 
     87     /* 6.8.3 expression statement */
     88     case TSEMICOLON:
     89       next();
     90       break;
     91     default:
     92       e = expr(s);
     93       v = funcexpr(f, e);
     94       delexpr(e);
     95       expect(TSEMICOLON, "after expression statement");
     96       break;
     97 
     98     /* 6.8.4 selection statement */
     99     case TIF:
    100       next();
    101       s = mkscope(s);
    102       expect(TLPAREN, "after 'if'");
    103       e = expr(s);
    104       t = e->type;
    105       if (!(t->prop & PROPSCALAR))
    106         error(
    107             &tok.loc,
    108             "controlling expression of if "
    109             "statement must have scalar type"
    110         );
    111       b[0] = mkblock("if_true");
    112       b[1] = mkblock("if_false");
    113       funcbranch(f, e, b[0], b[1]);
    114       delexpr(e);
    115       expect(TRPAREN, "after expression");
    116 
    117       funclabel(f, b[0]);
    118       s = mkscope(s);
    119       labelstmt(f, s);
    120       s = delscope(s);
    121 
    122       if (consume(TELSE)) {
    123         b[2] = mkblock("if_join");
    124         funcjmp(f, b[2]);
    125         funclabel(f, b[1]);
    126         s = mkscope(s);
    127         labelstmt(f, s);
    128         s = delscope(s);
    129         funclabel(f, b[2]);
    130       } else {
    131         funclabel(f, b[1]);
    132       }
    133       s = delscope(s);
    134       break;
    135     case TSWITCH:
    136       next();
    137 
    138       s = mkscope(s);
    139       expect(TLPAREN, "after 'switch'");
    140       e = expr(s);
    141       expect(TRPAREN, "after expression");
    142 
    143       if (!(e->type->prop & PROPINT))
    144         error(
    145             &tok.loc,
    146             "controlling expression of switch "
    147             "statement must have integer type"
    148         );
    149       e = exprpromote(e);
    150 
    151       swtch.root         = NULL;
    152       swtch.type         = e->type;
    153       swtch.defaultlabel = NULL;
    154 
    155       b[0] = mkblock("switch_cond");
    156       b[1] = mkblock("switch_join");
    157 
    158       v = funcexpr(f, e);
    159       funcjmp(f, b[0]);
    160       s              = mkscope(s);
    161       s->breaklabel  = b[1];
    162       s->switchcases = &swtch;
    163       labelstmt(f, s);
    164       funcjmp(f, b[1]);
    165 
    166       funclabel(f, b[0]);
    167       funcswitch(f, v, &swtch, swtch.defaultlabel ? swtch.defaultlabel : b[1]);
    168       s = delscope(s);
    169 
    170       funclabel(f, b[1]);
    171       s = delscope(s);
    172       break;
    173 
    174     /* 6.8.5 iteration statements */
    175     case TWHILE:
    176       next();
    177       s = mkscope(s);
    178       expect(TLPAREN, "after 'while'");
    179       e = expr(s);
    180       t = e->type;
    181       if (!(t->prop & PROPSCALAR))
    182         error(
    183             &tok.loc,
    184             "controlling expression of loop must "
    185             "have scalar type"
    186         );
    187       expect(TRPAREN, "after expression");
    188 
    189       b[0] = mkblock("while_cond");
    190       b[1] = mkblock("while_body");
    191       b[2] = mkblock("while_join");
    192 
    193       funclabel(f, b[0]);
    194       funcbranch(f, e, b[1], b[2]);
    195       funclabel(f, b[1]);
    196       s                = mkscope(s);
    197       s->continuelabel = b[0];
    198       s->breaklabel    = b[2];
    199       labelstmt(f, s);
    200       s = delscope(s);
    201       funcjmp(f, b[0]);
    202       funclabel(f, b[2]);
    203       s = delscope(s);
    204       break;
    205     case TDO:
    206       next();
    207 
    208       b[0] = mkblock("do_body");
    209       b[1] = mkblock("do_cond");
    210       b[2] = mkblock("do_join");
    211 
    212       s                = mkscope(s);
    213       s                = mkscope(s);
    214       s->continuelabel = b[1];
    215       s->breaklabel    = b[2];
    216       funclabel(f, b[0]);
    217       labelstmt(f, s);
    218       s = delscope(s);
    219 
    220       expect(TWHILE, "after 'do' statement");
    221       expect(TLPAREN, "after 'while'");
    222       funclabel(f, b[1]);
    223       e = expr(s);
    224       t = e->type;
    225       if (!(t->prop & PROPSCALAR))
    226         error(
    227             &tok.loc,
    228             "controlling expression of loop must "
    229             "have scalar type"
    230         );
    231       expect(TRPAREN, "after expression");
    232 
    233       funcbranch(f, e, b[0], b[2]);
    234       funclabel(f, b[2]);
    235       s = delscope(s);
    236       expect(TSEMICOLON, "after 'do' statement");
    237       break;
    238     case TFOR:
    239       next();
    240       expect(TLPAREN, "after 'for'");
    241       s = mkscope(s);
    242       if (!decl(s, f)) {
    243         if (tok.kind != TSEMICOLON) {
    244           e = expr(s);
    245           funcexpr(f, e);
    246           delexpr(e);
    247         }
    248         expect(TSEMICOLON, NULL);
    249       }
    250 
    251       b[0] = mkblock("for_cond");
    252       b[1] = mkblock("for_body");
    253       b[2] = mkblock("for_cont");
    254       b[3] = mkblock("for_join");
    255 
    256       funclabel(f, b[0]);
    257       if (tok.kind != TSEMICOLON) {
    258         e = expr(s);
    259         t = e->type;
    260         if (!(t->prop & PROPSCALAR))
    261           error(
    262               &tok.loc,
    263               "controlling expression of "
    264               "loop must have scalar type"
    265           );
    266         funcbranch(f, e, b[1], b[3]);
    267         delexpr(e);
    268       }
    269       expect(TSEMICOLON, NULL);
    270       e = tok.kind == TRPAREN ? NULL : expr(s);
    271       expect(TRPAREN, NULL);
    272 
    273       funclabel(f, b[1]);
    274       s                = mkscope(s);
    275       s->breaklabel    = b[3];
    276       s->continuelabel = b[2];
    277       labelstmt(f, s);
    278       s = delscope(s);
    279 
    280       funclabel(f, b[2]);
    281       if (e) {
    282         funcexpr(f, e);
    283         delexpr(e);
    284       }
    285       funcjmp(f, b[0]);
    286       funclabel(f, b[3]);
    287       s = delscope(s);
    288       break;
    289 
    290     /* 6.8.6 jump statements */
    291     case TGOTO:
    292       next();
    293       name = expect(TIDENT, "after 'goto'");
    294       funcjmp(f, funcgoto(f, name)->label);
    295       expect(TSEMICOLON, "after 'goto' statement");
    296       break;
    297     case TCONTINUE:
    298       next();
    299       if (!s->continuelabel)
    300         error(&tok.loc, "'continue' statement must be in loop");
    301       funcjmp(f, s->continuelabel);
    302       expect(TSEMICOLON, "after 'continue' statement");
    303       break;
    304     case TBREAK:
    305       next();
    306       if (!s->breaklabel)
    307         error(&tok.loc, "'break' statement must be in loop or switch");
    308       funcjmp(f, s->breaklabel);
    309       expect(TSEMICOLON, "after 'break' statement");
    310       break;
    311     case TRETURN:
    312       next();
    313       t = functype(f);
    314       if (t->base != &typevoid) {
    315         e = exprassign(expr(s), t->base);
    316         v = funcexpr(f, e);
    317         delexpr(e);
    318       } else {
    319         v = NULL;
    320       }
    321       funcret(f, v);
    322       expect(TSEMICOLON, "after 'return' statement");
    323       break;
    324 
    325     case T__ASM__:
    326       error(&tok.loc, "inline assembly is not yet supported");
    327   }
    328 }