* scm-valprint.c (scm_isymnames): Remove "#@" prefix.
[external/binutils.git] / gdb / scm-valprint.c
1 /* Scheme/Guile language support routines for GDB, the GNU debugger.
2    Copyright 1995 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19
20 #include "defs.h"
21 #include "symtab.h"
22 #include "gdbtypes.h"
23 #include "expression.h"
24 #include "parser-defs.h"
25 #include "language.h"
26 #include "value.h"
27 #include "scm-lang.h"
28 #include "valprint.h"
29
30 /* Prints the SCM value VALUE by invoking the inferior, if appropraite.
31    Returns >= 0 on succes;  retunr -1 if the inferior cannot/should not
32    print VALUE. */
33
34 int
35 scm_inferior_print (value, stream, format, deref_ref, recurse, pretty)
36      LONGEST value;
37      GDB_FILE *stream;
38      int format;
39      int deref_ref;
40      int recurse;
41      enum val_prettyprint pretty;
42 {
43   return -1;
44 }
45
46 /* {Names of immediate symbols}
47  * This table must agree with the declarations in scm.h: {Immediate Symbols}.*/
48
49 static char *scm_isymnames[] =
50 {
51   /* This table must agree with the declarations */
52   "and",
53   "begin",
54   "case",
55   "cond",
56   "do",
57   "if",
58   "lambda",
59   "let",
60   "let*",
61   "letrec",
62   "or",
63   "quote",
64   "set!",
65   "define",
66 #if 0
67   "literal-variable-ref",
68   "literal-variable-set!",
69 #endif
70   "apply",
71   "call-with-current-continuation",
72
73  /* user visible ISYMS */
74  /* other keywords */
75  /* Flags */
76
77   "#f",
78   "#t",
79   "#<undefined>",
80   "#<eof>",
81   "()",
82   "#<unspecified>"
83 };
84
85 static int
86 scm_scmlist_print (svalue, stream, format, deref_ref, recurse, pretty)
87      LONGEST svalue;
88      GDB_FILE *stream;
89      int format;
90      int deref_ref;
91      int recurse;
92      enum val_prettyprint pretty;
93 {
94   unsigned int more = print_max;
95   if (recurse > 6)
96     {
97       fputs_filtered ("...", stream);
98       return 0;
99     }
100   scm_scmval_print (SCM_CAR (svalue), stream, format,
101                     deref_ref, recurse + 1, pretty);
102   svalue = SCM_CDR (svalue);
103   for (; SCM_NIMP (svalue); svalue = SCM_CDR (svalue))
104     {
105       if (SCM_NECONSP (svalue))
106         break;
107       fputs_filtered (" ", stream);
108       if (--more == 0)
109         {
110           fputs_filtered ("...", stream);
111           return;
112         }
113       scm_scmval_print (SCM_CAR (svalue), stream, format,
114                         deref_ref, recurse + 1, pretty);
115     }
116   if (SCM_NNULLP (svalue))
117     {
118       fputs_filtered (" . ", stream);
119       scm_scmval_print (svalue, stream, format,
120                         deref_ref, recurse + 1, pretty);
121     }
122 }
123
124 static void
125 scm_ipruk (hdr, ptr, stream)
126      char *hdr;
127      LONGEST ptr;
128      GDB_FILE *stream;
129 {
130   fprintf_filtered (stream, "#<unknown-%s", hdr);
131 #define SCM_SIZE TYPE_LENGTH (builtin_type_scm)
132   if (SCM_CELLP (ptr))
133     fprintf_filtered (stream, " (0x%lx . 0x%lx) @",
134                       (long) SCM_CAR (ptr), (long) SCM_CDR (ptr));
135   fprintf_filtered (stream, " 0x%x>", ptr);
136 }
137
138 int
139 scm_scmval_print (svalue, stream, format, deref_ref, recurse, pretty)
140      LONGEST svalue;
141      GDB_FILE *stream;
142      int format;
143      int deref_ref;
144      int recurse;
145      enum val_prettyprint pretty;
146 {
147  taloop:
148   switch (7 & svalue)
149     {
150     case 2:
151     case 6:
152       print_longest (stream, format ? format : 'd', 1, svalue >> 2);
153       break;
154     case 4:
155       if (SCM_ICHRP (svalue))
156         {
157           svalue = SCM_ICHR (svalue);
158           scm_printchar (svalue, stream);
159           break;
160         }
161       else if (SCM_IFLAGP (svalue)
162                && (SCM_ISYMNUM (svalue)
163                    < (sizeof scm_isymnames / sizeof (char *))))
164         {
165           fputs_filtered (SCM_ISYMCHARS (svalue), stream);
166           break;
167         }
168       else if (SCM_ILOCP (svalue))
169         {
170           fprintf_filtered (stream, "#@%ld%c%ld",
171                             (long) SCM_IFRAME (svalue),
172                             SCM_ICDRP (svalue) ? '-' : '+',
173                             (long) SCM_IDIST (svalue));
174           break;
175         }
176       else
177         goto idef;
178       break;
179     case 1:
180       /* gloc */
181       svalue = SCM_CAR (svalue - 1);
182       goto taloop;
183     default:
184     idef:
185       scm_ipruk ("immediate", svalue, stream);
186       break;
187     case 0:
188
189       switch (SCM_TYP7 (svalue))
190         {
191         case scm_tcs_cons_gloc:
192           if (SCM_CDR (SCM_CAR (svalue) - 1L) == 0)
193             {
194               SCM name;
195               fputs_filtered ("#<latte ", stream);
196 #if 1
197               fputs_filtered ("???", stream);
198 #else
199               name = ((SCM n*)(STRUCT_TYPE( exp)))[struct_i_name];
200               scm_lfwrite (CHARS (name),
201                            (sizet) sizeof (char),
202                            (sizet) LENGTH (name),
203                            port);
204 #endif
205               fprintf_filtered (stream, " #X%lX>", svalue);
206               break;
207             }
208         case scm_tcs_cons_imcar:
209         case scm_tcs_cons_nimcar:
210           fputs_filtered ("(", stream);
211           scm_scmlist_print (svalue, stream, format,
212                              deref_ref, recurse + 1, pretty);
213           fputs_filtered (")", stream);
214           break;
215         case scm_tcs_closures:
216           fputs_filtered ("#<CLOSURE ", stream);
217           scm_scmlist_print (SCM_CODE (svalue), stream, format,
218                              deref_ref, recurse + 1, pretty);
219           fputs_filtered (">", stream);
220           break;
221         case scm_tc7_string:
222           {
223             int len = SCM_LENGTH (svalue);
224             CORE_ADDR addr = (CORE_ADDR) SCM_CDR (svalue);
225             int i;
226             int done = 0;
227             int buf_size;
228             char buffer[64];
229             int truncate = print_max && len > (int) print_max;
230             if (truncate)
231               len = print_max;
232             fputs_filtered ("\"", stream);
233             for (; done < len; done += buf_size)
234               {
235                 buf_size = min (len - done, 64);
236                 read_memory (addr + done, buffer, buf_size);
237                 
238                 for (i = 0; i < buf_size; ++i)
239                   switch (buffer[i])
240                     {
241                     case '\"':
242                     case '\\':
243                       fputs_filtered ("\\", stream);
244                     default:
245                       fprintf_filtered (stream, "%c", buffer[i]);
246                     }
247               }
248             fputs_filtered (truncate ? "...\"" : "\"", stream);
249             break;
250           }
251           break;
252         case scm_tcs_symbols:
253           {
254             int len = SCM_LENGTH (svalue);
255
256             char * str = (char*) alloca (len);
257             read_memory (SCM_CDR (svalue), str, len + 1);
258             /* Should handle weird characters FIXME */
259             str[len] = '\0';
260             fputs_filtered (str, stream);
261             break;
262           }
263         case scm_tc7_vector:
264           {
265             int len = SCM_LENGTH (svalue);
266             int i;
267             LONGEST elements = SCM_CDR(svalue);
268             fputs_filtered ("#(", stream);
269             for (i = 0; i < len; ++i)
270               {
271                 if (i > 0)
272                   fputs_filtered (" ", stream);
273                 scm_scmval_print (scm_get_field (elements, i), stream, format,
274                                   deref_ref, recurse + 1, pretty);
275               }
276             fputs_filtered (")", stream);
277           }
278           break;
279 #if 0
280         case tc7_lvector:
281           {
282             SCM result;
283             SCM hook;
284             hook = scm_get_lvector_hook (exp, LV_PRINT_FN);
285             if (hook == BOOL_F)
286               {
287                 scm_puts ("#<locked-vector ", port);
288                 scm_intprint(CDR(exp), 16, port);
289                 scm_puts (">", port);
290               }
291             else
292               {
293                 result
294                   = scm_apply (hook,
295                                scm_listify (exp, port, (writing ? BOOL_T : BOOL_F),
296                                             SCM_UNDEFINED),
297                                EOL);
298                 if (result == BOOL_F)
299                   goto punk;
300               }
301             break;
302           }
303           break;
304         case tc7_bvect:
305         case tc7_ivect:
306         case tc7_uvect:
307         case tc7_fvect:
308         case tc7_dvect:
309         case tc7_cvect:
310           scm_raprin1 (exp, port, writing);
311           break;
312 #endif
313         case scm_tcs_subrs:
314           {
315             int index = SCM_CAR (svalue) >> 8;
316 #if 1
317             char str[20];
318             sprintf (str, "#%d", index);
319 #else
320             char *str = index ? SCM_CHARS (scm_heap_org+index) : "";
321 #define SCM_CHARS(x) ((char *)(SCM_CDR(x)))
322             char *str = CHARS (SNAME (exp));
323 #endif
324             fprintf_filtered (stream, "#<primitive-procedure %s>",
325                               str);
326           }
327           break;
328 #if 0
329 #ifdef CCLO
330         case tc7_cclo:
331           scm_puts ("#<compiled-closure ", port);
332           scm_iprin1 (CCLO_SUBR (exp), port, writing);
333           scm_putc ('>', port);
334           break;
335 #endif
336         case tc7_contin:
337           fprintf_filtered (stream, "#<continuation %d @ #X%lx >",
338                             LENGTH (svalue),
339                             (long) CHARS (svalue));
340           break;
341         case tc7_port:
342           i = PTOBNUM (exp);
343           if (i < scm_numptob && scm_ptobs[i].print && (scm_ptobs[i].print) (exp, port, writing))
344             break;
345           goto punk;
346         case tc7_smob:
347           i = SMOBNUM (exp);
348           if (i < scm_numsmob && scm_smobs[i].print
349               && (scm_smobs[i].print) (exp, port, writing))
350             break;
351           goto punk;
352 #endif
353         default:
354         punk:scm_ipruk ("type", svalue, stream);
355         }
356       break;
357     }
358 }
359
360 int
361 scm_val_print (type, valaddr, address, stream, format, deref_ref, recurse,
362              pretty)
363      struct type *type;
364      char *valaddr;
365      CORE_ADDR address;
366      GDB_FILE *stream;
367      int format;
368      int deref_ref;
369      int recurse;
370      enum val_prettyprint pretty;
371 {
372   if (is_scmvalue_type (type))
373     {
374       LONGEST svalue = extract_signed_integer (valaddr, TYPE_LENGTH (type));
375       if (scm_inferior_print (svalue, stream, format,
376                               deref_ref, recurse, pretty) >= 0)
377         {
378         }
379       else
380         {
381           scm_scmval_print (svalue, stream, format,
382                               deref_ref, recurse, pretty);
383         }
384
385       gdb_flush (stream);
386       return (0);
387     }
388   else
389     {
390       return c_val_print (type, valaddr, address, stream, format,
391                           deref_ref, recurse, pretty);
392     }
393 }
394
395 int
396 scm_value_print (val, stream, format, pretty)
397      value_ptr val;
398      GDB_FILE *stream;
399      int format;
400      enum val_prettyprint pretty;
401 {
402   return (val_print (VALUE_TYPE (val), VALUE_CONTENTS (val),
403                      VALUE_ADDRESS (val), stream, format, 1, 0, pretty));
404 }