Start working on the code generator
This commit is contained in:
parent
8e80cc2985
commit
e8cc6ade11
57
src/main/java/de/dhbwstuttgart/target/bytecode/Codegen.java
Executable file
57
src/main/java/de/dhbwstuttgart/target/bytecode/Codegen.java
Executable 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();
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -2,4 +2,6 @@ package de.dhbwstuttgart.target.tree.type;
|
||||
|
||||
public sealed interface TargetType
|
||||
permits TargetExtendsWildcard, TargetFunNType, TargetGenericType, TargetRefType, TargetSuperWildcard {
|
||||
|
||||
public String toSignature();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user