1 /* Scheme interface to architecture.
3 Copyright (C) 2014-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 /* See README file in this directory for implementation notes, coding
21 conventions, et.al. */
25 #include "arch-utils.h"
26 #include "guile-internal.h"
28 /* The <gdb:arch> smob. */
32 /* This always appears first. */
35 struct gdbarch
*gdbarch
;
38 static const char arch_smob_name
[] = "gdb:arch";
40 /* The tag Guile knows the arch smob by. */
41 static scm_t_bits arch_smob_tag
;
43 /* Use a 'void *' here because it isn't guaranteed that SCM is a
45 static const registry
<gdbarch
>::key
<void, gdb::noop_deleter
<void>>
48 static int arscm_is_arch (SCM
);
50 /* Administrivia for arch smobs. */
52 /* The smob "print" function for <gdb:arch>. */
55 arscm_print_arch_smob (SCM self
, SCM port
, scm_print_state
*pstate
)
57 arch_smob
*a_smob
= (arch_smob
*) SCM_SMOB_DATA (self
);
58 struct gdbarch
*gdbarch
= a_smob
->gdbarch
;
60 gdbscm_printf (port
, "#<%s", arch_smob_name
);
61 gdbscm_printf (port
, " %s", gdbarch_bfd_arch_info (gdbarch
)->printable_name
);
64 scm_remember_upto_here_1 (self
);
66 /* Non-zero means success. */
70 /* Low level routine to create a <gdb:arch> object for GDBARCH. */
73 arscm_make_arch_smob (struct gdbarch
*gdbarch
)
75 arch_smob
*a_smob
= (arch_smob
*)
76 scm_gc_malloc (sizeof (arch_smob
), arch_smob_name
);
79 a_smob
->gdbarch
= gdbarch
;
80 a_scm
= scm_new_smob (arch_smob_tag
, (scm_t_bits
) a_smob
);
81 gdbscm_init_gsmob (&a_smob
->base
);
86 /* Return the gdbarch field of A_SMOB. */
89 arscm_get_gdbarch (arch_smob
*a_smob
)
91 return a_smob
->gdbarch
;
94 /* Return non-zero if SCM is an architecture smob. */
97 arscm_is_arch (SCM scm
)
99 return SCM_SMOB_PREDICATE (arch_smob_tag
, scm
);
102 /* (arch? object) -> boolean */
105 gdbscm_arch_p (SCM scm
)
107 return scm_from_bool (arscm_is_arch (scm
));
110 /* Return the <gdb:arch> object corresponding to GDBARCH.
111 The object is cached in GDBARCH so this is simple. */
114 arscm_scm_from_arch (struct gdbarch
*gdbarch
)
117 void *data
= arch_object_data
.get (gdbarch
);
120 arch_scm
= arscm_make_arch_smob (gdbarch
);
122 /* This object lasts the duration of the GDB session, so there
123 is no call to scm_gc_unprotect_object for it. */
124 scm_gc_protect_object (arch_scm
);
126 arch_object_data
.set (gdbarch
, (void *) arch_scm
);
129 arch_scm
= (SCM
) data
;
134 /* Return the <gdb:arch> smob in SELF.
135 Throws an exception if SELF is not a <gdb:arch> object. */
138 arscm_get_arch_arg_unsafe (SCM self
, int arg_pos
, const char *func_name
)
140 SCM_ASSERT_TYPE (arscm_is_arch (self
), self
, arg_pos
, func_name
,
146 /* Return a pointer to the arch smob of SELF.
147 Throws an exception if SELF is not a <gdb:arch> object. */
150 arscm_get_arch_smob_arg_unsafe (SCM self
, int arg_pos
, const char *func_name
)
152 SCM a_scm
= arscm_get_arch_arg_unsafe (self
, arg_pos
, func_name
);
153 arch_smob
*a_smob
= (arch_smob
*) SCM_SMOB_DATA (a_scm
);
160 /* (current-arch) -> <gdb:arch>
161 Return the architecture of the currently selected stack frame,
162 if there is one, or the current target if there isn't. */
165 gdbscm_current_arch (void)
167 return arscm_scm_from_arch (get_current_arch ());
170 /* (arch-name <gdb:arch>) -> string
171 Return the name of the architecture as a string value. */
174 gdbscm_arch_name (SCM self
)
177 = arscm_get_arch_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
178 struct gdbarch
*gdbarch
= a_smob
->gdbarch
;
181 name
= (gdbarch_bfd_arch_info (gdbarch
))->printable_name
;
183 return gdbscm_scm_from_c_string (name
);
186 /* (arch-charset <gdb:arch>) -> string */
189 gdbscm_arch_charset (SCM self
)
192 =arscm_get_arch_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
193 struct gdbarch
*gdbarch
= a_smob
->gdbarch
;
195 return gdbscm_scm_from_c_string (target_charset (gdbarch
));
198 /* (arch-wide-charset <gdb:arch>) -> string */
201 gdbscm_arch_wide_charset (SCM self
)
204 = arscm_get_arch_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
205 struct gdbarch
*gdbarch
= a_smob
->gdbarch
;
207 return gdbscm_scm_from_c_string (target_wide_charset (gdbarch
));
212 The order the types are defined here follows the order in
213 struct builtin_type. */
215 /* Helper routine to return a builtin type for <gdb:arch> object SELF.
216 OFFSET is offsetof (builtin_type, the_type).
217 Throws an exception if SELF is not a <gdb:arch> object. */
219 static const struct builtin_type
*
220 gdbscm_arch_builtin_type (SCM self
, const char *func_name
)
223 = arscm_get_arch_smob_arg_unsafe (self
, SCM_ARG1
, func_name
);
224 struct gdbarch
*gdbarch
= a_smob
->gdbarch
;
226 return builtin_type (gdbarch
);
229 /* (arch-void-type <gdb:arch>) -> <gdb:type> */
232 gdbscm_arch_void_type (SCM self
)
235 = gdbscm_arch_builtin_type (self
, FUNC_NAME
)->builtin_void
;
237 return tyscm_scm_from_type (type
);
240 /* (arch-char-type <gdb:arch>) -> <gdb:type> */
243 gdbscm_arch_char_type (SCM self
)
246 = gdbscm_arch_builtin_type (self
, FUNC_NAME
)->builtin_char
;
248 return tyscm_scm_from_type (type
);
251 /* (arch-short-type <gdb:arch>) -> <gdb:type> */
254 gdbscm_arch_short_type (SCM self
)
257 = gdbscm_arch_builtin_type (self
, FUNC_NAME
)->builtin_short
;
259 return tyscm_scm_from_type (type
);
262 /* (arch-int-type <gdb:arch>) -> <gdb:type> */
265 gdbscm_arch_int_type (SCM self
)
268 = gdbscm_arch_builtin_type (self
, FUNC_NAME
)->builtin_int
;
270 return tyscm_scm_from_type (type
);
273 /* (arch-long-type <gdb:arch>) -> <gdb:type> */
276 gdbscm_arch_long_type (SCM self
)
279 = gdbscm_arch_builtin_type (self
, FUNC_NAME
)->builtin_long
;
281 return tyscm_scm_from_type (type
);
284 /* (arch-schar-type <gdb:arch>) -> <gdb:type> */
287 gdbscm_arch_schar_type (SCM self
)
290 = gdbscm_arch_builtin_type (self
, FUNC_NAME
)->builtin_signed_char
;
292 return tyscm_scm_from_type (type
);
295 /* (arch-uchar-type <gdb:arch>) -> <gdb:type> */
298 gdbscm_arch_uchar_type (SCM self
)
301 = gdbscm_arch_builtin_type (self
, FUNC_NAME
)->builtin_unsigned_char
;
303 return tyscm_scm_from_type (type
);
306 /* (arch-ushort-type <gdb:arch>) -> <gdb:type> */
309 gdbscm_arch_ushort_type (SCM self
)
312 = gdbscm_arch_builtin_type (self
, FUNC_NAME
)->builtin_unsigned_short
;
314 return tyscm_scm_from_type (type
);
317 /* (arch-uint-type <gdb:arch>) -> <gdb:type> */
320 gdbscm_arch_uint_type (SCM self
)
323 = gdbscm_arch_builtin_type (self
, FUNC_NAME
)->builtin_unsigned_int
;
325 return tyscm_scm_from_type (type
);
328 /* (arch-ulong-type <gdb:arch>) -> <gdb:type> */
331 gdbscm_arch_ulong_type (SCM self
)
334 = gdbscm_arch_builtin_type (self
, FUNC_NAME
)->builtin_unsigned_long
;
336 return tyscm_scm_from_type (type
);
339 /* (arch-float-type <gdb:arch>) -> <gdb:type> */
342 gdbscm_arch_float_type (SCM self
)
345 = gdbscm_arch_builtin_type (self
, FUNC_NAME
)->builtin_float
;
347 return tyscm_scm_from_type (type
);
350 /* (arch-double-type <gdb:arch>) -> <gdb:type> */
353 gdbscm_arch_double_type (SCM self
)
356 = gdbscm_arch_builtin_type (self
, FUNC_NAME
)->builtin_double
;
358 return tyscm_scm_from_type (type
);
361 /* (arch-longdouble-type <gdb:arch>) -> <gdb:type> */
364 gdbscm_arch_longdouble_type (SCM self
)
367 = gdbscm_arch_builtin_type (self
, FUNC_NAME
)->builtin_long_double
;
369 return tyscm_scm_from_type (type
);
372 /* (arch-bool-type <gdb:arch>) -> <gdb:type> */
375 gdbscm_arch_bool_type (SCM self
)
378 = gdbscm_arch_builtin_type (self
, FUNC_NAME
)->builtin_bool
;
380 return tyscm_scm_from_type (type
);
383 /* (arch-longlong-type <gdb:arch>) -> <gdb:type> */
386 gdbscm_arch_longlong_type (SCM self
)
389 = gdbscm_arch_builtin_type (self
, FUNC_NAME
)->builtin_long_long
;
391 return tyscm_scm_from_type (type
);
394 /* (arch-ulonglong-type <gdb:arch>) -> <gdb:type> */
397 gdbscm_arch_ulonglong_type (SCM self
)
400 = gdbscm_arch_builtin_type (self
, FUNC_NAME
)->builtin_unsigned_long_long
;
402 return tyscm_scm_from_type (type
);
405 /* (arch-int8-type <gdb:arch>) -> <gdb:type> */
408 gdbscm_arch_int8_type (SCM self
)
411 = gdbscm_arch_builtin_type (self
, FUNC_NAME
)->builtin_int8
;
413 return tyscm_scm_from_type (type
);
416 /* (arch-uint8-type <gdb:arch>) -> <gdb:type> */
419 gdbscm_arch_uint8_type (SCM self
)
422 = gdbscm_arch_builtin_type (self
, FUNC_NAME
)->builtin_uint8
;
424 return tyscm_scm_from_type (type
);
427 /* (arch-int16-type <gdb:arch>) -> <gdb:type> */
430 gdbscm_arch_int16_type (SCM self
)
433 = gdbscm_arch_builtin_type (self
, FUNC_NAME
)->builtin_int16
;
435 return tyscm_scm_from_type (type
);
438 /* (arch-uint16-type <gdb:arch>) -> <gdb:type> */
441 gdbscm_arch_uint16_type (SCM self
)
444 = gdbscm_arch_builtin_type (self
, FUNC_NAME
)->builtin_uint16
;
446 return tyscm_scm_from_type (type
);
449 /* (arch-int32-type <gdb:arch>) -> <gdb:type> */
452 gdbscm_arch_int32_type (SCM self
)
455 = gdbscm_arch_builtin_type (self
, FUNC_NAME
)->builtin_int32
;
457 return tyscm_scm_from_type (type
);
460 /* (arch-uint32-type <gdb:arch>) -> <gdb:type> */
463 gdbscm_arch_uint32_type (SCM self
)
466 = gdbscm_arch_builtin_type (self
, FUNC_NAME
)->builtin_uint32
;
468 return tyscm_scm_from_type (type
);
471 /* (arch-int64-type <gdb:arch>) -> <gdb:type> */
474 gdbscm_arch_int64_type (SCM self
)
477 = gdbscm_arch_builtin_type (self
, FUNC_NAME
)->builtin_int64
;
479 return tyscm_scm_from_type (type
);
482 /* (arch-uint64-type <gdb:arch>) -> <gdb:type> */
485 gdbscm_arch_uint64_type (SCM self
)
488 = gdbscm_arch_builtin_type (self
, FUNC_NAME
)->builtin_uint64
;
490 return tyscm_scm_from_type (type
);
493 /* Initialize the Scheme architecture support. */
495 static const scheme_function arch_functions
[] =
497 { "arch?", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_p
),
499 Return #t if the object is a <gdb:arch> object." },
501 { "current-arch", 0, 0, 0, as_a_scm_t_subr (gdbscm_current_arch
),
503 Return the <gdb:arch> object representing the architecture of the\n\
504 currently selected stack frame, if there is one, or the architecture of the\n\
505 current target if there isn't.\n\
509 { "arch-name", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_name
),
511 Return the name of the architecture." },
513 { "arch-charset", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_charset
),
515 Return name of target character set as a string." },
517 { "arch-wide-charset", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_wide_charset
),
519 Return name of target wide character set as a string." },
521 { "arch-void-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_void_type
),
523 Return the <gdb:type> object for the \"void\" type\n\
524 of the architecture." },
526 { "arch-char-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_char_type
),
528 Return the <gdb:type> object for the \"char\" type\n\
529 of the architecture." },
531 { "arch-short-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_short_type
),
533 Return the <gdb:type> object for the \"short\" type\n\
534 of the architecture." },
536 { "arch-int-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_int_type
),
538 Return the <gdb:type> object for the \"int\" type\n\
539 of the architecture." },
541 { "arch-long-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_long_type
),
543 Return the <gdb:type> object for the \"long\" type\n\
544 of the architecture." },
546 { "arch-schar-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_schar_type
),
548 Return the <gdb:type> object for the \"signed char\" type\n\
549 of the architecture." },
551 { "arch-uchar-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_uchar_type
),
553 Return the <gdb:type> object for the \"unsigned char\" type\n\
554 of the architecture." },
556 { "arch-ushort-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_ushort_type
),
558 Return the <gdb:type> object for the \"unsigned short\" type\n\
559 of the architecture." },
561 { "arch-uint-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_uint_type
),
563 Return the <gdb:type> object for the \"unsigned int\" type\n\
564 of the architecture." },
566 { "arch-ulong-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_ulong_type
),
568 Return the <gdb:type> object for the \"unsigned long\" type\n\
569 of the architecture." },
571 { "arch-float-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_float_type
),
573 Return the <gdb:type> object for the \"float\" type\n\
574 of the architecture." },
576 { "arch-double-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_double_type
),
578 Return the <gdb:type> object for the \"double\" type\n\
579 of the architecture." },
581 { "arch-longdouble-type", 1, 0, 0,
582 as_a_scm_t_subr (gdbscm_arch_longdouble_type
),
584 Return the <gdb:type> object for the \"long double\" type\n\
585 of the architecture." },
587 { "arch-bool-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_bool_type
),
589 Return the <gdb:type> object for the \"bool\" type\n\
590 of the architecture." },
592 { "arch-longlong-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_longlong_type
),
594 Return the <gdb:type> object for the \"long long\" type\n\
595 of the architecture." },
597 { "arch-ulonglong-type", 1, 0, 0,
598 as_a_scm_t_subr (gdbscm_arch_ulonglong_type
),
600 Return the <gdb:type> object for the \"unsigned long long\" type\n\
601 of the architecture." },
603 { "arch-int8-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_int8_type
),
605 Return the <gdb:type> object for the \"int8\" type\n\
606 of the architecture." },
608 { "arch-uint8-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_uint8_type
),
610 Return the <gdb:type> object for the \"uint8\" type\n\
611 of the architecture." },
613 { "arch-int16-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_int16_type
),
615 Return the <gdb:type> object for the \"int16\" type\n\
616 of the architecture." },
618 { "arch-uint16-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_uint16_type
),
620 Return the <gdb:type> object for the \"uint16\" type\n\
621 of the architecture." },
623 { "arch-int32-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_int32_type
),
625 Return the <gdb:type> object for the \"int32\" type\n\
626 of the architecture." },
628 { "arch-uint32-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_uint32_type
),
630 Return the <gdb:type> object for the \"uint32\" type\n\
631 of the architecture." },
633 { "arch-int64-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_int64_type
),
635 Return the <gdb:type> object for the \"int64\" type\n\
636 of the architecture." },
638 { "arch-uint64-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_uint64_type
),
640 Return the <gdb:type> object for the \"uint64\" type\n\
641 of the architecture." },
647 gdbscm_initialize_arches (void)
649 arch_smob_tag
= gdbscm_make_smob_type (arch_smob_name
, sizeof (arch_smob
));
650 scm_set_smob_print (arch_smob_tag
, arscm_print_arch_smob
);
652 gdbscm_define_functions (arch_functions
, 1);