[SPEC2DEF]
[reactos.git] / reactos / tools / spec2def / spec2def.c
index e9ef407..5264f41 100644 (file)
@@ -7,12 +7,16 @@
 #define strcasecmp _stricmp
 #endif
 
+typedef struct _STRING
+{
+    const char *buf;
+    int len;
+} STRING, *PSTRING;
+
 typedef struct
 {
-    char *pcName;
-    size_t nNameLength;
-    char *pcRedirection;
-    int nRedirectionLength;
+    STRING strName;
+    STRING strTarget;
     int nCallingConvention;
     int nOrdinal;
     int nStackBytes;
@@ -32,21 +36,26 @@ enum _ARCH
 };
 
 typedef int (*PFNOUTLINE)(FILE *, EXPORT *);
-int gbKillAt = 0;
 int gbMSComp = 0;
 int gbImportLib = 0;
-int no_redirections = 0;
+int gbTracing = 0;
 int giArch = ARCH_X86;
 char *pszArchString = "i386";
 char *pszArchString2;
 char *pszDllName = 0;
 char *gpszUnderscore = "";
+int gbDebug;
+#define DbgPrint(...) (!gbDebug || fprintf(stderr, __VA_ARGS__))
 
 enum
 {
     FL_PRIVATE = 1,
     FL_STUB = 2,
     FL_NONAME = 4,
+    FL_ORDINAL = 8,
+    FL_NORELAY = 16,
+    FL_RET64 = 32,
+    FL_REGISTER = 64,
 };
 
 enum
@@ -67,6 +76,7 @@ enum
     ARG_WSTR,
     ARG_DBL,
     ARG_INT64,
+    ARG_INT128,
     ARG_FLOAT
 };
 
@@ -100,12 +110,13 @@ CompareToken(const char *token, const char *comparand)
     return 1;
 }
 
-int
+const char *
 ScanToken(const char *token, char chr)
 {
     while (!IsSeparator(*token))
     {
-        if (*token++ == chr) return 1;
+        if (*token == chr) return token;
+        token++;
     }
     return 0;
 }
@@ -154,50 +165,126 @@ void
 OutputHeader_stub(FILE *file)
 {
     fprintf(file, "/* This file is autogenerated, do not edit. */\n\n"
-            "#include <stubs.h>\n\n");
+            "#include <stubs.h>\n");
+
+    if (gbTracing)
+    {
+        fprintf(file, "#include <wine/debug.h>\n");
+        fprintf(file, "#include <inttypes.h>\n");
+        fprintf(file, "WINE_DECLARE_DEBUG_CHANNEL(relay);\n");
+    }
+
+    fprintf(file, "\n");
 }
 
 int
 OutputLine_stub(FILE *file, EXPORT *pexp)
 {
     int i;
+    int bRelay = 0;
+    int bInPrototype = 0;
 
     if (pexp->nCallingConvention != CC_STUB &&
-        (pexp->uFlags & FL_STUB) == 0) return 0;
-
-    fprintf(file, "int ");
-    if ((giArch == ARCH_X86) &&
-        pexp->nCallingConvention == CC_STDCALL)
+        (pexp->uFlags & FL_STUB) == 0)
     {
-        fprintf(file, "__stdcall ");
+        /* Only relay trace stdcall C functions */
+        if (!gbTracing || (pexp->nCallingConvention != CC_STDCALL)
+                || (pexp->uFlags & FL_NORELAY)
+                || (pexp->strName.buf[0] == '?'))
+        {
+            return 0;
+        }
+        bRelay = 1;
     }
 
-    /* Check for C++ */
-    if (pexp->pcName[0] == '?')
+    /* Declare the "real" function */
+    if (bRelay)
     {
-        fprintf(file, "stub_function%d(", pexp->nNumber);
+        fprintf(file, "extern ");
+        bInPrototype = 1;
     }
-    else
+
+    do
     {
-        fprintf(file, "%.*s(", pexp->nNameLength, pexp->pcName);
-    }
+        if (pexp->uFlags & FL_REGISTER)
+        {
+            /* FIXME: Not sure this is right */
+            fprintf(file, "void ");
+        }
+        else if (pexp->uFlags & FL_RET64)
+        {
+            fprintf(file, "__int64 ");
+        }
+        else
+        {
+            fprintf(file, "int ");
+        }
 
