]> git.sur5r.net Git - cc65/blobdiff - src/ld65/segments.c
New module strstack
[cc65] / src / ld65 / segments.c
index e203af508842a3e90adaa17c2e2943b559140f9b..1fb7fcbd792d16e58fc3e8dfcf57ff23338edb81 100644 (file)
@@ -6,9 +6,9 @@
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 1998-2001 Ullrich von Bassewitz                                       */
-/*               Wacholderweg 14                                             */
-/*               D-70597 Stuttgart                                           */
+/* (C) 1998-2003 Ullrich von Bassewitz                                       */
+/*               Römerstraße 52                                              */
+/*               D-70794 Filderstadt                                         */
 /* EMail:        uz@cc65.org                                                 */
 /*                                                                           */
 /*                                                                           */
@@ -39,6 +39,7 @@
 /* common */
 #include "check.h"
 #include "exprdefs.h"
+#include "fragdefs.h"
 #include "hashstr.h"
 #include "print.h"
 #include "segdefs.h"
@@ -53,6 +54,7 @@
 #include "global.h"
 #include "lineinfo.h"
 #include "segments.h"
+#include "spool.h"
 
 
 
 
 
 /* Hash table */
-#define HASHTAB_SIZE   253
+#define HASHTAB_MASK    0x3FU
+#define HASHTAB_SIZE   (HASHTAB_MASK + 1)
 static Segment*                HashTab [HASHTAB_SIZE];
 
 static unsigned                SegCount = 0;   /* Segment count */
-static Segment*                SegRoot = 0;    /* List of all segments */
+static Segment*                SegRoot = 0;    /* List of all segments */
 
 
 
@@ -77,47 +80,28 @@ static Segment*             SegRoot = 0;    /* List of all segments */
 
 
 
