]> git.sur5r.net Git - cc65/blob - src/ca65/listing.c
Fixes for the watcom makefiles:
[cc65] / src / ca65 / listing.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 listing.c                                 */
4 /*                                                                           */
5 /*                Listing support for the ca65 crossassembler                */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2000-2009, Ullrich von Bassewitz                                      */
10 /*                Roemerstrasse 52                                           */
11 /*                D-70794 Filderstadt                                        */
12 /* EMail:         uz@cc65.org                                                */
13 /*                                                                           */
14 /*                                                                           */
15 /* This software is provided 'as-is', without any expressed or implied       */
16 /* warranty.  In no event will the authors be held liable for any damages    */
17 /* arising from the use of this software.                                    */
18 /*                                                                           */
19 /* Permission is granted to anyone to use this software for any purpose,     */
20 /* including commercial applications, and to alter it and redistribute it    */
21 /* freely, subject to the following restrictions:                            */
22 /*                                                                           */
23 /* 1. The origin of this software must not be misrepresented; you must not   */
24 /*    claim that you wrote the original software. If you use this software   */
25 /*    in a product, an acknowledgment in the product documentation would be  */
26 /*    appreciated but is not required.                                       */
27 /* 2. Altered source versions must be plainly marked as such, and must not   */
28 /*    be misrepresented as being the original software.                      */
29 /* 3. This notice may not be removed or altered from any source              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <stdio.h>
37 #include <string.h>
38 #include <errno.h>
39
40 /* common */
41 #include "check.h"
42 #include "fname.h"
43 #include "fragdefs.h"
44 #include "version.h"
45 #include "xmalloc.h"
46
47 /* ca65 */
48 #include "error.h"
49 #include "filetab.h"
50 #include "global.h"
51 #include "listing.h"
52 #include "segment.h"
53
54
55
56 /*****************************************************************************/
57 /*                                   Data                                    */
58 /*****************************************************************************/
59
60
61
62 /* Single linked list of lines */
63 ListLine*       LineList = 0;           /* List of listing lines */
64 ListLine*       LineCur  = 0;           /* Current listing line */
65 ListLine*       LineLast = 0;           /* Last (current) listing line */
66
67 /* Page and other formatting */
68 int             PageLength = -1;        /* Length of a listing page */
69 static unsigned PageNumber = 1;         /* Current listing page number */
70 static int      PageLines  = 0;         /* Current line on page */
71 static unsigned ListBytes  = 12;        /* Number of bytes to list for one line */
72
73 /* Switch the listing on/off */
74 static int      ListingEnabled = 1;     /* Enabled if > 0 */
75
76
77
78 /*****************************************************************************/
79 /*                                   Code                                    */
80 /*****************************************************************************/
81
82
83
84 void NewListingLine (const char* Line, unsigned char File, unsigned char Depth)
85 /* Create a new ListLine struct and insert it */
86 {
87     /* Store only if listing is enabled */
88     if (Listing) {
89
90         ListLine* L;
91
92         /* Get the length of the line */
93         unsigned Len = strlen (Line);
94
95         /* Ignore trailing newlines */
96         while (Len > 0 && Line[Len-1] == '\n') {
97             --Len;
98         }
99
100         /* Allocate memory */
101         L = xmalloc (sizeof (ListLine) + Len);
102
103         /* Initialize the fields. */
104         L->Next         = 0;
105         L->FragList     = 0;
106         L->FragLast     = 0;
107         L->PC           = GetPC ();
108         L->Reloc        = GetRelocMode ();
109         L->File         = File;
110         L->Depth        = Depth;
111         L->Output       = (ListingEnabled > 0);
112         L->ListBytes    = (unsigned char) ListBytes;
113         memcpy (L->Line, Line, Len);
114         L->Line [Len] = '\0';
115
116         /* Insert the line into the list of lines */
117         if (LineList == 0) {
118             LineList = L;
119         } else {
120             LineLast->Next = L;
121         }
122         LineLast = L;
123     }
124 }
125
126
127
128 void EnableListing (void)
129 /* Enable output of lines to the listing */
130 {
131     if (Listing) {
132         /* If we're about to enable the listing, do this for the current line
133          * also, so we will see the source line that did this.
134          */
135         if (ListingEnabled++ == 0) {
136             LineCur->Output = 1;
137         }
138     }
139 }
140
141
142
143 void DisableListing (void)
144 /* Disable output of lines to the listing */
145 {
146     if (Listing) {
147         if (ListingEnabled == 0) {
148             /* Cannot switch the listing off once more */
149             Error ("Counter underflow");
150         } else {
151             --ListingEnabled;
152         }
153     }
154 }
155
156
157
158 void SetListBytes (int Bytes)
159 /* Set the maximum number of bytes listed for one line */
160 {
161     if (Bytes < 0) {
162         Bytes = 0;      /* Encode "unlimited" as zero */
163     }
164     ListBytes = Bytes;
165 }
166
167
168
169 void InitListingLine (void)
170 /* Initialize the current listing line */
171 {
172     if (Listing) {
173         /* Make the last loaded line the current line */
174         /* ###### This code is a hack! We really need to do it right
175          * as soon as we know, how:-(
176          */
177         if (LineCur && LineCur->Next && LineCur->Next != LineLast) {
178             ListLine* L = LineCur;
179             do {
180                 L = L->Next;
181                 /* Set the values for this line */
182                 CHECK (L != 0);
183                 L->PC            = GetPC ();
184                 L->Reloc         = GetRelocMode ();
185                 L->Output        = (ListingEnabled > 0);
186                 L->ListBytes = (unsigned char) ListBytes;
187             } while (L->Next != LineLast);
188         }
189         LineCur = LineLast;
190
191         /* Set the values for this line */
192         CHECK (LineCur != 0);
193         LineCur->PC         = GetPC ();
194         LineCur->Reloc      = GetRelocMode ();
195         LineCur->Output     = (ListingEnabled > 0);
196         LineCur->ListBytes  = (unsigned char) ListBytes;
197     }
198 }
199
200
201
202 static char* AddHex (char* S, unsigned Val)
203 /* Add a hex byte in ASCII to the given string and return the new pointer */
204 {
205     static const char HexTab [16] = {
206         '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
207     };
208
209     *S++ = HexTab [(Val >> 4) & 0x0F];
210     *S++ = HexTab [Val & 0x0F];
211
212     return S;
213 }
214
215
216
217 static void PrintPageHeader (FILE* F, const ListLine* L)
218 /* Print the header for a new page. It is assumed that the given line is the
219  * last line of the previous page.
220  */
221 {
222     /* Gte a pointer to the current input file */
223     const StrBuf* CurFile = GetFileName (L->File);
224
225     /* Print the header on the new page */
226     fprintf (F,
227              "ca65 V%s - %s\n"
228              "Main file   : %s\n"
229              "Current file: %.*s\n"
230              "\n",
231              GetVersionAsString (), Copyright,
232              InFile,
233              (int) SB_GetLen (CurFile), SB_GetConstBuf (CurFile));
234
235     /* Count pages, reset lines */
236     ++PageNumber;
237     PageLines = 4;
238 }
239
240
241
242 static void PrintLine (FILE* F, const char* Header, const char* Line, const ListLine* L)
243 /* Print one line to the listing file, adding a newline and counting lines */
244 {
245     /* Print the given line */
246     fprintf (F, "%s%s\n", Header, Line);
247
248     /* Increment the current line */
249     ++PageLines;
250
251     /* Switch to a new page if needed. Do not switch, if the current line is
252      * the last one, to avoid pages that consist of just the header.
253      */
254     if (PageLength > 0 && PageLines >= PageLength && L->Next != 0) {
255         /* Do a formfeed */
256         putc ('\f', F);
257         /* Print the header on the new page */
258         PrintPageHeader (F, L);
259     }
260 }
261
262
263
264 static char* AddMult (char* S, char C, unsigned Count)
265 /* Add multiple instances of character C to S, return updated S. */
266 {
267     memset (S, C, Count);
268     return S + Count;
269 }
270
271
272
273 static char* MakeLineHeader (char* H, const ListLine* L)
274 /* Prepare the line header */
275 {
276     char Mode;
277     char Depth;
278
279     /* Setup the PC mode */
280     Mode = (L->Reloc)? 'r' : ' ';
281
282     /* Set up the include depth */
283     Depth = (L->Depth < 10)? L->Depth + '0' : '+';
284
285     /* Format the line */
286     sprintf (H, "%06lX%c %c", L->PC, Mode, Depth);
287     memset (H+9, ' ', LINE_HEADER_LEN-9);
288
289     /* Return the buffer */
290     return H;
291 }
292
293
294
295 void CreateListing (void)
296 /* Create the listing */
297 {
298     FILE* F;
299     Fragment* Frag;
300     ListLine* L;
301     char HeaderBuf [LINE_HEADER_LEN+1];
302
303     /* Create the name of the listing file if needed */
304     if (ListFile == 0) {
305         ListFile = MakeFilename (InFile, ListExt);
306     }
307
308     /* Open the real listing file */
309     F = fopen (ListFile, "w");
310     if (F == 0) {
311         Fatal ("Cannot open listing file: %s", strerror (errno));
312     }
313
314     /* Reset variables, print the header for the first page */
315     PageNumber = 0;
316     PrintPageHeader (F, LineList);
317
318     /* Terminate the header buffer. The last byte will never get overwritten */
319     HeaderBuf [LINE_HEADER_LEN] = '\0';
320
321     /* Walk through all listing lines */
322     L = LineList;
323     while (L) {
324
325         char* Buf;
326         char* B;
327         unsigned Count;
328         unsigned I;
329         char* Line;
330
331         /* If we should not output this line, go to the next */
332         if (L->Output == 0) {
333             L = L->Next;
334             continue;
335         }
336
337         /* If we don't have a fragment list for this line, things are easy */
338         if (L->FragList == 0) {
339             PrintLine (F, MakeLineHeader (HeaderBuf, L), L->Line, L);
340             L = L->Next;
341             continue;
342         }
343
344         /* Count the number of bytes in the complete fragment list */
345         Count = 0;
346         Frag = L->FragList;
347         while (Frag) {
348             Count += Frag->Len;
349             Frag = Frag->LineList;
350         }
351
352         /* Allocate memory for the given number of bytes */
353         Buf = xmalloc (Count*2+1);
354
355         /* Copy an ASCII representation of the bytes into the buffer */
356         B = Buf;
357         Frag = L->FragList;
358         while (Frag) {
359
360             /* Write data depending on the type */
361             switch (Frag->Type) {
362
363                 case FRAG_LITERAL:
364                     for (I = 0; I < Frag->Len; ++I) {
365                         B = AddHex (B, Frag->V.Data[I]);
366                     }
367                     break;
368
369                 case FRAG_EXPR:
370                 case FRAG_SEXPR:
371                     B = AddMult (B, 'r', Frag->Len*2);
372                     break;
373
374                 case FRAG_FILL:
375                     B = AddMult (B, 'x', Frag->Len*2);
376                     break;
377
378                 default:
379                     Internal ("Invalid fragment type: %u", Frag->Type);
380
381             }
382
383             /* Next fragment */
384             Frag = Frag->LineList;
385
386         }
387
388         /* Limit the number of bytes actually printed */
389         if (L->ListBytes != 0) {
390             /* Not unlimited */
391             if (Count > L->ListBytes) {
392                 Count = L->ListBytes;
393             }
394         }
395
396         /* Output the data. The format of a listing line is:
397          *
398          *      PPPPPPm I  11 22 33 44
399          *
400          * where
401          *
402          *      PPPPPP  is the PC
403          *      m       is the mode ('r' or empty)
404          *      I       is the include level
405          *      11 ..   are code or data bytes
406          */
407         Line = L->Line;
408         B    = Buf;
409         while (Count) {
410
411             unsigned    Chunk;
412             char*       P;
413
414             /* Prepare the line header */
415             MakeLineHeader (HeaderBuf, L);
416
417             /* Get the number of bytes for the next line */
418             Chunk = Count;
419             if (Chunk > 4) {
420                 Chunk = 4;
421             }
422             Count -= Chunk;
423
424             /* Increment the program counter. Since we don't need the PC stored
425              * in the LineList object for anything else, just increment this
426              * variable.
427              */
428             L->PC += Chunk;
429
430             /* Copy the bytes into the line */
431             P = HeaderBuf + 11;
432             for (I = 0; I < Chunk; ++I) {
433                 *P++ = *B++;
434                 *P++ = *B++;
435                 *P++ = ' ';
436             }
437
438             /* Output this line */
439             PrintLine (F, HeaderBuf, Line, L);
440
441             /* Don't output a line twice */
442             Line = "";
443
444         }
445
446         /* Delete the temporary buffer */
447         xfree (Buf);
448
449         /* Next line */
450         L = L->Next;
451
452     }
453
454     /* Close the listing file */
455     (void) fclose (F);
456 }
457
458
459