scc

Simple C99 Compiler
Log | Files | Refs | README | LICENSE

commit 0c62dba5426ebaeaba6bef15f70862c4e869c767
parent 3af0039562d8ff0baaddc57d9caa36fe10b2ba16
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Thu,  2 Jun 2016 09:01:39 +0200

[cc2-qbe] Add parameters to function calls

This patch adds the parameters to the function calls, although
it is not perfect yet, because it does not pass the type of the
parameter to the call, but it is a big step to have the
implementation of functions in qbe.

Diffstat:
cc2/arch/qbe/arch.h | 2++
cc2/arch/qbe/cgen.c | 9+++++++--
cc2/arch/qbe/code.c | 14+++++++++++---
3 files changed, 20 insertions(+), 5 deletions(-)

diff --git a/cc2/arch/qbe/arch.h b/cc2/arch/qbe/arch.h @@ -139,4 +139,6 @@ enum asmop { ASCALLL, ASCALLD, ASCALL, + ASPAR, + ASPARE, }; diff --git a/cc2/arch/qbe/cgen.c b/cc2/arch/qbe/cgen.c @@ -237,10 +237,10 @@ call(Node *np) { int n, op; Type *tp = &np->type; - Node *tmp, *p, *pars[NR_FUNPARAM]; + Node **q, *tmp, *p, *pars[NR_FUNPARAM]; for (n = 0, p = np->right; p; p = p->right) - pars[n] = cgen(p->left); + pars[n++] = cgen(p->left); switch (tp->size) { case 0: @@ -263,6 +263,11 @@ call(Node *np) abort(); } code(op, tmpnode(np), np->left, NULL); + + for (q = pars; q < &pars[n]; ++q) { + op = (q == &pars[n-1]) ? ASPARE : ASPAR; + code(op, NULL, *q, NULL); + } code(ASCALL, NULL, NULL, NULL); return np; diff --git a/cc2/arch/qbe/code.c b/cc2/arch/qbe/code.c @@ -10,7 +10,7 @@ #define ADDR_LEN (INTIDENTSIZ+64) static void binary(void), unary(void), store(void), jmp(void), ret(void), - branch(void), call(void), ecall(void); + branch(void), call(void), ecall(void), param(void); static struct opdata { void (*fun)(void); @@ -133,6 +133,8 @@ static struct opdata { [ASCALLL] = {.fun = call, .letter = 'l'}, [ASCALLD] = {.fun = call, .letter = 'd'}, [ASCALL] = {.fun = ecall}, + [ASPAR] = {.fun = param, .txt = "\t\t%s,\n"}, + [ASPARE] = {.fun = param, .txt = "\t\t%s\n"}, }; static char buff[ADDR_LEN]; @@ -413,13 +415,19 @@ call(void) strcpy(to, addr2txt(&pc->to)); strcpy(from, addr2txt(&pc->from1)); - printf("\t%s =%c\tcall\t%s(", to, p->letter, from); + printf("\t%s =%c\tcall\t%s(\n", to, p->letter, from); +} + +static void +param(void) +{ + printf(optbl[pc->op].txt, addr2txt(&pc->from1)); } static void ecall(void) { - puts(")"); + puts("\t\t)"); } static void