Start working on the code generator

This commit is contained in:
Victorious3 2022-04-27 13:05:01 +02:00
parent 8e80cc2985
commit e8cc6ade11
6 changed files with 77 additions and 1 deletions

View File

@ -0,0 +1,57 @@
package de.dhbwstuttgart.target.bytecode;
import de.dhbwstuttgart.target.tree.TargetClass;
import de.dhbwstuttgart.target.tree.TargetConstructor;
import de.dhbwstuttgart.target.tree.TargetField;
import de.dhbwstuttgart.target.tree.TargetMethod;
import de.dhbwstuttgart.target.tree.expression.TargetBlock;
import de.dhbwstuttgart.target.tree.type.TargetType;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.MethodVisitor;
import static org.objectweb.asm.Opcodes.*;
public class Codegen {
private TargetClass clazz;
private ClassWriter cw;
public Codegen(TargetClass clazz) {
this.clazz = clazz;
this.cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
}
private void generateBlock(MethodVisitor mv, TargetBlock block) {
}
private void generateField(TargetField field) {
cw.visitField(field.access(), field.name(), field.getDescriptor(), null, null);
}
private void generateConstructor(TargetConstructor constructor) {
MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", constructor.getDescriptor(), null, null);
mv.visitEnd();
}
private void generateMethod(TargetMethod method) {
// TODO Access modifier
MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, method.name(), method.getDescriptor(), null, null);
generateBlock(mv, method.block());
mv.visitMaxs(0, 0);
mv.visitEnd();
}
public byte[] generate() {
// TODO Access modifier and signature
cw.visit(V12, ACC_PUBLIC, clazz.qualifiedName(),
null, clazz.superType().toSignature(),
clazz.implementingInterfaces().stream().map(TargetType::toSignature).toArray(String[]::new)
);
clazz.fields().forEach(this::generateField);
clazz.constructors().forEach(this::generateConstructor);
clazz.methods().forEach(this::generateMethod);
cw.visitEnd();
return cw.toByteArray();
}
}

View File

@ -3,5 +3,10 @@ package de.dhbwstuttgart.target.tree;
import java.util.List;
public record TargetConstructor(List<MethodParameter> parameterTypes) {
public String getDescriptor() {
// TODO
return null;
}
}

View File

@ -2,6 +2,10 @@ package de.dhbwstuttgart.target.tree;
import de.dhbwstuttgart.target.tree.type.TargetType;
public record TargetField(TargetType type, String name) {
public record TargetField(int access, TargetType type, String name) {
public String getDescriptor() {
// TODO
return null;
}
}

View File

@ -5,5 +5,9 @@ import de.dhbwstuttgart.target.tree.expression.TargetBlock;
import java.util.List;
public record TargetMethod(String name, List<MethodParameter> parameterTypes, TargetBlock block) {
public String getDescriptor() {
// TODO
return null;
}
}

View File

@ -5,4 +5,8 @@ import de.dhbwstuttgart.target.tree.type.TargetType;
import java.util.List;
public record TargetRefType(String name, List<TargetType> params) implements TargetType {
@Override
public String toSignature() {
return this.name;
}
}

View File

@ -2,4 +2,6 @@ package de.dhbwstuttgart.target.tree.type;
public sealed interface TargetType
permits TargetExtendsWildcard, TargetFunNType, TargetGenericType, TargetRefType, TargetSuperWildcard {
public String toSignature();
}