From be1673b8fbfe014fb6a61640e75440753da3ccc6 Mon Sep 17 00:00:00 2001 From: Sindre Stephansen Date: Fri, 22 Mar 2019 21:45:35 +0100 Subject: [PATCH] Complete 2.1, implement find_globals --- src/ir.c | 46 ++++++++++++++++++++++++++++++++++++++++++++-- src/vslc.c | 13 ++++++------- 2 files changed, 50 insertions(+), 9 deletions(-) diff --git a/src/ir.c b/src/ir.c index 737a253..b605445 100644 --- a/src/ir.c +++ b/src/ir.c @@ -1,3 +1,4 @@ +#include #include // Externally visible, for the generator @@ -19,6 +20,8 @@ void destroy_symtab(void); void create_symbol_table ( void ) { + global_names = malloc(sizeof(tlhash_t)); + tlhash_init(global_names, 20); find_globals(); size_t n_globals = tlhash_size ( global_names ); symbol_t *global_list[n_globals]; @@ -101,14 +104,14 @@ print_bindings ( node_t *root ) { switch ( root->entry->type ) { - case SYM_GLOBAL_VAR: + case SYM_GLOBAL_VAR: printf ( "Linked global var '%s'\n", root->entry->name ); break; case SYM_FUNCTION: printf ( "Linked function %zu ('%s')\n", root->entry->seq, root->entry->name ); - break; + break; case SYM_PARAMETER: printf ( "Linked parameter %zu ('%s')\n", root->entry->seq, root->entry->name @@ -142,6 +145,45 @@ destroy_symbol_table ( void ) void find_globals ( void ) { + node_t *global_list = root->children[0]; + int function_count = 0; + + for (int i = 0; i < global_list->n_children; i++) { + node_t *child = global_list->children[i]; + symbol_t *symbol = malloc(sizeof(symbol_t)); + symbol->node = child; + + if (child->type == DECLARATION) { + symbol->name = child->children[0]->children[0]->data; + symbol->type = SYM_GLOBAL_VAR; + } else if (child->type == FUNCTION) { + symbol->name = child->children[0]->data; + symbol->type = SYM_FUNCTION; + symbol->nparms = child->children[1]->n_children; + symbol->seq = function_count; + function_count++; + + symbol->locals = malloc(sizeof(tlhash_t)); + tlhash_init(symbol->locals, symbol->nparms/2); + + for (int j = 0; j < symbol->nparms; j++) { + node_t *parameter = child->children[1]->children[j]; + symbol_t *parameter_symbol = malloc(sizeof(symbol_t)); + parameter_symbol->name = parameter->data; + parameter_symbol->type = SYM_PARAMETER; + parameter_symbol->node = parameter; + parameter_symbol->seq = j; + + tlhash_insert(symbol->locals, parameter_symbol->name, strlen(parameter_symbol->name), parameter_symbol); + } + } else { + // The node should be a variable or a function. + // Something is wrong + exit(1); + } + + tlhash_insert(global_names, symbol->name, strlen(symbol->name), symbol); + } } void diff --git a/src/vslc.c b/src/vslc.c index b6866fd..7bbc442 100644 --- a/src/vslc.c +++ b/src/vslc.c @@ -3,10 +3,10 @@ #include -node_t *root; // Syntax tree -tlhash_t *global_names; // Symbol table +node_t *root; // Syntax tree +tlhash_t *global_names; // Symbol table char **string_list; // List of strings in the source -size_t n_string_list = 8; // Initial string list capacity (grow on demand) +size_t n_string_list = 8; // Initial string list capacity (grow on demand) size_t stringc = 0; // Initial string count @@ -17,9 +17,8 @@ main ( int argc, char **argv ) yyparse(); simplify_tree ( &root, root ); node_print ( root, 0 ); - // call function to create symbol table - // then call function to print symbol table + create_symbol_table(); + print_symbol_table(); destroy_subtree ( root ); - // call function to destroy symbol table - + destroy_symbol_table(); }