* src/SDCCmain.c (linkEdit): Added runtime path detection to the mcs51 port.
[fw/sdcc] / src / SDCCval.c
index 800f0046ee7c84fef74b049827bb2824ddb1c9a3..b5685aae5bc8ac7247b5b22836d88751312183db 100644 (file)
@@ -337,32 +337,31 @@ value *cheapestVal (value *val) {
   }
 
   if (SPEC_USIGN(val->type)) {
+    if (uval<=0xffff) {
+      SPEC_LONG(val->type)=0;
+      SPEC_CVAL(val->type).v_uint = uval;
+    }
     if (uval<=0xff) {
       SPEC_NOUN(val->type)=V_CHAR;
-      SPEC_LONG(val->type)=0;
-    } else {
-      if (uval<=0xffff) {
-       SPEC_LONG(val->type)=0;
-      }
     }
-  } else {
+  } else { // not unsigned
     if (sval<0) {
+      if (sval>=-32768) {
+       SPEC_LONG(val->type)=0;
+       SPEC_CVAL(val->type).v_int = sval & 0xffff;
+      }
       if (sval>=-128) {
        SPEC_NOUN(val->type)=V_CHAR;
+       SPEC_CVAL(val->type).v_int &= 0xff;
+      }
+    } else { // sval>=0
+      SPEC_USIGN(val->type)=1;
+      if (sval<=65535) {
        SPEC_LONG(val->type)=0;
-      } else {
-       if (sval>=-32768) {
-         SPEC_LONG(val->type)=0;
-       }
+       SPEC_CVAL(val->type).v_int = sval;
       }
-    } else {
-      if (sval<=127) {
+      if (sval<=255) {
        SPEC_NOUN(val->type)=V_CHAR;
-       SPEC_LONG(val->type)=0;
-      } else {
-       if (sval<=32767) {
-         SPEC_LONG(val->type)=0;
-       }
       }
     }
   }
@@ -394,9 +393,9 @@ value *
 constFloatVal (char *s)
 {
   value *val = newValue ();
-  float sval;
+  double sval;
 
-  if (sscanf (s, "%f", &sval) != 1)
+  if (sscanf (s, "%lf", &sval) != 1)
     {
       werror (E_INVALID_FLOAT_CONST, s);
       return constVal ("0");
@@ -420,7 +419,7 @@ value *constVal (char *s)
   short hex = 0, octal = 0;
   char scanFmt[10];
   int scI = 0;
-  unsigned long sval;
+  long sval;
 
   val = newValue ();           /* alloc space for value   */
 
@@ -431,10 +430,6 @@ value *constVal (char *s)
   SPEC_NOUN (val->type) = V_CHAR;
   SPEC_USIGN (val->type) = 1;
 
-  /* set the _long flag if 'lL' is found */
-  if (strchr (s, 'l') || strchr (s, 'L'))
-    SPEC_LONG (val->type) = 1;
-
   hex = ((strchr (s, 'x') || strchr (s, 'X')) ? 1 : 0);
 
   /* set the octal flag   */
@@ -444,6 +439,8 @@ value *constVal (char *s)
   /* create the scan string */
   scanFmt[scI++] = '%';
 
+  scanFmt[scI++] = 'l';
+
   if (octal)
     scanFmt[scI++] = 'o';
   else if (hex)
@@ -451,36 +448,57 @@ value *constVal (char *s)
   else
     scanFmt[scI++] = 'd';
 
-  scanFmt[scI++] = 'L';
   scanFmt[scI++] = '\0';
 
   sscanf (s, scanFmt, &sval);
 
+  /* Setup the flags first */
+  /* set the _long flag if 'lL' is found */
+  if (strchr (s, 'l') || strchr (s, 'L')) {
+    SPEC_NOUN (val->type) = V_INT;
+    SPEC_LONG (val->type) = 1;
+  }
+
   if (sval<0) { // "-28u" will still be signed and negative
     SPEC_USIGN (val->type) = 0;
-    if (sval<-32768) { // check if we have to promote to long
+    if (sval<-128) { // check if we have to promote to int
       SPEC_NOUN (val->type) = V_INT;
+    }
+    if (sval<-32768) { // check if we have to promote to long int
       SPEC_LONG (val->type) = 1;
-      SPEC_CVAL (val->type).v_long=sval;
-    } else {
-      SPEC_CVAL (val->type).v_int=sval;
-      if (sval<-128) { // check if we have to promote to int
-       SPEC_NOUN (val->type) = V_INT;
-      }
     }
-  } else {
-    if (sval>0xffff) { // check if we have to promote to long
+  } else { // >=0
+    if (sval>0xff) { // check if we have to promote to int
       SPEC_NOUN (val->type) = V_INT;
+    }
+    if (sval>0xffff) { // check if we have to promote to long int
       SPEC_LONG (val->type) = 1;
-      SPEC_CVAL (val->type).v_ulong=sval;
-    } else {
-      SPEC_CVAL (val->type).v_uint=sval;
-      if (sval>0xff) { // check if we have to promote to int
-       SPEC_NOUN (val->type) = V_INT;
-      }
     }
   }
 
+  if (SPEC_LONG (val->type))
+    {
+      if (SPEC_USIGN (val->type))
+        {
+          SPEC_CVAL (val->type).v_ulong = sval;
+        }
+      else
+        {
+          SPEC_CVAL (val->type).v_long = sval;
+        }
+    }
+  else
+    {
+      if (SPEC_USIGN (val->type))
+        {
+          SPEC_CVAL (val->type).v_uint = sval;
+        }
+      else
+        {
+          SPEC_CVAL (val->type).v_int = sval;
+        }
+    }
+
   return val;
 }
 
@@ -489,40 +507,34 @@ value *constVal (char *s)
     /param src Pointer to 'x' from start of hex character value
 */
 
-char hexEscape(char **src)
-
+unsigned char hexEscape(char **src)
 {
-char *s ;
-unsigned long value ;
-
-(*src)++ ;     /* Skip over the 'x' */
-s = *src ;     /* Save for error detection */
-
-value = strtol (*src, src, 16);
-
-if (s == *src) 
-  {
-  // no valid hex found
-  werror(E_INVALID_HEX);
-  } 
-
-else 
-  {
-  if (value > 255) 
-    {
-    werror(W_ESC_SEQ_OOR_FOR_CHAR);
+  char *s ;
+  unsigned long value ;
+  
+  (*src)++ ;   /* Skip over the 'x' */
+  s = *src ;   /* Save for error detection */
+  
+  value = strtol (*src, src, 16);
+  
+  if (s == *src) {
+      // no valid hex found
+      werror(E_INVALID_HEX);
+  } else {
+    if (value > 255) {
+      werror(W_ESC_SEQ_OOR_FOR_CHAR);
     }
   }
-
-return (char) value;
+  return (char) value;
 }
+
 /*------------------------------------------------------------------*/
 /* octalEscape - process an octal constant of max three digits      */
 /* return the octal value, throw a warning for illegal octal        */
 /* adjust src to point at the last proccesed char                   */
 /*------------------------------------------------------------------*/
 
-char octalEscape (char **str) {
+unsigned char octalEscape (char **str) {
   int digits;
   unsigned value=0;
 
@@ -811,20 +823,20 @@ charVal (char *s)
        case '5' :
        case '6' :
        case '7' :
-         SPEC_CVAL (val->type).v_int = octalEscape(&s);
+         SPEC_CVAL (val->type).v_uint = octalEscape(&s);
          break;
 
        case 'x':
-         SPEC_CVAL (val->type).v_int = hexEscape(&s) ;
+         SPEC_CVAL (val->type).v_uint = hexEscape(&s) ;
          break;
 
        default:
-         SPEC_CVAL (val->type).v_int = *s;
+         SPEC_CVAL (val->type).v_uint = (unsigned char)*s;
          break;
        }
     }
   else                         /* not a backslash */
-    SPEC_CVAL (val->type).v_int = *s;
+    SPEC_CVAL (val->type).v_uint = (unsigned char)*s;
 
   return val;
 }
@@ -863,23 +875,26 @@ floatFromVal (value * val)
 
   if (SPEC_NOUN (val->etype) == V_FLOAT)
     return (double) SPEC_CVAL (val->etype).v_float;
-  else
+
+  if (SPEC_LONG (val->etype))
     {
-      if (SPEC_LONG (val->etype))
-       {
-         if (SPEC_USIGN (val->etype))
-           return (double) SPEC_CVAL (val->etype).v_ulong;
-         else
-           return (double) SPEC_CVAL (val->etype).v_long;
-       }
+      if (SPEC_USIGN (val->etype))
+       return (double) SPEC_CVAL (val->etype).v_ulong;
       else
-       {
-         if (SPEC_USIGN (val->etype))
-           return (double) SPEC_CVAL (val->etype).v_uint;
-         else
-           return (double) SPEC_CVAL (val->etype).v_int;
-       }
+       return (double) SPEC_CVAL (val->etype).v_long;
     }
+  
+  if (SPEC_NOUN(val->etype)==V_INT) {
+    if (SPEC_USIGN (val->etype))
+      return (double) SPEC_CVAL (val->etype).v_uint;
+    else
+      return (double) SPEC_CVAL (val->etype).v_int;
+  } else { // SPEC_NOUN==V_CHAR
+    if (SPEC_USIGN (val->etype))
+      return (double) ((unsigned char)SPEC_CVAL (val->etype).v_uint);
+    else
+      return (double) ((signed char)SPEC_CVAL (val->etype).v_int);
+  }
 }
 
 
@@ -907,8 +922,13 @@ valUnaryPM (value * val)
            SPEC_CVAL (val->etype).v_uint = 0-SPEC_CVAL (val->etype).v_uint;
          else
            SPEC_CVAL (val->etype).v_int = -SPEC_CVAL (val->etype).v_int;
+         if (SPEC_NOUN (val->etype)==V_CHAR) {
+           SPEC_CVAL (val->etype).v_uint &= 0xff;
+         }
        }
     }
+  // -(unsigned 3) now really is signed
+  SPEC_USIGN(val->etype)=0;
   return val;
 }
 
@@ -932,6 +952,9 @@ valComplement (value * val)
        SPEC_CVAL (val->etype).v_uint = ~SPEC_CVAL (val->etype).v_uint;
       else
        SPEC_CVAL (val->etype).v_int = ~SPEC_CVAL (val->etype).v_int;
+      if (SPEC_NOUN (val->etype)==V_CHAR) {
+       SPEC_CVAL (val->etype).v_uint &= 0xff;
+      }
     }
   return val;
 }