-    for (i = 0; i < pexp->nArgCount; i++)
+        if ((giArch == ARCH_X86) &&
+            pexp->nCallingConvention == CC_STDCALL)
+        {
+            fprintf(file, "__stdcall ");
+        }
+
+        /* Check for C++ */
+        if (pexp->strName.buf[0] == '?')
+        {
+            fprintf(file, "stub_function%d(", pexp->nNumber);
+        }
+        else
+        {
+            if (!bRelay || bInPrototype)
+                fprintf(file, "%.*s(", pexp->strName.len, pexp->strName.buf);
+            else
+                fprintf(file, "$relaytrace$%.*s(", pexp->strName.len, pexp->strName.buf);
+        }
+
+        for (i = 0; i < pexp->nArgCount; i++)
+        {
+            if (i != 0) fprintf(file, ", ");
+            switch (pexp->anArgs[i])
+            {
+                case ARG_LONG: fprintf(file, "long"); break;
+                case ARG_PTR:  fprintf(file, "void*"); break;
+                case ARG_STR:  fprintf(file, "char*"); break;
+                case ARG_WSTR: fprintf(file, "wchar_t*"); break;
+                case ARG_DBL:  fprintf(file, "double"); break;
+                case ARG_INT64 :  fprintf(file, "__int64"); break;
+                case ARG_INT128 :  fprintf(file, "__int128"); break;
+                case ARG_FLOAT: fprintf(file, "float"); break;
+            }
+            fprintf(file, " a%d", i);
+        }
+
+        if (bInPrototype)
+        {
+            fprintf(file, ");\n\n");
+        }
+    } while (bInPrototype--);
+
+    if (!bRelay)
     {
-        if (i != 0) fprintf(file, ", ");
-        switch (pexp->anArgs[i])
+        fprintf(file, ")\n{\n\tDbgPrint(\"WARNING: calling stub %.*s(",
+                pexp->strName.len, pexp->strName.buf);
+    }
+    else
+    {
+        fprintf(file, ")\n{\n");
+        if (pexp->uFlags & FL_REGISTER)
+        {
+            /* No return value */
+        }
+        else if (pexp->uFlags & FL_RET64)
         {
-            case ARG_LONG: fprintf(file, "long"); break;
-            case ARG_PTR:  fprintf(file, "void*"); break;
-            case ARG_STR:  fprintf(file, "char*"); break;
-            case ARG_WSTR: fprintf(file, "wchar_t*"); break;
-            case ARG_DBL: case ARG_INT64 :  fprintf(file, "__int64"); break;
-            case ARG_FLOAT: fprintf(file, "float"); break;
+            fprintf(file, "\t__int64 retval;\n");
         }
-        fprintf(file, " a%d", i);
+        else
+        {
+            fprintf(file, "\tint retval;\n");
+        }
+        fprintf(file, "\tif (TRACE_ON(relay))\n\t\tDPRINTF(\"%s: %.*s(",
+                        pszDllName, pexp->strName.len, pexp->strName.buf);
     }
-    fprintf(file, ")\n{\n\tDPRINT1(\"WARNING: calling stub %.*s(",
-            pexp->nNameLength, pexp->pcName);
 
     for (i = 0; i < pexp->nArgCount; i++)
     {
@@ -210,6 +297,7 @@ OutputLine_stub(FILE *file, EXPORT *pexp)
             case ARG_WSTR: fprintf(file, "'%%ws'"); break;
             case ARG_DBL:  fprintf(file, "%%f"); break;
             case ARG_INT64: fprintf(file, "%%\"PRix64\""); break;
+            case ARG_INT128: fprintf(file, "%%\"PRix128\""); break;
             case ARG_FLOAT: fprintf(file, "%%f"); break;
         }
     }
@@ -226,6 +314,7 @@ OutputLine_stub(FILE *file, EXPORT *pexp)
             case ARG_WSTR: fprintf(file, "(wchar_t*)a%d", i); break;
             case ARG_DBL:  fprintf(file, "(double)a%d", i); break;
             case ARG_INT64: fprintf(file, "(__int64)a%d", i); break;
+            case ARG_INT128: fprintf(file, "(__int128)a%d", i); break;
             case ARG_FLOAT: fprintf(file, "(float)a%d", i); break;
         }
     }
@@ -235,8 +324,42 @@ OutputLine_stub(FILE *file, EXPORT *pexp)
     {
         fprintf(file, "\t__wine_spec_unimplemented_stub(\"%s\", __FUNCTION__);\n", pszDllName);
     }
+    else if (bRelay)
+    {
+        if (pexp->uFlags & FL_REGISTER)
+        {
+            fprintf(file,"\t");
+        }
+        else
+        {
+            fprintf(file, "\tretval = ");
+        }
+        fprintf(file, "%.*s(", pexp->strName.len, pexp->strName.buf);
+
+        for (i = 0; i < pexp->nArgCount; i++)
+        {
+            if (i != 0) fprintf(file, ", ");
+            fprintf(file, "a%d", i);
+        }
+        fprintf(file, ");\n");
+    }
 
-    fprintf(file, "\treturn 0;\n}\n\n");
+    if (!bRelay)
+        fprintf(file, "\treturn 0;\n}\n\n");
+    else if ((pexp->uFlags & FL_REGISTER) == 0)
+    {
+        if (pexp->uFlags & FL_RET64)
+        {
+            fprintf(file, "\tif (TRACE_ON(relay))\n\t\tDPRINTF(\"%s: %.*s: retval = %%\"PRIx64\"\\n\", retval);\n",
+                pszDllName, pexp->strName.len, pexp->strName.buf);
+        }
+        else
+        {
+            fprintf(file, "\tif (TRACE_ON(relay))\n\t\tDPRINTF(\"%s: %.*s: retval = 0x%%lx\\n\", retval);\n",
+                pszDllName, pexp->strName.len, pexp->strName.buf);
+        }
+        fprintf(file, "\treturn retval;\n}\n\n");
+    }
 
     return 1;
 }
@@ -247,52 +370,81 @@ OutputHeader_asmstub(FILE *file, char *libname)
     fprintf(file, "; File generated automatically, do not edit! \n\n");
 
     if (giArch == ARCH_X86)
-        fprintf(file, ".586\n.model flat\n");
+    {
+        fprintf(file, ".586\n.model flat\n.code\n");
+    }
+    else if (giArch == ARCH_AMD64)
+    {
+        fprintf(file, ".code\n");
+    }
+    else if (giArch == ARCH_ARM)
+    {
+        fprintf(file,
+                "    AREA |.text|,ALIGN=2,CODE,READONLY\n\n");
+    }
+}
 
