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
22 #define LIST_MAX_LEN 216 /* something sensible */
23 #define LIST_INDENT 40
24 #define LIST_HEXBIT 18
26 typedef struct MacroInhibit MacroInhibit;
28 static struct MacroInhibit {
34 static char xdigit[] = "0123456789ABCDEF";
36 #define HEX(a,b) (*(a)=xdigit[((b)>>4)&15],(a)[1]=xdigit[(b)&15]);
38 static char listline[LIST_MAX_LEN];
41 static char listdata[2 * LIST_INDENT]; /* we need less than that actually */
42 static int32_t listoffset;
44 static int32_t listlineno;
48 static int suppress; /* for INCBIN & TIMES special cases */
50 static int listlevel, listlevel_e;
54 static void list_emit(void)
56 if (!listlinep && !listdata[0])
59 fprintf(listfp, "%6"PRId32" ", ++listlineno);
62 fprintf(listfp, "%08"PRIX32" %-*s", listoffset, LIST_HEXBIT + 1,
65 fprintf(listfp, "%*s", LIST_HEXBIT + 10, "");
68 fprintf(listfp, "%s<%d>", (listlevel < 10 ? " " : ""),
74 fprintf(listfp, " %s", listline);
81 static void list_init(char *fname, efunc error)
83 listfp = fopen(fname, "w");
85 error(ERR_NONFATAL, "unable to open listing file `%s'", fname);
94 mistack = nasm_malloc(sizeof(MacroInhibit));
97 mistack->inhibiting = TRUE;
100 static void list_cleanup(void)
106 MacroInhibit *temp = mistack;
107 mistack = temp->next;
115 static void list_out(int32_t offset, char *str)
117 if (strlen(listdata) + strlen(str) > LIST_HEXBIT) {
118 strcat(listdata, "-");
123 strcat(listdata, str);
126 static void list_output(int32_t offset, const void *data, uint32_t type)
130 if (!listp || suppress || user_nolist) /* fbk - 9/2/00 */
133 typ = type & OUT_TYPMASK;
134 size = type & OUT_SIZMASK;
137 if (typ == OUT_RAWDATA) {
138 uint8_t const *p = data;
143 list_out(offset++, q);
146 } else if (typ == OUT_ADDRESS) {
147 uint64_t d = *(int64_t *)data;
149 uint8_t p[8], *r = p;
160 } else if (size == 8) {
183 } else if (typ == OUT_REL2ADR) {
184 uint32_t d = *(int32_t *)data;
186 uint8_t p[4], *r = p;
194 } else if (typ == OUT_REL4ADR) {
195 uint32_t d = *(int32_t *)data;
197 uint8_t p[4], *r = p;
207 } else if (typ == OUT_RESERVE) {
209 snprintf(q, sizeof(q), "<res %08"PRIX32">", size);
214 static void list_line(int type, char *line)
218 if (user_nolist) { /* fbk - 9/2/00 */
223 if (mistack && mistack->inhibiting) {
224 if (type == LIST_MACRO)
226 else { /* pop the m i stack */
227 MacroInhibit *temp = mistack;
228 mistack = temp->next;
234 strncpy(listline, line, LIST_MAX_LEN - 1);
235 listline[LIST_MAX_LEN - 1] = '\0';
236 listlevel_e = listlevel;
239 static void list_uplevel(int type)
243 if (type == LIST_INCBIN || type == LIST_TIMES) {
244 suppress |= (type == LIST_INCBIN ? 1 : 2);
245 list_out(listoffset, type == LIST_INCBIN ? "<incbin>" : "<rept>");
251 if (mistack && mistack->inhibiting && type == LIST_INCLUDE) {
252 MacroInhibit *temp = nasm_malloc(sizeof(MacroInhibit));
253 temp->next = mistack;
254 temp->level = listlevel;
255 temp->inhibiting = FALSE;
257 } else if (type == LIST_MACRO_NOLIST) {
258 MacroInhibit *temp = nasm_malloc(sizeof(MacroInhibit));
259 temp->next = mistack;
260 temp->level = listlevel;
261 temp->inhibiting = TRUE;
266 static void list_downlevel(int type)
271 if (type == LIST_INCBIN || type == LIST_TIMES) {
272 suppress &= ~(type == LIST_INCBIN ? 1 : 2);
277 while (mistack && mistack->level > listlevel) {
278 MacroInhibit *temp = mistack;
279 mistack = temp->next;