1 /*****************************************************************************/
5 /* Source file line info structure */
9 /* (C) 2001-2011, 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;
70 LI->Spans = EmptyCollection;
72 /* Return the new struct */
78 void FreeLineInfo (LineInfo* LI)
79 /* Free a LineInfo structure. */
81 /* Free the collections */
82 DoneCollection (&LI->Spans);
84 /* Free the structure itself */
90 LineInfo* GenLineInfo (const FilePos* Pos)
91 /* Generate a new (internally used) line info with the given information */
93 /* Create a new LineInfo struct */
94 LineInfo* LI = NewLineInfo ();
96 /* Initialize the fields in the new LineInfo */
99 /* Return the struct read */
105 LineInfo* ReadLineInfo (FILE* F, ObjData* O)
106 /* Read a line info from a file and return it */
108 /* Create a new LineInfo struct */
109 LineInfo* LI = NewLineInfo ();
111 /* Read/fill the fields in the new LineInfo */
112 LI->Pos.Line = ReadVar (F);
113 LI->Pos.Col = ReadVar (F);
114 LI->File = CollAt (&O->Files, ReadVar (F));
115 LI->Pos.Name = LI->File->Name;
116 LI->Type = ReadVar (F);
117 ReadSpans (&LI->Spans, F, O);
119 /* Return the struct read */
125 void ReadLineInfoList (FILE* F, ObjData* O, Collection* LineInfos)
126 /* Read a list of line infos stored as a list of indices in the object file,
127 * make real line infos from them and place them into the passed collection.
130 /* Read the number of line info indices that follow */
131 unsigned LineInfoCount = ReadVar (F);
133 /* Grow the collection as needed */
134 CollGrow (LineInfos, LineInfoCount);
136 /* Read the line infos and resolve them */
137 while (LineInfoCount--) {
140 unsigned LineInfoIndex = ReadVar (F);
142 /* The line info index was written by the assembler and must
143 * therefore be part of the line infos read from the object file.
145 if (LineInfoIndex >= CollCount (&O->LineInfos)) {
146 Internal ("Invalid line info index %u in module `%s' - max is %u",
149 CollCount (&O->LineInfos));
152 /* Add the line info to the collection */
153 CollAppend (LineInfos, CollAt (&O->LineInfos, LineInfoIndex));
159 const LineInfo* GetAsmLineInfo (const Collection* LineInfos)
160 /* Find a line info of type LI_TYPE_ASM in the given collection and return it.
161 * Return NULL if no such line info was found.
166 /* Search for a line info of LI_TYPE_ASM */
167 for (I = 0; I < CollCount (LineInfos); ++I) {
168 const LineInfo* LI = CollConstAt (LineInfos, I);
169 if (LI_GET_TYPE (LI->Type) == LI_TYPE_ASM) {
180 void AssignLineInfoIds (void)
181 /* Assign the ids to the line infos */
185 /* Walk over all line infos */
187 for (I = 0; I < CollCount (&ObjDataList); ++I) {
189 /* Get the object file */
190 ObjData* O = CollAtUnchecked (&ObjDataList, I);
192 /* Output the line infos */
193 for (J = 0; J < CollCount (&O->LineInfos); ++J) {
195 /* Get this line info */
196 LineInfo* LI = CollAtUnchecked (&O->LineInfos, J);
206 void PrintDbgLineInfo (FILE* F)
207 /* Output the line infos to a debug info file */
211 /* Print line infos from all modules we have linked into the output file */
212 for (I = 0; I < CollCount (&ObjDataList); ++I) {
214 /* Get the object file */
215 const ObjData* O = CollAtUnchecked (&ObjDataList, I);
217 /* Output the line infos */
218 for (J = 0; J < CollCount (&O->LineInfos); ++J) {
220 /* Get this line info */
221 const LineInfo* LI = CollConstAt (&O->LineInfos, J);
223 /* Get the line info type and count */
224 unsigned Type = LI_GET_TYPE (LI->Type);
225 unsigned Count = LI_GET_COUNT (LI->Type);
227 /* Get a pointer to the spans */
228 const Collection* Spans = &LI->Spans;
230 /* Print the start of the line */
232 "line\tid=%u,file=%u,line=%u",
233 LI->Id, LI->File->Id, GetSourceLine (LI));
235 /* Print type if not LI_TYPE_ASM and count if not zero */
236 if (Type != LI_TYPE_ASM) {
237 fprintf (F, ",type=%u", Type);
240 fprintf (F, ",count=%u", Count);
243 /* Add spans if the line info has it */
244 if (CollCount (Spans) > 0) {
246 /* Output the first span */
247 fprintf (F, ",span=%u", SpanId (O, CollConstAt (Spans, 0)));
249 /* Output the other spans */
250 for (K = 1; K < CollCount (Spans); ++K) {
251 fprintf (F, "+%u", SpanId (O, CollConstAt (Spans, K)));