Added some flex options to avoid compiler warnings.
[wine] / tools / winedump / output.c
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include "winedump.h"
25
26 /* Output files */
27 static FILE *specfile = NULL;
28 static FILE *hfile    = NULL;
29 static FILE *cfile    = NULL;
30
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);
37
38
39 /*******************************************************************
40  *         output_spec_preamble
41  *
42  * Write the first part of the .spec file
43  */
44 void  output_spec_preamble (void)
45 {
46   specfile = open_file (OUTPUT_DLL_NAME, ".spec", "w");
47
48   atexit (output_spec_postamble);
49
50   if (VERBOSE)
51     puts ("Creating .spec preamble");
52
53   fprintf (specfile,
54            "# Generated from %s by winedump\n\n", globals.input_name);
55 }
56
57
58 /*******************************************************************
59  *         output_spec_symbol
60  *
61  * Write a symbol to the .spec file
62  */
63 void  output_spec_symbol (const parsed_symbol *sym)
64 {
65   char ord_spec[16];
66
67   assert (specfile);
68   assert (sym && sym->symbol);
69
70   if (sym->ordinal >= 0)
71     snprintf(ord_spec, 8, "%d", sym->ordinal);
72   else
73   {
74     ord_spec[0] = '@';
75     ord_spec[1] = '\0';
76   }
77   if (sym->flags & SYM_THISCALL)
78     strcat (ord_spec, " -i386"); /* For binary compatibility only */
79
80   if (!globals.do_code || !sym->function_name)
81   {
82     if (sym->flags & SYM_DATA)
83     {
84       if (globals.forward_dll)
85         fprintf (specfile, "%s forward %s %s.%s #", ord_spec, sym->symbol,
86                  globals.forward_dll, sym->symbol);
87
88       fprintf (specfile, "%s extern %s %s\n", ord_spec, sym->symbol,
89                sym->arg_name[0]);
90       return;
91     }
92
93     if (globals.forward_dll)
94       fprintf (specfile, "%s forward %s %s.%s\n", ord_spec, sym->symbol,
95                globals.forward_dll, sym->symbol);
96     else
97       fprintf (specfile, "%s stub %s\n", ord_spec, sym->symbol);
98   }
99   else
100   {
101     unsigned int i = sym->flags & SYM_THISCALL ? 1 : 0;
102
103     fprintf (specfile, "%s %s %s(", ord_spec, sym->varargs ? "varargs" :
104              symbol_get_call_convention(sym), sym->symbol);
105
106     for (; i < sym->argc; i++)
107       fprintf (specfile, " %s", symbol_get_spec_type(sym, i));
108
109     if (sym->argc)
110       fputc (' ', specfile);
111     fprintf (specfile, ") %s_%s", OUTPUT_UC_DLL_NAME, sym->function_name);
112
113     if (sym->flags & SYM_THISCALL)
114       fputs (" # __thiscall", specfile);
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   hfile = open_file (OUTPUT_DLL_NAME, "_dll.h", "w");
142
143   atexit (output_header_postamble);
144
145   fprintf (hfile,
146            "/*\n * %s.dll\n *\n * Generated from %s.dll by winedump.\n *\n"
147            " * DO NOT SEND GENERATED DLLS FOR INCLUSION INTO WINE !\n * \n */"
148            "\n#ifndef __WINE_%s_DLL_H\n#define __WINE_%s_DLL_H\n\n#include "
149            "\"config.h\"\n#include \"windef.h\"\n#include \"wine/debug.h\"\n"
150            "#include \"winbase.h\"\n#include \"winnt.h\"\n\n\n",
151            OUTPUT_DLL_NAME, OUTPUT_DLL_NAME, OUTPUT_UC_DLL_NAME,
152            OUTPUT_UC_DLL_NAME);
153 }
154
155
156 /*******************************************************************
157  *         output_header_symbol
158  *
159  * Write a symbol to the .h file
160  */
161 void  output_header_symbol (const parsed_symbol *sym)
162 {
163   assert (hfile);
164   assert (sym && sym->symbol);
165
166   if (!globals.do_code)
167     return;
168
169   if (sym->flags & SYM_DATA)
170     return;
171
172   if (!sym->function_name)
173     fprintf (hfile, "/* __%s %s_%s(); */\n", symbol_get_call_convention(sym),
174              OUTPUT_UC_DLL_NAME, sym->symbol);
175   else
176   {
177     output_prototype (hfile, sym);
178     fputs (";\n", hfile);
179   }
180 }
181
182
183 /*******************************************************************
184  *         output_header_postamble
185  *
186  * Write the last part of the .h file
187  */
188 static void output_header_postamble (void)
189 {
190   if (hfile)
191   {
192     fprintf (hfile, "\n\n\n#endif\t/* __WINE_%s_DLL_H */\n",
193              OUTPUT_UC_DLL_NAME);
194     fclose (hfile);
195     hfile = NULL;
196   }
197 }
198
199
200 /*******************************************************************
201  *         output_c_preamble
202  *
203  * Write the first part of the .c file
204  */
205 void  output_c_preamble (void)
206 {
207   cfile = open_file (OUTPUT_DLL_NAME, "_main.c", "w");
208
209   atexit (output_c_postamble);
210
211   fprintf (cfile,
212            "/*\n * %s.dll\n *\n * Generated from %s by winedump.\n *\n"
213            " * DO NOT SUBMIT GENERATED DLLS FOR INCLUSION INTO WINE!\n * \n */"
214            "\n\n#include \"%s_dll.h\"\n\nWINE_DEFAULT_DEBUG_CHANNEL(%s);\n\n",
215            OUTPUT_DLL_NAME, globals.input_name, OUTPUT_DLL_NAME,
216            OUTPUT_DLL_NAME);
217
218   if (globals.forward_dll)
219   {
220     if (VERBOSE)
221       puts ("Creating a forwarding DLL");
222
223     fputs ("\nHMODULE hDLL=0;\t/* DLL to call */\n\n", cfile);
224   }
225
226   fputs ("#ifdef __i386__\n#define GET_THIS(t,p) t p;\\\n__asm__ __volatile__"
227          " (\"movl %%ecx, %0\" : \"=m\" (p))\n#endif\n\n\n", cfile);
228
229   fprintf (cfile,
230            "BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID "
231            "lpvReserved)\n{\n\tTRACE(\"(0x%%p, %%ld, %%p)\\n\",hinstDLL,"
232            "fdwReason,lpvReserved);\n\n\t"
233            "if (fdwReason == DLL_PROCESS_ATTACH)\n\t{\n\t\t");
234
235   if (globals.forward_dll)
236   {
237     fprintf (cfile,
238              "hDLL = LoadLibraryA( \"%s\" );\n\t\t"
239              "TRACE(\":Forwarding DLL (%s) loaded (%%ld)\\n\",(LONG)hDLL);",
240              globals.forward_dll, globals.forward_dll);
241   }
242   else
243     fputs ("/* FIXME: Initialisation */", cfile);
244
245   fputs ("\n\t}\n\telse if (fdwReason == DLL_PROCESS_DETACH)\n\t{\n\t\t",
246          cfile);
247
248   if (globals.forward_dll)
249   {
250     fprintf (cfile,
251              "FreeLibrary( hDLL );\n\t\tTRACE(\":Forwarding DLL (%s)"
252              " freed\\n\");", globals.forward_dll);
253   }
254   else
255     fputs ("/* FIXME: Cleanup */", cfile);
256
257   fputs ("\n\t}\n\n\treturn TRUE;\n}\n\n\n", cfile);
258 }
259
260
261 #define CPP_END  if (sym->flags & SYM_THISCALL) \
262   fputs ("#endif\n", cfile); fputs ("\n\n", cfile)
263 #define GET_THIS if (sym->flags & SYM_THISCALL) \
264   fprintf (cfile, "\tGET_THIS(%s,%s);\n", sym->arg_text[0],sym->arg_name[0])
265
266 /*******************************************************************
267  *         output_c_symbol
268  *
269  * Write a symbol to the .c file
270  */
271 void  output_c_symbol (const parsed_symbol *sym)
272 {
273   unsigned int i, start = sym->flags & SYM_THISCALL ? 1 : 0;
274   int is_void;
275
276   assert (cfile);
277   assert (sym && sym->symbol);
278
279   if (!globals.do_code)
280     return;
281
282   if (sym->flags & SYM_DATA)
283   {
284     fprintf (cfile, "/* FIXME: Move to top of file */\n%s;\n\n",
285              sym->arg_text[0]);
286     return;
287   }
288
289   if (sym->flags & SYM_THISCALL)
290     fputs ("#ifdef __i386__\n", cfile);
291
292   output_c_banner(sym);
293
294   if (!sym->function_name)
295   {
296     /* #ifdef'd dummy */
297     fprintf (cfile, "#if 0\n__%s %s_%s()\n{\n\t/* %s in .spec */\n}\n#endif\n",
298              symbol_get_call_convention(sym), OUTPUT_UC_DLL_NAME, sym->symbol,
299              globals.forward_dll ? "@forward" : "@stub");
300     CPP_END;
301     return;
302   }
303
304   is_void = !strcmp (sym->return_text, "void");
305
306   output_prototype (cfile, sym);
307   fputs ("\n{\n", cfile);
308
309   if (!globals.do_trace)
310   {
311     GET_THIS;
312     fputs ("\tFIXME(\":stub\\n\");\n", cfile);
313     if (!is_void)
314         fprintf (cfile, "\treturn (%s) 0;\n", sym->return_text);
315     fputs ("}\n", cfile);
316     CPP_END;
317     return;
318   }
319
320   /* Tracing, maybe forwarding as well */
321   if (globals.forward_dll)
322   {
323     /* Write variables for calling */
324     if (sym->varargs)
325       fputs("\tva_list valist;\n", cfile);
326
327     fprintf (cfile, "\t%s (__%s *pFunc)(", sym->return_text,
328              symbol_get_call_convention(sym));
329
330     for (i = start; i < sym->argc; i++)
331       fprintf (cfile, "%s%s", i > start ? ", " : "", sym->arg_text [i]);
332
333     fprintf (cfile, "%s);\n", sym->varargs ? ",..." : sym->argc == 1 &&
334              sym->flags & SYM_THISCALL ? "" : sym->argc ? "" : "void");
335
336     if (!is_void)
337       fprintf (cfile, "\t%s retVal;\n", sym->return_text);
338
339     GET_THIS;
340
341     fprintf (cfile, "\tpFunc=(void*)GetProcAddress(hDLL,\"%s\");\n",
342              sym->symbol);
343   }
344
345   /* TRACE input arguments */
346   fprintf (cfile, "\tTRACE(\"(%s", !sym->argc ? "void" : "");
347
348   for (i = 0; i < sym->argc; i++)
349     fprintf (cfile, "%s(%s)%s", i ? "," : "", sym->arg_text [i],
350              get_format_str (sym->arg_type [i]));
351
352   fprintf (cfile, "%s): %s\\n\"", sym->varargs ? ",..." : "",
353            globals.forward_dll ? "forward" : "stub");
354
355   for (i = 0; i < sym->argc; i++)
356     if (sym->arg_type[i] != ARG_STRUCT)
357       fprintf(cfile, ",%s%s%s%s", sym->arg_type[i] == ARG_LONG ? "(LONG)" : "",
358               sym->arg_type[i] == ARG_WIDE_STRING ? "debugstr_w(" : "",
359               sym->arg_name[i],
360               sym->arg_type[i] == ARG_WIDE_STRING ? ")" : "");
361
362   fputs (");\n", cfile);
363
364   if (!globals.forward_dll)
365   {
366     if (!is_void)
367       fprintf (cfile, "\treturn (%s) 0;\n", sym->return_text);
368     fputs ("}\n", cfile);
369     CPP_END;
370     return;
371   }
372
373   /* Call the DLL */
374   if (sym->varargs)
375     fprintf (cfile, "\tva_start(valist,%s);\n", sym->arg_name[sym->argc-1]);
376
377   fprintf (cfile, "\t%spFunc(", !is_void ? "retVal = " : "");
378
379   for (i = 0; i < sym->argc; i++)
380     fprintf (cfile, "%s%s", i ? "," : "", sym->arg_name [i]);
381
382   fputs (sym->varargs ? ",valist);\n\tva_end(valist);" : ");", cfile);
383
384   /* TRACE return value */
385   fprintf (cfile, "\n\tTRACE(\"Returned (%s)\\n\"",
386            get_format_str (sym->return_type));
387
388   if (!is_void)
389   {
390     if (sym->return_type == ARG_WIDE_STRING)
391       fputs (",debugstr_w(retVal)", cfile);
392     else
393       fprintf (cfile, ",%s%s", sym->return_type == ARG_LONG ? "(LONG)" : "",
394                sym->return_type == ARG_STRUCT ? "" : "retVal");
395     fputs (");\n\treturn retVal;\n", cfile);
396   }
397   else
398     fputs (");\n", cfile);
399
400   fputs ("}\n", cfile);
401   CPP_END;
402 }
403
404
405 /*******************************************************************
406  *         output_c_postamble
407  *
408  * Write the last part of the .c file
409  */
410 static void output_c_postamble (void)
411 {
412   if (cfile)
413     fclose (cfile);
414   cfile = NULL;
415 }
416
417
418 /*******************************************************************
419  *         output_makefile
420  *
421  * Write a Wine compatible makefile.in
422  */
423 void  output_makefile (void)
424 {
425   FILE *makefile = open_file ("Makefile", ".in", "w");
426
427   if (VERBOSE)
428     puts ("Creating makefile");
429
430   fprintf (makefile,
431            "# Generated from %s by winedump.\nTOPSRCDIR = @top_srcdir@\n"
432            "TOPOBJDIR = ../..\nSRCDIR    = @srcdir@\nVPATH     = @srcdir@\n"
433            "MODULE    = %s.dll\n", globals.input_name, OUTPUT_DLL_NAME);
434
435   fprintf (makefile, "IMPORTS   = kernel32");
436   if (globals.forward_dll)
437     fprintf (makefile, " %s", globals.forward_dll);
438
439   fprintf (makefile,
440            "\n\nC_SRCS = \\\n\t%s_main.c\n\n@MAKE_DLL_RULES@\n\n### Dependencies:",
441            OUTPUT_DLL_NAME);
442
443   if (globals.forward_dll)
444     fprintf (specfile,"#import %s.dll\n", globals.forward_dll);
445
446   fclose (makefile);
447 }
448
449
450 /*******************************************************************
451  *         output_install_script
452  *
453  * Write a script to insert the DLL into Wine
454  *
455  * Rather than using diff/patch, several sed calls are generated
456  * so the script can be re-run at any time without breaking.
457  */
458 void  output_install_script (void)
459 {
460   char cmd[128];
461   FILE *install_file = open_file (OUTPUT_DLL_NAME, "_install", "w");
462
463   if (VERBOSE)
464     puts ("Creating install script");
465
466   fprintf (install_file,
467     "#!/bin/bash\n"
468     "# Generated from %s.dll by winedump.\n\n"
469     "if [ $# -ne 1 ] || [ ! -d $1 ] || [ ! -f $1/AUTHORS ]; then\n"
470     "\t[ $# -eq 1 ] && echo \"Invalid path\"\n"
471     "\techo \"Usage: $0 wine-base-dir\"\n"
472     "\texit 1\n"
473     "fi\n\n"
474     "if [ -d $1/dlls/%s ]; then\n"
475     "\techo \"DLL is already present\"\n"
476     "\texit 1\n"
477     "fi\n\n"
478     "echo Adding DLL %s to Wine build tree...\n\n"
479     "mkdir $1/dlls/%s\n"
480     "cp %s.spec $1/dlls/%s\n"
481     "cp %s_main.c $1/dlls/%s\n"
482     "cp %s_dll.h $1/dlls/%s\n"
483     "cp Makefile.in $1/dlls/%s/Makefile.in\n"
484     "echo Copied DLL files\n\n"
485     "cd $1\n\n"
486     "sed '/dlls\\/x11drv\\/Makefile/"
487         "{G;s/$/dlls\\/%s\\/Makefile/;}' configure.ac >t.tmp\n"
488     "mv -f t.tmp configure.ac\n"
489     "echo Patched configure.ac\n\n"
490     "cd $1/dlls && ./make_dlls\n\n"
491     "echo Run \\'autoconf\\', \\'./configure\\' then \\'make\\' to rebuild Wine\n\n",
492            OUTPUT_DLL_NAME, OUTPUT_DLL_NAME, OUTPUT_DLL_NAME, OUTPUT_DLL_NAME,
493            OUTPUT_DLL_NAME, OUTPUT_DLL_NAME, OUTPUT_DLL_NAME, OUTPUT_DLL_NAME,
494            OUTPUT_DLL_NAME, OUTPUT_DLL_NAME, OUTPUT_DLL_NAME, OUTPUT_DLL_NAME);
495
496   fclose (install_file);
497   snprintf (cmd, sizeof (cmd), "chmod a+x %s_install", OUTPUT_DLL_NAME);
498   system (cmd);
499 }
500
501
502 /*******************************************************************
503  *         output_prototype
504  *
505  * Write a C prototype for a parsed symbol
506  */
507 void  output_prototype (FILE *file, const parsed_symbol *sym)
508 {
509   unsigned int i, start = sym->flags & SYM_THISCALL ? 1 : 0;
510
511   fprintf (file, "%s __%s %s_%s(", sym->return_text, symbol_get_call_convention(sym),
512            OUTPUT_UC_DLL_NAME, sym->function_name);
513
514   if (!sym->argc || (sym->argc == 1 && sym->flags & SYM_THISCALL))
515     fputs ("void", file);
516   else
517     for (i = start; i < sym->argc; i++)
518       fprintf (file, "%s%s %s", i > start ? ", " : "", sym->arg_text [i],
519                sym->arg_name [i]);
520   if (sym->varargs)
521     fputs (", ...", file);
522   fputc (')', file);
523 }
524
525
526 /*******************************************************************
527  *         output_c_banner
528  *
529  * Write a function banner to the .c file
530  */
531 void  output_c_banner (const parsed_symbol *sym)
532 {
533   char ord_spec[16];
534   size_t i;
535
536   if (sym->ordinal >= 0)
537     snprintf(ord_spec, sizeof (ord_spec), "%d", sym->ordinal);
538   else
539   {
540     ord_spec[0] = '@';
541     ord_spec[1] = '\0';
542   }
543
544   fprintf (cfile, "/*********************************************************"
545            "*********\n *\t\t%s (%s.%s)\n *\n", sym->symbol,
546            OUTPUT_UC_DLL_NAME, ord_spec);
547
548   if (globals.do_documentation && sym->function_name)
549   {
550     fputs (" *\n * PARAMS\n *\n", cfile);
551
552     if (!sym->argc)
553       fputs (" *  None.\n *\n", cfile);
554     else
555     {
556       for (i = 0; i < sym->argc; i++)
557         fprintf (cfile, " *  %s [%s]%s\n", sym->arg_name [i],
558                  get_in_or_out(sym, i),
559                  strcmp (sym->arg_name [i], "_this") ? "" :
560                  "     Pointer to the class object (in ECX)");
561
562       if (sym->varargs)
563         fputs (" *  ...[I]\n", cfile);
564       fputs (" *\n", cfile);
565     }
566
567     fputs (" * RETURNS\n *\n", cfile);
568
569     if (sym->return_text && !strcmp (sym->return_text, "void"))
570       fputs (" *  Nothing.\n", cfile);
571     else
572       fprintf (cfile, " *  %s\n", sym->return_text);
573   }
574   fputs (" *\n */\n", cfile);
575 }
576
577
578 /*******************************************************************
579  *         get_format_str
580  *
581  * Get a string containing the correct format string for a type
582  */
583 static const char *get_format_str (int type)
584 {
585   switch (type)
586   {
587   case ARG_VOID:        return "void";
588   case ARG_FLOAT:       return "%f";
589   case ARG_DOUBLE:      return "%g";
590   case ARG_POINTER:     return "%p";
591   case ARG_WIDE_STRING:
592   case ARG_STRING:      return "%s";
593   case ARG_LONG:        return "%ld";
594   case ARG_STRUCT:      return "struct";
595   }
596   assert (0);
597   return "";
598 }
599
600
601 /*******************************************************************
602  *         get_in_or_out
603  *
604  * Determine if a parameter is In or In/Out
605  */
606 static const char *get_in_or_out (const parsed_symbol *sym, size_t arg)
607 {
608   assert (sym && arg < sym->argc);
609   assert (globals.do_documentation);
610
611   if (sym->arg_flag [arg] & CT_CONST)
612     return "In";
613
614   switch (sym->arg_type [arg])
615   {
616   case ARG_FLOAT:
617   case ARG_DOUBLE:
618   case ARG_LONG:
619   case ARG_STRUCT:      return "In";
620   case ARG_POINTER:
621   case ARG_WIDE_STRING:
622   case ARG_STRING:      return "In/Out";
623   }
624   assert (0);
625   return "";
626 }