1 /* asdbg.c */
2 
3 /*
4  *  Copyright (C) 2003-2009  Alan R. Baldwin
5  *
6  *  This program is free software: you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation, either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  *
20  * Alan R. Baldwin
21  * 721 Berkeley St.
22  * Kent, Ohio  44240
23  *
24  *
25  * Code extracted from asnoice.c
26  * written by:
27  *      John L. Hartman (JLH)
28  *      3-Nov-1997
29  */
30 
31 #include <ctype.h>
32 #include "dbuf_string.h"
33 #include "asxxxx.h"
34 
35 /*)Module       asdbg.c
36  *
37  *      The module asdbg.c contains the functions that
38  *
39  *      1) generate debug symbols for assembler code
40  *         similiar to those generated by the SDCC compiler
41  *
42  *      2) generate debug symbols for the NoICE
43  *         remote debugger
44  *
45  *      asdbg.c contains the following functions:
46  *              VOID    DefineSDCC_Line()
47  *              VOID    DefineNoICE_Line()
48  *              char *  BaseFileName()
49  *
50  *      asdbg.c contains the static variables:
51  *              int     prevFile
52  *              char    baseName[FILSPC]
53  *
54  *          used by the BaseFileName function.
55  *
56  * NOTE:
57  *      These functions know nothing about 'include' files
58  *      and do not track their inclusion.
59  */
60 
61 /*)Function     VOID    DefineSDCC_Line()
62  *
63  *      The function DefineSDCC_Line() is called to create
64  *      a symbol of the form A$FILE$nnn where FILE is the
65  *      Base File Name, the file name without a path or
66  *      an extension, and nnn is the line number.
67  *
68  *      local variables:
69  *              struct dbuf_s   dbuf    a temporary to build the symbol
70  *              struct sym *    pSym    pointer to the created symbol structure
71  *
72  *      global variables:
73  *              asmf *  asmc            pointer to current assembler file structure
74  *              int     srcline         array of source file line numbers
75  *              a_uint  laddr           current assembler address
76  *              area    dot.s_area      pointer to the current area
77  *
78  *      functions called:
79  *              char *  BaseFileName()  asdbg.c
80  *              sym *   lookup()        assym.c
81  *              int     sprintf()       c_library
82  *
83  *      side effects:
84  *              A new symbol of the form A$FILE$nnn is created.
85  */
86 
87 #if SDCDB
88 VOID
DefineSDCC_Line(void)89 DefineSDCC_Line (void)
90 {
91         struct dbuf_s dbuf;
92         struct sym *pSym;
93 
94         /*
95          * Symbol is A$FILE$nnn
96          */
97         dbuf_init (&dbuf, NCPS);
98         dbuf_printf (&dbuf, "A$%s$%u", BaseFileName (asmc, 1), srcline);
99 
100         pSym = lookup (dbuf_c_str (&dbuf));
101         dbuf_destroy (&dbuf);
102 
103         pSym->s_type = S_USER;
104         pSym->s_area = dot.s_area;
105         pSym->s_addr = laddr;
106         pSym->s_flag |= S_GBL;
107 }
108 #endif
109 
110 
111 /*)Function     VOID    DefineNoICE_Line()
112  *
113  *      The function DefineNoICE_Line() is called to create
114  *      a symbol of the form FILE.nnn where FILE is the
115  *      Base File Name, the file name without a path or
116  *      an extension, and nnn is the line number.
117  *
118  *      local variables:
119  *              struct dbuf_s   dbuf    a temporary to build the symbol
120  *              struct sym *    pSym    pointer to the created symbol structure
121  *
122  *      global variables:
123  *              asmf *  asmc            pointer to current assembler file structure
124  *              int     srcline         array of source file line numbers
125  *              a_uint  laddr           current assembler address
126  *              area    dot.s_area      pointer to the current area
127  *
128  *      functions called:
129  *              char *  BaseFileName()  asdbg.c
130  *              sym *   lookup()        assym.c
131  *              int     sprintf()       c_library
132  *
133  *      side effects:
134  *              A new symbol of the form FILE.nnn is created.
135  */
136 
137 #if NOICE
138 VOID
DefineNoICE_Line(void)139 DefineNoICE_Line (void)
140 {
141         struct dbuf_s dbuf;
142         struct sym *pSym;
143 
144         /*
145          * Symbol is FILE.nnn
146          */
147         dbuf_init (&dbuf, NCPS);
148         dbuf_printf (&dbuf, "%s.%u", BaseFileName (asmc, 0), srcline);
149 
150         pSym = lookup (dbuf_c_str (&dbuf));
151         dbuf_destroy (&dbuf);
152 
153         pSym->s_type = S_USER;
154         pSym->s_area = dot.s_area;
155         pSym->s_addr = laddr;
156         pSym->s_flag |= S_GBL;
157 }
158 #endif
159 
160 
161 /*)Function     char *  BaseFileName(currFile, spacesToUnderscores)
162  *
163  *      The function BaseFileName() is called to extract
164  *      the file name from a string containing a path,
165  *      filename, and extension. If spacesToUnderscores != 0
166  *      then spaces are converted to underscores
167  *
168  *              currFile                is a pointer to the
169  *                                      current assembler object
170  *              spacesToUnderscores
171  *
172  *      local variables:
173  *              char    baseName[]      a place to put the file name
174  *              int     prevFile        previous assembler object
175  *              char *  p1              temporary string pointer
176  *              char *  p2              temporary string pointer
177  *
178  *      global variables:
179  *              FILE *  ofp             output file handle
180  *
181  *      functions called:
182  *              int     fprintf()       c_library
183  *              char *  strcpy()        c_library
184  *              char *  strrchr()       c_library
185  *              char *  isspace()       c_library
186  *
187  *      side effects:
188  *              A FILE command of the form ';!FILE string'
189  *              is written to the output file.
190  */
191 
192 #if (NOICE || SDCDB)
193 static  struct  asmf *  prevFile = NULL;
194 static  char    baseName[FILSPC];
195 
196 char *
BaseFileName(struct asmf * currFile,int spacesToUnderscores)197 BaseFileName (struct asmf * currFile, int spacesToUnderscores)
198 {
199         char *p1, *p2;
200 
201         if (currFile != prevFile) {
202                 prevFile = currFile;
203 
204                 strcpy(baseName, afn);
205                 p1 = baseName;
206 
207                 /*
208                  * Dump a FILE command with full path and extension
209                  */
210                 if (ofp)
211                   fprintf(ofp, ";!FILE %s\n", p1);
212 
213                 /*
214                  * The name starts after the last
215                  * '/' (Unices) or
216                  * ':' or '\' (DOS)
217                  *
218                  * and ends at the last
219                  * separator 'FSEPX'
220                  */
221                 if ((p2 = strrchr(p1,  '\\')) != NULL)  p1 = ++p2;
222                 if ((p2 = strrchr(p1,   '/')) != NULL)  p1 = ++p2;
223                 if ((p2 = strrchr(p1,   ':')) != NULL)  p1 = ++p2;
224                 if ((p2 = strrchr(p1, FSEPX)) != NULL) *p2 = 0;
225                 memmove(baseName, p1, strlen(p1)); /* Do not use strcpy(), since baseName and p1 may overlap */
226 
227                 if (spacesToUnderscores) {
228                         /* Convert spaces to underscores */
229                         for (p1 = baseName; *p1; ++p1)
230                                 if (isspace (*p1))
231                                         *p1 = '_';
232                 }
233         }
234         return(baseName);
235 }
236 #endif
237 
238