scc

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

commit be7c5f83d4655f77d592b626a702dd2da07c3fb8
parent ee42812835eb35735b73cc2f4588a8c25326355b
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Thu, 17 Jul 2014 18:05:07 +0200

Add support for labels in cc2

Labels are the base for all the loops and jumps, so they are strictly
necessary.

Diffstat:
cc2/parser.c | 38++++++++++++++++++++++++++++++++------
1 file changed, 32 insertions(+), 6 deletions(-)

diff --git a/cc2/parser.c b/cc2/parser.c @@ -11,8 +11,15 @@ #define NR_SYMBOLS 1024 typedef struct { - char vartype; - char storage; + union { + struct { + char type; + char storage; + } v; + struct { + short addr; + } l; + } u; } Symbol; typedef struct node { @@ -96,7 +103,7 @@ variable(char op) np->op = op; np->u.id = id; - np->type = symtbl[id].vartype; + np->type = symtbl[id].u.v.type; np->left = np->right = NULL; push(np); } @@ -125,6 +132,16 @@ operator(char op) push(np); } +static void +label(char op) +{ + Node *np = newnode(); + + np->left = np->right = NULL; + np->op = 'L'; + push(np); +} + static Node * getroot(void) { @@ -142,6 +159,7 @@ static void (*optbl[])(char) = { ['A'] = variable, ['T'] = variable, ['G'] = variable, + ['L'] = label, ['#'] = immediate, ['\177'] = NULL }; @@ -171,12 +189,20 @@ declaration(char sclass) Symbol *sym = &symtbl[getid()]; getchar(); /* skip tab */ - sym->storage = sclass; - sym->vartype = gettype(); + sym->u.v.storage = sclass; + sym->u.v.type = gettype(); if (getchar() != '\n') esyntax(); } +static void +deflabel(void) +{ + Symbol *sym = &symtbl[getid()]; + + sym->u.l.addr = listp - listexp; +} + int parse(void) { @@ -188,7 +214,7 @@ parse(void) expression(); break; case 'L': - /* label */ + deflabel(); break; case 'S': /* struct */