-
Notifications
You must be signed in to change notification settings - Fork 138
/
main.c
478 lines (339 loc) · 11.4 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/*
Copyright (C) 2001-2010, Parrot Foundation.
=head1 NAME
pbc_dump - Dump or convert Parrot bytecode (PBC) files
=head1 SYNOPSIS
pbc_dump [-tdh] [--terse|--disassemble|--header-only] file.pbc
pbc_dump -o converted.pbc file.pbc
=head1 DESCRIPTION
A program to dump pack files to human readable form.
=head2 Command-Line Options
=over 4
=item C<-d>
Disassemble bytecode segments.
=item C<-h>
Dump the bytecode header only.
=item C<-t>
Terse output.
=item C<-D> C--debug> 1-7
Display detailed packfile reader debugging information if
F<include/parrot/packfile.h> enables TRACE_PACKFILE.
1 print general debug info
2 print alignment info
4 print values
=item C<-o converted.pbc>
Repacks a PBC file into the platform's native binary format for better
efficiency on reading non-native PBCs.
=back
=head2 Functions
=over 4
=cut
*/
#include "parrot/parrot.h"
#include "parrot/longopt.h"
#include "parrot/oplib/ops.h"
#include "parrot/oplib/core_ops.h"
#include "parrot/embed.h"
/*
=item C<static void const_dump(PARROT_INTERP, const PackFile_Segment *segp)>
Dump the constant table.
=cut
*/
static void
const_dump(PARROT_INTERP, const PackFile_Segment *segp)
{
Parrot_io_printf(interp, "%Ss => [\n", segp->name);
PackFile_ConstTable_dump(interp, (const PackFile_ConstTable *)segp);
Parrot_io_printf(interp, "],\n");
}
/*
=item C<static void disas_dump(PARROT_INTERP, const PackFile_Segment *self)>
Disassemble and dump.
=cut
*/
static void
disas_dump(PARROT_INTERP, const PackFile_Segment *self)
{
const opcode_t *pc = self->data;
const PackFile_ByteCode_OpMapping *map = &((const PackFile_ByteCode *)self)->op_mapping;
INTVAL i;
Parrot_io_printf(interp, "%Ss => [ # %d ops at offs 0x%x\n",
self->name, (int)self->size, (int)self->file_offset + 4);
for (i = 0; i < map->n_libs; i++) {
INTVAL j, lib_num, table_num;
PackFile_ByteCode_OpMappingEntry *entry = &map->libs[i];
Parrot_io_printf(interp, " map #%d => [\n", i);
Parrot_io_printf(interp, " oplib: \"%s\" version %d.%d.%d (%d ops)\n",
entry->lib->name,
entry->lib->major_version,
entry->lib->minor_version,
entry->lib->patch_version,
entry->n_ops);
for (j = 0; j < map->libs[i].n_ops; j++) {
lib_num = entry->lib_ops[j];
table_num = entry->table_ops[j];
Parrot_io_printf(interp, " %08lx => %08lx (%s)\n", table_num, lib_num,
entry->lib->op_info_table[lib_num].full_name);
}
Parrot_io_printf(interp, " ]\n");
}
while (pc < self->data + self->size) {
/* n can't be const; the ADD_OP_VAR_PART macro increments it */
size_t n = (size_t)interp->code->op_info_table[*pc]->op_count;
size_t i;
/* trace_op_dump(interp, self->pf->src, pc); */
Parrot_io_printf(interp, " %04x: ", (int)(pc - self->data));
for (i = 0; i < 6; ++i)
if (i < n)
Parrot_io_printf(interp, "%08lx ", (unsigned long)pc[i]);
else
Parrot_io_printf(interp, " ");
Parrot_io_printf(interp, "%s\n",
interp->code->op_info_table[*pc]->full_name);
ADD_OP_VAR_PART(interp, interp->code, pc, n);
pc += n;
}
Parrot_io_printf(interp, "]\n");
}
/*
=item C<static void nums_dump(PARROT_INTERP, const PackFile_Segment *self)>
Disassembles and dumps op names and line numbers only.
=cut
*/
static void
nums_dump(PARROT_INTERP, const PackFile_Segment *self)
{
const STRING *debug_name = Parrot_str_concat(interp, self->name,
Parrot_str_new_constant(interp, "_DB"));
const PackFile_Segment *debug = PackFile_find_segment(interp,
self->dir, debug_name, 1);
opcode_t * pc = self->data;
opcode_t * debug_ops = debug->data;
op_info_t ** const op_info = interp->code->op_info_table;
while (pc < self->data + self->size) {
/* n can't be const; the ADD_OP_VAR_PART macro increments it */
size_t n = (size_t)op_info[*pc]->op_count;
Parrot_io_printf(interp, " %04x: %s\n",
*(debug_ops++), op_info[*pc]->full_name);
ADD_OP_VAR_PART(interp, interp->code, pc, n);
pc += n;
}
}
/*
=item C<static void null_dump(PARROT_INTERP, const PackFile_Segment *self)>
Produces no output for the given segment type.
=cut
*/
static void
null_dump(SHIM_INTERP, const PackFile_Segment *self)
{
UNUSED(self);
}
/*
=item C<static void null_dir_dump(PARROT_INTERP, const PackFile_Segment *self)>
Dumps all of the segments of the given PackFile_Directory, but produces no
output for the directory itself.
=cut
*/
static void
null_dir_dump(PARROT_INTERP, const PackFile_Segment *self)
{
const PackFile_Directory * const dir = (const PackFile_Directory *)self;
size_t i;
for (i = 0; i < dir->num_segments; ++i)
self->pf->PackFuncs[dir->segments[i]->type].dump(interp, dir->segments[i]);
}
/*
=item C<static void PackFile_header_dump(PARROT_INTERP, PackFile *pf)>
Dump the header.
=cut
*/
static void
PackFile_header_dump(PARROT_INTERP, PackFile *pf)
{
Parrot_io_printf(interp, "HEADER => [\n");
Parrot_io_printf(interp, "\twordsize = %d", pf->header->wordsize);
Parrot_io_printf(interp, "\t(interpreter's wordsize/INTVAL = %d/%d)\n",
sizeof (opcode_t), sizeof (INTVAL));
Parrot_io_printf(interp, "\tbyteorder = %d", pf->header->byteorder);
Parrot_io_printf(interp, "\t(interpreter's byteorder = %d)\n",
PARROT_BIGENDIAN);
Parrot_io_printf(interp, "\tfloattype = %d", pf->header->floattype);
Parrot_io_printf(interp, "\t(interpreter's NUMVAL_SIZE = %d)\n",
NUMVAL_SIZE);
Parrot_io_printf(interp, "\tparrot-version %d.%d.%d, "
"bytecode-version %d.%d\n",
pf->header->major, pf->header->minor, pf->header->patch,
pf->header->bc_major, pf->header->bc_minor);
Parrot_io_printf(interp, "\tUUID: type = %d, size = %d",
pf->header->uuid_type, pf->header->uuid_size);
if (pf->header->uuid_size)
Parrot_io_printf(interp, ", '%s'\n", pf->header->uuid_data);
else
Parrot_io_printf(interp, "\n");
Parrot_io_printf(interp, "\t%s endianize, %s opcode, %s numval transform\n",
pf->need_endianize ? "**need**" : "no",
pf->need_wordsize ? "**need**" : "no",
pf->fetch_nv ? "**need**" : "no");
Parrot_io_printf(interp, "\tdirformat = %d\n", pf->header->dir_format);
Parrot_io_printf(interp, "]\n");
}
/*
=item C<static void help(void)>
Print out the user help info.
=cut
*/
static void help(void)
{
printf("pbc_dump - dump or convert parrot bytecode (PBC) files\n");
printf("usage:\n");
printf("pbc_dump [-tdh] [--terse|--disassemble|--header-only|--line-nums]"
" file.pbc\n");
printf("pbc_dump -o converted.pbc file.pbc\n\n");
printf("\t-d ... disassemble bytecode segments\n");
printf("\t-h ... dump header only\n");
printf("\t-t ... terse output\n");
printf("\t-n ... show ops and line numbers only\n");
#if TRACE_PACKFILE
printf("\t-D<1-7> --debug debug output\n");
printf("\t 1 general info\n");
printf("\t 2 alignment\n");
printf("\t 4 values\n");
#endif
printf("\t-o converted.pbc ... repacks a PBC file into "
"the platform's native\n");
printf("\t binary format for better efficiency on reading "
"non native PBCs\n");
exit(EXIT_SUCCESS);
}
static struct longopt_opt_decl opt_options[] = {
{ 'h', 'h', OPTION_optional_FLAG, { "--header-only" } },
{ '?', '?', OPTION_optional_FLAG, { "--help" } },
{ 't', 't', OPTION_optional_FLAG, { "--terse" } },
{ 'n', 'n', OPTION_optional_FLAG, { "--line-nums" } },
{ 'd', 'd', OPTION_optional_FLAG, { "--disassemble" } },
{ 'o', 'o', OPTION_required_FLAG, { "--output" } }
#if TRACE_PACKFILE
{ 'D', 'D', OPTION_required_FLAG, { "--debug" } },
#endif
};
/*
=item C<int main(int argc, const char **argv)>
The run loop. Process the command-line arguments and dump accordingly.
=cut
*/
int
main(int argc, const char **argv)
{
PackFile *pf;
Interp *interp;
const char *file = NULL;
int terse = 0;
int disas = 0;
int convert = 0;
int nums_only = 0;
int options = PFOPT_UTILS;
struct longopt_opt_info opt = LONGOPT_OPT_INFO_INIT;
int status;
if (argc < 2)
help();
interp = Parrot_new(NULL);
/* init and set top of stack */
Parrot_init_stacktop(interp, &status);
while ((status = longopt_get(argc, argv, opt_options, &opt)) > 0) {
switch (opt.opt_id) {
#if TRACE_PACKFILE
case 'D':
options += atoi(opt.opt_arg) << 2;
break;
#endif
case 'h':
options += PFOPT_HEADERONLY;
break;
case 't':
terse = 1;
break;
case 'd':
disas = 1;
break;
case 'o':
file = opt.opt_arg;
convert = 1;
break;
case 'n':
nums_only = 1;
break;
case '?':
default:
help();
break;
}
}
if (status == -1)
help();
argc -= opt.opt_index;
argv += opt.opt_index;
pf = Parrot_pbc_read(interp, *argv, options);
if (!pf) {
printf("Can't read PBC\n");
return 1;
}
Parrot_pbc_load(interp, pf);
if (convert) {
size_t size = PackFile_pack_size(interp,
interp->code->base.pf) * sizeof (opcode_t);
opcode_t *pack = (opcode_t *)Parrot_gc_allocate_memory_chunk(interp,
size);
FILE *fp;
if (!pack) {
printf("out of mem\n");
exit(EXIT_FAILURE);
}
PackFile_pack(interp, interp->code->base.pf, pack);
if (STREQ(file, "-"))
fp = stdout;
else if ((fp = fopen(file, "wb")) == 0) {
printf("Couldn't open %s\n", file);
exit(EXIT_FAILURE);
}
if ((1 != fwrite(pack, size, 1, fp))) {
printf("Couldn't write %s\n", file);
exit(EXIT_FAILURE);
}
fclose(fp);
Parrot_gc_free_memory_chunk(interp, pack);
Parrot_x_exit(interp, 0);
}
if (!nums_only)
PackFile_header_dump(interp, pf);
if (options & PFOPT_HEADERONLY)
Parrot_x_exit(interp, 0);
/* install a dumper function */
if (!terse) {
pf->PackFuncs[PF_CONST_SEG].dump = const_dump;
}
if (disas)
pf->PackFuncs[PF_BYTEC_SEG].dump = disas_dump;
if (nums_only) {
int i;
for (i = PF_DIR_SEG + 1; i < PF_MAX_SEG; ++i)
pf->PackFuncs[i].dump = null_dump;
pf->PackFuncs[PF_DIR_SEG].dump = null_dir_dump;
pf->PackFuncs[PF_BYTEC_SEG].dump = nums_dump;
}
/* do a directory dump, which dumps segs then */
PackFile_Segment_dump(interp, &pf->directory.base);
Parrot_x_exit(interp, 0);
}
/*
=back
=head1 SEE ALSO
F<src/packdump.c>.
=cut
*/
/*
* Local variables:
* c-file-style: "parrot"
* End:
* vim: expandtab shiftwidth=4 cinoptions='\:2=2' :
*/