scc

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

commit e839d44a1d0bf83eccfbac6e9c333569ec7be0d0
parent b69d387190663d6ca1b7f92daa1fa342b589a21e
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Mon,  9 May 2016 10:07:39 +0200

[cc2] Do not move curstmt in addstmt()

Addstmt() was moving the pointer to the current statement because
it was useful in parser.c, but in the majority of cases we don't want
such behaviour, and to solve the problem we had to add prevstmt().

Diffstat:
Mcc2/arch/qbe/cgen.c | 3+--
Mcc2/cc2.h | 6++++--
Mcc2/node.c | 18++++++++----------
Mcc2/parser.c | 4++--
4 files changed, 15 insertions(+), 16 deletions(-)

diff --git a/cc2/arch/qbe/cgen.c b/cc2/arch/qbe/cgen.c @@ -230,8 +230,7 @@ cgen(Node *np) if (np->next == NULL) { Node *tmp = newnode(); tmp->op = ORET; - addstmt(tmp); - prevstmt(); + addstmt(tmp, KEEPCUR); } } l = cgen(np->left); diff --git a/cc2/cc2.h b/cc2/cc2.h @@ -204,13 +204,15 @@ extern void defvar(Symbol *), defpar(Symbol *), defglobal(Symbol *); extern void setlabel(Symbol *sym); /* node.c */ +#define SETCUR 1 +#define KEEPCUR 0 extern void apply(Node *(*fun)(Node *)); extern void cleannodes(void); extern void delnode(Node *np); extern void deltree(Node *np); extern Node *newnode(void); -extern Node *addstmt(Node *np); -extern Node *prevstmt(void), *nextstmt(void); +extern Node *addstmt(Node *np, int flags); +extern Node *nextstmt(void); /* symbol.c */ diff --git a/cc2/node.c b/cc2/node.c @@ -46,15 +46,19 @@ newnode(void) } Node * -addstmt(Node *np) +addstmt(Node *np, int flag) { + if (curstmt) + np->next = curstmt->next; + np->prev = curstmt; + if (!curfun->u.stmt) curfun->u.stmt = np; else curstmt->next = np; - np->next = NULL; - np->prev = curstmt; - curstmt = np; + + if (flag == SETCUR) + curstmt = np; return np; } @@ -83,12 +87,6 @@ nextstmt(void) return curstmt = curstmt->next; } -Node * -prevstmt(void) -{ - return curstmt = curstmt->prev; -} - void delnode(Node *np) { diff --git a/cc2/parser.c b/cc2/parser.c @@ -579,7 +579,7 @@ labeldcl(void) sym->kind = SLABEL; sym->u.stmt = np; np->label = sym; - addstmt(np); + addstmt(np, SETCUR); } static void @@ -593,7 +593,7 @@ stmt(void) deltree(np); return; } - addstmt(np); + addstmt(np, SETCUR); } static void