shithub: mc

Download patch

ref: e49f3ef67175b25f6e9539dbec3eaf2f2e95eb50
parent: abc7b8dce27323d49315f121a9ef33aff60e5b22
author: Ori Bernstein <[email protected]>
date: Tue Jun 19 18:15:23 EDT 2012

Make it easier to debug. Always generate .s files.

    This does 2 things; It allows us to step through the generated
    asm in a debugger, and it allows us to inspect it. At the current
    stage of the compiler, both of these things are useful.

--- a/8/main.c
+++ b/8/main.c
@@ -12,11 +12,11 @@
 #include "parse.h"
 #include "opt.h"
 #include "asm.h"
+#include "platform.h"
 
 /* FIXME: move into one place...? */
 Node *file;
 int debug;
-int asmonly;
 char *outfile;
 char **incpaths;
 size_t nincpaths;
@@ -31,33 +31,15 @@
     printf("\t-S\tGenerate assembly instead of object code\n");
 }
 
-char *outfmt(char *buf, size_t sz, char *infile, char *outfile)
+static void assem(char *f)
 {
-    char *p, *suffix;
-    size_t len;
+    char objfile[1024];
+    char cmd[1024];
 
-    if (outfile) {
-        snprintf(buf, sz, "%s", outfile);
-        return buf;
-    }
 
-    if (asmonly)
-        suffix = ".s";
-    else
-        suffix = ".o";
-
-    p = strrchr(infile, '.');
-    if (p)
-        len = (p - infile);
-    else
-        len = strlen(infile);
-    if (len + strlen(suffix) >= sz)
-        die("Output file name too long");
-    buf[0] = '\0';
-    strncat(buf, infile, len);
-    strcat(buf, suffix);
-
-    return buf;
+    swapsuffix(objfile, 1024, f, ".s", ".o");
+    snprintf(cmd, 1024, Asmcmd, objfile, f);
+    system(cmd);
 }
 
 int main(int argc, char **argv)
@@ -79,9 +61,6 @@
             case 'd':
                 debug++;
                 break;
-            case 'S':
-                asmonly++;
-                break;
             case 'I':
                 lappend(&incpaths, &nincpaths, optarg);
                 break;
@@ -109,7 +88,9 @@
         if (debug)
             dump(file, stdout);
 
-        gen(file, outfmt(buf, 1024, argv[i], outfile));
+        swapsuffix(buf, 1024, argv[i], ".myr", ".s");
+        gen(file, buf);
+        assem(buf);
     }
 
     return 0;
--- a/8/platform.h
+++ b/8/platform.h
@@ -1,9 +1,9 @@
 #if defined(__APPLE__) && defined(__MACH__)
 /* for OSX */
-#   define Assembler "as -arch i386 -g -o %s -"
+#   define Asmcmd "as -arch i386 -g -o %s %s"
 #   define Fprefix "_"
 #else
 /* Default to linux */
-#   define Assembler "as --32 -g -o %s -"
+#   define Asmcmd "as --32 -g -o %s %s"
 #   define Fprefix ""
 #endif
--- a/8/reduce.c
+++ b/8/reduce.c
@@ -895,7 +895,6 @@
     size_t nn, nfn, nblob;
     size_t i;
     FILE *fd;
-    char cmd[1024];
 
     /* declare useful constants */
     tyword = mkty(-1, Tyint);
@@ -928,11 +927,7 @@
         }
     }
 
-    sprintf(cmd, Assembler, out);
-    if (asmonly)
-      fd = fopen(out, "w");
-    else
-      fd = popen(cmd, "w");
+    fd = fopen(out, "w");
     if (!fd)
         die("Couldn't open fd %s", out);
 
@@ -940,9 +935,5 @@
         genblob(fd, blob[i], globls);
     for (i = 0; i < nfn; i++)
         genasm(fd, fn[i], globls);
-    fflush(fd);
-    if (asmonly)
-      fclose(fd);
-    else
-      pclose(fd);
+    fclose(fd);
 }
--- a/parse/parse.h
+++ b/parse/parse.h
@@ -394,6 +394,9 @@
 void wrbool(FILE *fd, int val);
 int rdbool(FILE *fd);
 
+/* suffix replacement */
+char *swapsuffix(char *buf, size_t sz, char *s, char *suf, char *swap);
+
 /* Options to control the compilation */
 extern int debug;
 extern int asmonly;
--- a/parse/util.c
+++ b/parse/util.c
@@ -338,3 +338,28 @@
     return rdbyte(fd);
 }
 
+char *swapsuffix(char *buf, size_t sz, char *s, char *suf, char *swap)
+{
+    size_t slen, suflen, swaplen;
+
+    slen = strlen(s);
+    suflen = strlen(suf);
+    swaplen = strlen(swap);
+
+    if (slen < suflen)
+        return NULL;
+    if (slen + swaplen >= sz)
+        die("swapsuffix: buf too small");
+
+    buf[0] = '\0';
+    if (suflen < slen && !strcmp(suf, &s[slen - suflen])) {
+        strncat(buf, s, slen - suflen);
+        strcat(buf, swap);
+    } else {
+        strncat(buf, s, slen);
+        strcat(buf, swap);
+    }
+
+    return buf;
+}
+