]> git.sur5r.net Git - cc65/blob - src/ld65/lineinfo.c
Changed data type used to store line numbers from unsigned long to unsigned.
[cc65] / src / ld65 / lineinfo.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                lineinfo.h                                 */
4 /*                                                                           */
5 /*                      Source file line info structure                      */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2001-2011, Ullrich von Bassewitz                                      */
10 /*                Roemerstrasse 52                                           */
11 /*                D-70794 Filderstadt                                        */
12 /* EMail:         uz@cc65.org                                                */
13 /*                                                                           */
14 /*                                                                           */
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.                                    */
18 /*                                                                           */
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:                            */
22 /*                                                                           */
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              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 /* common */
37 #include "check.h"
38 #include "lidefs.h"
39 #include "xmalloc.h"
40
41 /* ld65 */
42 #include "error.h"
43 #include "fileinfo.h"
44 #include "fileio.h"
45 #include "lineinfo.h"
46 #include "objdata.h"
47 #include "segments.h"
48
49
50
51 /*****************************************************************************/
52 /*                                   Code                                    */
53 /*****************************************************************************/
54
55
56
57 static LineInfo* NewLineInfo (void)
58 /* Create and return a new LineInfo struct with mostly empty fields */
59 {
60     /* Allocate memory */
61     LineInfo* LI = xmalloc (sizeof (LineInfo));
62
63     /* Initialize the fields */
64     LI->Id         = ~0U;
65     LI->File       = 0;
66     LI->Type       = LI_MAKE_TYPE (LI_TYPE_ASM, 0);
67     LI->Pos.Name   = INVALID_STRING_ID;
68     LI->Pos.Line   = 0;
69     LI->Pos.Col    = 0;
70     LI->Spans      = EmptyCollection;
71
72     /* Return the new struct */
73     return LI;
74 }
75
76
77
78 void FreeLineInfo (LineInfo* LI)
79 /* Free a LineInfo structure. */
80 {
81     /* Free the collections */
82     DoneCollection (&LI->Spans);
83
84     /* Free the structure itself */
85     xfree (LI);
86 }
87
88
89
90 LineInfo* GenLineInfo (const FilePos* Pos)
91 /* Generate a new (internally used) line info with the given information */
92 {
93     /* Create a new LineInfo struct */
94     LineInfo* LI = NewLineInfo ();
95
96     /* Initialize the fields in the new LineInfo */
97     LI->Pos = *Pos;
98
99     /* Return the struct read */
100     return LI;
101 }
102
103
104
105 LineInfo* ReadLineInfo (FILE* F, ObjData* O)
106 /* Read a line info from a file and return it */
107 {
108     /* Create a new LineInfo struct */
109     LineInfo* LI = NewLineInfo ();
110
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);
118
119     /* Return the struct read */
120     return LI;
121 }
122
123
124
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.
128  */
129 {
130     /* Read the number of line info indices that follow */
131     unsigned LineInfoCount = ReadVar (F);
132
133     /* Grow the collection as needed */
134     CollGrow (LineInfos, LineInfoCount);
135
136     /* Read the line infos and resolve them */
137     while (LineInfoCount--) {
138
139         /* Read an index */
140         unsigned LineInfoIndex = ReadVar (F);
141
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.
144          */
145         if (LineInfoIndex >= CollCount (&O->LineInfos)) {
146             Internal ("Invalid line info index %u in module `%s' - max is %u",
147                       LineInfoIndex,
148                       GetObjFileName (O),
149                       CollCount (&O->LineInfos));
150         }
151
152         /* Add the line info to the collection */
153         CollAppend (LineInfos, CollAt (&O->LineInfos, LineInfoIndex));
154     }
155 }
156
157
158
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.
162  */
163 {
164     unsigned I;
165
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) {
170             return LI;
171         }
172     }
173
174     /* Not found */
175     return 0;
176 }
177
178
179
180 void AssignLineInfoIds (void)
181 /* Assign the ids to the line infos */
182 {
183     unsigned I, J;
184
185     /* Walk over all line infos */
186     unsigned Id = 0;
187     for (I = 0; I < CollCount (&ObjDataList); ++I) {
188
189         /* Get the object file */
190         ObjData* O = CollAtUnchecked (&ObjDataList, I);
191
192         /* Output the line infos */
193         for (J = 0; J < CollCount (&O->LineInfos); ++J) {
194
195             /* Get this line info */
196             LineInfo* LI = CollAtUnchecked (&O->LineInfos, J);
197
198             /* Assign the id */
199             LI->Id = Id++;
200         }
201     }
202 }
203
204
205
206 void PrintDbgLineInfo (FILE* F)
207 /* Output the line infos to a debug info file */
208 {
209     unsigned I, J, K;
210
211     /* Print line infos from all modules we have linked into the output file */
212     for (I = 0; I < CollCount (&ObjDataList); ++I) {
213
214         /* Get the object file */
215         const ObjData* O = CollAtUnchecked (&ObjDataList, I);
216
217         /* Output the line infos */
218         for (J = 0; J < CollCount (&O->LineInfos); ++J) {
219
220             /* Get this line info */
221             const LineInfo* LI = CollConstAt (&O->LineInfos, J);
222
223             /* Get the line info type and count */
224             unsigned Type  = LI_GET_TYPE (LI->Type);
225             unsigned Count = LI_GET_COUNT (LI->Type);
226
227             /* Get a pointer to the spans */
228             const Collection* Spans = &LI->Spans;
229
230             /* Print the start of the line */
231             fprintf (F,
232                      "line\tid=%u,file=%u,line=%u",
233                      LI->Id, LI->File->Id, GetSourceLine (LI));
234
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);
238             }
239             if (Count != 0) {
240                 fprintf (F, ",count=%u", Count);
241             }
242
243             /* Add spans if the line info has it */
244             if (CollCount (Spans) > 0) {
245
246                 /* Output the first span */
247                 fprintf (F, ",span=%u", SpanId (O, CollConstAt (Spans, 0)));
248
249                 /* Output the other spans */
250                 for (K = 1; K < CollCount (Spans); ++K) {
251                     fprintf (F, "+%u", SpanId (O, CollConstAt (Spans, K)));
252                 }
253             }
254
255             /* Terminate line */
256             fputc ('\n', F);
257         }
258     }
259 }
260
261
262