@@ -956,6 +979,9 @@ valNot (value * val)
        SPEC_CVAL (val->etype).v_uint = !SPEC_CVAL (val->etype).v_uint;
       else
        SPEC_CVAL (val->etype).v_int = !SPEC_CVAL (val->etype).v_int;
+      if (SPEC_NOUN (val->etype)==V_CHAR) {
+       SPEC_CVAL (val->etype).v_uint &= 0xff;
+      }
     }
   return val;
 }
@@ -976,7 +1002,7 @@ valMult (value * lval, value * rval)
                           IS_FLOAT (rval->etype) ? V_FLOAT : V_INT);
   SPEC_SCLS (val->type) = S_LITERAL;   /* will remain literal */
   SPEC_USIGN (val->type) = (SPEC_USIGN (lval->etype) & SPEC_USIGN (rval->etype));
-  SPEC_LONG (val->type) = (SPEC_LONG (lval->etype) | SPEC_LONG (rval->etype));
+  SPEC_LONG (val->type) = 1;
 
   if (IS_FLOAT (val->type))
     SPEC_CVAL (val->type).v_float = floatFromVal (lval) * floatFromVal (rval);
@@ -991,15 +1017,6 @@ valMult (value * lval, value * rval)
            SPEC_CVAL (val->type).v_long = (long) floatFromVal (lval) *
              (long) floatFromVal (rval);
        }
