[Custom Thumbnail]
All the Code of the series can be found at the Github repository:
https://github.com/drifter1/compiler
AST_Node_Decl *temp_decl;
temp_decl = (struct AST_Node_Decl *) node;
for(i = 0; i names_count; i++){
insertVar(temp_decl->names[i]->st_name);
}sprintf(name, "_temp%d", temp_count);
insertVar(name);
temp_count++;if(temp_arithm->op != INC && temp_arithm->op != DEC){
insertEdge(temp_arithm->g_index, getGraphIndex(temp_arithm->left));
insertEdge(temp_arithm->g_index, getGraphIndex(temp_arithm->right));
insertEdge(getGraphIndex(temp_arithm->left), getGraphIndex(temp_arithm->right));
}
else{
insertEdge(temp_arithm->g_index, getGraphIndex(temp_arithm->left));
}
if(temp_bool->op != NOT){
insertEdge(temp_bool->g_index, getGraphIndex(temp_bool->left));
insertEdge(temp_bool->g_index, getGraphIndex(temp_bool->right));
insertEdge(getGraphIndex(temp_bool->left), getGraphIndex(temp_bool->right));
}
else{
insertEdge(temp_bool->g_index, getGraphIndex(temp_bool->left));
}
Rel:
insertEdge(temp_rel->g_index, getGraphIndex(temp_rel->left));
insertEdge(temp_rel->g_index, getGraphIndex(temp_rel->right));
insertEdge(getGraphIndex(temp_rel->left), getGraphIndex(temp_rel->right));
----------------------------------------------------------------------------
Equ:
insertEdge(temp_equ->g_index, getGraphIndex(temp_equ->left));
insertEdge(temp_equ->g_index, getGraphIndex(temp_equ->right));
insertEdge(getGraphIndex(temp_equ->left), getGraphIndex(temp_equ->right));
insertEdge(temp_assign->entry->g_index, getGraphIndex(temp_assign->assign_val));
void main_reg_allocation(AST_Node *node){
static int inst_num = 0;
AST_Node_Declarations *temp_declarations;
AST_Node_Decl *temp_decl;
AST_Node_Arithm *temp_arithm;
AST_Node_Bool *temp_bool;
AST_Node_Rel *temp_rel;
AST_Node_Equ *temp_equ;
AST_Node_Statements *temp_statements;
AST_Node_If *temp_if;
AST_Node_Elsif *temp_elsif;
AST_Node_For *temp_for;
AST_Node_While *temp_while;
AST_Node_Incr *temp_incr;
AST_Node_Assign *temp_assign;
AST_Node_Func_Call *temp_func_call;
AST_Node_Call_Params *temp_call_params;
/* temp variable name */
char name[MAXTOKENLEN];
int i;
/* check if empty */
if(node == NULL){
return;
}
switch(node->type){
/* declarations case */
case DECLARATIONS:
temp_declarations = (struct AST_Node_Declarations *) node;
for(i = 0; i declaration_count; i++){
main_reg_allocation(temp_declarations->declarations[i]);
}
break;
/* declaration case */
case DECL_NODE:
temp_decl = (struct AST_Node_Decl *) node;
for(i = 0; i names_count; i++){
insertVar(temp_decl->names[i]->st_name);
/* graph index */
temp_decl->names[i]->g_index = getVarIndex(temp_decl->names[i]->st_name);
}
break;
/* left and right child cases */
case BASIC_NODE:
main_reg_allocation(node->left);
main_reg_allocation(node->right);
break;
case ARITHM_NODE:
temp_arithm = (struct AST_Node_Arithm *) node;
main_reg_allocation(node->left);
main_reg_allocation(node->right);
/* insert temporary */
sprintf(name, "_temp%d", temp_count);
insertVar(name);
temp_count++;
declare = 1;
insert(name, strlen(name), temp_arithm->data_type, -1);
declare = 0;
/* graph index */
temp_arithm->g_index = var_count - 1;
/* manage graph */
if(temp_arithm->op != INC && temp_arithm->op != DEC){
insertEdge(temp_arithm->g_index, getGraphIndex(temp_arithm->left));
insertEdge(temp_arithm->g_index, getGraphIndex(temp_arithm->right));
insertEdge(getGraphIndex(temp_arithm->left), getGraphIndex(temp_arithm->right));
}
else{
insertEdge(temp_arithm->g_index, getGraphIndex(temp_arithm->left));
}
inst_num++;
break;
case BOOL_NODE:
temp_bool = (struct AST_Node_Bool *) node;
main_reg_allocation(node->left);
main_reg_allocation(node->right);
/* insert temporary */
sprintf(name, "_temp%d", temp_count);
insertVar(name);
temp_count++;
declare = 1;
insert(name, strlen(name), temp_bool->data_type, -1);
declare = 0;
/* graph index */
temp_bool->g_index = var_count - 1;
/* manage graph */
if(temp_bool->op != NOT){
insertEdge(temp_bool->g_index, getGraphIndex(temp_bool->left));
insertEdge(temp_bool->g_index, getGraphIndex(temp_bool->right));
insertEdge(getGraphIndex(temp_bool->left), getGraphIndex(temp_bool->right));
}
else{
insertEdge(temp_bool->g_index, getGraphIndex(temp_bool->left));
}
inst_num++;
break;
case REL_NODE:
temp_rel = (struct AST_Node_Rel *) node;
main_reg_allocation(node->left);
main_reg_allocation(node->right);
/* insert temporary */
sprintf(name, "_temp%d", temp_count);
insertVar(name);
temp_count++;
declare = 1;
insert(name, strlen(name), temp_rel->data_type, -1);
declare = 0;
/* graph index */
temp_rel->g_index = var_count - 1;
/* manage graph */
insertEdge(temp_rel->g_index, getGraphIndex(temp_rel->left));
insertEdge(temp_rel->g_index, getGraphIndex(temp_rel->right));
insertEdge(getGraphIndex(temp_rel->left), getGraphIndex(temp_rel->right));
inst_num++;
break;
case EQU_NODE:
temp_equ = (struct AST_Node_Equ *) node;
main_reg_allocation(node->left);
main_reg_allocation(node->right);
/* insert temporary */
sprintf(name, "_temp%d", temp_count);
insertVar(name);
temp_count++;
declare = 1;
insert(name, strlen(name), temp_equ->data_type, -1);
declare = 0;
/* graph index */
temp_equ->g_index = var_count - 1;
/* manage graph */
insertEdge(temp_equ->g_index, getGraphIndex(temp_equ->left));
insertEdge(temp_equ->g_index, getGraphIndex(temp_equ->right));
insertEdge(getGraphIndex(temp_equ->left), getGraphIndex(temp_equ->right));
inst_num++;
break;
/* reference case */
case REF_NODE:
/* all the entries are already being managed by the Decl case */
break;
/* constant case */
case CONST_NODE:
/* already managed in getGraphIndex */
break;
/* statements case */
case STATEMENTS:
temp_statements = (struct AST_Node_Statements *) node;
for(i = 0; i statement_count; i++){
main_reg_allocation(temp_statements->statements[i]);
}
break;
/* the if case */
case IF_NODE:
temp_if = (struct AST_Node_If *) node;
main_reg_allocation(temp_if->condition);
inst_num++;
main_reg_allocation(temp_if->if_branch);
if(temp_if->elseif_count > 0 ){
for(i = 0; i elseif_count; i++){
main_reg_allocation(temp_if->elsif_branches[i]);
}
}
if(temp_if->else_branch != NULL){
main_reg_allocation(temp_if->else_branch);
}
break;
/* the else if case */
case ELSIF_NODE:
temp_elsif = (struct AST_Node_Elsif *) node;
main_reg_allocation(temp_elsif->condition);
inst_num++;
main_reg_allocation(temp_elsif->elsif_branch);
break;
/* for case */
case FOR_NODE:
temp_for = (struct AST_Node_For *) node;
main_reg_allocation(temp_for->initialize);
inst_num++;
main_reg_allocation(temp_for->condition);
inst_num++;
main_reg_allocation(temp_for->for_branch);
main_reg_allocation(temp_for->increment);
inst_num++;
break;
/* while case */
case WHILE_NODE:
temp_while = (struct AST_Node_While *) node;
main_reg_allocation(temp_while->condition);
inst_num++;
main_reg_allocation(temp_while->while_branch);
break;
/* assign case */
case ASSIGN_NODE:
temp_assign = (struct AST_Node_Assign *) node;
/* manage graph */
insertEdge(temp_assign->entry->g_index, getGraphIndex(temp_assign->assign_val));
main_reg_allocation(temp_assign->assign_val);
break;
/* simple case */
case SIMPLE_NODE:
inst_num++;
break;
/* increment statement */
case INCR_NODE:
temp_incr = (AST_Node_Incr*) node;
inst_num++;
break;
/* function call case */
case FUNC_CALL:
temp_func_call = (struct AST_Node_Func_Call *) node;
if(temp_func_call->num_of_pars != 0){
for(i = 0; i num_of_pars; i++){
main_reg_allocation(temp_func_call->params[i]);
}
}
/* insert temporary when function non-void */
if(temp_func_call->entry->inf_type != VOID_TYPE){
sprintf(name, "_temp%d", temp_count);
insertVar(name);
temp_count++;
declare = 1;
insert(name, strlen(name), temp_func_call->entry->inf_type, -1);
declare = 0;
/* graph index */
temp_func_call->g_index = var_count - 1;
}
inst_num++;
break;
case CALL_PARAMS:
temp_call_params = (struct AST_Node_Call_Params*) node;
if(temp_call_params->num_of_pars > 0){
for(i = 0; i num_of_pars; i++){
main_reg_allocation(temp_call_params->params[i]);
}
}
break;
/* function declaration stuff */
case FUNC_DECLS:
case FUNC_DECL:
case RET_TYPE:
case DECL_PARAMS:
case RETURN_NODE:
/* can't occur in main */
break;
default: /* wrong choice case */
fprintf(stderr, "Error in node selection!\n");
exit(1);
}
}