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 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 "JMP";
  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. case LEA:
  45. return "LEA";
  46. case COP_LD:
  47. return "COP_LD";
  48. case COP_STR:
  49. return "COP_STR";
  50. case RET_FUN:
  51. return "RET_FUN";
  52. case CALL:
  53. return "CALL";
  54. default:
  55. break;
  56. }
  57. return "";
  58. }
  59. void initialise_asm(instructions_array * array){
  60. array->index = 0;
  61. }
  62. int add_instruction(instructions_array * array, instruction * instru){
  63. if (array->index >= INSTRUCTION_TABLE_SIZE){
  64. return 1;
  65. }
  66. array->array[array->index] = *instru;
  67. array->index++;
  68. return 0;
  69. }
  70. int new_temp(Table_Symboles * table){
  71. int ret_addr ;
  72. if(add_symbole_bottom(table) == -1) {
  73. return -1;
  74. }
  75. ret_addr = table->indexAvailableBottom + 1;
  76. return ret_addr;
  77. }
  78. int generate_instruction_0(instructions_array * array, enum operation op){
  79. instruction instru;
  80. char * opName = operationName(op);
  81. instru.operation = op;
  82. printf("%d\t %s\n", array->index, opName);
  83. if (add_instruction(array, &instru) != 0){
  84. //TODO: Error handling
  85. exit(1);
  86. }
  87. return 0;
  88. }
  89. int generate_instruction_1(instructions_array * array, enum operation op, int arg1){
  90. instruction instru;
  91. char * opName = operationName(op);
  92. instru.operation = op;
  93. instru.reg1 = arg1;
  94. printf("%d\t %s %d\n", array->index, opName, instru.reg1);
  95. if (add_instruction(array, &instru) != 0){
  96. //TODO: Error handling
  97. exit(1);
  98. }
  99. return 0;
  100. }
  101. int generate_instruction_2(instructions_array * array, enum operation op, int arg1, int arg2){
  102. instruction instru;
  103. char * opName = operationName(op);
  104. instru.operation = op;
  105. instru.reg1 = arg1;
  106. instru.reg2 = arg2;
  107. printf("%d\t %s %d %d\n", array->index, opName, instru.reg1, instru.reg2);
  108. if (add_instruction(array, &instru) != 0){
  109. //TODO: Error handling
  110. exit(1);
  111. }
  112. return 0;
  113. }
  114. int generate_instruction_3(instructions_array * array, enum operation op, int arg1, int arg2, int arg3){
  115. instruction instru;
  116. char * opName = operationName(op);
  117. instru.operation = op;
  118. instru.reg1 = arg1;
  119. instru.reg2 = arg2;
  120. instru.reg3 = arg3;
  121. printf("%d\t %s %d %d %d\n", array->index, opName, instru.reg1, instru.reg2, instru.reg3);
  122. if (add_instruction(array, &instru) != 0){
  123. //TODO: Error handling
  124. exit(1);
  125. }
  126. return 0;
  127. }
  128. void update_jmf(instructions_array * array, int instru_index, int adr_jmp){
  129. array->array[instru_index - 1].reg2 = adr_jmp;
  130. printf("%d\t JMP %d %d\n", (instru_index - 1), array->array[instru_index].reg1, array->array[instru_index].reg2);
  131. }
  132. void update_jmp(instructions_array * array, int instru_index, int adr_jmp){
  133. array->array[instru_index].reg1 = adr_jmp;
  134. printf("%d\t JMP %d\n", (instru_index - 1), array->array[instru_index].reg1);
  135. }
  136. void exportInstructions(instructions_array * array){
  137. FILE *file;
  138. file = fopen("instructions.txt", "w");
  139. instruction instru;
  140. enum operation op;
  141. for (int i = 0; i < array->index; i++){
  142. instru = array->array[i];
  143. op = instru.operation;
  144. switch (op) {
  145. //0 parameters
  146. case RET_FUN:
  147. fprintf(file, "%s\n", operationName(op));
  148. break;
  149. //1 parameter
  150. case JMP:
  151. case PRI:
  152. case RET:
  153. fprintf(file, "%s %d\n", operationName(op), instru.reg1);
  154. break;
  155. //2 parameters
  156. case JMF:
  157. case NOT:
  158. case AFC:
  159. case COP:
  160. case LEA:
  161. case CALL:
  162. fprintf(file, "%s %d %d\n", operationName(op), instru.reg1, instru.reg2);
  163. break;
  164. case COP_LD:
  165. fprintf(file, "%s %d [%d]\n", operationName(op), instru.reg1, instru.reg2);
  166. break;
  167. case COP_STR:
  168. fprintf(file, "%s [%d] %d\n", operationName(op), instru.reg1, instru.reg2);
  169. break;
  170. //3 parameters
  171. case ADD:
  172. case SOU:
  173. case DIV:
  174. case MUL:
  175. case AND:
  176. case OR:
  177. case EQ:
  178. case NEQ:
  179. case LT:
  180. case LTE:
  181. case GT:
  182. case GTE:
  183. fprintf(file, "%s %d %d %d\n", operationName(op), instru.reg1, instru.reg2, instru.reg3);
  184. break;
  185. default:
  186. break;
  187. }
  188. }
  189. fclose(file);
  190. }
  191. /*int gen_print(Table_Symboles * table, instructions_array * array, int arg1){
  192. instruction instru;
  193. instru.operation = PRI;
  194. instru.reg1 = arg1;
  195. printf("%d\t PRI %d\n", array->index, instru.reg1);
  196. if (array->index < INSTRUCTION_TABLE_SIZE){
  197. array->array[array->index] = instru;
  198. array->index++;
  199. }
  200. free_temp(table);
  201. }*/
  202. /*void gen_arithmetique(instructions_array * array, enum operation op, int arg1, int arg2){
  203. instruction instru;
  204. instru.reg1 = arg1;
  205. instru.reg2 = arg1;
  206. instru.reg3 = arg2;
  207. char * opName = operationName(op);
  208. printf("%d\t %s %d %d %d\n", array->index, opName, arg1, arg1, arg2);
  209. if (array->index < INSTRUCTION_TABLE_SIZE){
  210. array->array[array->index] = instru;
  211. array->index++;
  212. }
  213. }
  214. int gen_var(Table_Symboles * table, instructions_array * array, char * varName){
  215. int vt = new_temp(table);
  216. int varAddr = variable_exists(table, varName);
  217. //vérifier que non null
  218. instruction instru;
  219. instru.operation = COP;
  220. instru.reg1 = vt;
  221. instru.reg2 = varAddr;
  222. printf("%d\t COP %d %d\n", array->index, vt, varAddr);
  223. if (array->index < INSTRUCTION_TABLE_SIZE){
  224. array->array[array->index] = instru;
  225. array->index++;
  226. }
  227. return vt;
  228. }
  229. int gen_entier(Table_Symboles * table, instructions_array * array, int entier){
  230. int vt = new_temp(table);
  231. //vérifier que non null
  232. instruction instru;
  233. instru.operation = AFC;
  234. instru.reg1 = vt;
  235. instru.reg2 = entier;
  236. printf("%d\t AFC %d %d\n", array->index, vt, entier);
  237. if (array->index < INSTRUCTION_TABLE_SIZE){
  238. array->array[array->index] = instru;
  239. array->index++;
  240. }
  241. return vt;
  242. }
  243. int gen_condition(Table_Symboles * table, instructions_array * array, enum operation op, int arg1, int arg2){
  244. char * opName = operationName(op);
  245. instruction instru;
  246. instru.operation = op;
  247. instru.reg1 = arg1;
  248. instru.reg2 = arg1;
  249. if (op != NOT){
  250. instru.reg3 = arg2;
  251. printf("%d\t %s %d %d %d\n", array->index, opName, instru.reg1, instru.reg2, instru.reg3);
  252. free_temp(table);
  253. } else {
  254. printf("%d\t %s %d %d \n", array->index, opName, instru.reg1, instru.reg2);
  255. }
  256. if (array->index < INSTRUCTION_TABLE_SIZE){
  257. array->array[array->index] = instru;
  258. array->index++;
  259. }
  260. return instru.reg1;
  261. }
  262. int gen_return(Table_Symboles * table, instructions_array * array, int adr){
  263. //vérifier que non null
  264. instruction instru;
  265. instru.operation = RET;
  266. instru.reg1 = adr;
  267. printf("%d\t RET %d\n", array->index, adr);
  268. if (array->index < INSTRUCTION_TABLE_SIZE){
  269. array->array[array->index] = instru;
  270. array->index++;
  271. }
  272. //free_temp(table);
  273. return adr;
  274. }
  275. int gen_jmpf(Table_Symboles * table, instructions_array * array, int cond, int dest){
  276. //vérifier que non null
  277. instruction instru;
  278. instru.operation = JMF;
  279. instru.reg1 = cond;
  280. instru.reg2 = dest;
  281. printf("%d\t JMPF %d %d\n", array->index, instru.reg1 , instru.reg2);
  282. if (array->index < INSTRUCTION_TABLE_SIZE){
  283. array->array[array->index] = instru;
  284. array->index++;
  285. }
  286. //free_temp(table);
  287. return cond;
  288. }
  289. */