Skip to content

Commit 4fa7d88

Browse files
committed
Generate local variable annotations
1 parent 527419e commit 4fa7d88

File tree

1 file changed

+28
-18
lines changed

1 file changed

+28
-18
lines changed

src/vm/jvm/runtime/org/perl6/nqp/jast2bc/JASTToJVMBytecode.java

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -155,19 +155,22 @@ private static boolean processMethod(BufferedReader in, ClassWriter c, String cl
155155
List<String> criLex = new ArrayList<String>();
156156
List<String> crnLex = new ArrayList<String>();
157157
List<String> crsLex = new ArrayList<String>();
158+
Label beginAll = new Label();
159+
Label endAll = new Label();
158160
long[] crHandlers = null;
159161
int curArgIndex = 1;
160162

161163
MethodVisitor m = null;
164+
boolean contAfter = false;
162165

163166
boolean inMethodHeader = true;
164167
while ((curLine = in.readLine()) != null) {
165168
// See if we need to move to the next method.
166169
if (curLine.equals("+ method")) {
167170
if (inMethodHeader)
168171
throw new Exception("Unexpected + method in method header");
169-
finishMethod(m);
170-
return true;
172+
contAfter = true;
173+
break;
171174
}
172175

173176
// Is it a header line?
@@ -195,7 +198,7 @@ else if (curLine.startsWith("++ local ")) {
195198
if (localVariables.containsKey(bits[2]))
196199
throw new Exception("Duplicate local name: " + bits[2]);
197200
Type t = processType(bits[3]);
198-
localVariables.put(bits[2], new VariableDef(curArgIndex, t.getDescriptor()));
201+
localVariables.put(bits[2], new VariableDef(curArgIndex, t.getDescriptor(), beginAll, endAll));
199202
curArgIndex += (t == Type.LONG_TYPE || t == Type.DOUBLE_TYPE ? 2 : 1);
200203
}
201204
else if (curLine.startsWith("++ crname "))
@@ -273,11 +276,8 @@ else if (curLine.startsWith("++ handlers ")) {
273276
av.visitEnd();
274277
}
275278

276-
// Add locals.
277-
for (Map.Entry<String, VariableDef> e : localVariables.entrySet()) {
278-
VariableDef def = e.getValue();
279-
m.visitLocalVariable(e.getKey(), def.type, null, def.start, def.end, def.index);
280-
}
279+
m.visitCode();
280+
m.visitLabel(beginAll);
281281
}
282282

283283
// Check if it's a label.
@@ -363,8 +363,23 @@ else if (curLine.startsWith(".line ")) {
363363
}
364364
if (inMethodHeader)
365365
throw new Exception("Unexpected end of file in method header");
366-
finishMethod(m);
367-
return false;
366+
367+
// Add locals.
368+
m.visitLabel(endAll);
369+
for (Map.Entry<String, VariableDef> e : localVariables.entrySet()) {
370+
VariableDef def = e.getValue();
371+
m.visitLocalVariable(e.getKey(), def.type, null, def.start, def.end, def.index);
372+
}
373+
if (!isStatic)
374+
m.visitLocalVariable("this", "L"+className+";", null, beginAll, endAll, 0);
375+
for (int i = 0; i < argTypes.size(); i++) {
376+
m.visitLocalVariable(argNames.get(i), argTypes.get(i).getDescriptor(), null,
377+
beginAll, endAll, argIndexes.get(argNames.get(i)));
378+
}
379+
380+
m.visitMaxs(0, 0);
381+
m.visitEnd();
382+
return contAfter;
368383
}
369384

370385
private static String decodeString(String value) {
@@ -832,11 +847,6 @@ private static void emitTableSwitchInstruction(MethodVisitor m, Map<String, Labe
832847
m.visitTableSwitchInsn(0, labels.length - 1, defaultLabel, labels);
833848
}
834849

835-
private static void finishMethod(MethodVisitor m) throws Exception {
836-
// Finalize method.
837-
m.visitMaxs(0, 0);
838-
}
839-
840850
private static Type processType(String typeName) {
841851
// Long needs special treatment; getType doesn't cope with it.
842852
if (typeName.equals("Long"))
@@ -851,11 +861,11 @@ private static void usage()
851861
}
852862

853863
static class VariableDef {
854-
public VariableDef(int i, String t) {
864+
public VariableDef(int i, String t, Label s, Label e) {
855865
index = i;
856866
type = t;
857-
start = new Label();
858-
end = new Label();
867+
start = s;
868+
end = e;
859869
}
860870

861871
public final int index;

0 commit comments

Comments
 (0)