]> git.sur5r.net Git - cc65/blobdiff - src/da65/main.c
Followed the discussions in the Pull request #682.
[cc65] / src / da65 / main.c
index 83b1784e8b7f62abab9d97115d70f42ab41b4496..b0a784dd88338ceb5f03bf4fb5efbde4310e0f78 100644 (file)
@@ -1,15 +1,15 @@
 /*****************************************************************************/
 /*                                                                           */
-/*                                 main.c                                   */
+/*                                  main.c                                   */
 /*                                                                           */
-/*                 Main program for the da65 disassembler                   */
+/*                  Main program for the da65 disassembler                   */
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 1998-2007 Ullrich von Bassewitz                                       */
-/*               Roemerstrasse 52                                            */
-/*               D-70794 Filderstadt                                         */
-/* EMail:        uz@cc65.org                                                 */
+/* (C) 1998-2014, Ullrich von Bassewitz                                      */
+/*                Roemerstrasse 52                                           */
+/*                D-70794 Filderstadt                                        */
+/* EMail:         uz@cc65.org                                                */
 /*                                                                           */
 /*                                                                           */
 /* This software is provided 'as-is', without any expressed or implied       */
 #include "opctable.h"
 #include "output.h"
 #include "scanner.h"
+#include "segment.h"
 
 
 
 /*****************************************************************************/
-/*                                          Code                                    */
+/*                                   Code                                    */
 /*****************************************************************************/
 
 
@@ -81,6 +82,7 @@ static void Usage (void)
             "  -v\t\t\tIncrease verbosity\n"
             "  -F\t\t\tAdd formfeeds to the output\n"
             "  -S addr\t\tSet the start/load address\n"
+            "  -s\t\t\tAccept line markers in the info file\n"
             "  -V\t\t\tPrint the disassembler version\n"
             "\n"
             "Long options:\n"
@@ -97,6 +99,7 @@ static void Usage (void)
             "  --mnemonic-column n\tSpecify mnemonic start column\n"
             "  --pagelength n\tSet the page length for the listing\n"
             "  --start-addr addr\tSet the start/load address\n"
+            "  --sync-lines\t\tAccept line markers in the info file\n"
             "  --text-column n\tSpecify text start column\n"
             "  --verbose\t\tIncrease verbosity\n"
             "  --version\t\tPrint the disassembler version\n",
