[d] Committed merge with upstream dmd

Message ID CABOHX+dc3vRgx2vz2zbNEroheJKyPFd=ufwh6xM2rJQBef+ZWg@mail.gmail.com
State New
Headers show
Series
  • [d] Committed merge with upstream dmd
Related show

Commit Message

Iain Buclaw Nov. 4, 2018, 11:33 p.m.
Hi,

I've merged into the D front-end patches sent to upstream dmd, most
address problems found when building the compiler on OSX and Solaris.

This introduces a new header that pulls in system includes for use
only in the DMD front-end part of the compiler, fixing up uses of
problematic functions that are prevalent throughout the code.

Commits merged from dmd.

    Fix build of the D frontend on the Hurd and KFreeBSD.
    Initial patch from Matthias Klose.
    https://github.com/dlang/dmd/pull/8893

    Don't care about D/C++ compatibility in C++ port.
    Fixes build error in https://gcc.gnu.org/PR87788
    https://github.com/dlang/dmd/pull/8895

    Allow compiling front-end headers with strict warnings.
    https://github.com/dlang/dmd/pull/8909

    Add root/system.h header for wrapping system includes.
    Fixes https://gcc.gnu.org/PR87865
    https://github.com/dlang/dmd/pull/8910

    Move checkedint to dmd/root.
    https://github.com/dlang/dmd/pull/8912

    Use rmem instead of libc for malloc() and strdup().
    https://github.com/dlang/dmd/pull/8913

    Use align(8) for alignment of UnionExp, fixing several BUS errors
    due to alignment issues on SPARC.
    https://github.com/dlang/dmd/pull/8914

    Don't pass NULL pointer as format parameter to errorSupplemental.
    https://github.com/dlang/dmd/pull/8916

-- 
Iain

---
    gcc/d/ChangeLog:

    2018-11-05  Iain Buclaw  <ibuclaw@gdcproject.org>

            PR d/87865
            * d-system.h: New file.
---

Patch

diff --git a/gcc/d/d-system.h b/gcc/d/d-system.h
new file mode 100644
index 00000000000..25a83b675b5
--- /dev/null
+++ b/gcc/d/d-system.h
@@ -0,0 +1,53 @@ 
+/* d-system.h -- DMD frontend inclusion of gcc header files.
+ * Copyright (C) 2018 Free Software Foundation, Inc.
+ *
+ * GCC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3, or (at your option)
+ * any later version.
+ *
+ * GCC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with GCC; see the file COPYING3.  If not see
+ * <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef GCC_D_SYSTEM_H
+#define GCC_D_SYSTEM_H
+
+#include "config.h"
+#include "system.h"
+
+/* Used by the dmd front-end to determine if we have POSIX-style IO.  */
+#define POSIX (__linux__ || __GLIBC__ || __gnu_hurd__ || __APPLE__ \
+	       || __FreeBSD__ || __OpenBSD__ || __DragonFly__ || __sun)
+
+/* Forward assert invariants to gcc_assert.  */
+#undef assert
+#define assert(EXPR) gcc_assert(EXPR)
+
+/* Forward ctype.h macros used by the dmd front-end to safe-ctype.h.  */
+#undef isalpha
+#define isalpha(c) ISALPHA(c)
+#undef isalnum
+#define isalnum(c) ISALNUM(c)
+#undef isdigit
+#define isdigit(c) ISDIGIT(c)
+#undef islower
+#define islower(c) ISLOWER(c)
+#undef isprint
+#define isprint(c) ISPRINT(c)
+#undef isspace
+#define isspace(c) ISSPACE(c)
+#undef isupper
+#define isupper(c) ISUPPER(c)
+#undef isxdigit
+#define isxdigit(c) ISXDIGIT(c)
+#undef tolower
+#define tolower(c) TOLOWER(c)
+
+#endif  /* GCC_D_SYSTEM_H  */
diff --git a/gcc/d/dmd/MERGE b/gcc/d/dmd/MERGE
new file mode 100644
index 00000000000..7727205bed4
--- /dev/null
+++ b/gcc/d/dmd/MERGE
@@ -0,0 +1,4 @@ 
+6243fa6d2ceab4615a9fe21c5bc9484e52bb2d1e
+
+The first line of this file holds the git revision number of the last
+merge done from the dlang/dmd repository.
diff --git a/gcc/d/dmd/access.c b/gcc/d/dmd/access.c
index 37e9c8681d3..cd60cedc2a5 100644
--- a/gcc/d/dmd/access.c
+++ b/gcc/d/dmd/access.c
@@ -7,10 +7,7 @@ 
  * https://github.com/D-Programming-Language/dmd/blob/master/src/access.c
  */
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <assert.h>
-
+#include "root/dsystem.h"
 #include "root/root.h"
 #include "root/rmem.h"
 
diff --git a/gcc/d/dmd/aggregate.h b/gcc/d/dmd/aggregate.h
index d7db82b0f0e..cac0b8efd9f 100644
--- a/gcc/d/dmd/aggregate.h
+++ b/gcc/d/dmd/aggregate.h
@@ -191,7 +191,7 @@  public:
     void semantic(Scope *sc);
     void semanticTypeInfoMembers();
     Dsymbol *search(const Loc &loc, Identifier *ident, int flags = SearchLocalsOnly);
-    const char *kind();
+    const char *kind() const;
     void finalizeSize();
     bool fit(Loc loc, Scope *sc, Expressions *elements, Type *stype);
     bool isPOD();
@@ -205,7 +205,7 @@  class UnionDeclaration : public StructDeclaration
 public:
     UnionDeclaration(Loc loc, Identifier *id);
     Dsymbol *syntaxCopy(Dsymbol *s);
-    const char *kind();
+    const char *kind() const;
 
     UnionDeclaration *isUnionDeclaration() { return this; }
     void accept(Visitor *v) { v->visit(this); }
@@ -306,7 +306,7 @@  public:
     virtual bool isCPPinterface() const;
     bool isAbstract();
     virtual int vtblOffset() const;
-    const char *kind();
+    const char *kind() const;
 
     void addLocalClass(ClassDeclarations *);
 
@@ -326,7 +326,7 @@  public:
     void semantic(Scope *sc);
     bool isBaseOf(ClassDeclaration *cd, int *poffset);
     bool isBaseOf(BaseClass *bc, int *poffset);
-    const char *kind();
+    const char *kind() const;
     int vtblOffset() const;
     bool isCPPinterface() const;
     bool isCOMinterface() const;
diff --git a/gcc/d/dmd/aliasthis.c b/gcc/d/dmd/aliasthis.c
index 50921ecb300..f4d627ff42d 100644
--- a/gcc/d/dmd/aliasthis.c
+++ b/gcc/d/dmd/aliasthis.c
@@ -8,8 +8,7 @@ 
  * https://github.com/D-Programming-Language/dmd/blob/master/src/aliasthis.c
  */
 
-#include <stdio.h>
-#include <assert.h>
+#include "root/dsystem.h"
 
 #include "mars.h"
 #include "identifier.h"
@@ -163,7 +162,7 @@  void AliasThis::semantic(Scope *sc)
     semanticRun = PASSsemanticdone;
 }
 
-const char *AliasThis::kind()
+const char *AliasThis::kind() const
 {
     return "alias this";
 }
diff --git a/gcc/d/dmd/aliasthis.h b/gcc/d/dmd/aliasthis.h
index e5b0280d9e1..290ccd63088 100644
--- a/gcc/d/dmd/aliasthis.h
+++ b/gcc/d/dmd/aliasthis.h
@@ -24,7 +24,7 @@  public:
 
     Dsymbol *syntaxCopy(Dsymbol *);
     void semantic(Scope *sc);
-    const char *kind();
+    const char *kind() const;
     AliasThis *isAliasThis() { return this; }
     void accept(Visitor *v) { v->visit(this); }
 };
diff --git a/gcc/d/dmd/apply.c b/gcc/d/dmd/apply.c
index f20e411228f..bec32a892e2 100644
--- a/gcc/d/dmd/apply.c
+++ b/gcc/d/dmd/apply.c
@@ -8,8 +8,7 @@ 
  * https://github.com/D-Programming-Language/dmd/blob/master/src/apply.c
  */
 
-#include <stdio.h>
-#include <assert.h>
+#include "root/dsystem.h"
 
 #include "mars.h"
 #include "expression.h"
diff --git a/gcc/d/dmd/argtypes.c b/gcc/d/dmd/argtypes.c
index cad8d4ec92f..c37d48a3691 100644
--- a/gcc/d/dmd/argtypes.c
+++ b/gcc/d/dmd/argtypes.c
@@ -8,10 +8,8 @@ 
  * https://github.com/D-Programming-Language/dmd/blob/master/src/argtypes.c
  */
 
-#include <stdio.h>
-#include <assert.h>
-
-#include "checkedint.h"
+#include "root/dsystem.h"
+#include "root/checkedint.h"
 
 #include "mars.h"
 #include "dsymbol.h"
diff --git a/gcc/d/dmd/arrayop.c b/gcc/d/dmd/arrayop.c
index 0ea0d329399..91c1b76e4ab 100644
--- a/gcc/d/dmd/arrayop.c
+++ b/gcc/d/dmd/arrayop.c
@@ -8,10 +8,7 @@ 
  * https://github.com/D-Programming-Language/dmd/blob/master/src/arrayop.c
  */
 
-#include <stdio.h>
-#include <string.h>
-#include <assert.h>
-
+#include "root/dsystem.h"
 #include "root/rmem.h"
 #include "root/aav.h"
 
diff --git a/gcc/d/dmd/attrib.c b/gcc/d/dmd/attrib.c
index c4270ea63e6..05728518ff1 100644
--- a/gcc/d/dmd/attrib.c
+++ b/gcc/d/dmd/attrib.c
@@ -8,11 +8,7 @@ 
  * https://github.com/D-Programming-Language/dmd/blob/master/src/attrib.c
  */
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <assert.h>
-#include <string.h>                     // memcpy()
-
+#include "root/dsystem.h"               // memcmp()
 #include "root/rmem.h"
 
 #include "mars.h"
diff --git a/gcc/d/dmd/attrib.h b/gcc/d/dmd/attrib.h
index 83486a95431..a491605fc13 100644
--- a/gcc/d/dmd/attrib.h
+++ b/gcc/d/dmd/attrib.h
@@ -212,10 +212,10 @@  public:
     Dsymbols *cache;
 
     Dsymbol *syntaxCopy(Dsymbol *s);
-    bool oneMember(Dsymbol *ps, Identifier *ident);
+    bool oneMember(Dsymbol **ps, Identifier *ident);
     Dsymbols *include(Scope *sc, ScopeDsymbol *sds);
     void addMember(Scope *sc, ScopeDsymbol *sds);
-    void addComment(const char *comment);
+    void addComment(const utf8_t *comment);
     void setScope(Scope *sc);
     void importAll(Scope *sc);
     void semantic(Scope *sc);
diff --git a/gcc/d/dmd/canthrow.c b/gcc/d/dmd/canthrow.c
index a00741b7d72..ec15153eb4e 100644
--- a/gcc/d/dmd/canthrow.c
+++ b/gcc/d/dmd/canthrow.c
@@ -8,8 +8,7 @@ 
  * https://github.com/D-Programming-Language/dmd/blob/master/src/canthrow.c
  */
 
-#include <stdio.h>
-#include <assert.h>
+#include "root/dsystem.h"
 
 #include "mars.h"
 #include "init.h"
diff --git a/gcc/d/dmd/clone.c b/gcc/d/dmd/clone.c
index 9105d114eaa..4384dc8064f 100644
--- a/gcc/d/dmd/clone.c
+++ b/gcc/d/dmd/clone.c
@@ -8,11 +8,9 @@ 
  * https://github.com/D-Programming-Language/dmd/blob/master/src/clone.c
  */
 
-#include <stdio.h>
-#include <assert.h>
-#include <new>
-
+#include "root/dsystem.h"
 #include "root/root.h"
+
 #include "aggregate.h"
 #include "scope.h"
 #include "mtype.h"
diff --git a/gcc/d/dmd/compiler.h b/gcc/d/dmd/compiler.h
index cfcc317ff9c..6c5cf7bd474 100644
--- a/gcc/d/dmd/compiler.h
+++ b/gcc/d/dmd/compiler.h
@@ -10,6 +10,8 @@ 
 
 #pragma once
 
+#include "root/array.h"
+
 // This file contains a data structure that describes a back-end compiler
 // and implements compiler-specific actions.
 
@@ -18,6 +20,11 @@  class Module;
 class Type;
 struct Scope;
 
+// DMD-generated module `__entrypoint` where the C main resides
+extern Module *entrypoint;
+// Module in which the D main is
+extern Module *rootHasMain;
+
 struct Compiler
 {
     // CTFE support for cross-compilation.
diff --git a/gcc/d/dmd/cond.c b/gcc/d/dmd/cond.c
index 047eca261d8..0538929f310 100644
--- a/gcc/d/dmd/cond.c
+++ b/gcc/d/dmd/cond.c
@@ -8,9 +8,7 @@ 
  * https://github.com/D-Programming-Language/dmd/blob/master/src/cond.c
  */
 
-#include <stdio.h>
-#include <assert.h>
-#include <string.h>                     // strcmp()
+#include "root/dsystem.h"               // strcmp()
 
 #include "mars.h"
 #include "id.h"
diff --git a/gcc/d/dmd/constfold.c b/gcc/d/dmd/constfold.c
index 43e831f1dfe..4b5dceba62e 100644
--- a/gcc/d/dmd/constfold.c
+++ b/gcc/d/dmd/constfold.c
@@ -8,12 +8,11 @@ 
  * https://github.com/D-Programming-Language/dmd/blob/master/src/constfold.c
  */
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <assert.h>
-#include <string.h>                     // mem{cpy|set|cmp}()
+#include "root/dsystem.h"               // mem{cpy|set|cmp}()
+
+#ifndef IN_GCC
 #include <math.h>
-#include <new>
+#endif
 
 #include "root/rmem.h"
 #include "root/root.h"
@@ -481,13 +480,21 @@  UnionExp Mod(Loc loc, Type *type, Expression *e1, Expression *e2)
         {
             real_t r2 = e2->toReal();
 
+#ifdef IN_GCC
             c = complex_t(e1->toReal() % r2, e1->toImaginary() % r2);
+#else
+            c = complex_t(::fmodl(e1->toReal(), r2), ::fmodl(e1->toImaginary(), r2));
+#endif
         }
         else if (e2->type->isimaginary())
         {
             real_t i2 = e2->toImaginary();
 
+#ifdef IN_GCC
             c = complex_t(e1->toReal() % i2, e1->toImaginary() % i2);
+#else
+            c = complex_t(::fmodl(e1->toReal(), i2), ::fmodl(e1->toImaginary(), i2));
+#endif
         }
         else
             assert(0);
diff --git a/gcc/d/dmd/cppmangle.c b/gcc/d/dmd/cppmangle.c
index bb919a5f2bb..d2d357667cb 100644
--- a/gcc/d/dmd/cppmangle.c
+++ b/gcc/d/dmd/cppmangle.c
@@ -21,9 +21,7 @@ 
  *  enter `C++, mangling` as the keywords.
  */
 
-#include <stdio.h>
-#include <string.h>
-#include <assert.h>
+#include "root/dsystem.h"
 
 #include "mars.h"
 #include "dsymbol.h"
@@ -47,9 +45,9 @@  class CppMangleVisitor : public Visitor
 {
     Objects components;         // array of components available for substitution
     OutBuffer *buf;             // append the mangling to buf[]
+  public:
     Loc loc;                    // location for use in error messages
 
-    public:
     // Write <seq-id> to buf
     void write_seq_id(size_t i)
     {
diff --git a/gcc/d/dmd/ctfeexpr.c b/gcc/d/dmd/ctfeexpr.c
index ad5b827dcd3..430342dae54 100644
--- a/gcc/d/dmd/ctfeexpr.c
+++ b/gcc/d/dmd/ctfeexpr.c
@@ -8,12 +8,7 @@ 
  * https://github.com/D-Programming-Language/dmd/blob/master/src/ctfeexpr.c
  */
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <assert.h>
-#include <string.h>                     // mem{cpy|set}()
-#include <new>
-
+#include "root/dsystem.h"               // mem{cpy|set}()
 #include "root/rmem.h"
 
 #include "mars.h"
diff --git a/gcc/d/dmd/dcast.c b/gcc/d/dmd/dcast.c
index ee3bfd9d888..39471665e4a 100644
--- a/gcc/d/dmd/dcast.c
+++ b/gcc/d/dmd/dcast.c
@@ -8,10 +8,7 @@ 
  * https://github.com/D-Programming-Language/dmd/blob/master/src/cast.c
  */
 
-#include <stdio.h>
-#include <assert.h>
-#include <string.h>                     // mem{set|cpy}()
-
+#include "root/dsystem.h"               // mem{set|cpy}()
 #include "root/rmem.h"
 
 #include "mars.h"
diff --git a/gcc/d/dmd/dclass.c b/gcc/d/dmd/dclass.c
index 414332ccd34..6fe17b36576 100644
--- a/gcc/d/dmd/dclass.c
+++ b/gcc/d/dmd/dclass.c
@@ -8,11 +8,7 @@ 
  * https://github.com/D-Programming-Language/dmd/blob/master/src/class.c
  */
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <assert.h>
-#include <string.h>                     // mem{cpy|set}()
-
+#include "root/dsystem.h"               // mem{cpy|set}()
 #include "root/root.h"
 #include "root/rmem.h"
 
@@ -1389,7 +1385,7 @@  int ClassDeclaration::vtblOffset() const
 /****************************************
  */
 
-const char *ClassDeclaration::kind()
+const char *ClassDeclaration::kind() const
 {
     return "class";
 }
@@ -1827,7 +1823,7 @@  bool InterfaceDeclaration::isCPPinterface() const
 /*******************************************
  */
 
-const char *InterfaceDeclaration::kind()
+const char *InterfaceDeclaration::kind() const
 {
     return "interface";
 }
diff --git a/gcc/d/dmd/declaration.c b/gcc/d/dmd/declaration.c
index 76132b9458e..9f43b88dd0f 100644
--- a/gcc/d/dmd/declaration.c
+++ b/gcc/d/dmd/declaration.c
@@ -8,10 +8,8 @@ 
  * https://github.com/D-Programming-Language/dmd/blob/master/src/declaration.c
  */
 
-#include <stdio.h>
-#include <assert.h>
-
-#include "checkedint.h"
+#include "root/dsystem.h"
+#include "root/checkedint.h"
 
 #include "errors.h"
 #include "init.h"
@@ -87,7 +85,7 @@  void Declaration::semantic(Scope *)
 {
 }
 
-const char *Declaration::kind()
+const char *Declaration::kind() const
 {
     return "declaration";
 }
@@ -188,7 +186,7 @@  Dsymbol *TupleDeclaration::syntaxCopy(Dsymbol *)
     return NULL;
 }
 
-const char *TupleDeclaration::kind()
+const char *TupleDeclaration::kind() const
 {
     return "tuple";
 }
@@ -575,7 +573,7 @@  bool AliasDeclaration::overloadInsert(Dsymbol *s)
     return true;
 }
 
-const char *AliasDeclaration::kind()
+const char *AliasDeclaration::kind() const
 {
     return "alias";
 }
@@ -705,7 +703,7 @@  OverDeclaration::OverDeclaration(Identifier *ident, Dsymbol *s, bool hasOverload
     }
 }
 
-const char *OverDeclaration::kind()
+const char *OverDeclaration::kind() const
 {
     return "overload alias";    // todo
 }
@@ -1770,7 +1768,7 @@  void VarDeclaration::setFieldOffset(AggregateDeclaration *ad, unsigned *poffset,
     //printf(" addField '%s' to '%s' at offset %d, size = %d\n", toChars(), ad->toChars(), offset, memsize);
 }
 
-const char *VarDeclaration::kind()
+const char *VarDeclaration::kind() const
 {
     return "variable";
 }
diff --git a/gcc/d/dmd/declaration.h b/gcc/d/dmd/declaration.h
index 071ce2ca206..6952bffb38a 100644
--- a/gcc/d/dmd/declaration.h
+++ b/gcc/d/dmd/declaration.h
@@ -127,7 +127,7 @@  public:
 
     Declaration(Identifier *id);
     void semantic(Scope *sc);
-    const char *kind();
+    const char *kind() const;
     d_uns64 size(Loc loc);
     int checkModify(Loc loc, Scope *sc, Type *t, Expression *e1, int flag);
 
@@ -177,7 +177,7 @@  public:
 
     TupleDeclaration(Loc loc, Identifier *ident, Objects *objects);
     Dsymbol *syntaxCopy(Dsymbol *);
-    const char *kind();
+    const char *kind() const;
     Type *getType();
     Dsymbol *toAlias2();
     bool needThis();
@@ -202,7 +202,7 @@  public:
     void semantic(Scope *sc);
     void aliasSemantic(Scope *sc);
     bool overloadInsert(Dsymbol *s);
-    const char *kind();
+    const char *kind() const;
     Type *getType();
     Dsymbol *toAlias();
     Dsymbol *toAlias2();
@@ -222,7 +222,7 @@  public:
     bool hasOverloads;
 
     OverDeclaration(Identifier *ident, Dsymbol *s, bool hasOverloads = true);
-    const char *kind();
+    const char *kind() const;
     void semantic(Scope *sc);
     bool equals(RootObject *o);
     bool overloadInsert(Dsymbol *s);
@@ -269,7 +269,7 @@  public:
     void semantic(Scope *sc);
     void setFieldOffset(AggregateDeclaration *ad, unsigned *poffset, bool isunion);
     void semantic2(Scope *sc);
-    const char *kind();
+    const char *kind() const;
     AggregateDeclaration *isThis();
     bool needThis();
     bool isExport() const;
@@ -654,7 +654,7 @@  public:
     virtual bool isFinalFunc();
     virtual bool addPreInvariant();
     virtual bool addPostInvariant();
-    const char *kind();
+    const char *kind() const;
     FuncDeclaration *isUnique();
     bool checkNestedReference(Scope *sc, Loc loc);
     bool needsClosure();
@@ -690,7 +690,7 @@  public:
     FuncAliasDeclaration(Identifier *ident, FuncDeclaration *funcalias, bool hasOverloads = true);
 
     FuncAliasDeclaration *isFuncAliasDeclaration() { return this; }
-    const char *kind();
+    const char *kind() const;
 
     FuncDeclaration *toAliasFunc();
     void accept(Visitor *v) { v->visit(this); }
@@ -717,7 +717,7 @@  public:
     void modifyReturns(Scope *sc, Type *tret);
 
     FuncLiteralDeclaration *isFuncLiteralDeclaration() { return this; }
-    const char *kind();
+    const char *kind() const;
     const char *toPrettyChars(bool QualifyTypes = false);
     void accept(Visitor *v) { v->visit(this); }
 };
@@ -728,7 +728,7 @@  public:
     CtorDeclaration(Loc loc, Loc endloc, StorageClass stc, Type *type);
     Dsymbol *syntaxCopy(Dsymbol *);
     void semantic(Scope *sc);
-    const char *kind();
+    const char *kind() const;
     const char *toChars();
     bool isVirtual();
     bool addPreInvariant();
@@ -760,7 +760,7 @@  public:
     DtorDeclaration(Loc loc, Loc endloc, StorageClass stc, Identifier *id);
     Dsymbol *syntaxCopy(Dsymbol *);
     void semantic(Scope *sc);
-    const char *kind();
+    const char *kind() const;
     const char *toChars();
     bool isVirtual();
     bool addPreInvariant();
@@ -870,7 +870,7 @@  public:
     NewDeclaration(Loc loc, Loc endloc, StorageClass stc, Parameters *arguments, int varargs);
     Dsymbol *syntaxCopy(Dsymbol *);
     void semantic(Scope *sc);
-    const char *kind();
+    const char *kind() const;
     bool isVirtual();
     bool addPreInvariant();
     bool addPostInvariant();
@@ -888,7 +888,7 @@  public:
     DeleteDeclaration(Loc loc, Loc endloc, StorageClass stc, Parameters *arguments);
     Dsymbol *syntaxCopy(Dsymbol *);
     void semantic(Scope *sc);
-    const char *kind();
+    const char *kind() const;
     bool isDelete();
     bool isVirtual();
     bool addPreInvariant();
diff --git a/gcc/d/dmd/delegatize.c b/gcc/d/dmd/delegatize.c
index 99fe93828a6..8745026252e 100644
--- a/gcc/d/dmd/delegatize.c
+++ b/gcc/d/dmd/delegatize.c
@@ -8,8 +8,7 @@ 
  * https://github.com/D-Programming-Language/dmd/blob/master/src/delegatize.c
  */
 
-#include <stdio.h>
-#include <assert.h>
+#include "root/dsystem.h"
 
 #include "mars.h"
 #include "expression.h"
diff --git a/gcc/d/dmd/denum.c b/gcc/d/dmd/denum.c
index 4107f11738e..56652f219c9 100644
--- a/gcc/d/dmd/denum.c
+++ b/gcc/d/dmd/denum.c
@@ -8,10 +8,9 @@ 
  * https://github.com/D-Programming-Language/dmd/blob/master/src/enum.c
  */
 
-#include <stdio.h>
-#include <assert.h>
-
+#include "root/dsystem.h"
 #include "root/root.h"
+
 #include "errors.h"
 #include "enum.h"
 #include "mtype.h"
@@ -457,7 +456,7 @@  Type *EnumDeclaration::getType()
     return type;
 }
 
-const char *EnumDeclaration::kind()
+const char *EnumDeclaration::kind() const
 {
     return "enum";
 }
@@ -515,7 +514,7 @@  Dsymbol *EnumMember::syntaxCopy(Dsymbol *s)
         origType ? origType->syntaxCopy() : NULL);
 }
 
-const char *EnumMember::kind()
+const char *EnumMember::kind() const
 {
     return "enum member";
 }
diff --git a/gcc/d/dmd/dimport.c b/gcc/d/dmd/dimport.c
index 03b3d1f2c57..922e18a3fe9 100644
--- a/gcc/d/dmd/dimport.c
+++ b/gcc/d/dmd/dimport.c
@@ -8,10 +8,9 @@ 
  * https://github.com/D-Programming-Language/dmd/blob/master/src/import.c
  */
 
-#include <stdio.h>
-#include <assert.h>
-
+#include "root/dsystem.h"
 #include "root/root.h"
+
 #include "mars.h"
 #include "dsymbol.h"
 #include "import.h"
@@ -70,7 +69,7 @@  void Import::addAlias(Identifier *name, Identifier *alias)
     aliases.push(alias);
 }
 
-const char *Import::kind()
+const char *Import::kind() const
 {
     return isstatic ? "static import" : "import";
 }
diff --git a/gcc/d/dmd/dinterpret.c b/gcc/d/dmd/dinterpret.c
index 54bf00d5a41..2bb1aace2ca 100644
--- a/gcc/d/dmd/dinterpret.c
+++ b/gcc/d/dmd/dinterpret.c
@@ -8,12 +8,7 @@ 
  * https://github.com/D-Programming-Language/dmd/blob/master/src/interpret.c
  */
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <assert.h>
-#include <string.h>                     // mem{cpy|set}()
-#include <new>
-
+#include "root/dsystem.h"               // mem{cpy|set}()
 #include "root/rmem.h"
 
 #include "mars.h"
diff --git a/gcc/d/dmd/dmacro.c b/gcc/d/dmd/dmacro.c
index a5d8da10c36..c6171229fd9 100644
--- a/gcc/d/dmd/dmacro.c
+++ b/gcc/d/dmd/dmacro.c
@@ -11,11 +11,7 @@ 
 /* Simple macro text processor.
  */
 
-#include <stdio.h>
-#include <string.h>
-#include <time.h>
-#include <ctype.h>
-#include <assert.h>
+#include "root/dsystem.h"
 
 #include "mars.h"
 #include "errors.h"
diff --git a/gcc/d/dmd/dmangle.c b/gcc/d/dmd/dmangle.c
index 9734624b288..7686c69c9fe 100644
--- a/gcc/d/dmd/dmangle.c
+++ b/gcc/d/dmd/dmangle.c
@@ -8,11 +8,7 @@ 
  * https://github.com/D-Programming-Language/dmd/blob/master/src/mangle.c
  */
 
-#include <stdio.h>
-#include <string.h>
-#include <ctype.h>
-#include <assert.h>
-
+#include "root/dsystem.h"
 #include "root/root.h"
 
 #include "mangle.h"
diff --git a/gcc/d/dmd/dmodule.c b/gcc/d/dmd/dmodule.c
index a374aac73a7..55b54b1cee6 100644
--- a/gcc/d/dmd/dmodule.c
+++ b/gcc/d/dmd/dmodule.c
@@ -8,9 +8,8 @@ 
  * https://github.com/D-Programming-Language/dmd/blob/master/src/module.c
  */
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <assert.h>
+#include "root/dsystem.h"
+#include "root/rmem.h"
 
 #include "mars.h"
 #include "module.h"
@@ -24,14 +23,6 @@ 
 #include "lexer.h"
 #include "attrib.h"
 
-// For getcwd()
-#if _WIN32
-#include <direct.h>
-#endif
-#if POSIX
-#include <unistd.h>
-#endif
-
 AggregateDeclaration *Module::moduleinfo;
 
 Module *Module::rootModule;
@@ -193,7 +184,7 @@  void Module::deleteObjFile()
         docfile->remove();
 }
 
-const char *Module::kind()
+const char *Module::kind() const
 {
     return "module";
 }
@@ -310,7 +301,8 @@  bool Module::read(Loc loc)
         {
             ::error(loc, "cannot find source code for runtime library file 'object.d'");
             errorSupplemental(loc, "dmd might not be correctly installed. Run 'dmd -man' for installation instructions.");
-            errorSupplemental(loc, "config file: %s", FileName::canonicalName(global.inifilename));
+            const char *dmdConfFile = FileName::canonicalName(global.inifilename);
+            errorSupplemental(loc, "config file: %s", dmdConfFile ? dmdConfFile : "not found");
         }
         else
         {
@@ -1043,8 +1035,7 @@  void Module::runDeferredSemantic()
         }
         else
         {
-            todo = (Dsymbol **)malloc(len * sizeof(Dsymbol *));
-            assert(todo);
+            todo = (Dsymbol **)mem.xmalloc(len * sizeof(Dsymbol *));
             todoalloc = todo;
         }
         memcpy(todo, deferred.tdata(), len * sizeof(Dsymbol *));
@@ -1219,7 +1210,7 @@  Package::Package(Identifier *ident)
 }
 
 
-const char *Package::kind()
+const char *Package::kind() const
 {
     return "package";
 }
diff --git a/gcc/d/dmd/doc.c b/gcc/d/dmd/doc.c
index 92ce33cd546..d35ca7b2522 100644
--- a/gcc/d/dmd/doc.c
+++ b/gcc/d/dmd/doc.c
@@ -10,12 +10,7 @@ 
 
 // This implements the Ddoc capability.
 
-#include <stdio.h>
-#include <string.h>
-#include <time.h>
-#include <ctype.h>
-#include <assert.h>
-
+#include "root/dsystem.h"
 #include "root/rmem.h"
 #include "root/root.h"
 #include "root/port.h"
diff --git a/gcc/d/dmd/doc.h b/gcc/d/dmd/doc.h
index 7f3ef5127de..d1428fb8a69 100644
--- a/gcc/d/dmd/doc.h
+++ b/gcc/d/dmd/doc.h
@@ -10,5 +10,10 @@ 
 
 #pragma once
 
+#include "root/dsystem.h"
+
+class Module;
+struct OutBuffer;
+
 void escapeDdocString(OutBuffer *buf, size_t start);
 void gendocfile(Module *m);
diff --git a/gcc/d/dmd/dscope.c b/gcc/d/dmd/dscope.c
index 924a3764c00..27412b53d13 100644
--- a/gcc/d/dmd/dscope.c
+++ b/gcc/d/dmd/dscope.c
@@ -8,10 +8,7 @@ 
  * https://github.com/D-Programming-Language/dmd/blob/master/src/scope.c
  */
 
-#include <stdio.h>
-#include <assert.h>
-#include <string.h>                     // strlen()
-
+#include "root/dsystem.h"               // strlen()
 #include "root/root.h"
 #include "root/rmem.h"
 #include "root/speller.h"
diff --git a/gcc/d/dmd/dstruct.c b/gcc/d/dmd/dstruct.c
index c99abcea9aa..f9f15ba9092 100644
--- a/gcc/d/dmd/dstruct.c
+++ b/gcc/d/dmd/dstruct.c
@@ -8,10 +8,9 @@ 
  * https://github.com/D-Programming-Language/dmd/blob/master/src/struct.c
  */
 
-#include <stdio.h>
-#include <assert.h>
-
+#include "root/dsystem.h"
 #include "root/root.h"
+
 #include "errors.h"
 #include "aggregate.h"
 #include "scope.h"
@@ -1438,7 +1437,7 @@  bool StructDeclaration::isPOD()
     return (ispod == ISPODyes);
 }
 
-const char *StructDeclaration::kind()
+const char *StructDeclaration::kind() const
 {
     return "struct";
 }
@@ -1457,7 +1456,7 @@  Dsymbol *UnionDeclaration::syntaxCopy(Dsymbol *s)
     return StructDeclaration::syntaxCopy(ud);
 }
 
-const char *UnionDeclaration::kind()
+const char *UnionDeclaration::kind() const
 {
     return "union";
 }
diff --git a/gcc/d/dmd/dsymbol.c b/gcc/d/dmd/dsymbol.c
index 0f0a0dc6904..b511b79c7e0 100644
--- a/gcc/d/dmd/dsymbol.c
+++ b/gcc/d/dmd/dsymbol.c
@@ -8,11 +8,7 @@ 
  * https://github.com/D-Programming-Language/dmd/blob/master/src/dsymbol.c
  */
 
-#include <stdio.h>
-#include <string.h>
-#include <assert.h>
-#include <limits.h>
-
+#include "root/dsystem.h"
 #include "root/rmem.h"
 #include "root/speller.h"
 #include "root/aav.h"
@@ -241,9 +237,7 @@  const char *Dsymbol::toPrettyChars(bool QualifyTypes)
         ++complength;
 
     // Allocate temporary array comp[]
-    const char **comp = (const char **)malloc(complength * sizeof(char**));
-    if (!comp)
-        Mem::error();
+    const char **comp = (const char **)mem.xmalloc(complength * sizeof(char**));
 
     // Fill in comp[] and compute length of final result
     size_t length = 0;
@@ -293,7 +287,7 @@  const char *Dsymbol::locToChars()
     return getLoc().toChars();
 }
 
-const char *Dsymbol::kind()
+const char *Dsymbol::kind() const
 {
     return "symbol";
 }
@@ -921,7 +915,7 @@  void OverloadSet::push(Dsymbol *s)
     a.push(s);
 }
 
-const char *OverloadSet::kind()
+const char *OverloadSet::kind() const
 {
     return "overloadset";
 }
@@ -1273,7 +1267,7 @@  void ScopeDsymbol::multiplyDefined(Loc loc, Dsymbol *s1, Dsymbol *s2)
     }
 }
 
-const char *ScopeDsymbol::kind()
+const char *ScopeDsymbol::kind() const
 {
     return "ScopeDsymbol";
 }
diff --git a/gcc/d/dmd/dsymbol.h b/gcc/d/dmd/dsymbol.h
index 0f2f03de4f1..421a043c8a2 100644
--- a/gcc/d/dmd/dsymbol.h
+++ b/gcc/d/dmd/dsymbol.h
@@ -193,7 +193,7 @@  public:
 
     virtual Identifier *getIdent();
     virtual const char *toPrettyChars(bool QualifyTypes = false);
-    virtual const char *kind();
+    virtual const char *kind() const;
     virtual Dsymbol *toAlias();                 // resolve real symbol
     virtual Dsymbol *toAlias2();
     virtual int apply(Dsymbol_apply_ft_t fp, void *param);
@@ -309,7 +309,7 @@  public:
     virtual bool isPackageAccessible(Package *p, Prot protection, int flags = 0);
     bool isforwardRef();
     static void multiplyDefined(Loc loc, Dsymbol *s1, Dsymbol *s2);
-    const char *kind();
+    const char *kind() const;
     FuncDeclaration *findGetMembers();
     virtual Dsymbol *symtabInsert(Dsymbol *s);
     virtual Dsymbol *symtabLookup(Dsymbol *s, Identifier *id);
@@ -366,7 +366,7 @@  public:
     OverloadSet(Identifier *ident, OverloadSet *os = NULL);
     void push(Dsymbol *s);
     OverloadSet *isOverloadSet() { return this; }
-    const char *kind();
+    const char *kind() const;
     void accept(Visitor *v) { v->visit(this); }
 };
 
@@ -380,7 +380,7 @@  class ForwardingScopeDsymbol : public ScopeDsymbol
     Dsymbol *symtabLookup(Dsymbol *s, Identifier *id);
     void importScope(Dsymbol *s, Prot protection);
     void semantic(Scope *sc);
-    const char *kind();
+    const char *kind() const;
 
     ForwardingScopeDsymbol *isForwardingScopeDsymbol() { return this; }
 };
diff --git a/gcc/d/dmd/dtemplate.c b/gcc/d/dmd/dtemplate.c
index f2b3b2f7b21..9455e976074 100644
--- a/gcc/d/dmd/dtemplate.c
+++ b/gcc/d/dmd/dtemplate.c
@@ -10,9 +10,7 @@ 
 
 // Handle template implementation
 
-#include <stdio.h>
-#include <assert.h>
-
+#include "root/dsystem.h"
 #include "root/root.h"
 #include "root/aav.h"
 #include "root/rmem.h"
@@ -689,7 +687,7 @@  void TemplateDeclaration::semantic(Scope *sc)
      */
 }
 
-const char *TemplateDeclaration::kind()
+const char *TemplateDeclaration::kind() const
 {
     return (onemember && onemember->isAggregateDeclaration())
                 ? onemember->kind()
@@ -7844,7 +7842,7 @@  Dsymbol *TemplateInstance::toAlias()
     return inst;
 }
 
-const char *TemplateInstance::kind()
+const char *TemplateInstance::kind() const
 {
     return "template instance";
 }
@@ -8530,7 +8528,7 @@  void TemplateMixin::semantic3(Scope *sc)
     }
 }
 
-const char *TemplateMixin::kind()
+const char *TemplateMixin::kind() const
 {
     return "mixin";
 }
diff --git a/gcc/d/dmd/dversion.c b/gcc/d/dmd/dversion.c
index 47374868674..849afaaa6a8 100644
--- a/gcc/d/dmd/dversion.c
+++ b/gcc/d/dmd/dversion.c
@@ -8,9 +8,7 @@ 
  * https://github.com/D-Programming-Language/dmd/blob/master/src/version.c
  */
 
-#include <stdio.h>
-#include <assert.h>
-
+#include "root/dsystem.h"
 #include "root/root.h"
 
 #include "identifier.h"
@@ -106,7 +104,7 @@  void DebugSymbol::semantic(Scope *)
         semanticRun = PASSsemanticdone;
 }
 
-const char *DebugSymbol::kind()
+const char *DebugSymbol::kind() const
 {
     return "debug";
 }
@@ -196,7 +194,7 @@  void VersionSymbol::semantic(Scope *)
         semanticRun = PASSsemanticdone;
 }
 
-const char *VersionSymbol::kind()
+const char *VersionSymbol::kind() const
 {
     return "version";
 }
diff --git a/gcc/d/dmd/entity.c b/gcc/d/dmd/entity.c
index 38fd2d8713e..78abc8fad97 100644
--- a/gcc/d/dmd/entity.c
+++ b/gcc/d/dmd/entity.c
@@ -8,9 +8,7 @@ 
  * https://github.com/D-Programming-Language/dmd/blob/master/src/entity.c
  */
 
-#include <string.h>
-#include <ctype.h>
-
+#include "root/dsystem.h"
 #include "root/port.h"
 
 /*********************************************
diff --git a/gcc/d/dmd/enum.h b/gcc/d/dmd/enum.h
index 072eacf583e..ce500579e17 100644
--- a/gcc/d/dmd/enum.h
+++ b/gcc/d/dmd/enum.h
@@ -50,7 +50,7 @@  public:
     void semantic(Scope *sc);
     bool oneMember(Dsymbol **ps, Identifier *ident);
     Type *getType();
-    const char *kind();
+    const char *kind() const;
     Dsymbol *search(const Loc &loc, Identifier *ident, int flags = SearchLocalsOnly);
     bool isDeprecated();                // is Dsymbol deprecated?
     Prot prot();
@@ -86,7 +86,7 @@  public:
 
     EnumMember(Loc loc, Identifier *id, Expression *value, Type *origType);
     Dsymbol *syntaxCopy(Dsymbol *s);
-    const char *kind();
+    const char *kind() const;
     void semantic(Scope *sc);
     Expression *getVarExp(Loc loc, Scope *sc);
 
diff --git a/gcc/d/dmd/errors.h b/gcc/d/dmd/errors.h
index 7b3ebc0deeb..42cc1d70eea 100644
--- a/gcc/d/dmd/errors.h
+++ b/gcc/d/dmd/errors.h
@@ -10,6 +10,7 @@ 
 
 #pragma once
 
+#include "root/dsystem.h"
 #include "globals.h"
 
 bool isConsoleColorSupported();
diff --git a/gcc/d/dmd/expression.c b/gcc/d/dmd/expression.c
index dc0d8e7c017..359e4cc91fd 100644
--- a/gcc/d/dmd/expression.c
+++ b/gcc/d/dmd/expression.c
@@ -8,12 +8,7 @@ 
  * https://github.com/D-Programming-Language/dmd/blob/master/src/expression.c
  */
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <ctype.h>
-#include <math.h>
-#include <assert.h>
-
+#include "root/dsystem.h"
 #include "root/rmem.h"
 #include "root/root.h"
 
diff --git a/gcc/d/dmd/expression.h b/gcc/d/dmd/expression.h
index 57c516c1d23..27a153eaaf1 100644
--- a/gcc/d/dmd/expression.h
+++ b/gcc/d/dmd/expression.h
@@ -1490,6 +1490,14 @@  struct UnionExp
     Expression *copy();
 
 private:
+    // Ensure that the union is suitably aligned.
+#if defined(__GNUC__) || defined(__clang__)
+    __attribute__((aligned(8)))
+#elif defined(_MSC_VER)
+    __declspec(align(8))
+#elif defined(__DMC__)
+    #pragma pack(8)
+#endif
     union
     {
         char exp       [sizeof(Expression)];
@@ -1507,10 +1515,10 @@  private:
         char addrexp   [sizeof(AddrExp)];
         char indexexp  [sizeof(IndexExp)];
         char sliceexp  [sizeof(SliceExp)];
-
-        // Ensure that the union is suitably aligned.
-        real_t for_alignment_only;
     } u;
+#if defined(__DMC__)
+    #pragma pack()
+#endif
 };
 
 /****************************************************************/
diff --git a/gcc/d/dmd/expressionsem.c b/gcc/d/dmd/expressionsem.c
index 247d1433f4b..96ae7efd7e3 100644
--- a/gcc/d/dmd/expressionsem.c
+++ b/gcc/d/dmd/expressionsem.c
@@ -7,12 +7,7 @@ 
  * http://www.boost.org/LICENSE_1_0.txt
  */
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <ctype.h>
-#include <math.h>
-#include <assert.h>
-
+#include "root/dsystem.h"
 #include "root/rmem.h"
 #include "root/root.h"
 
diff --git a/gcc/d/dmd/func.c b/gcc/d/dmd/func.c
index 2feea9126cd..c8f9c5c350a 100644
--- a/gcc/d/dmd/func.c
+++ b/gcc/d/dmd/func.c
@@ -8,8 +8,7 @@ 
  * https://github.com/D-Programming-Language/dmd/blob/master/src/func.c
  */
 
-#include <stdio.h>
-#include <assert.h>
+#include "root/dsystem.h"
 
 #include "mars.h"
 #include "init.h"
@@ -4254,7 +4253,7 @@  void FuncDeclaration::checkDmain()
         error("parameters must be main() or main(string[] args)");
 }
 
-const char *FuncDeclaration::kind()
+const char *FuncDeclaration::kind() const
 {
     return generated ? "generated function" : "function";
 }
@@ -4645,7 +4644,7 @@  FuncAliasDeclaration::FuncAliasDeclaration(Identifier *ident, FuncDeclaration *f
     userAttribDecl = funcalias->userAttribDecl;
 }
 
-const char *FuncAliasDeclaration::kind()
+const char *FuncAliasDeclaration::kind() const
 {
     return "function alias";
 }
@@ -4756,7 +4755,7 @@  void FuncLiteralDeclaration::modifyReturns(Scope *sc, Type *tret)
         ((TypeFunction *)type)->next = tret;
 }
 
-const char *FuncLiteralDeclaration::kind()
+const char *FuncLiteralDeclaration::kind() const
 {
     return (tok != TOKfunction) ? "delegate" : "function";
 }
@@ -4869,7 +4868,7 @@  void CtorDeclaration::semantic(Scope *sc)
     }
 }
 
-const char *CtorDeclaration::kind()
+const char *CtorDeclaration::kind() const
 {
     return "constructor";
 }
@@ -5039,7 +5038,7 @@  bool DtorDeclaration::addPostInvariant()
     return false;
 }
 
-const char *DtorDeclaration::kind()
+const char *DtorDeclaration::kind() const
 {
     return "destructor";
 }
@@ -5521,7 +5520,7 @@  void NewDeclaration::semantic(Scope *sc)
     FuncDeclaration::semantic(sc);
 }
 
-const char *NewDeclaration::kind()
+const char *NewDeclaration::kind() const
 {
     return "allocator";
 }
@@ -5600,7 +5599,7 @@  void DeleteDeclaration::semantic(Scope *sc)
     FuncDeclaration::semantic(sc);
 }
 
-const char *DeleteDeclaration::kind()
+const char *DeleteDeclaration::kind() const
 {
     return "deallocator";
 }
diff --git a/gcc/d/dmd/globals.h b/gcc/d/dmd/globals.h
index 63caeb7b6b1..e5fefe911ea 100644
--- a/gcc/d/dmd/globals.h
+++ b/gcc/d/dmd/globals.h
@@ -234,22 +234,13 @@  struct Global
 
 extern Global global;
 
-// Because int64_t and friends may be any integral type of the
-// correct size, we have to explicitly ask for the correct
-// integer type to get the correct mangling with ddmd
-#if __LP64__
 // Be careful not to care about sign when using dinteger_t
 // use this instead of integer_t to
 // avoid conflicts with system #include's
-typedef unsigned long dinteger_t;
+typedef uint64_t dinteger_t;
 // Signed and unsigned variants
-typedef long sinteger_t;
-typedef unsigned long uinteger_t;
-#else
-typedef unsigned long long dinteger_t;
-typedef long long sinteger_t;
-typedef unsigned long long uinteger_t;
-#endif
+typedef int64_t sinteger_t;
+typedef uint64_t uinteger_t;
 
 typedef int8_t                  d_int8;
 typedef uint8_t                 d_uns8;
diff --git a/gcc/d/dmd/hdrgen.c b/gcc/d/dmd/hdrgen.c
index 2c734b31822..a1f3c12ba72 100644
--- a/gcc/d/dmd/hdrgen.c
+++ b/gcc/d/dmd/hdrgen.c
@@ -10,11 +10,7 @@ 
 
 // Routines to emit header files
 
-#include <ctype.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <assert.h>
-
+#include "root/dsystem.h"
 #include "root/rmem.h"
 
 #include "mars.h"
@@ -2336,7 +2332,8 @@  public:
         (ie, 8 chars more than mantissa). Plus one for trailing \0.
         Plus one for rounding. */
         const size_t BUFFER_LEN = sizeof(value) * 3 + 8 + 1 + 1;
-        char buffer[BUFFER_LEN] = {};
+        char buffer[BUFFER_LEN];
+        memset(buffer, 0, BUFFER_LEN);
         CTFloat::sprint(buffer, 'g', value);
         assert(strlen(buffer) < BUFFER_LEN);
 
diff --git a/gcc/d/dmd/hdrgen.h b/gcc/d/dmd/hdrgen.h
index c4bafca6d36..f9178beda8d 100644
--- a/gcc/d/dmd/hdrgen.h
+++ b/gcc/d/dmd/hdrgen.h
@@ -10,8 +10,7 @@ 
 
 #pragma once
 
-#include <string.h>                     // memset()
-
+#include "root/dsystem.h"               // memset()
 #include "dsymbol.h"
 
 void genhdrfile(Module *m);
diff --git a/gcc/d/dmd/identifier.c b/gcc/d/dmd/identifier.c
index 5e40746da7d..f535e5b62a1 100644
--- a/gcc/d/dmd/identifier.c
+++ b/gcc/d/dmd/identifier.c
@@ -8,11 +8,9 @@ 
  * https://github.com/D-Programming-Language/dmd/blob/master/src/identifier.c
  */
 
-#include <stdio.h>
-#include <string.h>
-#include <ctype.h>
-
+#include "root/dsystem.h"
 #include "root/root.h"
+
 #include "identifier.h"
 #include "mars.h"
 #include "id.h"
diff --git a/gcc/d/dmd/idgen.c b/gcc/d/dmd/idgen.c
index d360ec850d8..d07725b0253 100644
--- a/gcc/d/dmd/idgen.c
+++ b/gcc/d/dmd/idgen.c
@@ -14,10 +14,7 @@ 
 //      id.h
 //      id.c
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <assert.h>
+#include "root/dsystem.h"
 
 struct Msgtable
 {
diff --git a/gcc/d/dmd/impcnvgen.c b/gcc/d/dmd/impcnvgen.c
index 5b88e8e682e..15239fdc283 100644
--- a/gcc/d/dmd/impcnvgen.c
+++ b/gcc/d/dmd/impcnvgen.c
@@ -8,8 +8,7 @@ 
  * https://github.com/D-Programming-Language/dmd/blob/master/src/impcnvgen.c
  */
 
-#include <stdio.h>
-#include <stdlib.h>
+#include "root/dsystem.h"
 
 #include "mtype.h"
 
diff --git a/gcc/d/dmd/imphint.c b/gcc/d/dmd/imphint.c
index 6d75603d80c..9b29490d361 100644
--- a/gcc/d/dmd/imphint.c
+++ b/gcc/d/dmd/imphint.c
@@ -9,11 +9,7 @@ 
  */
 
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <ctype.h>
-#include <assert.h>
-#include <string.h>
+#include "root/dsystem.h"
 
 #include "mars.h"
 
diff --git a/gcc/d/dmd/import.h b/gcc/d/dmd/import.h
index e0fa14d5888..dcaec5c7acb 100644
--- a/gcc/d/dmd/import.h
+++ b/gcc/d/dmd/import.h
@@ -42,7 +42,7 @@  public:
     Import(Loc loc, Identifiers *packages, Identifier *id, Identifier *aliasId,
         int isstatic);
     void addAlias(Identifier *name, Identifier *alias);
-    const char *kind();
+    const char *kind() const;
     Prot prot();
     Dsymbol *syntaxCopy(Dsymbol *s);    // copy only syntax trees
     void load(Scope *sc);
diff --git a/gcc/d/dmd/init.c b/gcc/d/dmd/init.c
index e619cb4850a..6603f66bfe6 100644
--- a/gcc/d/dmd/init.c
+++ b/gcc/d/dmd/init.c
@@ -8,10 +8,9 @@ 
  * https://github.com/D-Programming-Language/dmd/blob/master/src/init.c
  */
 
-#include <stdio.h>
-#include <assert.h>
+#include "root/dsystem.h"
+#include "root/checkedint.h"
 
-#include "checkedint.h"
 #include "mars.h"
 #include "init.h"
 #include "expression.h"
diff --git a/gcc/d/dmd/initsem.c b/gcc/d/dmd/initsem.c
index bdaf2534a0c..bcb620d8209 100644
--- a/gcc/d/dmd/initsem.c
+++ b/gcc/d/dmd/initsem.c
@@ -7,7 +7,7 @@ 
  * http://www.boost.org/LICENSE_1_0.txt
  */
 
-#include "checkedint.h"
+#include "root/checkedint.h"
 #include "mars.h"
 #include "init.h"
 #include "expression.h"
diff --git a/gcc/d/dmd/intrange.c b/gcc/d/dmd/intrange.c
index b4d54dbfeea..966b9a2986e 100644
--- a/gcc/d/dmd/intrange.c
+++ b/gcc/d/dmd/intrange.c
@@ -8,8 +8,7 @@ 
  * https://github.com/D-Programming-Language/dmd/blob/master/src/intrange.c
  */
 
-#include <stddef.h>
-#include <stdint.h>
+#include "root/dsystem.h"
 
 #include "intrange.h"
 #include "mars.h"
diff --git a/gcc/d/dmd/json.c b/gcc/d/dmd/json.c
index 4c49023b542..5ee957103c6 100644
--- a/gcc/d/dmd/json.c
+++ b/gcc/d/dmd/json.c
@@ -10,10 +10,7 @@ 
 
 // This implements the JSON capability.
 
-#include <stdio.h>
-#include <string.h>
-#include <assert.h>
-
+#include "root/dsystem.h"
 #include "root/rmem.h"
 
 #include "mars.h"
diff --git a/gcc/d/dmd/lexer.c b/gcc/d/dmd/lexer.c
index 1fefe2b08de..0b1824538fd 100644
--- a/gcc/d/dmd/lexer.c
+++ b/gcc/d/dmd/lexer.c
@@ -10,16 +10,7 @@ 
 
 /* Lexical Analyzer */
 
-#include <stdio.h>
-#include <string.h>
-#include <ctype.h>
-#include <stdarg.h>
-#include <errno.h>
-#include <wchar.h>
-#include <stdlib.h>
-#include <assert.h>
-#include <time.h>       // for time() and ctime()
-
+#include "root/dsystem.h" // for time() and ctime()
 #include "root/rmem.h"
 
 #include "mars.h"
diff --git a/gcc/d/dmd/macro.h b/gcc/d/dmd/macro.h
index 006f11842c4..f44108b1517 100644
--- a/gcc/d/dmd/macro.h
+++ b/gcc/d/dmd/macro.h
@@ -10,11 +10,7 @@ 
 
 #pragma once
 
-#include <stdio.h>
-#include <string.h>
-#include <time.h>
-#include <ctype.h>
-
+#include "root/dsystem.h"
 #include "root/root.h"
 
 
diff --git a/gcc/d/dmd/mars.h b/gcc/d/dmd/mars.h
index 9cc5bb86fee..5aec7e6b458 100644
--- a/gcc/d/dmd/mars.h
+++ b/gcc/d/dmd/mars.h
@@ -54,9 +54,7 @@  the target object file format:
  */
 
 
-#include <stdio.h>
-#include <stdint.h>
-#include <stdarg.h>
+#include "root/dsystem.h"
 
 #ifdef __DMC__
 #ifdef DEBUG
diff --git a/gcc/d/dmd/module.h b/gcc/d/dmd/module.h
index 399b0085635..d8fb020175e 100644
--- a/gcc/d/dmd/module.h
+++ b/gcc/d/dmd/module.h
@@ -35,7 +35,7 @@  public:
     Module *mod;        // != NULL if isPkgMod == PKGmodule
 
     Package(Identifier *ident);
-    const char *kind();
+    const char *kind() const;
 
     static DsymbolTable *resolve(Identifiers *packages, Dsymbol **pparent, Package **ppkg);
 
@@ -117,7 +117,7 @@  public:
 
     static Module *load(Loc loc, Identifiers *packages, Identifier *ident);
 
-    const char *kind();
+    const char *kind() const;
     File *setOutfile(const char *name, const char *dir, const char *arg, const char *ext);
     void setDocfile();
     bool read(Loc loc); // read file, returns 'true' if succeed, 'false' otherwise.
diff --git a/gcc/d/dmd/mtype.c b/gcc/d/dmd/mtype.c
index a4c38e8a1ee..dedaf7dc83e 100644
--- a/gcc/d/dmd/mtype.c
+++ b/gcc/d/dmd/mtype.c
@@ -8,22 +8,8 @@ 
  * https://github.com/D-Programming-Language/dmd/blob/master/src/mtype.c
  */
 
-#define __C99FEATURES__ 1       // Needed on Solaris for NaN and more
-#define __USE_ISOC99 1          // so signbit() gets defined
-
-#include <math.h>
-#include <stdio.h>
-#include <assert.h>
-#include <float.h>
-
-#if _MSC_VER
-#include <malloc.h>
-#include <limits>
-#elif __MINGW32__
-#include <malloc.h>
-#endif
-
-#include "checkedint.h"
+#include "root/dsystem.h"
+#include "root/checkedint.h"
 #include "root/rmem.h"
 
 #include "mars.h"
@@ -2336,8 +2322,7 @@  Identifier *Type::getTypeInfoIdent()
     // Allocate buffer on stack, fail over to using malloc()
     char namebuf[128];
     size_t namelen = 19 + sizeof(len) * 3 + len + 1;
-    char *name = namelen <= sizeof(namebuf) ? namebuf : (char *)malloc(namelen);
-    assert(name);
+    char *name = namelen <= sizeof(namebuf) ? namebuf : (char *)mem.xmalloc(namelen);
 
     sprintf(name, "_D%lluTypeInfo_%s6__initZ", (unsigned long long) 9 + len, buf.data);
     //printf("%p, deco = %s, name = %s\n", this, deco, name);
@@ -9343,20 +9328,23 @@  bool Parameter::isCovariantScope(bool returnByRef, StorageClass from, StorageCla
         static unsigned buildSR(bool returnByRef, StorageClass stc)
         {
             unsigned result;
-            switch (stc & (STCref | STCscope | STCreturn))
-            {
-                case 0:                    result = SRNone;        break;
-                case STCref:               result = SRRef;         break;
-                case STCscope:             result = SRScope;       break;
-                case STCreturn | STCref:   result = SRReturnRef;   break;
-                case STCreturn | STCscope: result = SRReturnScope; break;
-                case STCref    | STCscope: result = SRRefScope;    break;
-                case STCreturn | STCref | STCscope:
-                    result = returnByRef ? SRReturnRef_Scope : SRRef_ReturnScope;
-                    break;
-                default:
-                    assert(0);
-            }
+            StorageClass stc2 = stc & (STCref | STCscope | STCreturn);
+            if (stc2 == 0)
+                result = SRNone;
+            else if (stc2 == STCref)
+                result = SRRef;
+            else if (stc2 == STCscope)
+                result = SRScope;
+            else if (stc2 == (STCscope | STCreturn))
+                result = SRReturnScope;
+            else if (stc2 == (STCref | STCreturn))
+                result = SRReturnRef;
+            else if (stc2 == (STCscope | STCref))
+                result = SRRefScope;
+            else if (stc2 == (STCscope | STCref | STCreturn))
+                result = returnByRef ? SRReturnRef_Scope : SRRef_ReturnScope;
+            else
+                assert(0);
             return result;
         }
 
diff --git a/gcc/d/dmd/nspace.c b/gcc/d/dmd/nspace.c
index 2bd6b969813..49c41f37677 100644
--- a/gcc/d/dmd/nspace.c
+++ b/gcc/d/dmd/nspace.c
@@ -6,9 +6,7 @@ 
 // Source: https://github.com/D-Programming-Language/dmd/blob/master/src/nspace.c
 
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <assert.h>
+#include "root/dsystem.h"
 
 #include "mars.h"
 #include "dsymbol.h"
@@ -156,7 +154,7 @@  void Nspace::semantic3(Scope *sc)
     }
 }
 
-const char *Nspace::kind()
+const char *Nspace::kind() const
 {
     return "namespace";
 }
diff --git a/gcc/d/dmd/nspace.h b/gcc/d/dmd/nspace.h
index 8ca01ae8485..f6c4a3d8288 100644
--- a/gcc/d/dmd/nspace.h
+++ b/gcc/d/dmd/nspace.h
@@ -32,7 +32,7 @@  class Nspace : public ScopeDsymbol
     int apply(Dsymbol_apply_ft_t fp, void *param);
     bool hasPointers();
     void setFieldOffset(AggregateDeclaration *ad, unsigned *poffset, bool isunion);
-    const char *kind();
+    const char *kind() const;
     Nspace *isNspace() { return this; }
     void accept(Visitor *v) { v->visit(this); }
 };
diff --git a/gcc/d/dmd/opover.c b/gcc/d/dmd/opover.c
index be6fd78886a..0bcccb63afd 100644
--- a/gcc/d/dmd/opover.c
+++ b/gcc/d/dmd/opover.c
@@ -8,12 +8,7 @@ 
  * https://github.com/D-Programming-Language/dmd/blob/master/src/opover.c
  */
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <ctype.h>
-#include <assert.h>
-#include <string.h>                     // memset()
-
+#include "root/dsystem.h"               // memset()
 #include "root/rmem.h"
 
 #include "mars.h"
diff --git a/gcc/d/dmd/optimize.c b/gcc/d/dmd/optimize.c
index 12224fa5188..89a584bea77 100644
--- a/gcc/d/dmd/optimize.c
+++ b/gcc/d/dmd/optimize.c
@@ -8,12 +8,9 @@ 
  * https://github.com/D-Programming-Language/dmd/blob/master/src/optimize.c
  */
 
-#include <stdio.h>
-#include <ctype.h>
-#include <assert.h>
-#include <math.h>
+#include "root/dsystem.h"
 
-#include "checkedint.h"
+#include "root/checkedint.h"
 #include "lexer.h"
 #include "mtype.h"
 #include "expression.h"
diff --git a/gcc/d/dmd/parse.c b/gcc/d/dmd/parse.c
index 9ee2f0837b1..119876065b1 100644
--- a/gcc/d/dmd/parse.c
+++ b/gcc/d/dmd/parse.c
@@ -10,11 +10,9 @@ 
 
 // This is the D parser
 
-#include <stdio.h>
-#include <assert.h>
-#include <string.h>                     // strlen(),memcpy()
-
+#include "root/dsystem.h"               // strlen(),memcpy()
 #include "root/rmem.h"
+
 #include "mars.h"
 #include "lexer.h"
 #include "parse.h"
diff --git a/gcc/d/dmd/parse.h b/gcc/d/dmd/parse.h
index 50381067917..92c09dec073 100644
--- a/gcc/d/dmd/parse.h
+++ b/gcc/d/dmd/parse.h
@@ -52,7 +52,7 @@  enum ParseStatementFlags
     PSscope = 2,        // start a new scope
     PScurly = 4,        // { } statement is required
     PScurlyscope = 8,   // { } starts a new scope
-    PSsemi_ok = 0x10,   // empty ';' are really ok
+    PSsemi_ok = 0x10    // empty ';' are really ok
 };
 
 
@@ -180,7 +180,7 @@  enum PREC
     PREC_mul,
     PREC_pow,
     PREC_unary,
-    PREC_primary,
+    PREC_primary
 };
 
 extern PREC precedence[TOKMAX];
diff --git a/gcc/d/dmd/root/aav.c b/gcc/d/dmd/root/aav.c
index 4c015bf0700..931a395a701 100644
--- a/gcc/d/dmd/root/aav.c
+++ b/gcc/d/dmd/root/aav.c
@@ -11,11 +11,7 @@ 
  *
  */
 
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <assert.h>
-
+#include "dsystem.h"
 #include "aav.h"
 #include "rmem.h"
 
diff --git a/gcc/d/dmd/root/aav.h b/gcc/d/dmd/root/aav.h
index 24c281a1503..ad57cb4ce4b 100644
--- a/gcc/d/dmd/root/aav.h
+++ b/gcc/d/dmd/root/aav.h
@@ -8,6 +8,8 @@ 
 
 #pragma once
 
+#include "dsystem.h"
+
 typedef void* Value;
 typedef void* Key;
 
diff --git a/gcc/d/dmd/root/array.h b/gcc/d/dmd/root/array.h
index ed128b93dd7..dd289982290 100644
--- a/gcc/d/dmd/root/array.h
+++ b/gcc/d/dmd/root/array.h
@@ -7,11 +7,7 @@ 
 
 #pragma once
 
-#include <assert.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
+#include "dsystem.h"
 #include "object.h"
 #include "rmem.h"
 
diff --git a/gcc/d/dmd/checkedint.c b/gcc/d/dmd/root/checkedint.c
similarity index 99%
rename from gcc/d/dmd/checkedint.c
rename to gcc/d/dmd/root/checkedint.c
index b9ccb5b2397..392904acf8a 100644
--- a/gcc/d/dmd/checkedint.c
+++ b/gcc/d/dmd/root/checkedint.c
@@ -24,13 +24,13 @@ 
  * Copyright: Copyright (C) 2014-2018 by The D Language Foundation, All Rights Reserved
  * License:   $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
  * Authors:   Walter Bright
- * Source:    https://github.com/D-Programming-Language/dmd/blob/master/src/root/port.c
+ * Source:    https://github.com/D-Programming-Language/dmd/blob/master/src/root/checkedint.c
  */
 
-#include <assert.h>
-
+#include "dsystem.h"
 #include "checkedint.h"
 
+
 /*******************************
  * Add two signed integers, checking for overflow.
  *
diff --git a/gcc/d/dmd/checkedint.h b/gcc/d/dmd/root/checkedint.h
similarity index 90%
rename from gcc/d/dmd/checkedint.h
rename to gcc/d/dmd/root/checkedint.h
index 1f6545b6c62..1362ff51be9 100644
--- a/gcc/d/dmd/checkedint.h
+++ b/gcc/d/dmd/root/checkedint.h
@@ -5,15 +5,10 @@ 
  * http://www.digitalmars.com
  * Distributed under the Boost Software License, Version 1.0.
  * http://www.boost.org/LICENSE_1_0.txt
- * https://github.com/D-Programming-Language/dmd/blob/master/src/checkedint.h
+ * https://github.com/D-Programming-Language/dmd/blob/master/src/root/checkedint.h
  */
 
-#pragma once
-
-#ifndef __STDC_LIMIT_MACROS
-#define __STDC_LIMIT_MACROS 1
-#endif
-#include <stdint.h>
+#include "dsystem.h"
 
 
 int adds(int x, int y, bool& overflow);
diff --git a/gcc/d/dmd/root/dcompat.h b/gcc/d/dmd/root/dcompat.h
index 3fc169c034e..f92f0abaabb 100644
--- a/gcc/d/dmd/root/dcompat.h
+++ b/gcc/d/dmd/root/dcompat.h
@@ -9,6 +9,8 @@ 
 
 #pragma once
 
+#include "dsystem.h"
+
 /// Represents a D [ ] array
 template<typename T>
 struct DArray
diff --git a/gcc/d/dmd/root/dsystem.h b/gcc/d/dmd/root/dsystem.h
new file mode 100644
index 00000000000..940ff500062
--- /dev/null
+++ b/gcc/d/dmd/root/dsystem.h
@@ -0,0 +1,24 @@ 
+/* dsystem.h -- Get common system includes from the host.
+ * Copyright (C) 2018 Free Software Foundation, Inc.
+ *
+ * GCC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3, or (at your option)
+ * any later version.
+ *
+ * GCC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with GCC; see the file COPYING3.  If not see
+ * <http://www.gnu.org/licenses/>.
+ */
+
+/* This header is used in place of dmd upstream so that we can wrap
+   around gcc's system.h.  */
+
+#pragma once
+
+#include "d-system.h"
diff --git a/gcc/d/dmd/root/file.c b/gcc/d/dmd/root/file.c
index f4fd51a0012..454d4cb0760 100644
--- a/gcc/d/dmd/root/file.c
+++ b/gcc/d/dmd/root/file.c
@@ -6,29 +6,14 @@ 
  * https://github.com/D-Programming-Language/dmd/blob/master/src/root/file.c
  */
 
+#include "dsystem.h"
 #include "file.h"
 
-#if defined (__sun)
-#include <alloca.h>
-#endif
-
-#if _MSC_VER ||__MINGW32__
-#include <malloc.h>
-#include <string>
-#endif
-
 #if _WIN32
 #include <windows.h>
-#include <direct.h>
-#include <errno.h>
 #endif
 
 #if POSIX
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <errno.h>
-#include <unistd.h>
 #include <utime.h>
 #endif
 
@@ -104,7 +89,11 @@  bool File::read()
         goto err2;
     }
     size = (size_t)buf.st_size;
+#ifdef IN_GCC
+    buffer = (unsigned char *) ::xmalloc(size + 2);
+#else
     buffer = (unsigned char *) ::malloc(size + 2);
+#endif
     if (!buffer)
     {
         printf("\tmalloc error, errno = %d\n",errno);
@@ -155,7 +144,11 @@  err1:
     ref = 0;
 
     size = GetFileSize(h,NULL);
+#ifdef IN_GCC
+    buffer = (unsigned char *) ::xmalloc(size + 2);
+#else
     buffer = (unsigned char *) ::malloc(size + 2);
+#endif
     if (!buffer)
         goto err2;
 
diff --git a/gcc/d/dmd/root/file.h b/gcc/d/dmd/root/file.h
index f6953ab34db..f4d2937bfd3 100644
--- a/gcc/d/dmd/root/file.h
+++ b/gcc/d/dmd/root/file.h
@@ -8,8 +8,7 @@ 
 
 #pragma once
 
-#include <stddef.h>
-
+#include "dsystem.h"
 #include "array.h"
 
 typedef Array<struct File *> Files;
diff --git a/gcc/d/dmd/root/filename.c b/gcc/d/dmd/root/filename.c
index 307e94f6584..b0bd1a5a4fd 100644
--- a/gcc/d/dmd/root/filename.c
+++ b/gcc/d/dmd/root/filename.c
@@ -6,37 +6,19 @@ 
  * https://github.com/D-Programming-Language/dmd/blob/master/src/root/filename.c
  */
 
+#include "dsystem.h"
 #include "filename.h"
 
-#include <stdint.h>
-#include <ctype.h>
-
 #include "outbuffer.h"
 #include "array.h"
 #include "file.h"
 #include "rmem.h"
 
-#if defined (__sun)
-#include <alloca.h>
-#endif
-
-#if _MSC_VER ||__MINGW32__
-#include <malloc.h>
-#include <string>
-#endif
-
 #if _WIN32
 #include <windows.h>
-#include <direct.h>
-#include <errno.h>
 #endif
 
 #if POSIX
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <errno.h>
-#include <unistd.h>
 #include <utime.h>
 #endif
 
diff --git a/gcc/d/dmd/root/hash.h b/gcc/d/dmd/root/hash.h
index 1b05672ff9f..f1bc6cc5cca 100644
--- a/gcc/d/dmd/root/hash.h
+++ b/gcc/d/dmd/root/hash.h
@@ -10,8 +10,7 @@ 
 
 #pragma once
 
-#include <stdint.h>                     // uint{8|16|32}_t
-#include <stdlib.h>
+#include "dsystem.h"                    // uint{8|16|32}_t
 
 // MurmurHash2 was written by Austin Appleby, and is placed in the public
 // domain. The author hereby disclaims copyright to this source code.
diff --git a/gcc/d/dmd/root/object.h b/gcc/d/dmd/root/object.h
index e9d770d5d3d..d465e363522 100644
--- a/gcc/d/dmd/root/object.h
+++ b/gcc/d/dmd/root/object.h
@@ -6,11 +6,9 @@ 
  * https://github.com/dlang/dmd/blob/master/src/root/object.h
  */
 
-#define POSIX (__linux__ || __APPLE__ || __FreeBSD__ || __OpenBSD__ || __sun)
-
 #pragma once
 
-#include <stddef.h>
+#include "dsystem.h"
 
 typedef size_t hash_t;
 
@@ -25,7 +23,7 @@  enum DYNCAST
     DYNCAST_IDENTIFIER,
     DYNCAST_TUPLE,
     DYNCAST_PARAMETER,
-    DYNCAST_STATEMENT,
+    DYNCAST_STATEMENT
 };
 
 /*
diff --git a/gcc/d/dmd/root/outbuffer.c b/gcc/d/dmd/root/outbuffer.c
index 7b6782defb1..4791bb177c9 100644
--- a/gcc/d/dmd/root/outbuffer.c
+++ b/gcc/d/dmd/root/outbuffer.c
@@ -6,16 +6,7 @@ 
  * https://github.com/D-Programming-Language/dmd/blob/master/src/root/outbuffer.c
  */
 
-#include <assert.h>
-#include <stdarg.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#if __sun
-#include <alloca.h>
-#endif
-
+#include "dsystem.h"
 #include "outbuffer.h"
 #include "object.h"
 
diff --git a/gcc/d/dmd/root/outbuffer.h b/gcc/d/dmd/root/outbuffer.h
index 6d3be107129..6e6b35d8d93 100644
--- a/gcc/d/dmd/root/outbuffer.h
+++ b/gcc/d/dmd/root/outbuffer.h
@@ -8,10 +8,7 @@ 
 
 #pragma once
 
-#include <stdlib.h>
-#include <stdarg.h>
-#include <string.h>
-#include <assert.h>
+#include "dsystem.h"
 #include "port.h"
 #include "rmem.h"
 
diff --git a/gcc/d/dmd/root/port.h b/gcc/d/dmd/root/port.h
index 3f3c46d9168..0dbb3197ad5 100644
--- a/gcc/d/dmd/root/port.h
+++ b/gcc/d/dmd/root/port.h
@@ -11,11 +11,9 @@ 
 // Portable wrapper around compiler/system specific things.
 // The idea is to minimize #ifdef's in the app code.
 
-#include <stdlib.h> // for alloca
-#include <stdint.h>
+#include "dsystem.h" // for alloca
 
 #if _MSC_VER
-#include <alloca.h>
 typedef __int64 longlong;
 typedef unsigned __int64 ulonglong;
 #else
diff --git a/gcc/d/dmd/root/rmem.c b/gcc/d/dmd/root/rmem.c
index 92e79ca95ae..d24e0126b37 100644
--- a/gcc/d/dmd/root/rmem.c
+++ b/gcc/d/dmd/root/rmem.c
@@ -6,10 +6,7 @@ 
  * https://github.com/D-Programming-Language/dmd/blob/master/src/root/rmem.c
  */
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
+#include "dsystem.h"
 #include "rmem.h"
 
 /* This implementation of the storage allocator uses the standard C allocation package.
@@ -23,7 +20,11 @@  char *Mem::xstrdup(const char *s)
 
     if (s)
     {
+#ifdef IN_GCC
+        p = ::xstrdup(s);
+#else
         p = strdup(s);
+#endif
         if (p)
             return p;
         error();
@@ -38,7 +39,11 @@  void *Mem::xmalloc(size_t size)
         p = NULL;
     else
     {
+#ifdef IN_GCC
+        p = ::xmalloc(size);
+#else
         p = malloc(size);
+#endif
         if (!p)
             error();
     }
@@ -52,7 +57,11 @@  void *Mem::xcalloc(size_t size, size_t n)
         p = NULL;
     else
     {
+#ifdef IN_GCC
+        p = ::xcalloc(size, n);
+#else
         p = calloc(size, n);
+#endif
         if (!p)
             error();
     }
@@ -70,14 +79,22 @@  void *Mem::xrealloc(void *p, size_t size)
     }
     else if (!p)
     {
+#ifdef IN_GCC
+        p = ::xmalloc(size);
+#else
         p = malloc(size);
+#endif
         if (!p)
             error();
     }
     else
     {
         void *psave = p;
+#ifdef IN_GCC
+        p = ::xrealloc(psave, size);
+#else
         p = realloc(psave, size);
+#endif
         if (!p)
         {   xfree(psave);
             error();
@@ -99,7 +116,11 @@  void *Mem::xmallocdup(void *o, size_t size)
         p = NULL;
     else
     {
+#ifdef IN_GCC
+        p = ::xmalloc(size);
+#else
         p = malloc(size);
+#endif
         if (!p)
             error();
         else
@@ -143,7 +164,11 @@  extern "C" void *allocmemory(size_t m_size)
 
     if (m_size > CHUNK_SIZE)
     {
+#ifdef IN_GCC
+        void *p = xmalloc(m_size);
+#else
         void *p = malloc(m_size);
+#endif
         if (p)
             return p;
         printf("Error: out of memory\n");
@@ -152,7 +177,11 @@  extern "C" void *allocmemory(size_t m_size)
     }
 
     heapleft = CHUNK_SIZE;
+#ifdef IN_GCC
+    heapp = xmalloc(CHUNK_SIZE);
+#else
     heapp = malloc(CHUNK_SIZE);
+#endif
     if (!heapp)
     {
         printf("Error: out of memory\n");
diff --git a/gcc/d/dmd/root/rmem.h b/gcc/d/dmd/root/rmem.h
index 87d465f1da3..0123402bfae 100644
--- a/gcc/d/dmd/root/rmem.h
+++ b/gcc/d/dmd/root/rmem.h
@@ -8,7 +8,7 @@ 
 
 #pragma once
 
-#include <stddef.h>     // for size_t
+#include "dsystem.h"    // for size_t
 
 #if __APPLE__ && __i386__
     /* size_t is 'unsigned long', which makes it mangle differently
diff --git a/gcc/d/dmd/root/rootobject.c b/gcc/d/dmd/root/rootobject.c
index cd239d6c3ca..b45e795e14e 100644
--- a/gcc/d/dmd/root/rootobject.c
+++ b/gcc/d/dmd/root/rootobject.c
@@ -5,8 +5,7 @@ 
  * https://github.com/D-Programming-Language/dmd/blob/master/src/root/object.c
  */
 
-#include <stdio.h>
-
+#include "dsystem.h"
 #include "object.h"
 #include "outbuffer.h"
 
diff --git a/gcc/d/dmd/root/speller.c b/gcc/d/dmd/root/speller.c
index 52b4e4767f8..a5c16bab12d 100644
--- a/gcc/d/dmd/root/speller.c
+++ b/gcc/d/dmd/root/speller.c
@@ -6,16 +6,7 @@ 
  * https://github.com/D-Programming-Language/dmd/blob/master/src/root/speller.c
  */
 
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <assert.h>
-#include <limits.h>
-
-#if __sun || _MSC_VER
-#include <alloca.h>
-#endif
-
+#include "dsystem.h"
 #include "speller.h"
 
 const char idchars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
diff --git a/gcc/d/dmd/root/stringtable.c b/gcc/d/dmd/root/stringtable.c
index 158e2af6e23..473df7cbe50 100644
--- a/gcc/d/dmd/root/stringtable.c
+++ b/gcc/d/dmd/root/stringtable.c
@@ -6,11 +6,7 @@ 
  * https://github.com/D-Programming-Language/dmd/blob/master/src/root/stringtable.c
  */
 
-#include <stdio.h>
-#include <stdint.h>                     // uint{8|16|32}_t
-#include <string.h>                     // memcpy()
-#include <stdlib.h>
-
+#include "dsystem.h"                    // uint{8|16|32}_t, memcpy()
 #include "root.h"
 #include "rmem.h"                       // mem
 #include "stringtable.h"
diff --git a/gcc/d/dmd/sapply.c b/gcc/d/dmd/sapply.c
index 89209770234..18689c1f2da 100644
--- a/gcc/d/dmd/sapply.c
+++ b/gcc/d/dmd/sapply.c
@@ -8,8 +8,7 @@ 
  * https://github.com/D-Programming-Language/dmd/blob/master/src/sapply.c
  */
 
-#include <stdio.h>
-#include <assert.h>
+#include "root/dsystem.h"
 
 #include "mars.h"
 #include "statement.h"
diff --git a/gcc/d/dmd/sideeffect.c b/gcc/d/dmd/sideeffect.c
index 83e79738041..56d7d078027 100644
--- a/gcc/d/dmd/sideeffect.c
+++ b/gcc/d/dmd/sideeffect.c
@@ -8,8 +8,7 @@ 
  * https://github.com/D-Programming-Language/dmd/blob/master/src/sideeffect.c
  */
 
-#include <stdio.h>
-#include <assert.h>
+#include "root/dsystem.h"
 
 #include "mars.h"
 #include "init.h"
diff --git a/gcc/d/dmd/statement.c b/gcc/d/dmd/statement.c
index 6e679074e0a..561791478ba 100644
--- a/gcc/d/dmd/statement.c
+++ b/gcc/d/dmd/statement.c
@@ -8,9 +8,7 @@ 
  * https://github.com/D-Programming-Language/dmd/blob/master/src/statement.c
  */
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <assert.h>
+#include "root/dsystem.h"
 
 #include "statement.h"
 #include "errors.h"
diff --git a/gcc/d/dmd/statementsem.c b/gcc/d/dmd/statementsem.c
index 7ee541c9d50..9be934fb4ef 100644
--- a/gcc/d/dmd/statementsem.c
+++ b/gcc/d/dmd/statementsem.c
@@ -7,12 +7,9 @@ 
  * http://www.boost.org/LICENSE_1_0.txt
  */
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <assert.h>
-
+#include "root/dsystem.h"
 #include "root/rmem.h"
-#include "checkedint.h"
+#include "root/checkedint.h"
 
 #include "errors.h"
 #include "statement.h"
diff --git a/gcc/d/dmd/staticassert.c b/gcc/d/dmd/staticassert.c
index 476668f900b..5da1f7d9b77 100644
--- a/gcc/d/dmd/staticassert.c
+++ b/gcc/d/dmd/staticassert.c
@@ -8,9 +8,7 @@ 
  * https://github.com/D-Programming-Language/dmd/blob/master/src/staticassert.c
  */
 
-#include <stdio.h>
-#include <string.h>
-#include <assert.h>
+#include "root/dsystem.h"
 
 #include "mars.h"
 #include "dsymbol.h"
@@ -98,7 +96,7 @@  bool StaticAssert::oneMember(Dsymbol **ps, Identifier *)
     return true;
 }
 
-const char *StaticAssert::kind()
+const char *StaticAssert::kind() const
 {
     return "static assert";
 }
diff --git a/gcc/d/dmd/staticassert.h b/gcc/d/dmd/staticassert.h
index 0112f027024..4a601ab7afa 100644
--- a/gcc/d/dmd/staticassert.h
+++ b/gcc/d/dmd/staticassert.h
@@ -27,6 +27,6 @@  public:
     void semantic(Scope *sc);
     void semantic2(Scope *sc);
     bool oneMember(Dsymbol **ps, Identifier *ident);
-    const char *kind();
+    const char *kind() const;
     void accept(Visitor *v) { v->visit(this); }
 };
diff --git a/gcc/d/dmd/target.h b/gcc/d/dmd/target.h
index 937900896b2..63dfcf261a4 100644
--- a/gcc/d/dmd/target.h
+++ b/gcc/d/dmd/target.h
@@ -19,6 +19,7 @@ 
 class ClassDeclaration;
 class Dsymbol;
 class Expression;
+class Parameter;
 class Type;
 struct OutBuffer;
 
diff --git a/gcc/d/dmd/template.h b/gcc/d/dmd/template.h
index f5991030f39..c913b2a5bfa 100644
--- a/gcc/d/dmd/template.h
+++ b/gcc/d/dmd/template.h
@@ -83,7 +83,7 @@  public:
     void semantic(Scope *sc);
     bool overloadInsert(Dsymbol *s);
     bool hasStaticCtorOrDtor();
-    const char *kind();
+    const char *kind() const;
     const char *toChars();
 
     Prot prot();
@@ -331,7 +331,7 @@  public:
     void semantic2(Scope *sc);
     void semantic3(Scope *sc);
     Dsymbol *toAlias();                 // resolve real symbol
-    const char *kind();
+    const char *kind() const;
     bool oneMember(Dsymbol **ps, Identifier *ident);
     const char *toChars();
     const char* toPrettyCharsHelper();
@@ -371,7 +371,7 @@  public:
     void semantic(Scope *sc);
     void semantic2(Scope *sc);
     void semantic3(Scope *sc);
-    const char *kind();
+    const char *kind() const;
     bool oneMember(Dsymbol **ps, Identifier *ident);
     int apply(Dsymbol_apply_ft_t fp, void *param);
     bool hasPointers();
diff --git a/gcc/d/dmd/tokens.c b/gcc/d/dmd/tokens.c
index 6f68e478dee..7251c261cdb 100644
--- a/gcc/d/dmd/tokens.c
+++ b/gcc/d/dmd/tokens.c
@@ -8,8 +8,7 @@ 
  * https://github.com/D-Programming-Language/dmd/blob/master/src/lexer.c
  */
 
-#include <stdio.h>
-#include <ctype.h>
+#include "root/dsystem.h"
 
 #include "tokens.h"
 #include "root/rmem.h"
diff --git a/gcc/d/dmd/traits.c b/gcc/d/dmd/traits.c
index b869893d4f1..e4ebea68389 100644
--- a/gcc/d/dmd/traits.c
+++ b/gcc/d/dmd/traits.c
@@ -8,16 +8,11 @@ 
  * https://github.com/D-Programming-Language/dmd/blob/master/src/traits.c
  */
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <ctype.h>
-#include <assert.h>
-#include <math.h>
-
+#include "root/dsystem.h"
 #include "root/rmem.h"
 #include "root/aav.h"
+#include "root/checkedint.h"
 
-#include "checkedint.h"
 #include "errors.h"
 #include "mtype.h"
 #include "init.h"
diff --git a/gcc/d/dmd/unittests.c b/gcc/d/dmd/unittests.c
deleted file mode 100644
index 482782255d7..00000000000
--- a/gcc/d/dmd/unittests.c
+++ /dev/null
@@ -1,26 +0,0 @@ 
-
-/* Compiler implementation of the D programming language
- * Copyright (C) 1999-2018 by The D Language Foundation, All Rights Reserved
- * written by Walter Bright
- * http://www.digitalmars.com
- * Distributed under the Boost Software License, Version 1.0.
- * http://www.boost.org/LICENSE_1_0.txt
- * https://github.com/D-Programming-Language/dmd/blob/master/src/unittests.c
- */
-
-#include <stdio.h>
-
-#include "mars.h"
-
-void unittest_speller();
-void unittest_importHint();
-void unittest_aa();
-
-void unittests()
-{
-#if UNITTEST
-    unittest_speller();
-    unittest_importHint();
-    unittest_aa();
-#endif
-}
diff --git a/gcc/d/dmd/utf.c b/gcc/d/dmd/utf.c
index f07340e95e0..6eb46993acf 100644
--- a/gcc/d/dmd/utf.c
+++ b/gcc/d/dmd/utf.c
@@ -17,8 +17,6 @@ 
 /// [3] http://unicode.org/faq/utf_bom.html
 /// [4] http://www.unicode.org/versions/Unicode6.1.0/ch03.pdf
 
-#include <assert.h>
-
 #include "utf.h"
 
 /* The following encodings are valid, except for the 5 and 6 byte
diff --git a/gcc/d/dmd/utf.h b/gcc/d/dmd/utf.h
index 154d39ad474..831128e7fa7 100644
--- a/gcc/d/dmd/utf.h
+++ b/gcc/d/dmd/utf.h
@@ -10,7 +10,7 @@ 
 
 #pragma once
 
-#include <stdlib.h>
+#include "root/dsystem.h"
 
 /// A UTF-8 code unit
 typedef unsigned char   utf8_t;
diff --git a/gcc/d/dmd/utils.c b/gcc/d/dmd/utils.c
index e3ea8c19e50..177f3cfa7a6 100644
--- a/gcc/d/dmd/utils.c
+++ b/gcc/d/dmd/utils.c
@@ -7,12 +7,13 @@ 
  * http://www.boost.org/LICENSE_1_0.txt
  */
 
-#include <string.h>
+#include "root/dsystem.h"
 #include "mars.h"
 #include "globals.h"
 #include "root/file.h"
 #include "root/filename.h"
 #include "root/outbuffer.h"
+#include "root/rmem.h"
 
 /**
  * Normalize path by turning forward slashes into backslashes
@@ -28,7 +29,7 @@  const char * toWinPath(const char *src)
     if (src == NULL)
         return NULL;
 
-    char *result = strdup(src);
+    char *result = mem.xstrdup(src);
     char *p = result;
     while (*p != '\0')
     {
diff --git a/gcc/d/dmd/version.h b/gcc/d/dmd/version.h
index 6268822df19..cdff1a873fc 100644
--- a/gcc/d/dmd/version.h
+++ b/gcc/d/dmd/version.h
@@ -24,7 +24,7 @@  public:
     const char *toChars();
     void addMember(Scope *sc, ScopeDsymbol *sds);
     void semantic(Scope *sc);
-    const char *kind();
+    const char *kind() const;
     void accept(Visitor *v) { v->visit(this); }
 };
 
@@ -40,6 +40,6 @@  public:
     const char *toChars();
     void addMember(Scope *sc, ScopeDsymbol *sds);
     void semantic(Scope *sc);
-    const char *kind();
+    const char *kind() const;
     void accept(Visitor *v) { v->visit(this); }
 };
diff --git a/gcc/d/dmd/visitor.h b/gcc/d/dmd/visitor.h
index ee0db983181..b86f4f675b2 100644
--- a/gcc/d/dmd/visitor.h
+++ b/gcc/d/dmd/visitor.h
@@ -9,7 +9,7 @@ 
 
 #pragma once
 
-#include <assert.h>
+#include "root/dsystem.h"
 
 class Statement;
 class ErrorStatement;