scc

simple C compiler
git clone git://git.2f30.org/scc
Log | Files | Refs | README | LICENSE

commit b180e15d2a5fb72ee3aa45a5cf6a3e27a016aed0
parent b584afc229761ee1e36f523ea33e717ea0ef2586
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Fri, 20 Mar 2015 11:48:38 -0400

Add initial support for return statements

This is only a stab, because it doesn't do anything with the
statement.

Diffstat:
Mcc2/cc2.h | 1+
Mcc2/cgen.c | 8+++++++-
Mcc2/parser.c | 39+++++++++++++++++++++++++++++++++------
3 files changed, 41 insertions(+), 7 deletions(-)

diff --git a/cc2/cc2.h b/cc2/cc2.h @@ -36,6 +36,7 @@ #define ONEG '_' #define OCPL '~' #define OCOMMA ',' +#define ORET 'y' #define ADDABLE 10 diff --git a/cc2/cgen.c b/cc2/cgen.c @@ -359,10 +359,16 @@ assign(Node *np) np->reg = rp->reg; } +static void +ret(Node *np) +{ +} + static void (*opnodes[])(Node *) = { [OADD] = add, [OSUB] = add, - [OASSIG] = assign + [OASSIG] = assign, + [ORET] = ret }; static void diff --git a/cc2/parser.c b/cc2/parser.c @@ -90,7 +90,7 @@ static Type l_uint64 = { static void cast(char *), operator(char *), assignment(char *), increment(char *), globvar(char *), localvar(char *), paramvar(char *), label(char *), - immediate(char *), unary(char *); + immediate(char *), unary(char *), oreturn(char *); /*TODO: Remove hardcoded symbols */ @@ -137,7 +137,7 @@ static void (*optbl[])(char *) = { ['['] = operator, ['='] = operator, ['!'] = unary, - ['y'] = NULL, + ['y'] = oreturn, ['j'] = NULL, ['o'] = operator, ['_'] = unary, @@ -421,20 +421,30 @@ cast(char *token) } static void -expression(char *token) +expr(char *token) { Node *np; void (*fun)(char *); unsigned c; - if (!curfun) - error(ESYNTAX); - do { if ((c = token[0]) > 0x7f || (fun = optbl[c]) == NULL) error(ESYNTAX); (*fun)(token); } while (token = strtok(NULL, "\t")); +} + +static void +expression(char *token) +{ + Node *np; + void (*fun)(char *); + unsigned c; + + if (!curfun) + error(ESYNTAX); + + expr(token); np = pop(); if (stackp != stack) @@ -445,6 +455,23 @@ expression(char *token) } static void +oreturn(char *token) +{ + Node *np = newnode(); + + np->op = token[0]; + + if (token = strtok(NULL, "\t")) { + expr(token); + np -> left = pop(); + } else { + np->left = NULL; + } + np->right = NULL; + push(np); +} + +static void deflabel(char *token) { Symbol *sym;