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