TDT4205/exercises/04/vslc/src/ir.c

449 lines
12 KiB
C

#include <vslc.h>
#define ERRPRT(format, args...) {fprintf(stderr, "[ERROR] "); fprintf(stderr ,format, ##args);}
// Externally visible, for the generator
extern tlhash_t *global_names;
extern char **string_list;
extern size_t n_string_list, stringc;
// Implementation choices, only relevant internally
static void find_globals ( void );
/** @param function Function's symbol table entry
* @param root Function's root node */
static void bind_names ( symbol_t *function, node_t *root );
/* Helper functions */
void print_global_tree(symbol_t* global);
void insert_symbol(tlhash_t *hash_table, symbol_t* symbol);
void push_scope(void);
void pop_scope(void);
static uint64_t no_scopes, cur_scope_depth;
static tlhash_t **scopes;
void
create_symbol_table ( void )
{
// Initialize string array
string_list = malloc(n_string_list * sizeof(char*));
n_string_list = DEFAULT_STRING_LIST_SIZE;
stringc = 0;
no_scopes = DEFAULT_NO_SCOPES;
cur_scope_depth = 0;
scopes = malloc(no_scopes * sizeof(tlhash_t));
find_globals();
uint64_t no_globals = tlhash_size(global_names);
symbol_t **global_list = malloc(no_globals * sizeof(symbol_t));
tlhash_values(global_names, (void **)global_list );
/* Iterate over the temporary list, bind names in each function */
for (uint64_t g = 0; g < no_globals; g++ )
{
if (global_list[g]->type == SYM_FUNCTION)
bind_names(global_list[g], global_list[g]->node);
}
free(global_list);
}
const char *symbol_names[4] = {"GLOBAL_VAR", "FUNCTION", "PARAMETER", "LOCAL_VAR"};
void
print_global_tree(symbol_t* global)
{
if (!global)
return;
printf("─%s(%s, nparams=%ld, seq=%ld, node=%p)\n",
symbol_names[global->type],
global->name,
global->nparms,
global->seq,
global->node
);
if (!global->nparms && !global->locals)
{putchar('\n');return;}
uint64_t no_locals = tlhash_size(global->locals);
symbol_t **locals_list = malloc(no_locals * sizeof(symbol_t));
tlhash_values(global->locals, (void **)locals_list );
for (int l = 0; l < no_locals; l++)
{
printf(" %s─%s(%s, seq=%ld, node=%p)\n",
(l < (no_locals - 1)) ? "" : "",
symbol_names[locals_list[l]->type],
locals_list[l]->name,
locals_list[l]->seq,
locals_list[l]->node
);
}
putchar('\n');
free(locals_list);
}
void
print_string_list(void)
{
printf("─STRINGS [%ld]\n", stringc);
for (uint64_t i = 0; i < stringc; i++)
printf(" %s─[%ld]: %s\n",
(i < (stringc - 1)) ? "" : "",
i, string_list[i]
);
}
void
print_symbol_table ( void )
{
/* TODO: output its contents */
/* Get the number of symbols, size up a temporary list and fill it */
uint64_t no_globals = tlhash_size(global_names);
symbol_t **global_list = malloc(no_globals * sizeof(symbol_t));
tlhash_values(global_names, (void **)global_list );
/* Iterate over the temporary list, printing entries */
for (uint64_t g = 0; g < no_globals; g++ )
{
//printf("global: %s\n", global_list[g]->name );
print_global_tree(global_list[g]);
}
free(global_list);
print_string_list();
}
void
destroy_global(symbol_t* global)
{
if (!global)
return;
if (!global->locals)
{
free(global);
return;
}
uint64_t no_locals = tlhash_size(global->locals);
symbol_t **locals_list = malloc(no_locals * sizeof(symbol_t));
tlhash_values(global->locals, (void **)locals_list );
for (int l = 0; l < no_locals; l++)
free(locals_list[l]);
tlhash_finalize(global->locals);
free(global->locals);
free(global);
free(locals_list);
}
void
destroy_symbol_table ( void )
{
// FREE STRINGS
// Free all strings that are kept in the array
for (uint64_t c = 0; c < stringc; c++)
free(string_list[c]);
// Free the actual list
free(string_list);
// FREE SCOPES
// At the end of program, all scopes have to be popped
// Therefore only free the list
free(scopes);
// FREE GLOBAL NAMES
if (!global_names)
return;
// Fetch list of globals
uint64_t no_globals = tlhash_size(global_names);
symbol_t **global_list = malloc(no_globals * sizeof(symbol_t));
tlhash_values(global_names, (void **)global_list );
// Destroy all global elements
for (uint64_t g = 0; g < no_globals; g++)
destroy_global(global_list[g]);
// Destory the global hash table
tlhash_finalize(global_names);
// Free the global hash table
free(global_names);
// Free the temp list
free(global_list);
}
void
insert_symbol(tlhash_t *hash_table, symbol_t* symbol)
{
tlhash_insert(hash_table, symbol->name, strlen(symbol->name), symbol);
}
void
find_globals ( void )
{
tlhash_init(global_names = malloc(sizeof(tlhash_t)), GLOBAL_BUCKET_SIZE);
uint64_t no_functions = 0;
node_t *global_list = root;
// Check if not nullptr
if (!global_list)
return;
symbol_t* global_symbol;
for (uint64_t global_i = 0; global_i < global_list->n_children; global_i++)
{
node_t *current_global = global_list->children[global_i];
switch (current_global->type)
{
case VARIABLE_LIST:
// Go through the variable list and get all the global variables
for (uint64_t var_i = 0; var_i < current_global->n_children; var_i++)
{
global_symbol = malloc(sizeof(symbol_t));
*global_symbol = (symbol_t){
.type = SYM_GLOBAL_VAR,
.name = current_global->children[var_i]->data,
.node = current_global->children[var_i],
.seq = 0,
.nparms = 0,
.locals = NULL
};
insert_symbol(global_names, global_symbol);
}
break;
case FUNCTION:
node_t *function = current_global;
// Function node allways have the same structure,
// [0] are the identifier
// [1] are the variable list, within a paramerer_list
// [2] are the actual block
if (!function->children[0])
break;
// Create the function symbol
global_symbol = malloc(sizeof(symbol_t));
*global_symbol = (symbol_t){
.type = SYM_FUNCTION,
.name = current_global->children[0]->data,
.node = current_global->children[2],
.seq = no_functions++,
.nparms = 0,
.locals = malloc(sizeof(tlhash_t))
};
// Initialize the local variable table
tlhash_init(global_symbol->locals, LOCAL_BUCKET_SIZE);
// Insert the pointer to the newly created symbol
insert_symbol(global_names, global_symbol);
// If there are no parameters in function, break.
if (!current_global->children[1]->n_children)
break;
// Find all params and insert into hash table in global_symbol
symbol_t *param_sym;
node_t *param_list = current_global->children[1]->children[0];
global_symbol->nparms = param_list->n_children;
for (uint64_t param_i = 0; param_i < param_list->n_children; param_i++)
{
param_sym = malloc(sizeof(symbol_t));
*param_sym = (symbol_t){
.type = SYM_PARAMETER,
.name = param_list->children[param_i]->data,
.node = param_list->children[param_i],
.seq = param_i,
.nparms = 0,
.locals = NULL
};
insert_symbol(global_symbol->locals, param_sym);
}
break;
}
}
}
void
push_scope(void)
{
scopes[cur_scope_depth] = malloc(sizeof(tlhash_t));
tlhash_init(scopes[cur_scope_depth++], LOCAL_BUCKET_SIZE);
// Grow the amount of scopes
if (cur_scope_depth >= no_scopes)
{
no_scopes *= 2;
tlhash_t **new_scopes = realloc(scopes, no_scopes * sizeof(tlhash_t));
if (!new_scopes)
{
ERRPRT("Could not realloc scopes!\n");
exit(EXIT_FAILURE);
}
scopes = new_scopes;
}
}
void
pop_scope(void)
{
tlhash_finalize(scopes[--cur_scope_depth]);
free(scopes[cur_scope_depth]);
}
void
insert_local_to_scope(symbol_t *local)
{
insert_symbol(scopes[cur_scope_depth - 1], local);
}
void
insert_local_to_func(symbol_t *function, symbol_t *root)
{
tlhash_insert(
function->locals, //! Insert local to the function var table
&root->seq, //! The key is a number, unique, strictly growing
sizeof(root->seq), //! Size of key
root //! The local symbol
);
}
void
insert_local_var(symbol_t *function, node_t *root)
{
size_t sequence = tlhash_size(function->locals);
symbol_t *variable = malloc(sizeof(symbol_t));
*variable = (symbol_t){
.type = SYM_LOCAL_VAR,
.name = root->data,
.node = root,
.seq = sequence, //! Use sequence as name in var list of function, strictly growing
.nparms = 0,
.locals = NULL
};
insert_local_to_scope(variable);
insert_local_to_func(function, variable);
}
void
collect_string(node_t *root)
{
if (!root->data)
return;
string_list[stringc] = root->data;
root->data = malloc(sizeof(size_t));
*((size_t*)root->data) = stringc++;
if (stringc >= n_string_list)
{
n_string_list *= 2;
char **new_string_list = realloc(string_list, n_string_list * sizeof(char*));
if (!new_string_list)
{
ERRPRT("Could not realloc string list!\n");
exit(EXIT_FAILURE);
}
string_list = new_string_list;
}
}
static symbol_t*
lookup_var(symbol_t *function, char* var)
{
// Symbol to store the stymbol to be found
symbol_t* symbol = NULL;
// Result stores the result of the hash lookups
int result;
// Try the local scopes first
for (int64_t d = cur_scope_depth - 1; d >= 0; d--)
{
result = tlhash_lookup(scopes[d], var, strlen(var), (void**)&symbol);
if (result == TLHASH_SUCCESS)
return symbol;
}
// Then move to parameters
result = tlhash_lookup(function->locals, var, strlen(var), (void**)&symbol);
if (result == TLHASH_SUCCESS)
return symbol;
// Last try global parameters
result = tlhash_lookup(global_names, var, strlen(var), (void**)&symbol);
if (result == TLHASH_SUCCESS)
return symbol;
// If nothing is found, return NULL
return NULL;
}
void
bind_names ( symbol_t *function, node_t *root )
{
if (!function)
return;
if (!root)
return;
// Can't declare variables inside switch unless
// it is in a new scope
node_t *declarations;
// We want do top to bottom traverse, so do not
// call recusivly unless we need to go deeper
switch (root->type)
{
case BLOCK:
push_scope();
for (uint64_t i = 0; i < root->n_children; i++)
bind_names(function, root->children[i]);
pop_scope();
break;
case DECLARATION_LIST:
if (!root->children[0])
break;
declarations = root->children[0];
for (uint64_t i = 0; i < declarations->n_children; i++)
// Insert each of the local variables in the declaration
insert_local_var(function, declarations->children[i]);
break;
case IDENTIFIER_DATA:
if (!root->data)
break;
if (!(root->entry = lookup_var(function, root->data)))
{
ERRPRT("Could not find %s in scope!\n", (char*)root->data)
exit(EXIT_FAILURE);
}
break;
case STRING_DATA:
collect_string(root);
break;
default:
for (uint64_t i = 0; i < root->n_children; i++)
bind_names(function, root->children[i]);
break;
}
}