From d4870a47099b8e0801d2ad2f668466d67c85bfec Mon Sep 17 00:00:00 2001 From: Alexis Nasr <alexis.nasr@lif.univ-mrs.fr> Date: Wed, 29 Jan 2025 14:36:37 +0100 Subject: [PATCH] =?UTF-8?q?initialisation=20d=C3=A9pot?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/.gitignore | 4 + src/Compiler.java | 220 ++++++++++++++++ src/Makefile | 24 ++ src/c3a/C3a.java | 87 +++++++ src/c3a/C3aBooleanConstant.java | 15 ++ src/c3a/C3aConstant.java | 15 ++ src/c3a/C3aFunction.java | 20 ++ src/c3a/C3aInst.java | 16 ++ src/c3a/C3aInstAdd.java | 27 ++ src/c3a/C3aInstAffect.java | 27 ++ src/c3a/C3aInstCall.java | 28 +++ src/c3a/C3aInstDiv.java | 27 ++ src/c3a/C3aInstFBegin.java | 23 ++ src/c3a/C3aInstFEnd.java | 21 ++ src/c3a/C3aInstJump.java | 24 ++ src/c3a/C3aInstJumpIfEqual.java | 27 ++ src/c3a/C3aInstJumpIfLess.java | 28 +++ src/c3a/C3aInstJumpIfNotEqual.java | 27 ++ src/c3a/C3aInstMult.java | 27 ++ src/c3a/C3aInstParam.java | 25 ++ src/c3a/C3aInstRead.java | 25 ++ src/c3a/C3aInstReturn.java | 25 ++ src/c3a/C3aInstStop.java | 27 ++ src/c3a/C3aInstSub.java | 27 ++ src/c3a/C3aInstWrite.java | 26 ++ src/c3a/C3aLabel.java | 29 +++ src/c3a/C3aOp.java | 24 ++ src/c3a/C3aOperand.java | 7 + src/c3a/C3aTemp.java | 16 ++ src/c3a/C3aVar.java | 24 ++ src/c3a/C3aVisitor.java | 30 +++ src/c3a/Sa2c3a.java | 46 ++++ src/fg/Fg.java | 89 +++++++ src/fg/FgSolution.java | 49 ++++ src/ig/Ig.java | 61 +++++ src/l.cfg | 32 +++ src/nasm/C3a2nasm.java | 62 +++++ src/nasm/Nasm.java | 196 +++++++++++++++ src/nasm/NasmAdd.java | 25 ++ src/nasm/NasmAddress.java | 26 ++ src/nasm/NasmAnd.java | 24 ++ src/nasm/NasmCall.java | 19 ++ src/nasm/NasmCmp.java | 23 ++ src/nasm/NasmConstant.java | 28 +++ src/nasm/NasmDiv.java | 19 ++ src/nasm/NasmEmpty.java | 18 ++ src/nasm/NasmExp.java | 11 + src/nasm/NasmExpMinus.java | 34 +++ src/nasm/NasmExpPlus.java | 34 +++ src/nasm/NasmExpTimes.java | 34 +++ src/nasm/NasmInst.java | 36 +++ src/nasm/NasmInt.java | 18 ++ src/nasm/NasmJe.java | 19 ++ src/nasm/NasmJg.java | 19 ++ src/nasm/NasmJge.java | 19 ++ src/nasm/NasmJl.java | 19 ++ src/nasm/NasmJle.java | 25 ++ src/nasm/NasmJmp.java | 19 ++ src/nasm/NasmJne.java | 19 ++ src/nasm/NasmLabel.java | 28 +++ src/nasm/NasmMov.java | 22 ++ src/nasm/NasmMul.java | 26 ++ src/nasm/NasmNot.java | 21 ++ src/nasm/NasmOperand.java | 8 + src/nasm/NasmOr.java | 24 ++ src/nasm/NasmPop.java | 20 ++ src/nasm/NasmPseudoInst.java | 28 +++ src/nasm/NasmPush.java | 20 ++ src/nasm/NasmRegister.java | 57 +++++ src/nasm/NasmResb.java | 21 ++ src/nasm/NasmResd.java | 21 ++ src/nasm/NasmResq.java | 21 ++ src/nasm/NasmRest.java | 21 ++ src/nasm/NasmResw.java | 21 ++ src/nasm/NasmRet.java | 18 ++ src/nasm/NasmSize.java | 20 ++ src/nasm/NasmSub.java | 22 ++ src/nasm/NasmVisitor.java | 47 ++++ src/nasm/NasmXor.java | 24 ++ src/sa/ErrorException.java | 23 ++ src/sa/SaAppel.java | 33 +++ src/sa/SaCheckLinkage.java | 42 ++++ src/sa/SaDec.java | 5 + src/sa/SaDecFonc.java | 34 +++ src/sa/SaDecTab.java | 31 +++ src/sa/SaDecVar.java | 11 + src/sa/SaDecVarSimple.java | 29 +++ src/sa/SaDepthFirstVisitor.java | 389 +++++++++++++++++++++++++++++ src/sa/SaExp.java | 6 + src/sa/SaExpAdd.java | 26 ++ src/sa/SaExpAnd.java | 26 ++ src/sa/SaExpAppel.java | 25 ++ src/sa/SaExpDiv.java | 27 ++ src/sa/SaExpEqual.java | 27 ++ src/sa/SaExpFaux.java | 28 +++ src/sa/SaExpInf.java | 28 +++ src/sa/SaExpInt.java | 26 ++ src/sa/SaExpLire.java | 21 ++ src/sa/SaExpMult.java | 27 ++ src/sa/SaExpNot.java | 26 ++ src/sa/SaExpOr.java | 28 +++ src/sa/SaExpSub.java | 28 +++ src/sa/SaExpVar.java | 24 ++ src/sa/SaExpVrai.java | 27 ++ src/sa/SaInst.java | 5 + src/sa/SaInstAffect.java | 22 ++ src/sa/SaInstBloc.java | 19 ++ src/sa/SaInstEcriture.java | 20 ++ src/sa/SaInstRetour.java | 24 ++ src/sa/SaInstSi.java | 26 ++ src/sa/SaInstTantQue.java | 23 ++ src/sa/SaLDecFonc.java | 28 +++ src/sa/SaLDecVar.java | 27 ++ src/sa/SaLExp.java | 30 +++ src/sa/SaLInst.java | 23 ++ src/sa/SaNode.java | 6 + src/sa/SaProg.java | 21 ++ src/sa/SaTypeCheck.java | 42 ++++ src/sa/SaVar.java | 6 + src/sa/SaVarIndicee.java | 27 ++ src/sa/SaVarSimple.java | 24 ++ src/sa/SaVisitor.java | 38 +++ src/sa/Sc2sa.java | 28 +++ src/sc/Sc2Xml.java | 48 ++++ src/ts/Sa2ts.java | 35 +++ src/ts/Ts.java | 147 +++++++++++ src/ts/TsItemFct.java | 30 +++ src/ts/TsItemVar.java | 20 ++ src/ts/TsItemVarSimple.java | 22 ++ src/ts/TsItemVarTab.java | 19 ++ src/util/Error.java | 21 ++ src/util/Memory.java | 224 +++++++++++++++++ src/util/Type.java | 30 +++ src/util/graph/ColorGraph.java | 162 ++++++++++++ src/util/graph/Graph.java | 83 ++++++ src/util/graph/Node.java | 55 ++++ src/util/graph/NodeList.java | 10 + src/util/graph/TestColorGraph.java | 77 ++++++ src/util/intset/IntSet.java | 94 +++++++ src/util/memory/MemSeg.java | 79 ++++++ src/util/memory/MemStack.java | 79 ++++++ 141 files changed, 5156 insertions(+) create mode 100644 src/.gitignore create mode 100644 src/Compiler.java create mode 100644 src/Makefile create mode 100644 src/c3a/C3a.java create mode 100644 src/c3a/C3aBooleanConstant.java create mode 100644 src/c3a/C3aConstant.java create mode 100644 src/c3a/C3aFunction.java create mode 100644 src/c3a/C3aInst.java create mode 100644 src/c3a/C3aInstAdd.java create mode 100644 src/c3a/C3aInstAffect.java create mode 100644 src/c3a/C3aInstCall.java create mode 100644 src/c3a/C3aInstDiv.java create mode 100644 src/c3a/C3aInstFBegin.java create mode 100644 src/c3a/C3aInstFEnd.java create mode 100644 src/c3a/C3aInstJump.java create mode 100644 src/c3a/C3aInstJumpIfEqual.java create mode 100644 src/c3a/C3aInstJumpIfLess.java create mode 100644 src/c3a/C3aInstJumpIfNotEqual.java create mode 100644 src/c3a/C3aInstMult.java create mode 100644 src/c3a/C3aInstParam.java create mode 100644 src/c3a/C3aInstRead.java create mode 100644 src/c3a/C3aInstReturn.java create mode 100644 src/c3a/C3aInstStop.java create mode 100644 src/c3a/C3aInstSub.java create mode 100644 src/c3a/C3aInstWrite.java create mode 100644 src/c3a/C3aLabel.java create mode 100644 src/c3a/C3aOp.java create mode 100644 src/c3a/C3aOperand.java create mode 100644 src/c3a/C3aTemp.java create mode 100644 src/c3a/C3aVar.java create mode 100644 src/c3a/C3aVisitor.java create mode 100644 src/c3a/Sa2c3a.java create mode 100644 src/fg/Fg.java create mode 100644 src/fg/FgSolution.java create mode 100644 src/ig/Ig.java create mode 100644 src/l.cfg create mode 100644 src/nasm/C3a2nasm.java create mode 100644 src/nasm/Nasm.java create mode 100644 src/nasm/NasmAdd.java create mode 100644 src/nasm/NasmAddress.java create mode 100644 src/nasm/NasmAnd.java create mode 100644 src/nasm/NasmCall.java create mode 100644 src/nasm/NasmCmp.java create mode 100644 src/nasm/NasmConstant.java create mode 100644 src/nasm/NasmDiv.java create mode 100644 src/nasm/NasmEmpty.java create mode 100644 src/nasm/NasmExp.java create mode 100644 src/nasm/NasmExpMinus.java create mode 100644 src/nasm/NasmExpPlus.java create mode 100644 src/nasm/NasmExpTimes.java create mode 100644 src/nasm/NasmInst.java create mode 100644 src/nasm/NasmInt.java create mode 100644 src/nasm/NasmJe.java create mode 100644 src/nasm/NasmJg.java create mode 100644 src/nasm/NasmJge.java create mode 100644 src/nasm/NasmJl.java create mode 100644 src/nasm/NasmJle.java create mode 100644 src/nasm/NasmJmp.java create mode 100644 src/nasm/NasmJne.java create mode 100644 src/nasm/NasmLabel.java create mode 100644 src/nasm/NasmMov.java create mode 100644 src/nasm/NasmMul.java create mode 100644 src/nasm/NasmNot.java create mode 100644 src/nasm/NasmOperand.java create mode 100644 src/nasm/NasmOr.java create mode 100644 src/nasm/NasmPop.java create mode 100644 src/nasm/NasmPseudoInst.java create mode 100644 src/nasm/NasmPush.java create mode 100644 src/nasm/NasmRegister.java create mode 100644 src/nasm/NasmResb.java create mode 100644 src/nasm/NasmResd.java create mode 100644 src/nasm/NasmResq.java create mode 100644 src/nasm/NasmRest.java create mode 100644 src/nasm/NasmResw.java create mode 100644 src/nasm/NasmRet.java create mode 100644 src/nasm/NasmSize.java create mode 100644 src/nasm/NasmSub.java create mode 100644 src/nasm/NasmVisitor.java create mode 100644 src/nasm/NasmXor.java create mode 100644 src/sa/ErrorException.java create mode 100644 src/sa/SaAppel.java create mode 100644 src/sa/SaCheckLinkage.java create mode 100644 src/sa/SaDec.java create mode 100644 src/sa/SaDecFonc.java create mode 100644 src/sa/SaDecTab.java create mode 100644 src/sa/SaDecVar.java create mode 100644 src/sa/SaDecVarSimple.java create mode 100644 src/sa/SaDepthFirstVisitor.java create mode 100644 src/sa/SaExp.java create mode 100644 src/sa/SaExpAdd.java create mode 100644 src/sa/SaExpAnd.java create mode 100644 src/sa/SaExpAppel.java create mode 100644 src/sa/SaExpDiv.java create mode 100644 src/sa/SaExpEqual.java create mode 100644 src/sa/SaExpFaux.java create mode 100644 src/sa/SaExpInf.java create mode 100644 src/sa/SaExpInt.java create mode 100644 src/sa/SaExpLire.java create mode 100644 src/sa/SaExpMult.java create mode 100644 src/sa/SaExpNot.java create mode 100644 src/sa/SaExpOr.java create mode 100644 src/sa/SaExpSub.java create mode 100644 src/sa/SaExpVar.java create mode 100644 src/sa/SaExpVrai.java create mode 100644 src/sa/SaInst.java create mode 100644 src/sa/SaInstAffect.java create mode 100644 src/sa/SaInstBloc.java create mode 100644 src/sa/SaInstEcriture.java create mode 100644 src/sa/SaInstRetour.java create mode 100644 src/sa/SaInstSi.java create mode 100644 src/sa/SaInstTantQue.java create mode 100644 src/sa/SaLDecFonc.java create mode 100644 src/sa/SaLDecVar.java create mode 100644 src/sa/SaLExp.java create mode 100644 src/sa/SaLInst.java create mode 100644 src/sa/SaNode.java create mode 100644 src/sa/SaProg.java create mode 100644 src/sa/SaTypeCheck.java create mode 100644 src/sa/SaVar.java create mode 100644 src/sa/SaVarIndicee.java create mode 100644 src/sa/SaVarSimple.java create mode 100644 src/sa/SaVisitor.java create mode 100644 src/sa/Sc2sa.java create mode 100644 src/sc/Sc2Xml.java create mode 100644 src/ts/Sa2ts.java create mode 100644 src/ts/Ts.java create mode 100644 src/ts/TsItemFct.java create mode 100644 src/ts/TsItemVar.java create mode 100644 src/ts/TsItemVarSimple.java create mode 100644 src/ts/TsItemVarTab.java create mode 100644 src/util/Error.java create mode 100644 src/util/Memory.java create mode 100644 src/util/Type.java create mode 100644 src/util/graph/ColorGraph.java create mode 100644 src/util/graph/Graph.java create mode 100644 src/util/graph/Node.java create mode 100644 src/util/graph/NodeList.java create mode 100644 src/util/graph/TestColorGraph.java create mode 100644 src/util/intset/IntSet.java create mode 100644 src/util/memory/MemSeg.java create mode 100644 src/util/memory/MemStack.java diff --git a/src/.gitignore b/src/.gitignore new file mode 100644 index 0000000..871caa1 --- /dev/null +++ b/src/.gitignore @@ -0,0 +1,4 @@ +*\.class +*/*\.class +*/*/*\.class +*~ \ No newline at end of file diff --git a/src/Compiler.java b/src/Compiler.java new file mode 100644 index 0000000..be83251 --- /dev/null +++ b/src/Compiler.java @@ -0,0 +1,220 @@ +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; + } + +} diff --git a/src/Makefile b/src/Makefile new file mode 100644 index 0000000..cc9c577 --- /dev/null +++ b/src/Makefile @@ -0,0 +1,24 @@ +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 + diff --git a/src/c3a/C3a.java b/src/c3a/C3a.java new file mode 100644 index 0000000..9fe8ad5 --- /dev/null +++ b/src/c3a/C3a.java @@ -0,0 +1,87 @@ +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); + } + +} + diff --git a/src/c3a/C3aBooleanConstant.java b/src/c3a/C3aBooleanConstant.java new file mode 100644 index 0000000..419e5e9 --- /dev/null +++ b/src/c3a/C3aBooleanConstant.java @@ -0,0 +1,15 @@ +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); + } +} diff --git a/src/c3a/C3aConstant.java b/src/c3a/C3aConstant.java new file mode 100644 index 0000000..657cdd3 --- /dev/null +++ b/src/c3a/C3aConstant.java @@ -0,0 +1,15 @@ +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); + } +} diff --git a/src/c3a/C3aFunction.java b/src/c3a/C3aFunction.java new file mode 100644 index 0000000..5966b6a --- /dev/null +++ b/src/c3a/C3aFunction.java @@ -0,0 +1,20 @@ +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); + } +} diff --git a/src/c3a/C3aInst.java b/src/c3a/C3aInst.java new file mode 100644 index 0000000..9aafc68 --- /dev/null +++ b/src/c3a/C3aInst.java @@ -0,0 +1,16 @@ +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); + } + +} + diff --git a/src/c3a/C3aInstAdd.java b/src/c3a/C3aInstAdd.java new file mode 100644 index 0000000..49b9d92 --- /dev/null +++ b/src/c3a/C3aInstAdd.java @@ -0,0 +1,27 @@ +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); + } + +} + diff --git a/src/c3a/C3aInstAffect.java b/src/c3a/C3aInstAffect.java new file mode 100644 index 0000000..8596b7b --- /dev/null +++ b/src/c3a/C3aInstAffect.java @@ -0,0 +1,27 @@ +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); + } + + +} + diff --git a/src/c3a/C3aInstCall.java b/src/c3a/C3aInstCall.java new file mode 100644 index 0000000..dc5794f --- /dev/null +++ b/src/c3a/C3aInstCall.java @@ -0,0 +1,28 @@ +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); + } +} + diff --git a/src/c3a/C3aInstDiv.java b/src/c3a/C3aInstDiv.java new file mode 100644 index 0000000..77885d1 --- /dev/null +++ b/src/c3a/C3aInstDiv.java @@ -0,0 +1,27 @@ +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); + } + +} + diff --git a/src/c3a/C3aInstFBegin.java b/src/c3a/C3aInstFBegin.java new file mode 100644 index 0000000..c8a5f2c --- /dev/null +++ b/src/c3a/C3aInstFBegin.java @@ -0,0 +1,23 @@ +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); + } +} + diff --git a/src/c3a/C3aInstFEnd.java b/src/c3a/C3aInstFEnd.java new file mode 100644 index 0000000..8e0380f --- /dev/null +++ b/src/c3a/C3aInstFEnd.java @@ -0,0 +1,21 @@ +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); + } +} + diff --git a/src/c3a/C3aInstJump.java b/src/c3a/C3aInstJump.java new file mode 100644 index 0000000..1324e5e --- /dev/null +++ b/src/c3a/C3aInstJump.java @@ -0,0 +1,24 @@ +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); + } +} + diff --git a/src/c3a/C3aInstJumpIfEqual.java b/src/c3a/C3aInstJumpIfEqual.java new file mode 100644 index 0000000..98bdb79 --- /dev/null +++ b/src/c3a/C3aInstJumpIfEqual.java @@ -0,0 +1,27 @@ +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); + } +} + diff --git a/src/c3a/C3aInstJumpIfLess.java b/src/c3a/C3aInstJumpIfLess.java new file mode 100644 index 0000000..a88a20e --- /dev/null +++ b/src/c3a/C3aInstJumpIfLess.java @@ -0,0 +1,28 @@ +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); + } +} + diff --git a/src/c3a/C3aInstJumpIfNotEqual.java b/src/c3a/C3aInstJumpIfNotEqual.java new file mode 100644 index 0000000..06bc8c4 --- /dev/null +++ b/src/c3a/C3aInstJumpIfNotEqual.java @@ -0,0 +1,27 @@ +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); + } +} + diff --git a/src/c3a/C3aInstMult.java b/src/c3a/C3aInstMult.java new file mode 100644 index 0000000..282d20a --- /dev/null +++ b/src/c3a/C3aInstMult.java @@ -0,0 +1,27 @@ +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); + } + +} + diff --git a/src/c3a/C3aInstParam.java b/src/c3a/C3aInstParam.java new file mode 100644 index 0000000..44b96e2 --- /dev/null +++ b/src/c3a/C3aInstParam.java @@ -0,0 +1,25 @@ +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); + } + +} + diff --git a/src/c3a/C3aInstRead.java b/src/c3a/C3aInstRead.java new file mode 100644 index 0000000..4586c03 --- /dev/null +++ b/src/c3a/C3aInstRead.java @@ -0,0 +1,25 @@ +package c3a; + +public class C3aInstRead extends C3aInst{ + public C3aOperand result; // résultat ou destination du saut (ou NULL) + + public C3aInstRead(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 + this.result + " = read #" + this.comment; + return s; + } + + public <T> T accept(C3aVisitor <T> visitor) { + return visitor.visit(this); + } + +} + diff --git a/src/c3a/C3aInstReturn.java b/src/c3a/C3aInstReturn.java new file mode 100644 index 0000000..48d47a3 --- /dev/null +++ b/src/c3a/C3aInstReturn.java @@ -0,0 +1,25 @@ +package c3a; + +public class C3aInstReturn extends C3aInst{ + public C3aOperand op1; // opérande 1 (ou null) + + public C3aInstReturn(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 + "ret " + this.op1; + return s; + } + + public <T> T accept(C3aVisitor <T> visitor) { + return visitor.visit(this); + } + +} + diff --git a/src/c3a/C3aInstStop.java b/src/c3a/C3aInstStop.java new file mode 100644 index 0000000..12404d5 --- /dev/null +++ b/src/c3a/C3aInstStop.java @@ -0,0 +1,27 @@ +package c3a; + +public class C3aInstStop extends C3aInst{ + public C3aOperand returnValue; + + public C3aInstStop(C3aOperand returnValue, String comment){ + this.returnValue = returnValue; + this.comment = comment; + } + + public String toString(){ + String s = ""; + if(this.label != null) + s = s + this.label; + s = s + "\t"; + s = s + "stop "; + if(this.returnValue != null) + s = s + this.returnValue; + s = s + this.comment; + return s; + } + + public <T> T accept(C3aVisitor <T> visitor) { + return visitor.visit(this); + } +} + diff --git a/src/c3a/C3aInstSub.java b/src/c3a/C3aInstSub.java new file mode 100644 index 0000000..18a9853 --- /dev/null +++ b/src/c3a/C3aInstSub.java @@ -0,0 +1,27 @@ +package c3a; + +public class C3aInstSub 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 C3aInstSub(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); + } + +} + diff --git a/src/c3a/C3aInstWrite.java b/src/c3a/C3aInstWrite.java new file mode 100644 index 0000000..0e1b1a7 --- /dev/null +++ b/src/c3a/C3aInstWrite.java @@ -0,0 +1,26 @@ +package c3a; + +public class C3aInstWrite extends C3aInst{ + public C3aOperand op1; // opérande 1 (ou null) + public C3aOperand result; // résultat ou destination du saut (ou NULL) + + public C3aInstWrite(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 + "write " + this.op1 + "\t" + this.comment; + return s; + } + + public <T> T accept(C3aVisitor <T> visitor) { + return visitor.visit(this); + } + +} + diff --git a/src/c3a/C3aLabel.java b/src/c3a/C3aLabel.java new file mode 100644 index 0000000..f70d1c3 --- /dev/null +++ b/src/c3a/C3aLabel.java @@ -0,0 +1,29 @@ +package c3a; + +public class C3aLabel extends C3aOperand{ + public int number; // numéro de l'étiquette + private int line; // ligne de l'opération correspondant à l'étiquette + // public String name; + + // public C3aLabel(int number, int line, String name){ + public C3aLabel(int number){ + this.number = number; + this.line = 0; + // this.name = name; + } + + public int getNumber(){return this.number;} + public int getLine(){return this.line;} + + public String toString(){return String.valueOf(this.number);} +// public String toString(){return "l" + this.number;} + + public void affect(C3aLabel other) { + this.number = other.number; + this.line = other.line; + } + + public <T> T accept(C3aVisitor <T> visitor) { + return visitor.visit(this); + } +} diff --git a/src/c3a/C3aOp.java b/src/c3a/C3aOp.java new file mode 100644 index 0000000..e03daf6 --- /dev/null +++ b/src/c3a/C3aOp.java @@ -0,0 +1,24 @@ +package c3a; + +public enum C3aOp{ + arithAdd, + arithSub, + arithMult, + arithDiv, + funcCall, + funcParam, + funcValRet, + funcBegin, + funcEnd, + assign, //affectation et transferts temporaires <-> variables + alloc, // allouer de la place pour variables + jump, + jump_if_less, + jump_if_less_or_equal, + jumpIfEqual, + jump_if_not_equal, + jump_if_greater, + jump_if_greater_or_equal, + sys_read, + sys_write; // lire et écrire (interruptions système) +} diff --git a/src/c3a/C3aOperand.java b/src/c3a/C3aOperand.java new file mode 100644 index 0000000..7744c0d --- /dev/null +++ b/src/c3a/C3aOperand.java @@ -0,0 +1,7 @@ +package c3a; + +public abstract class C3aOperand { + + public abstract <T> T accept(C3aVisitor <T> visitor); + +} diff --git a/src/c3a/C3aTemp.java b/src/c3a/C3aTemp.java new file mode 100644 index 0000000..6958c45 --- /dev/null +++ b/src/c3a/C3aTemp.java @@ -0,0 +1,16 @@ +package c3a; + +public class C3aTemp extends C3aOperand{ + public int num; + + public C3aTemp(int num){ + this.num = num; + } + + public String toString(){ + return "@" + this.num; + } + public <T> T accept(C3aVisitor <T> visitor) { + return visitor.visit(this); + } +} diff --git a/src/c3a/C3aVar.java b/src/c3a/C3aVar.java new file mode 100644 index 0000000..dcef2c2 --- /dev/null +++ b/src/c3a/C3aVar.java @@ -0,0 +1,24 @@ +package c3a; +import ts.*; + +public class C3aVar extends C3aOperand{ + public TsItemVar item; + public C3aOperand index; + + public C3aVar(TsItemVar item, C3aOperand index){ + this.item = item; + this.index = index; + } + + public String toString(){ + if(this.index != null) + // return this.item.getIdentif() + "[" + this.item.getTaille() + "]"; + return this.item.getIdentif() + "[" + this.index + "]"; + else + return this.item.getIdentif(); + } + + public <T> T accept(C3aVisitor <T> visitor) { + return visitor.visit(this); + } +} diff --git a/src/c3a/C3aVisitor.java b/src/c3a/C3aVisitor.java new file mode 100644 index 0000000..b8f2b96 --- /dev/null +++ b/src/c3a/C3aVisitor.java @@ -0,0 +1,30 @@ +package c3a; + +public interface C3aVisitor <T> { + public T visit(C3a c3a); + public T visit(C3aInstAdd inst); + public T visit(C3aInstCall inst); + public T visit(C3aInstFBegin inst); + public T visit(C3aInst inst); + public T visit(C3aInstJumpIfLess inst); + public T visit(C3aInstMult inst); + public T visit(C3aInstRead inst); + public T visit(C3aInstSub inst); + public T visit(C3aInstAffect inst); + public T visit(C3aInstDiv inst); + public T visit(C3aInstFEnd inst); + public T visit(C3aInstJumpIfEqual inst); + public T visit(C3aInstJumpIfNotEqual inst); + public T visit(C3aInstJump inst); + public T visit(C3aInstParam inst); + public T visit(C3aInstReturn inst); + public T visit(C3aInstWrite inst); + public T visit(C3aInstStop inst); + + public T visit(C3aConstant oper); + public T visit(C3aBooleanConstant oper); + public T visit(C3aLabel oper); + public T visit(C3aTemp oper); + public T visit(C3aVar oper); + public T visit(C3aFunction oper); +} diff --git a/src/c3a/Sa2c3a.java b/src/c3a/Sa2c3a.java new file mode 100644 index 0000000..ee74a84 --- /dev/null +++ b/src/c3a/Sa2c3a.java @@ -0,0 +1,46 @@ +package c3a; +import ts.*; +import sa.*; + +public class Sa2c3a extends SaDepthFirstVisitor <C3aOperand> { + private C3a c3a; + int indentation; + public C3a getC3a(){return this.c3a;} + + public Sa2c3a(SaNode root, Ts tableGlobale){ + c3a = new C3a(); + C3aTemp result = c3a.newTemp(); + C3aFunction fct = new C3aFunction(tableGlobale.getFct("main")); + c3a.ajouteInst(new C3aInstCall(fct, result, "")); + c3a.ajouteInst(new C3aInstStop(result, "")); + indentation = 0; + + } + + public void defaultIn(SaNode node) + { + //for(int i = 0; i < indentation; i++){System.out.print(" ");} + //indentation++; + //System.out.println("<" + node.getClass().getSimpleName() + ">"); + } + + public void defaultOut(SaNode node) + { + //indentation--; + // for(int i = 0; i < indentation; i++){System.out.print(" ");} + // System.out.println("</" + node.getClass().getSimpleName() + ">"); + } + + + public C3aOperand visit(SaExpAdd node) throws Exception + { + defaultIn(node); + C3aOperand op1 = node.getOp1().accept(this); + C3aOperand op2 = node.getOp2().accept(this); + C3aOperand result = c3a.newTemp(); + + c3a.ajouteInst(new C3aInstAdd(op1, op2, result, "")); + defaultOut(node); + return result; + } +} diff --git a/src/fg/Fg.java b/src/fg/Fg.java new file mode 100644 index 0000000..52d5714 --- /dev/null +++ b/src/fg/Fg.java @@ -0,0 +1,89 @@ +package fg; +import nasm.*; +import util.graph.*; +import java.util.*; +import java.io.*; + +public class Fg implements NasmVisitor <Void> { + public Nasm nasm; + public Graph graph; + Map< NasmInst, Node> inst2Node; + Map< Node, NasmInst> node2Inst; + Map< String, NasmInst> label2Inst; + + public Fg(Nasm nasm){ + this.nasm = nasm; + this.inst2Node = new HashMap< NasmInst, Node>(); + this.node2Inst = new HashMap< Node, NasmInst>(); + this.label2Inst = new HashMap< String, NasmInst>(); + this.graph = new Graph(); + } + + public void affiche(String baseFileName){ + String fileName; + PrintStream out = System.out; + + if (baseFileName != null){ + try { + baseFileName = baseFileName; + fileName = baseFileName + ".fg"; + out = new PrintStream(fileName); + } + + catch (IOException e) { + System.err.println("Error: " + e.getMessage()); + } + } + + for(NasmInst nasmInst : nasm.sectionText){ + Node n = this.inst2Node.get(nasmInst); + out.print(n + " : ( "); + for(NodeList q=n.succ(); q!=null; q=q.tail) { + out.print(q.head.toString()); + out.print(" "); + } + out.println(")\t" + nasmInst); + } + + } + + public Void visit(NasmAdd inst){return null;} + public Void visit(NasmCall inst){return null;} + public Void visit(NasmDiv inst){return null;} + public Void visit(NasmJe inst){return null;} + public Void visit(NasmJle inst){return null;} + public Void visit(NasmJne inst){return null;} + public Void visit(NasmMul inst){return null;} + public Void visit(NasmOr inst){return null;} + public Void visit(NasmCmp inst){return null;} + public Void visit(NasmInst inst){return null;} + public Void visit(NasmJge inst){return null;} + public Void visit(NasmJl inst){return null;} + public Void visit(NasmNot inst){return null;} + public Void visit(NasmPop inst){return null;} + public Void visit(NasmRet inst){return null;} + public Void visit(NasmXor inst){return null;} + public Void visit(NasmAnd inst){return null;} + public Void visit(NasmJg inst){return null;} + public Void visit(NasmJmp inst){return null;} + public Void visit(NasmMov inst){return null;} + public Void visit(NasmPush inst){return null;} + public Void visit(NasmSub inst){return null;} + public Void visit(NasmEmpty inst){return null;} + public Void visit(NasmInt inst){return null;} + public Void visit(NasmAddress operand){return null;} + public Void visit(NasmConstant operand){return null;} + public Void visit(NasmLabel operand){return null;} + public Void visit(NasmRegister operand){return null;} + public Void visit(NasmResb pseudoInst){return null;} + public Void visit(NasmResw pseudoInst){return null;} + public Void visit(NasmResd pseudoInst){return null;} + public Void visit(NasmResq pseudoInst){return null;} + public Void visit(NasmRest pseudoInst){return null;} + public Void visit(NasmExpPlus exp){return null;} + public Void visit(NasmExpMinus exp){return null;} + public Void visit(NasmExpTimes exp){return null;} + public Void visit(NasmExp exp){return null;} + + +} diff --git a/src/fg/FgSolution.java b/src/fg/FgSolution.java new file mode 100644 index 0000000..f1c6d4c --- /dev/null +++ b/src/fg/FgSolution.java @@ -0,0 +1,49 @@ +package fg; +import util.graph.*; +import nasm.*; +import util.intset.*; +import java.io.*; +import java.util.*; + +public class FgSolution{ + int iterNum = 0; + public Nasm nasm; + Fg fg; + public Map< NasmInst, IntSet> use; + public Map< NasmInst, IntSet> def; + public Map< NasmInst, IntSet> in; + public Map< NasmInst, IntSet> out; + + public FgSolution(Nasm nasm, Fg fg){ + this.nasm = nasm; + this.fg = fg; + this.use = new HashMap< NasmInst, IntSet>(); + this.def = new HashMap< NasmInst, IntSet>(); + this.in = new HashMap< NasmInst, IntSet>(); + this.out = new HashMap< NasmInst, IntSet>(); + } + + public void affiche(String baseFileName){ + String fileName; + PrintStream out = System.out; + + if (baseFileName != null){ + try { + baseFileName = baseFileName; + fileName = baseFileName + ".fgs"; + out = new PrintStream(fileName); + } + + catch (IOException e) { + System.err.println("Error: " + e.getMessage()); + } + } + + out.println("iter num = " + iterNum); + for(NasmInst nasmInst : this.nasm.sectionText){ + out.println("use = "+ this.use.get(nasmInst) + " def = "+ this.def.get(nasmInst) + "\tin = " + this.in.get(nasmInst) + "\t \tout = " + this.out.get(nasmInst) + "\t \t" + nasmInst); + } + + } + +} diff --git a/src/ig/Ig.java b/src/ig/Ig.java new file mode 100644 index 0000000..eaf4886 --- /dev/null +++ b/src/ig/Ig.java @@ -0,0 +1,61 @@ +package ig; + +import fg.*; +import nasm.*; +import util.graph.*; +import util.intset.*; +import java.util.*; +import java.io.*; + +public class Ig { + public Graph graph; + public FgSolution fgs; + public int regNb; + public Nasm nasm; + public Node int2Node[]; + + + public Ig(FgSolution fgs){ + this.fgs = fgs; + this.graph = new Graph(); + this.nasm = fgs.nasm; + this.regNb = this.nasm.getTempCounter(); + this.int2Node = new Node[regNb]; + this.build(); + } + + public void build(){ + } + + public void affiche(String baseFileName){ + String fileName; + PrintStream out = System.out; + + if (baseFileName != null){ + try { + baseFileName = baseFileName; + fileName = baseFileName + ".ig"; + out = new PrintStream(fileName); + } + + catch (IOException e) { + System.err.println("Error: " + e.getMessage()); + } + } + + for(int i = 0; i < regNb; i++){ + Node n = this.int2Node[i]; + out.print(n + " : ( "); + for(NodeList q=n.succ(); q!=null; q=q.tail) { + out.print(q.head.toString()); + out.print(" "); + } + out.println(")"); + } + } +} + + + + + diff --git a/src/l.cfg b/src/l.cfg new file mode 100644 index 0000000..5159f48 --- /dev/null +++ b/src/l.cfg @@ -0,0 +1,32 @@ +Package lParser; + +Helpers + +lettre = [['a' .. 'z'] + ['A' .. 'Z']]; +chiffre = ['0' .. '9']; +alpha = [lettre + ['_' + '$']]; +alphanum = [lettre + chiffre]; + +Tokens + +espaces = (' ' | 13 | 10)+; +commentaire= '#' [[0 .. 0xffff] - [10 + 13]]* (10 | 13 | 10 13); +plus = '+'; +nombre = chiffre+; + + +Ignored Tokens + +espaces, commentaire; + +Productions + +// l'axiome + +programme = exp ; + +exp = + {plus} exp plus nombre | + {nb} nombre ; + + diff --git a/src/nasm/C3a2nasm.java b/src/nasm/C3a2nasm.java new file mode 100644 index 0000000..1c51837 --- /dev/null +++ b/src/nasm/C3a2nasm.java @@ -0,0 +1,62 @@ +package nasm; +import java.util.*; +import ts.*; +import c3a.*; + +public class C3a2nasm implements C3aVisitor <NasmOperand> { + private C3a c3a; + private Nasm nasm; + private Ts tableGlobale; + private TsItemFct currentFct; + private NasmRegister esp; + private NasmRegister ebp; + + + public C3a2nasm(C3a c3a, Ts tableGlobale){ + this.c3a = c3a; + nasm = new Nasm(tableGlobale); + nasm.setTempCounter(c3a.getTempCounter()); + + this.tableGlobale = tableGlobale; + this.currentFct = null; + esp = new NasmRegister(-1); + esp.colorRegister(Nasm.REG_ESP); + + ebp = new NasmRegister(-1); + ebp.colorRegister(Nasm.REG_EBP); + } + + public Nasm getNasm(){return nasm;} + + public NasmOperand getLabelFromC3aInst(C3aInst inst){ + return (inst.label != null) ? inst.label.accept(this) : null; + } + + public NasmOperand visit(C3a c3a){return null;} + public NasmOperand visit(C3aInstAdd inst){return null;} + public NasmOperand visit(C3aInstCall inst){return null;} + public NasmOperand visit(C3aInstFBegin inst){return null;} + public NasmOperand visit(C3aInst inst){return null;} + public NasmOperand visit(C3aInstJumpIfLess inst){return null;} + public NasmOperand visit(C3aInstMult inst){return null;} + public NasmOperand visit(C3aInstRead inst){return null;} + public NasmOperand visit(C3aInstSub inst){return null;} + public NasmOperand visit(C3aInstAffect inst){return null;} + public NasmOperand visit(C3aInstDiv inst){return null;} + public NasmOperand visit(C3aInstFEnd inst){return null;} + public NasmOperand visit(C3aInstJumpIfEqual inst){return null;} + public NasmOperand visit(C3aInstJumpIfNotEqual inst){return null;} + public NasmOperand visit(C3aInstJump inst){return null;} + public NasmOperand visit(C3aInstParam inst){return null;} + public NasmOperand visit(C3aInstReturn inst){return null;} + public NasmOperand visit(C3aInstWrite inst){return null;} + public NasmOperand visit(C3aInstStop inst){return null;} + + public NasmOperand visit(C3aConstant oper){return null;} + public NasmOperand visit(C3aBooleanConstant oper){return null;} + public NasmOperand visit(C3aLabel oper){return null;} + public NasmOperand visit(C3aTemp oper){return null;} + public NasmOperand visit(C3aVar oper){return null;} + public NasmOperand visit(C3aFunction oper){return null;} + +} diff --git a/src/nasm/Nasm.java b/src/nasm/Nasm.java new file mode 100644 index 0000000..3a29845 --- /dev/null +++ b/src/nasm/Nasm.java @@ -0,0 +1,196 @@ +package nasm; +import java.util.*; +import java.io.*; +import ts.*; + +public class Nasm{ + public List<NasmInst> sectionText; + public List<NasmPseudoInst> sectionBss; + protected int tempCounter = 0; + Ts tableGlobale; + public static int REG_EAX = 0; + public static int REG_EBX = 1; + public static int REG_ECX = 2; + public static int REG_EDX = 3; + public static int REG_ESP = 4; + public static int REG_EBP = 5; + public static int REG_UNK = 6; + // public static int NB_PREDEFINED_REG = 7; + + + public Nasm(Ts tableGlobale){ + this.tableGlobale = tableGlobale; + this.sectionBss = new ArrayList<NasmPseudoInst>(); + this.sectionText = new ArrayList<NasmInst>(); + populateSectionBss(tableGlobale); + } + + public Nasm(){ + this.sectionBss = new ArrayList<NasmPseudoInst>(); + this.sectionText = new ArrayList<NasmInst>(); + } + + public int getTempCounter(){return this.tempCounter;} + public int setTempCounter(int c){return this.tempCounter = c;} + + public void ajoutePseudoInst(NasmPseudoInst pseudoInst){ + this.sectionBss.add(pseudoInst); + } + + public void ajouteInst(NasmInst inst){ + if(inst instanceof NasmMov && inst.destination instanceof NasmAddress && inst.source instanceof NasmAddress){ + NasmRegister newReg = newRegister(); + this.sectionText.add(new NasmMov(inst.label, newReg, inst.source, inst.comment)); + this.sectionText.add(new NasmMov(null, inst.destination, newReg, "on passe par un registre temporaire")); + return; + } + if(inst instanceof NasmAdd && inst.destination instanceof NasmAddress && inst.source instanceof NasmAddress){ + NasmRegister newReg = newRegister(); + this.sectionText.add(new NasmMov(inst.label, newReg, inst.source, inst.comment)); + this.sectionText.add(new NasmAdd(null, inst.destination, newReg, "on passe par un registre temporaire")); + return; + } + if(inst instanceof NasmSub && inst.destination instanceof NasmAddress && inst.source instanceof NasmAddress){ + NasmRegister newReg = newRegister(); + this.sectionText.add(new NasmMov(inst.label, newReg, inst.source, inst.comment)); + this.sectionText.add(new NasmSub(null, inst.destination, newReg, "on passe par un registre temporaire")); + return; + } + if(inst instanceof NasmMul && inst.destination instanceof NasmAddress && inst.source instanceof NasmAddress){ + NasmRegister newReg = newRegister(); + this.sectionText.add(new NasmMov(inst.label, newReg, inst.source, inst.comment)); + this.sectionText.add(new NasmMul(null, inst.destination, newReg, "on passe par un registre temporaire")); + return; + } + + + // if(inst instanceof NasmCmp && inst.destination instanceof NasmConstant && inst.source instanceof NasmConstant){ + if(inst instanceof NasmCmp + && (inst.destination instanceof NasmConstant + || (inst.destination instanceof NasmAddress && inst.source instanceof NasmAddress))){ + NasmRegister newReg = newRegister(); + this.sectionText.add(new NasmMov(inst.label, newReg, inst.destination, inst.comment)); + this.sectionText.add(new NasmCmp(null, newReg, inst.source, "on passe par un registre temporaire")); + return; + } + + this.sectionText.add(inst); + } + + public NasmRegister newRegister(){ + return new NasmRegister(tempCounter++); +} + + public NasmRegister newEaxRegister(){ + NasmRegister reg = newRegister(); + reg.colorRegister(Nasm.REG_EAX); + return reg; + } + + public NasmRegister newEbxRegister(){ + NasmRegister reg = newRegister(); + reg.colorRegister(Nasm.REG_EBX); + return reg; + } + + public NasmRegister newEcxRegister(){ + NasmRegister reg = newRegister(); + reg.colorRegister(Nasm.REG_ECX); + return reg; + } + + public NasmRegister newEdxRegister(){ + NasmRegister reg = newRegister(); + reg.colorRegister(Nasm.REG_EDX); + return reg; + } + + public void populateSectionBss(Ts tableGlobale){ + ajoutePseudoInst(new NasmResb(new NasmLabel("sinput"), 255, "reserve a 255 byte space in memory for the users input string")); + Set< Map.Entry< String, TsItemVar> > st = tableGlobale.variables.entrySet(); + for (Map.Entry< String, TsItemVar> me:st){ + TsItemVar tsItem = me.getValue(); + String identif = me.getKey(); + if(tsItem instanceof TsItemVarSimple) + ajoutePseudoInst(new NasmResd(new NasmLabel(identif), tsItem.type.taille(), "variable globale")); + if(tsItem instanceof TsItemVarTab) + ajoutePseudoInst(new NasmResd(new NasmLabel(identif), tsItem.type.taille() * ((TsItemVarTab)tsItem).taille, "variable globale")); + // ajoutePseudoInst(new NasmResd(new NasmLabel(identif), tsItem.taille*4, "variable globale")); + } + } + + + public void affichePreNasm(String baseFileName){ + String fileName; + PrintStream out = System.out; + + if (baseFileName != null){ + try { + baseFileName = baseFileName; + fileName = baseFileName + ".pre-nasm"; + out = new PrintStream(fileName); + } + + catch (IOException e) { + System.err.println("Error: " + e.getMessage()); + } + } + affiche(out); + } + + public void afficheNasm(String baseFileName){ + String fileName; + PrintStream out = System.out; + + if (baseFileName != null){ + try { + baseFileName = baseFileName; + fileName = baseFileName + ".nasm"; + out = new PrintStream(fileName); + } + + catch (IOException e) { + System.err.println("Error: " + e.getMessage()); + } + } + affiche(out); + } + + public void affiche(PrintStream out){ + out.println("%include\t'io.asm'\n"); + out.println("section\t.bss"); + Iterator<NasmPseudoInst> iter = this.sectionBss.iterator(); + while(iter.hasNext()){ + out.println(iter.next()); + } + + out.println("\nsection\t.text"); + out.println("global _start"); + out.println("_start:"); + Iterator<NasmInst> iter2 = this.sectionText.iterator(); + while(iter2.hasNext()){ + out.println(iter2.next()); + } + } +} +/* + public void affichePreambule(PrintStream out) + { + out.println("%include\t'io.asm'\n"); + out.println("section\t.bss"); + out.println("sinput:\tresb\t255\t;reserve a 255 byte space in memory for the users input string"); + + + Set< Map.Entry< String, TsItemVar> > st = tableGlobale.variables.entrySet(); + for (Map.Entry< String, TsItemVar> me:st){ + TsItemVar tsItem = me.getValue(); + String identif = me.getKey(); + out.println(identif + " :\tresd\t" + tsItem.taille * 4); + } + out.println("\nsection\t.text"); + out.println("global _start"); + out.println("_start:"); + } + + + */ diff --git a/src/nasm/NasmAdd.java b/src/nasm/NasmAdd.java new file mode 100644 index 0000000..340d0df --- /dev/null +++ b/src/nasm/NasmAdd.java @@ -0,0 +1,25 @@ +package nasm; + +public class NasmAdd extends NasmInst { + + public NasmAdd(NasmOperand label, NasmOperand destination, NasmOperand source, String comment){ + + destUse = true; + destDef = true; + srcUse = true; + this.label = label; + this.destination = destination; + this.source = source; + this.comment = comment; + } + + public <T> T accept(NasmVisitor <T> visitor) { + return visitor.visit(this); + } + + public String toString(){ + return super.formatInst(this.label, "add", this.destination, this.source, this.comment); + } + + +} diff --git a/src/nasm/NasmAddress.java b/src/nasm/NasmAddress.java new file mode 100644 index 0000000..f81e92a --- /dev/null +++ b/src/nasm/NasmAddress.java @@ -0,0 +1,26 @@ +package nasm; + +public class NasmAddress extends NasmOperand { + + public NasmExp val; + public NasmSize size; + + public NasmAddress(NasmExp val, NasmSize size){ + this.val = val; + this.size = size; + } + + public String toString(){ + return this.size.toString() + " [" + this.val.toString() + "]"; + } + + public boolean isGeneralRegister(){ + return false; + } + + public <T> T accept(NasmVisitor <T> visitor) { + return visitor.visit(this); + } + + +} diff --git a/src/nasm/NasmAnd.java b/src/nasm/NasmAnd.java new file mode 100644 index 0000000..c4c32e3 --- /dev/null +++ b/src/nasm/NasmAnd.java @@ -0,0 +1,24 @@ +package nasm; + +public class NasmAnd extends NasmInst { + + public NasmAnd(NasmOperand label, NasmOperand destination, NasmOperand source, String comment){ + destUse = true; + destDef = true; + srcUse = true; + this.label = label; + this.destination = destination; + this.source = source; + this.comment = comment; + } + + public <T> T accept(NasmVisitor <T> visitor) { + return visitor.visit(this); + } + + public String toString(){ + return super.formatInst(this.label, "and", this.destination, this.source, this.comment); + } + + +} diff --git a/src/nasm/NasmCall.java b/src/nasm/NasmCall.java new file mode 100644 index 0000000..d7a43e1 --- /dev/null +++ b/src/nasm/NasmCall.java @@ -0,0 +1,19 @@ +package nasm; + +public class NasmCall extends NasmInst { + + public NasmCall(NasmOperand label, NasmOperand address, String comment){ + this.label = label; + this.address = address; + this.comment = comment; + } + + public <T> T accept(NasmVisitor <T> visitor) { + return visitor.visit(this); + } + + public String toString(){ + return super.formatInst(this.label, "call", this.address, null, this.comment); + } + +} diff --git a/src/nasm/NasmCmp.java b/src/nasm/NasmCmp.java new file mode 100644 index 0000000..6926d8f --- /dev/null +++ b/src/nasm/NasmCmp.java @@ -0,0 +1,23 @@ +package nasm; + +public class NasmCmp extends NasmInst { + + public NasmCmp(NasmOperand label, NasmOperand destination, NasmOperand source, String comment){ + destUse = true; + srcUse= true; + this.label = label; + this.destination = destination; + this.source = source; + this.comment = comment; + } + + public <T> T accept(NasmVisitor <T> visitor) { + return visitor.visit(this); + } + + public String toString(){ + return super.formatInst(this.label, "cmp", this.destination, this.source, this.comment); + } + + +} diff --git a/src/nasm/NasmConstant.java b/src/nasm/NasmConstant.java new file mode 100644 index 0000000..9f6a749 --- /dev/null +++ b/src/nasm/NasmConstant.java @@ -0,0 +1,28 @@ +package nasm; +import java.util.ArrayList; + +public class NasmConstant extends NasmOperand implements NasmExp{ + public int val; + public NasmConstant(int val){ + this.val = val; + } + + public String toString(){ + return Integer.toString(this.val); + } + + public <T> T accept(NasmVisitor <T> visitor) { + return visitor.visit(this); + } + + public ArrayList<NasmRegister> getRegisters() + { + ArrayList<NasmRegister> registerList = new ArrayList<NasmRegister>(); + this.getRegisters(registerList); + return registerList; + } + public ArrayList<NasmRegister> getRegisters(ArrayList<NasmRegister> registerList) + { + return registerList; + } +} diff --git a/src/nasm/NasmDiv.java b/src/nasm/NasmDiv.java new file mode 100644 index 0000000..c3cc45f --- /dev/null +++ b/src/nasm/NasmDiv.java @@ -0,0 +1,19 @@ +package nasm; + +public class NasmDiv extends NasmInst { + + public NasmDiv(NasmOperand label, NasmOperand source, String comment){ + srcUse = true; + this.label = label; + this.source = source; + this.comment = comment; + } + + public <T> T accept(NasmVisitor <T> visitor) { + return visitor.visit(this); + } + + public String toString(){ + return super.formatInst(this.label, "idiv", this.source, null, this.comment); + } +} diff --git a/src/nasm/NasmEmpty.java b/src/nasm/NasmEmpty.java new file mode 100644 index 0000000..808f690 --- /dev/null +++ b/src/nasm/NasmEmpty.java @@ -0,0 +1,18 @@ +package nasm; + +public class NasmEmpty extends NasmInst { + + public NasmEmpty(NasmOperand label, String comment){ + this.label = label; + this.comment = comment; + } + + public <T> T accept(NasmVisitor <T> visitor) { + return visitor.visit(this); + } + + public String toString(){ + return super.formatInst(this.label, "", null, null, this.comment); + } + +} diff --git a/src/nasm/NasmExp.java b/src/nasm/NasmExp.java new file mode 100644 index 0000000..6e51c1d --- /dev/null +++ b/src/nasm/NasmExp.java @@ -0,0 +1,11 @@ +package nasm; +import java.util.ArrayList; + +public interface NasmExp { + // public boolean isStackAddress(); + public ArrayList<NasmRegister> getRegisters(); + public ArrayList<NasmRegister> getRegisters(ArrayList<NasmRegister> registerList); + + + public <T> T accept(NasmVisitor <T> visitor); +} diff --git a/src/nasm/NasmExpMinus.java b/src/nasm/NasmExpMinus.java new file mode 100644 index 0000000..419c1c1 --- /dev/null +++ b/src/nasm/NasmExpMinus.java @@ -0,0 +1,34 @@ +package nasm; +import java.util.ArrayList; + +public class NasmExpMinus implements NasmExp{ + public NasmExp op1; + public NasmExp op2; + + public NasmExpMinus(NasmExp op1, NasmExp op2){ + this.op1 = op1; + this.op2 = op2; + } + + @Override + public String toString() { + return op1.toString() + "-" + op2.toString(); + } + + public <T> T accept(NasmVisitor <T> visitor) { + return visitor.visit(this); + } + + public ArrayList<NasmRegister> getRegisters() + { + ArrayList<NasmRegister> registerList = new ArrayList<NasmRegister>(); + this.getRegisters(registerList); + return registerList; + } + public ArrayList<NasmRegister> getRegisters(ArrayList<NasmRegister> registerList) + { + this.op1.getRegisters(registerList); + this.op2.getRegisters(registerList); + return registerList; + } +} diff --git a/src/nasm/NasmExpPlus.java b/src/nasm/NasmExpPlus.java new file mode 100644 index 0000000..440d310 --- /dev/null +++ b/src/nasm/NasmExpPlus.java @@ -0,0 +1,34 @@ +package nasm; +import java.util.ArrayList; + +public class NasmExpPlus implements NasmExp{ + public NasmExp op1; + public NasmExp op2; + + public NasmExpPlus(NasmExp op1, NasmExp op2){ + this.op1 = op1; + this.op2 = op2; + } + + @Override + public String toString() { + return op1.toString() + "+" + op2.toString(); + } + + public <T> T accept(NasmVisitor <T> visitor) { + return visitor.visit(this); + } + + public ArrayList<NasmRegister> getRegisters() + { + ArrayList<NasmRegister> registerList = new ArrayList<NasmRegister>(); + this.getRegisters(registerList); + return registerList; + } + public ArrayList<NasmRegister> getRegisters(ArrayList<NasmRegister> registerList) + { + this.op1.getRegisters(registerList); + this.op2.getRegisters(registerList); + return registerList; + } +} diff --git a/src/nasm/NasmExpTimes.java b/src/nasm/NasmExpTimes.java new file mode 100644 index 0000000..6a83c67 --- /dev/null +++ b/src/nasm/NasmExpTimes.java @@ -0,0 +1,34 @@ +package nasm; +import java.util.ArrayList; + +public class NasmExpTimes implements NasmExp{ + public NasmExp op1; + public NasmExp op2; + + public NasmExpTimes(NasmExp op1, NasmExp op2){ + this.op1 = op1; + this.op2 = op2; + } + + @Override + public String toString() { + return op1.toString() + "*" + op2.toString(); + } + + public <T> T accept(NasmVisitor <T> visitor) { + return visitor.visit(this); + } + + public ArrayList<NasmRegister> getRegisters() + { + ArrayList<NasmRegister> registerList = new ArrayList<NasmRegister>(); + this.getRegisters(registerList); + return registerList; + } + public ArrayList<NasmRegister> getRegisters(ArrayList<NasmRegister> registerList) + { + this.op1.getRegisters(registerList); + this.op2.getRegisters(registerList); + return registerList; + } +} diff --git a/src/nasm/NasmInst.java b/src/nasm/NasmInst.java new file mode 100644 index 0000000..5d81f8f --- /dev/null +++ b/src/nasm/NasmInst.java @@ -0,0 +1,36 @@ +package nasm; + +public abstract class NasmInst{ + public NasmOperand label = null; + public NasmOperand destination = null; + public NasmOperand source = null; + public NasmOperand address = null; + public boolean destUse = false; + public boolean destDef = false; + public boolean srcUse = false; + public boolean srcDef = false; + String comment; + + void addLabel(String formatInst, NasmOperand label){ + formatInst += label; + } + + public String formatInst(NasmOperand label, String opcode, NasmOperand arg1, NasmOperand arg2, String comment){ + String s = ""; + if(label != null) + s = s + label + " :"; + s = s + "\t" + opcode; + if(arg1 != null) + s = s + "\t" + arg1; + if(arg2 != null) + s = s + ",\t" + arg2; + if(comment != null & comment != "") + s = s + "\t;" + comment; + return s; + } + + public <T> T accept(NasmVisitor <T> visitor) { + return visitor.visit(this); + } + +} diff --git a/src/nasm/NasmInt.java b/src/nasm/NasmInt.java new file mode 100644 index 0000000..3e6a5b5 --- /dev/null +++ b/src/nasm/NasmInt.java @@ -0,0 +1,18 @@ +package nasm; + +public class NasmInt extends NasmInst { + + public NasmInt(NasmOperand label, String comment){ + this.label = label; + this.comment = comment; + } + + public <T> T accept(NasmVisitor <T> visitor) { + return visitor.visit(this); + } + + public String toString(){ + return super.formatInst(this.label, "int 0x80", null, null, this.comment); + } + +} diff --git a/src/nasm/NasmJe.java b/src/nasm/NasmJe.java new file mode 100644 index 0000000..e40ab6c --- /dev/null +++ b/src/nasm/NasmJe.java @@ -0,0 +1,19 @@ +package nasm; + +public class NasmJe extends NasmInst { + + public NasmJe(NasmOperand label, NasmOperand address, String comment){ + this.label = label; + this.address = address; + this.comment = comment; + } + + public <T> T accept(NasmVisitor <T> visitor) { + return visitor.visit(this); + } + + public String toString(){ + return super.formatInst(this.label, "je", this.address, null, this.comment); + } + +} diff --git a/src/nasm/NasmJg.java b/src/nasm/NasmJg.java new file mode 100644 index 0000000..1f6881c --- /dev/null +++ b/src/nasm/NasmJg.java @@ -0,0 +1,19 @@ +package nasm; + +public class NasmJg extends NasmInst { + + public NasmJg(NasmOperand label, NasmOperand address, String comment){ + this.label = label; + this.address = address; + this.comment = comment; + } + + public <T> T accept(NasmVisitor <T> visitor) { + return visitor.visit(this); + } + + public String toString(){ + return super.formatInst(this.label, "jg", this.address, null, this.comment); + } + +} diff --git a/src/nasm/NasmJge.java b/src/nasm/NasmJge.java new file mode 100644 index 0000000..d5091cb --- /dev/null +++ b/src/nasm/NasmJge.java @@ -0,0 +1,19 @@ +package nasm; + +public class NasmJge extends NasmInst { + + public NasmJge(NasmOperand label, NasmOperand address, String comment){ + this.label = label; + this.address = address; + this.comment = comment; + } + + public <T> T accept(NasmVisitor <T> visitor) { + return visitor.visit(this); + } + + public String toString(){ + return super.formatInst(this.label, "jge", this.address, null, this.comment); + } + +} diff --git a/src/nasm/NasmJl.java b/src/nasm/NasmJl.java new file mode 100644 index 0000000..5216ba3 --- /dev/null +++ b/src/nasm/NasmJl.java @@ -0,0 +1,19 @@ +package nasm; + +public class NasmJl extends NasmInst { + + public NasmJl(NasmOperand label, NasmOperand address, String comment){ + this.label = label; + this.address = address; + this.comment = comment; + } + + public <T> T accept(NasmVisitor <T> visitor) { + return visitor.visit(this); + } + + public String toString(){ + return super.formatInst(this.label, "jl", this.address, null, this.comment); + } + +} diff --git a/src/nasm/NasmJle.java b/src/nasm/NasmJle.java new file mode 100644 index 0000000..ac84e86 --- /dev/null +++ b/src/nasm/NasmJle.java @@ -0,0 +1,25 @@ +package nasm; + +public class NasmJle extends NasmInst { + + public NasmJle(NasmOperand label, NasmOperand address, String comment){ + this.label = label; + this.address = address; + this.comment = comment; + } + + public <T> T accept(NasmVisitor <T> visitor) { + return visitor.visit(this); + } + + public String toString(){ + // String formatInst + // addLabel(label); + + //return formatInst; + + + return super.formatInst(this.label, "jle", this.address, null, this.comment); + } + +} diff --git a/src/nasm/NasmJmp.java b/src/nasm/NasmJmp.java new file mode 100644 index 0000000..62377f1 --- /dev/null +++ b/src/nasm/NasmJmp.java @@ -0,0 +1,19 @@ +package nasm; + +public class NasmJmp extends NasmInst { + + public NasmJmp(NasmOperand label, NasmOperand address, String comment){ + this.label = label; + this.address = address; + this.comment = comment; + } + + public <T> T accept(NasmVisitor <T> visitor) { + return visitor.visit(this); + } + + public String toString(){ + return super.formatInst(this.label, "jmp", this.address, null, this.comment); + } + +} diff --git a/src/nasm/NasmJne.java b/src/nasm/NasmJne.java new file mode 100644 index 0000000..06531c8 --- /dev/null +++ b/src/nasm/NasmJne.java @@ -0,0 +1,19 @@ +package nasm; + +public class NasmJne extends NasmInst { + + public NasmJne(NasmOperand label, NasmOperand address, String comment){ + this.label = label; + this.address = address; + this.comment = comment; + } + + public <T> T accept(NasmVisitor <T> visitor) { + return visitor.visit(this); + } + + public String toString(){ + return super.formatInst(this.label, "jne", this.address, null, this.comment); + } + +} diff --git a/src/nasm/NasmLabel.java b/src/nasm/NasmLabel.java new file mode 100644 index 0000000..85d4909 --- /dev/null +++ b/src/nasm/NasmLabel.java @@ -0,0 +1,28 @@ +package nasm; +import java.util.ArrayList; + +public class NasmLabel extends NasmOperand implements NasmExp { + public String val; + + public NasmLabel(String val){ + this.val = val; + } + public String toString(){ + return this.val; + } + public <T> T accept(NasmVisitor <T> visitor) { + return visitor.visit(this); + } + public boolean isStackAddress(){return false;} + + public ArrayList<NasmRegister> getRegisters() + { + ArrayList<NasmRegister> registerList = new ArrayList<NasmRegister>(); + this.getRegisters(registerList); + return registerList; + } + public ArrayList<NasmRegister> getRegisters(ArrayList<NasmRegister> registerList) + { + return registerList; + } +} diff --git a/src/nasm/NasmMov.java b/src/nasm/NasmMov.java new file mode 100644 index 0000000..8d3d743 --- /dev/null +++ b/src/nasm/NasmMov.java @@ -0,0 +1,22 @@ +package nasm; + +public class NasmMov extends NasmInst { + + public NasmMov(NasmOperand label, NasmOperand destination, NasmOperand source, String comment){ + destDef = true; + srcUse = true; + this.label = label; + this.destination = destination; + this.source = source; + this.comment = comment; + } + + public <T> T accept(NasmVisitor <T> visitor) { + return visitor.visit(this); + } + + public String toString(){ + return super.formatInst(this.label, "mov", this.destination, this.source, this.comment); + } + +} diff --git a/src/nasm/NasmMul.java b/src/nasm/NasmMul.java new file mode 100644 index 0000000..3be1297 --- /dev/null +++ b/src/nasm/NasmMul.java @@ -0,0 +1,26 @@ +package nasm; + +public class NasmMul extends NasmInst { + public NasmMul(NasmOperand label, NasmOperand destination, NasmOperand source, String comment){ + destUse = true; + destDef = true; + srcUse = true; + this.label = label; + this.destination = destination; + this.source = source; + this.comment = comment; + } + + public <T> T accept(NasmVisitor <T> visitor) { + return visitor.visit(this); + } + + /* public String toString(){ + return super.formatInst(this.label, "imul", this.source, null, this.comment); + }*/ + + public String toString(){ + return super.formatInst(this.label, "imul", this.destination, this.source, this.comment); + } + +} diff --git a/src/nasm/NasmNot.java b/src/nasm/NasmNot.java new file mode 100644 index 0000000..c43ddf2 --- /dev/null +++ b/src/nasm/NasmNot.java @@ -0,0 +1,21 @@ +package nasm; + +public class NasmNot extends NasmInst { + + public NasmNot(NasmOperand label, NasmOperand destination, String comment){ + destUse = true; + destDef = true; + this.label = label; + this.destination = destination; + this.comment = comment; + } + + public <T> T accept(NasmVisitor <T> visitor) { + return visitor.visit(this); + } + + public String toString(){ + return super.formatInst(this.label, "not", this.destination, null, this.comment); + } + +} diff --git a/src/nasm/NasmOperand.java b/src/nasm/NasmOperand.java new file mode 100644 index 0000000..5fb05ba --- /dev/null +++ b/src/nasm/NasmOperand.java @@ -0,0 +1,8 @@ +package nasm; + +public abstract class NasmOperand { + public boolean isGeneralRegister(){ + return false; + } + public abstract <T> T accept(NasmVisitor <T> visitor); +} diff --git a/src/nasm/NasmOr.java b/src/nasm/NasmOr.java new file mode 100644 index 0000000..3dd09f3 --- /dev/null +++ b/src/nasm/NasmOr.java @@ -0,0 +1,24 @@ +package nasm; + +public class NasmOr extends NasmInst { + + public NasmOr(NasmOperand label, NasmOperand destination, NasmOperand source, String comment){ + destUse = true; + destDef = true; + srcUse = true; + this.label = label; + this.destination = destination; + this.source = source; + this.comment = comment; + } + + public <T> T accept(NasmVisitor <T> visitor) { + return visitor.visit(this); + } + + public String toString(){ + return super.formatInst(this.label, "or", this.destination, this.source, this.comment); + } + + +} diff --git a/src/nasm/NasmPop.java b/src/nasm/NasmPop.java new file mode 100644 index 0000000..88c46bf --- /dev/null +++ b/src/nasm/NasmPop.java @@ -0,0 +1,20 @@ +package nasm; + +public class NasmPop extends NasmInst { + + public NasmPop(NasmOperand label, NasmOperand destination, String comment){ + destDef = true; + this.label = label; + this.destination = destination; + this.comment = comment; + } + + public <T> T accept(NasmVisitor <T> visitor) { + return visitor.visit(this); + } + + public String toString(){ + return super.formatInst(this.label, "pop", this.destination, null, this.comment); + } + +} diff --git a/src/nasm/NasmPseudoInst.java b/src/nasm/NasmPseudoInst.java new file mode 100644 index 0000000..27e14c7 --- /dev/null +++ b/src/nasm/NasmPseudoInst.java @@ -0,0 +1,28 @@ +package nasm; + +public abstract class NasmPseudoInst{ + public NasmLabel label = null; + public int nb = 1; + public int sizeInBytes; + String comment; + + void addLabel(String formatInst, NasmOperand label){ + formatInst += label; + } + + public String formatInst(NasmOperand label, String opcode, int nb, String comment){ + String s = ""; + if(label != null) + s = s + label + " :"; + s = s + "\t" + opcode; + s = s + "\t" + nb; + if(comment != null) + s = s + "\t;" + comment; + return s; + } + + /* public <T> T accept(NasmVisitor <T> visitor) { + return visitor.visit(this); + }*/ + +} diff --git a/src/nasm/NasmPush.java b/src/nasm/NasmPush.java new file mode 100644 index 0000000..a663634 --- /dev/null +++ b/src/nasm/NasmPush.java @@ -0,0 +1,20 @@ +package nasm; + +public class NasmPush extends NasmInst { + + public NasmPush(NasmOperand label, NasmOperand source, String comment){ + // srcUse = true; + this.label = label; + this.source = source; + this.comment = comment; + } + + public <T> T accept(NasmVisitor <T> visitor) { + return visitor.visit(this); + } + + public String toString(){ + return super.formatInst(this.label, "push", this.source, null, this.comment); + } + +} diff --git a/src/nasm/NasmRegister.java b/src/nasm/NasmRegister.java new file mode 100644 index 0000000..9f00453 --- /dev/null +++ b/src/nasm/NasmRegister.java @@ -0,0 +1,57 @@ +package nasm; +import java.util.ArrayList; + +public class NasmRegister extends NasmOperand implements NasmExp{ + public int val; + public int color = Nasm.REG_UNK; + + public NasmRegister(int val){ + this.val = val; + } + + public void colorRegister(int color){ + this.color = color; + } + + public String toString(){ + if(this.color == Nasm.REG_ESP) return "esp"; + if(this.color == Nasm.REG_EBP) return "ebp"; + if(this.color == Nasm.REG_EAX) return "eax"; + if(this.color == Nasm.REG_EBX) return "ebx"; + if(this.color == Nasm.REG_ECX) return "ecx"; + if(this.color == Nasm.REG_EDX) return "edx"; + else + return "@" + this.val; + } + + public boolean isEbp(){ + if(this.color == Nasm.REG_EBP) return true; + return false; + } + + public boolean isGeneralRegister(){ + if(this.color == Nasm.REG_EAX) return true; + if(this.color == Nasm.REG_EBX) return true; + if(this.color == Nasm.REG_ECX) return true; + if(this.color == Nasm.REG_EDX) return true; + if(this.color == Nasm.REG_UNK) return true; + return false; + } + + public <T> T accept(NasmVisitor <T> visitor) { + return visitor.visit(this); + } + + public ArrayList<NasmRegister> getRegisters() + { + ArrayList<NasmRegister> registerList = new ArrayList<NasmRegister>(); + this.getRegisters(registerList); + return registerList; + } + public ArrayList<NasmRegister> getRegisters(ArrayList<NasmRegister> registerList) + { + registerList.add(this); + return registerList; + } + +} diff --git a/src/nasm/NasmResb.java b/src/nasm/NasmResb.java new file mode 100644 index 0000000..c58418c --- /dev/null +++ b/src/nasm/NasmResb.java @@ -0,0 +1,21 @@ +package nasm; + +public class NasmResb extends NasmPseudoInst { + + public NasmResb(NasmLabel label, int nb, String comment){ + this.label = label; + this.nb = nb; + this.sizeInBytes = 1; + this.comment = comment; + } + + public <T> T accept(NasmVisitor <T> visitor) { + return visitor.visit(this); + } + + public String toString(){ + return super.formatInst(this.label, "resb", this.nb, this.comment); + } + + +} diff --git a/src/nasm/NasmResd.java b/src/nasm/NasmResd.java new file mode 100644 index 0000000..5b4e73f --- /dev/null +++ b/src/nasm/NasmResd.java @@ -0,0 +1,21 @@ +package nasm; + +public class NasmResd extends NasmPseudoInst { + + public NasmResd(NasmLabel label, int nb, String comment){ + this.label = label; + this.nb = nb; + this.sizeInBytes = 4; + this.comment = comment; + } + + public <T> T accept(NasmVisitor <T> visitor) { + return visitor.visit(this); + } + + public String toString(){ + return super.formatInst(this.label, "resd", this.nb, this.comment); + } + + +} diff --git a/src/nasm/NasmResq.java b/src/nasm/NasmResq.java new file mode 100644 index 0000000..671bbfa --- /dev/null +++ b/src/nasm/NasmResq.java @@ -0,0 +1,21 @@ +package nasm; + +public class NasmResq extends NasmPseudoInst { + + public NasmResq(NasmLabel label, int nb, String comment){ + this.label = label; + this.nb = nb; + this.sizeInBytes = 8; + this.comment = comment; + } + + public <T> T accept(NasmVisitor <T> visitor) { + return visitor.visit(this); + } + + public String toString(){ + return super.formatInst(this.label, "resq", this.nb, this.comment); + } + + +} diff --git a/src/nasm/NasmRest.java b/src/nasm/NasmRest.java new file mode 100644 index 0000000..6b1c2c0 --- /dev/null +++ b/src/nasm/NasmRest.java @@ -0,0 +1,21 @@ +package nasm; + +public class NasmRest extends NasmPseudoInst { + + public NasmRest(NasmLabel label, int nb, String comment){ + this.label = label; + this.nb = nb; + this.sizeInBytes = 10; + this.comment = comment; + } + + public <T> T accept(NasmVisitor <T> visitor) { + return visitor.visit(this); + } + + public String toString(){ + return super.formatInst(this.label, "rest", this.nb, this.comment); + } + + +} diff --git a/src/nasm/NasmResw.java b/src/nasm/NasmResw.java new file mode 100644 index 0000000..47bedc9 --- /dev/null +++ b/src/nasm/NasmResw.java @@ -0,0 +1,21 @@ +package nasm; + +public class NasmResw extends NasmPseudoInst { + + public NasmResw(NasmLabel label, int nb, String comment){ + this.label = label; + this.nb = nb; + this.sizeInBytes = 2; + this.comment = comment; + } + + public <T> T accept(NasmVisitor <T> visitor) { + return visitor.visit(this); + } + + public String toString(){ + return super.formatInst(this.label, "resw", this.nb, this.comment); + } + + +} diff --git a/src/nasm/NasmRet.java b/src/nasm/NasmRet.java new file mode 100644 index 0000000..0c95b98 --- /dev/null +++ b/src/nasm/NasmRet.java @@ -0,0 +1,18 @@ +package nasm; + +public class NasmRet extends NasmInst { + + public NasmRet(NasmOperand label, String comment){ + this.label = label; + this.comment = comment; + } + + public <T> T accept(NasmVisitor <T> visitor) { + return visitor.visit(this); + } + + public String toString(){ + return super.formatInst(this.label, "ret", null, null, this.comment); + } + +} diff --git a/src/nasm/NasmSize.java b/src/nasm/NasmSize.java new file mode 100644 index 0000000..3812007 --- /dev/null +++ b/src/nasm/NasmSize.java @@ -0,0 +1,20 @@ +package nasm; + +public enum NasmSize { + + BYTE(1, "byte"), + WORD(2, "word"), + DWORD(4, "dword"), + UNDEFINED(0, ""); + private final String name; + private final int value; + + NasmSize(int value, String name) { + this.value = value; + this.name = name; + } + + public String toString(){return name;} + public String getName(){return this.name;} + public int getValue(){return value;} +} \ No newline at end of file diff --git a/src/nasm/NasmSub.java b/src/nasm/NasmSub.java new file mode 100644 index 0000000..5162718 --- /dev/null +++ b/src/nasm/NasmSub.java @@ -0,0 +1,22 @@ +package nasm; + +public class NasmSub extends NasmInst { + + public NasmSub(NasmOperand label, NasmOperand destination, NasmOperand source, String comment){ + destUse = true; + destDef = true; + srcUse = true; + this.label = label; + this.destination = destination; + this.source = source; + this.comment = comment; + } + + public <T> T accept(NasmVisitor <T> visitor) { + return visitor.visit(this); + } + + public String toString(){ + return super.formatInst(this.label, "sub", this.destination, this.source, this.comment); + } +} diff --git a/src/nasm/NasmVisitor.java b/src/nasm/NasmVisitor.java new file mode 100644 index 0000000..091f20e --- /dev/null +++ b/src/nasm/NasmVisitor.java @@ -0,0 +1,47 @@ +package nasm; + +public interface NasmVisitor <T> { + public T visit(NasmAdd inst); + public T visit(NasmCall inst); + public T visit(NasmDiv inst); + public T visit(NasmJe inst); + public T visit(NasmJle inst); + public T visit(NasmJne inst); + public T visit(NasmMul inst); + public T visit(NasmOr inst); + public T visit(NasmCmp inst); + public T visit(NasmInst inst); + public T visit(NasmJge inst); + public T visit(NasmJl inst); + public T visit(NasmNot inst); + public T visit(NasmPop inst); + public T visit(NasmRet inst); + public T visit(NasmXor inst); + public T visit(NasmAnd inst); + public T visit(NasmJg inst); + public T visit(NasmJmp inst); + public T visit(NasmMov inst); + public T visit(NasmPush inst); + public T visit(NasmSub inst); + public T visit(NasmEmpty inst); + public T visit(NasmInt inst); + + public T visit(NasmAddress operand); + public T visit(NasmConstant operand); + public T visit(NasmLabel operand); + public T visit(NasmRegister operand); + + public T visit(NasmResb pseudoInst); + public T visit(NasmResw pseudoInst); + public T visit(NasmResd pseudoInst); + public T visit(NasmResq pseudoInst); + public T visit(NasmRest pseudoInst); + + public T visit(NasmExp exp); + public T visit(NasmExpPlus exp); + public T visit(NasmExpMinus exp); + public T visit(NasmExpTimes exp); + +} + + diff --git a/src/nasm/NasmXor.java b/src/nasm/NasmXor.java new file mode 100644 index 0000000..28a0803 --- /dev/null +++ b/src/nasm/NasmXor.java @@ -0,0 +1,24 @@ +package nasm; + +public class NasmXor extends NasmInst { + + public NasmXor(NasmOperand label, NasmOperand destination, NasmOperand source, String comment){ + destUse = true; + destDef = true; + srcUse = true; + this.label = label; + this.destination = destination; + this.source = source; + this.comment = comment; + } + + public <T> T accept(NasmVisitor <T> visitor) { + return visitor.visit(this); + } + + public String toString(){ + return super.formatInst(this.label, "xor", this.destination, this.source, this.comment); + } + + +} diff --git a/src/sa/ErrorException.java b/src/sa/ErrorException.java new file mode 100644 index 0000000..61963e6 --- /dev/null +++ b/src/sa/ErrorException.java @@ -0,0 +1,23 @@ +package sa; +import util.Error; + +public class ErrorException extends Exception{ + private Error error; + private String message; + + + public ErrorException(Error error, String message){ + this.error = error; + this.message = message; + } + + public String getMessage(){ + return message; + } + + public int getCode(){ + return error.code(); + } + + +} diff --git a/src/sa/SaAppel.java b/src/sa/SaAppel.java new file mode 100644 index 0000000..84a3f77 --- /dev/null +++ b/src/sa/SaAppel.java @@ -0,0 +1,33 @@ +package sa; +import ts.*; +import util.Type; + + +public class SaAppel implements SaExp, SaInst{ + private String nom; + private SaLExp arguments; + public TsItemFct tsItem; + + public SaAppel(String nom, SaLExp arguments){ + this.nom = nom; + this.arguments = arguments; + this.tsItem = null; + } + + public String getNom(){return this.nom;} + public SaLExp getArguments(){return this.arguments;} + + + public Type getType(){ + return tsItem.typeRetour; + } + + public <T> T accept(SaVisitor <T> visitor) throws Exception{ + return visitor.visit(this); + } + + @Override + public String toString() { + return "(" + this.getClass().getSimpleName()+ " " + nom + " " + arguments + ")"; + } +} diff --git a/src/sa/SaCheckLinkage.java b/src/sa/SaCheckLinkage.java new file mode 100644 index 0000000..f25f017 --- /dev/null +++ b/src/sa/SaCheckLinkage.java @@ -0,0 +1,42 @@ +package sa; + +import util.Error; + +public class SaCheckLinkage extends SaDepthFirstVisitor <Void>{ + + public Void visit(SaDecFonc node) throws Exception { + defaultIn(node); + if (node.tsItem == null) throw new ErrorException(Error.TS, "linkage error in SaDecFonc"); + if(node.getParametres() != null) node.getParametres().accept(this); + if(node.getVariable() != null) node.getVariable().accept(this); + if(node.getCorps() != null) node.getCorps().accept(this); + defaultOut(node); + return null; + + } + + public Void visit(SaDecVarSimple node) throws ErrorException { + if (node.tsItem == null) throw new ErrorException(Error.TS, "linkage error in SaDecVarSimple"); + return null; + } + + public Void visit(SaDecTab node) throws ErrorException { + if(node.tsItem == null) throw new ErrorException(Error.TS, "linkage error in SaDecTab"); + return null; + } + + public Void visit(SaAppel node) throws ErrorException{ + if(node.tsItem == null) throw new ErrorException(Error.TS, "linkage error in SaAppel"); + return null; + } + + public Void visit(SaVarSimple node) throws ErrorException{ + if(node.tsItem == null) throw new ErrorException(Error.TS, "linkage error in SaVarSimple"); + return null; + } + public Void visit(SaVarIndicee node) throws ErrorException{ + if(node.tsItem == null) throw new ErrorException(Error.TS, "linkage error in SaVarIndicee"); + return null; + } +} + diff --git a/src/sa/SaDec.java b/src/sa/SaDec.java new file mode 100644 index 0000000..275b4b6 --- /dev/null +++ b/src/sa/SaDec.java @@ -0,0 +1,5 @@ +package sa; + +public interface SaDec extends SaNode { + public String getNom(); +} diff --git a/src/sa/SaDecFonc.java b/src/sa/SaDecFonc.java new file mode 100644 index 0000000..7f4b817 --- /dev/null +++ b/src/sa/SaDecFonc.java @@ -0,0 +1,34 @@ +package sa; +import ts.*; +import util.Type; + +public class SaDecFonc implements SaDec{ + private String nom; + private SaLDecVar parametres; + private SaLDecVar variables; + private SaInst corps; + private Type typeRetour; + public TsItemFct tsItem; + + public SaDecFonc(String nom, Type typeRetour, SaLDecVar parametres, SaLDecVar variables, SaInst corps){ + this.nom = nom; + this.typeRetour = typeRetour; + this.parametres = parametres; + this.variables = variables; + this.corps = corps; + this.tsItem = null; + } + + public String getNom(){return this.nom;} + public Type getTypeRetour(){return this.typeRetour;} + public SaLDecVar getParametres(){return this.parametres;} + public SaLDecVar getVariable(){return this.variables;} + public SaInst getCorps(){return this.corps;} + + public <T> T accept(SaVisitor <T> visitor) throws Exception{ + return visitor.visit(this); + } + public String toString(){ + return "(" + this.getClass().getSimpleName() + " " + nom + " " + typeRetour + " " + parametres + " " + variables + " " + corps + ")"; + } +} diff --git a/src/sa/SaDecTab.java b/src/sa/SaDecTab.java new file mode 100644 index 0000000..8eaf209 --- /dev/null +++ b/src/sa/SaDecTab.java @@ -0,0 +1,31 @@ +package sa; +import ts.*; +import util.Type; + +public class SaDecTab implements SaDecVar{ + private String nom; + private int taille; + private Type type; + public TsItemVar tsItem; + + public SaDecTab(String nom, Type type, int taille){ + this.nom = nom; + this.type = type; + this.taille = taille; + this.tsItem = null; + } + + public String getNom(){return this.nom;} + public int getTaille(){return this.taille;} + public Type getType(){return this.type;} + public TsItemVar getTsItem(){return this.tsItem;} + public void setTsItem(TsItemVar tsItem){this.tsItem = tsItem;} + public <T> T accept(SaVisitor <T> visitor) throws Exception{ + return visitor.visit(this); + } + + @Override + public String toString() { + return "(" + this.getClass().getSimpleName() + " " + nom + " " + type + " " + taille + ")"; + } +} diff --git a/src/sa/SaDecVar.java b/src/sa/SaDecVar.java new file mode 100644 index 0000000..9cc6f02 --- /dev/null +++ b/src/sa/SaDecVar.java @@ -0,0 +1,11 @@ +package sa; +import util.Type; +import ts.*; + +public interface SaDecVar extends SaNode { + public String getNom(); + public Type getType(); + public TsItemVar getTsItem(); + public void setTsItem(TsItemVar tsItem); + +} diff --git a/src/sa/SaDecVarSimple.java b/src/sa/SaDecVarSimple.java new file mode 100644 index 0000000..557cf28 --- /dev/null +++ b/src/sa/SaDecVarSimple.java @@ -0,0 +1,29 @@ +package sa; +import ts.*; +import util.Type; + +public class SaDecVarSimple implements SaDecVar{ + private String nom; + private Type type; + public TsItemVar tsItem; + + public SaDecVarSimple(String nom, Type type){ + this.nom = nom; + this.type = type; + this.tsItem = null; + } + + public String getNom(){return this.nom;} + public Type getType(){return this.type;} + public TsItemVar getTsItem(){return this.tsItem;} + public void setTsItem(TsItemVar tsItem){this.tsItem = tsItem;} + + public <T> T accept(SaVisitor <T> visitor) throws Exception{ + return visitor.visit(this); + } + + @Override + public String toString() { + return "(" + this.getClass().getSimpleName() + " " + nom + " " + type + ")"; + } +} diff --git a/src/sa/SaDepthFirstVisitor.java b/src/sa/SaDepthFirstVisitor.java new file mode 100644 index 0000000..54796e8 --- /dev/null +++ b/src/sa/SaDepthFirstVisitor.java @@ -0,0 +1,389 @@ +package sa; + + +// P -> LDEC LDEC + +// DEC -> var id taille +// DEC -> fct id LDEC LDEC LINST +// DEC -> var id + +// LDEC -> DEC LDEC +// LDEC -> null + +// VAR ->simple id +// VAR ->indicee id EXP + +// LINST -> INST LINST +// LINST -> null + +// INST -> aff VAR EXP +// INST -> si EXP LINST LINST +// INST -> tq EXP LINST +// INST -> app APP +// INST -> ret EXP +// INST -> ecr EXP + +// APP -> id LEXP + +// LEXP -> EXP LEXP +// LEXP -> null + +// EXP -> op2 EXP EXP +// EXP -> op1 EXP +// EXP -> VAR +// EXP -> entier +// EXP -> APP +// EXP -> lire + + +//********** + +// VAR ->simple id +// VAR ->indicee id EXP + +// LINST -> INST LINST +// LINST -> null + +// INST -> aff VAR EXP +// INST -> si EXP LINST LINST +// INST -> tq EXP LINST +// INST -> app APP +// INST -> ecr EXP + +// APP -> id LEXP + +// LEXP -> EXP LEXP +// LEXP -> null + + +// EXP -> op1 EXP +// EXP -> VAR + +// EXP -> lire + + +public class SaDepthFirstVisitor <T> implements SaVisitor <T>{ + + // private NouvelleClasse x; + public void defaultIn(SaNode node) + { + } + + public void defaultOut(SaNode node) + { + } + + // P -> LDEC LDEC + public T visit(SaProg node) throws Exception + { + defaultIn(node); + if(node.getVariables() != null) + node.getVariables().accept(this); + if(node.getFonctions() != null) + node.getFonctions().accept(this); + defaultOut(node); + return null; + } + + // DEC -> var id taille + public T visit(SaDecTab node) throws Exception{ + defaultIn(node); + defaultOut(node); + return null; + } + + public T visit(SaExp node) throws Exception + { + defaultIn(node); + defaultOut(node); + return null; + } + + // EXP -> entier + public T visit(SaExpInt node) throws Exception + { + defaultIn(node); + defaultOut(node); + return null; + } + + // EXP -> vrai + public T visit(SaExpVrai node) throws Exception + { + defaultIn(node); + defaultOut(node); + return null; + } + + // EXP -> faux + public T visit(SaExpFaux node) throws Exception + { + defaultIn(node); + defaultOut(node); + return null; + } + + public T visit(SaExpVar node) throws Exception + { + defaultIn(node); + node.getVar().accept(this); + defaultOut(node); + return null; + } + + public T visit(SaInstEcriture node) throws Exception + { + defaultIn(node); + node.getArg().accept(this); + defaultOut(node); + return null; + } + + public T visit(SaInstTantQue node) throws Exception + { + defaultIn(node); + node.getTest().accept(this); + if (node.getFaire() != null) + node.getFaire().accept(this); + defaultOut(node); + return null; + } + public T visit(SaLInst node) throws Exception + { + defaultIn(node); + if(node != null){ + if(node.getTete() != null)node.getTete().accept(this); + if(node.getQueue() != null) node.getQueue().accept(this); + } + defaultOut(node); + return null; + } + + // DEC -> fct id LDEC LDEC LINST + public T visit(SaDecFonc node) throws Exception + { + defaultIn(node); + if(node.getParametres() != null) node.getParametres().accept(this); + if(node.getVariable() != null) node.getVariable().accept(this); + if(node.getCorps() != null) node.getCorps().accept(this); + defaultOut(node); + return null; + } + + // DEC -> var id + public T visit(SaDecVar node) throws Exception + { + defaultIn(node); + defaultOut(node); + return null; + } + + public T visit(SaInstAffect node) throws Exception + { + defaultIn(node); + node.getLhs().accept(this); + node.getRhs().accept(this); + defaultOut(node); + return null; + } + + // LDEC -> DEC LDEC + // LDEC -> null + /* public T visit(SaLDec node) throws Exception + { + defaultIn(node); + node.getTete().accept(this); + if(node.getQueue() != null) node.getQueue().accept(this); + defaultOut(node); + return null; + }*/ + + public T visit(SaLDecVar node) throws Exception + { + defaultIn(node); + node.getTete().accept(this); + if(node.getQueue() != null) node.getQueue().accept(this); + defaultOut(node); + return null; + } + + public T visit(SaLDecFonc node) throws Exception + { + defaultIn(node); + node.getTete().accept(this); + if(node.getQueue() != null) node.getQueue().accept(this); + defaultOut(node); + return null; + } + + public T visit(SaVarSimple node) throws Exception + { + defaultIn(node); + defaultOut(node); + return null; + } + + public T visit(SaAppel node) throws Exception + { + defaultIn(node); + if(node.getArguments() != null) node.getArguments().accept(this); + defaultOut(node); + return null; + } + + public T visit(SaExpAppel node) throws Exception + { + defaultIn(node); + node.getVal().accept(this); + defaultOut(node); + return null; + } + + // EXP -> add EXP EXP + public T visit(SaExpAdd node) throws Exception + { + defaultIn(node); + node.getOp1().accept(this); + node.getOp2().accept(this); + defaultOut(node); + return null; + } + + // EXP -> sub EXP EXP + public T visit(SaExpSub node) throws Exception + { + defaultIn(node); + node.getOp1().accept(this); + node.getOp2().accept(this); + defaultOut(node); + return null; + } + + // EXP -> mult EXP EXP + public T visit(SaExpMult node) throws Exception + { + defaultIn(node); + node.getOp1().accept(this); + node.getOp2().accept(this); + defaultOut(node); + return null; + } + + // EXP -> div EXP EXP + public T visit(SaExpDiv node) throws Exception + { + defaultIn(node); + node.getOp1().accept(this); + node.getOp2().accept(this); + defaultOut(node); + return null; + } + + // EXP -> inf EXP EXP + public T visit(SaExpInf node) throws Exception + { + defaultIn(node); + node.getOp1().accept(this); + node.getOp2().accept(this); + defaultOut(node); + return null; + } + + // EXP -> eq EXP EXP + public T visit(SaExpEqual node) throws Exception + { + defaultIn(node); + node.getOp1().accept(this); + node.getOp2().accept(this); + defaultOut(node); + return null; + } + + // EXP -> and EXP EXP + public T visit(SaExpAnd node) throws Exception + { + defaultIn(node); + node.getOp1().accept(this); + node.getOp2().accept(this); + defaultOut(node); + return null; + } + + + // EXP -> or EXP EXP + public T visit(SaExpOr node) throws Exception + { + defaultIn(node); + node.getOp1().accept(this); + node.getOp2().accept(this); + defaultOut(node); + return null; + } + + // EXP -> not EXP + public T visit(SaExpNot node) throws Exception + { + defaultIn(node); + node.getOp1().accept(this); + defaultOut(node); + return null; + } + + + public T visit(SaExpLire node) throws Exception + { + defaultIn(node); + defaultOut(node); + return null; + } + + public T visit(SaInstBloc node) throws Exception + { + defaultIn(node); + if ( node.getVal() != null ) + { + node.getVal().accept(this); + } + defaultOut(node); + return null; + } + + public T visit(SaInstSi node) throws Exception + { + defaultIn(node); + node.getTest().accept(this); + if (node.getAlors() != null) + node.getAlors().accept(this); + if(node.getSinon() != null) node.getSinon().accept(this); + defaultOut(node); + return null; + } + +// INST -> ret EXP + public T visit(SaInstRetour node) throws Exception + { + defaultIn(node); + node.getVal().accept(this); + defaultOut(node); + return null; + } + + + public T visit(SaLExp node) throws Exception + { + defaultIn(node); + node.getTete().accept(this); + if(node.getQueue() != null) + node.getQueue().accept(this); + defaultOut(node); + return null; + } + public T visit(SaVarIndicee node) throws Exception + { + defaultIn(node); + node.getIndice().accept(this); + defaultOut(node); + return null; + } + +} diff --git a/src/sa/SaExp.java b/src/sa/SaExp.java new file mode 100644 index 0000000..81cbf3c --- /dev/null +++ b/src/sa/SaExp.java @@ -0,0 +1,6 @@ +package sa; +import util.Type; + +public interface SaExp extends SaNode { + public Type getType(); +} diff --git a/src/sa/SaExpAdd.java b/src/sa/SaExpAdd.java new file mode 100644 index 0000000..0041eed --- /dev/null +++ b/src/sa/SaExpAdd.java @@ -0,0 +1,26 @@ +package sa; +import util.Type; + +public class SaExpAdd implements SaExp{ + private SaExp op1; + private SaExp op2; + + public SaExpAdd(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); + } + public String toString(){ + return "(" + this.getClass().getSimpleName() + " " + op1 + " " + op2 + ")"; + } +} diff --git a/src/sa/SaExpAnd.java b/src/sa/SaExpAnd.java new file mode 100644 index 0000000..df94c5b --- /dev/null +++ b/src/sa/SaExpAnd.java @@ -0,0 +1,26 @@ +package sa; +import util.Type; + +public class SaExpAnd implements SaExp{ + private SaExp op1; + private SaExp op2; + + public SaExpAnd(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.BOOL; + } + + public <T> T accept(SaVisitor <T> visitor) throws Exception{ + return visitor.visit(this); + } + public String toString() { + return "(" + this.getClass().getSimpleName() + " " + op1 + " " + op2 + ")"; + } +} diff --git a/src/sa/SaExpAppel.java b/src/sa/SaExpAppel.java new file mode 100644 index 0000000..611aa6a --- /dev/null +++ b/src/sa/SaExpAppel.java @@ -0,0 +1,25 @@ +package sa; +import util.Type; + +public class SaExpAppel implements SaExp{ + private SaAppel val; + + public SaExpAppel(SaAppel val){ + this.val = val; + } + + public SaAppel getVal(){return this.val;} + + public Type getType(){ + return val.getType(); + } + + public <T> T accept(SaVisitor <T> visitor) throws Exception{ + return visitor.visit(this); + } + + @Override + public String toString() { + return "(" + this.getClass().getSimpleName() + " " + val + ")"; + } +} diff --git a/src/sa/SaExpDiv.java b/src/sa/SaExpDiv.java new file mode 100644 index 0000000..cc2db6e --- /dev/null +++ b/src/sa/SaExpDiv.java @@ -0,0 +1,27 @@ +package sa; +import util.Type; + +public class SaExpDiv implements SaExp{ + private SaExp op1; + private SaExp op2; + + public SaExpDiv(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 + ")"; + } +} diff --git a/src/sa/SaExpEqual.java b/src/sa/SaExpEqual.java new file mode 100644 index 0000000..01eeb01 --- /dev/null +++ b/src/sa/SaExpEqual.java @@ -0,0 +1,27 @@ +package sa; +import util.Type; + +public class SaExpEqual implements SaExp{ + private SaExp op1; + private SaExp op2; + + public SaExpEqual(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.BOOL; + } + public <T> T accept(SaVisitor <T> visitor) throws Exception{ + return visitor.visit(this); + } + + @Override + public String toString() { + return "(" + this.getClass().getSimpleName() + " " + op1 + " " + op2 + ")"; + } +} diff --git a/src/sa/SaExpFaux.java b/src/sa/SaExpFaux.java new file mode 100644 index 0000000..125956f --- /dev/null +++ b/src/sa/SaExpFaux.java @@ -0,0 +1,28 @@ +package sa; +import util.Type; + +public class SaExpFaux implements SaExp{ + private final boolean val; + + public SaExpFaux(){ + this.val = false; + } + + public boolean getVal(){ + return this.val; + } + + public Type getType(){ + return Type.BOOL; + } + + + public <T> T accept(SaVisitor <T> visitor) throws Exception{ + return visitor.visit(this); + } + + @Override + public String toString() { + return "faux"; + } +} diff --git a/src/sa/SaExpInf.java b/src/sa/SaExpInf.java new file mode 100644 index 0000000..1f7c54a --- /dev/null +++ b/src/sa/SaExpInf.java @@ -0,0 +1,28 @@ +package sa; +import util.Type; + +public class SaExpInf implements SaExp{ + private SaExp op1; + private SaExp op2; + + public SaExpInf(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.BOOL; + } + + public <T> T accept(SaVisitor <T> visitor) throws Exception { + return visitor.visit(this); + } + + @Override + public String toString() { + return "(" + this.getClass().getSimpleName() + " " + op1 + " " + op2 + ")"; + } +} diff --git a/src/sa/SaExpInt.java b/src/sa/SaExpInt.java new file mode 100644 index 0000000..10bd6d6 --- /dev/null +++ b/src/sa/SaExpInt.java @@ -0,0 +1,26 @@ +package sa; +import util.Type; + +public class SaExpInt implements SaExp{ + private int val; + + public SaExpInt(int val){ + this.val = val; + } + + public int getVal(){ + return this.val; + } + + public Type getType(){ + return Type.ENTIER; + } + public <T> T accept(SaVisitor <T> visitor) throws Exception{ + return visitor.visit(this); + } + + @Override + public String toString() { + return Integer.toString(val); + } +} diff --git a/src/sa/SaExpLire.java b/src/sa/SaExpLire.java new file mode 100644 index 0000000..53d8520 --- /dev/null +++ b/src/sa/SaExpLire.java @@ -0,0 +1,21 @@ +package sa; +import util.Type; + +public class SaExpLire implements SaExp{ + + public SaExpLire(){ + } + + 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(); + } +} diff --git a/src/sa/SaExpMult.java b/src/sa/SaExpMult.java new file mode 100644 index 0000000..597038d --- /dev/null +++ b/src/sa/SaExpMult.java @@ -0,0 +1,27 @@ +package sa; +import util.Type; + +public class SaExpMult implements SaExp{ + private SaExp op1; + private SaExp op2; + + public SaExpMult(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 + ")"; + } +} diff --git a/src/sa/SaExpNot.java b/src/sa/SaExpNot.java new file mode 100644 index 0000000..60a1c08 --- /dev/null +++ b/src/sa/SaExpNot.java @@ -0,0 +1,26 @@ +package sa; +import util.Type; + +public class SaExpNot implements SaExp{ + private SaExp op1; + + public SaExpNot(SaExp op1){ + this.op1 = op1; + } + + public SaExp getOp1(){return this.op1;} + public SaExp getOp2(){return null;} + + public Type getType(){ + return Type.BOOL; + } + + public <T> T accept(SaVisitor <T> visitor) throws Exception{ + return visitor.visit(this); + } + + @Override + public String toString() { + return "(" + this.getClass().getSimpleName() + " " + op1 + ")"; + } +} diff --git a/src/sa/SaExpOr.java b/src/sa/SaExpOr.java new file mode 100644 index 0000000..ec9807d --- /dev/null +++ b/src/sa/SaExpOr.java @@ -0,0 +1,28 @@ +package sa; +import util.Type; + +public class SaExpOr implements SaExp{ + private SaExp op1; + private SaExp op2; + + public SaExpOr(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.BOOL; + } + + public <T> T accept(SaVisitor <T> visitor) throws Exception{ + return visitor.visit(this); + } + + @Override + public String toString() { + return "(" + this.getClass().getSimpleName() + " " + op1 + " " + op2 + ")"; + } +} diff --git a/src/sa/SaExpSub.java b/src/sa/SaExpSub.java new file mode 100644 index 0000000..bf02733 --- /dev/null +++ b/src/sa/SaExpSub.java @@ -0,0 +1,28 @@ +package sa; +import util.Type; + +public class SaExpSub implements SaExp{ + private SaExp op1; + private SaExp op2; + + public SaExpSub(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 + ")"; + } +} diff --git a/src/sa/SaExpVar.java b/src/sa/SaExpVar.java new file mode 100644 index 0000000..f983c1f --- /dev/null +++ b/src/sa/SaExpVar.java @@ -0,0 +1,24 @@ +package sa; +import util.Type; + +public class SaExpVar implements SaExp{ + private SaVar var; + + public SaExpVar(SaVar var){ + this.var = var; + } + + public SaVar getVar(){return this.var;} + + public Type getType(){ + return this.var.getTsItem().getType(); + } + public <T> T accept(SaVisitor <T> visitor) throws Exception{ + return visitor.visit(this); + } + + @Override + public String toString() { + return "(" + this.getClass().getSimpleName() + " " + var + ")"; + } +} diff --git a/src/sa/SaExpVrai.java b/src/sa/SaExpVrai.java new file mode 100644 index 0000000..ef99211 --- /dev/null +++ b/src/sa/SaExpVrai.java @@ -0,0 +1,27 @@ +package sa; +import util.Type; + +public class SaExpVrai implements SaExp{ + private final boolean val; + + public SaExpVrai(){ + this.val = true; + } + + public boolean getVal(){ + return this.val; + } + + public Type getType(){ + return Type.BOOL; + } + + public <T> T accept(SaVisitor <T> visitor) throws Exception{ + return visitor.visit(this); + } + + @Override + public String toString() { + return "vrai"; + } +} diff --git a/src/sa/SaInst.java b/src/sa/SaInst.java new file mode 100644 index 0000000..409ef53 --- /dev/null +++ b/src/sa/SaInst.java @@ -0,0 +1,5 @@ +package sa; + +public interface SaInst extends SaNode { + +} diff --git a/src/sa/SaInstAffect.java b/src/sa/SaInstAffect.java new file mode 100644 index 0000000..126f741 --- /dev/null +++ b/src/sa/SaInstAffect.java @@ -0,0 +1,22 @@ +package sa; + +public class SaInstAffect implements SaInst{ + private SaVar lhs; + private SaExp rhs; + + public SaInstAffect(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 + ")"; + } +} diff --git a/src/sa/SaInstBloc.java b/src/sa/SaInstBloc.java new file mode 100644 index 0000000..8f13f49 --- /dev/null +++ b/src/sa/SaInstBloc.java @@ -0,0 +1,19 @@ +package sa; + +public class SaInstBloc implements SaInst{ + private SaLInst val; + + public SaInstBloc(SaLInst val){ + this.val = val; + } + + public SaLInst getVal(){return this.val;} + public <T> T accept(SaVisitor <T> visitor) throws Exception{ + return visitor.visit(this); + } + + @Override + public String toString() { + return "(" + this.getClass().getSimpleName() + " " + val + ")"; + } +} diff --git a/src/sa/SaInstEcriture.java b/src/sa/SaInstEcriture.java new file mode 100644 index 0000000..4ba2af3 --- /dev/null +++ b/src/sa/SaInstEcriture.java @@ -0,0 +1,20 @@ +package sa; + +public class SaInstEcriture implements SaInst{ + private SaExp arg; + + public SaInstEcriture(SaExp arg){ + this.arg = arg; + } + + public SaExp getArg(){return this.arg;} + + public <T> T accept(SaVisitor <T> visitor) throws Exception{ + return visitor.visit(this); + } + + @Override + public String toString() { + return "(" + this.getClass().getSimpleName() + " " + arg + ")"; + } +} diff --git a/src/sa/SaInstRetour.java b/src/sa/SaInstRetour.java new file mode 100644 index 0000000..c3993e8 --- /dev/null +++ b/src/sa/SaInstRetour.java @@ -0,0 +1,24 @@ +package sa; +import util.Type; + +public class SaInstRetour implements SaInst{ + private SaExp val; + + public SaInstRetour(SaExp val){ + this.val = val; + } + + public Type getType(){ + return val.getType(); + } + public SaExp getVal(){return this.val;} + + public <T> T accept(SaVisitor <T> visitor) throws Exception{ + return visitor.visit(this); + } + + @Override + public String toString() { + return "(" + this.getClass().getSimpleName() + " " + val + ")"; + } +} diff --git a/src/sa/SaInstSi.java b/src/sa/SaInstSi.java new file mode 100644 index 0000000..fc8c1d7 --- /dev/null +++ b/src/sa/SaInstSi.java @@ -0,0 +1,26 @@ +package sa; + +public class SaInstSi implements SaInst{ + private SaExp test; + private SaInst alors; + private SaInst sinon; + + public SaInstSi(SaExp test, SaInst alors, SaInst sinon){ + this.test = test; + this.alors = alors; + this.sinon = sinon; + } + + public SaExp getTest(){return this.test;} + public SaInst getAlors(){return this.alors;} + public SaInst getSinon(){return this.sinon;} + + public <T> T accept(SaVisitor <T> visitor) throws Exception{ + return visitor.visit(this); + } + + @Override + public String toString() { + return "(" + this.getClass().getSimpleName() + " " + test + " " + alors + " " + sinon + ")"; + } +} diff --git a/src/sa/SaInstTantQue.java b/src/sa/SaInstTantQue.java new file mode 100644 index 0000000..dfc87e1 --- /dev/null +++ b/src/sa/SaInstTantQue.java @@ -0,0 +1,23 @@ +package sa; + +public class SaInstTantQue implements SaInst{ + private SaExp test; + private SaInst faire; + + public SaInstTantQue(SaExp test, SaInst faire){ + this.test = test; + this.faire = faire; + } + + public SaExp getTest(){return this.test;} + public SaInst getFaire(){return this.faire;} + + public <T> T accept(SaVisitor <T> visitor) throws Exception{ + return visitor.visit(this); + } + + @Override + public String toString() { + return "(" + this.getClass().getSimpleName() + " " + test + " " + faire + ")"; + } +} diff --git a/src/sa/SaLDecFonc.java b/src/sa/SaLDecFonc.java new file mode 100644 index 0000000..73135ae --- /dev/null +++ b/src/sa/SaLDecFonc.java @@ -0,0 +1,28 @@ +package sa; + +public class SaLDecFonc implements SaNode{ + private SaDecFonc tete; + private SaLDecFonc queue; + + public SaLDecFonc(SaDecFonc tete, SaLDecFonc queue){ + this.tete = tete; + this.queue = queue; + } + + public SaDecFonc getTete(){return this.tete;} + public SaLDecFonc getQueue(){return this.queue;} + + public int length(){ + if(this.queue == null) return 1; + return 1 + this.queue.length() ; + } + + public <T> T accept(SaVisitor <T> visitor) throws Exception{ + return visitor.visit(this); + } + + public String toString() { + return "(" + this.getClass().getSimpleName() + " " + tete + " " + queue + ")"; + } + +} diff --git a/src/sa/SaLDecVar.java b/src/sa/SaLDecVar.java new file mode 100644 index 0000000..9f69dc3 --- /dev/null +++ b/src/sa/SaLDecVar.java @@ -0,0 +1,27 @@ +package sa; + +public class SaLDecVar implements SaNode{ + private SaDecVar tete; + private SaLDecVar queue; + + public SaLDecVar(SaDecVar tete, SaLDecVar queue){ + this.tete = tete; + this.queue = queue; + } + + public SaDecVar getTete(){return this.tete;} + public SaLDecVar getQueue(){return this.queue;} + + public int length(){ + if(this.queue == null) return 1; + return 1 + this.queue.length() ; + } + + public <T> T accept(SaVisitor <T> visitor) throws Exception { + return visitor.visit(this); + } + public String toString(){ + return "(" + this.getClass().getSimpleName() + " " + tete + " " + queue + ")"; + } + +} diff --git a/src/sa/SaLExp.java b/src/sa/SaLExp.java new file mode 100644 index 0000000..a6e489f --- /dev/null +++ b/src/sa/SaLExp.java @@ -0,0 +1,30 @@ +package sa; + +public class SaLExp implements SaNode{ + private SaExp tete; + private SaLExp queue; + + public SaLExp(SaExp tete, SaLExp queue){ + this.tete = tete; + this.queue = queue; + } + + public SaExp getTete(){return this.tete;} + public SaLExp getQueue(){return this.queue;} + + public int length(){ + if(this.queue == null) return 1; + return 1 + this.queue.length() ; + } + + + + public <T> T accept(SaVisitor <T> visitor) throws Exception{ + return visitor.visit(this); + } + + @Override + public String toString() { + return "(" + this.getClass().getSimpleName() + " " + tete + " " + queue + ")"; + } +} diff --git a/src/sa/SaLInst.java b/src/sa/SaLInst.java new file mode 100644 index 0000000..29c7cd0 --- /dev/null +++ b/src/sa/SaLInst.java @@ -0,0 +1,23 @@ +package sa; + +public class SaLInst implements SaNode{ + private SaInst tete; + private SaLInst queue; + + public SaLInst(SaInst tete, SaLInst queue){ + this.tete = tete; + this.queue = queue; + } + + public SaInst getTete(){return this.tete;} + public SaLInst getQueue(){return this.queue;} + + public <T> T accept(SaVisitor <T> visitor) throws Exception{ + return visitor.visit(this); + } + + public String toString(){ + return "(" + this.getClass().getSimpleName() + " " + tete + " " + queue +")"; + } + +} diff --git a/src/sa/SaNode.java b/src/sa/SaNode.java new file mode 100644 index 0000000..cd65a01 --- /dev/null +++ b/src/sa/SaNode.java @@ -0,0 +1,6 @@ +package sa; + +public interface SaNode{ + public <T> T accept(SaVisitor <T> visitor) throws Exception; +} + diff --git a/src/sa/SaProg.java b/src/sa/SaProg.java new file mode 100644 index 0000000..f0081db --- /dev/null +++ b/src/sa/SaProg.java @@ -0,0 +1,21 @@ +package sa; + +public class SaProg implements SaNode{ + private SaLDecVar variables; + private SaLDecFonc fonctions; + + public SaProg(SaLDecVar variables, SaLDecFonc fonctions){ + this.variables = variables; + this.fonctions = fonctions; + } + + public SaLDecVar getVariables(){return this.variables;} + public SaLDecFonc getFonctions(){return this.fonctions;} + + public <T> T accept(SaVisitor <T> visitor) throws Exception{ + return visitor.visit(this); + } + public String toString(){ + return "(" + this.getClass().getSimpleName() + " " + variables + " " + fonctions +")"; + } +} diff --git a/src/sa/SaTypeCheck.java b/src/sa/SaTypeCheck.java new file mode 100644 index 0000000..dcae416 --- /dev/null +++ b/src/sa/SaTypeCheck.java @@ -0,0 +1,42 @@ +package sa; +import util.Type; +import util.Error; +import ts.*; + +public class SaTypeCheck extends SaDepthFirstVisitor <Void>{ + private TsItemFct fonctionCourante; + + public SaTypeCheck(SaNode root) + { + try{ + root.accept(this); + } catch(ErrorException e){ + System.err.print("ERREUR DE TYPAGE : "); + System.err.println(e.getMessage()); + System.exit(e.getCode()); + } catch(Exception e){} + } + + public void defaultIn(SaNode node) + { + // System.out.println("<" + node.getClass().getSimpleName() + ">"); + } + + public void defaultOut(SaNode node) + { + // System.out.println("</" + node.getClass().getSimpleName() + ">"); + } + + public Void visit(SaExpAdd node) throws Exception + { + defaultIn(node); + node.getOp1().accept(this); + node.getOp2().accept(this); + if(!Type.checkCompatibility(node.getOp1().getType(), Type.ENTIER) || + !Type.checkCompatibility(node.getOp2().getType(),Type.ENTIER)) + throw new ErrorException(Error.TYPE, "exp addition"); + defaultOut(node); + return null; + } + +} diff --git a/src/sa/SaVar.java b/src/sa/SaVar.java new file mode 100644 index 0000000..8e5536e --- /dev/null +++ b/src/sa/SaVar.java @@ -0,0 +1,6 @@ +package sa; +import ts.*; + +public interface SaVar extends SaNode { + TsItemVar getTsItem(); +} diff --git a/src/sa/SaVarIndicee.java b/src/sa/SaVarIndicee.java new file mode 100644 index 0000000..d125b11 --- /dev/null +++ b/src/sa/SaVarIndicee.java @@ -0,0 +1,27 @@ +package sa; +import ts.*; + +public class SaVarIndicee implements SaVar{ + private String nom; + private SaExp indice; + public TsItemVar tsItem; + + public SaVarIndicee(String nom, SaExp indice){ + this.nom = nom; + this.indice = indice; + this.tsItem = null; + } + + public TsItemVar getTsItem(){return this.tsItem;} + public String getNom(){return this.nom;} + public SaExp getIndice(){return this.indice;} + + public <T> T accept(SaVisitor <T> visitor) throws Exception{ + return visitor.visit(this); + } + + @Override + public String toString() { + return "(" + this.getClass().getSimpleName() + " " + nom + " " + indice +")"; + } +} diff --git a/src/sa/SaVarSimple.java b/src/sa/SaVarSimple.java new file mode 100644 index 0000000..d496e74 --- /dev/null +++ b/src/sa/SaVarSimple.java @@ -0,0 +1,24 @@ +package sa; +import ts.*; + +public class SaVarSimple implements SaVar{ + public String nom; + public TsItemVarSimple tsItem; + + public SaVarSimple(String nom){ + this.nom = nom; + this.tsItem = null; + } + + public String getNom(){return this.nom;} + public TsItemVarSimple getTsItem(){return this.tsItem;} + + public <T> T accept(SaVisitor <T> visitor) throws Exception{ + return visitor.visit(this); + } + + @Override + public String toString() { + return "(" + this.getClass().getSimpleName()+ " " + nom + ")"; + } +} diff --git a/src/sa/SaVisitor.java b/src/sa/SaVisitor.java new file mode 100644 index 0000000..3d28bb7 --- /dev/null +++ b/src/sa/SaVisitor.java @@ -0,0 +1,38 @@ +package sa; + +interface SaVisitor <T> { + public T visit(SaProg node) throws Exception; + public T visit(SaInstEcriture node) throws Exception; + public T visit(SaInstTantQue node) throws Exception; + public T visit(SaInstBloc node) throws Exception; + public T visit(SaInstSi node) throws Exception; + public T visit(SaInstAffect node) throws Exception; + public T visit(SaInstRetour node) throws Exception; + public T visit(SaLInst node) throws Exception; + public T visit(SaDecFonc node) throws Exception; + public T visit(SaDecVar node) throws Exception; + public T visit(SaDecTab node) throws Exception; + // public T visit(SaLDec node) throws Exception; + public T visit(SaLDecVar node) throws Exception; + public T visit(SaLDecFonc node) throws Exception; + public T visit(SaVarSimple node) throws Exception; + public T visit(SaVarIndicee node) throws Exception; + public T visit(SaAppel node) throws Exception; + public T visit(SaExp node) throws Exception; + public T visit(SaExpLire node) throws Exception; + public T visit(SaExpInt node) throws Exception; + public T visit(SaExpVrai node) throws Exception; + public T visit(SaExpFaux node) throws Exception; + public T visit(SaExpVar node) throws Exception; + public T visit(SaExpAppel node) throws Exception; + public T visit(SaExpAdd node) throws Exception; + public T visit(SaExpSub node) throws Exception; + public T visit(SaExpMult node) throws Exception; + public T visit(SaExpDiv node) throws Exception; + public T visit(SaExpInf node) throws Exception; + public T visit(SaExpEqual node) throws Exception; + public T visit(SaExpAnd node) throws Exception; + public T visit(SaExpOr node) throws Exception; + public T visit(SaExpNot node) throws Exception; + public T visit(SaLExp node) throws Exception; +} diff --git a/src/sa/Sc2sa.java b/src/sa/Sc2sa.java new file mode 100644 index 0000000..3d5c426 --- /dev/null +++ b/src/sa/Sc2sa.java @@ -0,0 +1,28 @@ +package sa; +import lParser.analysis.*; +import lParser.node.*; +import util.Type; + +public class Sc2sa extends DepthFirstAdapter +{ + private SaNode returnValue; + private Type returnType; + private SaProg saRoot = null; + + public void defaultIn(@SuppressWarnings("unused") Node node) + { + //System.out.println("<" + node.getClass().getSimpleName() + ">"); + } + + public void defaultOut(@SuppressWarnings("unused") Node node) + { + //System.out.println("</" + node.getClass().getSimpleName() + ">"); + } + + public SaProg getRoot() + { + return this.saRoot; + } + + +} diff --git a/src/sc/Sc2Xml.java b/src/sc/Sc2Xml.java new file mode 100644 index 0000000..a70781c --- /dev/null +++ b/src/sc/Sc2Xml.java @@ -0,0 +1,48 @@ +package sc; +import java.io.*; +import lParser.analysis.*; +import lParser.node.*; + +public class Sc2Xml extends DepthFirstAdapter +{ + private int indentation; + private String baseFileName; + private String fileName; + private PrintStream out; + + public Sc2Xml(String baseFileName) + { + + if (baseFileName == null){ + this.out = System.out; + + } + else{ + try { + this.baseFileName = baseFileName; + this.fileName = baseFileName + ".sc"; + this.out = new PrintStream(this.fileName); + } + + catch (IOException e) { + System.err.println("Error: " + e.getMessage()); + } + } + } + + + public void defaultIn(@SuppressWarnings("unused") Node node) + { + for(int i = 0; i < this.indentation; i++){this.out.print(" ");} + this.indentation++; + this.out.println("<" + node.getClass().getSimpleName() + ">"); + } + + public void defaultOut(@SuppressWarnings("unused") Node node) + { + this.indentation--; + for(int i = 0; i < this.indentation; i++){this.out.print(" ");} + this.out.println("</" + node.getClass().getSimpleName() + ">"); + } + +} diff --git a/src/ts/Sa2ts.java b/src/ts/Sa2ts.java new file mode 100644 index 0000000..d069f80 --- /dev/null +++ b/src/ts/Sa2ts.java @@ -0,0 +1,35 @@ +package ts; +import sa.*; +import util.Error; + +public class Sa2ts extends SaDepthFirstVisitor <Void> { + enum Context { + LOCAL, + GLOBAL, + PARAM + } + + private Ts tableGlobale; + private Ts tableLocaleCourante; + private Context context; + + public Ts getTableGlobale(){return this.tableGlobale;} + + public Sa2ts() + { + tableGlobale = new Ts(); + tableLocaleCourante = null; + context = Context.GLOBAL; + } + + public void defaultIn(SaNode node) + { + // System.out.println("<" + node.getClass().getSimpleName() + ">"); + } + + public void defaultOut(SaNode node) + { + // System.out.println("</" + node.getClass().getSimpleName() + ">"); + } + +} diff --git a/src/ts/Ts.java b/src/ts/Ts.java new file mode 100644 index 0000000..d95fb75 --- /dev/null +++ b/src/ts/Ts.java @@ -0,0 +1,147 @@ +package ts; +import java.io.*; +import java.util.*; +import sa.*; +import util.Type; + +public class Ts +{ + public Map< String, TsItemFct> fonctions; + public Map< String, TsItemVar> variables; + private int adrVarCourante; + private int adrArgCourante; + + public int getAdrVarCourante(){return adrVarCourante;} + public int getAdrArgCourante(){return adrArgCourante;} + + public Ts(){ + this.fonctions = new HashMap< String, TsItemFct>(); + this.variables = new HashMap< String, TsItemVar>(); + this.adrVarCourante = 0; + this.adrArgCourante = 0; + } + + public TsItemVar addVar(String identif, Type type) + { + TsItemVar item = new TsItemVarSimple(identif, type); + item.portee = this; + item.adresse = this.adrVarCourante; + item.isParam = false; + this.adrVarCourante += type.taille(); + this.variables.put(identif, item); + return item; + } + + public TsItemVar addParam(String identif, Type type) + { + TsItemVar item = new TsItemVarSimple(identif, type); + item.portee = this; + item.adresse = this.adrArgCourante; + item.isParam = true; + this.adrArgCourante += type.taille(); + this.variables.put(identif, item); + return item; + } + + public TsItemVar addTab(String identif, Type type, int taille) + { + TsItemVar item = new TsItemVarTab(identif, type, taille); + item.portee = this; + item.adresse = this.adrVarCourante; + this.adrVarCourante += taille * type.taille(); + this.variables.put(identif, item); + return item; + } + + public TsItemFct addFct(String identif, Type typeRetour, int nbArgs, Ts table, SaDecFonc saDecFonc) + { + TsItemFct item = new TsItemFct(identif, typeRetour, nbArgs, table, saDecFonc); + this.fonctions.put(identif, item); + return item; + } + + public TsItemVar getVar(String identif) + { + return variables.get(identif); + } + + public TsItemFct getFct(String identif) + { + return fonctions.get(identif); + } + + public Ts getTableLocale(String identif){ + TsItemFct item = this.fonctions.get(identif); + if(item == null) + return null; + else + return item.getTable(); + } + + + public void affiche(PrintStream out){ + ArrayList< Map.Entry< String, TsItemVar> > stVar = new ArrayList<Map.Entry< String, TsItemVar>>(this.variables.entrySet()); + Collections.sort(stVar, new Comparator<Map.Entry< String, TsItemVar>>() { + @Override + public int compare(Map.Entry< String, TsItemVar> entry1, Map.Entry< String, TsItemVar> entry2) + { + return entry1.getKey().compareTo(entry2.getKey()); + } + }); + for (Map.Entry< String, TsItemVar> me:stVar){ + out.println(me.getValue()); + // out.println(me.getKey() + ":\t" + me.getValue()); + } + + ArrayList< Map.Entry< String, TsItemFct> > stFct = new ArrayList<Map.Entry< String, TsItemFct>>(this.fonctions.entrySet()); + Collections.sort(stFct, new Comparator<Map.Entry< String, TsItemFct>>() { + @Override + public int compare(Map.Entry< String, TsItemFct> entry1, Map.Entry< String, TsItemFct> entry2) + { + return entry1.getKey().compareTo(entry2.getKey()); + } + }); + for (Map.Entry< String, TsItemFct> me:stFct){ + out.println(me.getValue()); + // out.println(me.getKey() + ":\t" + me.getValue()); + } + } + + public void afficheTablesLocales(PrintStream out){ + ArrayList< Map.Entry< String, TsItemFct> > st = new ArrayList<Map.Entry< String, TsItemFct>>(this.fonctions.entrySet()); + Collections.sort(st, new Comparator<Map.Entry< String, TsItemFct>>() { + @Override + public int compare(Map.Entry< String, TsItemFct> entry1, Map.Entry< String, TsItemFct> entry2) + { + return entry1.getKey().compareTo(entry2.getKey()); + } + }); + for (Map.Entry< String, TsItemFct> me:st){ + if(me.getValue().getTable() != null){ + out.println("TABLE LOCALE : " + me.getKey()); + me.getValue().getTable().affiche(out); + } + } + } + + public void afficheTout(String baseFileName){ + String fileName; + PrintStream out = System.out; + + if (baseFileName != null){ + try { + baseFileName = baseFileName; + fileName = baseFileName + ".ts"; + out = new PrintStream(fileName); + } + + catch (IOException e) { + System.err.println("Error: " + e.getMessage()); + } + } + out.println("TABLE GLOBALE"); + this.affiche(out); + this.afficheTablesLocales(out); + } + +} diff --git a/src/ts/TsItemFct.java b/src/ts/TsItemFct.java new file mode 100644 index 0000000..03e87f6 --- /dev/null +++ b/src/ts/TsItemFct.java @@ -0,0 +1,30 @@ +package ts; +import sa.*; +import util.Type; + + +public class TsItemFct { + public String identif; + public int nbArgs; + public Ts table; + public SaDecFonc saDecFonc; + public Type typeRetour; + + public TsItemFct(String identif, Type typeRetour, int nbArgs, Ts table, SaDecFonc saDecFonc){ + this.identif = identif; + this.nbArgs = nbArgs; + this.table = table; + this.saDecFonc = saDecFonc; + this.typeRetour = typeRetour; + } + + public int getNbArgs(){return this.nbArgs;} + public Ts getTable(){return this.table;} + public Type getTypeRetour(){return this.typeRetour;} + public String getIdentif(){return this.identif;} + public String toString(){ + return this.identif + "\tFCT\t" + typeRetour + "\t" + this.nbArgs; + } + +} + diff --git a/src/ts/TsItemVar.java b/src/ts/TsItemVar.java new file mode 100644 index 0000000..7dae2bb --- /dev/null +++ b/src/ts/TsItemVar.java @@ -0,0 +1,20 @@ +package ts; +import util.Type; + +public abstract class TsItemVar { + public Ts portee; + public int adresse; + public String identif; + public int taille; + public boolean isParam; + public Type type; + + public int getAdresse(){return this.adresse;} + public String getIdentif(){return this.identif;} + public Ts getPortee() {return this.portee;} + public Type getType() {return this.type;} + public int getTaille(){return this.taille;} + + +} + diff --git a/src/ts/TsItemVarSimple.java b/src/ts/TsItemVarSimple.java new file mode 100644 index 0000000..ae2a555 --- /dev/null +++ b/src/ts/TsItemVarSimple.java @@ -0,0 +1,22 @@ +package ts; +import util.Type; + +public class TsItemVarSimple extends TsItemVar{ + public TsItemVarSimple(String identif, Type type){ + this.identif = identif; + this.adresse = 0; + this.portee = null; + this.taille = 1; + this.isParam = false; + this.type = type; + } + + public String toString(){ + if(this.isParam) + return this.identif + "\tPARAM\t" + this.type + "\t" + this.adresse; + else + return this.identif + "\tVAR \t" + this.type + "\t" + this.adresse; + } + +} + diff --git a/src/ts/TsItemVarTab.java b/src/ts/TsItemVarTab.java new file mode 100644 index 0000000..56c0a11 --- /dev/null +++ b/src/ts/TsItemVarTab.java @@ -0,0 +1,19 @@ +package ts; +import util.Type; + +public class TsItemVarTab extends TsItemVar{ + public TsItemVarTab(String identif, Type type, int taille){ + this.identif = identif; + this.adresse = 0; + this.portee = null; + this.taille = taille; + this.isParam = false; + this.type = type; + } + + public String toString(){ + return this.identif + "\tTAB \t" + this.type + "\t" + this.adresse + "\t" + this.taille; + } + +} + diff --git a/src/util/Error.java b/src/util/Error.java new file mode 100644 index 0000000..4a75000 --- /dev/null +++ b/src/util/Error.java @@ -0,0 +1,21 @@ +package util; + +public enum Error{ + NOERROR(0, "sortie normale"), + TYPE(3, "erreur de types"), + TS(4, "table des symboles"), + SYNT(5,"erreur de syntaxe"), + LEX(6, "lexical error"); + + private final String message; + private final int code; + + Error(int code, String message){ + this.code = code; + this.message = message; + } + + public String toString(){return message;} + public String message(){return message;} + public int code(){return code;} +} diff --git a/src/util/Memory.java b/src/util/Memory.java new file mode 100644 index 0000000..688a9da --- /dev/null +++ b/src/util/Memory.java @@ -0,0 +1,224 @@ +package util; + +/* Big Endian */ + +public class Memory{ + /* stack segment */ + private int ss; + + /* data segment */ + private int ds; + + public int esp; // adresse du sommet de la pile en octets + private int memSize; + private int dataSize; + private int stackSize; + private byte[] mem; + + public Memory(int dataSize, int stackSize){ + this.dataSize = dataSize; + this.stackSize = stackSize; + this.memSize = dataSize + stackSize; + mem = new byte[memSize]; + ss = memSize - 1; + esp = ss; + ds = 0; + } + + public void printStack(){ + for(int adr = ss - 3; adr > esp; adr = adr - 4){ + System.out.print(readInt(adr) + "\t"); + } + System.out.println(); + } + + public int readInt(int address){ + if((address < 0) || (address + 3 > ss)) + throw new RuntimeException("segmentation fault"); + return bytesToInt(mem[address], mem[address + 1], mem[address + 2], mem[address + 3]); + } + + + public boolean readBoolean(int address){ + if((address < 0) || (address > ss)) + throw new RuntimeException("segmentation fault"); + return byteToBoolean(mem[address]); + } + + public byte readByte(int address){ + if((address < 0) || (address > ss)) + throw new RuntimeException("segmentation fault"); + return mem[address]; + } + + public void writeInt(int address, int value){ + if((address < 0) || (address + 3 > ss)) + throw new RuntimeException("segmentation fault"); + + byte[] fourBytes = intToBytes(value); + mem[address] = fourBytes[0]; + mem[address + 1] = fourBytes[1]; + mem[address + 2] = fourBytes[2]; + mem[address + 3] = fourBytes[3]; + } + + public void writeToMemory(int address, int value){ + if((address < 0) || (address + 3 > ss)) + throw new RuntimeException("segmentation fault"); + + byte[] fourBytes = intToBytes(value); + mem[address] = fourBytes[0]; + mem[address + 1] = fourBytes[1]; + mem[address + 2] = fourBytes[2]; + mem[address + 3] = fourBytes[3]; + } + + /* public void writeToMemory(int address, boolean value) + { + if((address < 0) || (address > ss)) + throw new RuntimeException("segmentation fault"); + mem[address] = booleanToByte(value); + }*/ + + + /* revoir c'est quoi cette affaire */ + public int readNBytes(int address, int n){ + // System.out.println("read memory at address " + address); + if((address < 0) || (address + n - 1 > ss)) + throw new RuntimeException("segmentation fault"); + if(n == 1) + return (int)mem[address]; + // if(n == 4) revoir pourquoi c'est commenté !! + return bytesToInt(mem[address], mem[address + 1], mem[address + 2], mem[address + 3]); + + } + + /* public void writeNBytes(int address, int value){ + if((address < 0) || (address + n - 1 > ss)) + throw new RuntimeException("segmentation fault"); + + byte[] fourBytes = intToBytes(value); + mem[address] = fourBytes[0]; + mem[address + 1] = fourBytes[1]; + mem[address + 2] = fourBytes[2]; + mem[address + 3] = fourBytes[3]; + }*/ + + + + public void writeBoolean(int address, boolean value) + { + if((address < 0) || (address > ss)) + throw new RuntimeException("segmentation fault"); + mem[address] = booleanToByte(value); + } + + public void pushInt(int value){ + if(esp - 3 < dataSize) + throw new RuntimeException("stack overflow"); + + byte[] fourBytes = intToBytes(value); + mem[--esp] = fourBytes[3]; + mem[--esp] = fourBytes[2]; + mem[--esp] = fourBytes[1]; + mem[--esp] = fourBytes[0]; + + + } + /* + public void pushBoolean(boolean value){ + if(esp == dataSize) + throw new RuntimeException("stack overflow"); + mem[esp] = booleanToByte(value); + esp--; + + }*/ + + public int getTopInt(){ + // if(esp == ss) + // throw new RuntimeException("empty stack"); + byte byte0 = mem[esp]; + byte byte1 = mem[esp-1]; + byte byte2 = mem[esp-2]; + byte byte3 = mem[esp-3]; + return bytesToInt(byte0, byte1, byte2, byte3); + } + + public int popInt(){ + if(esp + 3 >= ss) + throw new RuntimeException("stack underflow"); + byte byte0 = mem[esp++]; + byte byte1 = mem[esp++]; + byte byte2 = mem[esp++]; + byte byte3 = mem[esp++]; + + return bytesToInt(byte0, byte1, byte2, byte3); + } + + /* public boolean popBoolean() + { + if(esp == ss) + throw new RuntimeException("stack underflow"); + byte byte0 = mem[esp]; + esp++; + return byteToBoolean(byte0); + }*/ + + public int bytesToInt(byte byte0, byte byte1, byte byte2, byte byte3){ + return ((byte0 & 0xFF) << 24) | + ((byte1 & 0xFF) << 16) | + ((byte2 & 0xFF) << 8) | + ((byte3 & 0xFF) << 0); + } + + public byte[] intToBytes2( int data ) { + byte[] result = new byte[4]; + result[0] = (byte) ((data & 0xFF000000) >> 24); + result[1] = (byte) ((data & 0x00FF0000) >> 16); + result[2] = (byte) ((data & 0x0000FF00) >> 8); + result[3] = (byte) ((data & 0x000000FF) >> 0); + return result; +} + public byte[] intToBytes(int i) + { + byte[] result = new byte[4]; + + result[0] = (byte) (i >> 24); + result[1] = (byte) (i >> 16); + result[2] = (byte) (i >> 8); + result[3] = (byte) (i /*>> 0*/); + + return result; + } + + public byte booleanToByte(boolean value) + { + return (value == true)? (byte) 1 : (byte) 0; + } + + public boolean byteToBoolean(byte value) + { + return (value == 1)? true : false; + } + + + /* public static void main(String[] args){ + Memory mem = new Memory(100, 100); + mem.pushInt(3467); + int v = mem.popInt(); + System.out.println("val =" + v); + mem.pushInt(3468); + v = mem.popInt(); + System.out.println("val =" + v); + + mem.writeInt(96, 234); + int val = mem.readInt(96); + System.out.println("val =" + val); + + mem.writeInt(150, 23); + val = mem.readInt(250); + System.out.println("val =" + val); + + }*/ + +} diff --git a/src/util/Type.java b/src/util/Type.java new file mode 100644 index 0000000..89e5574 --- /dev/null +++ b/src/util/Type.java @@ -0,0 +1,30 @@ +package util; + +public enum Type{ + NUL("nul", 0), + ENTIER("entier", 4), + BOOL("bool", 1); + + private final String nom; + private final int taille; // taille en octets + + Type(String nom, int taille){ + this.nom = nom; + this.taille = taille; + } + + public String toString(){return nom;} + public static Type fromString(String nom){ + if(nom.equals("entier")){return Type.ENTIER;} + if(nom.equals("ENTIER")){return Type.ENTIER;} + if(nom.equals("bool")){return Type.BOOL;} + if(nom.equals("BOOL")){return Type.BOOL;} + if(nom.equals("nul")){return Type.NUL;} + if(nom.equals("NUL")){return Type.NUL;} + return null; + } + + public String nom(){return nom;} + public int taille(){return taille;} + public static boolean checkCompatibility(Type t1, Type t2){return t1 == t2;} +} diff --git a/src/util/graph/ColorGraph.java b/src/util/graph/ColorGraph.java new file mode 100644 index 0000000..61457fb --- /dev/null +++ b/src/util/graph/ColorGraph.java @@ -0,0 +1,162 @@ +package util.graph; + +import util.graph.*; +import util.intset.*; +import java.util.*; +import java.io.*; + +public class ColorGraph { + public Graph graph; + public int vertexNb; + private Stack<Integer> stack; + public IntSet removed; + public IntSet spill; + public int[] color; + public int colorNb; + public Node[] int2Node; + static int NOCOLOR = -1; + + public ColorGraph(Graph graph, int colorNb, int[] preColoredVertices){ + this.graph = graph; + this.colorNb = colorNb; + stack = new Stack<Integer>(); + vertexNb = graph.nodeCount(); + color = new int[vertexNb]; + removed = new IntSet(vertexNb); + spill = new IntSet(vertexNb); + int2Node = graph.nodeArray(); + for(int v=0; v < vertexNb; v++){ + int preColor = preColoredVertices[v]; + if(preColor >= 0 && preColor < colorNb) + color[v] = preColoredVertices[v]; + else + color[v] = NOCOLOR; + } + } + + /*-------------------------------------------------------------------------------------------------------------*/ + /* 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/src/util/graph/Graph.java b/src/util/graph/Graph.java new file mode 100644 index 0000000..d62bdb0 --- /dev/null +++ b/src/util/graph/Graph.java @@ -0,0 +1,83 @@ +package util.graph; + +public class Graph { + + int nodecount=0; + NodeList mynodes, mylast; + public NodeList nodes() { return mynodes;} + + public Node newNode() { + return new Node(this); + } + + void check(Node n) { + if (n.mygraph != this) + throw new Error("Graph.addEdge using nodes from the wrong graph"); + } + + public int nodeCount(){ + return nodecount; + } + + static boolean inList(Node a, NodeList l) { + for(NodeList p=l; p!=null; p=p.tail) + if (p.head==a) return true; + return false; + } + + public Node[] nodeArray(){ + Node[] array = new Node[nodecount]; + for(NodeList q=mynodes; q!=null; q=q.tail) { + array[q.head.mykey] = q.head; + } + return array; + } + + + public void addNOEdge(Node from, Node to) { + check(from); check(to); + if (! from.goesTo(to)){ + to.preds=new NodeList(from, to.preds); + from.succs=new NodeList(to, from.succs); + } + if (! to.goesTo(from)){ + from.preds=new NodeList(to, from.preds); + to.succs=new NodeList(from, to.succs); + } + } + + public void addEdge(Node from, Node to) { + check(from); check(to); + if (from.goesTo(to)) return; + to.preds=new NodeList(from, to.preds); + from.succs=new NodeList(to, from.succs); + } + + NodeList delete(Node a, NodeList l) { + if (l==null) throw new Error("Graph.rmEdge: edge nonexistent"); + else if (a==l.head) return l.tail; + else return new NodeList(l.head, delete(a, l.tail)); + } + + public void rmEdge(Node from, Node to) { + to.preds=delete(from,to.preds); + from.succs=delete(to,from.succs); + } + + /** + * Print a human-readable dump for debugging. + */ + public void show(java.io.PrintStream out) { + for (NodeList p=nodes(); p!=null; p=p.tail) { + Node n = p.head; + out.print(n.toString()); + out.print(": "); + for(NodeList q=n.succ(); q!=null; q=q.tail) { + out.print(q.head.toString()); + out.print(" "); + } + out.println(); + } + } + +} diff --git a/src/util/graph/Node.java b/src/util/graph/Node.java new file mode 100644 index 0000000..040f118 --- /dev/null +++ b/src/util/graph/Node.java @@ -0,0 +1,55 @@ +package util.graph; + +public class Node { + Graph mygraph; + int mykey; + NodeList succs; + NodeList preds; + + private Node(){} + public Node(Graph g) { + mygraph=g; + mykey= g.nodecount++; + NodeList p = new NodeList(this, null); + if (g.mylast==null) + g.mynodes=g.mylast=p; + else g.mylast = g.mylast.tail = p; + } + + public NodeList succ() {return succs;} + public NodeList pred() {return preds;} + + public int label() {return mykey;} + + + NodeList cat(NodeList a, NodeList b) { + if (a==null) return b; + else return new NodeList(a.head, cat(a.tail,b)); + } + public NodeList adj() {return cat(succ(), pred());} + + int len(NodeList l) { + int i=0; + for(NodeList p=l; p!=null; p=p.tail) i++; + return i; + } + + public int inDegree() {return len(pred());} + public int outDegree() {return len(succ());} + public int degree() {return inDegree()+outDegree();} + + public boolean goesTo(Node n) { + return Graph.inList(n, succ()); + } + + public boolean comesFrom(Node n) { + return Graph.inList(n, pred()); + } + + public boolean adj(Node n) { + return goesTo(n) || comesFrom(n); + } + + public String toString() {return String.valueOf(mykey);} + +} diff --git a/src/util/graph/NodeList.java b/src/util/graph/NodeList.java new file mode 100644 index 0000000..4493b7f --- /dev/null +++ b/src/util/graph/NodeList.java @@ -0,0 +1,10 @@ +package util.graph; + +public class NodeList { + public Node head; + public NodeList tail; + public NodeList(Node h, NodeList t) {head=h; tail=t;} +} + + + diff --git a/src/util/graph/TestColorGraph.java b/src/util/graph/TestColorGraph.java new file mode 100644 index 0000000..c496fae --- /dev/null +++ b/src/util/graph/TestColorGraph.java @@ -0,0 +1,77 @@ +package util.graph; +import util.graph.*; +import util.intset.*; +import java.util.*; +import java.io.*; + +public class TestColorGraph +{ + public static void main(String[] args) + { + Graph g = new Graph(); + /* + Node n0 = g.newNode(); + Node n1 = g.newNode(); + Node n2 = g.newNode(); + Node n3 = g.newNode(); + Node n4 = g.newNode(); + + g.addNOEdge(n0, n1); + g.addNOEdge(n0, n2); + g.addNOEdge(n0, n3); + g.addNOEdge(n0, n4); + + g.addNOEdge(n1, n2); + g.addNOEdge(n1, n3); + g.addNOEdge(n1, n4); + + g.addNOEdge(n2, n3); + g.addNOEdge(n2, n4); + + g.addNOEdge(n3, n4); + */ + + + Node n1 = g.newNode(); + Node n2 = g.newNode(); + Node n3 = g.newNode(); + Node n4 = g.newNode(); + Node n5 = g.newNode(); + Node n6 = g.newNode(); + Node n7 = g.newNode(); + Node n8 = g.newNode(); + Node n9 = g.newNode(); + Node n10 = g.newNode(); + + g.addNOEdge(n1, n2); + g.addNOEdge(n1, n4); + g.addNOEdge(n1, n9); + + g.addNOEdge(n2, n10); + + g.addNOEdge(n3, n8); + g.addNOEdge(n3, n9); + g.addNOEdge(n3, n10); + + g.addNOEdge(n4, n5); + g.addNOEdge(n4, n8); + g.addNOEdge(n4, n10); + + g.addNOEdge(n5, n8); + g.addNOEdge(n5, n10); + + g.addNOEdge(n6, n7); + g.addNOEdge(n6, n8); + g.addNOEdge(n6, n9); + + g.addNOEdge(n7, n8); + + g.addNOEdge(n8, n9); + + g.show(System.out); + int[] colors = new int[]{ -3,-3,-3,0,-3,2,1,-3,-3,-3}; + ColorGraph cg = new ColorGraph(g, 3, colors); + cg.color(); + cg.affiche(); + } +} diff --git a/src/util/intset/IntSet.java b/src/util/intset/IntSet.java new file mode 100644 index 0000000..3d0d5f4 --- /dev/null +++ b/src/util/intset/IntSet.java @@ -0,0 +1,94 @@ +package util.intset; + +public class IntSet{ + private int size; + private boolean array[]; + + public IntSet(int size){ + this.size = size; + array = new boolean[size]; + for (int i = 0; i < size; i++) + array[i] = false; + } + + public int getSize(){ + return size; + } + + public void empty(){ + for (int i = 0; i < size; i++) + array[i] = false; + } + + public boolean isMember(int elt){ + if(elt >= size) return false; + return array[elt]; + } + + public boolean isEmpty(){ + for (int i = 0; i < size; i++) + if(array[i] == true) + return false; + return true; + } + + + public void add(int elt){ + if(elt >= size){ + System.err.println("ERROR : set is too small !!!!"); + } + array[elt] = true; + } + + public void remove(int elt){ + if(elt >= size){ + System.err.println("ERROR : set is too small !!!!"); + } + array[elt] = false; + } + + public IntSet minus(IntSet s){ + for (int i = 0; i < s.size; i++) + if(s.array[i]) + array[i] = false; + return this; + } + + public IntSet copy(){ + IntSet c = new IntSet(size); + for (int i = 0; i < size; i++) + c.array[i] = array[i]; + return c; + } + + public IntSet inter(IntSet s){ + for (int i = 0; i < s.size; i++) + array[i] = array[i] && s.array[i]; + return this; + } + + public IntSet union(IntSet s){ + for (int i = 0; i < s.size; i++) + array[i] = array[i] || s.array[i]; + return this; + } + + public boolean equal(IntSet s){ + for (int i = 0; i < s.size; i++) + if(array[i] != s.array[i]) + return false; + return true; + } + + public String toString(){ + String s = "("; + for (int i = 0; i < size; i++) + if(array[i] == true){ + s += i; + s += " "; + } + s+= ")"; + return s; + } + +} diff --git a/src/util/memory/MemSeg.java b/src/util/memory/MemSeg.java new file mode 100644 index 0000000..5fa0dfd --- /dev/null +++ b/src/util/memory/MemSeg.java @@ -0,0 +1,79 @@ +package util.memory; + +/* Big Endian */ + +public class MemSeg{ + public int size; + public byte[] mem; + + public MemSeg(int size){ + this.size = size; + this.mem = new byte[size]; + } + + public int readInt(int address){ + //System.out.println("read memory at address " + address + " size = " + size); + if((address < 0) || (address + 3 >= size)) + throw new RuntimeException("segmentation fault"); + return bytesToInt(mem[address], mem[address + 1], mem[address + 2], mem[address + 3]); + } + + public byte readByte(int address){ + // System.out.println("read memory at address " + address); + if((address < 0) || (address >= size)) + throw new RuntimeException("segmentation fault"); + return mem[address]; + } + + public void writeInt(int address, int value){ + if((address < 0) || (address + 3 >= size)) + throw new RuntimeException("segmentation fault"); + + byte[] fourBytes = intToBytes(value); + mem[address] = fourBytes[0]; + mem[address + 1] = fourBytes[1]; + mem[address + 2] = fourBytes[2]; + mem[address + 3] = fourBytes[3]; + } + + public int bytesToInt(byte byte0, byte byte1, byte byte2, byte byte3){ + return ((byte0 & 0xFF) << 24) | + ((byte1 & 0xFF) << 16) | + ((byte2 & 0xFF) << 8) | + ((byte3 & 0xFF) << 0); + } + + public byte[] intToBytes2( int data ) { + byte[] result = new byte[4]; + result[0] = (byte) ((data & 0xFF000000) >> 24); + result[1] = (byte) ((data & 0x00FF0000) >> 16); + result[2] = (byte) ((data & 0x0000FF00) >> 8); + result[3] = (byte) ((data & 0x000000FF) >> 0); + return result; +} + public byte[] intToBytes(int i) + { + byte[] result = new byte[4]; + + result[0] = (byte) (i >> 24); + result[1] = (byte) (i >> 16); + result[2] = (byte) (i >> 8); + result[3] = (byte) (i /*>> 0*/); + + return result; + } + + /* public static void main(String[] args){ + Memory mem = new Memory(100, 100); + mem.pushInt(3467); + int v = mem.popInt(); + System.out.println("val =" + v); + v = mem.popInt(); + + mem.writeInt(96, 234); + int val = mem.readInt(96); + System.out.println("val =" + val); + + }*/ + +} diff --git a/src/util/memory/MemStack.java b/src/util/memory/MemStack.java new file mode 100644 index 0000000..d9359bc --- /dev/null +++ b/src/util/memory/MemStack.java @@ -0,0 +1,79 @@ +package util.memory; + +/* Big Endian */ + +public class MemStack extends MemSeg{ + private int top; + + + public MemStack(int size){ + super(size); + top = size; + } + + public int getTop(){ + return top; + } + + public void setTop(int value){ + top = value; + } + + public void print(){ + for(int adr = size - 4; adr >= top; adr = adr - 4){ + // System.out.println("adr =" + adr); + System.out.print("[" + adr + "] " + readInt(adr) + "\t"); + } + System.out.println(); + } + + public void shrink(int bytesNb){ + top += bytesNb; + } + + public void extend(int bytesNb){ + top -= bytesNb; + } + + public void pushInt(int value){ + if(top - 3 < 0) + throw new RuntimeException("stack overflow"); + + byte[] fourBytes = intToBytes(value); + top--; + mem[top] = fourBytes[3]; + top--; + mem[top] = fourBytes[2]; + top--; + mem[top] = fourBytes[1]; + top--; + mem[top] = fourBytes[0]; + /* mem[top--] = fourBytes[3]; + mem[top--] = fourBytes[2]; + mem[top--] = fourBytes[1]; + mem[top--] = fourBytes[0]; +*/ + + } + + public int popInt(){ + if(top + 3 >= size) + throw new RuntimeException("stack underflow"); + byte byte0 = mem[top]; + top++; + byte byte1 = mem[top]; + top++; + byte byte2 = mem[top]; + top++; + byte byte3 = mem[top]; + top++; + + /* byte byte0 = mem[++top]; + byte byte1 = mem[++top]; + byte byte2 = mem[++top]; + byte byte3 = mem[++top];*/ + + return bytesToInt(byte0, byte1, byte2, byte3); + } + +} -- GitLab