2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
24 /* listing.c listing file generator for the Netwide Assembler
26 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
27 * Julian Hall. All rights reserved. The software is
28 * redistributable under the licence given in the file "Licence"
29 * distributed in the NASM archive.
31 * initial version 2/vii/97 by Simon Tatham
44 #define LIST_MAX_LEN 216 /* something sensible */
45 #define LIST_INDENT 40
46 #define LIST_HEXBIT 18
48 typedef struct MacroInhibit MacroInhibit
;
50 static struct MacroInhibit
{
56 static char xdigit
[] = "0123456789ABCDEF";
58 #define HEX(a,b) (*(a)=xdigit[((b)>>4)&15],(a)[1]=xdigit[(b)&15]);
60 static char listline
[LIST_MAX_LEN
];
63 static char listdata
[2*LIST_INDENT
]; /* we need less than that actually */
64 static long listoffset
;
66 static long listlineno
;
70 static int suppress
; /* for INCBIN & TIMES special cases */
72 static int listlevel
, listlevel_e
;
76 static void list_emit (void) {
77 if (!listlinep
&& !listdata
[0])
79 fprintf(listfp
, "%6ld ", ++listlineno
);
81 fprintf(listfp
, "%08lX %-*s", listoffset
, LIST_HEXBIT
+1, listdata
);
83 fprintf(listfp
, "%*s", LIST_HEXBIT
+10, "");
85 fprintf(listfp
, "%s<%d>", (listlevel
< 10 ? " " : ""), listlevel_e
);
89 fprintf(listfp
, " %s", listline
);
95 static void list_init (char *fname
, efunc error
) {
96 listfp
= fopen (fname
, "w");
98 error (ERR_NONFATAL
, "unable to open listing file `%s'", fname
);
106 mistack
= nasm_malloc(sizeof(MacroInhibit
));
107 mistack
->next
= NULL
;
109 mistack
->inhibiting
= TRUE
;
112 static void list_cleanup (void) {
116 MacroInhibit
*temp
= mistack
;
117 mistack
= temp
->next
;
124 static void list_out (long offset
, char *str
) {
125 if (strlen(listdata
) + strlen(str
) > LIST_HEXBIT
) {
126 strcat(listdata
, "-");
131 strcat(listdata
, str
);
134 static void list_output (long offset
, void *data
, unsigned long type
) {
137 if (!listp
|| suppress
)
140 typ
= type
& OUT_TYPMASK
;
141 size
= type
& OUT_SIZMASK
;
143 if (typ
== OUT_RAWDATA
) {
144 unsigned char *p
= data
;
149 list_out (offset
++, q
);
152 } else if (typ
== OUT_ADDRESS
) {
153 unsigned long d
= *(long *)data
;
155 unsigned char p
[4], *r
= p
;
157 q
[0] = '['; q
[9] = ']'; q
[10] = '\0';
163 list_out (offset
, q
);
165 q
[0] = '['; q
[5] = ']'; q
[6] = '\0';
169 list_out (offset
, q
);
171 } else if (typ
== OUT_REL2ADR
) {
172 unsigned long d
= *(long *)data
;
174 unsigned char p
[4], *r
= p
;
175 q
[0] = '('; q
[5] = ')'; q
[6] = '\0';
179 list_out (offset
, q
);
180 } 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
);
191 } else if (typ
== OUT_RESERVE
) {
193 sprintf(q
, "<res %08lX>", size
);
194 list_out (offset
, q
);
198 static void list_line (int type
, char *line
) {
201 if (mistack
&& mistack
->inhibiting
) {
202 if (type
== LIST_MACRO
)
204 else { /* pop the m i stack */
205 MacroInhibit
*temp
= mistack
;
206 mistack
= temp
->next
;
212 strncpy (listline
, line
, LIST_MAX_LEN
-1);
213 listline
[LIST_MAX_LEN
-1] = '\0';
214 listlevel_e
= listlevel
;
217 static void list_uplevel (int type
) {
220 if (type
== LIST_INCBIN
|| type
== LIST_TIMES
) {
221 suppress
|= (type
== LIST_INCBIN
? 1 : 2);
222 list_out (listoffset
, type
== LIST_INCBIN
? "<incbin>" : "<rept>");
226 if (mistack
&& mistack
->inhibiting
&& type
== LIST_INCLUDE
) {
227 MacroInhibit
*temp
= nasm_malloc(sizeof(MacroInhibit
));
228 temp
->next
= mistack
;
229 temp
->level
= listlevel
;
230 temp
->inhibiting
= FALSE
;
232 } else if (type
== LIST_MACRO_NOLIST
) {
233 MacroInhibit
*temp
= nasm_malloc(sizeof(MacroInhibit
));
234 temp
->next
= mistack
;
235 temp
->level
= listlevel
;
236 temp
->inhibiting
= TRUE
;
241 static void list_downlevel (int type
) {
244 if (type
== LIST_INCBIN
|| type
== LIST_TIMES
) {
245 suppress
&= ~(type
== LIST_INCBIN
? 1 : 2);
249 while (mistack
&& mistack
->level
> listlevel
) {
250 MacroInhibit
*temp
= mistack
;
251 mistack
= temp
->next
;