-      else
-       {
-         if (SPEC_USIGN (val->type))
-           SPEC_CVAL (val->type).v_uint = (unsigned) floatFromVal (lval) *
-             (unsigned) floatFromVal (rval);
-         else
-           SPEC_CVAL (val->type).v_int = (int) floatFromVal (lval) *
-             (int) floatFromVal (rval);
-       }
     }
   return cheapestVal(val);
 }
@@ -1116,7 +1133,7 @@ valPlus (value * lval, value * rval)
     SPEC_USIGN (rval->etype) &&
     (floatFromVal(lval)+floatFromVal(rval))>=0;
     
-  SPEC_LONG (val->type) = (SPEC_LONG (lval->etype) | SPEC_LONG (rval->etype));
+  SPEC_LONG (val->type) = 1;
 
   if (IS_FLOAT (val->type))
     SPEC_CVAL (val->type).v_float = floatFromVal (lval) + floatFromVal (rval);
@@ -1131,16 +1148,6 @@ valPlus (value * lval, value * rval)
            SPEC_CVAL (val->type).v_long = (long) floatFromVal (lval) +
              (long) floatFromVal (rval);
        }
-      else
-       {
-         if (SPEC_USIGN (val->type)) {
-           SPEC_CVAL (val->type).v_uint = (unsigned) floatFromVal (lval) +
-             (unsigned) floatFromVal (rval);
-         } else {
-           SPEC_CVAL (val->type).v_int = (int) floatFromVal (lval) +
-             (int) floatFromVal (rval);
-         }
-       }
     }
   return cheapestVal(val);
 }
