Skip to content

Commit

Permalink
Finished parameter list and parameter scope variable definition
Browse files Browse the repository at this point in the history
  • Loading branch information
jefersonla committed Nov 8, 2016
1 parent c1c19ee commit 426d7f8
Show file tree
Hide file tree
Showing 7 changed files with 266 additions and 69 deletions.
78 changes: 53 additions & 25 deletions codegen_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,14 @@ bool cgenAllCode(TokenNode *root_token){
}

/* Initialize Global Symbol Table */
global_symbol_table = newSymbolTable(NULL);
global_symbol_table = newGlobalSymbolTable();

/* Check if the global symbol was correct initialized */
if(global_symbol_table == NULL){
printError("CANNOT CREATE GLOBAL SYMBOL TABLE.");
return false;
}

/* Configure symbol table as global */
global_symbol_table->start_address = GLOBAL_START_ADRESS;


/* Block token node */
TokenNode *block_token = listGetTokenByIndex(root_token->child_list, 1);

Expand Down Expand Up @@ -496,7 +493,7 @@ bool cgenWhile(TokenNode *while_token, SymbolTable *previous_symbol_table){
}

/* Create a new symbol table */
new_symbol_table = newSymbolTable(previous_symbol_table);
new_symbol_table = newSymbolTable(previous_symbol_table, REGISTER_TYPE_SP);

