Index: attrib.c
===================================================================
--- attrib.c	(revision 780)
+++ attrib.c	(working copy)
@@ -1435,6 +1435,7 @@
         se = se->toUTF8(sc);
         Parser p(sc->module, (unsigned char *)se->string, se->len, 0);
         p.loc = loc;
+        writeMixin(se, &p.loc);
         p.nextToken();
         decl = p.parseDeclDefs(0);
         if (p.token.value != TOKeof)
Index: mars.c
===================================================================
--- mars.c	(revision 780)
+++ mars.c	(working copy)
@@ -22,6 +22,7 @@
 #include "rmem.h"
 #include "root.h"
 #include "async.h"
+#include "tk/filespec.h"
 
 #include "mars.h"
 #include "module.h"
@@ -227,11 +228,67 @@
  */
 void halt()
 {
 #ifdef DEBUG
     *(char*)0=0;
 #endif
 }
 
+/**************************************
+ * dump mixin expansion to file for better debugging
+ */
+bool writeMixin(StringExp* se, Loc* loc)
+{
+    if(OutBuffer* ob = global.params.mixinOut)
+    {
+        ob->writenl();
+        global.params.mixinLines++;
+        ob->writestring("// expansion at ");
+        ob->writestring(loc->toChars());
+        ob->writenl();
+        global.params.mixinLines++;
+
+        loc->filename = global.params.mixinFile;
+        loc->linnum = global.params.mixinLines + 1;
+
+        // write by line to create consistent line endings
+        unsigned char* str = (unsigned char*) se->string;
+        int lp = 0;
+        for(int p = 0; p < se->len; p++)
+            if(str[p] == '\n')
+            {
+                ob->write(str + lp, p - lp);
+                ob->writenl();
+                global.params.mixinLines++;
+                lp = p + 1;
+            }
+        if(lp < se->len)
+            ob->write(str + lp, se->len - lp);
+        ob->writenl();
+        global.params.mixinLines++;
+        return true;
+    }
+    return false;
+}
+
+/**************************************
+ * we want to write the mixin expansion file also on error, but there
+ * are too many ways to terminate dmd, it's done by atexit(flushMixins)
+ */
+void flushMixins()
+{
+    if (global.params.mixinOut != NULL)
+    {
+        assert(global.params.mixinFile != NULL);
+
+        File mixin(global.params.mixinFile);
+        OutBuffer* ob = global.params.mixinOut;
+        mixin.setbuffer((void*)ob->data, ob->offset);
+        mixin.writev();
+    }
+}
+
+/**************************************/
+
 extern void backend_init();
 extern void backend_term();
 
@@ -282,6 +339,7 @@
   -lib           generate library rather than object files\n\
   -man           open web browser on manual page\n\
   -map           generate linker .map file\n\
+  -mixin=name    write string mixins to .mixin file\n\
   -noboundscheck turns off array bounds checking for all functions\n\
   -nofloat       do not emit reference to floating point\n\
   -O             optimize\n\
@@ -697,6 +755,17 @@
                     goto Lnoarg;
                 global.params.moduleDeps = new OutBuffer;
             }
+            else if (memcmp(p + 1, "mixin=", 6) == 0)
+            {
+                global.params.mixinFile = p + 1 + 6;
+                if (!global.params.mixinFile[0])
+                    goto Lnoarg;
+                if(!FileName::absolute(global.params.mixinFile))
+                    global.params.mixinFile = filespecrootpath(mem.strdup(global.params.mixinFile));
+
+                global.params.mixinOut = new OutBuffer;
+                atexit(flushMixins);
+            }
             else if (memcmp(p + 1, "man", 3) == 0)
             {
 #if _WIN32
Index: mars.h
===================================================================
--- mars.h	(revision 780)
+++ mars.h	(working copy)
@@ -199,6 +199,10 @@
     char *moduleDepsFile;       // filename for deps output
     OutBuffer *moduleDeps;      // contents to be written to deps file
 
+    char *mixinFile;            // filename for mixin output
+    OutBuffer *mixinOut;        // contents to be written to mixin file
+    int mixinLines;
+
     // Hidden debug switches
     char debuga;
     char debugb;
@@ -396,6 +400,9 @@
 void halt();
 void util_progress();
 
+class StringExp;
+bool writeMixin(StringExp* se, Loc* loc);
+
 /*** Where to send error messages ***/
 #if IN_GCC
 #define stdmsg stderr
Index: statement.c
===================================================================
--- statement.c	(revision 780)
+++ statement.c	(working copy)
@@ -329,6 +329,7 @@
     se = se->toUTF8(sc);
     Parser p(sc->module, (unsigned char *)se->string, se->len, 0);
     p.loc = loc;
+    writeMixin(se, &p.loc);
     p.nextToken();
 
     Statements *a = new Statements();
