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
21 #define LIST_MAX_LEN 216 /* something sensible */
22 #define LIST_INDENT 40
23 #define LIST_HEXBIT 18
25 typedef struct MacroInhibit MacroInhibit
;
27 static struct MacroInhibit
{
33 static char xdigit
[] = "0123456789ABCDEF";
35 #define HEX(a,b) (*(a)=xdigit[((b)>>4)&15],(a)[1]=xdigit[(b)&15]);
37 static char listline
[LIST_MAX_LEN
];
40 static char listdata
[2 * LIST_INDENT
]; /* we need less than that actually */
41 static long listoffset
;
43 static long listlineno
;
47 static int suppress
; /* for INCBIN & TIMES special cases */
49 static int listlevel
, listlevel_e
;
53 static void list_emit(void)
55 if (!listlinep
&& !listdata
[0])
58 fprintf(listfp
, "%6ld ", ++listlineno
);
61 fprintf(listfp
, "%08lX %-*s", listoffset
, LIST_HEXBIT
+ 1,
64 fprintf(listfp
, "%*s", LIST_HEXBIT
+ 10, "");
67 fprintf(listfp
, "%s<%d>", (listlevel
< 10 ? " " : ""),
73 fprintf(listfp
, " %s", listline
);
80 static void list_init(char *fname
, efunc error
)
82 listfp
= fopen(fname
, "w");
84 error(ERR_NONFATAL
, "unable to open listing file `%s'", fname
);
93 mistack
= nasm_malloc(sizeof(MacroInhibit
));
96 mistack
->inhibiting
= TRUE
;
99 static void list_cleanup(void)
105 MacroInhibit
*temp
= mistack
;
106 mistack
= temp
->next
;
114 static void list_out(long offset
, char *str
)
116 if (strlen(listdata
) + strlen(str
) > LIST_HEXBIT
) {
117 strcat(listdata
, "-");
122 strcat(listdata
, str
);
125 static void list_output(long offset
, const void *data
, unsigned long type
)
127 unsigned long typ
, size
;
129 if (!listp
|| suppress
|| user_nolist
) /* fbk - 9/2/00 */
132 typ
= type
& OUT_TYPMASK
;
133 size
= type
& OUT_SIZMASK
;
135 if (typ
== OUT_RAWDATA
) {
136 unsigned char const *p
= data
;
141 list_out(offset
++, q
);
144 } else if (typ
== OUT_ADDRESS
) {
145 unsigned long d
= *(long *)data
;
147 unsigned char p
[4], *r
= p
;
167 } else if (typ
== OUT_REL2ADR
) {
168 unsigned long d
= *(long *)data
;
170 unsigned char p
[4], *r
= p
;
178 } else if (typ
== OUT_REL4ADR
) {
179 unsigned long d
= *(long *)data
;
181 unsigned char p
[4], *r
= p
;
191 } else if (typ
== OUT_RESERVE
) {
193 snprintf(q
, sizeof(q
), "<res %08lX>", size
);
198 static void list_line(int type
, char *line
)
202 if (user_nolist
) { /* fbk - 9/2/00 */
207 if (mistack
&& mistack
->inhibiting
) {
208 if (type
== LIST_MACRO
)
210 else { /* pop the m i stack */
211 MacroInhibit
*temp
= mistack
;
212 mistack
= temp
->next
;
218 strncpy(listline
, line
, LIST_MAX_LEN
- 1);
219 listline
[LIST_MAX_LEN
- 1] = '\0';
220 listlevel_e
= listlevel
;
223 static void list_uplevel(int type
)
227 if (type
== LIST_INCBIN
|| type
== LIST_TIMES
) {
228 suppress
|= (type
== LIST_INCBIN
? 1 : 2);
229 list_out(listoffset
, type
== LIST_INCBIN
? "<incbin>" : "<rept>");
235 if (mistack
&& mistack
->inhibiting
&& type
== LIST_INCLUDE
) {
236 MacroInhibit
*temp
= nasm_malloc(sizeof(MacroInhibit
));
237 temp
->next
= mistack
;
238 temp
->level
= listlevel
;
239 temp
->inhibiting
= FALSE
;
241 } else if (type
== LIST_MACRO_NOLIST
) {
242 MacroInhibit
*temp
= nasm_malloc(sizeof(MacroInhibit
));
243 temp
->next
= mistack
;
244 temp
->level
= listlevel
;
245 temp
->inhibiting
= TRUE
;
250 static void list_downlevel(int type
)
255 if (type
== LIST_INCBIN
|| type
== LIST_TIMES
) {
256 suppress
&= ~(type
== LIST_INCBIN
? 1 : 2);
261 while (mistack
&& mistack
->level
> listlevel
) {
262 MacroInhibit
*temp
= mistack
;
263 mistack
= temp
->next
;