Guest User

Untitled

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