richedit: EN_UPDATE notification is sent on WM_PAINT.
[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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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     fputs (") ", specfile);
112
113     if (sym->flags & SYM_THISCALL)
114       fputs ("__thiscall_", specfile);
115     fprintf (specfile, "%s_%s", OUTPUT_UC_DLL_NAME, sym->function_name);
116
117     fputc ('\n',specfile);
118   }
119 }
120
121
122 /*******************************************************************
123  *         output_spec_postamble
124  *
125  * Write the last part of the .spec file
126  */
127 static void output_spec_postamble (void)
128 {
129   if (specfile)
130     fclose (specfile);
131   specfile = NULL;
132 }
133
134
135 /*******************************************************************
136  *         output_header_preamble
137  *
138  * Write the first part of the .h file
139  */
140 void  output_header_preamble (void)
141 {
142   if (!globals.do_code)
143       return;
144
145   hfile = open_file (OUTPUT_DLL_NAME, "_dll.h", "w");
146
147   atexit (output_header_postamble);
148
149   fprintf (hfile,
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,
156            OUTPUT_UC_DLL_NAME);
157 }
158
159
160 /*******************************************************************
161  *         output_header_symbol
162  *
163  * Write a symbol to the .h file
164  */
165 void  output_header_symbol (const parsed_symbol *sym)
166 {
167   if (!globals.do_code)
168       return;
169
170   assert (hfile);
171   assert (sym && sym->symbol);
172
173   if (!globals.do_code)
174     return;
175
176   if (sym->flags & SYM_DATA)
177     return;
178
179   if (!sym->function_name)
180     fprintf (hfile, "/* __%s %s_%s(); */\n", symbol_get_call_convention(sym),
181              OUTPUT_UC_DLL_NAME, sym->symbol);
182   else
183   {
184     output_prototype (hfile, sym);
185     fputs (";\n", hfile);
186   }
187 }
188
189
190 /*******************************************************************
191  *         output_header_postamble
192  *
193  * Write the last part of the .h file
194  */
195 static void output_header_postamble (void)
196 {
197   if (hfile)
198   {
199     fprintf (hfile, "\n\n\n#endif\t/* __WINE_%s_DLL_H */\n",
200              OUTPUT_UC_DLL_NAME);
201     fclose (hfile);
202     hfile = NULL;
203   }
204 }
205
206
207 /*******************************************************************
208  *         output_c_preamble
209  *
210  * Write the first part of the .c file
211  */
212 void  output_c_preamble (void)
213 {
214   cfile = open_file (OUTPUT_DLL_NAME, "_main.c", "w");
215
216   atexit (output_c_postamble);
217
218   fprintf (cfile,
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);
224
225   if (globals.do_code)
226     fprintf (cfile, "#include \"%s_dll.h\"\n", OUTPUT_DLL_NAME);
227
228   fprintf (cfile,"#include \"wine/debug.h\"\n\nWINE_DEFAULT_DEBUG_CHANNEL(%s);\n\n",
229            OUTPUT_DLL_NAME);
230
231   if (globals.forward_dll)
232   {
233     if (VERBOSE)
234       puts ("Creating a forwarding DLL");
235
236     fputs ("\nHMODULE hDLL=0;    /* DLL to call */\n\n", cfile);
237   }
238
239   fprintf (cfile,
240            "BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID "
241            "lpvReserved)\n{\n"
242            "    TRACE(\"(0x%%p, %%d, %%p)\\n\", hinstDLL, fdwReason, lpvReserved);\n\n"
243            "    switch (fdwReason)\n"
244            "    {\n"
245            "        case DLL_WINE_PREATTACH:\n"
246            "            return FALSE;    /* prefer native version */\n"
247            "        case DLL_PROCESS_ATTACH:\n");
248
249   if (globals.forward_dll)
250     fprintf (cfile,
251            "            DLL = LoadLibraryA(\"%s\");\n"
252            "            TRACE(\"Forwarding DLL (%s) loaded (%%p)\\n\", hDLL);\n",
253            globals.forward_dll, globals.forward_dll);
254   else
255     fprintf (cfile,
256            "            DisableThreadLibraryCalls(hinstDLL);\n");
257
258   fprintf (cfile,
259            "            break;\n"
260            "        case DLL_PROCESS_DETACH:\n");
261
262   if (globals.forward_dll)
263     fprintf (cfile,
264            "            FreeLibrary(hDLL);\n"
265            "            TRACE(\"Forwarding DLL (%s) freed\\n\");\n",
266            globals.forward_dll);
267
268   fprintf (cfile,
269            "            break;\n"
270            "    }\n\n"
271            "    return TRUE;\n}\n");
272 }
273
274
275 /*******************************************************************
276  *         output_c_symbol
277  *
278  * Write a symbol to the .c file
279  */
280 void  output_c_symbol (const parsed_symbol *sym)
281 {
282   unsigned int i, start = sym->flags & SYM_THISCALL ? 1 : 0;
283   int is_void;
284   static int has_thiscall = 0;
285
286   assert (cfile);
287   assert (sym && sym->symbol);
288
289   if (!globals.do_code)
290     return;
291
292   if (sym->flags & SYM_DATA)
293   {
294     fprintf (cfile, "/* FIXME: Move to top of file */\n%s;\n\n",
295              sym->arg_text[0]);
296     return;
297   }
298
299   if (sym->flags & SYM_THISCALL && !has_thiscall)
300   {
301     fputs ("#ifdef __i386__  /* thiscall functions are i386-specific */\n\n"
302            "#define THISCALL(func) __thiscall_ ## func\n"
303            "#define THISCALL_NAME(func) __ASM_NAME(\"__thiscall_\" #func)\n"
304            "#define DEFINE_THISCALL_WRAPPER(func) \\\n"
305            "\textern void THISCALL(func)(); \\\n"
306            "\t__ASM_GLOBAL_FUNC(__thiscall_ ## func, \\\n"
307            "\t\t\t\"popl %eax\\n\\t\" \\\n"
308            "\t\t\t\"pushl %ecx\\n\\t\" \\\n"
309            "\t\t\t\"pushl %eax\\n\\t\" \\\n"
310            "\t\t\t\"jmp \" __ASM_NAME(#func) )\n"
311            "#else /* __i386__ */\n\n"
312            "#define THISCALL(func) func\n"
313            "#define THISCALL_NAME(func) __ASM_NAME(#func)\n"
314            "#define DEFINE_THISCALL_WRAPPER(func) /* nothing */\n\n"
315            "#endif /* __i386__ */\n\n", cfile);
316     has_thiscall = 1;
317   }
318
319   output_c_banner(sym);
320
321   if (sym->flags & SYM_THISCALL)
322     fprintf(cfile, "DEFINE_THISCALL_WRAPPER(%s)\n", sym->function_name);
323
324   if (!sym->function_name)
325   {
326     /* #ifdef'd dummy */
327     fprintf (cfile, "#if 0\n__%s %s_%s()\n{\n\t/* %s in .spec */\n}\n#endif\n",
328              symbol_get_call_convention(sym), OUTPUT_UC_DLL_NAME, sym->symbol,
329              globals.forward_dll ? "@forward" : "@stub");
330     return;
331   }
332
333   is_void = !strcmp (sym->return_text, "void");
334
335   output_prototype (cfile, sym);
336   fputs ("\n{\n", cfile);
337
338   if (!globals.do_trace)
339   {
340     fputs ("\tFIXME(\":stub\\n\");\n", cfile);
341     if (!is_void)
342         fprintf (cfile, "\treturn (%s) 0;\n", sym->return_text);
343     fputs ("}\n", cfile);
344     return;
345   }
346
347   /* Tracing, maybe forwarding as well */
348   if (globals.forward_dll)
349   {
350     /* Write variables for calling */
351     if (sym->varargs)
352       fputs("\tva_list valist;\n", cfile);
353
354     fprintf (cfile, "\t%s (__%s *pFunc)(", sym->return_text,
355              symbol_get_call_convention(sym));
356
357     for (i = start; i < sym->argc; i++)
358       fprintf (cfile, "%s%s", i > start ? ", " : "", sym->arg_text [i]);
359
360     fprintf (cfile, "%s);\n", sym->varargs ? ",..." : sym->argc == 1 &&
361              sym->flags & SYM_THISCALL ? "" : sym->argc ? "" : "void");
362
363     if (!is_void)
364       fprintf (cfile, "\t%s retVal;\n", sym->return_text);
365
366     fprintf (cfile, "\tpFunc=(void*)GetProcAddress(hDLL,\"%s\");\n",
367              sym->symbol);
368   }
369
370   /* TRACE input arguments */
371   fprintf (cfile, "\tTRACE(\"(%s", !sym->argc ? "void" : "");
372
373   for (i = 0; i < sym->argc; i++)
374     fprintf (cfile, "%s(%s)%s", i ? "," : "", sym->arg_text [i],
375              get_format_str (sym->arg_type [i]));
376
377   fprintf (cfile, "%s): %s\\n\"", sym->varargs ? ",..." : "",
378            globals.forward_dll ? "forward" : "stub");
379
380   for (i = 0; i < sym->argc; i++)
381     if (sym->arg_type[i] != ARG_STRUCT)
382       fprintf(cfile, ",%s%s%s%s", sym->arg_type[i] == ARG_LONG ? "(LONG)" : "",
383               sym->arg_type[i] == ARG_WIDE_STRING ? "debugstr_w(" : "",
384               sym->arg_name[i],
385               sym->arg_type[i] == ARG_WIDE_STRING ? ")" : "");
386
387   fputs (");\n", cfile);
388
389   if (!globals.forward_dll)
390   {
391     if (!is_void)
392       fprintf (cfile, "\treturn (%s) 0;\n", sym->return_text);
393     fputs ("}\n", cfile);
394     return;
395   }
396
397   /* Call the DLL */
398   if (sym->varargs)
399     fprintf (cfile, "\tva_start(valist,%s);\n", sym->arg_name[sym->argc-1]);
400
401   fprintf (cfile, "\t%spFunc(", !is_void ? "retVal = " : "");
402
403   for (i = 0; i < sym->argc; i++)
404     fprintf (cfile, "%s%s", i ? "," : "", sym->arg_name [i]);
405
406   fputs (sym->varargs ? ",valist);\n\tva_end(valist);" : ");", cfile);
407
408   /* TRACE return value */
409   fprintf (cfile, "\n\tTRACE(\"Returned (%s)\\n\"",
410            get_format_str (sym->return_type));
411
412   if (!is_void)
413   {
414     if (sym->return_type == ARG_WIDE_STRING)
415       fputs (",debugstr_w(retVal)", cfile);
416     else
417       fprintf (cfile, ",%s%s", sym->return_type == ARG_LONG ? "(LONG)" : "",
418                sym->return_type == ARG_STRUCT ? "" : "retVal");
419     fputs (");\n\treturn retVal;\n", cfile);
420   }
421   else
422     fputs (");\n", cfile);
423
424   fputs ("}\n", cfile);
425 }
426
427
428 /*******************************************************************
429  *         output_c_postamble
430  *
431  * Write the last part of the .c file
432  */
433 static void output_c_postamble (void)
434 {
435   if (cfile)
436     fclose (cfile);
437   cfile = NULL;
438 }
439
440
441 /*******************************************************************
442  *         output_makefile
443  *
444  * Write a Wine compatible makefile.in
445  */
446 void  output_makefile (void)
447 {
448   FILE *makefile = open_file ("Makefile", ".in", "w");
449
450   if (VERBOSE)
451     puts ("Creating makefile");
452
453   fprintf (makefile,
454            "# Generated from %s by winedump.\nTOPSRCDIR = @top_srcdir@\n"
455            "TOPOBJDIR = ../..\nSRCDIR    = @srcdir@\nVPATH     = @srcdir@\n"
456            "MODULE    = %s.dll\n", globals.input_name, OUTPUT_DLL_NAME);
457
458   fprintf (makefile, "IMPORTS   = kernel32");
459   if (globals.forward_dll)
460     fprintf (makefile, " %s", globals.forward_dll);
461
462   fprintf (makefile, "\n\nC_SRCS = \\\n\t%s_main.c\n\n@MAKE_DLL_RULES@\n\n",
463            OUTPUT_DLL_NAME);
464
465   if (globals.forward_dll)
466     fprintf (specfile,"#import %s.dll\n", globals.forward_dll);
467
468   fclose (makefile);
469 }
470
471
472 /*******************************************************************
473  *         output_prototype
474  *
475  * Write a C prototype for a parsed symbol
476  */
477 void  output_prototype (FILE *file, const parsed_symbol *sym)
478 {
479   unsigned int i, start = sym->flags & SYM_THISCALL ? 1 : 0;
480
481   fprintf (file, "%s __%s %s_%s(", sym->return_text, symbol_get_call_convention(sym),
482            OUTPUT_UC_DLL_NAME, sym->function_name);
483
484   if (!sym->argc || (sym->argc == 1 && sym->flags & SYM_THISCALL))
485     fputs ("void", file);
486   else
487     for (i = start; i < sym->argc; i++)
488       fprintf (file, "%s%s %s", i > start ? ", " : "", sym->arg_text [i],
489                sym->arg_name [i]);
490   if (sym->varargs)
491     fputs (", ...", file);
492   fputc (')', file);
493 }
494
495
496 /*******************************************************************
497  *         output_c_banner
498  *
499  * Write a function banner to the .c file
500  */
501 void  output_c_banner (const parsed_symbol *sym)
502 {
503   char ord_spec[16];
504   size_t i;
505
506   if (sym->ordinal >= 0)
507     snprintf(ord_spec, sizeof (ord_spec), "%d", sym->ordinal);
508   else
509   {
510     ord_spec[0] = '@';
511     ord_spec[1] = '\0';
512   }
513
514   fprintf (cfile, "/*********************************************************"
515            "*********\n *\t\t%s (%s.%s)\n *\n", sym->symbol,
516            OUTPUT_UC_DLL_NAME, ord_spec);
517
518   if (globals.do_documentation && sym->function_name)
519   {
520     fputs (" *\n * PARAMS\n *\n", cfile);
521
522     if (!sym->argc)
523       fputs (" *  None.\n *\n", cfile);
524     else
525     {
526       for (i = 0; i < sym->argc; i++)
527         fprintf (cfile, " *  %s [%s]%s\n", sym->arg_name [i],
528                  get_in_or_out(sym, i),
529                  strcmp (sym->arg_name [i], "_this") ? "" :
530                  "     Pointer to the class object (in ECX)");
531
532       if (sym->varargs)
533         fputs (" *  ...[I]\n", cfile);
534       fputs (" *\n", cfile);
535     }
536
537     fputs (" * RETURNS\n *\n", cfile);
538
539     if (sym->return_text && !strcmp (sym->return_text, "void"))
540       fputs (" *  Nothing.\n", cfile);
541     else
542       fprintf (cfile, " *  %s\n", sym->return_text);
543   }
544   fputs (" *\n */\n", cfile);
545 }
546
547
548 /*******************************************************************
549  *         get_format_str
550  *
551  * Get a string containing the correct format string for a type
552  */
553 static const char *get_format_str (int type)
554 {
555   switch (type)
556   {
557   case ARG_VOID:        return "void";
558   case ARG_FLOAT:       return "%f";
559   case ARG_DOUBLE:      return "%g";
560   case ARG_POINTER:     return "%p";
561   case ARG_WIDE_STRING:
562   case ARG_STRING:      return "%s";
563   case ARG_LONG:        return "%ld";
564   case ARG_STRUCT:      return "struct";
565   }
566   assert (0);
567   return "";
568 }
569
570
571 /*******************************************************************
572  *         get_in_or_out
573  *
574  * Determine if a parameter is In or In/Out
575  */
576 static const char *get_in_or_out (const parsed_symbol *sym, size_t arg)
577 {
578   assert (sym && arg < sym->argc);
579   assert (globals.do_documentation);
580
581   if (sym->arg_flag [arg] & CT_CONST)
582     return "In";
583
584   switch (sym->arg_type [arg])
585   {
586   case ARG_FLOAT:
587   case ARG_DOUBLE:
588   case ARG_LONG:
589   case ARG_STRUCT:      return "In";
590   case ARG_POINTER:
591   case ARG_WIDE_STRING:
592   case ARG_STRING:      return "In/Out";
593   }
594   assert (0);
595   return "";
596 }