]> git.sur5r.net Git - cc65/blobdiff - src/da65/attrtab.c
fixed optimization bug where array index is 16-bit, e.g. arr16[i & 0x7f7f]
[cc65] / src / da65 / attrtab.c
index baa78bbb3732b5e4a3f52e389ef9a773d12303eb..a9143584ab81a3a010db6af72d5732ed3c9d8c3b 100644 (file)
@@ -1,15 +1,15 @@
 /*****************************************************************************/
 /*                                                                           */
-/*                                attrtab.c                                 */
+/*                                 attrtab.c                                 */
 /*                                                                           */
-/*                      Disassembler attribute table                        */
+/*                       Disassembler attribute table                        */
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 2000      Ullrich von Bassewitz                                       */
-/*               Wacholderweg 14                                             */
-/*               D-70597 Stuttgart                                           */
-/* EMail:        uz@musoftware.de                                            */
+/* (C) 2000-2014, Ullrich von Bassewitz                                      */
+/*                Roemerstrasse 52                                           */
+/*                D-70794 Filderstadt                                        */
+/* EMail:         uz@cc65.org                                                */
 /*                                                                           */
 /*                                                                           */
 /* This software is provided 'as-is', without any expressed or implied       */
 
 
 
-#include <stdio.h>
-#include <string.h>
-
-/* common */
-#include "xmalloc.h"
-#include "xsprintf.h"
-
 /* da65 */
-#include "code.h"
 #include "error.h"
-#include "global.h"
-#include "output.h"
 #include "attrtab.h"
 
 
 
 /*****************************************************************************/
-/*                                  Data                                    */
+/*                                   Data                                    */
 /*****************************************************************************/
 
 
 
 /* Attribute table */
-static unsigned char AttrTab [0x10000];
-
-/* Symbol table */
-static const char* SymTab [0x10000];
+static unsigned short AttrTab[0x10000];
 
 
 
 /*****************************************************************************/
-/*                                  Code                                    */
+/*                                   Code                                    */
 /*****************************************************************************/
 
 
@@ -73,117 +60,108 @@ void AddrCheck (unsigned Addr)
 /* Check if the given address has a valid range */
 {
     if (Addr >= 0x10000) {
-       Error ("Address out of range: %08X", Addr);
+        Error ("Address out of range: %08X", Addr);
     }
 }
 
 
 
-void MarkRange (unsigned Start, unsigned End, attr_t Attr)
-/* Mark a range with the given attribute */
+attr_t GetAttr (unsigned Addr)
+/* Return the attribute for the given address */
 {
-    /* Do it easy here... */
-    while (Start <= End) {
-       MarkAddr (Start++, Attr);
-    }
+    /* Check the given address */
+    AddrCheck (Addr);
+
+    /* Return the attribute */
+    return AttrTab[Addr];
 }
 
 
 
-void MarkAddr (unsigned Addr, attr_t Attr)
-/* Mark an address with an attribute */
+int SegmentDefined (unsigned Start, unsigned End)
+/* Return true if the atSegment bit is set somewhere in the given range */
 {
-    /* Check the given address */
-    AddrCheck (Addr);
-
-    /* We must not have more than one style bit */
-    if (Attr & atStyleMask) {
-       if (AttrTab[Addr] & atStyleMask) {
-           Error ("Duplicate style for address %04X", Addr);
-       }
+    while (Start <= End) {
+        if (AttrTab[Start++] & atSegment) {
+            return 1;
+        }
     }
-
-    /* Set the style */
-    AttrTab[Addr] |= Attr;
+    return 0;
 }
 
 
 
-const char* MakeLabelName (unsigned Addr)
-/* Make the default label name from the given address and return it in a
- * static buffer.
- */
+int IsSegmentEnd (unsigned Addr)
+/* Return true if a segment ends at the given address */
 {
-    static char LabelBuf [32];
-    xsprintf (LabelBuf, sizeof (LabelBuf), "L%04X", Addr);
-    return LabelBuf;
+    return (GetAttr (Addr) & atSegmentEnd) != 0x0000;
 }
 
 
 
-void AddLabel (unsigned Addr, attr_t Attr, const char* Name)
-/* Add a label */
+int IsSegmentStart (unsigned Addr)
+/* Return true if a segment starts at the given address */
 {
-    /* Get an existing label attribute */
-    attr_t ExistingAttr = GetLabelAttr (Addr);
-
-    /* Must not have two symbols for one address */
-    if (ExistingAttr != atNoLabel) {
-       /* Allow redefinition if identical */
-               if (ExistingAttr == Attr && strcmp (SymTab[Addr], Name) == 0) {
-           return;
-       }
-       Error ("Duplicate label for address %04X: %s/%s", Addr, SymTab[Addr], Name);
-    }
-
-    /* Create a new label */
-    SymTab[Addr] = xstrdup (Name);
-
-    /* Remember the attribute */
-    AttrTab[Addr] |= Attr;
+    return (GetAttr (Addr) & atSegmentStart) != 0x0000;
 }
 
 
 
-int HaveLabel (unsigned Addr)
-/* Check if there is a label for the given address */
+unsigned GetGranularity (attr_t Style)
+/* Get the granularity for the given style */
 {
-    /* Check the given address */
-    AddrCheck (Addr);
-
-    /* Check for a label */
-    return (SymTab[Addr] != 0);
+    switch (Style) {
+        case atDefault:  return 1;
+        case atCode:     return 1;
+        case atIllegal:  return 1;
+        case atByteTab:  return 1;
+        case atDByteTab: return 2;
+        case atWordTab:  return 2;
+        case atDWordTab: return 4;
+        case atAddrTab:  return 2;
+        case atRtsTab:   return 2;
+        case atTextTab:  return 1;
+
+        case atSkip:
+        default:
+            Internal ("GetGraularity called for style = %d", Style);
+            return 0;
+    }
 }
 
 
 
-int MustDefLabel (unsigned Addr)
-/* Return true if we must define a label for this address, that is, if there
- * is a label at this address, and it is an external or internal label.
- */
+void MarkRange (unsigned Start, unsigned End, attr_t Attr)
+/* Mark a range with the given attribute */
 {
-    /* Get the label attribute */
-    attr_t A = GetLabelAttr (Addr);
-
-    /* Check for an internal or external label */
-    return (A == atExtLabel || A == atIntLabel);
+    /* Do it easy here... */
+    while (Start <= End) {
+        MarkAddr (Start++, Attr);
+    }
 }
 
 
 
-const char* GetLabel (unsigned Addr)
-/* Return the label for an address */
+void MarkAddr (unsigned Addr, attr_t Attr)
+/* Mark an address with an attribute */
 {
     /* Check the given address */
     AddrCheck (Addr);
 
-    /* Return the label if any */
-    return SymTab[Addr];
+    /* We must not have more than one style bit */
+    if (Attr & atStyleMask) {
+        if (AttrTab[Addr] & atStyleMask) {
+            Error ("Duplicate style for address %04X", Addr);
+        }
+    }
+
+    /* Set the style */
+    AttrTab[Addr] |= Attr;
 }
 
 
 
-unsigned char GetStyleAttr (unsigned Addr)
+attr_t GetStyleAttr (unsigned Addr)
 /* Return the style attribute for the given address */
 {
     /* Check the given address */
@@ -195,7 +173,7 @@ unsigned char GetStyleAttr (unsigned Addr)
 
 
 
-unsigned char GetLabelAttr (unsigned Addr)
+attr_t GetLabelAttr (unsigned Addr)
 /* Return the label attribute for the given address */
 {
     /* Check the given address */
@@ -204,43 +182,3 @@ unsigned char GetLabelAttr (unsigned Addr)
     /* Return the attribute */
     return (AttrTab[Addr] & atLabelMask);
 }
-
-
-
-static void DefineConst (unsigned Addr)
-/* Define an address constant */
-{
-    Output ("%s", SymTab [Addr]);
-    Indent (AIndent);
-    Output ("= $%04X", Addr);
-    LineFeed ();
-}
-
-
-
-void DefOutOfRangeLabels (void)
-/* Output any labels that are out of the loaded code range */
-{
-    unsigned long Addr;
-
-    SeparatorLine ();
-
-    /* Low range */
-    for (Addr = 0; Addr < CodeStart; ++Addr) {
-       if (MustDefLabel (Addr)) {
-           DefineConst (Addr);
-       }
-    }
-
-    /* High range */
-    for (Addr = CodeEnd+1; Addr < 0x10000; ++Addr) {
-       if (MustDefLabel (Addr)) {
-           DefineConst (Addr);
-       }
-    }
-
-    SeparatorLine ();
-}
-
-
-