]> git.sur5r.net Git - cc65/blobdiff - src/cc65/assignment.c
Reverted part of change 4108 that prevented an optimization step to find
[cc65] / src / cc65 / assignment.c
index df9cc50c8789d7f7bd4860e110812990220682c2..662128c84821d3bafacaafe8667f516ad2a0869f 100644 (file)
@@ -6,10 +6,10 @@
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 2002-2004 Ullrich von Bassewitz                                       */
-/*               Römerstrasse 52                                             */
-/*               D-70794 Filderstadt                                         */
-/* EMail:        uz@cc65.org                                                 */
+/* (C) 2002-2009, Ullrich von Bassewitz                                      */
+/*                Roemerstrasse 52                                           */
+/*                D-70794 Filderstadt                                        */
+/* EMail:         uz@cc65.org                                                */
 /*                                                                           */
 /*                                                                           */
 /* This software is provided 'as-is', without any expressed or implied       */
 
 
 /* cc65 */
+#include "asmcode.h"
 #include "assignment.h"
 #include "codegen.h"
 #include "datatype.h"
 #include "error.h"
 #include "expr.h"
+#include "loadexpr.h"
 #include "scanner.h"
 #include "stdnames.h"
 #include "typecmp.h"
 
 
 
-void Assignment (ExprDesc* lval)
+void Assignment (ExprDesc* Expr)
 /* Parse an assignment */
 {
-    ExprDesc lval2;
-    type* ltype = lval->Type;
+    ExprDesc Expr2;
+    Type* ltype = Expr->Type;
 
 
     /* We must have an lvalue for an assignment */
-    if (ED_IsRVal (lval)) {
+    if (ED_IsRVal (Expr)) {
         Error ("Invalid lvalue in assignment");
     }
 
@@ -86,7 +88,7 @@ void Assignment (ExprDesc* lval)
          * the former case, push the address only if really needed.
          */
         int UseReg = 1;
-        type* stype;
+        Type* stype;
         switch (Size) {
             case SIZEOF_CHAR:   stype = type_uchar;             break;
             case SIZEOF_INT:    stype = type_uint;              break;
@@ -94,38 +96,39 @@ void Assignment (ExprDesc* lval)
             default:            stype = ltype; UseReg = 0;      break;
         }
         if (UseReg) {
-            PushAddr (lval);
+            PushAddr (Expr);
         } else {
-           ExprLoad (CF_NONE, lval);
+            ED_MakeRVal (Expr);
+           LoadExpr (CF_NONE, Expr);
             g_push (CF_PTR | CF_UNSIGNED, 0);
         }
 
        /* Get the expression on the right of the '=' into the primary */
-       hie1 (&lval2);
+       hie1 (&Expr2);
 
         /* Check for equality of the structs */
-        if (TypeCmp (ltype, lval2.Type) < TC_STRICT_COMPATIBLE) {
+        if (TypeCmp (ltype, Expr2.Type) < TC_STRICT_COMPATIBLE) {
             Error ("Incompatible types");
         }
 
         /* Check if the right hand side is an lvalue */
-       if (ED_IsLVal (&lval2)) {
+       if (ED_IsLVal (&Expr2)) {
            /* We have an lvalue. Do we copy using the primary? */
             if (UseReg) {
                 /* Just use the replacement type */
-                lval2.Type = stype;
+                Expr2.Type = stype;
 
                 /* Load the value into the primary */
-                ExprLoad (CF_FORCECHAR, &lval2);
+                LoadExpr (CF_FORCECHAR, &Expr2);
 
                 /* Store it into the new location */
-                Store (lval, stype);
+                Store (Expr, stype);
 
             } else {
 
                 /* We will use memcpy. Push the address of the rhs */
-                ED_MakeRVal (&lval2);
-                ExprLoad (CF_NONE, &lval2);
+                ED_MakeRVal (&Expr2);
+                LoadExpr (CF_NONE, &Expr2);
 
                 /* Push the address (or whatever is in ax in case of errors) */
                 g_push (CF_PTR | CF_UNSIGNED, 0);
@@ -146,7 +149,7 @@ void Assignment (ExprDesc* lval)
              */
             if (UseReg) {
                 /* Do the store */
-                Store (lval, stype);
+                Store (Expr, stype);
             } else {
                 /* Print a diagnostic */
                 Error ("Structs of this size are not supported");
@@ -156,27 +159,112 @@ void Assignment (ExprDesc* lval)
 
         }
 
+    } else if (ED_IsBitField (Expr)) {
+
+        CodeMark AndPos;
+        CodeMark PushPos;
+
+        unsigned Mask;
+        unsigned Flags;
+
+        /* If the bit-field fits within one byte, do the following operations
+         * with bytes.
+         */
+        if (Expr->BitOffs / CHAR_BITS == (Expr->BitOffs + Expr->BitWidth - 1) / CHAR_BITS) {
+            Expr->Type = type_uchar;
+        }
+
+        /* Determine code generator flags */
+        Flags = TypeOf (Expr->Type);
+
+        /* Assignment to a bit field. Get the address on stack for the store. */
+        PushAddr (Expr);
+
+        /* Load the value from the location */
+        Expr->Flags &= ~E_BITFIELD;
+        LoadExpr (CF_NONE, Expr);
+
+        /* Mask unwanted bits */
+        Mask = (0x0001U << Expr->BitWidth) - 1U;
+        GetCodePos (&AndPos);
+        g_and (Flags | CF_CONST, ~(Mask << Expr->BitOffs));
+
+        /* Push it on stack */
+        GetCodePos (&PushPos);
+        g_push (Flags, 0);
+
+       /* Read the expression on the right side of the '=' */
+               MarkedExprWithCheck (hie1, &Expr2);
+
+       /* Do type conversion if necessary. Beware: Do not use char type
+         * here!
+         */
+       TypeConversion (&Expr2, ltype);
+
+        /* Special treatment if the value is constant. */
+        /* Beware: Expr2 may contain side effects, so there must not be
+         * code generated for Expr2.
+         */
+        if (ED_IsConstAbsInt (&Expr2) && ED_CodeRangeIsEmpty (&Expr2)) {
+
+            /* Get the value and apply the mask */
+            unsigned Val = (unsigned) (Expr2.IVal & Mask);
+
+            /* Since we will do the OR with a constant, we can remove the push */
+            RemoveCode (&PushPos);
+
+            /* If the value is equal to the mask now, all bits are one, and we
+             * can remove the mask operation from above.
+             */
+            if (Val == Mask) {
+                RemoveCode (&AndPos);
+            }
+
+            /* Generate the or operation */
+            g_or (Flags | CF_CONST, Val << Expr->BitOffs);
+
+        } else {
+
+            /* If necessary, load the value into the primary register */
+            LoadExpr (CF_NONE, &Expr2);
+
+            /* Apply the mask */
+            g_and (Flags | CF_CONST, Mask);
+
+            /* Shift it into the right position */
+            g_asl (Flags | CF_CONST, Expr->BitOffs);
+
+            /* Or both values */
+            g_or (Flags, 0);
+        }
+
+       /* Generate a store instruction */
+       Store (Expr, 0);
+
+        /* Restore the expression type */
+        Expr->Type = ltype;
+
     } else {
 
        /* Get the address on stack if needed */
-       PushAddr (lval);
+       PushAddr (Expr);
 
        /* Read the expression on the right side of the '=' */
-       hie1 (&lval2);
+       hie1 (&Expr2);
 
        /* Do type conversion if necessary */
-       TypeConversion (&lval2, ltype);
+       TypeConversion (&Expr2, ltype);
 
        /* If necessary, load the value into the primary register */
-       ExprLoad (CF_NONE, &lval2);
+       LoadExpr (CF_NONE, &Expr2);
 
        /* Generate a store instruction */
-       Store (lval, 0);
+       Store (Expr, 0);
 
     }
 
     /* Value is still in primary and not an lvalue */
-    lval->Flags = E_MEXPR | E_RVAL;
+    ED_MakeRValExpr (Expr);
 }