output.c 16.8 KB
Newer Older
1 2 3
/*
 *  Code generation functions
 *
4
 *  Copyright 2000-2002 Jon Griffiths
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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 22 23

#include "config.h"
#include "wine/port.h"

24
#include "winedump.h"
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

/* Output files */
static FILE *specfile = NULL;
static FILE *hfile    = NULL;
static FILE *cfile    = NULL;

static void  output_spec_postamble (void);
static void  output_header_postamble (void);
static void  output_c_postamble (void);
static void  output_c_banner (const parsed_symbol *sym);
static const char *get_format_str (int type);
static const char *get_in_or_out (const parsed_symbol *sym, size_t arg);


/*******************************************************************
 *         output_spec_preamble
 *
 * Write the first part of the .spec file
 */
void  output_spec_preamble (void)
{
  specfile = open_file (OUTPUT_DLL_NAME, ".spec", "w");

  atexit (output_spec_postamble);

  if (VERBOSE)
    puts ("Creating .spec preamble");

  fprintf (specfile,
54
           "# Generated from %s by winedump\n\n", globals.input_name);
55 56 57 58 59 60 61 62 63 64
}


/*******************************************************************
 *         output_spec_symbol
 *
 * Write a symbol to the .spec file
 */
void  output_spec_symbol (const parsed_symbol *sym)
{
65 66
  char ord_spec[16];

67 68 69
  assert (specfile);
  assert (sym && sym->symbol);

70
  if (sym->ordinal >= 0)
71 72 73 74 75 76 77
    snprintf(ord_spec, 8, "%d", sym->ordinal);
  else
  {
    ord_spec[0] = '@';
    ord_spec[1] = '\0';
  }
  if (sym->flags & SYM_THISCALL)
Francois Gouget's avatar
Francois Gouget committed
78
    strcat (ord_spec, " -i386"); /* For binary compatibility only */
79

80 81
  if (!globals.do_code || !sym->function_name)
  {
82 83 84 85 86 87 88 89 90 91 92
    if (sym->flags & SYM_DATA)
    {
      if (globals.forward_dll)
        fprintf (specfile, "%s forward %s %s.%s #", ord_spec, sym->symbol,
                 globals.forward_dll, sym->symbol);

      fprintf (specfile, "%s extern %s %s\n", ord_spec, sym->symbol,
               sym->arg_name[0]);
      return;
    }

93
    if (globals.forward_dll)
94
      fprintf (specfile, "%s forward %s %s.%s\n", ord_spec, sym->symbol,
95 96
               globals.forward_dll, sym->symbol);
    else
97
      fprintf (specfile, "%s stub %s\n", ord_spec, sym->symbol);
98 99 100
  }
  else
  {
101
    unsigned int i = sym->flags & SYM_THISCALL ? 1 : 0;
102

103 104
    fprintf (specfile, "%s %s %s(", ord_spec, sym->varargs ? "varargs" :
             symbol_get_call_convention(sym), sym->symbol);
105

106
    for (; i < sym->argc; i++)
107 108 109 110
      fprintf (specfile, " %s", symbol_get_spec_type(sym, i));

    if (sym->argc)
      fputc (' ', specfile);
111 112 113 114 115 116
    fprintf (specfile, ") %s_%s", OUTPUT_UC_DLL_NAME, sym->function_name);

    if (sym->flags & SYM_THISCALL)
      fputs (" # __thiscall", specfile);

    fputc ('\n',specfile);
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
  }
}


/*******************************************************************
 *         output_spec_postamble
 *
 * Write the last part of the .spec file
 */
static void output_spec_postamble (void)
{
  if (specfile)
    fclose (specfile);
  specfile = NULL;
}


/*******************************************************************
 *         output_header_preamble
 *
 * Write the first part of the .h file
 */
void  output_header_preamble (void)
{
  hfile = open_file (OUTPUT_DLL_NAME, "_dll.h", "w");

  atexit (output_header_postamble);

  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 149
           "\n#ifndef __WINE_%s_DLL_H\n#define __WINE_%s_DLL_H\n\n"
           "#include \"windef.h\"\n#include \"wine/debug.h\"\n"
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
           "#include \"winbase.h\"\n#include \"winnt.h\"\n\n\n",
           OUTPUT_DLL_NAME, OUTPUT_DLL_NAME, OUTPUT_UC_DLL_NAME,
           OUTPUT_UC_DLL_NAME);
}


/*******************************************************************
 *         output_header_symbol
 *
 * Write a symbol to the .h file
 */
void  output_header_symbol (const parsed_symbol *sym)
{
  assert (hfile);
  assert (sym && sym->symbol);

  if (!globals.do_code)
    return;

169 170 171
  if (sym->flags & SYM_DATA)
    return;

172
  if (!sym->function_name)
173
    fprintf (hfile, "/* __%s %s_%s(); */\n", symbol_get_call_convention(sym),
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
             OUTPUT_UC_DLL_NAME, sym->symbol);
  else
  {
    output_prototype (hfile, sym);
    fputs (";\n", hfile);
  }
}


/*******************************************************************
 *         output_header_postamble
 *
 * Write the last part of the .h file
 */
static void output_header_postamble (void)
{
  if (hfile)
  {
    fprintf (hfile, "\n\n\n#endif\t/* __WINE_%s_DLL_H */\n",
             OUTPUT_UC_DLL_NAME);
    fclose (hfile);
    hfile = NULL;
  }
}


/*******************************************************************
 *         output_c_preamble
 *
 * Write the first part of the .c file
 */
void  output_c_preamble (void)
{
  cfile = open_file (OUTPUT_DLL_NAME, "_main.c", "w");

  atexit (output_c_postamble);

  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 215
           "\n\n#include \"config.h\"\n#include \"%s_dll.h\"\n\n"
           "WINE_DEFAULT_DEBUG_CHANNEL(%s);\n\n",
216 217 218 219 220 221 222 223
           OUTPUT_DLL_NAME, globals.input_name, OUTPUT_DLL_NAME,
           OUTPUT_DLL_NAME);

  if (globals.forward_dll)
  {
    if (VERBOSE)
      puts ("Creating a forwarding DLL");

224
    fputs ("\nHMODULE hDLL=0;\t/* DLL to call */\n\n", cfile);
225 226
  }

227 228 229
  fputs ("#ifdef __i386__\n#define GET_THIS(t,p) t p;\\\n__asm__ __volatile__"
         " (\"movl %%ecx, %0\" : \"=m\" (p))\n#endif\n\n\n", cfile);

230
  fprintf (cfile,
231
           "BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID "
232
           "lpvReserved)\n{\n\tTRACE(\"(0x%%p, %%ld, %%p)\\n\",hinstDLL,"
233
           "fdwReason,lpvReserved);\n\n\t"
234
           "if (fdwReason == DLL_PROCESS_ATTACH)\n\t{\n\t\t");
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261

  if (globals.forward_dll)
  {
    fprintf (cfile,
             "hDLL = LoadLibraryA( \"%s\" );\n\t\t"
             "TRACE(\":Forwarding DLL (%s) loaded (%%ld)\\n\",(LONG)hDLL);",
             globals.forward_dll, globals.forward_dll);
  }
  else
    fputs ("/* FIXME: Initialisation */", cfile);

  fputs ("\n\t}\n\telse if (fdwReason == DLL_PROCESS_DETACH)\n\t{\n\t\t",
         cfile);

  if (globals.forward_dll)
  {
    fprintf (cfile,
             "FreeLibrary( hDLL );\n\t\tTRACE(\":Forwarding DLL (%s)"
             " freed\\n\");", globals.forward_dll);
  }
  else
    fputs ("/* FIXME: Cleanup */", cfile);

  fputs ("\n\t}\n\n\treturn TRUE;\n}\n\n\n", cfile);
}


262 263 264 265 266
#define CPP_END  if (sym->flags & SYM_THISCALL) \
  fputs ("#endif\n", cfile); fputs ("\n\n", cfile)
#define GET_THIS if (sym->flags & SYM_THISCALL) \
  fprintf (cfile, "\tGET_THIS(%s,%s);\n", sym->arg_text[0],sym->arg_name[0])

267 268 269 270 271 272 273
/*******************************************************************
 *         output_c_symbol
 *
 * Write a symbol to the .c file
 */
