From c71a80b0afd9a035dd0aab156bf1931c6aaef7fa Mon Sep 17 00:00:00 2001 From: cuz Date: Thu, 3 May 2001 22:06:59 +0000 Subject: [PATCH] Working on the new backend git-svn-id: svn://svn.cc65.org/cc65/trunk@709 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- src/cc65/asmcode.c | 2 + src/cc65/codegen.h | 2 +- src/cc65/codelab.c | 12 ++++ src/cc65/codelab.h | 13 ++++ src/cc65/codeopt.c | 147 +++++++++++++++++++++++++++++++++++++++++++++ src/cc65/codeopt.h | 67 +++++++++++++++++++++ src/cc65/codeseg.c | 7 ++- src/cc65/opcodes.c | 8 +-- src/cc65/opcodes.h | 8 +-- 9 files changed, 254 insertions(+), 12 deletions(-) create mode 100644 src/cc65/codeopt.c create mode 100644 src/cc65/codeopt.h diff --git a/src/cc65/asmcode.c b/src/cc65/asmcode.c index e6fd47d21..b52987b34 100644 --- a/src/cc65/asmcode.c +++ b/src/cc65/asmcode.c @@ -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); diff --git a/src/cc65/codegen.h b/src/cc65/codegen.h index 93a4aa4b8..b04b5c9d8 100644 --- a/src/cc65/codegen.h +++ b/src/cc65/codegen.h @@ -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 */ diff --git a/src/cc65/codelab.c b/src/cc65/codelab.c index 46c43559a..bc52e5878 100644 --- a/src/cc65/codelab.c +++ b/src/cc65/codelab.c @@ -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 */ { diff --git a/src/cc65/codelab.h b/src/cc65/codelab.h index 24681386a..2193ada38 100644 --- a/src/cc65/codelab.h +++ b/src/cc65/codelab.h @@ -45,6 +45,16 @@ +/*****************************************************************************/ +/* 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 index 000000000..c4e6040a3 --- /dev/null +++ b/src/cc65/codeopt.c @@ -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 index 000000000..4edacb11b --- /dev/null +++ b/src/cc65/codeopt.h @@ -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 + + + diff --git a/src/cc65/codeseg.c b/src/cc65/codeseg.c index acab2ced3..b318b7739 100644 --- a/src/cc65/codeseg.c +++ b/src/cc65/codeseg.c @@ -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 */ diff --git a/src/cc65/opcodes.c b/src/cc65/opcodes.c index 7660b5207..5c9ba3a9f 100644 --- a/src/cc65/opcodes.c +++ b/src/cc65/opcodes.c @@ -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 */ diff --git a/src/cc65/opcodes.h b/src/cc65/opcodes.h index 6e0ca2382..5de93e0c6 100644 --- a/src/cc65/opcodes.h +++ b/src/cc65/opcodes.h @@ -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 */ -- 2.39.5