Advertisement
Guest User

Untitled

a guest
Oct 24th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. /*
  2. *********************************************
  3. * 314 Principles of Programming Languages *
  4. * Fall 2016 *
  5. *********************************************
  6. */
  7.  
  8. #include <assert.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include "InstrUtils.h"
  12. #include "Utils.h"
  13.  
  14. void optimizer(Instruction* ptr, int field);
  15.  
  16. int main()
  17. {
  18. Instruction *head;
  19.  
  20. head = ReadInstructionList(stdin);
  21. if (!head) {
  22. WARNING("No instructions\n");
  23. exit(EXIT_FAILURE);
  24. }
  25. /* YOUR CODE GOES HERE */
  26.  
  27. Instruction* curr = head;
  28. Instruction* ptr = head;
  29.  
  30. /*
  31. while(curr != NULL){
  32. curr = curr -> next;
  33. }
  34. */
  35. optimizer(curr, curr -> field1);
  36. optimizer(curr, curr -> field2);
  37. optimizer(curr, curr -> field3);
  38.  
  39. //iterative method
  40. /*
  41.  
  42. Instruction* instr1;
  43. Instruction* instr2;
  44. Instruction* instr3;
  45.  
  46. instr1 = head;
  47. instr2 = head -> next;
  48. instr3 = head -> next -> next;
  49.  
  50. while(instr3){
  51.  
  52.  
  53. if(instr1 -> opcode == LOADI && instr2 -> opcode == LOADI){
  54.  
  55. switch(instr3 -> opcode){
  56.  
  57. //missing load statement
  58. case LOADI:
  59. if(instr3 -> field1 != instr2 -> field1 && instr3 -> field1 != instr1 -> field1){
  60. printf("FATAL ERROR");
  61. exit(EXIT_FAILURE);
  62. break;
  63. }
  64. case STORE:
  65.  
  66. default:
  67. break;
  68. }
  69. }
  70.  
  71. instr1 = instr2;
  72. instr2 = instr3;
  73. instr3 = instr3 -> next;
  74. }
  75. */
  76.  
  77. while(ptr){
  78.  
  79. if(ptr -> critical == 'y'){
  80.  
  81. PrintInstruction(stdout, ptr);
  82. }
  83. ptr = ptr -> next;
  84. }
  85.  
  86.  
  87. //DestroyInstructionList(head);
  88. return EXIT_SUCCESS;
  89. }
  90.  
  91. void optimizer(Instruction* ptr, int field){
  92.  
  93. if(ptr == NULL) return;
  94.  
  95. if(ptr -> field1 == field || ptr -> field2 == field || ptr -> field3 == field){
  96.  
  97. ptr -> critical = 'y';
  98.  
  99. optimizer (ptr->next, ptr->field1);
  100. optimizer (ptr->next, ptr->field2);
  101. optimizer (ptr->next, ptr->field3);
  102. }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement