Ticket #258: tt258-pbc_disassemble-options.patch

File tt258-pbc_disassemble-options.patch, 8.0 KB (added by rurban, 13 years ago)
  • include/parrot/embed.h

    old new  
    2222 
    2323typedef int Parrot_warnclass; 
    2424 
     25typedef enum { 
     26    enum_DIS_BARE      = 1, 
     27    enum_DIS_HEADER    = 2 
     28} Parrot_disassemble_options; 
     29 
    2530PARROT_EXPORT Parrot_Interp Parrot_new(Parrot_Interp parent); 
    2631 
    2732PARROT_EXPORT void Parrot_init_stacktop(Parrot_Interp, void *); 
     
    5863 
    5964PARROT_EXPORT Parrot_Opcode * Parrot_debug(Parrot_Interp, Parrot_Interp, Parrot_Opcode *pc); 
    6065 
    61 PARROT_EXPORT void Parrot_disassemble(Parrot_Interp); 
     66PARROT_EXPORT void Parrot_disassemble(Parrot_Interp, char *outfile, Parrot_disassemble_options options); 
    6267 
    6368PARROT_EXPORT 
    6469PARROT_DOES_NOT_RETURN 
  • src/embed.c

    old new  
    10401040    INTVAL i; 
    10411041 
    10421042    /* TODO: would be nice to print the name of the file as well */ 
    1043     Parrot_io_printf(interp, "Constant-table\n"); 
     1043    Parrot_io_printf(interp, "=head1 Constant-table\n\n"); 
    10441044 
    10451045    for (i = 0; i < numconstants; ++i) { 
    10461046        PackFile_Constant *c = interp->code->const_table->constants[i]; 
     
    11101110        } 
    11111111    } 
    11121112 
    1113     Parrot_io_printf(interp, "\n"); 
     1113    Parrot_io_printf(interp, "\n=cut\n\n"); 
    11141114} 
    11151115 
    11161116 
     
    11281128 
    11291129PARROT_EXPORT 
    11301130void 
    1131 Parrot_disassemble(PARROT_INTERP) 
     1131Parrot_disassemble(PARROT_INTERP, char *outfile, Parrot_disassemble_options options) 
    11321132{ 
    11331133    PDB_line_t *line; 
    11341134    PDB_t      *pdb             = mem_allocate_zeroed_typed(PDB_t); 
     
    11461146    debugs = (interp->code->debugs != NULL); 
    11471147 
    11481148    print_constant_table(interp); 
     1149    if (options & enum_DIS_HEADER) 
     1150        return; 
    11491151 
    1150     Parrot_io_printf(interp, "%12s-%12s", "Seq_Op_Num", "Relative-PC"); 
     1152    if (!(options & enum_DIS_BARE)) 
     1153        Parrot_io_printf(interp, "# %12s-%12s", "Seq_Op_Num", "Relative-PC"); 
    11511154 
    11521155    if (debugs) { 
    1153         Parrot_io_printf(interp, " %6s:\n", "SrcLn#"); 
     1156        if (!(options & enum_DIS_BARE)) 
     1157            Parrot_io_printf(interp, " %6s:\n", "SrcLn#"); 
    11541158        num_mappings = interp->code->debugs->num_mappings; 
    11551159    } 
    11561160    else { 
     
    11681172            if (op_code_seq_num == interp->code->debugs->mappings[curr_mapping]->offset) { 
    11691173                const int filename_const_offset = 
    11701174                    interp->code->debugs->mappings[curr_mapping]->filename; 
    1171                 Parrot_io_printf(interp, "Current Source Filename %Ss\n", 
     1175                Parrot_io_printf(interp, "# Current Source Filename '%Ss'\n", 
    11721176                        interp->code->const_table->constants[filename_const_offset]->u.string); 
    11731177                curr_mapping++; 
    11741178            } 
    11751179        } 
    11761180 
    1177         Parrot_io_printf(interp, "%012i-%012i", 
    1178                 op_code_seq_num, line->opcode - interp->code->base.data); 
     1181        if (!(options & enum_DIS_BARE)) 
     1182            Parrot_io_printf(interp, "%012i-%012i", 
     1183                             op_code_seq_num, line->opcode - interp->code->base.data); 
    11791184 
    1180         if (debugs) 
     1185        if (debugs && !(options & enum_DIS_BARE)) 
    11811186            Parrot_io_printf(interp, " %06i: ", 
    11821187                    interp->code->debugs->base.data[op_code_seq_num]); 
    11831188 
  • src/pbc_disassemble.c

    old new  
    88 
    99=head1 SYNOPSIS 
    1010 
    11     pbc_disassemble file.pbc 
     11    pbc_disassemble [-bh?] [-o outfile] [file.pbc] 
    1212 
    1313=head1 DESCRIPTION 
    1414 
     
    1616which in turn uses the C<PDB_disassemble()> function from 
    1717F<src/debug.c>. 
    1818 
     19Without non-option arguments it reads the pbc from STDIN. 
     20 
    1921=head2 Functions 
    2022 
    2123=over 4 
     
    3032#include <stdlib.h> 
    3133#include <ctype.h> 
    3234 
    33 static void do_dis(Parrot_Interp); 
     35static void do_dis(Parrot_Interp, char *, Parrot_disassemble_options); 
     36 
     37/* 
     38 
     39=item C<static void help(void)> 
     40 
     41Print out the user help info. 
     42 
     43=cut 
     44 
     45*/ 
     46 
     47static void help(void) 
     48{ 
     49    printf("pbc_disassemble - dump or convert parrot bytecode (PBC) files\n"); 
     50    printf("usage:\n"); 
     51    printf("pbc_disassemble [-bh] [--bare|--header-only] [file.pbc]\n"); 
     52    printf("pbc_disassemble -o converted.pasm file.pbc\n\n"); 
     53    printf("  -b\t\t ... bare .pasm without header and left column\n"); 
     54    printf("  -h\t\t ... dump Constant-table header only\n"); 
     55    printf("  -o filename\t ... output to filename\n"); 
     56    exit(EXIT_SUCCESS); 
     57} 
     58 
     59static struct longopt_opt_decl options[] = { 
     60    { 'h', 'h', OPTION_optional_FLAG, { "--header-only" } }, 
     61    { '?', '?', OPTION_optional_FLAG, { "--help" } }, 
     62    { 'b', 'b', OPTION_optional_FLAG, { "--bare" } }, 
     63    { 'o', 'o', OPTION_required_FLAG, { "--output" } } 
     64}; 
    3465 
    3566/* 
    3667 
     
    4677int 
    4778main(int argc, char *argv[]) 
    4879{ 
    49     Parrot_Interp interp; 
    50     char *filename; 
    5180    Parrot_PackFile pf; 
     81    Parrot_Interp interp; 
     82    char *outfile = NULL; 
     83    int option = 0; 
     84    struct longopt_opt_info opt = LONGOPT_OPT_INFO_INIT; 
     85    int status; 
    5286 
    5387    interp = Parrot_new(NULL); 
    54  
    5588    if (!interp) { 
    5689        return 1; 
    5790    } 
    58  
    59     /* set the top of the stack so GC can trace it for GC-able pointers 
    60      * see trace_system_areas() in src/cpu_dep.c */ 
    61     interp->lo_var_ptr = &interp; 
    62  
    63     if (argc != 2) { 
    64         fprintf(stderr, "Usage: pbc_disassemble programfile \n"); 
    65         Parrot_exit(interp, 1); 
     91    /* init and set top of stack */ 
     92    Parrot_init_stacktop(interp, &status); 
     93    while ((status = longopt_get(interp, 
     94                    argc, argv, options, &opt)) > 0) { 
     95        switch (opt.opt_id) { 
     96            case 'h': 
     97                option += enum_DIS_HEADER; 
     98                break; 
     99            case 'b': 
     100                option += enum_DIS_BARE; 
     101                break; 
     102            case 'o': 
     103                outfile = opt.opt_arg; 
     104                break; 
     105            case '?': 
     106            default: 
     107                help(); 
     108                break; 
     109        } 
    66110    } 
     111    if (status == -1) { 
     112        help(); 
     113    } 
     114    argc -= opt.opt_index; 
     115    argv += opt.opt_index; 
    67116 
    68     filename = argv[1]; 
    69  
    70     pf = Parrot_readbc(interp, filename); 
     117    pf = Parrot_readbc(interp, argc ? *argv : "-"); 
    71118 
    72119    if (!pf) { 
     120        printf("Can't read PBC\n"); 
    73121        return 1; 
    74122    } 
    75123 
    76124    Parrot_loadbc(interp, pf); 
    77125 
    78     do_dis(interp); 
     126    do_dis(interp, outfile, option); 
    79127 
    80128    Parrot_exit(interp, 0); 
    81129} 
    82130 
    83131/* 
    84132 
    85 =item C<static void do_dis(PARROT_INTERP)> 
     133=item C<static void do_dis(PARROT_INTERP, outfile, options)> 
    86134 
    87135Do the disassembling. 
    88136 
     
    91139*/ 
    92140 
    93141static void 
    94 do_dis(PARROT_INTERP) 
     142do_dis(PARROT_INTERP, char *outfile, Parrot_disassemble_options options) 
    95143{ 
    96     Parrot_disassemble(interp); 
     144    Parrot_disassemble(interp, outfile, options); 
    97145} 
    98146 
    99147/* 
     
    112160actually run the disassembler to normal C comments (Wed, 16 Nov 2005). 
    113161 
    114162Reini Urban: Renamed from disassemble to pbc_disassemble (2008-07-03). 
     163             add options: help, -h, -o, bare (2009-01-29) 
    115164 
    116165=cut 
    117166 
  • tools/util/dump_pbc.pl

    old new  
    7070    my %cache; 
    7171 
    7272    foreach (@dis) { 
    73         if    (/^Current Source Filename (.*)/) { 
    74             if ($cur_file ne $1) { 
    75                 $cur_file           = $1; 
     73        if (/^(?:# )?Current Source Filename (.*)/) { 
     74            my $found = $1; 
     75            $found =~ s/^'//; 
     76            $found =~ s/'$//; 
     77            if ($cur_file ne $found) { 
     78                $cur_file           = $found; 
    7679                $cache{$cur_file} ||= slurp_file($cur_file); 
    7780                $cur_line           = -1; 
    7881