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 (Segment* Seg, unsigned long Offs, unsigned long Size)
56 /* Create and return a new CodeRange struct */
59 CodeRange* R = xmalloc (sizeof (CodeRange));
61 /* Initialize the fields */
66 /* Return the new struct */
72 LineInfo* NewLineInfo (ObjData* O, const FilePos* Pos)
73 /* Create and return a new LineInfo struct */
76 LineInfo* LI = xmalloc (sizeof (LineInfo));
78 /* Make sure the name index is valid */
79 CHECK (Pos->Name < CollCount (&O->Files));
81 /* Initialize the fields */
82 LI->File = CollAt (&O->Files, Pos->Name);
84 InitCollection (&LI->Fragments);
85 InitCollection (&LI->CodeRanges);
87 /* Return the new struct */
93 LineInfo* ReadLineInfo (FILE* F, ObjData* O)
94 /* Read a line info from a file and return it */
96 /* Read the file position */
98 ReadFilePos (F, &Pos);
100 /* Allocate a new LineInfo struct, initialize and return it */
101 return NewLineInfo (O, &Pos);
106 static void AddCodeRange (LineInfo* LI, Segment* Seg, unsigned long Offs,
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 /* Must be same segment */
124 if (Offs < R->Offs) {
126 /* Got the insert position */
127 if (Offs + Size == R->Offs) {
132 /* Insert a new entry */
133 CollInsert (CodeRanges, NewCodeRange (Seg, Offs, Size), I);
139 } else if (R->Offs + R->Size == Offs) {
140 /* This is the regular case. Merge the two. */
149 /* We must append an entry */
150 CollAppend (CodeRanges, NewCodeRange (Seg, Offs, Size));
155 void RelocLineInfo (Segment* S)
156 /* Relocate the line info for a segment. */
158 unsigned long Offs = S->PC;
160 /* Loop over all sections in this segment */
161 Section* Sec = S->SecRoot;
165 /* Adjust for fill bytes */
168 /* Loop over all fragments in this section */
169 Frag = Sec->FragRoot;
174 /* Add the range for this fragment to all line infos */
175 for (I = 0; I < CollCount (&Frag->LineInfos); ++I) {
176 AddCodeRange (CollAt (&Frag->LineInfos, I), S, Offs, Frag->Size);
179 /* Update the offset */