]> Shamusworld >> Repos - rmac/blob - debug.c
75520486cc3b24c6bcc624aecd0ad11918f9de87
[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 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 // --- Print 'c' Visibly ---------------------------------------------------------------------------
18 //
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 // --- Print expression, return ptr to just past the ENDEXPR ---------------------------------------
32 //
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("$%lx ", *tp++);
48                break;
49             case ACONST:
50                printf("ACONST=($%lx,$%lx) ", *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 // --- Dump data in a chunk (and maybe others) in the appropriate format ---------------------------
66 //
67
68 int chdump(CHUNK * ch, int format)
69 {
70    while (ch != NULL)
71    {
72       printf("chloc=$%08lx, chsize=$%lx\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 // --- Dump fixup records in printable format ------------------------------------------------------
82 //
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 $%08lx %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 // --- Dump marks ----------------------------------------------------------------------------------
127 //
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=$%08lx mcptr=$%08lx mcalloc=$%lx mcused=$%x\n",
143              (unsigned long int)mch,
144              (unsigned long int)(mch->mcptr.lw),
145              mch->mcalloc,
146              (unsigned int)(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=$%lx 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 //
189
190 int mdump(char *start, LONG count, int flg, LONG base) {
191    int i, j, k;
192
193    j = 0;
194    for(i = 0; i < (int)count;) {
195       if((i & 15) == 0) {
196          if(j < i) {
197             printf("  ");
198             while(j < i)
199                visprt(start[j++]);
200             putchar('\n');
201          }
202          j = i;
203          if(base != -1)
204             printf("%08lx  ", base);
205       }
206
207       switch(flg & 3) {
208          case 0:
209             printf("%02x ", start[i] & 0xff);
210             ++i;
211             break;
212          case 1:
213             printf("%02x%02x ", start[i] & 0xff, start[i+1] & 0xff);
214             i += 2;
215             break;
216          case 2:
217             printf("%02x%02x%02x%02x ", start[i] & 0xff, start[i+1] & 0xff,
218                    start[i+2] & 0xff, start[i+3] & 0xff);
219             i += 4;
220             break;
221          case 3:
222             break;
223       }
224
225       if(base != -1)
226          base += 1 << (flg & 3);
227    }
228
229    // Print remaining bit of ascii; the hairy expression computes the number of
230    // spaces to print to make the ascii line up nicely.
231    if(j != i) {
232       k = ((16 - (i - j)) / (1 << (flg & 3))) * siztab[flg & 3];
233       while(k--)
234          putchar(' ');
235       printf("  ");
236       while(j < i)
237          visprt(start[j++]);
238       putchar('\n');
239    }
240
241    return(0);
242 }
243
244 //
245 // --- Dump list of tokens on stdout in printable form ---------------------------------------------
246 //
247
248 int dumptok(TOKEN * tk)
249 {
250    int flg = 0;
251
252    while (*tk != EOL)
253    {
254       if (flg++)
255          printf(" ");
256
257       if (*tk >= 128)
258           {
259          printf("REG=%ld", *tk++ - 128);
260          continue;
261       }
262
263       switch ((int)*tk++)
264           {
265          case CONST:                                        // CONST <value>
266             printf("CONST=%ld", *tk++);
267             break;
268          case STRING:                                       // STRING <address>
269             printf("STRING='%s'", (char *)*tk++);
270             break;
271          case SYMBOL:                                       // SYMBOL <address> 
272             printf("SYMBOL='%s'", (char *)*tk++);
273             break;
274          case EOL:                                          // End of line 
275             printf("EOL");
276             break;
277          case TKEOF:                                        // End of file (or macro)
278             printf("TKEOF");
279             break;
280          case DEQUALS:                                      // == 
281             printf("DEQUALS");
282             break;
283          case DCOLON:                                       // :: 
284             printf("DCOLON");
285             break;
286          case GE:                                           // >= 
287             printf("GE");
288             break;
289          case LE:                                           // <= 
290             printf("LE");
291             break;
292          case NE:                                           // <> or != 
293             printf("NE");
294             break;
295          case SHR:                                          // >> 
296             printf("SHR");
297             break;
298          case SHL:                                          // << 
299             printf("SHL");
300             break;
301          default:
302             printf("%c", (int)tk[-1]);
303             break;
304       }
305    }
306
307    printf("\n");
308
309    return 0;
310 }
311
312 //
313 // --- Dump Everything -----------------------------------------------------------------------------
314 //
315
316 int dump_everything(void) {
317    int i;
318
319    for(i = 1; i < NSECTS; ++i)
320       if(sect[i].scattr & SUSED) {
321          printf("Section %d sloc=$%lx\n", i, sect[i].sloc);
322          printf("Code:\n");
323          chdump(sect[i].sfcode, 1);
324
325          printf("Fixup:\n");
326          fudump(sect[i].sffix);
327
328          printf("\n");
329       }
330
331    printf("\nMarks:\n");
332    mudump();                                                // Dump marks
333    printf("Total memory allocated=$%lx\n", amemtot);
334
335    return(0);
336 }