aboutsummaryrefslogtreecommitdiff
path: root/td.c
blob: b0c910cdd527ab0eb8fb84405e79674968db9deb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
#include "td.h"
#include "div.h"
#include "errloc.h"
#include "fn.h"
#include "lex.h"
#include "parse.h"
#include "prv.h"
#include "storage.h"
#include "type.h"
#include <errno.h>
#include <limits.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static int cid(const struct lex *, struct prv *);
static int cnum(const struct lex *, struct prv *);
static int end(const struct lex *, struct prv *);
static int u(const struct lex *, struct prv *);
static int s(const struct lex *, struct prv *);
static int c(const struct lex *, struct prv *);
static int a(const struct lex *, struct prv *);
static int p(const struct lex *, struct prv *);

static const struct seq seqs[] =
{
    {(struct step[]){{ID, "end"}, {0}}, .fn = end},
    {(struct step[]){{ID}, {ID, "constant"}, {ID, "to"}, {ID}, {0}}, .fn = cid},
    {(struct step[]){{ID}, {ID, "constant"}, {ID, "to"}, {NUM}, {0}}, .fn = cnum},
    {(struct step[]){{ID}, {ID, "alias"}, {ID, "of"}, {0}}, .fn = a},
    {(struct step[]){{ID}, {ID, "union"}, {0}}, .fn = u},
    {(struct step[]){{ID}, {ID, "struct"}, {0}}, .fn = s},
    {(struct step[]){{ID}, {ID, "constant"}, {0}}, .fn = c},
    {(struct step[]){{ID}, {ID, "prototype"}, {0}}, .fn = p},
    {0}
};

static void reset(struct prv *p)
{
    struct pos *pos = &p->pos[p->i];

    pos->seq = seqs;
    pos->step = seqs->steps;
    p->stk = p->tk;
}

static int bump(const struct tk *tk, const struct type *t, struct prv *p)
{
    struct tdconstant *tdc = p->tdc;
    int neg = 0;

    if (!t->sign)
    {
        unsigned long long uv = t->u.c.uv;

        if (uv + 1 < uv)
        {
            errloc(tk, "constant value exceeds maximum (%llu)", ULLONG_MAX);
            return -1;
        }

        tdc->u.uv = ++uv;
    }
    else
    {
        long long v = t->u.c.v;

        if (++v < 0)
            neg = 1;

        tdc->u.v = v;
    }

    tdc->neg = neg;

    for (size_t i = 0; i < p->block; i++)
        fputc('\t', stderr);

    fprintf(stderr, "\t\tnext %s constant value set to ",
        neg ? "negative" : "positive");

    if (neg)
        fprintf(stderr, "%lld", tdc->u.v);
    else
        fprintf(stderr, "%llu", tdc->u.uv);

    fputc('\n', stderr);
    return 0;
}

static int cid(const struct lex *l, struct prv *p)
{
    const struct fn *fn = fn_cur(p);
    const struct tk *id = p->stk, *v = p->stk + 3;
    const struct stentry *e;
    const struct type *t = type_find(fn, id->s);
    struct tdconstant *tdc = p->tdc;
    struct td *td = fn->td;
    size_t n = td->ntypes + 1;
    struct type *types, *nt;

    if (t)
    {
        const struct loc *loc = &t->tk->loc;

        errloc(id, "type \"%s\" already defined at %s:%d:%d", id->s, loc->f,
            loc->line, loc->col);
        return -1;
    }
    else if ((e = fn_var(fn, id)))
    {
        const struct loc *loc = &e->tk->loc;

        errloc(id, "\"%s\" already defined as a variable at %s:%d:%d", id->s,
            loc->f, loc->line, loc->col);
        return -1;
    }
    else if ((e = fn_var(fn, v)))
    {
        const struct loc *loc = &e->tk->loc;

        errloc(id, "value \"%s\" (defined at %s:%d:%d) is not a constant",
            v->s, loc->f, loc->line, loc->col);
        return -1;
    }
    else if (!(t = type_find(fn, v->s)))
    {
        errloc(id, "undefined reference to value \"%s\"", v->s);
        return -1;
    }
    else if (t->type != C)
    {
        const struct loc *loc = &t->tk->loc;

        errloc(id, "value \"%s\" (defined at %s:%d:%d) is not a constant",
            v->s, loc->f, loc->line, loc->col);
        return -1;
    }
    else if (!(types = realloc(td->types, n * sizeof *types)))
    {
        perror("realloc(3)");
        return -1;
    }

    td->types = types;
    nt = &td->types[td->ntypes++];
    *nt = (struct type)
    {
        .type = C,
        .tk = id,
        .sign = t->sign,
        .u.c = t->u.c
    };

    tdc->neg = t->sign;

    if (bump(id, t, p))
        return -1;

    for (size_t i = 0; i < p->block; i++)
        fputc('\t', stderr);

    fprintf(stderr, "\tadding constant %s to %s (%lld)\n", id->s, v->s,
        t->u.c.v);
    reset(p);
    return 1;
}

static int cnum(const struct lex *l, struct prv *p)
{
    const struct fn *fn = fn_cur(p);
    const struct tk *id = p->stk, *num = p->stk + 3;
    const char *name = id->s;
    const struct stentry *e;
    struct td *td = fn->td;
    size_t n = td->ntypes + 1;
    struct type *types, *nt;
    struct tdconstant *tdc = p->tdc;
    const struct type *t = type_find(fn, name);
    int sign = *num->s == '-';
    unsigned long long uv = 0;
    long long v;
    char *end;

    errno = 0;
    v = strtoll(num->s, &end, 0);

    if (!sign || errno || *end)
    {
        errno = 0;
        uv = strtoull(num->s, NULL, 0);
    }

    if (t)
    {
        const struct loc *loc = &t->tk->loc;

        errloc(id, "type \"%s\" already defined at %s:%d:%d", name, loc->f,
            loc->line, loc->col);
        return -1;
    }
    else if ((e = fn_var(fn, id)))
    {
        const struct loc *loc = &e->tk->loc;

        errloc(id, "\"%s\" already defined as a variable at %s:%d:%d", id->s,
            loc->f, loc->line, loc->col);
        return -1;
    }
    else if (!(types = realloc(td->types, n * sizeof *types)))
    {
        perror("realloc(3)");
        return -1;
    }

    td->types = types;
    nt = &td->types[td->ntypes++];
    *nt = (struct type)
    {
        .type = C,
        .tk = id,
        .sign = sign
    };

    if (sign)
        tdc->u.v = nt->u.c.v = v;
    else
        tdc->u.uv = nt->u.c.uv = uv;

    tdc->neg = sign;

    for (size_t i = 0; i < p->block; i++)
        fputc('\t', stderr);

    fprintf(stderr, "\tadding constant %s to %s\n", id->s, num->s);

    if (bump(id, nt, p))
        return -1;

    reset(p);
    return 1;
}

static int c(const struct lex *l, struct prv *p)
{
    const struct fn *fn = fn_cur(p);
    const struct tk *id = p->stk;
    const struct stentry *e;
    const struct type *t = type_find(fn, id->s);
    struct td *td = fn->td;
    size_t n = td->ntypes + 1;
    struct type *types, *nt;
    const struct tdconstant *tdc = p->tdc;

    if (t)
    {
        const struct loc *loc = &t->tk->loc;

        errloc(id, "type \"%s\" already defined at %s:%d:%d", id->s, loc->f,
            loc->line, loc->col);
        return -1;
    }
    else if ((e = fn_var(fn, id)))
    {
        const struct loc *loc = &e->tk->loc;

        errloc(id, "\"%s\" already defined as a variable at %s:%d:%d", id->s,
            loc->f, loc->line, loc->col);
        return -1;
    }
    else if (!(types = realloc(td->types, n * sizeof *types)))
    {
        perror("realloc(3)");
        return -1;
    }

    td->types = types;
    nt = &td->types[td->ntypes];
    *nt = (struct type)
    {
        .type = C,
        .tk = id,
        .sign = tdc->neg,
    };

    if (nt->sign)
        nt->u.c.v = tdc->u.v;
    else
        nt->u.c.uv = tdc->u.uv;

    for (size_t i = 0; i < p->block; i++)
        fputc('\t', stderr);

    fprintf(stderr, "\tadding constant %s to ", id->s);

    if (tdc->neg)
        fprintf(stderr, "%lld", tdc->u.v);
    else
        fprintf(stderr, "%llu", tdc->u.uv);

    fputc('\n', stderr);

    if (bump(id, &td->types[td->ntypes++], p))
        return -1;

    reset(p);
    return 1;
}

static int p(const struct lex *l, struct prv *p)
{
    struct fn *fn = fn_cur(p), *pt = NULL;
    struct td *td = fn->td;
    size_t n = td->ntypes + 1;
    struct type *types;

    if (!(pt = malloc(sizeof *pt)))
    {
        perror("malloc(3)");
        goto failure;
    }

    *pt = (struct fn){.tk = p->stk, .parent = fn};

    if (!(types = realloc(td->types, n * sizeof *types)))
    {
        perror("realloc(3)");
        goto failure;
    }

    types[td->ntypes++] = (struct type)
    {
        .type = P,
        .tk = p->stk,
        .sz = sizeof (void *),
        .align = sizeof (void *),
        .u.p.fn = pt
    };

    td->types = types;
    p->proto = pt;
    fprintf(stderr, "\tadding prototype %s\n", p->stk->s);
    return div_p(l, p);

failure:
    free(pt);
    return -1;
}

static int ends(struct type *t)
{
    const struct s *s = &t->u.s;
    const struct storage *st = s->storage;
    const struct stentry *e;

    if (!st->nentries)
    {
        errloc(t->tk, "struct \"%s\" has no members", t->tk->s);
        return -1;
    }

    e = &st->entries[st->nentries - 1];
    t->align = st->entries->t->align;
    t->sz = e->offset + e->t->sz;
    t->sz += t->align - (t->sz % t->align);
    fprintf(stderr, "\tadding struct %s, size %zu, align %zu\n", t->tk->s,
        t->sz, t->align);
    return 0;
}

static int endu(struct type *t)
{
    const struct u *u = &t->u.u;

    if (!u->storage->nentries)
    {
        errloc(t->tk, "union \"%s\" has no members", t->tk->s);
        return -1;
    }

    return 0;
}

static int end(const struct lex *l, struct prv *p)
{
    const struct tk *tk = p->tk - 1;
    struct type *t;

    if (!p->block)
    {
        errloc(tk, "unexpected \"end\" statement");
        return -1;
    }
    else if (!(t = p->ag))
    {
        errloc(tk, "unreachable");
        return -1;
    }

    switch (t->type)
    {
    case U:
        if (endu(t))
            return -1;

        break;

    case S:
        if (ends(t))
            return -1;

        break;

    default:
        errloc(tk, "unreachable");
        return -1;
    }

    p->ag = NULL;
    p->block--;
    reset(p);
    return 1;
}

static int s(const struct lex *l, struct prv *p)
{
    const struct tk *id = p->stk;
    struct fn *fn = fn_cur(p);
    struct td *td = fn->td;
    size_t nt = td->ntypes + 1;
    struct type *types, *newt;
    const struct type *t = type_find(fn, id->s);

    if (t)
    {
        const struct loc *loc = &t->tk->loc;

        errloc(id, "type \"%s\" already defined at %s:%d:%d", id->s, loc->f,
            loc->line, loc->col);
        return -1;
    }
    else if (!(types = realloc(td->types, nt * sizeof *types)))
    {
        perror("realloc(3)");
        return -1;
    }

    td->types = types;
    newt = &td->types[td->ntypes++];
    *newt = (struct type)
    {
        .type = S,
        .tk = id
    };

    p->ag = newt;
    p->block++;
    return storage(l, p, id->s, 0, &newt->u.s.storage);
}

static int u(const struct lex *l, struct prv *p)
{
    fprintf(stderr, "%s: TODO\n", __func__);
    return -1;
}

static int tt(const struct lex *l, struct prv *p, const struct type *t)
{
    int ret = -1;
    const struct fn *fn = fn_cur(p);
    const struct tk *id = p->id;
    const struct type *tp = type_ufind(fn, id->s);
    struct td *td = fn->td;
    size_t n = td->ntypes + 1;
    struct type *types, *nt;
    char *s = type_name(t);

    if (tp)
    {
        const struct loc *loc = &tp->tk->loc;

        errloc(id, "type \"%s\" already defined at %s:%d:%d", id->s, loc->f,
            loc->line, loc->col);
        goto end;
    }
    else if (pop(l, p))
        return -1;
    else if (!(types = realloc(td->types, n * sizeof *types)))
    {
        perror("realloc(3)");
        goto end;
    }

    td->types = types;
    nt = &td->types[td->ntypes++];
    *nt = (struct type)
    {
        .type = T,
        .tk = id,
        .u.t.alias = t
    };

    fprintf(stderr, "\tadding %s as alias for %s\n", id->s, s);
    reset(p);
    p->id = NULL;
    ret = 1;
end:
    free(s);
    return ret;
}

static int a(const struct lex *l, struct prv *p)
{
    p->id = p->stk;
    return type(l, p, tt);
}

void td_free(struct td *td)
{
    if (td)
    {
        for (size_t i = 0; i < td->ntypes; i++)
            type_free(&td->types[i]);

        free(td->types);
    }

    free(td);
}

int td(const struct lex *l, struct prv *p)
{
    const struct tk *tk = p->stk;
    struct fn *fn = fn_cur(p);
    struct td *td = NULL, *prev = fn->td;
    struct tdconstant *tdc = NULL;
    struct pos init =
    {
        .seq = seqs,
        .stseq = seqs,
        .step = seqs->steps
    };

    if (prev)
    {
        const struct loc *loc = &prev->tk->loc;

        errloc(tk, "types section already defined at %s:%d:%d", loc->f,
            loc->line, loc->col);
        goto failure;
    }
    else if (!(td = malloc(sizeof *td))
        || !(tdc = malloc(sizeof *tdc)))
    {
        perror("malloc(3)");
        goto failure;
    }
    else if (push(&init, p))
        goto failure;

    *tdc = (struct tdconstant){0};
    *td = (struct td){.tk = tk};
    fn->td = td;
    p->tdc = tdc;
    p->stk = p->tk;
    return 1;

failure:
    free(tdc);
    free(td);
    return -1;
}