1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
/* listing.c listing file generator for the Netwide Assembler
*
* The Netwide Assembler is copyright (C) 1996 Simon Tatham and
* Julian Hall. All rights reserved. The software is
* redistributable under the licence given in the file "Licence"
* distributed in the NASM archive.
*
* initial version 2/vii/97 by Simon Tatham
*/
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <ctype.h>
#include "nasm.h"
#include "nasmlib.h"
#include "listing.h"
#define LIST_MAX_LEN 216 /* something sensible */
#define LIST_INDENT 40
#define LIST_HEXBIT 18
typedef struct MacroInhibit MacroInhibit;
static struct MacroInhibit {
MacroInhibit *next;
int level;
int inhibiting;
} *mistack;
static char xdigit[] = "0123456789ABCDEF";
#define HEX(a,b) (*(a)=xdigit[((b)>>4)&15],(a)[1]=xdigit[(b)&15]);
static char listline[LIST_MAX_LEN];
static int listlinep;
static char listdata[2*LIST_INDENT]; /* we need less than that actually */
static long listoffset;
static long listlineno;
static long listp;
static int suppress; /* for INCBIN & TIMES special cases */
static int listlevel, listlevel_e;
static FILE *listfp;
static void list_emit (void)
{
if (!listlinep && !listdata[0])
return;
fprintf(listfp, "%6ld ", ++listlineno);
if (listdata[0])
fprintf(listfp, "%08lX %-*s", listoffset, LIST_HEXBIT+1, listdata);
else
fprintf(listfp, "%*s", LIST_HEXBIT+10, "");
if (listlevel_e)
fprintf(listfp, "%s<%d>", (listlevel < 10 ? " " : ""), listlevel_e);
else if (listlinep)
fprintf(listfp, " ");
if (listlinep)
fprintf(listfp, " %s", listline);
fputc('\n', listfp);
listlinep = FALSE;
listdata[0] = '\0';
}
static void list_init (char *fname, efunc error)
{
listfp = fopen (fname, "w");
if (!listfp) {
error (ERR_NONFATAL, "unable to open listing file `%s'", fname);
return;
}
*listline = '\0';
listlineno = 0;
listp = TRUE;
listlevel = 0;
suppress = 0;
mistack = nasm_malloc(sizeof(MacroInhibit));
mistack->next = NULL;
mistack->level = 0;
mistack->inhibiting = TRUE;
}
static void list_cleanup (void)
{
if (!listp)
return;
while (mistack) {
MacroInhibit *temp = mistack;
mistack = temp->next;
nasm_free (temp);
}
list_emit();
fclose (listfp);
}
static void list_out (long offset, char *str)
{
if (strlen(listdata) + strlen(str) > LIST_HEXBIT) {
strcat(listdata, "-");
list_emit();
}
if (!listdata[0])
listoffset = offset;
strcat(listdata, str);
}
static void list_output (long offset, const void *data, unsigned long type)
{
unsigned long typ, size;
if (!listp || suppress || user_nolist) /* fbk - 9/2/00 */
return;
typ = type & OUT_TYPMASK;
size = type & OUT_SIZMASK;
if (typ == OUT_RAWDATA)
{
unsigned char const *p = data;
char q[3];
while (size--)
{
HEX (q, *p);
q[2] = '\0';
list_out (offset++, q);
p++;
}
}
else if (typ == OUT_ADDRESS)
{
unsigned long d = *(long *)data;
char q[11];
unsigned char p[4], *r = p;
if (size == 4)
{
q[0] = '['; q[9] = ']'; q[10] = '\0';
WRITELONG (r, d);
HEX (q+1, p[0]);
HEX (q+3, p[1]);
HEX (q+5, p[2]);
HEX (q+7, p[3]);
list_out (offset, q);
}
else {
q[0] = '['; q[5] = ']'; q[6] = '\0';
WRITESHORT (r, d);
HEX (q+1, p[0]);
HEX (q+3, p[1]);
list_out (offset, q);
}
}
else if (typ == OUT_REL2ADR)
{
unsigned long d = *(long *)data;
char q[11];
unsigned char p[4], *r = p;
q[0] = '('; q[5] = ')'; q[6] = '\0';
WRITESHORT (r, d);
HEX (q+1, p[0]);
HEX (q+3, p[1]);
list_out (offset, q);
}
else if (typ == OUT_REL4ADR)
{
unsigned long d = *(long *)data;
char q[11];
unsigned char p[4], *r = p;
q[0] = '('; q[9] = ')'; q[10] = '\0';
WRITELONG (r, d);
HEX (q+1, p[0]);
HEX (q+3, p[1]);
HEX (q+5, p[2]);
HEX (q+7, p[3]);
list_out (offset, q);
}
else if (typ == OUT_RESERVE)
{
char q[20];
sprintf(q, "<res %08lX>", size);
list_out (offset, q);
}
}
static void list_line (int type, char *line)
{
if (!listp)
return;
if (user_nolist){ /* fbk - 9/2/00 */
listlineno++;
return;
}
if (mistack && mistack->inhibiting)
{
if (type == LIST_MACRO)
return;
else { /* pop the m i stack */
MacroInhibit *temp = mistack;
mistack = temp->next;
nasm_free (temp);
}
}
list_emit();
listlinep = TRUE;
strncpy (listline, line, LIST_MAX_LEN-1);
listline[LIST_MAX_LEN-1] = '\0';
listlevel_e = listlevel;
}
static void list_uplevel (int type)
{
if (!listp)
return;
if (type == LIST_INCBIN || type == LIST_TIMES)
{
suppress |= (type == LIST_INCBIN ? 1 : 2);
list_out (listoffset, type == LIST_INCBIN ? "<incbin>" : "<rept>");
return;
}
listlevel++;
if (mistack && mistack->inhibiting && type == LIST_INCLUDE)
{
MacroInhibit *temp = nasm_malloc(sizeof(MacroInhibit));
temp->next = mistack;
temp->level = listlevel;
temp->inhibiting = FALSE;
mistack = temp;
}
else if (type == LIST_MACRO_NOLIST)
{
MacroInhibit *temp = nasm_malloc(sizeof(MacroInhibit));
temp->next = mistack;
temp->level = listlevel;
temp->inhibiting = TRUE;
mistack = temp;
}
}
static void list_downlevel (int type)
{
if (!listp)
return;
if (type == LIST_INCBIN || type == LIST_TIMES)
{
suppress &= ~(type == LIST_INCBIN ? 1 : 2);
return;
}
listlevel--;
while (mistack && mistack->level > listlevel)
{
MacroInhibit *temp = mistack;
mistack = temp->next;
nasm_free (temp);
}
}
ListGen nasmlist = {
list_init,
list_cleanup,
list_output,
list_line,
list_uplevel,
list_downlevel
};
|