Added first version of the Perl regression testing framework.
[wine/multimedia.git] / tools / specmaker / output.c
blob0513cece17b6652190f9c3c73b02905ea03b2498
1 /*
2 * Code generation functions
4 * Copyright 2000 Jon Griffiths
5 */
6 #include "specmaker.h"
8 /* Output files */
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);
32 if (VERBOSE)
33 puts ("Creating .spec preamble");
35 fprintf (specfile,
36 "# Generated from %s.dll by specmaker\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,
39 OUTPUT_UC_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 /*******************************************************************
49 * output_spec_symbol
51 * Write a symbol to the .spec file
53 void output_spec_symbol (const parsed_symbol *sym)
55 char ord_spec[16];
57 assert (specfile);
58 assert (sym && sym->symbol);
60 if (globals.do_ordinals)
61 snprintf(ord_spec, 8, "%d", sym->ordinal);
62 else
64 ord_spec[0] = '@';
65 ord_spec[1] = '\0';
67 if (sym->flags & SYM_THISCALL)
68 strcat (ord_spec, " -i386"); /* For binary compatability 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,
79 sym->arg_name[0]);
80 return;
83 if (globals.forward_dll)
84 fprintf (specfile, "%s forward %s %s.%s\n", ord_spec, sym->symbol,
85 globals.forward_dll, sym->symbol);
86 else
87 fprintf (specfile, "%s stub %s\n", ord_spec, sym->symbol);
89 else
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));
99 if (sym->argc)
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)
118 if (specfile)
119 fclose (specfile);
120 specfile = NULL;
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);
135 fprintf (hfile,
136 "/*\n * %s.dll\n *\n * Generated from %s.dll by specmaker.\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 \"debugtools.h\"\n"
140 "#include \"winbase.h\"\n#include \"winnt.h\"\n\n\n",
141 OUTPUT_DLL_NAME, OUTPUT_DLL_NAME, OUTPUT_UC_DLL_NAME,
142 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)
153 assert (hfile);
154 assert (sym && sym->symbol);
156 if (!globals.do_code)
157 return;
159 if (sym->flags & SYM_DATA)
160 return;
162 if (!sym->function_name)
163 fprintf (hfile, "/* __%s %s_%s(); */\n", symbol_get_call_convention(sym),
164 OUTPUT_UC_DLL_NAME, sym->symbol);
165 else
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)
180 if (hfile)
182 fprintf (hfile, "\n\n\n#endif\t/* __WINE_%s_DLL_H */\n",
183 OUTPUT_UC_DLL_NAME);
184 fclose (hfile);
185 hfile = NULL;
190 /*******************************************************************
191 * output_c_preamble
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);
201 fprintf (cfile,
202 "/*\n * %s.dll\n *\n * Generated from %s.dll by specmaker.\n *\n"
203 " * DO NOT SUBMIT GENERATED DLLS FOR INCLUSION INTO WINE!\n * \n */"
204 "\n\n#include \"%s_dll.h\"\n\nDEFAULT_DEBUG_CHANNEL(%s);\n\n",
205 OUTPUT_DLL_NAME, globals.input_name, OUTPUT_DLL_NAME,
206 OUTPUT_DLL_NAME);
208 if (globals.forward_dll)
210 if (VERBOSE)
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);
219 fprintf (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",
224 OUTPUT_UC_DLL_NAME);
226 if (globals.forward_dll)
228 fprintf (cfile,
229 "hDLL = LoadLibraryA( \"%s\" );\n\t\t"
230 "TRACE(\":Forwarding DLL (%s) loaded (%%ld)\\n\",(LONG)hDLL);",
231 globals.forward_dll, globals.forward_dll);
233 else
234 fputs ("/* FIXME: Initialisation */", cfile);
236 fputs ("\n\t}\n\telse if (fdwReason == DLL_PROCESS_DETACH)\n\t{\n\t\t",
237 cfile);
239 if (globals.forward_dll)
241 fprintf (cfile,
242 "FreeLibrary( hDLL );\n\t\tTRACE(\":Forwarding DLL (%s)"
243 " freed\\n\");", globals.forward_dll);
245 else
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 /*******************************************************************
258 * output_c_symbol
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;
265 int is_void;
267 assert (cfile);
268 assert (sym && sym->symbol);
270 if (!globals.do_code)
271 return;
273 if (sym->flags & SYM_DATA)
275 fprintf (cfile, "/* FIXME: Move to top of file */\n%s;\n\n",
276 sym->arg_text[0]);
277 return;
280 if (sym->flags & SYM_THISCALL)
281 fputs ("#ifdef __i386__\n", cfile);
283 output_c_banner(sym);
285 if (!sym->function_name)
287 /* #ifdef'd dummy */
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");
291 CPP_END;
292 return;
295 is_void = !strcmp (sym->return_text, "void");
297 output_prototype (cfile, sym);
298 fputs ("\n{\n", cfile);
300 if (!globals.do_trace)
302 GET_THIS;
303 fputs ("\tFIXME(\":stub\\n\");\n", cfile);
304 if (!is_void)
305 fprintf (cfile, "\treturn (%s) 0;\n", sym->return_text);
306 fputs ("}\n", cfile);
307 CPP_END;
308 return;
311 /* Tracing, maybe forwarding as well */
312 if (globals.forward_dll)
314 /* Write variables for calling */
315 if (sym->varargs)
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");
327 if (!is_void)
328 fprintf (cfile, "\t%s retVal;\n", sym->return_text);
330 GET_THIS;
332 fprintf (cfile, "\tpFunc=(void*)GetProcAddress(hDLL,\"%s\");\n",
333 sym->symbol);
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(" : "",
350 sym->arg_name[i],
351 sym->arg_type[i] == ARG_WIDE_STRING ? ")" : "");
353 fputs (");\n", cfile);
355 if (!globals.forward_dll)
357 if (!is_void)
358 fprintf (cfile, "\treturn (%s) 0;\n", sym->return_text);
359 fputs ("}\n", cfile);
360 CPP_END;
361 return;
364 /* Call the DLL */
365 if (sym->varargs)
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));
379 if (!is_void)
381 if (sym->return_type == ARG_WIDE_STRING)
382 fputs (",debugstr_w(retVal)", cfile);
383 else
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);
388 else
389 fputs (");\n", cfile);
391 fputs ("}\n", cfile);
392 CPP_END;
396 /*******************************************************************
397 * output_c_postamble
399 * Write the last part of the .c file
401 static void output_c_postamble (void)
403 if (cfile)
404 fclose (cfile);
405 cfile = NULL;
409 /*******************************************************************
410 * output_makefile
412 * Write a Wine compatible makefile.in
414 void output_makefile (void)
416 FILE *makefile = open_file ("Makefile", ".in", "w");
418 if (VERBOSE)
419 puts ("Creating makefile");
421 fprintf (makefile,
422 "# Generated from %s.dll by specmaker.\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);
429 fclose (makefile);
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)
443 char cmd[128];
444 FILE *install_file = open_file (OUTPUT_DLL_NAME, "_install", "w");
446 if (VERBOSE)
447 puts ("Creating install script");
449 fprintf (install_file,
450 "#!/bin/bash\n# Generated from %s.dll by specmaker.\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\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 '/ws2_32/{G;s/$/\\^%s \\\\/;}' Make.rules.in | tr ^ \\\\t"
463 " >t.tmp\nmv -f t.tmp Make.rules.in\necho Patched Make.rules.in"
464 "\n\nsed '/DLLFILES =/{G;s/$/\\^%s\\/lib%s.so \\\\/;}'"
465 " dlls/Makefile.in| tr ^ \\\\t >t.tmp\n"
466 "sed '/SUBDIRS =/{G;s/$/\\^%s \\\\/;}' t.tmp | tr ^ \\\\t >t.tmp2"
467 "\nsed '/Map library name /{G;s/$/^\\$(RM) \\$\\@ \\&\\& \\$\\"
468 "(LN_S\\) %s\\/lib%s.\\@LIBEXT\\@ \\$\\@/;}' t.tmp2 | tr ^ \\\\t"
469 " > t.tmp\nsed '/Map library name /{G;s/$/lib%s.\\@LIBEXT\\@: "
470 "%s\\/lib%s.\\@LIBEXT\\@/;}' t.tmp > t.tmp2\nsed '/dll "
471 "dependencies /{G;s/$/%s\\/lib%s.\\@LIBEXT\\@\\: libkernel32."
472 "\\@LIBEXT\\@ libntdll.\\@LIBEXT\\@/;}' t.tmp2 > t.tmp\n"
473 "mv -f t.tmp dlls/Makefile.in\nrm -f t.tmp2\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);
485 system (cmd);
489 /*******************************************************************
490 * output_prototype
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);
503 else
504 for (i = start; i < sym->argc; i++)
505 fprintf (file, "%s%s %s", i > start ? ", " : "", sym->arg_text [i],
506 sym->arg_name [i]);
507 if (sym->varargs)
508 fputs (", ...", file);
509 fputc (')', file);
513 /*******************************************************************
514 * output_c_banner
516 * Write a function banner to the .c file
518 void output_c_banner (const parsed_symbol *sym)
520 char ord_spec[16];
521 size_t i;
523 if (globals.do_ordinals)
524 snprintf(ord_spec, sizeof (ord_spec), "%d", sym->ordinal);
525 else
527 ord_spec[0] = '@';
528 ord_spec[1] = '\0';
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);
539 if (!sym->argc)
540 fputs (" * None.\n *\n", cfile);
541 else
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)");
549 if (sym->varargs)
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);
558 else
559 fprintf (cfile, " * %s\n", sym->return_text);
561 fputs (" *\n */\n", cfile);
565 /*******************************************************************
566 * get_format_str
568 * Get a string containing the correct format string for a type
570 static const char *get_format_str (int type)
572 switch (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";
583 assert (0);
584 return "";
588 /*******************************************************************
589 * get_in_or_out
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)
599 return "In";
601 switch (sym->arg_type [arg])
603 case ARG_FLOAT:
604 case ARG_DOUBLE:
605 case ARG_LONG:
606 case ARG_STRUCT: return "In";
607 case ARG_POINTER:
608 case ARG_WIDE_STRING:
609 case ARG_STRING: return "In/Out";
611 assert (0);
612 return "";