shithub: mc

Download patch

ref: 1429ef6c0ba0dc6983461366995ff66683de1262
parent: d27c8ba011dde550e4af1c2ccea34810386761a5
author: Ori Bernstein <[email protected]>
date: Sun Jun 17 18:36:58 EDT 2012

Fix up blitting and copies.

    Sizes and such should be righter now.

--- a/8/isel.c
+++ b/8/isel.c
@@ -368,7 +368,7 @@
     /* Slightly funny loop condition: We might have trailing bytes
      * that we can't blit word-wise. */
     tmp = locreg(ModeL);
-    for (i = 0; i + 4 <= sz; i+= 4) {
+    for (i = 0; i < sz/4; i++) {
         src = locmem(i, sp, NULL, ModeL);
         dst = locmem(i, dp, NULL, ModeL);
         g(s, Imov, src, tmp, NULL);
@@ -376,7 +376,7 @@
     }
     /* now, the trailing bytes */
     tmp = locreg(ModeB);
-    for (; i < sz; i++) {
+    for (; i < sz%4; i++) {
         src = locmem(i, sp, NULL, ModeB);
         dst = locmem(i, dp, NULL, ModeB);
         g(s, Imov, src, tmp, NULL);
@@ -515,7 +515,6 @@
         case Ocall:
             r = gencall(s, n);
             break;
-        case Ocast: die("Unimplemented op %s", opstr(exprop(n))); break;
         case Ojmp:
             g(s, Ijmp, a = loclbl(args[0]), NULL);
             break;
@@ -547,7 +546,7 @@
         case Osubeq: case Omuleq: case Odiveq: case Omodeq: case Oboreq:
         case Obandeq: case Obxoreq: case Obsleq: case Obsreq: case Omemb:
         case Oslice: case Oidx: case Osize: case Numops:
-        case Oslbase: case Osllen:
+        case Oslbase: case Osllen: case Ocast:
             dump(n, stdout);
             die("Should not see %s in isel", opstr(exprop(n)));
             break;
@@ -742,12 +741,13 @@
 {
     char lbl[128];
     switch (v->lit.littype) {
-        case Lbool:     fprintf(fd, "\t.long %d\n", v->lit.boolval);    break;
+        case Lbool:     fprintf(fd, "\t.long %d\n", v->lit.boolval);     break;
         case Lchr:      fprintf(fd, "\t.byte %d\n",  v->lit.chrval);     break;
-        case Lint:      fprintf(fd, "\t.long %lld\n", v->lit.intval);     break;
+        case Lint:      fprintf(fd, "\t.long %lld\n", v->lit.intval);    break;
         case Lflt:      fprintf(fd, "\t.double %f\n", v->lit.fltval);    break;
-        case Lstr:      fprintf(fd, "\t.long $%s\n", genlblstr(lbl, 128));
+        case Lstr:      fprintf(fd, "\t.long %s\n", genlblstr(lbl, 128));
                         fprintf(fd, "\t.long %zd\n", strlen(v->lit.strval));
+                        fprintf(fd, "%s:\n", lbl);
                         writeblob(fd, v->lit.strval, strlen(v->lit.strval));
                         break;
         case Larray:
--- a/8/reduce.c
+++ b/8/reduce.c
@@ -53,6 +53,7 @@
 
 /* useful constants */
 static Node *one;
+static Node *zero;
 static Node *ptrsz;
 static Type *tyword;
 
@@ -115,8 +116,8 @@
 {
     Node *n;
 
+    assert(a != NULL && b != NULL);
     n = mkexpr(a->line, Ostor, a, b, NULL);
-    n->expr.type = b->expr.type;
     return n;
 }
 
@@ -499,6 +500,35 @@
     return t;
 }
 
+static Node *lowercast(Simp *s, Node *n)
+{
+    Node **args;
+    Node *r;
+
+    r = NULL;
+    args = n->expr.args;
+    switch (exprtype(n)->type) {
+        case Typtr:
+            switch (exprtype(args[0])->type) {
+                case Tyslice:
+                    r = slicebase(s, args[0], zero);
+                    break;
+                case Tyint:
+                    args[0]->expr.type = n->expr.type;
+                    r = args[0];
+                    break;
+                default:
+                    fatal(n->line, "Bad cast from %s to %s",
+                          tystr(exprtype(args[0])), tystr(exprtype(n)));
+            }
+            break;
+        default:
+            fatal(n->line, "Bad cast from %s to %s",
+                  tystr(exprtype(args[0])), tystr(exprtype(n)));
+    }
+    return r;
+}
+
 static Node *rval(Simp *s, Node *n)
 {
     Node *r; /* expression result */
@@ -542,12 +572,16 @@
                 r = slicelen(s, args[0]);
             } else if (exprtype(args[0])->type == Tyarray) {
                 assert(!strcmp(namestr(args[1]), "len"));
-                r = exprtype(n)->asize;
+                r = exprtype(args[0])->asize;
             } else {
                 t = membaddr(s, n);
                 r = load(t);
             }
             break;
+        case Ocast:
+            /* slice -> ptr cast */
+            r = lowercast(s, n);
+            break;
 
         /* fused ops:
          * foo ?= blah
@@ -664,7 +698,7 @@
 
 static Node *simp(Simp *s, Node *n)
 {
-    Node *r, *v;
+    Node *r, *t, *u, *v;
     size_t i;
 
     if (!n)
@@ -695,11 +729,19 @@
         case Ndecl:
 	    declarelocal(s, n);
 	    if (n->decl.init) {
-		v = rval(s, n->decl.init);
-		r = mkexpr(n->line, Ovar, n, NULL);
-		r->expr.did = n->decl.did;
-                r->expr.type = n->decl.type;
-		append(s, store(r, v));
+		t = mkexpr(n->line, Ovar, n, NULL);
+		t->expr.did = n->decl.did;
+                t->expr.type = n->decl.type;
+		u = rval(s, n->decl.init);
+                if (size(n) > 4) {
+                    t = addr(t, exprtype(n));
+                    u = addr(u, exprtype(n));
+                    v = word(n->line, size(n));
+                    r = mkexpr(n->line, Oblit, t, u, v, NULL);
+                } else {
+                    r = store(t, u);
+                }
+		append(s, r);
 	    }
             break;
         case Nlbl:
@@ -840,6 +882,7 @@
     /* declare useful constants */
     tyword = mkty(-1, Tyint);
     one = word(-1, 1);
+    zero = word(-1, 0);
     ptrsz = word(-1, 4);
 
     fn = NULL;
--- a/test/tests
+++ b/test/tests
@@ -22,6 +22,7 @@
 B struct1	E	12
 B struct	E	42
 B array		E	7
+B arraylen	E	12
 B call		E	42
 B loop		E	45
 B fib		E	21