]> git.sur5r.net Git - cc65/blob - src/ca65/lineinfo.c
3abfd6e3fedafebde5535428e77b766f2a0497d4
[cc65] / src / ca65 / lineinfo.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                lineinfo.c                                 */
4 /*                                                                           */
5 /*                      Source file line info structure                      */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2001      Ullrich von Bassewitz                                       */
10 /*               Wacholderweg 14                                             */
11 /*               D-70597 Stuttgart                                           */
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 /* Note: The line infos kept here are additional line infos supplied by the
37  * ".dbg line" command. The native line infos are always kept in the fragments
38  * itself (because one fragment always originates from one line). The
39  * additional line infos (which may not exist if none are supplied in the
40  * source) may have several fragments attached (as is the case with sources
41  * generated by the C compiler).
42  */
43
44
45
46 /* common */
47 #include "coll.h"
48 #include "xmalloc.h"
49
50 /* ca65 */
51 #include "objfile.h"
52 #include "lineinfo.h"
53
54
55
56 /*****************************************************************************/
57 /*                                   Data                                    */
58 /*****************************************************************************/
59
60
61
62 /* Collection containing all line infos */
63 Collection LineInfoColl = STATIC_COLLECTION_INITIALIZER;
64 unsigned  LineInfoValid = 0;              /* Valid, that is, used entries */
65
66 /* Static pointer to last line info or NULL if not active */
67 LineInfo* CurLineInfo   = 0;
68
69
70
71 /*****************************************************************************/
72 /*                                   Code                                    */
73 /*****************************************************************************/
74
75
76
77 static LineInfo* NewLineInfo (unsigned FileIndex, unsigned long LineNum)
78 /* Create and return a new line info. Usage will be zero. */
79 {
80     /* Allocate memory */
81     LineInfo* LI = xmalloc (sizeof (LineInfo));
82
83     /* Initialize the fields */
84     LI->Usage    = 0;
85     LI->Index    = 0;           /* Currently invalid */
86     LI->Pos.Line = LineNum;
87     LI->Pos.Col  = 0;
88     LI->Pos.Name = FileIndex;
89
90     /* Insert this structure into the collection */
91     CollAppend (&LineInfoColl, LI);
92
93     /* Return the new struct */
94     return LI;
95 }
96
97
98
99 LineInfo* UseLineInfo (LineInfo* LI)
100 /* Increase the reference count of the given line info and return it. The
101  * function will gracefully accept NULL pointers and do nothing in this case.
102  */
103 {
104     if (LI) {
105         if (LI->Usage++ == 0) {
106             /* One more valid line info */
107             ++LineInfoValid;
108         }
109     }
110     return LI;
111 }
112
113
114
115 void GenLineInfo (unsigned FileIndex, unsigned long LineNum)
116 /* Generate a new line info */
117 {
118     /* Create a new line info and make it current */
119     CurLineInfo = NewLineInfo (FileIndex, LineNum);
120 }
121
122
123
124 void ClearLineInfo (void)
125 /* Clear the current line info */
126 {
127     CurLineInfo = 0;
128 }
129
130
131
132 static int CmpLineInfo (void* Data, const void* LI1_, const void* LI2_)
133 /* Compare function for the sort */
134 {
135     /* Cast the pointers */
136     const LineInfo* LI1 = LI1_;
137     const LineInfo* LI2 = LI2_;
138
139     /* Unreferenced line infos are always larger, otherwise sort by file,
140      * then by line.
141      */
142     if ((LI1->Usage == 0) == (LI2->Usage == 0)) {
143         /* Both are either referenced or unreferenced */
144         if (LI1->Pos.Name< LI2->Pos.Name) {
145             return -1;
146         } else if (LI1->Pos.Name > LI2->Pos.Name) {
147             return 1;
148         } else if (LI1->Pos.Line < LI2->Pos.Line) {
149             return -1;
150         } else if (LI1->Pos.Line > LI2->Pos.Line) {
151             return 1;
152         } else {
153             return 0;
154         }
155     } else {
156         if (LI1->Usage > 0) {
157             return -1;
158         } else {
159             return 1;
160         }
161     }
162 }
163
164
165
166 void MakeLineInfoIndex (void)
167 /* Sort the line infos and drop all unreferenced ones */
168 {
169     /* Sort the collection */
170     CollSort (&LineInfoColl, CmpLineInfo, 0);
171 }
172
173
174
175 void WriteLineInfo (void)
176 /* Write a list of all line infos to the object file. */
177 {
178     /* Tell the object file module that we're about to write line infos */
179     ObjStartLineInfos ();
180
181     /* Check if debug info is requested */
182     if (DbgSyms) {
183
184         unsigned I;
185
186         /* Write the line info count to the list */
187         ObjWriteVar (LineInfoValid);
188
189         /* Walk through list and write all line infos that have references.
190          * Because of the sort, this are exactly the first LineInfoValid
191          * ones.
192          */
193         for (I = 0; I < LineInfoValid; ++I) {
194             /* Get a pointer to this line info */
195             LineInfo* LI = CollAtUnchecked (&LineInfoColl, I);
196             /* Write the source file position */
197             ObjWritePos (&LI->Pos);
198         }
199
200     } else {
201
202         /* No line infos */
203         ObjWriteVar (0);
204
205     }
206 }
207
208
209