From d131d412f05e953803f69c91e34bfad0712f6c6b Mon Sep 17 00:00:00 2001 From: i22007 Date: Wed, 8 May 2024 11:21:42 +0200 Subject: [PATCH 1/2] Add classFIleOutput folder --- classFileOutput/Example.class | Bin 0 -> 82 bytes src/main/java/bytecode/ByteCodeGenerator.java | 27 +++++++++--------- src/main/java/bytecode/FieldCodeGen.java | 10 +++++++ src/main/java/bytecode/MethodCodeGen.java | 16 +++++++++++ 4 files changed, 40 insertions(+), 13 deletions(-) create mode 100644 classFileOutput/Example.class create mode 100644 src/main/java/bytecode/FieldCodeGen.java create mode 100644 src/main/java/bytecode/MethodCodeGen.java diff --git a/classFileOutput/Example.class b/classFileOutput/Example.class new file mode 100644 index 0000000000000000000000000000000000000000..e44ef841c1ac9c7a2dea4460be34708d0b01a613 GIT binary patch literal 82 zcmX^0Z`VEs1_l!bc18wv*NVj4f}B)#21Z5(fvm)`ME#t^ymWp4q^#8B5_Se=Mg}&U h%)HDJJ4Oa(4b3nHMg}GZ7NBkhAd8WKm4OY!1OQO?4`2WQ literal 0 HcmV?d00001 diff --git a/src/main/java/bytecode/ByteCodeGenerator.java b/src/main/java/bytecode/ByteCodeGenerator.java index b69b823..aa0a117 100644 --- a/src/main/java/bytecode/ByteCodeGenerator.java +++ b/src/main/java/bytecode/ByteCodeGenerator.java @@ -2,8 +2,8 @@ package bytecode; import java.io.FileOutputStream; import java.io.IOException; + import org.objectweb.asm.ClassWriter; -import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.Opcodes; import parser.ClassDeclarationNode; import parser.ProgramNode; @@ -12,24 +12,25 @@ public class ByteCodeGenerator { public void generateByteCode(ProgramNode ast) { for (ClassDeclarationNode classDeclarationNode : ast.classes) { - ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS); - cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, classDeclarationNode.identifier, null, - "java/lang/Object", null); + ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS); + classWriter.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, classDeclarationNode.identifier, null, + "java/lang/Object", null); - MethodVisitor constructor = - cw.visitMethod(Opcodes.ACC_PUBLIC, - "", - "()V", - null, - null); - cw.visitEnd(); + FieldCodeGen fieldCodeGen = new FieldCodeGen(); + fieldCodeGen.generateFieldCode(classWriter); - printIntoClassFile(cw.toByteArray(), classDeclarationNode.identifier); + MethodCodeGen methodCodeGen = new MethodCodeGen(); + methodCodeGen.generateMethodCode(classWriter); + + classWriter.visitEnd(); + printIntoClassFile(classWriter.toByteArray(), classDeclarationNode.identifier); + + classWriter.visitEnd(); } } private void printIntoClassFile(byte[] byteCode, String name) { - String filePath = name + ".class"; + String filePath = "./classFileOutput/" + name + ".class"; try { FileOutputStream fileOutputStream = new FileOutputStream(filePath); diff --git a/src/main/java/bytecode/FieldCodeGen.java b/src/main/java/bytecode/FieldCodeGen.java new file mode 100644 index 0000000..4cca429 --- /dev/null +++ b/src/main/java/bytecode/FieldCodeGen.java @@ -0,0 +1,10 @@ +package bytecode; + +import org.objectweb.asm.ClassWriter; + +public class FieldCodeGen { + + public void generateFieldCode(ClassWriter classWriter) { + + } +} diff --git a/src/main/java/bytecode/MethodCodeGen.java b/src/main/java/bytecode/MethodCodeGen.java new file mode 100644 index 0000000..a54fda8 --- /dev/null +++ b/src/main/java/bytecode/MethodCodeGen.java @@ -0,0 +1,16 @@ +package bytecode; + +import org.objectweb.asm.ClassWriter; +import org.objectweb.asm.MethodVisitor; +import org.objectweb.asm.Opcodes; + +public class MethodCodeGen { + public void generateMethodCode(ClassWriter classWriter) { + MethodVisitor constructor = + classWriter.visitMethod(Opcodes.ACC_PUBLIC, + "", + "()V", + null, + null); + } +} From ed6fce0b5a784785007bd3c6aaf2218c9cb4f147 Mon Sep 17 00:00:00 2001 From: i22011 <89882946+notbad3500@users.noreply.github.com> Date: Wed, 8 May 2024 11:25:16 +0200 Subject: [PATCH 2/2] Tests and Main --- Tester.class | Bin 0 -> 287 bytes Tester.java | 2 +- src/main/java/parser/Main.java | 23 +++++++++++++++++++---- 3 files changed, 20 insertions(+), 5 deletions(-) create mode 100644 Tester.class diff --git a/Tester.class b/Tester.class new file mode 100644 index 0000000000000000000000000000000000000000..69949dc58acb0cdb41ccf87e28af0b97e050bc62 GIT binary patch literal 287 zcmYL@%TB^j5QhJ03#Tn8=)xTf7AD}rK7d3PMpr}^5Dam3iYMfd_CQK8zLu*dn)m=b zlyQbGn8o~a`Q~!?`~C$m#$^l-zCzGO15HAAtv_^