Ticket #384: tt384-pbc_utils.patch

File tt384-pbc_utils.patch, 14.1 KB (added by rurban, 13 years ago)
  • src/pbc_info.c

    old new  
    44 
    55=head1 NAME 
    66 
    7 pbc_info - PacFile demo 
     7pbc_info - PackFile demo 
    88 
    99=head1 SYNOPSIS 
    1010 
     
    7070 
    7171    interp = Parrot_new(NULL); 
    7272 
    73     pf = Parrot_pbc_read(interp, argv[1], 0); 
     73    pf = Parrot_pbc_read(interp, argv[1], PFOPT_UTILS); 
    7474 
    7575    /* 
    7676     * add some more segments 
  • src/pbc_disassemble.c

    old new  
    5353    printf("  -b\t\t ... bare .pasm without header and left column\n"); 
    5454    printf("  -h\t\t ... dump Constant-table header only\n"); 
    5555#if TRACE_PACKFILE 
    56     printf("  -d\t\t ... debug\n"); 
     56    printf("\t-D<1-7> --debug debug output\n"); 
     57    printf("\t   1 general info\n"); 
     58    printf("\t   2 alignment\n"); 
     59    printf("\t   4 values\n"); 
    5760#endif 
    5861    printf("  -o filename\t ... output to filename\n"); 
    5962    exit(EXIT_SUCCESS); 
     
    6366    { 'h', 'h', OPTION_optional_FLAG, { "--header-only" } }, 
    6467    { '?', '?', OPTION_optional_FLAG, { "--help" } }, 
    6568    { 'b', 'b', OPTION_optional_FLAG, { "--bare" } }, 
    66     { 'd', 'd', OPTION_optional_FLAG, { "--debug" } }, 
     69#if TRACE_PACKFILE 
     70    { 'D', 'D', OPTION_required_FLAG, { "--debug" } }, 
     71#endif 
    6772    { 'o', 'o', OPTION_required_FLAG, { "--output" } } 
    6873}; 
    6974 
     
    8590    Parrot_Interp interp; 
    8691    const char *outfile = NULL; 
    8792    int option = 0; 
    88     int debug = 0; 
     93    int debug = PFOPT_UTILS; 
    8994    struct longopt_opt_info opt = LONGOPT_OPT_INFO_INIT; 
    9095    int status; 
    9196 
     
    107112            case 'o': 
    108113                outfile = opt.opt_arg; 
    109114                break; 
    110             case 'd': 
    111                 debug = 1; 
     115#if TRACE_PACKFILE 
     116            case 'D': 
     117                debug += atoi(opt.opt_arg) << 2; 
    112118                break; 
     119#endif 
    113120            case '?': 
    114121            default: 
    115122                help(); 
     
    171178 
    172179Reini Urban: Renamed from disassemble to pbc_disassemble (2008-07-03). 
    173180             Add options: help, -h, -o, --debug, --bare (2009-01-29) 
     181             Force option 1 for passing version check (2009-03-07) 
    174182 
    175183=cut 
    176184 
  • src/pbc_dump.c

    old new  
    3232 
    3333Terse output. 
    3434 
    35 =item C<-e> C--debug> 
     35=item C<-D> C--debug> 1-7 
    3636 
    3737Display detailed packfile reader debugging information if 
    38 F<include/parrot/packfile.h> enables TRACE_PACKFILE 
     38F<include/parrot/packfile.h> enables TRACE_PACKFILE. 
     39 
     40  1  print general debug info 
     41  2  print alignment info 
     42  4  print values 
    3943 
    4044=item C<-o converted.pbc> 
    4145 
     
    156160    Parrot_io_printf(interp, "\tparrot-version %d.%d.%d, bytecode-version %d.%d\n", 
    157161                     pf->header->major, pf->header->minor, pf->header->patch, 
    158162                     pf->header->bc_major, pf->header->bc_minor); 
    159     Parrot_io_printf(interp, "\tUUID type = %d, UUID size = %d\n", 
     163    Parrot_io_printf(interp, "\tUUID: type = %d, size = %d", 
    160164                     pf->header->uuid_type, pf->header->uuid_size); 
     165    if (pf->header->uuid_size) 
     166        Parrot_io_printf(interp, ", '%s'\n", pf->header->uuid_data); 
     167    else 
     168        Parrot_io_printf(interp, "\n"); 
    161169    Parrot_io_printf(interp, "\t%s endianize, %s opcode, %s numval transform\n", 
    162170            pf->need_endianize ? "**need**" : "no", 
    163171            pf->need_wordsize ? "**need**" : "no", 
     
    186194    printf("\t-h ... dump header only\n"); 
    187195    printf("\t-t ... terse output\n"); 
    188196#if TRACE_PACKFILE 
    189     printf("\t--debug debug output\n"); 
     197    printf("\t-D<1-7> --debug debug output\n"); 
     198    printf("\t   1 general info\n"); 
     199    printf("\t   2 alignment\n"); 
     200    printf("\t   4 values\n"); 
    190201#endif 
    191     printf("\n\t-o converted.pbc repacks a PBC file into " 
     202    printf("\t-o converted.pbc ... repacks a PBC file into " 
    192203           "the platform's native\n"); 
    193204    printf("\t   binary format for better efficiency on reading " 
    194205           "non native PBCs\n"); 
    195206    exit(EXIT_SUCCESS); 
    196207} 
    197208 
    198 static struct longopt_opt_decl options[] = { 
     209static struct longopt_opt_decl opt_options[] = { 
    199210    { 'h', 'h', OPTION_optional_FLAG, { "--header-only" } }, 
    200211    { '?', '?', OPTION_optional_FLAG, { "--help" } }, 
    201212    { 't', 't', OPTION_optional_FLAG, { "--terse" } }, 
    202213    { 'd', 'd', OPTION_optional_FLAG, { "--disassemble" } }, 
    203     { 'e', 'e', OPTION_optional_FLAG, { "--debug" } }, 
     214#if TRACE_PACKFILE 
     215    { 'D', 'D', OPTION_required_FLAG, { "--debug" } }, 
     216#endif 
    204217    { 'o', 'o', OPTION_required_FLAG, { "--output" } } 
    205218}; 
    206219 
     
    223236    int terse = 0; 
    224237    int disas = 0; 
    225238    int convert = 0; 
    226     int header = 0; 
    227     int debug = 0; 
     239    int options = PFOPT_UTILS; 
    228240    const char *file = NULL; 
    229241    struct longopt_opt_info opt = LONGOPT_OPT_INFO_INIT; 
    230242    int status; 
     
    236248    /* init and set top of stack */ 
    237249    Parrot_init_stacktop(interp, &status); 
    238250    while ((status = longopt_get(interp, 
    239                     argc, argv, options, &opt)) > 0) { 
     251                    argc, argv, opt_options, &opt)) > 0) { 
    240252        switch (opt.opt_id) { 
    241253            case 'h': 
    242                 header = 1; 
     254                options += PFOPT_HEADERONLY; 
    243255                break; 
    244256            case 't': 
    245257                terse = 1; 
     
    247259            case 'd': 
    248260                disas = 1; 
    249261                break; 
    250             case 'e': 
    251                 debug = 1; 
     262#if TRACE_PACKFILE 
     263            case 'D': 
     264                options += atoi(opt.opt_arg) << 2; 
    252265                break; 
     266#endif 
    253267            case 'o': 
    254268                file = opt.opt_arg; 
    255269                convert = 1; 
     
    267281    argv += opt.opt_index; 
    268282 
    269283 
    270     pf = Parrot_pbc_read(interp, *argv, debug); 
     284    pf = Parrot_pbc_read(interp, *argv, options); 
    271285 
    272286    if (!pf) { 
    273287        printf("Can't read PBC\n"); 
     
    304318    } 
    305319 
    306320    PackFile_header_dump(interp, pf); 
    307     if (header) { 
     321    if (options & PFOPT_HEADERONLY) { 
    308322        Parrot_exit(interp, 0); 
    309323    } 
    310324    /* install a dumper function */ 
  • src/packfile/pf_items.c

    old new  
    231231/* 
    232232 * offset not in ptr diff, but in byte 
    233233 */ 
    234 #define OFFS(cursor) ((const char *)(cursor) - (const char *)(pf->src)) 
     234#define OFFS(cursor) ((pf) ? ((const char *)(cursor) - (const char *)(pf->src)) : 0) 
    235235 
    236236/* 
    237237 * low level FLOATVAL fetch and convert functions 
     
    13071307    charset_name = Parrot_charset_c_name(interp, charset_nr); 
    13081308    s = string_make(interp, (const char *)*cursor, size, charset_name, flags); 
    13091309 
    1310 #if TRACE_PACKFILE == 2 
    1311     if (pf->options & 3) { 
    1312         /* print only printable characters */ 
    1313         Parrot_io_eprintf(NULL, "PF_fetch_string(): string is '%s' at 0x%x\n", 
    1314                           s->strstart, OFFS(*cursor)); 
    1315     } 
    1316 #endif 
     1310    /* print only printable characters */ 
     1311    TRACE_PRINTF_VAL((NULL, "PF_fetch_string(): string is '%s' at 0x%x\n", 
     1312                          s->strstart, OFFS(*cursor))); 
    13171313 
    13181314/*    s = string_make(interp, *cursor, size, 
    13191315            encoding_lookup_index(encoding), 
  • src/packfile.c

    old new  
    457457#define ROUND_16(val) (((val) & 0xf) ? 16 - ((val) & 0xf) : 0) 
    458458#define ALIGN_16(st, cursor) \ 
    459459    (cursor) += ROUND_16((const char *)(cursor) - (const char *)(st))/sizeof (opcode_t) 
     460/* offset not in ptr diff, but in byte */ 
     461#define OFFS(cursor) ((pf) ? ((const char *)(cursor) - (const char *)(pf->src)) : 0) 
    460462 
    461463#if TRACE_PACKFILE 
    462464void 
     
    910912valid and that Parrot can read this bytecode version, Parrot, and performing 
    911913any required endian and word size transforms. 
    912914 
    913 Returns size of unpacked if everything is okay, else zero (0). 
     915Returns size of unpacked opcodes if everything is okay, else zero (0). 
    914916 
    915917=cut 
    916918 
     
    954956        Parrot_io_eprintf(NULL, "PackFile_unpack: This Parrot cannot read " 
    955957            "bytecode files with version %d.%d.\n", 
    956958            header->bc_major, header->bc_minor); 
    957         return 0; 
     959        if (!(self->options & PFOPT_UTILS)) 
     960            return 0; 
    958961    } 
    959962 
    960963    /* Check wordsize, byte order and floating point number type are valid. */ 
     
    10371040 
    10381041    TRACE_PRINTF(("PackFile_unpack: Directory read, offset %d.\n", 
    10391042                  (INTVAL)cursor - (INTVAL)packed)); 
    1040  
    10411043    self->directory.base.file_offset = (INTVAL)cursor - (INTVAL)self->src; 
     1044    if (self->options & PFOPT_HEADERONLY) 
     1045        return cursor - packed; 
    10421046 
    10431047    /* now unpack dir, which unpacks its contents ... */ 
    10441048    Parrot_block_GC_mark(interp); 
     
    12451249    header->floattype = FLOATTYPE_16; 
    12461250#    else 
    12471251    exit_fatal(1, "PackFile_set_header: Unsupported floattype NUMVAL_SIZE=%d," 
    1248                " PARROT_BIGENDIAN=%d\n", NUMVAL_SIZE, 
     1252               " PARROT_BIGENDIAN=%s\n", NUMVAL_SIZE, 
    12491253               PARROT_BIGENDIAN ? "big-endian" : "little-endian"); 
    12501254#    endif 
    12511255#  endif 
     
    13111315    PackFile * const pf = mem_allocate_zeroed_typed(PackFile); 
    13121316    pf->header          = mem_allocate_zeroed_typed(PackFile_Header); 
    13131317    pf->is_mmap_ped     = is_mapped; 
    1314     pf->options         = 0; 
     1318    pf->options         = PFOPT_NONE; 
    13151319 
    13161320    /* fill header with system specific data */ 
    13171321    PackFile_set_header(pf->header); 
     
    18331837        TRACE_PRINTF(("PackFile_Segment_unpack: special\n")); 
    18341838 
    18351839        cursor = (f)(interp, self, cursor); 
    1836         TRACE_PRINTF_VAL(("  PackFile_Segment_unpack: offset=0x%x\n", 
    1837                           pf->src - cursor)); 
    1838  
    18391840        if (!cursor) 
    18401841            return NULL; 
    18411842    } 
    18421843 
    1843     TRACE_PRINTF_VAL(("pre-ALIGN_16: offset=0x%x src=0x%x cursor=0x%x\n", 
    1844                       pf->src - cursor, pf->src, cursor)); 
    1845  
     1844    TRACE_PRINTF_ALIGN(("-ALIGN_16: offset=0x%x src=0x%x cursor=0x%x\n", 
     1845                      OFFS(cursor), pf->src, cursor)); 
    18461846    /* FIXME on 64bit reading 32bit */ 
    18471847    ALIGN_16(self->pf->src, cursor); 
    1848     TRACE_PRINTF_VAL(("ALIGN_16: offset=0x%x src=0x%x cursor=0x%x\n", 
    1849                       pf->src - cursor, pf->src, cursor)); 
     1848    TRACE_PRINTF_ALIGN(("+ALIGN_16: offset=0x%x src=0x%x cursor=0x%x\n", 
     1849                      OFFS(cursor), pf->src, cursor)); 
    18501850    return cursor; 
    18511851} 
    18521852 
     
    20302030        seg->dir         = dir; 
    20312031    } 
    20322032 
    2033     TRACE_PRINTF_VAL(("pre-ALIGN_16: offset=0x%x src=0x%x cursor=0x%x\n", 
    2034                       pf->src - cursor, pf->src, cursor)); 
    2035  
     2033    TRACE_PRINTF_ALIGN(("-ALIGN_16: offset=0x%x src=0x%x cursor=0x%x\n", 
     2034                      OFFS(cursor), pf->src, cursor)); 
    20362035    /* FIXME on 64bit reading 32bit */ 
    20372036    ALIGN_16(pf->src, cursor); 
    2038     TRACE_PRINTF_VAL(("ALIGN_16: offset=0x%x src=0x%x cursor=0x%x\n", 
    2039                       pf->src - cursor, pf->src, cursor)); 
     2037    TRACE_PRINTF_ALIGN(("+ALIGN_16: offset=0x%x src=0x%x cursor=0x%x\n", 
     2038                      OFFS(cursor), pf->src, cursor)); 
    20402039 
    20412040    /* and now unpack contents of dir */ 
    20422041    for (i = 0; cursor && i < dir->num_segments; i++) { 
     
    20612060            TRACE_PRINTF_VAL(("PackFile_Segment_unpack ok. pos=0x%x\n", pos)); 
    20622061        } 
    20632062 
    2064         /* BUG: on 64bit reading 32bit lurking here! */ 
     2063        /* FIXME bug on 64bit reading 32bit lurking here! TT #254 */ 
    20652064        if (pf->need_wordsize) { 
    20662065#if OPCODE_T_SIZE == 8 
    20672066            if (pf->header->wordsize == 4) 
  • include/parrot/packfile.h

    old new  
    4343#define FLOATTYPE_4           5 
    4444#define FLOATTYPE_4_NAME      "4-byte float" 
    4545 
     46/* 
     47** Debug printf packfile reading: 
     48**   0 to disable 
     49**   1 to print basic info 
     50**   2 to print also values 
     51** Use also ./pbc_dump -D<1-7> to finetune. See F<src/pbc_dump.c> 
     52*/ 
    4653#define TRACE_PACKFILE 0 
    4754 
     55/* 
     56** Parrot_pbc_read() options: 
     57**   parrot, pbc_merge, parrot_debugger use 0 
     58**   pbc_dump, pbc_disassemble, pbc_info use 1 to skip the version check 
     59**   pbc_dump -h requires 2 
     60**   The rest is for TRACE_PACKFILE debugging with switch -D in pbc_dump 
     61*/ 
     62#define PFOPT_NONE  0 
     63#define PFOPT_UTILS 1 
     64#define PFOPT_HEADERONLY 2 
     65#if TRACE_PACKFILE 
     66#  define PFOPT_DEBUG 4 
     67#  define PFOPT_ALIGN 8 
     68#  define PFOPT_VALUE 16 
     69#endif 
     70 
    4871#if TRACE_PACKFILE 
    4972/* Here we pass multipe args to a macro so the args may not be bracketed here! */ 
    50 #  define TRACE_PRINTF(args)       if (pf->options & 1) Parrot_trace_eprintf args 
    51 #  define TRACE_PRINTF_ALIGN(args) if (pf->options & 4) Parrot_trace_eprintf args 
     73#  define TRACE_PRINTF(args)       if (pf->options & PFOPT_DEBUG) \ 
     74        Parrot_trace_eprintf args 
     75#  define TRACE_PRINTF_ALIGN(args) if (pf->options & PFOPT_ALIGN) \ 
     76        Parrot_trace_eprintf args 
    5277#  if TRACE_PACKFILE == 2 
    53 #    define TRACE_PRINTF_VAL(args) if (pf->options & 2) Parrot_trace_eprintf args 
     78#    define TRACE_PRINTF_VAL(args) if (pf->options & PFOPT_VALUE) \ 
     79        Parrot_trace_eprintf args 
    5480#    define TRACE_PRINTF_2(args)   Parrot_trace_eprintf args 
    5581#  else 
    5682#    define TRACE_PRINTF_VAL(args) 
  • src/embed.c

    old new  
    541541        return NULL; 
    542542    } 
    543543 
    544     /* Set :main routine */ 
    545     do_sub_pragmas(interp, pf->cur_cs, PBC_PBC, NULL); 
     544    if (!(pf->options & PFOPT_HEADERONLY)) 
     545        /* Set :main routine */ 
     546        do_sub_pragmas(interp, pf->cur_cs, PBC_PBC, NULL); 
    546547 
    547548    /* JITting and/or prederefing the sub/the bytecode is done 
    548549     * in switch_to_cs before actual usage of the segment */