1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2020 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
48 #define LIST_MAX_LEN 1024 /* something sensible */
49 #define LIST_INDENT 40
50 #define LIST_HEXBIT 18
52 static const char xdigit
[] = "0123456789ABCDEF";
54 #define HEX(a,b) (*(a)=xdigit[((b)>>4)&15],(a)[1]=xdigit[(b)&15]);
56 uint64_t list_options
, active_list_options
;
58 static char listline
[LIST_MAX_LEN
];
59 static bool listlinep
;
61 static struct strlist
*list_errors
;
63 static char listdata
[2 * LIST_INDENT
]; /* we need less than that actually */
64 static int32_t listoffset
;
66 static int32_t listlineno
;
68 static int suppress
; /* for INCBIN & TIMES special cases */
70 static int listlevel
, listlevel_e
;
74 static void list_emit(void)
77 const struct strlist_entry
*e
;
79 if (listlinep
|| *listdata
) {
80 fprintf(listfp
, "%6"PRId32
" ", listlineno
);
83 fprintf(listfp
, "%08"PRIX32
" %-*s", listoffset
, LIST_HEXBIT
+ 1,
86 fprintf(listfp
, "%*s", LIST_HEXBIT
+ 10, "");
89 fprintf(listfp
, "%s<%d>", (listlevel
< 10 ? " " : ""),
95 fprintf(listfp
, " %s", listline
);
103 static const char fillchars
[] = " --***XX";
106 strlist_for_each(e
, list_errors
) {
107 fprintf(listfp
, "%6"PRId32
" ", listlineno
);
108 fillchar
= fillchars
[e
->pvt
.u
& ERR_MASK
];
109 for (i
= 0; i
< LIST_HEXBIT
; i
++)
110 putc(fillchar
, listfp
);
113 fprintf(listfp
, " %s<%d>", (listlevel
< 10 ? " " : ""),
116 fprintf(listfp
, " ");
118 fprintf(listfp
, " %s\n", e
->str
);
121 strlist_free(&list_errors
);
125 static void list_cleanup(void)
135 static void list_init(const char *fname
)
137 enum file_flags flags
= NF_TEXT
;
142 if (!fname
|| fname
[0] == '\0') {
147 if (list_option('w'))
150 listfp
= nasm_open_write(fname
, flags
);
152 nasm_nonfatal("unable to open listing file `%s'", fname
);
163 static void list_out(int64_t offset
, char *str
)
165 if (strlen(listdata
) + strlen(str
) > LIST_HEXBIT
) {
166 strcat(listdata
, "-");
171 strcat(listdata
, str
);
174 static void list_address(int64_t offset
, const char *brackets
,
175 int64_t addr
, int size
)
180 nasm_assert(size
<= 8);
193 static void list_size(int64_t offset
, const char *tag
, uint64_t size
)
198 if (list_option('d'))
199 fmt
= "<%s %"PRIu64
">";
201 fmt
= "<%s %"PRIX64
"h>";
203 snprintf(buf
, sizeof buf
, fmt
, tag
, size
);
204 list_out(offset
, buf
);
207 static void list_output(const struct out_data
*data
)
210 uint64_t size
= data
->size
;
211 uint64_t offset
= data
->offset
;
212 const uint8_t *p
= data
->data
;
215 if (!listfp
|| suppress
|| user_nolist
)
218 switch (data
->type
) {
221 list_size(offset
, "zero", size
);
231 listoffset
= data
->offset
;
236 list_out(offset
++, q
);
240 /* Used for listing on non-code generation passes with -Lp */
241 list_size(offset
, "len", size
);
246 list_address(offset
, "[]", data
->toffset
, size
);
250 memset(q
+1, 's', size
<< 1);
251 q
[(size
<< 1)+1] = ']';
252 q
[(size
<< 1)+2] = '\0';
257 list_address(offset
, "()", data
->toffset
, size
);
262 list_size(offset
, "res", size
);
264 memset(q
, '?', size
<< 1);
275 static void list_line(int type
, int32_t lineno
, const char *line
)
289 strlcpy(listline
, line
, LIST_MAX_LEN
-3);
290 memcpy(listline
+ LIST_MAX_LEN
-4, "...", 4);
291 listlevel_e
= listlevel
;
294 static void list_uplevel(int type
, int64_t size
)
302 list_size(listoffset
, "bin", size
);
307 list_size(listoffset
, "rep", size
);
320 static void list_downlevel(int type
)
340 static void list_error(errflags severity
, const char *fmt
, ...)
348 list_errors
= strlist_alloc(false);
351 strlist_vprintf(list_errors
, fmt
, ap
);
353 strlist_tail(list_errors
)->pvt
.u
= severity
;
355 if ((severity
& ERR_MASK
) >= ERR_FATAL
)
359 static void list_set_offset(uint64_t offset
)
364 static void list_update_options(const char *str
)
370 while ((c
= *str
++)) {
379 mask
= list_option_mask(c
);
381 list_options
|= mask
;
383 list_options
&= ~mask
;
389 enum directive_result
list_pragma(const struct pragma
*pragma
)
391 switch (pragma
->opcode
) {
393 list_update_options(pragma
->tail
);
401 static const struct lfmt nasm_list
= {
412 const struct lfmt
*lfmt
= &nasm_list
;