diff --git a/src/backend/cdef.h b/src/backend/cdef.h
index 2dc4657..ed292db 100644
--- a/src/backend/cdef.h
+++ b/src/backend/cdef.h
@@ -198,7 +198,7 @@ One and only one of these macros must be set by the makefile:
 #define TARGET_WINDOS   (!(TARGET_LINUX || TARGET_OSX || TARGET_FREEBSD || TARGET_OPENBSD || TARGET_SOLARIS))
 #endif
 
-#if __GNUC__
+#if __GNUC__ && !__MINGW32__
 #include "cdeflnx.h"
 #endif
 
diff --git a/src/backend/cgcod.c b/src/backend/cgcod.c
index 726bf87..52d872a 100644
--- a/src/backend/cgcod.c
+++ b/src/backend/cgcod.c
@@ -15,6 +15,9 @@
 #include        <string.h>
 #include        <stdlib.h>
 #include        <time.h>
+#if __MINGW32__
+#include <malloc.h>
+#endif
 
 #if __sun || _MSC_VER
 #include        <alloca.h>
diff --git a/src/backend/cgcv.h b/src/backend/cgcv.h
index eafa77f..7a86468 100644
--- a/src/backend/cgcv.h
+++ b/src/backend/cgcv.h
@@ -15,6 +15,14 @@
 #define CGCV_H
 //#pragma once
 
+#if !defined(GCC_PACKED)
+#if __MINGW32__
+#define GCC_PACKED __attribute__((packed,ms_struct))
+#else
+#define GCC_PACKED
+#endif
+#endif
+
 extern char *ftdbname;
 
 void cv_init ( void );
@@ -33,7 +41,7 @@ typedef unsigned idx_t;        // type of type index
 
 #pragma pack(1)
 
-typedef struct DEBTYP_T
+typedef struct GCC_PACKED DEBTYP_T
 {
     unsigned prev;              // previous debtyp_t with same hash
     unsigned short length;      // length of following array
diff --git a/src/backend/cgen.c b/src/backend/cgen.c
index ac2e139..2696a41 100644
--- a/src/backend/cgen.c
+++ b/src/backend/cgen.c
@@ -458,7 +458,7 @@ bool reghasvalue(regm_t regm,targ_size_t value,unsigned *preg)
 code *regwithvalue(code *c,regm_t regm,targ_size_t value,unsigned *preg,regm_t flags)
 {   unsigned reg;
 
-    //printf("regwithvalue(value = %lld)\n", (long long)value);
+    //printf("regwithvalue(value = "FMTS_I64")\n", (long long)value);
     if (!preg)
         preg = &reg;
 
diff --git a/src/backend/cgobj.c b/src/backend/cgobj.c
index 5e27358..1a8c06e 100644
--- a/src/backend/cgobj.c
+++ b/src/backend/cgobj.c
@@ -379,7 +379,7 @@ void too_many_symbols()
 #endif
 }
 
-#if !DEBUG && TX86 && __INTSIZE == 4 && !defined(_MSC_VER)
+#if !DEBUG && TX86 && __INTSIZE == 4 && !defined(_MSC_VER) && !__MINGW32__
 __declspec(naked) int __pascal insidx(char *p,unsigned index)
 {
 #undef AL
@@ -3654,7 +3654,7 @@ void Obj::far16thunk(Symbol *s)
         idx,idx);
     obj.CODE16offset += 4;
 
-    Obj::bytes(obj.code16segi,obj.CODE16offset,3,"\x66\x67\xEA");    // JMPF L2
+    Obj::bytes(obj.code16segi,obj.CODE16offset,3,(void*)"\x66\x67\xEA");    // JMPF L2
     obj.CODE16offset += 3;
 
     Obj::ledata(obj.code16segi,obj.CODE16offset,L2offset,
diff --git a/src/backend/cod1.c b/src/backend/cod1.c
index b757387..19bc6b6 100644
--- a/src/backend/cod1.c
+++ b/src/backend/cod1.c
@@ -15,6 +15,9 @@
 #include        <string.h>
 #include        <stdlib.h>
 #include        <time.h>
+#if __MINGW32__
+#include <malloc.h>
+#endif
 
 #if __sun || _MSC_VER
 #include        <alloca.h>
@@ -548,7 +551,7 @@ code *loadea(elem *e,code *cs,unsigned op,unsigned reg,targ_size_t offset,
 
 #ifdef DEBUG
   if (debugw)
-    printf("loadea: e=%p cs=%p op=x%x reg=%d offset=%lld keepmsk=%s desmsk=%s\n",
+    printf("loadea: e=%p cs=%p op=x%x reg=%d offset="FMTS_I64" keepmsk=%s desmsk=%s\n",
             e,cs,op,reg,(unsigned long long)offset,regm_str(keepmsk),regm_str(desmsk));
 #endif
 
diff --git a/src/backend/cod3.c b/src/backend/cod3.c
index a7bc151..76eec5a 100644
--- a/src/backend/cod3.c
+++ b/src/backend/cod3.c
@@ -1332,7 +1332,7 @@ void doswitch(block *b)
         qsort(casevals, ncases, sizeof(CaseVal), &CaseVal::cmp);
 
         //for (unsigned n = 0; n < ncases; n++)
-            //printf("casevals[%lld] = x%x\n", n, casevals[n].val);
+            //printf("casevals["FMTS_I64"] = x%x\n", n, casevals[n].val);
 
         // Generate binary tree of comparisons
         c = cat(c, ifthen(casevals, ncases, sz, reg, reg2, sreg, bdefault, bdefault != b->Bnext));
@@ -2522,7 +2522,7 @@ code *movregconst(code *c,unsigned reg,targ_size_t value,regm_t flags)
 {   unsigned r;
     regm_t mreg;
 
-    //printf("movregconst(reg=%s, value= %lld (%llx), flags=%x)\n", regm_str(mask[reg]), value, value, flags);
+    //printf("movregconst(reg=%s, value= "FMTS_I64" (%llx), flags=%x)\n", regm_str(mask[reg]), value, value, flags);
 #define genclrreg(a,r) genregs(a,0x31,r,r)
 
     regm_t regm = regcon.immed.mval & mask[reg];
@@ -5804,7 +5804,7 @@ static char *pgen;
 
 #define GEN(c)          (*pgen++ = (c))
 #define GENP(n,p)       (memcpy(pgen,(p),(n)), pgen += (n))
-#if ELFOBJ || MACHOBJ || _MSC_VER
+#if ELFOBJ || MACHOBJ || _MSC_VER || __MINGW32__
 #define FLUSH()         if (pgen-bytes) cod3_flush()
 #else
 #define FLUSH()         ((pgen - bytes) && cod3_flush())
@@ -6591,7 +6591,7 @@ STATIC void do8bit(enum FL fl,union evc *uev)
             if (uev->Vblock->Bsrcpos.Slinnum)
                 fprintf(stderr, "%s(%d): ", uev->Vblock->Bsrcpos.Sfilename, uev->Vblock->Bsrcpos.Slinnum);
 #endif
-            fprintf(stderr, "block displacement of %lld exceeds the maximum offset of -128 to 127.\n", (long long)delta);
+            fprintf(stderr, "block displacement of "FMTS_I64" exceeds the maximum offset of -128 to 127.\n", (long long)delta);
             err_exit();
         }
         c = delta;
diff --git a/src/backend/code_x86.h b/src/backend/code_x86.h
index 42b4370..8c15378 100644
--- a/src/backend/code_x86.h
+++ b/src/backend/code_x86.h
@@ -8,6 +8,13 @@
  * only. The license is in backendlicense.txt
  * For any other uses, please contact Digital Mars.
  */
+#if !defined(GCC_PACKED)
+#if __MINGW32__
+#define GCC_PACKED __attribute__((packed,ms_struct))
+#else
+#define GCC_PACKED
+#endif
+#endif
 
 /* Register definitions */
 
@@ -405,7 +412,7 @@ struct code
 
     union {
         unsigned _Iop;
-        struct {
+        struct GCC_PACKED {
 #pragma pack(1)
             unsigned char  op;
             unsigned short   pp : 2;
diff --git a/src/backend/debug.c b/src/backend/debug.c
index a319f5a..df0f7c7 100644
--- a/src/backend/debug.c
+++ b/src/backend/debug.c
@@ -356,7 +356,7 @@ printf("%p %d ", b, b->BC);
                 dbg_printf("\tdefault: %p\n",list_block(bl));
                 while (ncases--)
                 {   bl = list_next(bl);
-                    dbg_printf("\tcase %lld: %p\n",*++pu,list_block(bl));
+                    dbg_printf("\tcase "FMTS_I64": %p\n",*++pu,list_block(bl));
                 }
                 break;
             case BCiftrue:
diff --git a/src/backend/dwarf.c b/src/backend/dwarf.c
index 3e071b1..53c5dfb 100644
--- a/src/backend/dwarf.c
+++ b/src/backend/dwarf.c
@@ -57,6 +57,14 @@
 #include        "dwarf.h"
 #include        "dwarf2.h"
 
+#if !defined(GCC_PACKED)
+#if __MINGW32__
+#define GCC_PACKED __attribute__((packed,ms_struct))
+#else
+#define GCC_PACKED
+#endif
+#endif
+
 extern int seg_count;
 
 static char __file__[] = __FILE__;      // for tassert.h
@@ -405,7 +413,7 @@ void TypeInfo_Abuf::swap(void *p1, void *p2)
 }
 
 #pragma pack(1)
-struct DebugInfoHeader
+struct GCC_PACKED DebugInfoHeader
 {   unsigned total_length;
     unsigned short version;
     unsigned abbrev_offset;
@@ -428,7 +436,7 @@ static Outbuffer *linebuf;
 static size_t linebuf_filetab_end;
 
 #pragma pack(1)
-struct DebugLineHeader
+struct GCC_PACKED DebugLineHeader
 {   unsigned total_length;
     unsigned short version;
     unsigned prologue_length;
@@ -482,7 +490,7 @@ const char* debug_aranges = ".debug_aranges";
 void dwarf_initfile(const char *filename)
 {
     #pragma pack(1)
-    struct DebugFrameHeader
+    struct GCC_PACKED DebugFrameHeader
     {
         unsigned length;
         unsigned CIE_id;
@@ -1012,7 +1020,7 @@ void dwarf_func_term(Symbol *sfunc)
     if (I64)
     {
         #pragma pack(1)
-        struct DebugFrameFDE
+        struct GCC_PACKED DebugFrameFDE
         {
             unsigned length;
             unsigned CIE_pointer;
@@ -1054,7 +1062,7 @@ void dwarf_func_term(Symbol *sfunc)
     else
     {
         #pragma pack(1)
-        struct DebugFrameFDE
+        struct GCC_PACKED DebugFrameFDE
         {
             unsigned length;
             unsigned CIE_pointer;
diff --git a/src/backend/el.c b/src/backend/el.c
index de5ff31..d22b325 100644
--- a/src/backend/el.c
+++ b/src/backend/el.c
@@ -3104,18 +3104,18 @@ void elem_print(elem *e)
         switch (e->Eoper)
         {
             case OPrelconst:
-                dbg_printf(" %lld+&",(unsigned long long)e->Eoffset);
+                dbg_printf(" "FMTS_I64"+&",(unsigned long long)e->Eoffset);
                 dbg_printf(" %s",e->EV.sp.Vsym->Sident);
                 break;
             case OPvar:
                 if (e->Eoffset)
-                    dbg_printf(" %lld+",(unsigned long long)e->Eoffset);
+                    dbg_printf(" "FMTS_I64"+",(unsigned long long)e->Eoffset);
                 dbg_printf(" %s",e->EV.sp.Vsym->Sident);
                 break;
             case OPasm:
             case OPstring:
             case OPhstring:
-                dbg_printf(" '%s',%lld\n",e->EV.ss.Vstring,(unsigned long long)e->EV.ss.Voffset);
+                dbg_printf(" '%s',"FMTS_I64"\n",e->EV.ss.Vstring,(unsigned long long)e->EV.ss.Voffset);
                 break;
             case OPconst:
                 elem_print_const(e);
@@ -3190,16 +3190,16 @@ case_tym:
 
         case TYllong:
         L2:
-            dbg_printf("%lldLL ",e->EV.Vllong);
+            dbg_printf(""FMTS_I64"LL ",e->EV.Vllong);
             break;
 
         case TYullong:
-            dbg_printf("%lluLL ",e->EV.Vullong);
+            dbg_printf(""FMTS_U64"LL ",e->EV.Vullong);
             break;
 
         case TYcent:
         case TYucent:
-            dbg_printf("%lluLL+%lluLL ", e->EV.Vcent.msw, e->EV.Vcent.lsw);
+            dbg_printf(""FMTS_U64"LL+"FMTS_U64"LL ", e->EV.Vcent.msw, e->EV.Vcent.lsw);
             break;
 
         case TYfloat:
diff --git a/src/backend/global.h b/src/backend/global.h
index 02b0abb..7246948 100644
--- a/src/backend/global.h
+++ b/src/backend/global.h
@@ -133,7 +133,7 @@ void util_set32(void);
 void util_set64(void);
 int ispow2(targ_ullong);
 
-#if __GNUC__
+#if __GNUC__ && !__MINGW32__
 #define util_malloc(n,size) mem_malloc((n)*(size))
 #define util_calloc(n,size) mem_calloc((n)*(size))
 #define util_free       mem_free
diff --git a/src/backend/glocal.c b/src/backend/glocal.c
index d476c6f..0cffda6 100644
--- a/src/backend/glocal.c
+++ b/src/backend/glocal.c
@@ -18,6 +18,9 @@
 #if __sun || _MSC_VER
 #include        <alloca.h>
 #endif
+#if __MINGW32__
+#include <malloc.h>
+#endif
 
 #include        "cc.h"
 #include        "global.h"
diff --git a/src/backend/md5.c b/src/backend/md5.c
index 2034986..b12fca8 100644
--- a/src/backend/md5.c
+++ b/src/backend/md5.c
@@ -173,88 +173,88 @@ static void Transform (UINT4 *buf, UINT4 *in)
 #define S12 12
 #define S13 17
 #define S14 22
-  FF ( a, b, c, d, in[ 0], S11, 3614090360); /* 1 */
-  FF ( d, a, b, c, in[ 1], S12, 3905402710); /* 2 */
-  FF ( c, d, a, b, in[ 2], S13,  606105819); /* 3 */
-  FF ( b, c, d, a, in[ 3], S14, 3250441966); /* 4 */
-  FF ( a, b, c, d, in[ 4], S11, 4118548399); /* 5 */
-  FF ( d, a, b, c, in[ 5], S12, 1200080426); /* 6 */
-  FF ( c, d, a, b, in[ 6], S13, 2821735955); /* 7 */
-  FF ( b, c, d, a, in[ 7], S14, 4249261313); /* 8 */
-  FF ( a, b, c, d, in[ 8], S11, 1770035416); /* 9 */
-  FF ( d, a, b, c, in[ 9], S12, 2336552879); /* 10 */
-  FF ( c, d, a, b, in[10], S13, 4294925233); /* 11 */
-  FF ( b, c, d, a, in[11], S14, 2304563134); /* 12 */
-  FF ( a, b, c, d, in[12], S11, 1804603682); /* 13 */
-  FF ( d, a, b, c, in[13], S12, 4254626195); /* 14 */
-  FF ( c, d, a, b, in[14], S13, 2792965006); /* 15 */
-  FF ( b, c, d, a, in[15], S14, 1236535329); /* 16 */
+  FF ( a, b, c, d, in[ 0], S11, 3614090360u); /* 1 */
+  FF ( d, a, b, c, in[ 1], S12, 3905402710u); /* 2 */
+  FF ( c, d, a, b, in[ 2], S13,  606105819u); /* 3 */
+  FF ( b, c, d, a, in[ 3], S14, 3250441966u); /* 4 */
+  FF ( a, b, c, d, in[ 4], S11, 4118548399u); /* 5 */
+  FF ( d, a, b, c, in[ 5], S12, 1200080426u); /* 6 */
+  FF ( c, d, a, b, in[ 6], S13, 2821735955u); /* 7 */
+  FF ( b, c, d, a, in[ 7], S14, 4249261313u); /* 8 */
+  FF ( a, b, c, d, in[ 8], S11, 1770035416u); /* 9 */
+  FF ( d, a, b, c, in[ 9], S12, 2336552879u); /* 10 */
+  FF ( c, d, a, b, in[10], S13, 4294925233u); /* 11 */
+  FF ( b, c, d, a, in[11], S14, 2304563134u); /* 12 */
+  FF ( a, b, c, d, in[12], S11, 1804603682u); /* 13 */
+  FF ( d, a, b, c, in[13], S12, 4254626195u); /* 14 */
+  FF ( c, d, a, b, in[14], S13, 2792965006u); /* 15 */
+  FF ( b, c, d, a, in[15], S14, 1236535329u); /* 16 */
 
   /* Round 2 */
 #define S21 5
 #define S22 9
 #define S23 14
 #define S24 20
-  GG ( a, b, c, d, in[ 1], S21, 4129170786); /* 17 */
-  GG ( d, a, b, c, in[ 6], S22, 3225465664); /* 18 */
-  GG ( c, d, a, b, in[11], S23,  643717713); /* 19 */
-  GG ( b, c, d, a, in[ 0], S24, 3921069994); /* 20 */
-  GG ( a, b, c, d, in[ 5], S21, 3593408605); /* 21 */
-  GG ( d, a, b, c, in[10], S22,   38016083); /* 22 */
-  GG ( c, d, a, b, in[15], S23, 3634488961); /* 23 */
-  GG ( b, c, d, a, in[ 4], S24, 3889429448); /* 24 */
-  GG ( a, b, c, d, in[ 9], S21,  568446438); /* 25 */
-  GG ( d, a, b, c, in[14], S22, 3275163606); /* 26 */
-  GG ( c, d, a, b, in[ 3], S23, 4107603335); /* 27 */
-  GG ( b, c, d, a, in[ 8], S24, 1163531501); /* 28 */
-  GG ( a, b, c, d, in[13], S21, 2850285829); /* 29 */
-  GG ( d, a, b, c, in[ 2], S22, 4243563512); /* 30 */
-  GG ( c, d, a, b, in[ 7], S23, 1735328473); /* 31 */
-  GG ( b, c, d, a, in[12], S24, 2368359562); /* 32 */
+  GG ( a, b, c, d, in[ 1], S21, 4129170786u); /* 17 */
+  GG ( d, a, b, c, in[ 6], S22, 3225465664u); /* 18 */
+  GG ( c, d, a, b, in[11], S23,  643717713u); /* 19 */
+  GG ( b, c, d, a, in[ 0], S24, 3921069994u); /* 20 */
+  GG ( a, b, c, d, in[ 5], S21, 3593408605u); /* 21 */
+  GG ( d, a, b, c, in[10], S22,   38016083u); /* 22 */
+  GG ( c, d, a, b, in[15], S23, 3634488961u); /* 23 */
+  GG ( b, c, d, a, in[ 4], S24, 3889429448u); /* 24 */
+  GG ( a, b, c, d, in[ 9], S21,  568446438u); /* 25 */
+  GG ( d, a, b, c, in[14], S22, 3275163606u); /* 26 */
+  GG ( c, d, a, b, in[ 3], S23, 4107603335u); /* 27 */
+  GG ( b, c, d, a, in[ 8], S24, 1163531501u); /* 28 */
+  GG ( a, b, c, d, in[13], S21, 2850285829u); /* 29 */
+  GG ( d, a, b, c, in[ 2], S22, 4243563512u); /* 30 */
+  GG ( c, d, a, b, in[ 7], S23, 1735328473u); /* 31 */
+  GG ( b, c, d, a, in[12], S24, 2368359562u); /* 32 */
 
   /* Round 3 */
 #define S31 4
 #define S32 11
 #define S33 16
 #define S34 23
-  HH ( a, b, c, d, in[ 5], S31, 4294588738); /* 33 */
-  HH ( d, a, b, c, in[ 8], S32, 2272392833); /* 34 */
-  HH ( c, d, a, b, in[11], S33, 1839030562); /* 35 */
-  HH ( b, c, d, a, in[14], S34, 4259657740); /* 36 */
-  HH ( a, b, c, d, in[ 1], S31, 2763975236); /* 37 */
-  HH ( d, a, b, c, in[ 4], S32, 1272893353); /* 38 */
-  HH ( c, d, a, b, in[ 7], S33, 4139469664); /* 39 */
-  HH ( b, c, d, a, in[10], S34, 3200236656); /* 40 */
-  HH ( a, b, c, d, in[13], S31,  681279174); /* 41 */
-  HH ( d, a, b, c, in[ 0], S32, 3936430074); /* 42 */
-  HH ( c, d, a, b, in[ 3], S33, 3572445317); /* 43 */
-  HH ( b, c, d, a, in[ 6], S34,   76029189); /* 44 */
-  HH ( a, b, c, d, in[ 9], S31, 3654602809); /* 45 */
-  HH ( d, a, b, c, in[12], S32, 3873151461); /* 46 */
-  HH ( c, d, a, b, in[15], S33,  530742520); /* 47 */
-  HH ( b, c, d, a, in[ 2], S34, 3299628645); /* 48 */
+  HH ( a, b, c, d, in[ 5], S31, 4294588738u); /* 33 */
+  HH ( d, a, b, c, in[ 8], S32, 2272392833u); /* 34 */
+  HH ( c, d, a, b, in[11], S33, 1839030562u); /* 35 */
+  HH ( b, c, d, a, in[14], S34, 4259657740u); /* 36 */
+  HH ( a, b, c, d, in[ 1], S31, 2763975236u); /* 37 */
+  HH ( d, a, b, c, in[ 4], S32, 1272893353u); /* 38 */
+  HH ( c, d, a, b, in[ 7], S33, 4139469664u); /* 39 */
+  HH ( b, c, d, a, in[10], S34, 3200236656u); /* 40 */
+  HH ( a, b, c, d, in[13], S31,  681279174u); /* 41 */
+  HH ( d, a, b, c, in[ 0], S32, 3936430074u); /* 42 */
+  HH ( c, d, a, b, in[ 3], S33, 3572445317u); /* 43 */
+  HH ( b, c, d, a, in[ 6], S34,   76029189u); /* 44 */
+  HH ( a, b, c, d, in[ 9], S31, 3654602809u); /* 45 */
+  HH ( d, a, b, c, in[12], S32, 3873151461u); /* 46 */
+  HH ( c, d, a, b, in[15], S33,  530742520u); /* 47 */
+  HH ( b, c, d, a, in[ 2], S34, 3299628645u); /* 48 */
 
   /* Round 4 */
 #define S41 6
 #define S42 10
 #define S43 15
 #define S44 21
-  II ( a, b, c, d, in[ 0], S41, 4096336452); /* 49 */
-  II ( d, a, b, c, in[ 7], S42, 1126891415); /* 50 */
-  II ( c, d, a, b, in[14], S43, 2878612391); /* 51 */
-  II ( b, c, d, a, in[ 5], S44, 4237533241); /* 52 */
-  II ( a, b, c, d, in[12], S41, 1700485571); /* 53 */
-  II ( d, a, b, c, in[ 3], S42, 2399980690); /* 54 */
-  II ( c, d, a, b, in[10], S43, 4293915773); /* 55 */
-  II ( b, c, d, a, in[ 1], S44, 2240044497); /* 56 */
-  II ( a, b, c, d, in[ 8], S41, 1873313359); /* 57 */
-  II ( d, a, b, c, in[15], S42, 4264355552); /* 58 */
-  II ( c, d, a, b, in[ 6], S43, 2734768916); /* 59 */
-  II ( b, c, d, a, in[13], S44, 1309151649); /* 60 */
-  II ( a, b, c, d, in[ 4], S41, 4149444226); /* 61 */
-  II ( d, a, b, c, in[11], S42, 3174756917); /* 62 */
-  II ( c, d, a, b, in[ 2], S43,  718787259); /* 63 */
-  II ( b, c, d, a, in[ 9], S44, 3951481745); /* 64 */
+  II ( a, b, c, d, in[ 0], S41, 4096336452u); /* 49 */
+  II ( d, a, b, c, in[ 7], S42, 1126891415u); /* 50 */
+  II ( c, d, a, b, in[14], S43, 2878612391u); /* 51 */
+  II ( b, c, d, a, in[ 5], S44, 4237533241u); /* 52 */
+  II ( a, b, c, d, in[12], S41, 1700485571u); /* 53 */
+  II ( d, a, b, c, in[ 3], S42, 2399980690u); /* 54 */
+  II ( c, d, a, b, in[10], S43, 4293915773u); /* 55 */
+  II ( b, c, d, a, in[ 1], S44, 2240044497u); /* 56 */
+  II ( a, b, c, d, in[ 8], S41, 1873313359u); /* 57 */
+  II ( d, a, b, c, in[15], S42, 4264355552u); /* 58 */
+  II ( c, d, a, b, in[ 6], S43, 2734768916u); /* 59 */
+  II ( b, c, d, a, in[13], S44, 1309151649u); /* 60 */
+  II ( a, b, c, d, in[ 4], S41, 4149444226u); /* 61 */
+  II ( d, a, b, c, in[11], S42, 3174756917u); /* 62 */
+  II ( c, d, a, b, in[ 2], S43,  718787259u); /* 63 */
+  II ( b, c, d, a, in[ 9], S44, 3951481745u); /* 64 */
 
   buf[0] += a;
   buf[1] += b;
diff --git a/src/backend/mscoff.h b/src/backend/mscoff.h
index 1f9d045..c189791 100644
--- a/src/backend/mscoff.h
+++ b/src/backend/mscoff.h
@@ -8,9 +8,17 @@
 #pragma ZTC align 1
 #endif
 
+#if !defined(GCC_PACKED)
+#if __MINGW32__
+#define GCC_PACKED __attribute__((packed,ms_struct))
+#else
+#define GCC_PACKED
+#endif
+#endif
+
 /***********************************************/
 
-struct filehdr
+struct GCC_PACKED filehdr
 {
         unsigned short f_magic; // identifies type of target machine
 #define IMAGE_FILE_MACHINE_UNKNOWN 0            // applies to any machine type
@@ -42,7 +50,7 @@ struct filehdr
 /***********************************************/
 
 // size should be 40 bytes
-struct scnhdr
+struct GCC_PACKED scnhdr
 {
         char s_name[8];         // name or /nnnn, where nnnn is offset in string table
         long s_paddr;           // virtual size, 0 for obj files
@@ -93,13 +101,13 @@ struct scnhdr
 
 /***********************************************/
 
-struct syment
+struct GCC_PACKED syment
 {
-    union
+    union GCC_PACKED
     {
 #define SYMNMLEN        8
         char _n_name[SYMNMLEN];
-        struct
+        struct GCC_PACKED
         {   long _n_zeroes;
             long _n_offset;
         } _n_n;
@@ -132,7 +140,7 @@ struct syment
 
 /***********************************************/
 
-struct reloc
+struct GCC_PACKED reloc
 {
 #pragma pack(1)
     unsigned r_vaddr;           // file offset of relocation
@@ -175,9 +183,9 @@ struct reloc
 
 /***********************************************/
 
-struct lineno
+struct GCC_PACKED lineno
 {
-    union
+    union GCC_PACKED
     {
         unsigned l_symndx;
         unsigned l_paddr;
@@ -189,11 +197,11 @@ struct lineno
 /***********************************************/
 
 #pragma pack(1)
-union auxent
+union GCC_PACKED auxent
 {
 #pragma pack(1)
     // Function definitions
-    struct
+    struct GCC_PACKED
     {   unsigned TagIndex;
         unsigned TotalSize;
         unsigned PointerToLinenumber;
@@ -201,7 +209,7 @@ union auxent
     } x_fd;
 
     // .bf symbols
-    struct
+    struct GCC_PACKED
     {
 #pragma pack(1)
         unsigned Unused;
@@ -212,13 +220,13 @@ union auxent
     } x_bf;
 
     // .ef symbols
-    struct
+    struct GCC_PACKED
     {   unsigned Unused;
         unsigned short Linenumber;
     } x_ef;
 
     // Weak externals
-    struct
+    struct GCC_PACKED
     {   unsigned TagIndex;
         unsigned Characteristics;
 #define IMAGE_WEAK_EXTERN_SEARCH_NOLIBRARY
@@ -227,12 +235,12 @@ union auxent
     } x_weak;
 
     // Files
-    struct
+    struct GCC_PACKED
     {   char FileName[18];
     } x_filename;
 
     // Section definitions
-    struct
+    struct GCC_PACKED
     {   unsigned length;
         unsigned short NumberOfRelocations;
         unsigned short NumberOfLinenumbers;
diff --git a/src/backend/newman.c b/src/backend/newman.c
index 6f77fb9..67df543 100644
--- a/src/backend/newman.c
+++ b/src/backend/newman.c
@@ -131,8 +131,8 @@ struct OPTABLE
 {
     unsigned char tokn;
     unsigned char oper;
-    char *string;
-    char *pretty;
+    const char *string;
+    const char *pretty;
 }
 #endif
  oparray[] = {
diff --git a/src/backend/optabgen.c b/src/backend/optabgen.c
index cb886b9..024b8c4 100644
--- a/src/backend/optabgen.c
+++ b/src/backend/optabgen.c
@@ -683,7 +683,7 @@ void fltables()
         char stackfl[FLMAX];
         char flinsymtab[FLMAX];
 
-        static char indatafl[] =        /* is FLxxxx a data type?       */
+        static unsigned char indatafl[] =        /* is FLxxxx a data type?       */
         { FLdata,FLudata,FLreg,FLpseudo,FLauto,FLfast,FLpara,FLextern,
           FLcs,FLfltreg,FLallocatmp,FLdatseg,FLtlsdata,FLbprel,
           FLstack,FLregsave,
@@ -695,14 +695,14 @@ void fltables()
         static char indatafl_s[] = { FLfardata, };
 #endif
 
-        static char instackfl[] =       /* is FLxxxx a stack data type? */
+        static unsigned char instackfl[] =       /* is FLxxxx a stack data type? */
         { FLauto,FLfast,FLpara,FLcs,FLfltreg,FLallocatmp,FLbprel,FLstack,FLregsave,
 #if TX86
           FLndp,
 #endif
         };
 
-        static char inflinsymtab[] =    /* is FLxxxx in the symbol table? */
+        static unsigned char inflinsymtab[] =    /* is FLxxxx in the symbol table? */
         { FLdata,FLudata,FLreg,FLpseudo,FLauto,FLfast,FLpara,FLextern,FLfunc,
           FLtlsdata,FLbprel,FLstack };
 #if TARGET_SEGMENTED
diff --git a/src/backend/os.c b/src/backend/os.c
index cac16f5..0d90515 100644
--- a/src/backend/os.c
+++ b/src/backend/os.c
@@ -19,6 +19,9 @@
 #include <time.h>
 #include <stdlib.h>
 #include <string.h>
+#if __MINGW32__
+#include <malloc.h>
+#endif
 
 #if DOS386
 #include <dos.h>
@@ -35,7 +38,11 @@
 #define GetLastError() errno
 #elif _WIN32
 #include <dos.h>
-#include <sys\stat.h>
+#if __MINGW32__
+#include <sys/stat.h>
+#else
+#include <sys/stat.h>
+#endif
 #include        <windows.h>
 #endif
 
@@ -680,13 +687,13 @@ int os_file_exists(const char *name)
  * Get file size of open file. Return -1L on error.
  */
 
-#if _WIN32 && !_MSC_VER
+#if _WIN32 && !_MSC_VER && !__MINGW32__
 extern "C" void * __cdecl _osfhnd[];
 #endif
 
 long os_file_size(int fd)
 {
-#if _WIN32 && !_MSC_VER
+#if _WIN32 && !_MSC_VER && !__MINGW32__
     return GetFileSize(_osfhnd[fd],NULL);
 #else
     struct stat buf;
diff --git a/src/backend/pdata.c b/src/backend/pdata.c
index e2b0067..cdfc505 100644
--- a/src/backend/pdata.c
+++ b/src/backend/pdata.c
@@ -15,6 +15,9 @@
 #include        <string.h>
 #include        <stdlib.h>
 #include        <time.h>
+#if __MINGW32__
+#include <malloc.h>
+#endif
 
 #include        "cc.h"
 #include        "el.h"
diff --git a/src/backend/strtold.c b/src/backend/strtold.c
index 4f284c6..f97c4c3 100644
--- a/src/backend/strtold.c
+++ b/src/backend/strtold.c
@@ -19,7 +19,7 @@
 #include        <fenv.h>
 #include        <fltpnt.h>
 #endif
-#if __linux__ || __APPLE__ || __FreeBSD__ || __OpenBSD__ || __sun
+#if __linux__ || __APPLE__ || __FreeBSD__ || __OpenBSD__ || __sun || __MINGW32__
 #include        <errno.h>
 #endif
 
diff --git a/src/backend/type.c b/src/backend/type.c
index bb9d30c..5381e8e 100644
--- a/src/backend/type.c
+++ b/src/backend/type.c
@@ -1063,7 +1063,7 @@ void type_print(type *t)
 #endif
         tybasic(t->Tty) != TYmfunc &&
         tybasic(t->Tty) != TYarray)
-      dbg_printf(" Tsize=%lld",(long long)type_size(t));
+      dbg_printf(" Tsize="FMTS_I64"",(long long)type_size(t));
   dbg_printf(" Tnext=%p",t->Tnext);
   switch (tybasic(t->Tty))
   {     case TYstruct:
diff --git a/src/backend/var.c b/src/backend/var.c
index 4bdd1ef..b17ec74 100644
--- a/src/backend/var.c
+++ b/src/backend/var.c
@@ -200,7 +200,9 @@ char sytab[SCMAX] =
 };
 
 #endif /* !SPP */
+#if !__MINGW32__
 volatile int controlc_saw;              /* a control C was seen         */
+#endif
 symtab_t globsym;               /* global symbol table                  */
 Pstate pstate;                  // parser state
 Cstate cstate;                  // compiler state
diff --git a/src/cast.c b/src/cast.c
index ddac9a7..6e6c1a1 100644
--- a/src/cast.c
+++ b/src/cast.c
@@ -382,7 +382,7 @@ MATCH implicitConvTo(Expression *e, Type *t)
                     if ((oldty == Twchar || oldty == Tdchar) && value > 0x7F)
                         return;
                 case Tuns8:
-                    //printf("value = %llu %llu\n", (dinteger_t)(unsigned char)value, value);
+                    //printf("value = "FMTS_U64" "FMTS_U64"\n", (dinteger_t)(unsigned char)value, value);
                     if ((unsigned char)value != value)
                         return;
                     break;
diff --git a/src/constfold.c b/src/constfold.c
index 55853bf..c6ce18f 100644
--- a/src/constfold.c
+++ b/src/constfold.c
@@ -1361,7 +1361,7 @@ UnionExp Index(Type *type, Expression *e1, Expression *e2)
 
         if (i >= es1->len)
         {
-            e1->error("string index %llu is out of bounds [0 .. %llu]", i, (ulonglong)es1->len);
+            e1->error("string index "FMTS_U64" is out of bounds [0 .. "FMTS_U64"]", i, (ulonglong)es1->len);
             new(&ue) ErrorExp();
         }
         else
@@ -1377,7 +1377,7 @@ UnionExp Index(Type *type, Expression *e1, Expression *e2)
 
         if (i >= length)
         {
-            e1->error("array index %llu is out of bounds %s[0 .. %llu]", i, e1->toChars(), length);
+            e1->error("array index "FMTS_U64" is out of bounds %s[0 .. "FMTS_U64"]", i, e1->toChars(), length);
             new(&ue) ErrorExp();
         }
         else if (e1->op == TOKarrayliteral)
@@ -1403,7 +1403,7 @@ UnionExp Index(Type *type, Expression *e1, Expression *e2)
             ArrayLiteralExp *ale = (ArrayLiteralExp *)e1;
             if (i >= ale->elements->dim)
             {
-                e1->error("array index %llu is out of bounds %s[0 .. %u]", i, e1->toChars(), ale->elements->dim);
+                e1->error("array index "FMTS_U64" is out of bounds %s[0 .. %u]", i, e1->toChars(), ale->elements->dim);
                 new(&ue) ErrorExp();
             }
             else
@@ -1475,7 +1475,7 @@ UnionExp Slice(Type *type, Expression *e1, Expression *lwr, Expression *upr)
 
         if (iupr > es1->len || ilwr > iupr)
         {
-            e1->error("string slice [%llu .. %llu] is out of bounds", ilwr, iupr);
+            e1->error("string slice ["FMTS_U64" .. "FMTS_U64"] is out of bounds", ilwr, iupr);
             new(&ue) ErrorExp();
         }
         else
@@ -1505,7 +1505,7 @@ UnionExp Slice(Type *type, Expression *e1, Expression *lwr, Expression *upr)
 
         if (iupr > es1->elements->dim || ilwr > iupr)
         {
-            e1->error("array slice [%llu .. %llu] is out of bounds", ilwr, iupr);
+            e1->error("array slice ["FMTS_U64" .. "FMTS_U64"] is out of bounds", ilwr, iupr);
             new(&ue) ErrorExp();
         }
         else
diff --git a/src/cppmangle.c b/src/cppmangle.c
index e9d5d32..2e6a475 100644
--- a/src/cppmangle.c
+++ b/src/cppmangle.c
@@ -159,7 +159,7 @@ class CppMangleVisitor : public Visitor
                         tv->valType->accept(this);
                         if (tv->valType->isunsigned())
                         {
-                            buf.printf("%llu", e->toUInteger());
+                            buf.printf(""FMTS_U64"", e->toUInteger());
                         }
                         else
                         {
@@ -169,7 +169,7 @@ class CppMangleVisitor : public Visitor
                                 val = -val;
                                 buf.writeByte('n');
                             }
-                            buf.printf("%lld", val);
+                            buf.printf(""FMTS_I64"", val);
                         }
                         buf.writeByte('E');
                     }
@@ -672,7 +672,7 @@ public:
             buf.writeByte('K');
         assert(t->basetype && t->basetype->ty == Tsarray);
         assert(((TypeSArray *)t->basetype)->dim);
-        //buf.printf("Dv%llu_", ((TypeSArray *)t->basetype)->dim->toInteger());// -- Gnu ABI v.4
+        //buf.printf("Dv"FMTS_U64"_", ((TypeSArray *)t->basetype)->dim->toInteger());// -- Gnu ABI v.4
         buf.writestring("U8__vector"); //-- Gnu ABI v.3
         t->basetype->nextOf()->accept(this);
     }
@@ -688,7 +688,7 @@ public:
         }
         if (t->isConst())
             buf.writeByte('K');
-        buf.printf("A%llu_", t->dim ? t->dim->toInteger() : 0);
+        buf.printf("A"FMTS_U64"_", t->dim ? t->dim->toInteger() : 0);
         t->next->accept(this);
     }
 
diff --git a/src/ctfeexpr.c b/src/ctfeexpr.c
index 67e7ed3..39ad97c 100644
--- a/src/ctfeexpr.c
+++ b/src/ctfeexpr.c
@@ -795,7 +795,7 @@ UnionExp pointerArithmetic(Loc loc, TOK op, Type *type,
 
     if (indx < 0 || len < indx)
     {
-        error(loc, "cannot assign pointer to index %lld inside memory block [0..%lld]", indx, len);
+        error(loc, "cannot assign pointer to index "FMTS_I64" inside memory block [0.."FMTS_I64"]", indx, len);
         goto Lcant;
     }
 
@@ -1752,7 +1752,7 @@ Expression *ctfeIndex(Loc loc, Type *type, Expression *e1, uinteger_t indx)
         StringExp *es1 = (StringExp *)e1;
         if (indx >= es1->len)
         {
-            error(loc, "string index %llu is out of bounds [0 .. %llu]", indx, (ulonglong)es1->len);
+            error(loc, "string index "FMTS_U64" is out of bounds [0 .. "FMTS_U64"]", indx, (ulonglong)es1->len);
             return CTFEExp::cantexp;
         }
         return new IntegerExp(loc, es1->charAt(indx), type);
@@ -1762,7 +1762,7 @@ Expression *ctfeIndex(Loc loc, Type *type, Expression *e1, uinteger_t indx)
         ArrayLiteralExp *ale = (ArrayLiteralExp *)e1;
         if (indx >= ale->elements->dim)
         {
-            error(loc, "array index %llu is out of bounds %s[0 .. %llu]", indx, e1->toChars(), (ulonglong)ale->elements->dim);
+            error(loc, "array index "FMTS_U64" is out of bounds %s[0 .. "FMTS_U64"]", indx, e1->toChars(), (ulonglong)ale->elements->dim);
             return CTFEExp::cantexp;
         }
         Expression *e = (*ale->elements)[(size_t)indx];
diff --git a/src/declaration.c b/src/declaration.c
index 1b39989..a6acb2f 100644
--- a/src/declaration.c
+++ b/src/declaration.c
@@ -1024,7 +1024,7 @@ Lnomatch:
             Parameter *arg = Parameter::getNth(tt->arguments, i);
 
             OutBuffer buf;
-            buf.printf("__%s_field_%llu", ident->toChars(), (ulonglong)i);
+            buf.printf("__%s_field_"FMTS_U64"", ident->toChars(), (ulonglong)i);
             const char *name = buf.extractString();
             Identifier *id = Identifier::idPool(name);
 
diff --git a/src/enum.c b/src/enum.c
index 9a9b4b8..0a45346 100644
--- a/src/enum.c
+++ b/src/enum.c
@@ -268,7 +268,7 @@ void EnumDeclaration::semantic(Scope *sc)
         if (em)
             em->semantic(em->scope);
     }
-    //printf("defaultval = %lld\n", defaultval);
+    //printf("defaultval = "FMTS_I64"\n", defaultval);
 
     //if (defaultval) printf("defaultval: %s %s\n", defaultval->toChars(), defaultval->type->toChars());
     //members->print();
diff --git a/src/expression.c b/src/expression.c
index 4bfb685..c701382 100644
--- a/src/expression.c
+++ b/src/expression.c
@@ -1408,7 +1408,7 @@ bool functionParameters(Loc loc, Scope *sc, TypeFunction *tf,
 
     if (nargs > nparams && tf->varargs == 0)
     {
-        error(loc, "expected %llu arguments, not %llu for non-variadic function type %s", (ulonglong)nparams, (ulonglong)nargs, tf->toChars());
+        error(loc, "expected "FMTS_U64" arguments, not "FMTS_U64" for non-variadic function type %s", (ulonglong)nparams, (ulonglong)nargs, tf->toChars());
         return true;
     }
 
@@ -1468,7 +1468,7 @@ bool functionParameters(Loc loc, Scope *sc, TypeFunction *tf,
                 {
                     if (tf->varargs == 2 && i + 1 == nparams)
                         goto L2;
-                    error(loc, "expected %llu function arguments, not %llu", (ulonglong)nparams, (ulonglong)nargs);
+                    error(loc, "expected "FMTS_U64" function arguments, not "FMTS_U64"", (ulonglong)nparams, (ulonglong)nargs);
                     return true;
                 }
                 arg = p->defaultArg;
@@ -1489,7 +1489,7 @@ bool functionParameters(Loc loc, Scope *sc, TypeFunction *tf,
                         if (p->type->nextOf() && arg->implicitConvTo(p->type->nextOf()) >= m)
                             goto L2;
                         else if (nargs != nparams)
-                        {   error(loc, "expected %llu function arguments, not %llu", (ulonglong)nparams, (ulonglong)nargs);
+                        {   error(loc, "expected "FMTS_U64" function arguments, not "FMTS_U64"", (ulonglong)nparams, (ulonglong)nargs);
                             return true;
                         }
                         goto L1;
@@ -2720,7 +2720,7 @@ Expression *Expression::addDtorHook(Scope *sc)
 IntegerExp::IntegerExp(Loc loc, dinteger_t value, Type *type)
         : Expression(loc, TOKint64, sizeof(IntegerExp))
 {
-    //printf("IntegerExp(value = %lld, type = '%s')\n", value, type ? type->toChars() : "");
+    //printf("IntegerExp(value = "FMTS_I64", type = '%s')\n", value, type ? type->toChars() : "");
     assert(type);
     if (!type->isscalar())
     {
@@ -9959,7 +9959,7 @@ Lagain:
 
         if (i2 < i1 || length < i2)
         {
-            error("string slice [%llu .. %llu] is out of bounds", i1, i2);
+            error("string slice ["FMTS_U64" .. "FMTS_U64"] is out of bounds", i1, i2);
             return new ErrorExp();
         }
 
@@ -10592,7 +10592,7 @@ Expression *IndexExp::semantic(Scope *sc)
 
             if (length <= index)
             {
-                error("array index [%llu] is outside array bounds [0 .. %llu]",
+                error("array index ["FMTS_U64"] is outside array bounds [0 .. "FMTS_U64"]",
                         index, (ulonglong)length);
                 return new ErrorExp();
             }
diff --git a/src/glue.c b/src/glue.c
index 9c19e89..d290f11 100644
--- a/src/glue.c
+++ b/src/glue.c
@@ -258,7 +258,7 @@ void obj_end(Library *library, File *objfile)
 {
     const char *objfilename = objfile->name->toChars();
     objmod->term(objfilename);
-    delete objmod;
+    //k8:delete objmod; // don't do that, as we don't have virtual destructor for Obj
     objmod = NULL;
 
     if (library)
diff --git a/src/hdrgen.c b/src/hdrgen.c
index a02e4e8..494bd5c 100644
--- a/src/hdrgen.c
+++ b/src/hdrgen.c
@@ -2006,7 +2006,7 @@ public:
                     assert(0);
                 if (uval <= sizemax && uval <= 0x7FFFFFFFFFFFFFFFULL)
                 {
-                    buf->printf("%llu", uval);
+                    buf->printf(""FMTS_U64"", uval);
                     return;
                 }
             }
@@ -2113,12 +2113,12 @@ public:
                     break;
 
                 case Tint64:
-                    buf->printf("%lldL", v);
+                    buf->printf(""FMTS_I64"L", v);
                     break;
 
                 case Tuns64:
                 L4:
-                    buf->printf("%lluLU", v);
+                    buf->printf(""FMTS_U64"LU", v);
                     break;
 
                 case Tbool:
@@ -2153,7 +2153,7 @@ public:
         else if (v & 0x8000000000000000LL)
             buf->printf("0x%llx", v);
         else
-            buf->printf("%lld", v);
+            buf->printf(""FMTS_I64"", v);
     }
 
     void visit(ErrorExp *e)
diff --git a/src/iasm.c b/src/iasm.c
index b43cfe4..2b30a63 100644
--- a/src/iasm.c
+++ b/src/iasm.c
@@ -2633,7 +2633,7 @@ static void asm_make_modrm_byte(
 
 #ifdef DEBUG
         if (debuga)
-            printf("This is an mod = %d, popnd->s =%p, popnd->disp = %lld\n",
+            printf("This is an mod = %d, popnd->s =%p, popnd->disp = "FMTS_I64"\n",
                mrmb.modregrm.mod, s, (long long)popnd->disp);
 #endif
         if (!s || (!mrmb.modregrm.mod && popnd->disp))
@@ -2840,7 +2840,7 @@ static void asm_make_modrm_byte(
             {
 #ifdef DEBUG
                 if (debuga)
-                    printf("Setting up value %lld\n", (long long)popnd->disp);
+                    printf("Setting up value "FMTS_I64"\n", (long long)popnd->disp);
 #endif
                 pc->IEVint1 = popnd->disp;
                 pc->IFL1 = FLconst;
@@ -2864,7 +2864,7 @@ static void asm_make_modrm_byte(
             {
 #ifdef DEBUG
                 if (debuga)
-                    printf("Setting up value %lld\n", (long long)popnd->disp);
+                    printf("Setting up value "FMTS_I64"\n", (long long)popnd->disp);
 #endif
                 pc->IEVpointer1 = (targ_size_t) popnd->disp;
                 pc->IFL1 = FLconst;
diff --git a/src/identifier.c b/src/identifier.c
index f5bf2a2..b9d747e 100644
--- a/src/identifier.c
+++ b/src/identifier.c
@@ -96,7 +96,7 @@ Identifier *Identifier::generateId(const char *prefix, size_t i)
 {   OutBuffer buf;
 
     buf.writestring(prefix);
-    buf.printf("%llu", (ulonglong)i);
+    buf.printf(""FMTS_U64"", (ulonglong)i);
 
     char *id = buf.peekString();
     return idPool(id);
diff --git a/src/init.c b/src/init.c
index 200c6a3..23695a3 100644
--- a/src/init.c
+++ b/src/init.c
@@ -508,7 +508,7 @@ Initializer *ArrayInitializer::semantic(Scope *sc, Type *t, NeedInterpret needIn
         dinteger_t edim = ((TypeSArray *)t)->dim->toInteger();
         if (dim > edim)
         {
-            error(loc, "array initializer has %u elements, but array length is %lld", dim, edim);
+            error(loc, "array initializer has %u elements, but array length is "FMTS_I64"", dim, edim);
             goto Lerr;
         }
     }
diff --git a/src/interpret.c b/src/interpret.c
index db9a191..874fb54 100644
--- a/src/interpret.c
+++ b/src/interpret.c
@@ -3109,7 +3109,7 @@ public:
             d_uns64 sz = e1->type->size() * 8;
             if (i2 < 0 || i2 >= sz)
             {
-                e->error("shift by %lld is outside the range 0..%llu", i2, (ulonglong)sz - 1);
+                e->error("shift by "FMTS_I64" is outside the range 0.."FMTS_U64"", i2, (ulonglong)sz - 1);
                 result = CTFEExp::cantexp;
                 return;
             }
@@ -3937,7 +3937,7 @@ public:
                 SliceExp *oldse = (SliceExp *)aggregate;
                 if (oldse->upr->toInteger() < upperbound + oldse->lwr->toInteger())
                 {
-                    originalExp->error("slice [%d..%d] exceeds array bounds [0..%lld]",
+                    originalExp->error("slice [%d..%d] exceeds array bounds [0.."FMTS_I64"]",
                         lowerbound, upperbound,
                         oldse->upr->toInteger() - oldse->lwr->toInteger());
                     return CTFEExp::cantexp;
@@ -4855,7 +4855,7 @@ public:
                 dinteger_t len = resolveArrayLength(agg);
                 if (ofs + indx >= len)
                 {
-                    e->error("pointer index [%lld] exceeds allocated memory block [0..%lld]",
+                    e->error("pointer index ["FMTS_I64"] exceeds allocated memory block [0.."FMTS_I64"]",
                         ofs + indx, len);
                     return false;
                 }
@@ -4864,7 +4864,7 @@ public:
             {
                 if (ofs + indx != 0)
                 {
-                    e->error("pointer index [%lld] lies outside memory block [0..1]",
+                    e->error("pointer index ["FMTS_I64"] lies outside memory block [0..1]",
                         ofs + indx);
                     return false;
                 }
@@ -4922,7 +4922,7 @@ public:
 
             if (index > iupr - ilwr)
             {
-                e->error("index %llu exceeds array length %llu", index, iupr - ilwr);
+                e->error("index "FMTS_U64" exceeds array length "FMTS_U64"", index, iupr - ilwr);
                 return false;
             }
             *pagg = ((SliceExp *)e1)->e1;
@@ -4934,7 +4934,7 @@ public:
             *pidx = e2->toInteger();
             if (len <= *pidx)
             {
-                e->error("array index %lld is out of bounds [0..%lld]",
+                e->error("array index "FMTS_I64" is out of bounds [0.."FMTS_I64"]",
                     *pidx, len);
                 return false;
             }
@@ -5120,7 +5120,7 @@ public:
             //Type *pointee = ((TypePointer *)agg->type)->next;
             if (iupr > (len + 1) || iupr < ilwr)
             {
-                e->error("pointer slice [%lld..%lld] exceeds allocated memory block [0..%lld]",
+                e->error("pointer slice ["FMTS_I64".."FMTS_I64"] exceeds allocated memory block [0.."FMTS_I64"]",
                     ilwr, iupr, len);
                 result = CTFEExp::cantexp;
                 return;
@@ -5192,7 +5192,7 @@ public:
                 result = e1;
                 return;
             }
-            e1->error("slice [%llu..%llu] is out of bounds", ilwr, iupr);
+            e1->error("slice ["FMTS_U64".."FMTS_U64"] is out of bounds", ilwr, iupr);
             result = CTFEExp::cantexp;
             return;
         }
@@ -5205,7 +5205,7 @@ public:
             uinteger_t up1 = se->upr->toInteger();
             if (ilwr > iupr || iupr > up1 - lo1)
             {
-                e->error("slice[%llu..%llu] exceeds array bounds[%llu..%llu]",
+                e->error("slice["FMTS_U64".."FMTS_U64"] exceeds array bounds["FMTS_U64".."FMTS_U64"]",
                     ilwr, iupr, lo1, up1);
                 result = CTFEExp::cantexp;
                 return;
@@ -5222,7 +5222,7 @@ public:
         {
             if (iupr < ilwr || dollar < iupr)
             {
-                e->error("slice [%lld..%lld] exceeds array bounds [0..%lld]", ilwr, iupr, dollar);
+                e->error("slice ["FMTS_I64".."FMTS_I64"] exceeds array bounds [0.."FMTS_I64"]", ilwr, iupr, dollar);
                 result = CTFEExp::cantexp;
                 return;
             }
diff --git a/src/lexer.c b/src/lexer.c
index d73f554..4287eef 100644
--- a/src/lexer.c
+++ b/src/lexer.c
@@ -2127,7 +2127,7 @@ void Lexer::poundLine()
     {
         int lin = (int)(tok.uns64value - 1);
         if (lin != tok.uns64value - 1)
-            error("line number %lld out of range", (unsigned long long)tok.uns64value);
+            error("line number "FMTS_I64" out of range", (unsigned long long)tok.uns64value);
         else
             linnum = lin;
     }
diff --git a/src/libelf.c b/src/libelf.c
index f574960..6729c3b 100644
--- a/src/libelf.c
+++ b/src/libelf.c
@@ -205,7 +205,7 @@ void OmToHeader(Header *h, ObjModule *om)
      * write into the next field, which we will promptly overwrite
      * anyway. (So make sure to write the fields in ascending order.)
      */
-    len = sprintf(h->file_time, "%llu", (longlong)om->file_time);
+    len = sprintf(h->file_time, ""FMTS_U64"", (longlong)om->file_time);
     assert(len <= 12);
     memset(h->file_time + len, ' ', 12 - len);
 
diff --git a/src/libmach.c b/src/libmach.c
index 08c508d..7348e84 100644
--- a/src/libmach.c
+++ b/src/libmach.c
@@ -207,7 +207,7 @@ void OmToHeader(Header *h, ObjModule *om)
      * write into the next field, which we will promptly overwrite
      * anyway. (So make sure to write the fields in ascending order.)
      */
-    len = sprintf(h->file_time, "%llu", (longlong)om->file_time);
+    len = sprintf(h->file_time, ""FMTS_U64"", (longlong)om->file_time);
     assert(len <= 12);
     memset(h->file_time + len, ' ', 12 - len);
 
diff --git a/src/libmscoff.c b/src/libmscoff.c
index da0e84e..5d78a14 100644
--- a/src/libmscoff.c
+++ b/src/libmscoff.c
@@ -261,7 +261,7 @@ void OmToHeader(Header *h, ObjModule *om)
      * write into the next field, which we will promptly overwrite
      * anyway. (So make sure to write the fields in ascending order.)
      */
-    len = sprintf(h->file_time, "%llu", (longlong)om->file_time);
+    len = sprintf(h->file_time, ""FMTS_U64"", (longlong)om->file_time);
     assert(len <= 12);
     memset(h->file_time + len, ' ', 12 - len);
 
diff --git a/src/libomf.c b/src/libomf.c
index ab3fc4c..dc0a4b2 100644
--- a/src/libomf.c
+++ b/src/libomf.c
@@ -25,6 +25,14 @@
 #include "mars.h"
 #include "lib.h"
 
+#if !defined(GCC_PACKED)
+#if __MINGW32__
+#define GCC_PACKED __attribute__((packed,ms_struct))
+#else
+#define GCC_PACKED
+#endif
+#endif
+
 #define LOG 0
 
 struct ObjModule;
@@ -252,7 +260,7 @@ void LibOMF::addObject(const char *module_name, void *buf, size_t buflen)
      */
 
     #pragma pack(1)
-    struct LibHeader
+    struct GCC_PACKED LibHeader
     {
         unsigned char       recTyp;      // 0xF0
         unsigned short      pagesize;
@@ -316,7 +324,7 @@ void LibOMF::addObject(const char *module_name, void *buf, size_t buflen)
             Context *ctx = (Context *)pctx;
             ObjModule *om = new ObjModule();
             om->base = (unsigned char *)base;
-            om->page = om->page = (om->base - ctx->pstart) / ctx->pagesize;
+            om->page = /*om->page =*/ (om->base - ctx->pstart) / ctx->pagesize;
             om->length = length;
 
             /* Determine the name of the module
@@ -740,7 +748,7 @@ void LibOMF::WriteLibToBuffer(OutBuffer *libbuf)
 
     // Create library header
     #pragma pack(1)
-    struct Libheader
+    struct GCC_PACKED Libheader
     {
         unsigned char   recTyp;
         unsigned short  recLen;
diff --git a/src/link.c b/src/link.c
index 427ab9e..30a80df 100644
--- a/src/link.c
+++ b/src/link.c
@@ -22,6 +22,9 @@
 #include <windows.h>
 #endif
 #endif
+#if __MINGW32__
+#include <malloc.h>
+#endif
 
 #if __linux__ || __APPLE__ || __FreeBSD__ || __OpenBSD__ || __sun
 #include        <sys/types.h>
diff --git a/src/mangle.c b/src/mangle.c
index cfb8aeb..3605765 100644
--- a/src/mangle.c
+++ b/src/mangle.c
@@ -89,7 +89,7 @@ void initTypeMangle()
     for (size_t i = 0; i < TMAX; i++)
     {
         if (!mangleChar[i])
-            fprintf(stderr, "ty = %llu\n", (ulonglong)i);
+            fprintf(stderr, "ty = "FMTS_U64"\n", (ulonglong)i);
         assert(mangleChar[i]);
     }
 }
@@ -179,7 +179,7 @@ public:
     {
         visit((Type *)t);
         if (t->dim)
-            buf->printf("%llu", t->dim->toInteger());
+            buf->printf(""FMTS_U64"", t->dim->toInteger());
         if (t->next)
             visitWithMask(t->next, t->mod);
     }
@@ -273,6 +273,7 @@ public:
     {
         visit((Type *)t);
         const char *name = t->ident->toChars();
+        assert(name);
         size_t len = strlen(name);
         buf->printf("%u%s", (unsigned)len, name);
     }
@@ -307,7 +308,9 @@ public:
         Mangler v(&buf2);
         v.paramsToDecoBuffer(t->arguments);
         int len = (int)buf2.offset;
-        buf->printf("%d%.*s", len, len, buf2.extractData());
+        const char *d = buf2.extractData();
+        assert(d);
+        buf->printf("%d%.*s", len, len, d);
     }
 
     void visit(TypeNull *t)
@@ -392,10 +395,10 @@ public:
     {
         size_t len = strlen(id);
         if (len >= 8 * 1024 * 1024)         // 8 megs ought be enough for anyone
-            s->error("excessive length %llu for symbol, possible recursive expansion?", len);
+            s->error("excessive length "FMTS_U64" for symbol, possible recursive expansion?", len);
         else
         {
-            buf->printf("%llu", (ulonglong)len);
+            buf->printf(""FMTS_U64"", (ulonglong)len);
             buf->write(id, len);
         }
     }
@@ -640,9 +643,9 @@ public:
     void visit(IntegerExp *e)
     {
         if ((sinteger_t)e->value < 0)
-            buf->printf("N%lld", -e->value);
+            buf->printf("N"FMTS_I64"", -e->value);
         else
-            buf->printf("i%lld",  e->value);
+            buf->printf("i"FMTS_I64"",  e->value);
     }
 
     void visit(RealExp *e)
diff --git a/src/mars.c b/src/mars.c
index fc64cda..6fc0e28 100644
--- a/src/mars.c
+++ b/src/mars.c
@@ -118,7 +118,7 @@ extern void backend_term();
 
 static void logo()
 {
-    printf("DMD%llu D Compiler %s\n%s %s\n",
+    printf("DMD"FMTS_U64" D Compiler %s\n%s %s\n",
            (unsigned long long) sizeof(size_t) * 8,
         global.version, global.copyright, global.written);
 }
diff --git a/src/module.c b/src/module.c
index 4521de6..036eba9 100644
--- a/src/module.c
+++ b/src/module.c
@@ -284,7 +284,7 @@ bool Module::read(Loc loc)
                 for (size_t i = 0; i < global.path->dim; i++)
                 {
                     const char *p = (*global.path)[i];
-                    fprintf(stderr, "import path[%llu] = %s\n", (ulonglong)i, p);
+                    fprintf(stderr, "import path["FMTS_U64"] = %s\n", (ulonglong)i, p);
                 }
             }
             else
diff --git a/src/mtype.c b/src/mtype.c
index d6f93b3..b9ee1d3 100644
--- a/src/mtype.c
+++ b/src/mtype.c
@@ -2323,7 +2323,7 @@ Identifier *Type::getTypeInfoIdent(int internal)
     char *name = namelen <= sizeof(namebuf) ? namebuf : (char *)malloc(namelen);
     assert(name);
 
-    sprintf(name, "_D%lluTypeInfo_%s6__initZ", (unsigned long long) 9 + len, buf.data);
+    sprintf(name, "_D"FMTS_U64"TypeInfo_%s6__initZ", (unsigned long long) 9 + len, buf.data);
     //printf("%p, deco = %s, name = %s\n", this, deco, name);
     assert(strlen(name) < namelen);     // don't overflow the buffer
 
@@ -3809,7 +3809,7 @@ d_uns64 TypeSArray::size(Loc loc)
     return sz;
 
 Loverflow:
-    error(loc, "index %lld overflow for static array", (long long)sz);
+    error(loc, "index "FMTS_I64" overflow for static array", (long long)sz);
     return SIZE_INVALID;
 }
 
@@ -3894,7 +3894,7 @@ void TypeSArray::resolve(Loc loc, Scope *sc, Expression **pe, Type **pt, Dsymbol
 
             if (d >= td->objects->dim)
             {
-                error(loc, "tuple index %llu exceeds length %u", d, td->objects->dim);
+                error(loc, "tuple index "FMTS_U64" exceeds length %u", d, td->objects->dim);
                 *ps = NULL;
                 *pt = Type::terror;
                 return;
@@ -3965,7 +3965,7 @@ Type *TypeSArray::semantic(Loc loc, Scope *sc)
         uinteger_t d = dim->toUInteger();
 
         if (d >= sd->objects->dim)
-        {   error(loc, "tuple index %llu exceeds %u", d, sd->objects->dim);
+        {   error(loc, "tuple index "FMTS_U64" exceeds %u", d, sd->objects->dim);
             return Type::terror;
         }
         RootObject *o = (*sd->objects)[(size_t)d];
@@ -4031,7 +4031,7 @@ Type *TypeSArray::semantic(Loc loc, Scope *sc)
             if (mulu(tbn->size(loc), d2, overflow) >= 0x1000000 || overflow) // put a 'reasonable' limit on it
             {
               Loverflow:
-                error(loc, "index %llu overflow for static array", (unsigned long long)d1);
+                error(loc, "index "FMTS_U64" overflow for static array", (unsigned long long)d1);
                 goto Lerror;
             }
         }
@@ -4045,7 +4045,7 @@ Type *TypeSArray::semantic(Loc loc, Scope *sc)
             uinteger_t d = dim->toUInteger();
 
             if (d >= tt->arguments->dim)
-            {   error(loc, "tuple index %llu exceeds %u", d, tt->arguments->dim);
+            {   error(loc, "tuple index "FMTS_U64" exceeds %u", d, tt->arguments->dim);
                 goto Lerror;
             }
             Type *telem = (*tt->arguments)[(size_t)d]->type;
@@ -8708,7 +8708,7 @@ Type *TypeSlice::semantic(Loc loc, Scope *sc)
 
     if (!(i1 <= i2 && i2 <= tt->arguments->dim))
     {
-        error(loc, "slice [%llu..%llu] is out of range of [0..%u]", i1, i2, tt->arguments->dim);
+        error(loc, "slice ["FMTS_U64".."FMTS_U64"] is out of range of [0..%u]", i1, i2, tt->arguments->dim);
         return Type::terror;
     }
 
@@ -8760,7 +8760,7 @@ void TypeSlice::resolve(Loc loc, Scope *sc, Expression **pe, Type **pt, Dsymbol
 
             if (!(i1 <= i2 && i2 <= td->objects->dim))
             {
-                error(loc, "slice [%llu..%llu] is out of range of [0..%u]", i1, i2, td->objects->dim);
+                error(loc, "slice ["FMTS_U64".."FMTS_U64"] is out of range of [0..%u]", i1, i2, td->objects->dim);
                 goto Ldefault;
             }
 
diff --git a/src/optimize.c b/src/optimize.c
index 75f9c3a..9aafd1d 100644
--- a/src/optimize.c
+++ b/src/optimize.c
@@ -364,7 +364,7 @@ Expression *Expression_optimize(Expression *e, int result, bool keepLvalue)
                         sinteger_t dim = ts->dim->toInteger();
                         if (index < 0 || index >= dim)
                         {
-                            e->error("array index %lld is out of bounds [0..%lld]", index, dim);
+                            e->error("array index "FMTS_I64" is out of bounds [0.."FMTS_I64"]", index, dim);
                             ret = new ErrorExp();
                             return;
                         }
@@ -630,7 +630,7 @@ Expression *Expression_optimize(Expression *e, int result, bool keepLvalue)
                     d_uns64 sz = e->e1->type->size() * 8;
                     if (i2 < 0 || i2 >= sz)
                     {
-                        e->error("shift assign by %lld is outside the range 0..%llu", i2, (ulonglong)sz - 1);
+                        e->error("shift assign by "FMTS_I64" is outside the range 0.."FMTS_U64"", i2, (ulonglong)sz - 1);
                         ret = new ErrorExp();
                         return;
                     }
@@ -765,7 +765,7 @@ Expression *Expression_optimize(Expression *e, int result, bool keepLvalue)
                 d_uns64 sz = e->e1->type->size() * 8;
                 if (i2 < 0 || i2 >= sz)
                 {
-                    e->error("shift by %lld is outside the range 0..%llu", i2, (ulonglong)sz - 1);
+                    e->error("shift by "FMTS_I64" is outside the range 0.."FMTS_U64"", i2, (ulonglong)sz - 1);
                     ret = new ErrorExp();
                     return;
                 }
diff --git a/src/root/async.c b/src/root/async.c
index 5abdebe..807fb63 100644
--- a/src/root/async.c
+++ b/src/root/async.c
@@ -13,7 +13,7 @@
 #include <stdlib.h>
 #include <assert.h>
 
-#if _WIN32
+#if _WIN32 && !__MINGW32__
 
 #include <windows.h>
 #include <errno.h>
diff --git a/src/root/port.c b/src/root/port.c
index e62082c..2d64956 100644
--- a/src/root/port.c
+++ b/src/root/port.c
@@ -572,9 +572,15 @@ double Port::strtod(const char *p, char **endp)
     return ::strtod(p, endp);
 }
 
+//k8:FIXME
+// from backend/strtold.c, renamed to avoid clash with decl in stdlib.h
+//extern "C" longdouble strtold_dm(const char *p,char **endp);
+
 longdouble Port::strtold(const char *p, char **endp)
 {
-    return ::__mingw_strtold(p, endp);
+    //return ::__mingw_strtold(p, endp);
+    //return ::strtold_dm(p, endp);
+    return ::strtod(p, endp);
 }
 
 #endif
diff --git a/src/root/response.c b/src/root/response.c
index 33d28e9..d75806d 100644
--- a/src/root/response.c
+++ b/src/root/response.c
@@ -29,7 +29,7 @@
 #include <utime.h>
 #endif
 
-#if _MSC_VER
+#if _MSC_VER || __MINGW32__
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <fcntl.h>
diff --git a/src/root/speller.c b/src/root/speller.c
index eba4e9b..757de46 100644
--- a/src/root/speller.c
+++ b/src/root/speller.c
@@ -16,6 +16,9 @@
 #if __sun || _MSC_VER
 #include <alloca.h>
 #endif
+#if __MINGW32__
+#include <malloc.h>
+#endif
 
 #include "speller.h"
 
diff --git a/src/statement.c b/src/statement.c
index d9b25fc..1ed8441 100644
--- a/src/statement.c
+++ b/src/statement.c
@@ -1912,7 +1912,7 @@ Statement *ForeachStatement::semantic(Scope *sc)
                         IntRange dimrange = getIntRange(ta->dim);
                         if (!IntRange::fromType(var->type).contains(dimrange))
                         {
-                            error("index type '%s' cannot cover index range 0..%llu", p->type->toChars(), ta->dim->toInteger());
+                            error("index type '%s' cannot cover index range 0.."FMTS_U64"", p->type->toChars(), ta->dim->toInteger());
                             goto Lerror2;
                         }
                         key->range = new IntRange(SignExtendedNumber(0), dimrange.imax);
@@ -2419,7 +2419,7 @@ Statement *ForeachStatement::semantic(Scope *sc)
                     default:            assert(0);
                 }
                 const char *r = (op == TOKforeach_reverse) ? "R" : "";
-                int j = sprintf(fdname, "_aApply%s%.*s%llu", r, 2, fntab[flag], (ulonglong)dim);
+                int j = sprintf(fdname, "_aApply%s%.*s"FMTS_U64"", r, 2, fntab[flag], (ulonglong)dim);
                 assert(j < BUFFER_LEN);
 
                 FuncDeclaration *fdapply;
@@ -3479,7 +3479,7 @@ Statement *CaseRangeStatement::semantic(Scope *sc)
 
     if (lval - fval > 256)
     {
-        error("had %llu cases which is more than 256 cases in case range", lval - fval);
+        error("had "FMTS_U64" cases which is more than 256 cases in case range", lval - fval);
         errors = true;
         lval = fval + 256;
     }
diff --git a/src/template.c b/src/template.c
index 4cd1799..14da976 100644
--- a/src/template.c
+++ b/src/template.c
@@ -1072,6 +1072,7 @@ public:
         argexps.push(e);
         tparams.push(tparam);
     }
+    virtual ~TypeDeduced () {}
     void update(Expression *e, Type *tparam)
     {
         argexps.push(e);
@@ -7099,10 +7100,10 @@ Identifier *TemplateInstance::genIdent(Objects *args)
     if (!members)
     {
         // Use "__U" for the symbols declared inside template constraint.
-        buf.printf("__U%llu%s", (ulonglong)strlen(id), id);
+        buf.printf("__U"FMTS_U64"%s", (ulonglong)strlen(id), id);
     }
     else
-        buf.printf("__T%llu%s", (ulonglong)strlen(id), id);
+        buf.printf("__T"FMTS_U64"%s", (ulonglong)strlen(id), id);
     size_t nparams = tempdecl->parameters->dim - (tempdecl->isVariadic() ? 1 : 0);
     for (size_t i = 0; i < args->dim; i++)
     {
@@ -7190,7 +7191,7 @@ Identifier *TemplateInstance::genIdent(Objects *args)
              * Unfortunately, fixing this ambiguity will break existing binary
              * compatibility and the demanglers, so we'll leave it as is.
              */
-            buf.printf("%llu%s", (ulonglong)strlen(p), p);
+            buf.printf(""FMTS_U64"%s", (ulonglong)strlen(p), p);
         }
         else if (va)
         {
diff --git a/src/tocsym.c b/src/tocsym.c
index a545c96..264f3e3 100644
--- a/src/tocsym.c
+++ b/src/tocsym.c
@@ -17,6 +17,9 @@
 #if __sun
 #include <alloca.h>
 #endif
+#if __MINGW32__
+#include <malloc.h>
+#endif
 
 #include "mars.h"
 #include "module.h"
@@ -75,7 +78,7 @@ Symbol *toSymbolX(Dsymbol *ds, const char *prefix, int sclass, type *t, const ch
         assert(id);
     }
 
-    int nwritten = sprintf(id,"_D%s%llu%s%s", n, (unsigned long long)prefixlen, prefix, suffix);
+    int nwritten = sprintf(id,"_D%s"FMTS_U64"%s%s", n, (unsigned long long)prefixlen, prefix, suffix);
     assert((unsigned)nwritten < idlen);         // nwritten does not include the terminating 0 char
 
     Symbol *s = symbol_name(id, sclass, t);
diff --git a/src/tocvdebug.c b/src/tocvdebug.c
index b0bb680..3668d83 100644
--- a/src/tocvdebug.c
+++ b/src/tocvdebug.c
@@ -13,6 +13,9 @@
 #include <stddef.h>
 #include <time.h>
 #include <assert.h>
+#if __MINGW32__
+#include <malloc.h>
+#endif
 
 #include "mars.h"
 #include "module.h"
diff --git a/src/tokens.c b/src/tokens.c
index a0502f2..f6a790e 100644
--- a/src/tokens.c
+++ b/src/tokens.c
@@ -70,11 +70,11 @@ const char *Token::toChars()
             break;
 
         case TOKint64v:
-            sprintf(&buffer[0],"%lldL",(longlong)int64value);
+            sprintf(&buffer[0],""FMTS_I64"L",(longlong)int64value);
             break;
 
         case TOKuns64v:
-            sprintf(&buffer[0],"%lluUL",(ulonglong)uns64value);
+            sprintf(&buffer[0],""FMTS_U64"UL",(ulonglong)uns64value);
             break;
 
         case TOKfloat32v:
