]> git.sur5r.net Git - cc65/blob - src/cc65/lineinfo.c
Minor improvement of optimizations
[cc65] / src / cc65 / 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@musoftware.de                                             */
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 #include <string.h>
37
38 /* common */
39 #include "chartype.h"
40 #include "check.h"
41 #include "xmalloc.h"
42
43 /* cc65 */
44 #include "global.h"
45 #include "input.h"
46 #include "lineinfo.h"
47
48
49
50 /*****************************************************************************/
51 /*                                   Data                                    */
52 /*****************************************************************************/
53
54
55
56 /* Global pointer to line information for the current line */
57 static LineInfo* CurLineInfo = 0;
58
59
60
61 /*****************************************************************************/
62 /*                                   Code                                    */
63 /*****************************************************************************/
64
65
66
67 static LineInfo* NewLineInfo (struct IFile* F, unsigned LineNum, const char* Line)
68 /* Create and return a new line info. Ref count will be 1. */
69 {
70     unsigned  Len;
71     LineInfo* LI;
72     char*     T;
73
74     /* Skip leading spaces in Line */
75     while (IsBlank (*Line)) {
76         ++Line;
77     }
78
79     /* Calculate the length of the line */
80     Len = strlen (Line);
81
82     /* Allocate memory */
83     LI = xmalloc (sizeof (LineInfo) + Len);
84
85     /* Initialize the fields */
86     LI->RefCount  = 1;
87     LI->InputFile = F;
88     LI->LineNum   = LineNum;
89
90     /* Copy the line, replacing tabs by spaces in the given line since tabs
91      * will give rather arbitrary results when used in the output later, and
92      * if we do it here, we won't need another copy later.
93      */
94     T = LI->Line;
95     while (Len--) {
96         if (*Line == '\t') {
97             *T = ' ';
98         } else {
99             *T = *Line;
100         }
101         ++Line;
102         ++T;
103     }
104
105     /* Add the terminator */
106     *T = '\0';
107
108     /* Return the new struct */
109     return LI;
110 }
111
112
113
114 static void FreeLineInfo (LineInfo* LI)
115 /* Free a LineInfo structure */
116 {
117     xfree (LI);
118 }
119
120
121
122 LineInfo* UseLineInfo (LineInfo* LI)
123 /* Increase the reference count of the given line info and return it. */
124 {
125     CHECK (LI != 0);
126     ++LI->RefCount;
127     return LI;
128 }
129
130
131
132 void ReleaseLineInfo (LineInfo* LI)
133 /* Release a reference to the given line info, free the structure if the
134  * reference count drops to zero.
135  */
136 {
137     CHECK (LI && LI->RefCount > 0);
138     if (--LI->RefCount == 0) {
139         /* No more references, free it */
140         FreeLineInfo (LI);
141     }
142 }
143
144
145
146 LineInfo* GetCurLineInfo (void)
147 /* Return a pointer to the current line info. The reference count is NOT
148  * increased, use UseLineInfo for that purpose.
149  */
150 {
151     return CurLineInfo;
152 }
153
154
155
156 void UpdateLineInfo (struct IFile* F, unsigned LineNum, const char* Line)
157 /* Update the line info - called if a new line is read */
158 {
159     /* If a current line info exists, release it */
160     if (CurLineInfo) {
161         ReleaseLineInfo (CurLineInfo);
162     }
163
164     /* If we have intermixed assembly switched off, use an empty line instead
165      * of the supplied one to save some memory.
166      */
167     if (!AddSource) {
168         Line = "";
169     }
170
171     /* Create a new line info */
172     CurLineInfo = NewLineInfo (F, LineNum, Line);
173 }
174
175
176
177 const char* GetInputName (const LineInfo* LI)
178 /* Return the file name from a line info */
179 {
180     PRECONDITION (LI != 0);
181     return LI->InputFile->Name;
182 }
183
184
185
186 unsigned GetInputLine (const LineInfo* LI)
187 /* Return the line number from a line info */
188 {
189     PRECONDITION (LI != 0);
190     return LI->LineNum;
191 }
192
193
194