Skip to content

Commit b677b4c

Browse files
committed
Merge pull request #99 from donaldh/jvm-debug
Source file and line number annotations in generated .class files
2 parents 9db6da9 + 40b1b76 commit b677b4c

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

src/vm/jvm/QAST/Compiler.nqp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use JASTNodes;
22
use QASTNode;
3+
use NQPHLL;
34

45
# Instruction constants for argument-less ops.
56
my $ACONST_NULL := JAST::Instruction.new( :op('aconst_null') );
@@ -2823,12 +2824,15 @@ class QAST::CompilerJAST {
28232824
# Wrap $source in a QAST::Block if it's not already a viable root node.
28242825
$source := QAST::Block.new($source)
28252826
unless nqp::istype($source, QAST::CompUnit) || nqp::istype($source, QAST::Block);
2827+
2828+
my $file := nqp::getlexdyn('$?FILES');
28262829

28272830
# Set up a JAST::Class that will hold all the blocks (which become Java
28282831
# methods) that we shall compile.
28292832
my $*JCLASS := JAST::Class.new(
28302833
:name($classname),
2831-
:super('org.perl6.nqp.runtime.CompilationUnit')
2834+
:super('org.perl6.nqp.runtime.CompilationUnit'),
2835+
:filename($file)
28322836
);
28332837

28342838
# We'll also need to keep track of all the blocks we compile into Java
@@ -3486,7 +3490,14 @@ class QAST::CompilerJAST {
34863490
unless nqp::defined($resultchild) {
34873491
$resultchild := $n - 1;
34883492
}
3493+
34893494
for @stmts {
3495+
if $_.node {
3496+
my $node := $_.node;
3497+
my $line := HLL::Compiler.lineof($node.orig(), $node.from(), :cache(1));
3498+
$il.append(JAST::Annotation.new( :line($line) ));
3499+
}
3500+
34903501
my $void := $all_void || $i != $resultchild;
34913502
if $void {
34923503
if nqp::istype($_, QAST::Want) {

src/vm/jvm/QAST/JASTNodes.nqp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ class JAST::Node {
44
class JAST::Class is JAST::Node {
55
has str $!name;
66
has str $!super;
7+
has str $!filename;
78
has @!methods;
89
has @!fields;
910

10-
method BUILD(:$name!, :$super!) {
11+
method BUILD(:$name!, :$super!, :$filename) {
1112
$!name := $name;
1213
$!super := $super;
14+
$!filename := $filename;
1315
@!methods := [];
1416
@!fields := [];
1517
}
@@ -30,6 +32,7 @@ class JAST::Class is JAST::Node {
3032
my @dumped;
3133
nqp::push(@dumped, "+ class $!name");
3234
nqp::push(@dumped, "+ super $!super");
35+
nqp::push(@dumped, "+ filename $!filename");
3336
for @!fields {
3437
nqp::push(@dumped, $_.dump());
3538
}
@@ -355,6 +358,20 @@ class JAST::TryCatch is JAST::Node {
355358
}
356359
}
357360

361+
class JAST::Annotation is JAST::Node {
362+
has int $!line;
363+
364+
method BUILD(:$line!) {
365+
$!line := $line;
366+
}
367+
368+
method line() { $!line }
369+
370+
method dump() {
371+
".line $!line"
372+
}
373+
}
374+
358375
my %opmap := nqp::hash(
359376
'nop', 0x00,
360377
'aconst_null', 0x01,

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public static void writeClassFromString(String in, String filename) {
7171
private static JavaClass buildClassFrom(BufferedReader in) throws Exception
7272
{
7373
// Read in class name, superclass and any fields.
74-
String curLine, className = null, superName = null;
74+
String curLine, className = null, superName = null, fileName = null;
7575
List<String> fieldLines = new ArrayList<String>();
7676
while ((curLine = in.readLine()) != null) {
7777
if (curLine.startsWith("+ class ")) {
@@ -80,6 +80,9 @@ private static JavaClass buildClassFrom(BufferedReader in) throws Exception
8080
else if (curLine.startsWith("+ super ")) {
8181
superName = curLine.substring("+ super ".length());
8282
}
83+
else if (curLine.startsWith("+ filename ")) {
84+
fileName = curLine.substring("+ filename ".length());
85+
}
8386
else if (curLine.startsWith("+ field ")) {
8487
fieldLines.add(curLine.substring("+ field ".length()));
8588
}
@@ -97,10 +100,11 @@ else if (curLine.equals("+ method")) {
97100

98101
className = className.replace('.', '/');
99102
superName = superName.replace('.', '/');
100-
103+
101104
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
102105
cw.visit(Opcodes.V1_7, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, className, null,
103106
superName, null);
107+
cw.visitSource(fileName, null);
104108

105109
// Add the fields.
106110
for (String field : fieldLines) {
@@ -279,6 +283,12 @@ else if (curLine.equals(".endtry")) {
279283
tryStartStack.pop();
280284
m.visitLabel(tryEndStack.pop());
281285
}
286+
else if (curLine.startsWith(".line ")) {
287+
int lineNumber = Integer.parseInt(curLine.substring(".line ".length()), 10);
288+
Label l = new Label();
289+
m.visitLabel(l);
290+
m.visitLineNumber(lineNumber, l);
291+
}
282292
else {
283293
throw new Exception("Don't understand directive: " + curLine);
284294
}

0 commit comments

Comments
 (0)