-    fprintf(file, ".code\n");
+void
+Output_stublabel(FILE *fileDest, char* pszSymbolName)
+{
+    if (giArch == ARCH_ARM)
+    {
+        fprintf(fileDest,
+                "      EXPORT %s [FUNC]\n%s\n",
+                pszSymbolName,
+                pszSymbolName);
+    }
+    else
+    {
+        fprintf(fileDest,
+                "PUBLIC %s\n%s: nop\n",
+                pszSymbolName,
+                pszSymbolName);
+    }
 }
 
 int
 OutputLine_asmstub(FILE *fileDest, EXPORT *pexp)
 {
+    char szNameBuffer[128];
+
     /* Handle autoname */
-    if (pexp->nNameLength == 1 && pexp->pcName[0] == '@')
+    if (pexp->strName.len == 1 && pexp->strName.buf[0] == '@')
     {
-        fprintf(fileDest, "PUBLIC %sordinal%d\n%sordinal%d: nop\n",
+        sprintf(szNameBuffer, "%sordinal%d\n%sordinal%d: nop\n",
                 gpszUnderscore, pexp->nOrdinal, gpszUnderscore, pexp->nOrdinal);
     }
     else if (giArch != ARCH_X86)
     {
-        fprintf(fileDest, "PUBLIC _stub_%.*s\n_stub_%.*s: nop\n",
-                pexp->nNameLength, pexp->pcName,
-                pexp->nNameLength, pexp->pcName);
+        sprintf(szNameBuffer, "_stub_%.*s",
+                pexp->strName.len, pexp->strName.buf);
     }
     else if (pexp->nCallingConvention == CC_STDCALL)
     {
-        fprintf(fileDest, "PUBLIC __stub_%.*s@%d\n__stub_%.*s@%d: nop\n",
-                pexp->nNameLength, pexp->pcName, pexp->nStackBytes,
-                pexp->nNameLength, pexp->pcName, pexp->nStackBytes);
+        sprintf(szNameBuffer, "__stub_%.*s@%d",
+                pexp->strName.len, pexp->strName.buf, pexp->nStackBytes);
     }
     else if (pexp->nCallingConvention == CC_FASTCALL)
     {
-        fprintf(fileDest, "PUBLIC @_stub_%.*s@%d\n@_stub_%.*s@%d: nop\n",
-                pexp->nNameLength, pexp->pcName, pexp->nStackBytes,
-                pexp->nNameLength, pexp->pcName, pexp->nStackBytes);
+        sprintf(szNameBuffer, "@_stub_%.*s@%d",
+                pexp->strName.len, pexp->strName.buf, pexp->nStackBytes);
     }
-    else if (pexp->nCallingConvention == CC_CDECL ||
-             pexp->nCallingConvention == CC_STUB)
+    else if ((pexp->nCallingConvention == CC_CDECL) ||
+             (pexp->nCallingConvention == CC_THISCALL) ||
+             (pexp->nCallingConvention == CC_EXTERN) ||
+             (pexp->nCallingConvention == CC_STUB))
     {
-        fprintf(fileDest, "PUBLIC __stub_%.*s\n__stub_%.*s: nop\n",
-                pexp->nNameLength, pexp->pcName,
-                pexp->nNameLength, pexp->pcName);
+        sprintf(szNameBuffer, "__stub_%.*s",
+                pexp->strName.len, pexp->strName.buf);
     }
-    else if (pexp->nCallingConvention == CC_EXTERN)
+    else
     {
-        fprintf(fileDest, "PUBLIC __stub_%.*s\n__stub_%.*s:\n",
-                pexp->nNameLength, pexp->pcName,
-                pexp->nNameLength, pexp->pcName);
+        fprintf(stderr, "Invalid calling convention");
+        return 0;
     }
 
+    Output_stublabel(fileDest, szNameBuffer);
+
     return 1;
 }
 
@@ -301,85 +453,215 @@ OutputHeader_def(FILE *file, char *libname)
 {
     fprintf(file,
             "; File generated automatically, do not edit!\n\n"
-            "LIBRARY %s\n\n"
+            "NAME %s\n\n"
             "EXPORTS\n",
             libname);
 }
 
 void
-PrintName(FILE *fileDest, EXPORT *pexp, char *pszPrefix, int fRedir, int fDeco)
+PrintName(FILE *fileDest, EXPORT *pexp, PSTRING pstr, int fDeco)
 {
-    char *pcName = fRedir ? pexp->pcRedirection : pexp->pcName;
-    size_t nNameLength = fRedir ? pexp->nRedirectionLength : pexp->nNameLength;
+    const char *pcName = pstr->buf;
+    int nNameLength = pstr->len;
+    const char* pcDot, *pcAt;
 
-    /* Handle autoname */
-    if (nNameLength == 1 && pcName[0] == '@')
+    /* Check for non-x86 first */
+    if (giArch != ARCH_X86)
     {
-        fprintf(fileDest, "ordinal%d", pexp->nOrdinal);
+        /* Does the string already have stdcall decoration? */
+        pcAt = ScanToken(pcName, '@');
+        if (pcAt && (pcAt < (pcName + nNameLength)) && (pcName[0] == '_'))
+        {
+            /* Skip leading underscore and remove trailing decoration */
+            pcName++;
+            nNameLength = pcAt - pcName;
+        }
+
+        /* Print the undecorated function name */
+        fprintf(fileDest, "%.*s", nNameLength, pcName);
     }
-    else
+    else if (fDeco &&
+        ((pexp->nCallingConvention == CC_STDCALL) ||
+         (pexp->nCallingConvention == CC_FASTCALL)))
     {
-        if (fDeco && pexp->nCallingConvention == CC_FASTCALL)
-             fprintf(fileDest, "@");
-        fprintf(fileDest, "%s%.*s", pszPrefix, nNameLength, pcName);
-        if ((pexp->nCallingConvention == CC_STDCALL ||
-            pexp->nCallingConvention == CC_FASTCALL) && fDeco)
+        /* Scan for a dll forwarding dot */
+        pcDot = ScanToken(pcName, '.');
+        if (pcDot)
+        {
+            /* First print the dll name, followed by a dot */
+            nNameLength = pcDot - pcName;
+            fprintf(fileDest, "%.*s.", nNameLength, pcName);
+
+            /* Now the actual function name */
+            pcName = pcDot + 1;
+            nNameLength = pexp->strTarget.len - nNameLength - 1;
+        }
+
+        /* Does the string already have decoration? */
+        pcAt = ScanToken(pcName, '@');
+        if (pcAt && (pcAt < (pcName + nNameLength)))
+        {
+            /* On GCC, we need to remove the leading stdcall underscore */
+            if (!gbMSComp && (pexp->nCallingConvention == CC_STDCALL))
+            {
+                pcName++;
+                nNameLength--;
+            }
+
+            /* Print the already decorated function name */
+            fprintf(fileDest, "%.*s", nNameLength, pcName);
+        }
+        else
         {
-            fprintf(fileDest, "@%d", pexp->nStackBytes);
+            /* Print the prefix, but skip it for (GCC && stdcall) */
+            if (gbMSComp || (pexp->nCallingConvention != CC_STDCALL))
+            {
+                fprintf(fileDest, "%c", pexp->nCallingConvention == CC_FASTCALL ? '@' : '_');
+            }
+
+            /* Print the name with trailing decoration */
+            fprintf(fileDest, "%.*s@%d", nNameLength, pcName, pexp->nStackBytes);
         }
     }
+    else
+    {
+        /* Print the undecorated function name */
+        fprintf(fileDest, "%.*s", nNameLength, pcName);
+    }
 }
 
-int
-OutputLine_def(FILE *fileDest, EXPORT *pexp)
+void
+OutputLine_def_MS(FILE *fileDest, EXPORT *pexp)
 {
-    fprintf(fileDest, " ");
-
-    PrintName(fileDest, pexp, "", 0, (giArch == ARCH_X86) && !gbKillAt);
+    PrintName(fileDest, pexp, &pexp->strName, 0);
 
     if (gbImportLib)
     {
-        fprintf(fileDest, "=");
-        PrintName(fileDest, pexp, "_stub_", 0, 0);
+        /* Redirect to a stub function, to get the right decoration in the lib */
+        fprintf(fileDest, "=_stub_%.*s", pexp->strName.len, pexp->strName.buf);
     }
-    else if (pexp->pcRedirection)
+    else if (pexp->strTarget.buf)
     {
-        if (gbMSComp && (pexp->pcName[0] == '?'))
+        if (pexp->strName.buf[0] == '?')
         {
-            /* ignore c++ redirection, since link doesn't like that! */
+            fprintf(stderr, "warning: ignoring C++ redirection %.*s -> %.*s\n",
+                    pexp->strName.len, pexp->strName.buf, pexp->strTarget.len, pexp->strTarget.buf);
         }
         else
         {
-            int fDeco;
-
-            fDeco = ((giArch == ARCH_X86) && !ScanToken(pexp->pcRedirection, '.'));
             fprintf(fileDest, "=");
-            PrintName(fileDest, pexp, "", 1, fDeco && !gbMSComp);
+
+            /* If the original name was decorated, use decoration in the forwarder as well */
+            if ((giArch == ARCH_X86) && ScanToken(pexp->strName.buf, '@') &&
+                !ScanToken(pexp->strTarget.buf, '@') &&
+                ((pexp->nCallingConvention == CC_STDCALL) ||
+                (pexp->nCallingConvention == CC_FASTCALL)) )
+            {
+                PrintName(fileDest, pexp, &pexp->strTarget, 1);
+            }
+            else
+            {
+                /* Write the undecorated redirection name */
+                fprintf(fileDest, "%.*s", pexp->strTarget.len, pexp->strTarget.buf);
+            }
         }
     }
     else if (((pexp->uFlags & FL_STUB) || (pexp->nCallingConvention == CC_STUB)) &&
-             (pexp->pcName[0] == '?'))
+             (pexp->strName.buf[0] == '?'))
     {
         /* C++ stubs are forwarded to C stubs */
+        fprintf(fileDest, "=stub_function%d", pexp->nNumber);
+    }
+    else if (gbTracing && ((pexp->uFlags & FL_NORELAY) == 0) && (pexp->nCallingConvention == CC_STDCALL) &&
+            (pexp->strName.buf[0] != '?'))
+    {
+        /* Redirect it to the relay-tracing trampoline */
+        fprintf(fileDest, "=$relaytrace$%.*s", pexp->strName.len, pexp->strName.buf);
+    }
+}
+
+void
+OutputLine_def_GCC(FILE *fileDest, EXPORT *pexp)
+{
+    int bTracing = 0;
+    /* Print the function name, with decoration for export libs */
+    PrintName(fileDest, pexp, &pexp->strName, gbImportLib);
+    DbgPrint("Generating def line for '%.*s'\n", pexp->strName.len, pexp->strName.buf);
+
+    /* Check if this is a forwarded export */
+    if (pexp->strTarget.buf)
+    {
+        int fIsExternal = !!ScanToken(pexp->strTarget.buf, '.');
+        DbgPrint("Got redirect '%.*s'\n", pexp->strTarget.len, pexp->strTarget.buf);
+
+        /* print the target name, don't decorate if it is external */
         fprintf(fileDest, "=");
-        fprintf(fileDest, "stub_function%d", pexp->nNumber);
+        PrintName(fileDest, pexp, &pexp->strTarget, !fIsExternal);
+    }
+    else if (((pexp->uFlags & FL_STUB) || (pexp->nCallingConvention == CC_STUB)) &&
+             (pexp->strName.buf[0] == '?'))
+    {
+        /* C++ stubs are forwarded to C stubs */
+        fprintf(fileDest, "=stub_function%d", pexp->nNumber);
     }
-    else if ((giArch == ARCH_X86) && gbKillAt && !gbMSComp &&
-             (pexp->nCallingConvention == CC_STDCALL ||
-              pexp->nCallingConvention == CC_FASTCALL))
+    else if (gbTracing && ((pexp->uFlags & FL_NORELAY) == 0) && (pexp->nCallingConvention == CC_STDCALL) &&
+            (pexp->strName.buf[0] != '?'))
     {
+        /* Redirect it to the relay-tracing trampoline */
+        char buf[256];
+        STRING strTarget;
         fprintf(fileDest, "=");
-        PrintName(fileDest, pexp, "", 0, 1);
+        sprintf(buf, "$relaytrace$%.*s", pexp->strName.len, pexp->strName.buf);
+        strTarget.buf = buf;
+        strTarget.len = pexp->strName.len + 12;
+        PrintName(fileDest, pexp, &strTarget, 1);
+        bTracing = 1;
+    }
+
+    /* Special handling for stdcall and fastcall */
+    if ((giArch == ARCH_X86) &&
+        ((pexp->nCallingConvention == CC_STDCALL) ||
+         (pexp->nCallingConvention == CC_FASTCALL)))
+    {
+        /* Is this the import lib? */
+        if (gbImportLib)
+        {
+            /* Is the name in the spec file decorated? */
+            const char* pcDeco = ScanToken(pexp->strName.buf, '@');
+            if (pcDeco && (pcDeco < pexp->strName.buf + pexp->strName.len))
+            {
+                /* Write the name including the leading @  */
+                fprintf(fileDest, "==%.*s", pexp->strName.len, pexp->strName.buf);
+            }
+        }
+        else if ((!pexp->strTarget.buf) && !(bTracing))
+        {
+            /* Write a forwarder to the actual decorated symbol */
+            fprintf(fileDest, "=");
+            PrintName(fileDest, pexp, &pexp->strName, 1);
+        }
     }
+}
+
+int
+OutputLine_def(FILE *fileDest, EXPORT *pexp)
+{
+    DbgPrint("OutputLine_def: '%.*s'...\n", pexp->strName.len, pexp->strName.buf);
+    fprintf(fileDest, " ");
 
-    if (pexp->nOrdinal != -1)
+    if (gbMSComp)
+        OutputLine_def_MS(fileDest, pexp);
+    else
+        OutputLine_def_GCC(fileDest, pexp);
+
+    if (pexp->uFlags & FL_ORDINAL)
     {
         fprintf(fileDest, " @%d", pexp->nOrdinal);
     }
 
-    if (pexp->nCallingConvention == CC_EXTERN)
+    if (pexp->uFlags & FL_NONAME)
     {
-        fprintf(fileDest, " DATA");
+        fprintf(fileDest, " NONAME");
     }
 
     if (pexp->uFlags & FL_PRIVATE)
@@ -387,9 +669,9 @@ OutputLine_def(FILE *fileDest, EXPORT *pexp)
         fprintf(fileDest, " PRIVATE");
     }
 
-    if (pexp->uFlags & FL_NONAME)
+    if (pexp->nCallingConvention == CC_EXTERN)
     {
-        fprintf(fileDest, " NONAME");
+        fprintf(fileDest, " DATA");
     }
 
     fprintf(fileDest, "\n");
@@ -404,6 +686,7 @@ ParseFile(char* pcStart, FILE *fileDest, PFNOUTLINE OutputLine)
     int nLine;
     EXPORT exp;
     int included;
+    char namebuffer[16];
 
     //fprintf(stderr, "info: line %d, pcStart:'%.30s'\n", nLine, pcStart);
 
@@ -418,6 +701,10 @@ ParseFile(char* pcStart, FILE *fileDest, PFNOUTLINE OutputLine)
         exp.uFlags = 0;
         exp.nNumber++;
 
+
+        //if (!strncmp(pcLine, "22 stdcall @(long) MPR_Alloc",28))
+        //    gbDebug = 1;
+
         //fprintf(stderr, "info: line %d, token:'%d, %.20s'\n",
         //        nLine, TokenLength(pcLine), pcLine);
 
@@ -432,8 +719,15 @@ ParseFile(char* pcStart, FILE *fileDest, PFNOUTLINE OutputLine)
         //        nLine, TokenLength(pc), pc);
 
         /* Now we should get either an ordinal or @ */
-        if (*pc == '@') exp.nOrdinal = -1;
-        else exp.nOrdinal = atol(pc);
+        if (*pc == '@')
+            exp.nOrdinal = -1;
+        else
+        {
+            exp.nOrdinal = atol(pc);
+            /* The import lib should contain the ordinal only if -ordinal was specified */
+            if (!gbImportLib)
+                exp.uFlags |= FL_ORDINAL;
+        }
 
         /* Go to next token (type) */
         if (!(pc = NextToken(pc)))
@@ -442,7 +736,7 @@ ParseFile(char* pcStart, FILE *fileDest, PFNOUTLINE OutputLine)
             return -10;
         }
 
-        //fprintf(stderr, "info: Token:'%.10s'\n", pc);
+        //fprintf(stderr, "info: Token:'%.*s'\n", TokenLength(pc), pc);
 
         /* Now we should get the type */
         if (CompareToken(pc, "stdcall"))
@@ -472,7 +766,7 @@ ParseFile(char* pcStart, FILE *fileDest, PFNOUTLINE OutputLine)
         }
         else
         {
-            fprintf(stderr, "error: line %d, expected type, got '%.*s' %d\n",
+            fprintf(stderr, "error: line %d, expected callconv, got '%.*s' %d\n",
                     nLine, TokenLength(pc), pc, *pc);
             return -11;
         }
@@ -518,20 +812,33 @@ ParseFile(char* pcStart, FILE *fileDest, PFNOUTLINE OutputLine)
             {
                 exp.uFlags |= FL_PRIVATE;
             }
-            else if (CompareToken(pc, "-noname") ||
-                     CompareToken(pc, "-ordinal"))
+            else if (CompareToken(pc, "-noname"))
             {
-                exp.uFlags |= FL_NONAME;
+                exp.uFlags |= FL_ORDINAL | FL_NONAME;
+            }
+            else if (CompareToken(pc, "-ordinal"))
+            {
+                exp.uFlags |= FL_ORDINAL;
+                /* GCC doesn't automatically import by ordinal if an ordinal
+                 * is found in the def file. Force it. */
+                if (gbImportLib && !gbMSComp)
+                    exp.uFlags |= FL_NONAME;
             }
             else if (CompareToken(pc, "-stub"))
             {
                 exp.uFlags |= FL_STUB;
             }
-            else if (CompareToken(pc, "-norelay") ||
-                     CompareToken(pc, "-register") ||
-                     CompareToken(pc, "-ret64"))
+            else if (CompareToken(pc, "-norelay"))
+            {
+                exp.uFlags |= FL_NORELAY;
+            }
+            else if (CompareToken(pc, "-ret64"))
+            {
+                exp.uFlags |= FL_RET64;
+            }
+            else if (CompareToken(pc, "-register"))
             {
-                /* silently ignore these */
+                exp.uFlags |= FL_REGISTER;
             }
             else
             {
@@ -549,15 +856,24 @@ ParseFile(char* pcStart, FILE *fileDest, PFNOUTLINE OutputLine)
         if (!included) continue;
 
         /* Get name */
-        exp.pcName = pc;
-        exp.nNameLength = TokenLength(pc);
+        exp.strName.buf = pc;
+        exp.strName.len = TokenLength(pc);
+        DbgPrint("Got name: '%.*s'\n", exp.strName.len, exp.strName.buf);
+
+        /* Check for autoname */
+        if ((exp.strName.len == 1) && (exp.strName.buf[0] == '@'))
+        {
+            sprintf(namebuffer, "ordinal%d", exp.nOrdinal);
+            exp.strName.len = strlen(namebuffer);
+            exp.strName.buf = namebuffer;
+            exp.uFlags |= FL_ORDINAL | FL_NONAME;
+        }
 
         /* Handle parameters */
         exp.nStackBytes = 0;
         if (exp.nCallingConvention != CC_EXTERN &&
             exp.nCallingConvention != CC_STUB)
         {
-            //fprintf(stderr, "info: options:'%.10s'\n", pc);
             /* Go to next token */
             if (!(pc = NextToken(pc)))
             {
@@ -588,18 +904,31 @@ ParseFile(char* pcStart, FILE *fileDest, PFNOUTLINE OutputLine)
                     exp.nStackBytes += 8;
                     exp.anArgs[exp.nArgCount] = ARG_DBL;
                 }
-                else if (CompareToken(pc, "ptr") ||
-                         CompareToken(pc, "str") ||
-                         CompareToken(pc, "wstr"))
+                else if (CompareToken(pc, "ptr"))
+                {
+                    exp.nStackBytes += 4; // sizeof(void*) on x86
+                    exp.anArgs[exp.nArgCount] = ARG_PTR;
+                }
+                else if (CompareToken(pc, "str"))
+                {
+                    exp.nStackBytes += 4; // sizeof(void*) on x86
+                    exp.anArgs[exp.nArgCount] = ARG_STR;
+                }
+                else if (CompareToken(pc, "wstr"))
                 {
                     exp.nStackBytes += 4; // sizeof(void*) on x86
-                    exp.anArgs[exp.nArgCount] = ARG_PTR; // FIXME: handle strings
+                    exp.anArgs[exp.nArgCount] = ARG_WSTR;
                 }
                 else if (CompareToken(pc, "int64"))
                 {
                     exp.nStackBytes += 8;
                     exp.anArgs[exp.nArgCount] = ARG_INT64;
                 }
+                else if (CompareToken(pc, "int128"))
+                {
+                    exp.nStackBytes += 16;
+                    exp.anArgs[exp.nArgCount] = ARG_INT128;
+                }
                 else if (CompareToken(pc, "float"))
                 {
                     exp.nStackBytes += 4;
@@ -638,12 +967,14 @@ ParseFile(char* pcStart, FILE *fileDest, PFNOUTLINE OutputLine)
             else
             {
                 /* Check for stdcall name */
-                char *p = strchr(pc, '@');
-                if (p && ((size_t)(p - pc) < exp.nNameLength))
+                const char *p = ScanToken(pc, '@');
+                if (p && (p - pc < exp.strName.len))
                 {
                     int i;
-                    exp.nNameLength = p - pc;
-                    if (exp.nNameLength < 1)
+
+                    /* Truncate the name to before the @ */
+                    exp.strName.len = (int)(p - pc);
+                    if (exp.strName.len < 1)
                     {
                         fprintf(stderr, "error, @ in line %d\n", nLine);
                         return -1;
@@ -659,10 +990,11 @@ ParseFile(char* pcStart, FILE *fileDest, PFNOUTLINE OutputLine)
         }
 
         /* Get optional redirection */
-        if ((pc = NextToken(pc)))
+        pc = NextToken(pc);
+        if (pc)
         {
-            exp.pcRedirection = pc;
-            exp.nRedirectionLength = TokenLength(pc);
+            exp.strTarget.buf = pc;
+            exp.strTarget.len = TokenLength(pc);
 
             /* Check syntax (end of line) */
             if (NextToken(pc))
@@ -670,14 +1002,25 @@ ParseFile(char* pcStart, FILE *fileDest, PFNOUTLINE OutputLine)
                  fprintf(stderr, "error: line %d, additional tokens after ')'\n", nLine);
                  return -17;
             }
+
+            /* Don't relay-trace forwarded functions */
+            exp.uFlags |= FL_NORELAY;
         }
         else
         {
-            exp.pcRedirection = 0;
-            exp.nRedirectionLength = 0;
+            exp.strTarget.buf = 0;
+            exp.strTarget.len = 0;
+        }
+
+        /* Check for no-name without ordinal */
+        if ((exp.uFlags & FL_ORDINAL) && (exp.nOrdinal == -1))
+        {
+            fprintf(stderr, "error: line %d, ordinal export without ordinal!\n", nLine);
+            return -1;
         }
 
         OutputLine(fileDest, &exp);
+        gbDebug = 0;
     }
 
     return 0;
@@ -686,17 +1029,17 @@ ParseFile(char* pcStart, FILE *fileDest, PFNOUTLINE OutputLine)
 
 void usage(void)
 {
-    printf("syntax: spec2pdef [<options> ...] <spec file>\n"
+    printf("syntax: spec2def [<options> ...] <spec file>\n"
            "Possible options:\n"
-           "  -h --help   prints this screen\n"
-           "  -l=<file>   generates an asm lib stub\n"
-           "  -d=<file>   generates a def file\n"
-           "  -s=<file>   generates a stub file\n"
-           "  --ms        msvc compatibility\n"
-           "  -n=<name>   name of the dll\n"
-           "  --kill-at   removes @xx decorations from exports\n"
-           "  -r          removes redirections from def file\n"
-           "  -a=<arch>   Set architecture to <arch>. (i386, x86_64, arm)\n");
+           "  -h --help       prints this screen\n"
+           "  -l=<file>       generates an asm lib stub\n"
+           "  -d=<file>       generates a def file\n"
+           "  -s=<file>       generates a stub file\n"
+           "  --ms            msvc compatibility\n"
+           "  -n=<name>       name of the dll\n"
+           "  --implib        generate a def file for an import library\n"
+           "  -a=<arch>       Set architecture to <arch>. (i386, x86_64, arm)\n"
+           "  --with-tracing generates wine-like \"+relay\" trace trampolines. (necessitates -s)\n");
 }
 
 int main(int argc, char *argv[])
@@ -705,7 +1048,7 @@ int main(int argc, char *argv[])
     char *pszSource, *pszDefFileName = 0, *pszStubFileName = 0, *pszLibStubName = 0;
     char achDllName[40];
     FILE *file;
-    int result, i;
+    int result = 0, i;
 
     if (argc < 2)
     {
@@ -740,20 +1083,20 @@ int main(int argc, char *argv[])
         }
         else if ((strcasecmp(argv[i], "--implib") == 0))
         {
-            no_redirections = 1;
             gbImportLib = 1;
         }
-        else if ((strcasecmp(argv[i], "--kill-at") == 0))
-        {
-            gbKillAt = 1;
-        }
         else if ((strcasecmp(argv[i], "--ms") == 0))
         {
             gbMSComp = 1;
         }
-        else if ((strcasecmp(argv[i], "-r") == 0))
+        else if ((strcasecmp(argv[i], "--with-tracing") == 0))
         {
-            no_redirections = 1;
+            if (!pszStubFileName)
+            {
+                fprintf(stderr, "Error: cannot use --with-tracing without -s option.\n");
+                return -1;
+            }
+            gbTracing = 1;
         }
         else if (argv[i][1] == 'a' && argv[i][2] == '=')
         {
@@ -877,7 +1220,7 @@ int main(int argc, char *argv[])
 
         OutputHeader_asmstub(file, pszDllName);
         result = ParseFile(pszSource, file, OutputLine_asmstub);
-        fprintf(file, "\nEND\n");
+        fprintf(file, "\n    END\n");
         fclose(file);
     }