/* Check the new symbol table */
if(new_symbol_table == NULL){
Expand Down Expand Up @@ -603,7 +600,7 @@ bool cgenFor(TokenNode *for_token, SymbolTable *actual_symbol_table){
addInstructionMainQueue(mips_for_ini);

/* Initialize a new symbol table */
new_symbol_table = newSymbolTable(actual_symbol_table);
new_symbol_table = newSymbolTable(actual_symbol_table, REGISTER_TYPE_SP);

/* Check if new symbol table hasn't failed */
if(new_symbol_table == NULL){
Expand Down Expand Up @@ -777,7 +774,7 @@ bool cgenIf(TokenNode *if_token, SymbolTable *actual_symbol_table){
addInstructionMainQueueFormated(mips_check_if, cond_if_counter, 0);

/* Create a new escope */
new_symbol_table = newSymbolTable(actual_symbol_table);
new_symbol_table = newSymbolTable(actual_symbol_table, REGISTER_TYPE_SP);

/* Check if the new symbol table is correct */
if(new_symbol_table == NULL){
Expand Down Expand Up @@ -864,7 +861,7 @@ bool cgenIf(TokenNode *if_token, SymbolTable *actual_symbol_table){
addInstructionMainQueueFormated(mips_check_if, cond_if_counter, i);

/* Create a new escope */
new_symbol_table = newSymbolTable(actual_symbol_table);
new_symbol_table = newSymbolTable(actual_symbol_table, REGISTER_TYPE_SP);

/* Check if the new symbol table is correct */
if(new_symbol_table == NULL){
Expand Down Expand Up @@ -896,7 +893,7 @@ bool cgenIf(TokenNode *if_token, SymbolTable *actual_symbol_table){
}

/* Create a new escope */
new_symbol_table = newSymbolTable(actual_symbol_table);
new_symbol_table = newSymbolTable(actual_symbol_table, REGISTER_TYPE_SP);

/* Check if the new symbol table is correct */
if(new_symbol_table == NULL){
Expand Down Expand Up @@ -934,10 +931,11 @@ bool cgenIf(TokenNode *if_token, SymbolTable *actual_symbol_table){
bool cgenFunction(TokenNode *function_def_token, SymbolTable *actual_symbol_table) {
int i;
SymbolTable *new_table;
SymbolTable *new_params_table;
TokenNode *t_name;
TokenNode *parameters_list_token;
TokenNode *parameter_token;
TokenNode *block_token;
TokenNode *parameter_token;
TokenNode *parameters_list_token;

/* Check if function def is null */
if(function_def_token == NULL){
Expand All @@ -946,14 +944,26 @@ bool cgenFunction(TokenNode *function_def_token, SymbolTable *actual_symbol_tabl
}

/* New scope symbol_table */
new_table = newSymbolTable(NULL);
new_table = newSymbolTable(NULL, REGISTER_TYPE_SP);

/* Check if the new table is null */
if(new_table == NULL){
printError("NEW SYMBOL TABLE IS NULL!");
return false;
}

/* New parameters list symbol table */
new_params_table = newSymbolTable(NULL, REGISTER_TYPE_FP);

/* Check if the new table is not null */
if(new_params_table == NULL){
printError("PARAMETERS SYMBOL TABLE IS NULL!");
return false;
}

/* Link new_table with the parameters list table */
symbolTableAddBrother(new_table, new_params_table);

/* Get Token name */
t_name = listGetTokenByIndex(function_def_token->child_list, 2);

Expand All @@ -977,19 +987,22 @@ bool cgenFunction(TokenNode *function_def_token, SymbolTable *actual_symbol_tabl
if(parameters_list_token->token_type == TI_LISTADENOMES){

/* If we have a list of names we need to start from the last one to the first */
for(i = parameters_list_token->child_list->length; i > 0; i++){
for(i = parameters_list_token->child_list->length; i > 0; i--){

/* Get the token */
parameter_token = listGetTokenByIndex(parameters_list_token->child_list, i);

/* Add this token as a symbol in the symbol table */
symbolTableAddSymbol(new_table, parameter_token->lex_str, NUMBER_TYPE);
/* Check if this parameter is a t_name */
if(parameter_token->token_type == T_NAME){
/* Add this token as a symbol in the symbol table */
symbolTableAddSymbol(new_params_table, parameter_token->lex_str, NUMBER_TYPE);
}
}
}
else{
/* If we have only one name we just add this name to the new table */
parameter_token = parameters_list_token;
symbolTableAddSymbol(new_table, parameter_token->lex_str, NUMBER_TYPE);
symbolTableAddSymbol(new_params_table, parameter_token->lex_str, NUMBER_TYPE);
}

/* Get the block token */
Expand Down Expand Up @@ -1019,10 +1032,16 @@ bool cgenFunction(TokenNode *function_def_token, SymbolTable *actual_symbol_tabl

/* Finish function definition poping Record Activation */
addInstructionMainQueueFormated(mips_end_function_def, (t_name->lex_str));

/* Pop parameters on stack */
addInstructionMainQueueFormated(mips_pop_params, (new_params_table->shift_address));

/* Add final part */
addInstructionMainQueueFormated(mips_end_function_def2, (t_name->lex_str));

/* Delete local symbol table */
/* Delete local symbol table and parameters symbol table */
deleteSymbolTable(&new_table);
deleteSymbolTable(&new_params_table);

/* Return success */
return true;
Expand Down Expand Up @@ -1143,7 +1162,7 @@ bool cgenBlockCode(TokenNode *block_token, SymbolTable *previous_scope){
}

/* Actual Symbol Table */
actual_symbol_table = newSymbolTable(previous_scope);
actual_symbol_table = newSymbolTable(previous_scope, REGISTER_TYPE_SP);

/* Command list token */
command_list_token = listGetTokenByIndex(block_token->child_list, 1);
Expand Down Expand Up @@ -1481,14 +1500,23 @@ bool cgenExpression(TokenNode *exp_token, SymbolTable *symbol_table) {
symbol_node = symbolTableGetSymbolNodeByName(global_symbol_table, token_terminal->lex_str);
}

/* If this symbol is not on global symbol table, so it's nil */
/* If this symbol is not on global symbol table, so it's nil, and will be declared on global table */
if(symbol_node == NULL){
addInstructionMainQueue(mips_nil);
}
else{
/* Store the correct load instruction */
instructionQueueEnqueueInstructionNode(main_instruction_queue, symbolNodeGetLoadInstruction(symbol_node));
/* Add Symbol to table */
symbolTableAddSymbol(global_symbol_table, token_terminal->lex_str, NUMBER_TYPE);

/* Get the recent added symbol and assign nil to this variable */
symbol_node = symbolTableGetSymbolNodeByName(global_symbol_table, token_terminal->lex_str);

/* Check if new symbol node is valid */
if(symbol_node == NULL){
printError("CANNOT CREATE NEW SYMBOL NODE ON GLOBAL SYMBOL TABLE!");
return false;
}
}

/* Store the correct load instruction */
instructionQueueEnqueueInstructionNode(main_instruction_queue, symbolNodeGetLoadInstruction(symbol_node));
break;
case TI_CALL_FUNCTION:
case TI_CALL_FUNCTION_PAR:
Expand Down
22 changes: 18 additions & 4 deletions codegen_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,16 @@
/* Code Generator Constants */
/* ------------------------------------------------------------- */

/* Global Start Address */
#define GLOBAL_START_ADRESS -1
/* Register Types */

/* Register Type GP (Global Pointer) */
#define REGISTER_TYPE_GP 1

/* Register Type SP (Stack Pointer) */
#define REGISTER_TYPE_SP 2

/* Register Type FP (Frame Pointer) */
#define REGISTER_TYPE_FP 3

/* Function Prefix */
#define FUNCTION_PREFIX "function_"
Expand All @@ -29,10 +37,13 @@
#define NUMBER_TYPE 1

/* Standard way to add itens to main queue */
#define addInstructionMainQueue(VAR) instructionQueueEnqueueInstruction(main_instruction_queue, formatedInstruction(VAR), false)
#define addInstructionMainQueue(VAR) instructionQueueEnqueueInstruction( main_instruction_queue, \
formatedInstruction(VAR), false)

/* New array of instructions with variadic parameters */
#define addInstructionMainQueueFormated(VAR, ...) instructionQueueEnqueueInstruction(main_instruction_queue, formatedInstruction(VAR, ##__VA_ARGS__), false)
#define addInstructionMainQueueFormated(VAR, ...) instructionQueueEnqueueInstruction( main_instruction_queue, \
formatedInstruction(VAR, ##__VA_ARGS__), \
false)

/* ------------------------------------------------------------- */
/* Global Structures */
Expand Down Expand Up @@ -139,6 +150,9 @@ extern const char mips_push_a0[];
/* Pop stack value */
extern const char mips_pop[];

/* Pop stack params */
extern const char mips_pop_params[];

/* Load top value to $t1 */
extern const char mips_top_t1[];

Expand Down
22 changes: 16 additions & 6 deletions codegen_models.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const char mips_main[] =
"# -- System Main Definition -- #\n"
"main:\n"
"\n"
"\tjal main_user # Jumpt to main user function\n"
"\tjal main_user # Jump to main user function\n"
"\n"
"# Close Main Declaration\n"
"end_main:\n"
Expand Down Expand Up @@ -172,13 +172,13 @@ const char mips_global_load[] =
/* Store a local variable */
const char mips_local_store[] =
"\t# --------- Store $a0 in local variable --------- #\n"
"\tsw $a0, %d($sp)\n"
"\tsw $a0, %d($%cp)\n"
"\t# ----------------------------------------------- #\n";

/* Load a local variable into $a0 */
const char mips_local_load[] =
"\t# --------- Load local variable in $a0 ---------- #\n"
"\tlw $a0, %d($sp)\n"
"\tlw $a0, %d($%cp)\n"
"\t# ----------------------------------------------- #\n";

/* Push temporary return of a expression */
Expand All @@ -194,6 +194,12 @@ const char mips_pop[] =
"\taddiu $sp, $sp, " TO_STRING(BYTE_VARIABLE_SIZE) "\n"
"\t# ----------------------------------------------- #\n";

/* Pop stack params */
const char mips_pop_params[] =
"\t# ---------------- Pop Params Stack ------------- #\n"
"\taddiu $sp, $sp, %d\n"
"\t# ----------------------------------------------- #\n";

/* Load top value to $t1 */
const char mips_top_t1[] =
"\t# ------------- Top of stack to $t1 ------------- #\n"
Expand Down Expand Up @@ -567,13 +573,17 @@ const char mips_start_function_def2[] =
"\tsw $ra, 4($sp)\n"
"\tsw $fp, 8($sp)\n";

/* -- BLOCK -- */

/* End of function definition */
const char mips_end_function_def[] =
"end_function_%s:\n"
"\tlw $ra, 4($fp)\n"
"\tlw $ra, 4($sp)\n"
"\tlw $fp, 8($sp)\n"
"\taddiu $sp, $sp, -8\n";

"\taddiu $sp, $sp, 8\n";

/* -- POP PARAMETERS -- */

/* End of function definition part 2 */
const char mips_end_function_def2[] =
"\tjr $ra\n"
Expand Down
33 changes: 24 additions & 9 deletions test2.lua
Original file line number Diff line number Diff line change
Expand Up @@ -119,30 +119,45 @@ print(1 ~= 1) -- 0
-- Separator
print(1234567890)

-- teste while --
--[[
-- Test functions OK
function why()
while(0) do
print(3)
end
print(11)
z = 4
while(z ~= 10) do
print(2 + z)
z = z + 1
end
end

why()
]]


-- Separator
print(1234567890)

-- Test of while, with some assigns
while(0) do -- should don't enter here
print(3)
end

x = 4

while(x ~= 0) do -- should don't stop
print(4)
while(x ~= 0) do -- OK!
x = x - 1
print(45)
end

--[[
-- Separator
-- print(1234567890)
]]
-- Separator
print(1111111111)

-- Test of functions with parameters OK
function kk(xxx, yyy)
print(xxx + yyy)
end

xxx = 150

kk(100, 200)
Loading

0 comments on commit 426d7f8

Please sign in to comment.