1 /* ----------------------------------------------------------------------- *
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.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
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
51 #define LIST_MAX_LEN 216 /* something sensible */
52 #define LIST_INDENT 40
53 #define LIST_HEXBIT 18
55 typedef struct MacroInhibit MacroInhibit
;
57 static struct MacroInhibit
{
63 static char xdigit
[] = "0123456789ABCDEF";
65 #define HEX(a,b) (*(a)=xdigit[((b)>>4)&15],(a)[1]=xdigit[(b)&15]);
67 static char listline
[LIST_MAX_LEN
];
70 static char listdata
[2 * LIST_INDENT
]; /* we need less than that actually */
71 static int32_t listoffset
;
73 static int32_t listlineno
;
77 static int suppress
; /* for INCBIN & TIMES special cases */
79 static int listlevel
, listlevel_e
;
83 static void list_emit(void)
85 if (!listlinep
&& !listdata
[0])
88 fprintf(listfp
, "%6"PRId32
" ", ++listlineno
);
91 fprintf(listfp
, "%08"PRIX32
" %-*s", listoffset
, LIST_HEXBIT
+ 1,
94 fprintf(listfp
, "%*s", LIST_HEXBIT
+ 10, "");
97 fprintf(listfp
, "%s<%d>", (listlevel
< 10 ? " " : ""),
100 fprintf(listfp
, " ");
103 fprintf(listfp
, " %s", listline
);
110 static void list_init(char *fname
, efunc error
)
112 listfp
= fopen(fname
, "w");
114 error(ERR_NONFATAL
, "unable to open listing file `%s'", fname
);
123 mistack
= nasm_malloc(sizeof(MacroInhibit
));
124 mistack
->next
= NULL
;
126 mistack
->inhibiting
= true;
129 static void list_cleanup(void)
135 MacroInhibit
*temp
= mistack
;
136 mistack
= temp
->next
;
144 static void list_out(int32_t offset
, char *str
)
146 if (strlen(listdata
) + strlen(str
) > LIST_HEXBIT
) {
147 strcat(listdata
, "-");
152 strcat(listdata
, str
);
155 static void list_output(int32_t offset
, const void *data
,
156 enum out_type type
, uint64_t size
)
158 if (!listp
|| suppress
|| user_nolist
) /* fbk - 9/2/00 */
164 uint8_t const *p
= data
;
166 if (size
== 0 && !listdata
[0])
171 list_out(offset
++, q
);
178 uint64_t d
= *(int64_t *)data
;
180 uint8_t p
[8], *r
= p
;
191 } else if (size
== 8) {
218 uint32_t d
= *(int32_t *)data
;
220 uint8_t p
[4], *r
= p
;
232 uint32_t d
= *(int32_t *)data
;
234 uint8_t p
[4], *r
= p
;
248 uint64_t d
= *(int64_t *)data
;
250 uint8_t p
[8], *r
= p
;
269 snprintf(q
, sizeof(q
), "<res %08"PRIX64
">", size
);
276 static void list_line(int type
, char *line
)
280 if (user_nolist
) { /* fbk - 9/2/00 */
285 if (mistack
&& mistack
->inhibiting
) {
286 if (type
== LIST_MACRO
)
288 else { /* pop the m i stack */
289 MacroInhibit
*temp
= mistack
;
290 mistack
= temp
->next
;
296 strncpy(listline
, line
, LIST_MAX_LEN
- 1);
297 listline
[LIST_MAX_LEN
- 1] = '\0';
298 listlevel_e
= listlevel
;
301 static void list_uplevel(int type
)
305 if (type
== LIST_INCBIN
|| type
== LIST_TIMES
) {
306 suppress
|= (type
== LIST_INCBIN
? 1 : 2);
307 list_out(listoffset
, type
== LIST_INCBIN
? "<incbin>" : "<rept>");
313 if (mistack
&& mistack
->inhibiting
&& type
== LIST_INCLUDE
) {
314 MacroInhibit
*temp
= nasm_malloc(sizeof(MacroInhibit
));
315 temp
->next
= mistack
;
316 temp
->level
= listlevel
;
317 temp
->inhibiting
= false;
319 } else if (type
== LIST_MACRO_NOLIST
) {
320 MacroInhibit
*temp
= nasm_malloc(sizeof(MacroInhibit
));
321 temp
->next
= mistack
;
322 temp
->level
= listlevel
;
323 temp
->inhibiting
= true;
328 static void list_downlevel(int type
)
333 if (type
== LIST_INCBIN
|| type
== LIST_TIMES
) {
334 suppress
&= ~(type
== LIST_INCBIN
? 1 : 2);
339 while (mistack
&& mistack
->level
> listlevel
) {
340 MacroInhibit
*temp
= mistack
;
341 mistack
= temp
->next
;