shithub: mc

Download patch

ref: 01c1608893e34bf92640bee970fb35f01db97458
parent: d9bac20cb9000c9cab98349cb63152e7e90e73ea
author: Ori Bernstein <[email protected]>
date: Wed Jun 6 20:59:37 EDT 2012

Add support for div and mod.

--- a/8/insns.def
+++ b/8/insns.def
@@ -25,9 +25,10 @@
 Insn(Iadd,      "\tadd%t %r,%x\n",              0)
 Insn(Isub,      "\tsub%t %r,%x\n",              0)
 Insn(Imul,      "\tmul%t %r\n",              0)
-Insn(Iand,      "\tand%d %r,%x\n",              0)
-Insn(Ior,       "\tor%d %r,%x\n",               0)
-Insn(Ixor,      "\txor%d %r,%x\n",              0)
+Insn(Idiv,      "\tdiv%t %r\n",              0)
+Insn(Iand,      "\tand%t %r,%x\n",              0)
+Insn(Ior,       "\tor%t %r,%x\n",               0)
+Insn(Ixor,      "\txor%t %r,%x\n",              0)
 
 Insn(Itest,     "\ttest%t %r,%r\n",             0)
 Insn(Icmp,      "\tcmp%t %r,%r\n",              0)
--- a/8/isel.c
+++ b/8/isel.c
@@ -512,12 +512,13 @@
 Loc selexpr(Isel *s, Node *n)
 {
     Loc a, b, c, r;
-    Loc eax;
+    Loc eax, edx;
     Node **args;
 
     args = n->expr.args;
     r = (Loc){Locnone, };
     locreg(&eax, Reax);
+    locreg(&edx, Redx);
     switch (exprop(n)) {
         case Oadd:      r = binop(s, Iadd, args[0], args[1]); break;
         case Osub:      r = binop(s, Isub, args[0], args[1]); break;
@@ -534,8 +535,23 @@
             freereg(s, Redx);
             r = eax;
             break;
-        case Odiv:      die("Unimplemented op %s", opstr(exprop(n))); break;
-        case Omod:      die("Unimplemented op %s", opstr(exprop(n))); break;
+        case Odiv:
+        case Omod:
+            /* these get clobbered by the div insn */
+            claimreg(s, Reax);
+            claimreg(s, Redx);
+            a = selexpr(s, args[0]);
+            b = selexpr(s, args[1]);
+            b = inr(s, b);
+            g(s, Ixor, &edx, &edx, NULL);
+            g(s, Imov, &a, &eax, NULL);
+            g(s, Idiv, &b, NULL);
+            freereg(s, Redx);
+            if (exprop(n) == Odiv)
+                r = eax;
+            else
+                r = edx;
+            break;
         case Oneg:      die("Unimplemented op %s", opstr(exprop(n))); break;
 
         case Obor:      r = binop(s, Ior,  args[0], args[1]); break;
--- a/test/tests
+++ b/test/tests
@@ -1,6 +1,8 @@
 main	E	0
 add	E	53
 mul     E       84
+div     E       42
+mod     E       6
 struct1	E	12
 struct	E	42
 array	E	7