1 /* Scheme/Guile language support routines for GDB, the GNU debugger.
2 Copyright 1995 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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. */
24 #include "expression.h"
25 #include "parser-defs.h"
31 #include "gdb_string.h"
34 extern void _initialize_scheme_language
PARAMS ((void));
35 static value_ptr evaluate_subexp_scm
PARAMS ((struct type
*, struct expression
*,
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
));
41 extern struct type
**CONST_PTR (c_builtin_types
[]);
43 struct type
*builtin_type_scm
;
46 scm_printchar (c
, stream
)
50 fprintf_filtered (stream
, "#\\%c", c
);
54 scm_printstr (stream
, string
, length
, width
, force_ellipses
)
61 fprintf_filtered (stream
, "\"%s\"", string
);
65 is_scmvalue_type (type
)
68 if (TYPE_CODE (type
) == TYPE_CODE_INT
69 && TYPE_NAME (type
) && strcmp (TYPE_NAME (type
), "SCM") == 0)
76 /* Get the INDEX'th SCM value, assuming SVALUE is the address
80 scm_get_field (svalue
, index
)
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
));
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). */
95 scm_unpack (type
, valaddr
, context
)
98 enum type_code context
;
100 if (is_scmvalue_type (type
))
102 LONGEST svalue
= extract_signed_integer (valaddr
, TYPE_LENGTH (type
));
103 if (context
== TYPE_CODE_BOOL
)
105 if (svalue
== SCM_BOOL_F
)
110 switch (7 & (int) svalue
)
115 case 4: /* other immediate value */
116 if (SCM_ICHRP (svalue
)) /* character */
117 return SCM_ICHR (svalue
);
118 else if (SCM_IFLAGP (svalue
))
120 switch ((int) svalue
)
131 error ("Value can't be converted to integer.");
137 return unpack_long (type
, valaddr
);
140 /* True if we're correctly in Guile's eval.c (the evaluator and apply). */
145 if (current_source_symtab
&& current_source_symtab
->filename
)
147 char *filename
= current_source_symtab
->filename
;
148 int len
= strlen (filename
);
149 if (len
>= 6 && strcmp (filename
+ len
- 6, "eval.c") == 0)
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. */
160 scm_lookup_name (str
)
164 int len
= strlen (str
);
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
);
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
);
178 /* FIXME in this case, we should try lookup_symbol first */
179 args
[2] = value_from_longest (builtin_type_scm
, SCM_EOL
);
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
);
186 sym
= lookup_symbol (str
,
187 expression_context_block
,
188 VAR_NAMESPACE
, (int *) NULL
,
189 (struct symtab
**) NULL
);
191 return value_of_variable (sym
, NULL
);
192 error ("No symbol \"%s\" in current context.");
196 scm_evaluate_string (str
, len
)
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
);
211 evaluate_subexp_scm (expect_type
, exp
, pos
, noside
)
212 struct type
*expect_type
;
213 register struct expression
*exp
;
217 enum exp_opcode op
= exp
->elts
[*pos
].opcode
;
224 len
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
225 (*pos
) += 3 + BYTES_TO_EXP_ELEM (len
+ 1);
226 if (noside
== EVAL_SKIP
)
228 str
= &exp
->elts
[pc
+ 2].string
;
229 return scm_lookup_name (str
);
232 len
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
233 (*pos
) += 3 + BYTES_TO_EXP_ELEM (len
+ 1);
234 if (noside
== EVAL_SKIP
)
236 str
= &exp
->elts
[pc
+ 2].string
;
237 return scm_evaluate_string (str
, len
);
240 return evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
242 return value_from_longest (builtin_type_long
, (LONGEST
) 1);
245 const struct language_defn scm_language_defn
=
247 "scheme", /* Language name */
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 */
274 _initialize_scheme_language ()
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
);