Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. /*
  2. *********************************************
  3. * 314 Principles of Programming Languages *
  4. * Spring 2017 *
  5. * Author: Ulrich Kremer *
  6. * Student Version *
  7. *********************************************
  8. */
  9.  
  10. #include <assert.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include "InstrUtils.h"
  14. #include "Utils.h"
  15.  
  16. Instruction *RemoveInstr(Instruction* head,Instruction* target);
  17. void EvaluateInstr(Instruction* target,Instruction*head);
  18. void SearchMemoryInstr (Instruction* start, Instruction* head,int reg, int offset);
  19. void SearchRegisterInstr(Instruction* start, Instruction* head,int reg);
  20.  
  21.  
  22. int main()
  23. {
  24. Instruction *head,*tail,*ptr;
  25.  
  26. puts("<1>");
  27. head = ReadInstructionList(stdin);
  28. puts("<1>");
  29. if (!head) {
  30. WARNING("No instructions\n");
  31. exit(EXIT_FAILURE);
  32. }
  33. puts("<2>");
  34.  
  35. tail = head->prev;
  36. ptr = head;
  37. do{
  38. ptr -> critical = 'n';
  39. ptr = ptr->next;
  40. }
  41. while(ptr!=head);
  42. ptr = head->prev;
  43. do{
  44. EvaluateInstr(ptr,head);
  45. if(ptr->critical=='n'){
  46. RemoveInstr(head,ptr);
  47. }
  48. }while(ptr!=tail);
  49.  
  50.  
  51. /* YOUR CODE GOES HERE */
  52.  
  53. if (head)
  54. PrintInstructionList(stdout, head);
  55.  
  56. return EXIT_SUCCESS;
  57. }
  58.  
  59. //deletes Instruction and returns new head if there is only one target left in the linked list then it will return Null
  60. Instruction *RemoveInstr(Instruction* head,Instruction* target){
  61.  
  62. if(target->next==target){
  63. free(target);
  64. printf("Deleted final isntruction. Prepare for Errors");
  65. return NULL;
  66. }
  67.  
  68. (target->prev)->next = target -> next;
  69. (target->next)->prev= target -> prev;
  70.  
  71. printf("Deleting Instruction - #%d\n",target->opcode);
  72. if(target == head){
  73. Instruction* holder = target->next;
  74. free(target);
  75. return holder;
  76. }
  77. else{
  78. free(target);
  79. return head;
  80. }
  81.  
  82. }
  83. //marks appropriate values as critical
  84. void EvaluateInstr(Instruction* target,Instruction* head){
  85. if(target->opcode==OUTPUTAI){
  86. target->critical = 'y';
  87. SearchMemoryInstr(target,head,target->field1,target->field2);
  88. return;
  89. }
  90. if(target->critical=='n')
  91. return;
  92. switch(target->opcode){
  93. case LOADI:
  94. break;
  95. case LOADAI:
  96. SearchMemoryInstr(target,head,target->field1,target->field2);
  97. break;
  98. case STOREAI:
  99. SearchRegisterInstr(target,head,target->field1);
  100. break;
  101. case ADD:
  102. case SUB:
  103. case MUL:
  104. case DIV:
  105. SearchRegisterInstr(target,head,target->field1);
  106. SearchRegisterInstr(target,head,target->field2);
  107. break;
  108. default:
  109. break;
  110. }
  111. return;
  112. }
  113. //Searches for an instr that sets the location passed in memory
  114. //returns 1 if found
  115. //returns 0 if did not find
  116. void SearchMemoryInstr (Instruction* start, Instruction* head,int reg, int offset){
  117. Instruction* ptr = start;
  118. while(ptr!=head->prev){
  119. if(ptr->opcode==STOREAI){
  120. if(ptr->field2==reg &&ptr->field3==offset){
  121. printf("BUGTEST: MARKED INSTR %d, AS CRITICAL/n",ptr->opcode);
  122. ptr-> critical ='y';
  123. return;
  124. }
  125. }
  126. ptr = ptr->prev;
  127. }
  128. printf("ERROR did not find register%d,offset%d\n",reg,offset);
  129. return;
  130. }
  131. void SearchRegisterInstr(Instruction* start, Instruction* head,int reg){
  132. Instruction* ptr = start;
  133. while(ptr!=head->prev){
  134. switch(start->opcode){
  135. case LOADI:
  136. if(ptr->field2==reg){
  137. printf("BUGTEST: MARKED INSTR %d, AS CRITICAL/n",ptr->opcode);
  138. ptr->critical='y';
  139. return;
  140. }
  141. break;
  142. case LOADAI:
  143. if(ptr->field3==reg){
  144. printf("BUGTEST: MARKED INSTR %d, AS CRITICAL/n",ptr->opcode);
  145. ptr->critical='y';
  146. return;
  147. }
  148. case ADD:
  149. case SUB:
  150. case MUL:
  151. case DIV:
  152. if(ptr->field3==reg){
  153. printf("BUGTEST: MARKED INSTR %d, AS CRITICAL/n",ptr->opcode);
  154. ptr->critical='y';
  155. return;
  156. }
  157. break;
  158. default:
  159. break;
  160. }
  161. ptr = ptr->prev;
  162. }
  163. printf("ERROR did not find register%d\n",reg);
  164. return;
  165.  
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement