]> git.sur5r.net Git - cc65/blobdiff - src/da65/output.c
Merge remote-tracking branch 'upstream/master' into cbmkernal_stage2
[cc65] / src / da65 / output.c
index 3b4ffe47fd39753bd1122a1ec21810fa34ebd864..4daacb1ee5222f813dc6f6f82f27d3f7af2d1c0c 100644 (file)
@@ -1,15 +1,15 @@
 /*****************************************************************************/
 /*                                                                           */
-/*                                output.c                                  */
+/*                                 output.c                                  */
 /*                                                                           */
-/*                      Disassembler output routines                        */
+/*                       Disassembler output routines                        */
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 2000-2007 Ullrich von Bassewitz                                       */
-/*               Roemerstrasse 52                                            */
-/*               D-70794 Filderstadt                                         */
-/* EMail:        uz@cc65.org                                                 */
+/* (C) 2000-2014, Ullrich von Bassewitz                                      */
+/*                Roemerstrasse 52                                           */
+/*                D-70794 Filderstadt                                        */
+/* EMail:         uz@cc65.org                                                */
 /*                                                                           */
 /*                                                                           */
 /* This software is provided 'as-is', without any expressed or implied       */
 
 
 /*****************************************************************************/
-/*                                  Data                                    */
+/*                                   Data                                    */
 /*****************************************************************************/
 
 
 
-static FILE*   F       = 0;            /* Output stream */
-static unsigned        Col     = 1;            /* Current column */
-static unsigned Line           = 0;            /* Current line on page */
-static unsigned Page   = 1;            /* Current output page */
+static FILE*    F       = 0;            /* Output stream */
+static unsigned Col     = 1;            /* Current column */
+static unsigned Line    = 0;            /* Current line on page */
+static unsigned Page    = 1;            /* Current output page */
+
+static const char* SegmentName = 0;     /* Name of current segment */
 
 
 
 /*****************************************************************************/
-/*                                  Code                                    */
+/*                                   Code                                    */
 /*****************************************************************************/
 
 
@@ -75,14 +77,14 @@ static void PageHeader (void)
 /* Print a page header */
 {
     fprintf (F,
-                    "; da65 V%u.%u.%u - (C) Copyright 2000-2005,  Ullrich von Bassewitz\n"
+             "; da65 V%s\n"
              "; Created:    %s\n"
-            "; Input file: %s\n"
-            "; Page:       %u\n\n",
-                    VER_MAJOR, VER_MINOR, VER_PATCH,
+             "; Input file: %s\n"
+             "; Page:       %u\n\n",
+             GetVersionAsString (),
              Now,
-            InFile,
-            Page);
+             InFile,
+             Page);
 }
 
 
@@ -112,7 +114,7 @@ void CloseOutput (void)
 /* Close the output file */
 {
     if (F != stdout && fclose (F) != 0) {
-       Error ("Error closing output file: %s", strerror (errno));
+        Error ("Error closing output file: %s", strerror (errno));
     }
 }
 
@@ -122,10 +124,10 @@ void Output (const char* Format, ...)
 /* Write to the output file */
 {
     if (Pass == PassCount) {
-       va_list ap;
-       va_start (ap, Format);
-       Col += vfprintf (F, Format, ap);
-       va_end (ap);
+        va_list ap;
+        va_start (ap, Format);
+        Col += vfprintf (F, Format, ap);
+        va_end (ap);
     }
 }
 
@@ -135,10 +137,10 @@ void Indent (unsigned N)
 /* Make sure the current line column is at position N (zero based) */
 {
     if (Pass == PassCount) {
-       while (Col < N) {
-           fputc (' ', F);
-           ++Col;
-       }
+        while (Col < N) {
+            fputc (' ', F);
+            ++Col;
+        }
     }
 }
 
@@ -148,16 +150,16 @@ void LineFeed (void)
 /* Add a linefeed to the output file */
 {
     if (Pass == PassCount) {
-       fputc ('\n', F);
-               if (PageLength > 0 && ++Line >= PageLength) {
-           if (FormFeeds) {
-               fputc ('\f', F);
-           }
-           ++Page;
-           PageHeader ();
-           Line = 5;
-       }
-       Col = 1;
+        fputc ('\n', F);
+        if (PageLength > 0 && ++Line >= PageLength) {
+            if (FormFeeds) {
+                fputc ('\f', F);
+            }
+            ++Page;
+            PageHeader ();
+            Line = 5;
+        }
+        Col = 1;
     }
 }
 
@@ -168,10 +170,10 @@ void DefLabel (const char* Name)
 {
     Output ("%s:", Name);
     /* If the label is longer than the configured maximum, or if it runs into
-     * the opcode column, start a new line.
-     */
+    ** the opcode column, start a new line.
+    */
     if (Col > LBreak+2 || Col > MCol) {
-       LineFeed ();
+        LineFeed ();
     }
 }
 
