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