aboutsummaryrefslogtreecommitdiff
path: root/src/prc1/cgen/global.c
blob: 6371b96bb04c48dfc2a8d7d5ba0e1ee6e8dbd6a9 (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
#include "cgen.h"
#include <string.h>

int cgen_global(const struct fn *fn, const struct stentry *e)
{
    const struct storage *ws = fn->ws, *gl = fn->gl;
    const struct pr *pr = fn->pr;
    const struct param *ret = pr->ret;
    const char *name = e->tk->s;

    /* A variable is only global if it is defined inside the global
     * variables section ("gl"), or if it is not listed on the working
     * storage section ("ws"), as a function parameter or a return value. */

    if (gl)
        for (size_t i = 0; i < gl->nentries; i++)
            if (!strcmp(gl->entries[i].tk->s, name))
                return 1;

    if (cgen_param(fn, e) || (ret && !strcmp(ret->tk->s, name)))
        return 0;
    else if (ws)
        for (size_t i = 0; i < ws->nentries; i++)
            if (!strcmp(ws->entries[i].tk->s, name))
                return 0;

    for (const struct tmp *t = fn->tmps; t; t = t->next)
        if (!strcmp(t->tk.s, name))
            return 0;

    return 1;
}