]> git.sur5r.net Git - cc65/commitdiff
Working on the new backend
authorcuz <cuz@b7a2c559-68d2-44c3-8de9-860c34a00d81>
Thu, 3 May 2001 22:06:59 +0000 (22:06 +0000)
committercuz <cuz@b7a2c559-68d2-44c3-8de9-860c34a00d81>
Thu, 3 May 2001 22:06:59 +0000 (22:06 +0000)
git-svn-id: svn://svn.cc65.org/cc65/trunk@709 b7a2c559-68d2-44c3-8de9-860c34a00d81

src/cc65/asmcode.c
src/cc65/codegen.h
src/cc65/codelab.c
src/cc65/codelab.h
src/cc65/codeopt.c [new file with mode: 0644]
src/cc65/codeopt.h [new file with mode: 0644]
src/cc65/codeseg.c
src/cc65/opcodes.c
src/cc65/opcodes.h

index e6fd47d2198b5289fd71f42c53cfefb6cc01a967..b52987b342de3af8ebdfc78bdfa827a8248d7490 100644 (file)
@@ -37,6 +37,7 @@
 #include "check.h"
 
 /* b6502 */
+#include "codeopt.h"
 #include "codeseg.h"
 #include "dataseg.h"
 
@@ -111,6 +112,7 @@ void WriteOutput (FILE* F)
            /* Function which is defined and referenced or extern */
            PrintFunctionHeader (F, Entry);
            MergeCodeLabels (Entry->V.F.CS);
+           RunOpt (Entry->V.F.CS);
            fprintf (F, "; Data segment for function %s:\n", Entry->Name);
            OutputDataSeg (F, Entry->V.F.DS);
            fprintf (F, "; Code segment for function %s:\n", Entry->Name);
index 93a4aa4b8af3a85872805ba70dff54f2e3003c1b..b04b5c9d848382e78fcfaaebbd48ec8c9c7ab61f 100644 (file)
@@ -9,7 +9,7 @@
 /* (C) 1998-2001 llrich von Bassewitz                                        */
 /*               Wacholderweg 14                                             */
 /*               D-70597 Stuttgart                                           */
-/* EMail:        uz@musoftware.de                                            */
+/* EMail:        uz@cc65.org                                                 */
 /*                                                                           */
 /*                                                                           */
 /* This software is provided 'as-is', without any expressed or implied       */
index 46c43559a565fff8531d8910d211cfd77c9cba28..bc52e5878688cf68cc811ff7a31712f58da8b301 100644 (file)
@@ -82,6 +82,18 @@ void FreeCodeLabel (CodeLabel* L)
 
 
 
