2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 /* listing.c listing file generator for the Netwide Assembler
27 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
28 * Julian Hall. All rights reserved. The software is
29 * redistributable under the licence given in the file "Licence"
30 * distributed in the NASM archive.
32 * initial version 2/vii/97 by Simon Tatham
45 #define LIST_MAX_LEN 216 /* something sensible */
46 #define LIST_INDENT 40
47 #define LIST_HEXBIT 18
49 typedef struct MacroInhibit MacroInhibit
;
51 static struct MacroInhibit
{
57 static char xdigit
[] = "0123456789ABCDEF";
59 #define HEX(a,b) (*(a)=xdigit[((b)>>4)&15],(a)[1]=xdigit[(b)&15]);
61 static char listline
[LIST_MAX_LEN
];
64 static char listdata
[2*LIST_INDENT
]; /* we need less than that actually */
65 static long listoffset
;
67 static long listlineno
;
71 static int suppress
; /* for INCBIN & TIMES special cases */
73 static int listlevel
, listlevel_e
;
77 static void list_emit (void) {
78 if (!listlinep
&& !listdata
[0])
80 fprintf(listfp
, "%6ld ", ++listlineno
);
82 fprintf(listfp
, "%08lX %-*s", listoffset
, LIST_HEXBIT
+1, listdata
);
84 fprintf(listfp
, "%*s", LIST_HEXBIT
+10, "");
86 fprintf(listfp
, "%s<%d>", (listlevel
< 10 ? " " : ""), listlevel_e
);
90 fprintf(listfp
, " %s", listline
);
96 static void list_init (char *fname
, efunc error
) {
97 listfp
= fopen (fname
, "w");
99 error (ERR_NONFATAL
, "unable to open listing file `%s'", fname
);
107 mistack
= nasm_malloc(sizeof(MacroInhibit
));
108 mistack
->next
= NULL
;
110 mistack
->inhibiting
= TRUE
;
113 static void list_cleanup (void) {
117 MacroInhibit
*temp
= mistack
;
118 mistack
= temp
->next
;
125 static void list_out (long offset
, char *str
) {
126 if (strlen(listdata
) + strlen(str
) > LIST_HEXBIT
) {
127 strcat(listdata
, "-");
132 strcat(listdata
, str
);
135 static void list_output (long offset
, void *data
, unsigned long type
) {
138 if (!listp
|| suppress
)
141 typ
= type
& OUT_TYPMASK
;
142 size
= type
& OUT_SIZMASK
;
144 if (typ
== OUT_RAWDATA
) {
145 unsigned char *p
= data
;
150 list_out (offset
++, q
);
153 } else if (typ
== OUT_ADDRESS
) {
154 unsigned long d
= *(long *)data
;
156 unsigned char p
[4], *r
= p
;
158 q
[0] = '['; q
[9] = ']'; q
[10] = '\0';
164 list_out (offset
, q
);
166 q
[0] = '['; q
[5] = ']'; q
[6] = '\0';
170 list_out (offset
, q
);
172 } else if (typ
== OUT_REL2ADR
) {
173 unsigned long d
= *(long *)data
;
175 unsigned char p
[4], *r
= p
;
176 q
[0] = '('; q
[5] = ')'; q
[6] = '\0';
180 list_out (offset
, q
);
181 } else if (typ
== OUT_REL4ADR
) {
182 unsigned long d
= *(long *)data
;
184 unsigned char p
[4], *r
= p
;
185 q
[0] = '('; q
[9] = ')'; q
[10] = '\0';
191 list_out (offset
, q
);
192 } else if (typ
== OUT_RESERVE
) {
194 sprintf(q
, "<res %08lX>", size
);
195 list_out (offset
, q
);
199 static void list_line (int type
, char *line
) {
202 if (mistack
&& mistack
->inhibiting
) {
203 if (type
== LIST_MACRO
)
205 else { /* pop the m i stack */
206 MacroInhibit
*temp
= mistack
;
207 mistack
= temp
->next
;
213 strncpy (listline
, line
, LIST_MAX_LEN
-1);
214 listline
[LIST_MAX_LEN
-1] = '\0';
215 listlevel_e
= listlevel
;
218 static void list_uplevel (int type
) {
221 if (type
== LIST_INCBIN
|| type
== LIST_TIMES
) {
222 suppress
|= (type
== LIST_INCBIN
? 1 : 2);
223 list_out (listoffset
, type
== LIST_INCBIN
? "<incbin>" : "<rept>");
227 if (mistack
&& mistack
->inhibiting
&& type
== LIST_INCLUDE
) {
228 MacroInhibit
*temp
= nasm_malloc(sizeof(MacroInhibit
));
229 temp
->next
= mistack
;
230 temp
->level
= listlevel
;
231 temp
->inhibiting
= FALSE
;
233 } else if (type
== LIST_MACRO_NOLIST
) {
234 MacroInhibit
*temp
= nasm_malloc(sizeof(MacroInhibit
));
235 temp
->next
= mistack
;
236 temp
->level
= listlevel
;
237 temp
->inhibiting
= TRUE
;
242 static void list_downlevel (int type
) {
245 if (type
== LIST_INCBIN
|| type
== LIST_TIMES
) {
246 suppress
&= ~(type
== LIST_INCBIN
? 1 : 2);
250 while (mistack
&& mistack
->level
> listlevel
) {
251 MacroInhibit
*temp
= mistack
;
252 mistack
= temp
->next
;