]> Shamusworld >> Repos - rmac/blob - debug.c
Small fix for FU_JR fixups.
[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("$%ux ", *tp++);
52                                 break;
53                         case ACONST:
54                                 printf("ACONST=($%ux,$%ux) ", *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=$%08ux, chsize=$%ux\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 $%08ux %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                                 p.sy++;
120                         }
121
122                         if ((attr & 0x0F00) == FU_JR)
123                                 p.lp++;
124                 }
125
126                 ch = ch->chnext;
127         }
128
129         return 0;
130 }
131
132
133 //
134 // Dump marks
135 //
136 int mudump(void)
137 {
138         MCHUNK * mch;
139         PTR p;
140         WORD from;
141         WORD w;
142         LONG loc;
143         SYM * symbol;
144
145         from = 0;
146
147         for(mch=firstmch; mch!=NULL; mch=mch->mcnext)
148         {
149                 printf("mch=$%08ux mcptr=$%08ux mcalloc=$%ux mcused=$%x\n",
150                         (uint32_t)mch,
151                         (mch->mcptr.lw),
152                         mch->mcalloc,
153                         (mch->mcused));
154
155                 p = mch->mcptr;
156                 
157                 for(;;)
158                 {
159                         w = *p.wp++;
160
161                         if (w & MCHEND)
162                                 break;
163
164                         symbol = NULL;
165                         loc = *p.lp++;
166
167                         if (w & MCHFROM)
168                                 from = *p.wp++;
169
170                         if (w & MSYMBOL)
171                                 symbol = *p.sy++;
172
173                         printf("m=$%04x to=%d loc=$%ux from=%d siz=%s",
174                                         w, w & 0x00ff, loc, from, (w & MLONG) ? "long" : "word");
175
176                         if (symbol != NULL)
177                                 printf(" sym=`%s'", symbol->sname);
178
179                         printf("\n");
180                 }
181         }
182
183         return 0;
184 }
185
186
187 //
188 // Dump memory from 'start' for 'count' bytes; `flg' is the following ORed together:
189 // 0 - bytes
190 // 1 - words
191 // 2 - longwords
192 // 
193 // if `base' is not -1, then print it at the start of each line, incremented accordingly.
194 //
195 int mdump(char * start, LONG count, int flg, LONG base)
196 {
197         int i, j, k;
198         j = 0;
199
200         for(i=0; i<(int)count;)
201         {
202                 if ((i & 15) == 0)
203                 {
204                         if (j < i)
205                         {
206                                 printf("  ");
207
208                                 while(j < i)
209                                 visprt(start[j++]);
210
211                                 putchar('\n');
212                         }
213
214                         j = i;
215
216                         if (base != -1)
217                                 printf("%08ux  ", base);
218                 }
219
220                 switch (flg & 3)
221                 {
222                 case 0:
223                         printf("%02x ", start[i] & 0xff);
224                         ++i;
225                         break;
226                 case 1:
227                         printf("%02x%02x ", start[i] & 0xff, start[i+1] & 0xff);
228                         i += 2;
229                         break;
230                 case 2:
231                         printf("%02x%02x%02x%02x ", start[i] & 0xff, start[i+1] & 0xff,
232                                 start[i+2] & 0xff, start[i+3] & 0xff);
233                         i += 4;
234                         break;
235                 case 3:
236                         break;
237                 }
238
239                 if (base != -1)
240                         base += 1 << (flg & 3);
241         }
242
243         // Print remaining bit of ascii; the hairy expression computes the number of
244         // spaces to print to make the ascii line up nicely.
245         if (j != i)
246         {
247                 k = ((16 - (i - j)) / (1 << (flg & 3))) * siztab[flg & 3];
248
249                 while(k--)
250                         putchar(' ');
251
252                 printf("  ");
253
254                 while(j < i)
255                         visprt(start[j++]);
256
257                 putchar('\n');
258         }
259
260         return 0;
261 }
262
263
264 //
265 // Dump list of tokens on stdout in printable form
266 //
267 int dumptok(TOKEN * tk)
268 {
269         int flg = 0;
270
271         while (*tk != EOL)
272         {
273                 if (flg++)
274                         printf(" ");
275
276                 if (*tk >= 128)
277                 {
278                         printf("REG=%ud", *tk++ - 128);
279                         continue;
280                 }
281
282                 switch ((int)*tk++)
283                 {
284                 case CONST:                                        // CONST <value>
285                         printf("CONST=%ud", *tk++);
286                         break;
287                 case STRING:                                       // STRING <address>
288 //                      printf("STRING='%s'", (char *)*tk++);
289                         printf("STRING='%s'", string[*tk++]);
290                         break;
291                 case SYMBOL:                                       // SYMBOL <address> 
292 //                      printf("SYMBOL='%s'", (char *)*tk++);
293                         printf("SYMBOL='%s'", string[*tk++]);
294                         break;
295                 case EOL:                                          // End of line 
296                         printf("EOL");
297                         break;
298                 case TKEOF:                                        // End of file (or macro)
299                         printf("TKEOF");
300                         break;
301                 case DEQUALS:                                      // == 
302                         printf("DEQUALS");
303                         break;
304                 case DCOLON:                                       // :: 
305                         printf("DCOLON");
306                         break;
307                 case GE:                                           // >= 
308                         printf("GE");
309                         break;
310                 case LE:                                           // <= 
311                         printf("LE");
312                         break;
313                 case NE:                                           // <> or != 
314                         printf("NE");
315                         break;
316                 case SHR:                                          // >> 
317                         printf("SHR");
318                         break;
319                 case SHL:                                          // << 
320                         printf("SHL");
321                         break;
322                 default:
323                         printf("%c", (int)tk[-1]);
324                         break;
325                 }
326         }
327
328         printf("\n");
329
330         return 0;
331 }
332
333
334 //
335 // Dump Everything
336 //
337 int dump_everything(void)
338 {
339         int i;
340
341         for(i=1; i<NSECTS; i++)
342         {
343                 if (sect[i].scattr & SUSED)
344                 {
345                         printf("Section %d sloc=$%ux\n", i, sect[i].sloc);
346                         printf("Code:\n");
347                         chdump(sect[i].sfcode, 1);
348
349                         printf("Fixup:\n");
350                         fudump(sect[i].sffix);
351
352                         printf("\n");
353                 }
354         }
355
356         printf("\nMarks:\n");
357         mudump();                                                // Dump marks
358         printf("Total memory allocated=$%ux\n", amemtot);
359
360         return 0;
361 }