+unsigned RemoveLabelRef (CodeLabel* L, const struct CodeEntry* E)
+/* Remove a reference to this label, return the number of remaining references */
+{
+    /* Delete the item */
+    CollDeleteItem (&L->JumpFrom, E);
+
+    /* Return the number of remaining references */
+    return CollCount (&L->JumpFrom);
+}
+
+
+
 void OutputCodeLabel (FILE* F, const CodeLabel* L)
 /* Output the code label to a file */
 {
index 24681386a78e1289d3e44ab994691803e11c37fb..2193ada386597a5ab9f688a7802392bc89846767 100644 (file)
 
 
 
+/*****************************************************************************/
+/*                                Forwards                                  */
+/*****************************************************************************/
+
+
+
+struct CodeEntry;
+
+
+
 /*****************************************************************************/
 /*                            struct CodeLabel                              */
 /*****************************************************************************/
@@ -79,6 +89,9 @@ CodeLabel* NewCodeLabel (const char* Name, unsigned Hash);
 void FreeCodeLabel (CodeLabel* L);
 /* Free the given code label */
 
+unsigned RemoveLabelRef (CodeLabel* L, const struct CodeEntry* E);
+/* Remove a reference to this label, return the number of remaining references */
+
 void OutputCodeLabel (FILE* F, const CodeLabel* L);
 /* Output the code label to a file */
 
diff --git a/src/cc65/codeopt.c b/src/cc65/codeopt.c
new file mode 100644 (file)
index 0000000..c4e6040
--- /dev/null
@@ -0,0 +1,147 @@
+/*****************************************************************************/
+/*                                                                           */
+/*                                codeopt.c                                 */
+/*                                                                           */
+/*                          Optimizer subroutines                           */
+/*                                                                           */
+/*                                                                           */
+/*                                                                           */
+/* (C) 2001      Ullrich von Bassewitz                                       */
+/*               Wacholderweg 14                                             */
+/*               D-70597 Stuttgart                                           */
+/* EMail:        uz@cc65.org                                                 */
+/*                                                                           */
+/*                                                                           */
+/* This software is provided 'as-is', without any expressed or implied       */
+/* warranty.  In no event will the authors be held liable for any damages    */
+/* arising from the use of this software.                                    */
+/*                                                                           */
+/* Permission is granted to anyone to use this software for any purpose,     */
+/* including commercial applications, and to alter it and redistribute it    */
+/* freely, subject to the following restrictions:                            */
+/*                                                                           */
+/* 1. The origin of this software must not be misrepresented; you must not   */
+/*    claim that you wrote the original software. If you use this software   */
+/*    in a product, an acknowledgment in the product documentation would be  */
+/*    appreciated but is not required.                                       */
+/* 2. Altered source versions must be plainly marked as such, and must not   */
+/*    be misrepresented as being the original software.                      */
+/* 3. This notice may not be removed or altered from any source              */
+/*    distribution.                                                          */
+/*                                                                           */
+/*****************************************************************************/
+
+
+
+/* b6502 */
+#include "codeent.h"
+#include "codeopt.h"
+
+
+
+/*****************************************************************************/
+/*                                  Data                                    */
+/*****************************************************************************/
+
+
+
+/* Counter for the number of changes in one run. The optimizer process is
+ * repeated until there are no more changes.
+ */
+static unsigned OptChanges;
+
+
+
+/*****************************************************************************/
+/*                            Remove dead jumps                             */
+/*****************************************************************************/
+
+
+
+static void OptDeadJumps (CodeSeg* S)
+/* Remove dead jumps (jumps to the next instruction) */
+{
+    CodeEntry* E;
+    unsigned I;
+
+    /* Get the number of entries, bail out if we have less than two entries */
+    unsigned Count = CollCount (&S->Entries);
+    if (Count < 2) {
+       return;
+    }
+
+    /* Walk over all entries minus the last one */
+    I = 0;
+    while (I < Count-1) {
+
+       /* Get the next entry */
+       E = CollAt (&S->Entries, I);
+
+       /* Check if it's a branch, if it has a local target, and if the target
+        * is the next instruction.
+        */
+       if (E->AM == AM_BRA) {
+           printf ("BRA on entry %u:\n", I);
+           if (E->JumpTo) {
+               printf ("  JumpTo ok\n");
+               if (E->JumpTo->Owner == CollAt (&S->Entries, I+1)) {
+                   printf ("  Branch to next insn\n");
+               }
+           }
+       }
+
+       if (E->AM == AM_BRA && E->JumpTo && E->JumpTo->Owner == CollAt (&S->Entries, I+1)) {
+
+           /* Remember the label */
+           CodeLabel* L = E->JumpTo;
+
+           /* Jump to next instruction, remove it */
+           unsigned Remaining = RemoveLabelRef (L, E);
+           CollDelete (&S->Entries, I);
+           FreeCodeEntry (E);
+           --Count;
+
+           /* If the label has no more references, remove it */
+           if (Remaining == 0) {
+               CollDeleteItem (&L->Owner->Labels, L);
+               FreeCodeLabel (L);
+           }
+
+           /* Remember we had changes */
+           ++OptChanges;
+
+       } else {
+
+           /* Next entry */
+           ++I;
+
+       }
+    }
+}
+
+
+
+/*****************************************************************************/
+/*                                          Code                                    */
+/*****************************************************************************/
+
+
+
+void RunOpt (CodeSeg* S)
+/* Run the optimizer */
+{
+    printf ("Optimize\n");
+
+    /* Repeat all steps until there are no more changes */
+    do {
+
+       /* Reset the number of changes */
+       OptChanges = 0;
+
+       OptDeadJumps (S);
+
+    } while (OptChanges > 0);
+}
+
+
+
diff --git a/src/cc65/codeopt.h b/src/cc65/codeopt.h
new file mode 100644 (file)
index 0000000..4edacb1
--- /dev/null
@@ -0,0 +1,67 @@
+/*****************************************************************************/
+/*                                                                           */
+/*                                codeopt.h                                 */
+/*                                                                           */
+/*                          Optimizer subroutines                           */
+/*                                                                           */
+/*                                                                           */
+/*                                                                           */
+/* (C) 2001      Ullrich von Bassewitz                                       */
+/*               Wacholderweg 14                                             */
+/*               D-70597 Stuttgart                                           */
+/* EMail:        uz@cc65.org                                                 */
+/*                                                                           */
+/*                                                                           */
+/* This software is provided 'as-is', without any expressed or implied       */
+/* warranty.  In no event will the authors be held liable for any damages    */
+/* arising from the use of this software.                                    */
+/*                                                                           */
+/* Permission is granted to anyone to use this software for any purpose,     */
+/* including commercial applications, and to alter it and redistribute it    */
+/* freely, subject to the following restrictions:                            */
+/*                                                                           */
+/* 1. The origin of this software must not be misrepresented; you must not   */
+/*    claim that you wrote the original software. If you use this software   */
+/*    in a product, an acknowledgment in the product documentation would be  */
+/*    appreciated but is not required.                                       */
+/* 2. Altered source versions must be plainly marked as such, and must not   */
+/*    be misrepresented as being the original software.                      */
+/* 3. This notice may not be removed or altered from any source              */
+/*    distribution.                                                          */
+/*                                                                           */
+/*****************************************************************************/
+
+
+
+#ifndef CODEOPT_H
+#define CODEOPT_H
+
+
+
+/* b6502 */
+#include "codeseg.h"
+
+
+
+/*****************************************************************************/
+/*                                  Data                                    */
+/*****************************************************************************/
+
+
+
+/*****************************************************************************/
+/*                                          Code                                    */
+/*****************************************************************************/
+
+
+
+void RunOpt (CodeSeg* S);
+/* Run the optimizer */
+
+
+
+/* End of codeopt.h */
+#endif
+
+
+
index acab2ced3d5affd5c091f7bc888a076a2f8f3965..b318b773929f35f5c49e6a7a6cc4e2fe068e20d0 100644 (file)
@@ -270,11 +270,10 @@ static CodeEntry* ParseInsn (CodeSeg* S, const char* L)
     }
 
     /* If the instruction is a branch, check for the label and generate it
-     * if it does not exist. In case of a PC relative branch (*+x) we will
-     * not generate a label, because the target label will not be defined.
+     * if it does not exist. Ignore anything but local labels here.
      */
     Label = 0;
