1 /*****************************************************************************/
5 /* Source file line info structure */
9 /* (C) 2001 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
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 /*****************************************************************************/
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).
56 /*****************************************************************************/
58 /*****************************************************************************/
62 /* Collection containing all line infos */
63 Collection LineInfoColl = STATIC_COLLECTION_INITIALIZER;
64 unsigned LineInfoValid = 0; /* Valid, that is, used entries */
66 /* Static pointer to last line info or NULL if not active */
67 LineInfo* CurLineInfo = 0;
71 /*****************************************************************************/
73 /*****************************************************************************/
77 static LineInfo* NewLineInfo (unsigned FileIndex, unsigned long LineNum)
78 /* Create and return a new line info. Usage will be zero. */
81 LineInfo* LI = xmalloc (sizeof (LineInfo));
83 /* Initialize the fields */
85 LI->Index = 0; /* Currently invalid */
86 LI->Pos.Line = LineNum;
88 LI->Pos.Name = FileIndex;
90 /* Insert this structure into the collection */
91 CollAppend (&LineInfoColl, LI);
93 /* Return the new struct */
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.
105 if (LI->Usage++ == 0) {
106 /* One more valid line info */
115 void GenLineInfo (unsigned FileIndex, unsigned long LineNum)
116 /* Generate a new line info */
118 /* Create a new line info and make it current */
119 CurLineInfo = NewLineInfo (FileIndex, LineNum);
124 void ClearLineInfo (void)
125 /* Clear the current line info */
132 static int CmpLineInfo (void* Data attribute ((unused)),
133 const void* LI1_, const void* LI2_)
134 /* Compare function for the sort */
136 /* Cast the pointers */
137 const LineInfo* LI1 = LI1_;
138 const LineInfo* LI2 = LI2_;
140 /* Unreferenced line infos are always larger, otherwise sort by file,
143 if ((LI1->Usage == 0) == (LI2->Usage == 0)) {
144 /* Both are either referenced or unreferenced */
145 if (LI1->Pos.Name< LI2->Pos.Name) {
147 } else if (LI1->Pos.Name > LI2->Pos.Name) {
149 } else if (LI1->Pos.Line < LI2->Pos.Line) {
151 } else if (LI1->Pos.Line > LI2->Pos.Line) {
157 if (LI1->Usage > 0) {
167 void MakeLineInfoIndex (void)
168 /* Sort the line infos and drop all unreferenced ones */
172 /* Sort the collection */
173 CollSort (&LineInfoColl, CmpLineInfo, 0);
175 /* Walk over the list and index the line infos. */
176 for (I = 0; I < LineInfoValid; ++I) {
177 /* Get a pointer to this line info */
178 LineInfo* LI = CollAtUnchecked (&LineInfoColl, I);
185 void WriteLineInfo (void)
186 /* Write a list of all line infos to the object file. */
188 /* Tell the object file module that we're about to write line infos */
189 ObjStartLineInfos ();
191 /* Check if debug info is requested */
196 /* Write the line info count to the list */
197 ObjWriteVar (LineInfoValid);
199 /* Walk through list and write all line infos that have references.
200 * Because of the sort, this are exactly the first LineInfoValid
203 for (I = 0; I < LineInfoValid; ++I) {
204 /* Get a pointer to this line info */
205 LineInfo* LI = CollAtUnchecked (&LineInfoColl, I);
206 /* Write the source file position */
207 ObjWritePos (&LI->Pos);
217 /* End of line infos */