ref: e5e0b99538110a36bdc8f15176cf8849e9764b86
parent: 80065adc8365fd25c831018b6a5a5405cdce95ad
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Fri Mar 13 18:24:57 EDT 2015
Simplify main loop in cc2 All the operations are performed over the current function, so it is stupid to pass all the time the pointer to the beginning of the function.
--- a/cc2/cc2.h
+++ b/cc2/cc2.h
@@ -119,16 +119,18 @@
extern Type Funct, l_int8, l_int16, l_int32, l_int64,
l_uint8, l_uint16, l_uint32, l_uint64;
+extern Symbol *curfun;
+
/* main.c */
extern void error(unsigned nerror, ...);
/* cgen.c */
-extern Node *genaddable(Node *np);
-extern void generate(Symbol *fun);
-extern void apply(Node *list[], Node *(*fun)(Node *));
+extern void addable(void);
+extern void generate(void);
+extern void apply(Node *(*fun)(Node *));
/* parser.c */
-extern Symbol *parse(void);
+extern void parse(void);
extern void prtree(Node *np);
/* code.c */
@@ -136,5 +138,5 @@
extern void writeout(void);
/* optm.c */
-extern Node *optimize(Node *np);
+extern void optimize(void);
extern Node *imm(TINT i, Type *tp);
--- a/cc2/cgen.c
+++ b/cc2/cgen.c
@@ -260,20 +260,20 @@
}
void
-generate(Symbol *fun)
+generate(void)
{
extern char odebug;
- char frame = fun->u.f.locals != 0 || odebug;
+ char frame = curfun->u.f.locals != 0 || odebug;
if (frame) {
code(PUSH, NULL, regs[IX]);
code(MOV, regs[IX], regs[SP]);
- code(MOV, regs[HL], imm(-fun->u.f.locals, &l_int16));
+ code(MOV, regs[HL], imm(-curfun->u.f.locals, &l_int16));
code(ADD, regs[HL], regs[SP]);
code(MOV, regs[SP], regs[HL]);
}
- apply(fun->u.f.body, applycgen);
+ apply(applycgen);
if (frame) {
code(MOV, regs[SP], regs[IX]);
@@ -339,4 +339,10 @@
if (np->complex == 0)
++np->complex;
return np;
+}
+
+void
+addable(void)
+{
+ apply(genaddable);
}
--- a/cc2/main.c
+++ b/cc2/main.c
@@ -25,15 +25,29 @@
exit(EXIT_FAILURE);
}
+bool
+moreinput(void)
+{
+ int c;
+
+repeat:
+ if (feof(stdin))
+ return 0;
+ if ((c = getchar()) == '\n' || c == EOF)
+ goto repeat;
+ ungetc(c, stdin);
+ return 1;
+}
+
int
main(void)
{
- Symbol *fun;
-
- while (!feof(stdin) && (fun = parse())) {
- apply(fun->u.f.body, optimize);
- apply(fun->u.f.body, genaddable);
- generate(fun);
+ while (moreinput()) {
+ parse();
+ optimize();
+ addable();
+ generate();
writeout();
}
+ return 0;
}
--- a/cc2/optm.c
+++ b/cc2/optm.c
@@ -37,9 +37,15 @@
return np;
}
-Node *
-optimize(Node *np)
+static Node *
+opt(Node *np)
{
np = optcasts(np, &np->type);
return np;
+}
+
+void
+optimize(void)
+{
+ apply(opt);
}
--- a/cc2/parser.c
+++ b/cc2/parser.c
@@ -163,11 +163,11 @@
}
void
-apply(Node *list[], Node *(*fun)(Node *))
+apply(Node *(*fun)(Node *))
{
- Node *np;
+ Node **list, *np;
- while (np = *list)
+ for (list = curfun->u.f.body; np = *list; ++list)
*list++ = (*fun)(np);
}
@@ -503,7 +503,7 @@
sym->u.v.off = 1-curfun->u.f.locals;
}
-Symbol *
+void
parse(void)
{
void (*fun)(char *tok);
@@ -537,17 +537,8 @@
fun = globdcl;
break;
case '}':
- if (!curfun)
- error(ESYNTAX);
- return curfun;
- case EOF:
- if (ferror(stdin))
- error(EFERROR, strerror(errno));
if (curfun)
- goto syntax_error;
- return NULL;
- case '\n':
- continue;
+ return;
default:
goto syntax_error;
}
@@ -562,11 +553,6 @@
(*fun)(strtok(line, "\t"));
}
-found_eof:
-
-
- if (!curfun)
- return curfun;
syntax_error:
error(ESYNTAX);
}