Guest User

Untitled

a guest
Apr 25th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.46 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "s3inst.h"
  5. #include "functions.h"
  6.  
  7. extern inst_t instList;
  8. extern int count;
  9.  
  10. usedef_t *table;
  11.  
  12.  
  13. int firstInstruction() {
  14.  
  15. int index;
  16. inst_t current = instList;
  17.  
  18. while (!(current->label))
  19. current = current->next;
  20.  
  21. index = current->count;
  22.  
  23. return index;
  24. }
  25.  
  26.  
  27. void unionArrays(int *destArray, int *srcArray, int numRegs) {
  28.  
  29. int i;
  30.  
  31. for (i = 0; i < numRegs; i++) {
  32. if (srcArray[i] == 1)
  33. destArray[i] = 1;
  34. }
  35.  
  36. return;
  37. }
  38.  
  39.  
  40. int compareArrays(int *firstArray, int *secondArray, int numRegs) {
  41.  
  42. int i;
  43.  
  44. for (i = 0; i < numRegs; i++) {
  45. if (secondArray[i] != firstArray[i])
  46. return 1;
  47. }
  48. return 0;
  49. }
  50.  
  51.  
  52. void copyArray(int *firstArray, int *secondArray, int numRegs) {
  53.  
  54. int i;
  55.  
  56. for (i = 0; i < numRegs; i++) {
  57. secondArray[i] = firstArray[i];
  58. }
  59.  
  60. return;
  61. }
  62.  
  63.  
  64. void genUseDef()
  65. {
  66. inst_t current = instList;
  67.  
  68. while(current != NULL)
  69. {
  70. if(current->op == OP_BR)
  71. {
  72. if(current->ops[0].t == op_reg)
  73. table[current->count].use0 = current->ops[0].reg;
  74. }
  75. else if(current->op == OP_STR)
  76. {
  77. if(current->ops[0].t == op_reg)
  78. table[current->count].use0 = current->ops[0].reg;
  79. if(current->ops[1].t == op_reg)
  80. table[current->count].use1 = current->ops[1].reg;
  81. }
  82. else if(current->op == OP_IN)
  83. {
  84. table[current->count].def = 0;
  85. }
  86. else if(current->op == OP_OUT)
  87. {
  88. table[current->count].use0 = 0;
  89. }
  90. else if(current->op == OP_SET)
  91. {
  92. table[current->count].def = current->ops[0].reg;
  93. }
  94. else if(current->op != OP_BRA && current->op != OP_RET)
  95. {
  96. if(current->ops[0].t == op_reg)
  97. table[current->count].def = current->ops[0].reg;
  98. if(current->ops[1].t == op_reg)
  99. table[current->count].use0 = current->ops[1].reg;
  100. if(current->ops[2].t == op_reg)
  101. table[current->count].use1 = current->ops[2].reg;
  102. }
  103.  
  104. current = current->next;
  105. }
  106. }
  107.  
  108.  
  109. void successors(block_array *cfg) {
  110.  
  111. block *node = cfg->label_list[0];
  112. int curr_index = 0;
  113. int i;
  114.  
  115. while (node != NULL) {
  116.  
  117. if (node->instruction->label) {
  118. for (i = 0; i < cfg->num_of_labels; i++) {
  119. if (strcmp(cfg->label_list[i]->instruction->label, node->instruction->label) == 0) {
  120. curr_index = i;
  121. break;
  122. }
  123. }
  124. }
  125.  
  126. if (node->instruction->op == OP_BR) {
  127. table[node->instruction->count].succ0 = node->left->instruction->count;
  128. table[node->instruction->count].succ1 = node->right->instruction->count;
  129. node = node->right;
  130. } else if (node->instruction->op == OP_RET) {
  131. table[node->instruction->count].succ0 = -1;
  132. table[node->instruction->count].succ1 = -1;
  133. break;
  134. } else if (node->instruction->op == OP_BRA) {
  135. table[node->instruction->count].succ0 = node->left->instruction->count;
  136. table[node->instruction->count].succ1 = -1;
  137. node = cfg->label_list[curr_index + 1];
  138. } else {
  139. table[node->instruction->count].succ0 = node->left->instruction->count;
  140. table[node->instruction->count].succ1 = -1;
  141. node = node->left;
  142. }
  143. }
  144.  
  145. return;
  146. }
  147.  
  148.  
  149. void liveness()
  150. {
  151. int i, j;
  152. int changed = 1;
  153. int numRegs = number_of_registers();
  154.  
  155. int **liveInArray, **liveOutArray;
  156. int *temp;
  157.  
  158. instr_set* liveIn;
  159. instr_set* liveOut;
  160.  
  161. liveIn = (instr_set*)malloc(count*sizeof(instr_set));
  162. liveOut = (instr_set*)malloc(count*sizeof(instr_set));
  163.  
  164. liveInArray = (int**)malloc(count*sizeof(int*));
  165. liveOutArray = (int**)malloc(count*sizeof(int*));
  166. temp = (int*)calloc(numRegs, sizeof(int));
  167.  
  168. for(i=0;i<count;i++)
  169. {
  170. liveInArray[i] = (int*)calloc(numRegs, sizeof(int));
  171. liveOutArray[i] = (int*)calloc(numRegs, sizeof(int));
  172. }
  173.  
  174.  
  175. while(changed == 1) // iterate while no changes made
  176. {
  177. changed = 0;
  178.  
  179. // calc live ins
  180. for(i=3;i<count;i++) // iterate through all instructions
  181. {
  182. copyArray(liveOutArray[i], temp, numRegs); // make temp array
  183.  
  184. temp[table[i].def] = 0;
  185.  
  186. if(table[i].use0 != -1)
  187. temp[table[i].use0] = 1;
  188. if(table[i].use1 != -1)
  189. temp[table[i].use1] = 1;
  190.  
  191. changed = compareArrays(liveOutArray[i], temp, numRegs);
  192.  
  193. copyArray(temp, liveInArray[i], numRegs);
  194. }
  195.  
  196. // calc live outs
  197. for(i=3;i<count;i++) // iterate through all instructions
  198. {
  199. for(j=0;j<numRegs;j++) // clear array
  200. temp[j]=0;
  201.  
  202. if(table[i].succ0 != -1)
  203. unionArrays(temp, liveInArray[table[i].succ0], numRegs);
  204. if(table[i].succ1 != -1)
  205. unionArrays(temp, liveInArray[table[i].succ1], numRegs);
  206.  
  207. if(compareArrays(liveOutArray[i], temp, numRegs) == 1)
  208. changed = 1;
  209.  
  210. copyArray(temp, liveOutArray[i], numRegs);
  211. }
  212. }
  213.  
  214.  
  215.  
  216. }
  217.  
  218.  
  219. void calcLiveness(block_array* cfg)
  220. {
  221. int i;
  222.  
  223. table = (usedef_t*)malloc(count*sizeof(usedef_t));
  224.  
  225. for(i=0;i<count;i++)
  226. {
  227. table[i].use0 = -1;
  228. table[i].use1 = -1;
  229. table[i].def = -1;
  230. table[i].succ0 = -1;
  231. table[i].succ1 = -1;
  232. }
  233.  
  234. genUseDef();
  235. successors(cfg);
  236.  
  237. liveness();
  238.  
  239. for(i=3;i<count;i++)
  240. {
  241. printf("%03d: use0: %02d use1: %02d def: %02d succ0: %02d succ1: %02d\n",
  242. i, table[i].use0, table[i].use1, table[i].def, table[i].succ0, table[i].succ1);
  243. }
  244. }
Add Comment
Please, Sign In to add comment