243 lines
5.5 KiB
Java
243 lines
5.5 KiB
Java
|
package de.dhbwstuttgart.bytecode;
|
||
|
|
||
|
import java.util.HashMap;
|
||
|
|
||
|
import org.objectweb.asm.MethodVisitor;
|
||
|
import org.objectweb.asm.Opcodes;
|
||
|
|
||
|
import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal;
|
||
|
import de.dhbwstuttgart.syntaxtree.Method;
|
||
|
import de.dhbwstuttgart.syntaxtree.StatementVisitor;
|
||
|
import de.dhbwstuttgart.syntaxtree.statement.ArgumentList;
|
||
|
import de.dhbwstuttgart.syntaxtree.statement.Assign;
|
||
|
import de.dhbwstuttgart.syntaxtree.statement.AssignToField;
|
||
|
import de.dhbwstuttgart.syntaxtree.statement.Binary;
|
||
|
import de.dhbwstuttgart.syntaxtree.statement.Block;
|
||
|
import de.dhbwstuttgart.syntaxtree.statement.CastExpr;
|
||
|
import de.dhbwstuttgart.syntaxtree.statement.DoStmt;
|
||
|
import de.dhbwstuttgart.syntaxtree.statement.EmptyStmt;
|
||
|
import de.dhbwstuttgart.syntaxtree.statement.FieldVar;
|
||
|
import de.dhbwstuttgart.syntaxtree.statement.ForStmt;
|
||
|
import de.dhbwstuttgart.syntaxtree.statement.IfStmt;
|
||
|
import de.dhbwstuttgart.syntaxtree.statement.InstanceOf;
|
||
|
import de.dhbwstuttgart.syntaxtree.statement.LambdaExpression;
|
||
|
import de.dhbwstuttgart.syntaxtree.statement.LocalVar;
|
||
|
import de.dhbwstuttgart.syntaxtree.statement.LocalVarDecl;
|
||
|
import de.dhbwstuttgart.syntaxtree.statement.MethodCall;
|
||
|
import de.dhbwstuttgart.syntaxtree.statement.NewArray;
|
||
|
import de.dhbwstuttgart.syntaxtree.statement.NewClass;
|
||
|
import de.dhbwstuttgart.syntaxtree.statement.Receiver;
|
||
|
import de.dhbwstuttgart.syntaxtree.statement.Return;
|
||
|
import de.dhbwstuttgart.syntaxtree.statement.ReturnVoid;
|
||
|
import de.dhbwstuttgart.syntaxtree.statement.Statement;
|
||
|
import de.dhbwstuttgart.syntaxtree.statement.StaticClassName;
|
||
|
import de.dhbwstuttgart.syntaxtree.statement.Super;
|
||
|
import de.dhbwstuttgart.syntaxtree.statement.SuperCall;
|
||
|
import de.dhbwstuttgart.syntaxtree.statement.This;
|
||
|
import de.dhbwstuttgart.syntaxtree.statement.UnaryPlus;
|
||
|
import de.dhbwstuttgart.syntaxtree.statement.WhileStmt;
|
||
|
import de.dhbwstuttgart.syntaxtree.statement.literal.Literal;
|
||
|
import de.dhbwstuttgart.syntaxtree.statement.literal.Null;
|
||
|
|
||
|
public class BytecodeGenMethod implements StatementVisitor{
|
||
|
|
||
|
private Method m;
|
||
|
private MethodVisitor mv;
|
||
|
private HashMap<String, Integer> paramsAndLocals = new HashMap<>();
|
||
|
|
||
|
public BytecodeGenMethod(Method m, MethodVisitor mv, HashMap<String, Integer> paramsAndLocals,
|
||
|
boolean isConstructor) {
|
||
|
if(isConstructor) {
|
||
|
|
||
|
// mv.visitVarInsn(Opcodes.ALOAD, 0);
|
||
|
// mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
|
||
|
}
|
||
|
this.m = m;
|
||
|
this.mv = mv;
|
||
|
this.paramsAndLocals = paramsAndLocals;
|
||
|
this.m.block.accept(this);
|
||
|
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void visit(Block block) {
|
||
|
for(Statement stmt : block.getStatements()) {
|
||
|
stmt.accept(this);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void visit(SuperCall superCall) {
|
||
|
superCall.receiver.accept(this);
|
||
|
superCall.arglist.accept(this);
|
||
|
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", superCall.name, "()V");
|
||
|
}
|
||
|
|
||
|
// ??
|
||
|
@Override
|
||
|
public void visit(LocalVar localVar) {
|
||
|
|
||
|
}
|
||
|
// ??
|
||
|
@Override
|
||
|
public void visit(LocalVarDecl localVarDecl) {
|
||
|
// TODO Auto-generated method stub
|
||
|
Integer i;
|
||
|
|
||
|
}
|
||
|
// int c = 5;???
|
||
|
@Override
|
||
|
public void visit(Assign assign) {
|
||
|
assign.rightSide.accept(this);
|
||
|
// int index = paramsAndLocals.get(assign.lefSide.getName());
|
||
|
// mv.visitVarInsn(Opcodes.ASTORE, index);
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void visit(Binary binary) {
|
||
|
// TODO Auto-generated method stub
|
||
|
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void visit(LambdaExpression lambdaExpression) {
|
||
|
// TODO Auto-generated method stub
|
||
|
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void visit(CastExpr castExpr) {
|
||
|
// TODO Auto-generated method stub
|
||
|
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void visit(EmptyStmt emptyStmt) {
|
||
|
// TODO Auto-generated method stub
|
||
|
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void visit(FieldVar fieldVar) {
|
||
|
// TODO Auto-generated method stub
|
||
|
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void visit(ForStmt forStmt) {
|
||
|
// TODO Auto-generated method stub
|
||
|
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void visit(IfStmt ifStmt) {
|
||
|
// TODO Auto-generated method stub
|
||
|
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void visit(InstanceOf instanceOf) {
|
||
|
// TODO Auto-generated method stub
|
||
|
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void visit(MethodCall methodCall) {
|
||
|
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void visit(NewClass methodCall) {
|
||
|
// TODO Auto-generated method stub
|
||
|
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void visit(NewArray newArray) {
|
||
|
// TODO Auto-generated method stub
|
||
|
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void visit(Receiver receiver) {
|
||
|
receiver.expr.accept(this);
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void visit(Return aReturn) {
|
||
|
// TODO Auto-generated method stub
|
||
|
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void visit(ReturnVoid aReturn) {
|
||
|
mv.visitInsn(Opcodes.RETURN);
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void visit(StaticClassName staticClassName) {
|
||
|
// TODO Auto-generated method stub
|
||
|
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void visit(Super aSuper) {
|
||
|
// TODO Auto-generated method stub
|
||
|
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void visit(This aThis) {
|
||
|
mv.visitVarInsn(Opcodes.ALOAD, 0);
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void visit(UnaryPlus unaryPlus) {
|
||
|
// TODO Auto-generated method stub
|
||
|
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void visit(WhileStmt whileStmt) {
|
||
|
// TODO Auto-generated method stub
|
||
|
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void visit(DoStmt whileStmt) {
|
||
|
// TODO Auto-generated method stub
|
||
|
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void visit(Null aNull) {
|
||
|
// TODO Auto-generated method stub
|
||
|
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void visit(Literal literal) {
|
||
|
// TODO Auto-generated method stub
|
||
|
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void visit(ArgumentList argumentList) {
|
||
|
// TODO Auto-generated method stub
|
||
|
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void visit(AssignToField assignLeftSide) {
|
||
|
// TODO Auto-generated method stub
|
||
|
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void visit(AssignToLocal assignLeftSide) {
|
||
|
// TODO Auto-generated method stub
|
||
|
|
||
|
}
|
||
|
|
||
|
}
|