scc

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

commit 03ede7d69465d1ee44d12078a3c880a28cc75534
parent 4c8cd1d527ce2e8023299b091af90a824b7c1532
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Fri,  8 Aug 2014 18:28:51 +0200

Add Type structure in cc2

This type structure is simpler than the structure of cc1, because
here we only need the size and what kind of basic type is it.

Diffstat:
cc1/main.c | 3+--
cc2/cc2.h | 10++++++++--
cc2/cgen.c | 3++-
cc2/main.c | 1-
cc2/parser.c | 68++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
inc/cc.h | 1-
6 files changed, 75 insertions(+), 11 deletions(-)

diff --git a/cc1/main.c b/cc1/main.c @@ -1,9 +1,8 @@ -#include <stdbool.h> -#include <stddef.h> #include <stdint.h> #include <stdio.h> +#include <cc.h> #include "cc1.h" extern void init_keywords(void), diff --git a/cc2/cc2.h b/cc2/cc2.h @@ -1,11 +1,17 @@ +typedef struct { + short size; + bool sign : 1; + bool c_int : 1; +} Type; + typedef struct symbol { char public; char type; struct symbol *next; union { struct { - char type; + Type *type; char storage; } v; struct { @@ -21,7 +27,7 @@ typedef struct symbol { typedef struct node { char op; - char type; + Type *type; int8_t complex; int8_t addable; union { diff --git a/cc2/cgen.c b/cc2/cgen.c @@ -2,11 +2,12 @@ #include <stdint.h> #include <stdlib.h> +#include <cc.h> #include "cc2.h" - #include <stdio.h> + static void emit(char what, void *data) { diff --git a/cc2/main.c b/cc2/main.c @@ -5,7 +5,6 @@ #include <stdlib.h> #include <cc.h> -#include <sizes.h> #include "cc2.h" #include "error.h" diff --git a/cc2/parser.c b/cc2/parser.c @@ -24,6 +24,50 @@ static Node nodepool[NR_NODEPOOL], *newp = nodepool; static Symbol *localtbl; static Symbol *globaltbl; +Type l_int8 = { + .size = 1, + .sign = 1, + .c_int = 1, +}; + +Type l_int16 = { + .size = 2, + .sign = 1, + .c_int = 1, +}; + +Type l_int32 = { + .size = 4, + .sign = 1, + .c_int = 1, +}; + +Type l_int64 = { + .size = 8, + .sign = 1, + .c_int = 1, +}; + +Type l_uint8 = { + .size = 1, + .c_int = 1, +}; + +Type l_uint16 = { + .size = 2, + .c_int = 1, +}; + +Type l_uint32 = { + .size = 4, + .c_int = 1, +}; + +Type l_uint64 = { + .size = 8, + .c_int = 1, +}; + static Symbol * local(char *num) { @@ -74,12 +118,26 @@ pop(void) return *--stackp; } -static char +static Type * gettype(char *type) { switch (type[0]) { - case L_INT16: case L_INT8: - return type[0]; + case L_INT8: + return &l_int8; + case L_INT16: + return &l_int16; + case L_INT32: + return &l_int32; + case L_INT64: + return &l_int64; + case L_UINT8: + return &l_uint8; + case L_UINT16: + return &l_uint16; + case L_UINT32: + return &l_uint32; + case L_UINT64: + return &l_uint64; default: error(ETYPERR); } @@ -123,11 +181,13 @@ variable(char *token) static void immediate(char *token) { + static char buf[2]; Node *np = newnode(); np->op = CONST; /* TODO: deal with constant non integer */ - np->type = L_INT; + buf[0] = L_INT; + np->type = gettype(buf); np->u.imm = atoi(token+1); np->left = np->right = NULL; push(np); diff --git a/inc/cc.h b/inc/cc.h @@ -30,7 +30,6 @@ #define L_UINT16 'N' #define L_UINT32 'Z' #define L_UINT64 'O' -#define L_INT L_INT16 #define L_VOID '0' #define L_POINTER 'P'