@@ -108,8 +111,8 @@ static void Usage (void)
 static void RangeCheck (const char* Opt, unsigned long Val,
                         unsigned long Min, unsigned long Max)
 /* Do a range check for the given option and abort if there's a range
- * error.
- */
+** error.
+*/
 {
     if (Val < Min || Val > Max) {
         Error ("Argument for %s outside valid range (%ld-%ld)", Opt, Min, Max);
@@ -120,24 +123,24 @@ static void RangeCheck (const char* Opt, unsigned long Val,
 
 static unsigned long CvtNumber (const char* Arg, const char* Number)
 /* Convert a number from a string. Allow '$' and '0x' prefixes for hex
- * numbers.
- */
+** numbers.
+*/
 {
     unsigned long Val;
-    int          Converted;
+    int           Converted;
     char          BoundsCheck;
 
     /* Convert */
     if (*Number == '$') {
-       ++Number;
-       Converted = sscanf (Number, "%lx%c", &Val, &BoundsCheck);
+        ++Number;
+        Converted = sscanf (Number, "%lx%c", &Val, &BoundsCheck);
     } else {
-       Converted = sscanf (Number, "%li%c", (long*)&Val, &BoundsCheck);
+        Converted = sscanf (Number, "%li%c", (long*)&Val, &BoundsCheck);
     }
 
     /* Check if we do really have a number */
     if (Converted != 1) {
-               Error ("Invalid number given in argument: %s\n", Arg);
+        Error ("Invalid number given in argument: %s\n", Arg);
     }
 
     /* Return the result */
@@ -217,7 +220,7 @@ static void OptCPU (const char* Opt attribute ((unused)), const char* Arg)
 
 
 static void OptDebugInfo (const char* Opt attribute ((unused)),
-                         const char* Arg attribute ((unused)))
+                          const char* Arg attribute ((unused)))
 /* Add debug info to the object file */
 {
     DebugInfo = 1;
@@ -226,7 +229,7 @@ static void OptDebugInfo (const char* Opt attribute ((unused)),
 
 
 static void OptFormFeeds (const char* Opt attribute ((unused)),
-                         const char* Arg attribute ((unused)))
+                          const char* Arg attribute ((unused)))
 /* Add form feeds to the output */
 {
     FormFeeds = 1;
@@ -235,7 +238,7 @@ static void OptFormFeeds (const char* Opt attribute ((unused)),
 
 
 static void OptHelp (const char* Opt attribute ((unused)),
-                    const char* Arg attribute ((unused)))
+                     const char* Arg attribute ((unused)))
 /* Print usage information and exit */
 {
     Usage ();
@@ -245,7 +248,7 @@ static void OptHelp (const char* Opt attribute ((unused)),
 
 
 static void OptHexOffs (const char* Opt attribute ((unused)),
-                       const char* Arg attribute ((unused)))
+                        const char* Arg attribute ((unused)))
 /* Handle the --hexoffs option */
 {
     UseHexOffs = 1;
@@ -311,6 +314,15 @@ static void OptStartAddr (const char* Opt, const char* Arg)
 
 
 
+static void OptSyncLines (const char* Opt attribute ((unused)),
+                          const char* Arg attribute ((unused)))
+/* Handle the --sync-lines option */
+{
+    SyncLines = 1;
+}
+
+
+
 static void OptTextColumn (const char* Opt, const char* Arg)
 /* Handle the --text-column option */
 {
@@ -327,7 +339,7 @@ static void OptTextColumn (const char* Opt, const char* Arg)
 
 
 static void OptVerbose (const char* Opt attribute ((unused)),
-                       const char* Arg attribute ((unused)))
+                        const char* Arg attribute ((unused)))
 /* Increase verbosity */
 {
     ++Verbosity;
@@ -336,12 +348,11 @@ static void OptVerbose (const char* Opt attribute ((unused)),
 
 
 static void OptVersion (const char* Opt attribute ((unused)),
-                       const char* Arg attribute ((unused)))
+                        const char* Arg attribute ((unused)))
 /* Print the disassembler version */
 {
-    fprintf (stderr,
-                    "da65 V%u.%u.%u - (C) Copyright 2000-2006, Ullrich von Bassewitz\n",
-                    VER_MAJOR, VER_MINOR, VER_PATCH);
+    fprintf (stderr, "%s V%s\n", ProgName, GetVersionAsString ());
+    exit(EXIT_SUCCESS);
 }
 
 
@@ -349,6 +360,9 @@ static void OptVersion (const char* Opt attribute ((unused)),
 static void OneOpcode (unsigned RemainingBytes)
 /* Disassemble one opcode */
 {
+    unsigned I;
+    unsigned OldPC = PC;
+
     /* Get the opcode from the current address */
     unsigned char OPC = GetCodeByte (PC);
 
@@ -358,57 +372,74 @@ static void OneOpcode (unsigned RemainingBytes)
     /* Get the output style for the current PC */
     attr_t Style = GetStyleAttr (PC);
 
+    /* If a segment begins here, then name that segment.
+    ** Note that the segment is named even if its code is being skipped,
+    ** because some of its later code might not be skipped.
+    */
+    if (IsSegmentStart (PC)) {
+        StartSegment (GetSegmentStartName (PC), GetSegmentAddrSize (PC));
+    }
+
     /* If we have a label at this address, output the label and an attached
-     * comment, provided that we aren't in a skip area.
-     */
+    ** comment, provided that we aren't in a skip area.
+    */
     if (Style != atSkip && MustDefLabel (PC)) {
         const char* Comment = GetComment (PC);
         if (Comment) {
             UserComment (Comment);
         }
-       DefLabel (GetLabelName (PC));
+        DefLabel (GetLabelName (PC));
     }
 
     /* Check...
-     *   - ...if we have enough bytes remaining for the code at this address.
-     *   - ...if the current instruction is valid for the given CPU.
-     *   - ...if there is no label somewhere between the instruction bytes.
-     * If any of these conditions is false, switch to data mode.
-     */
+    **   - ...if we have enough bytes remaining for the code at this address.
+    **   - ...if the current instruction is valid for the given CPU.
+    **   - ...if there is no label somewhere between the instruction bytes.
+    **   - ...if there is no segment change between the instruction bytes.
+    ** If any one of those conditions is false, switch to data mode.
+    */
     if (Style == atDefault) {
-       if (D->Size > RemainingBytes) {
-           Style = atIllegal;
-           MarkAddr (PC, Style);
-               } else if (D->Flags & flIllegal) {
-           Style = atIllegal;
-           MarkAddr (PC, Style);
-       } else {
-           unsigned I;
-           for (I = 1; I < D->Size; ++I) {
-               if (HaveLabel (PC+I) || HaveSegmentChange (PC+I)) {
-                   Style = atIllegal;
-                   MarkAddr (PC, Style);
-                   break;
-               }
-           }
-       }
+        if (D->Size > RemainingBytes) {
+            Style = atIllegal;
+            MarkAddr (PC, Style);
+        } else if (D->Flags & flIllegal) {
+            Style = atIllegal;
+            MarkAddr (PC, Style);
+        } else {
+            for (I = PC + D->Size; --I > PC; ) {
+                if (HaveLabel (I) || IsSegmentStart (I)) {
+                    Style = atIllegal;
+                    MarkAddr (PC, Style);
+                    break;
+                }
+            }
+            for (I = 0; I < D->Size - 1u; ++I) {
+                if (IsSegmentEnd (PC + I)) {
+                    Style = atIllegal;
+                    MarkAddr (PC, Style);
+                    break;
+                }
+            }
+        }
     }
 
     /* Disassemble the line */
     switch (Style) {
 
-       case atDefault:
-           D->Handler (D);
-           PC += D->Size;
-           break;
+        case atDefault:
+            D->Handler (D);
+            PC += D->Size;
+            break;
 
-       case atCode:
+        case atCode:
             /* Beware: If we don't have enough bytes left to disassemble the
-             * following insn, fall through to byte mode.
-             */
+            ** following insn, fall through to byte mode.
+            */
             if (D->Size <= RemainingBytes) {
                 /* Output labels within the next insn */
-                ForwardLabels (D->Size);
+                for (I = 1; I < D->Size; ++I) {
+                    ForwardLabel (I);
+                }
                 /* Output the insn */
                 D->Handler (D);
                 PC += D->Size;
@@ -416,43 +447,52 @@ static void OneOpcode (unsigned RemainingBytes)
             }
             /* FALLTHROUGH */
 
-       case atByteTab:
-           ByteTable ();
-           break;
+        case atByteTab:
+            ByteTable ();
+            break;
 
         case atDByteTab:
             DByteTable ();
             break;
 
-       case atWordTab:
-           WordTable ();
-           break;
+        case atWordTab:
+            WordTable ();
+            break;
 
-       case atDWordTab:
-           DWordTable ();
-           break;
+        case atDWordTab:
+            DWordTable ();
+            break;
 
-       case atAddrTab:
-           AddrTable ();
-           break;
+        case atAddrTab:
+            AddrTable ();
+            break;
 
-       case atRtsTab:
-           RtsTable ();
-           break;
+        case atRtsTab:
+            RtsTable ();
+            break;
 
-       case atTextTab:
-           TextTable ();
-           break;
+        case atTextTab:
+            TextTable ();
+            break;
 
         case atSkip:
             ++PC;
             break;
 
-       default:
-           DataByteLine (1);
-           ++PC;
-           break;
+        default:
+            DataByteLine (1);
+            ++PC;
+            break;
+    }
 
+    /* Change back to the default CODE segment if
+    ** a named segment stops at the current address.
+    */
+    for (I = PC - OldPC; I > 0; --I) {
+        if (IsSegmentEnd (PC - I)) {
+            EndSegment ();
+            break;
+        }
     }
 }
 
@@ -465,7 +505,7 @@ static void OnePass (void)
 
     /* Disassemble until nothing left */
     while ((Count = GetRemainingBytes()) > 0) {
-       OneOpcode (Count);
+        OneOpcode (Count);
     }
 }
 
@@ -500,19 +540,20 @@ int main (int argc, char* argv [])
         { "--bytes-per-line",   1,      OptBytesPerLine         },
         { "--comment-column",   1,      OptCommentColumn        },
         { "--comments",         1,      OptComments             },
-        { "--cpu",                     1,      OptCPU                  },
-               { "--debug-info",       0,      OptDebugInfo            },
-       { "--formfeeds",        0,      OptFormFeeds            },
-       { "--help",             0,      OptHelp                 },
-               { "--hexoffs",          0,      OptHexOffs              },
-               { "--info",             1,      OptInfo                 },
+        { "--cpu",              1,      OptCPU                  },
+        { "--debug-info",       0,      OptDebugInfo            },
+        { "--formfeeds",        0,      OptFormFeeds            },
+        { "--help",             0,      OptHelp                 },
+        { "--hexoffs",          0,      OptHexOffs              },
+        { "--info",             1,      OptInfo                 },
         { "--label-break",      1,      OptLabelBreak           },
         { "--mnemonic-column",  1,      OptMnemonicColumn       },
-       { "--pagelength",       1,      OptPageLength           },
-       { "--start-addr",       1,      OptStartAddr            },
+        { "--pagelength",       1,      OptPageLength           },
+        { "--start-addr",       1,      OptStartAddr            },
+        { "--sync-lines",       0,      OptSyncLines            },
         { "--text-column",      1,      OptTextColumn           },
-       { "--verbose",          0,      OptVerbose              },
-       { "--version",          0,      OptVersion              },
+        { "--verbose",          0,      OptVerbose              },
+        { "--version",          0,      OptVersion              },
     };
 
     unsigned I;
@@ -525,63 +566,67 @@ int main (int argc, char* argv [])
     I = 1;
     while (I < ArgCount) {
 
-               /* Get the argument */
-               const char* Arg = ArgVec[I];
-
-               /* Check for an option */
-               if (Arg [0] == '-') {
-                   switch (Arg [1]) {
-
-               case '-':
-                   LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
-                   break;
-
-               case 'g':
-                   OptDebugInfo (Arg, 0);
-                   break;
-
-               case 'h':
-                   OptHelp (Arg, 0);
-                   break;
-
-                       case 'i':
-                           OptInfo (Arg, GetArg (&I, 2));
-                           break;
-
-                       case 'o':
-                           OutFile = GetArg (&I, 2);
-                           break;
-
-                       case 'v':
-                   OptVerbose (Arg, 0);
-                           break;
-
-               case 'S':
-                   OptStartAddr (Arg, GetArg (&I, 2));
-                   break;
-
-                       case 'V':
-                   OptVersion (Arg, 0);
-                           break;
-
-                       default:
-                           UnknownOption (Arg);
-                   break;
-
-           }
-               } else {
-           /* Filename. Check if we already had one */
-           if (InFile) {
-               fprintf (stderr, "%s: Don't know what to do with `%s'\n",
-                        ProgName, Arg);
-               exit (EXIT_FAILURE);
-           } else {
-               InFile = Arg;
-           }
-       }
-
-       /* Next argument */
-       ++I;
+        /* Get the argument */
+        const char* Arg = ArgVec[I];
+
+        /* Check for an option */
+        if (Arg [0] == '-') {
+            switch (Arg [1]) {
+
+                case '-':
+                    LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
+                    break;
+
+                case 'g':
+                    OptDebugInfo (Arg, 0);
+                    break;
+
+                case 'h':
+                    OptHelp (Arg, 0);
+                    break;
+
+                case 'i':
+                    OptInfo (Arg, GetArg (&I, 2));
+                    break;
+
+                case 'o':
+                    OutFile = GetArg (&I, 2);
+                    break;
+
+                case 'v':
+                    OptVerbose (Arg, 0);
+                    break;
+
+                case 'S':
+                    OptStartAddr (Arg, GetArg (&I, 2));
+                    break;
+
+                case 's':
+                    OptSyncLines (Arg, 0);
+                    break;
+
+                case 'V':
+                    OptVersion (Arg, 0);
+                    break;
+
+                default:
+                    UnknownOption (Arg);
+                    break;
+
+            }
+        } else {
+            /* Filename. Check if we already had one */
+            if (InFile) {
+                fprintf (stderr, "%s: Don't know what to do with `%s'\n",
+                         ProgName, Arg);
+                exit (EXIT_FAILURE);
+            } else {
+                InFile = Arg;
+            }
+        }
+
+        /* Next argument */
+        ++I;
     }
 
     /* Try to read the info file */
@@ -589,13 +634,13 @@ int main (int argc, char* argv [])
 
     /* Must have an input file */
     if (InFile == 0) {
-       AbEnd ("No input file");
+        AbEnd ("No input file");
     }
 
     /* Check the formatting options for reasonable values. Note: We will not
-     * really check that they make sense, just that they aren't complete
-     * garbage.
-     */
+    ** really check that they make sense, just that they aren't complete
+    ** garbage.
+    */
     if (MCol >= ACol) {
         AbEnd ("mnemonic-column value must be smaller than argument-column value");
     }
@@ -612,8 +657,8 @@ int main (int argc, char* argv [])
     }
 
     /* Get the current time and convert it to string so it can be used in
-     * the output page headers.
-     */
+    ** the output page headers.
+    */
     T = time (0);
     strftime (Now, sizeof (Now), "%Y-%m-%d %H:%M:%S", localtime (&T));
 
@@ -632,6 +677,3 @@ int main (int argc, char* argv [])
     /* Done */
     return EXIT_SUCCESS;
 }
-
-
-