7 #define strcasecmp _stricmp
10 typedef struct _STRING
20 int nCallingConvention
;
38 typedef int (*PFNOUTLINE
)(FILE *, EXPORT
*);
42 int giArch
= ARCH_X86
;
43 char *pszArchString
= "i386";
46 char *gpszUnderscore
= "";
48 #define DbgPrint(...) (!gbDebug || fprintf(stderr, __VA_ARGS__))
83 char* astrCallingConventions
[] =
96 return ((chr
<= ',' && chr
!= '$') ||
97 (chr
>= ':' && chr
< '?') );
101 CompareToken(const char *token
, const char *comparand
)
105 if (*token
!= *comparand
) return 0;
109 if (!IsSeparator(*token
)) return 0;
114 ScanToken(const char *token
, char chr
)
116 while (!IsSeparator(*token
))
118 if (*token
== chr
) return token
;
129 if (pc
[0] == '\n' && pc
[1] == '\r') return pc
+ 2;
130 else if (pc
[0] == '\n') return pc
+ 1;
137 TokenLength(char *pc
)
141 while (!IsSeparator(*pc
++)) length
++;
150 while (!IsSeparator(*pc
)) pc
++;
152 /* Skip white spaces */
153 while (*pc
== ' ' || *pc
== '\t') pc
++;
155 /* Check for end of line */
156 if (*pc
== '\n' || *pc
== '\r' || *pc
== 0) return 0;
158 /* Check for comment */
159 if (*pc
== '#' || *pc
== ';') return 0;
165 OutputHeader_stub(FILE *file
)
167 fprintf(file
, "/* This file is autogenerated, do not edit. */\n\n"
168 "#include <stubs.h>\n");
172 fprintf(file
, "#include <wine/debug.h>\n");
173 fprintf(file
, "#include <inttypes.h>\n");
174 fprintf(file
, "WINE_DECLARE_DEBUG_CHANNEL(relay);\n");
181 OutputLine_stub(FILE *file
, EXPORT
*pexp
)
185 int bInPrototype
= 0;
187 if (pexp
->nCallingConvention
!= CC_STUB
&&
188 (pexp
->uFlags
& FL_STUB
) == 0)
190 /* Only relay trace stdcall C functions */
191 if (!gbTracing
|| (pexp
->nCallingConvention
!= CC_STDCALL
)
192 || (pexp
->uFlags
& FL_NORELAY
)
193 || (pexp
->strName
.buf
[0] == '?'))
200 /* Declare the "real" function */
203 fprintf(file
, "extern ");
209 if (pexp
->uFlags
& FL_REGISTER
)
211 /* FIXME: Not sure this is right */
212 fprintf(file
, "void ");
214 else if (pexp
->uFlags
& FL_RET64
)
216 fprintf(file
, "__int64 ");
220 fprintf(file
, "int ");
223 if ((giArch
== ARCH_X86
) &&
224 pexp
->nCallingConvention
== CC_STDCALL
)
226 fprintf(file
, "__stdcall ");
230 if (pexp
->strName
.buf
[0] == '?')
232 fprintf(file
, "stub_function%d(", pexp
->nNumber
);
236 if (!bRelay
|| bInPrototype
)
237 fprintf(file
, "%.*s(", pexp
->strName
.len
, pexp
->strName
.buf
);
239 fprintf(file
, "$relaytrace$%.*s(", pexp
->strName
.len
, pexp
->strName
.buf
);
242 for (i
= 0; i
< pexp
->nArgCount
; i
++)
244 if (i
!= 0) fprintf(file
, ", ");
245 switch (pexp
->anArgs
[i
])
247 case ARG_LONG
: fprintf(file
, "long"); break;
248 case ARG_PTR
: fprintf(file
, "void*"); break;
249 case ARG_STR
: fprintf(file
, "char*"); break;
250 case ARG_WSTR
: fprintf(file
, "wchar_t*"); break;
251 case ARG_DBL
: fprintf(file
, "double"); break;
252 case ARG_INT64
: fprintf(file
, "__int64"); break;
253 case ARG_INT128
: fprintf(file
, "__int128"); break;
254 case ARG_FLOAT
: fprintf(file
, "float"); break;
256 fprintf(file
, " a%d", i
);
261 fprintf(file
, ");\n\n");
263 } while (bInPrototype
--);
267 fprintf(file
, ")\n{\n\tDbgPrint(\"WARNING: calling stub %.*s(",
268 pexp
->strName
.len
, pexp
->strName
.buf
);
272 fprintf(file
, ")\n{\n");
273 if (pexp
->uFlags
& FL_REGISTER
)
275 /* No return value */
277 else if (pexp
->uFlags
& FL_RET64
)
279 fprintf(file
, "\t__int64 retval;\n");
283 fprintf(file
, "\tint retval;\n");
285 fprintf(file
, "\tif (TRACE_ON(relay))\n\t\tDPRINTF(\"%s: %.*s(",
286 pszDllName
, pexp
->strName
.len
, pexp
->strName
.buf
);
289 for (i
= 0; i
< pexp
->nArgCount
; i
++)
291 if (i
!= 0) fprintf(file
, ",");
292 switch (pexp
->anArgs
[i
])
294 case ARG_LONG
: fprintf(file
, "0x%%lx"); break;
295 case ARG_PTR
: fprintf(file
, "0x%%p"); break;
296 case ARG_STR
: fprintf(file
, "'%%s'"); break;
297 case ARG_WSTR
: fprintf(file
, "'%%ws'"); break;
298 case ARG_DBL
: fprintf(file
, "%%f"); break;
299 case ARG_INT64
: fprintf(file
, "%%\"PRix64\""); break;
300 case ARG_INT128
: fprintf(file
, "%%\"PRix128\""); break;
301 case ARG_FLOAT
: fprintf(file
, "%%f"); break;
304 fprintf(file
, ")\\n\"");
306 for (i
= 0; i
< pexp
->nArgCount
; i
++)
309 switch (pexp
->anArgs
[i
])
311 case ARG_LONG
: fprintf(file
, "(long)a%d", i
); break;
312 case ARG_PTR
: fprintf(file
, "(void*)a%d", i
); break;
313 case ARG_STR
: fprintf(file
, "(char*)a%d", i
); break;
314 case ARG_WSTR
: fprintf(file
, "(wchar_t*)a%d", i
); break;
315 case ARG_DBL
: fprintf(file
, "(double)a%d", i
); break;
316 case ARG_INT64
: fprintf(file
, "(__int64)a%d", i
); break;
317 case ARG_INT128
: fprintf(file
, "(__int128)a%d", i
); break;
318 case ARG_FLOAT
: fprintf(file
, "(float)a%d", i
); break;
321 fprintf(file
, ");\n");
323 if (pexp
->nCallingConvention
== CC_STUB
)
325 fprintf(file
, "\t__wine_spec_unimplemented_stub(\"%s\", __FUNCTION__);\n", pszDllName
);
329 if (pexp
->uFlags
& FL_REGISTER
)
335 fprintf(file
, "\tretval = ");
337 fprintf(file
, "%.*s(", pexp
->strName
.len
, pexp
->strName
.buf
);
339 for (i
= 0; i
< pexp
->nArgCount
; i
++)
341 if (i
!= 0) fprintf(file
, ", ");
342 fprintf(file
, "a%d", i
);
344 fprintf(file
, ");\n");
348 fprintf(file
, "\treturn 0;\n}\n\n");
349 else if ((pexp
->uFlags
& FL_REGISTER
) == 0)
351 if (pexp
->uFlags
& FL_RET64
)
353 fprintf(file
, "\tif (TRACE_ON(relay))\n\t\tDPRINTF(\"%s: %.*s: retval = %%\"PRIx64\"\\n\", retval);\n",
354 pszDllName
, pexp
->strName
.len
, pexp
->strName
.buf
);
358 fprintf(file
, "\tif (TRACE_ON(relay))\n\t\tDPRINTF(\"%s: %.*s: retval = 0x%%lx\\n\", retval);\n",
359 pszDllName
, pexp
->strName
.len
, pexp
->strName
.buf
);
361 fprintf(file
, "\treturn retval;\n}\n\n");
368 OutputHeader_asmstub(FILE *file
, char *libname
)
370 fprintf(file
, "; File generated automatically, do not edit! \n\n");
372 if (giArch
== ARCH_X86
)
374 fprintf(file
, ".586\n.model flat\n.code\n");
376 else if (giArch
== ARCH_AMD64
)
378 fprintf(file
, ".code\n");
380 else if (giArch
== ARCH_ARM
)
383 " AREA |.text|,ALIGN=2,CODE,READONLY\n\n");
388 Output_stublabel(FILE *fileDest
, char* pszSymbolName
)
390 if (giArch
== ARCH_ARM
)
393 " EXPORT %s [FUNC]\n%s\n",
400 "PUBLIC %s\n%s: nop\n",
407 OutputLine_asmstub(FILE *fileDest
, EXPORT
*pexp
)
409 char szNameBuffer
[128];
411 /* Handle autoname */
412 if (pexp
->strName
.len
== 1 && pexp
->strName
.buf
[0] == '@')
414 sprintf(szNameBuffer
, "%sordinal%d\n%sordinal%d: nop\n",
415 gpszUnderscore
, pexp
->nOrdinal
, gpszUnderscore
, pexp
->nOrdinal
);
417 else if (giArch
!= ARCH_X86
)
419 sprintf(szNameBuffer
, "_stub_%.*s",
420 pexp
->strName
.len
, pexp
->strName
.buf
);
422 else if (pexp
->nCallingConvention
== CC_STDCALL
)
424 sprintf(szNameBuffer
, "__stub_%.*s@%d",
425 pexp
->strName
.len
, pexp
->strName
.buf
, pexp
->nStackBytes
);
427 else if (pexp
->nCallingConvention
== CC_FASTCALL
)
429 sprintf(szNameBuffer
, "@_stub_%.*s@%d",
430 pexp
->strName
.len
, pexp
->strName
.buf
, pexp
->nStackBytes
);
432 else if ((pexp
->nCallingConvention
== CC_CDECL
) ||
433 (pexp
->nCallingConvention
== CC_THISCALL
) ||
434 (pexp
->nCallingConvention
== CC_EXTERN
) ||
435 (pexp
->nCallingConvention
== CC_STUB
))
437 sprintf(szNameBuffer
, "__stub_%.*s",
438 pexp
->strName
.len
, pexp
->strName
.buf
);
442 fprintf(stderr
, "Invalid calling convention");
446 Output_stublabel(fileDest
, szNameBuffer
);
452 OutputHeader_def(FILE *file
, char *libname
)
455 "; File generated automatically, do not edit!\n\n"
462 PrintName(FILE *fileDest
, EXPORT
*pexp
, PSTRING pstr
, int fDeco
)
464 const char *pcName
= pstr
->buf
;
465 int nNameLength
= pstr
->len
;
466 const char* pcDot
, *pcAt
;
468 /* Check for non-x86 first */
469 if (giArch
!= ARCH_X86
)
471 /* Does the string already have stdcall decoration? */
472 pcAt
= ScanToken(pcName
, '@');
473 if (pcAt
&& (pcAt
< (pcName
+ nNameLength
)) && (pcName
[0] == '_'))
475 /* Skip leading underscore and remove trailing decoration */
477 nNameLength
= pcAt
- pcName
;
480 /* Print the undecorated function name */
481 fprintf(fileDest
, "%.*s", nNameLength
, pcName
);
484 ((pexp
->nCallingConvention
== CC_STDCALL
) ||
485 (pexp
->nCallingConvention
== CC_FASTCALL
)))
487 /* Scan for a dll forwarding dot */
488 pcDot
= ScanToken(pcName
, '.');
491 /* First print the dll name, followed by a dot */
492 nNameLength
= pcDot
- pcName
;
493 fprintf(fileDest
, "%.*s.", nNameLength
, pcName
);
495 /* Now the actual function name */
497 nNameLength
= pexp
->strTarget
.len
- nNameLength
- 1;
500 /* Does the string already have decoration? */
501 pcAt
= ScanToken(pcName
, '@');
502 if (pcAt
&& (pcAt
< (pcName
+ nNameLength
)))
504 /* On GCC, we need to remove the leading stdcall underscore */
505 if (!gbMSComp
&& (pexp
->nCallingConvention
== CC_STDCALL
))
511 /* Print the already decorated function name */
512 fprintf(fileDest
, "%.*s", nNameLength
, pcName
);
516 /* Print the prefix, but skip it for (GCC && stdcall) */
517 if (gbMSComp
|| (pexp
->nCallingConvention
!= CC_STDCALL
))
519 fprintf(fileDest
, "%c", pexp
->nCallingConvention
== CC_FASTCALL
? '@' : '_');
522 /* Print the name with trailing decoration */
523 fprintf(fileDest
, "%.*s@%d", nNameLength
, pcName
, pexp
->nStackBytes
);
528 /* Print the undecorated function name */
529 fprintf(fileDest
, "%.*s", nNameLength
, pcName
);
534 OutputLine_def_MS(FILE *fileDest
, EXPORT
*pexp
)
536 PrintName(fileDest
, pexp
, &pexp
->strName
, 0);
540 /* Redirect to a stub function, to get the right decoration in the lib */
541 fprintf(fileDest
, "=_stub_%.*s", pexp
->strName
.len
, pexp
->strName
.buf
);
543 else if (pexp
->strTarget
.buf
)
545 if (pexp
->strName
.buf
[0] == '?')
547 fprintf(stderr
, "warning: ignoring C++ redirection %.*s -> %.*s\n",
548 pexp
->strName
.len
, pexp
->strName
.buf
, pexp
->strTarget
.len
, pexp
->strTarget
.buf
);
552 fprintf(fileDest
, "=");
554 /* If the original name was decorated, use decoration in the forwarder as well */
555 if ((giArch
== ARCH_X86
) && ScanToken(pexp
->strName
.buf
, '@') &&
556 !ScanToken(pexp
->strTarget
.buf
, '@') &&
557 ((pexp
->nCallingConvention
== CC_STDCALL
) ||
558 (pexp
->nCallingConvention
== CC_FASTCALL
)) )
560 PrintName(fileDest
, pexp
, &pexp
->strTarget
, 1);
564 /* Write the undecorated redirection name */
565 fprintf(fileDest
, "%.*s", pexp
->strTarget
.len
, pexp
->strTarget
.buf
);
569 else if (((pexp
->uFlags
& FL_STUB
) || (pexp
->nCallingConvention
== CC_STUB
)) &&
570 (pexp
->strName
.buf
[0] == '?'))
572 /* C++ stubs are forwarded to C stubs */
573 fprintf(fileDest
, "=stub_function%d", pexp
->nNumber
);
575 else if (gbTracing
&& ((pexp
->uFlags
& FL_NORELAY
) == 0) && (pexp
->nCallingConvention
== CC_STDCALL
) &&
576 (pexp
->strName
.buf
[0] != '?'))
578 /* Redirect it to the relay-tracing trampoline */
579 fprintf(fileDest
, "=$relaytrace$%.*s", pexp
->strName
.len
, pexp
->strName
.buf
);
584 OutputLine_def_GCC(FILE *fileDest
, EXPORT
*pexp
)
587 /* Print the function name, with decoration for export libs */
588 PrintName(fileDest
, pexp
, &pexp
->strName
, gbImportLib
);
589 DbgPrint("Generating def line for '%.*s'\n", pexp
->strName
.len
, pexp
->strName
.buf
);
591 /* Check if this is a forwarded export */
592 if (pexp
->strTarget
.buf
)
594 int fIsExternal
= !!ScanToken(pexp
->strTarget
.buf
, '.');
595 DbgPrint("Got redirect '%.*s'\n", pexp
->strTarget
.len
, pexp
->strTarget
.buf
);
597 /* print the target name, don't decorate if it is external */
598 fprintf(fileDest
, "=");
599 PrintName(fileDest
, pexp
, &pexp
->strTarget
, !fIsExternal
);
601 else if (((pexp
->uFlags
& FL_STUB
) || (pexp
->nCallingConvention
== CC_STUB
)) &&
602 (pexp
->strName
.buf
[0] == '?'))
604 /* C++ stubs are forwarded to C stubs */
605 fprintf(fileDest
, "=stub_function%d", pexp
->nNumber
);
607 else if (gbTracing
&& ((pexp
->uFlags
& FL_NORELAY
) == 0) && (pexp
->nCallingConvention
== CC_STDCALL
) &&
608 (pexp
->strName
.buf
[0] != '?'))
610 /* Redirect it to the relay-tracing trampoline */
613 fprintf(fileDest
, "=");
614 sprintf(buf
, "$relaytrace$%.*s", pexp
->strName
.len
, pexp
->strName
.buf
);
616 strTarget
.len
= pexp
->strName
.len
+ 12;
617 PrintName(fileDest
, pexp
, &strTarget
, 1);
621 /* Special handling for stdcall and fastcall */
622 if ((giArch
== ARCH_X86
) &&
623 ((pexp
->nCallingConvention
== CC_STDCALL
) ||
624 (pexp
->nCallingConvention
== CC_FASTCALL
)))
626 /* Is this the import lib? */
629 /* Is the name in the spec file decorated? */
630 const char* pcDeco
= ScanToken(pexp
->strName
.buf
, '@');
631 if (pcDeco
&& (pcDeco
< pexp
->strName
.buf
+ pexp
->strName
.len
))
633 /* Write the name including the leading @ */
634 fprintf(fileDest
, "==%.*s", pexp
->strName
.len
, pexp
->strName
.buf
);
637 else if ((!pexp
->strTarget
.buf
) && !(bTracing
))
639 /* Write a forwarder to the actual decorated symbol */
640 fprintf(fileDest
, "=");
641 PrintName(fileDest
, pexp
, &pexp
->strName
, 1);
647 OutputLine_def(FILE *fileDest
, EXPORT
*pexp
)
649 DbgPrint("OutputLine_def: '%.*s'...\n", pexp
->strName
.len
, pexp
->strName
.buf
);
650 fprintf(fileDest
, " ");
653 OutputLine_def_MS(fileDest
, pexp
);
655 OutputLine_def_GCC(fileDest
, pexp
);
657 if (pexp
->uFlags
& FL_ORDINAL
)
659 fprintf(fileDest
, " @%d", pexp
->nOrdinal
);
662 if (pexp
->uFlags
& FL_NONAME
)
664 fprintf(fileDest
, " NONAME");
667 if (pexp
->uFlags
& FL_PRIVATE
)
669 fprintf(fileDest
, " PRIVATE");
672 if (pexp
->nCallingConvention
== CC_EXTERN
)
674 fprintf(fileDest
, " DATA");
677 fprintf(fileDest
, "\n");
683 ParseFile(char* pcStart
, FILE *fileDest
, PFNOUTLINE OutputLine
)
691 //fprintf(stderr, "info: line %d, pcStart:'%.30s'\n", nLine, pcStart);
696 for (pcLine
= pcStart
; *pcLine
; pcLine
= NextLine(pcLine
), nLine
++)
705 //if (!strncmp(pcLine, "22 stdcall @(long) MPR_Alloc",28))
708 //fprintf(stderr, "info: line %d, token:'%d, %.20s'\n",
709 // nLine, TokenLength(pcLine), pcLine);
711 /* Skip white spaces */
712 while (*pc
== ' ' || *pc
== '\t') pc
++;
714 /* Skip empty lines, stop at EOF */
715 if (*pc
== ';' || *pc
<= '#') continue;
716 if (*pc
== 0) return 0;
718 //fprintf(stderr, "info: line %d, token:'%.*s'\n",
719 // nLine, TokenLength(pc), pc);
721 /* Now we should get either an ordinal or @ */
726 exp
.nOrdinal
= atol(pc
);
727 /* The import lib should contain the ordinal only if -ordinal was specified */
729 exp
.uFlags
|= FL_ORDINAL
;
732 /* Go to next token (type) */
733 if (!(pc
= NextToken(pc
)))
735 fprintf(stderr
, "error: line %d, unexpected end of line\n", nLine
);
739 //fprintf(stderr, "info: Token:'%.*s'\n", TokenLength(pc), pc);
741 /* Now we should get the type */
742 if (CompareToken(pc
, "stdcall"))
744 exp
.nCallingConvention
= CC_STDCALL
;
746 else if (CompareToken(pc
, "cdecl") ||
747 CompareToken(pc
, "varargs"))
749 exp
.nCallingConvention
= CC_CDECL
;
751 else if (CompareToken(pc
, "fastcall"))
753 exp
.nCallingConvention
= CC_FASTCALL
;
755 else if (CompareToken(pc
, "thiscall"))
757 exp
.nCallingConvention
= CC_THISCALL
;
759 else if (CompareToken(pc
, "extern"))
761 exp
.nCallingConvention
= CC_EXTERN
;
763 else if (CompareToken(pc
, "stub"))
765 exp
.nCallingConvention
= CC_STUB
;
769 fprintf(stderr
, "error: line %d, expected callconv, got '%.*s' %d\n",
770 nLine
, TokenLength(pc
), pc
, *pc
);
774 //fprintf(stderr, "info: nCallingConvention: %d\n", exp.nCallingConvention);
776 /* Go to next token (options or name) */
777 if (!(pc
= NextToken(pc
)))
779 fprintf(stderr
, "fail2\n");
787 if (CompareToken(pc
, "-arch"))
789 /* Default to not included */
793 /* Look if we are included */
794 while (*pc
== '=' || *pc
== ',')
797 if (CompareToken(pc
, pszArchString
) ||
798 CompareToken(pc
, pszArchString2
))
803 /* Skip to next arch or end */
804 while (*pc
> ',') pc
++;
807 else if (CompareToken(pc
, "-i386"))
809 if (giArch
!= ARCH_X86
) included
= 0;
811 else if (CompareToken(pc
, "-private"))
813 exp
.uFlags
|= FL_PRIVATE
;
815 else if (CompareToken(pc
, "-noname"))
817 exp
.uFlags
|= FL_ORDINAL
| FL_NONAME
;
819 else if (CompareToken(pc
, "-ordinal"))
821 exp
.uFlags
|= FL_ORDINAL
;
822 /* GCC doesn't automatically import by ordinal if an ordinal
823 * is found in the def file. Force it. */
824 if (gbImportLib
&& !gbMSComp
)
825 exp
.uFlags
|= FL_NONAME
;
827 else if (CompareToken(pc
, "-stub"))
829 exp
.uFlags
|= FL_STUB
;
831 else if (CompareToken(pc
, "-norelay"))
833 exp
.uFlags
|= FL_NORELAY
;
835 else if (CompareToken(pc
, "-ret64"))
837 exp
.uFlags
|= FL_RET64
;
839 else if (CompareToken(pc
, "-register"))
841 exp
.uFlags
|= FL_REGISTER
;
845 fprintf(stderr
, "info: ignored option: '%.*s'\n",
846 TokenLength(pc
), pc
);
849 /* Go to next token */
853 //fprintf(stderr, "info: Name:'%.10s'\n", pc);
855 /* If arch didn't match ours, skip this entry */
856 if (!included
) continue;
859 exp
.strName
.buf
= pc
;
860 exp
.strName
.len
= TokenLength(pc
);
861 DbgPrint("Got name: '%.*s'\n", exp
.strName
.len
, exp
.strName
.buf
);
863 /* Check for autoname */
864 if ((exp
.strName
.len
== 1) && (exp
.strName
.buf
[0] == '@'))
866 sprintf(namebuffer
, "ordinal%d", exp
.nOrdinal
);
867 exp
.strName
.len
= strlen(namebuffer
);
868 exp
.strName
.buf
= namebuffer
;
869 exp
.uFlags
|= FL_ORDINAL
| FL_NONAME
;
872 /* Handle parameters */
874 if (exp
.nCallingConvention
!= CC_EXTERN
&&
875 exp
.nCallingConvention
!= CC_STUB
)
877 /* Go to next token */
878 if (!(pc
= NextToken(pc
)))
880 fprintf(stderr
, "fail4\n");
887 fprintf(stderr
, "error: line %d, expected '('\n", nLine
);
891 /* Skip whitespaces */
892 while (*pc
== ' ' || *pc
== '\t') pc
++;
897 if (CompareToken(pc
, "long"))
899 exp
.nStackBytes
+= 4;
900 exp
.anArgs
[exp
.nArgCount
] = ARG_LONG
;
902 else if (CompareToken(pc
, "double"))
904 exp
.nStackBytes
+= 8;
905 exp
.anArgs
[exp
.nArgCount
] = ARG_DBL
;
907 else if (CompareToken(pc
, "ptr"))
909 exp
.nStackBytes
+= 4; // sizeof(void*) on x86
910 exp
.anArgs
[exp
.nArgCount
] = ARG_PTR
;
912 else if (CompareToken(pc
, "str"))
914 exp
.nStackBytes
+= 4; // sizeof(void*) on x86
915 exp
.anArgs
[exp
.nArgCount
] = ARG_STR
;
917 else if (CompareToken(pc
, "wstr"))
919 exp
.nStackBytes
+= 4; // sizeof(void*) on x86
920 exp
.anArgs
[exp
.nArgCount
] = ARG_WSTR
;
922 else if (CompareToken(pc
, "int64"))
924 exp
.nStackBytes
+= 8;
925 exp
.anArgs
[exp
.nArgCount
] = ARG_INT64
;
927 else if (CompareToken(pc
, "int128"))
929 exp
.nStackBytes
+= 16;
930 exp
.anArgs
[exp
.nArgCount
] = ARG_INT128
;
932 else if (CompareToken(pc
, "float"))
934 exp
.nStackBytes
+= 4;
935 exp
.anArgs
[exp
.nArgCount
] = ARG_FLOAT
;
938 fprintf(stderr
, "error: line %d, expected type, got: %.10s\n", nLine
, pc
);
942 /* Go to next parameter */
943 if (!(pc
= NextToken(pc
)))
945 fprintf(stderr
, "fail5\n");
953 fprintf(stderr
, "error: line %d, expected ')'\n", nLine
);
958 /* Handle special stub cases */
959 if (exp
.nCallingConvention
== CC_STUB
)
961 /* Check for c++ mangled name */
964 //printf("Found c++ mangled name...\n");
969 /* Check for stdcall name */
970 const char *p
= ScanToken(pc
, '@');
971 if (p
&& (p
- pc
< exp
.strName
.len
))
975 /* Truncate the name to before the @ */
976 exp
.strName
.len
= (int)(p
- pc
);
977 if (exp
.strName
.len
< 1)
979 fprintf(stderr
, "error, @ in line %d\n", nLine
);
982 exp
.nStackBytes
= atoi(p
+ 1);
983 exp
.nArgCount
= exp
.nStackBytes
/ 4;
984 exp
.nCallingConvention
= CC_STDCALL
;
985 exp
.uFlags
|= FL_STUB
;
986 for (i
= 0; i
< exp
.nArgCount
; i
++)
987 exp
.anArgs
[i
] = ARG_LONG
;
992 /* Get optional redirection */
996 exp
.strTarget
.buf
= pc
;
997 exp
.strTarget
.len
= TokenLength(pc
);
999 /* Check syntax (end of line) */
1002 fprintf(stderr
, "error: line %d, additional tokens after ')'\n", nLine
);
1006 /* Don't relay-trace forwarded functions */
1007 exp
.uFlags
|= FL_NORELAY
;
1011 exp
.strTarget
.buf
= 0;
1012 exp
.strTarget
.len
= 0;
1015 /* Check for no-name without ordinal */
1016 if ((exp
.uFlags
& FL_ORDINAL
) && (exp
.nOrdinal
== -1))
1018 fprintf(stderr
, "error: line %d, ordinal export without ordinal!\n", nLine
);
1022 OutputLine(fileDest
, &exp
);
1032 printf("syntax: spec2def [<options> ...] <spec file>\n"
1033 "Possible options:\n"
1034 " -h --help prints this screen\n"
1035 " -l=<file> generates an asm lib stub\n"
1036 " -d=<file> generates a def file\n"
1037 " -s=<file> generates a stub file\n"
1038 " --ms msvc compatibility\n"
1039 " -n=<name> name of the dll\n"
1040 " --implib generate a def file for an import library\n"
1041 " -a=<arch> Set architecture to <arch>. (i386, x86_64, arm)\n"
1042 " --with-tracing generates wine-like \"+relay\" trace trampolines. (necessitates -s)\n");
1045 int main(int argc
, char *argv
[])
1048 char *pszSource
, *pszDefFileName
= 0, *pszStubFileName
= 0, *pszLibStubName
= 0;
1049 char achDllName
[40];
1060 for (i
= 1; i
< argc
&& *argv
[i
] == '-'; i
++)
1062 if ((strcasecmp(argv
[i
], "--help") == 0) ||
1063 (strcasecmp(argv
[i
], "-h") == 0))
1068 else if (argv
[i
][1] == 'd' && argv
[i
][2] == '=')
1070 pszDefFileName
= argv
[i
] + 3;
1072 else if (argv
[i
][1] == 'l' && argv
[i
][2] == '=')
1074 pszLibStubName
= argv
[i
] + 3;
1076 else if (argv
[i
][1] == 's' && argv
[i
][2] == '=')
1078 pszStubFileName
= argv
[i
] + 3;
1080 else if (argv
[i
][1] == 'n' && argv
[i
][2] == '=')
1082 pszDllName
= argv
[i
] + 3;
1084 else if ((strcasecmp(argv
[i
], "--implib") == 0))
1088 else if ((strcasecmp(argv
[i
], "--ms") == 0))
1092 else if ((strcasecmp(argv
[i
], "--with-tracing") == 0))
1094 if (!pszStubFileName
)
1096 fprintf(stderr
, "Error: cannot use --with-tracing without -s option.\n");
1101 else if (argv
[i
][1] == 'a' && argv
[i
][2] == '=')
1103 pszArchString
= argv
[i
] + 3;
1107 fprintf(stderr
, "Unrecognized option: %s\n", argv
[i
]);
1112 if (strcasecmp(pszArchString
, "i386") == 0)
1115 gpszUnderscore
= "_";
1117 else if (strcasecmp(pszArchString
, "x86_64") == 0) giArch
= ARCH_AMD64
;
1118 else if (strcasecmp(pszArchString
, "ia64") == 0) giArch
= ARCH_IA64
;
1119 else if (strcasecmp(pszArchString
, "arm") == 0) giArch
= ARCH_ARM
;
1120 else if (strcasecmp(pszArchString
, "ppc") == 0) giArch
= ARCH_PPC
;
1122 if ((giArch
== ARCH_AMD64
) || (giArch
== ARCH_IA64
))
1124 pszArchString2
= "win64";
1127 pszArchString2
= "win32";
1129 /* Set a default dll name */
1135 p1
= strrchr(argv
[i
], '\\');
1136 if (!p1
) p1
= strrchr(argv
[i
], '/');
1137 p2
= p1
= p1
? p1
+ 1 : argv
[i
];
1139 /* walk up to '.' */
1140 while (*p2
!= '.' && *p2
!= 0) p2
++;
1142 if (len
>= sizeof(achDllName
) - 5)
1144 fprintf(stderr
, "name too long: %s\n", p1
);
1148 strncpy(achDllName
, p1
, len
);
1149 strncpy(achDllName
+ len
, ".dll", sizeof(achDllName
) - len
);
1150 pszDllName
= achDllName
;
1153 /* Open input file argv[1] */
1154 file
= fopen(argv
[i
], "r");
1157 fprintf(stderr
, "error: could not open file %s ", argv
[i
]);
1162 fseek(file
, 0, SEEK_END
);
1163 nFileSize
= ftell(file
);
1166 /* Allocate memory buffer */
1167 pszSource
= malloc(nFileSize
+ 1);
1174 /* Load input file into memory */
1175 nFileSize
= fread(pszSource
, 1, nFileSize
, file
);
1178 /* Zero terminate the source */
1179 pszSource
[nFileSize
] = '\0';
1183 /* Open output file */
1184 file
= fopen(pszDefFileName
, "w");
1187 fprintf(stderr
, "error: could not open output file %s ", argv
[i
+ 1]);
1191 OutputHeader_def(file
, pszDllName
);
1192 result
= ParseFile(pszSource
, file
, OutputLine_def
);
1196 if (pszStubFileName
)
1198 /* Open output file */
1199 file
= fopen(pszStubFileName
, "w");
1202 fprintf(stderr
, "error: could not open output file %s ", argv
[i
+ 1]);
1206 OutputHeader_stub(file
);
1207 result
= ParseFile(pszSource
, file
, OutputLine_stub
);
1213 /* Open output file */
1214 file
= fopen(pszLibStubName
, "w");
1217 fprintf(stderr
, "error: could not open output file %s ", argv
[i
+ 1]);
1221 OutputHeader_asmstub(file
, pszDllName
);
1222 result
= ParseFile(pszSource
, file
, OutputLine_asmstub
);
1223 fprintf(file
, "\n END\n");