Select Git revision
C3a2nasm.java
Forked from
NASR Alexis / 2024_compilation
4 commits behind, 29 commits ahead of the upstream repository.
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
C3a2nasm.java 7.44 KiB
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;
private NasmLabel label;
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 visit(C3a c3a){
return null;
}
public NasmOperand visit(C3aInstAdd inst){
NasmOperand label = (inst.label != null) ?
inst.label.accept(this)
: null;
nasm.ajouteInst(new NasmMov(label,
inst.result.accept(this),
inst.op1.accept(this), ""));
nasm.ajouteInst(new NasmAdd(null,
inst.result.accept(this),
inst.op2.accept(this), ""));
return null;
}
public NasmOperand visit(C3aInstCall inst){
NasmOperand label = (inst.label != null) ? inst.label.accept(this):null;
NasmOperand addr = inst.result.accept(this);
nasm.ajouteInst(new NasmCall(label,addr,""));
return null;
}
private NasmOperand getLabelFromC3aInst(C3aInst inst) {
return new NasmLabel(inst.label.toString());
}
public NasmOperand visit(C3aInstFBegin inst){
NasmOperand label = (inst.label != null) ? inst.label.accept(this) : null;
nasm.ajouteInst(new NasmInst() {
@Override
void addLabel(String formatInst, NasmOperand label) {
super.addLabel(formatInst, label);
}
});
return null;
}
public NasmOperand visit(C3aInst inst){
NasmOperand label = (inst.label != null) ? inst.label.accept(this) : null;
NasmOperand addr = inst.label.accept(this);
NasmOperand destination = inst.label.accept(this);
NasmOperand source = inst.label.accept(this);
nasm.ajouteInst(new NasmInst() {
void addLabel(String formatInst, NasmOperand label) {
super.addLabel(formatInst, label);
}
});
return null;
}
public NasmOperand visit(C3aInstJumpIfLess inst){
NasmOperand label = (inst.label != null) ? inst.label.accept(this): null;
NasmOperand oper1 = inst.op1.accept(this);
NasmOperand oper2 = inst.op2.accept(this);
NasmOperand result = inst.result.accept(this);
nasm.ajouteInst(new NasmCmp(label,oper1,oper2,""));
nasm.ajouteInst(new NasmJle(null, result, ""));
return null;
}
public NasmOperand visit(C3aInstMult inst){
NasmOperand label = (inst.label != null) ? inst.label.accept(this) : null;
nasm.ajouteInst(new NasmMov(label, inst.result.accept(this), inst.op1.accept(this),""));
nasm.ajouteInst(new NasmMul(null, inst.result.accept(this), inst.op2.accept(this),""));
return null;
}
public NasmOperand visit(C3aInstRead inst){
nasm.ajouteInst(new NasmMov (getLabelFromC3aInst(inst),
nasm.newRegister(),
new NasmLabel("sinput"), ""));
nasm.ajouteInst(new NasmCall(null,
new NasmLabel("readline"), ""));
nasm.ajouteInst(new NasmCall(null,
new NasmLabel("atoi"), ""));
nasm.ajouteInst(new NasmMov (null,
inst.result.accept(this),
nasm.newRegister() , ""));
return null;
}
public NasmOperand visit(C3aInstSub inst){
NasmOperand label = (inst.label != null) ? inst.label.accept(this) : null;
nasm.ajouteInst(new NasmMov(label, inst.result.accept(this), inst.op1.accept(this),""));
nasm.ajouteInst(new NasmSub(null, inst.result.accept(this), inst.op2.accept(this),""));
return null;
}
public NasmOperand visit(C3aInstAffect inst) {
NasmOperand label = (inst.label != null) ? inst.label.accept(this): null;
NasmOperand oper1 = inst.op1.accept(this);
NasmOperand result = inst.result.accept(this);
nasm.ajouteInst(new NasmMov(label, inst.result.accept(this), inst.op1.accept(this),""));
nasm.ajouteInst(new NasmInst() {
void addLabel(String formatInst, NasmOperand label) {
super.addLabel(formatInst, label);
}
});
return null;
}
// à revoir
public NasmOperand visit(C3aInstDiv inst){
NasmOperand label = (inst.label != null) ? inst.label.accept(this) : null;
nasm.ajouteInst(new NasmMov(label, inst.result.accept(this), inst.op1.accept(this),""));
nasm.ajouteInst(new NasmDiv(null, inst.result.accept(this), inst.op2.accept(this),""));
return null;
}
//à revoir
public NasmOperand visit(C3aInstFEnd inst){
NasmOperand label = (inst.label != null) ? inst.label.accept(this) : null;
nasm.ajouteInst(new NasmInst() {
void addLabel(String formatInst, NasmOperand label) {
super.addLabel(formatInst, label);
}
});
return null;
}
public NasmOperand visit(C3aInstJumpIfEqual inst){
NasmOperand label = (inst.label != null) ? inst.label.accept(this): null;
NasmOperand oper1 = inst.op1.accept(this);
NasmOperand oper2 = inst.op2.accept(this);
NasmOperand result = inst.result.accept(this);
nasm.ajouteInst(new NasmCmp(label,oper1,oper2,""));
nasm.ajouteInst(new NasmJe(null, result, ""));
return null;
}
public NasmOperand visit(C3aInstJumpIfNotEqual inst){
NasmOperand label = (inst.label != null) ? inst.label.accept(this): null;
NasmOperand oper1 = inst.op1.accept(this);
NasmOperand oper2 = inst.op2.accept(this);
NasmOperand result = inst.result.accept(this);
nasm.ajouteInst(new NasmCmp(label,oper1,oper2,""));
nasm.ajouteInst(new NasmJne(null, result, ""));
return null;
}
public NasmOperand visit(C3aInstJump inst){
NasmOperand label = (inst.label != null) ? inst.label.accept(this):null;
NasmOperand addr = inst.result.accept(this);
nasm.ajouteInst(new NasmJmp(label,addr,""));
return null;
}
public NasmOperand visit(C3aInstParam inst){
NasmOperand label = (inst.label != null) ? inst.label.accept(this): null;
//nasm.ajouteInst(new NasmMov(label, ));
return null;
}
public NasmOperand visit(C3aInstReturn inst){
return null;
}
public NasmOperand visit(C3aInstWrite inst){
nasm.ajouteInst(new NasmMov (getLabelFromC3aInst(inst)));
nasm.ajouteInst(new NasmCall(null, new NasmLabel("iprintLF"), ""));
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 new NasmLabel("l" + label.number);
}
public NasmOperand visit(C3aTemp oper){
return null;
}
public NasmOperand visit(C3aVar oper){
return null;
}
public NasmOperand visit(C3aFunction oper){
return null;
}
}