fix codetest failure - ASSERT_ARGS does not have a ; after and
[parrot.git] / src / pbc_disassemble.c
blob261592e87cf583231f6ac2d42abd337af52dfe1a
1 /*
2 Copyright (C) 2001-2009, Parrot Foundation.
3 $Id$
5 =head1 NAME
7 pbc_disassemble - Parrot disassembler
9 =head1 SYNOPSIS
11 pbc_disassemble [-bdh?] [-o outfile] [file.pbc]
13 =head1 DESCRIPTION
15 This uses the C<Parrot_disassemble()> function from F<src/embed.c>,
16 which in turn uses the C<PDB_disassemble()> function from
17 F<src/debug.c>.
19 Without non-option arguments it reads the pbc from STDIN.
21 =head2 Functions
23 =over 4
25 =cut
29 #include <parrot/parrot.h>
30 #include "parrot/embed.h"
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <ctype.h>
35 static void do_dis(Parrot_Interp, const char *, int);
39 =item C<static void help(void)>
41 Print out the user help info.
43 =cut
47 static void help(void)
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 #if TRACE_PACKFILE
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");
60 #endif
61 printf(" -o filename\t ... output to filename\n");
62 exit(EXIT_SUCCESS);
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 #if TRACE_PACKFILE
70 { 'D', 'D', OPTION_required_FLAG, { "--debug" } },
71 #endif
72 { 'o', 'o', OPTION_required_FLAG, { "--output" } }
77 =item C<int main(int argc, const char *argv[])>
79 The run-loop. Starts up an interpreter, loads the bytecode from the
80 command-line and disassembles it.
82 =cut
86 int
87 main(int argc, const char *argv[])
89 Parrot_PackFile pf;
90 Parrot_Interp interp;
91 const char *outfile = NULL;
92 int option = 0;
93 int debug = PFOPT_UTILS;
94 struct longopt_opt_info opt = LONGOPT_OPT_INFO_INIT;
95 int status;
97 interp = Parrot_new(NULL);
98 if (!interp) {
99 return 1;
101 /* init and set top of stack */
102 Parrot_init_stacktop(interp, &status);
103 while ((status = longopt_get(interp,
104 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 #if TRACE_PACKFILE
116 case 'D':
117 debug += atoi(opt.opt_arg) << 2;
118 break;
119 #endif
120 case '?':
121 default:
122 help();
123 break;
126 if (status == -1) {
127 help();
129 argc -= opt.opt_index;
130 argv += opt.opt_index;
132 pf = Parrot_pbc_read(interp, argc ? *argv : "-", debug);
134 if (!pf) {
135 printf("Can't read PBC\n");
136 return 1;
139 Parrot_pbc_load(interp, pf);
141 do_dis(interp, outfile, option);
143 Parrot_exit(interp, 0);
148 =item C<static void do_dis(PARROT_INTERP, const char *outfile, int options)>
150 Do the disassembling.
152 C<option> is currently used to pass debugging flags to the packfile reader.
154 =cut
158 static void
159 do_dis(PARROT_INTERP, const char *outfile, int options)
161 Parrot_disassemble(interp, outfile, (Parrot_disassemble_options) options);
166 =back
168 =head1 SEE ALSO
170 F<src/embed.c> and F<src/debug.c>.
172 =head1 HISTORY
174 Initial version by Daniel Grunblatt on 2002.5.26.
176 Florian Ragwitz: Moved POD documentation that's not necessary to know how to
177 actually run the disassembler to normal C comments (Wed, 16 Nov 2005).
179 Reini Urban: Renamed from disassemble to pbc_disassemble (2008-07-03).
180 Add options: help, -h, -o, --debug, --bare (2009-01-29)
181 Force option 1 for passing version check (2009-03-07)
183 =cut
189 * Local variables:
190 * c-file-style: "parrot"
191 * End:
192 * vim: expandtab shiftwidth=4: