[SPEC2DEF]
[reactos.git] / reactos / tools / spec2def / spec2def.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <ctype.h>
4 #include <string.h>
5
6 #ifdef _MSC_VER
7 #define strcasecmp _stricmp
8 #endif
9
10 typedef struct _STRING
11 {
12 const char *buf;
13 int len;
14 } STRING, *PSTRING;
15
16 typedef struct
17 {
18 STRING strName;
19 STRING strTarget;
20 int nCallingConvention;
21 int nOrdinal;
22 int nStackBytes;
23 int nArgCount;
24 int anArgs[30];
25 unsigned int uFlags;
26 int nNumber;
27 } EXPORT;
28
29 enum _ARCH
30 {
31 ARCH_X86,
32 ARCH_AMD64,
33 ARCH_IA64,
34 ARCH_ARM,
35 ARCH_PPC
36 };
37
38 typedef int (*PFNOUTLINE)(FILE *, EXPORT *);
39 int gbMSComp = 0;
40 int gbImportLib = 0;
41 int gbTracing = 0;
42 int giArch = ARCH_X86;
43 char *pszArchString = "i386";
44 char *pszArchString2;
45 char *pszDllName = 0;
46 char *gpszUnderscore = "";
47 int gbDebug;
48 #define DbgPrint(...) (!gbDebug || fprintf(stderr, __VA_ARGS__))
49
50 enum
51 {
52 FL_PRIVATE = 1,
53 FL_STUB = 2,
54 FL_NONAME = 4,
55 FL_ORDINAL = 8,
56 FL_NORELAY = 16,
57 FL_RET64 = 32,
58 FL_REGISTER = 64,
59 };
60
61 enum
62 {
63 CC_STDCALL,
64 CC_CDECL,
65 CC_FASTCALL,
66 CC_THISCALL,
67 CC_EXTERN,
68 CC_STUB,
69 };
70
71 enum
72 {
73 ARG_LONG,
74 ARG_PTR,
75 ARG_STR,
76 ARG_WSTR,
77 ARG_DBL,
78 ARG_INT64,
79 ARG_INT128,
80 ARG_FLOAT
81 };
82
83 char* astrCallingConventions[] =
84 {
85 "STDCALL",
86 "CDECL",
87 "FASTCALL",
88 "THISCALL",
89 "EXTERN"
90 };
91
92 static
93 int
94 IsSeparator(char chr)
95 {
96 return ((chr <= ',' && chr != '$') ||
97 (chr >= ':' && chr < '?') );
98 }
99
100 int
101 CompareToken(const char *token, const char *comparand)
102 {
103 while (*comparand)
104 {
105 if (*token != *comparand) return 0;
106 token++;
107 comparand++;
108 }
109 if (!IsSeparator(*token)) return 0;
110 return 1;
111 }
112
113 const char *
114 ScanToken(const char *token, char chr)
115 {
116 while (!IsSeparator(*token))
117 {
118 if (*token == chr) return token;
119 token++;
120 }
121 return 0;
122 }
123
124 char *
125 NextLine(char *pc)
126 {
127 while (*pc != 0)
128 {
129 if (pc[0] == '\n' && pc[1] == '\r') return pc + 2;
130 else if (pc[0] == '\n') return pc + 1;
131 pc++;
132 }
133 return pc;
134 }
135
136 int
137 TokenLength(char *pc)
138 {
139 int length = 0;
140
141 while (!IsSeparator(*pc++)) length++;
142
143 return length;
144 }
145
146 char *
147 NextToken(char *pc)
148 {
149 /* Skip token */
150 while (!IsSeparator(*pc)) pc++;
151
152 /* Skip white spaces */
153 while (*pc == ' ' || *pc == '\t') pc++;
154
155 /* Check for end of line */
156 if (*pc == '\n' || *pc == '\r' || *pc == 0) return 0;
157
158 /* Check for comment */
159 if (*pc == '#' || *pc == ';') return 0;
160
161 return pc;
162 }
163
164 void
165 OutputHeader_stub(FILE *file)
166 {
167 fprintf(file, "/* This file is autogenerated, do not edit. */\n\n"
168 "#include <stubs.h>\n");
169
170 if (gbTracing)
171 {
172 fprintf(file, "#include <wine/debug.h>\n");
173 fprintf(file, "#include <inttypes.h>\n");
174 fprintf(file, "WINE_DECLARE_DEBUG_CHANNEL(relay);\n");
175 }
176
177 fprintf(file, "\n");
178 }
179
180 int
181 OutputLine_stub(FILE *file, EXPORT *pexp)
182 {
183 int i;
184 int bRelay = 0;
185 int bInPrototype = 0;
186
187 if (pexp->nCallingConvention != CC_STUB &&
188 (pexp->uFlags & FL_STUB) == 0)
189 {
190 /* Only relay trace stdcall C functions */
191 if (!gbTracing || (pexp->nCallingConvention != CC_STDCALL)
192 || (pexp->uFlags & FL_NORELAY)
193 || (pexp->strName.buf[0] == '?'))
194 {
195 return 0;
196 }
197 bRelay = 1;
198 }
199
200 /* Declare the "real" function */
201 if (bRelay)
202 {
203 fprintf(file, "extern ");
204 bInPrototype = 1;
205 }
206
207 do
208 {
209 if (pexp->uFlags & FL_REGISTER)
210 {
211 /* FIXME: Not sure this is right */
212 fprintf(file, "void ");
213 }
214 else if (pexp->uFlags & FL_RET64)
215 {
216 fprintf(file, "__int64 ");
217 }
218 else
219 {
220 fprintf(file, "int ");
221 }
222
223 if ((giArch == ARCH_X86) &&
224 pexp->nCallingConvention == CC_STDCALL)
225 {
226 fprintf(file, "__stdcall ");
227 }
228
229 /* Check for C++ */
230 if (pexp->strName.buf[0] == '?')
231 {
232 fprintf(file, "stub_function%d(", pexp->nNumber);
233 }
234 else
235 {
236 if (!bRelay || bInPrototype)
237 fprintf(file, "%.*s(", pexp->strName.len, pexp->strName.buf);
238 else
239 fprintf(file, "$relaytrace$%.*s(", pexp->strName.len, pexp->strName.buf);
240 }
241
242 for (i = 0; i < pexp->nArgCount; i++)
243 {
244 if (i != 0) fprintf(file, ", ");
245 switch (pexp->anArgs[i])
246 {
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;
255 }
256 fprintf(file, " a%d", i);
257 }
258
259 if (bInPrototype)
260 {
261 fprintf(file, ");\n\n");
262 }
263 } while (bInPrototype--);
264
265 if (!bRelay)
266 {
267 fprintf(file, ")\n{\n\tDbgPrint(\"WARNING: calling stub %.*s(",
268 pexp->strName.len, pexp->strName.buf);
269 }
270 else
271 {
272 fprintf(file, ")\n{\n");
273 if (pexp->uFlags & FL_REGISTER)
274 {
275 /* No return value */
276 }
277 else if (pexp->uFlags & FL_RET64)
278 {
279 fprintf(file, "\t__int64 retval;\n");
280 }
281 else
282 {
283 fprintf(file, "\tint retval;\n");
284 }
285 fprintf(file, "\tif (TRACE_ON(relay))\n\t\tDPRINTF(\"%s: %.*s(",
286 pszDllName, pexp->strName.len, pexp->strName.buf);
287 }
288
289 for (i = 0; i < pexp->nArgCount; i++)
290 {
291 if (i != 0) fprintf(file, ",");
292 switch (pexp->anArgs[i])
293 {
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;
302 }
303 }
304 fprintf(file, ")\\n\"");
305
306 for (i = 0; i < pexp->nArgCount; i++)
307 {
308 fprintf(file, ", ");
309 switch (pexp->anArgs[i])
310 {
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;
319 }
320 }
321 fprintf(file, ");\n");
322
323 if (pexp->nCallingConvention == CC_STUB)
324 {
325 fprintf(file, "\t__wine_spec_unimplemented_stub(\"%s\", __FUNCTION__);\n", pszDllName);
326 }
327 else if (bRelay)
328 {
329 if (pexp->uFlags & FL_REGISTER)
330 {
331 fprintf(file,"\t");
332 }
333 else
334 {
335 fprintf(file, "\tretval = ");
336 }
337 fprintf(file, "%.*s(", pexp->strName.len, pexp->strName.buf);
338
339 for (i = 0; i < pexp->nArgCount; i++)
340 {
341 if (i != 0) fprintf(file, ", ");
342 fprintf(file, "a%d", i);
343 }
344 fprintf(file, ");\n");
345 }
346
347 if (!bRelay)
348 fprintf(file, "\treturn 0;\n}\n\n");
349 else if ((pexp->uFlags & FL_REGISTER) == 0)
350 {
351 if (pexp->uFlags & FL_RET64)
352 {
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);
355 }
356 else
357 {
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);
360 }
361 fprintf(file, "\treturn retval;\n}\n\n");
362 }
363
364 return 1;
365 }
366
367 void
368 OutputHeader_asmstub(FILE *file, char *libname)
369 {
370 fprintf(file, "; File generated automatically, do not edit! \n\n");
371
372 if (giArch == ARCH_X86)
373 {
374 fprintf(file, ".586\n.model flat\n.code\n");
375 }
376 else if (giArch == ARCH_AMD64)
377 {
378 fprintf(file, ".code\n");
379 }
380 else if (giArch == ARCH_ARM)
381 {
382 fprintf(file,
383 " AREA |.text|,ALIGN=2,CODE,READONLY\n\n");
384 }
385 }
386
387 void
388 Output_stublabel(FILE *fileDest, char* pszSymbolName)
389 {
390 if (giArch == ARCH_ARM)
391 {
392 fprintf(fileDest,
393 " EXPORT %s [FUNC]\n%s\n",
394 pszSymbolName,
395 pszSymbolName);
396 }
397 else
398 {
399 fprintf(fileDest,
400 "PUBLIC %s\n%s: nop\n",
401 pszSymbolName,
402 pszSymbolName);
403 }
404 }
405
406 int
407 OutputLine_asmstub(FILE *fileDest, EXPORT *pexp)
408 {
409 char szNameBuffer[128];
410
411 /* Handle autoname */
412 if (pexp->strName.len == 1 && pexp->strName.buf[0] == '@')
413 {
414 sprintf(szNameBuffer, "%sordinal%d\n%sordinal%d: nop\n",
415 gpszUnderscore, pexp->nOrdinal, gpszUnderscore, pexp->nOrdinal);
416 }
417 else if (giArch != ARCH_X86)
418 {
419 sprintf(szNameBuffer, "_stub_%.*s",
420 pexp->strName.len, pexp->strName.buf);
421 }
422 else if (pexp->nCallingConvention == CC_STDCALL)
423 {
424 sprintf(szNameBuffer, "__stub_%.*s@%d",
425 pexp->strName.len, pexp->strName.buf, pexp->nStackBytes);
426 }
427 else if (pexp->nCallingConvention == CC_FASTCALL)
428 {
429 sprintf(szNameBuffer, "@_stub_%.*s@%d",
430 pexp->strName.len, pexp->strName.buf, pexp->nStackBytes);
431 }
432 else if ((pexp->nCallingConvention == CC_CDECL) ||
433 (pexp->nCallingConvention == CC_THISCALL) ||
434 (pexp->nCallingConvention == CC_EXTERN) ||
435 (pexp->nCallingConvention == CC_STUB))
436 {
437 sprintf(szNameBuffer, "__stub_%.*s",
438 pexp->strName.len, pexp->strName.buf);
439 }
440 else
441 {
442 fprintf(stderr, "Invalid calling convention");
443 return 0;
444 }
445
446 Output_stublabel(fileDest, szNameBuffer);
447
448 return 1;
449 }
450
451 void
452 OutputHeader_def(FILE *file, char *libname)
453 {
454 fprintf(file,
455 "; File generated automatically, do not edit!\n\n"
456 "NAME %s\n\n"
457 "EXPORTS\n",
458 libname);
459 }
460
461 void
462 PrintName(FILE *fileDest, EXPORT *pexp, PSTRING pstr, int fDeco)
463 {
464 const char *pcName = pstr->buf;
465 int nNameLength = pstr->len;
466 const char* pcDot, *pcAt;
467
468 /* Check for non-x86 first */
469 if (giArch != ARCH_X86)
470 {
471 /* Does the string already have stdcall decoration? */
472 pcAt = ScanToken(pcName, '@');
473 if (pcAt && (pcAt < (pcName + nNameLength)) && (pcName[0] == '_'))
474 {
475 /* Skip leading underscore and remove trailing decoration */
476 pcName++;
477 nNameLength = pcAt - pcName;
478 }
479
480 /* Print the undecorated function name */
481 fprintf(fileDest, "%.*s", nNameLength, pcName);
482 }
483 else if (fDeco &&
484 ((pexp->nCallingConvention == CC_STDCALL) ||
485 (pexp->nCallingConvention == CC_FASTCALL)))
486 {
487 /* Scan for a dll forwarding dot */
488 pcDot = ScanToken(pcName, '.');
489 if (pcDot)
490 {
491 /* First print the dll name, followed by a dot */
492 nNameLength = pcDot - pcName;
493 fprintf(fileDest, "%.*s.", nNameLength, pcName);
494
495 /* Now the actual function name */
496 pcName = pcDot + 1;
497 nNameLength = pexp->strTarget.len - nNameLength - 1;
498 }
499
500 /* Does the string already have decoration? */
501 pcAt = ScanToken(pcName, '@');
502 if (pcAt && (pcAt < (pcName + nNameLength)))
503 {
504 /* On GCC, we need to remove the leading stdcall underscore */
505 if (!gbMSComp && (pexp->nCallingConvention == CC_STDCALL))
506 {
507 pcName++;
508 nNameLength--;
509 }
510
511 /* Print the already decorated function name */
512 fprintf(fileDest, "%.*s", nNameLength, pcName);
513 }
514 else
515 {
516 /* Print the prefix, but skip it for (GCC && stdcall) */
517 if (gbMSComp || (pexp->nCallingConvention != CC_STDCALL))
518 {
519 fprintf(fileDest, "%c", pexp->nCallingConvention == CC_FASTCALL ? '@' : '_');
520 }
521
522 /* Print the name with trailing decoration */
523 fprintf(fileDest, "%.*s@%d", nNameLength, pcName, pexp->nStackBytes);
524 }
525 }
526 else
527 {
528 /* Print the undecorated function name */
529 fprintf(fileDest, "%.*s", nNameLength, pcName);
530 }
531 }
532
533 void
534 OutputLine_def_MS(FILE *fileDest, EXPORT *pexp)
535 {
536 PrintName(fileDest, pexp, &pexp->strName, 0);
537
538 if (gbImportLib)
539 {
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);
542 }
543 else if (pexp->strTarget.buf)
544 {
545 if (pexp->strName.buf[0] == '?')
546 {
547 fprintf(stderr, "warning: ignoring C++ redirection %.*s -> %.*s\n",
548 pexp->strName.len, pexp->strName.buf, pexp->strTarget.len, pexp->strTarget.buf);
549 }
550 else
551 {
552 fprintf(fileDest, "=");
553
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)) )
559 {
560 PrintName(fileDest, pexp, &pexp->strTarget, 1);
561 }
562 else
563 {
564 /* Write the undecorated redirection name */
565 fprintf(fileDest, "%.*s", pexp->strTarget.len, pexp->strTarget.buf);
566 }
567 }
568 }
569 else if (((pexp->uFlags & FL_STUB) || (pexp->nCallingConvention == CC_STUB)) &&
570 (pexp->strName.buf[0] == '?'))
571 {
572 /* C++ stubs are forwarded to C stubs */
573 fprintf(fileDest, "=stub_function%d", pexp->nNumber);
574 }
575 else if (gbTracing && ((pexp->uFlags & FL_NORELAY) == 0) && (pexp->nCallingConvention == CC_STDCALL) &&
576 (pexp->strName.buf[0] != '?'))
577 {
578 /* Redirect it to the relay-tracing trampoline */
579 fprintf(fileDest, "=$relaytrace$%.*s", pexp->strName.len, pexp->strName.buf);
580 }
581 }
582
583 void
584 OutputLine_def_GCC(FILE *fileDest, EXPORT *pexp)
585 {
586 int bTracing = 0;
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);
590
591 /* Check if this is a forwarded export */
592 if (pexp->strTarget.buf)
593 {
594 int fIsExternal = !!ScanToken(pexp->strTarget.buf, '.');
595 DbgPrint("Got redirect '%.*s'\n", pexp->strTarget.len, pexp->strTarget.buf);
596
597 /* print the target name, don't decorate if it is external */
598 fprintf(fileDest, "=");
599 PrintName(fileDest, pexp, &pexp->strTarget, !fIsExternal);
600 }
601 else if (((pexp->uFlags & FL_STUB) || (pexp->nCallingConvention == CC_STUB)) &&
602 (pexp->strName.buf[0] == '?'))
603 {
604 /* C++ stubs are forwarded to C stubs */
605 fprintf(fileDest, "=stub_function%d", pexp->nNumber);
606 }
607 else if (gbTracing && ((pexp->uFlags & FL_NORELAY) == 0) && (pexp->nCallingConvention == CC_STDCALL) &&
608 (pexp->strName.buf[0] != '?'))
609 {
610 /* Redirect it to the relay-tracing trampoline */
611 char buf[256];
612 STRING strTarget;
613 fprintf(fileDest, "=");
614 sprintf(buf, "$relaytrace$%.*s", pexp->strName.len, pexp->strName.buf);
615 strTarget.buf = buf;
616 strTarget.len = pexp->strName.len + 12;
617 PrintName(fileDest, pexp, &strTarget, 1);
618 bTracing = 1;
619 }
620
621 /* Special handling for stdcall and fastcall */
622 if ((giArch == ARCH_X86) &&
623 ((pexp->nCallingConvention == CC_STDCALL) ||
624 (pexp->nCallingConvention == CC_FASTCALL)))
625 {
626 /* Is this the import lib? */
627 if (gbImportLib)
628 {
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))
632 {
633 /* Write the name including the leading @ */
634 fprintf(fileDest, "==%.*s", pexp->strName.len, pexp->strName.buf);
635 }
636 }
637 else if ((!pexp->strTarget.buf) && !(bTracing))
638 {
639 /* Write a forwarder to the actual decorated symbol */
640 fprintf(fileDest, "=");
641 PrintName(fileDest, pexp, &pexp->strName, 1);
642 }
643 }
644 }
645
646 int
647 OutputLine_def(FILE *fileDest, EXPORT *pexp)
648 {
649 DbgPrint("OutputLine_def: '%.*s'...\n", pexp->strName.len, pexp->strName.buf);
650 fprintf(fileDest, " ");
651
652 if (gbMSComp)
653 OutputLine_def_MS(fileDest, pexp);
654 else
655 OutputLine_def_GCC(fileDest, pexp);
656
657 if (pexp->uFlags & FL_ORDINAL)
658 {
659 fprintf(fileDest, " @%d", pexp->nOrdinal);
660 }
661
662 if (pexp->uFlags & FL_NONAME)
663 {
664 fprintf(fileDest, " NONAME");
665 }
666
667 if (pexp->uFlags & FL_PRIVATE)
668 {
669 fprintf(fileDest, " PRIVATE");
670 }
671
672 if (pexp->nCallingConvention == CC_EXTERN)
673 {
674 fprintf(fileDest, " DATA");
675 }
676
677 fprintf(fileDest, "\n");
678
679 return 1;
680 }
681
682 int
683 ParseFile(char* pcStart, FILE *fileDest, PFNOUTLINE OutputLine)
684 {
685 char *pc, *pcLine;
686 int nLine;
687 EXPORT exp;
688 int included;
689 char namebuffer[16];
690
691 //fprintf(stderr, "info: line %d, pcStart:'%.30s'\n", nLine, pcStart);
692
693 /* Loop all lines */
694 nLine = 1;
695 exp.nNumber = 0;
696 for (pcLine = pcStart; *pcLine; pcLine = NextLine(pcLine), nLine++)
697 {
698 pc = pcLine;
699
700 exp.nArgCount = 0;
701 exp.uFlags = 0;
702 exp.nNumber++;
703
704
705 //if (!strncmp(pcLine, "22 stdcall @(long) MPR_Alloc",28))
706 // gbDebug = 1;
707
708 //fprintf(stderr, "info: line %d, token:'%d, %.20s'\n",
709 // nLine, TokenLength(pcLine), pcLine);
710
711 /* Skip white spaces */
712 while (*pc == ' ' || *pc == '\t') pc++;
713
714 /* Skip empty lines, stop at EOF */
715 if (*pc == ';' || *pc <= '#') continue;
716 if (*pc == 0) return 0;
717
718 //fprintf(stderr, "info: line %d, token:'%.*s'\n",
719 // nLine, TokenLength(pc), pc);
720
721 /* Now we should get either an ordinal or @ */
722 if (*pc == '@')
723 exp.nOrdinal = -1;
724 else
725 {
726 exp.nOrdinal = atol(pc);
727 /* The import lib should contain the ordinal only if -ordinal was specified */
728 if (!gbImportLib)
729 exp.uFlags |= FL_ORDINAL;
730 }
731
732 /* Go to next token (type) */
733 if (!(pc = NextToken(pc)))
734 {
735 fprintf(stderr, "error: line %d, unexpected end of line\n", nLine);
736 return -10;
737 }
738
739 //fprintf(stderr, "info: Token:'%.*s'\n", TokenLength(pc), pc);
740
741 /* Now we should get the type */
742 if (CompareToken(pc, "stdcall"))
743 {
744 exp.nCallingConvention = CC_STDCALL;
745 }
746 else if (CompareToken(pc, "cdecl") ||
747 CompareToken(pc, "varargs"))
748 {
749 exp.nCallingConvention = CC_CDECL;
750 }
751 else if (CompareToken(pc, "fastcall"))
752 {
753 exp.nCallingConvention = CC_FASTCALL;
754 }
755 else if (CompareToken(pc, "thiscall"))
756 {
757 exp.nCallingConvention = CC_THISCALL;
758 }
759 else if (CompareToken(pc, "extern"))
760 {
761 exp.nCallingConvention = CC_EXTERN;
762 }
763 else if (CompareToken(pc, "stub"))
764 {
765 exp.nCallingConvention = CC_STUB;
766 }
767 else
768 {
769 fprintf(stderr, "error: line %d, expected callconv, got '%.*s' %d\n",
770 nLine, TokenLength(pc), pc, *pc);
771 return -11;
772 }
773
774 //fprintf(stderr, "info: nCallingConvention: %d\n", exp.nCallingConvention);
775
776 /* Go to next token (options or name) */
777 if (!(pc = NextToken(pc)))
778 {
779 fprintf(stderr, "fail2\n");
780 return -12;
781 }
782
783 /* Handle options */
784 included = 1;
785 while (*pc == '-')
786 {
787 if (CompareToken(pc, "-arch"))
788 {
789 /* Default to not included */
790 included = 0;
791 pc += 5;
792
793 /* Look if we are included */
794 while (*pc == '=' || *pc == ',')
795 {
796 pc++;
797 if (CompareToken(pc, pszArchString) ||
798 CompareToken(pc, pszArchString2))
799 {
800 included = 1;
801 }
802
803 /* Skip to next arch or end */
804 while (*pc > ',') pc++;
805 }
806 }
807 else if (CompareToken(pc, "-i386"))
808 {
809 if (giArch != ARCH_X86) included = 0;
810 }
811 else if (CompareToken(pc, "-private"))
812 {
813 exp.uFlags |= FL_PRIVATE;
814 }
815 else if (CompareToken(pc, "-noname"))
816 {
817 exp.uFlags |= FL_ORDINAL | FL_NONAME;
818 }
819 else if (CompareToken(pc, "-ordinal"))
820 {
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;
826 }
827 else if (CompareToken(pc, "-stub"))
828 {
829 exp.uFlags |= FL_STUB;
830 }
831 else if (CompareToken(pc, "-norelay"))
832 {
833 exp.uFlags |= FL_NORELAY;
834 }
835 else if (CompareToken(pc, "-ret64"))
836 {
837 exp.uFlags |= FL_RET64;
838 }
839 else if (CompareToken(pc, "-register"))
840 {
841 exp.uFlags |= FL_REGISTER;
842 }
843 else
844 {
845 fprintf(stderr, "info: ignored option: '%.*s'\n",
846 TokenLength(pc), pc);
847 }
848
849 /* Go to next token */
850 pc = NextToken(pc);
851 }
852
853 //fprintf(stderr, "info: Name:'%.10s'\n", pc);
854
855 /* If arch didn't match ours, skip this entry */
856 if (!included) continue;
857
858 /* Get name */
859 exp.strName.buf = pc;
860 exp.strName.len = TokenLength(pc);
861 DbgPrint("Got name: '%.*s'\n", exp.strName.len, exp.strName.buf);
862
863 /* Check for autoname */
864 if ((exp.strName.len == 1) && (exp.strName.buf[0] == '@'))
865 {
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;
870 }
871
872 /* Handle parameters */
873 exp.nStackBytes = 0;
874 if (exp.nCallingConvention != CC_EXTERN &&
875 exp.nCallingConvention != CC_STUB)
876 {
877 /* Go to next token */
878 if (!(pc = NextToken(pc)))
879 {
880 fprintf(stderr, "fail4\n");
881 return -13;
882 }
883
884 /* Verify syntax */
885 if (*pc++ != '(')
886 {
887 fprintf(stderr, "error: line %d, expected '('\n", nLine);
888 return -14;
889 }
890
891 /* Skip whitespaces */
892 while (*pc == ' ' || *pc == '\t') pc++;
893
894 exp.nStackBytes = 0;
895 while (*pc >= '0')
896 {
897 if (CompareToken(pc, "long"))
898 {
899 exp.nStackBytes += 4;
900 exp.anArgs[exp.nArgCount] = ARG_LONG;
901 }
902 else if (CompareToken(pc, "double"))
903 {
904 exp.nStackBytes += 8;
905 exp.anArgs[exp.nArgCount] = ARG_DBL;
906 }
907 else if (CompareToken(pc, "ptr"))
908 {
909 exp.nStackBytes += 4; // sizeof(void*) on x86
910 exp.anArgs[exp.nArgCount] = ARG_PTR;
911 }
912 else if (CompareToken(pc, "str"))
913 {
914 exp.nStackBytes += 4; // sizeof(void*) on x86
915 exp.anArgs[exp.nArgCount] = ARG_STR;
916 }
917 else if (CompareToken(pc, "wstr"))
918 {
919 exp.nStackBytes += 4; // sizeof(void*) on x86
920 exp.anArgs[exp.nArgCount] = ARG_WSTR;
921 }
922 else if (CompareToken(pc, "int64"))
923 {
924 exp.nStackBytes += 8;
925 exp.anArgs[exp.nArgCount] = ARG_INT64;
926 }
927 else if (CompareToken(pc, "int128"))
928 {
929 exp.nStackBytes += 16;
930 exp.anArgs[exp.nArgCount] = ARG_INT128;
931 }
932 else if (CompareToken(pc, "float"))
933 {
934 exp.nStackBytes += 4;
935 exp.anArgs[exp.nArgCount] = ARG_FLOAT;
936 }
937 else
938 fprintf(stderr, "error: line %d, expected type, got: %.10s\n", nLine, pc);
939
940 exp.nArgCount++;
941
942 /* Go to next parameter */
943 if (!(pc = NextToken(pc)))
944 {
945 fprintf(stderr, "fail5\n");
946 return -15;
947 }
948 }
949
950 /* Check syntax */
951 if (*pc++ != ')')
952 {
953 fprintf(stderr, "error: line %d, expected ')'\n", nLine);
954 return -16;
955 }
956 }
957
958 /* Handle special stub cases */
959 if (exp.nCallingConvention == CC_STUB)
960 {
961 /* Check for c++ mangled name */
962 if (pc[0] == '?')
963 {
964 //printf("Found c++ mangled name...\n");
965 //
966 }
967 else
968 {
969 /* Check for stdcall name */
970 const char *p = ScanToken(pc, '@');
971 if (p && (p - pc < exp.strName.len))
972 {
973 int i;
974
975 /* Truncate the name to before the @ */
976 exp.strName.len = (int)(p - pc);
977 if (exp.strName.len < 1)
978 {
979 fprintf(stderr, "error, @ in line %d\n", nLine);
980 return -1;
981 }
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;
988 }
989 }
990 }
991
992 /* Get optional redirection */
993 pc = NextToken(pc);
994 if (pc)
995 {
996 exp.strTarget.buf = pc;
997 exp.strTarget.len = TokenLength(pc);
998
999 /* Check syntax (end of line) */
1000 if (NextToken(pc))
1001 {
1002 fprintf(stderr, "error: line %d, additional tokens after ')'\n", nLine);
1003 return -17;
1004 }
1005
1006 /* Don't relay-trace forwarded functions */
1007 exp.uFlags |= FL_NORELAY;
1008 }
1009 else
1010 {
1011 exp.strTarget.buf = 0;
1012 exp.strTarget.len = 0;
1013 }
1014
1015 /* Check for no-name without ordinal */
1016 if ((exp.uFlags & FL_ORDINAL) && (exp.nOrdinal == -1))
1017 {
1018 fprintf(stderr, "error: line %d, ordinal export without ordinal!\n", nLine);
1019 return -1;
1020 }
1021
1022 OutputLine(fileDest, &exp);
1023 gbDebug = 0;
1024 }
1025
1026 return 0;
1027 }
1028
1029
1030 void usage(void)
1031 {
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");
1043 }
1044
1045 int main(int argc, char *argv[])
1046 {
1047 size_t nFileSize;
1048 char *pszSource, *pszDefFileName = 0, *pszStubFileName = 0, *pszLibStubName = 0;
1049 char achDllName[40];
1050 FILE *file;
1051 int result = 0, i;
1052
1053 if (argc < 2)
1054 {
1055 usage();
1056 return -1;
1057 }
1058
1059 /* Read options */
1060 for (i = 1; i < argc && *argv[i] == '-'; i++)
1061 {
1062 if ((strcasecmp(argv[i], "--help") == 0) ||
1063 (strcasecmp(argv[i], "-h") == 0))
1064 {
1065 usage();
1066 return 0;
1067 }
1068 else if (argv[i][1] == 'd' && argv[i][2] == '=')
1069 {
1070 pszDefFileName = argv[i] + 3;
1071 }
1072 else if (argv[i][1] == 'l' && argv[i][2] == '=')
1073 {
1074 pszLibStubName = argv[i] + 3;
1075 }
1076 else if (argv[i][1] == 's' && argv[i][2] == '=')
1077 {
1078 pszStubFileName = argv[i] + 3;
1079 }
1080 else if (argv[i][1] == 'n' && argv[i][2] == '=')
1081 {
1082 pszDllName = argv[i] + 3;
1083 }
1084 else if ((strcasecmp(argv[i], "--implib") == 0))
1085 {
1086 gbImportLib = 1;
1087 }
1088 else if ((strcasecmp(argv[i], "--ms") == 0))
1089 {
1090 gbMSComp = 1;
1091 }
1092 else if ((strcasecmp(argv[i], "--with-tracing") == 0))
1093 {
1094 if (!pszStubFileName)
1095 {
1096 fprintf(stderr, "Error: cannot use --with-tracing without -s option.\n");
1097 return -1;
1098 }
1099 gbTracing = 1;
1100 }
1101 else if (argv[i][1] == 'a' && argv[i][2] == '=')
1102 {
1103 pszArchString = argv[i] + 3;
1104 }
1105 else
1106 {
1107 fprintf(stderr, "Unrecognized option: %s\n", argv[i]);
1108 return -1;
1109 }
1110 }
1111
1112 if (strcasecmp(pszArchString, "i386") == 0)
1113 {
1114 giArch = ARCH_X86;
1115 gpszUnderscore = "_";
1116 }
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;
1121
1122 if ((giArch == ARCH_AMD64) || (giArch == ARCH_IA64))
1123 {
1124 pszArchString2 = "win64";
1125 }
1126 else
1127 pszArchString2 = "win32";
1128
1129 /* Set a default dll name */
1130 if (!pszDllName)
1131 {
1132 char *p1, *p2;
1133 size_t len;
1134
1135 p1 = strrchr(argv[i], '\\');
1136 if (!p1) p1 = strrchr(argv[i], '/');
1137 p2 = p1 = p1 ? p1 + 1 : argv[i];
1138
1139 /* walk up to '.' */
1140 while (*p2 != '.' && *p2 != 0) p2++;
1141 len = p2 - p1;
1142 if (len >= sizeof(achDllName) - 5)
1143 {
1144 fprintf(stderr, "name too long: %s\n", p1);
1145 return -2;
1146 }
1147
1148 strncpy(achDllName, p1, len);
1149 strncpy(achDllName + len, ".dll", sizeof(achDllName) - len);
1150 pszDllName = achDllName;
1151 }
1152
1153 /* Open input file argv[1] */
1154 file = fopen(argv[i], "r");
1155 if (!file)
1156 {
1157 fprintf(stderr, "error: could not open file %s ", argv[i]);
1158 return -3;
1159 }
1160
1161 /* Get file size */
1162 fseek(file, 0, SEEK_END);
1163 nFileSize = ftell(file);
1164 rewind(file);
1165
1166 /* Allocate memory buffer */
1167 pszSource = malloc(nFileSize + 1);
1168 if (!pszSource)
1169 {
1170 fclose(file);
1171 return -4;
1172 }
1173
1174 /* Load input file into memory */
1175 nFileSize = fread(pszSource, 1, nFileSize, file);
1176 fclose(file);
1177
1178 /* Zero terminate the source */
1179 pszSource[nFileSize] = '\0';
1180
1181 if (pszDefFileName)
1182 {
1183 /* Open output file */
1184 file = fopen(pszDefFileName, "w");
1185 if (!file)
1186 {
1187 fprintf(stderr, "error: could not open output file %s ", argv[i + 1]);
1188 return -5;
1189 }
1190
1191 OutputHeader_def(file, pszDllName);
1192 result = ParseFile(pszSource, file, OutputLine_def);
1193 fclose(file);
1194 }
1195
1196 if (pszStubFileName)
1197 {
1198 /* Open output file */
1199 file = fopen(pszStubFileName, "w");
1200 if (!file)
1201 {
1202 fprintf(stderr, "error: could not open output file %s ", argv[i + 1]);
1203 return -5;
1204 }
1205
1206 OutputHeader_stub(file);
1207 result = ParseFile(pszSource, file, OutputLine_stub);
1208 fclose(file);
1209 }
1210
1211 if (pszLibStubName)
1212 {
1213 /* Open output file */
1214 file = fopen(pszLibStubName, "w");
1215 if (!file)
1216 {
1217 fprintf(stderr, "error: could not open output file %s ", argv[i + 1]);
1218 return -5;
1219 }
1220
1221 OutputHeader_asmstub(file, pszDllName);
1222 result = ParseFile(pszSource, file, OutputLine_asmstub);
1223 fprintf(file, "\n END\n");
1224 fclose(file);
1225 }
1226
1227
1228 return result;
1229 }