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

commit original

parent 8a887d34
Branches
No related tags found
No related merge requests found
Showing
with 1233 additions and 0 deletions
File added
File added
*\.class
*/*\.class
*/*/*\.class
*~
\ No newline at end of file
import java.io.IOException;
import c3a.C3a;
import c3a.C3aEval;
import c3a.LoadC3a;
import ts.Ts;
import ts.TsParser;
public class C3aVM {
private int verboseLevel = 0;
private TsParser stParser;
private String symbolsTableFileName;
private String C3AFileName;
private int stackSize;
private C3a code;
private Ts globalTable;
private LoadC3a loadC3a;
public C3aVM(String symbolsTableFileName, String C3AFileName, int stackSize, int verboseLevel){
this.stackSize = stackSize;
this.verboseLevel = verboseLevel;
this.symbolsTableFileName = symbolsTableFileName;
this.C3AFileName = C3AFileName;
stParser = new TsParser();
this.globalTable = stParser.parse(symbolsTableFileName);
loadC3a = new LoadC3a(C3AFileName, this.globalTable);
this.code = loadC3a.getC3a();
}
public void run() throws IOException {
if(verboseLevel > 0)
code.affiche(null);
C3aEval c3aEval = new C3aEval(code, globalTable, stackSize, verboseLevel);
code.accept(c3aEval);
c3aEval.affiche(null);
// eval.affiche(C3AFileName.substring(0, C3AFileName.length() - 4));
}
public static void main(String[] args){
int verboseLevel = 0;
String symbolsTableFileName = null;
String C3AFileName = null;
int stackSize = 10000;
try {
for (int i = 0; i < args.length; i++) {
if(args[i].equals("-v"))
verboseLevel = Integer.parseInt(args[++i]);
else if(args[i].equals("-s"))
stackSize = Integer.parseInt(args[++i]);
else if(args[i].equals("-c3a"))
C3AFileName = args[++i];
else if(args[i].equals("-ts"))
symbolsTableFileName = args[++i];
}
if(C3AFileName == null || symbolsTableFileName == null){
System.out.println("java C3aVM -c3a C3AFile -ts TSFile -s stackSize -v verboseLevel");
System.exit(1);
}
C3aVM vm = new C3aVM(symbolsTableFileName, C3AFileName, stackSize, verboseLevel);
vm.run();
}catch(IOException e){
e.printStackTrace();
}
}
}
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 ignored) {
}
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);
//new Sa2Xml(saRoot, baseName);
}
}
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 ignored) {
}
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){}
if (verboseLevel > 1) {
System.out.println("[PRINT C3A] ");
c3a.affiche(baseName);
}
}
private static void buildPreNasm() {
C3a2nasm c3a2nasm = new C3a2nasm(c3a, tableGlobale);
c3a.accept(c3a2nasm);
nasm = c3a2nasm.getNasm();
if (verboseLevel > 1) {
System.out.println("[PRINT PRE NASM] ");
nasm.affichePreNasm(baseName);
}
}
private static void buildFg() {
flowGraph = new Fg(nasm);
if (verboseLevel > 1) {
System.out.println("[PRINT FLOW GRAPH] ");
flowGraph.affiche(baseName);
}
}
private static void solveFg() {
flowGraphSolution = new FgSolution(nasm, flowGraph);
if (verboseLevel > 1) {
System.out.println("[PRINT FLOW GRAPH SOLUTION] ");
flowGraphSolution.affiche(baseName);
}
}
private static void buildIg() {
interferenceGraph = new Ig(flowGraphSolution);
if (verboseLevel > 1) {
System.out.println("[PRINT INTERFERENCE GRAPH] ");
interferenceGraph.affiche(baseName);
}
}
/*catch (Exception e)
{
e.printStackTrace();
System.out.println(e.getMessage());
System.exit(1);
}
}*/
public static String removeSuffix(final String s, final String suffix)
{
if (s != null && suffix != null && s.endsWith(suffix)){
return s.substring(0, s.length() - suffix.length());
}
return s;
}
}
all: Compiler C3aVM NasmVM SaVM
SaVM: SaVM.java saParser
javac SaVM.java
NasmVM.jar : NasmVM
jar cmf NasmVM.mf NasmVM.jar NasmVM.class nasm
C3aVM: C3aVM.java c3aParser
javac C3aVM.java
NasmVM: NasmVM.java nasmParser
javac NasmVM.java
Compiler.jar : Compiler
jar cmf Compiler.mf Compiler.jar *.class sc sa ts nasm util c3a fg ig lParser
Compiler: Compiler.java lParser
javac Compiler.java
lParser: l.cfg
java -jar ../sablecc/sablecc.jar l.cfg
saParser: sa.cfg
java -jar ../sablecc/sablecc.jar sa.cfg
c3aParser: c3a.cfg
java -jar ../sablecc/sablecc.jar c3a.cfg
nasmParser: nasm.cfg
java -jar ../sablecc/sablecc.jar nasm.cfg
ualParser: ual.cfg
java -jar ../sablecc/sablecc.jar ual.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 util/*.class
-rm -r lParser
-rm -r saParser
-rm -r c3aParser
-rm -r nasmParser
import ts.Ts;
import ts.TsParser;
import nasm.NasmEval;
import nasm.Nasm;
import nasm.LoadNasm;
import java.io.IOException;
public class NasmVM {
private int verboseLevel;
private int stackSize;
private Nasm code;
private String nasmFileName;
public NasmVM(String nasmFileName, int stackSize, int verboseLevel){
this.nasmFileName = nasmFileName;
this.stackSize = stackSize;
this.verboseLevel = verboseLevel;
}
public void run() throws IOException {
LoadNasm loadNasm = new LoadNasm(nasmFileName);
this.code = loadNasm.getNasm();
if(verboseLevel > 0)
code.afficheNasm(null);
NasmEval eval = new NasmEval(code, stackSize, verboseLevel);
eval.displayOutput();
}
public static void main(String[] args){
int verboseLevel = 0;
int stackSize = 10000;
String nasmFileName = null;
try {
for (int i = 0; i < args.length; i++) {
if(args[i].equals("-v"))
verboseLevel = Integer.parseInt(args[++i]);
else if(args[i].equals("-s"))
stackSize = Integer.parseInt(args[++i]);
else if(args[i].equals("-nasm"))
nasmFileName = args[++i];
}
if(nasmFileName == null){
System.out.println("java NasmVM -nasm nasmFile -s stackSize -v verboseLevel");
System.exit(1);
}
var vm = new NasmVM(nasmFileName, stackSize, verboseLevel);
vm.run();
}catch(IOException e){
e.printStackTrace();
}
}
}
/*
else{
var pathTab = preNasmFilePath.split("/");
var fileNamePreNasm = pathTab[pathTab.length - 1];
var fileName = outputPath + fileNamePreNasm.substring(0, fileNamePreNasm.length()-3)+ ".out";
vm.displayOutput(fileName);
}
}
*/
import sa.*;
import ts.*;
public class SaVM {
public static void main(String[] args) {
int verboseLevel = 0;
String saFileName = null;
try {
for (int i = 0; i < args.length; i++) {
if(args[i].equals("-v"))
verboseLevel = Integer.parseInt(args[++i]);
else if(args[i].equals("-sa"))
saFileName = args[++i];
}
if(saFileName == null){
System.out.println("java SaVM -sa saFile -v verboseLevel");
System.exit(1);
}
if(verboseLevel > 0)
System.err.println("parsing xml document");
if(verboseLevel > 0)
System.err.println("building sa tree");
SaProg prog = new LoadSa(saFileName).getRoot();
if(verboseLevel > 0)
System.out.println(prog);
if(verboseLevel > 0)
System.err.println("building symbol table");
Sa2ts sa2ts = new Sa2ts();
prog.accept(sa2ts);
Ts tableGlobale = sa2ts.getTableGlobale();
if(verboseLevel > 0)
System.err.println("evaluating sa tree");
SaEval saEval = new SaEval(prog, tableGlobale);
saEval.affiche(null);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Package c3aParser;
Helpers
lettre = [['a' .. 'z'] + ['A' .. 'Z']];
chiffre = ['0' .. '9'];
alpha = [lettre + ['_' + '$']];
alphanum = [lettre + chiffre];
Tokens
espaces = (' ' | 13 | 10 | 9)+;
commentaire= '#' [[0 .. 0xffff] - [10 + 13]]* (10 | 13 | 10 13);
co = '[';
cf = ']';
call = 'call';
stop = 'stop';
fbegin = 'fbegin';
fend = 'fend';
aff = '=';
noteq = '!=';
inf = '<';
infeq = '<=';
sup = '>';
supeq = '>=';
eq = '==';
goto = 'goto';
param = 'param';
ret = 'ret';
if = 'if';
moins = '-';
plus = '+';
fois = '*';
divise = '/';
write = 'write';
read = 'read';
nombre = chiffre+;
prefixe_temp = '@';
identif = alpha alphanum*;
Ignored Tokens
espaces, commentaire;
Productions
prog = listeinst;
listeinst = {recursif} inst listeinst |
{final} inst
;
inst = {add} add |
{sub} sub |
{mul} mul |
{div} div |
{aff} affect |
{jmp} jmp |
{jmpeg} jmpeg |
{jmpneg} jmpneg |
{jmpinf} jmpinf |
{jminfeg} jmpinfeg |
{jmpsup} jmpsup |
{jmpsupeg} jmpsupeg |
{fcall} fcall |
{iwrite} iwrite |
{iread} iread |
{arg} arg |
{return} return |
{begin} begin |
{end} end |
{stop} istop
;
add = etiqop [result]:tv aff [op1]:ctv plus [op2]:ctv;
sub = etiqop [result]:tv aff [op1]:ctv moins [op2]:ctv;
mul = etiqop [result]:tv aff [op1]:ctv fois [op2]:ctv;
div = etiqop [result]:tv aff [op1]:ctv divise [op2]:ctv;
affect = etiqop [result]:tv aff [op1]:ctv;
jmpeg = etiqop if [op1]:ctv eq [op2]:ctv goto [result]:constante;
jmpneg = etiqop if [op1]:ctv noteq [op2]:ctv goto [result]:constante;
jmpinf = etiqop if [op1]:ctv inf [op2]:ctv goto [result]:constante;
jmpinfeg = etiqop if [op1]:ctv infeq [op2]:ctv goto [result]:constante;
jmpsup = etiqop if [op1]:ctv sup [op2]:ctv goto [result]:constante;
jmpsupeg = etiqop if [op1]:ctv supeq [op2]:ctv goto [result]:constante;
jmp = etiqop goto [result]:constante;
begin = identif fbegin;
end = etiqop fend;
fcall = etiqop [result]:tv aff call identif;
iwrite = etiqop write ctv;
iread = etiqop [result]:tv aff read;
arg = etiqop param ctv;
return = etiqop ret ctv;
istop = etiqop stop ctv;
ctv = {c} constante | {t} temporaire | {v} variable;
tv = {t} temporaire | {v} variable;
constante = nombre;
temporaire = prefixe_temp nombre;
variable = {scalaire} identif |
{tab} identif co ctv cf
;
etiqop = {cte} nombre |
{vide}
;
package c3a;
import java.util.*;
import java.io.*;
import ts.*;
import sa.*;
public class C3a{
public List<C3aInst> listeInst;
// compteur des temporaires, pour générer des noms uniques
private int tempCounter;
// étiquette de la prochaine instruction, on retarde l'ajout de l'étiquette
private C3aLabel nextLabel;
private int labelCounter;
public C3aConstant True;
public C3aConstant False;
public C3a(){
this.listeInst = new ArrayList<C3aInst>();
this.labelCounter = 0;
this.tempCounter = 0;
this.nextLabel = null;
this.True = new C3aConstant(1); // constantes utilisées partout
this.False = new C3aConstant(0); // constantes utilisées partout
}
public int getTempCounter(){return this.tempCounter;}
public void setTempCounter(int value){this.tempCounter = value;}
public int getLabelCounter(){return this.labelCounter;}
public void setLableCounter(int value){this.labelCounter = value;}
public void ajouteInst(C3aInst inst){
if(this.nextLabel != null){
inst.setLabel(this.nextLabel);
this.nextLabel = null;
}
this.listeInst.add(inst);
}
public C3aLabel newAutoLabel(){
return new C3aLabel(this.labelCounter++);
}
public C3aTemp newTemp(){
return new C3aTemp(this.tempCounter++);
}
public void addLabelToNextInst(C3aLabel label){
if(this.nextLabel != null){
label.affect(this.nextLabel);
System.err.println("WARNING : Étiquette précédente ignorée " + this.nextLabel.getNumber());
}
this.nextLabel = label;
// if(etiquette->oper_type == O_ETIQUETTE){
// etiquette->u.oper_etiquette.ligne = c3a->next;
//}
//if(etiquette->oper_type == O_FCT){
// etiquette->u.oper_fct->adresse = c3a->next;
//}
}
public void affiche(String baseFileName){
String fileName;
PrintStream out = System.out;
if (baseFileName != null){
try {
baseFileName = baseFileName;
fileName = baseFileName + ".c3a";
out = new PrintStream(fileName);
}
catch (IOException e) {
System.err.println("Error: " + e.getMessage());
}
}
Iterator<C3aInst> iter = this.listeInst.iterator();
while(iter.hasNext()){
out.println(iter.next());
}
}
public <T> T accept(C3aVisitor <T> visitor) {
return visitor.visit(this);
}
}
package c3a;
public class C3aBooleanConstant extends C3aOperand{
public boolean val;
public C3aBooleanConstant(boolean val){
this.val = val;
}
public String toString(){return String.valueOf(this.val);}
public <T> T accept(C3aVisitor <T> visitor) {
return visitor.visit(this);
}
}
package c3a;
public class C3aConstant extends C3aOperand{
public int val;
public C3aConstant(int val){
this.val = val;
}
public String toString(){return Integer.toString(this.val);}
public <T> T accept(C3aVisitor <T> visitor) {
return visitor.visit(this);
}
}
package c3a;
import java.util.*;
import java.io.*;
import ts.*;
import util.Memory;
import util.Type;
public class C3aEval implements C3aVisitor <Integer> {
private C3a c3a;
private Ts tableGlobale;
private int nbReg;
private int[] reg;
private int[] stack;
private Memory varGlob;
private int eip;
private int ebp;
private int esp;
private int stackSize;
private Map< Integer, Integer> label2index;
private Map< String, Integer> function2index;
private boolean debug;
private boolean stop;
private ArrayList<String> programOutput = new ArrayList<String>();
private TsItemFct currentFct;
private Stack<TsItemFct> stackCurrentFct;
private C3aOperand storeReturnValue;
private Stack<C3aOperand> stackStoreReturnValue;
private int verboseLevel;
public C3aEval(C3a c3a, Ts tableGlobale, int stackSize, int verboseLevel){
debug = true;
this.verboseLevel = verboseLevel;
this.c3a = c3a;
stack = new int[stackSize];
nbReg = c3a.getTempCounter();
reg = new int[nbReg];
esp = 0;
varGlob = new Memory(tableGlobale.getAdrVarCourante(), 0);
this.tableGlobale = tableGlobale;
this.currentFct = null;
storeReturnValue = null;
label2index = new HashMap< Integer, Integer>();
function2index = new HashMap< String, Integer>();
C3aInst c3aInst = null;
stackCurrentFct = new Stack<>();
stackStoreReturnValue = new Stack<>();
eip = 0;
for(int i = 0; i < c3a.listeInst.size(); i++){
c3aInst = c3a.listeInst.get(i);
if(c3aInst.label != null){
label2index.put(c3aInst.label.number, i);
}
if(c3aInst instanceof C3aInstFBegin){
String identif = ((C3aInstFBegin)c3aInst).val.identif;
function2index.put(identif, i);
}
}
}
public C3a getC3a(){
return this.c3a;
}
public void afficheEtat(C3aInst inst){
System.out.println("---------------------------------------------");
System.out.println("eip = " + eip + "\tesp = " + esp + "\tebp = " + ebp);
System.out.print("PILE : ");
affichePile();
System.out.print("REGISTRES : ");
afficheReg();
System.out.println(inst);
}
public void affiche(String baseFileName){
String fileName;
PrintStream out = System.out;
if (baseFileName != null){
try {
baseFileName = baseFileName;
fileName = baseFileName + ".c3aout";
out = new PrintStream(fileName);
}
catch (IOException e) {
System.err.println("Error: " + e.getMessage());
}
}
for (String line : programOutput)
out.println(line);
}
public void affichePileVertical(){
for(int i = esp-1; i >= 0; i--){
System.out.println(i + ":" + stack[i]);
}
}
public void affichePile(){
for(int i = 0; i < esp; i++){
System.out.print(stack[i] + " ");
}
System.out.println("");
}
public void push(int i){
if(esp == stackSize){
System.out.println("stack overflow !!!");
System.exit(1);
}
stack[esp] = i;
esp++;
}
public void afficheReg(){
for(int i=0; i <nbReg; i++)
System.out.print(i + ":" + reg[i] + " ");
System.out.println("");
}
public void pushReg(){
// System.out.println("pushReg " + nbReg + " registers");
for(int i=0; i <nbReg; i++)
push(reg[i]);
}
public void popReg(){
// System.out.println("popReg " + nbReg + " registers");
for(int i=nbReg-1; i >=0; i--)
reg[i] = pop();
}
public int pop(){
if(esp == 0){
System.out.println("cannot pop empty stack !!!");
System.exit(1);
}
esp--;
return stack[esp];
}
public Integer visit(C3a c3a){
stop = false;
C3aInst c3aInst;
while(!stop){
c3aInst = c3a.listeInst.get(eip);
// System.out.println("<" + c3aInst.getClass().getSimpleName() + ">");
if(verboseLevel > 0)
afficheEtat(c3aInst);
c3aInst.accept(this);
}
return 0;
}
public Integer visit(C3aConstant constant){
return constant.val;
}
public Integer visit(C3aBooleanConstant constant){
return (constant.val == true)? 1 : 0;
}
public Integer visit(C3aTemp temp){
return reg[temp.num];
}
public Integer visit(C3aVar var){
int adresse = var.item.adresse;
Type type = var.item.type;
// variable globale
if(var.item.portee == this.tableGlobale){
int offset = (var.index != null)? var.index.accept(this) : 0;
// return varGlob[adresse/4 + offset];
// System.out.println("adresse = " + adresse + " taille = " + type.taille());
return varGlob.readNBytes(adresse + offset * type.taille(), type.taille());
}
if(var.item.isParam){ // parametre
int nbArgs = this.currentFct.nbArgs;
return stack[ebp - 2 - nbArgs + adresse / 4];
}
// variable locale
return stack[ebp + 1 + adresse / 4];
}
public void affect(C3aOperand op, int val){
if(op instanceof C3aVar){
C3aVar var = (C3aVar) op;
int adresse = var.item.adresse;
int size = var.item.getType().taille();
// variable globale
if(var.item.portee == this.tableGlobale){
int offset = (var.index != null)? var.index.accept(this) : 0;
varGlob.writeInt(adresse + offset * size, val);
}
else if(var.item.isParam){ // parametre
int nbArgs = this.currentFct.nbArgs;
stack[ebp - 2 - nbArgs + adresse/4] = val;
}
else // variable locale
stack[ebp + 1 + adresse/4] = val;
}
else if(op instanceof C3aTemp){
C3aTemp temp = (C3aTemp) op;
reg[temp.num] = val;
}
}
public Integer visit(C3aFunction fct){
return null;
}
public Integer visit(C3aLabel label){
return label.number;
}
/*--------------------------------------------------------------------------------------------------------------*/
public Integer visit(C3aInstStop inst){
stop = true;
return null;
}
public Integer visit(C3aInstAdd inst){
//if(debug){afficheEtat(inst);}
affect(inst.result, inst.op1.accept(this) + inst.op2.accept(this));
eip++;
return null;
}
public Integer visit(C3aInstSub inst){
//if(debug){afficheEtat(inst);}
affect(inst.result, inst.op1.accept(this) - inst.op2.accept(this));
eip++;
return null;
}
public Integer visit(C3aInstMult inst){
//if(debug){afficheEtat(inst);}
affect(inst.result, inst.op1.accept(this) * inst.op2.accept(this));
eip++;
return null;
}
public Integer visit(C3aInstDiv inst){
//if(debug){afficheEtat(inst);}
affect(inst.result, inst.op1.accept(this) / inst.op2.accept(this));
eip++;
return null;
}
public Integer visit(C3aInstJumpIfLess inst){
//if(debug){afficheEtat(inst);}
if(inst.op1.accept(this) < inst.op2.accept(this))
eip = label2index.get(inst.result.accept(this));
else
eip++;
return null;
}
public Integer visit(C3aInstJumpIfEqual inst){
//if(debug){afficheEtat(inst);}
if(inst.op1.accept(this) == inst.op2.accept(this))
eip = label2index.get(inst.result.accept(this));
else
eip++;
return null;
}
public Integer visit(C3aInstJumpIfNotEqual inst){
//if(debug){afficheEtat(inst);}
if(inst.op1.accept(this) != inst.op2.accept(this))
eip = label2index.get(inst.result.accept(this));
else
eip++;
return null;
}
public Integer visit(C3aInstJump inst){
//if(debug){afficheEtat(inst);}
eip = label2index.get(inst.result.accept(this));
return null;
}
public Integer visit(C3aInstAffect inst){
//if(debug){afficheEtat(inst);}
affect(inst.result, inst.op1.accept(this));
eip++;
return null;
}
public Integer visit(C3aInstParam inst){
//if(debug){afficheEtat(inst);}
push(inst.op1.accept(this));
eip++;
return null;
}
public Integer visit(C3aInstReturn inst){
//if(debug){afficheEtat(inst);}
stack[ebp - 2] = (inst.op1 != null)? inst.op1.accept(this) : 0;
eip++;
return null;
}
public Integer visit(C3aInstWrite inst){
//if(debug){afficheEtat(inst);}
programOutput.add(inst.op1.accept(this).toString());
eip++;
return null;
}
public Integer visit(C3aInstCall inst){
//if(debug){afficheEtat(inst);}
// storeReturnValue = inst.result;
stackStoreReturnValue.push(inst.result);
// allocation mémoire pour la valeur de retour
//if(debug){System.out.println("allocation mémoire pour la valeur de retour, esp++");}
esp++;
//if(debug){System.out.println("sauvegarde de l'index de l'instruction à effectuer après l'appel : push(eip + 1)");}
push(eip + 1);
stackCurrentFct.push(currentFct);
eip = function2index.get(inst.op1.val.identif);
return null;
}
public Integer visit(C3aInstFBegin inst){
//if(debug){afficheEtat(inst);}
currentFct = inst.val;
int nbVarLoc = currentFct.getTable().getAdrVarCourante();
//if(debug){System.out.println("sauvegarde de l'ancienne valeur de ebp");}
push(ebp);
//if(debug){System.out.println("nouvelle valeur de ebp");}
ebp = esp - 1;
//if(debug){System.out.println("allocation des variables locales");}
esp = esp + nbVarLoc;
pushReg();
eip++;
return null;
}
public Integer visit(C3aInstFEnd inst){
//if(debug){afficheEtat(inst);}
int nbVarLoc = currentFct.getTable().getAdrVarCourante();
int nbParam = currentFct.nbArgs;
popReg();
esp = esp - nbVarLoc;
ebp = pop();
eip = pop();
int rv = pop();
storeReturnValue = stackStoreReturnValue.pop();
if(storeReturnValue != null){
affect(storeReturnValue, rv);
storeReturnValue = null;
}
currentFct = stackCurrentFct.pop();
esp = esp - nbParam;
return null;
}
public Integer visit(C3aInstRead inst){
/* Int label = (inst.label != null) ? inst.label.accept(this) : null;
Int result = inst.result.accept(this);
NasmRegister reg_eax = nasm.newRegister();
reg_eax.colorRegister(Nasm.REG_EAX);
nasm.ajouteInst(new NasmMov (label, reg_eax, new NasmLabel("sinput"), "Read 1"));
nasm.ajouteInst(new NasmCall(null, new NasmLabel("readline"), "Read 2"));
nasm.ajouteInst(new NasmMov (null, reg_eax, new NasmLabel("sinput"), "Read 3"));
nasm.ajouteInst(new NasmMov (null, result, reg_eax , "Read 4"));*/
eip++;
return null;
}
public Integer visit(C3aInst inst){
return null;}
}
package c3a;
import ts.*;
public class C3aFunction extends C3aOperand{
public TsItemFct val;
public C3aFunction(TsItemFct val){
this.val = val;
}
public TsItemFct getValue(){return val;}
public String toString(){
return this.val.getIdentif();
}
public <T> T accept(C3aVisitor <T> visitor) {
return visitor.visit(this);
}
}
package c3a;
public class C3aInst{
public C3aLabel label ; // étiquette optionnelle, si la ligne est cible d'un saut
public String comment; // commentaire explicatif - utile pour le débogage
public void setLabel(C3aLabel label){
this.label = label;
}
public <T> T accept(C3aVisitor <T> visitor) {
return visitor.visit(this);
}
}
package c3a;
public class C3aInstAdd extends C3aInst{
public C3aOperand op1; // opérande 1 (ou null)
public C3aOperand op2; // opérande 2 (ou null)
public C3aOperand result; // résultat ou destination du saut (ou NULL)
public C3aInstAdd(C3aOperand op1, C3aOperand op2, C3aOperand result, String comment){
this.op1 = op1;
this.op2 = op2;
this.result = result;
this.comment = comment;
}
public String toString(){
if(this.label != null)
return this.label + "\t" + this.result + " = " + this.op1 + " + " + this.op2 + this.comment;
else
return "\t" + this.result + " = " + this.op1 + " + " + this.op2 + this.comment;
}
public <T> T accept(C3aVisitor <T> visitor) {
return visitor.visit(this);
}
}
package c3a;
public class C3aInstAffect extends C3aInst{
public C3aOperand op1; // opérande 1 (ou null)
public C3aOperand result; // résultat ou destination du saut (ou NULL)
public C3aInstAffect(C3aOperand op1, C3aOperand result, String comment){
this.op1 = op1;
this.result = result;
this.comment = comment;
}
public String toString(){
String s = "";
if(this.label != null) s = s + this.label;
s = s + "\t";
s = s + this.result + " = " + this.op1 + this.comment;
return s;
}
public <T> T accept(C3aVisitor <T> visitor) {
return visitor.visit(this);
}
}
package c3a;
public class C3aInstCall extends C3aInst{
public C3aFunction op1; // opérande 1 (ou null)
public C3aOperand result; // résultat ou destination du saut (ou NULL)
public C3aInstCall(C3aFunction op1, C3aOperand result, String comment){
this.op1 = op1;
this.result = result;
this.comment = comment;
}
public String toString(){
String s = "";
if(this.label != null)
s = s + this.label;
s = s + "\t";
if(this.result != null)
s = s + this.result + " =";
s = s + " call " + this.op1 + this.comment;
return s;
}
public <T> T accept(C3aVisitor <T> visitor) {
return visitor.visit(this);
}
}
package c3a;
public class C3aInstDiv extends C3aInst{
public C3aOperand op1; // opérande 1 (ou null)
public C3aOperand op2; // opérande 2 (ou null)
public C3aOperand result; // résultat ou destination du saut (ou NULL)
public C3aInstDiv(C3aOperand op1, C3aOperand op2, C3aOperand result, String comment){
this.op1 = op1;
this.op2 = op2;
this.result = result;
this.comment = comment;
}
public String toString(){
if(this.label != null)
return this.label + "\t" + this.result + " = " + this.op1 + " / " + this.op2 + this.comment;
else
return "\t" + this.result + " = " + this.op1 + " / " + this.op2 + this.comment;
}
public <T> T accept(C3aVisitor <T> visitor) {
return visitor.visit(this);
}
}
package c3a;
import ts.*;
public class C3aInstFBegin extends C3aInst{
public TsItemFct val;
public C3aInstFBegin(TsItemFct val, String comment){
this.val = val;
this.comment = comment;
}
public String toString(){
// String s = "";
// if(this.label != null)
// s = s + this.label;
return this.val.getIdentif() + "\tfbegin\t#" + this.comment;
// return s;
}
public <T> T accept(C3aVisitor <T> visitor) {
return visitor.visit(this);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment