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.

tab_instruc.c 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include "tab_instruc.h"
  2. #define MAXTAILLE 1024
  3. // Structure représentant une opération
  4. struct operation_t {
  5. enum opcode_t opcode;
  6. int arg1;
  7. int arg2;
  8. int arg3;
  9. };
  10. // Index dans le tableau des instruction
  11. int current_index = 1;
  12. // Tableau des instructions (programme en assembleur)
  13. struct operation_t tab_op[MAXTAILLE];
  14. // Insertion d'une opération dans le tableau
  15. void add_operation(enum opcode_t opcode, int arg1, int arg2, int arg3){
  16. if (current_index == MAXTAILLE){
  17. printf("Taillemax tableau operations atteinte\n");
  18. }
  19. else{
  20. struct operation_t new_op = {opcode, arg1, arg2, arg3};
  21. tab_op[current_index] = new_op;
  22. current_index++;
  23. }
  24. }
  25. // Les fonctions étant compilées en premiers, la première instruction du programme doit être un JMP vers le main
  26. void create_jump_to_main(int line){
  27. struct operation_t new_op = {JMP,line, 0, 0};
  28. tab_op[0] = new_op;
  29. }
  30. // Fonction traduisant une opération en assembleur
  31. char * get_asm_line_from_op(struct operation_t op){
  32. char * buffer = malloc(sizeof(char)*200);
  33. switch (op.opcode){
  34. case (ADD):
  35. sprintf(buffer,"ADD %d %d %d\n",op.arg1, op.arg2, op.arg3);
  36. break;
  37. case (MUL):
  38. sprintf(buffer,"MUL %d %d %d\n",op.arg1, op.arg2, op.arg3);
  39. break;
  40. case (SOU):
  41. sprintf(buffer,"SOU %d %d %d\n",op.arg1, op.arg2, op.arg3);
  42. break;
  43. case (DIV):
  44. sprintf(buffer,"DIV %d %d %d\n",op.arg1, op.arg2, op.arg3);
  45. break;
  46. case (COP):
  47. sprintf(buffer,"COP %d %d\n",op.arg1, op.arg2);
  48. break;
  49. case (AFC):
  50. sprintf(buffer,"AFC %d %d\n",op.arg1, op.arg2);
  51. break;
  52. case (AFCA):
  53. sprintf(buffer,"AFCA %d %d\n",op.arg1, op.arg2);
  54. break;
  55. case (JMP):
  56. sprintf(buffer,"JMP %d\n",op.arg1);
  57. break;
  58. case (JMF):
  59. sprintf(buffer,"JMF %d %d\n",op.arg1, op.arg2);
  60. break;
  61. case (INF):
  62. sprintf(buffer,"INF %d %d %d\n",op.arg1, op.arg2, op.arg3);
  63. break;
  64. case (SUP):
  65. sprintf(buffer,"SUP %d %d %d\n",op.arg1, op.arg2, op.arg3);
  66. break;
  67. case (EQU):
  68. sprintf(buffer,"DIV %d %d %d\n",op.arg1, op.arg2, op.arg3);
  69. break;
  70. case (PRI):
  71. sprintf(buffer,"PRI %d\n",op.arg1);
  72. break;
  73. case (READ):
  74. sprintf(buffer,"READ %d %d\n",op.arg1, op.arg2);
  75. break;
  76. case (WR):
  77. sprintf(buffer,"WR %d %d\n",op.arg1, op.arg2);
  78. break;
  79. case (CALL):
  80. sprintf(buffer,"CALL %d %d\n",op.arg1, op.arg2);
  81. break;
  82. case (RET):
  83. sprintf(buffer,"RET\n");
  84. break;
  85. case (GET):
  86. sprintf(buffer,"GET %d\n",op.arg1);
  87. break;
  88. case (STOP):
  89. sprintf(buffer,"STOP %d\n", op.arg1);
  90. break;
  91. }
  92. return buffer;
  93. }
  94. // Genere le code asm dans un fichier
  95. void create_asm(){
  96. FILE * output = fopen("output.txt","w");
  97. for (int i = 0; i < current_index; i++){
  98. char * line = get_asm_line_from_op(tab_op[i]);
  99. fputs(line, output);
  100. free(line);
  101. }
  102. }
  103. // Renvoi l'index courrant dans le tableau
  104. int get_current_index(){return current_index;}
  105. // Permet de modifier un instruction à postériorie (pour les JMP)
  106. void patch(int index, int arg){
  107. if (tab_op[index].opcode == JMP){
  108. tab_op[index].arg1 = arg;
  109. }
  110. else if (tab_op[index].opcode == JMF){
  111. tab_op[index].arg2 = arg;
  112. }
  113. }