]> git.sur5r.net Git - cc65/blobdiff - src/cc65/codeseg.c
Amiga install files by Stefan Haubenthal.
[cc65] / src / cc65 / codeseg.c
index eb2cb0679bade49b5fc6c386110c546695d0ae03..1c7c0b5f6d89ee6878e1c867ad51091f9a4f2707 100644 (file)
@@ -6,10 +6,10 @@
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 2001-2003 Ullrich von Bassewitz                                       */
-/*               Römerstrasse 52                                             */
-/*               D-70794 Filderstadt                                         */
-/* 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       */
 #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"
 
 
 
@@ -288,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 '#':
@@ -444,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));
@@ -471,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;
 }
@@ -497,11 +506,11 @@ void CS_AddVLine (CodeSeg* S, LineInfo* LI, const char* Format, va_list ap)
     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 */
@@ -530,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);
 }
 
 
@@ -829,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);
 
@@ -875,7 +887,10 @@ 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;
                }
 
@@ -999,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) {
@@ -1225,8 +1240,16 @@ void CS_OutputPrologue (const CodeSeg* S, FILE* F)
      * 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\n\n", S->SegName, Func->Name);
+        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);
     }
 
 }
@@ -1239,13 +1262,13 @@ void CS_OutputEpilogue (const CodeSeg* S, FILE* F)
  */
 {
     if (S->Func) {
-       fprintf (F, "\n.endproc\n\n");
+               fputs ("\n.endproc\n\n", F);
     }
 }
 
 
 
-void CS_Output (const CodeSeg* S, FILE* F)
+void CS_Output (CodeSeg* S, FILE* F)
 /* Output the code segment data to a file */
 {
     unsigned I;
@@ -1259,6 +1282,9 @@ 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);
 
@@ -1295,7 +1321,7 @@ void CS_Output (const CodeSeg* S, FILE* 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 */
@@ -1304,8 +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);
     }
+
+    /* Free register info */
+    CS_FreeRegInfo (S);
 }
 
 
@@ -1397,7 +1426,7 @@ void CS_GenRegInfo (CodeSeg* S)
                        break;
                    }
                    if (J->RI->Out2.RegA != Regs.RegA) {
-                       Regs.RegA = UNKNOWN_REGVAL;
+                       Regs.RegA = UNKNOWN_REGVAL;
                    }
                    if (J->RI->Out2.RegX != Regs.RegX) {
                        Regs.RegX = UNKNOWN_REGVAL;
@@ -1443,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:
@@ -1455,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;
@@ -1464,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;
 
@@ -1477,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 {
@@ -1489,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 {
@@ -1508,7 +1537,7 @@ void CS_GenRegInfo (CodeSeg* S)
                            E->RI->Out2.RegX = 0;
                        } else {
                            E->RI->Out.RegX = 0;
-                       }
+                       }
                        break;
 
                    case OP65_DEY: