[perl #54148] [NEW] Add tools/util/dump-pbc.pl (weave PIR source and PBC disassembly)

2008-06-03 Thread Bernhard Schmalhofer via RT

 
 The previous version of the patch didn't work on Windows, because pipe
 open doesn't work there, grrr.  Please try the attached version of the
 patch.
 

It looks like the current version of the patch has been applied.
In r28039 I added a sanity test in the new file t/tools/dump_pbc.pl.
If that works across platforms, I propose to close this ticket.

-- 
/* [EMAIL PROTECTED] */


[perl #54148] [NEW] Add tools/util/dump-pbc.pl (weave PIR source and PBC disassembly)

2008-06-02 Thread Will Coleda via RT
On Mon Jun 02 13:19:16 2008, bernhard wrote:
 
  
  The previous version of the patch didn't work on Windows, because 
pipe
  open doesn't work there, grrr.  Please try the attached version of 
the
  patch.
  
 
 It looks like the current version of the patch has been applied.
 In r28039 I added a sanity test in the new file t/tools/dump_pbc.pl.
 If that works across platforms, I propose to close this ticket.

C:\research\parrotprove t\tools\dump_pbc.t
t\tools\dump_pbc..ok
All tests successful.
Files=1, Tests=1,  1 wallclock secs ( 0.02 usr +  0.00 sys =  0.02 CPU)
Result: PASS





[perl #54148] [NEW] Add tools/util/dump-pbc.pl (weave PIR source and PBC disassembly)

2008-05-14 Thread via RT
# New Ticket Created by  Geoffrey Broadwell 
# Please include the string:  [perl #54148]
# in the subject line of all future correspondence about this issue. 
# URL: http://rt.perl.org/rt3/Ticket/Display.html?id=54148 


The attached patch adds the new utility script tools/util/dump-pbc.pl,
which produces a (mostly) easy to read weave of PBC disassembly and PIR
source (from possibly multiple files).  It also enables compile of the
'disassemble' program in the Makefile.  (All of the structure was there,
it just wasn't enabled.)


-'f

=== MANIFEST
==
--- MANIFEST	(revision 5166)
+++ MANIFEST	(local)
@@ -3705,6 +3705,7 @@
 tools/docs/write_docs.pl[devel]
 tools/install/smoke.pl  []
 tools/util/crow.pir []
+tools/util/dump-pbc.pl  []
 tools/util/gen_release_info.pl  []
 tools/util/ncidef2pasm.pl   []
 tools/util/perltidy.conf[]
=== config/gen/makefiles/root.in
==
--- config/gen/makefiles/root.in	(revision 5166)
+++ config/gen/makefiles/root.in	(local)
@@ -585,6 +585,7 @@
 dynoplibs \
 compilers \
 $(PBC_TO_EXE) \
+$(DIS) \
 $(PBCMERGE)
 
 $(GEN_LIBRARY) : $(PARROT)
=== tools/util/dump-pbc.pl
==
--- tools/util/dump-pbc.pl	(revision 5166)
+++ tools/util/dump-pbc.pl	(local)
@@ -0,0 +1,105 @@
+#! perl
+
+# Copyright (C) 2008, The Perl Foundation.
+# $Id: $
+
+=head1 NAME
+
+tools/util/dump-pbc.pl - Weave together PBC disassembly with PIR source
+
+=head1 SYNOPSIS
+
+ perl tools/util/dump-pbc.pl foo.pbc
+
+=head1 DESCRIPTION
+
+dump-pbc.pl uses Parrot's Fdisassemble program to disassemble the opcodes
+in a PBC (Parrot ByteCode) file, then weaves the disassembly together with
+the original PIR source file(s).  This makes it easier to see how the PIR
+syntactic sugar is desugared into raw Parrot opcodes.
+
+=head1 BUGS
+
+This program has only been tested for a few simple cases.  Also, the name
+might suggest a different use than its actual purpose.
+
+While it is not a bug in Fdump-pbc.pl per se, there is a line numbering
+bug for some PBC opcode sequences that will result in the disassembled
+opcodes appearing just before the source lines they represent, rather
+than just after.  There does not appear to be consensus yet about where
+this bug actually resides.
+
+=cut
+
+use strict;
+use warnings;
+use Cwd;
+use FindBin;
+
+my $PARROT_ROOT  = Cwd::abs_path($FindBin::Bin/../..);
+my $RUNTIME_DIR  = $PARROT_ROOT/runtime/parrot;
+my $DISASSEMBLER = $PARROT_ROOT/disassemble;
+
+go(@ARGV);
+
+sub go {
+my $pbc = shift;
+
+open my $dis, '-|', $DISASSEMBLER, $pbc
+or die   Could not start disassembler '$DISASSEMBLER': $!;
+
+my $cur_file = '';
+my $cur_line = -1;
+my %cache;
+
+while ($dis) {
+if(/^Current Source Filename (.*)/) {
+if ($cur_file ne $1) {
+$cur_file   = $1;
+$cache{$cur_file} ||= slurp_file($cur_file);
+$cur_line   = -1;
+
+print \n $cur_file\n;
+}
+}
+elsif (my ($info, $seq, $pc, $line, $code) = /^((\d+)-(\d+) (\d+): )(.*)/) {
+my $int_line = int$line;
+my $len_line = length $line;
+if ($cur_line != $int_line) {
+$cur_line = 0 if $cur_line == -1;
+print \n;
+foreach my $i ($cur_line + 1 .. $int_line) {
+my $source_code = $cache{$cur_file}[$i-1];
+# nextunless $source_code =~ /\S/;
+printf # %*d:   %s, $len_line, $i, $source_code;
+print  \n if $source_code =~ /^\.end/;
+}
+$cur_line  = $int_line;
+}
+
+print ' ' x ($len_line + 4), $code\n;
+}
+}
+}
+
+sub slurp_file {
+my $file = shift;
+my $source;
+
+   open $source, '', $file
+or open $source, '', $PARROT_ROOT/$file
+or open $source, '', $RUNTIME_DIR/$file
+or die Could not open source file '$file': $!;
+
+my @lines = $source;
+
+return [EMAIL PROTECTED];
+}
+
+
+# Local Variables:
+#   mode: cperl
+#   cperl-indent-level: 4
+#   fill-column: 100
+# End:
+# vim: expandtab shiftwidth=4:

Property changes on: tools/util/dump-pbc.pl
___
Name: svn:executable
 +*



Re: [perl #54148] [NEW] Add tools/util/dump-pbc.pl (weave PIR source and PBC disassembly)

2008-05-14 Thread Will Coleda
On Tue, May 13, 2008 at 9:17 PM, via RT Geoffrey Broadwell
[EMAIL PROTECTED] wrote:
 # New Ticket Created by  Geoffrey Broadwell
 # Please include the string:  [perl #54148]
 # in the subject line of all future correspondence about this issue.
 # URL: http://rt.perl.org/rt3/Ticket/Display.html?id=54148 


 The attached patch adds the new utility script tools/util/dump-pbc.pl,
 which produces a (mostly) easy to read weave of PBC disassembly and PIR
 source (from possibly multiple files).  It also enables compile of the
 'disassemble' program in the Makefile.  (All of the structure was there,
 it just wasn't enabled.)


 -'f



Nifty!

Minor nits:

- can we use _ instead of - in the filename?
- can we make the error message more graceful if the disassemble
executable hasn't been built?
- it's not always going to be called disassemble - you'll need to
poke into Parrot::Config and pull out the executable suffix and use
that. (Might be disassemble.exe on windows, for example).


-- 
Will Coke Coleda


Re: [perl #54148] [NEW] Add tools/util/dump-pbc.pl (weave PIR source and PBC disassembly)

2008-05-14 Thread Geoffrey Broadwell
On Wed, 2008-05-14 at 07:50 -0700, Will Coleda via RT wrote:
 Nifty!
 
 Minor nits:
 
 - can we use _ instead of - in the filename?
 - can we make the error message more graceful if the disassemble
 executable hasn't been built?
 - it's not always going to be called disassemble - you'll need to
 poke into Parrot::Config and pull out the executable suffix and use
 that. (Might be disassemble.exe on windows, for example).


I believe the attached patch addresses all of these nits, and also adds
the matching CREDITS diff that I had forgotten to include last time.


-'f

=== CREDITS
==
--- CREDITS	(revision 5166)
+++ CREDITS	(local)
@@ -257,7 +257,8 @@
 
 N: Geoff Broadwell
 D: OpenGL binding
-D: examples fixes
+D: Disassembly/source weaver
+D: Miscellaneous fixes
 
 N: Gerard Goossen
 D: Documentation patch for Parrot_PMC_get_pointer_intkey()
=== MANIFEST
==
--- MANIFEST	(revision 5166)
+++ MANIFEST	(local)
@@ -3705,6 +3705,7 @@
 tools/docs/write_docs.pl[devel]
 tools/install/smoke.pl  []
 tools/util/crow.pir []
+tools/util/dump_pbc.pl  []
 tools/util/gen_release_info.pl  []
 tools/util/ncidef2pasm.pl   []
 tools/util/perltidy.conf[]
=== config/gen/makefiles/root.in
==
--- config/gen/makefiles/root.in	(revision 5166)
+++ config/gen/makefiles/root.in	(local)
@@ -585,6 +585,7 @@
 dynoplibs \
 compilers \
 $(PBC_TO_EXE) \
+$(DIS) \
 $(PBCMERGE)
 
 $(GEN_LIBRARY) : $(PARROT)
=== tools/util/dump_pbc.pl
==
--- tools/util/dump_pbc.pl	(revision 5166)
+++ tools/util/dump_pbc.pl	(local)
@@ -0,0 +1,112 @@
+#! perl
+
+# Copyright (C) 2008, The Perl Foundation.
+# $Id: $
+
+=head1 NAME
+
+tools/util/dump_pbc.pl - Weave together PBC disassembly with PIR source
+
+=head1 SYNOPSIS
+
+ perl tools/util/dump_pbc.pl foo.pbc
+
+=head1 DESCRIPTION
+
+dump_pbc.pl uses Parrot's Fdisassemble program to disassemble the opcodes
+in a PBC (Parrot ByteCode) file, then weaves the disassembly together with
+the original PIR source file(s).  This makes it easier to see how the PIR
+syntactic sugar is desugared into raw Parrot opcodes.
+
+=head1 BUGS
+
+This program has only been tested for a few simple cases.  Also, the name
+might suggest a different use than its actual purpose.
+
+While it is not a bug in Fdump_pbc.pl per se, there is a line numbering
+bug for some PBC opcode sequences that will result in the disassembled
+opcodes appearing just before the source lines they represent, rather
+than just after.  There does not appear to be consensus yet about where
+this bug actually resides.
+
+=cut
+
+use strict;
+use warnings;
+use Cwd;
+use FindBin;
+
+my ($PARROT_ROOT, $RUNTIME_DIR);
+BEGIN {
+$PARROT_ROOT = Cwd::abs_path($FindBin::Bin/../..);
+$RUNTIME_DIR = $PARROT_ROOT/runtime/parrot;
+}
+
+use lib $PARROT_ROOT/lib;
+use Parrot::Config '%PConfig';
+
+my $DISASSEMBLER = $PConfig{build_dir}$PConfig{slash}disassemble$PConfig{exe};
+
+go(@ARGV);
+
+sub go {
+my $pbc = shift;
+
+open my $dis, '-|', $DISASSEMBLER, $pbc
+or die Could not start disassembler, did you remember to make parrot first?\n;
+
+my $cur_file = '';
+my $cur_line = -1;
+my %cache;
+
+while ($dis) {
+if(/^Current Source Filename (.*)/) {
+if ($cur_file ne $1) {
+$cur_file   = $1;
+$cache{$cur_file} ||= slurp_file($cur_file);
+$cur_line   = -1;
+
+print \n $cur_file\n;
+}
+}
+elsif (my ($info, $seq, $pc, $line, $code) = /^((\d+)-(\d+) (\d+): )(.*)/) {
+my $int_line = int$line;
+my $len_line = length $line;
+if ($cur_line != $int_line) {
+$cur_line = 0 if $cur_line == -1;
+print \n;
+foreach my $i ($cur_line + 1 .. $int_line) {
+my $source_code = $cache{$cur_file}[$i-1];
+# nextunless $source_code =~ /\S/;
+printf # %*d:   %s, $len_line, $i, $source_code;
+print  \n if $source_code =~ /^\.end/;
+}
+$cur_line  = $int_line;
+}
+
+print ' ' x ($len_line + 4), $code\n;
+}
+}
+}
+
+sub slurp_file {
+my $file = shift;
+my $source;
+
+   open $source, '', $file
+or open $source, '', $PARROT_ROOT/$file
+or open $source, '', $RUNTIME_DIR/$file
+or die Could not open source file '$file': $!;
+
+my @lines = $source;
+
+return [EMAIL 

Re: [perl #54148] [NEW] Add tools/util/dump-pbc.pl (weave PIR source and PBC disassembly)

2008-05-14 Thread Will Coleda
On Wed, May 14, 2008 at 1:09 PM, Geoffrey Broadwell [EMAIL PROTECTED] wrote:
 On Wed, 2008-05-14 at 07:50 -0700, Will Coleda via RT wrote:
 Nifty!

 Minor nits:

 - can we use _ instead of - in the filename?
 - can we make the error message more graceful if the disassemble
 executable hasn't been built?
 - it's not always going to be called disassemble - you'll need to
 poke into Parrot::Config and pull out the executable suffix and use
 that. (Might be disassemble.exe on windows, for example).


 I believe the attached patch addresses all of these nits, and also adds
 the matching CREDITS diff that I had forgotten to include last time.


 -'f



Excellent. I was about to ask if we could have someone windows test to
make sure this works for them... and it would be a lot easier if we
had a test (even a very minimal one) to make sure that we were getting
the expected output. ^_^

If you can whip that up (on a trivial hello world PIR is fine), great,
if not, we need to make sure that we add a ticket to request it;
doesn't mean we can't commit this without.

Thanks!

-- 
Will Coke Coleda


Re: [perl #54148] [NEW] Add tools/util/dump-pbc.pl (weave PIR source and PBC disassembly)

2008-05-14 Thread Geoffrey Broadwell
On Wed, 2008-05-14 at 10:40 -0700, Will Coleda via RT wrote:
 Excellent. I was about to ask if we could have someone windows test to
 make sure this works for them...

Sure ... but I don't know who to ask.  I'm hoping you do.

  and it would be a lot easier if we
 had a test (even a very minimal one) to make sure that we were getting
 the expected output. ^_^

:-)

 If you can whip that up (on a trivial hello world PIR is fine), great,
 if not, we need to make sure that we add a ticket to request it;
 doesn't mean we can't commit this without.

I've probably stolen more time than I should for this as it is, so I
can't write said test right now.  I (or someone else) might be able to
do this another day, so I'd say commit and create an RT for the test.

Where should tests for tools/util/ scripts go, anyway?  Should I create
a matching t/tools/util/ subdir, or is it a functional thing, so I
create a t/tools/pbc/ subdir or somesuch?


-'f




Re: [perl #54148] [NEW] Add tools/util/dump-pbc.pl (weave PIR source and PBC disassembly)

2008-05-14 Thread Geoffrey Broadwell
On Wed, 2008-05-14 at 11:03 -0700, Geoffrey Broadwell wrote:
 On Wed, 2008-05-14 at 10:40 -0700, Will Coleda via RT wrote:
  Excellent. I was about to ask if we could have someone windows test to
  make sure this works for them...

The previous version of the patch didn't work on Windows, because pipe
open doesn't work there, grrr.  Please try the attached version of the
patch.


-'f

=== CREDITS
==
--- CREDITS	(revision 5180)
+++ CREDITS	(local)
@@ -257,7 +257,8 @@
 
 N: Geoff Broadwell
 D: OpenGL binding
-D: examples fixes
+D: Disassembly/source weaver
+D: Miscellaneous fixes
 
 N: Gerard Goossen
 D: Documentation patch for Parrot_PMC_get_pointer_intkey()
=== MANIFEST
==
--- MANIFEST	(revision 5180)
+++ MANIFEST	(local)
@@ -3710,6 +3710,7 @@
 tools/docs/write_docs.pl[devel]
 tools/install/smoke.pl  []
 tools/util/crow.pir []
+tools/util/dump_pbc.pl  []
 tools/util/gen_release_info.pl  []
 tools/util/ncidef2pasm.pl   []
 tools/util/perltidy.conf[]
=== config/gen/makefiles/root.in
==
--- config/gen/makefiles/root.in	(revision 5180)
+++ config/gen/makefiles/root.in	(local)
@@ -585,6 +585,7 @@
 dynoplibs \
 compilers \
 $(PBC_TO_EXE) \
+$(DIS) \
 $(PBCMERGE)
 
 $(GEN_LIBRARY) : $(PARROT)
=== tools/util/dump_pbc.pl
==
--- tools/util/dump_pbc.pl	(revision 5180)
+++ tools/util/dump_pbc.pl	(local)
@@ -0,0 +1,122 @@
+#! perl
+
+# Copyright (C) 2008, The Perl Foundation.
+# $Id: $
+
+=head1 NAME
+
+tools/util/dump_pbc.pl - Weave together PBC disassembly with PIR source
+
+=head1 SYNOPSIS
+
+ perl tools/util/dump_pbc.pl foo.pbc
+
+=head1 DESCRIPTION
+
+dump_pbc.pl uses Parrot's Fdisassemble program to disassemble the opcodes
+in a PBC (Parrot ByteCode) file, then weaves the disassembly together with
+the original PIR source file(s).  This makes it easier to see how the PIR
+syntactic sugar is desugared into raw Parrot opcodes.
+
+=head1 BUGS
+
+This program has only been tested for a few simple cases.  Also, the name
+might suggest a different use than its actual purpose.
+
+While it is not a bug in Fdump_pbc.pl per se, there is a line numbering
+bug for some PBC opcode sequences that will result in the disassembled
+opcodes appearing just before the source lines they represent, rather
+than just after.  There does not appear to be consensus yet about where
+this bug actually resides.
+
+=cut
+
+use strict;
+use warnings;
+use Cwd;
+use FindBin;
+
+my ($PARROT_ROOT, $RUNTIME_DIR);
+BEGIN {
+$PARROT_ROOT = Cwd::abs_path($FindBin::Bin/../..);
+$RUNTIME_DIR = $PARROT_ROOT/runtime/parrot;
+}
+
+use lib $PARROT_ROOT/lib;
+use Parrot::Config '%PConfig';
+
+my $DISASSEMBLER = $PConfig{build_dir}$PConfig{slash}disassemble$PConfig{exe};
+
+go(@ARGV);
+
+sub go {
+my $pbc = shift;
+
+# The following mess brought to you by Win32, where pipe open doesn't work,
+# and thus its greater security and cleaner error handling are unavailable.
+
+-f $pbc  -r _
+or die PBC file '$pbc' does not exist or is not readable.\n;
+
+-f $DISASSEMBLER  -x _
+or die  Can't find disassembler '$DISASSEMBLER';
+  . did you remember to make parrot first?\n;
+
+my @dis = `$DISASSEMBLER $pbc`;
+die No disassembly; errors: $?, $! unless @dis;
+
+my $cur_file = '';
+my $cur_line = -1;
+my %cache;
+
+foreach (@dis) {
+if(/^Current Source Filename (.*)/) {
+if ($cur_file ne $1) {
+$cur_file   = $1;
+$cache{$cur_file} ||= slurp_file($cur_file);
+$cur_line   = -1;
+
+print \n $cur_file\n;
+}
+}
+elsif (my ($info, $seq, $pc, $line, $code) = /^((\d+)-(\d+) (\d+): )(.*)/) {
+my $int_line = int$line;
+my $len_line = length $line;
+if ($cur_line != $int_line) {
+$cur_line = 0 if $cur_line == -1;
+print \n;
+foreach my $i ($cur_line + 1 .. $int_line) {
+my $source_code = $cache{$cur_file}[$i-1];
+# nextunless $source_code =~ /\S/;
+printf # %*d:   %s, $len_line, $i, $source_code;
+print  \n if $source_code =~ /^\.end/;
+}
+$cur_line  = $int_line;
+}
+
+print ' ' x ($len_line + 4), $code\n;
+}
+}
+}
+
+sub slurp_file {
+my $file = shift;
+my $source;
+
+   open $source, '', $file
+or open