-static Segment* SegFindInternal (const char* Name, unsigned HashVal)
-/* Try to find the segment with the given name, return a pointer to the
- * segment structure, or 0 if not found.
- */
-{
-    Segment* S = HashTab [HashVal];
-    while (S) {
-       if (strcmp (Name, S->Name) == 0) {
-           /* Found */
-           break;
-       }
-       S = S->Next;
-    }
-    /* Not found */
-    return S;
-}
-
-
-
-static Segment* NewSegment (const char* Name, unsigned HashVal, unsigned char Type)
+static Segment* NewSegment (unsigned Name, unsigned char AddrSize)
 /* Create a new segment and initialize it */
 {
-    /* Get the length of the symbol name */
-    unsigned Len = strlen (Name);
+    unsigned Hash;
 
     /* Allocate memory */
-    Segment* S = xmalloc (sizeof (Segment) + Len);
+    Segment* S = xmalloc (sizeof (Segment));
 
     /* Initialize the fields */
-    S->Next    = 0;
-    S->SecRoot = 0;
-    S->SecLast = 0;
-    S->PC      = 0;
-    S->Size            = 0;
-    S->AlignObj        = 0;
-    S->Align    = 0;
-    S->FillVal = 0;
-    S->Type     = Type;
-    S->Dumped   = 0;
-    memcpy (S->Name, Name, Len);
-    S->Name [Len] = '\0';
+    S->Name        = Name;
+    S->Next       = 0;
+    S->SecRoot    = 0;
+    S->SecLast    = 0;
+    S->PC         = 0;
+    S->Size               = 0;
+    S->AlignObj           = 0;
+    S->Align       = 0;
+    S->FillVal    = 0;
+    S->AddrSize    = AddrSize;
+    S->ReadOnly    = 0;
+    S->Relocatable = 0;
+    S->Dumped      = 0;
 
     /* Insert the segment into the segment list */
     S->List = SegRoot;
@@ -125,8 +109,9 @@ static Segment* NewSegment (const char* Name, unsigned HashVal, unsigned char Ty
     ++SegCount;
 
     /* Insert the segment into the segment hash list */
-    S->Next = HashTab [HashVal];
-    HashTab [HashVal] = S;
+    Hash = (S->Name & HASHTAB_MASK);
+    S->Next = HashTab[Hash];
+    HashTab[Hash] = S;
 
     /* Return the new entry */
     return S;
@@ -134,30 +119,30 @@ static Segment* NewSegment (const char* Name, unsigned HashVal, unsigned char Ty
 
 
 
-Segment* GetSegment (const char* Name, unsigned char Type, const char* ObjName)
+Segment* GetSegment (unsigned Name, unsigned char AddrSize, const char* ObjName)
 /* Search for a segment and return an existing one. If the segment does not
  * exist, create a new one and return that. ObjName is only used for the error
  * message and may be NULL if the segment is linker generated.
  */
 {
-    /* Create a hash over the name and try to locate the segment in the table */
-    unsigned HashVal = HashStr (Name) % HASHTAB_SIZE;
-    Segment* S = SegFindInternal (Name, HashVal);
+    /* Try to locate the segment in the table */
+    Segment* S = SegFind (Name);
 
     /* If we don't have that segment already, allocate it using the type of
      * the first section.
      */
     if (S == 0) {
        /* Create a new segment */
-       S = NewSegment (Name, HashVal, Type);
+       S = NewSegment (Name, AddrSize);
     } else {
-               /* Check if the existing segment has the requested type */
-               if (S->Type != Type) {
+               /* Check if the existing segment has the requested address size */
+               if (S->AddrSize != AddrSize) {
            /* Allow an empty object name */
            if (ObjName == 0) {
                ObjName = "[linker generated]";
            }
-           Error ("Module `%s': Type mismatch for segment `%s'", ObjName, Name);
+           Error ("Module `%s': Type mismatch for segment `%s'", ObjName,
+                   GetString (Name));
        }
     }
 
@@ -167,14 +152,14 @@ Segment* GetSegment (const char* Name, unsigned char Type, const char* ObjName)
 
 
 
-Section* NewSection (Segment* Seg, unsigned char Align, unsigned char Type)
+Section* NewSection (Segment* Seg, unsigned char Align, unsigned char AddrSize)
 /* Create a new section for the given segment */
 {
     unsigned long V;
 
 
     /* Allocate memory */
-    Section* S = xmalloc (sizeof (Segment));
+    Section* S = xmalloc (sizeof (Section));
 
     /* Initialize the data */
     S->Next    = 0;
@@ -183,7 +168,7 @@ Section* NewSection (Segment* Seg, unsigned char Align, unsigned char Type)
     S->FragLast = 0;
     S->Size    = 0;
     S->Align    = Align;
-    S->Type     = Type;
+    S->AddrSize = AddrSize;
 
     /* Calculate the alignment bytes needed for the section */
     V = (0x01UL << S->Align) - 1;
@@ -211,35 +196,30 @@ Section* NewSection (Segment* Seg, unsigned char Align, unsigned char Type)
 Section* ReadSection (FILE* F, ObjData* O)
 /* Read a section from a file */
 {
-    char* Name;
-    unsigned long Size;
+    unsigned      Name;
+    unsigned      Size;
     unsigned char Align;
     unsigned char Type;
-    Segment* S;
-    Section* Sec;
-
-    /* Read the name */
-    Name = ReadStr (F);
+    unsigned      FragCount;
+    Segment*      S;
+    Section*      Sec;
 
-    /* Read the size */
-    Size = Read32 (F);
+    /* Read the segment data */
+    (void) Read32 (F);            /* File size of data */
+    Name      = MakeGlobalStringId (O, ReadVar (F));    /* Segment name */
+    Size      = Read32 (F);       /* Size of data */
+    Align     = Read8 (F);        /* Alignment */
+    Type      = Read8 (F);        /* Segment type */
+    FragCount = ReadVar (F);      /* Number of fragments */
 
-    /* Read the alignment */
-    Align = Read8 (F);
-
-    /* Read the segment type */
-    Type = Read8 (F);
 
     /* Print some data */
-    Print (stdout, 2, "Module `%s': Found segment `%s', size = %lu, align = %u, type = %u\n",
-          GetObjFileName (O), Name, Size, Align, Type);
+    Print (stdout, 2, "Module `%s': Found segment `%s', size = %u, align = %u, type = %u\n",
+          GetObjFileName (O), GetString (Name), Size, Align, Type);
 
     /* Get the segment for this section */
     S = GetSegment (Name, Type, GetObjFileName (O));
 
-    /* We have the segment and don't need the name any longer */
-    xfree (Name);
-
     /* Allocate the section we will return later */
     Sec = NewSection (S, Align, Type);
 
@@ -251,7 +231,7 @@ Section* ReadSection (FILE* F, ObjData* O)
     }
 
     /* Start reading fragments from the file and insert them into the section . */
-    while (Size) {
+    while (FragCount--) {
 
        Fragment* Frag;
        unsigned  LineInfoIndex;
@@ -259,22 +239,22 @@ Section* ReadSection (FILE* F, ObjData* O)
        /* Read the fragment type */
        unsigned char Type = Read8 (F);
 
+        /* Extract the check mask from the type */
+        unsigned char Bytes = Type & FRAG_BYTEMASK;
+        Type &= FRAG_TYPEMASK;
+
        /* Handle the different fragment types */
        switch (Type) {
 
            case FRAG_LITERAL:
                Frag = NewFragment (Type, ReadVar (F), Sec);
+               ReadData (F, Frag->LitBuf, Frag->Size);
                break;
 
-           case FRAG_EXPR8:
-           case FRAG_EXPR16:
-                   case FRAG_EXPR24:
-           case FRAG_EXPR32:
-           case FRAG_SEXPR8:
-           case FRAG_SEXPR16:
-           case FRAG_SEXPR24:
-           case FRAG_SEXPR32:
-                       Frag = NewFragment (Type & FRAG_TYPEMASK, Type & FRAG_BYTEMASK, Sec);
+           case FRAG_EXPR:
+           case FRAG_SEXPR:
+                       Frag = NewFragment (Type, Bytes, Sec);
+               Frag->Expr = ReadExpr (F, O);
                break;
 
            case FRAG_FILL:
@@ -284,27 +264,11 @@ Section* ReadSection (FILE* F, ObjData* O)
 
            default:
                Error ("Unknown fragment type in module `%s', segment `%s': %02X",
-                      GetObjFileName (O), S->Name, Type);
+                      GetObjFileName (O), GetString (S->Name), Type);
                /* NOTREACHED */
                return 0;
                }
 
-       /* Now read the fragment data */
-       switch (Frag->Type) {
-
-           case FRAG_LITERAL:
-               /* Literal data */
-               ReadData (F, Frag->LitBuf, Frag->Size);
-               break;
-
-           case FRAG_EXPR:
-           case FRAG_SEXPR:
-               /* An expression */
-               Frag->Expr = ReadExpr (F, O);
-               break;
-
-       }
-
        /* Read the file position of the fragment */
        ReadFilePos (F, &Frag->Pos);
 
@@ -327,10 +291,6 @@ Section* ReadSection (FILE* F, ObjData* O)
 
        /* Remember the module we had this fragment from */
        Frag->Obj = O;
-
-       /* Next one */
-       CHECK (Size >= Frag->Size);
-       Size -= Frag->Size;
     }
 
     /* Return the section */
@@ -339,10 +299,19 @@ Section* ReadSection (FILE* F, ObjData* O)
 
 
 
-Segment* SegFind (const char* Name)
+Segment* SegFind (unsigned Name)
 /* Return the given segment or NULL if not found. */
 {
-    return SegFindInternal (Name, HashStr (Name) % HASHTAB_SIZE);
+    Segment* S = HashTab[Name & HASHTAB_MASK];
+    while (S) {
+               if (Name == S->Name) {
+           /* Found */
+           break;
+       }
+       S = S->Next;
+    }
+    /* Not found */
+    return S;
 }
 
 
@@ -363,7 +332,7 @@ int IsBSSType (Segment* S)
                unsigned long Count = F->Size;
                while (Count--) {
                    if (*Data++ != 0) {
-                       return 0;
+                       return 0;
                    }
                }
            } else if (F->Type == FRAG_EXPR || F->Type == FRAG_SEXPR) {
@@ -390,7 +359,7 @@ void SegDump (void)
     Segment* Seg = SegRoot;
     while (Seg) {
        Section* S = Seg->SecRoot;
-               printf ("Segment: %s (%lu)\n", Seg->Name, Seg->Size);
+               printf ("Segment: %s (%lu)\n", GetString (Seg->Name), Seg->Size);
        while (S) {
            Fragment* F = S->FragRoot;
            printf ("  Section:\n");
@@ -398,7 +367,7 @@ void SegDump (void)
                switch (F->Type) {
 
                    case FRAG_LITERAL:
-                       printf ("    Literal (%lu bytes):", F->Size);
+                       printf ("    Literal (%u bytes):", F->Size);
                        Count = F->Size;
                        Data  = F->LitBuf;
                        I = 100;
@@ -414,19 +383,19 @@ void SegDump (void)
                        break;
 
                    case FRAG_EXPR:
-                       printf ("    Expression (%lu bytes):\n", F->Size);
+                       printf ("    Expression (%u bytes):\n", F->Size);
                        printf ("    ");
-                       DumpExpr (F->Expr);
+                       DumpExpr (F->Expr, 0);
                        break;
 
                    case FRAG_SEXPR:
-                       printf ("    Signed expression (%lu bytes):\n", F->Size);
+                       printf ("    Signed expression (%u bytes):\n", F->Size);
                        printf ("      ");
-                       DumpExpr (F->Expr);
+                       DumpExpr (F->Expr, 0);
                        break;
 
                    case FRAG_FILL:
-                       printf ("    Empty space (%lu bytes)\n", F->Size);
+                       printf ("    Empty space (%u bytes)\n", F->Size);
                        break;
 
                    default:
@@ -507,6 +476,11 @@ void SegWrite (FILE* Tgt, Segment* S, SegWriteFunc F, void* Data)
        Frag = Sec->FragRoot;
        while (Frag) {
 
+            /* Do fragment alignment checks */
+
+
+
+            /* Output fragment data */
            switch (Frag->Type) {
 
                case FRAG_LITERAL:
@@ -519,18 +493,18 @@ void SegWrite (FILE* Tgt, Segment* S, SegWriteFunc F, void* Data)
                    /* Call the users function and evaluate the result */
                    switch (F (Frag->Expr, Sign, Frag->Size, Offs, Data)) {
 
-                       case SEG_EXPR_OK:
-                           break;
+                       case SEG_EXPR_OK:
+                           break;
 
-                       case SEG_EXPR_RANGE_ERROR:
-                           Error ("Range error in module `%s', line %lu",
-                                  GetSourceFileName (Frag->Obj, Frag->Pos.Name),
-                                  Frag->Pos.Line);
-                           break;
+                       case SEG_EXPR_RANGE_ERROR:
+                           Error ("Range error in module `%s', line %lu",
+                                  GetSourceFileName (Frag->Obj, Frag->Pos.Name),
+                                  Frag->Pos.Line);
+                           break;
 
-                       case SEG_EXPR_TOO_COMPLEX:
-                           Error ("Expression too complex in module `%s', line %lu",
-                                  GetSourceFileName (Frag->Obj, Frag->Pos.Name),
+                       case SEG_EXPR_TOO_COMPLEX:
+                           Error ("Expression too complex in module `%s', line %lu",
+                                  GetSourceFileName (Frag->Obj, Frag->Pos.Name),
                                   Frag->Pos.Line);
                            break;
 
@@ -581,7 +555,7 @@ static int CmpSegStart (const void* K1, const void* K2)
        return -1;
     } else {
        /* Sort segments with equal starts by name */
-       return strcmp (S1->Name, S2->Name);
+       return strcmp (GetString (S1->Name), GetString (S2->Name));
     }
 }
 
@@ -629,11 +603,16 @@ void PrintSegmentMap (FILE* F)
        /* Get a pointer to the segment */
        S = SegPool [I];
 
-       /* Print empty segments only if explicitly requested */
-       if (VerboseMap || S->Size > 0) {
-           /* Print the segment data */
-           fprintf (F, "%-20s  %06lX  %06lX  %06lX\n",
-                    S->Name, S->PC, S->PC + S->Size, S->Size);
+       /* Print empty segments only if explicitly requested */
+       if (VerboseMap || S->Size > 0) {
+           /* Print the segment data */
+           long End = S->PC + S->Size;
+           if (S->Size > 0) {
+               /* Point to last element addressed */
+               --End;
+           }
+           fprintf (F, "%-20s  %06lX  %06lX  %06lX\n",
+                            GetString (S->Name), S->PC, End, S->Size);
        }
     }
 
@@ -643,6 +622,32 @@ void PrintSegmentMap (FILE* F)
 
 
 
+void PrintDbgSegments (FILE* F)
+/* Output the segments to the debug file */
+{
+    Segment* S;
+
+    /* Walk over all segments */
+    S = SegRoot;
+    while (S) {
+
+       /* Ignore empty segments */
+        if (S->Size > 0) {
+
+           /* Print the segment data */
+                   fprintf (F, "segment\t\"%s\",start=0x%06lX,size=0x%04lX,addrsize=%s,type=%s\n",
+                            GetString (S->Name), S->PC, S->Size,
+                     AddrSizeToStr (S->AddrSize),
+                     S->ReadOnly? "ro" : "rw");
+       }
+
+       /* Follow the linked list */
+       S = S->List;
+    }
+}
+
+
+
 void CheckSegments (void)
 /* Walk through the segment list and check if there are segments that were
  * not written to the output file. Output an error if this is the case.
@@ -650,8 +655,9 @@ void CheckSegments (void)
 {
     Segment* S = SegRoot;
     while (S) {
-       if (S->Size > 0 && S->Dumped == 0) {
-                   Error ("Missing memory area assignment for segment `%s'", S->Name);
+       if (S->Size > 0 && S->Dumped == 0) {
+                   Error ("Missing memory area assignment for segment `%s'",
+                   GetString (S->Name));
        }
        S = S->List;
     }