@@ -1209,7 +1216,7 @@ valShift (value * lval, value * rval, int lr)
   val->type = val->etype = newIntLink ();
   SPEC_SCLS (val->type) = S_LITERAL;   /* will remain literal */
   SPEC_USIGN (val->type) = (SPEC_USIGN (lval->etype) & SPEC_USIGN (rval->etype));
-  SPEC_LONG (val->type) = (SPEC_LONG (lval->etype) | SPEC_LONG (rval->etype));
+  SPEC_LONG (val->type) = 1;
 
   if (SPEC_LONG (val->type))
     {
@@ -1222,18 +1229,6 @@ valShift (value * lval, value * rval, int lr)
          (long) floatFromVal (lval) << (long) floatFromVal (rval) : \
          (long) floatFromVal (lval) >> (long) floatFromVal (rval);
     }
-  else
-    {
-      if (SPEC_USIGN (val->type)) {
-       SPEC_CVAL (val->type).v_uint = lr ? 
-         (unsigned) floatFromVal (lval) << (unsigned) floatFromVal (rval) :\
-         (unsigned) floatFromVal (lval) >> (unsigned) floatFromVal (rval);
-      } else {
-       SPEC_CVAL (val->type).v_int = lr ?
-         (int) floatFromVal (lval) << (int) floatFromVal (rval) : \
-         (int) floatFromVal (lval) >> (int) floatFromVal (rval);
-      }
-    }
 
   return cheapestVal(val);
 }
@@ -1433,17 +1428,12 @@ valCastLiteral (sym_link * dtype, double fval)
       else
        {
          if (SPEC_USIGN (val->etype))
-           if (SPEC_NOUN (val->etype)==V_CHAR) {
-             SPEC_CVAL (val->etype).v_uint = (unsigned char) fval;
-           } else {
-             SPEC_CVAL (val->etype).v_uint = (unsigned int) fval;
-           }
+           SPEC_CVAL (val->etype).v_uint = (unsigned short)fval;
          else
-           if (SPEC_NOUN (val->etype)==V_CHAR) {
-             SPEC_CVAL (val->etype).v_int = (char) fval;
-           } else {
-             SPEC_CVAL (val->etype).v_int = (int) fval;
-           }
+           SPEC_CVAL (val->etype).v_int = (short)fval;
+         if (SPEC_NOUN (val->etype)==V_CHAR) {
+           SPEC_CVAL (val->etype).v_uint &= 0xff; 
+         }
        }
     }
   return val;