NASM 0.98p3.5
[nasm.git] / listing.c
blobaf8a9bf8be21b7d2c9cf88243c0be9a53aa120cc
1 /* listing.c listing file generator for the Netwide Assembler
3 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
4 * Julian Hall. All rights reserved. The software is
5 * redistributable under the licence given in the file "Licence"
6 * distributed in the NASM archive.
8 * initial version 2/vii/97 by Simon Tatham
9 */
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <stddef.h>
14 #include <string.h>
15 #include <ctype.h>
17 #include "nasm.h"
18 #include "nasmlib.h"
19 #include "listing.h"
21 #define LIST_MAX_LEN 216 /* something sensible */
22 #define LIST_INDENT 40
23 #define LIST_HEXBIT 18
25 typedef struct MacroInhibit MacroInhibit;
27 static struct MacroInhibit {
28 MacroInhibit *next;
29 int level;
30 int inhibiting;
31 } *mistack;
33 static char xdigit[] = "0123456789ABCDEF";
35 #define HEX(a,b) (*(a)=xdigit[((b)>>4)&15],(a)[1]=xdigit[(b)&15]);
37 static char listline[LIST_MAX_LEN];
38 static int listlinep;
40 static char listdata[2*LIST_INDENT]; /* we need less than that actually */
41 static long listoffset;
43 static long listlineno;
45 static long listp;
47 static int suppress; /* for INCBIN & TIMES special cases */
49 static int listlevel, listlevel_e;
51 static FILE *listfp;
53 static void list_emit (void)
55 if (!listlinep && !listdata[0])
56 return;
58 fprintf(listfp, "%6ld ", ++listlineno);
60 if (listdata[0])
61 fprintf(listfp, "%08lX %-*s", listoffset, LIST_HEXBIT+1, listdata);
62 else
63 fprintf(listfp, "%*s", LIST_HEXBIT+10, "");
65 if (listlevel_e)
66 fprintf(listfp, "%s<%d>", (listlevel < 10 ? " " : ""), listlevel_e);
67 else if (listlinep)
68 fprintf(listfp, " ");
70 if (listlinep)
71 fprintf(listfp, " %s", listline);
73 fputc('\n', listfp);
74 listlinep = FALSE;
75 listdata[0] = '\0';
78 static void list_init (char *fname, efunc error)
80 listfp = fopen (fname, "w");
81 if (!listfp) {
82 error (ERR_NONFATAL, "unable to open listing file `%s'", fname);
83 return;
86 *listline = '\0';
87 listlineno = 0;
88 listp = TRUE;
89 listlevel = 0;
90 suppress = 0;
91 mistack = nasm_malloc(sizeof(MacroInhibit));
92 mistack->next = NULL;
93 mistack->level = 0;
94 mistack->inhibiting = TRUE;
97 static void list_cleanup (void)
99 if (!listp)
100 return;
102 while (mistack) {
103 MacroInhibit *temp = mistack;
104 mistack = temp->next;
105 nasm_free (temp);
108 list_emit();
109 fclose (listfp);
112 static void list_out (long offset, char *str)
114 if (strlen(listdata) + strlen(str) > LIST_HEXBIT) {
115 strcat(listdata, "-");
116 list_emit();
118 if (!listdata[0])
119 listoffset = offset;
120 strcat(listdata, str);
123 static void list_output (long offset, void *data, unsigned long type)
125 long typ, size;
127 if (!listp || suppress)
128 return;
130 typ = type & OUT_TYPMASK;
131 size = type & OUT_SIZMASK;
133 if (typ == OUT_RAWDATA)
135 unsigned char *p = data;
136 char q[3];
137 while (size--)
139 HEX (q, *p);
140 q[2] = '\0';
141 list_out (offset++, q);
142 p++;
145 else if (typ == OUT_ADDRESS)
147 unsigned long d = *(long *)data;
148 char q[11];
149 unsigned char p[4], *r = p;
150 if (size == 4)
152 q[0] = '['; q[9] = ']'; q[10] = '\0';
153 WRITELONG (r, d);
154 HEX (q+1, p[0]);
155 HEX (q+3, p[1]);
156 HEX (q+5, p[2]);
157 HEX (q+7, p[3]);
158 list_out (offset, q);
160 else {
161 q[0] = '['; q[5] = ']'; q[6] = '\0';
162 WRITESHORT (r, d);
163 HEX (q+1, p[0]);
164 HEX (q+3, p[1]);
165 list_out (offset, q);
168 else if (typ == OUT_REL2ADR)
170 unsigned long d = *(long *)data;
171 char q[11];
172 unsigned char p[4], *r = p;
173 q[0] = '('; q[5] = ')'; q[6] = '\0';
174 WRITESHORT (r, d);
175 HEX (q+1, p[0]);
176 HEX (q+3, p[1]);
177 list_out (offset, q);
179 else if (typ == OUT_REL4ADR)
181 unsigned long d = *(long *)data;
182 char q[11];
183 unsigned char p[4], *r = p;
184 q[0] = '('; q[9] = ')'; q[10] = '\0';
185 WRITELONG (r, d);
186 HEX (q+1, p[0]);
187 HEX (q+3, p[1]);
188 HEX (q+5, p[2]);
189 HEX (q+7, p[3]);
190 list_out (offset, q);
192 else if (typ == OUT_RESERVE)
194 char q[20];
195 sprintf(q, "<res %08lX>", size);
196 list_out (offset, q);
200 static void list_line (int type, char *line)
202 if (!listp)
203 return;
205 if (mistack && mistack->inhibiting)
207 if (type == LIST_MACRO)
208 return;
209 else { /* pop the m i stack */
210 MacroInhibit *temp = mistack;
211 mistack = temp->next;
212 nasm_free (temp);
215 list_emit();
216 listlinep = TRUE;
217 strncpy (listline, line, LIST_MAX_LEN-1);
218 listline[LIST_MAX_LEN-1] = '\0';
219 listlevel_e = listlevel;
222 static void list_uplevel (int type)
224 if (!listp)
225 return;
226 if (type == LIST_INCBIN || type == LIST_TIMES)
228 suppress |= (type == LIST_INCBIN ? 1 : 2);
229 list_out (listoffset, type == LIST_INCBIN ? "<incbin>" : "<rept>");
230 return;
233 listlevel++;
235 if (mistack && mistack->inhibiting && type == LIST_INCLUDE)
237 MacroInhibit *temp = nasm_malloc(sizeof(MacroInhibit));
238 temp->next = mistack;
239 temp->level = listlevel;
240 temp->inhibiting = FALSE;
241 mistack = temp;
243 else if (type == LIST_MACRO_NOLIST)
245 MacroInhibit *temp = nasm_malloc(sizeof(MacroInhibit));
246 temp->next = mistack;
247 temp->level = listlevel;
248 temp->inhibiting = TRUE;
249 mistack = temp;
253 static void list_downlevel (int type)
255 if (!listp)
256 return;
258 if (type == LIST_INCBIN || type == LIST_TIMES)
260 suppress &= ~(type == LIST_INCBIN ? 1 : 2);
261 return;
264 listlevel--;
265 while (mistack && mistack->level > listlevel)
267 MacroInhibit *temp = mistack;
268 mistack = temp->next;
269 nasm_free (temp);
273 ListGen nasmlist = {
274 list_init,
275 list_cleanup,
276 list_output,
277 list_line,
278 list_uplevel,
279 list_downlevel