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.

as.y 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. %union {
  2. int nombre;
  3. char id[30];
  4. }
  5. %{
  6. #include "../Tables/Fonctions/tab_fonctions.h"
  7. #include "../Tables/Symboles/table_symboles.h"
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <stdlib.h>
  11. #include "../Tables/Instructions/tab_instruc.h"
  12. #define TAILLE 1024
  13. struct type_t type_courant;
  14. struct type_t return_type_fonc;
  15. int instructions_ligne_to_patch[10][20];
  16. int nbs_instructions_to_patch[10];
  17. %}
  18. %token tMAIN
  19. %token tOBRACKET tCBRACKET
  20. %token<nombre> tOBRACE tCBRACE
  21. %token tOCROCH tCCROCH
  22. %token tINT
  23. %token tCONST
  24. %token tPV tCOMA
  25. %token tMUL tDIV tADD tSUB tEQ
  26. %token<nombre> tNB tNBEXP
  27. %token<id> tID
  28. %token tPRINTF tGET tSTOP
  29. %token tERROR
  30. %token<nombre> tIF tWHILE tELSE
  31. %token tRETURN
  32. %token tLT tGT tEQCOND
  33. %token tAND tOR
  34. %token tADDR
  35. %left tLT tGT
  36. %left tEQCOND
  37. %left tAND tOR
  38. %left tNOT
  39. %left tADD tSUB
  40. %left tMUL tDIV
  41. %type<nombre> E DebutAff SuiteAffPointeur DebutAffPointeur EBis Invocation Args ArgSuite Arg SuiteParams Params Get
  42. //%type<nombre> E
  43. /******************************************** FAIRE LA GENERATION DU CODE ASSEMBLEUR DANS UN TABLEAU AVEC UN FPRINTF *******************/
  44. %%
  45. C : Fonction Fonctions {add_operation(STOP,0,0,0); create_asm();};
  46. Fonctions : Fonction Fonctions;
  47. Fonctions : ;
  48. Main : tINT {printf("Déclaration du main\n"); create_jump_to_main(get_current_index());} tMAIN tOBRACE Args tCBRACE Body { print(); } ;
  49. Fonction : Type tID {return_type_fonc = type_courant; printf("Déclaration de la fonction %s\n", $2);} tOBRACE {inc_prof();} Args {decrement_prof(); push_fonction($2,return_type_fonc,get_current_index(), $6);} tCBRACE Body { print_fonctions(); add_operation(RET,0,0,0);} ;
  50. Fonction : Main {print_fonctions();};
  51. Get : tGET tOBRACE tCBRACE {int addr = push("0_TEMPORARY", 0, integer); add_operation(GET,addr,0,0); $$ = addr;};
  52. Stop : tSTOP tOBRACE tNB tCBRACE {add_operation(STOP,$3,0,0);};
  53. Return : tRETURN E tPV {add_operation(COP,0,$2,0); pop(); };
  54. Args : Arg ArgSuite {$$ = $1 + $2; printf("Les arguments de la fonctions vont avoir une taille dans la pile de : %d\n",$$);};
  55. Args : {$$ = 0;};
  56. Arg : Type tID {int addr = push($2,1, type_courant); if (type_courant.pointeur_level > 0){$$ = taille_types[INT];} else{$$ = taille_types[type_courant.base];}};
  57. Arg : Type tID tOCROCH tCCROCH {type_courant.isTab = 2; int addr = push($2,1, type_courant); $$ = taille_types[INT];};
  58. ArgSuite : tCOMA Arg ArgSuite {$$ = $2 + $3;} ;
  59. ArgSuite : {$$ = 0;};
  60. Body : tOBRACKET {inc_prof();} Instructions tCBRACKET {print(); reset_prof();};
  61. Instructions : Instruction Instructions ;
  62. Instructions : ;
  63. Instruction : Aff {};
  64. Instruction : Decl {};
  65. Instruction : Invocation tPV{pop();};
  66. Instruction : If {};
  67. Instruction : While {};
  68. Instruction : Return {};
  69. Instruction : Stop tPV {};
  70. Invocation : tID tOBRACE {struct fonction_t fonc = get_fonction($1);}
  71. Params tCBRACE {struct fonction_t fonc = get_fonction($1); multiple_pop($4);
  72. add_operation(CALL,fonc.first_instruction_line, get_last_addr(),0); if (fonc.return_type.pointeur_level > 0 || fonc.return_type.isTab){
  73. $$ = push("0_TEMPORARY_RETURN", 0, integer);
  74. }
  75. else{
  76. $$ = push("0_TEMPORARY_RETURN", 0, fonc.return_type);
  77. }};
  78. Invocation : tPRINTF tOBRACE E tCBRACE{add_operation(PRI,$3,0,0);};
  79. Params : {$$ = 0; printf("Sans Params\n"); } ;
  80. Params : Param SuiteParams {$$ = $2 + 1;};
  81. Param : E {printf("Parametre : %d\n", $1);};
  82. SuiteParams : tCOMA Param SuiteParams {$$ = $3 + 1;};
  83. SuiteParams : {$$ = 0;};
  84. //On considère que la première ligne du code en ASM est la ligne 0
  85. If : tIF tOBRACE E tCBRACE {
  86. add_operation(JMF,$3,0,0); $1 = get_current_index() - 1;}
  87. Body {int current = get_current_index();
  88. patch($1,current + 1);
  89. add_operation(JMP,0,0,0);
  90. instructions_ligne_to_patch[get_prof()][nbs_instructions_to_patch[get_prof()]] = current;
  91. nbs_instructions_to_patch[get_prof()]++;
  92. pop();}
  93. Else {printf("If reconnu\n");};
  94. Else : tELSE If { printf("Else if reconnu\n"); };
  95. Else : tELSE Body { printf("Else reconnu\n"); int current = get_current_index();
  96. for (int i = 0; i< nbs_instructions_to_patch[get_prof()]; i++){
  97. patch(instructions_ligne_to_patch[get_prof()][i],current);
  98. }
  99. nbs_instructions_to_patch[get_prof()] = 0;
  100. };
  101. Else : {int current = get_current_index();
  102. for (int i = 0; i< nbs_instructions_to_patch[get_prof()]; i++){
  103. patch(instructions_ligne_to_patch[get_prof()][i],current);
  104. }
  105. nbs_instructions_to_patch[get_prof()] = 0;};
  106. While : tWHILE tOBRACE E tCBRACE {
  107. add_operation(JMF,$3,0,0);
  108. $1 = get_current_index() - 1;
  109. pop();}
  110. Body { printf("While reconnu\n");
  111. int current = get_current_index();
  112. patch($1,current + 1);
  113. add_operation(JMP,$1,0,0);};
  114. Aff : DebutAff tEQ E tPV {add_operation(COP, $1, $3,0); pop();} ;
  115. Aff : DebutAffPointeur tEQ E tPV {add_operation(WR,$1,$3,0); pop(); pop();};
  116. DebutAff : tID {struct symbole_t * symbole = get_variable($1); symbole->initialized = 1; $$=symbole->adresse; printf("%s prend une valeur\n", $1);};
  117. DebutAffPointeur : tMUL SuiteAffPointeur {add_operation(READ, $2, $2, 0); $$=$2;};
  118. DebutAffPointeur : SuiteAffPointeur {$$=$1;};
  119. SuiteAffPointeur : tMUL tID {struct symbole_t * symbole = get_variable($2); int addr = push("0_TEMPORARY", 1, symbole->type); add_operation(COP, addr,symbole->adresse,0); $$=addr;};
  120. SuiteAffPointeur : tID tOCROCH E tCCROCH {struct symbole_t * symbole = get_variable($1); int addr = push("0_TEMPORARY", 1, symbole->type); if (symbole->type.isTab == 2){add_operation(COP, addr,symbole->adresse,0);} else{add_operation(AFCA, addr,symbole->adresse,0);} int addr2 = push("0_TEMPORARY", 1, integer); add_operation(AFC, addr2, taille_types[symbole->type.base],0); add_operation(MUL,$3,addr2,$3); add_operation(ADD,$3,addr,$3); $$=$3; pop(); pop();};
  121. E : tNB { int addr = push("0_TEMPORARY", 1, integer); add_operation(AFC, addr,$1,0); $$ = addr;};
  122. E : tNBEXP { printf("Nombre exp\n"); int addr = push("0_TEMPORARY", 1, integer); add_operation(AFC, addr,$1,0); $$ = addr;};
  123. E : E tMUL E { printf("Mul\n"); add_operation(MUL,$1,$1,$3); $$ = $1; pop();};
  124. E : E tDIV E { printf("Div\n"); add_operation(DIV, $1,$1,$3); $$ = $1; pop();};
  125. E : E tSUB E { printf("Sub\n"); add_operation(SOU,$1,$1,$3); $$ = $1; pop();};
  126. E : E tADD E { printf("Add\n"); add_operation(ADD,$1,$1,$3); $$ = $1; pop();};
  127. E : Invocation {$$ = $1;};
  128. E : tOBRACE E tCBRACE { printf("Parentheses\n"); $$=$2;};
  129. E : tSUB E { printf("Moins\n"); int addr = push("0_TEMPORARY", 1, integer); add_operation(AFC, addr,0,0); add_operation(SOU, $2,$2,addr); $$ = $2; pop();};
  130. E : E tEQCOND E { printf("==\n"); add_operation(EQU,$1,$1,$3); $$ = $1; pop();};
  131. E : E tGT E { printf(">\n"); add_operation(SUP,$1,$1,$3); $$ = $1; pop();};
  132. E : E tLT E { printf("<\n"); add_operation(INF,$1,$1,$3); $$ = $1; pop();};
  133. E : tNOT E { printf("!\n"); };
  134. E : E tAND E {add_operation(MUL,$1,$1,$3); $$ = $1; pop();};
  135. E : E tOR E {add_operation(ADD,$1,$1,$3); $$ = $1; pop();} ;
  136. E : tMUL E { add_operation(READ, $2, $2, 0); $$=$2;};
  137. E : tID { printf("Id\n"); struct symbole_t * symbole = get_variable($1); struct type_t type = symbole->type; type.nb_blocs = 1; int addr = push("0_TEMPORARY", 1, type); if (symbole->type.isTab == 1){add_operation(AFCA, addr,symbole->adresse,0);} else{add_operation(COP, addr,symbole->adresse,0);} $$=addr;};
  138. E : tID tOCROCH E tCCROCH {struct symbole_t * symbole = get_variable($1); struct type_t type = symbole->type; type.nb_blocs = 1; int addr = push("0_TEMPORARY", 1, type); if(type.isTab == 2) {add_operation(COP, addr,symbole->adresse,0);} else{add_operation(AFCA, addr,symbole->adresse,0);} int addr2 = push("0_TEMPORARY", 1, integer); add_operation(AFC, addr2, taille_types[symbole->type.base],0); add_operation(MUL,$3,addr2,$3);
  139. add_operation(ADD,$3,addr,$3); add_operation(READ,$3,$3,0); $$=$3; pop(); pop();};
  140. E : tADDR EBis {$$=$2;};
  141. E : Get {$$ = $1;};
  142. EBis : tID tOCROCH E tCCROCH {struct symbole_t * symbole = get_variable($1);
  143. struct type_t type = symbole->type; type.nb_blocs = 1;
  144. int addr = push("0_TEMPORARY", 1, type);
  145. if(type.isTab == 2) {
  146. add_operation(COP, addr,symbole->adresse,0);
  147. }
  148. else{
  149. add_operation(AFCA, addr,symbole->adresse,0);
  150. }
  151. int addr2 = push("0_TEMPORARY", 1, integer);
  152. add_operation(AFC, addr2, taille_types[symbole->type.base],0);
  153. add_operation(MUL,$3,addr2,$3);
  154. add_operation(ADD,$3,addr,$3); $$=$3;
  155. pop(); pop();};
  156. EBis : tID { printf("Id\n"); struct symbole_t * symbole = get_variable($1); struct type_t type = symbole->type; type.nb_blocs = 1; int addr = push("0_TEMPORARY", 1, type); add_operation(AFCA, addr,symbole->adresse,0); $$=addr;};
  157. //Créer un champ isConst dans la table des symboles
  158. Type : tINT {type_courant.base = INT; type_courant.pointeur_level = 0; type_courant.isTab = 0; type_courant.nb_blocs = 1; printf("Type int\n");} ;
  159. Type : Type tMUL {type_courant.pointeur_level++; printf("Type int *\n");};
  160. Decl : Type SuiteDecl FinDecl ;
  161. Decl : tCONST Type SuiteDeclConst FinDeclConst;
  162. SuiteDecl : tID {push($1, 0, type_courant); printf("Suite Decl\n");};
  163. SuiteDecl : tID tEQ E {pop(); int addr = push($1,1, type_courant);};
  164. SuiteDecl : tID tOCROCH tNB tCCROCH {type_courant.isTab = 1; type_courant.nb_blocs = $3; push($1, 0, type_courant);} ;
  165. FinDecl : tPV { printf("Fin Decl\n");};
  166. FinDecl : tCOMA SuiteDecl FinDecl ;
  167. SuiteDeclConst : tID tEQ E {pop(); int addr = push($1,1, type_courant);};
  168. FinDeclConst : tPV;
  169. FinDeclConst : tCOMA SuiteDeclConst FinDeclConst;
  170. %%
  171. void main(void) {
  172. init();
  173. yyparse();
  174. }