Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/tools/winedump/output.c
4389 views
1
/*
2
* Code generation functions
3
*
4
* Copyright 2000-2002 Jon Griffiths
5
*
6
* This library is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU Lesser General Public
8
* License as published by the Free Software Foundation; either
9
* version 2.1 of the License, or (at your option) any later version.
10
*
11
* This library is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* Lesser General Public License for more details.
15
*
16
* You should have received a copy of the GNU Lesser General Public
17
* License along with this library; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19
*/
20
21
#include "config.h"
22
23
#include "winedump.h"
24
25
/* Output files */
26
static FILE *specfile = NULL;
27
static FILE *hfile = NULL;
28
static FILE *cfile = NULL;
29
30
static void output_spec_postamble (void);
31
static void output_header_postamble (void);
32
static void output_c_postamble (void);
33
static void output_c_banner (const parsed_symbol *sym);
34
static const char *get_format_str (int type);
35
static const char *get_in_or_out (const parsed_symbol *sym, size_t arg);
36
37
38
/*******************************************************************
39
* output_spec_preamble
40
*
41
* Write the first part of the .spec file
42
*/
43
void output_spec_preamble (void)
44
{
45
specfile = open_file (OUTPUT_DLL_NAME, ".spec", "w");
46
47
atexit (output_spec_postamble);
48
49
if (VERBOSE)
50
puts ("Creating .spec preamble");
51
52
fprintf (specfile,
53
"# Generated from %s by winedump\n\n", globals.input_name);
54
}
55
56
57
/*******************************************************************
58
* output_spec_symbol
59
*
60
* Write a symbol to the .spec file
61
*/
62
void output_spec_symbol (const parsed_symbol *sym)
63
{
64
char ord_spec[20];
65
66
assert (specfile);
67
assert (sym && sym->symbol);
68
69
if (sym->ordinal >= 0)
70
sprintf(ord_spec, "%d", sym->ordinal);
71
else
72
{
73
ord_spec[0] = '@';
74
ord_spec[1] = '\0';
75
}
76
if (sym->flags & SYM_THISCALL)
77
strcat (ord_spec, " -i386"); /* For binary compatibility only */
78
79
if (!globals.do_code || !sym->function_name)
80
{
81
if (sym->flags & SYM_DATA)
82
{
83
if (globals.forward_dll)
84
fprintf (specfile, "%s forward %s %s.%s #", ord_spec, sym->symbol,
85
globals.forward_dll, sym->symbol);
86
87
fprintf (specfile, "%s extern %s %s\n", ord_spec, sym->symbol,
88
sym->arg_name[0]);
89
return;
90
}
91
92
if (globals.forward_dll)
93
fprintf (specfile, "%s forward %s %s.%s\n", ord_spec, sym->symbol,
94
globals.forward_dll, sym->symbol);
95
else
96
fprintf (specfile, "%s stub %s\n", ord_spec, sym->symbol);
97
}
98
else
99
{
100
unsigned int i = sym->flags & SYM_THISCALL ? 1 : 0;
101
102
fprintf (specfile, "%s %s %s(", ord_spec, sym->varargs ? "varargs" :
103
symbol_get_call_convention(sym), sym->symbol);
104
105
for (; i < sym->argc; i++)
106
fprintf (specfile, " %s", symbol_get_spec_type(sym, i));
107
108
if (sym->argc)
109
fputc (' ', specfile);
110
fputs (") ", specfile);
111
112
if (sym->flags & SYM_THISCALL)
113
fputs ("__thiscall_", specfile);
114
fprintf (specfile, "%s_%s", OUTPUT_UC_DLL_NAME, sym->function_name);
115
116
fputc ('\n',specfile);
117
}
118
}
119
120
121
/*******************************************************************
122
* output_spec_postamble
123
*
124
* Write the last part of the .spec file
125
*/
126
static void output_spec_postamble (void)
127
{
128
if (specfile)
129
fclose (specfile);
130
specfile = NULL;
131
}
132
133
134
/*******************************************************************
135
* output_header_preamble
136
*
137
* Write the first part of the .h file
138
*/
139
void output_header_preamble (void)
140
{
141
if (!globals.do_code)
142
return;
143
144
hfile = open_file (OUTPUT_DLL_NAME, "_dll.h", "w");
145
146
atexit (output_header_postamble);
147
148
fprintf (hfile,
149
"/*\n * %s.dll\n *\n * Generated from %s.dll by winedump.\n *\n"
150
" * DO NOT SEND GENERATED DLLS FOR INCLUSION INTO WINE !\n *\n */"
151
"\n#ifndef __WINE_%s_DLL_H\n#define __WINE_%s_DLL_H\n\n"
152
"#include \"windef.h\"\n#include \"wine/debug.h\"\n"
153
"#include \"winbase.h\"\n#include \"winnt.h\"\n\n\n",
154
OUTPUT_DLL_NAME, OUTPUT_DLL_NAME, OUTPUT_UC_DLL_NAME,
155
OUTPUT_UC_DLL_NAME);
156
}
157
158
159
/*******************************************************************
160
* output_header_symbol
161
*
162
* Write a symbol to the .h file
163
*/
164
void output_header_symbol (const parsed_symbol *sym)
165
{
166
if (!globals.do_code)
167
return;
168
169
assert (hfile);
170
assert (sym && sym->symbol);
171
172
if (!globals.do_code)
173
return;
174
175
if (sym->flags & SYM_DATA)
176
return;
177
178
if (!sym->function_name)
179
fprintf (hfile, "/* __%s %s_%s(); */\n", symbol_get_call_convention(sym),
180
OUTPUT_UC_DLL_NAME, sym->symbol);
181
else
182
{
183
output_prototype (hfile, sym);
184
fputs (";\n", hfile);
185
}
186
}
187
188
189
/*******************************************************************
190
* output_header_postamble
191
*
192
* Write the last part of the .h file
193
*/
194
static void output_header_postamble (void)
195
{
196
if (hfile)
197
{
198
fprintf (hfile, "\n\n\n#endif\t/* __WINE_%s_DLL_H */\n",
199
OUTPUT_UC_DLL_NAME);
200
fclose (hfile);
201
hfile = NULL;
202
}
203
}
204
205
206
/*******************************************************************
207
* output_c_preamble
208
*
209
* Write the first part of the .c file
210
*/
211
void output_c_preamble (void)
212
{
213
cfile = open_file (OUTPUT_DLL_NAME, "_main.c", "w");
214
215
atexit (output_c_postamble);
216
217
fprintf (cfile,
218
"/*\n * %s.dll\n *\n * Generated from %s by winedump.\n *\n"
219
" * DO NOT SUBMIT GENERATED DLLS FOR INCLUSION INTO WINE!\n *\n */"
220
"\n\n#include \"config.h\"\n\n#include <stdarg.h>\n\n"
221
"#include \"windef.h\"\n#include \"winbase.h\"\n",
222
OUTPUT_DLL_NAME, globals.input_name);
223
224
if (globals.do_code)
225
fprintf (cfile, "#include \"%s_dll.h\"\n", OUTPUT_DLL_NAME);
226
227
fprintf (cfile,"#include \"wine/debug.h\"\n\nWINE_DEFAULT_DEBUG_CHANNEL(%s);\n\n",
228
OUTPUT_DLL_NAME);
229
230
if (globals.forward_dll)
231
{
232
if (VERBOSE)
233
puts ("Creating a forwarding DLL");
234
235
fputs ("\nHMODULE hDLL=0; /* DLL to call */\n\n", cfile);
236
237
fprintf (cfile,
238
"BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, void *reserved)\n{\n"
239
" TRACE(\"(%%p, %%u, %%p)\\n\", instance, reason, reserved);\n\n"
240
" switch (reason)\n"
241
" {\n"
242
" case DLL_PROCESS_ATTACH:\n"
243
" hDLL = LoadLibraryA(\"%s\");\n"
244
" TRACE(\"Forwarding DLL (%s) loaded (%%p)\\n\", hDLL);\n"
245
" DisableThreadLibraryCalls(instance);\n"
246
" break;\n"
247
" case DLL_PROCESS_DETACH:\n"
248
" FreeLibrary(hDLL);\n"
249
" TRACE(\"Forwarding DLL (%s) freed\\n\");\n"
250
" break;\n"
251
" }\n\n"
252
" return TRUE;\n}\n",
253
globals.forward_dll, globals.forward_dll, globals.forward_dll);
254
}
255
}
256
257
258
/*******************************************************************
259
* output_c_symbol
260
*
261
* Write a symbol to the .c file
262
*/
263
void output_c_symbol (const parsed_symbol *sym)
264
{
265
unsigned int i, start = sym->flags & SYM_THISCALL ? 1 : 0;
266
BOOL is_void;
267
static BOOL has_thiscall = FALSE;
268
269
assert (cfile);
270
assert (sym && sym->symbol);
271
272
if (!globals.do_code)
273
return;
274
275
if (sym->flags & SYM_DATA)
276
{
277
fprintf (cfile, "/* FIXME: Move to top of file */\n%s;\n\n",
278
sym->arg_text[0]);
279
return;
280
}
281
282
if (sym->flags & SYM_THISCALL && !has_thiscall)
283
{
284
fputs ("#ifdef __i386__ /* thiscall functions are i386-specific */\n\n"
285
"#define THISCALL(func) __thiscall_ ## func\n"
286
"#define THISCALL_NAME(func) __ASM_NAME(\"__thiscall_\" #func)\n"
287
"#define DEFINE_THISCALL_WRAPPER(func) \\\n"
288
"\textern void THISCALL(func)(); \\\n"
289
"\t__ASM_GLOBAL_FUNC(__thiscall_ ## func, \\\n"
290
"\t\t\t\"popl %eax\\n\\t\" \\\n"
291
"\t\t\t\"pushl %ecx\\n\\t\" \\\n"
292
"\t\t\t\"pushl %eax\\n\\t\" \\\n"
293
"\t\t\t\"jmp \" __ASM_NAME(#func) )\n"
294
"#else /* __i386__ */\n\n"
295
"#define THISCALL(func) func\n"
296
"#define THISCALL_NAME(func) __ASM_NAME(#func)\n"
297
"#define DEFINE_THISCALL_WRAPPER(func) /* nothing */\n\n"
298
"#endif /* __i386__ */\n\n", cfile);
299
has_thiscall = TRUE;
300
}
301
302
output_c_banner(sym);
303
304
if (sym->flags & SYM_THISCALL)
305
fprintf(cfile, "DEFINE_THISCALL_WRAPPER(%s)\n", sym->function_name);
306
307
if (!sym->function_name)
308
{
309
/* #ifdef'd dummy */
310
fprintf (cfile, "#if 0\n__%s %s_%s()\n{\n\t/* %s in .spec */\n}\n#endif\n",
311
symbol_get_call_convention(sym), OUTPUT_UC_DLL_NAME, sym->symbol,
312
globals.forward_dll ? "@forward" : "@stub");
313
return;
314
}
315
316
is_void = !strcasecmp (sym->return_text, "void");
317
318
output_prototype (cfile, sym);
319
fputs ("\n{\n", cfile);
320
321
if (!globals.do_trace)
322
{
323
fputs ("\tFIXME(\":stub\\n\");\n", cfile);
324
if (!is_void)
325
fprintf (cfile, "\treturn (%s) 0;\n", sym->return_text);
326
fputs ("}\n", cfile);
327
return;
328
}
329
330
/* Tracing, maybe forwarding as well */
331
if (globals.forward_dll)
332
{
333
/* Write variables for calling */
334
if (sym->varargs)
335
fputs("\tva_list valist;\n", cfile);
336
337
fprintf (cfile, "\t%s (__%s *pFunc)(", sym->return_text,
338
symbol_get_call_convention(sym));
339
340
for (i = start; i < sym->argc; i++)
341
fprintf (cfile, "%s%s", i > start ? ", " : "", sym->arg_text [i]);
342
343
fprintf (cfile, "%s);\n", sym->varargs ? ",..." : sym->argc == 1 &&
344
sym->flags & SYM_THISCALL ? "" : sym->argc ? "" : "void");
345
346
if (!is_void)
347
fprintf (cfile, "\t%s retVal;\n", sym->return_text);
348
349
fprintf (cfile, "\tpFunc=(void*)GetProcAddress(hDLL,\"%s\");\n",
350
sym->symbol);
351
}
352
353
/* TRACE input arguments */
354
fprintf (cfile, "\tTRACE(\"(%s", !sym->argc ? "void" : "");
355
356
for (i = 0; i < sym->argc; i++)
357
fprintf (cfile, "%s(%s)%s", i ? "," : "", sym->arg_text [i],
358
get_format_str (sym->arg_type [i]));
359
360
fprintf (cfile, "%s): %s\\n\"", sym->varargs ? ",..." : "",
361
globals.forward_dll ? "forward" : "stub");
362
363
for (i = 0; i < sym->argc; i++)
364
if (sym->arg_type[i] != ARG_STRUCT)
365
fprintf(cfile, ",%s%s%s%s", sym->arg_type[i] == ARG_LONG ? "(LONG)" : "",
366
sym->arg_type[i] == ARG_WIDE_STRING ? "debugstr_w(" : "",
367
sym->arg_name[i],
368
sym->arg_type[i] == ARG_WIDE_STRING ? ")" : "");
369
370
fputs (");\n", cfile);
371
372
if (!globals.forward_dll)
373
{
374
if (!is_void)
375
fprintf (cfile, "\treturn (%s) 0;\n", sym->return_text);
376
fputs ("}\n", cfile);
377
return;
378
}
379
380
/* Call the DLL */
381
if (sym->varargs)
382
fprintf (cfile, "\tva_start(valist,%s);\n", sym->arg_name[sym->argc-1]);
383
384
fprintf (cfile, "\t%spFunc(", !is_void ? "retVal = " : "");
385
386
for (i = 0; i < sym->argc; i++)
387
fprintf (cfile, "%s%s", i ? "," : "", sym->arg_name [i]);
388
389
fputs (sym->varargs ? ",valist);\n\tva_end(valist);" : ");", cfile);
390
391
/* TRACE return value */
392
fprintf (cfile, "\n\tTRACE(\"Returned (%s)\\n\"",
393
get_format_str (sym->return_type));
394
395
if (!is_void)
396
{
397
if (sym->return_type == ARG_WIDE_STRING)
398
fputs (",debugstr_w(retVal)", cfile);
399
else
400
fprintf (cfile, ",%s%s", sym->return_type == ARG_LONG ? "(LONG)" : "",
401
sym->return_type == ARG_STRUCT ? "" : "retVal");
402
fputs (");\n\treturn retVal;\n", cfile);
403
}
404
else
405
fputs (");\n", cfile);
406
407
fputs ("}\n", cfile);
408
}
409
410
411
/*******************************************************************
412
* output_c_postamble
413
*
414
* Write the last part of the .c file
415
*/
416
static void output_c_postamble (void)
417
{
418
if (cfile)
419
fclose (cfile);
420
cfile = NULL;
421
}
422
423
424
/*******************************************************************
425
* output_makefile
426
*
427
* Write a Wine compatible makefile.in
428
*/
429
void output_makefile (void)
430
{
431
FILE *makefile = open_file ("Makefile", ".in", "w");
432
433
if (VERBOSE)
434
puts ("Creating makefile");
435
436
fprintf (makefile,
437
"# Generated from %s by winedump.\n"
438
"MODULE = %s.dll\n", globals.input_name, OUTPUT_DLL_NAME);
439
440
if (globals.forward_dll)
441
fprintf (makefile, "IMPORTS = %s", globals.forward_dll);
442
443
fprintf (makefile, "\n\nC_SRCS = \\\n\t%s_main.c\n", OUTPUT_DLL_NAME);
444
445
if (globals.forward_dll)
446
fprintf (specfile,"#import %s.dll\n", globals.forward_dll);
447
448
fclose (makefile);
449
}
450
451
452
/*******************************************************************
453
* output_prototype
454
*
455
* Write a C prototype for a parsed symbol
456
*/
457
void output_prototype (FILE *file, const parsed_symbol *sym)
458
{
459
unsigned int i, start = sym->flags & SYM_THISCALL ? 1 : 0;
460
461
fprintf (file, "%s __%s %s_%s(", sym->return_text, symbol_get_call_convention(sym),
462
OUTPUT_UC_DLL_NAME, sym->function_name);
463
464
if (!sym->argc || (sym->argc == 1 && sym->flags & SYM_THISCALL))
465
fputs ("void", file);
466
else
467
for (i = start; i < sym->argc; i++)
468
fprintf (file, "%s%s %s", i > start ? ", " : "", sym->arg_text [i],
469
sym->arg_name [i]);
470
if (sym->varargs)
471
fputs (", ...", file);
472
fputc (')', file);
473
}
474
475
476
/*******************************************************************
477
* output_c_banner
478
*
479
* Write a function banner to the .c file
480
*/
481
void output_c_banner (const parsed_symbol *sym)
482
{
483
char ord_spec[16];
484
size_t i;
485
486
if (sym->ordinal >= 0)
487
sprintf(ord_spec, "%d", sym->ordinal);
488
else
489
{
490
ord_spec[0] = '@';
491
ord_spec[1] = '\0';
492
}
493
494
fprintf (cfile, "/*********************************************************"
495
"*********\n *\t\t%s (%s.%s)\n *\n", sym->symbol,
496
OUTPUT_UC_DLL_NAME, ord_spec);
497
498
if (globals.do_documentation && sym->function_name)
499
{
500
fputs (" *\n * PARAMS\n *\n", cfile);
501
502
if (!sym->argc)
503
fputs (" * None.\n *\n", cfile);
504
else
505
{
506
for (i = 0; i < sym->argc; i++)
507
fprintf (cfile, " * %s [%s]%s\n", sym->arg_name [i],
508
get_in_or_out(sym, i),
509
strcmp (sym->arg_name [i], "_this") ? "" :
510
" Pointer to the class object (in ECX)");
511
512
if (sym->varargs)
513
fputs (" * ...[I]\n", cfile);
514
fputs (" *\n", cfile);
515
}
516
517
fputs (" * RETURNS\n *\n", cfile);
518
519
if (sym->return_text && !strcmp (sym->return_text, "void"))
520
fputs (" * Nothing.\n", cfile);
521
else
522
fprintf (cfile, " * %s\n", sym->return_text);
523
}
524
fputs (" *\n */\n", cfile);
525
}
526
527
528
/*******************************************************************
529
* get_format_str
530
*
531
* Get a string containing the correct format string for a type
532
*/
533
static const char *get_format_str (int type)
534
{
535
switch (type)
536
{
537
case ARG_VOID: return "void";
538
case ARG_FLOAT: return "%f";
539
case ARG_DOUBLE: return "%g";
540
case ARG_POINTER: return "%p";
541
case ARG_WIDE_STRING:
542
case ARG_STRING: return "%s";
543
case ARG_LONG: return "%ld";
544
case ARG_STRUCT: return "struct";
545
}
546
assert (0);
547
return "";
548
}
549
550
551
/*******************************************************************
552
* get_in_or_out
553
*
554
* Determine if a parameter is In or In/Out
555
*/
556
static const char *get_in_or_out (const parsed_symbol *sym, size_t arg)
557
{
558
assert (sym && arg < sym->argc);
559
assert (globals.do_documentation);
560
561
if (sym->arg_flag [arg] & CT_CONST)
562
return "In";
563
564
switch (sym->arg_type [arg])
565
{
566
case ARG_FLOAT:
567
case ARG_DOUBLE:
568
case ARG_LONG:
569
case ARG_STRUCT: return "In";
570
case ARG_POINTER:
571
case ARG_WIDE_STRING:
572
case ARG_STRING: return "In/Out";
573
}
574
assert (0);
575
return "";
576
}
577
578