/* Runs through the y86 file, and assign an address to each label Also stores each source line in source_lines by calling add_source_line (this just happens to be the easiest place to do so) */ void parse_labels(FILE *str_in) { char line[4096]; int len, cur_addr = 0; Label *cur_label = NULL; assert(str_in != NULL); while (read_y86_line(str_in, line, sizeof(line))) { if (cur_addr > 4096) { DBG_PRINT("cur_addr exceeds 4096\n"); exit(0); } len = strlen(line); DBG_PRINT("Read in line: %s\n", line); if (len == 0) continue; /* blank line or line with only a comment, so skip */ add_source_line(line, cur_addr); if (str_ends_with(line, ':')) { // this is a label line line[len-1] = '\0'; cur_label = malloc(sizeof(Label)); strcpy(cur_label->name, line); cur_label->addr = cur_addr; DBG_PRINT("Got a label line %s at address %x\n", line, cur_addr); labels = realloc(labels, (num_labels + 1) * sizeof(Label)); labels[num_labels++] = cur_label; } else { // this is not a label line, but we still need to keep counting the current address so we know where we are DBG_PRINT("cur instr size = %d, cur addr = %x\n", get_instr_size(line, cur_addr), cur_addr); cur_addr += get_instr_size(line, cur_addr); /* line now contains only the instruction */ } } }
int get_node_size(node_t cur_node) { int cond_size = 0; int true_size = 0; if (cur_node == NULL) return 0; if (cur_node->conditional_expr != NULL) { if (!cur_node->cond_executed) { cur_node->cond_executed = 1; cond_size = get_node_size(cur_node->conditional_expr); } else { cur_node->cond_executed = 0; return 0; } } if (cur_node->instruction_process->instruction_type->type == WHILE){ if (!cur_node->while_executed) { cur_node->while_executed = 1; true_size = get_node_size(cur_node->true_node); return get_node_size(cur_node->false_node) + cond_size + true_size + sizeof(graph_node) + get_instr_size(cur_node->instruction_process); } else { cur_node->while_executed = 0; return 0; } } return get_node_size(cur_node->true_node) + cond_size + sizeof(graph_node) + get_instr_size(cur_node->instruction_process); }