@@ -179,10 +181,16 @@ void DefLabel (const char* Name)
 
 void DefForward (const char* Name, const char* Comment, unsigned Offs)
 /* Define a label as "* + x", where x is the offset relative to the
- * current PC.
- */
+** current PC.
+*/
 {
     if (Pass == PassCount) {
+        /* Flush existing output if necessary */
+        if (Col > 1) {
+            LineFeed ();
+        }
+
+        /* Output the forward definition */
         Output ("%s", Name);
         Indent (ACol);
         if (UseHexOffs) {
@@ -217,23 +225,6 @@ void DefConst (const char* Name, const char* Comment, unsigned Addr)
 
 
 
-void StartSegment (const char* Name, unsigned AddrSize)
-/* Start a segment */
-{
-    if (Pass == PassCount) {
-        Output (".segment");
-        Indent (ACol);
-        if (AddrSize == ADDR_SIZE_DEFAULT) {
-            Output ("\"%s\"", Name);
-        } else {
-            Output ("\"%s\": %s", Name, AddrSizeToStr (AddrSize));
-        }
-        LineFeed ();
-    }
-}
-
-
-
 void DataByteLine (unsigned ByteCount)
 /* Output a line with bytes */
 {
@@ -243,11 +234,11 @@ void DataByteLine (unsigned ByteCount)
     Output (".byte");
     Indent (ACol);
     for (I = 0; I < ByteCount; ++I) {
-       if (I > 0) {
-           Output (",$%02X", CodeBuf[PC+I]);
-       } else {
-           Output ("$%02X", CodeBuf[PC+I]);
-       }
+        if (I > 0) {
+            Output (",$%02X", CodeBuf[PC+I]);
+        } else {
+            Output ("$%02X", CodeBuf[PC+I]);
+        }
     }
     LineComment (PC, ByteCount);
     LineFeed ();
@@ -264,11 +255,11 @@ void DataDByteLine (unsigned ByteCount)
     Output (".dbyt");
     Indent (ACol);
     for (I = 0; I < ByteCount; I += 2) {
-       if (I > 0) {
-                   Output (",$%04X", GetCodeDByte (PC+I));
-       } else {
-           Output ("$%04X", GetCodeDByte (PC+I));
-       }
+        if (I > 0) {
+            Output (",$%04X", GetCodeDByte (PC+I));
+        } else {
+            Output ("$%04X", GetCodeDByte (PC+I));
+        }
     }
     LineComment (PC, ByteCount);
     LineFeed ();
@@ -285,11 +276,11 @@ void DataWordLine (unsigned ByteCount)
     Output (".word");
     Indent (ACol);
     for (I = 0; I < ByteCount; I += 2) {
-       if (I > 0) {
-           Output (",$%04X", GetCodeWord (PC+I));
-       } else {
-           Output ("$%04X", GetCodeWord (PC+I));
-       }
+        if (I > 0) {
+            Output (",$%04X", GetCodeWord (PC+I));
+        } else {
+            Output ("$%04X", GetCodeWord (PC+I));
+        }
     }
     LineComment (PC, ByteCount);
     LineFeed ();
@@ -306,11 +297,11 @@ void DataDWordLine (unsigned ByteCount)
     Output (".dword");
     Indent (ACol);
     for (I = 0; I < ByteCount; I += 4) {
-       if (I > 0) {
-           Output (",$%08lX", GetCodeDWord (PC+I));
-       } else {
-           Output ("$%08lX", GetCodeDWord (PC+I));
-       }
+        if (I > 0) {
+            Output (",$%08lX", GetCodeDWord (PC+I));
+        } else {
+            Output ("$%08lX", GetCodeDWord (PC+I));
+        }
     }
     LineComment (PC, ByteCount);
     LineFeed ();
@@ -322,13 +313,46 @@ void SeparatorLine (void)
 /* Print a separator line */
 {
     if (Pass == PassCount && Comments >= 1) {
-       Output ("; ----------------------------------------------------------------------------");
-       LineFeed ();
+        Output ("; ----------------------------------------------------------------------------");
+        LineFeed ();
     }
 }
 
 
 
+void StartSegment (const char* Name, unsigned AddrSize)
+/* Start a segment */
+{
+    if (Pass == PassCount) {
+        LineFeed ();
+        Output (".segment");
+        Indent (ACol);
+        SegmentName = Name;
+        Output ("\"%s\"", Name);
+        if (AddrSize != ADDR_SIZE_DEFAULT) {
+            Output (": %s", AddrSizeToStr (AddrSize));
+        }
+        LineFeed ();
+        LineFeed ();
+    }
+}
+
+
+
+void EndSegment (void)
+/* End a segment */
+{
+    LineFeed ();
+    Output ("; End of \"%s\" segment", SegmentName);
+    LineFeed ();
+    SeparatorLine ();
+    Output (".code");
+    LineFeed ();
+    LineFeed ();
+}
+
+
+
 void UserComment (const char* Comment)
 /* Output a comment line */
 {
@@ -344,23 +368,23 @@ void LineComment (unsigned PC, unsigned Count)
     unsigned I;
 
     if (Pass == PassCount && Comments >= 2) {
-       Indent (CCol);
-       Output ("; %04X", PC);
-       if (Comments >= 3) {
-           for (I = 0; I < Count; ++I) {
-               Output (" %02X", CodeBuf [PC+I]);
-           }
-           if (Comments >= 4) {
-               Indent (TCol);
-               for (I = 0; I < Count; ++I) {
-                   unsigned char C = CodeBuf [PC+I];
-                   if (!isprint (C)) {
-                       C = '.';
-                   }
-                   Output ("%c", C);
-               }
-           }
-       }
+        Indent (CCol);
+        Output ("; %04X", PC);
+        if (Comments >= 3) {
+            for (I = 0; I < Count; ++I) {
+                Output (" %02X", CodeBuf [PC+I]);
+            }
+            if (Comments >= 4) {
+                Indent (TCol);
+                for (I = 0; I < Count; ++I) {
+                    unsigned char C = CodeBuf [PC+I];
+                    if (!isprint (C)) {
+                        C = '.';
+                    }
+                    Output ("%c", C);
+                }
+            }
+        }
     }
 }
 
@@ -377,6 +401,3 @@ void OutputSettings (void)
     LineFeed ();
     LineFeed ();
 }
-
-
-