Move declarations before statements
[nasm.git] / output / outdbg.c
blob43c6504a209c23c9b3523cc50a8a0da6ab5c0a82
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 "compiler.h"
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <ctype.h>
16 #include <inttypes.h>
18 #include "nasm.h"
19 #include "nasmlib.h"
20 #include "outform.h"
22 #ifdef OF_DBG
24 struct Section {
25 struct Section *next;
26 int32_t number;
27 char *name;
28 } *dbgsect;
30 FILE *dbgf;
31 efunc dbgef;
33 struct ofmt of_dbg;
34 static void dbg_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
36 (void)eval;
38 dbgf = fp;
39 dbgef = errfunc;
40 dbgsect = NULL;
41 (void)ldef;
42 fprintf(fp, "NASM Output format debug dump\n");
43 of_dbg.current_dfmt->init(&of_dbg, 0, fp, errfunc);
47 static void dbg_cleanup(int debuginfo)
49 (void)debuginfo;
50 of_dbg.current_dfmt->cleanup();
51 while (dbgsect) {
52 struct Section *tmp = dbgsect;
53 dbgsect = dbgsect->next;
54 nasm_free(tmp->name);
55 nasm_free(tmp);
57 fclose(dbgf);
60 static int32_t dbg_section_names(char *name, int pass, int *bits)
62 int seg;
65 * We must have an initial default: let's make it 16.
67 if (!name)
68 *bits = 16;
70 if (!name)
71 fprintf(dbgf, "section_name on init: returning %d\n",
72 seg = seg_alloc());
73 else {
74 int n = strcspn(name, " \t");
75 char *sname = nasm_strndup(name, n);
76 struct Section *s;
78 seg = NO_SEG;
79 for (s = dbgsect; s; s = s->next)
80 if (!strcmp(s->name, sname))
81 seg = s->number;
83 if (seg == NO_SEG) {
84 s = nasm_malloc(sizeof(*s));
85 s->name = sname;
86 s->number = seg = seg_alloc();
87 s->next = dbgsect;
88 dbgsect = s;
89 fprintf(dbgf, "section_name %s (pass %d): returning %d\n",
90 name, pass, seg);
93 return seg;
96 static void dbg_deflabel(char *name, int32_t segment, int32_t offset,
97 int is_global, char *special)
99 fprintf(dbgf, "deflabel %s := %08lx:%08lx %s (%d)%s%s\n",
100 name, segment, offset,
101 is_global == 2 ? "common" : is_global ? "global" : "local",
102 is_global, special ? ": " : "", special);
105 static void dbg_out(int32_t segto, const void *data, uint32_t type,
106 int32_t segment, int32_t wrt)
108 int32_t realbytes = type & OUT_SIZMASK;
109 int32_t ldata;
110 int id;
112 type &= OUT_TYPMASK;
114 fprintf(dbgf, "out to %lx, len = %ld: ", segto, realbytes);
116 switch (type) {
117 case OUT_RESERVE:
118 fprintf(dbgf, "reserved.\n");
119 break;
120 case OUT_RAWDATA:
121 fprintf(dbgf, "raw data = ");
122 while (realbytes--) {
123 id = *(uint8_t *)data;
124 data = (char *)data + 1;
125 fprintf(dbgf, "%02x ", id);
127 fprintf(dbgf, "\n");
128 break;
129 case OUT_ADDRESS:
130 ldata = 0; /* placate gcc */
131 if (realbytes == 1)
132 ldata = *((char *)data);
133 else if (realbytes == 2)
134 ldata = *((int16_t *)data);
135 else if (realbytes == 4)
136 ldata = *((int32_t *)data);
137 fprintf(dbgf, "addr %08lx (seg %08lx, wrt %08lx)\n", ldata,
138 segment, wrt);
139 break;
140 case OUT_REL2ADR:
141 fprintf(dbgf, "rel2adr %04x (seg %08lx)\n", (int)*(int16_t *)data,
142 segment);
143 break;
144 case OUT_REL4ADR:
145 fprintf(dbgf, "rel4adr %08lx (seg %08lx)\n", *(int32_t *)data,
146 segment);
147 break;
148 default:
149 fprintf(dbgf, "unknown\n");
150 break;
154 static int32_t dbg_segbase(int32_t segment)
156 return segment;
159 static int dbg_directive(char *directive, char *value, int pass)
161 fprintf(dbgf, "directive [%s] value [%s] (pass %d)\n",
162 directive, value, pass);
163 return 1;
166 static void dbg_filename(char *inname, char *outname, efunc error)
168 standard_extension(inname, outname, ".dbg", error);
171 static int dbg_set_info(enum geninfo type, char **val)
173 (void)type;
174 (void)val;
175 return 0;
178 char *types[] = {
179 "unknown", "label", "byte", "word", "dword", "float", "qword", "tbyte"
181 void dbgdbg_init(struct ofmt *of, void *id, FILE * fp, efunc error)
183 (void)of;
184 (void)id;
185 (void)fp;
186 (void)error;
187 fprintf(fp, " With debug info\n");
189 static void dbgdbg_cleanup(void)
193 static void dbgdbg_linnum(const char *lnfname, int32_t lineno, int32_t segto)
195 fprintf(dbgf, "dbglinenum %s(%ld) := %08lx\n", lnfname, lineno, segto);
197 static void dbgdbg_deflabel(char *name, int32_t segment,
198 int32_t offset, int is_global, char *special)
200 fprintf(dbgf, "dbglabel %s := %08lx:%08lx %s (%d)%s%s\n",
201 name,
202 segment, offset,
203 is_global == 2 ? "common" : is_global ? "global" : "local",
204 is_global, special ? ": " : "", special);
206 static void dbgdbg_define(const char *type, const char *params)
208 fprintf(dbgf, "dbgdirective [%s] value [%s]\n", type, params);
210 static void dbgdbg_output(int output_type, void *param)
212 (void)output_type;
213 (void)param;
215 static void dbgdbg_typevalue(int32_t type)
217 fprintf(dbgf, "new type: %s(%lX)\n",
218 types[TYM_TYPE(type) >> 3], TYM_ELEMENTS(type));
220 static struct dfmt debug_debug_form = {
221 "Trace of all info passed to debug stage",
222 "debug",
223 dbgdbg_init,
224 dbgdbg_linnum,
225 dbgdbg_deflabel,
226 dbgdbg_define,
227 dbgdbg_typevalue,
228 dbgdbg_output,
229 dbgdbg_cleanup,
232 static struct dfmt *debug_debug_arr[3] = {
233 &debug_debug_form,
234 &null_debug_form,
235 NULL
237 struct ofmt of_dbg = {
238 "Trace of all info passed to output stage",
239 "dbg",
240 NULL,
241 debug_debug_arr,
242 &null_debug_form,
243 NULL,
244 dbg_init,
245 dbg_set_info,
246 dbg_out,
247 dbg_deflabel,
248 dbg_section_names,
249 dbg_segbase,
250 dbg_directive,
251 dbg_filename,
252 dbg_cleanup
255 #endif /* OF_DBG */