Advance TP
This commit is contained in:
@@ -5,6 +5,10 @@
|
|||||||
#include "dico.h"
|
#include "dico.h"
|
||||||
|
|
||||||
void printMips( const char * msg );
|
void printMips( const char * msg );
|
||||||
|
void debutFunc();
|
||||||
|
void finFunc();
|
||||||
|
void empile(char *reg);
|
||||||
|
void depile(char *reg);
|
||||||
|
|
||||||
void parcours_table(n_prog *n);
|
void parcours_table(n_prog *n);
|
||||||
void parcours_l_instr(n_l_instr *n);
|
void parcours_l_instr(n_l_instr *n);
|
||||||
@@ -17,21 +21,21 @@ void parcours_instr_affect(n_instr *n);
|
|||||||
void parcours_instr_appel(n_instr *n);
|
void parcours_instr_appel(n_instr *n);
|
||||||
void parcours_instr_retour(n_instr *n);
|
void parcours_instr_retour(n_instr *n);
|
||||||
void parcours_instr_ecrire(n_instr *n);
|
void parcours_instr_ecrire(n_instr *n);
|
||||||
int parcours_l_exp(n_l_exp *n);
|
void parcours_l_exp(n_l_exp *n);
|
||||||
char* parcours_exp(n_exp *n);
|
void parcours_exp(n_exp *n); // OKI
|
||||||
char* parcours_varExp(n_exp *n);
|
void parcours_varExp(n_exp *n); // OKI
|
||||||
char* parcours_opExp(n_exp *n);
|
void parcours_opExp(n_exp *n); // OKI
|
||||||
char* parcours_intExp(n_exp *n);
|
void parcours_intExp(n_exp *n); // OKI
|
||||||
char* parcours_lireExp(n_exp *n);
|
void parcours_lireExp(n_exp *n); // OKI
|
||||||
char* parcours_appelExp(n_exp *n);
|
void parcours_appelExp(n_exp *n);
|
||||||
void parcours_l_dec(n_l_dec *n);
|
void parcours_l_dec(n_l_dec *n);
|
||||||
void parcours_dec(n_dec *n);
|
void parcours_dec(n_dec *n);
|
||||||
void parcours_foncDec(n_dec *n);
|
void parcours_foncDec(n_dec *n);
|
||||||
void parcours_varDec(n_dec *n);
|
void parcours_varDec(n_dec *n);
|
||||||
void parcours_tabDec(n_dec *n);
|
void parcours_tabDec(n_dec *n);
|
||||||
char* parcours_var(n_var *n);
|
void parcours_var(n_var *n, char * var); // OKI
|
||||||
char* parcours_var_simple(n_var *n);
|
void parcours_var_simple(n_var *n, char * var); // OKI // modifier pour var non global
|
||||||
char* parcours_var_indicee(n_var *n);
|
void parcours_var_indicee(n_var *n);
|
||||||
void parcours_appel(n_appel *n);
|
void parcours_appel(n_appel *n);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
176
src/parcours.c
176
src/parcours.c
@@ -20,13 +20,45 @@ int newReg() {
|
|||||||
return creg++%10;
|
return creg++%10;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void debutFunc() {
|
||||||
|
printMips("subi $sp, $sp, 4");
|
||||||
|
printMips("sw $fp, 0($sp)");
|
||||||
|
printMips("move $fp, $sp");
|
||||||
|
printMips("subi $sp, $sp, 4");
|
||||||
|
printMips("sw $ra, 0($sp)");
|
||||||
|
}
|
||||||
|
|
||||||
|
void finFunc() {
|
||||||
|
printMips("lw $ra, 0($sp)");
|
||||||
|
printMips("addi $sp, $sp, 4");
|
||||||
|
printMips("lw $fp, 0($sp)");
|
||||||
|
printMips("addi $sp, $sp, 4");
|
||||||
|
printMips("jr $ra");
|
||||||
|
}
|
||||||
|
|
||||||
|
void empile(char *reg) {
|
||||||
|
if(afficheMips) {
|
||||||
|
printf("subu $sp, $sp, 4\n");
|
||||||
|
printf("sw %s, 0($sp)\n", reg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void depile(char *reg) {
|
||||||
|
if(afficheMips) {
|
||||||
|
printf("lw %s, 0($sp)\n", reg);
|
||||||
|
printf("addu $sp, $sp, 4\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*-------------------------------------------------------------------------*/
|
/*-------------------------------------------------------------------------*/
|
||||||
|
|
||||||
void parcours_table(n_prog *n)
|
void parcours_table(n_prog *n)
|
||||||
{
|
{
|
||||||
printMips(".data");
|
printMips(".data");
|
||||||
parcours_l_dec(n->variables);
|
parcours_l_dec(n->variables);
|
||||||
printMips(".text");
|
printMips("jal main");
|
||||||
|
printMips("li $v0, 10");
|
||||||
|
printMips("syscall");
|
||||||
parcours_l_dec(n->fonctions);
|
parcours_l_dec(n->fonctions);
|
||||||
|
|
||||||
int id = rechercheExecutable("main");
|
int id = rechercheExecutable("main");
|
||||||
@@ -106,8 +138,8 @@ void parcours_instr_pour(n_instr *n)
|
|||||||
void parcours_instr_affect(n_instr *n)
|
void parcours_instr_affect(n_instr *n)
|
||||||
{
|
{
|
||||||
|
|
||||||
char * var = parcours_var(n->u.affecte_.var);
|
int var = parcours_var(n->u.affecte_.var);
|
||||||
char * tRegistre = parcours_exp(n->u.affecte_.exp);
|
int tRegistre = parcours_exp(n->u.affecte_.exp);
|
||||||
char tmp[250];
|
char tmp[250];
|
||||||
sprintf(tmp,"sw %s, %s ",tRegistre, var);
|
sprintf(tmp,"sw %s, %s ",tRegistre, var);
|
||||||
printMips(tmp);
|
printMips(tmp);
|
||||||
@@ -147,11 +179,11 @@ void parcours_instr_retour(n_instr *n)
|
|||||||
/*-------------------------------------------------------------------------*/
|
/*-------------------------------------------------------------------------*/
|
||||||
void parcours_instr_ecrire(n_instr *n)
|
void parcours_instr_ecrire(n_instr *n)
|
||||||
{
|
{
|
||||||
char* name = parcours_exp(n->u.ecrire_.expression);
|
parcours_exp(n->u.ecrire_.expression);
|
||||||
char tmp[250];
|
char tmp[250];
|
||||||
int iRegistre;
|
int iRegistre;
|
||||||
iRegistre = newReg();
|
|
||||||
sprintf(tmp,"lw $t%d, %s",iRegistre,name);
|
sprintf(tmp,"lw $t%d, %s",iRegistre, name);
|
||||||
sprintf(name,"$t%d",iRegistre);
|
sprintf(name,"$t%d",iRegistre);
|
||||||
printMips(tmp);
|
printMips(tmp);
|
||||||
sprintf(tmp, "move $a0, %s", name);
|
sprintf(tmp, "move $a0, %s", name);
|
||||||
@@ -173,89 +205,101 @@ int parcours_l_exp(n_l_exp *n)
|
|||||||
|
|
||||||
/*-------------------------------------------------------------------------*/
|
/*-------------------------------------------------------------------------*/
|
||||||
|
|
||||||
char* parcours_exp(n_exp *n)
|
void parcours_exp(n_exp *n)
|
||||||
{
|
{
|
||||||
if(n->type == varExp) return parcours_varExp(n);
|
if(n->type == varExp) parcours_varExp(n);
|
||||||
else if(n->type == opExp) return parcours_opExp(n);
|
else if(n->type == opExp) parcours_opExp(n);
|
||||||
else if(n->type == intExp) return parcours_intExp(n);
|
else if(n->type == intExp) parcours_intExp(n);
|
||||||
else if(n->type == appelExp) return parcours_appelExp(n);
|
else if(n->type == appelExp) parcours_appelExp(n);
|
||||||
else if(n->type == lireExp) return parcours_lireExp(n);
|
else if(n->type == lireExp) parcours_lireExp(n);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*-------------------------------------------------------------------------*/
|
/*-------------------------------------------------------------------------*/
|
||||||
|
|
||||||
char* parcours_varExp(n_exp *n)
|
void parcours_varExp(n_exp *n)
|
||||||
{
|
{
|
||||||
return parcours_var(n->u.var);
|
char var[256];
|
||||||
}
|
parcours_var(n->u.var, &var);
|
||||||
|
|
||||||
/*-------------------------------------------------------------------------*/
|
|
||||||
char* parcours_opExp(n_exp *n)
|
|
||||||
{
|
|
||||||
int iRegistre;
|
|
||||||
iRegistre = newReg();
|
|
||||||
|
|
||||||
char * Exp1 = parcours_exp(n->u.opExp_.op1);
|
|
||||||
char * Exp2 = parcours_exp(n->u.opExp_.op2);
|
|
||||||
|
|
||||||
int iRegistre2;
|
|
||||||
iRegistre2 = newReg();
|
|
||||||
int iRegistre3;
|
|
||||||
iRegistre3 = newReg();
|
|
||||||
//GLOBALE
|
|
||||||
char tmp[256];
|
char tmp[256];
|
||||||
sprintf(tmp,"lw $t%d %s",iRegistre2, Exp1);
|
int iRegistre = newReg();
|
||||||
printMips(tmp);
|
sprintf(tmp, "lw $t%d, %s", iRegistre, var);
|
||||||
sprintf(tmp,"lw $t%d %s",iRegistre3, Exp2);
|
|
||||||
printMips(tmp);
|
printMips(tmp);
|
||||||
|
sprintf(tmp, "$t%d", iRegistre);
|
||||||
|
empile(tmp);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*-------------------------------------------------------------------------*/
|
||||||
|
void parcours_opExp(n_exp *n)
|
||||||
|
{
|
||||||
|
int iRegistre = newReg();
|
||||||
|
int iRegistre2 = newReg();
|
||||||
|
int iRegistre3 = newReg();
|
||||||
|
|
||||||
|
parcours_exp(n->u.opExp_.op1);
|
||||||
|
parcours_exp(n->u.opExp_.op2);
|
||||||
|
|
||||||
|
char tmp[256];
|
||||||
|
|
||||||
|
sprintf(tmp, "$t%d", iRegistre3);
|
||||||
|
depile(tmp);
|
||||||
|
sprintf(tmp, "$t%d", iRegistre2);
|
||||||
|
depile(tmp);
|
||||||
|
|
||||||
|
|
||||||
if(n->u.opExp_.op == plus) {
|
if(n->u.opExp_.op == plus) {
|
||||||
sprintf(tmp,"add $t%d, $t%d, $t%d",iRegistre, iRegistre2,iRegistre3);
|
sprintf(tmp,"add $t%d, $t%d, $t%d",iRegistre, iRegistre2, iRegistre3);
|
||||||
printMips(tmp);
|
printMips(tmp);
|
||||||
}
|
} else if(n->u.opExp_.op == moins){
|
||||||
else if(n->u.opExp_.op == moins){
|
sprintf(tmp,"sub $t%d, $t%d, $t%d",iRegistre, iRegistre2, iRegistre3);
|
||||||
sprintf(tmp,"sub $t%d, $t%d, $t%d",iRegistre, iRegistre2,iRegistre3);
|
|
||||||
printMips(tmp);
|
printMips(tmp);
|
||||||
}
|
} else if(n->u.opExp_.op == fois) {
|
||||||
if(n->u.opExp_.op == fois) {
|
sprintf(tmp,"mult $t%d, $t%d", iRegistre2, iRegistre3);
|
||||||
sprintf(tmp,"mult $t%d, $t%d", iRegistre2,iRegistre3);
|
printMips(tmp);
|
||||||
|
sprintf(tmp,"mflo $t%d",iRegistre);
|
||||||
|
printMips(tmp);
|
||||||
|
} else if(n->u.opExp_.op == divise) {
|
||||||
|
sprintf(tmp,"div $t%d, $t%d", iRegistre2, iRegistre3);
|
||||||
printMips(tmp);
|
printMips(tmp);
|
||||||
sprintf(tmp,"mflo $t%d",iRegistre);
|
sprintf(tmp,"mflo $t%d",iRegistre);
|
||||||
printMips(tmp);
|
printMips(tmp);
|
||||||
}
|
}
|
||||||
char * back = malloc(sizeof (char) * 256);
|
|
||||||
sprintf(back,"$t%d",iRegistre);
|
sprintf(tmp, "$t%d", iRegistre);
|
||||||
return back;
|
empile(tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*-------------------------------- -----------------------------------------*/
|
/*-------------------------------- -----------------------------------------*/
|
||||||
|
|
||||||
char* parcours_intExp(n_exp *n)
|
void parcours_intExp(n_exp *n)
|
||||||
{
|
{
|
||||||
char *texte = malloc (sizeof (char) * 256); // Max. 50 chiffres
|
|
||||||
int iRegistre = newReg();
|
int iRegistre = newReg();
|
||||||
sprintf(texte, "li $t%d, %d",iRegistre, n->u.entier);
|
|
||||||
printMips(texte);
|
char tmp[256];
|
||||||
sprintf(texte,"$t%d",iRegistre);
|
sprintf(tmp, "li $t%d, %d", iRegistre, n->u.entier);
|
||||||
return texte;
|
printMips(tmp);
|
||||||
|
sprintf(tmp, "$t%d", iRegistre);
|
||||||
|
empile(tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*-------------------------------------------------------------------------*/
|
/*-------------------------------------------------------------------------*/
|
||||||
char* parcours_lireExp(n_exp *n)
|
void parcours_lireExp(n_exp *n)
|
||||||
{
|
{
|
||||||
printMips("li $v0 5");
|
printMips("li $v0 5");
|
||||||
printMips("syscall");
|
printMips("syscall");
|
||||||
|
|
||||||
int iRegistre = newReg();
|
int iRegistre = newReg();
|
||||||
char *tmp = malloc (sizeof (char) * 256);
|
char tmp[256];
|
||||||
|
|
||||||
sprintf(tmp, "move $t%d, $v0", iRegistre);
|
sprintf(tmp, "move $t%d, $v0", iRegistre);
|
||||||
printMips(tmp);
|
printMips(tmp);
|
||||||
sprintf(tmp, "$t%d",iRegistre);
|
sprintf(tmp, "$t%d", iRegistre);
|
||||||
return tmp;
|
empile(tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*-------------------------------------------------------------------------*/
|
/*-------------------------------------------------------------------------*/
|
||||||
|
|
||||||
char* parcours_appelExp(n_exp *n)
|
void parcours_appelExp(n_exp *n)
|
||||||
{
|
{
|
||||||
parcours_appel(n->u.appel);
|
parcours_appel(n->u.appel);
|
||||||
}
|
}
|
||||||
@@ -292,6 +336,11 @@ void parcours_dec(n_dec *n)
|
|||||||
|
|
||||||
void parcours_foncDec(n_dec *n)
|
void parcours_foncDec(n_dec *n)
|
||||||
{
|
{
|
||||||
|
char tmp[256];
|
||||||
|
sprintf(tmp, "%s :", n->nom);
|
||||||
|
printMips(tmp);
|
||||||
|
debutFunc();
|
||||||
|
|
||||||
int i = rechercheExecutable(n->nom);
|
int i = rechercheExecutable(n->nom);
|
||||||
if( i >= 0) {
|
if( i >= 0) {
|
||||||
printf("Fonction %s déjà déclaré\n", n->nom);
|
printf("Fonction %s déjà déclaré\n", n->nom);
|
||||||
@@ -316,6 +365,7 @@ void parcours_foncDec(n_dec *n)
|
|||||||
contexte = C_VARIABLE_LOCALE;
|
contexte = C_VARIABLE_LOCALE;
|
||||||
parcours_l_dec(n->u.foncDec_.variables);
|
parcours_l_dec(n->u.foncDec_.variables);
|
||||||
parcours_instr(n->u.foncDec_.corps);
|
parcours_instr(n->u.foncDec_.corps);
|
||||||
|
finFunc();
|
||||||
affiche_dico();
|
affiche_dico();
|
||||||
sortieFonction();
|
sortieFonction();
|
||||||
}
|
}
|
||||||
@@ -337,6 +387,7 @@ void parcours_varDec(n_dec *n)
|
|||||||
printMips(tmp);
|
printMips(tmp);
|
||||||
adresseGlobalCourante += 4;
|
adresseGlobalCourante += 4;
|
||||||
} else if (contexte == C_VARIABLE_LOCALE) {
|
} else if (contexte == C_VARIABLE_LOCALE) {
|
||||||
|
printMips("subbi $sp, $sp, 4");
|
||||||
ajouteIdentificateur(n->nom, contexte, T_ENTIER, adresseLocaleCourante, -1);
|
ajouteIdentificateur(n->nom, contexte, T_ENTIER, adresseLocaleCourante, -1);
|
||||||
adresseLocaleCourante += 4;
|
adresseLocaleCourante += 4;
|
||||||
} else if (contexte == C_ARGUMENT) {
|
} else if (contexte == C_ARGUMENT) {
|
||||||
@@ -378,18 +429,18 @@ void parcours_tabDec(n_dec *n)
|
|||||||
|
|
||||||
/*-------------------------------------------------------------------------*/
|
/*-------------------------------------------------------------------------*/
|
||||||
|
|
||||||
char* parcours_var(n_var *n)
|
void parcours_var(n_var *n, , char * var)
|
||||||
{
|
{
|
||||||
if(n->type == simple) {
|
if(n->type == simple) {
|
||||||
return parcours_var_simple(n);
|
parcours_var_simple(n, var);
|
||||||
}
|
}
|
||||||
else if(n->type == indicee) {
|
else if(n->type == indicee) {
|
||||||
return parcours_var_indicee(n);
|
parcours_var_indicee(n, var);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*-------------------------------------------------------------------------*/
|
/*-------------------------------------------------------------------------*/
|
||||||
char* parcours_var_simple(n_var *n)
|
void parcours_var_simple(n_var *n, char * var)
|
||||||
{
|
{
|
||||||
int i = rechercheExecutable(n->nom);
|
int i = rechercheExecutable(n->nom);
|
||||||
if( i < 0) {
|
if( i < 0) {
|
||||||
@@ -402,13 +453,15 @@ char* parcours_var_simple(n_var *n)
|
|||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
char *tmp = malloc (sizeof (char) * 256);
|
if(dico.tab[i].classe == C_VARIABLE_GLOBALE) {
|
||||||
sprintf(tmp,"$%s",n->nom);
|
sprintf(var, "$%s", n->nom);
|
||||||
return tmp;
|
} else {
|
||||||
|
sprintf(var, "%d", n->adresse);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*-------------------------------------------------------------------------*/
|
/*-------------------------------------------------------------------------*/
|
||||||
char* parcours_var_indicee(n_var *n)
|
void parcours_var_indicee(n_var *n, char * var)
|
||||||
{
|
{
|
||||||
int i = rechercheExecutable(n->nom);
|
int i = rechercheExecutable(n->nom);
|
||||||
if( i < 0) {
|
if( i < 0) {
|
||||||
@@ -422,7 +475,6 @@ char* parcours_var_indicee(n_var *n)
|
|||||||
}
|
}
|
||||||
|
|
||||||
parcours_exp( n->u.indicee_.indice );
|
parcours_exp( n->u.indicee_.indice );
|
||||||
return n->nom;
|
|
||||||
}
|
}
|
||||||
/*-------------------------------------------------------------------------*/
|
/*-------------------------------------------------------------------------*/
|
||||||
|
|
||||||
Reference in New Issue
Block a user