diff options
| author | Xavier Del Campo Romero <xavi92@disroot.org> | 2026-09-05 17:14:53 +0200 |
|---|---|---|
| committer | Xavier Del Campo Romero <xavi92@disroot.org> | 2026-09-26 03:41:55 +0200 |
| commit | 3c0819236e4f07c8f57aaf1ca46d0d68aa761558 (patch) | |
| tree | bd8b57cf5420cabc4ec29815052df545b55026f9 /cgen.c | |
| parent | a3f56c76444fee090e62a59f257ed5f60b4e552b (diff) | |
| download | prc-3c0819236e4f07c8f57aaf1ca46d0d68aa761558.tar.gz | |
Move source to src/prc1
Diffstat (limited to 'cgen.c')
| -rw-r--r-- | cgen.c | 195 |
1 files changed, 0 insertions, 195 deletions
@@ -1,195 +0,0 @@ -#include "cgen.h" -#include "fn.h" -#include "lex.h" -#include "lit.h" -#include "parse.h" -#include "storage.h" -#include <stddef.h> -#include <stdlib.h> -#include <string.h> - -void cgen_free(struct cgen *c) -{ -} - -const char *cgen_sz(const size_t sz) -{ - switch (sz) - { - case 1: - return "b"; - case 2: - return "h"; - case 4: - return "w"; - case 8: - return "l"; - default: - break; - } - - return NULL; -} - -const char *cgen_tmp(const size_t sz) -{ - switch (sz) - { - case 1: - return "vb"; - case 2: - return "vh"; - case 4: - return "vw"; - case 8: - return "vl"; - default: - break; - } - - return "v"; -} - -const char *cgen_type(const struct type *t) -{ - switch (t->sz) - { - case 1: - return t->sign ? "b" : "ub"; - case 2: - return t->sign ? "h" : "uh"; - case 4: - return t->sign ? "w" : "uw"; - case 8: - return t->sign ? "l" : "ul"; - - default: - break; - } - - return NULL; -} - -const char *cgen_abity(const struct type *t) -{ - switch (t->sz) - { - case 1: - return t->sign ? "b" : "ub"; - case 2: - return t->sign ? "h" : "uh"; - case 4: - return "w"; - case 8: - return "l"; - - default: - break; - } - - return NULL; -} - -const char *cgen_load(const struct type *t) -{ - switch (t->sz) - { - case 1: - return t->sign ? "sb" : "ub"; - case 2: - return t->sign ? "sh" : "uh"; - case 4: - return t->sign ? "sw" : "uw"; - case 8: - return "l"; - - default: - break; - } - - return NULL; -} - -int cgen_param(const struct fn *fn, const struct stentry *e) -{ - const struct pr *pr = fn->pr; - const char *name = e->tk->s; - - for (size_t i = 0; i < pr->nparams; i++) - if (!strcmp(pr->params[i].tk->s, name)) - return 1; - - return 0; -} - -void cgen_printvar(const struct fn *fn, const struct stentry *e) -{ - const char *name = e->tk->s; - - if (cgen_global(fn, e)) - printf("$%s", name); - else - printf("%%%s_", name); -} - -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; -} - -static void pushfmt(const char *type, const char *fmt) -{ - printf("data $____fmt%s = { b \"%s\" , b 0 }\n", type, fmt); -} - -int cgen(const struct ast *ast, struct cgen *c) -{ - /* generate literals to use with printf(3) */ - pushfmt("b", "%hhd"); - pushfmt("h", "%hd"); - pushfmt("w", "%d"); - pushfmt("l", "%ld"); - pushfmt("ub", "%hhu"); - pushfmt("uh", "%hu"); - pushfmt("uw", "%u"); - pushfmt("ul", "%lu"); - pushfmt("bx", "%#hhx"); - pushfmt("hx", "%#hx"); - pushfmt("wx", "%#x"); - pushfmt("lx", "%#lx"); - - for (const struct lit *l = ast->lits; l; l = l->next) - if (lit_cgen(l)) - return-1; - - for (size_t i = 0; i < ast->nfns; i++) - if (fn_cgen((c->fn = &ast->fns[i]), c)) - return -1; - - return 0; -} |
