/
jit_debug_xcoff.c
381 lines (314 loc) · 10.3 KB
/
jit_debug_xcoff.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
/*
Copyright (C) 2001-2008, Parrot Foundation.
$Id$
=head1 NAME
src/jit_debug_xcoff.c - XCOFF stabs for JIT
=head1 DESCRIPTION
Write an XCOFF stabs file for JIT code. This file is based on
F<src/jit_debug.c>.
Stabs is a file format for information that describes a program to a
debugger.
For more information see the stabs documentation at
http://sources.redhat.com/gdb/current/onlinedocs/stabs_toc.html.
=head2 Functions
=over 4
=cut
*/
/* HEADERIZER HFILE: none */
/* HEADERIZER STOP */
#include <parrot/parrot.h>
#include "parrot/exec.h"
#include "jit.h"
#ifdef __IBMC__
/* following from /usr/include/dbxstclass.h */
# define C_GSYM "0x80" /* global variable */
# define C_LSYM "0x81" /* stack variable */
# define C_PSYM "0x82" /* parameter */
# define C_RSYM "0x83" /* register variable */
# define C_RPSYM "0x84"
# define C_STSYM "0x85" /* variable in data section */
# define C_TCSYM "0x86"
# define C_BCOMM "0x87"
# define C_ECOML "0x88"
# define C_ECOMM "0x89"
# define C_DECL "0x8c" /* type declaration */
# define C_ENTRY "0x8d"
# define C_FUN "0x8e"
# define C_BSTAT "0x8f"
# define C_ESTAT "0x90"
void Parrot_jit_debug(PARROT_INTERP);
# define BIT_SIZE(t) ((int)(sizeof (t)*8))
# define BYTE_SIZE(t) ((int)sizeof (t))
# define BIT_OFFSET(str, field) ((int)(offsetof((str), (field)) * 8))
typedef struct BaseTypes {
const char *name;
const char *spec;
} BaseTypes;
/*
=item C<static void write_types(FILE *stabs, PARROT_INTERP)>
Writes the types to C<stabs>.
=cut
*/
static void
write_types(FILE *stabs, PARROT_INTERP)
{
int i, j;
/* borrowed from mono */
static BaseTypes base_types[] = {
{"Char", "-6"},
{"Byte", "-5"},
{"Int16", "-3"},
{"UInt16", "-7"},
{"Int32", "-1"}, /* 5 */
{"UInt32", "-8"},
{"Int64", ";01000000000000000000000;0777777777777777777777;"},
{"UInt64", ";0000000000000;01777777777777777777777;"},
{"Single", "-12"},
{"Double", "-13"}, /* 10 */
{"LongDouble", "-14"},
# if INTVAL_SIZE == 4
{"INTVAL", "5;"}, /* 12 */
# else
{"INTVAL", "7;"},
# endif
# if NUMVAL_SIZE == 8
{"FLOATVAL", "10;"}, /* 13 */
# else
{"FLOATVAL", "11;"},
# endif
{"Ptr", "*0;"},
{"CharPtr", "*1;"}, /* 15 */
{0, 0}
};
for (i = 0; base_types[i].name; ++i) {
if (! base_types[i].spec)
continue;
fprintf(stabs, ".stabx \"%s:t%d=", base_types[i].name, i);
if (base_types[i].spec [0] == ';') {
fprintf(stabs, "r%d%s\"", i, base_types[i].spec);
}
else {
fprintf(stabs, "%s\"", base_types[i].spec);
}
fprintf(stabs, ",0," C_DECL ",0\n");
}
fprintf(stabs, ".stabx \"STRING:t%d=*%d\""
",0," C_DECL ",0\n", i, i+1);
++i;
fprintf(stabs, ".stabx \"Parrot_String:T%d=s%d"
"bufstart:14,%d,%d;"
"buflen:6,%d,%d;" /* XXX type */
"flags:12,%d,%d;"
"bufused:12,%d,%d;"
"strstart:15,%d,%d;" /* fake a char* */
";\""
",0," C_DECL ",0\n", i++, BYTE_SIZE(STRING),
BIT_OFFSET(STRING, cache._b._bufstart), BIT_SIZE(void*),
BIT_OFFSET(STRING, cache._b._buflen), BIT_SIZE(size_t),
BIT_OFFSET(STRING, flags), BIT_SIZE(UINTVAL),
BIT_OFFSET(STRING, bufused), BIT_SIZE(UINTVAL),
BIT_OFFSET(STRING, strstart), BIT_SIZE(void*));
fprintf(stabs, ".stabx \"PMCType:T%d=e", i++);
for (j = 0; j < interp->n_vtable_max; ++j) {
if (interp->vtables[j] && interp->vtables[j]->whoami) {
STRING* name = interp->vtables[j]->whoami;
fwrite(name->strstart, name->strlen, 1, stabs);
fprintf(stabs, ":%d,", j);
}
}
fprintf(stabs, ";\",0," C_DECL ",0\n");
/* PMC type */
fprintf(stabs, ".stabx \"PMC:T%d=s%d", i, BYTE_SIZE(PMC));
fprintf(stabs, "cache:%d,%d,%d;",
i + 1, BIT_OFFSET(PMC, cache), BIT_SIZE(UnionVal));
fprintf(stabs, "flags:%d,%d,%d;",
i + 1, BIT_OFFSET(PMC, flags), BIT_SIZE(Parrot_UInt));
fprintf(stabs, "vtable:*%d,%d,%d;",
i + 3, BIT_OFFSET(PMC, vtable), BIT_SIZE(void*));
fprintf(stabs, "data:14,%d,%d;",
BIT_OFFSET(PMC, data), BIT_SIZE(void*));
fprintf(stabs, "pmc_ext:*%d,%d,%d;",
i, BIT_OFFSET(PMC, pmc_ext), BIT_SIZE(void*));
fprintf(stabs, ";\"");
fprintf(stabs, ",0," C_DECL ",0\n");
fprintf(stabs, ".stabx \"cache:%d,%d,%d;"
"flags:12,%d,%d;"
";\""
",0," C_DECL ",0\n",
i + 2, BIT_SIZE(UnionVal), BIT_SIZE(Parrot_UInt));
fprintf(stabs, ".stabx \"UnionVal:T%d=u%d"
"int_val:12,%d,%d;"
"pmc_val:*%d,%d,%d;"
";\""
",0," C_DECL ",0\n", i + 2, BYTE_SIZE(UnionVal),
BIT_OFFSET(UnionVal, int_val), BIT_SIZE(INTVAL),
i, BIT_OFFSET(UnionVal, pmc_val), BIT_SIZE(void*));
fprintf(stabs, ".stabx \"VTABLE:T%d=s%d"
"base_type:%d,%d,%d;"
";\""
",0," C_DECL ",0\n", i + 3, BYTE_SIZE(UnionVal),
i - 1, BIT_OFFSET(VTABLE, base_type), BIT_SIZE(INTVAL));
i += 4;
}
/*
=item C<static void
write_vars(FILE *stabs, PARROT_INTERP)>
Writes the contents of the registers to C<stabs>.
=cut
*/
static void
write_vars(FILE *stabs, PARROT_INTERP)
{
int i;
/* fake static var stabs */
fprintf(stabs, ".bs parrot_jit_vars\n");
for (i = 0; i < NUM_REGISTERS; i++) {
fprintf(stabs, ".stabx \"I%d:V12\",0x%p," C_STSYM ",0\n", i,
(char*)®_INT(interp, i));
fprintf(stabs, ".stabx \"N%d:V13\",0x%p," C_STSYM ",0\n", i,
(char*)®_NUM(interp, i));
fprintf(stabs, ".stabx \"S%d:V16\",0x%p," C_STSYM ",0\n", i,
(char*)®_STR(interp, i));
fprintf(stabs, ".stabx \"P%d:V19\",0x%p," C_STSYM ",0\n", i,
(char*)®_PMC(interp, i));
}
fprintf(stabs, ".es\n");
}
/*
=item C<static STRING *
debug_file(PARROT_INTERP, STRING *file, const char *ext)>
Returns C<file> with C<ext> appended.
=cut
*/
static STRING *
debug_file(PARROT_INTERP, STRING *file, const char *ext)
{
STRING *ret;
ret = Parrot_str_copy(interp, file);
ret = Parrot_str_append(interp, ret,
string_make(interp, ext, strlen(ext), NULL,
PObj_external_FLAG));
return ret;
}
/*
=item C<static void
Parrot_jit_debug_stabs(PARROT_INTERP)>
Writes the JIT debugging stabs.
=cut
*/
static void
Parrot_jit_debug_stabs(PARROT_INTERP)
{
Parrot_jit_info_t *jit_info = interp->jit_info;
STRING *file = NULL;
STRING *pasmfile, *stabsfile, *ofile, *cmd;
FILE *stabs;
size_t i;
int line;
opcode_t lc;
if (interp->code->debugs) {
char *ext;
char * const src = Parrot_str_to_cstring(interp,
Parrot_debug_pc_to_filename(interp,
interp->code->debugs, 0));
pasmfile = string_make(interp, src, strlen(src), NULL,
PObj_external_FLAG);
file = Parrot_str_copy(interp, pasmfile);
/* chop pasm/pir */
ext = strrchr(src, '.');
if (ext && STREQ(ext, ".pasm"))
Parrot_str_chopn_inplace(interp, file, 4);
else if (ext && STREQ(ext, ".pir"))
Parrot_str_chopn_inplace(interp, file, 3);
else if (!ext) /* EVAL_n */
file = Parrot_str_append(interp, file,
string_make(interp, ".", 1, NULL, PObj_external_FLAG));
Parrot_str_free_cstring(src);
}
else {
/* chop pbc */
Parrot_str_chopn_inplace(interp, file, 3);
pasmfile = debug_file(interp, file, "pasm");
}
stabsfile = debug_file(interp, file, "stabs.s");
ofile = debug_file(interp, file, "o");
{
char * const temp = Parrot_str_to_cstring(interp, stabsfile);
stabs = fopen(temp, "w");
Parrot_str_free_cstring(temp);
}
if (stabs == NULL)
return;
{
char * const temp = Parrot_str_to_cstring(interp, pasmfile);
/* filename info */
fprintf(stabs, ".file \"%s\"\n", temp);
Parrot_str_free_cstring(temp);
}
/* declare function name */
fprintf(stabs, ".jit_func:\n");
/* jit_func start addr */
fprintf(stabs, ".stabx \"jit_func:F1\",0x%p," C_FUN ",0\n",
jit_info->arena.start);
fprintf(stabs, ".function .jit_func,.jit_func,0,0\n");
fprintf(stabs, ".bf 1\n");
write_types(stabs, interp);
write_vars(stabs, interp);
/* if we don't have line numbers, emit dummys, assuming there are
* no comments and spaces in source for testing
*/
/* jit_begin */
fprintf(stabs, ".line 1\n");
line = 1;
lc = 0;
for (i = 0; i < interp->code->base.size; i++) {
if (jit_info->arena.op_map[i].ptr) {
op_info_t* op = &interp->op_info_table[
interp->code->base.data[i]];
if (interp->code->debugs) {
line = (int)interp->code->debugs->base.data[lc++];
}
fprintf(stabs, ".line %d # 0x%p %s\n", line,
(int)((char *)jit_info->arena.op_map[i].ptr -
(char *)jit_info->arena.start),
op->full_name);
line++;
}
}
/* eof */
fprintf(stabs, ".stabx \"\",0x%p," C_FUN ",0\n",
(char *) jit_info->arena.size);
fprintf(stabs, ".ef %d\n", line);
fclose(stabs);
/* run the stabs file through C<as> generating file.o */
cmd = Parrot_sprintf_c(interp, "as %Ss -o %Ss", stabsfile, ofile);
{
char * const temp = Parrot_str_to_cstring(interp, cmd);
system(temp);
Parrot_str_free_cstring(temp);
}
}
/*
=item C<void
Parrot_jit_debug(PARROT_INTERP)>
Writes the JIT debugging stabs. Just calls C<Parrot_jit_debug_stabs()>.
=cut
*/
void
Parrot_jit_debug(PARROT_INTERP)
{
Parrot_jit_debug_stabs(interp);
}
#endif
/*
=back
=head1 SEE ALSO
F<src/jit_debug.c>, F<src/jit.c>, F<src/jit.h>.
=cut
*/
/*
* Local variables:
* c-file-style: "parrot"
* End:
* vim: expandtab shiftwidth=4:
*/