[svn:parrot] r36671 - in trunk: include/parrot src tools/util
rurban at svn.parrot.org
rurban at svn.parrot.org
Fri Feb 13 12:25:13 UTC 2009
Author: rurban
Date: Fri Feb 13 12:25:12 2009
New Revision: 36671
URL: https://trac.parrot.org/parrot/changeset/36671
Log:
[tools] TT #258 Add pdb_disassmble options, enable roundtrips with -b
Modified:
trunk/include/parrot/embed.h
trunk/include/parrot/packfile.h
trunk/src/embed.c
trunk/src/pbc_disassemble.c
trunk/tools/util/dump_pbc.pl
Modified: trunk/include/parrot/embed.h
==============================================================================
--- trunk/include/parrot/embed.h Fri Feb 13 11:19:46 2009 (r36670)
+++ trunk/include/parrot/embed.h Fri Feb 13 12:25:12 2009 (r36671)
@@ -22,6 +22,11 @@
typedef int Parrot_warnclass;
+typedef enum {
+ enum_DIS_BARE = 1,
+ enum_DIS_HEADER = 2
+} Parrot_disassemble_options;
+
PARROT_EXPORT Parrot_Interp Parrot_new(Parrot_Interp parent);
PARROT_EXPORT void Parrot_init_stacktop(Parrot_Interp, void *);
@@ -58,7 +63,7 @@
PARROT_EXPORT Parrot_Opcode * Parrot_debug(Parrot_Interp, Parrot_Interp, Parrot_Opcode *pc);
-PARROT_EXPORT void Parrot_disassemble(Parrot_Interp);
+PARROT_EXPORT void Parrot_disassemble(Parrot_Interp, const char *outfile, Parrot_disassemble_options options);
PARROT_EXPORT
PARROT_DOES_NOT_RETURN
Modified: trunk/include/parrot/packfile.h
==============================================================================
--- trunk/include/parrot/packfile.h Fri Feb 13 11:19:46 2009 (r36670)
+++ trunk/include/parrot/packfile.h Fri Feb 13 12:25:12 2009 (r36671)
@@ -271,6 +271,7 @@
PackFile_ByteCode * cur_cs; /* used during PF loading */
+ INTVAL options;
INTVAL need_wordsize;
INTVAL need_endianize;
Modified: trunk/src/embed.c
==============================================================================
--- trunk/src/embed.c Fri Feb 13 11:19:46 2009 (r36670)
+++ trunk/src/embed.c Fri Feb 13 12:25:12 2009 (r36671)
@@ -1041,7 +1041,7 @@
INTVAL i;
/* TODO: would be nice to print the name of the file as well */
- Parrot_io_printf(interp, "Constant-table\n");
+ Parrot_io_printf(interp, "=head1 Constant-table\n\n");
for (i = 0; i < numconstants; ++i) {
PackFile_Constant *c = interp->code->const_table->constants[i];
@@ -1111,7 +1111,7 @@
}
}
- Parrot_io_printf(interp, "\n");
+ Parrot_io_printf(interp, "\n=cut\n\n");
}
@@ -1129,7 +1129,7 @@
PARROT_EXPORT
void
-Parrot_disassemble(PARROT_INTERP)
+Parrot_disassemble(PARROT_INTERP, const char *outfile, Parrot_disassemble_options options)
{
PDB_line_t *line;
PDB_t *pdb = mem_allocate_zeroed_typed(PDB_t);
@@ -1147,11 +1147,15 @@
debugs = (interp->code->debugs != NULL);
print_constant_table(interp);
+ if (options & enum_DIS_HEADER)
+ return;
- Parrot_io_printf(interp, "%12s-%12s", "Seq_Op_Num", "Relative-PC");
+ if (!(options & enum_DIS_BARE))
+ Parrot_io_printf(interp, "# %12s-%12s", "Seq_Op_Num", "Relative-PC");
if (debugs) {
- Parrot_io_printf(interp, " %6s:\n", "SrcLn#");
+ if (!(options & enum_DIS_BARE))
+ Parrot_io_printf(interp, " %6s:\n", "SrcLn#");
num_mappings = interp->code->debugs->num_mappings;
}
else {
@@ -1169,16 +1173,17 @@
if (op_code_seq_num == interp->code->debugs->mappings[curr_mapping]->offset) {
const int filename_const_offset =
interp->code->debugs->mappings[curr_mapping]->filename;
- Parrot_io_printf(interp, "Current Source Filename %Ss\n",
+ Parrot_io_printf(interp, "# Current Source Filename '%Ss'\n",
interp->code->const_table->constants[filename_const_offset]->u.string);
curr_mapping++;
}
}
- Parrot_io_printf(interp, "%012i-%012i",
- op_code_seq_num, line->opcode - interp->code->base.data);
+ if (!(options & enum_DIS_BARE))
+ Parrot_io_printf(interp, "%012i-%012i",
+ op_code_seq_num, line->opcode - interp->code->base.data);
- if (debugs)
+ if (debugs && !(options & enum_DIS_BARE))
Parrot_io_printf(interp, " %06i: ",
interp->code->debugs->base.data[op_code_seq_num]);
Modified: trunk/src/pbc_disassemble.c
==============================================================================
--- trunk/src/pbc_disassemble.c Fri Feb 13 11:19:46 2009 (r36670)
+++ trunk/src/pbc_disassemble.c Fri Feb 13 12:25:12 2009 (r36671)
@@ -8,7 +8,7 @@
=head1 SYNOPSIS
- pbc_disassemble file.pbc
+ pbc_disassemble [-bh?] [-o outfile] [file.pbc]
=head1 DESCRIPTION
@@ -16,6 +16,8 @@
which in turn uses the C<PDB_disassemble()> function from
F<src/debug.c>.
+Without non-option arguments it reads the pbc from STDIN.
+
=head2 Functions
=over 4
@@ -30,7 +32,36 @@
#include <stdlib.h>
#include <ctype.h>
-static void do_dis(Parrot_Interp);
+static void do_dis(Parrot_Interp, const char *, Parrot_disassemble_options);
+
+/*
+
+=item C<static void help(void)>
+
+Print out the user help info.
+
+=cut
+
+*/
+
+static void help(void)
+{
+ printf("pbc_disassemble - dump or convert parrot bytecode (PBC) files\n");
+ printf("usage:\n");
+ printf("pbc_disassemble [-bh] [--bare|--header-only] [file.pbc]\n");
+ printf("pbc_disassemble -o converted.pasm file.pbc\n\n");
+ printf(" -b\t\t ... bare .pasm without header and left column\n");
+ printf(" -h\t\t ... dump Constant-table header only\n");
+ printf(" -o filename\t ... output to filename\n");
+ exit(EXIT_SUCCESS);
+}
+
+static struct longopt_opt_decl options[] = {
+ { 'h', 'h', OPTION_optional_FLAG, { "--header-only" } },
+ { '?', '?', OPTION_optional_FLAG, { "--help" } },
+ { 'b', 'b', OPTION_optional_FLAG, { "--bare" } },
+ { 'o', 'o', OPTION_required_FLAG, { "--output" } }
+};
/*
@@ -44,45 +75,62 @@
*/
int
-main(int argc, char *argv[])
+main(int argc, const char *argv[])
{
- Parrot_Interp interp;
- char *filename;
Parrot_PackFile pf;
+ Parrot_Interp interp;
+ const char *outfile = NULL;
+ int option = 0;
+ struct longopt_opt_info opt = LONGOPT_OPT_INFO_INIT;
+ int status;
interp = Parrot_new(NULL);
-
if (!interp) {
return 1;
}
-
- /* set the top of the stack so GC can trace it for GC-able pointers
- * see trace_system_areas() in src/cpu_dep.c */
- interp->lo_var_ptr = &interp;
-
- if (argc != 2) {
- fprintf(stderr, "Usage: pbc_disassemble programfile \n");
- Parrot_exit(interp, 1);
+ /* init and set top of stack */
+ Parrot_init_stacktop(interp, &status);
+ while ((status = longopt_get(interp,
+ argc, argv, options, &opt)) > 0) {
+ switch (opt.opt_id) {
+ case 'h':
+ option += enum_DIS_HEADER;
+ break;
+ case 'b':
+ option += enum_DIS_BARE;
+ break;
+ case 'o':
+ outfile = opt.opt_arg;
+ break;
+ case '?':
+ default:
+ help();
+ break;
+ }
}
+ if (status == -1) {
+ help();
+ }
+ argc -= opt.opt_index;
+ argv += opt.opt_index;
- filename = argv[1];
-
- pf = Parrot_readbc(interp, filename);
+ pf = Parrot_readbc(interp, argc ? *argv : "-");
if (!pf) {
+ printf("Can't read PBC\n");
return 1;
}
Parrot_loadbc(interp, pf);
- do_dis(interp);
+ do_dis(interp, outfile, option);
Parrot_exit(interp, 0);
}
/*
-=item C<static void do_dis(PARROT_INTERP)>
+=item C<static void do_dis(PARROT_INTERP, outfile, options)>
Do the disassembling.
@@ -91,9 +139,9 @@
*/
static void
-do_dis(PARROT_INTERP)
+do_dis(PARROT_INTERP, const char *outfile, Parrot_disassemble_options options)
{
- Parrot_disassemble(interp);
+ Parrot_disassemble(interp, outfile, options);
}
/*
@@ -112,6 +160,7 @@
actually run the disassembler to normal C comments (Wed, 16 Nov 2005).
Reini Urban: Renamed from disassemble to pbc_disassemble (2008-07-03).
+ Add options: help, -h, -o, bare (2009-01-29)
=cut
Modified: trunk/tools/util/dump_pbc.pl
==============================================================================
--- trunk/tools/util/dump_pbc.pl Fri Feb 13 11:19:46 2009 (r36670)
+++ trunk/tools/util/dump_pbc.pl Fri Feb 13 12:25:12 2009 (r36671)
@@ -70,9 +70,12 @@
my %cache;
foreach (@dis) {
- if (/^Current Source Filename (.*)/) {
- if ($cur_file ne $1) {
- $cur_file = $1;
+ if (/^(?:# )?Current Source Filename (.*)/) {
+ my $found = $1;
+ $found =~ s/^'//;
+ $found =~ s/'$//;
+ if ($cur_file ne $found) {
+ $cur_file = $found;
$cache{$cur_file} ||= slurp_file($cur_file);
$cur_line = -1;
More information about the parrot-commits
mailing list