Automatic Copyright Year update after running gdb/copyright.py
[deliverable/binutils-gdb.git] / gdb / guile / scm-arch.c
CommitLineData
ed3ef339
DE
1/* Scheme interface to architecture.
2
88b9d363 3 Copyright (C) 2014-2022 Free Software Foundation, Inc.
ed3ef339
DE
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20/* See README file in this directory for implementation notes, coding
21 conventions, et.al. */
22
23#include "defs.h"
24#include "charset.h"
25#include "gdbarch.h"
26#include "arch-utils.h"
27#include "guile-internal.h"
28
f99b5177 29/* The <gdb:arch> smob. */
ed3ef339 30
f99b5177 31struct arch_smob
ed3ef339
DE
32{
33 /* This always appears first. */
34 gdb_smob base;
35
36 struct gdbarch *gdbarch;
37};
38
39static const char arch_smob_name[] = "gdb:arch";
40
41/* The tag Guile knows the arch smob by. */
42static scm_t_bits arch_smob_tag;
43
44static struct gdbarch_data *arch_object_data = NULL;
45
46static int arscm_is_arch (SCM);
47\f
48/* Administrivia for arch smobs. */
49
ed3ef339
DE
50/* The smob "print" function for <gdb:arch>. */
51
52static int
53arscm_print_arch_smob (SCM self, SCM port, scm_print_state *pstate)
54{
55 arch_smob *a_smob = (arch_smob *) SCM_SMOB_DATA (self);
56 struct gdbarch *gdbarch = a_smob->gdbarch;
57
58 gdbscm_printf (port, "#<%s", arch_smob_name);
59 gdbscm_printf (port, " %s", gdbarch_bfd_arch_info (gdbarch)->printable_name);
60 scm_puts (">", port);
61
62 scm_remember_upto_here_1 (self);
63
64 /* Non-zero means success. */
65 return 1;
66}
67
68/* Low level routine to create a <gdb:arch> object for GDBARCH. */
69
70static SCM
71arscm_make_arch_smob (struct gdbarch *gdbarch)
72{
73 arch_smob *a_smob = (arch_smob *)
74 scm_gc_malloc (sizeof (arch_smob), arch_smob_name);
75 SCM a_scm;
76
77 a_smob->gdbarch = gdbarch;
78 a_scm = scm_new_smob (arch_smob_tag, (scm_t_bits) a_smob);
79 gdbscm_init_gsmob (&a_smob->base);
80
81 return a_scm;
82}
83
84/* Return the gdbarch field of A_SMOB. */
85
86struct gdbarch *
87arscm_get_gdbarch (arch_smob *a_smob)
88{
89 return a_smob->gdbarch;
90}
91
92/* Return non-zero if SCM is an architecture smob. */
93
94static int
95arscm_is_arch (SCM scm)
96{
97 return SCM_SMOB_PREDICATE (arch_smob_tag, scm);
98}
99
100/* (arch? object) -> boolean */
101
102static SCM
103gdbscm_arch_p (SCM scm)
104{
105 return scm_from_bool (arscm_is_arch (scm));
106}
107
108/* Associates an arch_object with GDBARCH as gdbarch_data via the gdbarch
109 post init registration mechanism (gdbarch_data_register_post_init). */
110
111static void *
112arscm_object_data_init (struct gdbarch *gdbarch)
113{
114 SCM arch_scm = arscm_make_arch_smob (gdbarch);
115
116 /* This object lasts the duration of the GDB session, so there is no
117 call to scm_gc_unprotect_object for it. */
118 scm_gc_protect_object (arch_scm);
119
120 return (void *) arch_scm;
121}
122
123/* Return the <gdb:arch> object corresponding to GDBARCH.
124 The object is cached in GDBARCH so this is simple. */
125
126SCM
127arscm_scm_from_arch (struct gdbarch *gdbarch)
128{
129 SCM a_scm = (SCM) gdbarch_data (gdbarch, arch_object_data);
130
131 return a_scm;
132}
133
134/* Return the <gdb:arch> smob in SELF.
135 Throws an exception if SELF is not a <gdb:arch> object. */
136
137static SCM
138arscm_get_arch_arg_unsafe (SCM self, int arg_pos, const char *func_name)
139{
140 SCM_ASSERT_TYPE (arscm_is_arch (self), self, arg_pos, func_name,
141 arch_smob_name);
142
143 return self;
144}
145
146/* Return a pointer to the arch smob of SELF.
147 Throws an exception if SELF is not a <gdb:arch> object. */
148
149arch_smob *
150arscm_get_arch_smob_arg_unsafe (SCM self, int arg_pos, const char *func_name)
151{
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);
154
155 return a_smob;
156}
157\f
158/* Arch methods. */
159
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. */
163
164static SCM
165gdbscm_current_arch (void)
166{
167 return arscm_scm_from_arch (get_current_arch ());
168}
169
170/* (arch-name <gdb:arch>) -> string
171 Return the name of the architecture as a string value. */
172
173static SCM
174gdbscm_arch_name (SCM self)
175{
176 arch_smob *a_smob
177 = arscm_get_arch_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
178 struct gdbarch *gdbarch = a_smob->gdbarch;
179 const char *name;
180
181 name = (gdbarch_bfd_arch_info (gdbarch))->printable_name;
182
183 return gdbscm_scm_from_c_string (name);
184}
185
186/* (arch-charset <gdb:arch>) -> string */
187
188static SCM
189gdbscm_arch_charset (SCM self)
190{
191 arch_smob *a_smob
192 =arscm_get_arch_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
193 struct gdbarch *gdbarch = a_smob->gdbarch;
194
195 return gdbscm_scm_from_c_string (target_charset (gdbarch));
196}
197
198/* (arch-wide-charset <gdb:arch>) -> string */
199
200static SCM
201gdbscm_arch_wide_charset (SCM self)
202{
203 arch_smob *a_smob
204 = arscm_get_arch_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
205 struct gdbarch *gdbarch = a_smob->gdbarch;
206
207 return gdbscm_scm_from_c_string (target_wide_charset (gdbarch));
208}
209\f
210/* Builtin types.
211
212 The order the types are defined here follows the order in
213 struct builtin_type. */
214
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. */
218
219static const struct builtin_type *
220gdbscm_arch_builtin_type (SCM self, const char *func_name)
221{
222 arch_smob *a_smob
223 = arscm_get_arch_smob_arg_unsafe (self, SCM_ARG1, func_name);
224 struct gdbarch *gdbarch = a_smob->gdbarch;
225
226 return builtin_type (gdbarch);
227}
228
229/* (arch-void-type <gdb:arch>) -> <gdb:type> */
230
231static SCM
232gdbscm_arch_void_type (SCM self)
233{
234 struct type *type
235 = gdbscm_arch_builtin_type (self, FUNC_NAME)->builtin_void;
236
237 return tyscm_scm_from_type (type);
238}
239
240/* (arch-char-type <gdb:arch>) -> <gdb:type> */
241
242static SCM
243gdbscm_arch_char_type (SCM self)
244{
245 struct type *type
246 = gdbscm_arch_builtin_type (self, FUNC_NAME)->builtin_char;
247
248 return tyscm_scm_from_type (type);
249}
250
251/* (arch-short-type <gdb:arch>) -> <gdb:type> */
252
253static SCM
254gdbscm_arch_short_type (SCM self)
255{
256 struct type *type
257 = gdbscm_arch_builtin_type (self, FUNC_NAME)->builtin_short;
258
259 return tyscm_scm_from_type (type);
260}
261
262/* (arch-int-type <gdb:arch>) -> <gdb:type> */
263
264static SCM
265gdbscm_arch_int_type (SCM self)
266{
267 struct type *type
268 = gdbscm_arch_builtin_type (self, FUNC_NAME)->builtin_int;
269
270 return tyscm_scm_from_type (type);
271}
272
273/* (arch-long-type <gdb:arch>) -> <gdb:type> */
274
275static SCM
276gdbscm_arch_long_type (SCM self)
277{
278 struct type *type
279 = gdbscm_arch_builtin_type (self, FUNC_NAME)->builtin_long;
280
281 return tyscm_scm_from_type (type);
282}
283
284/* (arch-schar-type <gdb:arch>) -> <gdb:type> */
285
286static SCM
287gdbscm_arch_schar_type (SCM self)
288{
289 struct type *type
290 = gdbscm_arch_builtin_type (self, FUNC_NAME)->builtin_signed_char;
291
292 return tyscm_scm_from_type (type);
293}
294
295/* (arch-uchar-type <gdb:arch>) -> <gdb:type> */
296
297static SCM
298gdbscm_arch_uchar_type (SCM self)
299{
300 struct type *type
301 = gdbscm_arch_builtin_type (self, FUNC_NAME)->builtin_unsigned_char;
302
303 return tyscm_scm_from_type (type);
304}
305
306/* (arch-ushort-type <gdb:arch>) -> <gdb:type> */
307
308static SCM
309gdbscm_arch_ushort_type (SCM self)
310{
311 struct type *type
312 = gdbscm_arch_builtin_type (self, FUNC_NAME)->builtin_unsigned_short;
313
314 return tyscm_scm_from_type (type);
315}
316
317/* (arch-uint-type <gdb:arch>) -> <gdb:type> */
318
319static SCM
320gdbscm_arch_uint_type (SCM self)
321{
322 struct type *type
323 = gdbscm_arch_builtin_type (self, FUNC_NAME)->builtin_unsigned_int;
324
325 return tyscm_scm_from_type (type);
326}
327
328/* (arch-ulong-type <gdb:arch>) -> <gdb:type> */
329
330static SCM
331gdbscm_arch_ulong_type (SCM self)
332{
333 struct type *type
334 = gdbscm_arch_builtin_type (self, FUNC_NAME)->builtin_unsigned_long;
335
336 return tyscm_scm_from_type (type);
337}
338
339/* (arch-float-type <gdb:arch>) -> <gdb:type> */
340
341static SCM
342gdbscm_arch_float_type (SCM self)
343{
344 struct type *type
345 = gdbscm_arch_builtin_type (self, FUNC_NAME)->builtin_float;
346
347 return tyscm_scm_from_type (type);
348}
349
350/* (arch-double-type <gdb:arch>) -> <gdb:type> */
351
352static SCM
353gdbscm_arch_double_type (SCM self)
354{
355 struct type *type
356 = gdbscm_arch_builtin_type (self, FUNC_NAME)->builtin_double;
357
358 return tyscm_scm_from_type (type);
359}
360
361/* (arch-longdouble-type <gdb:arch>) -> <gdb:type> */
362
363static SCM
364gdbscm_arch_longdouble_type (SCM self)
365{
366 struct type *type
367 = gdbscm_arch_builtin_type (self, FUNC_NAME)->builtin_long_double;
368
369 return tyscm_scm_from_type (type);
370}
371
372/* (arch-bool-type <gdb:arch>) -> <gdb:type> */
373
374static SCM
375gdbscm_arch_bool_type (SCM self)
376{
377 struct type *type
378 = gdbscm_arch_builtin_type (self, FUNC_NAME)->builtin_bool;
379
380 return tyscm_scm_from_type (type);
381}
382
383/* (arch-longlong-type <gdb:arch>) -> <gdb:type> */
384
385static SCM
386gdbscm_arch_longlong_type (SCM self)
387{
388 struct type *type
389 = gdbscm_arch_builtin_type (self, FUNC_NAME)->builtin_long_long;
390
391 return tyscm_scm_from_type (type);
392}
393
394/* (arch-ulonglong-type <gdb:arch>) -> <gdb:type> */
395
396static SCM
397gdbscm_arch_ulonglong_type (SCM self)
398{
399 struct type *type
400 = gdbscm_arch_builtin_type (self, FUNC_NAME)->builtin_unsigned_long_long;
401
402 return tyscm_scm_from_type (type);
403}
404
405/* (arch-int8-type <gdb:arch>) -> <gdb:type> */
406
407static SCM
408gdbscm_arch_int8_type (SCM self)
409{
410 struct type *type
411 = gdbscm_arch_builtin_type (self, FUNC_NAME)->builtin_int8;
412
413 return tyscm_scm_from_type (type);
414}
415
416/* (arch-uint8-type <gdb:arch>) -> <gdb:type> */
417
418static SCM
419gdbscm_arch_uint8_type (SCM self)
420{
421 struct type *type
422 = gdbscm_arch_builtin_type (self, FUNC_NAME)->builtin_uint8;
423
424 return tyscm_scm_from_type (type);
425}
426
427/* (arch-int16-type <gdb:arch>) -> <gdb:type> */
428
429static SCM
430gdbscm_arch_int16_type (SCM self)
431{
432 struct type *type
433 = gdbscm_arch_builtin_type (self, FUNC_NAME)->builtin_int16;
434
435 return tyscm_scm_from_type (type);
436}
437
438/* (arch-uint16-type <gdb:arch>) -> <gdb:type> */
439
440static SCM
441gdbscm_arch_uint16_type (SCM self)
442{
443 struct type *type
444 = gdbscm_arch_builtin_type (self, FUNC_NAME)->builtin_uint16;
445
446 return tyscm_scm_from_type (type);
447}
448
449/* (arch-int32-type <gdb:arch>) -> <gdb:type> */
450
451static SCM
452gdbscm_arch_int32_type (SCM self)
453{
454 struct type *type
455 = gdbscm_arch_builtin_type (self, FUNC_NAME)->builtin_int32;
456
457 return tyscm_scm_from_type (type);
458}
459
460/* (arch-uint32-type <gdb:arch>) -> <gdb:type> */
461
462static SCM
463gdbscm_arch_uint32_type (SCM self)
464{
465 struct type *type
466 = gdbscm_arch_builtin_type (self, FUNC_NAME)->builtin_uint32;
467
468 return tyscm_scm_from_type (type);
469}
470
471/* (arch-int64-type <gdb:arch>) -> <gdb:type> */
472
473static SCM
474gdbscm_arch_int64_type (SCM self)
475{
476 struct type *type
477 = gdbscm_arch_builtin_type (self, FUNC_NAME)->builtin_int64;
478
479 return tyscm_scm_from_type (type);
480}
481
482/* (arch-uint64-type <gdb:arch>) -> <gdb:type> */
483
484static SCM
485gdbscm_arch_uint64_type (SCM self)
486{
487 struct type *type
488 = gdbscm_arch_builtin_type (self, FUNC_NAME)->builtin_uint64;
489
490 return tyscm_scm_from_type (type);
491}
492\f
493/* Initialize the Scheme architecture support. */
494
495static const scheme_function arch_functions[] =
496{
72e02483 497 { "arch?", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_p),
ed3ef339
DE
498 "\
499Return #t if the object is a <gdb:arch> object." },
500
72e02483 501 { "current-arch", 0, 0, 0, as_a_scm_t_subr (gdbscm_current_arch),
ed3ef339
DE
502 "\
503Return the <gdb:arch> object representing the architecture of the\n\
504currently selected stack frame, if there is one, or the architecture of the\n\
505current target if there isn't.\n\
506\n\
507 Arguments: none" },
508
72e02483 509 { "arch-name", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_name),
ed3ef339
DE
510 "\
511Return the name of the architecture." },
512
72e02483 513 { "arch-charset", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_charset),
ed3ef339
DE
514 "\
515Return name of target character set as a string." },
516
72e02483 517 { "arch-wide-charset", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_wide_charset),
ed3ef339
DE
518 "\
519Return name of target wide character set as a string." },
520
72e02483 521 { "arch-void-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_void_type),
ed3ef339
DE
522 "\
523Return the <gdb:type> object for the \"void\" type\n\
524of the architecture." },
525
72e02483 526 { "arch-char-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_char_type),
ed3ef339
DE
527 "\
528Return the <gdb:type> object for the \"char\" type\n\
529of the architecture." },
530
72e02483 531 { "arch-short-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_short_type),
ed3ef339
DE
532 "\
533Return the <gdb:type> object for the \"short\" type\n\
534of the architecture." },
535
72e02483 536 { "arch-int-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_int_type),
ed3ef339
DE
537 "\
538Return the <gdb:type> object for the \"int\" type\n\
539of the architecture." },
540
72e02483 541 { "arch-long-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_long_type),
ed3ef339
DE
542 "\
543Return the <gdb:type> object for the \"long\" type\n\
544of the architecture." },
545
72e02483 546 { "arch-schar-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_schar_type),
ed3ef339
DE
547 "\
548Return the <gdb:type> object for the \"signed char\" type\n\
549of the architecture." },
550
72e02483 551 { "arch-uchar-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_uchar_type),
ed3ef339
DE
552 "\
553Return the <gdb:type> object for the \"unsigned char\" type\n\
554of the architecture." },
555
72e02483 556 { "arch-ushort-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_ushort_type),
ed3ef339
DE
557 "\
558Return the <gdb:type> object for the \"unsigned short\" type\n\
559of the architecture." },
560
72e02483 561 { "arch-uint-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_uint_type),
ed3ef339
DE
562 "\
563Return the <gdb:type> object for the \"unsigned int\" type\n\
564of the architecture." },
565
72e02483 566 { "arch-ulong-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_ulong_type),
ed3ef339
DE
567 "\
568Return the <gdb:type> object for the \"unsigned long\" type\n\
569of the architecture." },
570
72e02483 571 { "arch-float-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_float_type),
ed3ef339
DE
572 "\
573Return the <gdb:type> object for the \"float\" type\n\
574of the architecture." },
575
72e02483 576 { "arch-double-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_double_type),
ed3ef339
DE
577 "\
578Return the <gdb:type> object for the \"double\" type\n\
579of the architecture." },
580
72e02483
PA
581 { "arch-longdouble-type", 1, 0, 0,
582 as_a_scm_t_subr (gdbscm_arch_longdouble_type),
ed3ef339
DE
583 "\
584Return the <gdb:type> object for the \"long double\" type\n\
585of the architecture." },
586
72e02483 587 { "arch-bool-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_bool_type),
ed3ef339
DE
588 "\
589Return the <gdb:type> object for the \"bool\" type\n\
590of the architecture." },
591
72e02483 592 { "arch-longlong-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_longlong_type),
ed3ef339
DE
593 "\
594Return the <gdb:type> object for the \"long long\" type\n\
595of the architecture." },
596
597 { "arch-ulonglong-type", 1, 0, 0,
72e02483 598 as_a_scm_t_subr (gdbscm_arch_ulonglong_type),
ed3ef339
DE
599 "\
600Return the <gdb:type> object for the \"unsigned long long\" type\n\
601of the architecture." },
602
72e02483 603 { "arch-int8-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_int8_type),
ed3ef339
DE
604 "\
605Return the <gdb:type> object for the \"int8\" type\n\
606of the architecture." },
607
72e02483 608 { "arch-uint8-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_uint8_type),
ed3ef339
DE
609 "\
610Return the <gdb:type> object for the \"uint8\" type\n\
611of the architecture." },
612
72e02483 613 { "arch-int16-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_int16_type),
ed3ef339
DE
614 "\
615Return the <gdb:type> object for the \"int16\" type\n\
616of the architecture." },
617
72e02483 618 { "arch-uint16-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_uint16_type),
ed3ef339
DE
619 "\
620Return the <gdb:type> object for the \"uint16\" type\n\
621of the architecture." },
622
72e02483 623 { "arch-int32-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_int32_type),
ed3ef339
DE
624 "\
625Return the <gdb:type> object for the \"int32\" type\n\
626of the architecture." },
627
72e02483 628 { "arch-uint32-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_uint32_type),
ed3ef339
DE
629 "\
630Return the <gdb:type> object for the \"uint32\" type\n\
631of the architecture." },
632
72e02483 633 { "arch-int64-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_int64_type),
ed3ef339
DE
634 "\
635Return the <gdb:type> object for the \"int64\" type\n\
636of the architecture." },
637
72e02483 638 { "arch-uint64-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_uint64_type),
ed3ef339
DE
639 "\
640Return the <gdb:type> object for the \"uint64\" type\n\
641of the architecture." },
642
643 END_FUNCTIONS
644};
645
646void
647gdbscm_initialize_arches (void)
648{
649 arch_smob_tag = gdbscm_make_smob_type (arch_smob_name, sizeof (arch_smob));
ed3ef339
DE
650 scm_set_smob_print (arch_smob_tag, arscm_print_arch_smob);
651
652 gdbscm_define_functions (arch_functions, 1);
880ae75a 653}
ed3ef339 654
880ae75a
AB
655void _initialize_scm_arch ();
656void
657_initialize_scm_arch ()
658{
ed3ef339
DE
659 arch_object_data
660 = gdbarch_data_register_post_init (arscm_object_data_init);
661}
This page took 0.753399 seconds and 4 git commands to generate.