Correct /is4 encoding for EVEX instructions
[nasm.git] / asm / listing.c
blob90cc12719581d6bb569ae84a80f37bfd727e00b2
1 /* ----------------------------------------------------------------------- *
2 *
3 * Copyright 1996-2016 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * listing.c listing file generator for the Netwide Assembler
38 #include "compiler.h"
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <stddef.h>
43 #include <string.h>
44 #include <ctype.h>
46 #include "nasm.h"
47 #include "nasmlib.h"
48 #include "listing.h"
50 #define LIST_MAX_LEN 216 /* something sensible */
51 #define LIST_INDENT 40
52 #define LIST_HEXBIT 18
54 typedef struct MacroInhibit MacroInhibit;
56 static struct MacroInhibit {
57 MacroInhibit *next;
58 int level;
59 int inhibiting;
60 } *mistack;
62 static char xdigit[] = "0123456789ABCDEF";
64 #define HEX(a,b) (*(a)=xdigit[((b)>>4)&15],(a)[1]=xdigit[(b)&15]);
66 static char listline[LIST_MAX_LEN];
67 static bool listlinep;
69 static char listerror[LIST_MAX_LEN];
71 static char listdata[2 * LIST_INDENT]; /* we need less than that actually */
72 static int32_t listoffset;
74 static int32_t listlineno;
76 static int32_t listp;
78 static int suppress; /* for INCBIN & TIMES special cases */
80 static int listlevel, listlevel_e;
82 static FILE *listfp;
84 static void list_emit(void)
86 int i;
88 if (!listlinep && !listdata[0])
89 return;
91 fprintf(listfp, "%6"PRId32" ", listlineno);
93 if (listdata[0])
94 fprintf(listfp, "%08"PRIX32" %-*s", listoffset, LIST_HEXBIT + 1,
95 listdata);
96 else
97 fprintf(listfp, "%*s", LIST_HEXBIT + 10, "");
99 if (listlevel_e)
100 fprintf(listfp, "%s<%d>", (listlevel < 10 ? " " : ""),
101 listlevel_e);
102 else if (listlinep)
103 fprintf(listfp, " ");
105 if (listlinep)
106 fprintf(listfp, " %s", listline);
108 putc('\n', listfp);
109 listlinep = false;
110 listdata[0] = '\0';
112 if (listerror[0]) {
113 fprintf(listfp, "%6"PRId32" ", listlineno);
114 for (i = 0; i < LIST_HEXBIT; i++)
115 putc('*', listfp);
117 if (listlevel_e)
118 fprintf(listfp, " %s<%d>", (listlevel < 10 ? " " : ""),
119 listlevel_e);
120 else
121 fprintf(listfp, " ");
123 fprintf(listfp, " %s\n", listerror);
124 listerror[0] = '\0';
128 static void list_init(const char *fname)
130 if (!fname || fname[0] == '\0') {
131 listfp = NULL;
132 return;
135 listfp = nasm_open_write(fname, NF_TEXT);
136 if (!listfp) {
137 nasm_error(ERR_NONFATAL, "unable to open listing file `%s'",
138 fname);
139 return;
142 *listline = '\0';
143 listlineno = 0;
144 *listerror = '\0';
145 listp = true;
146 listlevel = 0;
147 suppress = 0;
148 mistack = nasm_malloc(sizeof(MacroInhibit));
149 mistack->next = NULL;
150 mistack->level = 0;
151 mistack->inhibiting = true;
154 static void list_cleanup(void)
156 if (!listp)
157 return;
159 while (mistack) {
160 MacroInhibit *temp = mistack;
161 mistack = temp->next;
162 nasm_free(temp);
165 list_emit();
166 fclose(listfp);
169 static void list_out(int64_t offset, char *str)
171 if (strlen(listdata) + strlen(str) > LIST_HEXBIT) {
172 strcat(listdata, "-");
173 list_emit();
175 if (!listdata[0])
176 listoffset = offset;
177 strcat(listdata, str);
180 static void list_address(int64_t offset, const char *brackets,
181 int64_t addr, int size)
183 char q[20];
184 char *r = q;
186 nasm_assert(size <= 8);
188 *r++ = brackets[0];
189 while (size--) {
190 HEX(r, addr);
191 addr >>= 8;
192 r += 2;
194 *r++ = brackets[1];
195 *r = '\0';
196 list_out(offset, q);
199 static void list_output(const struct out_data *data)
201 char q[20];
202 uint64_t size = data->size;
203 uint64_t offset = data->offset;
205 if (!listp || suppress || user_nolist)
206 return;
208 switch (data->type) {
209 case OUT_RAWDATA:
211 const uint8_t *p = data->data;
213 if (size == 0 && !listdata[0])
214 listoffset = data->offset;
215 while (size--) {
216 HEX(q, *p);
217 q[2] = '\0';
218 list_out(offset++, q);
219 p++;
221 break;
223 case OUT_ADDRESS:
224 case OUT_SEGMENT:
225 list_address(offset, "[]", data->toffset, size);
226 break;
227 case OUT_RELADDR:
228 list_address(offset, "()", data->toffset, size);
229 break;
230 case OUT_RESERVE:
232 snprintf(q, sizeof(q), "<res %08"PRIX64">", size);
233 list_out(offset, q);
234 break;
236 default:
237 panic();
241 static void list_line(int type, char *line)
243 if (!listp)
244 return;
246 if (user_nolist)
247 return;
249 if (mistack && mistack->inhibiting) {
250 if (type == LIST_MACRO)
251 return;
252 else { /* pop the m i stack */
253 MacroInhibit *temp = mistack;
254 mistack = temp->next;
255 nasm_free(temp);
258 list_emit();
259 listlineno = src_get_linnum();
260 listlinep = true;
261 strncpy(listline, line, LIST_MAX_LEN - 1);
262 listline[LIST_MAX_LEN - 1] = '\0';
263 listlevel_e = listlevel;
266 static void list_uplevel(int type)
268 if (!listp)
269 return;
270 if (type == LIST_INCBIN || type == LIST_TIMES) {
271 suppress |= (type == LIST_INCBIN ? 1 : 2);
272 list_out(listoffset, type == LIST_INCBIN ? "<incbin>" : "<rept>");
273 return;
276 listlevel++;
278 if (mistack && mistack->inhibiting && type == LIST_INCLUDE) {
279 MacroInhibit *temp = nasm_malloc(sizeof(MacroInhibit));
280 temp->next = mistack;
281 temp->level = listlevel;
282 temp->inhibiting = false;
283 mistack = temp;
284 } else if (type == LIST_MACRO_NOLIST) {
285 MacroInhibit *temp = nasm_malloc(sizeof(MacroInhibit));
286 temp->next = mistack;
287 temp->level = listlevel;
288 temp->inhibiting = true;
289 mistack = temp;
293 static void list_downlevel(int type)
295 if (!listp)
296 return;
298 if (type == LIST_INCBIN || type == LIST_TIMES) {
299 suppress &= ~(type == LIST_INCBIN ? 1 : 2);
300 return;
303 listlevel--;
304 while (mistack && mistack->level > listlevel) {
305 MacroInhibit *temp = mistack;
306 mistack = temp->next;
307 nasm_free(temp);
311 static void list_error(int severity, const char *pfx, const char *msg)
313 if (!listfp)
314 return;
316 snprintf(listerror, sizeof listerror, "%s%s", pfx, msg);
318 if ((severity & ERR_MASK) >= ERR_FATAL)
319 list_emit();
322 static void list_set_offset(uint64_t offset)
324 listoffset = offset;
327 static const struct lfmt nasm_list = {
328 list_init,
329 list_cleanup,
330 list_output,
331 list_line,
332 list_uplevel,
333 list_downlevel,
334 list_error,
335 list_set_offset
338 const struct lfmt *lfmt = &nasm_list;