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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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
  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
  42. //%type<nombre> E
  43. /******************************************** FAIRE LA GENERATION DU CODE ASSEMBLEUR DANS UN TABLEAU AVEC UN FPRINTF *******************/
  44. %%
  45. C : Fonction Fonctions;
  46. Fonctions : Fonction Fonctions;
  47. Fonctions : ;
  48. Main : tINT {printf("Déclaration du main\n");} tMAIN tOBRACE Args tCBRACE Body { print(); create_asm();} ;
  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. Return : tRETURN E tPV {if (return_type_fonc.pointeur_level > 0 || return_type_fonc.isTab){add_operation(COPR,$2, taille_types[INT],0);} else {add_operation(COPR,$2,taille_types[return_type_fonc.base],0);} pop(); };
  52. Args : Arg ArgSuite {$$ = $1 + $2; printf("Les arguments de la fonctions vont avoir une taille dans la pile de : %d\n",$$);};
  53. Args : {$$ = 0;};
  54. 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];}};
  55. Arg : Type tID tOCROCH tCCROCH {int addr = push($2,1, type_courant); $$ = taille_types[INT];};
  56. ArgSuite : tCOMA Arg ArgSuite {$$ = $2 + $3;} ;
  57. ArgSuite : {$$ = 0;};
  58. Body : tOBRACKET {inc_prof();} Instructions tCBRACKET {print(); reset_prof();} ;
  59. Instructions : Instruction Instructions ;
  60. Instructions : ;
  61. Instruction : Aff {};
  62. Instruction : Decl {};
  63. Instruction : Invocation tPV{};
  64. Instruction : If {};
  65. Instruction : While {};
  66. Instruction : Return {};
  67. Invocation : tID tOBRACE {struct fonction_t fonc = get_fonction($1); if (fonc.return_type.pointeur_level > 0 || fonc.return_type.isTab){
  68. $2 = push("0_TEMPORARY_RETURN", 0, integer);
  69. }
  70. else{
  71. $2 = push("0_TEMPORARY_RETURN", 0, fonc.return_type);
  72. }}
  73. Params tCBRACE {struct fonction_t fonc = get_fonction($1); multiple_pop($4);
  74. add_operation(CALL,fonc.first_instruction_line, fonc.taille_args,get_current_index() + 1); $$ = $2;};
  75. //Pour les tableaux quand on les passe en arguments, il faudra faire gaffe à bien les passer comme un pointeur vers un tableau et pas juste un tableau car sinon in ne pourra pas accéder au tableau de base depuis la fonction appellée. Il faut pour cela peut être rajouter un nouveau champ (isArg) dans la table des symboles (ou autre idée?)
  76. Params : {$$ = 0; printf("Sans Params\n"); } ;
  77. Params : Param SuiteParams {$$ = $2 + 1;};
  78. Param : E {printf("Parametre : %d\n", $1);};
  79. SuiteParams : tCOMA Param SuiteParams {$$ = $3 + 1;};
  80. SuiteParams : {$$ = 0;};
  81. //On considère que la première ligne du code en ASM est la ligne 0
  82. If : tIF tOBRACE E tCBRACE {
  83. add_operation(JMF,$3,0,0); $1 = get_current_index() - 1;}
  84. Body {int current = get_current_index();
  85. patch($1,current + 1);
  86. add_operation(JMP,0,0,0);
  87. instructions_ligne_to_patch[get_prof()][nbs_instructions_to_patch[get_prof()]] = current;
  88. nbs_instructions_to_patch[get_prof()]++;
  89. pop();}
  90. Else {printf("If reconnu\n");};
  91. Else : tELSE If { printf("Else if reconnu\n"); };
  92. Else : tELSE Body { printf("Else reconnu\n"); int current = get_current_index();
  93. for (int i = 0; i< nbs_instructions_to_patch[get_prof()]; i++){
  94. patch(instructions_ligne_to_patch[get_prof()][i],current);
  95. }
  96. nbs_instructions_to_patch[get_prof()] = 0;
  97. };
  98. Else : {int current = get_current_index();
  99. for (int i = 0; i< nbs_instructions_to_patch[get_prof()]; i++){
  100. patch(instructions_ligne_to_patch[get_prof()][i],current);
  101. }
  102. nbs_instructions_to_patch[get_prof()] = 0;};
  103. While : tWHILE tOBRACE E tCBRACE {
  104. add_operation(JMF,$3,0,0);
  105. $1 = get_current_index() - 1;
  106. pop();}
  107. Body { printf("While reconnu\n");
  108. int current = get_current_index();
  109. patch($1,current + 1);
  110. add_operation(JMP,$1,0,0);};
  111. Aff : DebutAff tEQ E tPV {add_operation(COP, $1, $3,0); pop();} ;
  112. Aff : DebutAffPointeur tEQ E tPV {add_operation(WR,$1,$3,0); pop(); pop();};
  113. DebutAff : tID {struct symbole_t * symbole = get_variable($1); symbole->initialized = 1; $$=symbole->adresse; printf("%s prend une valeur\n", $1);};
  114. DebutAffPointeur : tMUL SuiteAffPointeur {add_operation(READ, $2, $2, 0); $$=$2;};
  115. DebutAffPointeur : SuiteAffPointeur {$$=$1;};
  116. 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;};
  117. SuiteAffPointeur : tID tOCROCH E tCCROCH {struct symbole_t * symbole = get_variable($1); int addr = push("0_TEMPORARY", 1, symbole->type); if (symbole->type.pointeur_level > 0){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();};
  118. E : tNB { int addr = push("0_TEMPORARY", 1, integer); add_operation(AFC, addr,$1,0); $$ = addr;};
  119. E : tNBEXP { printf("Nombre exp\n"); int addr = push("0_TEMPORARY", 1, integer); add_operation(AFC, addr,$1,0); $$ = addr;};
  120. E : E tMUL E { printf("Mul\n"); add_operation(MUL,$1,$1,$3); $$ = $1; pop();};
  121. E : E tDIV E { printf("Div\n"); add_operation(DIV, $1,$1,$3); $$ = $1; pop();};
  122. E : E tSUB E { printf("Sub\n"); add_operation(SOU,$1,$1,$3); $$ = $1; pop();};
  123. E : E tADD E { printf("Add\n"); add_operation(ADD,$1,$1,$3); $$ = $1; pop();};
  124. E : Invocation {$$ = $1;};
  125. E : tOBRACE E tCBRACE { printf("Parentheses\n"); $$=$2;};
  126. 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();};
  127. E : E tEQCOND E { printf("==\n"); add_operation(EQU,$1,$1,$3); $$ = $1; pop();};
  128. E : E tGT E { printf(">\n"); add_operation(SUP,$1,$1,$3); $$ = $1; pop();};
  129. E : E tLT E { printf("<\n"); add_operation(INF,$1,$1,$3); $$ = $1; pop();};
  130. E : tNOT E { printf("!\n"); };
  131. E : E tAND E {add_operation(MUL,$1,$1,$3); $$ = $1; pop();};
  132. E : E tOR E {add_operation(ADD,$1,$1,$3); $$ = $1; pop();} ;
  133. E : tMUL E { add_operation(READ, $2, $2, 0); $$=$2;};
  134. 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){add_operation(AFCA, addr,symbole->adresse,0); } else{add_operation(COP, addr,symbole->adresse,0);} $$=addr;};
  135. 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.pointeur_level > 0) {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);
  136. add_operation(ADD,$3,addr,$3); add_operation(READ,$3,$3,0); $$=$3; pop(); pop();};
  137. E : tADDR EBis {$$=$2;};
  138. EBis : tID tOCROCH E tCCROCH {struct symbole_t * symbole = get_variable($1);
  139. struct type_t type = symbole->type; type.nb_blocs = 1;
  140. int addr = push("0_TEMPORARY", 1, type);
  141. if(type.pointeur_level > 0) {
  142. add_operation(COP, addr,symbole->adresse,0);
  143. }
  144. else{
  145. add_operation(AFCA, addr,symbole->adresse,0);
  146. }
  147. int addr2 = push("0_TEMPORARY", 1, integer);
  148. add_operation(AFC, addr2, taille_types[symbole->type.base],0);
  149. add_operation(MUL,$3,addr2,$3);
  150. add_operation(ADD,$3,addr,$3); $$=$3;
  151. pop(); pop();};
  152. 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;};
  153. //Créer un champ isConst dans la table des symboles
  154. Type : tINT {type_courant.base = INT; type_courant.pointeur_level = 0; type_courant.isTab = 0; type_courant.nb_blocs = 1; printf("Type int\n");} ;
  155. Type : Type tMUL {type_courant.pointeur_level++; printf("Type int *\n");};
  156. Decl : Type SuiteDecl FinDecl ;
  157. Decl : tCONST Type SuiteDeclConst FinDeclConst;
  158. SuiteDecl : tID {push($1, 0, type_courant); printf("Suite Decl\n");};
  159. SuiteDecl : tID tEQ E {pop(); int addr = push($1,1, type_courant);};
  160. SuiteDecl : tID tOCROCH tNB tCCROCH {type_courant.isTab = 1; type_courant.nb_blocs = $3; push($1, 0, type_courant);} ;
  161. FinDecl : tPV { printf("Fin Decl\n");};
  162. FinDecl : tCOMA SuiteDecl FinDecl ;
  163. SuiteDeclConst : tID tEQ E {pop(); int addr = push($1,1, type_courant);};
  164. FinDeclConst : tPV;
  165. FinDeclConst : tCOMA SuiteDeclConst FinDeclConst;
  166. %%
  167. void main(void) {
  168. init();
  169. yyparse();
  170. }