]> git.sur5r.net Git - cc65/blobdiff - src/cc65/codeseg.c
Amiga install files by Stefan Haubenthal.
[cc65] / src / cc65 / codeseg.c
index 99497da07bb5a59342a6f8f379e5f61e8e312381..1c7c0b5f6d89ee6878e1c867ad51091f9a4f2707 100644 (file)
@@ -6,10 +6,10 @@
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 2001      Ullrich von Bassewitz                                       */
-/*               Wacholderweg 14                                             */
-/*               D-70597 Stuttgart                                           */
-/* EMail:        uz@cc65.org                                                 */
+/* (C) 2001-2006, Ullrich von Bassewitz                                      */
+/*                Römerstrasse 52                                            */
+/*                D-70794 Filderstadt                                        */
+/* EMail:         uz@cc65.org                                                */
 /*                                                                           */
 /*                                                                           */
 /* This software is provided 'as-is', without any expressed or implied       */
 /* common */
 #include "chartype.h"
 #include "check.h"
+#include "debugflag.h"
 #include "global.h"
 #include "hashstr.h"
+#include "strbuf.h"
 #include "strutil.h"
 #include "xmalloc.h"
-#include "xsprintf.h"
 
 /* cc65 */
 #include "asmlabel.h"
 #include "codeent.h"
 #include "codeinfo.h"
+#include "codeseg.h"
 #include "datatype.h"
 #include "error.h"
+#include "global.h"
+#include "ident.h"
 #include "symentry.h"
-#include "codeseg.h"
 
 
 
 
 
 
+static void CS_PrintFunctionHeader (const CodeSeg* S, FILE* F)
+/* Print a comment with the function signature to the given file */
+{
+    /* Get the associated function */
+    const SymEntry* Func = S->Func;
+
+    /* If this is a global code segment, do nothing */
+    if (Func) {
+        fprintf (F,
+                 "; ---------------------------------------------------------------\n"
+                 "; ");
+        PrintFuncSig (F, Func->Name, Func->Type);
+        fprintf (F,
+                 "\n"
+                 "; ---------------------------------------------------------------\n"
+                 "\n");
+    }
+}
+
+
+
 static void CS_MoveLabelsToEntry (CodeSeg* S, CodeEntry* E)
 /* Move all labels from the label pool to the given entry and remove them
  * from the pool.
@@ -160,41 +184,6 @@ static void CS_RemoveLabelFromHash (CodeSeg* S, CodeLabel* L)
 
 
 
-static CodeLabel* CS_AddLabelInternal (CodeSeg* S, const char* Name,
-                                      void (*ErrorFunc) (const char*, ...))
-/* Add a code label for the next instruction to follow */
-{
-    /* Calculate the hash from the name */
-    unsigned Hash = HashStr (Name) % CS_LABEL_HASH_SIZE;
-
-    /* Try to find the code label if it does already exist */
-    CodeLabel* L = CS_FindLabel (S, Name, Hash);
-
-    /* Did we find it? */
-    if (L) {
-       /* We found it - be sure it does not already have an owner */
-       if (L->Owner) {
-           ErrorFunc ("ASM label `%s' is already defined", Name);
-       }
-    } else {
-       /* Not found - create a new one */
-       L = CS_NewCodeLabel (S, Name, Hash);
-    }
-
-    /* Safety. This call is quite costly, but safety is better */
-    if (CollIndex (&S->Labels, L) >= 0) {
-       ErrorFunc ("ASM label `%s' is already defined", Name);
-    }
-
-    /* We do now have a valid label. Remember it for later */
-    CollAppend (&S->Labels, L);
-
-    /* Return the label */
-    return L;
-}
-
-
-
 /*****************************************************************************/
 /*                   Functions for parsing instructions                     */
 /*****************************************************************************/
@@ -256,13 +245,13 @@ static CodeEntry* ParseInsn (CodeSeg* S, LineInfo* LI, const char* L)
  * white space, for example.
  */
 {
-    char                       Mnemo[64];
-    const OPCDesc*     OPC;
-    am_t               AM = 0;         /* Initialize to keep gcc silent */
-    char               Arg[64];
+    char                Mnemo[IDENTSIZE+10];
+    const OPCDesc*     OPC;
+    am_t               AM = 0;         /* Initialize to keep gcc silent */
+    char                Arg[IDENTSIZE+10];
     char               Reg;
     CodeEntry*         E;
-    CodeLabel*         Label;
+    CodeLabel*         Label;
 
     /* Read the first token and skip white space after it */
     L = SkipSpace (ReadToken (L, " \t:", Mnemo, sizeof (Mnemo)));
@@ -274,7 +263,7 @@ static CodeEntry* ParseInsn (CodeSeg* S, LineInfo* LI, const char* L)
        L = SkipSpace (L+1);
 
        /* Add the label */
-       CS_AddLabelInternal (S, Mnemo, Error);
+       CS_AddLabel (S, Mnemo);
 
        /* If we have reached end of line, bail out, otherwise a mnemonic
         * may follow.
@@ -300,8 +289,12 @@ static CodeEntry* ParseInsn (CodeSeg* S, LineInfo* LI, const char* L)
     switch (*L) {
 
        case '\0':
-           /* Implicit */
-           AM = AM65_IMP;
+           /* Implicit or accu */
+            if (OPC->Info & OF_NOIMP) {
+                AM = AM65_ACC;
+            } else {
+                AM = AM65_IMP;
+            }
            break;
 
        case '#':
@@ -399,7 +392,7 @@ static CodeEntry* ParseInsn (CodeSeg* S, LineInfo* LI, const char* L)
                            AM = AM65_ZPX;
                        } else {
                            AM = AM65_ABSX;
-                       }
+                       }
                    } else if (Reg == 'Y') {
                        AM = AM65_ABSY;
                    } else {
@@ -456,7 +449,7 @@ CodeSeg* NewCodeSeg (const char* SegName, SymEntry* Func)
 /* Create a new code segment, initialize and return it */
 {
     unsigned I;
-    const type* RetType;
+    const Type* RetType;
 
     /* Allocate memory */
     CodeSeg* S = xmalloc (sizeof (CodeSeg));
@@ -483,6 +476,10 @@ CodeSeg* NewCodeSeg (const char* SegName, SymEntry* Func)
        S->ExitRegs = REG_NONE;
     }
 
+    /* Copy the global optimization settings */
+    S->Optimize       = (unsigned char) IS_Get (&Optimize);
+    S->CodeSizeFactor = (unsigned) IS_Get (&CodeSizeFactor);
+
     /* Return the new struct */
     return S;
 }
@@ -506,17 +503,17 @@ void CS_AddVLine (CodeSeg* S, LineInfo* LI, const char* Format, va_list ap)
 {
     const char* L;
     CodeEntry*  E;
-    char       Token[64];
+    char        Token[IDENTSIZE+10];
 
     /* Format the line */
-    char Buf [256];
-    xvsprintf (Buf, sizeof (Buf), Format, ap);
+    StrBuf Buf = STATIC_STRBUF_INITIALIZER;
+    SB_VPrintf (&Buf, Format, ap);
 
     /* Skip whitespace */
-    L = SkipSpace (Buf);
+    L = SkipSpace (SB_GetConstBuf (&Buf));
 
     /* Check which type of instruction we have */
-    E = 0;     /* Assume no insn created */
+    E = 0;     /* Assume no insn created */
     switch (*L) {
 
        case '\0':
@@ -542,6 +539,9 @@ void CS_AddVLine (CodeSeg* S, LineInfo* LI, const char* Format, va_list ap)
     if (E) {
        CS_AddEntry (S, E);
     }
+
+    /* Cleanup the string buffer */
+    DoneStrBuf (&Buf);
 }
 
 
@@ -572,6 +572,8 @@ void CS_DelEntry (CodeSeg* S, unsigned Index)
 /* Delete an entry from the code segment. This includes moving any associated
  * labels, removing references to labels and even removing the referenced labels
  * if the reference count drops to zero.
+ * Note: Labels are moved forward if possible, that is, they are moved to the
+ * next insn (not the preceeding one).
  */
 {
     /* Get the code entry for the given index */
@@ -748,7 +750,7 @@ int CS_RangeHasLabel (CodeSeg* S, unsigned Start, unsigned Count)
            return 1;
        }
     }
-    
+
     /* No label in the complete range */
     return 0;
 }
@@ -758,7 +760,35 @@ int CS_RangeHasLabel (CodeSeg* S, unsigned Start, unsigned Count)
 CodeLabel* CS_AddLabel (CodeSeg* S, const char* Name)
 /* Add a code label for the next instruction to follow */
 {
-    return CS_AddLabelInternal (S, Name, Internal);
+    /* Calculate the hash from the name */
+    unsigned Hash = HashStr (Name) % CS_LABEL_HASH_SIZE;
+
+    /* Try to find the code label if it does already exist */
+    CodeLabel* L = CS_FindLabel (S, Name, Hash);
+
+    /* Did we find it? */
+    if (L) {
+       /* We found it - be sure it does not already have an owner */
+       if (L->Owner) {
+                   Error ("ASM label `%s' is already defined", Name);
+            return L;
+       }
+    } else {
+       /* Not found - create a new one */
+       L = CS_NewCodeLabel (S, Name, Hash);
+    }
+
+    /* Safety. This call is quite costly, but safety is better */
+    if (CollIndex (&S->Labels, L) >= 0) {
+               Error ("ASM label `%s' is already defined", Name);
+        return L;
+    }
+
+    /* We do now have a valid label. Remember it for later */
+    CollAppend (&S->Labels, L);
+
+    /* Return the label */
+    return L;
 }
 
 
@@ -811,7 +841,7 @@ void CS_DelLabel (CodeSeg* S, CodeLabel* L)
                /* Get the insn referencing this label */
                CodeEntry* E = CollAt (&L->JumpFrom, I);
                /* Remove the reference */
-               E->JumpTo = 0;
+               CE_ClearJumpTo (E);
     }
     CollDeleteAll (&L->JumpFrom);
 
