1 /*****************************************************************************/
5 /* Source file line info structure */
9 /* (C) 2001 Ullrich von Bassewitz */
10 /* Römerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
48 /*****************************************************************************/
50 /*****************************************************************************/
54 static CodeRange* NewCodeRange (unsigned long Offs, unsigned long Size)
55 /* Create and return a new CodeRange struct */
58 CodeRange* R = xmalloc (sizeof (CodeRange));
60 /* Initialize the fields */
64 /* Return the new struct */
70 static LineInfo* NewLineInfo (void)
71 /* Create and return a new LineInfo struct */
74 LineInfo* LI = xmalloc (sizeof (LineInfo));
76 /* Initialize the fields */
78 InitFilePos (&LI->Pos);
79 InitCollection (&LI->Fragments);
80 InitCollection (&LI->CodeRanges);
82 /* Return the new struct */
88 LineInfo* ReadLineInfo (FILE* F, ObjData* O)
89 /* Read a line info from a file and return it */
91 /* Allocate a new LineInfo struct and initialize it */
92 LineInfo* LI = NewLineInfo ();
94 /* Read the file position */
95 ReadFilePos (F, &LI->Pos);
97 /* Resolve the file index to a pointer to FileInfo struct */
98 CHECK (LI->Pos.Name < O->FileCount);
99 LI->File = O->Files[LI->Pos.Name];
101 /* Return the new LineInfo */
107 static void AddCodeRange (LineInfo* LI, unsigned long Offs, unsigned long Size)
108 /* Add a range of code to this line */
112 /* Get a pointer to the collection */
113 Collection* CodeRanges = &LI->CodeRanges;
115 /* We will keep the CodeRanges collection sorted by starting offset,
116 * so we have to search for the correct insert position. Since in most
117 * cases, the fragments have increasing order, and there is usually not
118 * more than one or two ranges, we do a linear search.
120 for (I = 0; I < CollCount (CodeRanges); ++I) {
121 CodeRange* R = CollAtUnchecked (CodeRanges, I);
122 if (Offs < R->Offs) {
124 /* Got the insert position */
125 if (Offs + Size == R->Offs) {
130 /* Insert a new entry */
131 CollInsert (CodeRanges, NewCodeRange (Offs, Size), I);
137 } else if (R->Offs + R->Size == Offs) {
139 /* This is the regular case. Merge the two. */
148 /* We must append an entry */
149 CollAppend (CodeRanges, NewCodeRange (Offs, Size));
154 void RelocLineInfo (Segment* S)
155 /* Relocate the line info for a segment. */
157 unsigned long Offs = 0;
159 /* Loop over all sections in this segment */
160 Section* Sec = S->SecRoot;
164 /* Adjust for fill bytes */
167 /* Loop over all fragments in this section */
168 Frag = Sec->FragRoot;
171 /* Add the range for this fragment to the line info if there
175 AddCodeRange (Frag->LI, Offs, Frag->Size);
178 /* Update the offset */