aboutsummaryrefslogtreecommitdiff
path: root/type.c
blob: c7e43d0886efd9fa34f86c0ac73e1d02175e6582 (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
#include "type.h"
#include "errloc.h"
#include "fn.h"
#include "lex.h"
#include "parse.h"
#include "prv.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static int aetc(const struct lex *l, struct prv *p);
static int aid(const struct lex *l, struct prv *p);
static int anum(const struct lex *l, struct prv *p);
static int p(const struct lex *l, struct prv *p);
static int t(const struct lex *l, struct prv *p);

static const struct seq seqs[] =
{
    {(struct step[])
        {{ID, "array"}, {ID, "of"}, {ID, "etc"}, {0}}, .fn = aetc},
    {(struct step[]){{ID, "array"}, {ID, "of"}, {NUM}, {0}}, .fn = anum},
    {(struct step[]){{ID, "array"}, {ID, "of"}, {ID}, {0}}, .fn = aid},
    {(struct step[]){{ID, "pointer"}, {ID, "to"}, {0}}, .fn = p},
    {(struct step[]){{ID, "void"}, {0}}, .fn = t},
    {(struct step[]){{ID, "byte"}, {0}}, .fn = t},
    {(struct step[]){{ID, "halfword"}, {0}}, .fn = t},
    {(struct step[]){{ID, "word"}, {0}}, .fn = t},
    {(struct step[]){{ID, "long"}, {0}}, .fn = t},
    {(struct step[]){{ID, "ubyte"}, {0}}, .fn = t},
    {(struct step[]){{ID, "uhalfword"}, {0}}, .fn = t},
    {(struct step[]){{ID, "uword"}, {0}}, .fn = t},
    {(struct step[]){{ID, "ulong"}, {0}}, .fn = t},
    {(struct step[]){{ID}, {0}}, .fn = t},
    {0}
};

void type_free(struct type *t)
{
    switch (t->type)
    {
    case U:
    {
        struct u *u = &t->u.u;

        storage_free(u->storage);
    }
        break;

    case S:
    {
        struct s *s = &t->u.s;

        storage_free(s->storage);
    }
        break;

    case C:
        break;

    case P:
    {
        struct p *p = &t->u.p;

        fn_free(p->fn);
        free(p->fn);
    }
        break;

    case T:
    case ARY:
    case PTR:
    case BUILTIN:
    break;
    }
}

void type_lfree(struct type *t)
{
    if (t->child)
        type_lfree(t->child);

    free(t);
}

static int append(const char *s, char **out)
{
    size_t n = *out ? strlen(*out) : 0;
    char *ns = realloc(*out, strlen(s) + n + 1);

    if (!ns)
    {
        perror("realloc(3)");
        return -1;
    }

    strcpy(ns + n, s);
    *out = ns;
    return 0;
}

char *type_name(const struct type *t)
{
    char *ret = NULL;

    while (t)
    {
        int n = -1;

        switch (t->type)
        {
        case BUILTIN:
            n = append(t->u.b.name, &ret);
            break;

        case ARY:
        {
            const struct ary *ary = &t->u.ary;
            char s[sizeof "18446744073709551615 "];

            if (ary->etc)
                snprintf(s, sizeof s, "etc ");
            else
                snprintf(s, sizeof s, "%llu ", t->u.ary.n);

            n = append("array of ", &ret) || append(s, &ret);
            break;
        }

        case PTR:
            n = append("pointer to ", &ret);
            break;

        case U:
        case S:
        case C:
        case P:
        case T:
            n = append(t->tk->s, &ret);
            break;
        }

        if (n)
        {
            free(ret);
            return NULL;
        }

        t = t->child;
    }

    return ret;
}

const struct type *type_ufind(const struct fn *fn, const char *s)
{
    static const struct type builtins[] =
    {
        {.type = BUILTIN, .u.b.name = "void"},
        {.type = BUILTIN, .u.b.name = "byte", .sz = 1, .sign = 1, .align = 1},
        {.type = BUILTIN, .u.b.name = "halfword", .sz = 2, .sign = 1, .align = 2},
        {.type = BUILTIN, .u.b.name = "word", .sz = 4, .sign = 1, .align = 4},
        {.type = BUILTIN, .u.b.name = "long", .sz = 8, .sign = 1, .align = 8},
        {.type = BUILTIN, .u.b.name = "ubyte", .sz = 1, .align = 1},
        {.type = BUILTIN, .u.b.name = "uhalfword", .sz = 2, .align = 2},
        {.type = BUILTIN, .u.b.name = "uword", .sz = 4, .align = 4},
        {.type = BUILTIN, .u.b.name = "ulong", .sz = 8, .align = 8}
    };

    const struct td *td = fn->td;

    if (fn->parent)
    {
        const struct type *t = type_find(fn->parent, s);

        if (t)
            return t;
    }

    for (size_t i = 0; i < sizeof builtins / sizeof *builtins; i++)
    {
        const struct type *t = &builtins[i];

        if (!strcmp(t->u.b.name, s))
            return t;
    }

    if (td)
        for (size_t i = 0; i < td->ntypes; i++)
        {
            const struct type *t = &td->types[i];

            if (!strcmp(t->tk->s, s))
                return t;
        }

    for (size_t i = 0; i < fn->nimps; i++)
    {
        const struct type *t = type_ufind(fn->imps[i].ast.fns, s);

        if (t)
            return t;
    }

    return NULL;
}

const struct type *type_find(const struct fn *fn, const char *s)
{
    const struct type *t = type_ufind(fn, s);

    if (t && t->type == T)
        return t->u.t.alias;

    return t;
}

static int queue(struct prv *p, struct type *t)
{
    struct type *tt;
    struct pos *pos = &p->pos[p->i];

    if (!(tt = p->t))
        p->t = t;
    else
    {
        while (tt->child)
            tt = tt->child;

        tt->child = t;
    }

    pos->seq = pos->stseq;
    pos->step = pos->seq->steps;
    p->stk = p->tk;
    return 1;
}

static int aetc(const struct lex *l, struct prv *p)
{
    struct type *t;
    const struct type *tt = p->t;

    while (tt)
    {
        if (tt->type == ARY && tt->u.ary.etc)
        {
            errloc(p->stk, "multi-dimensional arrays "
                "with unknown size are not allowed");
            return -1;
        }

        tt = tt->next;
    }

    if (!(t = malloc(sizeof *t)))
    {
        perror("malloc");
        return -1;
    }

    *t = (struct type){.type = ARY, .u.ary.etc = 1};
    return queue(p, t);
}

static int aid(const struct lex *l, struct prv *p)
{
    const struct tk *qty = p->stk + 2;
    const struct type *ct = type_find(fn_cur(p), qty->s);
    struct type *t;
    unsigned long long v;

    if (!ct)
    {
        errloc(qty, "undefined reference to type \"%s\"", qty->s);
        return -1;
    }
    else if (ct->type != C)
    {
        errloc(qty, "type \"%s\" not a constant", qty->s);
        return -1;
    }
    else if (ct->sign)
    {
        errloc(qty, "type \"%s\" defines a negative constant (%lld)",
            qty->s, ct->u.c.v);
        return -1;
    }
    else if (!(v = ct->u.c.uv))
    {
        errloc(qty, "constant \"%s\" equals zero and cannot be used "
            "for array sizes", qty->s);
        return -1;
    }
    else if (!(t = malloc(sizeof *t)))
    {
        perror("malloc");
        return -1;
    }

    *t = (struct type){.type = ARY, .u.ary.n = v};
    return queue(p, t);
}

static int anum(const struct lex *l, struct prv *p)
{
    const struct tk *qty = p->stk + 2;
    unsigned long long n = strtoull(qty->s, NULL, 0);
    struct type *t;

    if (*qty->s == '-' || errno)
    {
        errloc(qty, "invalid negative size for array");
        return -1;
    }
    else if (!n)
    {
        errloc(qty, "array size cannot be zero");
        return -1;
    }
    else if (!(t = malloc(sizeof *t)))
    {
        perror("malloc");
        return -1;
    }

    *t = (struct type){.type = ARY, .u.ary.n = n};
    return queue(p, t);
}

static int p(const struct lex *l, struct prv *p)
{
    struct type *t = malloc(sizeof *t), *tt;

    if (!t)
    {
        perror("malloc");
        return -1;
    }

    *t = (struct type)
    {
        .type = PTR,
        .sz = sizeof (void *),
        .align = sizeof (void *)
    };

    if ((tt = p->t) && tt->type == ARY)
    {
        tt->sz = tt->u.ary.n * sizeof (void *);
        tt->align = sizeof (void *);
    }

    return queue(p, t);
}

static int aryeq(const struct type *a, const struct type *b)
{
    const struct ary *aa = &a->u.ary, *ab = &b->u.ary;

    return !memcmp(aa, ab, sizeof *aa);
}

static int eq(const struct type *a, const struct type *b)
{
    for (; a && b; a = a->child, b = b->child)
        if (a->type != b->type)
            return 0;
        else if (a->type == ARY && !aryeq(a, b))
            return 0;

    return !a && !b;
}

static const struct type *insert(struct prv *p)
{
    struct ast *ast = p->ast;
    struct type *a = p->t, *tt = ast->types;

    for (const struct type *b = ast->types; b; b = b->next)
        if (eq(a, b))
        {
            type_lfree(a);
            return b;
        }

    if (!ast->types)
        ast->types = a;
    else
    {
        while (tt->next)
            tt = tt->next;

        tt->next = a;
    }

    return a;
}

static int t(const struct lex *l, struct prv *p)
{
    const struct fn *fn = fn_cur(p);
    const struct tk *tk = p->stk;
    const struct type *t = type_find(fn, tk->s);
    struct type *tt = p->t;

    if (!t)
    {
        errloc(tk, "undefined reference to type \"%s\"", tk->s);
        return -1;
    }
    else if (tt)
    {
        struct type *nt = malloc(sizeof *nt);

        if (!nt)
        {
            perror("malloc(3)");
            return -1;
        }

        *nt = *t;

        while (tt->child)
            tt = tt->child;

        tt->child = nt;
        tt = p->t;

        /* array size still undecided */
        if (tt->type == ARY && !tt->sz)
        {
            tt->sz = tt->u.ary.n * nt->sz;
            tt->align = t->align;
        }

        if (!(t = insert(p)))
            return -1;

        p->t = NULL;
    }

    return p->type(l, p, t);
}

int type(const struct lex *l, struct prv *p,
    int (*fn)(const struct lex *, struct prv *, const struct type *))
{
    struct pos init =
    {
        .seq = seqs,
        .stseq = seqs,
        .step = seqs->steps
    };

    p->stk = p->tk;
    p->type = fn;
    return push(&init, p) ? -1 : 1;
}