]> git.sur5r.net Git - cc65/blobdiff - src/cc65/loadexpr.c
Fixed two compiler warnings.
[cc65] / src / cc65 / loadexpr.c
index b39506a004efb1c513fa0821bbaeee52365220e7..99c5684d16f4242245b53b969377fb915ca31a24 100644 (file)
@@ -6,10 +6,10 @@
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 2004      Ullrich von Bassewitz                                       */
-/*               Römerstraße 52                                              */
-/*               D-70794 Filderstadt                                         */
-/* EMail:        uz@cc65.org                                                 */
+/* (C) 2004-2009, Ullrich von Bassewitz                                      */
+/*                Roemerstrasse 52                                           */
+/*                D-70794 Filderstadt                                        */
+/* EMail:         uz@cc65.org                                                */
 /*                                                                           */
 /*                                                                           */
 /* This software is provided 'as-is', without any expressed or implied       */
@@ -95,8 +95,17 @@ void LoadExpr (unsigned Flags, struct ExprDesc* Expr)
 {
     if (ED_IsLVal (Expr)) {
 
-               /* Dereferenced lvalue */
-               Flags |= TypeOf (Expr->Type);
+               /* Dereferenced lvalue. If this is a bit field its type is unsigned.
+         * But if the field is completely contained in the lower byte, we will
+         * throw away the high byte anyway and may therefore load just the
+         * low byte.
+         */
+        if (ED_IsBitField (Expr)) {
+            Flags |= (Expr->BitOffs + Expr->BitWidth <= CHAR_BITS)? CF_CHAR : CF_INT;
+            Flags |= CF_UNSIGNED;
+        } else {
+            Flags |= TypeOf (Expr->Type);
+        }
        if (ED_NeedsTest (Expr)) {
            Flags |= CF_TEST;
        }
@@ -145,6 +154,21 @@ void LoadExpr (unsigned Flags, struct ExprDesc* Expr)
                 Internal ("Invalid location in LoadExpr: 0x%04X", ED_GetLoc (Expr));
         }
 
+        /* Handle bit fields. The actual type may have been casted or
+         * converted, so be sure to always use unsigned ints for the
+         * operations.
+         */
+        if (ED_IsBitField (Expr)) {
+            unsigned F = CF_INT | CF_UNSIGNED | CF_CONST | (Flags & CF_TEST);
+            /* Shift right by the bit offset */
+            g_asr (F, Expr->BitOffs);
+            /* And by the width if the field doesn't end on an int boundary */
+            if (Expr->BitOffs + Expr->BitWidth != CHAR_BITS &&
+                Expr->BitOffs + Expr->BitWidth != INT_BITS) {
+                g_and (F, (0x0001U << Expr->BitWidth) - 1U);
+            }
+        }
+
         /* Expression was tested */
         ED_TestDone (Expr);
 
@@ -171,6 +195,7 @@ void LoadExpr (unsigned Flags, struct ExprDesc* Expr)
             ED_TestDone (Expr);
         }
     }
+
 }