commit e5e0b99538110a36bdc8f15176cf8849e9764b86
parent 80065adc8365fd25c831018b6a5a5405cdce95ad
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Fri, 13 Mar 2015 22:24:57 +0000
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.
Diffstat:
5 files changed, 50 insertions(+), 36 deletions(-)
diff --git a/cc2/cc2.h b/cc2/cc2.h
@@ -119,16 +119,18 @@ enum {
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 code(uint8_t op, Node *to, Node *from);
extern void writeout(void);
/* optm.c */
-extern Node *optimize(Node *np);
+extern void optimize(void);
extern Node *imm(TINT i, Type *tp);
diff --git a/cc2/cgen.c b/cc2/cgen.c
@@ -260,20 +260,20 @@ applycgen(Node *np)
}
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]);
@@ -340,3 +340,9 @@ genaddable(Node *np)
++np->complex;
return np;
}
+
+void
+addable(void)
+{
+ apply(genaddable);
+}
diff --git a/cc2/main.c b/cc2/main.c
@@ -25,15 +25,29 @@ error(unsigned nerror, ...)
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;
}
diff --git a/cc2/optm.c b/cc2/optm.c
@@ -37,9 +37,15 @@ repeat:
return np;
}
-Node *
-optimize(Node *np)
+static Node *
+opt(Node *np)
{
np = optcasts(np, &np->type);
return np;
}
+
+void
+optimize(void)
+{
+ apply(opt);
+}
diff --git a/cc2/parser.c b/cc2/parser.c
@@ -163,11 +163,11 @@ prtree(Node *np)
}
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 @@ localdcl(char *token)
sym->u.v.off = 1-curfun->u.f.locals;
}
-Symbol *
+void
parse(void)
{
void (*fun)(char *tok);
@@ -537,17 +537,8 @@ parse(void)
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 @@ parse(void)
(*fun)(strtok(line, "\t"));
}
-found_eof:
-
-
- if (!curfun)
- return curfun;
syntax_error:
error(ESYNTAX);
}