output.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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. #include "config.h"
  21. #include "wine/port.h"
  22. #include "winedump.h"
  23. /* Output files */
  24. static FILE *specfile = NULL;
  25. static FILE *hfile = NULL;
  26. static FILE *cfile = NULL;
  27. static void output_spec_postamble (void);
  28. static void output_header_postamble (void);
  29. static void output_c_postamble (void);
  30. static void output_c_banner (const parsed_symbol *sym);
  31. static const char *get_format_str (int type);
  32. static const char *get_in_or_out (const parsed_symbol *sym, size_t arg);
  33. /*******************************************************************
  34. * output_spec_preamble
  35. *
  36. * Write the first part of the .spec file
  37. */
  38. void output_spec_preamble (void)
  39. {
  40. specfile = open_file (OUTPUT_DLL_NAME, ".spec", "w");
  41. atexit (output_spec_postamble);
  42. if (VERBOSE)
  43. puts ("Creating .spec preamble");
  44. fprintf (specfile,
  45. "# Generated from %s by winedump\n\n", globals.input_name);
  46. }
  47. /*******************************************************************
  48. * output_spec_symbol
  49. *
  50. * Write a symbol to the .spec file
  51. */
  52. void output_spec_symbol (const parsed_symbol *sym)
  53. {
  54. char ord_spec[20];
  55. assert (specfile);
  56. assert (sym && sym->symbol);
  57. if (sym->ordinal >= 0)
  58. snprintf(ord_spec, 12, "%d", sym->ordinal);
  59. else
  60. {
  61. ord_spec[0] = '@';
  62. ord_spec[1] = '\0';
  63. }
  64. if (sym->flags & SYM_THISCALL)
  65. strcat (ord_spec, " -i386"); /* For binary compatibility only */
  66. if (!globals.do_code || !sym->function_name)
  67. {
  68. if (sym->flags & SYM_DATA)
  69. {
  70. if (globals.forward_dll)
  71. fprintf (specfile, "%s forward %s %s.%s #", ord_spec, sym->symbol,
  72. globals.forward_dll, sym->symbol);
  73. fprintf (specfile, "%s extern %s %s\n", ord_spec, sym->symbol,
  74. sym->arg_name[0]);
  75. return;
  76. }
  77. if (globals.forward_dll)
  78. fprintf (specfile, "%s forward %s %s.%s\n", ord_spec, sym->symbol,
  79. globals.forward_dll, sym->symbol);
  80. else
  81. fprintf (specfile, "%s stub %s\n", ord_spec, sym->symbol);
  82. }
  83. else
  84. {
  85. unsigned int i = sym->flags & SYM_THISCALL ? 1 : 0;
  86. fprintf (specfile, "%s %s %s(", ord_spec, sym->varargs ? "varargs" :
  87. symbol_get_call_convention(sym), sym->symbol);
  88. for (; i < sym->argc; i++)
  89. fprintf (specfile, " %s", symbol_get_spec_type(sym, i));
  90. if (sym->argc)
  91. fputc (' ', specfile);
  92. fputs (") ", specfile);
  93. if (sym->flags & SYM_THISCALL)
  94. fputs ("__thiscall_", specfile);
  95. fprintf (specfile, "%s_%s", OUTPUT_UC_DLL_NAME, sym->function_name);
  96. fputc ('\n',specfile);
  97. }
  98. }
  99. /*******************************************************************
  100. * output_spec_postamble
  101. *
  102. * Write the last part of the .spec file
  103. */
  104. static void output_spec_postamble (void)
  105. {
  106. if (specfile)
  107. fclose (specfile);
  108. specfile = NULL;
  109. }
  110. /*******************************************************************
  111. * output_header_preamble
  112. *
  113. * Write the first part of the .h file
  114. */
  115. void output_header_preamble (void)
  116. {
  117. if (!globals.do_code)
  118. return;
  119. hfile = open_file (OUTPUT_DLL_NAME, "_dll.h", "w");
  120. atexit (output_header_postamble);
  121. fprintf (hfile,
  122. "/*\n * %s.dll\n *\n * Generated from %s.dll by winedump.\n *\n"
  123. " * DO NOT SEND GENERATED DLLS FOR INCLUSION INTO WINE !\n *\n */"
  124. "\n#ifndef __WINE_%s_DLL_H\n#define __WINE_%s_DLL_H\n\n"
  125. "#include \"windef.h\"\n#include \"wine/debug.h\"\n"
  126. "#include \"winbase.h\"\n#include \"winnt.h\"\n\n\n",
  127. OUTPUT_DLL_NAME, OUTPUT_DLL_NAME, OUTPUT_UC_DLL_NAME,
  128. OUTPUT_UC_DLL_NAME);
  129. }
  130. /*******************************************************************
  131. * output_header_symbol
  132. *
  133. * Write a symbol to the .h file
  134. */
  135. void output_header_symbol (const parsed_symbol *sym)
  136. {
  137. if (!globals.do_code)
  138. return;
  139. assert (hfile);
  140. assert (sym && sym->symbol);
  141. if (!globals.do_code)
  142. return;
  143. if (sym->flags & SYM_DATA)
  144. return;
  145. if (!sym->function_name)
  146. fprintf (hfile, "/* __%s %s_%s(); */\n", symbol_get_call_convention(sym),
  147. OUTPUT_UC_DLL_NAME, sym->symbol);
  148. else
  149. {
  150. output_prototype (hfile, sym);
  151. fputs (";\n", hfile);
  152. }
  153. }
  154. /*******************************************************************
  155. * output_header_postamble
  156. *
  157. * Write the last part of the .h file
  158. */
  159. static void output_header_postamble (void)
  160. {
  161. if (hfile)
  162. {
  163. fprintf (hfile, "\n\n\n#endif\t/* __WINE_%s_DLL_H */\n",
  164. OUTPUT_UC_DLL_NAME);
  165. fclose (hfile);
  166. hfile = NULL;
  167. }
  168. }
  169. /*******************************************************************
  170. * output_c_preamble
  171. *
  172. * Write the first part of the .c file
  173. */
  174. void output_c_preamble (void)
  175. {
  176. cfile = open_file (OUTPUT_DLL_NAME, "_main.c", "w");
  177. atexit (output_c_postamble);
  178. fprintf (cfile,
  179. "/*\n * %s.dll\n *\n * Generated from %s by winedump.\n *\n"
  180. " * DO NOT SUBMIT GENERATED DLLS FOR INCLUSION INTO WINE!\n *\n */"
  181. "\n\n#include \"config.h\"\n\n#include <stdarg.h>\n\n"
  182. "#include \"windef.h\"\n#include \"winbase.h\"\n",
  183. OUTPUT_DLL_NAME, globals.input_name);
  184. if (globals.do_code)
  185. fprintf (cfile, "#include \"%s_dll.h\"\n", OUTPUT_DLL_NAME);
  186. fprintf (cfile,"#include \"wine/debug.h\"\n\nWINE_DEFAULT_DEBUG_CHANNEL(%s);\n\n",
  187. OUTPUT_DLL_NAME);
  188. if (globals.forward_dll)
  189. {
  190. if (VERBOSE)
  191. puts ("Creating a forwarding DLL");
  192. fputs ("\nHMODULE hDLL=0; /* DLL to call */\n\n", cfile);
  193. fprintf (cfile,
  194. "BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, void *reserved)\n{\n"
  195. " TRACE(\"(%%p, %%u, %%p)\\n\", instance, reason, reserved);\n\n"
  196. " switch (reason)\n"
  197. " {\n"
  198. " case DLL_PROCESS_ATTACH:\n"
  199. " hDLL = LoadLibraryA(\"%s\");\n"
  200. " TRACE(\"Forwarding DLL (%s) loaded (%%p)\\n\", hDLL);\n"
  201. " DisableThreadLibraryCalls(instance);\n"
  202. " break;\n"
  203. " case DLL_PROCESS_DETACH:\n"
  204. " FreeLibrary(hDLL);\n"
  205. " TRACE(\"Forwarding DLL (%s) freed\\n\");\n"
  206. " break;\n"
  207. " }\n\n"
  208. " return TRUE;\n}\n",
  209. globals.forward_dll, globals.forward_dll, globals.forward_dll);
  210. }
  211. }
  212. /*******************************************************************
  213. * output_c_symbol
  214. *
  215. * Write a symbol to the .c file
  216. */
  217. void output_c_symbol (const parsed_symbol *sym)
  218. {
  219. unsigned int i, start = sym->flags & SYM_THISCALL ? 1 : 0;
  220. BOOL is_void;
  221. static BOOL has_thiscall = FALSE;
  222. assert (cfile);
  223. assert (sym && sym->symbol);
  224. if (!globals.do_code)
  225. return;
  226. if (sym->flags & SYM_DATA)
  227. {
  228. fprintf (cfile, "/* FIXME: Move to top of file */\n%s;\n\n",
  229. sym->arg_text[0]);
  230. return;
  231. }
  232. if (sym->flags & SYM_THISCALL && !has_thiscall)
  233. {
  234. fputs ("#ifdef __i386__ /* thiscall functions are i386-specific */\n\n"
  235. "#define THISCALL(func) __thiscall_ ## func\n"
  236. "#define THISCALL_NAME(func) __ASM_NAME(\"__thiscall_\" #func)\n"
  237. "#define DEFINE_THISCALL_WRAPPER(func) \\\n"
  238. "\textern void THISCALL(func)(); \\\n"
  239. "\t__ASM_GLOBAL_FUNC(__thiscall_ ## func, \\\n"
  240. "\t\t\t\"popl %eax\\n\\t\" \\\n"
  241. "\t\t\t\"pushl %ecx\\n\\t\" \\\n"
  242. "\t\t\t\"pushl %eax\\n\\t\" \\\n"
  243. "\t\t\t\"jmp \" __ASM_NAME(#func) )\n"
  244. "#else /* __i386__ */\n\n"
  245. "#define THISCALL(func) func\n"
  246. "#define THISCALL_NAME(func) __ASM_NAME(#func)\n"
  247. "#define DEFINE_THISCALL_WRAPPER(func) /* nothing */\n\n"
  248. "#endif /* __i386__ */\n\n", cfile);
  249. has_thiscall = TRUE;
  250. }
  251. output_c_banner(sym);
  252. if (sym->flags & SYM_THISCALL)
  253. fprintf(cfile, "DEFINE_THISCALL_WRAPPER(%s)\n", sym->function_name);
  254. if (!sym->function_name)
  255. {
  256. /* #ifdef'd dummy */
  257. fprintf (cfile, "#if 0\n__%s %s_%s()\n{\n\t/* %s in .spec */\n}\n#endif\n",
  258. symbol_get_call_convention(sym), OUTPUT_UC_DLL_NAME, sym->symbol,
  259. globals.forward_dll ? "@forward" : "@stub");
  260. return;
  261. }
  262. is_void = !strcasecmp (sym->return_text, "void");
  263. output_prototype (cfile, sym);
  264. fputs ("\n{\n", cfile);
  265. if (!globals.do_trace)
  266. {
  267. fputs ("\tFIXME(\":stub\\n\");\n", cfile);
  268. if (!is_void)
  269. fprintf (cfile, "\treturn (%s) 0;\n", sym->return_text);
  270. fputs ("}\n", cfile);
  271. return;
  272. }
  273. /* Tracing, maybe forwarding as well */
  274. if (globals.forward_dll)
  275. {
  276. /* Write variables for calling */
  277. if (sym->varargs)
  278. fputs("\tva_list valist;\n", cfile);
  279. fprintf (cfile, "\t%s (__%s *pFunc)(", sym->return_text,
  280. symbol_get_call_convention(sym));
  281. for (i = start; i < sym->argc; i++)
  282. fprintf (cfile, "%s%s", i > start ? ", " : "", sym->arg_text [i]);
  283. fprintf (cfile, "%s);\n", sym->varargs ? ",..." : sym->argc == 1 &&
  284. sym->flags & SYM_THISCALL ? "" : sym->argc ? "" : "void");
  285. if (!is_void)
  286. fprintf (cfile, "\t%s retVal;\n", sym->return_text);
  287. fprintf (cfile, "\tpFunc=(void*)GetProcAddress(hDLL,\"%s\");\n",
  288. sym->symbol);
  289. }
  290. /* TRACE input arguments */
  291. fprintf (cfile, "\tTRACE(\"(%s", !sym->argc ? "void" : "");
  292. for (i = 0; i < sym->argc; i++)
  293. fprintf (cfile, "%s(%s)%s", i ? "," : "", sym->arg_text [i],
  294. get_format_str (sym->arg_type [i]));
  295. fprintf (cfile, "%s): %s\\n\"", sym->varargs ? ",..." : "",
  296. globals.forward_dll ? "forward" : "stub");
  297. for (i = 0; i < sym->argc; i++)
  298. if (sym->arg_type[i] != ARG_STRUCT)
  299. fprintf(cfile, ",%s%s%s%s", sym->arg_type[i] == ARG_LONG ? "(LONG)" : "",
  300. sym->arg_type[i] == ARG_WIDE_STRING ? "debugstr_w(" : "",
  301. sym->arg_name[i],
  302. sym->arg_type[i] == ARG_WIDE_STRING ? ")" : "");
  303. fputs (");\n", cfile);
  304. if (!globals.forward_dll)
  305. {
  306. if (!is_void)
  307. fprintf (cfile, "\treturn (%s) 0;\n", sym->return_text);
  308. fputs ("}\n", cfile);
  309. return;
  310. }
  311. /* Call the DLL */
  312. if (sym->varargs)
  313. fprintf (cfile, "\tva_start(valist,%s);\n", sym->arg_name[sym->argc-1]);
  314. fprintf (cfile, "\t%spFunc(", !is_void ? "retVal = " : "");
  315. for (i = 0; i < sym->argc; i++)
  316. fprintf (cfile, "%s%s", i ? "," : "", sym->arg_name [i]);
  317. fputs (sym->varargs ? ",valist);\n\tva_end(valist);" : ");", cfile);
  318. /* TRACE return value */
  319. fprintf (cfile, "\n\tTRACE(\"Returned (%s)\\n\"",
  320. get_format_str (sym->return_type));
  321. if (!is_void)
  322. {
  323. if (sym->return_type == ARG_WIDE_STRING)
  324. fputs (",debugstr_w(retVal)", cfile);
  325. else
  326. fprintf (cfile, ",%s%s", sym->return_type == ARG_LONG ? "(LONG)" : "",
  327. sym->return_type == ARG_STRUCT ? "" : "retVal");
  328. fputs (");\n\treturn retVal;\n", cfile);
  329. }
  330. else
  331. fputs (");\n", cfile);
  332. fputs ("}\n", cfile);
  333. }
  334. /*******************************************************************
  335. * output_c_postamble
  336. *
  337. * Write the last part of the .c file
  338. */
  339. static void output_c_postamble (void)
  340. {
  341. if (cfile)
  342. fclose (cfile);
  343. cfile = NULL;
  344. }
  345. /*******************************************************************
  346. * output_makefile
  347. *
  348. * Write a Wine compatible makefile.in
  349. */
  350. void output_makefile (void)
  351. {
  352. FILE *makefile = open_file ("Makefile", ".in", "w");
  353. if (VERBOSE)
  354. puts ("Creating makefile");
  355. fprintf (makefile,
  356. "# Generated from %s by winedump.\n"
  357. "MODULE = %s.dll\n", globals.input_name, OUTPUT_DLL_NAME);
  358. if (globals.forward_dll)
  359. fprintf (makefile, "IMPORTS = %s", globals.forward_dll);
  360. fprintf (makefile, "\n\nC_SRCS = \\\n\t%s_main.c\n", OUTPUT_DLL_NAME);
  361. if (globals.forward_dll)
  362. fprintf (specfile,"#import %s.dll\n", globals.forward_dll);
  363. fclose (makefile);
  364. }
  365. /*******************************************************************
  366. * output_prototype
  367. *
  368. * Write a C prototype for a parsed symbol
  369. */
  370. void output_prototype (FILE *file, const parsed_symbol *sym)
  371. {
  372. unsigned int i, start = sym->flags & SYM_THISCALL ? 1 : 0;
  373. fprintf (file, "%s __%s %s_%s(", sym->return_text, symbol_get_call_convention(sym),
  374. OUTPUT_UC_DLL_NAME, sym->function_name);
  375. if (!sym->argc || (sym->argc == 1 && sym->flags & SYM_THISCALL))
  376. fputs ("void", file);
  377. else
  378. for (i = start; i < sym->argc; i++)
  379. fprintf (file, "%s%s %s", i > start ? ", " : "", sym->arg_text [i],
  380. sym->arg_name [i]);
  381. if (sym->varargs)
  382. fputs (", ...", file);
  383. fputc (')', file);
  384. }
  385. /*******************************************************************
  386. * output_c_banner
  387. *
  388. * Write a function banner to the .c file
  389. */
  390. void output_c_banner (const parsed_symbol *sym)
  391. {
  392. char ord_spec[16];
  393. size_t i;
  394. if (sym->ordinal >= 0)
  395. snprintf(ord_spec, sizeof (ord_spec), "%d", sym->ordinal);
  396. else
  397. {
  398. ord_spec[0] = '@';
  399. ord_spec[1] = '\0';
  400. }
  401. fprintf (cfile, "/*********************************************************"
  402. "*********\n *\t\t%s (%s.%s)\n *\n", sym->symbol,
  403. OUTPUT_UC_DLL_NAME, ord_spec);
  404. if (globals.do_documentation && sym->function_name)
  405. {
  406. fputs (" *\n * PARAMS\n *\n", cfile);
  407. if (!sym->argc)
  408. fputs (" * None.\n *\n", cfile);
  409. else
  410. {
  411. for (i = 0; i < sym->argc; i++)
  412. fprintf (cfile, " * %s [%s]%s\n", sym->arg_name [i],
  413. get_in_or_out(sym, i),
  414. strcmp (sym->arg_name [i], "_this") ? "" :
  415. " Pointer to the class object (in ECX)");
  416. if (sym->varargs)
  417. fputs (" * ...[I]\n", cfile);
  418. fputs (" *\n", cfile);
  419. }
  420. fputs (" * RETURNS\n *\n", cfile);
  421. if (sym->return_text && !strcmp (sym->return_text, "void"))
  422. fputs (" * Nothing.\n", cfile);
  423. else
  424. fprintf (cfile, " * %s\n", sym->return_text);
  425. }
  426. fputs (" *\n */\n", cfile);
  427. }
  428. /*******************************************************************
  429. * get_format_str
  430. *
  431. * Get a string containing the correct format string for a type
  432. */
  433. static const char *get_format_str (int type)
  434. {
  435. switch (type)
  436. {
  437. case ARG_VOID: return "void";
  438. case ARG_FLOAT: return "%f";
  439. case ARG_DOUBLE: return "%g";
  440. case ARG_POINTER: return "%p";
  441. case ARG_WIDE_STRING:
  442. case ARG_STRING: return "%s";
  443. case ARG_LONG: return "%ld";
  444. case ARG_STRUCT: return "struct";
  445. }
  446. assert (0);
  447. return "";
  448. }
  449. /*******************************************************************
  450. * get_in_or_out
  451. *
  452. * Determine if a parameter is In or In/Out
  453. */
  454. static const char *get_in_or_out (const parsed_symbol *sym, size_t arg)
  455. {
  456. assert (sym && arg < sym->argc);
  457. assert (globals.do_documentation);
  458. if (sym->arg_flag [arg] & CT_CONST)
  459. return "In";
  460. switch (sym->arg_type [arg])
  461. {
  462. case ARG_FLOAT:
  463. case ARG_DOUBLE:
  464. case ARG_LONG:
  465. case ARG_STRUCT: return "In";
  466. case ARG_POINTER:
  467. case ARG_WIDE_STRING:
  468. case ARG_STRING: return "In/Out";
  469. }
  470. assert (0);
  471. return "";
  472. }