aboutsummaryrefslogtreecommitdiff
path: root/src/prc1/cgen/global.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/prc1/cgen/global.c')
-rw-r--r--src/prc1/cgen/global.c32
1 files changed, 32 insertions, 0 deletions
diff --git a/src/prc1/cgen/global.c b/src/prc1/cgen/global.c
new file mode 100644
index 0000000..6371b96
--- /dev/null
+++ b/src/prc1/cgen/global.c
@@ -0,0 +1,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;
+}