void c_dot_file_symbol (const char *filename, int appfile ATTRIBUTE_UNUSED) { symbolS *symbolP; /* BFD converts filename to a .file symbol with an aux entry. It also handles chaining. */ symbolP = symbol_new (filename, bfd_abs_section_ptr, 0, &zero_address_frag); S_SET_STORAGE_CLASS (symbolP, C_FILE); S_SET_NUMBER_AUXILIARY (symbolP, 1); symbol_get_bfdsym (symbolP)->flags = BSF_DEBUGGING; #ifndef NO_LISTING { extern int listing; if (listing) listing_source_file (filename); } #endif /* Make sure that the symbol is first on the symbol chain. */ if (symbol_rootP != symbolP) { symbol_remove (symbolP, &symbol_rootP, &symbol_lastP); symbol_insert (symbolP, symbol_rootP, &symbol_rootP, &symbol_lastP); } }
//Goes down to the child node(which is a VARIABLE_LIST), and adds all it's children to the symbol table. void declaration(node_t *node) { for (int i = 0; i < node->children[0]->n_children; i++) { symbol_t *new_symbol = (void*)malloc(sizeof(void)); new_symbol->stack_offset = var_offset; var_offset = var_offset - 4; new_symbol->label = NULL; symbol_insert((char*)(node->children[0]->children[i]->data), new_symbol); } }
void add_functions_to_symtab ( node_t *function_list_n ) { for ( int i = 0; i < function_list_n->n_children; i++ ) { node_t *func = function_list_n->children[i]; symbol_t *var = malloc( sizeof( symbol_t ) ); *var = (symbol_t) { 0, 0, (char *) func->children[0]->data }; symbol_insert ( var->label, var ); } }
static int first_pass(FILE * input) { int pc = 0; /* program counter for symbol values */ char *line; /* whole input line */ char **tokens; /* ARGV-style split of line */ sourcelinenumber = 0; nerrors = 0; while ((line = getLine(input)) != NULL) { tokens = lexer(line); /* convert line to tokens */ sourcelinenumber++; /* update line number for errors */ /* * blank line -- ignore it */ if (tokens[0] == NULL) continue; /* * delimiter character as first token?! */ if (is_delimiter(tokens[0])) { fprintf(stderr, "%s:%d: bad delimiter character '%s' at begining of line\n", sourcefilename, sourcelinenumber, tokens[0]); nerrors++; continue; } /* * second token is ':' delimiter -- assume the first token * is a label and add it in with the value of the current PC. */ if (tokens[1] == colon) { if (!symbol_insert(tokens[0], pc)) { fprintf(stderr, "%s:%d: duplicate symbol '%s'\n", sourcefilename, sourcelinenumber, tokens[0]); nerrors++; } tokens++; /* advance ptr to skip the label */ tokens++; /* advance ptr to skip the delimiter */ } /* * if the line (also) contains an opcode, bump the PC */ if (tokens[0] != NULL) pc++; } fprintf(stderr, "%d errors on the first pass\n", nerrors); return (nerrors == 0); /* 1 on success, 0 on failure */ }
void declare(node_t* node, int32_t offset, char function){ symbol_t* symbol = malloc(sizeof(symbol_t)); char* key = (char*)node->data; //"Variables don't need labels" if(function){ symbol->label = key; } symbol->stack_offset = offset; symbol_insert(key, symbol); }
void bind_functions(node_t *node) { //Binds all the function names. for (int i = 0; i < node->n_children; i++) { symbol_t *newSymbol = (void*)malloc(sizeof(void)); newSymbol->stack_offset = 0; newSymbol->label = (char*)(node->children[i]->children[0]->data); symbol_insert(newSymbol->label, newSymbol); } //Continues down the tree. for (int i = 0; i < node->n_children; i++) { bind_names(node->children[i]); } }
void add_functions( node_t* child ) { node_t* varlist = child->children[1]; // add function symbol node_t* var = child->children[0]; symbol_t* func = malloc(sizeof(symbol_t)); func->stack_offset = 0; func->depth = 0; if(varlist != NULL) func->n_args = varlist->n_children; else func->n_args = 0; func->label = var->data; symbol_insert( var->data, func ); }
void function (node_t *node) { //adds a new scope int temp_offset = var_offset; var_offset = -4; scope_add(); //Skips the first child(name of the function, since it's already been added in bind_functions(node_t). //adds the parameters that lies within the second child. if (node->children[1] != NULL) { int offset = 8 + 4*(node->children[1]->n_children-1); for (int i = 0; i < (node->children[1])->n_children; i++) { symbol_t *new_symbol = (void*)malloc(sizeof(void));; new_symbol->stack_offset = offset; offset = offset - 4; new_symbol->label = NULL; symbol_insert((char*)(node->children[1]->children[i]->data), new_symbol); } } //Skips the block node, which is the third child, and jumps directly to its children, since the scope is already added. for (int i = 0; i < node->children[2]->n_children; i++) { bind_names(node->children[2]->children[i]); } scope_remove(); var_offset = temp_offset; }
void bind_names ( node_t *root ) { symbol_t *symbol = malloc(sizeof(symbol_t)); switch(root->type.index){ case FUNCTION_LIST: scope_add(); current_depth = 0; for(int i = 0; i < root->n_children; i++){ symbol_t *child = malloc(sizeof(symbol_t)); child->label = &root->children[i]->data; symbol_insert(child->label, child); } for(int i = 0; i < root->n_children; i++) if(root->children[i] != NULL) bind_names(root->children[i]); break; case FUNCTION: symbol->label = &root->data; scope_add(); current_depth++; symbol_insert(symbol->label, symbol); for(int i = 0; i < root->n_children; i++){ if(root->children[i] != NULL){ if(root->children[i]->type.index = PARAMETER_LIST){ int32_t local_offset = root->children[i]->n_children*4 + 8; for(int j = 0; j < root->children[i]->n_children; j++){ if(root->children[i]->children[j] != NULL){ symbol_t *parameter = malloc(sizeof(symbol_t*)); parameter->stack_offset = local_offset; local_offset -= 4; parameter->depth = current_depth; root->children[i]->children[j]->entry = ¶meter; symbol_insert(&root->children[i]->children[j]->data, parameter); } } } bind_names(root->children[i]); } } scope_remove(); current_depth--; break; case BLOCK: scope_add(); int local_offset = -4; current_offset = -4; for(int i = 0; i < root->n_children; i++) if(root->children[i] != NULL) bind_names(root->children[i]); scope_remove(); break; } for(int i = 0; i < root->n_children; i++) if(root->children[i] != NULL) bind_names(root->children[i]); }
void bind_names ( node_t* root ) { /* TODO: bind tree nodes to symtab entries */ node_t* child; if(root != NULL) { for(int i = 0; i < root->n_children; i++) { child = root->children[i]; int counter = 0; if(child == NULL) continue; // move along, nothing to see here switch(child->type.index) { case FUNCTION_LIST: // add an upper scope to hold the functions (can functions be defined in a function?) // need this so that the scope in another function can know about all the other functions that are defined. scope_add(); for(int i = 0; i < child->n_children; i++) { // populate symbol table with known functions add_functions(child->children[i]); } bind_names(child); scope_remove(); break; case FUNCTION: ; /* reset depth, reset offset, add arguments */ dybde = 0; local_offset = -4; scope_add(); // add vars in the var list under (arguments) node_t* varlist = child->children[1]; if( varlist != NULL) { int count = 8+(4*(varlist->n_children-1)); for( int i = 0; i < varlist->n_children; i++ ) { char* key = varlist->children[i]->data; symbol_t* s = malloc(sizeof(symbol_t)); s->stack_offset = count; count -= 4; s->n_args = 0; s->depth = dybde; s->label = key; symbol_insert( key, s ); } } // traverse child 2 ( a BLOCK ) bind_names(child->children[2]); scope_remove(); break; case BLOCK: ; /* start new scope, add depth */ scope_add(); dybde++; local_offset = -4; bind_names( child ); dybde--; scope_remove(); break; case DECLARATION_LIST: ; for( int dec = 0; dec < child->n_children; dec++) { node_t* declaration = child->children[dec]; // add vars in the var list under node_t* varlist = declaration->children[0]; for( int i = 0; i < varlist->n_children; i++ ) { char* key = varlist->children[i]->data; symbol_t* s = malloc(sizeof(symbol_t)); s->stack_offset = local_offset; local_offset -= 4; s->n_args = 0; s->depth = dybde; s->label = key; symbol_insert( key, s ); } } //return; // safe to return now ? //bind_names(child); // not needed? break; case VARIABLE: ; char* key = child->data; symbol_t* temp = NULL; symbol_get( &temp, key ); if( temp != NULL ) { child->entry = temp; } else { // symbol not yet declared, do some error ? fprintf( stderr, "Error: Symbol \"%s\" not yet declared\n", key ); } bind_names( child ); break; case TEXT: ; /* set data to index in the table of strings */ uint32_t* index = malloc(sizeof(uint32_t)); *index = strings_add(child->data); child->data = index; break; default: ; bind_names( child ); } } } }
void add_var_to_scope ( node_t *variable_n, int stack_offset ) { symbol_t *var = malloc( sizeof(symbol_t) ); *var = (symbol_t) { stack_offset, -1, NULL }; symbol_insert ( (char *) variable_n->data, var ); }