void  output_c_symbol (const parsed_symbol *sym)
{
274
  unsigned int i, start = sym->flags & SYM_THISCALL ? 1 : 0;
275 276 277 278 279 280 281 282
  int is_void;

  assert (cfile);
  assert (sym && sym->symbol);

  if (!globals.do_code)
    return;

283 284 285 286 287 288 289 290 291 292
  if (sym->flags & SYM_DATA)
  {
    fprintf (cfile, "/* FIXME: Move to top of file */\n%s;\n\n",
             sym->arg_text[0]);
    return;
  }

  if (sym->flags & SYM_THISCALL)
    fputs ("#ifdef __i386__\n", cfile);

293 294 295 296 297
  output_c_banner(sym);

  if (!sym->function_name)
  {
    /* #ifdef'd dummy */
298 299
    fprintf (cfile, "#if 0\n__%s %s_%s()\n{\n\t/* %s in .spec */\n}\n#endif\n",
             symbol_get_call_convention(sym), OUTPUT_UC_DLL_NAME, sym->symbol,
300
             globals.forward_dll ? "@forward" : "@stub");
301
    CPP_END;
302 303 304 305 306 307 308 309 310 311
    return;
  }

  is_void = !strcmp (sym->return_text, "void");

  output_prototype (cfile, sym);
  fputs ("\n{\n", cfile);

  if (!globals.do_trace)
  {
312
    GET_THIS;
313 314 315
    fputs ("\tFIXME(\":stub\\n\");\n", cfile);
    if (!is_void)
        fprintf (cfile, "\treturn (%s) 0;\n", sym->return_text);
316 317
    fputs ("}\n", cfile);
    CPP_END;
318 319 320 321 322 323 324
    return;
  }

  /* Tracing, maybe forwarding as well */
  if (globals.forward_dll)
  {
    /* Write variables for calling */
325 326 327 328 329
    if (sym->varargs)
      fputs("\tva_list valist;\n", cfile);

    fprintf (cfile, "\t%s (__%s *pFunc)(", sym->return_text,
             symbol_get_call_convention(sym));
330

331 332
    for (i = start; i < sym->argc; i++)
      fprintf (cfile, "%s%s", i > start ? ", " : "", sym->arg_text [i]);
333

334 335
    fprintf (cfile, "%s);\n", sym->varargs ? ",..." : sym->argc == 1 &&
             sym->flags & SYM_THISCALL ? "" : sym->argc ? "" : "void");
336 337 338

    if (!is_void)
      fprintf (cfile, "\t%s retVal;\n", sym->return_text);
339 340 341 342 343

    GET_THIS;

    fprintf (cfile, "\tpFunc=(void*)GetProcAddress(hDLL,\"%s\");\n",
             sym->symbol);
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
  }

  /* TRACE input arguments */
  fprintf (cfile, "\tTRACE(\"(%s", !sym->argc ? "void" : "");

  for (i = 0; i < sym->argc; i++)
    fprintf (cfile, "%s(%s)%s", i ? "," : "", sym->arg_text [i],
             get_format_str (sym->arg_type [i]));

  fprintf (cfile, "%s): %s\\n\"", sym->varargs ? ",..." : "",
           globals.forward_dll ? "forward" : "stub");

  for (i = 0; i < sym->argc; i++)
    if (sym->arg_type[i] != ARG_STRUCT)
      fprintf(cfile, ",%s%s%s%s", sym->arg_type[i] == ARG_LONG ? "(LONG)" : "",
              sym->arg_type[i] == ARG_WIDE_STRING ? "debugstr_w(" : "",
              sym->arg_name[i],
              sym->arg_type[i] == ARG_WIDE_STRING ? ")" : "");

  fputs (");\n", cfile);

  if (!globals.forward_dll)
  {
    if (!is_void)
      fprintf (cfile, "\treturn (%s) 0;\n", sym->return_text);
369 370
    fputs ("}\n", cfile);
    CPP_END;
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
    return;
  }

  /* Call the DLL */
  if (sym->varargs)
    fprintf (cfile, "\tva_start(valist,%s);\n", sym->arg_name[sym->argc-1]);

  fprintf (cfile, "\t%spFunc(", !is_void ? "retVal = " : "");

  for (i = 0; i < sym->argc; i++)
    fprintf (cfile, "%s%s", i ? "," : "", sym->arg_name [i]);

  fputs (sym->varargs ? ",valist);\n\tva_end(valist);" : ");", cfile);

  /* TRACE return value */
  fprintf (cfile, "\n\tTRACE(\"Returned (%s)\\n\"",
           get_format_str (sym->return_type));

  if (!is_void)
  {
    if (sym->return_type == ARG_WIDE_STRING)
      fputs (",debugstr_w(retVal)", cfile);
    else
      fprintf (cfile, ",%s%s", sym->return_type == ARG_LONG ? "(LONG)" : "",
               sym->return_type == ARG_STRUCT ? "" : "retVal");
    fputs (");\n\treturn retVal;\n", cfile);
  }
  else
    fputs (");\n", cfile);

401 402
  fputs ("}\n", cfile);
  CPP_END;
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
}


