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
22 #include "wine/port.h"
27 static FILE *specfile = NULL;
28 static FILE *hfile = NULL;
29 static FILE *cfile = NULL;
31 static void output_spec_postamble (void);
32 static void output_header_postamble (void);
33 static void output_c_postamble (void);
34 static void output_c_banner (const parsed_symbol *sym);
35 static const char *get_format_str (int type);
36 static const char *get_in_or_out (const parsed_symbol *sym, size_t arg);
39 /*******************************************************************
40 * output_spec_preamble
42 * Write the first part of the .spec file
44 void output_spec_preamble (void)
46 specfile = open_file (OUTPUT_DLL_NAME, ".spec", "w");
48 atexit (output_spec_postamble);
51 puts ("Creating .spec preamble");
54 "# Generated from %s by winedump\n\n", globals.input_name);
58 /*******************************************************************
61 * Write a symbol to the .spec file
63 void output_spec_symbol (const parsed_symbol *sym)
68 assert (sym && sym->symbol);
70 if (sym->ordinal >= 0)
71 snprintf(ord_spec, 8, "%d", sym->ordinal);
77 if (sym->flags & SYM_THISCALL)
78 strcat (ord_spec, " -i386"); /* For binary compatibility only */
80 if (!globals.do_code || !sym->function_name)
82 if (sym->flags & SYM_DATA)
84 if (globals.forward_dll)
85 fprintf (specfile, "%s forward %s %s.%s #", ord_spec, sym->symbol,
86 globals.forward_dll, sym->symbol);
88 fprintf (specfile, "%s extern %s %s\n", ord_spec, sym->symbol,
93 if (globals.forward_dll)
94 fprintf (specfile, "%s forward %s %s.%s\n", ord_spec, sym->symbol,
95 globals.forward_dll, sym->symbol);
97 fprintf (specfile, "%s stub %s\n", ord_spec, sym->symbol);
101 unsigned int i = sym->flags & SYM_THISCALL ? 1 : 0;
103 fprintf (specfile, "%s %s %s(", ord_spec, sym->varargs ? "varargs" :
104 symbol_get_call_convention(sym), sym->symbol);
106 for (; i < sym->argc; i++)
107 fprintf (specfile, " %s", symbol_get_spec_type(sym, i));
110 fputc (' ', specfile);
111 fputs (") ", specfile);
113 if (sym->flags & SYM_THISCALL)
114 fputs ("__thiscall_", specfile);
115 fprintf (specfile, "%s_%s", OUTPUT_UC_DLL_NAME, sym->function_name);
117 fputc ('\n',specfile);
122 /*******************************************************************
123 * output_spec_postamble
125 * Write the last part of the .spec file
127 static void output_spec_postamble (void)
135 /*******************************************************************
136 * output_header_preamble
138 * Write the first part of the .h file
140 void output_header_preamble (void)
142 if (!globals.do_code)
145 hfile = open_file (OUTPUT_DLL_NAME, "_dll.h", "w");
147 atexit (output_header_postamble);
150 "/*\n * %s.dll\n *\n * Generated from %s.dll by winedump.\n *\n"
151 " * DO NOT SEND GENERATED DLLS FOR INCLUSION INTO WINE !\n * \n */"
152 "\n#ifndef __WINE_%s_DLL_H\n#define __WINE_%s_DLL_H\n\n"
153 "#include \"windef.h\"\n#include \"wine/debug.h\"\n"
154 "#include \"winbase.h\"\n#include \"winnt.h\"\n\n\n",
155 OUTPUT_DLL_NAME, OUTPUT_DLL_NAME, OUTPUT_UC_DLL_NAME,
160 /*******************************************************************
161 * output_header_symbol
163 * Write a symbol to the .h file
165 void output_header_symbol (const parsed_symbol *sym)
167 if (!globals.do_code)
171 assert (sym && sym->symbol);
173 if (!globals.do_code)
176 if (sym->flags & SYM_DATA)
179 if (!sym->function_name)
180 fprintf (hfile, "/* __%s %s_%s(); */\n", symbol_get_call_convention(sym),
181 OUTPUT_UC_DLL_NAME, sym->symbol);
184 output_prototype (hfile, sym);
185 fputs (";\n", hfile);
190 /*******************************************************************
191 * output_header_postamble
193 * Write the last part of the .h file
195 static void output_header_postamble (void)
199 fprintf (hfile, "\n\n\n#endif\t/* __WINE_%s_DLL_H */\n",
207 /*******************************************************************
210 * Write the first part of the .c file
212 void output_c_preamble (void)
214 cfile = open_file (OUTPUT_DLL_NAME, "_main.c", "w");
216 atexit (output_c_postamble);
219 "/*\n * %s.dll\n *\n * Generated from %s by winedump.\n *\n"
220 " * DO NOT SUBMIT GENERATED DLLS FOR INCLUSION INTO WINE!\n *\n */"
221 "\n\n#include \"config.h\"\n\n#include <stdarg.h>\n\n"
222 "#include \"windef.h\"\n#include \"winbase.h\"\n",
223 OUTPUT_DLL_NAME, globals.input_name);
226 fprintf (cfile, "#include \"%s_dll.h\"\n", OUTPUT_DLL_NAME);
228 fprintf (cfile,"#include \"wine/debug.h\"\n\nWINE_DEFAULT_DEBUG_CHANNEL(%s);\n\n",
231 if (globals.forward_dll)
234 puts ("Creating a forwarding DLL");
236 fputs ("\nHMODULE hDLL=0;\t/* DLL to call */\n\n", cfile);
240 "BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID "
242 "\tTRACE(\"(0x%%p, %%d, %%p)\\n\", hinstDLL, fdwReason, lpvReserved);\n"
243 "\n\tswitch (fdwReason)\n\t{\n"
244 "\t\tcase DLL_WINE_PREATTACH:\n"
245 "\t\t\treturn FALSE; /* prefer native version */\n"
246 "\t\tcase DLL_PROCESS_ATTACH:\n");
248 if (globals.forward_dll)
249 fprintf (cfile, "\t\t\thDLL = LoadLibraryA(\"%s\");\n"
250 "\t\t\tTRACE(\"Forwarding DLL (%s) loaded (%%p)\\n\", hDLL);\n",
251 globals.forward_dll, globals.forward_dll);
253 fprintf (cfile, "\t\t\t/* FIXME: Initialisation */\n"
254 "\t\t\tDisableThreadLibraryCalls(hinstDLL);\n\t\t\tbreak;\n");
256 fprintf (cfile, "\t\t\tbreak;\n\t\tcase DLL_PROCESS_DETACH:\n");
258 if (globals.forward_dll)
259 fprintf (cfile, "\t\t\tFreeLibrary(hDLL);\n"
260 "\t\t\tTRACE(\"Forwarding DLL (%s) freed\\n\");\n",
261 globals.forward_dll);
263 fprintf (cfile, "\t\t\tbreak;\n\t\tdefault:\n\t\t\tbreak;\n\t}\n\n"
264 "\treturn TRUE;\n}\n\n\n");
268 /*******************************************************************
271 * Write a symbol to the .c file
273 void output_c_symbol (const parsed_symbol *sym)
275 unsigned int i, start = sym->flags & SYM_THISCALL ? 1 : 0;
277 static int has_thiscall = 0;
280 assert (sym && sym->symbol);
282 if (!globals.do_code)
285 if (sym->flags & SYM_DATA)
287 fprintf (cfile, "/* FIXME: Move to top of file */\n%s;\n\n",
292 if (sym->flags & SYM_THISCALL && !has_thiscall)
294 fputs ("#ifdef __i386__ /* thiscall functions are i386-specific */\n\n"
295 "#define THISCALL(func) __thiscall_ ## func\n"
296 "#define THISCALL_NAME(func) __ASM_NAME(\"__thiscall_\" #func)\n"
297 "#define DEFINE_THISCALL_WRAPPER(func) \\\n"
298 "\textern void THISCALL(func)(); \\\n"
299 "\t__ASM_GLOBAL_FUNC(__thiscall_ ## func, \\\n"
300 "\t\t\t\"popl %eax\\n\\t\" \\\n"
301 "\t\t\t\"pushl %ecx\\n\\t\" \\\n"
302 "\t\t\t\"pushl %eax\\n\\t\" \\\n"
303 "\t\t\t\"jmp \" __ASM_NAME(#func) )\n"
304 "#else /* __i386__ */\n\n"
305 "#define THISCALL(func) func\n"
306 "#define THISCALL_NAME(func) __ASM_NAME(#func)\n"
307 "#define DEFINE_THISCALL_WRAPPER(func) /* nothing */\n\n"
308 "#endif /* __i386__ */\n\n", cfile);
312 output_c_banner(sym);
314 if (sym->flags & SYM_THISCALL)
315 fprintf(cfile, "DEFINE_THISCALL_WRAPPER(%s)\n", sym->function_name);
317 if (!sym->function_name)
320 fprintf (cfile, "#if 0\n__%s %s_%s()\n{\n\t/* %s in .spec */\n}\n#endif\n",
321 symbol_get_call_convention(sym), OUTPUT_UC_DLL_NAME, sym->symbol,
322 globals.forward_dll ? "@forward" : "@stub");
326 is_void = !strcmp (sym->return_text, "void");
328 output_prototype (cfile, sym);
329 fputs ("\n{\n", cfile);
331 if (!globals.do_trace)
333 fputs ("\tFIXME(\":stub\\n\");\n", cfile);
335 fprintf (cfile, "\treturn (%s) 0;\n", sym->return_text);
336 fputs ("}\n", cfile);
340 /* Tracing, maybe forwarding as well */
341 if (globals.forward_dll)
343 /* Write variables for calling */
345 fputs("\tva_list valist;\n", cfile);
347 fprintf (cfile, "\t%s (__%s *pFunc)(", sym->return_text,
348 symbol_get_call_convention(sym));
350 for (i = start; i < sym->argc; i++)
351 fprintf (cfile, "%s%s", i > start ? ", " : "", sym->arg_text [i]);
353 fprintf (cfile, "%s);\n", sym->varargs ? ",..." : sym->argc == 1 &&
354 sym->flags & SYM_THISCALL ? "" : sym->argc ? "" : "void");
357 fprintf (cfile, "\t%s retVal;\n", sym->return_text);
359 fprintf (cfile, "\tpFunc=(void*)GetProcAddress(hDLL,\"%s\");\n",
363 /* TRACE input arguments */
364 fprintf (cfile, "\tTRACE(\"(%s", !sym->argc ? "void" : "");
366 for (i = 0; i < sym->argc; i++)
367 fprintf (cfile, "%s(%s)%s", i ? "," : "", sym->arg_text [i],
368 get_format_str (sym->arg_type [i]));
370 fprintf (cfile, "%s): %s\\n\"", sym->varargs ? ",..." : "",
371 globals.forward_dll ? "forward" : "stub");
373 for (i = 0; i < sym->argc; i++)
374 if (sym->arg_type[i] != ARG_STRUCT)
375 fprintf(cfile, ",%s%s%s%s", sym->arg_type[i] == ARG_LONG ? "(LONG)" : "",
376 sym->arg_type[i] == ARG_WIDE_STRING ? "debugstr_w(" : "",
378 sym->arg_type[i] == ARG_WIDE_STRING ? ")" : "");
380 fputs (");\n", cfile);
382 if (!globals.forward_dll)
385 fprintf (cfile, "\treturn (%s) 0;\n", sym->return_text);
386 fputs ("}\n", cfile);
392 fprintf (cfile, "\tva_start(valist,%s);\n", sym->arg_name[sym->argc-1]);
394 fprintf (cfile, "\t%spFunc(", !is_void ? "retVal = " : "");
396 for (i = 0; i < sym->argc; i++)
397 fprintf (cfile, "%s%s", i ? "," : "", sym->arg_name [i]);
399 fputs (sym->varargs ? ",valist);\n\tva_end(valist);" : ");", cfile);
401 /* TRACE return value */
402 fprintf (cfile, "\n\tTRACE(\"Returned (%s)\\n\"",
403 get_format_str (sym->return_type));
407 if (sym->return_type == ARG_WIDE_STRING)
408 fputs (",debugstr_w(retVal)", cfile);
410 fprintf (cfile, ",%s%s", sym->return_type == ARG_LONG ? "(LONG)" : "",
411 sym->return_type == ARG_STRUCT ? "" : "retVal");
412 fputs (");\n\treturn retVal;\n", cfile);
415 fputs (");\n", cfile);
417 fputs ("}\n", cfile);
421 /*******************************************************************
424 * Write the last part of the .c file
426 static void output_c_postamble (void)
434 /*******************************************************************
437 * Write a Wine compatible makefile.in
439 void output_makefile (void)
441 FILE *makefile = open_file ("Makefile", ".in", "w");
444 puts ("Creating makefile");
447 "# Generated from %s by winedump.\nTOPSRCDIR = @top_srcdir@\n"
448 "TOPOBJDIR = ../..\nSRCDIR = @srcdir@\nVPATH = @srcdir@\n"
449 "MODULE = %s.dll\n", globals.input_name, OUTPUT_DLL_NAME);
451 fprintf (makefile, "IMPORTS = kernel32");
452 if (globals.forward_dll)
453 fprintf (makefile, " %s", globals.forward_dll);
456 "\n\nC_SRCS = \\\n\t%s_main.c\n\n@MAKE_DLL_RULES@\n\n"
457 "@DEPENDENCIES@ # everything below this line is overwritten by make depend",
460 if (globals.forward_dll)
461 fprintf (specfile,"#import %s.dll\n", globals.forward_dll);
467 /*******************************************************************
470 * Write a C prototype for a parsed symbol
472 void output_prototype (FILE *file, const parsed_symbol *sym)
474 unsigned int i, start = sym->flags & SYM_THISCALL ? 1 : 0;
476 fprintf (file, "%s __%s %s_%s(", sym->return_text, symbol_get_call_convention(sym),
477 OUTPUT_UC_DLL_NAME, sym->function_name);
479 if (!sym->argc || (sym->argc == 1 && sym->flags & SYM_THISCALL))
480 fputs ("void", file);
482 for (i = start; i < sym->argc; i++)
483 fprintf (file, "%s%s %s", i > start ? ", " : "", sym->arg_text [i],
486 fputs (", ...", file);
491 /*******************************************************************
494 * Write a function banner to the .c file
496 void output_c_banner (const parsed_symbol *sym)
501 if (sym->ordinal >= 0)
502 snprintf(ord_spec, sizeof (ord_spec), "%d", sym->ordinal);
509 fprintf (cfile, "/*********************************************************"
510 "*********\n *\t\t%s (%s.%s)\n *\n", sym->symbol,
511 OUTPUT_UC_DLL_NAME, ord_spec);
513 if (globals.do_documentation && sym->function_name)
515 fputs (" *\n * PARAMS\n *\n", cfile);
518 fputs (" * None.\n *\n", cfile);
521 for (i = 0; i < sym->argc; i++)
522 fprintf (cfile, " * %s [%s]%s\n", sym->arg_name [i],
523 get_in_or_out(sym, i),
524 strcmp (sym->arg_name [i], "_this") ? "" :
525 " Pointer to the class object (in ECX)");
528 fputs (" * ...[I]\n", cfile);
529 fputs (" *\n", cfile);
532 fputs (" * RETURNS\n *\n", cfile);
534 if (sym->return_text && !strcmp (sym->return_text, "void"))
535 fputs (" * Nothing.\n", cfile);
537 fprintf (cfile, " * %s\n", sym->return_text);
539 fputs (" *\n */\n", cfile);
543 /*******************************************************************
546 * Get a string containing the correct format string for a type
548 static const char *get_format_str (int type)
552 case ARG_VOID: return "void";
553 case ARG_FLOAT: return "%f";
554 case ARG_DOUBLE: return "%g";
555 case ARG_POINTER: return "%p";
556 case ARG_WIDE_STRING:
557 case ARG_STRING: return "%s";
558 case ARG_LONG: return "%ld";
559 case ARG_STRUCT: return "struct";
566 /*******************************************************************
569 * Determine if a parameter is In or In/Out
571 static const char *get_in_or_out (const parsed_symbol *sym, size_t arg)
573 assert (sym && arg < sym->argc);
574 assert (globals.do_documentation);
576 if (sym->arg_flag [arg] & CT_CONST)
579 switch (sym->arg_type [arg])
584 case ARG_STRUCT: return "In";
586 case ARG_WIDE_STRING:
587 case ARG_STRING: return "In/Out";