scc

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

commit 1b4e19b40cb3bbd1e85e4e612174c029cb82a0a1
parent 79059d14d5c6a36a81e74697aabdab5901c59b5c
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Mon, 16 Feb 2015 22:28:02 +0100

Merge remote-tracking branch 'gitorius/master'

Diffstat:
Mcc1/Makefile | 2+-
Mcc1/code.c | 4++--
Mcc1/decl.c | 16++++++++--------
Mcc1/error.c | 2+-
Mcc1/expr.c | 50+++++++++++++++++++++++++-------------------------
Mcc1/lex.c | 40++++++++++++++++++++--------------------
Mcc1/main.c | 2+-
Mcc1/stmt.c | 2+-
Mcc1/symbol.c | 16++++++++--------
Mcc1/types.c | 6+++---
Mcc2/Makefile | 2+-
Mcc2/cc2.h | 2+-
Mcc2/cgen.c | 3+--
Mcc2/code.c | 64++++++++++++++++++++++++++++++++++++----------------------------
Mcc2/main.c | 2+-
Mcc2/optm.c | 2+-
Mcc2/parser.c | 6+++---
Mlib/Makefile | 6++++--
Mlib/die.c | 2+-
Mlib/xcalloc.c | 4++--
Mlib/xmalloc.c | 4++--
Mlib/xrealloc.c | 6+++---
Mlib/xstrdup.c | 6+++---
23 files changed, 129 insertions(+), 120 deletions(-)

diff --git 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 diff --git 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 @@ emitvar(Symbol *sym) static void emitconst(Node *np) { - register char *bp, c; + char *bp, c; Symbol *sym = np->u.sym; if (np->type == inttype) { diff --git 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 *declarator0(struct dcldata *dp, uint8_t ns); 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 @@ directdcl(struct dcldata *dp, uint8_t ns) 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 @@ static Symbol * 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 @@ specifier(int8_t *sclass) qlf = sign = type = cls = size = 0; for (;;) { - register int8_t *p; + int8_t *p; Type *(*dcl)(void) = NULL; switch (yytoken) { @@ -270,7 +270,7 @@ initializer(Symbol *sym) static Symbol * newtag(uint8_t tag) { - register Symbol *sym; + Symbol *sym; static uint8_t ns = NS_STRUCTS; switch (yytoken) { @@ -363,7 +363,7 @@ structdcl(void) static Type * enumdcl(void) { - register Type *tp; + Type *tp; Symbol *sym; int val = 0; diff --git 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; diff --git 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 @@ arguments(Node *np) static Node * postfix(void) { - register Node *np1, *np2; + Node *np1, *np2; np1 = primary(); for (;;) { @@ -511,7 +511,7 @@ typeof(Node *np) static Type * sizeexp(void) { - register Type *tp; + Type *tp; expect('('); switch (yytoken) { @@ -531,8 +531,8 @@ static Node *cast(void); 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 @@ unary(void) static Node * cast(void) { - register Node *np1, *np2; - register Type *tp; + Node *np1, *np2; + Type *tp; if (!accept('(')) return unary(); @@ -594,8 +594,8 @@ cast(void) 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 @@ mul(void) static Node * add(void) { - register char op; - register Node *np; + char op; + Node *np; np = mul(); for (;;) { @@ -631,8 +631,8 @@ add(void) static Node * shift(void) { - register char op; - register Node *np; + char op; + Node *np; np = add(); for (;;) { @@ -649,8 +649,8 @@ shift(void) static Node * relational(void) { - register char op; - register Node *np; + char op; + Node *np; np = shift(); for (;;) { @@ -669,8 +669,8 @@ relational(void) static Node * eq(void) { - register char op; - register Node *np; + char op; + Node *np; np = relational(); for (;;) { @@ -687,7 +687,7 @@ eq(void) static Node * bit_and(void) { - register Node *np; + Node *np; np = eq(); while (accept('&')) @@ -698,7 +698,7 @@ bit_and(void) static Node * bit_xor(void) { - register Node *np; + Node *np; np = bit_and(); while (accept('^')) @@ -709,7 +709,7 @@ bit_xor(void) static Node * bit_or(void) { - register Node *np; + Node *np; np = bit_xor(); while (accept('|')) @@ -720,7 +720,7 @@ bit_or(void) static Node * and(void) { - register Node *np; + Node *np; np = bit_or(); while (accept(AND)) @@ -731,7 +731,7 @@ and(void) static Node * or(void) { - register Node *np; + Node *np; np = and(); while (accept(OR)) @@ -758,8 +758,8 @@ ternary(void) 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 @@ assign(void) Node * expr(void) { - register Node *np1, *np2; + Node *np1, *np2; np1 = assign(); while (accept(',')) { diff --git 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 @@ type: static uint8_t number(void) { - register char ch, *bp; + char ch, *bp; static char base; if ((ch = getc(yyin)) == '0') { @@ -137,7 +137,7 @@ static uint8_t character(void) { static char c; - register Symbol *sym; + Symbol *sym; getc(yyin); /* discard the initial ' */ c = getc(yyin); @@ -156,8 +156,8 @@ static uint8_t 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 @@ end_string: 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 @@ iden(void) 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 @@ follow(int expect, int ifyes, int ifno) static uint8_t minus(void) { - register int c = getc(yyin); + int c = getc(yyin); yytext[1] = c; yytext[2] = '\0'; @@ -245,7 +245,7 @@ minus(void) static uint8_t plus(void) { - register int c = getc(yyin); + int c = getc(yyin); yytext[1] = c; yytext[2] = '\0'; @@ -262,7 +262,7 @@ plus(void) 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 @@ relational(uint8_t op, uint8_t equal, uint8_t shift, uint8_t assig) 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 @@ dot(void) 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 @@ static int skipspaces(void) { - register int c; + int c; while (isspace(c = getc(yyin))) { if (c == '\n') @@ -350,7 +350,7 @@ skipspaces(void) uint8_t next(void) { - register int c; + int c; ungetc(c = skipspaces(), yyin); @@ -372,7 +372,7 @@ next(void) } void -expect(register uint8_t tok) +expect(uint8_t tok) { if (yytoken != tok) unexpected(); @@ -382,7 +382,7 @@ expect(register uint8_t tok) uint8_t ahead(void) { - register int c; + int c; ungetc(c = skipspaces(), yyin); @@ -390,7 +390,7 @@ ahead(void) } void -open_file(register const char *file) +open_file(const char *file) { if (yyin != NULL) fclose(yyin); diff --git 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), diff --git 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 { diff --git 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 @@ static struct symtab { } 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 @@ static void 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 @@ popctx(void) } 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 @@ lookup(register char *s, uint8_t ns) 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 @@ init_keywords(void) {"while", WHILE, WHILE}, {NULL, 0, 0}, }; - register Symbol *sym; + Symbol *sym; for (bp = buff; bp->str; ++bp) { sym = install(bp->str, NS_IDEN); diff --git 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 @@ mktype(Type *tp, uint8_t op, short nelem, void *data) { 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, diff --git 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 diff --git a/cc2/cc2.h b/cc2/cc2.h @@ -101,7 +101,7 @@ enum nerrors { enum { - PUSH, POP, LD, ADD, RET, ADDI, LDI, ADDR, ADDX, ADCX, LDX, + PUSH, POP, LD, ADD, RET, ADDI, LDI, ADDX, ADCX, LDX, LDFX }; diff --git 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 @@ generate(Symbol *fun) extern char odebug; char frame = fun->u.f.locals != 0 || odebug; - code(ADDR, fun->name); if (frame) { code(PUSH, IX); code(LD, IX, SP); diff --git a/cc2/code.c b/cc2/code.c @@ -5,9 +5,26 @@ #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", @@ -34,36 +51,27 @@ static char *opfmt[] = { [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, ...) +{ } diff --git 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" diff --git 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" diff --git 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 @@ enum { 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; diff --git 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 diff --git 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, ...) diff --git 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"); diff --git 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"); diff --git 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"); diff --git 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); }