/*******************************************************************
 *         output_c_postamble
 *
 * Write the last part of the .c file
 */
static void output_c_postamble (void)
{
  if (cfile)
    fclose (cfile);
  cfile = NULL;
}


/*******************************************************************
 *         output_makefile
 *
422
 * Write a Wine compatible makefile.in
423 424 425 426 427 428 429 430 431
 */
void  output_makefile (void)
{
  FILE *makefile = open_file ("Makefile", ".in", "w");

  if (VERBOSE)
    puts ("Creating makefile");

  fprintf (makefile,
432
           "# Generated from %s by winedump.\nTOPSRCDIR = @top_srcdir@\n"
433
           "TOPOBJDIR = ../..\nSRCDIR    = @srcdir@\nVPATH     = @srcdir@\n"
434
           "MODULE    = %s.dll\n", globals.input_name, OUTPUT_DLL_NAME);
435

436
  fprintf (makefile, "IMPORTS   = kernel32");
437 438 439 440
  if (globals.forward_dll)
    fprintf (makefile, " %s", globals.forward_dll);

  fprintf (makefile,
441
           "\n\nC_SRCS = \\\n\t%s_main.c\n\n@MAKE_DLL_RULES@\n\n### Dependencies:",
442
           OUTPUT_DLL_NAME);
443

444 445
  if (globals.forward_dll)
    fprintf (specfile,"#import %s.dll\n", globals.forward_dll);
446

447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
  fclose (makefile);
}


/*******************************************************************
 *         output_install_script
 *
 * Write a script to insert the DLL into Wine
 *
 * Rather than using diff/patch, several sed calls are generated
 * so the script can be re-run at any time without breaking.
 */
