From e8cc6ade11f5129e250e0a50bba4aac1ab413e4a Mon Sep 17 00:00:00 2001 From: Victorious3 Date: Wed, 27 Apr 2022 13:05:01 +0200 Subject: [PATCH] Start working on the code generator --- .../target/bytecode/Codegen.java | 57 +++++++++++++++++++ .../target/tree/TargetConstructor.java | 5 ++ .../target/tree/TargetField.java | 6 +- .../target/tree/TargetMethod.java | 4 ++ .../target/tree/type/TargetRefType.java | 4 ++ .../target/tree/type/TargetType.java | 2 + 6 files changed, 77 insertions(+), 1 deletion(-) create mode 100755 src/main/java/de/dhbwstuttgart/target/bytecode/Codegen.java 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 00000000..90a04fb0 --- /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 b93219ff..94aab071 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 cbba1dec..c466b159 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 6538b7a5..9c50f6e7 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 1c27cd62..33c38f5d 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 eed6dcab..f4d8f7d0 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(); }