int main() { Instruction *head; head = ReadInstructionList(stdin); /* --- FIRST: ALGEBRAIC SIMPLIFICATION PASS --- */ if (!head) { ERROR("No instructions\n"); exit(EXIT_FAILURE); } handle_simplification(head); /* --- SECOND: CONSTANT FOLDING PASS --- */ if (!head) { ERROR("No instructions\n"); exit(EXIT_FAILURE); } handle_folding(head); PrintInstructionList(stdout, head); DestroyInstructionList(head); return EXIT_SUCCESS; }
int main() { Instruction *head; Instruction *curr, *prev, *next; head = ReadInstructionList(stdin); if (!head) { ERROR("No instructions\n"); exit(EXIT_FAILURE); } curr = head; prev = head->prev; next = head->next; while(curr && next){ if(prev && prev->opcode == LOADI && curr && curr->opcode == LOADI){ switch(next->opcode){ /*add*/ case ADD: curr->field1 = next->field1; curr->field2 = prev->field2 + curr->field2; /*sub*/ case SUB: curr->field1 = next->field1; if(next->field2 > next->field3){ curr->field2 = curr->field2 - prev->field2; } else{ curr->field2 = curr->field2 - prev->field2; } /*mul*/ case MUL: curr->field1 = next->field1; curr->field2 = curr->field2 * prev->field2; curr->next = next->next; curr->next->prev = curr; curr->prev = prev->prev; curr->prev->next = curr; free(prev); free(next); default: break; } } curr = curr->next; prev = curr->prev; next = curr->next; } PrintInstructionList(stdout, head); DestroyInstructionList(head); return EXIT_SUCCESS; }
int main() { Instruction *head; int* regs; int numRegs, i; numRegs = 0; regs = NULL; head = ReadInstructionList(stdin); if (!head) { WARNING("No instructions\n"); exit(EXIT_FAILURE); } /* YOUR CODE GOES HERE */ // Prereq: Know how many registers there are numRegs = findNumOfRegisters(head); // Build array with length numRegs // Start with all -1, if unused register // regs[0] corresponds to r1 // regs[i] corresponds to r(i+1) regs = (int*) malloc(sizeof(int) * numRegs); for (i = 0; i < numRegs; i++) regs[i] = -1; // Find all outputs // First: make an array of all regs that contribute to outputs // regs[i] = -1 : Unused, can delete // regs[i] = 1 : Used, keep checkIfRegIsUsed(head, regs); // Second: delete all unused regs for (i = 0; i < numRegs; i++) if (regs[i] != 1) deleteOccurrencesOfRegister(head, i+1, regs[i]); // It should noe be optimized if (head) PrintInstructionList(stdout, head); return EXIT_SUCCESS; }
int main() { Instruction *head; head = ReadInstructionList(stdin); if (!head) { WARNING("No instructions\n"); exit(EXIT_FAILURE); } Instruction* ptr=head; Instruction* nextInstr =NULL; Instruction* prevInstr=NULL; /* YOUR CODE GOES HERE */ while(ptr!=NULL) { if(ptr->opcode == LOADI) { if(ptr->next != NULL) { if(ptr->next->opcode == LOADI) { nextInstr=ptr->next; prevInstr=ptr->next->next; if(ptr->field2 == prevInstr->field1 && nextInstr->field2 == prevInstr->field2){ if(prevInstr->opcode == ADD || prevInstr->opcode == SUB || prevInstr->opcode == MUL){ if(prevInstr->opcode == ADD) { ptr->field1 = ptr->field1 + nextInstr->field1; ptr->field2 = prevInstr->field3; ptr->next= prevInstr -> next; prevInstr->next->prev = ptr; } else if(prevInstr->opcode == SUB) { ptr->field1 = ptr->field1 - nextInstr->field1; ptr->field2 = prevInstr->field3; ptr->next= prevInstr -> next; prevInstr->next->prev = ptr; } else if(prevInstr->opcode == MUL) { ptr->field1 = ptr->field1 * nextInstr->field1; ptr->field2 = prevInstr->field3; ptr->next= prevInstr -> next; prevInstr->next->prev = ptr; } free(nextInstr); free(prevInstr); } } } } } nextInstr=NULL; prevInstr=NULL; ptr=ptr->next; }//while if (head) PrintInstructionList(stdout, head); return EXIT_SUCCESS; }