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