Add classFIleOutput folder
This commit is contained in:
parent
d0448b01cd
commit
d131d412f0
BIN
classFileOutput/Example.class
Normal file
BIN
classFileOutput/Example.class
Normal file
Binary file not shown.
@ -2,8 +2,8 @@ package bytecode;
|
|||||||
|
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import org.objectweb.asm.ClassWriter;
|
import org.objectweb.asm.ClassWriter;
|
||||||
import org.objectweb.asm.MethodVisitor;
|
|
||||||
import org.objectweb.asm.Opcodes;
|
import org.objectweb.asm.Opcodes;
|
||||||
import parser.ClassDeclarationNode;
|
import parser.ClassDeclarationNode;
|
||||||
import parser.ProgramNode;
|
import parser.ProgramNode;
|
||||||
@ -12,24 +12,25 @@ public class ByteCodeGenerator {
|
|||||||
|
|
||||||
public void generateByteCode(ProgramNode ast) {
|
public void generateByteCode(ProgramNode ast) {
|
||||||
for (ClassDeclarationNode classDeclarationNode : ast.classes) {
|
for (ClassDeclarationNode classDeclarationNode : ast.classes) {
|
||||||
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
|
ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
|
||||||
cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, classDeclarationNode.identifier, null,
|
classWriter.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, classDeclarationNode.identifier, null,
|
||||||
"java/lang/Object", null);
|
"java/lang/Object", null);
|
||||||
|
|
||||||
MethodVisitor constructor =
|
FieldCodeGen fieldCodeGen = new FieldCodeGen();
|
||||||
cw.visitMethod(Opcodes.ACC_PUBLIC,
|
fieldCodeGen.generateFieldCode(classWriter);
|
||||||
"<init>",
|
|
||||||
"()V",
|
|
||||||
null,
|
|
||||||
null);
|
|
||||||
cw.visitEnd();
|
|
||||||
|
|
||||||
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) {
|
private void printIntoClassFile(byte[] byteCode, String name) {
|
||||||
String filePath = name + ".class";
|
String filePath = "./classFileOutput/" + name + ".class";
|
||||||
|
|
||||||
try {
|
try {
|
||||||
FileOutputStream fileOutputStream = new FileOutputStream(filePath);
|
FileOutputStream fileOutputStream = new FileOutputStream(filePath);
|
||||||
|
10
src/main/java/bytecode/FieldCodeGen.java
Normal file
10
src/main/java/bytecode/FieldCodeGen.java
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
package bytecode;
|
||||||
|
|
||||||
|
import org.objectweb.asm.ClassWriter;
|
||||||
|
|
||||||
|
public class FieldCodeGen {
|
||||||
|
|
||||||
|
public void generateFieldCode(ClassWriter classWriter) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
16
src/main/java/bytecode/MethodCodeGen.java
Normal file
16
src/main/java/bytecode/MethodCodeGen.java
Normal file
@ -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,
|
||||||
|
"<init>",
|
||||||
|
"()V",
|
||||||
|
null,
|
||||||
|
null);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user