getelementptr

This commit is contained in:
Rochas
2025-04-30 16:11:55 +02:00
parent 15ad215e6f
commit a1f597c67e
5 changed files with 24 additions and 1 deletions

View File

@@ -115,7 +115,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
h = r.symTable; h = r.symTable;
list.add(new AssignLLVMImp(new VarLLVMImp(t2, r.var,false),new allocaLLVMImp(t2))); list.add(new AssignLLVMImp(new VarLLVMImp(t2, r.var,false),new allocaLLVMImp(t2)));
}else { }else {
TypeLLVM arrayType = new ArrayLLVMImp(t2, size); TypeLLVM arrayType = new PointerLLVMImp(t2);
//Alloca //Alloca
Result r_size= h.addNewTempVar(); Result r_size= h.addNewTempVar();

View File

@@ -55,6 +55,7 @@ public interface Interface {
public S visitVarLLVM(VarLLVMImp e,H h); public S visitVarLLVM(VarLLVMImp e,H h);
public S visitIcmpLLVM(IcmpLLVMImp e, H h); public S visitIcmpLLVM(IcmpLLVMImp e, H h);
public S visitCallLLVM(CallLLVMImp e, H h); public S visitCallLLVM(CallLLVMImp e, H h);
public S visitGetElementPtrLLVM(GetElementPtr getElementPtr, H h);
} }
/*public interface IdentifierLLVM{ //globaux @ et local % /*public interface IdentifierLLVM{ //globaux @ et local %

View File

@@ -120,6 +120,13 @@ TypeLLVMVisitor<String,String>
return str + e.type().accept(this,h) + " " + e.val1().accept(this,h) + ", " + e.val2().accept(this,h); return str + e.type().accept(this,h) + " " + e.val1().accept(this,h) + ", " + e.val2().accept(this,h);
} }
@Override
public String visitGetElementPtrLLVM(GetElementPtr exp, String h) {
PointerLLVMImp type = (PointerLLVMImp)exp.getType();
//getelementptr <type de sortie>, <type d'entrée> <Var d'entrée>, <type de l'indice> <indice>
return "getelementptr " + type.type().accept(this, h)+ ", " + exp.ptrVar().type().accept(this, h) + " " + exp.ptrVar().accept(this, h) + ", " + exp.i().getType().accept(this, h) + exp.i().accept(this, h);
}
@Override @Override
public String visitIcmpLLVM(IcmpLLVMImp e, String h) { public String visitIcmpLLVM(IcmpLLVMImp e, String h) {
return "icmp ne " + e.val1().getType().accept(this, h) +" "+ e.val1().accept(this, h) + ", " + e.val2().accept(this, h); return "icmp ne " + e.val1().getType().accept(this, h) +" "+ e.val1().accept(this, h) + ", " + e.val2().accept(this, h);

View File

@@ -168,6 +168,20 @@ public class ProgramLLVM {
} }
} }
public static record GetElementPtr(VarLLVMImp ptrVar, ExpressionLLVM i) implements ExpressionLLVM{
@Override
public <H, S> S accept(ExpressionLLVMVisitor<H, S> v, H h) {
return v.visitGetElementPtrLLVM(this, h);
}
@Override
public TypeLLVM getType() {
return ptrVar.type();
}
}
//Val //Val
public static record ValLLVMImp(TypeLLVM type, int val) implements ValLLVM{ public static record ValLLVMImp(TypeLLVM type, int val) implements ValLLVM{

View File

@@ -2,4 +2,5 @@ FUNC INT main() {
INT a,b[10],c,d[11] INT a,b[10],c,d[11]
a:=1 a:=1
c:=2 c:=2
b[1] := 2
} }