Fixed bitfield initialization in code space
authorsandeep <sandeep@4a8a32a2-be11-0410-ad9d-d568d2c75423>
Mon, 26 Feb 2001 00:33:54 +0000 (00:33 +0000)
committersandeep <sandeep@4a8a32a2-be11-0410-ad9d-d568d2c75423>
Mon, 26 Feb 2001 00:33:54 +0000 (00:33 +0000)
git-svn-id: https://sdcc.svn.sourceforge.net/svnroot/sdcc/trunk/sdcc@656 4a8a32a2-be11-0410-ad9d-d568d2c75423

src/SDCCglue.c
src/SDCCsymt.c
src/mcs51/gen.c

index 31c1394a4b093b06da1f17fc82f6515abeff90ef..3ac3ddb4d3c99cb14287bee1c3603ad318c275b4 100644 (file)
@@ -102,49 +102,46 @@ copyFile (FILE * dest, FILE * src)
 char *
 aopLiteralLong (value * val, int offset, int size)
 {
-  char *rs;
-  union
-    {
-      float f;
-      unsigned char c[4];
-    }
-  fl;
-
-  /* if it is a float then it gets tricky */
-  /* otherwise it is fairly simple */
-  if (!IS_FLOAT (val->type))
-    {
-      unsigned long v = floatFromVal (val);
-
-      v >>= (offset * 8);
-      switch (size)
-       {
-       case 1:
-         tsprintf (buffer, "!immedbyte", (unsigned int) v & 0xff);
-         break;
-       case 2:
-         tsprintf (buffer, "!immedword", (unsigned int) v & 0xffff);
-         break;
-       default:
-         /* Hmm.  Too big for now. */
-         assert (0);
+       char *rs;
+       union {
+               float f;
+               unsigned char c[4];
+       }
+       fl;
+
+       /* if it is a float then it gets tricky */
+       /* otherwise it is fairly simple */
+       if (!IS_FLOAT (val->type)) {
+               unsigned long v = floatFromVal (val);
+
+               v >>= (offset * 8);
+               switch (size) {
+               case 1:
+                       tsprintf (buffer, "!immedbyte", (unsigned int) v & 0xff);
+                       break;
+               case 2:
+                       tsprintf (buffer, "!immedword", (unsigned int) v & 0xffff);
+                       break;
+               default:
+                       /* Hmm.  Too big for now. */
+                       assert (0);
+               }
+               rs = Safe_calloc (1, strlen (buffer) + 1);
+               return strcpy (rs, buffer);
        }
-      rs = Safe_calloc (1, strlen (buffer) + 1);
-      return strcpy (rs, buffer);
-    }
 
-  /* PENDING: For now size must be 1 */
-  assert (size == 1);
+       /* PENDING: For now size must be 1 */
+       assert (size == 1);
 
-  /* it is type float */
-  fl.f = (float) floatFromVal (val);
+       /* it is type float */
+       fl.f = (float) floatFromVal (val);
 #ifdef _BIG_ENDIAN
-  tsprintf (buffer, "!immedbyte", fl.c[3 - offset]);
+       tsprintf (buffer, "!immedbyte", fl.c[3 - offset]);
 #else
-  tsprintf (buffer, "!immedbyte", fl.c[offset]);
+       tsprintf (buffer, "!immedbyte", fl.c[offset]);
 #endif
-  rs = Safe_calloc (1, strlen (buffer) + 1);
-  return strcpy (rs, buffer);
+       rs = Safe_calloc (1, strlen (buffer) + 1);
+       return strcpy (rs, buffer);
 }
 
 /*-----------------------------------------------------------------*/
@@ -153,7 +150,7 @@ aopLiteralLong (value * val, int offset, int size)
 char *
 aopLiteral (value * val, int offset)
 {
-  return aopLiteralLong (val, offset, 1);
+       return aopLiteralLong (val, offset, 1);
 }
 
 /*-----------------------------------------------------------------*/
@@ -484,43 +481,90 @@ printGPointerType (FILE * oFile, const char *name,
 void 
 printIvalType (sym_link * type, initList * ilist, FILE * oFile)
 {
-  value *val;
+       value *val;
 
-  /* if initList is deep */
-  if (ilist->type == INIT_DEEP)
-    ilist = ilist->init.deep;
+       /* if initList is deep */
+       if (ilist->type == INIT_DEEP)
+               ilist = ilist->init.deep;
 
-  val = list2val (ilist);
-  switch (getSize (type))
-    {
-    case 1:
-      if (!val)
-       tfprintf (oFile, "\t!db !constbyte\n", 0);
-      else
-       tfprintf (oFile, "\t!dbs\n",
-                 aopLiteral (val, 0));
-      break;
+       val = list2val (ilist);
+       switch (getSize (type)) {
+       case 1:
+               if (!val)
+                       tfprintf (oFile, "\t!db !constbyte\n", 0);
+               else
+                       tfprintf (oFile, "\t!dbs\n",
+                                 aopLiteral (val, 0));
+               break;
 
-    case 2:
-      if (port->use_dw_for_init)
-       tfprintf (oFile, "\t!dws\n", aopLiteralLong (val, 0, 2));
-      else
-       fprintf (oFile, "\t.byte %s,%s\n", aopLiteral (val, 0), aopLiteral (val, 1));
-      break;
-    case 4:
-      if (!val)
-       {
-         tfprintf (oFile, "\t!dw !constword\n", 0);
-         tfprintf (oFile, "\t!dw !constword\n", 0);
+       case 2:
+               if (port->use_dw_for_init)
+                       tfprintf (oFile, "\t!dws\n", aopLiteralLong (val, 0, 2));
+               else
+                       fprintf (oFile, "\t.byte %s,%s\n", aopLiteral (val, 0), aopLiteral (val, 1));
+               break;
+       case 4:
+               if (!val) {
+                       tfprintf (oFile, "\t!dw !constword\n", 0);
+                       tfprintf (oFile, "\t!dw !constword\n", 0);
+               }
+               else {
+                       fprintf (oFile, "\t.byte %s,%s,%s,%s\n",
+                                aopLiteral (val, 0), aopLiteral (val, 1),
+                                aopLiteral (val, 2), aopLiteral (val, 3));
+               }
+               break;
        }
-      else
-       {
-         fprintf (oFile, "\t.byte %s,%s,%s,%s\n",
-                  aopLiteral (val, 0), aopLiteral (val, 1),
-                  aopLiteral (val, 2), aopLiteral (val, 3));
+}
+
+/*-----------------------------------------------------------------*/
+/* printIvalBitFields - generate initializer for bitfields         */
+/*-----------------------------------------------------------------*/
+void printIvalBitFields(symbol **sym, initList **ilist, FILE * oFile)
+{
+       value *val ;
+       symbol *lsym = *sym;
+       initList *lilist = *ilist ;
+       unsigned long ival = 0;
+       int size =0;
+
+       
+       do {
+               unsigned long i;
+               val = list2val(lilist);
+               if (size) {
+                       if (SPEC_BLEN(lsym->etype) > 8) {
+                               size += ((SPEC_BLEN (lsym->etype) / 8) + 
+                                        (SPEC_BLEN (lsym->etype) % 8 ? 1 : 0));
+                       }
+               } else {
+                       size = ((SPEC_BLEN (lsym->etype) / 8) + 
+                                (SPEC_BLEN (lsym->etype) % 8 ? 1 : 0));
+               }
+               i = (unsigned long)floatFromVal(val);
+               i <<= SPEC_BSTR (lsym->etype);
+               ival |= i;
+               if (! ( lsym->next &&
+                       (IS_BITFIELD(lsym->next->type)) &&
+                       (SPEC_BSTR(lsym->next->etype)))) break;
+               lsym = lsym->next;
+               lilist = lilist->next;
+       } while (1);
+       switch (size) {
+       case 1:
+               tfprintf (oFile, "\t!db !constbyte\n",ival);
+               break;
+
+       case 2:
+               tfprintf (oFile, "\t!dw !constword\n",ival);
+               break;
+       case 4:
+               tfprintf (oFile, "\t!db  !constword,!constword\n",
+                        (ival >> 8) & 0xffff, (ival & 0xffff));
+               break;
        }
-      break;
-    }
+       *sym = lsym;
+       *ilist = lilist;
 }
 
 /*-----------------------------------------------------------------*/
@@ -530,22 +574,25 @@ void
 printIvalStruct (symbol * sym, sym_link * type,
                 initList * ilist, FILE * oFile)
 {
-  symbol *sflds;
-  initList *iloop;
-
-  sflds = SPEC_STRUCT (type)->fields;
-  if (ilist->type != INIT_DEEP)
-    {
-      werror (E_INIT_STRUCT, sym->name);
-      return;
-    }
+       symbol *sflds;
+       initList *iloop;
 
-  iloop = ilist->init.deep;
+       sflds = SPEC_STRUCT (type)->fields;
+       if (ilist->type != INIT_DEEP) {
+               werror (E_INIT_STRUCT, sym->name);
+               return;
+       }
 
-  for (; sflds; sflds = sflds->next, iloop = (iloop ? iloop->next : NULL))
-    printIval (sflds, sflds->type, iloop, oFile);
+       iloop = ilist->init.deep;
 
-  return;
+       for (; sflds; sflds = sflds->next, iloop = (iloop ? iloop->next : NULL)) {
+               if (IS_BITFIELD(sflds->type)) {
+                       printIvalBitFields(&sflds,&iloop,oFile);
+               } else {
+                       printIval (sflds, sflds->type, iloop, oFile);
+               }
+       }
+       return;
 }
 
 /*-----------------------------------------------------------------*/
@@ -567,10 +614,6 @@ printIvalChar (sym_link * type, initList * ilist, FILE * oFile, char *s)
          if (!DCL_ELEM (type))
            DCL_ELEM (type) = strlen (SPEC_CVAL (val->etype).v_char) + 1;
 
-         /* if size mismatch  */
-/*      if (DCL_ELEM (type) < ((int) strlen (SPEC_CVAL (val->etype).v_char) + 1)) */
-/*    werror (E_ARRAY_BOUND); */
-
          printChar (oFile, SPEC_CVAL (val->etype).v_char, DCL_ELEM (type));
 
          if ((remain = (DCL_ELEM (type) - strlen (SPEC_CVAL (val->etype).v_char) - 1)) > 0)
index 2f1ba81a62160417e838af9daa66748cfddec5c8..774e132ae4b28e0901145928bf7e9ae87e0944c5 100644 (file)
@@ -905,81 +905,72 @@ getStructElement (structdef * sdef, symbol * sym)
 int 
 compStructSize (int su, structdef * sdef)
 {
-  int sum = 0, usum = 0;
-  int bitOffset = 0;
-  symbol *loop;
-
-  /* for the identifiers  */
-  loop = sdef->fields;
-  while (loop)
-    {
-
-      /* create the internal name for this variable */
-      sprintf (loop->rname, "_%s", loop->name);
-      loop->offset = (su == UNION ? sum = 0 : sum);
-      SPEC_VOLATILE (loop->etype) |= (su == UNION ? 1 : 0);
-
-      /* if this is a bit field  */
-      if (loop->bitVar)
-       {
-
-         /* change it to a unsigned bit */
-         SPEC_NOUN (loop->etype) = V_BIT;
-         SPEC_USIGN (loop->etype) = 1;
-         /* check if this fit into the remaining   */
-         /* bits of this byte else align it to the */
-         /* next byte boundary                     */
-         if ((SPEC_BLEN (loop->etype) = loop->bitVar) <= (8 - bitOffset))
-           {
-             SPEC_BSTR (loop->etype) = bitOffset;
-             if ((bitOffset += (loop->bitVar % 8)) == 8)
-               sum++;
+    int sum = 0, usum = 0;
+    int bitOffset = 0;
+    symbol *loop;
+
+    /* for the identifiers  */
+    loop = sdef->fields;
+    while (loop) {
+
+       /* create the internal name for this variable */
+       sprintf (loop->rname, "_%s", loop->name);
+       loop->offset = (su == UNION ? sum = 0 : sum);
+       SPEC_VOLATILE (loop->etype) |= (su == UNION ? 1 : 0);
+
+       /* if this is a bit field  */
+       if (loop->bitVar) {
+
+           /* change it to a unsigned bit */
+           SPEC_NOUN (loop->etype) = V_BIT;
+           SPEC_USIGN (loop->etype) = 1;
+           /* check if this fit into the remaining   */
+           /* bits of this byte else align it to the */
+           /* next byte boundary                     */
+           if ((SPEC_BLEN (loop->etype) = loop->bitVar) <= (8 - bitOffset)) {
+               SPEC_BSTR (loop->etype) = bitOffset;
+               if ((bitOffset += (loop->bitVar % 8)) == 8)
+                   sum++;
            }
-         else
-           /* does not fit */
-           {
-             bitOffset = 0;
-             SPEC_BSTR (loop->etype) = bitOffset;
-             sum += (loop->bitVar / 8);
-             bitOffset += (loop->bitVar % 8);
+           else /* does not fit */ {
+               bitOffset = 0;
+               SPEC_BSTR (loop->etype) = bitOffset;
+               sum += (loop->bitVar / 8);
+               bitOffset += (loop->bitVar % 8);
            }
-         /* if this is the last field then pad */
-         if (!loop->next && bitOffset && bitOffset != 8)
-           {
-             bitOffset = 0;
-             sum++;
+           /* if this is the last field then pad */
+           if (!loop->next && bitOffset && bitOffset != 8) {
+               bitOffset = 0;
+               sum++;
            }
        }
-      else
-       {
-         checkDecl (loop);
-         sum += getSize (loop->type);
+       else {
+           checkDecl (loop);
+           sum += getSize (loop->type);
        }
 
-      /* if function then do the arguments for it */
-      if (funcInChain (loop->type))
-       {
-         processFuncArgs (loop, 1);
+       /* if function then do the arguments for it */
+       if (funcInChain (loop->type)) {
+           processFuncArgs (loop, 1);
        }
 
-      loop = loop->next;
+       loop = loop->next;
 
-      /* if this is not a bitfield but the */
-      /* previous one was and did not take */
-      /* the whole byte then pad the rest  */
-      if ((loop && !loop->bitVar) && bitOffset)
-       {
-         bitOffset = 0;
-         sum++;
+       /* if this is not a bitfield but the */
+       /* previous one was and did not take */
+       /* the whole byte then pad the rest  */
+       if ((loop && !loop->bitVar) && bitOffset) {
+           bitOffset = 0;
+           sum++;
        }
 
-      /* if union then size = sizeof larget field */
-      if (su == UNION)
-       usum = max (usum, sum);
+       /* if union then size = sizeof larget field */
+       if (su == UNION)
+           usum = max (usum, sum);
 
     }
 
-  return (su == UNION ? usum : sum);
+    return (su == UNION ? usum : sum);
 }
 
 /*------------------------------------------------------------------*/
index f4740450e07e346328eae623d93e6bc5b07c8e86..ac7e903300355ca6bb1f8a98ce07d90d47057c3e 100644 (file)
@@ -6630,7 +6630,7 @@ genUnpackBits (operand * result, char *rname, int ptype)
 
     case CPOINTER:
       emitcode ("clr", "a");
-      emitcode ("movc", "a", "@a+dptr");
+      emitcode ("movc", "a,%s", "@a+dptr");
       break;
 
     case GPOINTER: