Move code generation

This commit is contained in:
i22007 2024-05-08 13:10:29 +02:00
parent b173d77620
commit ce2ea07f96

View File

@ -1,43 +1,14 @@
package bytecode; package bytecode;
import java.io.FileOutputStream;
import java.io.IOException;
import ast.ClassNode; import ast.ClassNode;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes;
import ast.ProgramNode; import ast.ProgramNode;
public class ByteCodeGenerator { public class ByteCodeGenerator {
public void generateByteCode(ProgramNode ast) { public void generateByteCode(ProgramNode ast) {
for (ClassNode classNode : ast.classes) { for (ClassNode classNode : ast.classes) {
ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS); ClassCodeGen classCodeGen = new ClassCodeGen();
classWriter.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, classNode.name, null, classCodeGen.generateClassCode(classNode);
"java/lang/Object", null);
FieldCodeGen fieldCodeGen = new FieldCodeGen();
fieldCodeGen.generateFieldCode(classWriter);
MethodCodeGen methodCodeGen = new MethodCodeGen();
methodCodeGen.generateMethodCode(classWriter);
classWriter.visitEnd();
printIntoClassFile(classWriter.toByteArray(), classNode.name);
classWriter.visitEnd();
}
}
private void printIntoClassFile(byte[] byteCode, String name) {
String filePath = "./classFileOutput/" + name + ".class";
try {
FileOutputStream fileOutputStream = new FileOutputStream(filePath);
fileOutputStream.write(byteCode);
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
} }
} }
} }