7 #define strcasecmp _stricmp
10 #define ARRAYSIZE(a) (sizeof(a) / sizeof((a)[0]))
12 typedef struct _STRING
22 int nCallingConvention
;
40 typedef int (*PFNOUTLINE
)(FILE *, EXPORT
*);
43 int gbNotPrivateNoWarn
= 0;
45 int giArch
= ARCH_X86
;
46 char *pszArchString
= "i386";
48 char *pszSourceFileName
= NULL
;
49 char *pszDllName
= NULL
;
50 char *gpszUnderscore
= "";
52 #define DbgPrint(...) (!gbDebug || fprintf(stderr, __VA_ARGS__))
87 const char* astrCallingConventions
[] =
96 static const char* astrShouldBePrivate
[] =
100 "DllGetClassFactoryFromClassString",
101 "DllGetDocumentation",
105 "DllRegisterServerEx",
106 "DllRegisterServerExW",
108 "DllUnregisterServer",
109 "RasCustomDeleteEntryNotify",
117 IsSeparator(char chr
)
119 return ((chr
<= ',' && chr
!= '$' && chr
!= '#') ||
120 (chr
>= ':' && chr
< '?') );
124 CompareToken(const char *token
, const char *comparand
)
128 if (*token
!= *comparand
) return 0;
132 if (!IsSeparator(*token
)) return 0;
137 ScanToken(const char *token
, char chr
)
139 while (!IsSeparator(*token
))
141 if (*token
== chr
) return token
;
152 if (pc
[0] == '\n' && pc
[1] == '\r') return pc
+ 2;
153 else if (pc
[0] == '\n') return pc
+ 1;
160 TokenLength(char *pc
)
164 while (!IsSeparator(*pc
++)) length
++;
173 while (!IsSeparator(*pc
)) pc
++;
175 /* Skip white spaces */
176 while (*pc
== ' ' || *pc
== '\t') pc
++;
178 /* Check for end of line */
179 if (*pc
== '\n' || *pc
== '\r' || *pc
== 0) return 0;
181 /* Check for comment */
182 if (*pc
== '#' || *pc
== ';') return 0;
188 OutputHeader_stub(FILE *file
)
190 fprintf(file
, "/* This file is autogenerated, do not edit. */\n\n"
191 "#include <stubs.h>\n");
195 fprintf(file
, "#include <wine/debug.h>\n");
196 fprintf(file
, "#include <inttypes.h>\n");
197 fprintf(file
, "WINE_DECLARE_DEBUG_CHANNEL(relay);\n");
204 OutputLine_stub(FILE *file
, EXPORT
*pexp
)
208 int bInPrototype
= 0;
210 if (pexp
->nCallingConvention
!= CC_STUB
&&
211 (pexp
->uFlags
& FL_STUB
) == 0)
213 /* Only relay trace stdcall C functions */
214 if (!gbTracing
|| (pexp
->nCallingConvention
!= CC_STDCALL
)
215 || (pexp
->uFlags
& FL_NORELAY
)
216 || (pexp
->strName
.buf
[0] == '?'))
223 /* Declare the "real" function */
226 fprintf(file
, "extern ");
232 if (pexp
->uFlags
& FL_REGISTER
)
234 /* FIXME: Not sure this is right */
235 fprintf(file
, "void ");
237 else if (pexp
->uFlags
& FL_RET64
)
239 fprintf(file
, "__int64 ");
243 fprintf(file
, "int ");
246 if ((giArch
== ARCH_X86
) &&
247 pexp
->nCallingConvention
== CC_STDCALL
)
249 fprintf(file
, "__stdcall ");
253 if (pexp
->strName
.buf
[0] == '?')
255 fprintf(file
, "stub_function%d(", pexp
->nNumber
);
259 if (!bRelay
|| bInPrototype
)
260 fprintf(file
, "%.*s(", pexp
->strName
.len
, pexp
->strName
.buf
);
262 fprintf(file
, "$relaytrace$%.*s(", pexp
->strName
.len
, pexp
->strName
.buf
);
265 for (i
= 0; i
< pexp
->nArgCount
; i
++)
267 if (i
!= 0) fprintf(file
, ", ");
268 switch (pexp
->anArgs
[i
])
270 case ARG_LONG
: fprintf(file
, "long"); break;
271 case ARG_PTR
: fprintf(file
, "void*"); break;
272 case ARG_STR
: fprintf(file
, "char*"); break;
273 case ARG_WSTR
: fprintf(file
, "wchar_t*"); break;
274 case ARG_DBL
: fprintf(file
, "double"); break;
275 case ARG_INT64
: fprintf(file
, "__int64"); break;
276 /* __int128 is not supported on x86, and int128 in spec files most often represents a GUID */
277 case ARG_INT128
: fprintf(file
, "GUID"); break;
278 case ARG_FLOAT
: fprintf(file
, "float"); break;
280 fprintf(file
, " a%d", i
);
285 fprintf(file
, ");\n\n");
287 } while (bInPrototype
--);
291 fprintf(file
, ")\n{\n\tDbgPrint(\"WARNING: calling stub %.*s(",
292 pexp
->strName
.len
, pexp
->strName
.buf
);
296 fprintf(file
, ")\n{\n");
297 if (pexp
->uFlags
& FL_REGISTER
)
299 /* No return value */
301 else if (pexp
->uFlags
& FL_RET64
)
303 fprintf(file
, "\t__int64 retval;\n");
307 fprintf(file
, "\tint retval;\n");
309 fprintf(file
, "\tif (TRACE_ON(relay))\n\t\tDPRINTF(\"%s: %.*s(",
310 pszDllName
, pexp
->strName
.len
, pexp
->strName
.buf
);
313 for (i
= 0; i
< pexp
->nArgCount
; i
++)
315 if (i
!= 0) fprintf(file
, ",");
316 switch (pexp
->anArgs
[i
])
318 case ARG_LONG
: fprintf(file
, "0x%%lx"); break;
319 case ARG_PTR
: fprintf(file
, "0x%%p"); break;
320 case ARG_STR
: fprintf(file
, "'%%s'"); break;
321 case ARG_WSTR
: fprintf(file
, "'%%ws'"); break;
322 case ARG_DBL
: fprintf(file
, "%%f"); break;
323 case ARG_INT64
: fprintf(file
, "%%\"PRIx64\""); break;
324 case ARG_INT128
: fprintf(file
, "'%%s'"); break;
325 case ARG_FLOAT
: fprintf(file
, "%%f"); break;
328 fprintf(file
, ")\\n\"");
330 for (i
= 0; i
< pexp
->nArgCount
; i
++)
333 switch (pexp
->anArgs
[i
])
335 case ARG_LONG
: fprintf(file
, "(long)a%d", i
); break;
336 case ARG_PTR
: fprintf(file
, "(void*)a%d", i
); break;
337 case ARG_STR
: fprintf(file
, "(char*)a%d", i
); break;
338 case ARG_WSTR
: fprintf(file
, "(wchar_t*)a%d", i
); break;
339 case ARG_DBL
: fprintf(file
, "(double)a%d", i
); break;
340 case ARG_INT64
: fprintf(file
, "(__int64)a%d", i
); break;
341 case ARG_INT128
: fprintf(file
, "wine_dbgstr_guid(&a%d)", i
); break;
342 case ARG_FLOAT
: fprintf(file
, "(float)a%d", i
); break;
345 fprintf(file
, ");\n");
347 if (pexp
->nCallingConvention
== CC_STUB
)
349 fprintf(file
, "\t__wine_spec_unimplemented_stub(\"%s\", __FUNCTION__);\n", pszDllName
);
353 if (pexp
->uFlags
& FL_REGISTER
)
359 fprintf(file
, "\tretval = ");
361 fprintf(file
, "%.*s(", pexp
->strName
.len
, pexp
->strName
.buf
);
363 for (i
= 0; i
< pexp
->nArgCount
; i
++)
365 if (i
!= 0) fprintf(file
, ", ");
366 fprintf(file
, "a%d", i
);
368 fprintf(file
, ");\n");
372 fprintf(file
, "\treturn 0;\n}\n\n");
373 else if ((pexp
->uFlags
& FL_REGISTER
) == 0)
375 if (pexp
->uFlags
& FL_RET64
)
377 fprintf(file
, "\tif (TRACE_ON(relay))\n\t\tDPRINTF(\"%s: %.*s: retval = %%\"PRIx64\"\\n\", retval);\n",
378 pszDllName
, pexp
->strName
.len
, pexp
->strName
.buf
);
382 fprintf(file
, "\tif (TRACE_ON(relay))\n\t\tDPRINTF(\"%s: %.*s: retval = 0x%%lx\\n\", retval);\n",
383 pszDllName
, pexp
->strName
.len
, pexp
->strName
.buf
);
385 fprintf(file
, "\treturn retval;\n}\n\n");
392 OutputHeader_asmstub(FILE *file
, char *libname
)
394 fprintf(file
, "; File generated automatically, do not edit! \n\n");
396 if (giArch
== ARCH_X86
)
398 fprintf(file
, ".586\n.model flat\n.code\n");
400 else if (giArch
== ARCH_AMD64
)
402 fprintf(file
, ".code\n");
404 else if (giArch
== ARCH_ARM
)
406 fprintf(file
, " AREA |.text|,ALIGN=2,CODE,READONLY\n\n");
411 Output_stublabel(FILE *fileDest
, char* pszSymbolName
)
413 if (giArch
== ARCH_ARM
)
416 "\tEXPORT |%s| [FUNC]\n|%s|\n",
423 "PUBLIC %s\n%s: nop\n",
430 OutputLine_asmstub(FILE *fileDest
, EXPORT
*pexp
)
432 char szNameBuffer
[128];
434 /* Handle autoname */
435 if (pexp
->strName
.len
== 1 && pexp
->strName
.buf
[0] == '@')
437 sprintf(szNameBuffer
, "%sordinal%d\n%sordinal%d: nop\n",
438 gpszUnderscore
, pexp
->nOrdinal
, gpszUnderscore
, pexp
->nOrdinal
);
440 else if (giArch
!= ARCH_X86
)
442 sprintf(szNameBuffer
, "_stub_%.*s",
443 pexp
->strName
.len
, pexp
->strName
.buf
);
445 else if (pexp
->nCallingConvention
== CC_STDCALL
)
447 sprintf(szNameBuffer
, "__stub_%.*s@%d",
448 pexp
->strName
.len
, pexp
->strName
.buf
, pexp
->nStackBytes
);
450 else if (pexp
->nCallingConvention
== CC_FASTCALL
)
452 sprintf(szNameBuffer
, "@_stub_%.*s@%d",
453 pexp
->strName
.len
, pexp
->strName
.buf
, pexp
->nStackBytes
);
455 else if ((pexp
->nCallingConvention
== CC_CDECL
) ||
456 (pexp
->nCallingConvention
== CC_THISCALL
) ||
457 (pexp
->nCallingConvention
== CC_EXTERN
) ||
458 (pexp
->nCallingConvention
== CC_STUB
))
460 sprintf(szNameBuffer
, "__stub_%.*s",
461 pexp
->strName
.len
, pexp
->strName
.buf
);
465 fprintf(stderr
, "Invalid calling convention");
469 Output_stublabel(fileDest
, szNameBuffer
);
475 OutputHeader_def(FILE *file
, char *libname
)
478 "; File generated automatically, do not edit!\n\n"
485 PrintName(FILE *fileDest
, EXPORT
*pexp
, PSTRING pstr
, int fDeco
)
487 const char *pcName
= pstr
->buf
;
488 int nNameLength
= pstr
->len
;
489 const char* pcDot
, *pcAt
;
491 /* Check for non-x86 first */
492 if (giArch
!= ARCH_X86
)
494 /* Does the string already have stdcall decoration? */
495 pcAt
= ScanToken(pcName
, '@');
496 if (pcAt
&& (pcAt
< (pcName
+ nNameLength
)) && (pcName
[0] == '_'))
498 /* Skip leading underscore and remove trailing decoration */
500 nNameLength
= pcAt
- pcName
;
503 /* Print the undecorated function name */
504 fprintf(fileDest
, "%.*s", nNameLength
, pcName
);
507 ((pexp
->nCallingConvention
== CC_STDCALL
) ||
508 (pexp
->nCallingConvention
== CC_FASTCALL
)))
510 /* Scan for a dll forwarding dot */
511 pcDot
= ScanToken(pcName
, '.');
514 /* First print the dll name, followed by a dot */
515 nNameLength
= pcDot
- pcName
;
516 fprintf(fileDest
, "%.*s.", nNameLength
, pcName
);
518 /* Now the actual function name */
520 nNameLength
= pexp
->strTarget
.len
- nNameLength
- 1;
523 /* Does the string already have decoration? */
524 pcAt
= ScanToken(pcName
, '@');
525 if (pcAt
&& (pcAt
< (pcName
+ nNameLength
)))
527 /* On GCC, we need to remove the leading stdcall underscore */
528 if (!gbMSComp
&& (pexp
->nCallingConvention
== CC_STDCALL
))
534 /* Print the already decorated function name */
535 fprintf(fileDest
, "%.*s", nNameLength
, pcName
);
539 /* Print the prefix, but skip it for (GCC && stdcall) */
540 if (gbMSComp
|| (pexp
->nCallingConvention
!= CC_STDCALL
))
542 fprintf(fileDest
, "%c", pexp
->nCallingConvention
== CC_FASTCALL
? '@' : '_');
545 /* Print the name with trailing decoration */
546 fprintf(fileDest
, "%.*s@%d", nNameLength
, pcName
, pexp
->nStackBytes
);
551 /* Print the undecorated function name */
552 fprintf(fileDest
, "%.*s", nNameLength
, pcName
);
557 OutputLine_def_MS(FILE *fileDest
, EXPORT
*pexp
)
559 PrintName(fileDest
, pexp
, &pexp
->strName
, 0);
563 /* Redirect to a stub function, to get the right decoration in the lib */
564 fprintf(fileDest
, "=_stub_%.*s", pexp
->strName
.len
, pexp
->strName
.buf
);
566 else if (pexp
->strTarget
.buf
)
568 if (pexp
->strName
.buf
[0] == '?')
570 //fprintf(stderr, "warning: ignoring C++ redirection %.*s -> %.*s\n",
571 // pexp->strName.len, pexp->strName.buf, pexp->strTarget.len, pexp->strTarget.buf);
575 fprintf(fileDest
, "=");
577 /* If the original name was decorated, use decoration in the forwarder as well */
578 if ((giArch
== ARCH_X86
) && ScanToken(pexp
->strName
.buf
, '@') &&
579 !ScanToken(pexp
->strTarget
.buf
, '@') &&
580 ((pexp
->nCallingConvention
== CC_STDCALL
) ||
581 (pexp
->nCallingConvention
== CC_FASTCALL
)) )
583 PrintName(fileDest
, pexp
, &pexp
->strTarget
, 1);
587 /* Write the undecorated redirection name */
588 fprintf(fileDest
, "%.*s", pexp
->strTarget
.len
, pexp
->strTarget
.buf
);
592 else if (((pexp
->uFlags
& FL_STUB
) || (pexp
->nCallingConvention
== CC_STUB
)) &&
593 (pexp
->strName
.buf
[0] == '?'))
595 /* C++ stubs are forwarded to C stubs */
596 fprintf(fileDest
, "=stub_function%d", pexp
->nNumber
);
598 else if (gbTracing
&& ((pexp
->uFlags
& FL_NORELAY
) == 0) && (pexp
->nCallingConvention
== CC_STDCALL
) &&
599 (pexp
->strName
.buf
[0] != '?'))
601 /* Redirect it to the relay-tracing trampoline */
602 fprintf(fileDest
, "=$relaytrace$%.*s", pexp
->strName
.len
, pexp
->strName
.buf
);
607 OutputLine_def_GCC(FILE *fileDest
, EXPORT
*pexp
)
610 /* Print the function name, with decoration for export libs */
611 PrintName(fileDest
, pexp
, &pexp
->strName
, gbImportLib
);
612 DbgPrint("Generating def line for '%.*s'\n", pexp
->strName
.len
, pexp
->strName
.buf
);
614 /* Check if this is a forwarded export */
615 if (pexp
->strTarget
.buf
)
617 int fIsExternal
= !!ScanToken(pexp
->strTarget
.buf
, '.');
618 DbgPrint("Got redirect '%.*s'\n", pexp
->strTarget
.len
, pexp
->strTarget
.buf
);
620 /* print the target name, don't decorate if it is external */
621 fprintf(fileDest
, "=");
622 PrintName(fileDest
, pexp
, &pexp
->strTarget
, !fIsExternal
);
624 else if (((pexp
->uFlags
& FL_STUB
) || (pexp
->nCallingConvention
== CC_STUB
)) &&
625 (pexp
->strName
.buf
[0] == '?'))
627 /* C++ stubs are forwarded to C stubs */
628 fprintf(fileDest
, "=stub_function%d", pexp
->nNumber
);
630 else if (gbTracing
&& ((pexp
->uFlags
& FL_NORELAY
) == 0) &&
631 (pexp
->nCallingConvention
== CC_STDCALL
) &&
632 (pexp
->strName
.buf
[0] != '?'))
634 /* Redirect it to the relay-tracing trampoline */
637 fprintf(fileDest
, "=");
638 sprintf(buf
, "$relaytrace$%.*s", pexp
->strName
.len
, pexp
->strName
.buf
);
640 strTarget
.len
= pexp
->strName
.len
+ 12;
641 PrintName(fileDest
, pexp
, &strTarget
, 1);
645 /* Special handling for stdcall and fastcall */
646 if ((giArch
== ARCH_X86
) &&
647 ((pexp
->nCallingConvention
== CC_STDCALL
) ||
648 (pexp
->nCallingConvention
== CC_FASTCALL
)))
650 /* Is this the import lib? */
653 /* Is the name in the spec file decorated? */
654 const char* pcDeco
= ScanToken(pexp
->strName
.buf
, '@');
655 if (pcDeco
&& (pcDeco
< pexp
->strName
.buf
+ pexp
->strName
.len
))
657 /* Write the name including the leading @ */
658 fprintf(fileDest
, "==%.*s", pexp
->strName
.len
, pexp
->strName
.buf
);
661 else if ((!pexp
->strTarget
.buf
) && !(bTracing
))
663 /* Write a forwarder to the actual decorated symbol */
664 fprintf(fileDest
, "=");
665 PrintName(fileDest
, pexp
, &pexp
->strName
, 1);
671 OutputLine_def(FILE *fileDest
, EXPORT
*pexp
)
673 DbgPrint("OutputLine_def: '%.*s'...\n", pexp
->strName
.len
, pexp
->strName
.buf
);
674 fprintf(fileDest
, " ");
677 OutputLine_def_MS(fileDest
, pexp
);
679 OutputLine_def_GCC(fileDest
, pexp
);
681 if (pexp
->uFlags
& FL_ORDINAL
)
683 fprintf(fileDest
, " @%d", pexp
->nOrdinal
);
686 if (pexp
->uFlags
& FL_NONAME
)
688 fprintf(fileDest
, " NONAME");
691 /* Either PRIVATE or DATA */
692 if (pexp
->uFlags
& FL_PRIVATE
)
694 fprintf(fileDest
, " PRIVATE");
696 else if (pexp
->nCallingConvention
== CC_EXTERN
)
698 fprintf(fileDest
, " DATA");
701 fprintf(fileDest
, "\n");
707 ParseFile(char* pcStart
, FILE *fileDest
, PFNOUTLINE OutputLine
)
716 //fprintf(stderr, "info: line %d, pcStart:'%.30s'\n", nLine, pcStart);
721 for (pcLine
= pcStart
; *pcLine
; pcLine
= NextLine(pcLine
), nLine
++)
729 //if (!strncmp(pcLine, "22 stdcall @(long) MPR_Alloc",28))
732 //fprintf(stderr, "info: line %d, token:'%d, %.20s'\n",
733 // nLine, TokenLength(pcLine), pcLine);
735 /* Skip white spaces */
736 while (*pc
== ' ' || *pc
== '\t') pc
++;
738 /* Skip empty lines, stop at EOF */
739 if (*pc
== ';' || *pc
<= '#') continue;
740 if (*pc
== 0) return 0;
742 //fprintf(stderr, "info: line %d, token:'%.*s'\n",
743 // nLine, TokenLength(pc), pc);
745 /* Now we should get either an ordinal or @ */
750 exp
.nOrdinal
= atol(pc
);
751 /* The import lib should contain the ordinal only if -ordinal was specified */
753 exp
.uFlags
|= FL_ORDINAL
;
756 /* Go to next token (type) */
757 if (!(pc
= NextToken(pc
)))
759 fprintf(stderr
, "%s line %d: error: unexpected end of line\n", pszSourceFileName
, nLine
);
763 //fprintf(stderr, "info: Token:'%.*s'\n", TokenLength(pc), pc);
765 /* Now we should get the type */
766 if (CompareToken(pc
, "stdcall"))
768 exp
.nCallingConvention
= CC_STDCALL
;
770 else if (CompareToken(pc
, "cdecl") ||
771 CompareToken(pc
, "varargs"))
773 exp
.nCallingConvention
= CC_CDECL
;
775 else if (CompareToken(pc
, "fastcall"))
777 exp
.nCallingConvention
= CC_FASTCALL
;
779 else if (CompareToken(pc
, "thiscall"))
781 exp
.nCallingConvention
= CC_THISCALL
;
783 else if (CompareToken(pc
, "extern"))
785 exp
.nCallingConvention
= CC_EXTERN
;
787 else if (CompareToken(pc
, "stub"))
789 exp
.nCallingConvention
= CC_STUB
;
793 fprintf(stderr
, "%s line %d: error: expected callconv, got '%.*s' %d\n",
794 pszSourceFileName
, nLine
, TokenLength(pc
), pc
, *pc
);
798 //fprintf(stderr, "info: nCallingConvention: %d\n", exp.nCallingConvention);
800 /* Go to next token (options or name) */
801 if (!(pc
= NextToken(pc
)))
803 fprintf(stderr
, "fail2\n");
811 if (CompareToken(pc
, "-arch"))
813 /* Default to not included */
817 /* Look if we are included */
818 while (*pc
== '=' || *pc
== ',')
821 if (CompareToken(pc
, pszArchString
) ||
822 CompareToken(pc
, pszArchString2
))
827 /* Skip to next arch or end */
828 while (*pc
> ',') pc
++;
831 else if (CompareToken(pc
, "-i386"))
833 if (giArch
!= ARCH_X86
) included
= 0;
835 else if (CompareToken(pc
, "-private"))
837 exp
.uFlags
|= FL_PRIVATE
;
839 else if (CompareToken(pc
, "-noname"))
841 exp
.uFlags
|= FL_ORDINAL
| FL_NONAME
;
843 else if (CompareToken(pc
, "-ordinal"))
845 exp
.uFlags
|= FL_ORDINAL
;
846 /* GCC doesn't automatically import by ordinal if an ordinal
847 * is found in the def file. Force it. */
848 if (gbImportLib
&& !gbMSComp
)
849 exp
.uFlags
|= FL_NONAME
;
851 else if (CompareToken(pc
, "-stub"))
853 exp
.uFlags
|= FL_STUB
;
855 else if (CompareToken(pc
, "-norelay"))
857 exp
.uFlags
|= FL_NORELAY
;
859 else if (CompareToken(pc
, "-ret64"))
861 exp
.uFlags
|= FL_RET64
;
863 else if (CompareToken(pc
, "-register"))
865 exp
.uFlags
|= FL_REGISTER
;
869 fprintf(stderr
, "info: ignored option: '%.*s'\n",
870 TokenLength(pc
), pc
);
873 /* Go to next token */
877 //fprintf(stderr, "info: Name:'%.10s'\n", pc);
879 /* If arch didn't match ours, skip this entry */
880 if (!included
) continue;
883 exp
.strName
.buf
= pc
;
884 exp
.strName
.len
= TokenLength(pc
);
885 DbgPrint("Got name: '%.*s'\n", exp
.strName
.len
, exp
.strName
.buf
);
887 /* Check for autoname */
888 if ((exp
.strName
.len
== 1) && (exp
.strName
.buf
[0] == '@'))
890 sprintf(namebuffer
, "ordinal%d", exp
.nOrdinal
);
891 exp
.strName
.len
= strlen(namebuffer
);
892 exp
.strName
.buf
= namebuffer
;
893 exp
.uFlags
|= FL_ORDINAL
| FL_NONAME
;
896 /* Handle parameters */
898 if (exp
.nCallingConvention
!= CC_EXTERN
&&
899 exp
.nCallingConvention
!= CC_STUB
)
901 /* Go to next token */
902 if (!(pc
= NextToken(pc
)))
904 fprintf(stderr
, "%s line %d: error: expected token\n", pszSourceFileName
, nLine
);
911 fprintf(stderr
, "%s line %d: error: expected '('\n", pszSourceFileName
, nLine
);
915 /* Skip whitespaces */
916 while (*pc
== ' ' || *pc
== '\t') pc
++;
921 if (CompareToken(pc
, "long"))
923 exp
.nStackBytes
+= 4;
924 exp
.anArgs
[exp
.nArgCount
] = ARG_LONG
;
926 else if (CompareToken(pc
, "double"))
928 exp
.nStackBytes
+= 8;
929 exp
.anArgs
[exp
.nArgCount
] = ARG_DBL
;
931 else if (CompareToken(pc
, "ptr"))
933 exp
.nStackBytes
+= 4; // sizeof(void*) on x86
934 exp
.anArgs
[exp
.nArgCount
] = ARG_PTR
;
936 else if (CompareToken(pc
, "str"))
938 exp
.nStackBytes
+= 4; // sizeof(void*) on x86
939 exp
.anArgs
[exp
.nArgCount
] = ARG_STR
;
941 else if (CompareToken(pc
, "wstr"))
943 exp
.nStackBytes
+= 4; // sizeof(void*) on x86
944 exp
.anArgs
[exp
.nArgCount
] = ARG_WSTR
;
946 else if (CompareToken(pc
, "int64"))
948 exp
.nStackBytes
+= 8;
949 exp
.anArgs
[exp
.nArgCount
] = ARG_INT64
;
951 else if (CompareToken(pc
, "int128"))
953 exp
.nStackBytes
+= 16;
954 exp
.anArgs
[exp
.nArgCount
] = ARG_INT128
;
956 else if (CompareToken(pc
, "float"))
958 exp
.nStackBytes
+= 4;
959 exp
.anArgs
[exp
.nArgCount
] = ARG_FLOAT
;
962 fprintf(stderr
, "%s line %d: error: expected type, got: %.10s\n", pszSourceFileName
, nLine
, pc
);
966 /* Go to next parameter */
967 if (!(pc
= NextToken(pc
)))
969 fprintf(stderr
, "fail5\n");
977 fprintf(stderr
, "%s line %d: error: expected ')'\n", pszSourceFileName
, nLine
);
982 /* Handle special stub cases */
983 if (exp
.nCallingConvention
== CC_STUB
)
985 /* Check for c++ mangled name */
988 //printf("Found c++ mangled name...\n");
993 /* Check for stdcall name */
994 const char *p
= ScanToken(pc
, '@');
995 if (p
&& (p
- pc
< exp
.strName
.len
))
999 /* Truncate the name to before the @ */
1000 exp
.strName
.len
= (int)(p
- pc
);
1001 if (exp
.strName
.len
< 1)
1003 fprintf(stderr
, "%s line %d: error: unexpected @ found\n", pszSourceFileName
, nLine
);
1006 exp
.nStackBytes
= atoi(p
+ 1);
1007 exp
.nArgCount
= exp
.nStackBytes
/ 4;
1008 exp
.nCallingConvention
= CC_STDCALL
;
1009 exp
.uFlags
|= FL_STUB
;
1010 for (i
= 0; i
< exp
.nArgCount
; i
++)
1011 exp
.anArgs
[i
] = ARG_LONG
;
1016 /* Get optional redirection */
1020 exp
.strTarget
.buf
= pc
;
1021 exp
.strTarget
.len
= TokenLength(pc
);
1023 /* Check syntax (end of line) */
1026 fprintf(stderr
, "%s line %d: error: additional tokens after ')'\n", pszSourceFileName
, nLine
);
1030 /* Don't relay-trace forwarded functions */
1031 exp
.uFlags
|= FL_NORELAY
;
1035 exp
.strTarget
.buf
= NULL
;
1036 exp
.strTarget
.len
= 0;
1039 /* Check for no-name without ordinal */
1040 if ((exp
.uFlags
& FL_ORDINAL
) && (exp
.nOrdinal
== -1))
1042 fprintf(stderr
, "%s line %d: error: ordinal export without ordinal!\n", pszSourceFileName
, nLine
);
1046 if (!gbMSComp
&& !gbNotPrivateNoWarn
&& gbImportLib
&& !(exp
.uFlags
& FL_PRIVATE
))
1048 for (i
= 0; i
< ARRAYSIZE(astrShouldBePrivate
); i
++)
1050 if (strlen(astrShouldBePrivate
[i
]) == exp
.strName
.len
&&
1051 strncmp(exp
.strName
.buf
, astrShouldBePrivate
[i
], exp
.strName
.len
) == 0)
1053 fprintf(stderr
, "%s line %d: warning: export of '%.*s' should be PRIVATE\n",
1054 pszSourceFileName
, nLine
, exp
.strName
.len
, exp
.strName
.buf
);
1059 OutputLine(fileDest
, &exp
);
1068 printf("syntax: spec2def [<options> ...] <spec file>\n"
1069 "Possible options:\n"
1070 " -h --help print this help screen\n"
1071 " -l=<file> generate an asm lib stub\n"
1072 " -d=<file> generate a def file\n"
1073 " -s=<file> generate a stub file\n"
1074 " --ms MSVC compatibility\n"
1075 " -n=<name> name of the dll\n"
1076 " --implib generate a def file for an import library\n"
1077 " --no-private-warnings suppress warnings about symbols that should be -private\n"
1078 " -a=<arch> set architecture to <arch> (i386, x86_64, arm)\n"
1079 " --with-tracing generate wine-like \"+relay\" trace trampolines (needs -s)\n");
1082 int main(int argc
, char *argv
[])
1085 char *pszSource
, *pszDefFileName
= NULL
, *pszStubFileName
= NULL
, *pszLibStubName
= NULL
;
1086 char achDllName
[40];
1097 for (i
= 1; i
< argc
&& *argv
[i
] == '-'; i
++)
1099 if ((strcasecmp(argv
[i
], "--help") == 0) ||
1100 (strcasecmp(argv
[i
], "-h") == 0))
1105 else if (argv
[i
][1] == 'd' && argv
[i
][2] == '=')
1107 pszDefFileName
= argv
[i
] + 3;
1109 else if (argv
[i
][1] == 'l' && argv
[i
][2] == '=')
1111 pszLibStubName
= argv
[i
] + 3;
1113 else if (argv
[i
][1] == 's' && argv
[i
][2] == '=')
1115 pszStubFileName
= argv
[i
] + 3;
1117 else if (argv
[i
][1] == 'n' && argv
[i
][2] == '=')
1119 pszDllName
= argv
[i
] + 3;
1121 else if (strcasecmp(argv
[i
], "--implib") == 0)
1125 else if (strcasecmp(argv
[i
], "--ms") == 0)
1129 else if (strcasecmp(argv
[i
], "--no-private-warnings") == 0)
1131 gbNotPrivateNoWarn
= 1;
1133 else if (strcasecmp(argv
[i
], "--with-tracing") == 0)
1135 if (!pszStubFileName
)
1137 fprintf(stderr
, "Error: cannot use --with-tracing without -s option.\n");
1142 else if (argv
[i
][1] == 'a' && argv
[i
][2] == '=')
1144 pszArchString
= argv
[i
] + 3;
1148 fprintf(stderr
, "Unrecognized option: %s\n", argv
[i
]);
1153 if (strcasecmp(pszArchString
, "i386") == 0)
1156 gpszUnderscore
= "_";
1158 else if (strcasecmp(pszArchString
, "x86_64") == 0) giArch
= ARCH_AMD64
;
1159 else if (strcasecmp(pszArchString
, "ia64") == 0) giArch
= ARCH_IA64
;
1160 else if (strcasecmp(pszArchString
, "arm") == 0) giArch
= ARCH_ARM
;
1161 else if (strcasecmp(pszArchString
, "ppc") == 0) giArch
= ARCH_PPC
;
1163 if ((giArch
== ARCH_AMD64
) || (giArch
== ARCH_IA64
))
1165 pszArchString2
= "win64";
1168 pszArchString2
= "win32";
1170 /* Set a default dll name */
1176 p1
= strrchr(argv
[i
], '\\');
1177 if (!p1
) p1
= strrchr(argv
[i
], '/');
1178 p2
= p1
= p1
? p1
+ 1 : argv
[i
];
1180 /* walk up to '.' */
1181 while (*p2
!= '.' && *p2
!= 0) p2
++;
1183 if (len
>= sizeof(achDllName
) - 5)
1185 fprintf(stderr
, "name too long: %s\n", p1
);
1189 strncpy(achDllName
, p1
, len
);
1190 strncpy(achDllName
+ len
, ".dll", sizeof(achDllName
) - len
);
1191 pszDllName
= achDllName
;
1194 /* Open input file */
1195 pszSourceFileName
= argv
[i
];
1196 file
= fopen(pszSourceFileName
, "r");
1199 fprintf(stderr
, "error: could not open file %s\n", pszSourceFileName
);
1204 fseek(file
, 0, SEEK_END
);
1205 nFileSize
= ftell(file
);
1208 /* Allocate memory buffer */
1209 pszSource
= malloc(nFileSize
+ 1);
1216 /* Load input file into memory */
1217 nFileSize
= fread(pszSource
, 1, nFileSize
, file
);
1220 /* Zero terminate the source */
1221 pszSource
[nFileSize
] = '\0';
1225 /* Open output file */
1226 file
= fopen(pszDefFileName
, "w");
1229 fprintf(stderr
, "error: could not open output file %s\n", argv
[i
+ 1]);
1233 OutputHeader_def(file
, pszDllName
);
1234 result
= ParseFile(pszSource
, file
, OutputLine_def
);
1238 if (pszStubFileName
)
1240 /* Open output file */
1241 file
= fopen(pszStubFileName
, "w");
1244 fprintf(stderr
, "error: could not open output file %s\n", argv
[i
+ 1]);
1248 OutputHeader_stub(file
);
1249 result
= ParseFile(pszSource
, file
, OutputLine_stub
);
1255 /* Open output file */
1256 file
= fopen(pszLibStubName
, "w");
1259 fprintf(stderr
, "error: could not open output file %s\n", argv
[i
+ 1]);
1263 OutputHeader_asmstub(file
, pszDllName
);
1264 result
= ParseFile(pszSource
, file
, OutputLine_asmstub
);
1265 fprintf(file
, "\n END\n");