1 /*****************************************************************************/
5 /* Source file line info structure */
9 /* (C) 2001-2012, 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 /*****************************************************************************/
51 /*****************************************************************************/
53 /*****************************************************************************/
57 static LineInfo* NewLineInfo (void)
58 /* Create and return a new LineInfo struct with mostly empty fields */
61 LineInfo* LI = xmalloc (sizeof (LineInfo));
63 /* Initialize the fields */
66 LI->Type = LI_MAKE_TYPE (LI_TYPE_ASM, 0);
67 LI->Pos.Name = INVALID_STRING_ID;
72 /* Return the new struct */
78 void FreeLineInfo (LineInfo* LI)
79 /* Free a LineInfo structure. */
81 /* Free the span list */
84 /* Free the structure itself */
90 LineInfo* DupLineInfo (const LineInfo* LI)
91 /* Creates a duplicate of a line info structure */
94 LineInfo* New = xmalloc (sizeof (LineInfo));
96 /* Copy the fields (leave id invalid) */
101 New->Spans = DupSpanList (LI->Spans);
103 /* Return the copy */
109 LineInfo* GenLineInfo (const FilePos* Pos)
110 /* Generate a new (internally used) line info with the given information */
112 /* Create a new LineInfo struct */
113 LineInfo* LI = NewLineInfo ();
115 /* Initialize the fields in the new LineInfo */
118 /* Return the struct read */
124 LineInfo* ReadLineInfo (FILE* F, ObjData* O)
125 /* Read a line info from a file and return it */
127 /* Create a new LineInfo struct */
128 LineInfo* LI = NewLineInfo ();
130 /* Read/fill the fields in the new LineInfo */
131 LI->Pos.Line = ReadVar (F);
132 LI->Pos.Col = ReadVar (F);
133 LI->File = CollAt (&O->Files, ReadVar (F));
134 LI->Pos.Name = LI->File->Name;
135 LI->Type = ReadVar (F);
136 LI->Spans = ReadSpanList (F);
138 /* Return the struct read */
144 void ReadLineInfoList (FILE* F, ObjData* O, Collection* LineInfos)
145 /* Read a list of line infos stored as a list of indices in the object file,
146 * make real line infos from them and place them into the passed collection.
149 /* Read the number of line info indices that follow */
150 unsigned LineInfoCount = ReadVar (F);
152 /* Grow the collection as needed */
153 CollGrow (LineInfos, LineInfoCount);
155 /* Read the line infos and resolve them */
156 while (LineInfoCount--) {
159 unsigned LineInfoIndex = ReadVar (F);
161 /* The line info index was written by the assembler and must
162 * therefore be part of the line infos read from the object file.
164 if (LineInfoIndex >= CollCount (&O->LineInfos)) {
165 Internal ("Invalid line info index %u in module `%s' - max is %u",
168 CollCount (&O->LineInfos));
171 /* Add the line info to the collection */
172 CollAppend (LineInfos, CollAt (&O->LineInfos, LineInfoIndex));
178 const LineInfo* GetAsmLineInfo (const Collection* LineInfos)
179 /* Find a line info of type LI_TYPE_ASM and count zero in the given collection
180 * and return it. Return NULL if no such line info was found.
185 /* Search for a line info of LI_TYPE_ASM */
186 for (I = 0; I < CollCount (LineInfos); ++I) {
187 const LineInfo* LI = CollConstAt (LineInfos, I);
188 if (LI->Type == LI_MAKE_TYPE (LI_TYPE_ASM, 0)) {
199 unsigned LineInfoCount (void)
200 /* Return the total number of line infos */
202 /* Walk over all object files */
205 for (I = 0; I < CollCount (&ObjDataList); ++I) {
207 /* Get this object file */
208 const ObjData* O = CollAtUnchecked (&ObjDataList, I);
211 Count += CollCount (&O->LineInfos);
219 void AssignLineInfoIds (void)
220 /* Assign the ids to the line infos */
224 /* Walk over all line infos */
226 for (I = 0; I < CollCount (&ObjDataList); ++I) {
228 /* Get the object file */
229 ObjData* O = CollAtUnchecked (&ObjDataList, I);
231 /* Output the line infos */
232 for (J = 0; J < CollCount (&O->LineInfos); ++J) {
234 /* Get this line info */
235 LineInfo* LI = CollAtUnchecked (&O->LineInfos, J);
245 void PrintDbgLineInfo (FILE* F)
246 /* Output the line infos to a debug info file */
250 /* Print line infos from all modules we have linked into the output file */
251 for (I = 0; I < CollCount (&ObjDataList); ++I) {
253 /* Get the object file */
254 const ObjData* O = CollAtUnchecked (&ObjDataList, I);
256 /* Output the line infos */
257 for (J = 0; J < CollCount (&O->LineInfos); ++J) {
259 /* Get this line info */
260 const LineInfo* LI = CollConstAt (&O->LineInfos, J);
262 /* Get the line info type and count */
263 unsigned Type = LI_GET_TYPE (LI->Type);
264 unsigned Count = LI_GET_COUNT (LI->Type);
266 /* Print the start of the line */
268 "line\tid=%u,file=%u,line=%u",
269 LI->Id, LI->File->Id, GetSourceLine (LI));
271 /* Print type if not LI_TYPE_ASM and count if not zero */
272 if (Type != LI_TYPE_ASM) {
273 fprintf (F, ",type=%u", Type);
276 fprintf (F, ",count=%u", Count);
279 /* Add spans if the line info has it */
280 PrintDbgSpanList (F, O, LI->Spans);