1 /* $Id: print.c,v 1.16 1994/06/24 11:00:47 ceriel Exp $ */
17 extern char *strindex();
18 extern char *malloc();
21 print_unsigned(tp, v, format)
24 register char *format;
26 while (format && *format) {
27 if (strindex("cdohx", *format)) break;
30 switch(format == 0 ? 0 : *format) {
32 fprintf(db_out, currlang->uns_fmt, v);
35 if (tp != uchar_type) {
36 fprintf(db_out, currlang->uns_fmt, v);
41 (*currlang->printchar)((int) v);
44 fprintf(db_out, currlang->decint_fmt, v);
47 fprintf(db_out, currlang->octint_fmt, v);
51 fprintf(db_out, currlang->hexint_fmt, v);
57 print_literal(tp, v, compressed, format)
63 register struct literal *lit = tp->ty_literals;
67 print_unsigned(tp, v, format);
70 for (i = tp->ty_nenums; i; i--, lit++) {
71 if (lit->lit_val == v) {
72 fputs(lit->lit_name, db_out);
78 compressed ? "?%ld?" : "unknown enumeration value %ld",
84 print_integer(tp, v, format)
87 register char *format;
89 while (format && *format) {
90 if (strindex("cdohx", *format)) break;
93 switch(format == 0 ? 0 : *format) {
95 if (tp != char_type) {
96 fprintf(db_out, currlang->decint_fmt, v);
101 (*currlang->printchar)((int) v);
104 fprintf(db_out, currlang->decint_fmt, v);
107 fprintf(db_out, currlang->octint_fmt, v);
111 fprintf(db_out, currlang->hexint_fmt, v);
114 fprintf(db_out, currlang->uns_fmt, v);
119 print_params(tp, AB, static_link)
126 register struct param *par;
130 assert(tp->ty_class == T_PROCEDURE);
132 if ((i = tp->ty_nparams) == 0) return;
134 /* get parameter bytes */
136 size = tp->ty_nbparams;
137 if (static_link) size += pointer_size;
138 param_bytes = p = malloc((unsigned)size);
140 error("could not allocate enough memory");
143 if (! get_bytes(size, AB, p)) {
149 p = param_bytes + par->par_off;
150 if (par->par_kind == 'v' || par->par_kind == 'i') {
151 /* call by reference parameter, or
152 call by value parameter, but address is passed;
156 t_addr addr = get_int(p, pointer_size, T_UNSIGNED);
158 if ((size = par->par_type->ty_size) == 0) {
159 size = compute_size(par->par_type, param_bytes);
161 q = malloc((unsigned) size);
163 error("could not allocate enough memory");
167 if (! get_bytes(size, addr, q)) {
168 fprintf(db_out, currlang->addr_fmt, (long) addr);
171 print_val(par->par_type, size, q, 1, 0, (char *)0);
175 else print_val(par->par_type, par->par_type->ty_size, p, 1, 0, (char *)0);
176 if (i) fputs(", ", db_out);
182 print_val(tp, tp_sz, addr, compressed, indent, format)
183 p_type tp; /* type of value to be printed */
184 long tp_sz; /* size of object to be printed */
185 char *addr; /* address to get value from */
186 int compressed; /* for parameter lists */
187 int indent; /* indentation */
188 register char *format; /* format given or 0 */
193 if (indent == 0) indent = 4;
194 switch(tp->ty_class) {
196 if (! tp->ty_cross) {
197 error("unknown type");
200 print_val(tp->ty_cross, tp_sz, addr, compressed, indent, format);
203 print_val(tp->ty_base, tp_sz, addr, compressed, indent, format);
206 if ((!format || strindex(format, 'a') == 0) &&
207 (tp->ty_elements == char_type ||
208 tp->ty_elements == uchar_type)) {
209 print_val(string_type, tp_sz, addr, compressed, indent, format);
213 fprintf(db_out, currlang->open_array_display);
216 fprintf(db_out, "\n%*c%s%*c",
219 currlang->open_array_display,
220 4-strlen(currlang->open_array_display), ' ');
223 elsize = (*currlang->arrayelsize)(tp->ty_elements->ty_size);
224 for (i = tp_sz/elsize; i; i--) {
225 print_val(tp->ty_elements, tp->ty_elements->ty_size, addr, compressed, indent, format);
227 if (compressed && i > 1) {
228 fprintf(db_out, ", ...");
234 fprintf(db_out, "\n%*c", i > 1 ? indent : indent - 4, ' ');
236 fprintf(db_out, currlang->close_array_display);
240 register struct fields *fld = tp->ty_fields;
243 fprintf(db_out, currlang->open_struct_display);
246 fprintf(db_out, "\n%*c%s%*c",
249 currlang->open_struct_display,
250 4-strlen(currlang->open_struct_display), ' ');
253 for (i = tp->ty_nfields; i; i--, fld++) {
254 long sz = fld->fld_type->ty_size;
255 if (! compressed) fprintf(db_out, "%s = ", fld->fld_name);
256 if (fld->fld_bitsize < (sz << 3)) {
257 /* apparently a bit field */
259 fprintf(db_out, "<bitfield, %ld, %ld>", fld->fld_bitsize, sz);
261 else print_val(fld->fld_type, sz, addr+(fld->fld_pos>>3), compressed, indent, format);
262 if (compressed && i > 1) {
263 fprintf(db_out, ", ...");
269 fprintf(db_out, "\n%*c", i > 1 ? indent : indent - 4, ' ');
272 fprintf(db_out, currlang->close_struct_display);
276 fprintf(db_out, "<union>");
279 print_literal(tp, get_int(addr, tp_sz, T_ENUM), compressed, format);
282 register p_scope sc = get_scope_from_addr((t_addr) get_int(addr, pointer_size, T_UNSIGNED));
284 if (sc && sc->sc_definedby) {
285 fprintf(db_out, sc->sc_definedby->sy_idf->id_text);
289 fprintf(db_out, currlang->addr_fmt, get_int(addr, pointer_size, T_UNSIGNED));
292 t_addr a = get_int(addr, tp_sz, T_UNSIGNED);
294 fprintf(db_out, currlang->addr_fmt, a);
295 if (format && strindex(format, 's') &&
296 (tp->ty_ptrto == char_type || tp->ty_ptrto == uchar_type)) {
297 char *naddr = malloc(512);
300 fputs(" (could not allocate memory)", db_out);
303 if (! get_string(511L, a, naddr)) {
304 fputs(" (not a valid pointer)", db_out);
309 print_val(string_type, 512L, naddr, 0, indent, format);
314 if (tp->ty_ptrto->ty_class == T_PROCEDURE) {
315 p_scope sc = get_scope_from_addr(a);
316 if (sc && sc->sc_definedby && a == sc->sc_start) {
317 fprintf(db_out, " (%s)", sc->sc_definedby->sy_idf->id_text);
323 fprintf(db_out, "<file>");
326 long val = tp->ty_setlow;
327 p_type base = tp->ty_setbase;
328 long nelements = tp->ty_size << 3;
330 int rsft = 3 + (int_size == 2 ? 1 : 2);
331 long mask = int_size == 2 ? 017: 037;
333 if (base->ty_class == T_SUBRANGE) base = base->ty_base;
335 fprintf(db_out, currlang->open_set_display);
338 fprintf(db_out, "\n%*c%s%*c",
341 currlang->open_set_display,
342 4-strlen(currlang->open_set_display), ' ');
345 for (i = 0; i < nelements; i++) {
346 if (get_int(addr + (i >> rsft), int_size, T_UNSIGNED) & (1 << (i & mask))) {
350 fprintf(db_out, ", ...");
353 fprintf(db_out, ",\n%*c", indent , ' ');
355 switch(base->ty_class) {
357 print_integer(base, val+i, format);
360 print_unsigned(base, val+i, format);
363 print_literal(base, val+i, compressed, format);
371 fprintf(db_out, "\n%*c", indent-4 , ' ');
374 fprintf(db_out, currlang->close_set_display);
378 fprintf(db_out, currlang->real_fmt, get_real(addr, tp->ty_size));
381 print_unsigned(tp, get_int(addr, tp_sz, T_UNSIGNED), format);
384 print_integer(tp, get_int(addr, tp_sz, T_INTEGER), format);
387 (*currlang->printstring)(db_out, addr, (int) tp_sz);