diff --git a/src/main/java/de/dhbwstuttgart/target/bytecode/Codegen.java b/src/main/java/de/dhbwstuttgart/target/bytecode/Codegen.java new file mode 100755 index 000000000..90a04fb0c --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/target/bytecode/Codegen.java @@ -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, "", 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(); + } +} diff --git a/src/main/java/de/dhbwstuttgart/target/tree/TargetConstructor.java b/src/main/java/de/dhbwstuttgart/target/tree/TargetConstructor.java index b93219ff5..94aab071d 100644 --- a/src/main/java/de/dhbwstuttgart/target/tree/TargetConstructor.java +++ b/src/main/java/de/dhbwstuttgart/target/tree/TargetConstructor.java @@ -3,5 +3,10 @@ package de.dhbwstuttgart.target.tree; import java.util.List; public record TargetConstructor(List parameterTypes) { + + public String getDescriptor() { + // TODO + return null; + } } diff --git a/src/main/java/de/dhbwstuttgart/target/tree/TargetField.java b/src/main/java/de/dhbwstuttgart/target/tree/TargetField.java index cbba1dec3..c466b159c 100644 --- a/src/main/java/de/dhbwstuttgart/target/tree/TargetField.java +++ b/src/main/java/de/dhbwstuttgart/target/tree/TargetField.java @@ -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; + } } diff --git a/src/main/java/de/dhbwstuttgart/target/tree/TargetMethod.java b/src/main/java/de/dhbwstuttgart/target/tree/TargetMethod.java index 6538b7a5a..9c50f6e75 100644 --- a/src/main/java/de/dhbwstuttgart/target/tree/TargetMethod.java +++ b/src/main/java/de/dhbwstuttgart/target/tree/TargetMethod.java @@ -5,5 +5,9 @@ import de.dhbwstuttgart.target.tree.expression.TargetBlock; import java.util.List; public record TargetMethod(String name, List parameterTypes, TargetBlock block) { + public String getDescriptor() { + // TODO + return null; + } } diff --git a/src/main/java/de/dhbwstuttgart/target/tree/type/TargetRefType.java b/src/main/java/de/dhbwstuttgart/target/tree/type/TargetRefType.java index 1c27cd62c..33c38f5d1 100644 --- a/src/main/java/de/dhbwstuttgart/target/tree/type/TargetRefType.java +++ b/src/main/java/de/dhbwstuttgart/target/tree/type/TargetRefType.java @@ -5,4 +5,8 @@ import de.dhbwstuttgart.target.tree.type.TargetType; import java.util.List; public record TargetRefType(String name, List params) implements TargetType { + @Override + public String toSignature() { + return this.name; + } } diff --git a/src/main/java/de/dhbwstuttgart/target/tree/type/TargetType.java b/src/main/java/de/dhbwstuttgart/target/tree/type/TargetType.java index eed6dcab5..f4d8f7d0b 100644 --- a/src/main/java/de/dhbwstuttgart/target/tree/type/TargetType.java +++ b/src/main/java/de/dhbwstuttgart/target/tree/type/TargetType.java @@ -2,4 +2,6 @@ package de.dhbwstuttgart.target.tree.type; public sealed interface TargetType permits TargetExtendsWildcard, TargetFunNType, TargetGenericType, TargetRefType, TargetSuperWildcard { + + public String toSignature(); }