1 /*****************************************************************************/
5 /* Source file line info structure */
9 /* (C) 2001-2010, Ullrich von Bassewitz */
10 /* Roemerstrasse 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 /*****************************************************************************/
49 /*****************************************************************************/
51 /*****************************************************************************/
55 static CodeRange* NewCodeRange (unsigned long Offs, unsigned long Size)
56 /* Create and return a new CodeRange struct */
59 CodeRange* R = xmalloc (sizeof (CodeRange));
61 /* Initialize the fields */
65 /* Return the new struct */
71 LineInfo* NewLineInfo (ObjData* O, const FilePos* Pos)
72 /* Create and return a new LineInfo struct */
75 LineInfo* LI = xmalloc (sizeof (LineInfo));
77 /* Make sure the name index is valid */
78 CHECK (Pos->Name < CollCount (&O->Files));
80 /* Initialize the fields */
81 LI->File = CollAt (&O->Files, Pos->Name);
83 InitCollection (&LI->Fragments);
84 InitCollection (&LI->CodeRanges);
86 /* Return the new struct */
92 LineInfo* ReadLineInfo (FILE* F, ObjData* O)
93 /* Read a line info from a file and return it */
95 /* Read the file position */
97 ReadFilePos (F, &Pos);
99 /* Allocate a new LineInfo struct, initialize and return it */
100 return NewLineInfo (O, &Pos);
105 static void AddCodeRange (LineInfo* LI, unsigned long Offs, unsigned long Size)
106 /* Add a range of code to this line */
110 /* Get a pointer to the collection */
111 Collection* CodeRanges = &LI->CodeRanges;
113 /* We will keep the CodeRanges collection sorted by starting offset,
114 * so we have to search for the correct insert position. Since in most
115 * cases, the fragments have increasing order, and there is usually not
116 * more than one or two ranges, we do a linear search.
118 for (I = 0; I < CollCount (CodeRanges); ++I) {
119 CodeRange* R = CollAtUnchecked (CodeRanges, I);
120 if (Offs < R->Offs) {
122 /* Got the insert position */
123 if (Offs + Size == R->Offs) {
128 /* Insert a new entry */
129 CollInsert (CodeRanges, NewCodeRange (Offs, Size), I);
135 } else if (R->Offs + R->Size == Offs) {
137 /* This is the regular case. Merge the two. */
146 /* We must append an entry */
147 CollAppend (CodeRanges, NewCodeRange (Offs, Size));
152 void RelocLineInfo (Segment* S)
153 /* Relocate the line info for a segment. */
155 unsigned long Offs = S->PC;
157 /* Loop over all sections in this segment */
158 Section* Sec = S->SecRoot;
162 /* Adjust for fill bytes */
165 /* Loop over all fragments in this section */
166 Frag = Sec->FragRoot;
171 /* Add the range for this fragment to all line infos */
172 for (I = 0; I < CollCount (&Frag->LineInfos); ++I) {
173 AddCodeRange (CollAt (&Frag->LineInfos, I), Offs, Frag->Size);
176 /* Update the offset */