changeset 11:fe4d840c13eb

Implement reference checking to reduce outputting of useless labels.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 14 May 2020 20:35:40 +0300
parents 122134900c0e
children 3c00e5162f34
files dump.c main.c scan.c structures.h
diffstat 4 files changed, 49 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/dump.c	Fri May 31 02:10:19 2019 +0300
+++ b/dump.c	Thu May 14 20:35:40 2020 +0300
@@ -87,11 +87,10 @@
   }
 
   /* print the label definitions */
-
   for (address = EndAddress < StartAddress ? EndAddress : 0;
        address != StartAddress; address++)
   {
-    if (IsLabeled (address) && !IsInsideRegion(address))
+    if (IsLabeled (address) && !IsInsideRegion(address) && IsReferenced(address))
     {
       fprintf (stdout, "%s%s = $%x\n", lineprefix, Label (address, abso, 0),
 	address);
@@ -102,7 +101,7 @@
   {
     for (address = EndAddress; address; address++)
     {
-      if (IsLabeled (address) && !IsInsideRegion(address))
+      if (IsLabeled (address) && !IsInsideRegion(address) && IsReferenced(address))
       {
         fprintf (stdout, "%s%s = $%x\n", lineprefix, Label (address, abso, 0),
 		address);
@@ -122,7 +121,8 @@
   for (address = StartAddress; (ADDR_T)(address - StartAddress) <
        (ADDR_T)(EndAddress - StartAddress); address += size)
     if (GetMemType (address) == MEM_INSTRUCTION) {
-      if (IsLabeled (address)) {
+
+      if (IsLabeled (address) && IsReferenced(address)) {
 	if (Options & M_ADDRESSES)
 	  fprintf (stdout, "%04x %s%s:\n", address,
                    lineinfix, Label (address, abso, 0));
@@ -137,7 +137,8 @@
       size = sizes[instr->admode];
 
       for (counter = 1; counter < size; counter++) {
-        if (IsLabeled (address + counter)) {
+        if (IsLabeled (address + counter))
+        {
 	  if (Options & M_ADDRESSES)
 	    fprintf (stdout, "\t%04x %s%s = * + %u\n",
                      (ADDR_T)(address + counter),
--- a/main.c	Fri May 31 02:10:19 2019 +0300
+++ b/main.c	Thu May 14 20:35:40 2020 +0300
@@ -131,6 +131,9 @@
   for (address1 = sizeof LowByte / sizeof *LowByte; address1--;
        HighByte[address1] = LowByte[address1] = 0);
 
+  for (address1 = sizeof MemReferenced / sizeof *MemReferenced; address1--;
+       MemReferenced[address1] = 0);
+
   for (prog = *argv; *prog; prog++);
   for (;prog > *argv; prog--)
     if (*prog == '/') {
--- a/scan.c	Fri May 31 02:10:19 2019 +0300
+++ b/scan.c	Thu May 14 20:35:40 2020 +0300
@@ -92,7 +92,9 @@
       if (((ADDR_T)(addr - StartAddress) <
 	   (ADDR_T)(EndAddress - StartAddress) ||
 	   Options & B_LBL_ALWAYS)) {
+
 	PutLabel (addr);
+	PutReference (addr);
 	PutLowByte (addr);
 	PutHighByte (addr);
       }
@@ -121,6 +123,7 @@
           (ADDR_T)(EndAddress - StartAddress)) {
         if (GetMemType (addr) == MEM_INSTRUCTION) {
           PutLabel (addr);
+          PutReference (addr);
           PutLowByte (addr);
           PutHighByte (addr);
         }
@@ -131,6 +134,7 @@
       }
       else if (Options & B_LBL_ALWAYS) {
         PutLabel (addr);
+	PutReference (addr);
         PutLowByte (addr);
         PutHighByte (addr);
       }
@@ -149,6 +153,7 @@
       if (instr->admode == iabs && (ADDR_T)(addr - StartAddress) <
 	  (ADDR_T)(EndAddress - StartAddress)) {
 	PutLabel (addr);
+        PutReference (addr);
 	PutLowByte (addr);
 	PutHighByte (addr);
 
@@ -183,6 +188,7 @@
       }
       else if (Options & B_LBL_ALWAYS) {
 	PutLabel (addr);
+	PutReference (addr);
 	PutLowByte (addr);
 	PutHighByte (addr);
       }
@@ -241,6 +247,7 @@
         (ADDR_T)(EndAddress - StartAddress) ||
         Options & B_LBL_ALWAYS)) {
       PutLabel (addr);
+      PutReference (addr);
       PutLowByte (addr);
       PutHighByte (addr);
     }
@@ -572,6 +579,7 @@
 
   while ((entry = FindNextEntryType (NULL, ~0, RTN_SURE))) {
     PutLabel (entry->address);
+    PutReference (entry->address);
     PutLowByte (entry->address);
     PutHighByte (entry->address);
 
@@ -701,6 +709,7 @@
       while ((entry = FindNextEntryType (entry, ~0,
                                          RTN_SUSPECTED | RTN_B_PROCESSED))) {
         PutLabel (entry->address);
+        PutReference (entry->address);
         PutLowByte (entry->address);
         PutHighByte (entry->address);
         DeleteEntry (entry);
@@ -733,6 +742,7 @@
           /* fall through */
         case WRN_I_LABEL_NEEDED:
           PutLabel (entry->address);
+          PutReference (entry->address);
           PutLowByte (entry->address);
           PutHighByte (entry->address);
           DeleteEntry (entry);
@@ -807,6 +817,7 @@
 
       if (GetMemType (address) != MEM_DATA) {
         PutLabel (address);
+        PutReference (address);
         PutLowByte (address);
         PutHighByte (address);
       }
@@ -830,6 +841,7 @@
       while ((entry = FindNextEntryType (entry, ~0,
                                          RTN_SUSPECTED | RTN_B_PROCESSED))) {
         PutLabel (entry->address);
+        PutReference (entry->address);
         PutLowByte (entry->address);
         PutHighByte (entry->address);
         DeleteEntry (entry);
@@ -857,6 +869,7 @@
           /* fall through */
         case WRN_I_LABEL_NEEDED:
           PutLabel (entry->address);
+          PutReference (entry->address);
           PutLowByte (entry->address);
           PutHighByte (entry->address);
           DeleteEntry (entry);
--- a/structures.h	Fri May 31 02:10:19 2019 +0300
+++ b/structures.h	Thu May 14 20:35:40 2020 +0300
@@ -97,6 +97,13 @@
      LowByte[(1 << 8) / sizeof(unsigned)],
      HighByte[(1 << 8) / sizeof(unsigned)];
 
+
+#ifndef _MAIN_C_
+extern
+#endif
+unsigned char MemReferenced[1 << 16];
+
+
 #define GetMemFlag(address) \
      ((MemFlag[((ADDR_T)address) / (8 * sizeof *MemFlag)] >>    \
        (address % (8 * sizeof *MemFlag))) & 1)
@@ -112,9 +119,26 @@
      ((MemLabel[((ADDR_T)address) / (8 * sizeof *MemLabel)] >>  \
        (address % (8 * sizeof *MemLabel))) & 1)
 
-#define PutLabel(address) \
-     (MemLabel[((ADDR_T)address) / (8 * sizeof *MemLabel)] |=   \
-      (1 << (address % (8 * sizeof *MemLabel))))
+#define PutLabel(address) DOPutLabel(address, __func__, __LINE__)
+
+
+static inline void DOPutLabel(unsigned int address, const char *name, const int line)
+{
+  MemLabel[((ADDR_T)address) / (8 * sizeof *MemLabel)] |= (1 << (address % (8 * sizeof *MemLabel)));
+}
+
+static inline unsigned char IsReferenced(unsigned int address)
+{
+  return MemReferenced[address];
+}
+
+
+static inline void DOPutReference(unsigned int address, const char *name, const int line)
+{
+  MemReferenced[address]++;
+}
+
+#define PutReference(address) DOPutReference(address, __func__, __LINE__)
 
 /* These macros tell if there is a label for a given address, or cause a
    label to be produced for an address. */