compiler: Added (u)intsize to map to size_t; Updated C prototypes to use it; Fixed...
[sdk] / compiler / ecc / ecc.ec
index d473cd6..ecbbada 100644 (file)
@@ -6,7 +6,8 @@ import "ecere"
 import "ec"
 #endif
 
-#include <stdarg.h>
+
+//#include <stdarg.h>
 
 static Context globalContext { };
 static Module privateModule;
@@ -126,6 +127,7 @@ class CompilerApp : Application
       int c;
       bool valid = true;
       char defaultOutputFile[MAX_LOCATION];
+      bool buildingBootStrap = false;
 
       Platform targetPlatform = GetRuntimePlatform();
       int targetBits = GetHostBits();
@@ -164,17 +166,12 @@ class CompilerApp : Application
          {
             if(!strcmp(arg + 1, "m32") || !strcmp(arg + 1, "m64"))
             {
-#if defined(__WIN32__)
-               if(strcmp(arg + 1, "m64"))    // Until we set up MinGW-w64
-#endif
-               {
-                  int argLen = strlen(arg);
-                  int newLen = cppOptionsLen + 1 + argLen;
-                  cppOptions = renew cppOptions char[newLen + 1];
-                  cppOptions[cppOptionsLen] = ' ';
-                  strcpy(cppOptions + cppOptionsLen + 1, arg);
-                  cppOptionsLen = newLen;
-               }
+               int argLen = strlen(arg);
+               int newLen = cppOptionsLen + 1 + argLen;
+               cppOptions = renew cppOptions char[newLen + 1];
+               cppOptions[cppOptionsLen] = ' ';
+               strcpy(cppOptions + cppOptionsLen + 1, arg);
+               cppOptionsLen = newLen;
                targetBits = !strcmp(arg + 1, "m32") ? 32 : 64;
             }
             else if(arg[1] == 'D')
@@ -189,6 +186,8 @@ class CompilerApp : Application
                   SetBuildingEcereCom(true);
                else if(!strcmp(arg, "-DECERE_COM_MODULE"))
                   SetBuildingEcereComModule(true);
+               else if(!strcmp(arg, "-DECERE_BOOTSTRAP"))
+                  buildingBootStrap = true;
             }
             else if(arg[1] == 'I')
             {
@@ -330,7 +329,7 @@ class CompilerApp : Application
          SetTargetBits(targetBits);
          SetEchoOn(false);
 
-         privateModule = (Module)__ecere_COM_Initialize(true | ((targetBits == 64)?2:0), 1, null);
+         privateModule = (Module)__ecere_COM_Initialize(true | (targetBits == sizeof(uintptr)*8 ? 0 : targetBits == 64 ? 2 : targetBits==32 ? 4 : 0) | 8, 1, null);
          SetPrivateModule(privateModule);
 
          globalContext.types.Add((BTNode)Symbol { string = CopyString("uint"), type = ProcessTypeString("unsigned int", false) });
@@ -338,6 +337,14 @@ class CompilerApp : Application
          globalContext.types.Add((BTNode)Symbol { string = CopyString("uint32"), type = ProcessTypeString("unsigned int", false) });
          globalContext.types.Add((BTNode)Symbol { string = CopyString("uint16"), type = ProcessTypeString("unsigned short", false) });
          globalContext.types.Add((BTNode)Symbol { string = CopyString("byte"), type = ProcessTypeString("unsigned char", false) });
+         if(buildingBootStrap)
+         {
+            // Do not define this when we pre-include stdint.h or the eC compiler will be confused when parsing these types (External prioritization in pass15.ec will fail)
+            globalContext.types.Add((BTNode)Symbol { string = CopyString("intptr_t"), type = ProcessTypeString("intptr", false) });
+            globalContext.types.Add((BTNode)Symbol { string = CopyString("uintptr_t"), type = ProcessTypeString("uintptr", false) });
+            globalContext.types.Add((BTNode)Symbol { string = CopyString("ssize_t"), type = ProcessTypeString("intsize", false) });
+            globalContext.types.Add((BTNode)Symbol { string = CopyString("size_t"), type = ProcessTypeString("uintsize", false) });
+         }
 
          {
             GlobalData data { fullName = CopyString("__thisModule"), dataTypeString = CopyString("Module"), module = privateModule };
@@ -345,7 +352,7 @@ class CompilerApp : Application
             globalData.functions.Add((BTNode)data);
          }
 
-         snprintf(command, sizeof(command), "%s%s -x c -E \"%s\"", cppCommand, cppOptions ? cppOptions : "", GetSourceFile());
+         snprintf(command, sizeof(command), "%s%s -x c -E %s\"%s\"", cppCommand, cppOptions ? cppOptions : "", buildingBootStrap ? "" : "-include stdint.h -include sys/types.h ", GetSourceFile());
          command[sizeof(command)-1] = 0;
          if((cppOutput = DualPipeOpen({ output = true }, command)))
          {
@@ -494,27 +501,12 @@ class CompilerApp : Application
                   File output = FileOpen(GetOutputFile(), write);
                   if(output)
                   {
-                     //output.Printf("#include <ecereCOM.h>\n\n");
-
-                     // Temporary patch, fix with defines or something...
-                     /*
-                     if(!strstr(GetSourceFile(), "instance.ec"))
-                     {
-                        output.Printf("#if defined(__GNUC__) && defined(__WIN32__)\n");
-                        output.Printf("#include <x87inline.h>\n");
-                        output.Printf("#endif\n");
-                     }
-                     */
-                     /*output.Printf("#if defined(__GNUC__) \n");
-                        output.Printf("typedef long long int64;\n");
-                        output.Printf("typedef unsigned long long uint64;\n");
-                     output.Printf("#else\n");
-                        output.Printf("typedef __int64 int64;\n");
-                        output.Printf("typedef unsigned __int64 uint64;\n");
-                     output.Printf("#endif\n");*/
                      output.Printf("#if defined(__GNUC__)\n");
                         output.Printf("typedef long long int64;\n");
                         output.Printf("typedef unsigned long long uint64;\n");
+                        output.Printf("#ifndef _WIN32\n");
+                           output.Printf("#define __declspec(x)\n");
+                        output.Printf("#endif\n");
                      output.Printf("#elif defined(__TINYC__)\n");
                         output.Printf("#include <stdarg.h>\n");
                         output.Printf("#define __builtin_va_list va_list\n");
@@ -524,6 +516,8 @@ class CompilerApp : Application
                            output.Printf("#define strcasecmp stricmp\n");
                            output.Printf("#define strncasecmp strnicmp\n");
                            output.Printf("#define __declspec(x) __attribute__((x))\n");
+                        output.Printf("#else\n");
+                           output.Printf("#define __declspec(x)\n");
                         output.Printf("#endif\n");
                         output.Printf("typedef long long int64;\n");
                         output.Printf("typedef unsigned long long uint64;\n");
@@ -531,20 +525,29 @@ class CompilerApp : Application
                         output.Printf("typedef __int64 int64;\n");
                         output.Printf("typedef unsigned __int64 uint64;\n");
                      output.Printf("#endif\n");
-                     /*output.Printf("#if defined(__GNUC__) || defined(__TINYC__)\n");
-                        output.Printf("typedef long long int64;\n");
-                        output.Printf("typedef unsigned long long uint64;\n");
-                     output.Printf("#elif defined(__TINYC__)\n");
-                     output.Printf("#else\n");
-                        output.Printf("typedef __int64 int64;\n");
-                        output.Printf("typedef unsigned __int64 uint64;\n");
-                     output.Printf("#endif\n");*/
                      output.Printf("#ifdef __BIG_ENDIAN__\n");
                         output.Printf("#define __ENDIAN_PAD(x) (8 - (x))\n");
                      output.Printf("#else\n");
                         output.Printf("#define __ENDIAN_PAD(x) 0\n");
                      output.Printf("#endif\n");
-                     output.Printf("#include <stdint.h>\n");
+                     if(buildingBootStrap)
+                     {
+                        //output.Printf("#ifdef __MINGW32__\n");
+                        //output.Printf("#ifdef _WIN64\n");
+                        /*
+                        output.Printf("#if defined(_WIN64) || WORDSIZE == 64\n");
+                        output.Printf("typedef unsigned long long int uintptr_t;\n");
+                        output.Printf("typedef long long int intptr_t;\n");
+                        output.Printf("#else\n");
+                        output.Printf("typedef unsigned int uintptr_t;\n");
+                        output.Printf("typedef int intptr_t;\n");
+                        output.Printf("#endif\n");
+                        */
+                        //output.Printf("#else\n");
+                        output.Printf("#include <stdint.h>\n");
+                        output.Printf("#include <sys/types.h>\n");
+                        //output.Printf("#endif\n");
+                     }
 
                      // NOTE: If anything is changed up there, the start outputLine must be updated in libec's output.c or Debugging lines will be wrong