]> git.sur5r.net Git - cc65/blobdiff - src/cc65/exprdesc.c
Merge remote-tracking branch 'upstream/master' into a5200
[cc65] / src / cc65 / exprdesc.c
index 9b9a0a6fb4a90c69133785252e0b18f9e062f8a9..a5787d96745dc05c68ead8eb27fc5dcd85969175 100644 (file)
@@ -6,10 +6,10 @@
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 2002-2004 Ullrich von Bassewitz                                       */
-/*               Römerstraße 52                                              */
-/*               D-70794 Filderstadt                                         */
-/* EMail:        uz@cc65.org                                                 */
+/* (C) 2002-2010, Ullrich von Bassewitz                                      */
+/*                Roemerstrasse 52                                           */
+/*                D-70794 Filderstadt                                        */
+/* EMail:         uz@cc65.org                                                */
 /*                                                                           */
 /*                                                                           */
 /* This software is provided 'as-is', without any expressed or implied       */
 
 /* common */
 #include "check.h"
-#include "xsprintf.h"
+#include "strbuf.h"
 
 /* cc65 */
 #include "asmlabel.h"
 #include "datatype.h"
 #include "error.h"
+#include "exprdesc.h"
 #include "stackptr.h"
 #include "symentry.h"
 #include "exprdesc.h"
@@ -48,7 +49,7 @@
 
 
 /*****************************************************************************/
-/*                                  Code                                    */
+/*                                   Code                                    */
 /*****************************************************************************/
 
 
 ExprDesc* ED_Init (ExprDesc* Expr)
 /* Initialize an ExprDesc */
 {
-    Expr->Sym   = 0;
-    Expr->Type  = 0;
-    Expr->Flags = 0;
-    Expr->Name  = 0;
-    Expr->IVal  = 0;
-    Expr->FVal  = 0.0;
+    Expr->Sym       = 0;
+    Expr->Type      = 0;
+    Expr->Flags     = 0;
+    Expr->Name      = 0;
+    Expr->IVal      = 0;
+    Expr->FVal      = FP_D_Make (0.0);
+    Expr->LVal      = 0;
+    Expr->BitOffs   = 0;
+    Expr->BitWidth  = 0;
     return Expr;
 }
 
 
 
+void ED_MakeBitField (ExprDesc* Expr, unsigned BitOffs, unsigned BitWidth)
+/* Make this expression a bit field expression */
+{
+    Expr->Flags   |= E_BITFIELD;
+    Expr->BitOffs  = BitOffs;
+    Expr->BitWidth = BitWidth;
+}
+
+
+
+void ED_SetCodeRange (ExprDesc* Expr, const CodeMark* Start, const CodeMark* End)
+/* Set the code range for this expression */
+{
+    Expr->Flags |= E_HAVE_MARKS;
+    Expr->Start = *Start;
+    Expr->End   = *End;
+}
+
+
+
+int ED_CodeRangeIsEmpty (const ExprDesc* Expr)
+/* Return true if no code was output for this expression */
+{
+    /* We must have code marks */
+    PRECONDITION (Expr->Flags & E_HAVE_MARKS);
+
+    return CodeRangeIsEmpty (&Expr->Start, &Expr->End);
+}
+
+
+
 const char* ED_GetLabelName (const ExprDesc* Expr, long Offs)
 /* Return the assembler label name of the given expression. Beware: This
  * function may use a static buffer, so the name may get "lost" on the second
  * call to the function.
  */
 {
-    static char Buf[256];
+    static StrBuf Buf = STATIC_STRBUF_INITIALIZER;
 
     /* Expr may have it's own offset, adjust Offs accordingly */
     Offs += Expr->IVal;
@@ -83,36 +118,31 @@ const char* ED_GetLabelName (const ExprDesc* Expr, long Offs)
 
         case E_LOC_ABS:
             /* Absolute: numeric address or const */
-           xsprintf (Buf, sizeof (Buf), "$%04X", (int)(Offs & 0xFFFF));
+            SB_Printf (&Buf, "$%04X", (int)(Offs & 0xFFFF));
             break;
 
         case E_LOC_GLOBAL:
         case E_LOC_STATIC:
             /* Global or static variable */
-           if (Offs) {
-               xsprintf (Buf, sizeof (Buf), "%s%+ld",
-                          SymGetAsmName (Expr->Sym), Offs);
-           } else {
-               xsprintf (Buf, sizeof (Buf), "%s",
-                          SymGetAsmName (Expr->Sym));
-           }
+            if (Offs) {
+                SB_Printf (&Buf, "%s%+ld", SymGetAsmName (Expr->Sym), Offs);
+            } else {
+                SB_Printf (&Buf, "%s", SymGetAsmName (Expr->Sym));
+            }
             break;
 
         case E_LOC_REGISTER:
             /* Register variable */
-           xsprintf (Buf, sizeof (Buf), "regbank+%u",
-                      (unsigned)((Offs + Expr->Name) & 0xFFFFU));
+            SB_Printf (&Buf, "regbank+%u", (unsigned)((Offs + Expr->Name) & 0xFFFFU));
             break;
 
         case E_LOC_LITERAL:
             /* Literal in the literal pool */
-           if (Offs) {
-               xsprintf (Buf, sizeof (Buf), "%s%+ld",
-                          LocalLabelName (Expr->Name), Offs);
-           } else {
-                       xsprintf (Buf, sizeof (Buf), "%s",
-                          LocalLabelName (Expr->Name));
-           }
+            if (Offs) {
+                SB_Printf (&Buf, "%s%+ld", LocalLabelName (Expr->Name), Offs);
+            } else {
+                SB_Printf (&Buf, "%s", LocalLabelName (Expr->Name));
+            }
             break;
 
         default:
@@ -120,7 +150,7 @@ const char* ED_GetLabelName (const ExprDesc* Expr, long Offs)
     }
 
     /* Return a pointer to the static buffer */
-    return Buf;
+    return SB_GetConstBuf (&Buf);
 }
 
 
@@ -138,15 +168,15 @@ int ED_GetStackOffs (const ExprDesc* Expr, int Offs)
 
 
 
-ExprDesc* ED_MakeConstAbs (ExprDesc* Expr, long Value, type* Type)
+ExprDesc* ED_MakeConstAbs (ExprDesc* Expr, long Value, Type* Type)
 /* Make Expr an absolute const with the given value and type. */
 {
     Expr->Sym   = 0;
     Expr->Type  = Type;
-    Expr->Flags = E_LOC_ABS | E_RTYPE_RVAL;
+    Expr->Flags = E_LOC_ABS | E_RTYPE_RVAL | (Expr->Flags & E_HAVE_MARKS);
     Expr->Name  = 0;
     Expr->IVal  = Value;
-    Expr->FVal  = 0.0;
+    Expr->FVal  = FP_D_Make (0.0);
     return Expr;
 }
 
@@ -157,10 +187,10 @@ ExprDesc* ED_MakeConstAbsInt (ExprDesc* Expr, long Value)
 {
     Expr->Sym   = 0;
     Expr->Type  = type_int;
-    Expr->Flags = E_LOC_ABS | E_RTYPE_RVAL;
+    Expr->Flags = E_LOC_ABS | E_RTYPE_RVAL | (Expr->Flags & E_HAVE_MARKS);
     Expr->Name  = 0;
     Expr->IVal  = Value;
-    Expr->FVal  = 0.0;
+    Expr->FVal  = FP_D_Make (0.0);
     return Expr;
 }
 
@@ -172,11 +202,11 @@ ExprDesc* ED_MakeRValExpr (ExprDesc* Expr)
  */
 {
     Expr->Sym   = 0;
-    Expr->Flags &= ~(E_MASK_LOC | E_MASK_RTYPE | E_NEED_TEST | E_CC_SET);
+    Expr->Flags &= ~(E_MASK_LOC | E_MASK_RTYPE | E_BITFIELD | E_NEED_TEST | E_CC_SET);
     Expr->Flags |= (E_LOC_EXPR | E_RTYPE_RVAL);
     Expr->Name  = 0;
     Expr->IVal  = 0;    /* No offset */
-    Expr->FVal  = 0.0;
+    Expr->FVal  = FP_D_Make (0.0);
     return Expr;
 }
 
