Apply Nindent to all .c and .h files
[nasm/avx512.git] / listing.c
blob1f5e961bb9487fa1d411aac33cc55b12c7e8af03
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,
62 listdata);
63 else
64 fprintf(listfp, "%*s", LIST_HEXBIT + 10, "");
66 if (listlevel_e)
67 fprintf(listfp, "%s<%d>", (listlevel < 10 ? " " : ""),
68 listlevel_e);
69 else if (listlinep)
70 fprintf(listfp, " ");
72 if (listlinep)
73 fprintf(listfp, " %s", listline);
75 fputc('\n', listfp);
76 listlinep = FALSE;
77 listdata[0] = '\0';
80 static void list_init(char *fname, efunc error)
82 listfp = fopen(fname, "w");
83 if (!listfp) {
84 error(ERR_NONFATAL, "unable to open listing file `%s'", fname);
85 return;
88 *listline = '\0';
89 listlineno = 0;
90 listp = TRUE;
91 listlevel = 0;
92 suppress = 0;
93 mistack = nasm_malloc(sizeof(MacroInhibit));
94 mistack->next = NULL;
95 mistack->level = 0;
96 mistack->inhibiting = TRUE;
99 static void list_cleanup(void)
101 if (!listp)
102 return;
104 while (mistack) {
105 MacroInhibit *temp = mistack;
106 mistack = temp->next;
107 nasm_free(temp);
110 list_emit();
111 fclose(listfp);
114 static void list_out(long offset, char *str)
116 if (strlen(listdata) + strlen(str) > LIST_HEXBIT) {
117 strcat(listdata, "-");
118 list_emit();
120 if (!listdata[0])
121 listoffset = offset;
122 strcat(listdata, str);
125 static void list_output(long offset, const void *data, unsigned long type)
127 unsigned long typ, size;
129 if (!listp || suppress || user_nolist) /* fbk - 9/2/00 */
130 return;
132 typ = type & OUT_TYPMASK;
133 size = type & OUT_SIZMASK;
135 if (typ == OUT_RAWDATA) {
136 unsigned char const *p = data;
137 char q[3];
138 while (size--) {
139 HEX(q, *p);
140 q[2] = '\0';
141 list_out(offset++, q);
142 p++;
144 } else if (typ == OUT_ADDRESS) {
145 unsigned long d = *(long *)data;
146 char q[11];
147 unsigned char p[4], *r = p;
148 if (size == 4) {
149 q[0] = '[';
150 q[9] = ']';
151 q[10] = '\0';
152 WRITELONG(r, d);
153 HEX(q + 1, p[0]);
154 HEX(q + 3, p[1]);
155 HEX(q + 5, p[2]);
156 HEX(q + 7, p[3]);
157 list_out(offset, q);
158 } else {
159 q[0] = '[';
160 q[5] = ']';
161 q[6] = '\0';
162 WRITESHORT(r, d);
163 HEX(q + 1, p[0]);
164 HEX(q + 3, p[1]);
165 list_out(offset, q);
167 } else if (typ == OUT_REL2ADR) {
168 unsigned long d = *(long *)data;
169 char q[11];
170 unsigned char p[4], *r = p;
171 q[0] = '(';
172 q[5] = ')';
173 q[6] = '\0';
174 WRITESHORT(r, d);
175 HEX(q + 1, p[0]);
176 HEX(q + 3, p[1]);
177 list_out(offset, q);
178 } else if (typ == OUT_REL4ADR) {
179 unsigned long d = *(long *)data;
180 char q[11];
181 unsigned char p[4], *r = p;
182 q[0] = '(';
183 q[9] = ')';
184 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);
191 } else if (typ == OUT_RESERVE) {
192 char q[20];
193 snprintf(q, sizeof(q), "<res %08lX>", size);
194 list_out(offset, q);
198 static void list_line(int type, char *line)
200 if (!listp)
201 return;
202 if (user_nolist) { /* fbk - 9/2/00 */
203 listlineno++;
204 return;
207 if (mistack && mistack->inhibiting) {
208 if (type == LIST_MACRO)
209 return;
210 else { /* pop the m i stack */
211 MacroInhibit *temp = mistack;
212 mistack = temp->next;
213 nasm_free(temp);
216 list_emit();
217 listlinep = TRUE;
218 strncpy(listline, line, LIST_MAX_LEN - 1);
219 listline[LIST_MAX_LEN - 1] = '\0';
220 listlevel_e = listlevel;
223 static void list_uplevel(int type)
225 if (!listp)
226 return;
227 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) {
236 MacroInhibit *temp = nasm_malloc(sizeof(MacroInhibit));
237 temp->next = mistack;
238 temp->level = listlevel;
239 temp->inhibiting = FALSE;
240 mistack = temp;
241 } else if (type == LIST_MACRO_NOLIST) {
242 MacroInhibit *temp = nasm_malloc(sizeof(MacroInhibit));
243 temp->next = mistack;
244 temp->level = listlevel;
245 temp->inhibiting = TRUE;
246 mistack = temp;
250 static void list_downlevel(int type)
252 if (!listp)
253 return;
255 if (type == LIST_INCBIN || type == LIST_TIMES) {
256 suppress &= ~(type == LIST_INCBIN ? 1 : 2);
257 return;
260 listlevel--;
261 while (mistack && mistack->level > listlevel) {
262 MacroInhibit *temp = mistack;
263 mistack = temp->next;
264 nasm_free(temp);
268 ListGen nasmlist = {
269 list_init,
270 list_cleanup,
271 list_output,
272 list_line,
273 list_uplevel,
274 list_downlevel