Ticket #2134: tt2134.patch

File tt2134.patch, 16.2 KB (added by soh_cah_toa, 11 years ago)
  • MANIFEST

    diff --git a/MANIFEST b/MANIFEST
    index ea2a093..da7e37d 100644
    a b  
    869869ext/winxed/driver.pir                                       [] 
    870870frontend/parrot/main.c                                      [] 
    871871frontend/parrot_debugger/main.c                             [] 
     872frontend/pbc_disassemble/main.c                             [] 
    872873frontend/pbc_dump/main.c                                    [] 
    873874frontend/pbc_dump/packdump.c                                [] 
    874875frontend/pbc_merge/main.c                                   [] 
     
    12861287src/packfile/object_serialization.c                         [] 
    12871288src/packfile/output.c                                       [] 
    12881289src/packfile/pf_items.c                                     [] 
    1289 src/pbc_disassemble.c                                       [] 
    12901290src/platform/aix/asm.s                                      [] 
    12911291src/platform/ansi/dl.c                                      [] 
    12921292src/platform/ansi/exec.c                                    [] 
  • config/gen/makefiles/root.in

    diff --git a/config/gen/makefiles/root.in b/config/gen/makefiles/root.in
    index 947f3a9..fdd0a29 100644
    a b  
    10731073# Parrot Disassembler 
    10741074# 
    10751075 
    1076 src/pbc_disassemble$(O) : \ 
     1076frontend/pbc_disassemble/main$(O) : \ 
    10771077    $(PARROT_H_HEADERS) \ 
    10781078    $(INC_DIR)/api.h \ 
    10791079    $(INC_DIR)/longopt.h \ 
    1080     src/pbc_disassemble.c \ 
     1080    frontend/pbc_disassemble/main.c 
    10811081 
    1082 $(DIS) : src/pbc_disassemble$(O) $(LIBPARROT) 
     1082$(DIS) : frontend/pbc_disassemble/main$(O) $(LIBPARROT) 
    10831083        $(LINK) @ld_out@$@ \ 
    1084     src/pbc_disassemble$(O) \ 
     1084    frontend/pbc_disassemble/main$(O) \ 
    10851085    src/longopt$(O) \ 
    10861086    $(RPATH_BLIB) $(ALL_PARROT_LIBS) $(LINKFLAGS) 
    10871087#IF(win32):     if exist $@.manifest mt.exe -nologo -manifest $@.manifest -outputresource:$@;1 
    10881088 
    1089 $(INSTALLABLEDIS) : src/pbc_disassemble$(O) \ 
     1089$(INSTALLABLEDIS) : frontend/pbc_disassemble/main$(O) \ 
    10901090        src/install_config$(O) $(LIBPARROT) 
    10911091        $(LINK) @ld_out@$@ \ 
    1092     src/pbc_disassemble$(O) \ 
     1092    frontend/pbc_disassemble/main$(O) \ 
    10931093    src/longopt$(O) \ 
    10941094    @rpath_lib@ $(ALL_PARROT_LIBS) $(LINKFLAGS) 
    10951095#IF(win32):     if exist $@.manifest mt.exe -nologo -manifest $@.manifest -outputresource:$@;1 
     
    22862286    $(PDUMP) frontend/pbc_dump/main$(O) frontend/pbc_dump/packdump$(O) \ 
    22872287    $(PDB) frontend/parrot_debugger/main$(O) \ 
    22882288    $(PBC_MERGE) frontend/pbc_merge/main$(O) \ 
    2289     $(DIS) src/pbc_disassemble$(O) 
     2289    $(DIS) frontend/pbc_disassemble/main$(O) 
    22902290        $(RM_F) \ 
    22912291    frontend/parrot/main$(O) \ 
    22922292    src/null_config$(O) \ 
     
    23232323    $(PDUMP) frontend/pbc_dump/main$(O) frontend/pbc_dump/packdump$(O) \ 
    23242324    $(PDB) frontend/parrot_debugger/main$(O) \ 
    23252325    $(PBC_MERGE) frontend/pbc_merge/main$(O) \ 
    2326     $(DIS) src/pbc_disassemble$(O) \ 
     2326    $(DIS) frontend/pbc_disassemble/main$(O) \ 
    23272327    $(PARROT_CONFIG) parrot_config$(O) parrot_config.c \ 
    23282328    src/parrot_config$(O) parrot_config.pbc \ 
    23292329    pbc_to_exe$(EXE) pbc_to_exe$(O) pbc_to_exe.pbc \ 
  • (a) /dev/null vs. (b) b/frontend/pbc_disassemble/main.c

    diff --git a/frontend/pbc_disassemble/main.c b/frontend/pbc_disassemble/main.c
    new file mode 100644
    index 0000000..a213ccc
    a b  
     1/* Copyright (C) 2001-2009, Parrot Foundation. */ 
     2 
     3/* 
     4 
     5=head1 NAME 
     6 
     7pbc_disassemble - parrot bytecode disassembler 
     8 
     9=head1 SYNOPSIS 
     10 
     11pbc_disassemble [-bh?] [--bare|--header-only] [-o outfile] [file.pbc] 
     12 
     13=head1 DESCRIPTION 
     14 
     15C<pbc_disassemble> translates Parrot bytecode (C<PBC>) into Parrot assembly 
     16language (C<PASM>). 
     17 
     18C<file.pbc> is the bytecode file to disassemble. If a file is not specified, the 
     19bytecode will be read from C<stdin>. Additionally, if the C<-o> switch is not 
     20given, the output is displayed to C<stdout>. 
     21 
     22=head1 OPTIONS 
     23 
     24=over 4 
     25 
     26=item B<-?>, B<--help> 
     27 
     28Displays usage and help information. 
     29 
     30=item B<-b>, B<--bare> 
     31 
     32Displays bare PASM without the header and left column. 
     33 
     34=item B<-h>, B<--header-only> 
     35 
     36Displays only the constants table header. 
     37 
     38=item B<-o> filename, B<--output> filename 
     39 
     40Writes output to C<filename>. 
     41 
     42=back 
     43 
     44=head1 STATIC FUNCTIONS 
     45 
     46=over 4 
     47 
     48=cut 
     49 
     50*/ 
     51 
     52#include <stdio.h> 
     53#include <stdlib.h> 
     54#include <ctype.h> 
     55 
     56#include "parrot/parrot.h" 
     57#include "parrot/api.h" 
     58#include "parrot/longopt.h" 
     59 
     60#define PFOPT_UTILS 1 
     61 
     62/* NOTE: 
     63 * C<pbc_disassemble> uses the C<Parrot_disassemble()> function from F<src/embed.c>, 
     64 * which in turn uses the C<PDB_disassemble()> function from F<src/debug.c>. 
     65 */ 
     66 
     67/* Flags indicating the presence of the -b and -h command-line switches */ 
     68typedef enum { 
     69    enum_DIS_BARE      = 1,    /* -b switch */ 
     70    enum_DIS_HEADER    = 2     /* -h switch */ 
     71} Parrot_disassemble_options; 
     72 
     73/* Longopts option table */ 
     74static struct longopt_opt_decl options[] = { 
     75    { '?', '?', OPTION_optional_FLAG, { "--help"        } }, 
     76    { 'b', 'b', OPTION_optional_FLAG, { "--bare"        } }, 
     77    { 'h', 'h', OPTION_optional_FLAG, { "--header-only" } }, 
     78    { 'o', 'o', OPTION_required_FLAG, { "--output"      } }, 
     79    {  0 ,  0,  OPTION_optional_FLAG, { NULL            } } 
     80}; 
     81 
     82static void help(void); 
     83static void show_last_error_and_exit(Parrot_PMC interp); 
     84 
     85/* 
     86 
     87=item C<int main(int argc, const char *argv[])> 
     88 
     89Execution entry point. Starts up an interpreter, loads the bytecode from the 
     90command-line, and disassembles it. 
     91 
     92=cut 
     93 
     94*/ 
     95 
     96int 
     97main(int argc, const char *argv[]) 
     98{ 
     99    const char             *outfile  = NULL; 
     100    int                     option   = 0, 
     101                            status   = 0, 
     102                            debug    = PFOPT_UTILS; 
     103    struct longopt_opt_info opt      = LONGOPT_OPT_INFO_INIT; 
     104 
     105    Parrot_PMC              interp   = NULL, 
     106                            pbc      = NULL; 
     107    Parrot_String           filename = NULL; 
     108    Parrot_Init_Args       *initargs = NULL; 
     109 
     110    /* Parse command-line arguments */ 
     111    while ((status = longopt_get(argc, argv, options, &opt)) > 0) { 
     112        switch (opt.opt_id) { 
     113          case 'h': 
     114            option += enum_DIS_HEADER; 
     115            break; 
     116          case 'b': 
     117            option += enum_DIS_BARE; 
     118            break; 
     119          case 'o': 
     120            outfile = opt.opt_arg; 
     121            break; 
     122          case '?': 
     123            /* Fall through */ 
     124          default: 
     125            help(); 
     126            break; 
     127        } 
     128    } 
     129 
     130    /* Check for parse errors */ 
     131    if (status == -1) { 
     132        help(); 
     133    } 
     134 
     135    /* Set initialization parameters */ 
     136    GET_INIT_STRUCT(initargs); 
     137 
     138    /* Create new interpreter and set executable name */ 
     139    if (!(Parrot_api_make_interpreter(NULL, 0, initargs, &interp) 
     140        && Parrot_api_set_executable_name(interp, argv[0]))) { 
     141 
     142        fprintf(stderr, "PARROT VM: Could not initialize new interpreter\n"); 
     143        show_last_error_and_exit(interp); 
     144    } 
     145 
     146    argc -= opt.opt_index; 
     147    argv += opt.opt_index; 
     148 
     149    /* What to do about this debug flag? */ 
     150    /* pf = Parrot_pbc_read(interp, argc ? *argv : "-", debug); */ 
     151 
     152    /* Convert native char * to Parrot_String */ 
     153    if (!Parrot_api_string_import(interp, argc ? *argv : "-", &filename)) { 
     154        fprintf(stderr, "String transformation failed\n"); 
     155        show_last_error_and_exit(interp); 
     156    } 
     157 
     158    /* Disassemble bytecode and destroy interpreter */ 
     159    if (!(Parrot_api_load_bytecode_file(interp, filename, &pbc) 
     160        && Parrot_api_disassemble_bytecode(interp, pbc, outfile, option) 
     161        && Parrot_api_destroy_interpreter(interp))) { 
     162 
     163        fprintf(stderr, "Error during disassembly\n"); 
     164        show_last_error_and_exit(interp); 
     165    } 
     166 
     167    return 0; 
     168} 
     169 
     170/* 
     171 
     172=item C<static void help(void)> 
     173 
     174Prints out the user help information and exits. 
     175 
     176=cut 
     177 
     178*/ 
     179 
     180static void 
     181help(void) 
     182{ 
     183    puts("pbc_disassemble - parrot bytecode disassembler\n"); 
     184 
     185    puts("Usage:"); 
     186    puts("pbc_disassemble [-bh] [file.pbc]"); 
     187    puts("pbc_disassemble -o file.pasm file.pbc\n"); 
     188 
     189    puts("    -?, --help                   Displays help information"); 
     190    puts("    -b, --bare                   Displays bare PASM without header and left column"); 
     191    puts("    -h, --header-only            Dumps only the constants table header"); 
     192    puts("    -o, --output \"filename\"    Writes output to \"filename\""); 
     193 
     194    exit(EXIT_SUCCESS); 
     195} 
     196 
     197/* 
     198 
     199=item C<static void show_last_error_and_exit(Parrot_PMC interp)> 
     200 
     201Prints out the C<interp>'s last error and exits. 
     202 
     203=cut 
     204 
     205*/ 
     206 
     207static void 
     208show_last_error_and_exit(Parrot_PMC interp) 
     209{ 
     210    Parrot_Int    is_error, 
     211                  exit_code; 
     212    Parrot_String errmsg, 
     213                  backtrace; 
     214    Parrot_PMC    exception; 
     215 
     216    /* Get result of last API function call and exception backtrace */ 
     217    if (!(Parrot_api_get_result(interp, &is_error, &exception, &exit_code, &errmsg) 
     218        && Parrot_api_get_exception_backtrace(interp, exception, &backtrace))) { 
     219 
     220        fprintf(stderr, "PARROT VM: Cannot recover\n"); 
     221        exit(EXIT_FAILURE); 
     222    } 
     223 
     224    /* Check for unhandled exceptions */ 
     225    if (is_error) { 
     226        char *msg; 
     227 
     228        /* Display error message */ 
     229        Parrot_api_string_export_ascii(interp, errmsg, &msg); 
     230        fprintf(stderr, "%s\n", msg); 
     231        Parrot_api_string_free_exported_ascii(interp, msg); 
     232 
     233        /* Display exception backtrace */ 
     234        Parrot_api_string_export_ascii(interp, backtrace, &msg); 
     235        fprintf(stderr, "%s\n", msg); 
     236        Parrot_api_string_free_exported_ascii(interp, msg); 
     237    } 
     238 
     239    exit(exit_code); 
     240} 
     241 
     242/* 
     243 
     244=back 
     245 
     246=head1 SEE ALSO 
     247 
     248F<src/embed.c> and F<src/debug.c>. 
     249 
     250=head1 HISTORY 
     251 
     252Initial version by Daniel Grunblatt on 2002.5.26. 
     253 
     254Florian Ragwitz: Moved POD documentation that's not necessary to know how to 
     255actually run the disassembler to normal C comments (Wed, 16 Nov 2005). 
     256 
     257Reini Urban: Renamed from disassemble to pbc_disassemble (2008-07-03). 
     258             Add options: help, -h, -o, --debug, --bare (2009-01-29) 
     259             Force option 1 for passing version check (2009-03-07) 
     260 
     261Kevin Polulak (soh_cah_toa): Updated to use embedding API, moved source file 
     262                             to frontend/pbc_disassemble, and cleaned up 
     263                             source code and perldoc. (2011-06-19) 
     264 
     265=cut 
     266 
     267*/ 
     268 
     269/* 
     270 * Local variables: 
     271 *   c-file-style: "parrot" 
     272 * End: 
     273 * vim: expandtab shiftwidth=4 cinoptions='\:2=2' : 
     274 */ 
     275 
  • (a) a/src/pbc_disassemble.c vs. (b) /dev/null

    diff --git a/src/pbc_disassemble.c b/src/pbc_disassemble.c
    deleted file mode 100644
    index 8f59938..0000000
    a b  
    1 /* 
    2 Copyright (C) 2001-2009, Parrot Foundation. 
    3  
    4 =head1 NAME 
    5  
    6 pbc_disassemble - Parrot disassembler 
    7  
    8 =head1 SYNOPSIS 
    9  
    10     pbc_disassemble [-bdh?] [-o outfile] [file.pbc] 
    11  
    12 =head1 DESCRIPTION 
    13  
    14 This uses the C<Parrot_disassemble()> function from F<src/embed.c>, 
    15 which in turn uses the C<PDB_disassemble()> function from 
    16 F<src/debug.c>. 
    17  
    18 Without non-option arguments it reads the pbc from STDIN. 
    19  
    20 =head2 Functions 
    21  
    22 =over 4 
    23  
    24 =cut 
    25  
    26 */ 
    27  
    28 #include <stdio.h> 
    29 #include <stdlib.h> 
    30 #include <ctype.h> 
    31 #include "parrot/parrot.h" 
    32 #include "parrot/longopt.h" 
    33  
    34 #define PFOPT_UTILS 1 
    35  
    36 typedef enum { 
    37     enum_DIS_BARE      = 1, 
    38     enum_DIS_HEADER    = 2 
    39 } Parrot_disassemble_options; 
    40  
    41 static void show_last_error_and_exit(Parrot_PMC interp); 
    42  
    43 /* 
    44  
    45 =item C<static void help(void)> 
    46  
    47 Print out the user help info. 
    48  
    49 =cut 
    50  
    51 */ 
    52  
    53 static void help(void) 
    54 { 
    55     printf("pbc_disassemble - dump or convert parrot bytecode (PBC) files\n"); 
    56     printf("usage:\n"); 
    57     printf("pbc_disassemble [-bh] [--bare|--header-only] [file.pbc]\n"); 
    58     printf("pbc_disassemble -o converted.pasm file.pbc\n\n"); 
    59     printf("  -b\t\t ... bare .pasm without header and left column\n"); 
    60     printf("  -h\t\t ... dump Constant-table header only\n"); 
    61     printf("  -o filename\t ... output to filename\n"); 
    62     exit(EXIT_SUCCESS); 
    63 } 
    64  
    65 static struct longopt_opt_decl options[] = { 
    66     { 'h', 'h', OPTION_optional_FLAG, { "--header-only" } }, 
    67     { '?', '?', OPTION_optional_FLAG, { "--help" } }, 
    68     { 'b', 'b', OPTION_optional_FLAG, { "--bare" } }, 
    69     { 'o', 'o', OPTION_required_FLAG, { "--output" } }, 
    70     {  0 ,  0,  OPTION_optional_FLAG, { NULL } } 
    71 }; 
    72  
    73 /* 
    74  
    75 =item C<int main(int argc, const char *argv[])> 
    76  
    77 The run-loop. Starts up an interpreter, loads the bytecode from the 
    78 command-line and disassembles it. 
    79  
    80 =cut 
    81  
    82 */ 
    83  
    84 int 
    85 main(int argc, const char *argv[]) 
    86 { 
    87     Parrot_PMC interp; 
    88     Parrot_PMC pbc; 
    89     Parrot_String filename; 
    90     const char *outfile = NULL; 
    91     int option = 0; 
    92     int debug = PFOPT_UTILS; 
    93     struct longopt_opt_info opt = LONGOPT_OPT_INFO_INIT; 
    94     int status; 
    95     Parrot_Init_Args *initargs; 
    96     GET_INIT_STRUCT(initargs); 
    97  
    98     if (!(Parrot_api_make_interpreter(NULL, 0, initargs, &interp) && 
    99           Parrot_api_set_executable_name(interp, argv[0]))) { 
    100         fprintf(stderr, "PARROT VM: Could not initialize new interpreter"); 
    101         show_last_error_and_exit(interp); 
    102     } 
    103  
    104     while ((status = longopt_get(argc, argv, options, &opt)) > 0) { 
    105         switch (opt.opt_id) { 
    106           case 'h': 
    107             option += enum_DIS_HEADER; 
    108             break; 
    109           case 'b': 
    110             option += enum_DIS_BARE; 
    111             break; 
    112           case 'o': 
    113             outfile = opt.opt_arg; 
    114             break; 
    115           case '?': 
    116           default: 
    117             help(); 
    118             break; 
    119         } 
    120     } 
    121     if (status == -1) { 
    122         help(); 
    123     } 
    124     argc -= opt.opt_index; 
    125     argv += opt.opt_index; 
    126  
    127     /* What to do about this debug flag? */ 
    128     /* pf = Parrot_pbc_read(interp, argc ? *argv : "-", debug); */ 
    129  
    130     Parrot_api_string_import(interp, argc ? *argv : "-", &filename); 
    131     if (!(Parrot_api_load_bytecode_file(interp, filename, &pbc) && 
    132           Parrot_api_disassemble_bytecode(interp, pbc, outfile, option) && 
    133           Parrot_api_destroy_interpreter(interp))) { 
    134         fprintf(stderr, "Error during disassembly\n"); 
    135         show_last_error_and_exit(interp); 
    136     } 
    137     exit(EXIT_SUCCESS); 
    138 } 
    139  
    140 /* 
    141  
    142 =item C<static void show_last_error_and_exit(Parrot_PMC interp)> 
    143  
    144 Prints out the C<interp>'s last error and exits. 
    145  
    146 =cut 
    147  
    148 */ 
    149  
    150 static void 
    151 show_last_error_and_exit(Parrot_PMC interp) 
    152 { 
    153     Parrot_String errmsg, backtrace; 
    154     Parrot_Int exit_code, is_error; 
    155     Parrot_PMC exception; 
    156  
    157     if (!(Parrot_api_get_result(interp, &is_error, &exception, &exit_code, &errmsg) && 
    158           Parrot_api_get_exception_backtrace(interp, exception, &backtrace))) { 
    159         fprintf(stderr, "PARROT VM: Cannot recover\n"); 
    160         exit(EXIT_FAILURE); 
    161     } 
    162  
    163     if (errmsg) { 
    164         char * errmsg_raw; 
    165         Parrot_api_string_export_ascii(interp, errmsg, &errmsg_raw); 
    166         fprintf(stderr, "%s\n", errmsg_raw); 
    167         Parrot_api_string_free_exported_ascii(interp, errmsg_raw); 
    168  
    169         Parrot_api_string_export_ascii(interp, backtrace, &errmsg_raw); 
    170         fprintf(stderr, "%s\n", errmsg_raw); 
    171         Parrot_api_string_free_exported_ascii(interp, errmsg_raw); 
    172     } 
    173     exit(exit_code); 
    174 } 
    175  
    176 /* 
    177  
    178 =back 
    179  
    180 =head1 SEE ALSO 
    181  
    182 F<src/embed.c> and F<src/debug.c>. 
    183  
    184 =head1 HISTORY 
    185  
    186 Initial version by Daniel Grunblatt on 2002.5.26. 
    187  
    188 Florian Ragwitz: Moved POD documentation that's not necessary to know how to 
    189 actually run the disassembler to normal C comments (Wed, 16 Nov 2005). 
    190  
    191 Reini Urban: Renamed from disassemble to pbc_disassemble (2008-07-03). 
    192              Add options: help, -h, -o, --debug, --bare (2009-01-29) 
    193              Force option 1 for passing version check (2009-03-07) 
    194  
    195 =cut 
    196  
    197 */ 
    198  
    199  
    200 /* 
    201  * Local variables: 
    202  *   c-file-style: "parrot" 
    203  * End: 
    204  * vim: expandtab shiftwidth=4 cinoptions='\:2=2' : 
    205  */ 
  • t/tools/pbc_disassemble.t

    diff --git a/t/tools/pbc_disassemble.t b/t/tools/pbc_disassemble.t
    index b383c3f..2246506 100644
    a b  
    4848} 
    4949 
    5050my $helpregex = <<OUTPUT; 
    51 /pbc_disassemble - dump or convert parrot bytecode [(]PBC[)] files 
    52 usage: 
     51/pbc_disassemble - parrot bytecode disassembler 
     52 
     53Usage: 
    5354pbc_disassemble .* [[]file.pbc[]] 
    54 pbc_disassemble -o converted[.]pasm file[.]pbc 
    55 \\s+(-.*\\s+[.]{3}\\s+.* 
     55pbc_disassemble -o file[.]pasm file[.]pbc 
     56 
     57\\s+(-.{1},\\s+--.*(["]\\w+["])?\\s+.* 
    5658)+/m 
    5759OUTPUT 
    5860