Skip to content
Snippets Groups Projects
Commit 42e8b37d authored by BAZIZI Zakaria's avatar BAZIZI Zakaria
Browse files

11.9

parent ad1c7593
Branches
No related tags found
No related merge requests found
...@@ -33,13 +33,13 @@ public class Compiler ...@@ -33,13 +33,13 @@ public class Compiler
buildSc(); buildSc();
System.out.println("[BUILD SA] "); System.out.println("[BUILD SA] ");
buildSa(); buildSa();
/*
System.out.println("[BUILD TS] "); System.out.println("[BUILD TS] ");
buildTs(); buildTs();
System.out.println("[TYPE CHECKING]"); System.out.println("[TYPE CHECKING]");
typeCheck(); typeCheck();
System.out.println("[BUILD C3A] "); System.out.println("[BUILD C3A] ");
buildC3a(); buildC3a();
/*
System.out.println("[BUILD PRE NASM] "); System.out.println("[BUILD PRE NASM] ");
buildPreNasm(); buildPreNasm();
System.out.println("[BUILD FLOW GRAPH] "); System.out.println("[BUILD FLOW GRAPH] ");
...@@ -48,6 +48,7 @@ public class Compiler ...@@ -48,6 +48,7 @@ public class Compiler
solveFg(); solveFg();
System.out.println("[BUILD INTERFERENCE GRAPH] "); System.out.println("[BUILD INTERFERENCE GRAPH] ");
buildIg(); buildIg();
System.out.println("[ALLOCATE REGISTERS]"); System.out.println("[ALLOCATE REGISTERS]");
interferenceGraph.allocateRegisters(); interferenceGraph.allocateRegisters();
System.out.println("[PRINT NASM]"); System.out.println("[PRINT NASM]");
......
package c3a; package c3a;
import java.util.*;
import ts.*; import ts.*;
import sa.*; import sa.*;
public class Sa2c3a extends SaDepthFirstVisitor <C3aOperand> { public class Sa2c3a extends SaDepthFirstVisitor <C3aOperand> {
private C3a c3a; private C3a c3a;
int indentation; int indentation;
...@@ -14,36 +14,261 @@ public class Sa2c3a extends SaDepthFirstVisitor <C3aOperand> { ...@@ -14,36 +14,261 @@ public class Sa2c3a extends SaDepthFirstVisitor <C3aOperand> {
C3aFunction fct = new C3aFunction(tableGlobale.getFct("main")); C3aFunction fct = new C3aFunction(tableGlobale.getFct("main"));
c3a.ajouteInst(new C3aInstCall(fct, result, "")); c3a.ajouteInst(new C3aInstCall(fct, result, ""));
c3a.ajouteInst(new C3aInstStop(result, "")); c3a.ajouteInst(new C3aInstStop(result, ""));
indentation = 0; indentation = 0;
try{
root.accept(this);
}
catch(Exception e){}
} }
public void defaultIn(SaNode node) /*public void defaultIn(SaNode node)
{ {
//for(int i = 0; i < indentation; i++){System.out.print(" ");} for(int i = 0; i < indentation; i++){System.out.print(" ");}
//indentation++; indentation++;
//System.out.println("<" + node.getClass().getSimpleName() + ">"); System.out.println("<" + node.getClass().getSimpleName() + ">");
} }
public void defaultOut(SaNode node) public void defaultOut(SaNode node)
{ {
//indentation--; indentation--;
// for(int i = 0; i < indentation; i++){System.out.print(" ");} for(int i = 0; i < indentation; i++){System.out.print(" ");}
// System.out.println("</" + node.getClass().getSimpleName() + ">"); System.out.println("</" + node.getClass().getSimpleName() + ">");
} }
/*
*/
// EXP -> op2 EXP EXP public C3aOperand visit(SaInstTantQue node)throws Exception
{
C3aOperand startloop= c3a.newAutoLabel();
C3aOperand endloop=c3a.newAutoLabel();
c3a.addLabelToNextInst((C3aLabel) startloop);
C3aOperand test=c3a.newTemp();
C3aOperand value=node.getTest().accept(this);
c3a.ajouteInst(new C3aInstAffect(value,test,""));
c3a.ajouteInst(new C3aInstJumpIfEqual(test, c3a.False,endloop,""));
if (node.getFaire()!=null)
{
node.getFaire().accept(this);
}
c3a.ajouteInst(new C3aInstJump(startloop,""));
c3a.addLabelToNextInst((C3aLabel) endloop);
return null;
}
@Override
public C3aOperand visit(SaExpAdd node) throws Exception public C3aOperand visit(SaExpAdd node) throws Exception
{ {
defaultIn(node); defaultIn(node);
C3aOperand op1 = node.getOp1().accept(this); C3aOperand op1 = node.getOp1().accept(this);
C3aOperand op2 = node.getOp2().accept(this); C3aOperand op2 = node.getOp2().accept(this);
C3aOperand result = c3a.newTemp(); C3aOperand result = c3a.newTemp();
c3a.ajouteInst(new C3aInstAdd(op1, op2, result, "")); c3a.ajouteInst(new C3aInstAdd(op1, op2, result, ""));
defaultOut(node); defaultOut(node);
return result; return result;
} }
@Override
public C3aOperand visit(SaExpDiv node) throws Exception
{
defaultIn(node);
C3aOperand op1 = node.getOp1().accept(this);
C3aOperand op2 = node.getOp2().accept(this);
C3aOperand result = c3a.newTemp();
c3a.ajouteInst(new C3aInstDiv(op1, op2, result, ""));
defaultOut(node);
return result;
}
@Override
public C3aOperand visit(SaExpSub node) throws Exception
{
defaultIn(node);
C3aOperand op1 = node.getOp1().accept(this);
C3aOperand op2 = node.getOp2().accept(this);
C3aOperand result = c3a.newTemp();
c3a.ajouteInst(new C3aInstSub(op1, op2, result, ""));
defaultOut(node);
return result;
}
@Override
public C3aOperand visit(SaExpMult node) throws Exception
{
defaultIn(node);
C3aOperand op1 = node.getOp1().accept(this);
C3aOperand op2 = node.getOp2().accept(this);
C3aOperand result = c3a.newTemp();
c3a.ajouteInst(new C3aInstMult(op1, op2, result, ""));
defaultOut(node);
return result;
}
public C3aOperand visit(SaExpInt node)
{
return new C3aConstant(node.getVal());
}
public C3aOperand visit(SaExpAppel node)throws Exception
{
return node.getVal().accept(this);
}
public C3aOperand visit(SaAppel node) throws Exception
{
if (node.getArguments()!=null)
{
SaLExp arguments=node.getArguments();
while (arguments!=null)
{
SaExp arg=arguments.getTete();
c3a.ajouteInst(new C3aInstParam(arg.accept(this),""));
arguments=arguments.getQueue();
}
}
C3aOperand result = c3a.newTemp();
c3a.ajouteInst(new C3aInstCall(new C3aFunction(node.tsItem),result,""));
return result;
}
public C3aOperand visit(SaDecFonc node)throws Exception
{
c3a.ajouteInst(new C3aInstFBegin(node.tsItem,""));
if (node.getCorps()!=null)
{
node.getCorps().accept(this);
}
c3a.ajouteInst(new C3aInstFEnd(""));
return null;
}
public C3aOperand visit(SaInstAffect node) throws Exception
{
System.out.println("here");
C3aOperand var=node.getLhs().accept(this);
C3aOperand value=node.getRhs().accept(this);
c3a.ajouteInst(new C3aInstAffect(value,var,""));
return null;
}
public C3aOperand visit(SaInstRetour node)throws Exception
{
C3aOperand returnval=node.getVal().accept(this);
c3a.ajouteInst(new C3aInstReturn(returnval,""));
return returnval;
}
public C3aOperand visit(SaExpLire node)
{
C3aOperand temp=c3a.newTemp();
c3a.ajouteInst(new C3aInstRead(temp, ""));
return temp;
}
public C3aOperand visit(SaVarSimple node)
{
if(node.tsItem==null)
System.out.println("null");
return new C3aVar(node.tsItem,null);
}
public C3aOperand visit(SaVarIndicee node)throws Exception
{
C3aOperand index=node.getIndice().accept(this);
return new C3aVar(node.getTsItem(),index);
}
public C3aOperand visit(SaInstEcriture node)throws Exception
{
C3aOperand op=node.getArg().accept(this);
c3a.ajouteInst(new C3aInstWrite(op,""));
return null;
}
public C3aOperand visit(SaExpVar node) throws Exception
{
return node.getVar().accept(this);
}
public C3aOperand visit(SaExpVrai node)
{
return c3a.True;
}
public C3aOperand visit(SaExpFaux node)
{
return c3a.False;
}
public C3aOperand visit(SaExpInf node)throws Exception
{
C3aOperand value=c3a.newTemp();
C3aOperand label=c3a.newAutoLabel();
c3a.ajouteInst(new C3aInstAffect( c3a.True,value, ""));
c3a.ajouteInst(new C3aInstJumpIfLess(node.getOp1().accept(this),node.getOp2().accept(this),label,""));
c3a.ajouteInst(new C3aInstAffect(c3a.False,value, ""));
c3a.addLabelToNextInst((C3aLabel) label);
return value;
}
public C3aOperand visit(SaExpAnd node)throws Exception
{
C3aOperand value=c3a.newTemp();
C3aOperand label1=c3a.newAutoLabel();
C3aOperand label2=c3a.newAutoLabel();
c3a.ajouteInst(new C3aInstJumpIfEqual(node.getOp1().accept(this), c3a.False,label1 ,""));
c3a.ajouteInst(new C3aInstJumpIfEqual(node.getOp2().accept(this), c3a.False,label1 ,""));
c3a.ajouteInst(new C3aInstAffect( c3a.True,value,""));
c3a.ajouteInst(new C3aInstJump(label2,""));
c3a.addLabelToNextInst((C3aLabel) label1);
c3a.ajouteInst(new C3aInstAffect( c3a.False,value,""));
c3a.addLabelToNextInst((C3aLabel) label2);
return value;
}
public C3aOperand visit(SaExpOr node)throws Exception
{
C3aOperand value=c3a.newTemp();
C3aOperand label=c3a.newAutoLabel();
c3a.ajouteInst(new C3aInstAffect( c3a.True,value,""));
c3a.ajouteInst(new C3aInstJumpIfEqual(node.getOp1().accept(this), c3a.True,label ,""));
c3a.ajouteInst(new C3aInstJumpIfEqual(node.getOp2().accept(this), c3a.True,label ,""));
c3a.ajouteInst(new C3aInstAffect( c3a.False,value,""));
c3a.addLabelToNextInst((C3aLabel) label);
return value;
}
public C3aOperand visit(SaExpEqual node)throws Exception
{
C3aOperand value=c3a.newTemp();
C3aOperand label=c3a.newAutoLabel();
c3a.ajouteInst(new C3aInstAffect( c3a.True,value,""));
c3a.ajouteInst(new C3aInstJumpIfEqual(node.getOp1().accept(this), node.getOp2().accept(this),label ,""));
c3a.ajouteInst(new C3aInstAffect( c3a.False,value,""));
c3a.addLabelToNextInst((C3aLabel) label);
return value;
}
public C3aOperand visit(SaExpNot node)throws Exception
{
C3aOperand value=c3a.newTemp();
C3aOperand label1=c3a.newAutoLabel();
C3aOperand label2=c3a.newAutoLabel();
c3a.ajouteInst(new C3aInstAffect( node.getOp1().accept(this),value,""));
c3a.ajouteInst(new C3aInstJumpIfEqual(value, c3a.False,label1 ,""));
c3a.ajouteInst(new C3aInstAffect(c3a.False, value,""));
c3a.ajouteInst(new C3aInstJump(label2,""));
c3a.addLabelToNextInst((C3aLabel) label1);
c3a.ajouteInst(new C3aInstAffect(c3a.True, value,""));
c3a.addLabelToNextInst((C3aLabel) label2);
return value;
}
public C3aOperand visit(SaInstSi node)throws Exception
{
C3aOperand test=c3a.newTemp();
C3aOperand value=node.getTest().accept(this);
c3a.ajouteInst(new C3aInstAffect(value,test,""));
C3aOperand alors=c3a.newAutoLabel();
C3aOperand sinon= c3a.newAutoLabel();
C3aOperand endsi= c3a.newAutoLabel();
c3a.ajouteInst(new C3aInstJumpIfEqual(test, c3a.True,alors,""));
if (node.getSinon()!=null)
c3a.ajouteInst(new C3aInstJumpIfEqual(test, c3a.False,sinon,""));
else
c3a.ajouteInst(new C3aInstJumpIfEqual(test, c3a.False,endsi,""));
if (node.getAlors()!=null)
{
c3a.addLabelToNextInst((C3aLabel) alors);
node.getAlors().accept(this);
}
c3a.ajouteInst(new C3aInstJump(endsi,""));
if (node.getSinon()!=null)
{
c3a.addLabelToNextInst((C3aLabel) sinon);
node.getSinon().accept(this);
}
c3a.addLabelToNextInst((C3aLabel) endsi);
return null;
}
} }
package ts; package ts;
import sa.*; import sa.*;
import util.Error; import util.Error;
import util.Type;
public class Sa2ts extends SaDepthFirstVisitor <Void> { public class Sa2ts extends SaDepthFirstVisitor <Void> {
enum Context { enum Context {
...@@ -23,89 +24,125 @@ public class Sa2ts extends SaDepthFirstVisitor <Void> { ...@@ -23,89 +24,125 @@ public class Sa2ts extends SaDepthFirstVisitor <Void> {
context = Context.GLOBAL; context = Context.GLOBAL;
} }
public Void visit(SaDecVar node) throws Exception{ public Void visit(SaDecFonc node)throws Exception
{
defaultIn(node); defaultIn(node);
TsItemVar ts = null; if (tableGlobale.getFct(node.getNom())==null)
try { {
if (context == Context.PARAM) { tableLocaleCourante=new Ts();
ts = tableLocaleCourante.addParam(node.getNom(), node.getType()); int nbrArgs;
} else if (context == Context.LOCAL) { if (node.getParametres()!=null)
if (tableLocaleCourante.getVar(node.getNom()) != null) {
throw new ErrorException(Error.TS, "Variable locale"); context=Context.PARAM;
ts = tableLocaleCourante.addVar(node.getNom(), node.getType()); node.getParametres().accept(this);
} else { nbrArgs=node.getParametres().length();
if (tableGlobale.getVar(node.getNom()) != null)
throw new ErrorException(Error.TS, "Variable globale");
ts = tableLocaleCourante.addVar(node.getNom(),node.getType());
} }
}catch(ErrorException e){ else nbrArgs=0;
System.err.print("Erreur dans la declaration de variable"); if(node.getVariable()!=null)
System.err.println(e.getMessage()); {
System.exit(e.getCode()); context=Context.LOCAL;
node.getVariable().accept(this);
} }
node.setTsItem(ts); String nom= node.getNom();
defaultOut(node); Type type=node.getTypeRetour();
System.out.println(tableGlobale.variables); node.tsItem=tableGlobale.addFct(nom,type,nbrArgs,tableLocaleCourante,node);
return null; context=Context.GLOBAL;
if (node.getCorps()!=null)
{
context=Context.LOCAL;
node.getCorps().accept(this);
} }
public Void visit(SaDecTab node) { }else {
defaultIn(node); throw new ErrorException(Error.TS, "La fonction a été déja definie");
if (tableGlobale.getVar(node.getNom()) != null) }
node.setTsItem(this.tableGlobale.addTab(node.getNom(),node.getType(),node.getTaille()));
else
throw new RuntimeException("Erreur");
defaultOut(node); defaultOut(node);
return null; return null;
} }
public Void visit(SaDecVar node)throws ErrorException
public Void visit(SaDecFonc node) throws Exception { {
defaultIn(node); defaultIn(node);
tableLocaleCourante = new Ts(); if (context==Context.PARAM)
int nbArgs = 0; {
this.context = Context.GLOBAL; if(tableLocaleCourante.getVar(node.getNom())==null)
if(tableGlobale.getFct(node.getNom()) == null){ {
tableLocaleCourante.addFct(node.getNom(),node.getTypeRetour(),nbArgs,tableLocaleCourante, node); node.setTsItem(tableLocaleCourante.addParam(node.getNom(),node.getType()));
}else
{
throw new ErrorException(Error.TS,"param déja declarée");
} }
else{
nbArgs = node.getParametres().length();
tableLocaleCourante.addFct(node.getNom(),node.getTypeRetour(),nbArgs,tableLocaleCourante, node);
} }
this.context = Context.PARAM; if (context==Context.LOCAL)
if(node.getParametres() == null){ {
node.getParametres().accept(this); if(tableLocaleCourante.getVar(node.getNom())==null)
{
node.setTsItem(tableLocaleCourante.addVar(node.getNom(),node.getType()));
}else
{
throw new ErrorException(Error.TS,"varibale déja declarée");
} }
this.context = Context.LOCAL;
if(node.getVariable() == null){
node.getParametres().accept(this);
} }
if(node.getCorps() == null){ if (context==Context.GLOBAL)
node.getCorps().accept(this); {
if(tableGlobale.getVar(node.getNom())==null) {
node.setTsItem(tableGlobale.addVar(node.getNom(), node.getType()));
}
else
{
throw new ErrorException(Error.TS,"varibale déja declarée");
}
} }
defaultOut(node); defaultOut(node);
return null; return null;
} }
public Void visit(SaDecTab node)throws ErrorException
{
defaultIn(node);
if (context==Context.GLOBAL)
{
if(tableGlobale.getVar(node.getNom())==null)
{
node.setTsItem(tableGlobale.addTab(node.getNom(),node.getType(),node.getTaille()));
}else
throw new ErrorException(Error.TS,"variable deja declarée");
}
if (context==Context.LOCAL)
{
if(tableLocaleCourante.getVar(node.getNom())==null)
{
node.setTsItem(tableLocaleCourante.addTab(node.getNom(),node.getType(),node.getTaille()));
}else
throw new ErrorException(Error.TS,"variable deja declarée");
}
defaultOut(node);
public Void visit(SaVarSimple node) throws Exception { return null;
defaultIn(node);
if (tableLocaleCourante.getVar(node.getNom()) != null){
if (tableLocaleCourante.getVar(node.getNom()).getTaille() != 1) throw new ErrorException(Error.TS,"Var not find");
node.tsItem = (TsItemVarSimple) tableLocaleCourante.getVar(node.getNom());
} }
else if (tableGlobale.getVar(node.getNom()) != null){ public Void visit(SaVarSimple node) throws ErrorException
if (tableGlobale.getVar(node.getNom()).getTaille() != 1) throw new ErrorException(Error.TS,"Var not find"); {
defaultIn(node);
if (tableGlobale.getVar(node.getNom())!=null)
{
node.tsItem=(TsItemVarSimple) tableGlobale.getVar(node.getNom()); node.tsItem=(TsItemVarSimple) tableGlobale.getVar(node.getNom());
} }
else { else if (tableLocaleCourante.getVar(node.getNom())!=null)
throw new ErrorException(Error.TS,"Var not find"); {
node.tsItem=(TsItemVarSimple) tableLocaleCourante.getVar(node.getNom());
}
else
{
throw new ErrorException(Error.TS,"variable jamais declaré");
} }
defaultOut(node); defaultOut(node);
return super.visit(node); return null;
} }
public Void visit(SaVarIndicee node)throws Exception
public Void visit(SaVarIndicee node) throws Exception { {
defaultIn(node); defaultIn(node);
if (tableGlobale.getVar(node.getNom())!=null) if (tableGlobale.getVar(node.getNom())!=null)
{ {
...@@ -125,35 +162,29 @@ public class Sa2ts extends SaDepthFirstVisitor <Void> { ...@@ -125,35 +162,29 @@ public class Sa2ts extends SaDepthFirstVisitor <Void> {
return null; return null;
} }
public Void visit(SaAppel node) throws Exception { public Void visit(SaAppel node) throws Exception
{
defaultIn(node); defaultIn(node);
if(node.getArguments().length() == tableGlobale.getFct(node.getNom()).getNbArgs()){ if (tableGlobale.getFct(node.getNom())==null )
node.getArguments().accept(this); {
node.tsItem = tableGlobale.getFct(node.getNom()); throw new ErrorException(Error.TS,"fonction non declaré ");
}
else if(node.getArguments() == null && tableGlobale.getFct(node.getNom()).getNbArgs() == 0) {
node.tsItem = tableGlobale.getFct(node.getNom());
} }
else { if (node.getArguments()!=null)
throw new ErrorException(Error.TS,""); {
node.getArguments().accept(this);
if (node.getArguments().length()!=tableGlobale.getFct(node.getNom()).getNbArgs())
throw new ErrorException(Error.TS," nombre de parametre n'est pas correcte 1");
}else
{
if (tableGlobale.getFct(node.getNom()).getNbArgs()>0)
throw new ErrorException(Error.TS," nombre de parametre n'est pas correcte 2");
} }
context = Context.LOCAL;
node.tsItem=tableGlobale.getFct(node.getNom()); node.tsItem=tableGlobale.getFct(node.getNom());
context = Context.GLOBAL;
defaultOut(node); defaultOut(node);
return null; return null;
} }
public void defaultIn(SaNode node)
{
System.out.println("<" + node.getClass().getSimpleName() + ">");
}
public void defaultOut(SaNode node)
{
System.out.println("</" + node.getClass().getSimpleName() + ">");
}
} }
\ No newline at end of file
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment