mirror of
https://github.com/JonathanFleischmann/CompilerULTIMATE.git
synced 2024-12-28 17:08:03 +00:00
generate code for fields and constructor (without block tho)
This commit is contained in:
parent
a2fc1686b1
commit
a7eedbe98d
29
src/main/java/de/maishai/typedast/CodeGenUtils.java
Normal file
29
src/main/java/de/maishai/typedast/CodeGenUtils.java
Normal file
@ -0,0 +1,29 @@
|
||||
package de.maishai.typedast;
|
||||
|
||||
import de.maishai.typedast.typedclass.TypedParameter;
|
||||
import org.objectweb.asm.ClassWriter;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class CodeGenUtils {
|
||||
|
||||
public static String generateDescriptor(List<TypedParameter> arguments, Type returnType) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append('(');
|
||||
arguments.forEach(type -> builder.append(type.getType().getDescriptor()));
|
||||
builder.append(')');
|
||||
builder.append(returnType.getDescriptor());
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
public static void writeClassfile(ClassWriter cw, String name) {
|
||||
byte[] code = cw.toByteArray();
|
||||
try {
|
||||
java.io.FileOutputStream fos = new java.io.FileOutputStream("output/" + name + ".class");
|
||||
fos.write(code);
|
||||
fos.close();
|
||||
} catch (java.io.IOException e) {
|
||||
System.out.println("Error writing classfile: " + e);
|
||||
}
|
||||
}
|
||||
}
|
@ -25,4 +25,14 @@ public class Type {
|
||||
return new Type(Kind.REFERENCE, reference);
|
||||
}
|
||||
|
||||
public String getDescriptor(){
|
||||
return switch (kind) {
|
||||
case INT -> "I";
|
||||
case BOOL -> "Z";
|
||||
case CHAR -> "C";
|
||||
case VOID -> "V";
|
||||
case REFERENCE -> "L" + reference + ";";
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -9,10 +9,12 @@ import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Data
|
||||
public class TypedBlock implements Node {
|
||||
private List<TypedLocalVariable> vars;
|
||||
private List<Statement> stmts;
|
||||
|
||||
@Override
|
||||
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
|
||||
Type typeOfLastStmt = Type.VOID;
|
||||
|
@ -1,5 +1,6 @@
|
||||
package de.maishai.typedast.typedclass;
|
||||
|
||||
import de.maishai.typedast.CodeGenUtils;
|
||||
import de.maishai.typedast.Node;
|
||||
import de.maishai.typedast.Type;
|
||||
import lombok.Data;
|
||||
@ -51,6 +52,10 @@ public class TypedClass implements Node {
|
||||
typedClass.getTypedFields().add((TypedField) field.convertToTypedAST(localVar, classes, unTypedAST));
|
||||
}
|
||||
|
||||
for (TypedConstructor constructor : typedConstructors) {
|
||||
typedClass.getTypedConstructors().add((TypedConstructor) constructor.convertToTypedAST(localVar, classes, unTypedAST));
|
||||
}
|
||||
|
||||
for (TypedMethod method : typedMethods) {
|
||||
typedClass.getTypedMethods().add((TypedMethod) method.convertToTypedAST(localVar, classes, unTypedAST));
|
||||
}
|
||||
@ -62,31 +67,38 @@ public class TypedClass implements Node {
|
||||
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
|
||||
cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, typedId.getName(), null, "java/lang/Object", null);
|
||||
for (TypedField field : typedFields) {
|
||||
//field.codeGen(cw);
|
||||
field.codeGen(cw);
|
||||
}
|
||||
|
||||
for (TypedConstructor constructor : typedConstructors) {
|
||||
constructor.codeGen(cw);
|
||||
}
|
||||
|
||||
for (TypedMethod m : typedMethods) {
|
||||
//m.codeGen(cw);
|
||||
}
|
||||
writeClassfile(cw, typedId.getName());
|
||||
}
|
||||
|
||||
public static void writeClassfile(ClassWriter cw, String name) {
|
||||
byte[] code = cw.toByteArray();
|
||||
try {
|
||||
java.io.FileOutputStream fos = new java.io.FileOutputStream("output/" + name + ".class");
|
||||
fos.write(code);
|
||||
fos.close();
|
||||
} catch (java.io.IOException e) {
|
||||
System.out.println("Error writing classfile: " + e);
|
||||
}
|
||||
CodeGenUtils.writeClassfile(cw, typedId.getName());
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
TypedClass c = new TypedClass();
|
||||
c.typedFields = new ArrayList<>();
|
||||
c.typedId = new TypedId("SomeClass");
|
||||
|
||||
//Fields
|
||||
TypedField f1 = new TypedField(new TypedId("someNumber"), Type.INT);
|
||||
TypedField f2 = new TypedField(new TypedId("someChar"), Type.CHAR);
|
||||
c.typedFields = List.of(f1, f2);
|
||||
|
||||
//Constructors
|
||||
TypedConstructor constructor = new TypedConstructor(false, new TypedId("SomeClass"), new ArrayList<>(), new TypedBlock());
|
||||
c.typedConstructors = List.of(constructor);
|
||||
|
||||
//Methods
|
||||
c.typedMethods = new ArrayList<>();
|
||||
c.typedId.setName("Test");
|
||||
|
||||
//codeGen
|
||||
c.codeGen();
|
||||
}
|
||||
}
|
||||
|
@ -3,12 +3,21 @@ package de.maishai.typedast.typedclass;
|
||||
import de.maishai.ast.records.Block;
|
||||
import de.maishai.ast.records.Constructor;
|
||||
import de.maishai.ast.records.Parameter;
|
||||
import de.maishai.typedast.CodeGenUtils;
|
||||
import de.maishai.typedast.Node;
|
||||
import de.maishai.typedast.Type;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.objectweb.asm.ClassWriter;
|
||||
import org.objectweb.asm.MethodVisitor;
|
||||
import org.objectweb.asm.Opcodes;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Data
|
||||
public class TypedConstructor implements Node {
|
||||
|
||||
@ -16,6 +25,7 @@ public class TypedConstructor implements Node {
|
||||
private TypedId typedId;
|
||||
private List<TypedParameter> typedParameters;
|
||||
private TypedBlock typedBlock;
|
||||
|
||||
//constructor: PUBLIC? id '(' params? ')' block;
|
||||
@Override
|
||||
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
|
||||
@ -53,4 +63,22 @@ public class TypedConstructor implements Node {
|
||||
typedConstructor.setTypedBlock(typedBlock);
|
||||
return typedConstructor;
|
||||
}
|
||||
|
||||
public void codeGen(ClassWriter cw) {
|
||||
int accessModifier = Opcodes.ACC_PUBLIC; // ist laut Andi ok
|
||||
MethodVisitor mv = cw.visitMethod(accessModifier, "<init>", CodeGenUtils.generateDescriptor(typedParameters, Type.VOID), null, null);
|
||||
mv.visitCode();
|
||||
|
||||
mv.visitVarInsn(Opcodes.ALOAD, 0);
|
||||
|
||||
//super();
|
||||
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
|
||||
|
||||
//TODO: deal with block
|
||||
|
||||
mv.visitInsn(Opcodes.RETURN);
|
||||
|
||||
mv.visitMaxs(0, 0);
|
||||
mv.visitEnd();
|
||||
}
|
||||
}
|
||||
|
@ -4,10 +4,16 @@ import de.maishai.ast.records.Field;
|
||||
import de.maishai.typedast.Node;
|
||||
import de.maishai.typedast.Type;
|
||||
import de.maishai.typedast.TypeMapper;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.objectweb.asm.ClassWriter;
|
||||
import org.objectweb.asm.Opcodes;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@AllArgsConstructor
|
||||
@RequiredArgsConstructor
|
||||
@Data
|
||||
public class TypedField implements Node {
|
||||
private TypedId typedId;
|
||||
@ -32,5 +38,8 @@ public class TypedField implements Node {
|
||||
return typedField;
|
||||
}
|
||||
|
||||
|
||||
public void codeGen(ClassWriter cw) {
|
||||
int access = Opcodes.ACC_PUBLIC; // laut Andi ist es ok, dass alle Felder public sind
|
||||
cw.visitField(access, typedId.getName(), type.getDescriptor(), null, null).visitEnd();
|
||||
}
|
||||
}
|
||||
|
@ -3,9 +3,11 @@ package de.maishai.typedast.typedclass;
|
||||
import de.maishai.typedast.Node;
|
||||
import de.maishai.typedast.Type;
|
||||
import lombok.Data;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
@Data
|
||||
public class TypedParameter implements Node {
|
||||
private TypedId typedId;
|
||||
|
Loading…
Reference in New Issue
Block a user