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 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation, Inc.,
10 * 51 Franklin St, Fifth Floor, Boston MA 02110-1301, USA; version 2.1,
11 * or, at your option, any later version, incorporated herein by
14 * Patches submitted to this file are required to be dual licensed
15 * under the LGPL 2.1+ and the 2-clause BSD license:
17 * Copyright 1996-2009 the NASM Authors - All rights reserved.
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following
23 * * Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 * * Redistributions in binary form must reproduce the above
26 * copyright notice, this list of conditions and the following
27 * disclaimer in the documentation and/or other materials provided
28 * with the distribution.
30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
31 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
32 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
33 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
34 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
35 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
37 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
40 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
41 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
42 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 * ----------------------------------------------------------------------- */
47 * listing.c listing file generator for the Netwide Assembler
63 #define LIST_MAX_LEN 216 /* something sensible */
64 #define LIST_INDENT 40
65 #define LIST_HEXBIT 18
67 typedef struct MacroInhibit MacroInhibit
;
69 static struct MacroInhibit
{
75 static char xdigit
[] = "0123456789ABCDEF";
77 #define HEX(a,b) (*(a)=xdigit[((b)>>4)&15],(a)[1]=xdigit[(b)&15]);
79 static char listline
[LIST_MAX_LEN
];
82 static char listdata
[2 * LIST_INDENT
]; /* we need less than that actually */
83 static int32_t listoffset
;
85 static int32_t listlineno
;
89 static int suppress
; /* for INCBIN & TIMES special cases */
91 static int listlevel
, listlevel_e
;
95 static void list_emit(void)
97 if (!listlinep
&& !listdata
[0])
100 fprintf(listfp
, "%6"PRId32
" ", ++listlineno
);
103 fprintf(listfp
, "%08"PRIX32
" %-*s", listoffset
, LIST_HEXBIT
+ 1,
106 fprintf(listfp
, "%*s", LIST_HEXBIT
+ 10, "");
109 fprintf(listfp
, "%s<%d>", (listlevel
< 10 ? " " : ""),
112 fprintf(listfp
, " ");
115 fprintf(listfp
, " %s", listline
);
122 static void list_init(char *fname
, efunc error
)
124 listfp
= fopen(fname
, "w");
126 error(ERR_NONFATAL
, "unable to open listing file `%s'", fname
);
135 mistack
= nasm_malloc(sizeof(MacroInhibit
));
136 mistack
->next
= NULL
;
138 mistack
->inhibiting
= true;
141 static void list_cleanup(void)
147 MacroInhibit
*temp
= mistack
;
148 mistack
= temp
->next
;
156 static void list_out(int32_t offset
, char *str
)
158 if (strlen(listdata
) + strlen(str
) > LIST_HEXBIT
) {
159 strcat(listdata
, "-");
164 strcat(listdata
, str
);
167 static void list_output(int32_t offset
, const void *data
,
168 enum out_type type
, uint64_t size
)
170 if (!listp
|| suppress
|| user_nolist
) /* fbk - 9/2/00 */
176 uint8_t const *p
= data
;
178 if (size
== 0 && !listdata
[0])
183 list_out(offset
++, q
);
190 uint64_t d
= *(int64_t *)data
;
192 uint8_t p
[8], *r
= p
;
203 } else if (size
== 8) {
230 uint32_t d
= *(int32_t *)data
;
232 uint8_t p
[4], *r
= p
;
244 uint32_t d
= *(int32_t *)data
;
246 uint8_t p
[4], *r
= p
;
260 uint64_t d
= *(int64_t *)data
;
262 uint8_t p
[8], *r
= p
;
281 snprintf(q
, sizeof(q
), "<res %08"PRIX64
">", size
);
288 static void list_line(int type
, char *line
)
292 if (user_nolist
) { /* fbk - 9/2/00 */
297 if (mistack
&& mistack
->inhibiting
) {
298 if (type
== LIST_MACRO
)
300 else { /* pop the m i stack */
301 MacroInhibit
*temp
= mistack
;
302 mistack
= temp
->next
;
308 strncpy(listline
, line
, LIST_MAX_LEN
- 1);
309 listline
[LIST_MAX_LEN
- 1] = '\0';
310 listlevel_e
= listlevel
;
313 static void list_uplevel(int type
)
317 if (type
== LIST_INCBIN
|| type
== LIST_TIMES
) {
318 suppress
|= (type
== LIST_INCBIN
? 1 : 2);
319 list_out(listoffset
, type
== LIST_INCBIN
? "<incbin>" : "<rept>");
325 if (mistack
&& mistack
->inhibiting
&& type
== LIST_INCLUDE
) {
326 MacroInhibit
*temp
= nasm_malloc(sizeof(MacroInhibit
));
327 temp
->next
= mistack
;
328 temp
->level
= listlevel
;
329 temp
->inhibiting
= false;
331 } else if (type
== LIST_MACRO_NOLIST
) {
332 MacroInhibit
*temp
= nasm_malloc(sizeof(MacroInhibit
));
333 temp
->next
= mistack
;
334 temp
->level
= listlevel
;
335 temp
->inhibiting
= true;
340 static void list_downlevel(int type
)
345 if (type
== LIST_INCBIN
|| type
== LIST_TIMES
) {
346 suppress
&= ~(type
== LIST_INCBIN
? 1 : 2);
351 while (mistack
&& mistack
->level
> listlevel
) {
352 MacroInhibit
*temp
= mistack
;
353 mistack
= temp
->next
;