shithub: mc

Download patch

ref: f5bfe1d30b12311b6347ca71eb00b4ca86ff7484
parent: 39292381c64ec2f8735cef1b0627d863edf8d43a
author: Ori Bernstein <[email protected]>
date: Tue Jun 19 13:17:47 EDT 2012

Generate object files by calling the assembler.

--- a/8/main.c
+++ b/8/main.c
@@ -16,6 +16,7 @@
 /* FIXME: move into one place...? */
 Node *file;
 int debug;
+int asmonly;
 char *outfile;
 char **incpaths;
 size_t nincpaths;
@@ -27,15 +28,45 @@
     printf("\t-I path\tAdd 'path' to use search path\n");
     printf("\t-d\tPrint debug dumps\n");
     printf("\t-o\tOutput to outfile\n");
+    printf("\t-S\tGenerate assembly instead of object code\n");
 }
 
+char *outfmt(char *buf, size_t sz, char *infile, char *outfile)
+{
+    char *p, *suffix;
+    size_t len;
+
+    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");
+    strncpy(buf, infile, len);
+    strcat(buf, suffix);
+
+    return buf;
+}
+
 int main(int argc, char **argv)
 {
     int opt;
     int i;
     Stab *globls;
+    char buf[1024];
 
-    while ((opt = getopt(argc, argv, "dho:I:")) != -1) {
+    while ((opt = getopt(argc, argv, "dhSo:I:")) != -1) {
         switch (opt) {
             case 'o':
                 outfile = optarg;
@@ -47,6 +78,9 @@
             case 'd':
                 debug++;
                 break;
+            case 'S':
+                asmonly++;
+                break;
             case 'I':
                 lappend(&incpaths, &nincpaths, optarg);
                 break;
@@ -73,7 +107,8 @@
         /* after all processing */
         if (debug)
             dump(file, stdout);
-        gen(file, "a.s");
+
+        gen(file, outfmt(buf, 1024, argv[i], outfile));
     }
 
     return 0;
--- a/8/platform.h
+++ b/8/platform.h
@@ -1,5 +1,8 @@
 #if defined(__APPLE__) && defined(__MACH__)
-#define Fprefix "_"
+/* for OSX */
+#   define Fprefix "_"
 #else
-#define Fprefix ""
+/* Default to linux */
+#   define Fprefix ""
 #endif
+#define Assembler "as --32 -g -o %s -"
--- a/8/reduce.c
+++ b/8/reduce.c
@@ -871,6 +871,7 @@
     size_t nn, nfn, nblob;
     size_t i;
     FILE *fd;
+    char cmd[1024];
 
     /* declare useful constants */
     tyword = mkty(-1, Tyint);
@@ -889,9 +890,6 @@
     /* We need to define all global variables before use */
     fillglobls(file->file.globls, globls);
 
-    fd = fopen(out, "w");
-    if (!fd)
-        die("Couldn't open fd %s", out);
     for (i = 0; i < nn; i++) {
         switch (n[i]->type) {
             case Nuse: /* nothing to do */ 
@@ -905,9 +903,21 @@
         }
     }
 
+    sprintf(cmd, Assembler, out);
+    if (asmonly)
+      fd = fopen(out, "w");
+    else
+      fd = popen(cmd, "w");
+    if (!fd)
+        die("Couldn't open fd %s", out);
+
     for (i = 0; i < nblob; i++)
         genblob(fd, blob[i], globls);
     for (i = 0; i < nfn; i++)
         genasm(fd, fn[i], globls);
-    fclose(fd);
+    fflush(fd);
+    if (asmonly)
+      fclose(fd);
+    else
+      pclose(fd);
 }
--- a/parse/parse.h
+++ b/parse/parse.h
@@ -397,6 +397,7 @@
 
 /* Options to control the compilation */
 extern int debug;
+extern int asmonly;
 extern char *outfile;
 extern char **incpaths;
 extern size_t nincpaths;
--- a/test/test.sh
+++ b/test/test.sh
@@ -2,7 +2,7 @@
 export PATH=.:$PATH
 export MC=../8/8m
 export MU=../util/muse
-export ASOPT="-g"
+export CC=cc
 
 function use {
     rm -f $1
@@ -14,8 +14,7 @@
     rm -f $1
     echo $MC $1.myr && \
     $MC $1.myr && \
-    mv a.s $1.s && \
-    cc $ASOPT -m32 -o $1 $1.s
+    $CC -g -m32 -o $1 $1.o
 }
 
 function prints {