Apply Nindent to all .c and .h files
[nasm/avx512.git] / output / outdbg.c
blob5000407bfcafcdab55d34dfeefd27c4984e96292
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>
15 #include "nasm.h"
16 #include "nasmlib.h"
17 #include "outform.h"
19 #ifdef OF_DBG
21 struct Section {
22 struct Section *next;
23 long number;
24 char *name;
25 } *dbgsect;
27 FILE *dbgf;
28 efunc dbgef;
30 struct ofmt of_dbg;
31 static void dbg_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
33 (void)eval;
35 dbgf = fp;
36 dbgef = errfunc;
37 dbgsect = NULL;
38 (void)ldef;
39 fprintf(fp, "NASM Output format debug dump\n");
40 of_dbg.current_dfmt->init(&of_dbg, 0, fp, errfunc);
44 static void dbg_cleanup(int debuginfo)
46 (void)debuginfo;
47 of_dbg.current_dfmt->cleanup();
48 while (dbgsect) {
49 struct Section *tmp = dbgsect;
50 dbgsect = dbgsect->next;
51 nasm_free(tmp->name);
52 nasm_free(tmp);
54 fclose(dbgf);
57 static long dbg_section_names(char *name, int pass, int *bits)
59 int seg;
62 * We must have an initial default: let's make it 16.
64 if (!name)
65 *bits = 16;
67 if (!name)
68 fprintf(dbgf, "section_name on init: returning %d\n",
69 seg = seg_alloc());
70 else {
71 int n = strcspn(name, " \t");
72 char *sname = nasm_strndup(name, n);
73 struct Section *s;
75 seg = NO_SEG;
76 for (s = dbgsect; s; s = s->next)
77 if (!strcmp(s->name, sname))
78 seg = s->number;
80 if (seg == NO_SEG) {
81 s = nasm_malloc(sizeof(*s));
82 s->name = sname;
83 s->number = seg = seg_alloc();
84 s->next = dbgsect;
85 dbgsect = s;
86 fprintf(dbgf, "section_name %s (pass %d): returning %d\n",
87 name, pass, seg);
90 return seg;
93 static void dbg_deflabel(char *name, long segment, long offset,
94 int is_global, char *special)
96 fprintf(dbgf, "deflabel %s := %08lx:%08lx %s (%d)%s%s\n",
97 name, segment, offset,
98 is_global == 2 ? "common" : is_global ? "global" : "local",
99 is_global, special ? ": " : "", special);
102 static void dbg_out(long segto, void *data, unsigned long type,
103 long segment, long wrt)
105 long realbytes = type & OUT_SIZMASK;
106 long ldata;
107 int id;
109 type &= OUT_TYPMASK;
111 fprintf(dbgf, "out to %lx, len = %ld: ", segto, realbytes);
113 switch (type) {
114 case OUT_RESERVE:
115 fprintf(dbgf, "reserved.\n");
116 break;
117 case OUT_RAWDATA:
118 fprintf(dbgf, "raw data = ");
119 while (realbytes--) {
120 id = *(unsigned char *)data;
121 data = (char *)data + 1;
122 fprintf(dbgf, "%02x ", id);
124 fprintf(dbgf, "\n");
125 break;
126 case OUT_ADDRESS:
127 ldata = 0; /* placate gcc */
128 if (realbytes == 1)
129 ldata = *((char *)data);
130 else if (realbytes == 2)
131 ldata = *((short *)data);
132 else if (realbytes == 4)
133 ldata = *((long *)data);
134 fprintf(dbgf, "addr %08lx (seg %08lx, wrt %08lx)\n", ldata,
135 segment, wrt);
136 break;
137 case OUT_REL2ADR:
138 fprintf(dbgf, "rel2adr %04x (seg %08lx)\n", (int)*(short *)data,
139 segment);
140 break;
141 case OUT_REL4ADR:
142 fprintf(dbgf, "rel4adr %08lx (seg %08lx)\n", *(long *)data,
143 segment);
144 break;
145 default:
146 fprintf(dbgf, "unknown\n");
147 break;
151 static long dbg_segbase(long segment)
153 return segment;
156 static int dbg_directive(char *directive, char *value, int pass)
158 fprintf(dbgf, "directive [%s] value [%s] (pass %d)\n",
159 directive, value, pass);
160 return 1;
163 static void dbg_filename(char *inname, char *outname, efunc error)
165 standard_extension(inname, outname, ".dbg", error);
168 static int dbg_set_info(enum geninfo type, char **val)
170 (void)type;
171 (void)val;
172 return 0;
175 char *types[] = {
176 "unknown", "label", "byte", "word", "dword", "float", "qword", "tbyte"
178 void dbgdbg_init(struct ofmt *of, void *id, FILE * fp, efunc error)
180 (void)of;
181 (void)id;
182 (void)fp;
183 (void)error;
184 fprintf(fp, " With debug info\n");
186 static void dbgdbg_cleanup(void)
190 static void dbgdbg_linnum(const char *lnfname, long lineno, long segto)
192 fprintf(dbgf, "dbglinenum %s(%ld) := %08lx\n", lnfname, lineno, segto);
194 static void dbgdbg_deflabel(char *name, long segment,
195 long offset, int is_global, char *special)
197 fprintf(dbgf, "dbglabel %s := %08lx:%08lx %s (%d)%s%s\n",
198 name,
199 segment, offset,
200 is_global == 2 ? "common" : is_global ? "global" : "local",
201 is_global, special ? ": " : "", special);
203 static void dbgdbg_define(const char *type, const char *params)
205 fprintf(dbgf, "dbgdirective [%s] value [%s]\n", type, params);
207 static void dbgdbg_output(int output_type, void *param)
209 (void)output_type;
210 (void)param;
212 static void dbgdbg_typevalue(long type)
214 fprintf(dbgf, "new type: %s(%lX)\n",
215 types[TYM_TYPE(type) >> 3], TYM_ELEMENTS(type));
217 static struct dfmt debug_debug_form = {
218 "Trace of all info passed to debug stage",
219 "debug",
220 dbgdbg_init,
221 dbgdbg_linnum,
222 dbgdbg_deflabel,
223 dbgdbg_define,
224 dbgdbg_typevalue,
225 dbgdbg_output,
226 dbgdbg_cleanup,
229 static struct dfmt *debug_debug_arr[3] = {
230 &debug_debug_form,
231 &null_debug_form,
232 NULL
234 struct ofmt of_dbg = {
235 "Trace of all info passed to output stage",
236 "dbg",
237 NULL,
238 debug_debug_arr,
239 &null_debug_form,
240 NULL,
241 dbg_init,
242 dbg_set_info,
243 dbg_out,
244 dbg_deflabel,
245 dbg_section_names,
246 dbg_segbase,
247 dbg_directive,
248 dbg_filename,
249 dbg_cleanup
252 #endif /* OF_DBG */