Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision
Loading items

Target

Select target project
No results found
Select Git revision
Loading items
Show changes
Showing
with 322 additions and 30 deletions
...@@ -104,6 +104,8 @@ public class Compiler ...@@ -104,6 +104,8 @@ public class Compiler
scRoot.apply(sc2sa); scRoot.apply(sc2sa);
saRoot = sc2sa.getRoot(); saRoot = sc2sa.getRoot();
} catch (Exception ignored) { } catch (Exception ignored) {
System.out.println("exception construction arbre abstrait");
ignored.printStackTrace();
} }
PrintStream out = System.out; PrintStream out = System.out;
if (verboseLevel > 1) { if (verboseLevel > 1) {
...@@ -132,7 +134,8 @@ public class Compiler ...@@ -132,7 +134,8 @@ public class Compiler
System.err.print("ERREUR TABLE DES SYMBOLES : "); System.err.print("ERREUR TABLE DES SYMBOLES : ");
System.err.println(e.getMessage()); System.err.println(e.getMessage());
System.exit(e.getCode()); System.exit(e.getCode());
} catch (Exception ignored) { } catch (Exception e) {
e.printStackTrace();
} }
if (verboseLevel > 1) { if (verboseLevel > 1) {
System.out.println("[PRINT TS]"); System.out.println("[PRINT TS]");
......
...@@ -30,12 +30,14 @@ saexpinf = 'SaExpInf'; ...@@ -30,12 +30,14 @@ saexpinf = 'SaExpInf';
saexpint = 'SaExpInt'; saexpint = 'SaExpInt';
saexplire = 'SaExpLire'; saexplire = 'SaExpLire';
saexpmult = 'SaExpMult'; saexpmult = 'SaExpMult';
saexpmodulo = 'SaExpModulo';
saexpnot = 'SaExpNot'; saexpnot = 'SaExpNot';
saexpor = 'SaExpOr'; saexpor = 'SaExpOr';
saexpsub = 'SaExpSub'; saexpsub = 'SaExpSub';
saexpvar = 'SaExpVar'; saexpvar = 'SaExpVar';
saexpvrai = 'SaExpVrai'; saexpvrai = 'SaExpVrai';
sainstaffect = 'SaInstAffect'; sainstaffect = 'SaInstAffect';
saincr= 'SaIncr';
sainstbloc = 'SaInstBloc'; sainstbloc = 'SaInstBloc';
sainstecriture = 'SaInstEcriture'; sainstecriture = 'SaInstEcriture';
sainstretour = 'SaInstRetour'; sainstretour = 'SaInstRetour';
...@@ -73,6 +75,7 @@ exp = {add} po saexpadd [op1]:exp [op2]:exp pf ...@@ -73,6 +75,7 @@ exp = {add} po saexpadd [op1]:exp [op2]:exp pf
| {equal} po saexpequal [op1]:exp [op2]:exp pf | {equal} po saexpequal [op1]:exp [op2]:exp pf
| {inf} po saexpinf [op1]:exp [op2]:exp pf | {inf} po saexpinf [op1]:exp [op2]:exp pf
| {mult} po saexpmult [op1]:exp [op2]:exp pf | {mult} po saexpmult [op1]:exp [op2]:exp pf
| {modulo} po saexpmodulo [op1]:exp [op2]:exp pf
| {or} po saexpor [op1]:exp [op2]:exp pf | {or} po saexpor [op1]:exp [op2]:exp pf
| {sub} po saexpsub [op1]:exp [op2]:exp pf | {sub} po saexpsub [op1]:exp [op2]:exp pf
| {not} po saexpnot exp pf | {not} po saexpnot exp pf
...@@ -85,6 +88,7 @@ exp = {add} po saexpadd [op1]:exp [op2]:exp pf ...@@ -85,6 +88,7 @@ exp = {add} po saexpadd [op1]:exp [op2]:exp pf
; ;
inst = {affect} po sainstaffect var exp pf inst = {affect} po sainstaffect var exp pf
| {incr} po saincr var exp pf
| {bloc} po sainstbloc linst pf | {bloc} po sainstbloc linst pf
| {ecriture} po sainstecriture exp pf | {ecriture} po sainstecriture exp pf
| {retour} po sainstretour exp pf | {retour} po sainstretour exp pf
......
...@@ -159,6 +159,19 @@ public class LoadSa extends DepthFirstAdapter { ...@@ -159,6 +159,19 @@ public class LoadSa extends DepthFirstAdapter {
outAMultExp(node); outAMultExp(node);
} }
@Override
public void caseAModuloExp(AModuloExp node)
{
inAModuloExp(node);
node.getOp1().apply(this);
SaExp op1 = (SaExp) returnValue;
node.getOp2().apply(this);
SaExp op2 = (SaExp) returnValue;
returnValue = new SaExpModulo(op1,op2);
outAModuloExp(node);
}
// exp = {or} po saexpor [op1]:exp [op2]:exp pf // exp = {or} po saexpor [op1]:exp [op2]:exp pf
@Override @Override
public void caseAOrExp(AOrExp node) public void caseAOrExp(AOrExp node)
...@@ -267,6 +280,20 @@ public class LoadSa extends DepthFirstAdapter { ...@@ -267,6 +280,20 @@ public class LoadSa extends DepthFirstAdapter {
outAAffectInst(node); outAAffectInst(node);
} }
@Override
public void caseAIncrInst(AIncrInst node)
{
inAIncrInst(node);
node.getVar().apply(this);
SaVar var = (SaVar) returnValue;
node.getExp().apply(this);
SaExp exp = (SaExp) returnValue;
returnValue = new SaIncr(var, exp);
outAIncrInst(node);
}
// inst = {bloc} po sainstbloc linst pf // inst = {bloc} po sainstbloc linst pf
@Override @Override
public void caseABlocInst(ABlocInst node) public void caseABlocInst(ABlocInst node)
......
...@@ -187,6 +187,15 @@ public class SaDepthFirstVisitor <T> implements SaVisitor <T>{ ...@@ -187,6 +187,15 @@ public class SaDepthFirstVisitor <T> implements SaVisitor <T>{
return null; return null;
} }
public T visit(SaIncr node) throws Exception
{
defaultIn(node);
node.getLhs().accept(this);
node.getRhs().accept(this);
defaultOut(node);
return null;
}
// LDEC -> DEC LDEC // LDEC -> DEC LDEC
// LDEC -> null // LDEC -> null
/* public T visit(SaLDec node) throws Exception /* public T visit(SaLDec node) throws Exception
...@@ -269,6 +278,15 @@ public class SaDepthFirstVisitor <T> implements SaVisitor <T>{ ...@@ -269,6 +278,15 @@ public class SaDepthFirstVisitor <T> implements SaVisitor <T>{
return null; return null;
} }
public T visit(SaExpModulo node) throws Exception
{
defaultIn(node);
node.getOp1().accept(this);
node.getOp2().accept(this);
defaultOut(node);
return null;
}
// EXP -> div EXP EXP // EXP -> div EXP EXP
public T visit(SaExpDiv node) throws Exception public T visit(SaExpDiv node) throws Exception
{ {
......
...@@ -246,6 +246,21 @@ public class SaEval extends SaDepthFirstVisitor <TypeVal> { ...@@ -246,6 +246,21 @@ public class SaEval extends SaDepthFirstVisitor <TypeVal> {
defaultOut(node); defaultOut(node);
return null; return null;
} }
public TypeVal visit(SaIncr node) throws Exception
{
defaultIn(node);
TypeVal incr = node.getRhs().accept(this);
if(node.getLhs() instanceof SaVarIndicee){ // c'est une case de tableau, donc forcément globale
SaVarIndicee lhsIndicee = (SaVarIndicee) node.getLhs();
TypeVal indice = lhsIndicee.getIndice().accept(this);
setVarGlobIndicee(lhsIndicee, indice, new TypeVal(getVarGlobIndicee(lhsIndicee, indice).valInt + incr.valInt));
} else{
setVar((SaVarSimple) node.getLhs(), new TypeVal(getVar((SaVarSimple) node.getLhs()).valInt + incr.valInt));
}
defaultOut(node);
return null;
}
// LDEC -> DEC LDEC // LDEC -> DEC LDEC
// LDEC -> null // LDEC -> null
...@@ -342,6 +357,15 @@ public class SaEval extends SaDepthFirstVisitor <TypeVal> { ...@@ -342,6 +357,15 @@ public class SaEval extends SaDepthFirstVisitor <TypeVal> {
return new TypeVal(op1.valInt * op2.valInt); return new TypeVal(op1.valInt * op2.valInt);
} }
public TypeVal visit(SaExpModulo node) throws Exception
{
defaultIn(node);
TypeVal op1 = node.getOp1().accept(this);
TypeVal op2 = node.getOp2().accept(this);
defaultOut(node);
return new TypeVal(op1.valInt % op2.valInt);
}
// EXP -> div EXP EXP // EXP -> div EXP EXP
public TypeVal visit(SaExpDiv node) throws Exception public TypeVal visit(SaExpDiv node) throws Exception
{ {
......
package sa;
import util.Type;
public class SaExpModulo implements SaExp{
private SaExp op1;
private SaExp op2;
public SaExpModulo(SaExp op1, SaExp op2){
this.op1 = op1;
this.op2 = op2;
}
public SaExp getOp1(){return this.op1;}
public SaExp getOp2(){return this.op2;}
public Type getType(){
return Type.ENTIER;
}
public <T> T accept(SaVisitor <T> visitor) throws Exception{
return visitor.visit(this);
}
@Override
public String toString() {
return "(" + this.getClass().getSimpleName() + " " + op1 + " " + op2 + ")";
}
}
package sa;
public class SaIncr implements SaInst{
private SaVar lhs;
private SaExp rhs;
public SaIncr(SaVar lhs, SaExp rhs){
this.lhs = lhs;
this.rhs = rhs;
}
public SaVar getLhs(){return this.lhs;}
public SaExp getRhs(){return this.rhs;}
public <T> T accept(SaVisitor <T> visitor) throws Exception{
return visitor.visit(this);
}
@Override
public String toString() {
return "(" + this.getClass().getSimpleName()+ " " + lhs + " " + rhs + ")";
}
}
...@@ -28,6 +28,7 @@ interface SaVisitor <T> { ...@@ -28,6 +28,7 @@ interface SaVisitor <T> {
public T visit(SaExpAdd node) throws Exception; public T visit(SaExpAdd node) throws Exception;
public T visit(SaExpSub node) throws Exception; public T visit(SaExpSub node) throws Exception;
public T visit(SaExpMult node) throws Exception; public T visit(SaExpMult node) throws Exception;
public T visit(SaExpModulo node) throws Exception;
public T visit(SaExpDiv node) throws Exception; public T visit(SaExpDiv node) throws Exception;
public T visit(SaExpInf node) throws Exception; public T visit(SaExpInf node) throws Exception;
public T visit(SaExpEqual node) throws Exception; public T visit(SaExpEqual node) throws Exception;
...@@ -35,4 +36,6 @@ interface SaVisitor <T> { ...@@ -35,4 +36,6 @@ interface SaVisitor <T> {
public T visit(SaExpOr node) throws Exception; public T visit(SaExpOr node) throws Exception;
public T visit(SaExpNot node) throws Exception; public T visit(SaExpNot node) throws Exception;
public T visit(SaLExp node) throws Exception; public T visit(SaLExp node) throws Exception;
public T visit(SaIncr node) throws Exception;
} }
...@@ -34,5 +34,129 @@ public class ColorGraph { ...@@ -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]);
}
}
} }
...@@ -19,7 +19,7 @@ fibo fbegin #entree fonction ...@@ -19,7 +19,7 @@ fibo fbegin #entree fonction
fend fend
1 fend 1 fend
main fbegin #entree fonction main fbegin #entree fonction
param 9 param 3
@7 = call fibo @7 = call fibo
write @7 write @7
fend fend
...@@ -18,16 +18,16 @@ ERR_LEX_CODE = 6 ...@@ -18,16 +18,16 @@ ERR_LEX_CODE = 6
SC_COEFF = 4 SC_COEFF = 4
SA_DIFF_COEFF = 0 SA_DIFF_COEFF = 0
TS_DIFF_COEFF = 0 TS_DIFF_COEFF = 0
SA_COEFF = 3 SA_COEFF = 4
C3A_DIFF_COEFF = 0 C3A_DIFF_COEFF = 0
C3A_COEFF = 3 C3A_COEFF = 4
PRE_NASM_DIFF = 0 PRE_NASM_DIFF = 0
PRE_NASM_COEFF = 3 PRE_NASM_COEFF = 3
NASM_DIFF_COEFF = 0 NASM_DIFF_COEFF = 0
NASM_COEFF = 3 NASM_COEFF = 3
EXE_COEFF = 0 EXE_COEFF = 0
ERR_TS_COEFF = 2 ERR_TS_COEFF = 2
ERR_TYPE_COEFF = 2 ERR_TYPE_COEFF = 0
# Paths relative to this file (evaluate.py) # Paths relative to this file (evaluate.py)
inputPath = "./input/" inputPath = "./input/"
......
...@@ -10,5 +10,5 @@ entier fibo( entier n ) ...@@ -10,5 +10,5 @@ entier fibo( entier n )
main() main()
{ {
ecrire( fibo( 9 ) ); ecrire( fibo( 3 ) );
} }
...@@ -72,7 +72,7 @@ main : push ebp ;sauvegarde la valeur de ebp ...@@ -72,7 +72,7 @@ main : push ebp ;sauvegarde la valeur de ebp
push ecx ;sauvegarde de ecx push ecx ;sauvegarde de ecx
push edx ;sauvegarde de edx push edx ;sauvegarde de edx
sub esp, 0 ;allocation des variables locales sub esp, 0 ;allocation des variables locales
push 9 ;Param push 3 ;Param
sub esp, 4 ;allocation mémoire pour la valeur de retour sub esp, 4 ;allocation mémoire pour la valeur de retour
call fibo call fibo
pop eax ;récupération de la valeur de retour pop eax ;récupération de la valeur de retour
......
55 3
...@@ -72,7 +72,7 @@ main : push ebp ;sauvegarde la valeur de ebp ...@@ -72,7 +72,7 @@ main : push ebp ;sauvegarde la valeur de ebp
push ecx ;sauvegarde de ecx push ecx ;sauvegarde de ecx
push edx ;sauvegarde de edx push edx ;sauvegarde de edx
sub esp, 0 ;allocation des variables locales sub esp, 0 ;allocation des variables locales
push 9 ;Param push 3 ;Param
sub esp, 4 ;allocation mémoire pour la valeur de retour sub esp, 4 ;allocation mémoire pour la valeur de retour
call fibo call fibo
pop @7 ;récupération de la valeur de retour pop @7 ;récupération de la valeur de retour
......
(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)))
@0 = call main
stop @0
main fbegin #entree fonction
a = 4
@1 = a + 1
a = @1
write a
fend
@0 = call main
stop @0
main fbegin #entree fonction
a = 4
@1 = a + 1
a = @1
write a
fend
@0 = call main
stop @0
main fbegin #entree fonction
t[1] = 100
@1 = t[1] + 10
t[1] = @1
write t[1]
fend
@0 = call main
stop @0
main fbegin #entree fonction
a = 4
@1 = 12 * 2
@2 = a + @1
@3 = a + @2
a = @3
write a
fend