@@ -857,9 +887,12 @@ void CS_MergeLabels (CodeSeg* S)
                for (J = 0; J < CL_GetRefCount (X); ++J) {
                    /* Get the entry referencing this label */
                    CodeEntry* E = CL_GetRef (X, J);
-                   /* And remove the reference */
+                   /* And remove the reference. Do NOT call CE_ClearJumpTo
+                     * here, because this will also clear the label name,
+                     * which is not what we want.
+                     */
                    E->JumpTo = 0;
-               }
+               }
 
                /* Print some debugging output */
                if (Debug) {
@@ -981,7 +1014,7 @@ void CS_RemoveLabelRef (CodeSeg* S, struct CodeEntry* E)
     CollDeleteItem (&L->JumpFrom, E);
 
     /* The entry jumps no longer to L */
-    E->JumpTo = 0;
+    CE_ClearJumpTo (E);
 
     /* If there are no more references, delete the label */
     if (CollCount (&L->JumpFrom) == 0) {
@@ -1072,7 +1105,170 @@ void CS_DelCodeAfter (CodeSeg* S, unsigned Last)
 
 
 
-void CS_Output (const CodeSeg* S, FILE* F)
+void CS_ResetMarks (CodeSeg* S, unsigned First, unsigned Last)
+/* Remove all user marks from the entries in the given range */
+{
+    while (First <= Last) {
+        CE_ResetMark (CS_GetEntry (S, First++));
+    }
+}
+
+
+
+int CS_IsBasicBlock (CodeSeg* S, unsigned First, unsigned Last)
+/* Check if the given code segment range is a basic block. That is, check if
+ * First is the only entrance and Last is the only exit. This means that no
+ * jump/branch inside the block may jump to an insn below First or after(!)
+ * Last, and that no insn may jump into this block from the outside.
+ */
+{
+    unsigned I;
+
+    /* Don't accept invalid ranges */
+    CHECK (First <= Last);
+
+    /* First pass: Walk over the range and remove all marks from the entries */
+    CS_ResetMarks (S, First, Last);
+
+    /* Second pass: Walk over the range checking all labels. Note: There may be
+     * label on the first insn which is ok.
+     */
+    I = First + 1;
+    while (I <= Last) {
+
+        /* Get the next entry */
+        CodeEntry* E = CS_GetEntry (S, I);
+
+        /* Check if this entry has one or more labels, if so, check which
+         * entries jump to this label.
+         */
+        unsigned LabelCount = CE_GetLabelCount (E);
+        unsigned LabelIndex;
+        for (LabelIndex = 0; LabelIndex < LabelCount; ++LabelIndex) {
+
+            /* Get this label */
+            CodeLabel* L = CE_GetLabel (E, LabelIndex);
+
+            /* Walk over all entries that jump to this label. Check for each
+             * of the entries if it is out of the range.
+             */
+            unsigned RefCount = CL_GetRefCount (L);
+            unsigned RefIndex;
+            for (RefIndex = 0; RefIndex < RefCount; ++RefIndex) {
+
+                /* Get the code entry that jumps here */
+                CodeEntry* Ref = CL_GetRef (L, RefIndex);
+
+                /* Walk over out complete range and check if we find the
+                 * refering entry. This is cheaper than using CS_GetEntryIndex,
+                 * because CS_GetEntryIndex will search the complete code
+                 * segment and not just our range.
+                 */
+                unsigned J;
+                for (J = First; J <= Last; ++J) {
+                    if (Ref == CS_GetEntry (S, J)) {
+                        break;
+                    }
+                }
+                if (J > Last) {
+                    /* We did not find the entry. This means that the jump to
+                     * out code segment entry E came from outside the range,
+                     * which in turn means that the given range is not a basic
+                     * block.
+                     */
+                    CS_ResetMarks (S, First, Last);
+                    return 0;
+                }
+
+                /* If we come here, we found the entry. Mark it, so we know
+                 * that the branch to the label is in range.
+                 */
+                CE_SetMark (Ref);
+            }
+        }
+
+        /* Next entry */
+        ++I;
+    }
+
+    /* Third pass: Walk again over the range and check all branches. If we
+     * find a branch that is not marked, its target is not inside the range
+     * (since we checked all the labels in the range before).
+     */
+    I = First;
+    while (I <= Last) {
+
+        /* Get the next entry */
+        CodeEntry* E = CS_GetEntry (S, I);
+
+        /* Check if this is a branch and if so, if it has a mark */
+        if (E->Info & (OF_UBRA | OF_CBRA)) {
+            if (!CE_HasMark (E)) {
+                /* No mark means not a basic block. Before bailing out, be sure
+                 * to remove the marks from the remaining entries.
+                 */
+                CS_ResetMarks (S, I+1, Last);
+                return 0;
+            }
+
+            /* Remove the mark */
+            CE_ResetMark (E);
+        }
+
+        /* Next entry */
+        ++I;
+    }
+
+    /* Done - this is a basic block */
+    return 1;
+}
+
+
+
+void CS_OutputPrologue (const CodeSeg* S, FILE* F)
+/* If the given code segment is a code segment for a function, output the
+ * assembler prologue into the file. That is: Output a comment header, switch
+ * to the correct segment and enter the local function scope. If the code
+ * segment is global, do nothing.
+ */
+{
+    /* Get the function associated with the code segment */
+    SymEntry* Func = S->Func;
+
+    /* If the code segment is associated with a function, print a function
+     * header and enter a local scope. Be sure to switch to the correct
+     * segment before outputing the function label.
+     */
+    if (Func) {
+        /* Get the function descriptor */
+        const FuncDesc* D = GetFuncDesc (Func->Type);
+               CS_PrintFunctionHeader (S, F);
+        fprintf (F, ".segment\t\"%s\"\n\n.proc\t_%s", S->SegName, Func->Name);
+        if (D->Flags & FD_NEAR) {
+            fputs (": near", F);
+        } else if (D->Flags & FD_FAR) {
+            fputs (": far", F);
+        }
+        fputs ("\n\n", F);
+    }
+
+}
+
+
+
+void CS_OutputEpilogue (const CodeSeg* S, FILE* F)
+/* If the given code segment is a code segment for a function, output the
+ * assembler epilogue into the file. That is: Close the local function scope.
+ */
+{
+    if (S->Func) {
+               fputs ("\n.endproc\n\n", F);
+    }
+}
+
+
+
+void CS_Output (CodeSeg* S, FILE* F)
 /* Output the code segment data to a file */
 {
     unsigned I;
@@ -1086,14 +1282,12 @@ void CS_Output (const CodeSeg* S, FILE* F)
        return;
     }
 
+    /* Generate register info */
+    CS_GenRegInfo (S);
+
     /* Output the segment directive */
     fprintf (F, ".segment\t\"%s\"\n\n", S->SegName);
 
-    /* If this is a segment for a function, enter a function */
-    if (S->Func) {
-       fprintf (F, ".proc\t_%s\n\n", S->Func->Name);
-    }
-
     /* Output all entries, prepended by the line information if it has changed */
     LI = 0;
     for (I = 0; I < Count; ++I) {
@@ -1107,15 +1301,27 @@ void CS_Output (const CodeSeg* S, FILE* F)
            /* Line info has changed, remember the new line info */
            LI = E->LI;
 
-           /* Add the source line as a comment */
+           /* Add the source line as a comment. Beware: When line continuation
+             * was used, the line may contain newlines.
+             */
            if (AddSource) {
-               fprintf (F, ";\n; %s\n;\n", LI->Line);
+                const char* L = LI->Line;
+                fputs (";\n; ", F);
+                while (*L) {
+                    if (*L == '\n') {
+                        fputs ("\n; ", F);
+                    } else {
+                        fputc (*L, F);
+                    }
+                    ++L;
+                }
+                fputs ("\n;\n", F);
            }
 
            /* Add line debug info */
            if (DebugInfo) {
                fprintf (F, "\t.dbg\tline, \"%s\", %u\n",
-                        GetInputName (LI), GetInputLine (LI));
+                        GetInputName (LI), GetInputLine (LI));
            }
        }
        /* Output the code */
@@ -1124,13 +1330,11 @@ void CS_Output (const CodeSeg* S, FILE* F)
 
     /* If debug info is enabled, terminate the last line number information */
     if (DebugInfo) {
-       fprintf (F, "\t.dbg\tline\n");
+               fputs ("\t.dbg\tline\n", F);
     }
 
-    /* If this is a segment for a function, leave the function */
-    if (S->Func) {
-       fprintf (F, "\n.endproc\n\n");
-    }
+    /* Free register info */
+    CS_FreeRegInfo (S);
 }
 
 
@@ -1222,19 +1426,22 @@ void CS_GenRegInfo (CodeSeg* S)
                        break;
                    }
                    if (J->RI->Out2.RegA != Regs.RegA) {
-                       Regs.RegA = -1;
+                       Regs.RegA = UNKNOWN_REGVAL;
                    }
                    if (J->RI->Out2.RegX != Regs.RegX) {
-                       Regs.RegX = -1;
+                       Regs.RegX = UNKNOWN_REGVAL;
                    }
                    if (J->RI->Out2.RegY != Regs.RegY) {
-                       Regs.RegY = -1;
+                       Regs.RegY = UNKNOWN_REGVAL;
                    }
                    if (J->RI->Out2.SRegLo != Regs.SRegLo) {
-                       Regs.SRegLo = -1;
+                       Regs.SRegLo = UNKNOWN_REGVAL;
                    }
                    if (J->RI->Out2.SRegHi != Regs.SRegHi) {
-                       Regs.SRegHi = -1;
+                       Regs.SRegHi = UNKNOWN_REGVAL;
+                   }
+                           if (J->RI->Out2.Tmp1 != Regs.Tmp1) {
+                       Regs.Tmp1 = UNKNOWN_REGVAL;
                    }
                    ++Entry;
                }
@@ -1265,7 +1472,7 @@ void CS_GenRegInfo (CodeSeg* S)
                /* Check the previous instruction */
                switch (P->OPC) {
 
-                   case OP65_ADC:
+                   case OP65_ADC:
                    case OP65_AND:
                    case OP65_DEA:
                    case OP65_EOR:
@@ -1277,7 +1484,7 @@ void CS_GenRegInfo (CodeSeg* S)
                        /* A is zero in one execution flow direction */
                        if (BC == BC_EQ) {
                            E->RI->Out2.RegA = 0;
-                       } else {
+                       } else {
                            E->RI->Out.RegA = 0;
                        }
                        break;
@@ -1286,12 +1493,12 @@ void CS_GenRegInfo (CodeSeg* S)
                        /* If this is an immidiate compare, the A register has
                         * the value of the compare later.
                         */
-                       if (CE_KnownImm (P)) {
+                       if (CE_IsConstImm (P)) {
                            if (BC == BC_EQ) {
                                E->RI->Out2.RegA = (unsigned char)P->Num;
                            } else {
                                E->RI->Out.RegA = (unsigned char)P->Num;
-                           }
+                           }
                        }
                        break;
 
@@ -1299,7 +1506,7 @@ void CS_GenRegInfo (CodeSeg* S)
                        /* If this is an immidiate compare, the X register has
                         * the value of the compare later.
                         */
-                       if (CE_KnownImm (P)) {
+                       if (CE_IsConstImm (P)) {
                            if (BC == BC_EQ) {
                                E->RI->Out2.RegX = (unsigned char)P->Num;
                            } else {
@@ -1311,8 +1518,8 @@ void CS_GenRegInfo (CodeSeg* S)
                    case OP65_CPY:
                        /* If this is an immidiate compare, the Y register has
                         * the value of the compare later.
-                        */
-                       if (CE_KnownImm (P)) {
+                        */
+                       if (CE_IsConstImm (P)) {
                            if (BC == BC_EQ) {
                                E->RI->Out2.RegY = (unsigned char)P->Num;
                            } else {
@@ -1330,7 +1537,7 @@ void CS_GenRegInfo (CodeSeg* S)
                            E->RI->Out2.RegX = 0;
                        } else {
                            E->RI->Out.RegX = 0;
-                       }
+                       }
                        break;
 
                    case OP65_DEY: