MPX: Adapt GAS's mib syntax with an index reg only
[platform/upstream/nasm.git] / listing.c
1 /* ----------------------------------------------------------------------- *
2  *   
3  *   Copyright 1996-2009 The NASM Authors - All Rights Reserved
4  *   See the file AUTHORS included with the NASM distribution for
5  *   the specific copyright holders.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following
9  *   conditions are met:
10  *
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.
17  *     
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.
31  *
32  * ----------------------------------------------------------------------- */
33
34 /*
35  * listing.c    listing file generator for the Netwide Assembler
36  */
37
38 #include "compiler.h"
39
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <stddef.h>
43 #include <string.h>
44 #include <ctype.h>
45 #include <inttypes.h>
46
47 #include "nasm.h"
48 #include "nasmlib.h"
49 #include "listing.h"
50
51 #define LIST_MAX_LEN 216        /* something sensible */
52 #define LIST_INDENT  40
53 #define LIST_HEXBIT  18
54
55 typedef struct MacroInhibit MacroInhibit;
56
57 static struct MacroInhibit {
58     MacroInhibit *next;
59     int level;
60     int inhibiting;
61 } *mistack;
62
63 static char xdigit[] = "0123456789ABCDEF";
64
65 #define HEX(a,b) (*(a)=xdigit[((b)>>4)&15],(a)[1]=xdigit[(b)&15]);
66
67 static char listline[LIST_MAX_LEN];
68 static bool listlinep;
69
70 static char listerror[LIST_MAX_LEN];
71
72 static char listdata[2 * LIST_INDENT];  /* we need less than that actually */
73 static int32_t listoffset;
74
75 static int32_t listlineno;
76
77 static int32_t listp;
78
79 static int suppress;            /* for INCBIN & TIMES special cases */
80
81 static int listlevel, listlevel_e;
82
83 static FILE *listfp;
84
85 static void list_emit(void)
86 {
87     int i;
88
89     if (!listlinep && !listdata[0])
90         return;
91
92     fprintf(listfp, "%6"PRId32" ", ++listlineno);
93
94     if (listdata[0])
95         fprintf(listfp, "%08"PRIX32" %-*s", listoffset, LIST_HEXBIT + 1,
96                 listdata);
97     else
98         fprintf(listfp, "%*s", LIST_HEXBIT + 10, "");
99
100     if (listlevel_e)
101         fprintf(listfp, "%s<%d>", (listlevel < 10 ? " " : ""),
102                 listlevel_e);
103     else if (listlinep)
104         fprintf(listfp, "    ");
105
106     if (listlinep)
107         fprintf(listfp, " %s", listline);
108
109     putc('\n', listfp);
110     listlinep = false;
111     listdata[0] = '\0';
112
113     if (listerror[0]) {
114         fprintf(listfp, "%6"PRId32"          ", ++listlineno);
115         for (i = 0; i < LIST_HEXBIT; i++)
116             putc('*', listfp);
117         
118         if (listlevel_e)
119             fprintf(listfp, " %s<%d>", (listlevel < 10 ? " " : ""),
120                     listlevel_e);
121         else
122             fprintf(listfp, "     ");
123
124         fprintf(listfp, "  %s\n", listerror);
125         listerror[0] = '\0';
126     }
127 }
128
129 static void list_init(char *fname, efunc error)
130 {
131     listfp = fopen(fname, "w");
132     if (!listfp) {
133         error(ERR_NONFATAL, "unable to open listing file `%s'",
134               fname);
135         return;
136     }
137
138     *listline = '\0';
139     listlineno = 0;
140     *listerror = '\0';
141     listp = true;
142     listlevel = 0;
143     suppress = 0;
144     mistack = nasm_malloc(sizeof(MacroInhibit));
145     mistack->next = NULL;
146     mistack->level = 0;
147     mistack->inhibiting = true;
148 }
149
150 static void list_cleanup(void)
151 {
152     if (!listp)
153         return;
154
155     while (mistack) {
156         MacroInhibit *temp = mistack;
157         mistack = temp->next;
158         nasm_free(temp);
159     }
160
161     list_emit();
162     fclose(listfp);
163 }
164
165 static void list_out(int32_t offset, char *str)
166 {
167     if (strlen(listdata) + strlen(str) > LIST_HEXBIT) {
168         strcat(listdata, "-");
169         list_emit();
170     }
171     if (!listdata[0])
172         listoffset = offset;
173     strcat(listdata, str);
174 }
175
176 static void list_address(int32_t offset, const char *brackets,
177                          int64_t addr, int size)
178 {
179     char q[20];
180     char *r = q;
181
182     nasm_assert(size <= 8);
183
184     *r++ = brackets[0];
185     while (size--) {
186         HEX(r, addr);
187         addr >>= 8;
188         r += 2;
189     }
190     *r++ = brackets[1];
191     *r = '\0';
192     list_out(offset, q);
193 }
194
195 static void list_output(int32_t offset, const void *data,
196                         enum out_type type, uint64_t size)
197 {
198     char q[20];
199
200     if (!listp || suppress || user_nolist)      /* fbk - 9/2/00 */
201         return;
202
203     switch (type) {
204     case OUT_RAWDATA:
205     {
206         uint8_t const *p = data;
207
208         if (size == 0 && !listdata[0])
209             listoffset = offset;
210         while (size--) {
211             HEX(q, *p);
212             q[2] = '\0';
213             list_out(offset++, q);
214             p++;
215         }
216         break;
217     }
218     case OUT_ADDRESS:
219         list_address(offset, "[]", *(int64_t *)data, size);
220         break;
221     case OUT_REL1ADR:
222         list_address(offset, "()", *(int64_t *)data, 1);
223         break;
224     case OUT_REL2ADR:
225         list_address(offset, "()", *(int64_t *)data, 2);
226         break;
227     case OUT_REL4ADR:
228         list_address(offset, "()", *(int64_t *)data, 4);
229         break;
230     case OUT_REL8ADR:
231         list_address(offset, "()", *(int64_t *)data, 8);
232         break;
233     case OUT_RESERVE:
234     {
235         snprintf(q, sizeof(q), "<res %08"PRIX64">", size);
236         list_out(offset, q);
237         break;
238     }
239     }
240 }
241
242 static void list_line(int type, char *line)
243 {
244     if (!listp)
245         return;
246     if (user_nolist) {          /* fbk - 9/2/00 */
247         listlineno++;
248         return;
249     }
250
251     if (mistack && mistack->inhibiting) {
252         if (type == LIST_MACRO)
253             return;
254         else {                  /* pop the m i stack */
255             MacroInhibit *temp = mistack;
256             mistack = temp->next;
257             nasm_free(temp);
258         }
259     }
260     list_emit();
261     listlinep = true;
262     strncpy(listline, line, LIST_MAX_LEN - 1);
263     listline[LIST_MAX_LEN - 1] = '\0';
264     listlevel_e = listlevel;
265 }
266
267 static void list_uplevel(int type)
268 {
269     if (!listp)
270         return;
271     if (type == LIST_INCBIN || type == LIST_TIMES) {
272         suppress |= (type == LIST_INCBIN ? 1 : 2);
273         list_out(listoffset, type == LIST_INCBIN ? "<incbin>" : "<rept>");
274         return;
275     }
276
277     listlevel++;
278
279     if (mistack && mistack->inhibiting && type == LIST_INCLUDE) {
280         MacroInhibit *temp = nasm_malloc(sizeof(MacroInhibit));
281         temp->next = mistack;
282         temp->level = listlevel;
283         temp->inhibiting = false;
284         mistack = temp;
285     } else if (type == LIST_MACRO_NOLIST) {
286         MacroInhibit *temp = nasm_malloc(sizeof(MacroInhibit));
287         temp->next = mistack;
288         temp->level = listlevel;
289         temp->inhibiting = true;
290         mistack = temp;
291     }
292 }
293
294 static void list_downlevel(int type)
295 {
296     if (!listp)
297         return;
298
299     if (type == LIST_INCBIN || type == LIST_TIMES) {
300         suppress &= ~(type == LIST_INCBIN ? 1 : 2);
301         return;
302     }
303
304     listlevel--;
305     while (mistack && mistack->level > listlevel) {
306         MacroInhibit *temp = mistack;
307         mistack = temp->next;
308         nasm_free(temp);
309     }
310 }
311
312 static void list_error(int severity, const char *pfx, const char *msg)
313 {
314     if (!listfp)
315         return;
316
317     snprintf(listerror, sizeof listerror, "%s%s", pfx, msg);
318
319     if ((severity & ERR_MASK) >= ERR_FATAL)
320         list_emit();
321 }
322
323
324 ListGen nasmlist = {
325     list_init,
326     list_cleanup,
327     list_output,
328     list_line,
329     list_uplevel,
330     list_downlevel,
331     list_error
332 };