void  output_install_script (void)
{
  FILE *install_file = open_file (OUTPUT_DLL_NAME, "_install", "w");

  if (VERBOSE)
    puts ("Creating install script");

  fprintf (install_file,
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
    "#!/bin/bash\n"
    "# Generated from %s.dll by winedump.\n\n"
    "if [ $# -ne 1 ] || [ ! -d $1 ] || [ ! -f $1/AUTHORS ]; then\n"
    "\t[ $# -eq 1 ] && echo \"Invalid path\"\n"
    "\techo \"Usage: $0 wine-base-dir\"\n"
    "\texit 1\n"
    "fi\n\n"
    "if [ -d $1/dlls/%s ]; then\n"
    "\techo \"DLL is already present\"\n"
    "\texit 1\n"
    "fi\n\n"
    "echo Adding DLL %s to Wine build tree...\n\n"
    "mkdir $1/dlls/%s\n"
    "cp %s.spec $1/dlls/%s\n"
    "cp %s_main.c $1/dlls/%s\n"
    "cp %s_dll.h $1/dlls/%s\n"
    "cp Makefile.in $1/dlls/%s/Makefile.in\n"
    "echo Copied DLL files\n\n"
    "cd $1\n\n"
    "sed '/dlls\\/x11drv\\/Makefile/"
        "{G;s/$/dlls\\/%s\\/Makefile/;}' configure.ac >t.tmp\n"
    "mv -f t.tmp configure.ac\n"
    "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 493
           OUTPUT_DLL_NAME, OUTPUT_DLL_NAME, OUTPUT_DLL_NAME, OUTPUT_DLL_NAME,
           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
  fchmod (fileno(install_file), 0755);
497 498 499 500 501 502 503 504 505
  fclose (install_file);
}


/*******************************************************************
 *         output_prototype
 *
 * Write a C prototype for a parsed symbol
 */
506
void  output_prototype (FILE *file, const parsed_symbol *sym)
507
{
508
  unsigned int i, start = sym->flags & SYM_THISCALL ? 1 : 0;
509

510
  fprintf (file, "%s __%s %s_%s(", sym->return_text, symbol_get_call_convention(sym),
511 512
           OUTPUT_UC_DLL_NAME, sym->function_name);

513
  if (!sym->argc || (sym->argc == 1 && sym->flags & SYM_THISCALL))
514 515
    fputs ("void", file);
  else
516 517
    for (i = start; i < sym->argc; i++)
      fprintf (file, "%s%s %s", i > start ? ", " : "", sym->arg_text [i],
518 519 520 521 522 523 524 525 526 527 528 529 530 531
               sym->arg_name [i]);
  if (sym->varargs)
    fputs (", ...", file);
  fputc (')', file);
}


/*******************************************************************
 *         output_c_banner
 *
 * Write a function banner to the .c file
 */
void  output_c_banner (const parsed_symbol *sym)
{
532
  char ord_spec[16];
533 534
  size_t i;

535
  if (sym->ordinal >= 0)
536 537 538 539 540 541 542
    snprintf(ord_spec, sizeof (ord_spec), "%d", sym->ordinal);
  else
  {
    ord_spec[0] = '@';
    ord_spec[1] = '\0';
  }

543
  fprintf (cfile, "/*********************************************************"
544 545
           "*********\n *\t\t%s (%s.%s)\n *\n", sym->symbol,
           OUTPUT_UC_DLL_NAME, ord_spec);
546 547 548 549 550 551 552 553 554 555 556 557 558

  if (globals.do_documentation && sym->function_name)
  {
    fputs (" *\n * PARAMS\n *\n", cfile);

    if (!sym->argc)
      fputs (" *  None.\n *\n", cfile);
    else
    {
      for (i = 0; i < sym->argc; i++)
        fprintf (cfile, " *  %s [%s]%s\n", sym->arg_name [i],
                 get_in_or_out(sym, i),
                 strcmp (sym->arg_name [i], "_this") ? "" :
559
                 "     Pointer to the class object (in ECX)");
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602

      if (sym->varargs)
        fputs (" *  ...[I]\n", cfile);
      fputs (" *\n", cfile);
    }

    fputs (" * RETURNS\n *\n", cfile);

    if (sym->return_text && !strcmp (sym->return_text, "void"))
      fputs (" *  Nothing.\n", cfile);
    else
      fprintf (cfile, " *  %s\n", sym->return_text);
  }
  fputs (" *\n */\n", cfile);
}


/*******************************************************************
 *         get_format_str
 *
 * Get a string containing the correct format string for a type
 */
static const char *get_format_str (int type)
{
  switch (type)
  {
  case ARG_VOID:        return "void";
  case ARG_FLOAT:       return "%f";
  case ARG_DOUBLE:      return "%g";
  case ARG_POINTER:     return "%p";
  case ARG_WIDE_STRING:
  case ARG_STRING:      return "%s";
  case ARG_LONG:        return "%ld";
  case ARG_STRUCT:      return "struct";
  }
  assert (0);
  return "";
}


/*******************************************************************
 *         get_in_or_out
 *
603
 * Determine if a parameter is In or In/Out
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
 */
static const char *get_in_or_out (const parsed_symbol *sym, size_t arg)
{
  assert (sym && arg < sym->argc);
  assert (globals.do_documentation);

  if (sym->arg_flag [arg] & CT_CONST)
    return "In";

  switch (sym->arg_type [arg])
  {
  case ARG_FLOAT:
  case ARG_DOUBLE:
  case ARG_LONG:
  case ARG_STRUCT:      return "In";
  case ARG_POINTER:
  case ARG_WIDE_STRING:
  case ARG_STRING:      return "In/Out";
  }
  assert (0);
  return "";
625
}