diff --git a/src/util/graph/ColorGraph.java b/src/util/graph/ColorGraph.java index 55b867e821c8182073a421475d2ab4cb875c7215..61457fbb3de49feb2268a8f358f5c37724460306 100644 --- a/src/util/graph/ColorGraph.java +++ b/src/util/graph/ColorGraph.java @@ -34,5 +34,129 @@ public class ColorGraph { } } + /*-------------------------------------------------------------------------------------------------------------*/ + /* associe une couleur à tous les sommets se trouvant dans la pile */ + /*-------------------------------------------------------------------------------------------------------------*/ + + public void select() + { + int t; + while(!stack.empty()){ + t = stack.pop(); + removed.remove(t); + if(color[t] == NOCOLOR) + color[t] = chooseAvailableColor(neighborsColor(t)); + if(color[t] == NOCOLOR) + System.out.println("cannot find a color for vertex "+ t); + } + } + + /*-------------------------------------------------------------------------------------------------------------*/ + /* récupère les couleurs des voisins de t */ + /*-------------------------------------------------------------------------------------------------------------*/ + + public IntSet neighborsColor(int t) + { + IntSet colorSet = new IntSet(colorNb); + + for(NodeList p = int2Node[t].succ(); p!=null; p=p.tail) + if(color[p.head.label()] != NOCOLOR) + colorSet.add(color[p.head.label()]); + return colorSet; + } + + /*-------------------------------------------------------------------------------------------------------------*/ + /* recherche une couleur absente de colorSet */ + /*-------------------------------------------------------------------------------------------------------------*/ + + public int chooseAvailableColor(IntSet colorSet) + { + for(int c=0; c < colorSet.getSize(); c++) + if(!colorSet.isMember(c)) + return c; + return NOCOLOR; + } + + /*-------------------------------------------------------------------------------------------------------------*/ + /* calcule le nombre de voisins du sommet t */ + /*-------------------------------------------------------------------------------------------------------------*/ + + public int neighborsNb(int t) + { + int nb = 0; + for(NodeList p = this.int2Node[t].succ(); p!=null; p=p.tail) + if(!removed.isMember(p.head.label())) + nb++; + return nb; + } + + /*-------------------------------------------------------------------------------------------------------------*/ + /* simplifie le graphe d'interférence g */ + /* la simplification consiste à enlever du graphe les temporaires qui ont moins de k voisins */ + /* et à les mettre dans une pile */ + /* à la fin du processus, le graphe peut ne pas être vide, il s'agit des temporaires qui ont au moins k voisin */ + /*-------------------------------------------------------------------------------------------------------------*/ + + public int simplify() + { + boolean removedAtLeastOneTemp = true; + while(removedAtLeastOneTemp && stack.size() != vertexNb){ + removedAtLeastOneTemp = false; + for(int t = 0; t < vertexNb; t++){ + if(!removed.isMember(t)){ + int n = neighborsNb(t); + // System.out.println("node " + t + " has " + n + " neighbours"); + int precolored = (color[t] == NOCOLOR)? 0 : 1; + if(n < (colorNb - precolored)){ + stack.push(t); + removed.add(t); + // System.out.println("remove vertex " + t); + removedAtLeastOneTemp = true; + } + } + } + } + return stack.size(); + } + + /*-------------------------------------------------------------------------------------------------------------*/ + /*-------------------------------------------------------------------------------------------------------------*/ + + public void spill() + { + int t; + while(stack.size() != vertexNb){ /* some nodes have not been pushed */ + for(t=0; t < vertexNb; t++){ + if(!removed.isMember(t)){ /* t i still in the graph */ + System.out.println("vertex " + t + " is a potential spill"); + spill.add(t); + removed.add(t); + stack.push(t); + simplify(); + } + } + } + } + + + /*-------------------------------------------------------------------------------------------------------------*/ + /*-------------------------------------------------------------------------------------------------------------*/ + + public void color() + { + this.simplify(); + this.spill(); + this.select(); + } + + public void affiche() + { + System.out.println("vertex\tcolor"); + for(int i = 0; i < vertexNb; i++){ + System.out.println(i + "\t" + color[i]); + } + } + + } diff --git a/test/c3a-ref/fibo.c3a b/test/c3a-ref/fibo.c3a index a67a5259d751fa02376cc809d14a249698da7adc..54c7eee800a02330d5079dbfecc6f4de98036726 100644 --- a/test/c3a-ref/fibo.c3a +++ b/test/c3a-ref/fibo.c3a @@ -19,7 +19,7 @@ fibo fbegin #entree fonction fend 1 fend main fbegin #entree fonction - param 9 + param 3 @7 = call fibo write @7 fend diff --git a/test/evaluate.py b/test/evaluate.py index febaaf25b8cfd221f36fb690f76da55d10ae42ab..cace0c27a0a206196b9822fbdce4279bf0ed5457 100755 --- a/test/evaluate.py +++ b/test/evaluate.py @@ -18,16 +18,16 @@ ERR_LEX_CODE = 6 SC_COEFF = 4 SA_DIFF_COEFF = 0 TS_DIFF_COEFF = 0 -SA_COEFF = 3 +SA_COEFF = 4 C3A_DIFF_COEFF = 0 -C3A_COEFF = 3 +C3A_COEFF = 4 PRE_NASM_DIFF = 0 PRE_NASM_COEFF = 3 NASM_DIFF_COEFF = 0 NASM_COEFF = 3 EXE_COEFF = 0 ERR_TS_COEFF = 2 -ERR_TYPE_COEFF = 2 +ERR_TYPE_COEFF = 0 # Paths relative to this file (evaluate.py) inputPath = "./input/" diff --git a/test/input/fibo.l b/test/input/fibo.l index f67922d1d7a91db785e6f240983a44174d93b51f..71b4af8194d7d94568580df5c4953794bff28018 100644 --- a/test/input/fibo.l +++ b/test/input/fibo.l @@ -10,5 +10,5 @@ entier fibo( entier n ) main() { - ecrire( fibo( 9 ) ); + ecrire( fibo( 3 ) ); } diff --git a/test/nasm-ref/fibo.nasm b/test/nasm-ref/fibo.nasm index ec408a375dfe24c1246df3e9a453499df4595844..21a296f2b2937d736c8659f0570798d92ca9bdec 100644 --- a/test/nasm-ref/fibo.nasm +++ b/test/nasm-ref/fibo.nasm @@ -72,7 +72,7 @@ main : push ebp ;sauvegarde la valeur de ebp push ecx ;sauvegarde de ecx push edx ;sauvegarde de edx sub esp, 0 ;allocation des variables locales - push 9 ;Param + push 3 ;Param sub esp, 4 ;allocation mémoire pour la valeur de retour call fibo pop eax ;récupération de la valeur de retour diff --git a/test/out-ref/fibo.out b/test/out-ref/fibo.out index c3f407c0955bb5738e40a82664c79b63f04a9adb..00750edc07d6415dcc07ae0351e9397b0222b7ba 100644 --- a/test/out-ref/fibo.out +++ b/test/out-ref/fibo.out @@ -1 +1 @@ -55 +3 diff --git a/test/pre-nasm-ref/fibo.pre-nasm b/test/pre-nasm-ref/fibo.pre-nasm index bea531a62c6829c395621108469648e4d03f58e6..e157dca58b2c9215bd7b9b0b593dbb373944a826 100644 --- a/test/pre-nasm-ref/fibo.pre-nasm +++ b/test/pre-nasm-ref/fibo.pre-nasm @@ -72,7 +72,7 @@ main : push ebp ;sauvegarde la valeur de ebp push ecx ;sauvegarde de ecx push edx ;sauvegarde de edx sub esp, 0 ;allocation des variables locales - push 9 ;Param + push 3 ;Param sub esp, 4 ;allocation mémoire pour la valeur de retour call fibo pop @7 ;récupération de la valeur de retour diff --git a/test/sa-ref/fibo.sa b/test/sa-ref/fibo.sa index 407ebdef6012000c38432bb735959ffea8990f7a..86e597c87e77a0a152780dde5604d735165f7054 100644 --- a/test/sa-ref/fibo.sa +++ b/test/sa-ref/fibo.sa @@ -1 +1 @@ -(SaProg null (SaLDecFonc (SaDecFonc fibo entier (SaLDecVar (SaDecVarSimple n entier) null) null (SaInstBloc (SaLInst (SaInstSi (SaExpInf (SaExpVar (SaVarSimple n)) 2) (SaInstBloc (SaLInst (SaInstRetour 1) null)) (SaInstBloc (SaLInst (SaInstRetour (SaExpAdd (SaExpAppel (SaAppel fibo (SaLExp (SaExpSub (SaExpVar (SaVarSimple n)) 1) null))) (SaExpAppel (SaAppel fibo (SaLExp (SaExpSub (SaExpVar (SaVarSimple n)) 2) null))))) null))) null))) (SaLDecFonc (SaDecFonc main nul null null (SaInstBloc (SaLInst (SaInstEcriture (SaExpAppel (SaAppel fibo (SaLExp 9 null)))) null))) null))) +(SaProg null (SaLDecFonc (SaDecFonc fibo entier (SaLDecVar (SaDecVarSimple n entier) null) null (SaInstBloc (SaLInst (SaInstSi (SaExpInf (SaExpVar (SaVarSimple n)) 2) (SaInstBloc (SaLInst (SaInstRetour 1) null)) (SaInstBloc (SaLInst (SaInstRetour (SaExpAdd (SaExpAppel (SaAppel fibo (SaLExp (SaExpSub (SaExpVar (SaVarSimple n)) 1) null))) (SaExpAppel (SaAppel fibo (SaLExp (SaExpSub (SaExpVar (SaVarSimple n)) 2) null))))) null))) null))) (SaLDecFonc (SaDecFonc main nul null null (SaInstBloc (SaLInst (SaInstEcriture (SaExpAppel (SaAppel fibo (SaLExp 3 null)))) null))) null)))