Skip to content
Snippets Groups Projects
Commit d4870a47 authored by Alexis Nasr's avatar Alexis Nasr
Browse files

initialisation dépot

parent 2bb59489
Branches
No related tags found
No related merge requests found
Showing
with 712 additions and 0 deletions
*\.class
*/*\.class
*/*/*\.class
*~
\ No newline at end of file
import lParser.parser.*;
import lParser.lexer.*;
import lParser.node.*;
import java.io.*;
import sc.*;
import sa.*;
import ts.*;
import c3a.*;
import nasm.*;
import fg.*;
import ig.*;
import util.Error;
public class Compiler
{
private static String baseName = null;
private static String inputFileName = null;
private static int verboseLevel = 0;
private static Start scRoot = null;
private static SaProg saRoot = null;
private static Ts tableGlobale = null;
private static C3a c3a = null;
private static Nasm nasm = null;
private static Fg flowGraph = null;
private static FgSolution flowGraphSolution = null;
private static Ig interferenceGraph = null;
public static void main(String[] args) {
processCommandLine(args);
System.out.println("[BUILD SC] ");
// buildSc();
// System.out.println("[BUILD SA] ");
// buildSa();
// System.out.println("[BUILD TS] ");
// buildTs();
// System.out.println("[TYPE CHECKING]");
// typeCheck();
// System.out.println("[BUILD C3A] ");
// buildC3a();
// System.out.println("[BUILD PRE NASM] ");
// buildPreNasm();
// System.out.println("[BUILD FLOW GRAPH] ");
// buildFg();
// System.out.println("[SOLVE FLOW GRAPH]");
// solveFg();
// System.out.println("[BUILD INTERFERENCE GRAPH] ");
// buildIg();
// System.out.println("[ALLOCATE REGISTERS]");
// interferenceGraph.allocateRegisters();
// System.out.println("[PRINT NASM]");
// nasm.afficheNasm(baseName);
// System.exit(Error.NOERROR.code());
}
private static void processCommandLine(String[] args) {
for (int i = 0; i < args.length; i++) {
if (args[i].equals("-v")) {
verboseLevel = Integer.parseInt(args[++i]);
} else {
inputFileName = args[i];
baseName = removeSuffix(inputFileName, ".l");
}
}
if (inputFileName == null) {
System.out.println("java Compiler input_file -v verbose_level");
System.exit(1);
}
}
private static void buildSc() {
PushbackReader fileReader = null;
Parser parser = null;
try {
fileReader = new PushbackReader(new FileReader(inputFileName));
} catch (IOException e) {
System.err.println("cannot open input file");
e.printStackTrace();
}
try {
parser = new Parser(new Lexer(fileReader));
scRoot = parser.parse();
} catch (ParserException e) {
System.err.println("syntax error");
e.printStackTrace();
System.exit(Error.SYNT.code());
} catch (LexerException e) {
System.err.println("lexical error");
e.printStackTrace();
System.exit(Error.LEX.code());
} catch (IOException e) {
e.printStackTrace();
}
if (verboseLevel > 1) {
System.out.println("[PRINT SC]");
scRoot.apply(new Sc2Xml(baseName));
}
}
private static void buildSa() {
try {
Sc2sa sc2sa = new Sc2sa();
scRoot.apply(sc2sa);
saRoot = sc2sa.getRoot();
} catch (Exception e) {
System.err.println("Cannot build abstract syntax tree");
e.printStackTrace();
}
PrintStream out = System.out;
if (verboseLevel > 1) {
System.out.println("[PRINT SA]");
try {
out = new PrintStream(baseName + ".sa");
}
catch (IOException e) {
System.err.println("Error: " + e.getMessage());
}
out.println(saRoot);
}
}
private static void buildTs() {
try {
Sa2ts sa2ts = new Sa2ts();
saRoot.accept(sa2ts);
tableGlobale = sa2ts.getTableGlobale();
SaCheckLinkage saCheckLinkage = new SaCheckLinkage();
saCheckLinkage.visit(saRoot);
if (tableGlobale.getFct("main") == null)
throw new ErrorException(Error.TS, "la fonction main n'est pas définie");
} catch (ErrorException e) {
System.err.print("ERREUR TABLE DES SYMBOLES : ");
System.err.println(e.getMessage());
System.exit(e.getCode());
} catch (Exception e) {
e.printStackTrace();
}
if (verboseLevel > 1) {
System.out.println("[PRINT TS]");
tableGlobale.afficheTout(baseName);
}
}
private static void typeCheck() {
new SaTypeCheck(saRoot);
}
private static void buildC3a() {
try{
Sa2c3a sa2c3a = new Sa2c3a(saRoot, tableGlobale);
saRoot.accept(sa2c3a);
c3a = sa2c3a.getC3a();
}
catch(Exception e){
System.err.println("Cannot build c3a");
e.printStackTrace();
}
if (verboseLevel > 1) {
System.out.println("[PRINT C3A] ");
c3a.affiche(baseName);
}
}
private static void buildPreNasm() {
C3a2nasm c3a2nasm = new C3a2nasm(c3a, tableGlobale);
c3a.accept(c3a2nasm);
nasm = c3a2nasm.getNasm();
if (verboseLevel > 1) {
System.out.println("[PRINT PRE NASM] ");
nasm.affichePreNasm(baseName);
}
}
private static void buildFg() {
flowGraph = new Fg(nasm);
if (verboseLevel > 1) {
System.out.println("[PRINT FLOW GRAPH] ");
flowGraph.affiche(baseName);
}
}
private static void solveFg() {
flowGraphSolution = new FgSolution(nasm, flowGraph);
if (verboseLevel > 1) {
System.out.println("[PRINT FLOW GRAPH SOLUTION] ");
flowGraphSolution.affiche(baseName);
}
}
private static void buildIg() {
interferenceGraph = new Ig(flowGraphSolution);
if (verboseLevel > 1) {
System.out.println("[PRINT INTERFERENCE GRAPH] ");
interferenceGraph.affiche(baseName);
}
}
/*catch (Exception e)
{
e.printStackTrace();
System.out.println(e.getMessage());
System.exit(1);
}
}*/
public static String removeSuffix(final String s, final String suffix)
{
if (s != null && suffix != null && s.endsWith(suffix)){
return s.substring(0, s.length() - suffix.length());
}
return s;
}
}
all: Compiler
Compiler: Compiler.java lParser
javac Compiler.java
lParser: l.cfg
java -jar ../sablecc/sablecc.jar l.cfg
clean:
-rm *.class
-rm sc/*.class
-rm sa/*.class
-rm c3a/*.class
-rm ts/*.class
-rm nasm/*.class
-rm fg/*.class
-rm ig/*.class
-rm util/intset/*.class
-rm util/graph/*.class
-rm -r lParser
-rm -r saParser
-rm -r c3aParser
-rm -r nasmParser
package c3a;
import java.util.*;
import java.io.*;
import ts.*;
import sa.*;
public class C3a{
public List<C3aInst> listeInst;
// compteur des temporaires, pour générer des noms uniques
private int tempCounter;
// étiquette de la prochaine instruction, on retarde l'ajout de l'étiquette
private C3aLabel nextLabel;
private int labelCounter;
public C3aConstant True;
public C3aConstant False;
public C3a(){
this.listeInst = new ArrayList<C3aInst>();
this.labelCounter = 0;
this.tempCounter = 0;
this.nextLabel = null;
this.True = new C3aConstant(1); // constantes utilisées partout
this.False = new C3aConstant(0); // constantes utilisées partout
}
public int getTempCounter(){return this.tempCounter;}
public void setTempCounter(int value){this.tempCounter = value;}
public int getLabelCounter(){return this.labelCounter;}
public void setLableCounter(int value){this.labelCounter = value;}
public void ajouteInst(C3aInst inst){
if(this.nextLabel != null){
inst.setLabel(this.nextLabel);
this.nextLabel = null;
}
this.listeInst.add(inst);
}
public C3aLabel newAutoLabel(){
return new C3aLabel(this.labelCounter++);
}
public C3aTemp newTemp(){
return new C3aTemp(this.tempCounter++);
}
public void addLabelToNextInst(C3aLabel label){
if(this.nextLabel != null){
label.affect(this.nextLabel);
System.err.println("WARNING : Étiquette précédente ignorée " + this.nextLabel.getNumber());
}
this.nextLabel = label;
// if(etiquette->oper_type == O_ETIQUETTE){
// etiquette->u.oper_etiquette.ligne = c3a->next;
//}
//if(etiquette->oper_type == O_FCT){
// etiquette->u.oper_fct->adresse = c3a->next;
//}
}
public void affiche(String baseFileName){
String fileName;
PrintStream out = System.out;
if (baseFileName != null){
try {
baseFileName = baseFileName;
fileName = baseFileName + ".c3a";
out = new PrintStream(fileName);
}
catch (IOException e) {
System.err.println("Error: " + e.getMessage());
}
}
Iterator<C3aInst> iter = this.listeInst.iterator();
while(iter.hasNext()){
out.println(iter.next());
}
}
public <T> T accept(C3aVisitor <T> visitor) {
return visitor.visit(this);
}
}
package c3a;
public class C3aBooleanConstant extends C3aOperand{
public boolean val;
public C3aBooleanConstant(boolean val){
this.val = val;
}
public String toString(){return String.valueOf(this.val);}
public <T> T accept(C3aVisitor <T> visitor) {
return visitor.visit(this);
}
}
package c3a;
public class C3aConstant extends C3aOperand{
public int val;
public C3aConstant(int val){
this.val = val;
}
public String toString(){return Integer.toString(this.val);}
public <T> T accept(C3aVisitor <T> visitor) {
return visitor.visit(this);
}
}
package c3a;
import ts.*;
public class C3aFunction extends C3aOperand{
public TsItemFct val;
public C3aFunction(TsItemFct val){
this.val = val;
}
public TsItemFct getValue(){return val;}
public String toString(){
return this.val.getIdentif();
}
public <T> T accept(C3aVisitor <T> visitor) {
return visitor.visit(this);
}
}
package c3a;
public class C3aInst{
public C3aLabel label ; // étiquette optionnelle, si la ligne est cible d'un saut
public String comment; // commentaire explicatif - utile pour le débogage
public void setLabel(C3aLabel label){
this.label = label;
}
public <T> T accept(C3aVisitor <T> visitor) {
return visitor.visit(this);
}
}
package c3a;
public class C3aInstAdd extends C3aInst{
public C3aOperand op1; // opérande 1 (ou null)
public C3aOperand op2; // opérande 2 (ou null)
public C3aOperand result; // résultat ou destination du saut (ou NULL)
public C3aInstAdd(C3aOperand op1, C3aOperand op2, C3aOperand result, String comment){
this.op1 = op1;
this.op2 = op2;
this.result = result;
this.comment = comment;
}
public String toString(){
if(this.label != null)
return this.label + "\t" + this.result + " = " + this.op1 + " + " + this.op2 + this.comment;
else
return "\t" + this.result + " = " + this.op1 + " + " + this.op2 + this.comment;
}
public <T> T accept(C3aVisitor <T> visitor) {
return visitor.visit(this);
}
}
package c3a;
public class C3aInstAffect extends C3aInst{
public C3aOperand op1; // opérande 1 (ou null)
public C3aOperand result; // résultat ou destination du saut (ou NULL)
public C3aInstAffect(C3aOperand op1, C3aOperand result, String comment){
this.op1 = op1;
this.result = result;
this.comment = comment;
}
public String toString(){
String s = "";
if(this.label != null) s = s + this.label;
s = s + "\t";
s = s + this.result + " = " + this.op1 + this.comment;
return s;
}
public <T> T accept(C3aVisitor <T> visitor) {
return visitor.visit(this);
}
}
package c3a;
public class C3aInstCall extends C3aInst{
public C3aFunction op1; // opérande 1 (ou null)
public C3aOperand result; // résultat ou destination du saut (ou NULL)
public C3aInstCall(C3aFunction op1, C3aOperand result, String comment){
this.op1 = op1;
this.result = result;
this.comment = comment;
}
public String toString(){
String s = "";
if(this.label != null)
s = s + this.label;
s = s + "\t";
if(this.result != null)
s = s + this.result + " =";
s = s + " call " + this.op1 + this.comment;
return s;
}
public <T> T accept(C3aVisitor <T> visitor) {
return visitor.visit(this);
}
}
package c3a;
public class C3aInstDiv extends C3aInst{
public C3aOperand op1; // opérande 1 (ou null)
public C3aOperand op2; // opérande 2 (ou null)
public C3aOperand result; // résultat ou destination du saut (ou NULL)
public C3aInstDiv(C3aOperand op1, C3aOperand op2, C3aOperand result, String comment){
this.op1 = op1;
this.op2 = op2;
this.result = result;
this.comment = comment;
}
public String toString(){
if(this.label != null)
return this.label + "\t" + this.result + " = " + this.op1 + " / " + this.op2 + this.comment;
else
return "\t" + this.result + " = " + this.op1 + " / " + this.op2 + this.comment;
}
public <T> T accept(C3aVisitor <T> visitor) {
return visitor.visit(this);
}
}
package c3a;
import ts.*;
public class C3aInstFBegin extends C3aInst{
public TsItemFct val;
public C3aInstFBegin(TsItemFct val, String comment){
this.val = val;
this.comment = comment;
}
public String toString(){
// String s = "";
// if(this.label != null)
// s = s + this.label;
return this.val.getIdentif() + "\tfbegin\t#" + this.comment;
// return s;
}
public <T> T accept(C3aVisitor <T> visitor) {
return visitor.visit(this);
}
}
package c3a;
public class C3aInstFEnd extends C3aInst{
public C3aInstFEnd(String comment){
this.comment = comment;
}
public String toString(){
String s = "";
if(this.label != null)
s = s + this.label;
s = s + "\t";
s = s + "fend";
return s;
}
public <T> T accept(C3aVisitor <T> visitor) {
return visitor.visit(this);
}
}
package c3a;
public class C3aInstJump extends C3aInst{
public C3aOperand result; // résultat ou destination du saut (ou NULL)
public C3aInstJump(C3aOperand result, String comment){
this.result = result;
this.comment = comment;
}
public String toString(){
String s = "";
if(this.label != null)
s = s + this.label;
s = s + "\t";
s = s + "goto " + this.result;
return s;
}
public <T> T accept(C3aVisitor <T> visitor) {
return visitor.visit(this);
}
}
package c3a;
public class C3aInstJumpIfEqual extends C3aInst{
public C3aOperand op1; // opérande 1 (ou null)
public C3aOperand op2; // opérande 2 (ou null)
public C3aOperand result; // résultat ou destination du saut (ou NULL)
public C3aInstJumpIfEqual(C3aOperand op1, C3aOperand op2, C3aOperand result, String comment){
this.op1 = op1;
this.op2 = op2;
this.result = result;
this.comment = comment;
}
public String toString(){
String s = "";
if(this.label != null)
s = s + this.label;
s = s + "\tif " + this.op1 + " == " + this.op2 + " goto " + this.result;
return s;
}
public <T> T accept(C3aVisitor <T> visitor) {
return visitor.visit(this);
}
}
package c3a;
public class C3aInstJumpIfLess extends C3aInst{
public C3aOperand op1; // opérande 1 (ou null)
public C3aOperand op2; // opérande 2 (ou null)
public C3aOperand result; // résultat ou destination du saut (ou NULL)
public C3aInstJumpIfLess(C3aOperand op1, C3aOperand op2, C3aOperand result, String comment){
this.op1 = op1;
this.op2 = op2;
this.result = result;
this.comment = comment;
}
public String toString(){
String s = "";
if(this.label != null)
s = s + this.label;
s = s + "\t";
s = s + "if " + this.op1 + " < " + this.op2 + " goto " + this.result;
return s;
}
public <T> T accept(C3aVisitor <T> visitor) {
return visitor.visit(this);
}
}
package c3a;
public class C3aInstJumpIfNotEqual extends C3aInst{
public C3aOperand op1; // opérande 1 (ou null)
public C3aOperand op2; // opérande 2 (ou null)
public C3aOperand result; // résultat ou destination du saut (ou NULL)
public C3aInstJumpIfNotEqual(C3aOperand op1, C3aOperand op2, C3aOperand result, String comment){
this.op1 = op1;
this.op2 = op2;
this.result = result;
this.comment = comment;
}
public String toString(){
String s = "";
if(this.label != null)
s = s + this.label;
s = s + "\tif " + this.op1 + " != " + this.op2 + " goto " + this.result;
return s;
}
public <T> T accept(C3aVisitor <T> visitor) {
return visitor.visit(this);
}
}
package c3a;
public class C3aInstMult extends C3aInst{
public C3aOperand op1; // opérande 1 (ou null)
public C3aOperand op2; // opérande 2 (ou null)
public C3aOperand result; // résultat ou destination du saut (ou NULL)
public C3aInstMult(C3aOperand op1, C3aOperand op2, C3aOperand result, String comment){
this.op1 = op1;
this.op2 = op2;
this.result = result;
this.comment = comment;
}
public String toString(){
if(this.label != null)
return this.label + "\t" + this.result + " = " + this.op1 + " * " + this.op2 + this.comment;
else
return "\t" + this.result + " = " + this.op1 + " * " + this.op2 + this.comment;
}
public <T> T accept(C3aVisitor <T> visitor) {
return visitor.visit(this);
}
}
package c3a;
public class C3aInstParam extends C3aInst{
public C3aOperand op1; // opérande 1 (ou null)
public C3aInstParam(C3aOperand op1, String comment){
this.op1 = op1;
this.comment = comment;
}
public String toString(){
String s = "";
if(this.label != null)
s = s + this.label;
s = s + "\t";
s = s + "param " + this.op1;
return s;
}
public <T> T accept(C3aVisitor <T> visitor) {
return visitor.visit(this);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment