shithub: mc

Download patch

ref: 2a1cde0c50b357ce46c785dd52dd20972f1d1054
parent: 0b7f1ba84f3385eacef2b44758ca9be69c760cef
author: Ori Bernstein <[email protected]>
date: Mon Jul 30 17:16:57 EDT 2012

Work towards x64 code gen.

--- a/8/isel.c
+++ b/8/isel.c
@@ -27,6 +27,7 @@
   [ModeB] = 'b',
   [ModeS] = 's',
   [ModeL] = 'l',
+  [ModeQ] = 'q',
   [ModeF] = 'f',
   [ModeD] = 'd'
 };
@@ -60,6 +61,7 @@
                 case 1: return ModeB; break;
                 case 2: return ModeS; break;
                 case 4: return ModeL; break;
+                case 8: return ModeQ; break;
             }
             break;
     }
@@ -147,7 +149,7 @@
         case Ovar:
             if (hthas(s->locs, n)) {
                 stkoff = (size_t)htget(s->locs, n);
-                l = locmem(-stkoff, locphysreg(Rebp), NULL, mode(n));
+                l = locmem(-stkoff, locphysreg(Rrbp), NULL, mode(n));
             } else if (hthas(s->globls, n)) {
                 l = locstrlbl(htget(s->globls, n));
             } else {
@@ -203,7 +205,8 @@
 
 static void movz(Isel *s, Loc *src, Loc *dst)
 {
-    if (src->mode == dst->mode)
+    if (src->mode == dst->mode ||
+        (src->mode == ModeL && dst->mode == ModeQ))
         g(s, Imov, src, dst, NULL);
     else
         g(s, Imovz, src, dst, NULL);
@@ -416,13 +419,13 @@
 static Loc *gencall(Isel *s, Node *n)
 {
     Loc *src, *dst, *arg, *fn;   /* values we reduced */
-    Loc *eax, *esp;       /* hard-coded registers */
+    Loc *rax, *rsp;       /* hard-coded registers */
     Loc *stkbump;        /* calculated stack offset */
     int argsz, argoff;
     size_t i;
 
-    esp = locphysreg(Resp);
-    eax = locphysreg(Reax);
+    rsp = locphysreg(Rrsp);
+    rax = locphysreg(Rrax);
     argsz = 0;
     /* Have to calculate the amount to bump the stack
      * pointer by in one pass first, otherwise if we push
@@ -434,7 +437,7 @@
         argsz += size(n->expr.args[i]);
     stkbump = loclit(argsz, ModeL);
     if (argsz)
-        g(s, Isub, stkbump, esp, NULL);
+        g(s, Isub, stkbump, rsp, NULL);
 
     /* Now, we can evaluate the arguments */
     argoff = 0;
@@ -444,9 +447,9 @@
             dst = locreg(ModeL);
             src = locreg(ModeL);
             g(s, Ilea, arg, src, NULL);
-            blit(s, esp, src, argoff, 0, size(n->expr.args[i]));
+            blit(s, rsp, src, argoff, 0, size(n->expr.args[i]));
         } else {
-            dst = locmem(argoff, esp, NULL, arg->mode);
+            dst = locmem(argoff, rsp, NULL, arg->mode);
             arg = inri(s, arg);
             stor(s, arg, dst);
         }
@@ -458,8 +461,8 @@
     else
         g(s, Icallind, fn, NULL);
     if (argsz)
-        g(s, Iadd, stkbump, esp, NULL);
-    return eax;
+        g(s, Iadd, stkbump, rsp, NULL);
+    return rax;
 }
 
 Loc *selexpr(Isel *s, Node *n)
@@ -772,33 +775,33 @@
 
 static void prologue(Isel *s, size_t sz)
 {
-    Loc *esp;
-    Loc *ebp;
+    Loc *rsp;
+    Loc *rbp;
     Loc *stksz;
 
-    esp = locphysreg(Resp);
-    ebp = locphysreg(Rebp);
-    stksz = loclit(sz, ModeL);
-    g(s, Ipush, ebp, NULL);
-    g(s, Imov, esp, ebp, NULL);
-    g(s, Isub, stksz, esp, NULL);
+    rsp = locphysreg(Rrsp);
+    rbp = locphysreg(Rrbp);
+    stksz = loclit(sz, ModeQ);
+    g(s, Ipush, rbp, NULL);
+    g(s, Imov, rsp, rbp, NULL);
+    g(s, Isub, stksz, rsp, NULL);
     s->stksz = stksz; /* need to update if we spill */
 }
 
 static void epilogue(Isel *s)
 {
-    Loc *esp, *ebp, *eax;
+    Loc *rsp, *rbp, *rax;
     Loc *ret;
 
-    esp = locphysreg(Resp);
-    ebp = locphysreg(Rebp);
-    eax = locphysreg(Reax);
+    rsp = locphysreg(Rrsp);
+    rbp = locphysreg(Rrbp);
+    rax = locphysreg(Rrax);
     if (s->ret) {
         ret = loc(s, s->ret);
-        movz(s, ret, eax);
+        g(s, Imov, ret, coreg(Rax, ret->mode), NULL);
     }
-    g(s, Imov, ebp, esp, NULL);
-    g(s, Ipop, ebp, NULL);
+    g(s, Imov, rbp, rsp, NULL);
+    g(s, Ipop, rbp, NULL);
     g(s, Iret, NULL);
 }
 
--- a/8/platform.h
+++ b/8/platform.h
@@ -1,9 +1,9 @@
 #if defined(__APPLE__) && defined(__MACH__)
 /* for OSX */
-#   define Asmcmd "as -arch i386 -g -o %s %s"
+#   define Asmcmd "as -g -o %s %s"
 #   define Fprefix "_"
 #else
 /* Default to linux */
-#   define Asmcmd "as --32 -g -o %s %s"
+#   define Asmcmd "as -g -o %s %s"
 #   define Fprefix ""
 #endif
--- a/8/ra.c
+++ b/8/ra.c
@@ -36,13 +36,23 @@
 #undef Def
 };
 
-Reg regmap[6][Nmode] = {
-    [0] = {Rnone, Ral, Rax, Reax},
-    [1] = {Rnone, Rcl, Rcx, Recx},
-    [2] = {Rnone, Rdl, Rdx, Redx},
-    [3] = {Rnone, Rbl, Rbx, Rebx},
-    [4] = {Rnone, Rnone, Rnone, Resp},
-    [5] = {Rnone, Rnone, Rnone, Rebp},
+Reg regmap[][Nmode] = {
+    [0]  = {Rnone, Ral, Rax, Reax, Rrax},
+    [1]  = {Rnone, Rcl, Rcx, Recx, Rrcx},
+    [2]  = {Rnone, Rdl, Rdx, Redx, Rrdx},
+    [3]  = {Rnone, Rbl, Rbx, Rebx, Rrbx},
+    [4]  = {Rnone, Rsil, Rsi, Resi, Rrsi},
+    [5]  = {Rnone, Rdil, Rdi, Redi, Rrdi},
+    [6]  = {Rnone, R8b, R8w, R8d, R8},
+    [7]  = {Rnone, R9b, R9w, R9d, R9},
+    [8]  = {Rnone, R10b, R10w, R10d, R10},
+    [9]  = {Rnone, R11b, R11w, R11d, R11},
+    [10]  = {Rnone, R12b, R12w, R12d, R12},
+    [11]  = {Rnone, R13b, R13w, R13d, R13},
+    [12]  = {Rnone, R14b, R14w, R14d, R14},
+    [13]  = {Rnone, R15b, R15w, R15d, R15},
+    [14]  = {Rnone, Rnone, Rnone, Resp},
+    [15]  = {Rnone, Rnone, Rnone, Rebp},
 };
 
 int colourmap[Nreg] = {
--- a/test/test.sh
+++ b/test/test.sh
@@ -15,7 +15,7 @@
     rm -f $1 $1.o $1.s $1.use
     echo $MC $1.myr && \
     $MC $1.myr && \
-    $CC -g -m32 -o $1 $1.o
+    $CC -g -o $1 $1.o
 }
 
 function prints {