[t][TT #1119] Convert t/op/bitwise.t to PIR
[parrot.git] / src / packdump.c
blob1db951876a1080df52184e2e963f488e59cc1a89
1 /*
2 Copyright (C) 2001-2009, Parrot Foundation.
3 This program is free software. It is subject to the same license as
4 Parrot itself.
5 $Id$
7 =head1 NAME
9 src/packdump.c - Functions for dumping packfile structures
11 =head1 DESCRIPTION
13 This is only used by the PBC dumper C<pbc_dump>.
15 =head2 Functions
17 =over 4
19 =cut
23 #include "parrot/parrot.h"
24 #include "parrot/packfile.h"
25 #include "pmc/pmc_sub.h"
26 #include "pmc/pmc_key.h"
28 /* HEADERIZER HFILE: include/parrot/packfile.h */
30 /* HEADERIZER BEGIN: static */
31 /* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
33 static void PackFile_Constant_dump(PARROT_INTERP,
34 ARGIN(const PackFile_ConstTable *ct),
35 ARGIN(const PackFile_Constant *self))
36 __attribute__nonnull__(1)
37 __attribute__nonnull__(2)
38 __attribute__nonnull__(3);
40 static void pobj_flag_dump(PARROT_INTERP, long flags)
41 __attribute__nonnull__(1);
43 #define ASSERT_ARGS_PackFile_Constant_dump __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
44 PARROT_ASSERT_ARG(interp) \
45 , PARROT_ASSERT_ARG(ct) \
46 , PARROT_ASSERT_ARG(self))
47 #define ASSERT_ARGS_pobj_flag_dump __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
48 PARROT_ASSERT_ARG(interp))
49 /* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
50 /* HEADERIZER END: static */
55 =item C<void PackFile_ConstTable_dump(PARROT_INTERP, const PackFile_ConstTable
56 *self)>
58 Dumps the constant table C<self>.
60 =cut
64 PARROT_EXPORT
65 void
66 PackFile_ConstTable_dump(PARROT_INTERP, ARGIN(const PackFile_ConstTable *self))
68 ASSERT_ARGS(PackFile_ConstTable_dump)
69 opcode_t i;
71 for (i = 0; i < self->const_count; i++) {
72 Parrot_io_printf(interp, " # %ld:\n", (long)i);
73 PackFile_Constant_dump(interp, self, self->constants[i]);
79 =item C<static void PackFile_Constant_dump(PARROT_INTERP, const
80 PackFile_ConstTable *ct, const PackFile_Constant *self)>
82 Dumps the constant C<self>.
84 =cut
88 /* [this desperately needs better abstraction, so we're not duplicating the enum
89 * PObj_enum definition in the include/parrot/pobj.h file. -- rgr, 1-Mar-08.]
91 PARROT_OBSERVER static const char * const flag_bit_names[] =
93 "private0",
94 "private1",
95 "private2",
96 "private3",
97 "private4",
98 "private5",
99 "private6",
100 "private7",
101 "is_string",
102 "is_PMC",
103 "is_shared",
104 "constant",
105 "external",
106 "aligned",
107 "sysmem",
108 "COW",
109 "is_COWable",
110 "live",
111 "on_free_list",
112 "custom_mark",
113 "custom_GC",
114 "custom_destroy",
115 "report",
116 "data_is_PMC_array",
117 "need_finalize",
118 "high_priority_gc",
119 "needs_early_gc",
120 "is_class",
121 "is_object"
126 =item C<static void pobj_flag_dump(PARROT_INTERP, long flags)>
128 Given a word of flags, generate a dump line of the whole word in hex,
129 followed by individual bits.
131 =cut
135 static void
136 pobj_flag_dump(PARROT_INTERP, long flags)
138 ASSERT_ARGS(pobj_flag_dump)
139 INTVAL idx = 0;
140 int printed_flag_p = 0;
142 Parrot_io_printf(interp, "\tFLAGS => 0x%04lx (", flags);
143 while (flags) {
144 if (flags & 1) {
145 if (printed_flag_p)
146 Parrot_io_printf(interp, ",");
147 Parrot_io_printf(interp, "%s", flag_bit_names[idx]);
148 printed_flag_p++;
150 idx++;
151 flags >>= 1;
153 Parrot_io_printf(interp, ")\n");
156 static void
157 PackFile_Constant_dump(PARROT_INTERP, ARGIN(const PackFile_ConstTable *ct),
158 ARGIN(const PackFile_Constant *self))
160 ASSERT_ARGS(PackFile_Constant_dump)
161 PMC *key;
162 size_t i;
164 switch (self->type) {
166 case PFC_NUMBER:
167 Parrot_io_printf(interp, " [ 'PFC_NUMBER', %g ],\n", self->u.number);
168 break;
170 case PFC_STRING:
171 Parrot_io_printf(interp, " [ 'PFC_STRING', {\n");
172 pobj_flag_dump(interp, (long)PObj_get_FLAGS(self->u.string));
173 Parrot_io_printf(interp, " CHARSET => %ld,\n",
174 self->u.string->charset);
175 i = self->u.string->bufused;
176 Parrot_io_printf(interp, " SIZE => %ld,\n",
177 (long)i);
179 Parrot_io_printf(interp, " DATA => \"%Ss\"\n",
180 Parrot_str_escape(interp, self->u.string));
181 Parrot_io_printf(interp, " } ],\n");
182 break;
184 case PFC_KEY:
185 for (i = 0, key = self->u.key; key; i++) {
186 GETATTR_Key_next_key(interp, key, key);
188 /* number of key components */
189 Parrot_io_printf(interp, " [ 'PFC_KEY' (%ld items)\n", i);
190 /* and now type / value per component */
191 for (key = self->u.key; key;) {
192 opcode_t type = PObj_get_FLAGS(key);
194 Parrot_io_printf(interp, " {\n");
196 type &= KEY_type_FLAGS;
197 pobj_flag_dump(interp, (long)PObj_get_FLAGS(key));
198 switch (type) {
199 case KEY_integer_FLAG:
200 Parrot_io_printf(interp, " TYPE => INTEGER\n");
201 Parrot_io_printf(interp, " DATA => %ld\n",
202 VTABLE_get_integer(interp, key));
203 Parrot_io_printf(interp, " },\n");
204 break;
205 case KEY_number_FLAG:
207 const PackFile_Constant *detail;
208 size_t ct_index;
210 Parrot_io_printf(interp, " TYPE => NUMBER\n");
211 ct_index = PackFile_find_in_const(interp, ct, key, PFC_NUMBER);
212 Parrot_io_printf(interp, " PFC_OFFSET => %ld\n", ct_index);
213 detail = ct->constants[ct_index];
214 Parrot_io_printf(interp, " DATA => %ld\n", detail->u.number);
215 Parrot_io_printf(interp, " },\n");
217 break;
218 case KEY_string_FLAG:
220 const PackFile_Constant *detail;
221 size_t ct_index;
223 Parrot_io_printf(interp, " TYPE => STRING\n");
224 ct_index = PackFile_find_in_const(interp, ct, key, PFC_STRING);
225 Parrot_io_printf(interp, " PFC_OFFSET => %ld\n", ct_index);
226 detail = ct->constants[ct_index];
227 Parrot_io_printf(interp, " DATA => '%Ss'\n",
228 detail->u.string);
229 Parrot_io_printf(interp, " },\n");
231 break;
232 case KEY_integer_FLAG | KEY_register_FLAG:
233 Parrot_io_printf(interp, " TYPE => I REGISTER\n");
234 Parrot_io_printf(interp, " DATA => %ld\n",
235 VTABLE_get_integer(interp, key));
236 Parrot_io_printf(interp, " },\n");
237 break;
238 case KEY_number_FLAG | KEY_register_FLAG:
239 Parrot_io_printf(interp, " TYPE => N REGISTER\n");
240 Parrot_io_printf(interp, " DATA => %ld\n",
241 VTABLE_get_integer(interp, key));
242 Parrot_io_printf(interp, " },\n");
243 break;
244 case KEY_string_FLAG | KEY_register_FLAG:
245 Parrot_io_printf(interp, " TYPE => S REGISTER\n");
246 Parrot_io_printf(interp, " DATA => %ld\n",
247 VTABLE_get_integer(interp, key));
248 Parrot_io_printf(interp, " },\n");
249 break;
250 case KEY_pmc_FLAG | KEY_register_FLAG:
251 Parrot_io_printf(interp, " TYPE => P REGISTER\n");
252 Parrot_io_printf(interp, " DATA => %ld\n",
253 VTABLE_get_integer(interp, key));
254 Parrot_io_printf(interp, " },\n");
255 break;
256 default:
257 Parrot_io_eprintf(NULL, "PackFile_Constant_pack: "
258 "unsupported constant type\n");
259 Parrot_exit(interp, 1);
261 GETATTR_Key_next_key(interp, key, key);
263 Parrot_io_printf(interp, " ],\n");
264 break;
265 case PFC_PMC:
266 Parrot_io_printf(interp, " [ 'PFC_PMC', {\n");
268 PMC * const pmc = self->u.key;
269 Parrot_Sub_attributes *sub;
270 STRING * const null = Parrot_str_new_constant(interp, "(null)");
271 STRING *namespace_description;
273 pobj_flag_dump(interp, (long)PObj_get_FLAGS(pmc));
274 switch (pmc->vtable->base_type) {
275 case enum_class_FixedBooleanArray:
276 case enum_class_FixedFloatArray:
277 case enum_class_FixedPMCArray:
278 case enum_class_FixedStringArray:
279 case enum_class_ResizableBooleanArray:
280 case enum_class_ResizableIntegerArray:
281 case enum_class_ResizableFloatArray:
282 case enum_class_ResizablePMCArray:
283 case enum_class_ResizableStringArray:
285 const int n = VTABLE_get_integer(interp, pmc);
286 STRING* const out_buffer = VTABLE_get_repr(interp, pmc);
287 Parrot_io_printf(interp,
288 "\tclass => %Ss,\n"
289 "\telement count => %d,\n"
290 "\telements => %Ss,\n",
291 pmc->vtable->whoami,
293 out_buffer);
295 break;
296 case enum_class_Sub:
297 case enum_class_Coroutine:
298 PMC_get_sub(interp, pmc, sub);
299 if (sub->namespace_name) {
300 switch (sub->namespace_name->vtable->base_type) {
301 case enum_class_String:
302 namespace_description = Parrot_str_new(interp, "'", 1);
303 namespace_description = Parrot_str_append(interp,
304 namespace_description,
305 VTABLE_get_string(interp, sub->namespace_name));
306 namespace_description = Parrot_str_append(interp,
307 namespace_description,
308 Parrot_str_new(interp, "'", 1));
309 break;
310 case enum_class_Key:
311 namespace_description =
312 key_set_to_string(interp, sub->namespace_name);
313 break;
314 default:
315 namespace_description = sub->namespace_name->vtable->whoami;
318 else {
319 namespace_description = null;
321 Parrot_io_printf(interp,
322 "\tclass => %Ss,\n"
323 "\tstart_offs => %d,\n"
324 "\tend_offs => %d,\n"
325 "\tname => '%Ss',\n"
326 "\tsubid => '%Ss',\n"
327 "\tmethod => '%Ss',\n"
328 "\tnsentry => '%Ss',\n"
329 "\tnamespace => %Ss\n"
330 "\tHLL_id => %d,\n",
331 pmc->vtable->whoami,
332 sub->start_offs,
333 sub->end_offs,
334 sub->name,
335 sub->subid,
336 sub->method_name,
337 sub->ns_entry_name,
338 namespace_description,
339 sub->HLL_id);
340 break;
341 case enum_class_FixedIntegerArray:
342 Parrot_io_printf(interp,
343 "\tclass => %Ss,\n"
344 "\trepr => '%Ss'\n",
345 pmc->vtable->whoami,
346 VTABLE_get_repr(interp, pmc));
347 break;
348 default:
349 Parrot_io_printf(interp, "\tno dump info for PMC %ld %Ss\n",
350 pmc->vtable->base_type, pmc->vtable->whoami);
351 Parrot_io_printf(interp, "\tclass => %Ss,\n", pmc->vtable->whoami);
354 Parrot_io_printf(interp, " } ],\n");
355 break;
356 default:
357 Parrot_io_printf(interp, " [ 'PFC_\?\?\?', type '0x%x' ],\n",
358 self->type);
359 break;
365 =item C<void PackFile_Fixup_dump(PARROT_INTERP, const PackFile_FixupTable *ft)>
367 Dumps the fix-up table C<ft>.
369 =cut
373 PARROT_EXPORT
374 void
375 PackFile_Fixup_dump(PARROT_INTERP, ARGIN(const PackFile_FixupTable *ft))
377 ASSERT_ARGS(PackFile_Fixup_dump)
378 opcode_t i;
380 for (i = 0; i < ft->fixup_count; i++) {
381 Parrot_io_printf(interp, "\t#%d\n", (int) i);
382 switch (ft->fixups[i]->type) {
383 case enum_fixup_label:
384 case enum_fixup_sub:
385 Parrot_io_printf(interp,
386 "\ttype => %d offs => %8d name => '%s',\n",
387 (int)ft->fixups[i]->type,
388 (int)ft->fixups[i]->offset,
389 ft->fixups[i]->name);
390 break;
391 default:
392 Parrot_io_printf(interp, "\ttype => %d ???,\n",
393 (int) ft->fixups[i]->type);
394 break;
401 =back
403 =head1 SEE ALSO
405 F<src/pbc_dump.c>.
407 =cut
413 * Local variables:
414 * c-file-style: "parrot"
415 * End:
416 * vim: expandtab shiftwidth=4: