Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- *********************************************
- * 314 Principles of Programming Languages *
- * Fall 2016 *
- *********************************************
- */
- #include <assert.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include "InstrUtils.h"
- #include "Utils.h"
- void optimizer(Instruction* ptr, int field);
- int main()
- {
- Instruction *head;
- head = ReadInstructionList(stdin);
- if (!head) {
- WARNING("No instructions\n");
- exit(EXIT_FAILURE);
- }
- /* YOUR CODE GOES HERE */
- Instruction* curr = head;
- Instruction* ptr = head;
- /*
- while(curr != NULL){
- curr = curr -> next;
- }
- */
- optimizer(curr, curr -> field1);
- optimizer(curr, curr -> field2);
- optimizer(curr, curr -> field3);
- //iterative method
- /*
- Instruction* instr1;
- Instruction* instr2;
- Instruction* instr3;
- instr1 = head;
- instr2 = head -> next;
- instr3 = head -> next -> next;
- while(instr3){
- if(instr1 -> opcode == LOADI && instr2 -> opcode == LOADI){
- switch(instr3 -> opcode){
- //missing load statement
- case LOADI:
- if(instr3 -> field1 != instr2 -> field1 && instr3 -> field1 != instr1 -> field1){
- printf("FATAL ERROR");
- exit(EXIT_FAILURE);
- break;
- }
- case STORE:
- default:
- break;
- }
- }
- instr1 = instr2;
- instr2 = instr3;
- instr3 = instr3 -> next;
- }
- */
- while(ptr){
- if(ptr -> critical == 'y'){
- PrintInstruction(stdout, ptr);
- }
- ptr = ptr -> next;
- }
- //DestroyInstructionList(head);
- return EXIT_SUCCESS;
- }
- void optimizer(Instruction* ptr, int field){
- if(ptr == NULL) return;
- if(ptr -> field1 == field || ptr -> field2 == field || ptr -> field3 == field){
- ptr -> critical = 'y';
- optimizer (ptr->next, ptr->field1);
- optimizer (ptr->next, ptr->field2);
- optimizer (ptr->next, ptr->field3);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement