scc

Simple C99 Compiler
Log | Files | Refs | README | LICENSE

commit 4b2e771b524ffb7e9b903a416eb8cb1e995b9449
parent 2f998515cd50f7d48a4271ffa9ef1d9808dd8503
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Fri, 25 Sep 2015 16:58:54 +0200

Add initial support for multi arch in cc1

Diffstat:
cc1/Makefile | 2+-
cc1/arch/z80/arch.h | 8++++++++
cc1/arch/z80/types.c | 284+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
cc1/cc1.h | 10++++------
cc1/expr.c | 6+++---
cc1/types.c | 269++++---------------------------------------------------------------------------
config.mk | 3++-
inc/cc.h | 36------------------------------------
8 files changed, 315 insertions(+), 303 deletions(-)

diff --git a/cc1/Makefile b/cc1/Makefile @@ -1,7 +1,7 @@ include ../config.mk OBJS = types.o decl.o lex.o error.o symbol.o main.o expr.o \ - code.o stmt.o cpp.o fold.o + code.o stmt.o cpp.o fold.o arch/$(ARCH)/types.o all: cc1 diff --git a/cc1/arch/z80/arch.h b/cc1/arch/z80/arch.h @@ -0,0 +1,7 @@ + +#define TINT long long +#define TUINT unsigned long long +#define TFLOAT double + +#define L_INT L_INT16 +#define L_ENUM L_INT+ \ No newline at end of file diff --git a/cc1/arch/z80/types.c b/cc1/arch/z80/types.c @@ -0,0 +1,284 @@ + +#include <stdio.h> + +#include "../../../inc/cc.h" +#include "../../cc1.h" +#include "arch.h" + +#define RANK_BOOL 0 +#define RANK_SCHAR 1 +#define RANK_UCHAR 2 +#define RANK_CHAR 3 +#define RANK_SHORT 4 +#define RANK_USHORT 5 +#define RANK_INT 6 +#define RANK_UINT 7 +#define RANK_LONG 8 +#define RANK_ULONG 9 +#define RANK_LLONG 10 +#define RANK_ULLONG 11 +#define RANK_FLOAT 12 +#define RANK_DOUBLE 13 +#define RANK_LDOUBLE 15 + +#define L_SCHAR L_INT8 +#define L_UCHAR L_UINT8 +#define L_CHAR L_UINT8 +#define L_SHORT L_INT16 +#define L_USHORT L_UINT16 +#define L_UINT L_UINT16 +#define L_LONG L_INT32 +#define L_ULONG L_UINT32 +#define L_LLONG L_INT64 +#define L_ULLONG L_UINT64 +#define L_BOOL 'B' +#define L_FLOAT 'J' +#define L_DOUBLE 'D' +#define L_LDOUBLE 'H' + +/* + * Compiler can generate warnings here if the ranges of TINT, + * TUINT and TFLOAT are smaller than any of the constants in this + * array. Ignore them if you know that the target types are correct + */ +struct limits limits[][4] = { + { + { /* 0 = unsigned 1 byte */ + .min.u = 0, + .max.u = 255 + }, + { /* 1 = unsigned 2 bytes */ + .min.u = 0, + .max.u = 65535u + }, + { /* 2 = unsigned 4 bytes */ + .min.u = 0, + .max.u = 4294967295u + }, + { /* 3 = unsigned 4 bytes */ + .min.u = 0, + .max.u = 18446744073709551615u + } + }, + { + { /* 0 = signed 1 byte */ + .min.i = -127, + .max.i = 127 + }, + { /* 1 = signed 2 byte */ + .min.i = -32767, + .max.i = 32767 + }, + { /* 2 = signed 4 byte */ + .min.i = -2147483647L, + .max.i = 2147483647L + }, + { /* 3 = signed 8 byte */ + .min.i = -9223372036854775807LL, + .max.i = 9223372036854775807LL, + } + }, + { + { + /* 0 = float 4 bytes */ + .min.f = -1, + .max.f = 2 + }, + { + /* 1 = float 8 bytes */ + .min.f = -1, + .max.f = 2, + }, + { + /* 2 = float 16 bytes */ + .min.f = -1, + .max.f = 2, + } + } +}; + +/* + * Initializaion of type pointers were done with + * a C99 initilizator '... = &(Type) {...', but + * c compiler in Plan9 gives error with this + * syntax, so I have switched it to this ugly form + * I hope I will change it again in the future + */ +static Type types[] = { + { /* 0 = voidtype */ + .op = VOID, + .letter = L_VOID, + .printed = 1 + }, + { /* 1 = pvoidtype */ + .op = PTR, + .letter = L_POINTER, + .size = 2, + .align = 2, + .printed = 1 + }, + { /* 2 = booltype */ + .op = INT, + .letter = L_BOOL, + .defined = 1, + .size = 1, + .align = 1, + .n.rank = RANK_BOOL, + .printed = 1 + }, + { /* 3 = schartype */ + .op = INT, + .letter = L_SCHAR, + .defined = 1, + .size = 1, + .align = 1, + .sign = 1, + .n.rank = RANK_SCHAR, + .printed = 1 + }, + { /* 4 = uchartype */ + .op = INT, + .letter = L_UCHAR, + .defined = 1, + .size = 1, + .align = 1, + .n.rank = RANK_UCHAR, + .printed = 1 + }, + { /* 5 = chartype */ + .op = INT, + .letter = L_CHAR, + .defined = 1, + .size = 1, + .align = 1, + .sign = 1, + .n.rank = RANK_CHAR, + .printed = 1 + }, + { /* 6 = ushortype */ + .op = INT, + .letter = L_USHORT, + .defined = 1, + .size = 2, + .align = 1, + .n.rank = RANK_USHORT, + .printed = 1 + }, + { /* 7 = shortype */ + .op = INT, + .letter = L_SHORT, + .defined = 1, + .size = 2, + .align = 1, + .sign = 1, + .n.rank = RANK_SHORT, + .printed = 1 + }, + { /* 8 = uinttype */ + .op = INT, + .letter = L_UINT, + .defined = 1, + .size = 2, + .align = 1, + .n.rank = RANK_UINT, + .printed = 1 + }, + { /* 9 = inttype */ + .op = INT, + .letter = L_INT, + .defined = 1, + .size = 2, + .align = 1, + .sign = 1, + .n.rank = RANK_INT, + .printed = 1 + }, + { /* 10 = longtype */ + .op = INT, + .letter = L_LONG, + .defined = 1, + .size = 4, + .align = 1, + .sign = 1, + .n.rank = RANK_LONG, + .printed = 1 + }, + { /* 11 = ulongtype */ + .op = INT, + .letter = L_ULONG, + .defined = 1, + .size = 4, + .align = 1, + .n.rank = RANK_ULONG, + .printed = 1 + }, + { /* 12 = ullongtype */ + .op = INT, + .letter = L_ULLONG, + .defined = 1, + .size = 8, + .align = 1, + .n.rank = RANK_ULLONG, + .printed = 1 + }, + { /* 13 = llongtype */ + .op = INT, + .letter = L_LLONG, + .defined = 1, + .size = 8, + .align = 1, + .sign = 1, + .n.rank = RANK_LLONG, + .printed = 1 + }, + { /* 14 = floattype */ + .op = FLOAT, + .letter = L_FLOAT, + .defined = 1, + .size = 4, + .align = 1, + .n.rank = RANK_FLOAT, + .printed = 1 + }, + { /* 15 = doubletype */ + .op = FLOAT, + .letter = L_DOUBLE, + .defined = 1, + .size = 8, + .align = 1, + .n.rank = RANK_DOUBLE, + .printed = 1 + }, + { /* 16 = ldoubletype */ + .op = FLOAT, + .letter = L_LDOUBLE, + .defined = 1, + .size = 16, + .align = 1, + .n.rank = RANK_LDOUBLE, + .printed = 1 + }, + { /* 17 = sizettype */ + .op = INT, + .letter = L_UINT, + .defined = 1, + .size = 2, + .align = 1, + .n.rank = RANK_UINT, + .printed = 1 + } +}; + +Type *voidtype = &types[0], *pvoidtype = &types[1], + *booltype = &types[2], *schartype = &types[3], + *uchartype = &types[4], *chartype = &types[5], + *ushortype = &types[6], *shortype = &types[7], + *uinttype = &types[8], *inttype = &types[9], + *longtype = &types[10], *ulongtype = &types[11], + *ullongtype = &types[12], *llongtype = &types[13], + *floattype = &types[14], *doubletype = &types[15], + *ldoubletype = &types[16], *sizettype = &types[17]; + +static Symbol dummy0 = {.u.i = 0, .type = &types[9]}, + dummy1 = {.u.i = 1, .type = &types[9]}; +Symbol *zero = &dummy0, *one = &dummy1; diff --git a/cc1/cc1.h b/cc1/cc1.h @@ -1,4 +1,6 @@ +#include "arch.h" + #define INPUTSIZ LINESIZ #ifndef PREFIX #define PREFIX "/usr/" @@ -28,11 +30,7 @@ struct limits { } min; }; -/* - * TODO: Some of the data stored in type is shared with - * cc2, so it should be stored in a table shared - * between both programs, and dependant of the target. - */ + struct type { unsigned char op; /* type builder operator */ char ns; /* namespace for struct members */ @@ -397,7 +395,7 @@ extern unsigned curctx; extern Symbol *curfun, *zero, *one; extern Type *voidtype, *pvoidtype, *booltype, - *uchartype, *chartype, + *uchartype, *chartype, *schartype, *uinttype, *inttype, *sizettype, *ushortype, *shortype, *longtype, *ulongtype, diff --git a/cc1/expr.c b/cc1/expr.c @@ -54,13 +54,13 @@ promote(Node *np) { Type *tp; Node *new; - unsigned r; + unsigned r, ur = uinttype->n.rank; tp = np->type; r = tp->n.rank; - if (r > RANK_UINT || tp == inttype || tp == uinttype) + if (r > ur || tp == inttype || tp == uinttype) return np; - tp = (r == RANK_UINT) ? uinttype : inttype; + tp = (r == ur) ? uinttype : inttype; if ((new = convert(np, tp, 1)) != NULL) return new; return np; diff --git a/cc1/types.c b/cc1/types.c @@ -7,256 +7,11 @@ #include "../inc/sizes.h" #include "../inc/cc.h" #include "cc1.h" +#include "arch.h" #define NR_TYPE_HASH 16 -/* - * Compiler can generate warnings here if the ranges of TINT, - * TUINT and TFLOAT are smaller than any of the constants in this - * array. Ignore them if you know that the target types are correct - */ -static struct limits limits[][4] = { - { - { /* 0 = unsigned 1 byte */ - .min.u = 0, - .max.u = 255 - }, - { /* 1 = unsigned 2 bytes */ - .min.u = 0, - .max.u = 65535u - }, - { /* 2 = unsigned 4 bytes */ - .min.u = 0, - .max.u = 4294967295u - }, - { /* 3 = unsigned 4 bytes */ - .min.u = 0, - .max.u = 18446744073709551615u - } - }, - { - { /* 0 = signed 1 byte */ - .min.i = -127, - .max.i = 127 - }, - { /* 1 = signed 2 byte */ - .min.i = -32767, - .max.i = 32767 - }, - { /* 2 = signed 4 byte */ - .min.i = -2147483647L, - .max.i = 2147483647L - }, - { /* 3 = signed 8 byte */ - .min.i = -9223372036854775807LL, - .max.i = 9223372036854775807LL, - } - }, - { - { - /* 0 = float 4 bytes */ - .min.f = -1, - .max.f = 2 - }, - { - /* 1 = float 8 bytes */ - .min.f = -1, - .max.f = 2, - }, - { - /* 2 = float 16 bytes */ - .min.f = -1, - .max.f = 2, - } - } -}; - -/* - * Initializaion of type pointers were done with - * a C99 initilizator '... = &(Type) {...', but - * c compiler in Plan9 gives error with this - * syntax, so I have switched it to this ugly form - * I hope I will change it again in the future - */ -static Type types[] = { - { /* 0 = voidtype */ - .op = VOID, - .letter = L_VOID, - .printed = 1 - }, - { /* 1 = pvoidtype */ - .op = PTR, - .letter = L_POINTER, - .size = 2, - .align = 2, - .printed = 1 - }, - { /* 2 = booltype */ - .op = INT, - .letter = L_BOOL, - .defined = 1, - .size = 1, - .align = 1, - .n.rank = RANK_BOOL, - .printed = 1 - }, - { /* 3 = schartype */ - .op = INT, - .letter = L_SCHAR, - .defined = 1, - .size = 1, - .align = 1, - .sign = 1, - .n.rank = RANK_SCHAR, - .printed = 1 - }, - { /* 4 = uchartype */ - .op = INT, - .letter = L_UCHAR, - .defined = 1, - .size = 1, - .align = 1, - .n.rank = RANK_UCHAR, - .printed = 1 - }, - { /* 5 = chartype */ - .op = INT, - .letter = L_CHAR, - .defined = 1, - .size = 1, - .align = 1, - .sign = 1, - .n.rank = RANK_CHAR, - .printed = 1 - }, - { /* 6 = ushortype */ - .op = INT, - .letter = L_USHORT, - .defined = 1, - .size = 2, - .align = 1, - .n.rank = RANK_USHORT, - .printed = 1 - }, - { /* 7 = shortype */ - .op = INT, - .letter = L_SHORT, - .defined = 1, - .size = 2, - .align = 1, - .sign = 1, - .n.rank = RANK_SHORT, - .printed = 1 - }, - { /* 8 = uinttype */ - .op = INT, - .letter = L_UINT, - .defined = 1, - .size = 2, - .align = 1, - .n.rank = RANK_UINT, - .printed = 1 - }, - { /* 9 = inttype */ - .op = INT, - .letter = L_INT, - .defined = 1, - .size = 2, - .align = 1, - .sign = 1, - .n.rank = RANK_INT, - .printed = 1 - }, - { /* 10 = longtype */ - .op = INT, - .letter = L_LONG, - .defined = 1, - .size = 4, - .align = 1, - .sign = 1, - .n.rank = RANK_LONG, - .printed = 1 - }, - { /* 11 = ulongtype */ - .op = INT, - .letter = L_ULONG, - .defined = 1, - .size = 4, - .align = 1, - .n.rank = RANK_ULONG, - .printed = 1 - }, - { /* 12 = ullongtype */ - .op = INT, - .letter = L_ULLONG, - .defined = 1, - .size = 8, - .align = 1, - .n.rank = RANK_ULLONG, - .printed = 1 - }, - { /* 13 = llongtype */ - .op = INT, - .letter = L_LLONG, - .defined = 1, - .size = 8, - .align = 1, - .sign = 1, - .n.rank = RANK_LLONG, - .printed = 1 - }, - { /* 14 = floattype */ - .op = FLOAT, - .letter = L_FLOAT, - .defined = 1, - .size = 4, - .align = 1, - .n.rank = RANK_FLOAT, - .printed = 1 - }, - { /* 15 = doubletype */ - .op = FLOAT, - .letter = L_DOUBLE, - .defined = 1, - .size = 8, - .align = 1, - .n.rank = RANK_DOUBLE, - .printed = 1 - }, - { /* 16 = ldoubletype */ - .op = FLOAT, - .letter = L_LDOUBLE, - .defined = 1, - .size = 16, - .align = 1, - .n.rank = RANK_LDOUBLE, - .printed = 1 - }, - { /* 17 = sizettype */ - .op = INT, - .letter = L_UINT, - .defined = 1, - .size = 2, - .align = 1, - .n.rank = RANK_UINT, - .printed = 1 - } -}; - -Type *voidtype = &types[0], *pvoidtype = &types[1], - *booltype = &types[2], *schartype = &types[3], - *uchartype = &types[4], *chartype = &types[5], - *ushortype = &types[6], *shortype = &types[7], - *uinttype = &types[8], *inttype = &types[9], - *longtype = &types[10], *ulongtype = &types[11], - *ullongtype = &types[12], *llongtype = &types[13], - *floattype = &types[14], *doubletype = &types[15], - *ldoubletype = &types[16], *sizettype = &types[17]; - -static Symbol dummy0 = {.u.i = 0, .type = &types[9]}, - dummy1 = {.u.i = 1, .type = &types[9]}; -Symbol *zero = &dummy0, *one = &dummy1; - +extern struct limits limits[][4]; struct limits * getlimits(Type *tp) @@ -395,22 +150,24 @@ mktype(Type *tp, int op, TINT nelem, Type *pars[]) Type **tbl, type; unsigned t; Type *bp; - static char letters[] = { - [PTR] = L_POINTER, - [ARY] = L_ARRAY, - [FTN] = L_FUNCTION, - [ENUM] = L_INT, - [STRUCT] = L_STRUCT, - [UNION] = L_UNION - }; + int c; if (op == PTR && tp == voidtype) return pvoidtype; + switch (op) { + case PTR: c = L_POINTER; break; + case ARY: c = L_ARRAY; break; + case FTN: c = L_FUNCTION; break; + case ENUM: c = L_ENUM; break; + case STRUCT: c = L_STRUCT; break; + case UNION: c = L_UNION; break; + } + type.type = tp; type.op = op; type.printed = 0; - type.letter = letters[op]; + type.letter = c; type.p.pars = pars; type.n.elem = nelem; type.ns = 0; diff --git a/config.mk b/config.mk @@ -2,6 +2,7 @@ VERSION = 0.1 # Customize below to fit your system +ARCH = z80 # paths PREFIX = /usr/local/ @@ -12,6 +13,6 @@ LD = $(CC) AR = ar # for Plan9 add -D_SUSV2_SOURCE -DNBOOL -CPPFLAGS = -DNDEBUG CFLAGS = -O2 -std=c99 LDFLAGS = -s +CPPFLAGS = -DNDEBUG -Iarch/$(ARCH) diff --git a/inc/cc.h b/inc/cc.h @@ -13,26 +13,6 @@ typedef unsigned bool; #define DBG(...) #endif -#define TINT long long -#define TUINT unsigned long long -#define TFLOAT double - -#define RANK_BOOL 0 -#define RANK_SCHAR 1 -#define RANK_UCHAR 2 -#define RANK_CHAR 3 -#define RANK_SHORT 4 -#define RANK_USHORT 5 -#define RANK_INT 6 -#define RANK_UINT 7 -#define RANK_LONG 8 -#define RANK_ULONG 9 -#define RANK_LLONG 10 -#define RANK_ULLONG 11 -#define RANK_FLOAT 12 -#define RANK_DOUBLE 13 -#define RANK_LDOUBLE 15 - #define L_INT8 'C' #define L_INT16 'I' #define L_INT32 'W' @@ -49,22 +29,6 @@ typedef unsigned bool; #define L_UNION 'U' #define L_STRUCT 'S' -#define L_SCHAR L_INT8 -#define L_UCHAR L_UINT8 -#define L_CHAR L_UINT8 -#define L_SHORT L_INT16 -#define L_USHORT L_UINT16 -#define L_INT L_INT16 -#define L_UINT L_UINT16 -#define L_LONG L_INT32 -#define L_ULONG L_UINT32 -#define L_LLONG L_INT64 -#define L_ULLONG L_UINT64 -#define L_BOOL 'B' -#define L_FLOAT 'J' -#define L_DOUBLE 'D' -#define L_LDOUBLE 'H' - #define L_PUBLIC 'G' #define L_PRIVATE 'Y' #define L_LOCAL 'T'