ref: 1b4e19b40cb3bbd1e85e4e612174c029cb82a0a1
parent: 79059d14d5c6a36a81e74697aabdab5901c59b5c
parent: 8ab588d0ba2eaa191b6b93fdb4053db29fdcf8fa
author: Roberto E. Vargas Caballero <[email protected]>
date: Mon Feb 16 17:28:02 EST 2015
Merge remote-tracking branch 'gitorius/master'
--- a/cc1/Makefile
+++ b/cc1/Makefile
@@ -2,7 +2,7 @@
OBJS = types.o decl.o lex.o error.o symbol.o main.o expr.o \
code.o stmt.o
-CPPFLAGS = -I../inc
+CPPFLAGS =
LDFLAGS = -L../lib
LIBS = -lcc
--- a/cc1/code.c
+++ b/cc1/code.c
@@ -3,7 +3,7 @@
#include <stdio.h>
#include <stdlib.h>
-#include <cc.h>
+#include "../inc/cc.h"
#include "cc1.h"
#define SYM(s) ((union unode) {.sym = s})
@@ -104,7 +104,7 @@
static void
emitconst(Node *np)
{
- register char *bp, c;
+ char *bp, c;
Symbol *sym = np->u.sym;
if (np->type == inttype) {
--- a/cc1/decl.c
+++ b/cc1/decl.c
@@ -3,8 +3,8 @@
#include <stdint.h>
#include <string.h>
-#include <sizes.h>
-#include <cc.h>
+#include "../inc/sizes.h"
+#include "../inc/cc.h"
#include "cc1.h"
#define ID_EXPECTED 1
@@ -106,7 +106,7 @@
static struct dcldata *
directdcl(struct dcldata *dp, uint8_t ns)
{
- register Symbol *sym;
+ Symbol *sym;
if (accept('(')) {
dp = declarator0(dp, ns);
@@ -131,7 +131,7 @@
static struct dcldata*
declarator0(struct dcldata *dp, uint8_t ns)
{
- register uint8_t n;
+ uint8_t n;
for (n = 0; accept('*'); ++n) {
while (accept(TQUALIFIER))
@@ -150,7 +150,7 @@
declarator(Type *tp, int8_t flags, uint8_t ns)
{
struct dcldata data[NR_DECLARATORS+2];
- register struct dcldata *bp;
+ struct dcldata *bp;
Symbol *sym;
memset(data, 0, sizeof(data));
@@ -186,7 +186,7 @@
qlf = sign = type = cls = size = 0;
for (;;) {
- register int8_t *p;
+ int8_t *p;
Type *(*dcl)(void) = NULL;
switch (yytoken) {
@@ -270,7 +270,7 @@
static Symbol *
newtag(uint8_t tag)
{
- register Symbol *sym;
+ Symbol *sym;
static uint8_t ns = NS_STRUCTS;
switch (yytoken) {
@@ -363,7 +363,7 @@
static Type *
enumdcl(void)
{
- register Type *tp;
+ Type *tp;
Symbol *sym;
int val = 0;
--- a/cc1/error.c
+++ b/cc1/error.c
@@ -4,7 +4,7 @@
#include <stdint.h>
#include <stdio.h>
-#include <cc.h>
+#include "../inc/cc.h"
#include "cc1.h"
extern unsigned linenum;
--- a/cc1/expr.c
+++ b/cc1/expr.c
@@ -2,7 +2,7 @@
#include <stdio.h>
#include <string.h>
-#include <cc.h>
+#include "../inc/cc.h"
#include "cc1.h"
static Symbol *zero, *one;
@@ -465,7 +465,7 @@
static Node *
postfix(void)
{
- register Node *np1, *np2;
+ Node *np1, *np2;
np1 = primary();
for (;;) {
@@ -511,7 +511,7 @@
static Type *
sizeexp(void)
{
- register Type *tp;
+ Type *tp;
expect('(');
switch (yytoken) {
@@ -531,8 +531,8 @@
static Node *
unary(void)
{
- register Node *(*fun)(char, Node *);
- register char op;
+ Node *(*fun)(char, Node *);
+ char op;
Type *tp;
switch (yytoken) {
@@ -560,8 +560,8 @@
static Node *
cast(void)
{
- register Node *np1, *np2;
- register Type *tp;
+ Node *np1, *np2;
+ Type *tp;
if (!accept('('))
return unary();
@@ -594,8 +594,8 @@
static Node *
mul(void)
{
- register Node *np, *(*fun)(char, Node *, Node *);
- register char op;
+ Node *np, *(*fun)(char, Node *, Node *);
+ char op;
np = cast();
for (;;) {
@@ -613,8 +613,8 @@
static Node *
add(void)
{
- register char op;
- register Node *np;
+ char op;
+ Node *np;
np = mul();
for (;;) {
@@ -631,8 +631,8 @@
static Node *
shift(void)
{
- register char op;
- register Node *np;
+ char op;
+ Node *np;
np = add();
for (;;) {
@@ -649,8 +649,8 @@
static Node *
relational(void)
{
- register char op;
- register Node *np;
+ char op;
+ Node *np;
np = shift();
for (;;) {
@@ -669,8 +669,8 @@
static Node *
eq(void)
{
- register char op;
- register Node *np;
+ char op;
+ Node *np;
np = relational();
for (;;) {
@@ -687,7 +687,7 @@
static Node *
bit_and(void)
{
- register Node *np;
+ Node *np;
np = eq();
while (accept('&'))
@@ -698,7 +698,7 @@
static Node *
bit_xor(void)
{
- register Node *np;
+ Node *np;
np = bit_and();
while (accept('^'))
@@ -709,7 +709,7 @@
static Node *
bit_or(void)
{
- register Node *np;
+ Node *np;
np = bit_xor();
while (accept('|'))
@@ -720,7 +720,7 @@
static Node *
and(void)
{
- register Node *np;
+ Node *np;
np = bit_or();
while (accept(AND))
@@ -731,7 +731,7 @@
static Node *
or(void)
{
- register Node *np;
+ Node *np;
np = and();
while (accept(OR))
@@ -758,8 +758,8 @@
static Node *
assign(void)
{
- register Node *np, *(*fun)(char , Node *, Node *);
- register char op;
+ Node *np, *(*fun)(char , Node *, Node *);
+ char op;
np = ternary();
for (;;) {
@@ -786,7 +786,7 @@
Node *
expr(void)
{
- register Node *np1, *np2;
+ Node *np1, *np2;
np1 = assign();
while (accept(',')) {
--- a/cc1/lex.c
+++ b/cc1/lex.c
@@ -5,8 +5,8 @@
#include <string.h>
#include <ctype.h>
-#include <sizes.h>
-#include <cc.h>
+#include "../inc/sizes.h"
+#include "../inc/cc.h"
#include "cc1.h"
static FILE *yyin;
@@ -58,7 +58,7 @@
static uint8_t
number(void)
{
- register char ch, *bp;
+ char ch, *bp;
static char base;
if ((ch = getc(yyin)) == '0') {
@@ -137,7 +137,7 @@
character(void)
{
static char c;
- register Symbol *sym;
+ Symbol *sym;
getc(yyin); /* discard the initial ' */
c = getc(yyin);
@@ -156,8 +156,8 @@
string(void)
{
static char buf[STRINGSIZ+1];
- register char *bp;
- register int c;
+ char *bp;
+ int c;
static Symbol *sym;
getc(yyin); /* discard the initial " */
@@ -190,9 +190,9 @@
static uint8_t
iden(void)
{
- register char *bp;
- register int c;
- register Symbol *sym;
+ char *bp;
+ int c;
+ Symbol *sym;
for (bp = yytext; bp < &yytext[IDENTSIZ]; *bp++ = c) {
if (!isalnum(c = getc(yyin)) && c != '_')
@@ -213,7 +213,7 @@
static uint8_t
follow(int expect, int ifyes, int ifno)
{
- register int c = getc(yyin);
+ int c = getc(yyin);
if (c == expect) {
yytext[1] = c;
@@ -227,7 +227,7 @@
static uint8_t
minus(void)
{
- register int c = getc(yyin);
+ int c = getc(yyin);
yytext[1] = c;
yytext[2] = '\0';
@@ -245,7 +245,7 @@
static uint8_t
plus(void)
{
- register int c = getc(yyin);
+ int c = getc(yyin);
yytext[1] = c;
yytext[2] = '\0';
@@ -262,7 +262,7 @@
static uint8_t
relational(uint8_t op, uint8_t equal, uint8_t shift, uint8_t assig)
{
- register int c = getc(yyin);
+ int c = getc(yyin);
yytext[1] = c;
yytext[2] = '\0';
@@ -279,7 +279,7 @@
static uint8_t
logic(uint8_t op, uint8_t equal, uint8_t logic)
{
- register int c = getc(yyin);
+ int c = getc(yyin);
yytext[1] = c;
yytext[2] = '\0';
@@ -313,7 +313,7 @@
static uint8_t
operator(void)
{
- register uint8_t c = getc(yyin);
+ uint8_t c = getc(yyin);
yytext[0] = c;
yytext[1] = '\0';
@@ -338,7 +338,7 @@
skipspaces(void)
{
- register int c;
+ int c;
while (isspace(c = getc(yyin))) {
if (c == '\n')
@@ -350,7 +350,7 @@
uint8_t
next(void)
{
- register int c;
+ int c;
ungetc(c = skipspaces(), yyin);
@@ -372,7 +372,7 @@
}
void
-expect(register uint8_t tok)
+expect(uint8_t tok)
{
if (yytoken != tok)
unexpected();
@@ -382,7 +382,7 @@
uint8_t
ahead(void)
{
- register int c;
+ int c;
ungetc(c = skipspaces(), yyin);
@@ -390,7 +390,7 @@
}
void
-open_file(register const char *file)
+open_file(const char *file)
{
if (yyin != NULL)
fclose(yyin);
--- a/cc1/main.c
+++ b/cc1/main.c
@@ -2,7 +2,7 @@
#include <stdint.h>
#include <stdio.h>
-#include <cc.h>
+#include "../inc/cc.h"
#include "cc1.h"
extern void init_keywords(void),
--- a/cc1/stmt.c
+++ b/cc1/stmt.c
@@ -3,7 +3,7 @@
#include <stdint.h>
#include <stdio.h>
-#include <cc.h>
+#include "../inc/cc.h"
#include "cc1.h"
struct scase {
--- a/cc1/symbol.c
+++ b/cc1/symbol.c
@@ -4,7 +4,7 @@
#include <stdlib.h>
#include <string.h>
-#include <cc.h>
+#include "../inc/cc.h"
#include "cc1.h"
#define NR_SYM_HASH 32
@@ -19,9 +19,9 @@
} symtab [NR_NAMESPACES];
static inline uint8_t
-hash(register const char *s)
+hash(const char *s)
{
- register uint8_t h, ch;
+ uint8_t h, ch;
for (h = 0; ch = *s++; h += ch)
/* nothing */;
@@ -32,7 +32,7 @@
freesyms(uint8_t ns)
{
static struct symtab *tbl;
- register Symbol *sym, *next;
+ Symbol *sym, *next;
tbl = &symtab[ns];
for (sym = tbl->head; sym; sym = next) {
@@ -69,10 +69,10 @@
}
Symbol *
-lookup(register char *s, uint8_t ns)
+lookup(char *s, uint8_t ns)
{
struct symtab *tbl;
- register Symbol *sym;
+ Symbol *sym;
tbl = &symtab[(ns > NS_STRUCTS) ? NS_STRUCTS : ns];
for (sym = tbl->htab[hash(s)]; sym; sym = sym->hash) {
@@ -86,7 +86,7 @@
Symbol *
install(char *s, uint8_t ns)
{
- register Symbol *sym, **t;
+ Symbol *sym, **t;
struct symtab *tbl;
sym = xcalloc(1, sizeof(*sym));
@@ -148,7 +148,7 @@
{"while", WHILE, WHILE},
{NULL, 0, 0},
};
- register Symbol *sym;
+ Symbol *sym;
for (bp = buff; bp->str; ++bp) {
sym = install(bp->str, NS_IDEN);
--- a/cc1/types.c
+++ b/cc1/types.c
@@ -4,8 +4,8 @@
#include <stdlib.h>
#include <string.h>
-#include <sizes.h>
-#include <cc.h>
+#include "../inc/sizes.h"
+#include "../inc/cc.h"
#include "cc1.h"
#define NR_TYPE_HASH 16
@@ -190,7 +190,7 @@
{
static Type *typetab[NR_TYPE_HASH], **tbl, type;
static uint8_t t;
- register Type *bp;
+ Type *bp;
static char letters[] = {
[PTR] = L_POINTER, [ARY] = L_ARRAY,
[FTN] = L_FUNCTION, [ENUM] = L_INT,
--- a/cc2/Makefile
+++ b/cc2/Makefile
@@ -1,7 +1,7 @@
OBJS = main.o parser.o cgen.o code.o optm.o
-CPPFLAGS = -I../inc
+CPPFLAGS =
LDFLAGS = -L../lib
LIBS = -lcc
--- a/cc2/cc2.h
+++ b/cc2/cc2.h
@@ -101,7 +101,7 @@
enum {
- PUSH, POP, LD, ADD, RET, ADDI, LDI, ADDR, ADDX, ADCX, LDX,
+ PUSH, POP, LD, ADD, RET, ADDI, LDI, ADDX, ADCX, LDX,
LDFX
};
--- a/cc2/cgen.c
+++ b/cc2/cgen.c
@@ -4,7 +4,7 @@
#include <stdint.h>
#include <stdlib.h>
-#include <cc.h>
+#include "../inc/cc.h"
#include "cc2.h"
#include <stdio.h>
@@ -196,7 +196,6 @@
extern char odebug;
char frame = fun->u.f.locals != 0 || odebug;
- code(ADDR, fun->name);
if (frame) {
code(PUSH, IX);
code(LD, IX, SP);
--- a/cc2/code.c
+++ b/cc2/code.c
@@ -5,10 +5,27 @@
#include <stdint.h>
#include <stdio.h>
-#include <cc.h>
+#include "../inc/cc.h"
#include "cc2.h"
+typedef struct inst Inst;
+typedef struct addr Addr;
+struct addr {
+ char kind;
+ union {
+ char reg;
+ Inst *pc;
+ Symbol *sym;
+ } u;
+};
+
+struct inst {
+ char op;
+ Addr from, to;
+ Inst *next;
+};
+
static char *opnames[] = {
[PUSH] = "PUSH", [POP] = "POP", [LD] = "LD", [ADD] = "ADD",
[RET] = "RET" , [ADDI]= "ADD", [LDI] = "LD", [ADDX] = "ADD",
@@ -34,36 +51,27 @@
[ADCX] = "\to\tr,(r+i)",
[LDFX] = "\to\tr,(r+i)",
[LDX] = "\to\t(r+i),r",
- [ADDR] = "a:"
};
-void
-code(char op, ...)
+Inst *prog, *pc;
+
+Inst *
+inst(uint8_t op)
{
- va_list va;
- char *cp, c;
+ Inst *new;
- va_start(va, op);
- for (cp = opfmt[op]; c = *cp; ++cp) {
- switch (c) {
- case 'o':
- fputs(opnames[op], stdout);
- break;
- case 'r':
- fputs(regnames[va_arg(va, int)], stdout);
- break;
- case 'i':
- printf("%d", va_arg(va, int));
- break;
- case 'a':
- fputs(va_arg(va, char *), stdout);
- break;
- default:
- putchar(c);
- break;
- }
- }
- putchar('\n');
+ new = xmalloc(sizeof(*new));
+ if (!pc)
+ prog = new;
+ else
+ pc->next = new;
+ pc = new;
+ pc->op = op;
+ pc->next = NULL;
+ return pc;
+}
- va_end(va);
+void
+code(char op, ...)
+{
}
--- a/cc2/main.c
+++ b/cc2/main.c
@@ -4,7 +4,7 @@
#include <stdio.h>
#include <stdlib.h>
-#include <cc.h>
+#include "../inc/cc.h"
#include "cc2.h"
#include "error.h"
--- a/cc2/optm.c
+++ b/cc2/optm.c
@@ -2,7 +2,7 @@
#include <stddef.h>
#include <stdint.h>
-#include <cc.h>
+#include "../inc/cc.h"
#include "cc2.h"
--- a/cc2/parser.c
+++ b/cc2/parser.c
@@ -5,8 +5,8 @@
#include <stdlib.h>
#include <string.h>
-#include <cc.h>
-#include <sizes.h>
+#include "../inc/cc.h"
+#include "../inc/sizes.h"
#include "cc2.h"
@@ -19,7 +19,7 @@
LOCAL, GLOBAL, PARAMETER
};
-static Symbol *curfun;
+Symbol *curfun;
static Node *stack[NR_STACKSIZ], **stackp;
static Node *listexp[NR_EXPRESSIONS], **listp;
static Node nodepool[NR_NODEPOOL], *newp;
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -1,8 +1,10 @@
OBJS = die.o xcalloc.o xmalloc.o xrealloc.o xstrdup.o
-CPPFLAGS = -I../inc
-all: libcc.a($(OBJS))
+all: libcc.a
+
+libcc.a: $(OBJS)
+ ar r $@ $?
clean:
rm -f *.o *.a
--- a/lib/die.c
+++ b/lib/die.c
@@ -3,7 +3,7 @@
#include <stdlib.h>
#include <stdio.h>
-#include <cc.h>
+#include "../inc/cc.h"
void
die(const char *fmt, ...)
--- a/lib/xcalloc.c
+++ b/lib/xcalloc.c
@@ -1,11 +1,11 @@
#include <stdlib.h>
-#include <cc.h>
+#include "../inc/cc.h"
void *
xcalloc(size_t n, size_t size)
{
- register void *p = calloc(n, size);
+ void *p = calloc(n, size);
if (!p)
die("out of memory");
--- a/lib/xmalloc.c
+++ b/lib/xmalloc.c
@@ -1,11 +1,11 @@
#include <stdlib.h>
-#include <cc.h>
+#include "../inc/cc.h"
void *
xmalloc(size_t size)
{
- register void *p = malloc(size);
+ void *p = malloc(size);
if (!p)
die("out of memory");
--- a/lib/xrealloc.c
+++ b/lib/xrealloc.c
@@ -1,11 +1,11 @@
#include <stdlib.h>
-#include <cc.h>
+#include "../inc/cc.h"
void *
-xrealloc(void *buff, register size_t size)
+xrealloc(void *buff, size_t size)
{
- register void *p = realloc(buff, size);
+ void *p = realloc(buff, size);
if (!p)
die("out of memory");
--- a/lib/xstrdup.c
+++ b/lib/xstrdup.c
@@ -1,12 +1,12 @@
#include <string.h>
-#include <cc.h>
+#include "../inc/cc.h"
char *
xstrdup(const char *s)
{
- register size_t len = strlen(s) + 1;
- register char *p = xmalloc(len);
+ size_t len = strlen(s) + 1;
+ char *p = xmalloc(len);
return memcpy(p, s, len);
}