output.c 16 KB

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