@@ -188,11 +218,11 @@ ExprDesc* ED_MakeLValExpr (ExprDesc* Expr)
  */
 {
     Expr->Sym   = 0;
-    Expr->Flags &= ~(E_MASK_LOC | E_MASK_RTYPE | E_NEED_TEST | E_CC_SET);
+    Expr->Flags &= ~(E_MASK_LOC | E_MASK_RTYPE | E_BITFIELD | E_NEED_TEST | E_CC_SET);
     Expr->Flags |= (E_LOC_EXPR | E_RTYPE_LVAL);
     Expr->Name  = 0;
     Expr->IVal  = 0;    /* No offset */
-    Expr->FVal  = 0.0; 
+    Expr->FVal  = FP_D_Make (0.0);
     return Expr;
 }
 
@@ -209,16 +239,6 @@ int ED_IsConst (const ExprDesc* Expr)
 
 
 
-int ED_IsConstAbs (const ExprDesc* Expr)
-/* Return true if the expression denotes a constant absolute value. This can be
- * a numeric constant, cast to any type.
- */
-{
-    return (Expr->Flags & (E_MASK_LOC|E_MASK_RTYPE)) == (E_LOC_ABS|E_RTYPE_RVAL);
-}
-
-
-
 int ED_IsConstAbsInt (const ExprDesc* Expr)
 /* Return true if the expression is a constant (numeric) integer. */
 {
@@ -231,8 +251,9 @@ int ED_IsConstAbsInt (const ExprDesc* Expr)
 int ED_IsNullPtr (const ExprDesc* Expr)
 /* Return true if the given expression is a NULL pointer constant */
 {
-    return (Expr->Flags & (E_MASK_LOC|E_MASK_RTYPE)) == (E_LOC_ABS|E_RTYPE_RVAL) &&
-           Expr->IVal == 0                                                        &&
+    return (Expr->Flags & (E_MASK_LOC|E_MASK_RTYPE|E_BITFIELD)) ==
+                                (E_LOC_ABS|E_RTYPE_RVAL) &&
+           Expr->IVal == 0                               &&
            IsClassInt (Expr->Type);
 }
 
@@ -268,7 +289,7 @@ void PrintExprDesc (FILE* F, ExprDesc* E)
                     "Raw type: (unknown)\n");
     }
     fprintf (F, "IVal:     0x%08lX\n", E->IVal);
-    fprintf (F, "FVal:     %f\n", E->FVal);
+    fprintf (F, "FVal:     %f\n", FP_D_ToFloat (E->FVal));
 
     Flags = E->Flags;
     Sep   = '(';
@@ -318,6 +339,11 @@ void PrintExprDesc (FILE* F, ExprDesc* E)
         Flags &= ~E_RTYPE_LVAL;
         Sep = ',';
     }
+    if (Flags & E_BITFIELD) {
+        fprintf (F, "%cE_BITFIELD", Sep);
+        Flags &= ~E_BITFIELD;
+        Sep = ',';
+    }
     if (Flags & E_NEED_TEST) {
         fprintf (F, "%cE_NEED_TEST", Sep);
         Flags &= ~E_NEED_TEST;
@@ -340,13 +366,10 @@ void PrintExprDesc (FILE* F, ExprDesc* E)
 
 
 
-type* ReplaceType (ExprDesc* Expr, const type* NewType)
+Type* ReplaceType (ExprDesc* Expr, const Type* NewType)
 /* Replace the type of Expr by a copy of Newtype and return the old type string */
 {
-    type* OldType = Expr->Type;
+    Type* OldType = Expr->Type;
     Expr->Type = TypeDup (NewType);
     return OldType;
 }
-
-
-