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, listdata
);
63 fprintf(listfp
, "%*s", LIST_HEXBIT
+10, "");
66 fprintf(listfp
, "%s<%d>", (listlevel
< 10 ? " " : ""), listlevel_e
);
71 fprintf(listfp
, " %s", listline
);
78 static void list_init (char *fname
, efunc error
)
80 listfp
= fopen (fname
, "w");
82 error (ERR_NONFATAL
, "unable to open listing file `%s'", fname
);
91 mistack
= nasm_malloc(sizeof(MacroInhibit
));
94 mistack
->inhibiting
= TRUE
;
97 static void list_cleanup (void)
103 MacroInhibit
*temp
= mistack
;
104 mistack
= temp
->next
;
112 static void list_out (long offset
, char *str
)
114 if (strlen(listdata
) + strlen(str
) > LIST_HEXBIT
) {
115 strcat(listdata
, "-");
120 strcat(listdata
, str
);
123 static void list_output (long offset
, const void *data
, unsigned long type
)
125 unsigned long typ
, size
;
127 if (!listp
|| suppress
|| user_nolist
) /* fbk - 9/2/00 */
130 typ
= type
& OUT_TYPMASK
;
131 size
= type
& OUT_SIZMASK
;
133 if (typ
== OUT_RAWDATA
)
135 unsigned char const *p
= data
;
141 list_out (offset
++, q
);
145 else if (typ
== OUT_ADDRESS
)
147 unsigned long d
= *(long *)data
;
149 unsigned char p
[4], *r
= p
;
152 q
[0] = '['; q
[9] = ']'; q
[10] = '\0';
158 list_out (offset
, q
);
161 q
[0] = '['; q
[5] = ']'; q
[6] = '\0';
165 list_out (offset
, q
);
168 else if (typ
== OUT_REL2ADR
)
170 unsigned long d
= *(long *)data
;
172 unsigned char p
[4], *r
= p
;
173 q
[0] = '('; q
[5] = ')'; q
[6] = '\0';
177 list_out (offset
, q
);
179 else if (typ
== OUT_REL4ADR
)
181 unsigned long d
= *(long *)data
;
183 unsigned char p
[4], *r
= p
;
184 q
[0] = '('; q
[9] = ')'; q
[10] = '\0';
190 list_out (offset
, q
);
192 else if (typ
== OUT_RESERVE
)
195 sprintf(q
, "<res %08lX>", size
);
196 list_out (offset
, q
);
200 static void list_line (int type
, char *line
)
204 if (user_nolist
){ /* fbk - 9/2/00 */
209 if (mistack
&& mistack
->inhibiting
)
211 if (type
== LIST_MACRO
)
213 else { /* pop the m i stack */
214 MacroInhibit
*temp
= mistack
;
215 mistack
= temp
->next
;
221 strncpy (listline
, line
, LIST_MAX_LEN
-1);
222 listline
[LIST_MAX_LEN
-1] = '\0';
223 listlevel_e
= listlevel
;
226 static void list_uplevel (int type
)
230 if (type
== LIST_INCBIN
|| type
== LIST_TIMES
)
232 suppress
|= (type
== LIST_INCBIN
? 1 : 2);
233 list_out (listoffset
, type
== LIST_INCBIN
? "<incbin>" : "<rept>");
239 if (mistack
&& mistack
->inhibiting
&& type
== LIST_INCLUDE
)
241 MacroInhibit
*temp
= nasm_malloc(sizeof(MacroInhibit
));
242 temp
->next
= mistack
;
243 temp
->level
= listlevel
;
244 temp
->inhibiting
= FALSE
;
247 else if (type
== LIST_MACRO_NOLIST
)
249 MacroInhibit
*temp
= nasm_malloc(sizeof(MacroInhibit
));
250 temp
->next
= mistack
;
251 temp
->level
= listlevel
;
252 temp
->inhibiting
= TRUE
;
257 static void list_downlevel (int type
)
262 if (type
== LIST_INCBIN
|| type
== LIST_TIMES
)
264 suppress
&= ~(type
== LIST_INCBIN
? 1 : 2);
269 while (mistack
&& mistack
->level
> listlevel
)
271 MacroInhibit
*temp
= mistack
;
272 mistack
= temp
->next
;