Use 'ao-dbg' instead of 's51' to communicate with TeleMetrum
[fw/sdcc] / src / cdbFile.c
1
2
3 #include "common.h"
4
5
6 /*************************************************************
7  *
8  *
9  *
10  *
11  *************************************************************/
12
13 int cdbOpenFile(char *file);
14 int cdbCloseFile(void);
15 int cdbWriteFunction(symbol *pSym, iCode *ic);
16 int cdbWriteEndFunction(symbol *pSym, iCode *ic, int offset);
17 int cdbWriteLabel(symbol *pSym, iCode *ic);
18 int cdbWriteScope(iCode *ic);
19 int cdbWriteSymbol(symbol *pSym);
20 int cdbWriteType(structdef *sdef, int block, int inStruct, char *tag);
21 int cdbWriteModule(char *name);
22 int cdbWriteCLine(iCode *ic);
23 int cdbWriteALine(char *module, int Line);
24 int cdbWriteFrameAddress(char *variable, struct regs *reg, int offset);
25 int cdbWriteBasicSymbol(symbol *sym, int isStructSym, int isFunc);
26 void cdbTypeInfo (sym_link * type);
27      
28
29 DEBUGFILE cdbDebugFile = 
30   {
31     &cdbOpenFile,
32     &cdbCloseFile,
33     &cdbWriteModule,
34     &cdbWriteFunction,
35     &cdbWriteEndFunction,
36     &cdbWriteLabel,
37     &cdbWriteScope,
38     &cdbWriteSymbol,
39     &cdbWriteType,
40     &cdbWriteCLine,
41     &cdbWriteALine,
42     &cdbWriteFrameAddress
43   };
44
45 FILE *cdbFilePtr = NULL;
46 char *cdbModuleName = NULL;
47
48 /******************************************************************
49  * spacesToUnderscores - replace all non alpha-numerics with
50  * underscores
51  *
52  *
53  *****************************************************************/
54
55 static char *
56 spacesToUnderscores (char *dest, const char *src, size_t len)
57 {
58   unsigned int i;
59   char *p;
60
61   assert(dest != NULL);
62   assert(src != NULL);
63   assert(len > 0);
64
65   --len;
66   for (p = dest, i = 0; *src != '\0' && i < len; ++src, ++i) {
67     *p++ = (isspace((unsigned char)*src) || (*src == '-')) ? '_' : *src;
68   }
69   *p = '\0';
70
71   return dest;
72 }
73
74
75 /******************************************************************
76  *
77  *
78  *
79  *
80  *****************************************************************/
81
82 int cdbOpenFile(char *file)
83 {
84   if (getenv("SDCC_DEBUG_FUNCTION_POINTERS"))
85     fprintf (stderr, "cdbFile.c:cdbOpenFile(%s)\n", file);
86
87   return (cdbFilePtr = fopen(file, "w")) ? 1 : 0; 
88 }
89
90 /******************************************************************
91  *
92  *
93  *
94  *
95  *****************************************************************/
96 int cdbCloseFile(void)
97 {
98   if (getenv("SDCC_DEBUG_FUNCTION_POINTERS"))
99     fprintf (stderr, "cdbFile.c:cdbCloseFile()\n");
100
101   if(!cdbFilePtr) return 0;
102  
103   fclose(cdbFilePtr);
104   cdbFilePtr = NULL;
105   cdbModuleName = NULL;
106
107   return 1;
108 }
109
110 /******************************************************************
111  *
112  *
113  *
114  *
115  *****************************************************************/
116
117 int cdbWriteFunction(symbol *pSym, iCode *ic)
118 {
119   char debugSym[INITIAL_INLINEASM];
120   
121   if (getenv("SDCC_DEBUG_FUNCTION_POINTERS"))
122     fprintf (stderr, "cdbFile.c:cdbWriteFunction()\n");
123
124
125   if(!cdbFilePtr) return 0;
126
127   if (IS_STATIC (pSym->etype))
128     sprintf (debugSym, "F%s$%s$0$0", moduleName, pSym->name);
129   else
130     sprintf (debugSym, "G$%s$0$0", pSym->name);
131   emitDebuggerSymbol (debugSym);
132     
133   return cdbWriteBasicSymbol(pSym, FALSE, TRUE);
134 }
135
136 /******************************************************************
137  *
138  *
139  *
140  *
141  *****************************************************************/
142
143 int cdbWriteEndFunction(symbol *pSym, iCode *ic, int offset)
144 {
145   char debugSym[INITIAL_INLINEASM];
146   
147   if (getenv("SDCC_DEBUG_FUNCTION_POINTERS"))
148     fprintf (stderr, "cdbFile.c:cdbWriteEndFunction()\n");
149
150   if(!cdbFilePtr) return 0;
151           
152   if (ic)
153     {
154       sprintf (debugSym, "C$%s$%d$%d$%d",
155                FileBaseName (ic->filename), pSym->lastLine,
156                ic->level, ic->block);
157       spacesToUnderscores (debugSym, debugSym, sizeof (debugSym));
158       emitDebuggerSymbol (debugSym);
159     }
160
161   if (IS_STATIC (pSym->etype))
162     sprintf (debugSym, "XF%s$%s$0$0", moduleName, pSym->name);
163   else
164     sprintf (debugSym, "XG$%s$0$0", pSym->name);
165   emitDebuggerSymbol (debugSym);
166     
167   return 1;
168 }
169
170 /******************************************************************
171  *
172  *
173  *
174  *
175  *****************************************************************/
176
177 int cdbWriteLabel(symbol *pSym, iCode *ic)
178 {
179   if (getenv("SDCC_DEBUG_FUNCTION_POINTERS"))
180     fprintf (stderr, "cdbFile.c:cdbWriteLabel()\n");
181
182   if(!cdbFilePtr) return 0;
183           
184   return 1;
185 }
186
187 /******************************************************************
188  *
189  *
190  *
191  *
192  *****************************************************************/
193
194 int cdbWriteScope(iCode *ic)
195 {
196   if (getenv("SDCC_DEBUG_FUNCTION_POINTERS"))
197     fprintf (stderr, "cdbFile.c:cdbWriteScope()\n");
198
199   if(!cdbFilePtr) return 0;
200           
201   return 1;
202 }
203
204 /******************************************************************
205  *
206  *
207  *
208  *
209  *****************************************************************/
210
211 int cdbWriteSymbol(symbol *pSym)
212 {
213   if (getenv("SDCC_DEBUG_FUNCTION_POINTERS"))
214     fprintf (stderr, "cdbFile.c:cdbWriteSymbol()\n");
215
216   if(!cdbFilePtr) return 0;
217
218   return cdbWriteBasicSymbol(pSym, FALSE, FALSE);
219 }
220
221 /******************************************************************
222  *
223  *
224  *
225  *
226  *****************************************************************/
227
228 int cdbWriteType(structdef *sdef, int block, int inStruct, char *tag)
229 {
230   symbol *sym;
231
232   if (getenv("SDCC_DEBUG_FUNCTION_POINTERS"))
233     fprintf (stderr, "cdbFile.c:cdbWriteType()\n");
234
235   if(!cdbFilePtr) return 0;
236
237   fprintf (cdbFilePtr, "T:");
238
239   /* if block # then must have function scope */
240   fprintf (cdbFilePtr, "F%s$", moduleName);
241
242   fprintf (cdbFilePtr, "%s[", (tag ? tag : sdef->tag));
243
244   for (sym = sdef->fields; sym; sym = sym->next)
245     {
246       fprintf (cdbFilePtr, "({%d}", sym->offset);
247       cdbWriteBasicSymbol(sym, TRUE, FALSE);
248       fprintf(cdbFilePtr, ")");
249     }
250
251   fprintf (cdbFilePtr, "]");
252
253   if (!inStruct)
254     fprintf (cdbFilePtr, "\n");
255
256   return 1;
257 }
258
259 /******************************************************************
260  *
261  *
262  *
263  *
264  *****************************************************************/
265
266 int cdbWriteModule(char *name)
267 {
268   if (getenv("SDCC_DEBUG_FUNCTION_POINTERS"))
269     fprintf (stderr, "cdbFile.c:cdbWriteModule()\n");
270
271   if(!cdbFilePtr) return 0;
272   cdbModuleName = name;
273
274   fprintf(cdbFilePtr, "M:%s\n", cdbModuleName);
275
276   return 1;
277 }
278
279 /******************************************************************
280  *
281  *
282  *
283  *
284  *****************************************************************/
285 int cdbWriteCLine(iCode *ic)
286 {
287   char debugSym[INITIAL_INLINEASM];
288   
289   if(!cdbFilePtr) return 0;
290               
291   sprintf (debugSym, "C$%s$%d$%d$%d", 
292            FileBaseName (ic->filename), ic->lineno,
293            ic->level, ic->block);
294   spacesToUnderscores (debugSym, debugSym, sizeof (debugSym));
295   emitDebuggerSymbol (debugSym);
296
297   return 1;
298 }
299
300 /******************************************************************
301  *
302  *
303  *
304  *
305  *****************************************************************/
306
307 int cdbWriteALine(char *module, int Line)
308 {
309   if(!cdbFilePtr) return 0;
310
311   return 1;
312 }
313
314 /******************************************************************
315  *
316  *
317  *
318  *
319  *****************************************************************/
320
321 int cdbWriteFrameAddress(char *variable, struct regs *reg, int offset)
322 {
323   if (getenv("SDCC_DEBUG_FUNCTION_POINTERS"))
324     fprintf (stderr, "cdbFile.c:cdbWriteFrameAddress()\n");
325
326   if(!cdbFilePtr) return 0;
327           
328   return 1;
329 }
330
331 /******************************************************************
332  *
333  *
334  *
335  *
336  *****************************************************************/
337
338 int cdbWriteBasicSymbol(symbol *sym, int isStructSym, int isFunc)
339 {
340   memmap *map;
341
342   if (getenv("SDCC_DEBUG_FUNCTION_POINTERS"))
343     fprintf (stderr, "cdbFile.c:cdbWriteBasicSymbol()\n");
344
345   if(!cdbFilePtr) return 0;
346
347   if (!sym) return 0;
348
349   /* WRITE HEADER, Function or Symbol */
350   if (isFunc)
351     fprintf (cdbFilePtr, "F:");   
352   else
353     fprintf (cdbFilePtr, "S:");
354
355   /* STRUCTS do not have scope info.. */
356
357   if (!isStructSym)
358     {
359       if (!sym->level)
360         {
361           /* global */
362           if (IS_STATIC (sym->etype))
363             fprintf (cdbFilePtr, "F%s$", moduleName);   /* scope is file */
364           else
365             fprintf (cdbFilePtr, "G$"); /* scope is global */
366         }
367       else
368         /* symbol is local */
369         fprintf (cdbFilePtr, "L%s$", (sym->localof ? sym->localof->name : "-null-"));
370     }
371   else
372     fprintf (cdbFilePtr, "S$");         /* scope is structure */
373
374   /* print the name, & mangled name */
375   fprintf (cdbFilePtr, "%s$%d$%d(", sym->name,
376            sym->level, sym->block);
377
378   cdbTypeInfo (sym->type);
379
380   fprintf (cdbFilePtr, "),");
381
382   /* CHECK FOR REGISTER SYMBOL... */ 
383   if (!sym->allocreq && sym->reqv)
384   {
385     int a;
386     symbol *TempSym = OP_SYMBOL (sym->reqv);
387
388     fprintf(cdbFilePtr, "R,0,0,[");
389
390     for(a = 0; a < 4; a++)
391       if(TempSym->regs[a])     
392         fprintf(cdbFilePtr, "%s%s", port->getRegName(TempSym->regs[a]),
393                 ((a < 3) && (TempSym->regs[a+1])) ? "," : "");
394
395     fprintf(cdbFilePtr, "]");
396   }
397   else
398   {
399     /* print the address space */
400     map = SPEC_OCLS (sym->etype);
401
402     fprintf (cdbFilePtr, "%c,%d,%d",
403              (map ? map->dbName : 'Z'), sym->onStack, SPEC_STAK (sym->etype));
404   }
405
406   /* if assigned to registers then output register names */
407   /* if this is a function then print
408      if is it an interrupt routine & interrupt number
409      and the register bank it is using */
410   if (isFunc)
411     fprintf (cdbFilePtr, ",%d,%d,%d", FUNC_ISISR (sym->type),
412              FUNC_INTNO (sym->type), FUNC_REGBANK (sym->type));
413   
414
415 /* alternate location to find this symbol @ : eg registers
416      or spillication */
417
418   if (!isStructSym)
419     fprintf (cdbFilePtr, "\n");
420
421   return 1;
422 }
423
424 /******************************************************************
425  *
426  *
427  *
428  *
429  *****************************************************************/
430
431 /*-----------------------------------------------------------------*/
432 /* cdbTypeInfo - print the type information for debugger           */
433 /*-----------------------------------------------------------------*/
434 void cdbTypeInfo (sym_link * type)
435 {
436   fprintf (cdbFilePtr, "{%d}", getSize (type));
437
438   while (type)
439     {
440       if (IS_DECL (type))
441         {
442           switch (DCL_TYPE (type))
443             {
444             case FUNCTION: fprintf (cdbFilePtr, "DF,"); break;
445             case GPOINTER: fprintf (cdbFilePtr, "DG,"); break;
446             case CPOINTER: fprintf (cdbFilePtr, "DC,"); break;
447             case FPOINTER: fprintf (cdbFilePtr, "DX,"); break;
448             case POINTER:  fprintf (cdbFilePtr, "DD,"); break;
449             case IPOINTER: fprintf (cdbFilePtr, "DI,"); break;
450             case PPOINTER: fprintf (cdbFilePtr, "DP,"); break;
451             case EEPPOINTER: fprintf (cdbFilePtr, "DA,"); break;
452             case ARRAY: fprintf (cdbFilePtr, "DA%d,", DCL_ELEM (type)); break;
453             default:
454               break;
455             }
456         }
457       else
458         {
459           switch (SPEC_NOUN (type))
460             {
461             case V_INT:
462               if (IS_LONG (type))
463                 fprintf (cdbFilePtr, "SL");
464               else
465                 fprintf (cdbFilePtr, "SI");
466               break;
467
468             case V_CHAR: fprintf (cdbFilePtr, "SC"); break;
469             case V_VOID: fprintf (cdbFilePtr, "SV"); break;
470             case V_FLOAT: fprintf (cdbFilePtr, "SF"); break;
471             case V_FIXED16X16: fprintf(cdbFilePtr, "SQ"); break;
472             case V_STRUCT: 
473               fprintf (cdbFilePtr, "ST%s", SPEC_STRUCT (type)->tag); 
474               break;
475             
476             case V_SBIT: fprintf (cdbFilePtr, "SX"); break;
477             case V_BIT: 
478             case V_BITFIELD: 
479               fprintf (cdbFilePtr, "SB%d$%d", SPEC_BSTR (type), 
480                        SPEC_BLEN (type));
481               break;
482
483             default:
484               break;
485             }
486           fputs (":", cdbFilePtr);
487           if (SPEC_USIGN (type))
488             fputs ("U", cdbFilePtr);
489           else
490             fputs ("S", cdbFilePtr);
491         }
492       type = type->next;
493     }
494 }
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513