From b173d776209987bf791eef8f7afce9e4739b4da9 Mon Sep 17 00:00:00 2001 From: i22007 Date: Wed, 8 May 2024 13:06:27 +0200 Subject: [PATCH 1/2] Fixed change errors --- src/main/java/Main.java | 8 ++-- src/main/java/bytecode/ByteCodeGenerator.java | 7 ++-- src/main/java/bytecode/ClassCodeGen.java | 39 +++++++++++++++++++ 3 files changed, 47 insertions(+), 7 deletions(-) create mode 100644 src/main/java/bytecode/ClassCodeGen.java diff --git a/src/main/java/Main.java b/src/main/java/Main.java index ab4a4fe..309d16f 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,11 +1,11 @@ +import ast.ClassNode; +import ast.ProgramNode; import bytecode.ByteCodeGenerator; import org.antlr.v4.runtime.CharStream; import org.antlr.v4.runtime.CharStreams; import org.antlr.v4.runtime.CommonTokenStream; import org.antlr.v4.runtime.tree.ParseTree; import parser.ASTBuilder; -import parser.ClassDeclarationNode; -import parser.ProgramNode; import parser.generated.SimpleJavaLexer; import parser.generated.SimpleJavaParser; @@ -37,8 +37,8 @@ public class Main { // Optionally print or process the AST System.out.println("Parsed " + ast.classes.size() + " classes with identifiers/names:"); - for (ClassDeclarationNode classNode : ast.classes) { - System.out.println(classNode.identifier); + for (ClassNode classNode : ast.classes) { + System.out.println(classNode.name); } ByteCodeGenerator byteCodeGenerator = new ByteCodeGenerator(); diff --git a/src/main/java/bytecode/ByteCodeGenerator.java b/src/main/java/bytecode/ByteCodeGenerator.java index b00f429..60c8d46 100644 --- a/src/main/java/bytecode/ByteCodeGenerator.java +++ b/src/main/java/bytecode/ByteCodeGenerator.java @@ -3,6 +3,7 @@ package bytecode; import java.io.FileOutputStream; import java.io.IOException; +import ast.ClassNode; import org.objectweb.asm.ClassWriter; import org.objectweb.asm.Opcodes; import ast.ProgramNode; @@ -10,9 +11,9 @@ import ast.ProgramNode; public class ByteCodeGenerator { public void generateByteCode(ProgramNode ast) { - for (ClassDeclarationNode classDeclarationNode : ast.classes) { + for (ClassNode classNode : ast.classes) { ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS); - classWriter.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, classDeclarationNode.identifier, null, + classWriter.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, classNode.name, null, "java/lang/Object", null); FieldCodeGen fieldCodeGen = new FieldCodeGen(); @@ -22,7 +23,7 @@ public class ByteCodeGenerator { methodCodeGen.generateMethodCode(classWriter); classWriter.visitEnd(); - printIntoClassFile(classWriter.toByteArray(), classDeclarationNode.identifier); + printIntoClassFile(classWriter.toByteArray(), classNode.name); classWriter.visitEnd(); } diff --git a/src/main/java/bytecode/ClassCodeGen.java b/src/main/java/bytecode/ClassCodeGen.java new file mode 100644 index 0000000..619068a --- /dev/null +++ b/src/main/java/bytecode/ClassCodeGen.java @@ -0,0 +1,39 @@ +package bytecode; + +import ast.ClassNode; +import org.objectweb.asm.ClassWriter; +import org.objectweb.asm.Opcodes; + +import java.io.FileOutputStream; +import java.io.IOException; + +public class ClassCodeGen { + public void generateClassCode(ClassNode classNode) { + ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS); + classWriter.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, classNode.name, null, + "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(); + } + } +} From ce2ea07f9686513fd6ece4ae8378e21f034bf72c Mon Sep 17 00:00:00 2001 From: i22007 Date: Wed, 8 May 2024 13:10:29 +0200 Subject: [PATCH 2/2] Move code generation --- src/main/java/bytecode/ByteCodeGenerator.java | 33 ++----------------- 1 file changed, 2 insertions(+), 31 deletions(-) diff --git a/src/main/java/bytecode/ByteCodeGenerator.java b/src/main/java/bytecode/ByteCodeGenerator.java index 60c8d46..f8a5570 100644 --- a/src/main/java/bytecode/ByteCodeGenerator.java +++ b/src/main/java/bytecode/ByteCodeGenerator.java @@ -1,43 +1,14 @@ package bytecode; -import java.io.FileOutputStream; -import java.io.IOException; - import ast.ClassNode; -import org.objectweb.asm.ClassWriter; -import org.objectweb.asm.Opcodes; import ast.ProgramNode; public class ByteCodeGenerator { public void generateByteCode(ProgramNode ast) { for (ClassNode classNode : ast.classes) { - ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS); - classWriter.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, classNode.name, null, - "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(); + ClassCodeGen classCodeGen = new ClassCodeGen(); + classCodeGen.generateClassCode(classNode); } } }