1090a6ba9a4d19c21a9653c19f92488e433e9f15
[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;
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_symbol(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_STUB)
434 {
435 sprintf(szNameBuffer, "__stub_%.*s",
436 pexp->strName.len, pexp->strName.buf);
437 }
438 else if (pexp->nCallingConvention == CC_EXTERN)
439 {
440 sprintf(szNameBuffer, "__stub_%.*s",
441 pexp->strName.len, pexp->strName.buf);
442 }
443
444 Output_symbol(fileDest, szNameBuffer);
445
446 return 1;
447 }
448
449 void
450 OutputHeader_def(FILE *file, char *libname)
451 {
452 fprintf(file,
453 "; File generated automatically, do not edit!\n\n"
454 "NAME %s\n\n"
455 "EXPORTS\n",
456 libname);
457 }
458
459 void
460 PrintName(FILE *fileDest, EXPORT *pexp, PSTRING pstr, int fDeco)
461 {
462 const char *pcName = pstr->buf;
463 int nNameLength = pstr->len;
464 const char* pcDot, *pcAt;
465
466 /* Check for non-x86 first */
467 if (giArch != ARCH_X86)
468 {
469 /* Does the string already have stdcall decoration? */
470 pcAt = ScanToken(pcName, '@');
471 if (pcAt && (pcAt < (pcName + nNameLength)) && (pcName[0] == '_'))
472 {
473 /* Skip leading underscore and remove trailing decoration */
474 pcName++;
475 nNameLength = pcAt - pcName;
476 }
477
478 /* Print the undecorated function name */
479 fprintf(fileDest, "%.*s", nNameLength, pcName);
480 }
481 else if (fDeco &&
482 ((pexp->nCallingConvention == CC_STDCALL) ||
483 (pexp->nCallingConvention == CC_FASTCALL)))
484 {
485 /* Scan for a dll forwarding dot */
486 pcDot = ScanToken(pcName, '.');
487 if (pcDot)
488 {
489 /* First print the dll name, followed by a dot */
490 nNameLength = pcDot - pcName;
491 fprintf(fileDest, "%.*s.", nNameLength, pcName);
492
493 /* Now the actual function name */
494 pcName = pcDot + 1;
495 nNameLength = pexp->strTarget.len - nNameLength - 1;
496 }
497
498 /* Does the string already have decoration? */
499 pcAt = ScanToken(pcName, '@');
500 if (pcAt && (pcAt < (pcName + nNameLength)))
501 {
502 /* On GCC, we need to remove the leading stdcall underscore */
503 if (!gbMSComp && (pexp->nCallingConvention == CC_STDCALL))
504 {
505 pcName++;
506 nNameLength--;
507 }
508
509 /* Print the already decorated function name */
510 fprintf(fileDest, "%.*s", nNameLength, pcName);
511 }
512 else
513 {
514 /* Print the prefix, but skip it for (GCC && stdcall) */
515 if (gbMSComp || (pexp->nCallingConvention != CC_STDCALL))
516 {
517 fprintf(fileDest, "%c", pexp->nCallingConvention == CC_FASTCALL ? '@' : '_');
518 }
519
520 /* Print the name with trailing decoration */
521 fprintf(fileDest, "%.*s@%d", nNameLength, pcName, pexp->nStackBytes);
522 }
523 }
524 else
525 {
526 /* Print the undecorated function name */
527 fprintf(fileDest, "%.*s", nNameLength, pcName);
528 }
529 }
530
531 void
532 OutputLine_def_MS(FILE *fileDest, EXPORT *pexp)
533 {
534 PrintName(fileDest, pexp, &pexp->strName, 0);
535
536 if (gbImportLib)
537 {
538 /* Redirect to a stub function, to get the right decoration in the lib */
539 fprintf(fileDest, "=_stub_%.*s", pexp->strName.len, pexp->strName.buf);
540 }
541 else if (pexp->strTarget.buf)
542 {
543 if (pexp->strName.buf[0] == '?')
544 {
545 fprintf(stderr, "warning: ignoring C++ redirection %.*s -> %.*s\n",
546 pexp->strName.len, pexp->strName.buf, pexp->strTarget.len, pexp->strTarget.buf);
547 }
548 else
549 {
550 fprintf(fileDest, "=");
551
552 /* If the original name was decorated, use decoration in the forwarder as well */
553 if ((giArch == ARCH_X86) && ScanToken(pexp->strName.buf, '@') &&
554 !ScanToken(pexp->strTarget.buf, '@') &&
555 ((pexp->nCallingConvention == CC_STDCALL) ||
556 (pexp->nCallingConvention == CC_FASTCALL)) )
557 {
558 PrintName(fileDest, pexp, &pexp->strTarget, 1);
559 }
560 else
561 {
562 /* Write the undecorated redirection name */
563 fprintf(fileDest, "%.*s", pexp->strTarget.len, pexp->strTarget.buf);
564 }
565 }
566 }
567 else if (((pexp->uFlags & FL_STUB) || (pexp->nCallingConvention == CC_STUB)) &&
568 (pexp->strName.buf[0] == '?'))
569 {
570 /* C++ stubs are forwarded to C stubs */
571 fprintf(fileDest, "=stub_function%d", pexp->nNumber);
572 }
573 else if (gbTracing && ((pexp->uFlags & FL_NORELAY) == 0) && (pexp->nCallingConvention == CC_STDCALL) &&
574 (pexp->strName.buf[0] != '?'))
575 {
576 /* Redirect it to the relay-tracing trampoline */
577 fprintf(fileDest, "=$relaytrace$%.*s", pexp->strName.len, pexp->strName.buf);
578 }
579 }
580
581 void
582 OutputLine_def_GCC(FILE *fileDest, EXPORT *pexp)
583 {
584 int bTracing = 0;
585 /* Print the function name, with decoration for export libs */
586 PrintName(fileDest, pexp, &pexp->strName, gbImportLib);
587 DbgPrint("Generating def line for '%.*s'\n", pexp->strName.len, pexp->strName.buf);
588
589 /* Check if this is a forwarded export */
590 if (pexp->strTarget.buf)
591 {
592 int fIsExternal = !!ScanToken(pexp->strTarget.buf, '.');
593 DbgPrint("Got redirect '%.*s'\n", pexp->strTarget.len, pexp->strTarget.buf);
594
595 /* print the target name, don't decorate if it is external */
596 fprintf(fileDest, "=");
597 PrintName(fileDest, pexp, &pexp->strTarget, !fIsExternal);
598 }
599 else if (((pexp->uFlags & FL_STUB) || (pexp->nCallingConvention == CC_STUB)) &&
600 (pexp->strName.buf[0] == '?'))
601 {
602 /* C++ stubs are forwarded to C stubs */
603 fprintf(fileDest, "=stub_function%d", pexp->nNumber);
604 }
605 else if (gbTracing && ((pexp->uFlags & FL_NORELAY) == 0) && (pexp->nCallingConvention == CC_STDCALL) &&
606 (pexp->strName.buf[0] != '?'))
607 {
608 /* Redirect it to the relay-tracing trampoline */
609 char buf[256];
610 STRING strTarget;
611 fprintf(fileDest, "=");
612 sprintf(buf, "$relaytrace$%.*s", pexp->strName.len, pexp->strName.buf);
613 strTarget.buf = buf;
614 strTarget.len = pexp->strName.len + 12;
615 PrintName(fileDest, pexp, &strTarget, 1);
616 bTracing = 1;
617 }
618
619 /* Special handling for stdcall and fastcall */
620 if ((giArch == ARCH_X86) &&
621 ((pexp->nCallingConvention == CC_STDCALL) ||
622 (pexp->nCallingConvention == CC_FASTCALL)))
623 {
624 /* Is this the import lib? */
625 if (gbImportLib)
626 {
627 /* Is the name in the spec file decorated? */
628 const char* pcDeco = ScanToken(pexp->strName.buf, '@');
629 if (pcDeco && (pcDeco < pexp->strName.buf + pexp->strName.len))
630 {
631 /* Write the name including the leading @ */
632 fprintf(fileDest, "==%.*s", pexp->strName.len, pexp->strName.buf);
633 }
634 }
635 else if ((!pexp->strTarget.buf) && !(bTracing))
636 {
637 /* Write a forwarder to the actual decorated symbol */
638 fprintf(fileDest, "=");
639 PrintName(fileDest, pexp, &pexp->strName, 1);
640 }
641 }
642 }
643
644 int
645 OutputLine_def(FILE *fileDest, EXPORT *pexp)
646 {
647 DbgPrint("OutputLine_def: '%.*s'...\n", pexp->strName.len, pexp->strName.buf);
648 fprintf(fileDest, " ");
649
650 if (gbMSComp)
651 OutputLine_def_MS(fileDest, pexp);
652 else
653 OutputLine_def_GCC(fileDest, pexp);
654
655 if (pexp->uFlags & FL_ORDINAL)
656 {
657 fprintf(fileDest, " @%d", pexp->nOrdinal);
658 }
659
660 if (pexp->uFlags & FL_NONAME)
661 {
662 fprintf(fileDest, " NONAME");
663 }
664
665 if (pexp->uFlags & FL_PRIVATE)
666 {
667 fprintf(fileDest, " PRIVATE");
668 }
669
670 if (pexp->nCallingConvention == CC_EXTERN)
671 {
672 fprintf(fileDest, " DATA");
673 }
674
675 fprintf(fileDest, "\n");
676
677 return 1;
678 }
679
680 int
681 ParseFile(char* pcStart, FILE *fileDest, PFNOUTLINE OutputLine)
682 {
683 char *pc, *pcLine;
684 int nLine;
685 EXPORT exp;
686 int included;
687 char namebuffer[16];
688
689 //fprintf(stderr, "info: line %d, pcStart:'%.30s'\n", nLine, pcStart);
690
691 /* Loop all lines */
692 nLine = 1;
693 exp.nNumber = 0;
694 for (pcLine = pcStart; *pcLine; pcLine = NextLine(pcLine), nLine++)
695 {
696 pc = pcLine;
697
698 exp.nArgCount = 0;
699 exp.uFlags = 0;
700 exp.nNumber++;
701
702
703 //if (!strncmp(pcLine, "22 stdcall @(long) MPR_Alloc",28))
704 // gbDebug = 1;
705
706 //fprintf(stderr, "info: line %d, token:'%d, %.20s'\n",
707 // nLine, TokenLength(pcLine), pcLine);
708
709 /* Skip white spaces */
710 while (*pc == ' ' || *pc == '\t') pc++;
711
712 /* Skip empty lines, stop at EOF */
713 if (*pc == ';' || *pc <= '#') continue;
714 if (*pc == 0) return 0;
715
716 //fprintf(stderr, "info: line %d, token:'%.*s'\n",
717 // nLine, TokenLength(pc), pc);
718
719 /* Now we should get either an ordinal or @ */
720 if (*pc == '@')
721 exp.nOrdinal = -1;
722 else
723 {
724 exp.nOrdinal = atol(pc);
725 /* The import lib should contain the ordinal only if -ordinal was specified */
726 if (!gbImportLib)
727 exp.uFlags |= FL_ORDINAL;
728 }
729
730 /* Go to next token (type) */
731 if (!(pc = NextToken(pc)))
732 {
733 fprintf(stderr, "error: line %d, unexpected end of line\n", nLine);
734 return -10;
735 }
736
737 //fprintf(stderr, "info: Token:'%.*s'\n", TokenLength(pc), pc);
738
739 /* Now we should get the type */
740 if (CompareToken(pc, "stdcall"))
741 {
742 exp.nCallingConvention = CC_STDCALL;
743 }
744 else if (CompareToken(pc, "cdecl") ||
745 CompareToken(pc, "varargs"))
746 {
747 exp.nCallingConvention = CC_CDECL;
748 }
749 else if (CompareToken(pc, "fastcall"))
750 {
751 exp.nCallingConvention = CC_FASTCALL;
752 }
753 else if (CompareToken(pc, "thiscall"))
754 {
755 exp.nCallingConvention = CC_THISCALL;
756 }
757 else if (CompareToken(pc, "extern"))
758 {
759 exp.nCallingConvention = CC_EXTERN;
760 }
761 else if (CompareToken(pc, "stub"))
762 {
763 exp.nCallingConvention = CC_STUB;
764 }
765 else
766 {
767 fprintf(stderr, "error: line %d, expected callconv, got '%.*s' %d\n",
768 nLine, TokenLength(pc), pc, *pc);
769 return -11;
770 }
771
772 //fprintf(stderr, "info: nCallingConvention: %d\n", exp.nCallingConvention);
773
774 /* Go to next token (options or name) */
775 if (!(pc = NextToken(pc)))
776 {
777 fprintf(stderr, "fail2\n");
778 return -12;
779 }
780
781 /* Handle options */
782 included = 1;
783 while (*pc == '-')
784 {
785 if (CompareToken(pc, "-arch"))
786 {
787 /* Default to not included */
788 included = 0;
789 pc += 5;
790
791 /* Look if we are included */
792 while (*pc == '=' || *pc == ',')
793 {
794 pc++;
795 if (CompareToken(pc, pszArchString) ||
796 CompareToken(pc, pszArchString2))
797 {
798 included = 1;
799 }
800
801 /* Skip to next arch or end */
802 while (*pc > ',') pc++;
803 }
804 }
805 else if (CompareToken(pc, "-i386"))
806 {
807 if (giArch != ARCH_X86) included = 0;
808 }
809 else if (CompareToken(pc, "-private"))
810 {
811 exp.uFlags |= FL_PRIVATE;
812 }
813 else if (CompareToken(pc, "-noname"))
814 {
815 exp.uFlags |= FL_ORDINAL | FL_NONAME;
816 }
817 else if (CompareToken(pc, "-ordinal"))
818 {
819 exp.uFlags |= FL_ORDINAL;
820 /* GCC doesn't automatically import by ordinal if an ordinal
821 * is found in the def file. Force it. */
822 if (gbImportLib && !gbMSComp)
823 exp.uFlags |= FL_NONAME;
824 }
825 else if (CompareToken(pc, "-stub"))
826 {
827 exp.uFlags |= FL_STUB;
828 }
829 else if (CompareToken(pc, "-norelay"))
830 {
831 exp.uFlags |= FL_NORELAY;
832 }
833 else if (CompareToken(pc, "-ret64"))
834 {
835 exp.uFlags |= FL_RET64;
836 }
837 else if (CompareToken(pc, "-register"))
838 {
839 exp.uFlags |= FL_REGISTER;
840 }
841 else
842 {
843 fprintf(stderr, "info: ignored option: '%.*s'\n",
844 TokenLength(pc), pc);
845 }
846
847 /* Go to next token */
848 pc = NextToken(pc);
849 }
850
851 //fprintf(stderr, "info: Name:'%.10s'\n", pc);
852
853 /* If arch didn't match ours, skip this entry */
854 if (!included) continue;
855
856 /* Get name */
857 exp.strName.buf = pc;
858 exp.strName.len = TokenLength(pc);
859 DbgPrint("Got name: '%.*s'\n", exp.strName.len, exp.strName.buf);
860
861 /* Check for autoname */
862 if ((exp.strName.len == 1) && (exp.strName.buf[0] == '@'))
863 {
864 sprintf(namebuffer, "ordinal%d", exp.nOrdinal);
865 exp.strName.len = strlen(namebuffer);
866 exp.strName.buf = namebuffer;
867 exp.uFlags |= FL_ORDINAL | FL_NONAME;
868 }
869
870 /* Handle parameters */
871 exp.nStackBytes = 0;
872 if (exp.nCallingConvention != CC_EXTERN &&
873 exp.nCallingConvention != CC_STUB)
874 {
875 /* Go to next token */
876 if (!(pc = NextToken(pc)))
877 {
878 fprintf(stderr, "fail4\n");
879 return -13;
880 }
881
882 /* Verify syntax */
883 if (*pc++ != '(')
884 {
885 fprintf(stderr, "error: line %d, expected '('\n", nLine);
886 return -14;
887 }
888
889 /* Skip whitespaces */
890 while (*pc == ' ' || *pc == '\t') pc++;
891
892 exp.nStackBytes = 0;
893 while (*pc >= '0')
894 {
895 if (CompareToken(pc, "long"))
896 {
897 exp.nStackBytes += 4;
898 exp.anArgs[exp.nArgCount] = ARG_LONG;
899 }
900 else if (CompareToken(pc, "double"))
901 {
902 exp.nStackBytes += 8;
903 exp.anArgs[exp.nArgCount] = ARG_DBL;
904 }
905 else if (CompareToken(pc, "ptr"))
906 {
907 exp.nStackBytes += 4; // sizeof(void*) on x86
908 exp.anArgs[exp.nArgCount] = ARG_PTR;
909 }
910 else if (CompareToken(pc, "str"))
911 {
912 exp.nStackBytes += 4; // sizeof(void*) on x86
913 exp.anArgs[exp.nArgCount] = ARG_STR;
914 }
915 else if (CompareToken(pc, "wstr"))
916 {
917 exp.nStackBytes += 4; // sizeof(void*) on x86
918 exp.anArgs[exp.nArgCount] = ARG_WSTR;
919 }
920 else if (CompareToken(pc, "int64"))
921 {
922 exp.nStackBytes += 8;
923 exp.anArgs[exp.nArgCount] = ARG_INT64;
924 }
925 else if (CompareToken(pc, "int128"))
926 {
927 exp.nStackBytes += 16;
928 exp.anArgs[exp.nArgCount] = ARG_INT128;
929 }
930 else if (CompareToken(pc, "float"))
931 {
932 exp.nStackBytes += 4;
933 exp.anArgs[exp.nArgCount] = ARG_FLOAT;
934 }
935 else
936 fprintf(stderr, "error: line %d, expected type, got: %.10s\n", nLine, pc);
937
938 exp.nArgCount++;
939
940 /* Go to next parameter */
941 if (!(pc = NextToken(pc)))
942 {
943 fprintf(stderr, "fail5\n");
944 return -15;
945 }
946 }
947
948 /* Check syntax */
949 if (*pc++ != ')')
950 {
951 fprintf(stderr, "error: line %d, expected ')'\n", nLine);
952 return -16;
953 }
954 }
955
956 /* Handle special stub cases */
957 if (exp.nCallingConvention == CC_STUB)
958 {
959 /* Check for c++ mangled name */
960 if (pc[0] == '?')
961 {
962 //printf("Found c++ mangled name...\n");
963 //
964 }
965 else
966 {
967 /* Check for stdcall name */
968 const char *p = ScanToken(pc, '@');
969 if (p && (p - pc < exp.strName.len))
970 {
971 int i;
972
973 /* Truncate the name to before the @ */
974 exp.strName.len = (int)(p - pc);
975 if (exp.strName.len < 1)
976 {
977 fprintf(stderr, "error, @ in line %d\n", nLine);
978 return -1;
979 }
980 exp.nStackBytes = atoi(p + 1);
981 exp.nArgCount = exp.nStackBytes / 4;
982 exp.nCallingConvention = CC_STDCALL;
983 exp.uFlags |= FL_STUB;
984 for (i = 0; i < exp.nArgCount; i++)
985 exp.anArgs[i] = ARG_LONG;
986 }
987 }
988 }
989
990 /* Get optional redirection */
991 pc = NextToken(pc);
992 if (pc)
993 {
994 exp.strTarget.buf = pc;
995 exp.strTarget.len = TokenLength(pc);
996
997 /* Check syntax (end of line) */
998 if (NextToken(pc))
999 {
1000 fprintf(stderr, "error: line %d, additional tokens after ')'\n", nLine);
1001 return -17;
1002 }
1003
1004 /* Don't relay-trace forwarded functions */
1005 exp.uFlags |= FL_NORELAY;
1006 }
1007 else
1008 {
1009 exp.strTarget.buf = 0;
1010 exp.strTarget.len = 0;
1011 }
1012
1013 /* Check for no-name without ordinal */
1014 if ((exp.uFlags & FL_ORDINAL) && (exp.nOrdinal == -1))
1015 {
1016 fprintf(stderr, "error: line %d, ordinal export without ordinal!\n", nLine);
1017 return -1;
1018 }
1019
1020 OutputLine(fileDest, &exp);
1021 gbDebug = 0;
1022 }
1023
1024 return 0;
1025 }
1026
1027
1028 void usage(void)
1029 {
1030 printf("syntax: spec2def [<options> ...] <spec file>\n"
1031 "Possible options:\n"
1032 " -h --help prints this screen\n"
1033 " -l=<file> generates an asm lib stub\n"
1034 " -d=<file> generates a def file\n"
1035 " -s=<file> generates a stub file\n"
1036 " --ms msvc compatibility\n"
1037 " -n=<name> name of the dll\n"
1038 " --implib generate a def file for an import library\n"
1039 " -a=<arch> Set architecture to <arch>. (i386, x86_64, arm)\n"
1040 " --with-tracing generates wine-like \"+relay\" trace trampolines. (necessitates -s)\n");
1041 }
1042
1043 int main(int argc, char *argv[])
1044 {
1045 size_t nFileSize;
1046 char *pszSource, *pszDefFileName = 0, *pszStubFileName = 0, *pszLibStubName = 0;
1047 char achDllName[40];
1048 FILE *file;
1049 int result = 0, i;
1050
1051 if (argc < 2)
1052 {
1053 usage();
1054 return -1;
1055 }
1056
1057 /* Read options */
1058 for (i = 1; i < argc && *argv[i] == '-'; i++)
1059 {
1060 if ((strcasecmp(argv[i], "--help") == 0) ||
1061 (strcasecmp(argv[i], "-h") == 0))
1062 {
1063 usage();
1064 return 0;
1065 }
1066 else if (argv[i][1] == 'd' && argv[i][2] == '=')
1067 {
1068 pszDefFileName = argv[i] + 3;
1069 }
1070 else if (argv[i][1] == 'l' && argv[i][2] == '=')
1071 {
1072 pszLibStubName = argv[i] + 3;
1073 }
1074 else if (argv[i][1] == 's' && argv[i][2] == '=')
1075 {
1076 pszStubFileName = argv[i] + 3;
1077 }
1078 else if (argv[i][1] == 'n' && argv[i][2] == '=')
1079 {
1080 pszDllName = argv[i] + 3;
1081 }
1082 else if ((strcasecmp(argv[i], "--implib") == 0))
1083 {
1084 gbImportLib = 1;
1085 }
1086 else if ((strcasecmp(argv[i], "--ms") == 0))
1087 {
1088 gbMSComp = 1;
1089 }
1090 else if ((strcasecmp(argv[i], "--with-tracing") == 0))
1091 {
1092 if (!pszStubFileName)
1093 {
1094 fprintf(stderr, "Error: cannot use --with-tracing without -s option.\n");
1095 return -1;
1096 }
1097 gbTracing = 1;
1098 }
1099 else if (argv[i][1] == 'a' && argv[i][2] == '=')
1100 {
1101 pszArchString = argv[i] + 3;
1102 }
1103 else
1104 {
1105 fprintf(stderr, "Unrecognized option: %s\n", argv[i]);
1106 return -1;
1107 }
1108 }
1109
1110 if (strcasecmp(pszArchString, "i386") == 0)
1111 {
1112 giArch = ARCH_X86;
1113 gpszUnderscore = "_";
1114 }
1115 else if (strcasecmp(pszArchString, "x86_64") == 0) giArch = ARCH_AMD64;
1116 else if (strcasecmp(pszArchString, "ia64") == 0) giArch = ARCH_IA64;
1117 else if (strcasecmp(pszArchString, "arm") == 0) giArch = ARCH_ARM;
1118 else if (strcasecmp(pszArchString, "ppc") == 0) giArch = ARCH_PPC;
1119
1120 if ((giArch == ARCH_AMD64) || (giArch == ARCH_IA64))
1121 {
1122 pszArchString2 = "win64";
1123 }
1124 else
1125 pszArchString2 = "win32";
1126
1127 /* Set a default dll name */
1128 if (!pszDllName)
1129 {
1130 char *p1, *p2;
1131 size_t len;
1132
1133 p1 = strrchr(argv[i], '\\');
1134 if (!p1) p1 = strrchr(argv[i], '/');
1135 p2 = p1 = p1 ? p1 + 1 : argv[i];
1136
1137 /* walk up to '.' */
1138 while (*p2 != '.' && *p2 != 0) p2++;
1139 len = p2 - p1;
1140 if (len >= sizeof(achDllName) - 5)
1141 {
1142 fprintf(stderr, "name too long: %s\n", p1);
1143 return -2;
1144 }
1145
1146 strncpy(achDllName, p1, len);
1147 strncpy(achDllName + len, ".dll", sizeof(achDllName) - len);
1148 pszDllName = achDllName;
1149 }
1150
1151 /* Open input file argv[1] */
1152 file = fopen(argv[i], "r");
1153 if (!file)
1154 {
1155 fprintf(stderr, "error: could not open file %s ", argv[i]);
1156 return -3;
1157 }
1158
1159 /* Get file size */
1160 fseek(file, 0, SEEK_END);
1161 nFileSize = ftell(file);
1162 rewind(file);
1163
1164 /* Allocate memory buffer */
1165 pszSource = malloc(nFileSize + 1);
1166 if (!pszSource)
1167 {
1168 fclose(file);
1169 return -4;
1170 }
1171
1172 /* Load input file into memory */
1173 nFileSize = fread(pszSource, 1, nFileSize, file);
1174 fclose(file);
1175
1176 /* Zero terminate the source */
1177 pszSource[nFileSize] = '\0';
1178
1179 if (pszDefFileName)
1180 {
1181 /* Open output file */
1182 file = fopen(pszDefFileName, "w");
1183 if (!file)
1184 {
1185 fprintf(stderr, "error: could not open output file %s ", argv[i + 1]);
1186 return -5;
1187 }
1188
1189 OutputHeader_def(file, pszDllName);
1190 result = ParseFile(pszSource, file, OutputLine_def);
1191 fclose(file);
1192 }
1193
1194 if (pszStubFileName)
1195 {
1196 /* Open output file */
1197 file = fopen(pszStubFileName, "w");
1198 if (!file)
1199 {
1200 fprintf(stderr, "error: could not open output file %s ", argv[i + 1]);
1201 return -5;
1202 }
1203
1204 OutputHeader_stub(file);
1205 result = ParseFile(pszSource, file, OutputLine_stub);
1206 fclose(file);
1207 }
1208
1209 if (pszLibStubName)
1210 {
1211 /* Open output file */
1212 file = fopen(pszLibStubName, "w");
1213 if (!file)
1214 {
1215 fprintf(stderr, "error: could not open output file %s ", argv[i + 1]);
1216 return -5;
1217 }
1218
1219 OutputHeader_asmstub(file, pszDllName);
1220 result = ParseFile(pszSource, file, OutputLine_asmstub);
1221 fprintf(file, "\n END\n");
1222 fclose(file);
1223 }
1224
1225
1226 return result;
1227 }