le compilateur super performant
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.

as.y 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. %{
  2. #include "stdio.h"
  3. #include "stdlib.h"
  4. #include "symbol_table.h"
  5. #include "asm_instructions.h"
  6. #include "yacc_util.h"
  7. enum Type current_type = TYPE_INT;
  8. SymbolTable symbol_table;
  9. InstructionTable instruction_table;
  10. %}
  11. %union {
  12. char* symbol_name;
  13. SymbolItem* symbol;
  14. int nombre;
  15. int address;
  16. Instruction instruction;
  17. struct Condition* condition;
  18. }
  19. %token<nombre> tNB
  20. %token<instruction> tADD
  21. %token<instruction> tSUB
  22. %token<instruction> tMUL
  23. %token<instruction> tDIV
  24. %token tPO
  25. %token tPF
  26. %token tPV
  27. %token tVIRG
  28. %token tAO
  29. %token tAF
  30. %token tEQ
  31. %token<condition> tEQ2
  32. %token<condition> tNOTEQ
  33. %token<condition> tINF
  34. %token<condition> tINFEQ
  35. %token<condition> tSUP
  36. %token<condition> tSUPEQ
  37. %token tNOT
  38. %token tAND
  39. %token tOR
  40. %token tINT
  41. %token tCONST
  42. %token tMAIN
  43. %token tIF
  44. %token tELSE
  45. %token tELSIF
  46. %token tWHILE
  47. %token tPRINTF
  48. %token<symbol_name> tID
  49. %left tADD
  50. %left tSUB
  51. %left tMUL
  52. %left tDIV
  53. %type<symbol> E
  54. %type<symbol> Cond
  55. %type<address> IfSimple
  56. %%
  57. S : Main { printf("S\n"); } ;
  58. Main : tINT tMAIN tPO Params tPF MainBody { printf("Main\n"); } ;
  59. Params : | Param SuiteParams { printf("Params\n"); } ;
  60. Param : tINT tID { printf("Param\n"); } ;
  61. SuiteParams : tVIRG Param SuiteParams { printf("SuiteParam\n"); } ;
  62. MainBody : tAO Declarations Instructions tAF { printf("MainBody\n"); } ;
  63. Body : tAO Instructions tAF { printf("Body\n"); } ;
  64. Instructions : Instruction Instructions | { printf("Instructions\n"); } ;
  65. Instruction : Aff | Printf | If { printf("Instruction\n"); } ;
  66. Aff : tID tEQ E tPV { write_affectation(&symbol_table, &instruction_table, $1, $3); } ;
  67. E : tNB { $$ = init_temp_symbol(&symbol_table, &instruction_table, $1); }
  68. | tID { $$ = get_symbol_item(&symbol_table, $1); }
  69. | E tADD E {$$ = write_op(&symbol_table, &instruction_table, $2, $1, $3); }
  70. | E tMUL E { $$ = write_op(&symbol_table, &instruction_table, $2, $1, $3); }
  71. | E tSUB E { $$ = write_op(&symbol_table, &instruction_table, $2, $1, $3); }
  72. | E tDIV E { $$ = write_op(&symbol_table, &instruction_table, $2, $1, $3); }
  73. | tPO E tPF { $$ = $2; }
  74. | tSUB E { $$ = write_negation(&symbol_table, &instruction_table, $2); } ;
  75. Declarations : | Declaration Declarations ;
  76. Declaration : DeclarationInt | DeclarationConst ;
  77. DeclarationInt : tINT { current_type = TYPE_INT; } DeclarationBody tPV ;
  78. DeclarationConst : tCONST { current_type = TYPE_CONST; } DeclarationBody tPV ;
  79. DeclarationBody : VariableIdentifier tVIRG DeclarationBody | VariableIdentifier ;
  80. VariableIdentifier: tID { add_symbol(&symbol_table, current_type, $1); } ;
  81. // While : tWHILE tPO Cond tPF Body { printf("While\n"); } ;
  82. If : IfSimple { update_jmf(&instruction_table, $1); } ;
  83. IfSimple : tIF tPO Cond tPF { $<address>$ = write_jmf(&instruction_table, $3); } Body { $$ = $<address>5; };
  84. // IfElse : IfSimple tELSE Body { printf("IfElse\n"); } ;
  85. Cond : tNOT Cond { $$ = write_not_condition(&symbol_table, &instruction_table, $2); }
  86. | Cond tAND Cond { $$ = write_op(&symbol_table, &instruction_table, MUL, $1, $3); }
  87. | Cond tOR Cond { $$ = write_op(&symbol_table, &instruction_table, ADD, $1, $3); }
  88. | E tEQ2 E { $$ = write_condition(&symbol_table, &instruction_table, $2, $1, $3); }
  89. | E tINF E { $$ = write_condition(&symbol_table, &instruction_table, $2, $1, $3); }
  90. | E tINFEQ E { $$ = write_condition(&symbol_table, &instruction_table, $2, $1, $3); }
  91. | E tSUP E { $$ = write_condition(&symbol_table, &instruction_table, $2, $1, $3); }
  92. | E tSUPEQ E { $$ = write_condition(&symbol_table, &instruction_table, $2, $1, $3); }
  93. | E tNOTEQ E { $$ = write_condition(&symbol_table, &instruction_table, $2, $1, $3); }
  94. | tNOT tPO Cond tPF { $$ = write_not_condition(&symbol_table, &instruction_table, $3); } ;
  95. Printf : tPRINTF tPO E tPF tPV { write_print(&instruction_table, $3); } ;
  96. %%
  97. #include <string.h>
  98. #define LABEL_SIZE 10
  99. // ADD @ + @ -> @
  100. // SUB @ - @ -> @
  101. // AFC nb -> @
  102. // CPY @ -> @
  103. // Exemple
  104. // y = 1 + 2;
  105. // AFC 1 32
  106. // AFC 2 33
  107. // ADD 32 33 32
  108. // CPY 32 @y
  109. /*
  110. table des symboles (tS) :
  111. sous forme de pile
  112. -> où sera la variable lors de l'exec
  113. |
  114. | nom | type | @ | init |
  115. |-------|--------|-----|--------|
  116. | a | | 0 | |
  117. | b | | 1 | |
  118. | c | | 2 | |
  119. | | | | |
  120. | . | | . | | -> entiers sur 1 octet
  121. | . | | . | |
  122. | . | | . | |
  123. | | | | |
  124. | y | | 26 | |
  125. Donc on remplace @y par 26 dans CPY 32 @y
  126. /!\
  127. E + E + E
  128. | | |
  129. -> -> -> vt variable temporaire (autre expression)
  130. -> -> -> variable -> ADD @E1 @E2 vt
  131. -> -> -> nombre
  132. Donc 9 possibilités (3 par E)
  133. On suppose que E est tout le temps une vt, donc on génère tout le temps le même code
  134. */
  135. void init() {
  136. init_table(&symbol_table);
  137. init_instruction_table(&instruction_table);
  138. }
  139. void yyerror(char const* err) {
  140. printf("%s\n", err);
  141. exit(1);
  142. }
  143. void main(int argc, char *argv[]) {
  144. char *output_filename = "output.asm";
  145. if (argc >= 2) {
  146. output_filename = argv[1];
  147. }
  148. init();
  149. yyparse();
  150. print_table(&symbol_table);
  151. FILE *f = fopen(output_filename, "w+");
  152. write_instruction_table(&instruction_table, f);
  153. fclose(f);
  154. }