]> git.sur5r.net Git - cc65/blob - src/ca65/lineinfo.c
26e12fff5b56a3bd8fd04cc3e520155771ad52f3
[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 /* Linked list of all line infos */
63 LineInfo* LineInfoRoot  = 0;
64 LineInfo* LineInfoLast  = 0;
65 unsigned  LineInfoCount = 0;
66 unsigned  LineInfoValid = 0;              /* Valid, that is, used entries */
67
68 /* Static pointer to last line info or NULL if not active */
69 LineInfo* CurLineInfo   = 0;
70
71
72
73 /*****************************************************************************/
74 /*                                   Code                                    */
75 /*****************************************************************************/
76
77
78
79 static LineInfo* NewLineInfo (unsigned FileIndex, unsigned long LineNum)
80 /* Create and return a new line info. Usage will be zero. */
81 {
82     /* Allocate memory */
83     LineInfo* LI = xmalloc (sizeof (LineInfo));
84
85     /* Initialize the fields */
86     LI->Next     = 0;
87     LI->Usage    = 0;
88     LI->Index    = 0;           /* Currently invalid */
89     LI->Pos.Line = LineNum;
90     LI->Pos.Col  = 0;
91     LI->Pos.Name = FileIndex;
92
93     /* Insert this structure into the line info list */
94     if (LineInfoLast == 0) {
95         LineInfoRoot = LI;
96     } else {
97         LineInfoLast->Next = LI;
98     }
99     LineInfoLast = LI;
100
101     /* Count the line infos */
102     ++LineInfoCount;
103
104     /* Return the new struct */
105     return LI;
106 }
107
108
109
110 LineInfo* UseLineInfo (LineInfo* LI)
111 /* Increase the reference count of the given line info and return it. The
112  * function will gracefully accept NULL pointers and do nothing in this case.
113  */
114 {
115     if (LI) {
116         ++LI->Usage;
117     }
118     return LI;
119 }
120
121
122
123 void GenLineInfo (unsigned FileIndex, unsigned long LineNum)
124 /* Generate a new line info */
125 {
126     /* Create a new line info and make it current */
127     CurLineInfo = NewLineInfo (FileIndex, LineNum);
128 }
129
130
131
132 void ClearLineInfo (void)
133 /* Clear the current line info */
134 {
135     CurLineInfo = 0;
136 }
137
138
139
140 void MakeLineInfoIndex (void)
141 /* Walk over the line info list and make an index of all entries ignoring
142  * those with a usage count of zero.
143  */
144 {
145     LineInfo* LI  = LineInfoRoot;
146     LineInfoValid = 0;
147     while (LI) {
148         if (LI->Usage) {
149             LI->Index = LineInfoValid++;
150         }
151         LI = LI->Next;
152     }
153 }
154
155
156
157 void WriteLineInfo (void)
158 /* Write a list of all line infos to the object file. */
159 {
160     LineInfo* LI;
161
162     /* Tell the object file module that we're about to write line infos */
163     ObjStartLineInfos ();
164
165     /* Check if debug info is requested */
166     if (DbgSyms) {
167
168         /* Write the line info count to the list */
169         ObjWriteVar (LineInfoValid);
170
171         /* Walk through list and write all line infos that have references */
172         LI = LineInfoRoot;
173         while (LI) {
174             if (LI->Usage) {
175                 /* Write the source file position */
176                 ObjWritePos (&LI->Pos);
177             }
178             LI = LI->Next;
179         }
180
181     } else {
182
183         /* No line infos */
184         ObjWriteVar (0);
185
186     }
187 }
188
189
190