backend: psas inslen == 0 to the backend for the INCBIN case
[nasm.git] / output / outdbg.c
blob783bd36fc3e6a0c44a35b2c77a9d2748a46ff09e
1 /* ----------------------------------------------------------------------- *
2 *
3 * Copyright 1996-2013 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * outdbg.c output routines for the Netwide Assembler to produce
36 * a debugging trace
39 #include "compiler.h"
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <ctype.h>
46 #include "nasm.h"
47 #include "nasmlib.h"
48 #include "outform.h"
49 #include "outlib.h"
51 #ifdef OF_DBG
53 struct Section {
54 struct Section *next;
55 int32_t number;
56 char *name;
57 } *dbgsect;
59 const struct ofmt of_dbg;
60 static void dbg_init(void)
62 dbgsect = NULL;
63 fprintf(ofile, "NASM Output format debug dump\n");
66 static void dbg_cleanup(void)
68 dfmt->cleanup();
69 while (dbgsect) {
70 struct Section *tmp = dbgsect;
71 dbgsect = dbgsect->next;
72 nasm_free(tmp->name);
73 nasm_free(tmp);
77 static int32_t dbg_section_names(char *name, int pass, int *bits)
79 int seg;
82 * We must have an initial default: let's make it 16.
84 if (!name)
85 *bits = 16;
87 if (!name)
88 fprintf(ofile, "section_name on init: returning %d\n",
89 seg = seg_alloc());
90 else {
91 int n = strcspn(name, " \t");
92 char *sname = nasm_strndup(name, n);
93 struct Section *s;
95 seg = NO_SEG;
96 for (s = dbgsect; s; s = s->next)
97 if (!strcmp(s->name, sname))
98 seg = s->number;
100 if (seg == NO_SEG) {
101 s = nasm_malloc(sizeof(*s));
102 s->name = sname;
103 s->number = seg = seg_alloc();
104 s->next = dbgsect;
105 dbgsect = s;
106 fprintf(ofile, "section_name %s (pass %d): returning %d\n",
107 name, pass, seg);
110 return seg;
113 static void dbg_deflabel(char *name, int32_t segment, int64_t offset,
114 int is_global, char *special)
116 fprintf(ofile, "deflabel %s := %08"PRIx32":%016"PRIx64" %s (%d)%s%s\n",
117 name, segment, offset,
118 is_global == 2 ? "common" : is_global ? "global" : "local",
119 is_global, special ? ": " : "", special);
122 static void dbg_out(int32_t segto, const void *data,
123 enum out_type type, uint64_t size,
124 int32_t segment, int32_t wrt)
126 int32_t ldata;
127 int id;
129 if (type == OUT_ADDRESS)
130 fprintf(ofile, "out to %"PRIx32", len = %d: ", segto, (int)abs((int)size));
131 else
132 fprintf(ofile, "out to %"PRIx32", len = %"PRIu64": ", segto, size);
134 switch (type) {
135 case OUT_RESERVE:
136 fprintf(ofile, "reserved.\n");
137 break;
138 case OUT_RAWDATA:
139 fprintf(ofile, "raw data = ");
140 while (size--) {
141 id = *(uint8_t *)data;
142 data = (char *)data + 1;
143 fprintf(ofile, "%02x ", id);
145 fprintf(ofile, "\n");
146 break;
147 case OUT_ADDRESS:
148 ldata = *(int64_t *)data;
149 fprintf(ofile, "addr %08"PRIx32" (seg %08"PRIx32", wrt %08"PRIx32")\n",
150 ldata, segment, wrt);
151 break;
152 case OUT_REL1ADR:
153 fprintf(ofile, "rel1adr %02"PRIx8" (seg %08"PRIx32")\n",
154 (uint8_t)*(int64_t *)data, segment);
155 break;
156 case OUT_REL2ADR:
157 fprintf(ofile, "rel2adr %04"PRIx16" (seg %08"PRIx32")\n",
158 (uint16_t)*(int64_t *)data, segment);
159 break;
160 case OUT_REL4ADR:
161 fprintf(ofile, "rel4adr %08"PRIx32" (seg %08"PRIx32")\n",
162 (uint32_t)*(int64_t *)data,
163 segment);
164 break;
165 case OUT_REL8ADR:
166 fprintf(ofile, "rel8adr %016"PRIx64" (seg %08"PRIx32")\n",
167 (uint64_t)*(int64_t *)data, segment);
168 break;
169 default:
170 fprintf(ofile, "unknown\n");
171 break;
175 static void dbg_sectalign(int32_t seg, unsigned int value)
177 fprintf(ofile, "set alignment (%d) for segment (%u)\n",
178 seg, value);
181 static int32_t dbg_segbase(int32_t segment)
183 return segment;
186 static int dbg_directive(enum directives directive, char *value, int pass)
188 fprintf(ofile, "directive [%s] value [%s] (pass %d)\n",
189 directives[directive], value, pass);
190 return 1;
193 static void dbg_filename(char *inname, char *outname)
195 standard_extension(inname, outname, ".dbg");
198 static int dbg_set_info(enum geninfo type, char **val)
200 (void)type;
201 (void)val;
202 return 0;
205 static const char * const types[] = {
206 "unknown", "label", "byte", "word", "dword", "float", "qword", "tbyte"
208 static void dbgdbg_init(void)
210 fprintf(ofile, " With debug info\n");
212 static void dbgdbg_cleanup(void)
216 static void dbgdbg_linnum(const char *lnfname, int32_t lineno, int32_t segto)
218 fprintf(ofile, "dbglinenum %s(%"PRId32") := %08"PRIx32"\n",
219 lnfname, lineno, segto);
221 static void dbgdbg_deflabel(char *name, int32_t segment,
222 int64_t offset, int is_global, char *special)
224 fprintf(ofile, "dbglabel %s := %08"PRIx32":%016"PRIx64" %s (%d)%s%s\n",
225 name,
226 segment, offset,
227 is_global == 2 ? "common" : is_global ? "global" : "local",
228 is_global, special ? ": " : "", special);
230 static void dbgdbg_define(const char *type, const char *params)
232 fprintf(ofile, "dbgdirective [%s] value [%s]\n", type, params);
234 static void dbgdbg_output(int output_type, void *param)
236 (void)output_type;
237 (void)param;
239 static void dbgdbg_typevalue(int32_t type)
241 fprintf(ofile, "new type: %s(%"PRIX32")\n",
242 types[TYM_TYPE(type) >> 3], TYM_ELEMENTS(type));
244 static const struct dfmt debug_debug_form = {
245 "Trace of all info passed to debug stage",
246 "debug",
247 dbgdbg_init,
248 dbgdbg_linnum,
249 dbgdbg_deflabel,
250 dbgdbg_define,
251 dbgdbg_typevalue,
252 dbgdbg_output,
253 dbgdbg_cleanup,
256 static const struct dfmt * const debug_debug_arr[3] = {
257 &debug_debug_form,
258 &null_debug_form,
259 NULL
262 const struct ofmt of_dbg = {
263 "Trace of all info passed to output stage",
264 "dbg",
265 OFMT_TEXT,
267 debug_debug_arr,
268 &debug_debug_form,
269 NULL,
270 dbg_init,
271 dbg_set_info,
272 nasm_do_legacy_output,
273 dbg_out,
274 dbg_deflabel,
275 dbg_section_names,
276 dbg_sectalign,
277 dbg_segbase,
278 dbg_directive,
279 dbg_filename,
280 dbg_cleanup
283 #endif /* OF_DBG */