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) {
54 if (!listlinep
&& !listdata
[0])
56 fprintf(listfp
, "%6ld ", ++listlineno
);
58 fprintf(listfp
, "%08lX %-*s", listoffset
, LIST_HEXBIT
+1, listdata
);
60 fprintf(listfp
, "%*s", LIST_HEXBIT
+10, "");
62 fprintf(listfp
, "%s<%d>", (listlevel
< 10 ? " " : ""), listlevel_e
);
66 fprintf(listfp
, " %s", listline
);
72 static void list_init (char *fname
, efunc error
) {
73 listfp
= fopen (fname
, "w");
75 error (ERR_NONFATAL
, "unable to open listing file `%s'", fname
);
83 mistack
= nasm_malloc(sizeof(MacroInhibit
));
86 mistack
->inhibiting
= TRUE
;
89 static void list_cleanup (void) {
93 MacroInhibit
*temp
= mistack
;
101 static void list_out (long offset
, char *str
) {
102 if (strlen(listdata
) + strlen(str
) > LIST_HEXBIT
) {
103 strcat(listdata
, "-");
108 strcat(listdata
, str
);
111 static void list_output (long offset
, void *data
, unsigned long type
) {
114 if (!listp
|| suppress
)
117 typ
= type
& OUT_TYPMASK
;
118 size
= type
& OUT_SIZMASK
;
120 if (typ
== OUT_RAWDATA
) {
121 unsigned char *p
= data
;
126 list_out (offset
++, q
);
129 } else if (typ
== OUT_ADDRESS
) {
130 unsigned long d
= *(long *)data
;
132 unsigned char p
[4], *r
= p
;
134 q
[0] = '['; q
[9] = ']'; q
[10] = '\0';
140 list_out (offset
, q
);
142 q
[0] = '['; q
[5] = ']'; q
[6] = '\0';
146 list_out (offset
, q
);
148 } else if (typ
== OUT_REL2ADR
) {
149 unsigned long d
= *(long *)data
;
151 unsigned char p
[4], *r
= p
;
152 q
[0] = '('; q
[5] = ')'; q
[6] = '\0';
156 list_out (offset
, q
);
157 } else if (typ
== OUT_REL4ADR
) {
158 unsigned long d
= *(long *)data
;
160 unsigned char p
[4], *r
= p
;
161 q
[0] = '('; q
[9] = ')'; q
[10] = '\0';
167 list_out (offset
, q
);
168 } else if (typ
== OUT_RESERVE
) {
170 sprintf(q
, "<res %08lX>", size
);
171 list_out (offset
, q
);
175 static void list_line (int type
, char *line
) {
178 if (mistack
&& mistack
->inhibiting
) {
179 if (type
== LIST_MACRO
)
181 else { /* pop the m i stack */
182 MacroInhibit
*temp
= mistack
;
183 mistack
= temp
->next
;
189 strncpy (listline
, line
, LIST_MAX_LEN
-1);
190 listline
[LIST_MAX_LEN
-1] = '\0';
191 listlevel_e
= listlevel
;
194 static void list_uplevel (int type
) {
197 if (type
== LIST_INCBIN
|| type
== LIST_TIMES
) {
198 suppress
|= (type
== LIST_INCBIN
? 1 : 2);
199 list_out (listoffset
, type
== LIST_INCBIN
? "<incbin>" : "<rept>");
203 if (mistack
&& mistack
->inhibiting
&& type
== LIST_INCLUDE
) {
204 MacroInhibit
*temp
= nasm_malloc(sizeof(MacroInhibit
));
205 temp
->next
= mistack
;
206 temp
->level
= listlevel
;
207 temp
->inhibiting
= FALSE
;
209 } else if (type
== LIST_MACRO_NOLIST
) {
210 MacroInhibit
*temp
= nasm_malloc(sizeof(MacroInhibit
));
211 temp
->next
= mistack
;
212 temp
->level
= listlevel
;
213 temp
->inhibiting
= TRUE
;
218 static void list_downlevel (int type
) {
221 if (type
== LIST_INCBIN
|| type
== LIST_TIMES
) {
222 suppress
&= ~(type
== LIST_INCBIN
? 1 : 2);
226 while (mistack
&& mistack
->level
> listlevel
) {
227 MacroInhibit
*temp
= mistack
;
228 mistack
= temp
->next
;