No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

gen_assembleur.c 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. #include "gen_assembleur.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. char * operationName(enum operation op){
  5. switch(op){
  6. case EQ:
  7. return "EQ";
  8. case NEQ:
  9. return "NEQ";
  10. case LT:
  11. return "LT";
  12. case GT:
  13. return "GT";
  14. case LTE:
  15. return "LTE";
  16. case GTE:
  17. return "GTE";
  18. case ADD:
  19. return "ADD";
  20. case SOU:
  21. return "SOU";
  22. case DIV:
  23. return "DIV";
  24. case MUL:
  25. return "MUL";
  26. case COP:
  27. return "COP";
  28. case AFC:
  29. return "AFC";
  30. case RET:
  31. return "RET";
  32. case JMF:
  33. return "JPF";
  34. case JMP:
  35. return "JPM";
  36. case AND:
  37. return "AND";
  38. case OR:
  39. return "OR";
  40. case NOT:
  41. return "NOT";
  42. case PRI:
  43. return "PRI";
  44. default:
  45. break;
  46. }
  47. return "";
  48. }
  49. void initialise_asm(instructions_array * array){
  50. array->index = 0;
  51. }
  52. int add_instruction(instructions_array * array, instruction * instru){
  53. if (array->index >= INSTRUCTION_TABLE_SIZE){
  54. return 1;
  55. }
  56. array->array[array->index] = *instru;
  57. array->index++;
  58. return 0;
  59. }
  60. int new_temp(Table_Symboles * table){
  61. int ret_addr ;
  62. if(add_symbole_bottom(table) == -1) {
  63. return -1;
  64. }
  65. ret_addr = table->indexAvailableBottom + 1;
  66. return ret_addr;
  67. }
  68. int generate_instruction_1(instructions_array * array, enum operation op, int arg1){
  69. instruction instru;
  70. char * opName = operationName(op);
  71. instru.operation = op;
  72. instru.reg1 = arg1;
  73. printf("%d\t %s %d\n", array->index, opName, instru.reg1);
  74. if (add_instruction(array, &instru) != 0){
  75. //TODO: Error handling
  76. exit(1);
  77. }
  78. return 0;
  79. }
  80. int generate_instruction_2(instructions_array * array, enum operation op, int arg1, int arg2){
  81. instruction instru;
  82. char * opName = operationName(op);
  83. instru.operation = op;
  84. instru.reg1 = arg1;
  85. instru.reg2 = arg2;
  86. printf("%d\t %s %d %d\n", array->index, opName, instru.reg1, instru.reg2);
  87. if (add_instruction(array, &instru) != 0){
  88. //TODO: Error handling
  89. exit(1);
  90. }
  91. return 0;
  92. }
  93. int generate_instruction_3(instructions_array * array, enum operation op, int arg1, int arg2, int arg3){
  94. instruction instru;
  95. char * opName = operationName(op);
  96. instru.operation = op;
  97. instru.reg1 = arg1;
  98. instru.reg2 = arg2;
  99. instru.reg3 = arg3;
  100. printf("%d\t %s %d %d %d\n", array->index, opName, instru.reg1, instru.reg2, instru.reg3);
  101. if (add_instruction(array, &instru) != 0){
  102. //TODO: Error handling
  103. exit(1);
  104. }
  105. return 0;
  106. }
  107. void update_jmf(instructions_array * array, int instru_index, int adr_jmp){
  108. array->array[instru_index - 1].reg2 = adr_jmp;
  109. printf("%d\t JMP %d %d\n", (instru_index - 1), array->array[instru_index].reg1, array->array[instru_index].reg2);
  110. }
  111. void exportInstructions(instructions_array * array){
  112. FILE *file;
  113. file = fopen("instructions.txt", "w");
  114. instruction instru;
  115. enum operation op;
  116. for (int i = 0; i < array->index; i++){
  117. instru = array->array[i];
  118. op = instru.operation;
  119. switch (op) {
  120. //1 parameter
  121. case JMP:
  122. case PRI:
  123. case RET:
  124. fprintf(file, "%d\t %s %d\n", i, operationName(op), instru.reg1);
  125. break;
  126. //2 parameters
  127. case JMF:
  128. case NOT:
  129. case AFC:
  130. case COP:
  131. fprintf(file, "%d\t %s %d %d\n", i, operationName(op), instru.reg1, instru.reg2);
  132. break;
  133. //3 parameters
  134. case ADD:
  135. case SOU:
  136. case DIV:
  137. case MUL:
  138. case AND:
  139. case OR:
  140. case EQ:
  141. case NEQ:
  142. case LT:
  143. case LTE:
  144. case GT:
  145. case GTE:
  146. fprintf(file, "%d\t %s %d %d %d\n", i, operationName(op), instru.reg1, instru.reg2, instru.reg3);
  147. break;
  148. default:
  149. break;
  150. }
  151. }
  152. fclose(file);
  153. }
  154. /*int gen_print(Table_Symboles * table, instructions_array * array, int arg1){
  155. instruction instru;
  156. instru.operation = PRI;
  157. instru.reg1 = arg1;
  158. printf("%d\t PRI %d\n", array->index, instru.reg1);
  159. if (array->index < INSTRUCTION_TABLE_SIZE){
  160. array->array[array->index] = instru;
  161. array->index++;
  162. }
  163. free_temp(table);
  164. }*/
  165. /*void gen_arithmetique(instructions_array * array, enum operation op, int arg1, int arg2){
  166. instruction instru;
  167. instru.reg1 = arg1;
  168. instru.reg2 = arg1;
  169. instru.reg3 = arg2;
  170. char * opName = operationName(op);
  171. printf("%d\t %s %d %d %d\n", array->index, opName, arg1, arg1, arg2);
  172. if (array->index < INSTRUCTION_TABLE_SIZE){
  173. array->array[array->index] = instru;
  174. array->index++;
  175. }
  176. }
  177. int gen_var(Table_Symboles * table, instructions_array * array, char * varName){
  178. int vt = new_temp(table);
  179. int varAddr = variable_exists(table, varName);
  180. //vérifier que non null
  181. instruction instru;
  182. instru.operation = COP;
  183. instru.reg1 = vt;
  184. instru.reg2 = varAddr;
  185. printf("%d\t COP %d %d\n", array->index, vt, varAddr);
  186. if (array->index < INSTRUCTION_TABLE_SIZE){
  187. array->array[array->index] = instru;
  188. array->index++;
  189. }
  190. return vt;
  191. }
  192. int gen_entier(Table_Symboles * table, instructions_array * array, int entier){
  193. int vt = new_temp(table);
  194. //vérifier que non null
  195. instruction instru;
  196. instru.operation = AFC;
  197. instru.reg1 = vt;
  198. instru.reg2 = entier;
  199. printf("%d\t AFC %d %d\n", array->index, vt, entier);
  200. if (array->index < INSTRUCTION_TABLE_SIZE){
  201. array->array[array->index] = instru;
  202. array->index++;
  203. }
  204. return vt;
  205. }
  206. int gen_condition(Table_Symboles * table, instructions_array * array, enum operation op, int arg1, int arg2){
  207. char * opName = operationName(op);
  208. instruction instru;
  209. instru.operation = op;
  210. instru.reg1 = arg1;
  211. instru.reg2 = arg1;
  212. if (op != NOT){
  213. instru.reg3 = arg2;
  214. printf("%d\t %s %d %d %d\n", array->index, opName, instru.reg1, instru.reg2, instru.reg3);
  215. free_temp(table);
  216. } else {
  217. printf("%d\t %s %d %d \n", array->index, opName, instru.reg1, instru.reg2);
  218. }
  219. if (array->index < INSTRUCTION_TABLE_SIZE){
  220. array->array[array->index] = instru;
  221. array->index++;
  222. }
  223. return instru.reg1;
  224. }
  225. int gen_return(Table_Symboles * table, instructions_array * array, int adr){
  226. //vérifier que non null
  227. instruction instru;
  228. instru.operation = RET;
  229. instru.reg1 = adr;
  230. printf("%d\t RET %d\n", array->index, adr);
  231. if (array->index < INSTRUCTION_TABLE_SIZE){
  232. array->array[array->index] = instru;
  233. array->index++;
  234. }
  235. //free_temp(table);
  236. return adr;
  237. }
  238. int gen_jmpf(Table_Symboles * table, instructions_array * array, int cond, int dest){
  239. //vérifier que non null
  240. instruction instru;
  241. instru.operation = JMF;
  242. instru.reg1 = cond;
  243. instru.reg2 = dest;
  244. printf("%d\t JMPF %d %d\n", array->index, instru.reg1 , instru.reg2);
  245. if (array->index < INSTRUCTION_TABLE_SIZE){
  246. array->array[array->index] = instru;
  247. array->index++;
  248. }
  249. //free_temp(table);
  250. return cond;
  251. }
  252. */