Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Let JASTToJVMBytecode work with a list of lines instead of a huge str…
…ing.
  • Loading branch information
donaldh committed Sep 25, 2013
1 parent 61f2f50 commit 7b02322
Showing 1 changed file with 35 additions and 17 deletions.
52 changes: 35 additions & 17 deletions src/vm/jvm/runtime/org/perl6/nqp/jast2bc/JASTToJVMBytecode.java
Expand Up @@ -4,14 +4,15 @@
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.lang.invoke.CallSite;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Stack;
Expand All @@ -27,7 +28,6 @@
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;

import org.perl6.nqp.runtime.Base64;

public class JASTToJVMBytecode {
Expand All @@ -38,10 +38,15 @@ public static void main(String[] argv)

try
{
List<String> lines = new LinkedList<String>();
String line;
BufferedReader in = new BufferedReader(new InputStreamReader(
new FileInputStream(argv[0]), "UTF-8"));
JavaClass c = buildClassFrom(in, true);
while ((line = in.readLine()) != null) {
lines.add(line);
}
in.close();
JavaClass c = buildClassFrom(lines, true);
FileOutputStream fos = new FileOutputStream(argv[1]);
fos.write(c.bytes);
fos.close();
Expand All @@ -55,20 +60,29 @@ public static void main(String[] argv)
}

public static JavaClass buildClassFromString(String in, boolean split) {
List<String> lines = Arrays.asList(in.split("\n"));
return buildClassFromString(lines, split);
}

public static JavaClass buildClassFromString(List<String> lines, boolean split) {
try {
BufferedReader br = new BufferedReader(new StringReader(in));
JavaClass c = buildClassFrom(br, split);
JavaClass c = buildClassFrom(lines, split);
return c;
}
catch (Exception e) {
if (!split && "Method code too large!".equals(e.getMessage()))
return buildClassFromString(in, true);
return buildClassFromString(lines, true);
throw new RuntimeException(e);
}
}

public static void writeClassFromString(String in, String filename) {
JavaClass c = buildClassFromString(in, false);
List<String> lines = Arrays.asList(in.split("\n"));
writeClassFromString(lines, filename);
}

public static void writeClassFromString(List<String> lines, String filename) {
JavaClass c = buildClassFromString(lines, false);
try {
FileOutputStream fos = new FileOutputStream(filename);
if (c.serialized == null) {
Expand Down Expand Up @@ -99,14 +113,17 @@ public static void writeClassFromString(String in, String filename) {
}
}

private static JavaClass buildClassFrom(BufferedReader in, boolean split) throws Exception
private static JavaClass buildClassFrom(List<String> lines, boolean split) throws Exception
{
JavaClass c = new JavaClass();
// Read in class name, superclass and any fields.
String curLine, className = null, superName = null, fileName = null;
String curLine = null, className = null, superName = null, fileName = null;
byte[] serData = null;
List<String> fieldLines = new ArrayList<String>();
while ((curLine = in.readLine()) != null) {

Iterator<String> iter = lines.iterator();
while (iter.hasNext()) {
curLine = iter.next();
if (curLine.startsWith("+ class ")) {
className = c.name = curLine.substring("+ class ".length());
}
Expand Down Expand Up @@ -158,7 +175,7 @@ else if (curLine.equals("+ method")) {
// Process all of the methods.
if (!curLine.equals("+ method"))
throw new Exception("Expected method after class configuration");
while (processMethod(c, in, cw, className, split))
while (processMethod(c, iter, cw, className, split))
;

// Add empty constructor.
Expand All @@ -182,7 +199,7 @@ private static class LabelInfo {
public boolean defined;
}

private static boolean processMethod(JavaClass jcout, BufferedReader in, ClassWriter c, String className, boolean split) throws Exception {
private static boolean processMethod(JavaClass jcout, Iterator<String> in, ClassWriter c, String className, boolean split) throws Exception {
String curLine, methodName = null, returnType = null, desc = null;
String crName = null, crCuid = null;
int crOuterIx = -2; // not coderef
Expand All @@ -205,7 +222,8 @@ private static boolean processMethod(JavaClass jcout, BufferedReader in, ClassWr
MethodVisitor m = null;
boolean contAfter = false;
boolean inMethodHeader = true;
while ((curLine = in.readLine()) != null) {
while (in.hasNext()) {
curLine = in.next();
// See if we need to move to the next method.
if (curLine.equals("+ method")) {
if (inMethodHeader)
Expand Down Expand Up @@ -464,7 +482,7 @@ private static String decodeString(String value) {
return sb.toString();
}

private static void emitInstruction(BufferedReader in, MethodVisitor m,
private static void emitInstruction(Iterator<String> in, MethodVisitor m,
Map<String, LabelInfo> labelMap,
Map<String, VariableDef> localVariables,
String curLine) throws Exception {
Expand Down Expand Up @@ -828,7 +846,7 @@ private static void emitCall(MethodVisitor m,
Type.getMethodDescriptor(returnType, argumentTypes));
}

private static void emitInvokeDynamic(BufferedReader in, MethodVisitor m, String callSpec) {
private static void emitInvokeDynamic(Iterator<String> in, MethodVisitor m, String callSpec) {
String[] bits = callSpec.split("\\s");
int numExtraArgs;
if (bits.length == 4)
Expand All @@ -844,7 +862,7 @@ else if (bits.length == 5)
Object[] extraArgs = new Object[numExtraArgs];
for (int i = 0; i < numExtraArgs; i++) {
try {
String curLine = in.readLine();
String curLine = in.next();
if (curLine.startsWith(".push_ic ")) {
extraArgs[i] = Long.parseLong(curLine.substring(".push_ic ".length()));
bsmMT = bsmMT.appendParameterTypes(long.class);
Expand Down

0 comments on commit 7b02322

Please sign in to comment.