2 * Code generation functions
4 * Copyright 2000-2002 Jon Griffiths
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.
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.
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
26 static FILE *specfile
= NULL
;
27 static FILE *hfile
= NULL
;
28 static FILE *cfile
= NULL
;
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
);
38 /*******************************************************************
39 * output_spec_preamble
41 * Write the first part of the .spec file
43 void output_spec_preamble (void)
45 specfile
= open_file (OUTPUT_DLL_NAME
, ".spec", "w");
47 atexit (output_spec_postamble
);
50 puts ("Creating .spec preamble");
53 "# Generated from %s by winedump\n\n", globals
.input_name
);
57 /*******************************************************************
60 * Write a symbol to the .spec file
62 void output_spec_symbol (const parsed_symbol
*sym
)
67 assert (sym
&& sym
->symbol
);
69 if (sym
->ordinal
>= 0)
70 sprintf(ord_spec
, "%d", sym
->ordinal
);
76 if (sym
->flags
& SYM_THISCALL
)
77 strcat (ord_spec
, " -i386"); /* For binary compatibility only */
79 if (!globals
.do_code
|| !sym
->function_name
)
81 if (sym
->flags
& SYM_DATA
)
83 if (globals
.forward_dll
)
84 fprintf (specfile
, "%s forward %s %s.%s #", ord_spec
, sym
->symbol
,
85 globals
.forward_dll
, sym
->symbol
);
87 fprintf (specfile
, "%s extern %s %s\n", ord_spec
, sym
->symbol
,
92 if (globals
.forward_dll
)
93 fprintf (specfile
, "%s forward %s %s.%s\n", ord_spec
, sym
->symbol
,
94 globals
.forward_dll
, sym
->symbol
);
96 fprintf (specfile
, "%s stub %s\n", ord_spec
, sym
->symbol
);
100 unsigned int i
= sym
->flags
& SYM_THISCALL
? 1 : 0;
102 fprintf (specfile
, "%s %s %s(", ord_spec
, sym
->varargs
? "varargs" :
103 symbol_get_call_convention(sym
), sym
->symbol
);
105 for (; i
< sym
->argc
; i
++)
106 fprintf (specfile
, " %s", symbol_get_spec_type(sym
, i
));
109 fputc (' ', specfile
);
110 fputs (") ", specfile
);
112 if (sym
->flags
& SYM_THISCALL
)
113 fputs ("__thiscall_", specfile
);
114 fprintf (specfile
, "%s_%s", OUTPUT_UC_DLL_NAME
, sym
->function_name
);
116 fputc ('\n',specfile
);
121 /*******************************************************************
122 * output_spec_postamble
124 * Write the last part of the .spec file
126 static void output_spec_postamble (void)
134 /*******************************************************************
135 * output_header_preamble
137 * Write the first part of the .h file
139 void output_header_preamble (void)
141 if (!globals
.do_code
)
144 hfile
= open_file (OUTPUT_DLL_NAME
, "_dll.h", "w");
146 atexit (output_header_postamble
);
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
,
159 /*******************************************************************
160 * output_header_symbol
162 * Write a symbol to the .h file
164 void output_header_symbol (const parsed_symbol
*sym
)
166 if (!globals
.do_code
)
170 assert (sym
&& sym
->symbol
);
172 if (!globals
.do_code
)
175 if (sym
->flags
& SYM_DATA
)
178 if (!sym
->function_name
)
179 fprintf (hfile
, "/* __%s %s_%s(); */\n", symbol_get_call_convention(sym
),
180 OUTPUT_UC_DLL_NAME
, sym
->symbol
);
183 output_prototype (hfile
, sym
);
184 fputs (";\n", hfile
);
189 /*******************************************************************
190 * output_header_postamble
192 * Write the last part of the .h file
194 static void output_header_postamble (void)
198 fprintf (hfile
, "\n\n\n#endif\t/* __WINE_%s_DLL_H */\n",
206 /*******************************************************************
209 * Write the first part of the .c file
211 void output_c_preamble (void)
213 cfile
= open_file (OUTPUT_DLL_NAME
, "_main.c", "w");
215 atexit (output_c_postamble
);
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
);
225 fprintf (cfile
, "#include \"%s_dll.h\"\n", OUTPUT_DLL_NAME
);
227 fprintf (cfile
,"#include \"wine/debug.h\"\n\nWINE_DEFAULT_DEBUG_CHANNEL(%s);\n\n",
230 if (globals
.forward_dll
)
233 puts ("Creating a forwarding DLL");
235 fputs ("\nHMODULE hDLL=0; /* DLL to call */\n\n", cfile
);
238 "BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, void *reserved)\n{\n"
239 " TRACE(\"(%%p, %%u, %%p)\\n\", instance, reason, reserved);\n\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"
247 " case DLL_PROCESS_DETACH:\n"
248 " FreeLibrary(hDLL);\n"
249 " TRACE(\"Forwarding DLL (%s) freed\\n\");\n"
252 " return TRUE;\n}\n",
253 globals
.forward_dll
, globals
.forward_dll
, globals
.forward_dll
);
258 /*******************************************************************
261 * Write a symbol to the .c file
263 void output_c_symbol (const parsed_symbol
*sym
)
265 unsigned int i
, start
= sym
->flags
& SYM_THISCALL
? 1 : 0;
267 static BOOL has_thiscall
= FALSE
;
270 assert (sym
&& sym
->symbol
);
272 if (!globals
.do_code
)
275 if (sym
->flags
& SYM_DATA
)
277 fprintf (cfile
, "/* FIXME: Move to top of file */\n%s;\n\n",
282 if (sym
->flags
& SYM_THISCALL
&& !has_thiscall
)
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
);
302 output_c_banner(sym
);
304 if (sym
->flags
& SYM_THISCALL
)
305 fprintf(cfile
, "DEFINE_THISCALL_WRAPPER(%s)\n", sym
->function_name
);
307 if (!sym
->function_name
)
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");
316 is_void
= !strcasecmp (sym
->return_text
, "void");
318 output_prototype (cfile
, sym
);
319 fputs ("\n{\n", cfile
);
321 if (!globals
.do_trace
)
323 fputs ("\tFIXME(\":stub\\n\");\n", cfile
);
325 fprintf (cfile
, "\treturn (%s) 0;\n", sym
->return_text
);
326 fputs ("}\n", cfile
);
330 /* Tracing, maybe forwarding as well */
331 if (globals
.forward_dll
)
333 /* Write variables for calling */
335 fputs("\tva_list valist;\n", cfile
);
337 fprintf (cfile
, "\t%s (__%s *pFunc)(", sym
->return_text
,
338 symbol_get_call_convention(sym
));
340 for (i
= start
; i
< sym
->argc
; i
++)
341 fprintf (cfile
, "%s%s", i
> start
? ", " : "", sym
->arg_text
[i
]);
343 fprintf (cfile
, "%s);\n", sym
->varargs
? ",..." : sym
->argc
== 1 &&
344 sym
->flags
& SYM_THISCALL
? "" : sym
->argc
? "" : "void");
347 fprintf (cfile
, "\t%s retVal;\n", sym
->return_text
);
349 fprintf (cfile
, "\tpFunc=(void*)GetProcAddress(hDLL,\"%s\");\n",
353 /* TRACE input arguments */
354 fprintf (cfile
, "\tTRACE(\"(%s", !sym
->argc
? "void" : "");
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
]));
360 fprintf (cfile
, "%s): %s\\n\"", sym
->varargs
? ",..." : "",
361 globals
.forward_dll
? "forward" : "stub");
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(" : "",
368 sym
->arg_type
[i
] == ARG_WIDE_STRING
? ")" : "");
370 fputs (");\n", cfile
);
372 if (!globals
.forward_dll
)
375 fprintf (cfile
, "\treturn (%s) 0;\n", sym
->return_text
);
376 fputs ("}\n", cfile
);
382 fprintf (cfile
, "\tva_start(valist,%s);\n", sym
->arg_name
[sym
->argc
-1]);
384 fprintf (cfile
, "\t%spFunc(", !is_void
? "retVal = " : "");
386 for (i
= 0; i
< sym
->argc
; i
++)
387 fprintf (cfile
, "%s%s", i
? "," : "", sym
->arg_name
[i
]);
389 fputs (sym
->varargs
? ",valist);\n\tva_end(valist);" : ");", cfile
);
391 /* TRACE return value */
392 fprintf (cfile
, "\n\tTRACE(\"Returned (%s)\\n\"",
393 get_format_str (sym
->return_type
));
397 if (sym
->return_type
== ARG_WIDE_STRING
)
398 fputs (",debugstr_w(retVal)", cfile
);
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
);
405 fputs (");\n", cfile
);
407 fputs ("}\n", cfile
);
411 /*******************************************************************
414 * Write the last part of the .c file
416 static void output_c_postamble (void)
424 /*******************************************************************
427 * Write a Wine compatible makefile.in
429 void output_makefile (void)
431 FILE *makefile
= open_file ("Makefile", ".in", "w");
434 puts ("Creating makefile");
437 "# Generated from %s by winedump.\n"
438 "MODULE = %s.dll\n", globals
.input_name
, OUTPUT_DLL_NAME
);
440 if (globals
.forward_dll
)
441 fprintf (makefile
, "IMPORTS = %s", globals
.forward_dll
);
443 fprintf (makefile
, "\n\nC_SRCS = \\\n\t%s_main.c\n", OUTPUT_DLL_NAME
);
445 if (globals
.forward_dll
)
446 fprintf (specfile
,"#import %s.dll\n", globals
.forward_dll
);
452 /*******************************************************************
455 * Write a C prototype for a parsed symbol
457 void output_prototype (FILE *file
, const parsed_symbol
*sym
)
459 unsigned int i
, start
= sym
->flags
& SYM_THISCALL
? 1 : 0;
461 fprintf (file
, "%s __%s %s_%s(", sym
->return_text
, symbol_get_call_convention(sym
),
462 OUTPUT_UC_DLL_NAME
, sym
->function_name
);
464 if (!sym
->argc
|| (sym
->argc
== 1 && sym
->flags
& SYM_THISCALL
))
465 fputs ("void", file
);
467 for (i
= start
; i
< sym
->argc
; i
++)
468 fprintf (file
, "%s%s %s", i
> start
? ", " : "", sym
->arg_text
[i
],
471 fputs (", ...", file
);
476 /*******************************************************************
479 * Write a function banner to the .c file
481 void output_c_banner (const parsed_symbol
*sym
)
486 if (sym
->ordinal
>= 0)
487 sprintf(ord_spec
, "%d", sym
->ordinal
);
494 fprintf (cfile
, "/*********************************************************"
495 "*********\n *\t\t%s (%s.%s)\n *\n", sym
->symbol
,
496 OUTPUT_UC_DLL_NAME
, ord_spec
);
498 if (globals
.do_documentation
&& sym
->function_name
)
500 fputs (" *\n * PARAMS\n *\n", cfile
);
503 fputs (" * None.\n *\n", cfile
);
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)");
513 fputs (" * ...[I]\n", cfile
);
514 fputs (" *\n", cfile
);
517 fputs (" * RETURNS\n *\n", cfile
);
519 if (sym
->return_text
&& !strcmp (sym
->return_text
, "void"))
520 fputs (" * Nothing.\n", cfile
);
522 fprintf (cfile
, " * %s\n", sym
->return_text
);
524 fputs (" *\n */\n", cfile
);
528 /*******************************************************************
531 * Get a string containing the correct format string for a type
533 static const char *get_format_str (int type
)
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";
551 /*******************************************************************
554 * Determine if a parameter is In or In/Out
556 static const char *get_in_or_out (const parsed_symbol
*sym
, size_t arg
)
558 assert (sym
&& arg
< sym
->argc
);
559 assert (globals
.do_documentation
);
561 if (sym
->arg_flag
[arg
] & CT_CONST
)
564 switch (sym
->arg_type
[arg
])
569 case ARG_STRUCT
: return "In";
571 case ARG_WIDE_STRING
:
572 case ARG_STRING
: return "In/Out";