aboutsummaryrefslogtreecommitdiff
path: root/src/prc1/from.c
blob: 8561b8f56133212fc08b73c00477157397130d1e (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
#include "from.h"
#include "dbg.h"
#include "fn.h"
#include "errloc.h"
#include "stmt.h"
#include "storage.h"
#include "tmp.h"
#include "triplet.h"
#include "type.h"
#include <errno.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void from_free(struct from *fr)
{
}

static int checksym(const struct fn *fn, const struct tk *tk,
    struct from_it *fr)
{
    const struct stentry *e = fn_var(fn, tk);

    if (!e)
    {
        errloc(tk, "undefined reference to \"%s\"", tk->s);
        return -1;
    }

    switch (e->t->type)
    {
    case C:
    case BUILTIN:
        break;
    case U:
    case S:
    case P:
    case T:
    case PTR:
    case ARY:
    {
        char *s = type_name(e->t);

        if (s)
            errloc(tk, "\"%s\" (\"%s\") cannot be used in \"from\" loop",
                tk->s, s);

        free(s);
        return -1;
    }
    }

    fr->type = FR_VAR;
    fr->u.e = e;
    return 0;
}

static int check(const struct fn *fn, const struct tk *tk, struct from_it *fr)
{
    switch (tk->type)
    {
    case NUM:
        fr->type = FR_VAL;
        fr->u.tk = tk;
        break;
    case ID:
        return checksym(fn, tk, fr);
    case LIT:
        errloc(tk, "unexpected literal");
        return -1;
    case UNDEF:
    case ANY:
        fprintf(stderr, "%s: unreachable\n", __func__);
        return -1;
    }

    return 0;
}

static int finalize(struct prv *p, struct fn *fn, struct from *fr)
{
    struct stmt *s;
    struct pos *pos = &p->pos[p->i];
    size_t nst = fn->nstmts + 1, nbl = fn->nblocks + 1,
        *bl = realloc(fn->blocks, nbl * sizeof *bl);

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

    fr->block_i = bl[fn->nblocks++] = fn->nstmts;
    fn->blocks = bl;

    if (!(s = realloc(fn->stmts, nst * sizeof *s)))
    {
        perror("realloc(3)");
        return -1;
    }

    s[fn->nstmts++] = (struct stmt)
    {
        .type = FROM,
        .u.from = *fr,
        .end = from_end
    };

    fn->stmts = s;
    pos->seq = pos->stseq = stmts;
    pos->step = stmts->steps;
    dbg(", iterator %s, block index %zu\n", fr->var->tk->s, bl[fn->nblocks - 1]);
    return 0;
}

static const struct type *valtype(const struct fn *fn, const char *tr,
    const struct tk *tk)
{
    char *end;

    errno = 0;
    strtol(tk->s, &end, 0);

    if (errno || *end)
    {
        errno = 0;
        strtoll(tk->s, &end, 0);

        if (errno || *end)
            return type_find(fn, tr, "ulong");

        return type_find(fn, tr, "long");
    }

    return type_find(fn, tr, "word");
}

static const struct type *ittype(const struct fn *fn, const char *tr,
    const struct from_it *it)
{
    switch (it->type)
    {
    case FR_VAL:
        return valtype(fn, tr, it->u.tk);
    case FR_VAR:
        return it->u.e->t;
    }

    fprintf(stderr, "%s: unreachable\n", __func__);
    return NULL;
}

static const struct type *gettype(const struct fn *fn,
    const char *tr, const struct from_it *a, const struct from_it *b)
{
    const struct type *ta = ittype(fn, tr, a), *tb = ittype(fn, tr, b);

    return ta->sz > tb->sz ? ta : tb;
}

int from(const struct lex *l, struct prv *p)
{
    const struct tk *start = p->stk + 1, *end = p->stk + 3;
    const char *tr = triplet(p);
    struct fn *fn = fn_cur(p);
    struct from fr = {0};
    const struct type *t;

    if (check(fn, start, &fr.start)
        || check(fn, end, &fr.end)
        || !(t = gettype(fn, tr, &fr.start, &fr.end))
        || !(fr.var = tmp_create(fn, t)))
        return -1;

    dbg("\t\tadd from statement [%s, %s]", start->s, end->s);

    if (finalize(p, fn, &fr))
        return -1;

    p->stk = ++end;
    return 0;
}

static int checkvar(const struct fn *fn, const struct tk *tk, struct from *fr)
{
    const struct stentry *e;

    if (tk->type != ID)
    {
        errloc(tk, "\"%s\" not a variable", tk->s);
        return -1;
    }
    else if (!(e = fn_var(fn, tk)))
    {
        errloc(tk, "undefined reference to \"%s\"", tk->s);
        return -1;
    }
    else if (e->t->type != BUILTIN)
    {
        errloc(tk, "variable \"%s\" not an integer", tk->s);
        return -1;
    }

    fr->var = e;
    return 0;
}

int from_id(const struct lex *l, struct prv *p)
{
    const struct tk *start = p->stk + 1, *end = p->stk + 3, *var = p->stk + 5;
    struct fn *fn = fn_cur(p);
    struct from fr = {0};

    if (check(fn, start, &fr.start)
        || check(fn, end, &fr.end)
        || checkvar(fn, var, &fr))
        return -1;

    dbg("\t\tadd from statement [%s, %s] using %s", start->s, end->s,
        var->s);

    if (finalize(p, fn, &fr))
        return -1;

    p->stk = ++var;
    return 0;
}

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

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