mirror of
https://github.com/JonathanFleischmann/CompilerULTIMATE.git
synced 2024-12-27 09:28:03 +00:00
refactor ctx structure
This commit is contained in:
parent
ba97c04414
commit
4fe433f961
16
src/main/java/de/maishai/typedast/ClassContext.java
Normal file
16
src/main/java/de/maishai/typedast/ClassContext.java
Normal file
@ -0,0 +1,16 @@
|
||||
package de.maishai.typedast;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import org.objectweb.asm.ClassWriter;
|
||||
|
||||
@Data
|
||||
public class ClassContext {
|
||||
private Type name;
|
||||
private ClassWriter cw;
|
||||
|
||||
public ClassContext(String name, ClassWriter cw) {
|
||||
this.name = Type.REFERENCE(name);
|
||||
this.cw = cw;
|
||||
}
|
||||
}
|
@ -9,16 +9,16 @@ import java.util.Map;
|
||||
import java.util.Stack;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@Data
|
||||
public class MethodContext {
|
||||
private record LocalVariable(String name, int index, Type type) {}
|
||||
private Label startLabel;
|
||||
private Label endLabel;
|
||||
private MethodVisitor mv;
|
||||
private String classname;
|
||||
private int localVarIndex = 0;
|
||||
private Map<String, Integer> variableIndex = new HashMap<>();
|
||||
//private Stack<Integer> stack = new Stack<>();
|
||||
private final Map<String, LocalVariable> variableIndex = new HashMap<>();
|
||||
private Stack<Integer> stack = new Stack<>();
|
||||
private int maxStack = 0;
|
||||
|
||||
public MethodContext(MethodVisitor mv) {
|
||||
startLabel = new Label();
|
||||
@ -28,16 +28,35 @@ public class MethodContext {
|
||||
mv.visitLabel(startLabel);
|
||||
}
|
||||
|
||||
public int addVariable(String name) {
|
||||
public int addVariable(String name, Type type) {
|
||||
int index = localVarIndex;
|
||||
localVarIndex++;
|
||||
variableIndex.put(name, index);
|
||||
variableIndex.put(name, new LocalVariable(name, index, type));
|
||||
return index;
|
||||
}
|
||||
|
||||
public void wrapUp(int maxStack, int maxLocals) {
|
||||
public int getVariableIndex(String name) {
|
||||
int index = variableIndex.get(name).index;
|
||||
if (index == -1) {
|
||||
throw new RuntimeException("Variable not declared");
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
public void pushStack() {
|
||||
stack.push(localVarIndex);
|
||||
if (stack.size() > maxStack) {
|
||||
maxStack = stack.size();
|
||||
}
|
||||
}
|
||||
|
||||
public void popStack() {
|
||||
stack.pop();
|
||||
}
|
||||
|
||||
public void wrapUp() {
|
||||
mv.visitLabel(endLabel);
|
||||
mv.visitMaxs(maxStack, maxLocals);
|
||||
mv.visitMaxs(maxStack, localVarIndex);
|
||||
mv.visitEnd();
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,7 @@
|
||||
package de.maishai.typedast;
|
||||
|
||||
import org.objectweb.asm.MethodVisitor;
|
||||
|
||||
public interface TypedExpression extends TypedNode {
|
||||
public void codeGen(MethodVisitor mv, MethodContext ctx);
|
||||
public void codeGen(MethodContext ctx);
|
||||
|
||||
public Type getType();
|
||||
}
|
||||
|
@ -1,7 +1,6 @@
|
||||
package de.maishai.typedast;
|
||||
|
||||
import org.objectweb.asm.MethodVisitor;
|
||||
|
||||
public interface TypedStatement extends TypedNode {
|
||||
public void codeGen(MethodVisitor mv, MethodContext ctx);
|
||||
public void codeGen(MethodContext ctx);
|
||||
}
|
||||
|
@ -56,11 +56,10 @@ public class TypedAssignment implements TypedStatement {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void codeGen(MethodVisitor mv, MethodContext ctx) {
|
||||
public void codeGen(MethodContext ctx) {
|
||||
if (value instanceof TypedIntLiteral) {
|
||||
TypedIntLiteral intLiteral = (TypedIntLiteral) value;
|
||||
mv.visitIntInsn(Opcodes.BIPUSH, ((TypedIntLiteral) value).getValue());
|
||||
mv.visitVarInsn(Opcodes.ISTORE, 1);
|
||||
ctx.getMv().visitIntInsn(Opcodes.BIPUSH, ((TypedIntLiteral) value).getValue());
|
||||
ctx.getMv().visitVarInsn(Opcodes.ISTORE, ctx.getLocalVarIndex());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -63,7 +63,7 @@ public class TypedBinary implements TypedExpression {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void codeGen(MethodVisitor mv, MethodContext ctx) {
|
||||
public void codeGen(MethodContext ctx) {
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -116,12 +116,12 @@ public class TypedBlock implements TypedNode {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void codeGen(MethodVisitor mv, MethodContext ctx) {
|
||||
public void codeGen(MethodContext ctx) {
|
||||
for (TypedLocalVariable var : vars) {
|
||||
//var.codeGen(mv, ctx);
|
||||
//var.codeGen(ctx);
|
||||
}
|
||||
for (TypedStatement stmt : stmts) {
|
||||
stmt.codeGen(mv, ctx);
|
||||
stmt.codeGen(ctx);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -35,11 +35,11 @@ public class TypedBoolLiteral implements TypedExpression {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void codeGen(MethodVisitor mv, MethodContext ctx) {
|
||||
public void codeGen(MethodContext ctx) {
|
||||
if(value){
|
||||
mv.visitInsn(Opcodes.ICONST_1);
|
||||
ctx.getMv().visitInsn(Opcodes.ICONST_1);
|
||||
}else{
|
||||
mv.visitInsn(Opcodes.ICONST_0);
|
||||
ctx.getMv().visitInsn(Opcodes.ICONST_0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -24,7 +24,7 @@ public class TypedBreak implements TypedStatement {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void codeGen(MethodVisitor mv, MethodContext ctx) {
|
||||
public void codeGen(MethodContext ctx) {
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -29,7 +29,7 @@ public class TypedCharLiteral implements TypedExpression {
|
||||
|
||||
|
||||
@Override
|
||||
public void codeGen(MethodVisitor mv, MethodContext ctx) {
|
||||
mv.visitLdcInsn(value);
|
||||
public void codeGen(MethodContext ctx) {
|
||||
ctx.getMv().visitLdcInsn(value);
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package de.maishai.typedast.typedclass;
|
||||
import de.maishai.ast.Operator;
|
||||
import de.maishai.ast.records.*;
|
||||
import de.maishai.ast.records.Class;
|
||||
import de.maishai.typedast.ClassContext;
|
||||
import de.maishai.typedast.CodeGenUtils;
|
||||
import de.maishai.typedast.Type;
|
||||
import lombok.AllArgsConstructor;
|
||||
@ -196,17 +197,18 @@ public class TypedClass implements TypedNode {
|
||||
|
||||
public byte[] codeGen() {
|
||||
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
|
||||
ClassContext ctx = new ClassContext(className, cw);
|
||||
cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, className, null, "java/lang/Object", null);
|
||||
for (TypedField field : typedFields) {
|
||||
field.codeGen(cw);
|
||||
}
|
||||
|
||||
for (TypedConstructor constructor : typedConstructors) {
|
||||
constructor.codeGen(cw);
|
||||
constructor.codeGen(ctx);
|
||||
}
|
||||
|
||||
for (TypedMethod m : typedMethods) {
|
||||
m.codeGen(cw);
|
||||
m.codeGen(ctx);
|
||||
}
|
||||
|
||||
return cw.toByteArray();
|
||||
|
@ -2,10 +2,7 @@ package de.maishai.typedast.typedclass;
|
||||
|
||||
import de.maishai.ast.records.Constructor;
|
||||
import de.maishai.ast.records.Parameter;
|
||||
import de.maishai.typedast.CodeGenUtils;
|
||||
import de.maishai.typedast.MethodContext;
|
||||
import de.maishai.typedast.TypedNode;
|
||||
import de.maishai.typedast.Type;
|
||||
import de.maishai.typedast.*;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@ -55,20 +52,20 @@ public class TypedConstructor implements TypedNode {
|
||||
}
|
||||
|
||||
|
||||
public void codeGen(ClassWriter cw) {
|
||||
public void codeGen(ClassContext ctx) {
|
||||
int accessModifier = Opcodes.ACC_PUBLIC; // ist laut Andi ok
|
||||
MethodVisitor mv = cw.visitMethod(accessModifier, "<init>", CodeGenUtils.generateDescriptor(typedParameters, Type.VOID), null, null);
|
||||
MethodContext context = new MethodContext(mv);
|
||||
context.addVariable("this");
|
||||
typedParameters.forEach(param -> context.addVariable(param.getParaName()));
|
||||
MethodVisitor mv = ctx.getCw().visitMethod(accessModifier, "<init>", CodeGenUtils.generateDescriptor(typedParameters, Type.VOID), null, null);
|
||||
MethodContext mctx = new MethodContext(mv);
|
||||
mctx.addVariable("this", ctx.getName());
|
||||
typedParameters.forEach(param -> mctx.addVariable(param.getParaName(), param.getType()));
|
||||
//super();
|
||||
mv.visitVarInsn(Opcodes.ALOAD, 0);
|
||||
mv.visitVarInsn(Opcodes.ALOAD, mctx.getVariableIndex("this"));
|
||||
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
|
||||
|
||||
typedBlock.codeGen(mv, context);
|
||||
typedBlock.codeGen(mctx);
|
||||
|
||||
mv.visitInsn(Opcodes.RETURN);
|
||||
|
||||
context.wrapUp(0,0);
|
||||
mctx.wrapUp();
|
||||
}
|
||||
}
|
||||
|
@ -34,7 +34,7 @@ public class TypedDoWhile implements TypedStatement {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void codeGen(MethodVisitor mv, MethodContext ctx) {
|
||||
public void codeGen(MethodContext ctx) {
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -60,7 +60,7 @@ public class TypedFieldVarAccess implements TypedExpression {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void codeGen(MethodVisitor mv, MethodContext ctx) {
|
||||
public void codeGen(MethodContext ctx) {
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -42,7 +42,7 @@ public class TypedFor implements TypedStatement {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void codeGen(MethodVisitor mv, MethodContext ctx) {
|
||||
public void codeGen(MethodContext ctx) {
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -42,18 +42,18 @@ public class TypedIfElse implements TypedStatement {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void codeGen(MethodVisitor mv, MethodContext ctx) {
|
||||
public void codeGen(MethodContext ctx) {
|
||||
Label falseLabel = new Label();
|
||||
Label end = new Label();
|
||||
typedCon.codeGen(mv, ctx);
|
||||
mv.visitJumpInsn(Opcodes.IFEQ, falseLabel);
|
||||
ifTypedBlock.codeGen(mv, ctx);
|
||||
mv.visitJumpInsn(Opcodes.GOTO, end);
|
||||
typedCon.codeGen(ctx);
|
||||
ctx.getMv().visitJumpInsn(Opcodes.IFEQ, falseLabel);
|
||||
ifTypedBlock.codeGen(ctx);
|
||||
ctx.getMv().visitJumpInsn(Opcodes.GOTO, end);
|
||||
|
||||
mv.visitLabel(falseLabel);
|
||||
ctx.getMv().visitLabel(falseLabel);
|
||||
if (elseTypedBlock != null) {
|
||||
elseTypedBlock.codeGen(mv, ctx);
|
||||
elseTypedBlock.codeGen(ctx);
|
||||
}
|
||||
mv.visitLabel(end);
|
||||
ctx.getMv().visitLabel(end);
|
||||
}
|
||||
}
|
||||
|
@ -38,7 +38,7 @@ public class TypedIntLiteral implements TypedExpression {
|
||||
|
||||
|
||||
@Override
|
||||
public void codeGen(MethodVisitor mv, MethodContext ctx) {
|
||||
mv.visitLdcInsn(value);
|
||||
public void codeGen(MethodContext ctx) {
|
||||
ctx.getMv().visitLdcInsn(value);
|
||||
}
|
||||
}
|
||||
|
@ -40,9 +40,6 @@ public final class TypedLocalVariable implements TypedNode {
|
||||
}
|
||||
|
||||
public void codeGen(MethodVisitor mv, MethodContext ctx) {
|
||||
System.out.println("Generating code for local variable " + name);
|
||||
int index = ctx.addVariable(name);
|
||||
mv.visitLocalVariable(name, type.getDescriptor(), null, ctx.getStartLabel(), ctx.getEndLabel(), index);
|
||||
|
||||
ctx.addVariable(name, type);
|
||||
}
|
||||
}
|
||||
|
@ -37,8 +37,8 @@ public class TypedMethod implements TypedNode {
|
||||
if (method.getName().equals(name) && method.getTypedParameters().size() == typedParameters.size()
|
||||
&& method.getReturnType().equals(returnType)) {
|
||||
|
||||
for (int i = 0; i < method.getTypedParameters().size(); i++){
|
||||
if(method.getTypedParameters().get(i).getType().equals(typedParameters.get(i).getType())){
|
||||
for (int i = 0; i < method.getTypedParameters().size(); i++) {
|
||||
if (method.getTypedParameters().get(i).getType().equals(typedParameters.get(i).getType())) {
|
||||
throw new RuntimeException("Method " + name + " already exists");
|
||||
}
|
||||
}
|
||||
@ -69,16 +69,16 @@ public class TypedMethod implements TypedNode {
|
||||
}
|
||||
|
||||
|
||||
public void codeGen(ClassWriter cw) {
|
||||
public void codeGen(ClassContext ctx) {
|
||||
int accessModifier = Opcodes.ACC_PUBLIC; // ist laut Andi ok
|
||||
MethodVisitor mv = cw.visitMethod(accessModifier, name,
|
||||
MethodVisitor mv = ctx.getCw().visitMethod(accessModifier, name,
|
||||
CodeGenUtils.generateDescriptor(typedParameters, returnType), null, null);
|
||||
MethodContext context = new MethodContext(mv);
|
||||
context.addVariable("this");
|
||||
typedParameters.forEach(param -> context.addVariable(param.getParaName()));
|
||||
context.addVariable("this", ctx.getName());
|
||||
typedParameters.forEach(param -> context.addVariable(param.getParaName(), param.getType()));
|
||||
|
||||
typedBlock.codeGen(mv, context);
|
||||
context.wrapUp(0, 0);
|
||||
typedBlock.codeGen(context);
|
||||
context.wrapUp();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -38,7 +38,7 @@ public class TypedMethodCall implements TypedExpression, TypedStatement {
|
||||
|
||||
|
||||
@Override
|
||||
public void codeGen(MethodVisitor mv, MethodContext ctx) {
|
||||
public void codeGen(MethodContext ctx) {
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -52,7 +52,7 @@ public class TypedNew implements TypedExpression, TypedStatement {
|
||||
|
||||
|
||||
@Override
|
||||
public void codeGen(MethodVisitor mv, MethodContext ctx) {
|
||||
public void codeGen(MethodContext ctx) {
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -42,15 +42,15 @@ public class TypedReturn implements TypedStatement {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void codeGen(MethodVisitor mv, MethodContext ctx) {
|
||||
public void codeGen(MethodContext ctx) {
|
||||
if (ret == null) {
|
||||
mv.visitInsn(Opcodes.RETURN);
|
||||
ctx.getMv().visitInsn(Opcodes.RETURN);
|
||||
} else {
|
||||
ret.codeGen(mv, ctx);
|
||||
ret.codeGen(ctx);
|
||||
if (ret.getType().getKind() != Type.Kind.REFERENCE) {
|
||||
mv.visitInsn(Opcodes.IRETURN);
|
||||
ctx.getMv().visitInsn(Opcodes.IRETURN);
|
||||
} else {
|
||||
mv.visitInsn(Opcodes.ARETURN);
|
||||
ctx.getMv().visitInsn(Opcodes.ARETURN);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -46,14 +46,14 @@ public class TypedUnary implements TypedExpression {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void codeGen(MethodVisitor mv, MethodContext ctx) {
|
||||
right.codeGen(mv, ctx);
|
||||
public void codeGen(MethodContext ctx) {
|
||||
right.codeGen(ctx);
|
||||
if(op == UnaryOperator.NOT){
|
||||
mv.visitInsn(Opcodes.ICONST_1);
|
||||
mv.visitInsn(Opcodes.IXOR);
|
||||
ctx.getMv().visitInsn(Opcodes.ICONST_1);
|
||||
ctx.getMv().visitInsn(Opcodes.IXOR);
|
||||
}
|
||||
if(op == UnaryOperator.SUB){
|
||||
mv.visitInsn(Opcodes.INEG);
|
||||
ctx.getMv().visitInsn(Opcodes.INEG);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -35,7 +35,7 @@ public class TypedWhile implements TypedStatement {
|
||||
|
||||
|
||||
@Override
|
||||
public void codeGen(MethodVisitor mv, MethodContext ctx) {
|
||||
public void codeGen(MethodContext ctx) {
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user