From: Michael D. Lowis Date: Fri, 16 Jul 2021 02:46:13 +0000 (-0400) Subject: added type cache output to symbol file X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=d85d4ea023ad1eb1f910faa3f64f2a06ec209d3f;p=proto%2Fobnc.git added type cache output to symbol file --- diff --git a/cerise/inc/cerise.h b/cerise/inc/cerise.h index 0b65656..08f1474 100644 --- a/cerise/inc/cerise.h +++ b/cerise/inc/cerise.h @@ -131,10 +131,15 @@ typedef struct { Tok tok; char* name; long curr_reg; + size_t scope; + size_t msyms; size_t nsyms; Symbol* syms; - size_t scope; + + size_t mtypes; + size_t ntypes; + Type** types; } Parser; // src/stdlib.c @@ -158,6 +163,7 @@ char* expect_text(Parser* p, TokType type); int consume(Parser* p); // src/sym.c +Type* symbol_newtype(Parser* p); Symbol* symbol_new(Parser* p, size_t scope, char* name, int class, bool export); Symbol* symbol_get(Parser* p, size_t module, char* name, int class); size_t symbol_getid(Parser* p, size_t module, char* name, int class); diff --git a/cerise/src/grammar.c b/cerise/src/grammar.c index 071fee5..b5dbffa 100644 --- a/cerise/src/grammar.c +++ b/cerise/src/grammar.c @@ -338,7 +338,8 @@ static Type* type(Parser* p) long long length = ast_asint(lnode); expect(p, OF); Type* base = type(p); - ret = calloc(1, sizeof(Type)); +// ret = calloc(1, sizeof(Type)); + ret = symbol_newtype(p); ret->form = FORM_ARRAY; ret->size = length; ret->base = base; @@ -346,7 +347,8 @@ static Type* type(Parser* p) else if (accept(p, RECORD)) { long offset = 0; - ret = calloc(1, sizeof(Type)); +// ret = calloc(1, sizeof(Type)); + ret = symbol_newtype(p); ret->form = FORM_RECORD; while (peek(p)->type != END) @@ -483,6 +485,7 @@ static void const_decl(Parser* p) sym = symbol_new(p, 0, name, SYM_CONST, export); expect(p, '='); sym->value = expression(p); + sym->type = sym->value->hdr.type; } while (matches(p, IDENT)); @@ -497,7 +500,8 @@ void proc_decl(Parser* p) char* name = expect_text(p, IDENT); bool export = accept(p, '*'); Symbol* proc = symbol_new(p, 0, name, SYM_PROC, export); - Type* proctype = calloc(1, sizeof(Type)); +// Type* proctype = calloc(1, sizeof(Type)); + Type* proctype = symbol_newtype(p); proctype->form = FORM_PROC; proc->type = proctype; @@ -554,7 +558,8 @@ void proc_decl(Parser* p) expect(p, END); symbol_closescope(p, scope); - ast_print(p, proc->value); +// ast_print(p, proc->value); + (void)proc->value; EXIT_RULE(); } @@ -617,7 +622,8 @@ static void module(Parser* p) if (accept(p, BEGIN)) { AstNode* block = statement_seq(p); - ast_print(p, block); +// ast_print(p, block); + (void)block; expect(p, END); } @@ -664,10 +670,16 @@ void compile(char* fname) .curr_reg = 0 }; + p->mtypes = 8; + p->types = calloc(p->mtypes, sizeof(Type*)); symbol_new(p, 0, "Bool", SYM_TYPE, 0)->type = &BoolType; + p->types[p->ntypes++] = &BoolType; symbol_new(p, 0, "Int", SYM_TYPE, 0)->type = &IntType; + p->types[p->ntypes++] = &IntType; symbol_new(p, 0, "Real", SYM_TYPE, 0)->type = &RealType; + p->types[p->ntypes++] = &RealType; symbol_new(p, 0, "String", SYM_TYPE, 0)->type = &StringType; + p->types[p->ntypes++] = &StringType; module(p); } diff --git a/cerise/src/sym.c b/cerise/src/sym.c index 77b6c59..687f406 100644 --- a/cerise/src/sym.c +++ b/cerise/src/sym.c @@ -59,6 +59,22 @@ static size_t lookup(Parser* p, size_t scope, size_t module, char* name, int cla return ret; } +Type* symbol_newtype(Parser* p) +{ + Type* type = calloc(1, sizeof(Type)); + + if (p->ntypes == p->mtypes) + { + p->mtypes = (p->mtypes ? (p->mtypes << 1) : 512u); + p->types = realloc(p->types, p->mtypes * sizeof(Type*)); + } + + p->types[p->ntypes] = type; + p->ntypes++; + + return type; +} + Symbol* symbol_new(Parser* p, size_t scope, char* name, int class, bool export) { if (p->nsyms == p->msyms) @@ -129,6 +145,17 @@ void symbol_closescope(Parser* p, size_t scope) /* Symbol File Generation *****************************************************************************/ +static const char TypeIdents[FORM_COUNT] = { + [FORM_BOOL] = 'b', + [FORM_INT] = 'i', + [FORM_REAL] = 'r', + [FORM_ARRAY] = 'a', + [FORM_STRING] = 's', + [FORM_RECORD] = 'r', + [FORM_PROC] = 'p', + [FORM_VOID] = 'v', +}; + static const char SymTypes[5] = { [SYM_MODULE] = 'M', [SYM_CONST] = 'C', @@ -137,14 +164,33 @@ static const char SymTypes[5] = { [SYM_PROC] = 'P', }; +static size_t typeid(Parser* p, Type* type) +{ + size_t i; + for (i = 0; i < p->ntypes; i++) + { + if (p->types[i] == type) + break; + } + assert(i < p->ntypes); + return i; +} + void symbol_export(Parser* p, char* path) { (void)path; + for (size_t i = 0; i < p->ntypes; i++) + { + printf("t %d %c\n", i, TypeIdents[p->types[i]->form]); + } + for (size_t i = 0; i < p->nsyms; i++) { Symbol* sym = &(p->syms[i]); if (!sym->export) continue; - printf("%c %s\n", SymTypes[sym->class], sym->name); + printf("%c %s ", SymTypes[sym->class], sym->name); + fflush(stdout); + printf("%d\n", typeid(p, sym->type)); } }