This commit was generated by cvs2svn to track changes on a CVS vendor
[platform/upstream/binutils.git] / gdb / scm-lang.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,
19    Boston, MA 02111-1307, USA.  */
20
21 #include "defs.h"
22 #include "symtab.h"
23 #include "gdbtypes.h"
24 #include "expression.h"
25 #include "parser-defs.h"
26 #include "language.h"
27 #include "value.h"
28 #include "c-lang.h"
29 #include "scm-lang.h"
30 #include "scm-tags.h"
31 #include "gdb_string.h"
32 #include "gdbcore.h"
33
34 extern void _initialize_scheme_language PARAMS ((void));
35 static value_ptr evaluate_subexp_scm PARAMS ((struct type *, struct expression *,
36                                               int *, enum noside));
37 static value_ptr scm_lookup_name PARAMS ((char *));
38 static int in_eval_c PARAMS ((void));
39 static void scm_printstr PARAMS ((GDB_FILE * stream, char *string, unsigned int length, int width, int force_ellipses));
40
41 extern struct type **CONST_PTR (c_builtin_types[]);
42
43 struct type *builtin_type_scm;
44
45 void
46 scm_printchar (c, stream)
47      int c;
48      GDB_FILE *stream;
49 {
50   fprintf_filtered (stream, "#\\%c", c);
51 }
52
53 static void
54 scm_printstr (stream, string, length, width, force_ellipses)
55      GDB_FILE *stream;
56      char *string;
57      unsigned int length;
58      int width;
59      int force_ellipses;
60 {
61   fprintf_filtered (stream, "\"%s\"", string);
62 }
63
64 int
65 is_scmvalue_type (type)
66      struct type *type;
67 {
68   if (TYPE_CODE (type) == TYPE_CODE_INT
69       && TYPE_NAME (type) && strcmp (TYPE_NAME (type), "SCM") == 0)
70     {
71       return 1;
72     }
73   return 0;
74 }
75
76 /* Get the INDEX'th SCM value, assuming SVALUE is the address
77    of the 0'th one.  */
78
79 LONGEST
80 scm_get_field (svalue, index)
81      LONGEST svalue;
82      int index;
83 {
84   char buffer[20];
85   read_memory (SCM2PTR (svalue) + index * TYPE_LENGTH (builtin_type_scm),
86                buffer, TYPE_LENGTH (builtin_type_scm));
87   return extract_signed_integer (buffer, TYPE_LENGTH (builtin_type_scm));
88 }
89
90 /* Unpack a value of type TYPE in buffer VALADDR as an integer
91    (if CONTEXT == TYPE_CODE_IN), a pointer (CONTEXT == TYPE_CODE_PTR),
92    or Boolean (CONTEXT == TYPE_CODE_BOOL).  */
93
94 LONGEST
95 scm_unpack (type, valaddr, context)
96      struct type *type;
97      char *valaddr;
98      enum type_code context;
99 {
100   if (is_scmvalue_type (type))
101     {
102       LONGEST svalue = extract_signed_integer (valaddr, TYPE_LENGTH (type));
103       if (context == TYPE_CODE_BOOL)
104         {
105           if (svalue == SCM_BOOL_F)
106             return 0;
107           else
108             return 1;
109         }
110       switch (7 & (int) svalue)
111         {
112         case 2:
113         case 6:         /* fixnum */
114           return svalue >> 2;
115         case 4:         /* other immediate value */
116           if (SCM_ICHRP (svalue))       /* character */
117             return SCM_ICHR (svalue);
118           else if (SCM_IFLAGP (svalue))
119             {
120               switch ((int) svalue)
121                 {
122 #ifndef SICP
123                 case SCM_EOL:
124 #endif
125                 case SCM_BOOL_F:
126                   return 0;
127                 case SCM_BOOL_T:
128                   return 1;
129                 }
130             }
131           error ("Value can't be converted to integer.");
132         default:
133           return svalue;
134         }
135     }
136   else
137     return unpack_long (type, valaddr);
138 }
139
140 /* True if we're correctly in Guile's eval.c (the evaluator and apply). */
141
142 static int
143 in_eval_c ()
144 {
145   if (current_source_symtab && current_source_symtab->filename)
146     {
147       char *filename = current_source_symtab->filename;
148       int len = strlen (filename);
149       if (len >= 6 && strcmp (filename + len - 6, "eval.c") == 0)
150         return 1;
151     }
152   return 0;
153 }
154
155 /* Lookup a value for the variable named STR.
156    First lookup in Scheme context (using the scm_lookup_cstr inferior
157    function), then try lookup_symbol for compiled variables. */
158
159 static value_ptr
160 scm_lookup_name (str)
161      char *str;
162 {
163   value_ptr args[3];
164   int len = strlen (str);
165   value_ptr func, val;
166   struct symbol *sym;
167   args[0] = value_allocate_space_in_inferior (len);
168   args[1] = value_from_longest (builtin_type_int, len);
169   write_memory (value_as_long (args[0]), str, len);
170
171   if (in_eval_c ()
172       && (sym = lookup_symbol ("env",
173                                expression_context_block,
174                                VAR_NAMESPACE, (int *) NULL,
175                                (struct symtab **) NULL)) != NULL)
176     args[2] = value_of_variable (sym, expression_context_block);
177   else
178     /* FIXME in this case, we should try lookup_symbol first */
179     args[2] = value_from_longest (builtin_type_scm, SCM_EOL);
180
181   func = find_function_in_inferior ("scm_lookup_cstr");
182   val = call_function_by_hand (func, 3, args);
183   if (!value_logical_not (val))
184     return value_ind (val);
185
186   sym = lookup_symbol (str,
187                        expression_context_block,
188                        VAR_NAMESPACE, (int *) NULL,
189                        (struct symtab **) NULL);
190   if (sym)
191     return value_of_variable (sym, NULL);
192   error ("No symbol \"%s\" in current context.");
193 }
194
195 value_ptr
196 scm_evaluate_string (str, len)
197      char *str;
198      int len;
199 {
200   value_ptr func;
201   value_ptr addr = value_allocate_space_in_inferior (len + 1);
202   LONGEST iaddr = value_as_long (addr);
203   write_memory (iaddr, str, len);
204   /* FIXME - should find and pass env */
205   write_memory (iaddr + len, "", 1);
206   func = find_function_in_inferior ("scm_evstr");
207   return call_function_by_hand (func, 1, &addr);
208 }
209
210 static value_ptr
211 evaluate_subexp_scm (expect_type, exp, pos, noside)
212      struct type *expect_type;
213      register struct expression *exp;
214      register int *pos;
215      enum noside noside;
216 {
217   enum exp_opcode op = exp->elts[*pos].opcode;
218   int len, pc;
219   char *str;
220   switch (op)
221     {
222     case OP_NAME:
223       pc = (*pos)++;
224       len = longest_to_int (exp->elts[pc + 1].longconst);
225       (*pos) += 3 + BYTES_TO_EXP_ELEM (len + 1);
226       if (noside == EVAL_SKIP)
227         goto nosideret;
228       str = &exp->elts[pc + 2].string;
229       return scm_lookup_name (str);
230     case OP_EXPRSTRING:
231       pc = (*pos)++;
232       len = longest_to_int (exp->elts[pc + 1].longconst);
233       (*pos) += 3 + BYTES_TO_EXP_ELEM (len + 1);
234       if (noside == EVAL_SKIP)
235         goto nosideret;
236       str = &exp->elts[pc + 2].string;
237       return scm_evaluate_string (str, len);
238     default:;
239     }
240   return evaluate_subexp_standard (expect_type, exp, pos, noside);
241 nosideret:
242   return value_from_longest (builtin_type_long, (LONGEST) 1);
243 }
244
245 const struct language_defn scm_language_defn =
246 {
247   "scheme",                     /* Language name */
248   language_scm,
249   c_builtin_types,
250   range_check_off,
251   type_check_off,
252   scm_parse,
253   c_error,
254   evaluate_subexp_scm,
255   scm_printchar,                /* Print a character constant */
256   scm_printstr,                 /* Function to print string constant */
257   NULL,                         /* Function to print a single character */
258   NULL,                         /* Create fundamental type in this language */
259   c_print_type,                 /* Print a type using appropriate syntax */
260   scm_val_print,                /* Print a value using appropriate syntax */
261   scm_value_print,              /* Print a top-level value */
262   {"", "", "", ""},             /* Binary format info */
263   {"#o%lo", "#o", "o", ""},     /* Octal format info */
264   {"%ld", "", "d", ""},         /* Decimal format info */
265   {"#x%lX", "#X", "X", ""},     /* Hex format info */
266   NULL,                         /* expression operators for printing */
267   1,                            /* c-style arrays */
268   0,                            /* String lower bound */
269   &builtin_type_char,           /* Type of string elements */
270   LANG_MAGIC
271 };
272
273 void
274 _initialize_scheme_language ()
275 {
276   add_language (&scm_language_defn);
277   builtin_type_scm = init_type (TYPE_CODE_INT,
278                                 TARGET_LONG_BIT / TARGET_CHAR_BIT,
279                                 0, "SCM", (struct objfile *) NULL);
280 }