remove deprecation notice for TT #449
[parrot.git] / src / pbc_dump.c
blobcce46448157ad437fc0bba4db04ff1d3dcc878b5
1 /*
2 Copyright (C) 2001-2010, Parrot Foundation.
3 $Id$
5 =head1 NAME
7 pbc_dump - Dump or convert Parrot bytecode (PBC) files
9 =head1 SYNOPSIS
11 pbc_dump [-tdh] [--terse|--disassemble|--header-only] file.pbc
13 pbc_dump -o converted.pbc file.pbc
15 =head1 DESCRIPTION
17 A program to dump pack files to human readable form.
19 =head2 Command-Line Options
21 =over 4
23 =item C<-d>
25 Disassemble bytecode segments.
27 =item C<-h>
29 Dump the bytecode header only.
31 =item C<-t>
33 Terse output.
35 =item C<-D> C--debug> 1-7
37 Display detailed packfile reader debugging information if
38 F<include/parrot/packfile.h> enables TRACE_PACKFILE.
40 1 print general debug info
41 2 print alignment info
42 4 print values
44 =item C<-o converted.pbc>
46 Repacks a PBC file into the platform's native binary format for better
47 efficiency on reading non-native PBCs.
49 =back
51 =head2 Functions
53 =over 4
55 =cut
59 #include "parrot/parrot.h"
60 #include "parrot/embed.h"
61 #include "parrot/oplib/ops.h"
65 =item C<static void const_dump(PARROT_INTERP, const PackFile_Segment *segp)>
67 Dump the constant table.
69 =cut
73 static void
74 const_dump(PARROT_INTERP, const PackFile_Segment *segp)
76 Parrot_io_printf(interp, "%Ss => [\n", segp->name);
77 PackFile_ConstTable_dump(interp, (const PackFile_ConstTable *)segp);
78 Parrot_io_printf(interp, "],\n");
84 =item C<static void fixup_dump(PARROT_INTERP, const PackFile_Segment *segp)>
86 Dump the fixup table.
88 =cut
92 static void
93 fixup_dump(PARROT_INTERP, const PackFile_Segment *segp)
95 Parrot_io_printf(interp, "%Ss => [\n", segp->name);
96 PackFile_Fixup_dump(interp, (const PackFile_FixupTable *)segp);
97 Parrot_io_printf(interp, "],\n");
103 =item C<static void disas_dump(PARROT_INTERP, const PackFile_Segment *self)>
105 Disassemble and dump.
107 =cut
111 static void
112 disas_dump(PARROT_INTERP, const PackFile_Segment *self)
114 const opcode_t *pc = self->data;
116 Parrot_io_printf(interp, "%Ss => [ # %d ops at offs 0x%x\n",
117 self->name, (int)self->size, (int)self->file_offset + 4);
119 while (pc < self->data + self->size) {
120 /* n can't be const; the ADD_OP_VAR_PART macro increments it */
121 size_t n = (size_t)interp->op_info_table[*pc].op_count;
122 size_t i;
124 /* trace_op_dump(interp, self->pf->src, pc); */
125 Parrot_io_printf(interp, " %04x: ", (int)(pc - self->data));
127 for (i = 0; i < 6; ++i)
128 if (i < n)
129 Parrot_io_printf(interp, "%08lx ", (unsigned long)pc[i]);
130 else
131 Parrot_io_printf(interp, " ");
133 Parrot_io_printf(interp, "%s\n",
134 interp->op_info_table[*pc].full_name);
136 ADD_OP_VAR_PART(interp, interp->code, pc, n);
137 pc += n;
140 Parrot_io_printf(interp, "]\n");
146 =item C<static void nums_dump(PARROT_INTERP, const PackFile_Segment *self)>
148 Disassembles and dumps op names and line numbers only.
150 =cut
154 static void
155 nums_dump(PARROT_INTERP, const PackFile_Segment *self)
157 const STRING *debug_name = Parrot_str_concat(interp, self->name,
158 Parrot_str_new_constant(interp, "_DB"));
159 const PackFile_Segment *debug = PackFile_find_segment(interp,
160 self->dir, debug_name, 1);
162 const opcode_t * pc = self->data;
163 const opcode_t * debug_ops = debug->data;
164 const op_info_t * const op_info = interp->op_info_table;
166 while (pc < self->data + self->size) {
167 /* n can't be const; the ADD_OP_VAR_PART macro increments it */
168 size_t n = (size_t)op_info[*pc].op_count;
170 Parrot_io_printf(interp, " %04x: %s\n",
171 *(debug_ops++), op_info[*pc].full_name);
173 ADD_OP_VAR_PART(interp, interp->code, pc, n);
174 pc += n;
181 =item C<static void null_dump(PARROT_INTERP, const PackFile_Segment *self)>
183 Produces no output for the given segment type.
185 =cut
189 static void
190 null_dump(SHIM_INTERP, const PackFile_Segment *self)
192 UNUSED(self);
198 =item C<static void null_dir_dump(PARROT_INTERP, const PackFile_Segment *self)>
200 Dumps all of the segments of the given PackFile_Directory, but produces no
201 output for the directory itself.
203 =cut
207 static void
208 null_dir_dump(PARROT_INTERP, const PackFile_Segment *self)
210 const PackFile_Directory * const dir = (const PackFile_Directory *)self;
211 size_t i;
213 for (i = 0; i < dir->num_segments; ++i)
214 self->pf->PackFuncs[dir->segments[i]->type].dump(interp, dir->segments[i]);
220 =item C<static void PackFile_header_dump(PARROT_INTERP, PackFile *pf)>
222 Dump the header.
224 =cut
228 static void
229 PackFile_header_dump(PARROT_INTERP, PackFile *pf)
231 Parrot_io_printf(interp, "HEADER => [\n");
232 Parrot_io_printf(interp, "\twordsize = %d", pf->header->wordsize);
233 Parrot_io_printf(interp, "\t(interpreter's wordsize/INTVAL = %d/%d)\n",
234 sizeof (opcode_t), sizeof (INTVAL));
235 Parrot_io_printf(interp, "\tbyteorder = %d", pf->header->byteorder);
236 Parrot_io_printf(interp, "\t(interpreter's byteorder = %d)\n",
237 PARROT_BIGENDIAN);
238 Parrot_io_printf(interp, "\tfloattype = %d", pf->header->floattype);
239 Parrot_io_printf(interp, "\t(interpreter's NUMVAL_SIZE = %d)\n",
240 NUMVAL_SIZE);
241 Parrot_io_printf(interp, "\tparrot-version %d.%d.%d, "
242 "bytecode-version %d.%d\n",
243 pf->header->major, pf->header->minor, pf->header->patch,
244 pf->header->bc_major, pf->header->bc_minor);
245 Parrot_io_printf(interp, "\tUUID: type = %d, size = %d",
246 pf->header->uuid_type, pf->header->uuid_size);
248 if (pf->header->uuid_size)
249 Parrot_io_printf(interp, ", '%s'\n", pf->header->uuid_data);
250 else
251 Parrot_io_printf(interp, "\n");
253 Parrot_io_printf(interp, "\t%s endianize, %s opcode, %s numval transform\n",
254 pf->need_endianize ? "**need**" : "no",
255 pf->need_wordsize ? "**need**" : "no",
256 pf->fetch_nv ? "**need**" : "no");
258 Parrot_io_printf(interp, "\tdirformat = %d\n", pf->header->dir_format);
259 Parrot_io_printf(interp, "]\n");
265 =item C<static void help(void)>
267 Print out the user help info.
269 =cut
273 static void help(void)
275 printf("pbc_dump - dump or convert parrot bytecode (PBC) files\n");
276 printf("usage:\n");
277 printf("pbc_dump [-tdh] [--terse|--disassemble|--header-only|--line-nums]"
278 " file.pbc\n");
279 printf("pbc_dump -o converted.pbc file.pbc\n\n");
280 printf("\t-d ... disassemble bytecode segments\n");
281 printf("\t-h ... dump header only\n");
282 printf("\t-t ... terse output\n");
283 printf("\t-n ... show ops and line numbers only\n");
285 #if TRACE_PACKFILE
286 printf("\t-D<1-7> --debug debug output\n");
287 printf("\t 1 general info\n");
288 printf("\t 2 alignment\n");
289 printf("\t 4 values\n");
290 #endif
292 printf("\t-o converted.pbc ... repacks a PBC file into "
293 "the platform's native\n");
294 printf("\t binary format for better efficiency on reading "
295 "non native PBCs\n");
296 exit(EXIT_SUCCESS);
300 static struct longopt_opt_decl opt_options[] = {
301 { 'h', 'h', OPTION_optional_FLAG, { "--header-only" } },
302 { '?', '?', OPTION_optional_FLAG, { "--help" } },
303 { 't', 't', OPTION_optional_FLAG, { "--terse" } },
304 { 'n', 'n', OPTION_optional_FLAG, { "--line-nums" } },
305 { 'd', 'd', OPTION_optional_FLAG, { "--disassemble" } },
306 { 'o', 'o', OPTION_required_FLAG, { "--output" } }
308 #if TRACE_PACKFILE
309 { 'D', 'D', OPTION_required_FLAG, { "--debug" } },
310 #endif
316 =item C<int main(int argc, const char **argv)>
318 The run loop. Process the command-line arguments and dump accordingly.
320 =cut
325 main(int argc, const char **argv)
327 PackFile *pf;
328 Interp *interp;
330 const char *file = NULL;
331 int terse = 0;
332 int disas = 0;
333 int convert = 0;
334 int nums_only = 0;
335 int options = PFOPT_UTILS;
337 struct longopt_opt_info opt = LONGOPT_OPT_INFO_INIT;
339 int status;
341 if (argc < 2)
342 help();
344 interp = Parrot_new(NULL);
346 /* init and set top of stack */
347 Parrot_init_stacktop(interp, &status);
349 while ((status = longopt_get(interp, argc, argv, opt_options, &opt)) > 0) {
350 switch (opt.opt_id) {
351 #if TRACE_PACKFILE
352 case 'D':
353 options += atoi(opt.opt_arg) << 2;
354 break;
355 #endif
356 case 'h':
357 options += PFOPT_HEADERONLY;
358 break;
359 case 't':
360 terse = 1;
361 break;
362 case 'd':
363 disas = 1;
364 break;
365 case 'o':
366 file = opt.opt_arg;
367 convert = 1;
368 break;
369 case 'n':
370 nums_only = 1;
371 break;
372 case '?':
373 default:
374 help();
375 break;
379 if (status == -1)
380 help();
382 argc -= opt.opt_index;
383 argv += opt.opt_index;
385 pf = Parrot_pbc_read(interp, *argv, options);
387 if (!pf) {
388 printf("Can't read PBC\n");
389 return 1;
392 Parrot_pbc_load(interp, pf);
394 if (convert) {
395 size_t size = PackFile_pack_size(interp,
396 interp->code->base.pf) * sizeof (opcode_t);
397 opcode_t *pack = (opcode_t *)Parrot_gc_allocate_memory_chunk(interp,
398 size);
399 FILE *fp;
401 if (!pack) {
402 printf("out of mem\n");
403 exit(EXIT_FAILURE);
406 PackFile_pack(interp, interp->code->base.pf, pack);
408 if (STREQ(file, "-"))
409 fp = stdout;
410 else if ((fp = fopen(file, "wb")) == 0) {
411 printf("Couldn't open %s\n", file);
412 exit(EXIT_FAILURE);
415 if ((1 != fwrite(pack, size, 1, fp))) {
416 printf("Couldn't write %s\n", file);
417 exit(EXIT_FAILURE);
420 fclose(fp);
421 Parrot_gc_free_memory_chunk(interp, pack);
422 Parrot_exit(interp, 0);
425 if (!nums_only)
426 PackFile_header_dump(interp, pf);
428 if (options & PFOPT_HEADERONLY)
429 Parrot_exit(interp, 0);
431 /* install a dumper function */
432 if (!terse) {
433 pf->PackFuncs[PF_CONST_SEG].dump = const_dump;
434 pf->PackFuncs[PF_FIXUP_SEG].dump = fixup_dump;
437 if (disas)
438 pf->PackFuncs[PF_BYTEC_SEG].dump = disas_dump;
440 if (nums_only) {
441 int i;
443 for (i = PF_DIR_SEG + 1; i < PF_MAX_SEG; ++i)
444 pf->PackFuncs[i].dump = null_dump;
446 pf->PackFuncs[PF_DIR_SEG].dump = null_dir_dump;
447 pf->PackFuncs[PF_BYTEC_SEG].dump = nums_dump;
450 /* do a directory dump, which dumps segs then */
451 PackFile_Segment_dump(interp, &pf->directory.base);
453 Parrot_exit(interp, 0);
459 =back
461 =head1 SEE ALSO
463 F<src/packdump.c>.
465 =cut
470 * Local variables:
471 * c-file-style: "parrot"
472 * End:
473 * vim: expandtab shiftwidth=4: