Version 0.99.03
[nasm/avx512.git] / output / outdbg.c
blob0dda75a67a56b4c12709fb048ad61d14063e71f0
1 /* outdbg.c output routines for the Netwide Assembler to produce
2 * a debugging trace
4 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
5 * Julian Hall. All rights reserved. The software is
6 * redistributable under the licence given in the file "Licence"
7 * distributed in the NASM archive.
8 */
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <ctype.h>
14 #include <inttypes.h>
16 #include "nasm.h"
17 #include "nasmlib.h"
18 #include "outform.h"
20 #ifdef OF_DBG
22 struct Section {
23 struct Section *next;
24 int32_t number;
25 char *name;
26 } *dbgsect;
28 FILE *dbgf;
29 efunc dbgef;
31 struct ofmt of_dbg;
32 static void dbg_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
34 (void)eval;
36 dbgf = fp;
37 dbgef = errfunc;
38 dbgsect = NULL;
39 (void)ldef;
40 fprintf(fp, "NASM Output format debug dump\n");
41 of_dbg.current_dfmt->init(&of_dbg, 0, fp, errfunc);
45 static void dbg_cleanup(int debuginfo)
47 (void)debuginfo;
48 of_dbg.current_dfmt->cleanup();
49 while (dbgsect) {
50 struct Section *tmp = dbgsect;
51 dbgsect = dbgsect->next;
52 nasm_free(tmp->name);
53 nasm_free(tmp);
55 fclose(dbgf);
58 static int32_t dbg_section_names(char *name, int pass, int *bits)
60 int seg;
63 * We must have an initial default: let's make it 16.
65 if (!name)
66 *bits = 16;
68 if (!name)
69 fprintf(dbgf, "section_name on init: returning %d\n",
70 seg = seg_alloc());
71 else {
72 int n = strcspn(name, " \t");
73 char *sname = nasm_strndup(name, n);
74 struct Section *s;
76 seg = NO_SEG;
77 for (s = dbgsect; s; s = s->next)
78 if (!strcmp(s->name, sname))
79 seg = s->number;
81 if (seg == NO_SEG) {
82 s = nasm_malloc(sizeof(*s));
83 s->name = sname;
84 s->number = seg = seg_alloc();
85 s->next = dbgsect;
86 dbgsect = s;
87 fprintf(dbgf, "section_name %s (pass %d): returning %d\n",
88 name, pass, seg);
91 return seg;
94 static void dbg_deflabel(char *name, int32_t segment, int32_t offset,
95 int is_global, char *special)
97 fprintf(dbgf, "deflabel %s := %08lx:%08lx %s (%d)%s%s\n",
98 name, segment, offset,
99 is_global == 2 ? "common" : is_global ? "global" : "local",
100 is_global, special ? ": " : "", special);
103 static void dbg_out(int32_t segto, const void *data, uint32_t type,
104 int32_t segment, int32_t wrt)
106 int32_t realbytes = type & OUT_SIZMASK;
107 int32_t ldata;
108 int id;
110 type &= OUT_TYPMASK;
112 fprintf(dbgf, "out to %lx, len = %ld: ", segto, realbytes);
114 switch (type) {
115 case OUT_RESERVE:
116 fprintf(dbgf, "reserved.\n");
117 break;
118 case OUT_RAWDATA:
119 fprintf(dbgf, "raw data = ");
120 while (realbytes--) {
121 id = *(uint8_t *)data;
122 data = (char *)data + 1;
123 fprintf(dbgf, "%02x ", id);
125 fprintf(dbgf, "\n");
126 break;
127 case OUT_ADDRESS:
128 ldata = 0; /* placate gcc */
129 if (realbytes == 1)
130 ldata = *((char *)data);
131 else if (realbytes == 2)
132 ldata = *((int16_t *)data);
133 else if (realbytes == 4)
134 ldata = *((int32_t *)data);
135 fprintf(dbgf, "addr %08lx (seg %08lx, wrt %08lx)\n", ldata,
136 segment, wrt);
137 break;
138 case OUT_REL2ADR:
139 fprintf(dbgf, "rel2adr %04x (seg %08lx)\n", (int)*(int16_t *)data,
140 segment);
141 break;
142 case OUT_REL4ADR:
143 fprintf(dbgf, "rel4adr %08lx (seg %08lx)\n", *(int32_t *)data,
144 segment);
145 break;
146 default:
147 fprintf(dbgf, "unknown\n");
148 break;
152 static int32_t dbg_segbase(int32_t segment)
154 return segment;
157 static int dbg_directive(char *directive, char *value, int pass)
159 fprintf(dbgf, "directive [%s] value [%s] (pass %d)\n",
160 directive, value, pass);
161 return 1;
164 static void dbg_filename(char *inname, char *outname, efunc error)
166 standard_extension(inname, outname, ".dbg", error);
169 static int dbg_set_info(enum geninfo type, char **val)
171 (void)type;
172 (void)val;
173 return 0;
176 char *types[] = {
177 "unknown", "label", "byte", "word", "dword", "float", "qword", "tbyte"
179 void dbgdbg_init(struct ofmt *of, void *id, FILE * fp, efunc error)
181 (void)of;
182 (void)id;
183 (void)fp;
184 (void)error;
185 fprintf(fp, " With debug info\n");
187 static void dbgdbg_cleanup(void)
191 static void dbgdbg_linnum(const char *lnfname, int32_t lineno, int32_t segto)
193 fprintf(dbgf, "dbglinenum %s(%ld) := %08lx\n", lnfname, lineno, segto);
195 static void dbgdbg_deflabel(char *name, int32_t segment,
196 int32_t offset, int is_global, char *special)
198 fprintf(dbgf, "dbglabel %s := %08lx:%08lx %s (%d)%s%s\n",
199 name,
200 segment, offset,
201 is_global == 2 ? "common" : is_global ? "global" : "local",
202 is_global, special ? ": " : "", special);
204 static void dbgdbg_define(const char *type, const char *params)
206 fprintf(dbgf, "dbgdirective [%s] value [%s]\n", type, params);
208 static void dbgdbg_output(int output_type, void *param)
210 (void)output_type;
211 (void)param;
213 static void dbgdbg_typevalue(int32_t type)
215 fprintf(dbgf, "new type: %s(%lX)\n",
216 types[TYM_TYPE(type) >> 3], TYM_ELEMENTS(type));
218 static struct dfmt debug_debug_form = {
219 "Trace of all info passed to debug stage",
220 "debug",
221 dbgdbg_init,
222 dbgdbg_linnum,
223 dbgdbg_deflabel,
224 dbgdbg_define,
225 dbgdbg_typevalue,
226 dbgdbg_output,
227 dbgdbg_cleanup,
230 static struct dfmt *debug_debug_arr[3] = {
231 &debug_debug_form,
232 &null_debug_form,
233 NULL
235 struct ofmt of_dbg = {
236 "Trace of all info passed to output stage",
237 "dbg",
238 NULL,
239 debug_debug_arr,
240 &null_debug_form,
241 NULL,
242 dbg_init,
243 dbg_set_info,
244 dbg_out,
245 dbg_deflabel,
246 dbg_section_names,
247 dbg_segbase,
248 dbg_directive,
249 dbg_filename,
250 dbg_cleanup
253 #endif /* OF_DBG */