2 * Code generation functions
4 * Copyright 2000 Jon Griffiths
9 static FILE *specfile = NULL;
10 static FILE *hfile = NULL;
11 static FILE *cfile = NULL;
13 static void output_spec_postamble (void);
14 static void output_header_postamble (void);
15 static void output_c_postamble (void);
16 static void output_c_banner (const parsed_symbol *sym);
17 static const char *get_format_str (int type);
18 static const char *get_in_or_out (const parsed_symbol *sym, size_t arg);
21 /*******************************************************************
22 * output_spec_preamble
24 * Write the first part of the .spec file
26 void output_spec_preamble (void)
28 specfile = open_file (OUTPUT_DLL_NAME, ".spec", "w");
30 atexit (output_spec_postamble);
33 puts ("Creating .spec preamble");
36 "# Generated from %s by winedump\nname %s\n"
37 "type win32\ninit %s_Init\n\nimport kernel32.dll\n"
38 "import ntdll.dll\n", globals.input_name, OUTPUT_DLL_NAME,
41 if (globals.forward_dll)
42 fprintf (specfile,"#import %s.dll\n", globals.forward_dll);
44 fprintf (specfile, "\n\ndebug_channels (%s)\n\n", OUTPUT_DLL_NAME);
48 /*******************************************************************
51 * Write a symbol to the .spec file
53 void output_spec_symbol (const parsed_symbol *sym)
58 assert (sym && sym->symbol);
60 if (sym->ordinal >= 0)
61 snprintf(ord_spec, 8, "%d", sym->ordinal);
67 if (sym->flags & SYM_THISCALL)
68 strcat (ord_spec, " -i386"); /* For binary compatibility only */
70 if (!globals.do_code || !sym->function_name)
72 if (sym->flags & SYM_DATA)
74 if (globals.forward_dll)
75 fprintf (specfile, "%s forward %s %s.%s #", ord_spec, sym->symbol,
76 globals.forward_dll, sym->symbol);
78 fprintf (specfile, "%s extern %s %s\n", ord_spec, sym->symbol,
83 if (globals.forward_dll)
84 fprintf (specfile, "%s forward %s %s.%s\n", ord_spec, sym->symbol,
85 globals.forward_dll, sym->symbol);
87 fprintf (specfile, "%s stub %s\n", ord_spec, sym->symbol);
91 unsigned int i = sym->flags & SYM_THISCALL ? 1 : 0;
93 fprintf (specfile, "%s %s %s(", ord_spec, sym->varargs ? "varargs" :
94 symbol_get_call_convention(sym), sym->symbol);
96 for (; i < sym->argc; i++)
97 fprintf (specfile, " %s", symbol_get_spec_type(sym, i));
100 fputc (' ', specfile);
101 fprintf (specfile, ") %s_%s", OUTPUT_UC_DLL_NAME, sym->function_name);
103 if (sym->flags & SYM_THISCALL)
104 fputs (" # __thiscall", specfile);
106 fputc ('\n',specfile);
111 /*******************************************************************
112 * output_spec_postamble
114 * Write the last part of the .spec file
116 static void output_spec_postamble (void)
124 /*******************************************************************
125 * output_header_preamble
127 * Write the first part of the .h file
129 void output_header_preamble (void)
131 hfile = open_file (OUTPUT_DLL_NAME, "_dll.h", "w");
133 atexit (output_header_postamble);
136 "/*\n * %s.dll\n *\n * Generated from %s.dll by winedump.\n *\n"
137 " * DO NOT SEND GENERATED DLLS FOR INCLUSION INTO WINE !\n * \n */"
138 "\n#ifndef __WINE_%s_DLL_H\n#define __WINE_%s_DLL_H\n\n#include "
139 "\"config.h\"\n#include \"windef.h\"\n#include \"wine/debug.h\"\n"
140 "#include \"winbase.h\"\n#include \"winnt.h\"\n\n\n",
141 OUTPUT_DLL_NAME, OUTPUT_DLL_NAME, OUTPUT_UC_DLL_NAME,
146 /*******************************************************************
147 * output_header_symbol
149 * Write a symbol to the .h file
151 void output_header_symbol (const parsed_symbol *sym)
154 assert (sym && sym->symbol);
156 if (!globals.do_code)
159 if (sym->flags & SYM_DATA)
162 if (!sym->function_name)
163 fprintf (hfile, "/* __%s %s_%s(); */\n", symbol_get_call_convention(sym),
164 OUTPUT_UC_DLL_NAME, sym->symbol);
167 output_prototype (hfile, sym);
168 fputs (";\n", hfile);
173 /*******************************************************************
174 * output_header_postamble
176 * Write the last part of the .h file
178 static void output_header_postamble (void)
182 fprintf (hfile, "\n\n\n#endif\t/* __WINE_%s_DLL_H */\n",
190 /*******************************************************************
193 * Write the first part of the .c file
195 void output_c_preamble (void)
197 cfile = open_file (OUTPUT_DLL_NAME, "_main.c", "w");
199 atexit (output_c_postamble);
202 "/*\n * %s.dll\n *\n * Generated from %s by winedump.\n *\n"
203 " * DO NOT SUBMIT GENERATED DLLS FOR INCLUSION INTO WINE!\n * \n */"
204 "\n\n#include \"%s_dll.h\"\n\nWINE_DEFAULT_DEBUG_CHANNEL(%s);\n\n",
205 OUTPUT_DLL_NAME, globals.input_name, OUTPUT_DLL_NAME,
208 if (globals.forward_dll)
211 puts ("Creating a forwarding DLL");
213 fputs ("\nHMODULE hDLL=0;\t/* DLL to call */\n\n", cfile);
216 fputs ("#ifdef __i386__\n#define GET_THIS(t,p) t p;\\\n__asm__ __volatile__"
217 " (\"movl %%ecx, %0\" : \"=m\" (p))\n#endif\n\n\n", cfile);
220 "BOOL WINAPI %s_Init(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID "
221 "lpvReserved)\n{\n\tTRACE(\"(0x%%08x, %%ld, %%p)\\n\",hinstDLL,"
222 "fdwReason,lpvReserved);\n\n\t"
223 "if (fdwReason == DLL_PROCESS_ATTACH)\n\t{\n\t\t",
226 if (globals.forward_dll)
229 "hDLL = LoadLibraryA( \"%s\" );\n\t\t"
230 "TRACE(\":Forwarding DLL (%s) loaded (%%ld)\\n\",(LONG)hDLL);",
231 globals.forward_dll, globals.forward_dll);
234 fputs ("/* FIXME: Initialisation */", cfile);
236 fputs ("\n\t}\n\telse if (fdwReason == DLL_PROCESS_DETACH)\n\t{\n\t\t",
239 if (globals.forward_dll)
242 "FreeLibrary( hDLL );\n\t\tTRACE(\":Forwarding DLL (%s)"
243 " freed\\n\");", globals.forward_dll);
246 fputs ("/* FIXME: Cleanup */", cfile);
248 fputs ("\n\t}\n\n\treturn TRUE;\n}\n\n\n", cfile);
252 #define CPP_END if (sym->flags & SYM_THISCALL) \
253 fputs ("#endif\n", cfile); fputs ("\n\n", cfile)
254 #define GET_THIS if (sym->flags & SYM_THISCALL) \
255 fprintf (cfile, "\tGET_THIS(%s,%s);\n", sym->arg_text[0],sym->arg_name[0])
257 /*******************************************************************
260 * Write a symbol to the .c file
262 void output_c_symbol (const parsed_symbol *sym)
264 unsigned int i, start = sym->flags & SYM_THISCALL ? 1 : 0;
268 assert (sym && sym->symbol);
270 if (!globals.do_code)
273 if (sym->flags & SYM_DATA)
275 fprintf (cfile, "/* FIXME: Move to top of file */\n%s;\n\n",
280 if (sym->flags & SYM_THISCALL)
281 fputs ("#ifdef __i386__\n", cfile);
283 output_c_banner(sym);
285 if (!sym->function_name)
288 fprintf (cfile, "#if 0\n__%s %s_%s()\n{\n\t/* %s in .spec */\n}\n#endif\n",
289 symbol_get_call_convention(sym), OUTPUT_UC_DLL_NAME, sym->symbol,
290 globals.forward_dll ? "@forward" : "@stub");
295 is_void = !strcmp (sym->return_text, "void");
297 output_prototype (cfile, sym);
298 fputs ("\n{\n", cfile);
300 if (!globals.do_trace)
303 fputs ("\tFIXME(\":stub\\n\");\n", cfile);
305 fprintf (cfile, "\treturn (%s) 0;\n", sym->return_text);
306 fputs ("}\n", cfile);
311 /* Tracing, maybe forwarding as well */
312 if (globals.forward_dll)
314 /* Write variables for calling */
316 fputs("\tva_list valist;\n", cfile);
318 fprintf (cfile, "\t%s (__%s *pFunc)(", sym->return_text,
319 symbol_get_call_convention(sym));
321 for (i = start; i < sym->argc; i++)
322 fprintf (cfile, "%s%s", i > start ? ", " : "", sym->arg_text [i]);
324 fprintf (cfile, "%s);\n", sym->varargs ? ",..." : sym->argc == 1 &&
325 sym->flags & SYM_THISCALL ? "" : sym->argc ? "" : "void");
328 fprintf (cfile, "\t%s retVal;\n", sym->return_text);
332 fprintf (cfile, "\tpFunc=(void*)GetProcAddress(hDLL,\"%s\");\n",
336 /* TRACE input arguments */
337 fprintf (cfile, "\tTRACE(\"(%s", !sym->argc ? "void" : "");
339 for (i = 0; i < sym->argc; i++)
340 fprintf (cfile, "%s(%s)%s", i ? "," : "", sym->arg_text [i],
341 get_format_str (sym->arg_type [i]));
343 fprintf (cfile, "%s): %s\\n\"", sym->varargs ? ",..." : "",
344 globals.forward_dll ? "forward" : "stub");
346 for (i = 0; i < sym->argc; i++)
347 if (sym->arg_type[i] != ARG_STRUCT)
348 fprintf(cfile, ",%s%s%s%s", sym->arg_type[i] == ARG_LONG ? "(LONG)" : "",
349 sym->arg_type[i] == ARG_WIDE_STRING ? "debugstr_w(" : "",
351 sym->arg_type[i] == ARG_WIDE_STRING ? ")" : "");
353 fputs (");\n", cfile);
355 if (!globals.forward_dll)
358 fprintf (cfile, "\treturn (%s) 0;\n", sym->return_text);
359 fputs ("}\n", cfile);
366 fprintf (cfile, "\tva_start(valist,%s);\n", sym->arg_name[sym->argc-1]);
368 fprintf (cfile, "\t%spFunc(", !is_void ? "retVal = " : "");
370 for (i = 0; i < sym->argc; i++)
371 fprintf (cfile, "%s%s", i ? "," : "", sym->arg_name [i]);
373 fputs (sym->varargs ? ",valist);\n\tva_end(valist);" : ");", cfile);
375 /* TRACE return value */
376 fprintf (cfile, "\n\tTRACE(\"Returned (%s)\\n\"",
377 get_format_str (sym->return_type));
381 if (sym->return_type == ARG_WIDE_STRING)
382 fputs (",debugstr_w(retVal)", cfile);
384 fprintf (cfile, ",%s%s", sym->return_type == ARG_LONG ? "(LONG)" : "",
385 sym->return_type == ARG_STRUCT ? "" : "retVal");
386 fputs (");\n\treturn retVal;\n", cfile);
389 fputs (");\n", cfile);
391 fputs ("}\n", cfile);
396 /*******************************************************************
399 * Write the last part of the .c file
401 static void output_c_postamble (void)
409 /*******************************************************************
412 * Write a Wine compatible makefile.in
414 void output_makefile (void)
416 FILE *makefile = open_file ("Makefile", ".in", "w");
419 puts ("Creating makefile");
422 "# Generated from %s by winedump.\nTOPSRCDIR = @top_srcdir@\n"
423 "TOPOBJDIR = ../..\nSRCDIR = @srcdir@\nVPATH = @srcdir@\n"
424 "MODULE = %s\nEXTRALIBS = $(LIBUNICODE)\n\n"
425 "LDDLLFLAGS = @LDDLLFLAGS@\nSYMBOLFILE = $(MODULE).tmp.o\n\n"
426 "C_SRCS = \\\n\t%s_main.c\n\n@MAKE_DLL_RULES@\n\n### Dependencies:",
427 globals.input_name, OUTPUT_DLL_NAME, OUTPUT_DLL_NAME);
433 /*******************************************************************
434 * output_install_script
436 * Write a script to insert the DLL into Wine
438 * Rather than using diff/patch, several sed calls are generated
439 * so the script can be re-run at any time without breaking.
441 void output_install_script (void)
444 FILE *install_file = open_file (OUTPUT_DLL_NAME, "_install", "w");
447 puts ("Creating install script");
449 fprintf (install_file,
450 "#!/bin/bash\n# Generated from %s.dll by winedump.\n\n"
451 "if [ $# -ne 1 ] || [ ! -d $1 ] || [ ! -f"
452 " $1/AUTHORS ]; then\n\t[ $# -eq 1 ] && echo \"Invalid path\"\n"
453 "\techo \"Usage: $0 wine-base-dir\"\n\texit 1\nfi\n\n"
454 "if [ -d $1/dlls/%s ]; then\n\techo \"DLL is already present\"\n"
455 "\texit 1\nfi\n\necho Adding DLL %s to Wine build tree...\n"
456 "echo\n\nmkdir $1/dlls/%s\ncp %s.spec $1/dlls/%s\n"
457 "cp %s_main.c $1/dlls/%s\ncp %s_dll.h $1/dlls/%s\n"
458 "cp Makefile.in $1/dlls/%s/Makefile.in\necho Copied DLL files\n\n"
459 "cd $1\n\nsed '/dlls\\/"
460 "x11drv\\/Makefile/{G;s/$/dlls\\/%s\\/Makefile/;}' configure.in"
461 " >t.tmp\nmv -f t.tmp configure.in\necho Patched configure.in\n\n"
462 "sed '/all:/{G;s/$/\\^lib%s.so \\\\/;}'"
463 " dlls/Makefile.in| tr ^ \\\\t >t.tmp\n"
464 "sed '/SUBDIRS =/{G;s/$/\\^%s \\\\/;}' t.tmp | tr ^ \\\\t >t.tmp2"
465 "\nsed '/Map library name /{G;s/$/^\\$(RM) \\$\\@ \\&\\& \\$\\"
466 "(LN_S\\) %s\\/lib%s.\\$(LIBEXT) \\$\\@/;}' t.tmp2 | tr ^ \\\\t"
467 " > t.tmp\nsed '/Map library name /{G;s/$/lib%s.\\$(LIBEXT): "
468 "%s\\/lib%s.\\$(LIBEXT)/;}' t.tmp > t.tmp2\nsed '/dll "
469 "dependencies/{G;s/$/^\\@cd %s \\&\\& \\$(MAKE) lib%s.\\$(LIBEXT)"
470 "/;}' t.tmp2 | tr ^ \\\\t > t.tmp\nsed '/dll "
471 "dependencies/{G;s/$/%s\\/lib%s.\\$(LIBEXT)\\: libkernel32."
472 "\\$(LIBEXT) libntdll.\\$(LIBEXT)/;}' t.tmp > t.tmp2\n"
473 "mv -f t.tmp2 dlls/Makefile.in\nrm -f t.tmp\necho Patched dlls/"
474 "Makefile.in\n\necho\necho ...done.\necho Run \\'autoconf\\', "
475 "\\'./configure\\' then \\'make\\' to rebuild Wine\n\n",
476 OUTPUT_DLL_NAME, OUTPUT_DLL_NAME, OUTPUT_DLL_NAME, OUTPUT_DLL_NAME,
477 OUTPUT_DLL_NAME, OUTPUT_DLL_NAME, OUTPUT_DLL_NAME, OUTPUT_DLL_NAME,
478 OUTPUT_DLL_NAME, OUTPUT_DLL_NAME, OUTPUT_DLL_NAME, OUTPUT_DLL_NAME,
479 OUTPUT_DLL_NAME, OUTPUT_DLL_NAME, OUTPUT_DLL_NAME, OUTPUT_DLL_NAME,
480 OUTPUT_DLL_NAME, OUTPUT_DLL_NAME, OUTPUT_DLL_NAME, OUTPUT_DLL_NAME,
481 OUTPUT_DLL_NAME, OUTPUT_DLL_NAME, OUTPUT_DLL_NAME);
483 fclose (install_file);
484 snprintf (cmd, sizeof (cmd), "chmod a+x %s_install", OUTPUT_DLL_NAME);
489 /*******************************************************************
492 * Write a C prototype for a parsed symbol
494 void output_prototype (FILE *file, const parsed_symbol *sym)
496 unsigned int i, start = sym->flags & SYM_THISCALL ? 1 : 0;
498 fprintf (file, "%s __%s %s_%s(", sym->return_text, symbol_get_call_convention(sym),
499 OUTPUT_UC_DLL_NAME, sym->function_name);
501 if (!sym->argc || (sym->argc == 1 && sym->flags & SYM_THISCALL))
502 fputs ("void", file);
504 for (i = start; i < sym->argc; i++)
505 fprintf (file, "%s%s %s", i > start ? ", " : "", sym->arg_text [i],
508 fputs (", ...", file);
513 /*******************************************************************
516 * Write a function banner to the .c file
518 void output_c_banner (const parsed_symbol *sym)
523 if (sym->ordinal >= 0)
524 snprintf(ord_spec, sizeof (ord_spec), "%d", sym->ordinal);
531 fprintf (cfile, "/*********************************************************"
532 "*********\n *\t\t%s (%s.%s)\n *\n", sym->symbol,
533 OUTPUT_UC_DLL_NAME, ord_spec);
535 if (globals.do_documentation && sym->function_name)
537 fputs (" *\n * PARAMS\n *\n", cfile);
540 fputs (" * None.\n *\n", cfile);
543 for (i = 0; i < sym->argc; i++)
544 fprintf (cfile, " * %s [%s]%s\n", sym->arg_name [i],
545 get_in_or_out(sym, i),
546 strcmp (sym->arg_name [i], "_this") ? "" :
547 " Pointer to the class object (in ECX)");
550 fputs (" * ...[I]\n", cfile);
551 fputs (" *\n", cfile);
554 fputs (" * RETURNS\n *\n", cfile);
556 if (sym->return_text && !strcmp (sym->return_text, "void"))
557 fputs (" * Nothing.\n", cfile);
559 fprintf (cfile, " * %s\n", sym->return_text);
561 fputs (" *\n */\n", cfile);
565 /*******************************************************************
568 * Get a string containing the correct format string for a type
570 static const char *get_format_str (int type)
574 case ARG_VOID: return "void";
575 case ARG_FLOAT: return "%f";
576 case ARG_DOUBLE: return "%g";
577 case ARG_POINTER: return "%p";
578 case ARG_WIDE_STRING:
579 case ARG_STRING: return "%s";
580 case ARG_LONG: return "%ld";
581 case ARG_STRUCT: return "struct";
588 /*******************************************************************
591 * Determine if a parameter is In or In/Out
593 static const char *get_in_or_out (const parsed_symbol *sym, size_t arg)
595 assert (sym && arg < sym->argc);
596 assert (globals.do_documentation);
598 if (sym->arg_flag [arg] & CT_CONST)
601 switch (sym->arg_type [arg])
606 case ARG_STRUCT: return "In";
608 case ARG_WIDE_STRING:
609 case ARG_STRING: return "In/Out";