]> Shamusworld >> Repos - rmac/blob - debug.c
Fixed problem with nested MACROs.
[rmac] / debug.c
1 //
2 // RMAC - Reboot's Macro Assembler for the Atari Jaguar Console System
3 // DEBUG.C - Debugging Messages
4 // Copyright (C) 199x Landon Dyer, 2011-2012 Reboot and Friends
5 // RMAC derived from MADMAC v1.07 Written by Landon Dyer, 1986
6 // Source Utilised with the Kind Permission of Landon Dyer
7 //
8
9 #include "debug.h"
10 #include "amode.h"
11 #include "direct.h"
12 #include "mark.h"
13 #include "sect.h"
14 #include "token.h"
15
16
17 static int siztab[4] = { 3, 5, 9, 9 };
18
19
20 //
21 // Print 'c' Visibly
22 //
23 int visprt(char c)
24 {
25         if (c < 0x20 || c >= 0x7F)
26                 putchar('.');
27         else
28                 putchar(c);
29
30         return 0;
31 }
32
33
34 //
35 // Print expression, return ptr to just past the ENDEXPR
36 //
37 TOKEN * printexpr(TOKEN * tp)
38 {
39         if (tp != NULL)
40         {
41                 while (*tp != ENDEXPR)
42                 {
43                         switch ((int)*tp++)
44                         {
45                         case SYMBOL:
46 //                              printf("`%s' ", ((SYM *)*tp)->sname);
47                                 printf("`%s' ", symbolPtr[*tp]->sname);
48                                 tp++;
49                                 break;
50                         case CONST:
51                                 printf("$%X ", *tp++);
52                                 break;
53                         case ACONST:
54                                 printf("ACONST=($%X,$%X) ", *tp, tp[1]);
55                                 tp += 2;
56                                 break;
57                         default:
58                                 printf("%c ", (char)tp[-1]);
59                                 break;
60                         }
61                 }
62         }
63
64 //      printf(";\n");
65         return tp + 1;
66 }
67
68
69 //
70 // Dump data in a chunk (and maybe others) in the appropriate format
71 //
72 int chdump(CHUNK * ch, int format)
73 {
74         while (ch != NULL)
75         {
76                 printf("chloc=$%08X, chsize=$%X\n", ch->chloc, ch->ch_size);
77                 mdump(ch->chptr, ch->ch_size, format, ch->chloc);
78                 ch = ch->chnext;
79         }
80
81         return 0;
82 }
83
84
85 //
86 // Dump fixup records in printable format
87 //
88 int fudump(CHUNK * ch)
89 {
90         PTR p;
91         char * ep;
92         WORD attr, esiz;
93         WORD line, file;
94         LONG loc;
95
96         for(; ch!=NULL;)
97         {
98                 p.cp = ch->chptr;
99                 ep = ch->chptr + ch->ch_size;
100
101                 while (p.cp < ep)
102                 {
103                         attr = *p.wp++;
104                         loc = *p.lp++;
105                         file = *p.wp++;
106                         line = *p.wp++;
107
108                         printf("$%04X $%08X %d.%d: ", (int)attr, loc, (int)file, (int)line);
109
110                         if (attr & FU_EXPR)
111                         {
112                                 esiz = *p.wp++;
113                                 printf("(%d long) ", (int)esiz);
114                                 p.tk = printexpr(p.tk);
115                         }
116                         else
117                         {
118 //                              printf("`%s' ;\n", (*p.sy)->sname);
119                                 printf("`%s' ;", (*p.sy)->sname);
120                                 p.sy++;
121                         }
122
123                         if ((attr & 0x0F00) == FU_JR)
124                         {
125                                 printf(" *=$%X", *p.lp);
126                                 p.lp++;
127                         }
128
129                         printf("\n");
130                 }
131
132                 ch = ch->chnext;
133         }
134
135         return 0;
136 }
137
138
139 //
140 // Dump marks
141 //
142 int mudump(void)
143 {
144         MCHUNK * mch;
145         PTR p;
146         WORD from;
147         WORD w;
148         LONG loc;
149         SYM * symbol;
150
151         from = 0;
152
153         for(mch=firstmch; mch!=NULL; mch=mch->mcnext)
154         {
155                 printf("mch=$%08X mcptr=$%08X mcalloc=$%X mcused=$%X\n",
156                         (uint32_t)mch,
157                         (mch->mcptr.lw),
158                         mch->mcalloc,
159                         (mch->mcused));
160
161                 p = mch->mcptr;
162                 
163                 for(;;)
164                 {
165                         w = *p.wp++;
166
167                         if (w & MCHEND)
168                                 break;
169
170                         symbol = NULL;
171                         loc = *p.lp++;
172
173                         if (w & MCHFROM)
174                                 from = *p.wp++;
175
176                         if (w & MSYMBOL)
177                                 symbol = *p.sy++;
178
179                         printf("m=$%04X to=%d loc=$%X from=%d siz=%s",
180                                         w, w & 0x00FF, loc, from, (w & MLONG) ? "long" : "word");
181
182                         if (symbol != NULL)
183                                 printf(" sym=`%s'", symbol->sname);
184
185                         printf("\n");
186                 }
187         }
188
189         return 0;
190 }
191
192
193 //
194 // Dump memory from 'start' for 'count' bytes; `flg' is the following ORed together:
195 // 0 - bytes
196 // 1 - words
197 // 2 - longwords
198 // 
199 // if `base' is not -1, then print it at the start of each line, incremented accordingly.
200 //
201 int mdump(char * start, LONG count, int flg, LONG base)
202 {
203         int i, j, k;
204         j = 0;
205
206         for(i=0; i<(int)count;)
207         {
208                 if ((i & 15) == 0)
209                 {
210                         if (j < i)
211                         {
212                                 printf("  ");
213
214                                 while(j < i)
215                                 visprt(start[j++]);
216
217                                 putchar('\n');
218                         }
219
220                         j = i;
221
222                         if (base != -1)
223                                 printf("%08X  ", base);
224                 }
225
226                 switch (flg & 3)
227                 {
228                 case 0:
229                         printf("%02X ", start[i] & 0xff);
230                         ++i;
231                         break;
232                 case 1:
233                         printf("%02X%02X ", start[i] & 0xff, start[i+1] & 0xff);
234                         i += 2;
235                         break;
236                 case 2:
237                         printf("%02X%02X%02X%02X ", start[i] & 0xff, start[i+1] & 0xff,
238                                 start[i+2] & 0xff, start[i+3] & 0xff);
239                         i += 4;
240                         break;
241                 case 3:
242                         break;
243                 }
244
245                 if (base != -1)
246                         base += 1 << (flg & 3);
247         }
248
249         // Print remaining bit of ascii; the hairy expression computes the number of
250         // spaces to print to make the ascii line up nicely.
251         if (j != i)
252         {
253                 k = ((16 - (i - j)) / (1 << (flg & 3))) * siztab[flg & 3];
254
255                 while(k--)
256                         putchar(' ');
257
258                 printf("  ");
259
260                 while(j < i)
261                         visprt(start[j++]);
262
263                 putchar('\n');
264         }
265
266         return 0;
267 }
268
269
270 //
271 // Dump list of tokens on stdout in printable form
272 //
273 int dumptok(TOKEN * tk)
274 {
275         int flg = 0;
276
277         while (*tk != EOL)
278         {
279                 if (flg++)
280                         printf(" ");
281
282                 if (*tk >= 128)
283                 {
284                         printf("REG=%u", *tk++ - 128);
285                         continue;
286                 }
287
288                 switch ((int)*tk++)
289                 {
290                 case CONST:                                        // CONST <value>
291                         printf("CONST=%u", *tk++);
292                         break;
293                 case STRING:                                       // STRING <address>
294                         printf("STRING='%s'", string[*tk++]);
295                         break;
296                 case SYMBOL:                                       // SYMBOL <address> 
297                         printf("SYMBOL='%s'", string[*tk++]);
298                         break;
299                 case EOL:                                          // End of line 
300                         printf("EOL");
301                         break;
302                 case TKEOF:                                        // End of file (or macro)
303                         printf("TKEOF");
304                         break;
305                 case DEQUALS:                                      // == 
306                         printf("DEQUALS");
307                         break;
308                 case DCOLON:                                       // :: 
309                         printf("DCOLON");
310                         break;
311                 case GE:                                           // >= 
312                         printf("GE");
313                         break;
314                 case LE:                                           // <= 
315                         printf("LE");
316                         break;
317                 case NE:                                           // <> or != 
318                         printf("NE");
319                         break;
320                 case SHR:                                          // >> 
321                         printf("SHR");
322                         break;
323                 case SHL:                                          // << 
324                         printf("SHL");
325                         break;
326                 default:
327                         printf("%c", (int)tk[-1]);
328                         break;
329                 }
330         }
331
332         printf("\n");
333
334         return 0;
335 }
336
337
338 //
339 // Dump Everything
340 //
341 int dump_everything(void)
342 {
343         int i;
344
345         for(i=1; i<NSECTS; i++)
346         {
347                 if (sect[i].scattr & SUSED)
348                 {
349                         printf("Section %d sloc=$%X\n", i, sect[i].sloc);
350                         printf("Code:\n");
351                         chdump(sect[i].sfcode, 1);
352
353                         printf("Fixup:\n");
354                         fudump(sect[i].sffix);
355
356                         printf("\n");
357                 }
358         }
359
360         printf("\nMarks:\n");
361         mudump();                                                               // Dump marks
362         printf("Total memory allocated=$%X\n", amemtot);
363
364         return 0;
365 }