]> git.sur5r.net Git - cc65/blob - src/ca65/dbginfo.c
Started to add debug infos for C functions and symbols.
[cc65] / src / ca65 / dbginfo.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 dbginfo.c                                 */
4 /*                                                                           */
5 /*                         Handle the .dbg commands                          */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2000-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 #include <string.h>
37
38 /* common */
39 #include "strbuf.h"
40
41 /* ca65 */
42 #include "error.h"
43 #include "expr.h"
44 #include "filetab.h"
45 #include "lineinfo.h"
46 #include "nexttok.h"
47 #include "dbginfo.h"
48
49
50
51 /*****************************************************************************/
52 /*                                   Data                                    */
53 /*****************************************************************************/
54
55
56
57 /* The current line info */
58 static LineInfo* CurLineInfo = 0;
59
60
61
62 /*****************************************************************************/
63 /*                                   Code                                    */
64 /*****************************************************************************/
65
66
67
68 void DbgInfoFile (void)
69 /* Parse and handle FILE subcommand of the .dbg pseudo instruction */
70 {
71     StrBuf Name = STATIC_STRBUF_INITIALIZER;
72     unsigned long Size;
73     unsigned long MTime;
74
75     /* Parameters are separated by a comma */
76     ConsumeComma ();
77
78     /* Name */
79     if (CurTok.Tok != TOK_STRCON) {
80         ErrorSkip ("String constant expected");
81         return;
82     }
83     SB_Copy (&Name, &CurTok.SVal);
84     NextTok ();
85
86     /* Comma expected */
87     ConsumeComma ();
88
89     /* Size */
90     Size = ConstExpression ();
91
92     /* Comma expected */
93     ConsumeComma ();
94
95     /* MTime */
96     MTime = ConstExpression ();
97
98     /* Insert the file into the table */
99     AddFile (&Name, FT_DBGINFO, Size, MTime);
100
101     /* Free memory used for Name */
102     SB_Done (&Name);
103 }
104
105
106
107 void DbgInfoFunc (void)
108 /* Parse and handle func subcommand of the .dbg pseudo instruction */
109 {
110     static const char* StorageKeys[] = {
111         "EXTERN",
112         "STATIC",
113     };
114
115     StrBuf        Name = STATIC_STRBUF_INITIALIZER;
116     StrBuf        AsmName = STATIC_STRBUF_INITIALIZER;
117     int           StorageClass;
118
119
120     /* Parameters are separated by a comma */
121     ConsumeComma ();
122
123     /* Name */
124     if (CurTok.Tok != TOK_STRCON) {
125         ErrorSkip ("String constant expected");
126         return;
127     }
128     SB_Copy (&Name, &CurTok.SVal);
129     NextTok ();
130
131     /* Comma expected */
132     ConsumeComma ();
133
134     /* The storage class follows */
135     if (CurTok.Tok != TOK_IDENT) {
136         ErrorSkip ("Storage class specifier expected");
137         return;
138     }
139     StorageClass = GetSubKey (StorageKeys, sizeof (StorageKeys)/sizeof (StorageKeys[0]));
140     if (StorageClass < 0) {
141         ErrorSkip ("Storage class specifier expected");
142         return;
143     }
144     NextTok ();
145
146     /* Comma expected */
147     ConsumeComma ();
148
149     /* Assembler name follows */
150     if (CurTok.Tok != TOK_STRCON) {
151         ErrorSkip ("String constant expected");
152         return;
153     }
154     SB_Copy (&AsmName, &CurTok.SVal);
155     NextTok ();
156
157     /* Free memory used for the strings */
158     SB_Done (&AsmName);
159     SB_Done (&Name);
160 }
161
162
163
164 void DbgInfoLine (void)
165 /* Parse and handle LINE subcommand of the .dbg pseudo instruction */
166 {
167     long Line;
168     FilePos Pos = STATIC_FILEPOS_INITIALIZER;
169
170     /* Any new line info terminates the last one */
171     if (CurLineInfo) {
172         EndLine (CurLineInfo);
173         CurLineInfo = 0;
174     }
175
176     /* If a parameters follow, this is actual line info. If no parameters
177      * follow, the last line info is terminated.
178      */
179     if (CurTok.Tok == TOK_SEP) {
180         return;
181     }
182
183     /* Parameters are separated by a comma */
184     ConsumeComma ();
185
186     /* The name of the file follows */
187     if (CurTok.Tok != TOK_STRCON) {
188         ErrorSkip ("String constant expected");
189         return;
190     }
191
192     /* Get the index in the file table for the name */
193     Pos.Name = GetFileIndex (&CurTok.SVal);
194
195     /* Skip the name */
196     NextTok ();
197
198     /* Comma expected */
199     ConsumeComma ();
200
201     /* Line number */
202     Line = ConstExpression ();
203     if (Line < 0) {
204         ErrorSkip ("Line number is out of valid range");
205         return;
206     }
207     Pos.Line = Line;
208
209     /* Generate a new external line info */
210     CurLineInfo = StartLine (&Pos, LI_TYPE_EXT, 0);
211 }
212
213
214
215 void DbgInfoSym (void)
216 /* Parse and handle SYM subcommand of the .dbg pseudo instruction */
217 {
218     static const char* StorageKeys[] = {
219         "AUTO",
220         "EXTERN",
221         "REGISTER",
222         "STATIC",
223     };
224
225     StrBuf        Name = STATIC_STRBUF_INITIALIZER;
226     StrBuf        AsmName = STATIC_STRBUF_INITIALIZER;
227     int           StorageClass;
228     int           Offs;
229
230
231     /* Parameters are separated by a comma */
232     ConsumeComma ();
233
234     /* Name */
235     if (CurTok.Tok != TOK_STRCON) {
236         ErrorSkip ("String constant expected");
237         return;
238     }
239     SB_Copy (&Name, &CurTok.SVal);
240     NextTok ();
241
242     /* Comma expected */
243     ConsumeComma ();
244
245     /* The storage class follows */
246     if (CurTok.Tok != TOK_IDENT) {
247         ErrorSkip ("Storage class specifier expected");
248         return;
249     }
250     StorageClass = GetSubKey (StorageKeys, sizeof (StorageKeys)/sizeof (StorageKeys[0]));
251     if (StorageClass < 0) {
252         ErrorSkip ("Storage class specifier expected");
253         return;
254     }
255
256     /* Skip the storage class token and the following comma */
257     NextTok ();
258     ConsumeComma ();
259
260     /* The next tokens depend on the storage class */
261     if (StorageClass == 0) {
262         /* Auto: Stack offset follows */
263         Offs = ConstExpression ();
264     } else if (StorageClass == 2) {
265         /* Register: Register bank offset follows */
266         Offs = ConstExpression ();
267     } else {
268         /* Extern or static: Assembler name follows */
269         if (CurTok.Tok != TOK_STRCON) {
270             ErrorSkip ("String constant expected");
271             return;
272         }
273         SB_Copy (&AsmName, &CurTok.SVal);
274         NextTok ();
275     }
276
277     /* Free memory used for the strings */
278     SB_Done (&AsmName);
279     SB_Done (&Name);
280 }
281
282
283