1 /* listing.c listing file generator for the Netwide Assembler
3 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
4 * Julian Hall. All rights reserved. The software is
5 * redistributable under the licence given in the file "Licence"
6 * distributed in the NASM archive.
8 * initial version 2/vii/97 by Simon Tatham
24 #define LIST_MAX_LEN 216 /* something sensible */
25 #define LIST_INDENT 40
26 #define LIST_HEXBIT 18
28 typedef struct MacroInhibit MacroInhibit
;
30 static struct MacroInhibit
{
36 static char xdigit
[] = "0123456789ABCDEF";
38 #define HEX(a,b) (*(a)=xdigit[((b)>>4)&15],(a)[1]=xdigit[(b)&15]);
40 static char listline
[LIST_MAX_LEN
];
43 static char listdata
[2 * LIST_INDENT
]; /* we need less than that actually */
44 static int32_t listoffset
;
46 static int32_t listlineno
;
50 static int suppress
; /* for INCBIN & TIMES special cases */
52 static int listlevel
, listlevel_e
;
56 static void list_emit(void)
58 if (!listlinep
&& !listdata
[0])
61 fprintf(listfp
, "%6"PRId32
" ", ++listlineno
);
64 fprintf(listfp
, "%08"PRIX32
" %-*s", listoffset
, LIST_HEXBIT
+ 1,
67 fprintf(listfp
, "%*s", LIST_HEXBIT
+ 10, "");
70 fprintf(listfp
, "%s<%d>", (listlevel
< 10 ? " " : ""),
76 fprintf(listfp
, " %s", listline
);
83 static void list_init(char *fname
, efunc error
)
85 listfp
= fopen(fname
, "w");
87 error(ERR_NONFATAL
, "unable to open listing file `%s'", fname
);
96 mistack
= nasm_malloc(sizeof(MacroInhibit
));
99 mistack
->inhibiting
= true;
102 static void list_cleanup(void)
108 MacroInhibit
*temp
= mistack
;
109 mistack
= temp
->next
;
117 static void list_out(int32_t offset
, char *str
)
119 if (strlen(listdata
) + strlen(str
) > LIST_HEXBIT
) {
120 strcat(listdata
, "-");
125 strcat(listdata
, str
);
128 static void list_output(int32_t offset
, const void *data
,
129 enum out_type type
, uint64_t size
)
131 if (!listp
|| suppress
|| user_nolist
) /* fbk - 9/2/00 */
137 uint8_t const *p
= data
;
142 list_out(offset
++, q
);
149 uint64_t d
= *(int64_t *)data
;
151 uint8_t p
[8], *r
= p
;
162 } else if (size
== 8) {
189 uint32_t d
= *(int32_t *)data
;
191 uint8_t p
[4], *r
= p
;
203 uint32_t d
= *(int32_t *)data
;
205 uint8_t p
[4], *r
= p
;
219 uint64_t d
= *(int64_t *)data
;
221 uint8_t p
[4], *r
= p
;
240 snprintf(q
, sizeof(q
), "<res %08"PRIX64
">", size
);
247 static void list_line(int type
, char *line
)
251 if (user_nolist
) { /* fbk - 9/2/00 */
256 if (mistack
&& mistack
->inhibiting
) {
257 if (type
== LIST_MACRO
)
259 else { /* pop the m i stack */
260 MacroInhibit
*temp
= mistack
;
261 mistack
= temp
->next
;
267 strncpy(listline
, line
, LIST_MAX_LEN
- 1);
268 listline
[LIST_MAX_LEN
- 1] = '\0';
269 listlevel_e
= listlevel
;
272 static void list_uplevel(int type
)
276 if (type
== LIST_INCBIN
|| type
== LIST_TIMES
) {
277 suppress
|= (type
== LIST_INCBIN
? 1 : 2);
278 list_out(listoffset
, type
== LIST_INCBIN
? "<incbin>" : "<rept>");
284 if (mistack
&& mistack
->inhibiting
&& type
== LIST_INCLUDE
) {
285 MacroInhibit
*temp
= nasm_malloc(sizeof(MacroInhibit
));
286 temp
->next
= mistack
;
287 temp
->level
= listlevel
;
288 temp
->inhibiting
= false;
290 } else if (type
== LIST_MACRO_NOLIST
) {
291 MacroInhibit
*temp
= nasm_malloc(sizeof(MacroInhibit
));
292 temp
->next
= mistack
;
293 temp
->level
= listlevel
;
294 temp
->inhibiting
= true;
299 static void list_downlevel(int type
)
304 if (type
== LIST_INCBIN
|| type
== LIST_TIMES
) {
305 suppress
&= ~(type
== LIST_INCBIN
? 1 : 2);
310 while (mistack
&& mistack
->level
> listlevel
) {
311 MacroInhibit
*temp
= mistack
;
312 mistack
= temp
->next
;