aboutsummaryrefslogtreecommitdiffstats
path: root/asm/listing.c
blob: 9d26376991032bd50ede58d18fb096cc28ce7357 (plain)
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/* ----------------------------------------------------------------------- *
 *
 *   Copyright 1996-2019 The NASM Authors - All Rights Reserved
 *   See the file AUTHORS included with the NASM distribution for
 *   the specific copyright holders.
 *
 *   Redistribution and use in source and binary forms, with or without
 *   modification, are permitted provided that the following
 *   conditions are met:
 *
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above
 *     copyright notice, this list of conditions and the following
 *     disclaimer in the documentation and/or other materials provided
 *     with the distribution.
 *
 *     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
 *     CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 *     INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 *     MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 *     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 *     CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 *     SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 *     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 *     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 *     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 *     OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 *     EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * ----------------------------------------------------------------------- */

/*
 * listing.c    listing file generator for the Netwide Assembler
 */

#include "compiler.h"

#include "nctype.h"

#include "nasm.h"
#include "nasmlib.h"
#include "error.h"
#include "strlist.h"
#include "listing.h"

#define LIST_MAX_LEN 1024       /* 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 const char xdigit[] = "0123456789ABCDEF";

#define HEX(a,b) (*(a)=xdigit[((b)>>4)&15],(a)[1]=xdigit[(b)&15]);

static char listline[LIST_MAX_LEN];
static bool listlinep;

struct strlist *list_errors;

static char listdata[2 * LIST_INDENT];  /* we need less than that actually */
static int32_t listoffset;

static int32_t listlineno;

static int32_t listp;

static int suppress;            /* for INCBIN & TIMES special cases */

static int listlevel, listlevel_e;

static FILE *listfp;

static void list_emit(void)
{
    int i;
    const struct strlist_entry *e;

    if (listlinep || *listdata) {
        fprintf(listfp, "%6"PRId32" ", listlineno);

        if (listdata[0])
            fprintf(listfp, "%08"PRIX32" %-*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);

        putc('\n', listfp);
        listlinep = false;
        listdata[0] = '\0';
    }

    if (list_errors) {
        static const char fillchars[] = " --***XX";
        char fillchar;

        strlist_for_each(e, list_errors) {
            fprintf(listfp, "%6"PRId32"          ", listlineno);
            fillchar = fillchars[e->pvt.u & ERR_MASK];
            for (i = 0; i < LIST_HEXBIT; i++)
                putc(fillchar, listfp);

            if (listlevel_e)
                fprintf(listfp, " %s<%d>", (listlevel < 10 ? " " : ""),
                        listlevel_e);
            else
                fprintf(listfp, "     ");

            fprintf(listfp, "  %s\n", e->str);
        }

        strlist_free(&list_errors);
    }
}

static void list_init(const char *fname)
{
    if (!fname || fname[0] == '\0') {
	listfp = NULL;
	return;
    }

    listfp = nasm_open_write(fname, NF_TEXT);
    if (!listfp) {
        nasm_nonfatal("unable to open listing file `%s'", fname);
        return;
    }

    *listline = '\0';
    listlineno = 0;
    list_errors = NULL;
    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(int64_t 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_address(int64_t offset, const char *brackets,
			 int64_t addr, int size)
{
    char q[20];
    char *r = q;

    nasm_assert(size <= 8);

    *r++ = brackets[0];
    while (size--) {
	HEX(r, addr);
	addr >>= 8;
	r += 2;
    }
    *r++ = brackets[1];
    *r = '\0';
    list_out(offset, q);
}

static void list_size(int64_t offset, const char *tag, uint64_t size)
{
    char buf[64];
    const char *fmt;

    if (list_option('d'))
        fmt = "<%s %"PRIu64">";
    else
        fmt = "<%s %"PRIX64">";

    snprintf(buf, sizeof buf, fmt, tag, size);
    list_out(offset, buf);
}

static void list_output(const struct out_data *data)
{
    char q[24];
    uint64_t size = data->size;
    uint64_t offset = data->offset;
    const uint8_t *p = data->data;


    if (!listp || suppress || user_nolist)
        return;

    switch (data->type) {
    case OUT_ZERODATA:
        if (size > 16) {
            list_size(offset, "zero", size);
            break;
        } else {
            p = zero_buffer;
        }
        /* fall through */
    case OUT_RAWDATA:
    {
	if (size == 0) {
            if (!listdata[0])
                listoffset = data->offset;
        } else if (p) {
            while (size--) {
                HEX(q, *p);
                q[2] = '\0';
                list_out(offset++, q);
                p++;
            }
        } else {
            /* Used for listing on non-code generation passes with -Lp */
            list_size(offset, "len", size);
        }
	break;
    }
    case OUT_ADDRESS:
        list_address(offset, "[]", data->toffset, size);
	break;
    case OUT_SEGMENT:
        q[0] = '[';
        memset(q+1, 's', size << 1);
        q[(size << 1)+1] = ']';
        q[(size << 1)+2] = '\0';
        list_out(offset, q);
        offset += size;
        break;
    case OUT_RELADDR:
	list_address(offset, "()", data->toffset, size);
	break;
    case OUT_RESERVE:
    {
        if (size)
            list_size(offset, "res", size);
	break;
    }
    default:
        panic();
    }
}

static void list_line(int type, int32_t lineno, const char *line)
{
    if (!listp)
        return;

    if (user_nolist)
      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();
    if (lineno >= 0)
        listlineno = lineno;
    listlinep = true;
    strlcpy(listline, line, LIST_MAX_LEN-3);
    memcpy(listline + LIST_MAX_LEN-4, "...", 4);
    listlevel_e = listlevel;
}

static void mistack_push(bool inhibiting)
{
    MacroInhibit *temp = nasm_malloc(sizeof(MacroInhibit));
    temp->next = mistack;
    temp->level = listlevel;
    temp->inhibiting = inhibiting;
    mistack = temp;
}

static void list_uplevel(int type, int64_t size)
{
    if (!listp)
        return;

    switch (type) {
    case LIST_INCBIN:
        suppress |= 1;
        list_size(listoffset, "bin", size);
        break;

    case LIST_TIMES:
        suppress |= 2;
        list_size(listoffset, "rep", size);
        break;

    case LIST_INCLUDE:
        listlevel++;
        if (mistack && mistack->inhibiting)
            mistack_push(false);
        break;

    case LIST_MACRO_NOLIST:
        listlevel++;
        mistack_push(true);
        break;

    default:
        listlevel++;
        break;
    }
}

static void list_downlevel(int type)
{
    if (!listp)
        return;

    switch (type) {
    case LIST_INCBIN:
        suppress &= ~1;
        break;

    case LIST_TIMES:
        suppress &= ~2;
        break;

    default:
        listlevel--;
        while (mistack && mistack->level > listlevel) {
            MacroInhibit *temp = mistack;
            mistack = temp->next;
            nasm_free(temp);
        }
        break;
    }
}

static void list_error(errflags severity, const char *fmt, ...)
{
    va_list ap;

    if (!listfp)
	return;

    if (!list_errors)
        list_errors = strlist_alloc(false);

    va_start(ap, fmt);
    strlist_vprintf(list_errors, fmt, ap);
    va_end(ap);
    strlist_tail(list_errors)->pvt.u = severity;

    if ((severity & ERR_MASK) >= ERR_FATAL)
	list_emit();
}

static void list_set_offset(uint64_t offset)
{
    listoffset = offset;
}

static const struct lfmt nasm_list = {
    list_init,
    list_cleanup,
    list_output,
    list_line,
    list_uplevel,
    list_downlevel,
    list_error,
    list_set_offset
};

const struct lfmt *lfmt = &nasm_list;