-    if ((OPC->Info & CI_MASK_BRA) == CI_BRA && Expr[0] != '*') {
+    if ((OPC->Info & CI_MASK_BRA) == CI_BRA && Expr[0] == 'L') {
 
        unsigned Hash;
 
@@ -455,6 +454,8 @@ void AddCodeSegLine (CodeSeg* S, const char* Format, ...)
            L->Flags |= LF_DEF;
            /* Move it to the code entry */
            CollAppend (&E->Labels, L);
+           /* Tell the label about it's owner */
+           L->Owner = E;
        }
 
        /* Delete the transfered labels */
index 7660b52072824ab77a11ffa9dc539b4ec9ec0227..5c9ba3a9fb5718918a3b3e3c9145589aa202eaaa 100644 (file)
@@ -6,10 +6,10 @@
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 2001     Ullrich von Bassewitz                                        */
-/*              Wacholderweg 14                                              */
-/*              D-70597 Stuttgart                                            */
-/* EMail:       uz@musoftware.de                                             */
+/* (C) 2001      Ullrich von Bassewitz                                       */
+/*               Wacholderweg 14                                             */
+/*               D-70597 Stuttgart                                           */
+/* EMail:        uz@cc65.org                                                 */
 /*                                                                           */
 /*                                                                           */
 /* This software is provided 'as-is', without any expressed or implied       */
index 6e0ca2382ab7123f3d06e16c45c31aa075ca7abd..5de93e0c664815f99ce83697e40799bd07f62a65 100644 (file)
@@ -6,10 +6,10 @@
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 2001     Ullrich von Bassewitz                                        */
-/*              Wacholderweg 14                                              */
-/*              D-70597 Stuttgart                                            */
-/* EMail:       uz@musoftware.de                                             */
+/* (C) 2001      Ullrich von Bassewitz                                       */
+/*               Wacholderweg 14                                             */
+/*               D-70597 Stuttgart                                           */
+/* EMail:        uz@cc65.org                                                 */
 /*                                                                           */
 /*                                                                           */
 /* This software is provided 'as-is', without any expressed or implied       */