shithub: mc

Download patch

ref: 0d44442f0e5519ca65eb787896fbed5ac9246ab1
parent: c25a3ef79ba455adc1c9310867086ac6de416e1e
author: Ori Bernstein <[email protected]>
date: Sat Jan 19 09:00:14 EST 2013

Refactor the integer conversion code.

--- a/6/simp.c
+++ b/6/simp.c
@@ -722,15 +722,33 @@
     }
 }
 
+static Node *intconvert(Simp *s, Node *from, Type *to, int issigned)
+{
+    Node *r;
+    size_t fromsz, tosz;
+
+    fromsz = size(from);
+    tosz = tysize(to);
+    r = rval(s, from, NULL);
+    if (fromsz > tosz) {
+        r = mkexpr(from->line, Otrunc, r, NULL);
+    } else if (tosz > fromsz) {
+        if (issigned)
+            r = mkexpr(from->line, Oswiden, r, NULL);
+        else
+            r = mkexpr(from->line, Ozwiden, r, NULL);
+    }
+    r->expr.type = to;
+    return r;
+}
+
 static Node *simpcast(Simp *s, Node *val, Type *to)
 {
     Node *r;
     Type *t;
-    int issigned;
-    size_t fromsz, tosz;
 
-    issigned = 0;
     r = NULL;
+    /* do the type conversion */
     switch (tybase(to)->type) {
         case Tyint8: case Tyint16: case Tyint32: case Tyint64:
         case Tyuint8: case Tyuint16: case Tyuint32: case Tyuint64:
@@ -739,6 +757,7 @@
         case Typtr:
             t = tybase(exprtype(val));
             switch (t->type) {
+                /* ptr -> slice conversion is disallowed */
                 case Tyslice:
                     if (t->type == Typtr)
                         fatal(val->line, "Bad cast from %s to %s",
@@ -745,24 +764,16 @@
                               tystr(exprtype(val)), tystr(to));
                     r = slicebase(s, val, NULL);
                     break;
+                /* signed conversions */
                 case Tyint8: case Tyint16: case Tyint32: case Tyint64:
                 case Tyint: case Tylong:
-                    issigned = 1;
+                    r = intconvert(s, val, to, 1);
+                    break;
+                /* unsigned conversions */
                 case Tyuint8: case Tyuint16: case Tyuint32: case Tyuint64:
                 case Tyuint: case Tyulong: case Tychar: case Tybyte:
                 case Typtr:
-                    fromsz = size(val);
-                    tosz = tysize(to);
-                    r = rval(s, val, NULL);
-                    if (fromsz > tosz) {
-                        r = mkexpr(val->line, Otrunc, r, NULL);
-                    } else if (tosz > fromsz) {
-                        if (issigned)
-                            r = mkexpr(val->line, Oswiden, r, NULL);
-                        else
-                            r = mkexpr(val->line, Ozwiden, r, NULL);
-                    }
-                    r->expr.type = to;
+                    r = intconvert(s, val, to, 0);
                     break;
                 default:
                     fatal(val->line, "Bad cast from %s to %s",