scm-arch.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. /* Scheme interface to architecture.
  2. Copyright (C) 2014-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. /* See README file in this directory for implementation notes, coding
  15. conventions, et.al. */
  16. #include "defs.h"
  17. #include "charset.h"
  18. #include "gdbarch.h"
  19. #include "arch-utils.h"
  20. #include "guile-internal.h"
  21. /* The <gdb:arch> smob. */
  22. struct arch_smob
  23. {
  24. /* This always appears first. */
  25. gdb_smob base;
  26. struct gdbarch *gdbarch;
  27. };
  28. static const char arch_smob_name[] = "gdb:arch";
  29. /* The tag Guile knows the arch smob by. */
  30. static scm_t_bits arch_smob_tag;
  31. static struct gdbarch_data *arch_object_data = NULL;
  32. static int arscm_is_arch (SCM);
  33. /* Administrivia for arch smobs. */
  34. /* The smob "print" function for <gdb:arch>. */
  35. static int
  36. arscm_print_arch_smob (SCM self, SCM port, scm_print_state *pstate)
  37. {
  38. arch_smob *a_smob = (arch_smob *) SCM_SMOB_DATA (self);
  39. struct gdbarch *gdbarch = a_smob->gdbarch;
  40. gdbscm_printf (port, "#<%s", arch_smob_name);
  41. gdbscm_printf (port, " %s", gdbarch_bfd_arch_info (gdbarch)->printable_name);
  42. scm_puts (">", port);
  43. scm_remember_upto_here_1 (self);
  44. /* Non-zero means success. */
  45. return 1;
  46. }
  47. /* Low level routine to create a <gdb:arch> object for GDBARCH. */
  48. static SCM
  49. arscm_make_arch_smob (struct gdbarch *gdbarch)
  50. {
  51. arch_smob *a_smob = (arch_smob *)
  52. scm_gc_malloc (sizeof (arch_smob), arch_smob_name);
  53. SCM a_scm;
  54. a_smob->gdbarch = gdbarch;
  55. a_scm = scm_new_smob (arch_smob_tag, (scm_t_bits) a_smob);
  56. gdbscm_init_gsmob (&a_smob->base);
  57. return a_scm;
  58. }
  59. /* Return the gdbarch field of A_SMOB. */
  60. struct gdbarch *
  61. arscm_get_gdbarch (arch_smob *a_smob)
  62. {
  63. return a_smob->gdbarch;
  64. }
  65. /* Return non-zero if SCM is an architecture smob. */
  66. static int
  67. arscm_is_arch (SCM scm)
  68. {
  69. return SCM_SMOB_PREDICATE (arch_smob_tag, scm);
  70. }
  71. /* (arch? object) -> boolean */
  72. static SCM
  73. gdbscm_arch_p (SCM scm)
  74. {
  75. return scm_from_bool (arscm_is_arch (scm));
  76. }
  77. /* Associates an arch_object with GDBARCH as gdbarch_data via the gdbarch
  78. post init registration mechanism (gdbarch_data_register_post_init). */
  79. static void *
  80. arscm_object_data_init (struct gdbarch *gdbarch)
  81. {
  82. SCM arch_scm = arscm_make_arch_smob (gdbarch);
  83. /* This object lasts the duration of the GDB session, so there is no
  84. call to scm_gc_unprotect_object for it. */
  85. scm_gc_protect_object (arch_scm);
  86. return (void *) arch_scm;
  87. }
  88. /* Return the <gdb:arch> object corresponding to GDBARCH.
  89. The object is cached in GDBARCH so this is simple. */
  90. SCM
  91. arscm_scm_from_arch (struct gdbarch *gdbarch)
  92. {
  93. SCM a_scm = (SCM) gdbarch_data (gdbarch, arch_object_data);
  94. return a_scm;
  95. }
  96. /* Return the <gdb:arch> smob in SELF.
  97. Throws an exception if SELF is not a <gdb:arch> object. */
  98. static SCM
  99. arscm_get_arch_arg_unsafe (SCM self, int arg_pos, const char *func_name)
  100. {
  101. SCM_ASSERT_TYPE (arscm_is_arch (self), self, arg_pos, func_name,
  102. arch_smob_name);
  103. return self;
  104. }
  105. /* Return a pointer to the arch smob of SELF.
  106. Throws an exception if SELF is not a <gdb:arch> object. */
  107. arch_smob *
  108. arscm_get_arch_smob_arg_unsafe (SCM self, int arg_pos, const char *func_name)
  109. {
  110. SCM a_scm = arscm_get_arch_arg_unsafe (self, arg_pos, func_name);
  111. arch_smob *a_smob = (arch_smob *) SCM_SMOB_DATA (a_scm);
  112. return a_smob;
  113. }
  114. /* Arch methods. */
  115. /* (current-arch) -> <gdb:arch>
  116. Return the architecture of the currently selected stack frame,
  117. if there is one, or the current target if there isn't. */
  118. static SCM
  119. gdbscm_current_arch (void)
  120. {
  121. return arscm_scm_from_arch (get_current_arch ());
  122. }
  123. /* (arch-name <gdb:arch>) -> string
  124. Return the name of the architecture as a string value. */
  125. static SCM
  126. gdbscm_arch_name (SCM self)
  127. {
  128. arch_smob *a_smob
  129. = arscm_get_arch_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
  130. struct gdbarch *gdbarch = a_smob->gdbarch;
  131. const char *name;
  132. name = (gdbarch_bfd_arch_info (gdbarch))->printable_name;
  133. return gdbscm_scm_from_c_string (name);
  134. }
  135. /* (arch-charset <gdb:arch>) -> string */
  136. static SCM
  137. gdbscm_arch_charset (SCM self)
  138. {
  139. arch_smob *a_smob
  140. =arscm_get_arch_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
  141. struct gdbarch *gdbarch = a_smob->gdbarch;
  142. return gdbscm_scm_from_c_string (target_charset (gdbarch));
  143. }
  144. /* (arch-wide-charset <gdb:arch>) -> string */
  145. static SCM
  146. gdbscm_arch_wide_charset (SCM self)
  147. {
  148. arch_smob *a_smob
  149. = arscm_get_arch_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
  150. struct gdbarch *gdbarch = a_smob->gdbarch;
  151. return gdbscm_scm_from_c_string (target_wide_charset (gdbarch));
  152. }
  153. /* Builtin types.
  154. The order the types are defined here follows the order in
  155. struct builtin_type. */
  156. /* Helper routine to return a builtin type for <gdb:arch> object SELF.
  157. OFFSET is offsetof (builtin_type, the_type).
  158. Throws an exception if SELF is not a <gdb:arch> object. */
  159. static const struct builtin_type *
  160. gdbscm_arch_builtin_type (SCM self, const char *func_name)
  161. {
  162. arch_smob *a_smob
  163. = arscm_get_arch_smob_arg_unsafe (self, SCM_ARG1, func_name);
  164. struct gdbarch *gdbarch = a_smob->gdbarch;
  165. return builtin_type (gdbarch);
  166. }
  167. /* (arch-void-type <gdb:arch>) -> <gdb:type> */
  168. static SCM
  169. gdbscm_arch_void_type (SCM self)
  170. {
  171. struct type *type
  172. = gdbscm_arch_builtin_type (self, FUNC_NAME)->builtin_void;
  173. return tyscm_scm_from_type (type);
  174. }
  175. /* (arch-char-type <gdb:arch>) -> <gdb:type> */
  176. static SCM
  177. gdbscm_arch_char_type (SCM self)
  178. {
  179. struct type *type
  180. = gdbscm_arch_builtin_type (self, FUNC_NAME)->builtin_char;
  181. return tyscm_scm_from_type (type);
  182. }
  183. /* (arch-short-type <gdb:arch>) -> <gdb:type> */
  184. static SCM
  185. gdbscm_arch_short_type (SCM self)
  186. {
  187. struct type *type
  188. = gdbscm_arch_builtin_type (self, FUNC_NAME)->builtin_short;
  189. return tyscm_scm_from_type (type);
  190. }
  191. /* (arch-int-type <gdb:arch>) -> <gdb:type> */
  192. static SCM
  193. gdbscm_arch_int_type (SCM self)
  194. {
  195. struct type *type
  196. = gdbscm_arch_builtin_type (self, FUNC_NAME)->builtin_int;
  197. return tyscm_scm_from_type (type);
  198. }
  199. /* (arch-long-type <gdb:arch>) -> <gdb:type> */
  200. static SCM
  201. gdbscm_arch_long_type (SCM self)
  202. {
  203. struct type *type
  204. = gdbscm_arch_builtin_type (self, FUNC_NAME)->builtin_long;
  205. return tyscm_scm_from_type (type);
  206. }
  207. /* (arch-schar-type <gdb:arch>) -> <gdb:type> */
  208. static SCM
  209. gdbscm_arch_schar_type (SCM self)
  210. {
  211. struct type *type
  212. = gdbscm_arch_builtin_type (self, FUNC_NAME)->builtin_signed_char;
  213. return tyscm_scm_from_type (type);
  214. }
  215. /* (arch-uchar-type <gdb:arch>) -> <gdb:type> */
  216. static SCM
  217. gdbscm_arch_uchar_type (SCM self)
  218. {
  219. struct type *type
  220. = gdbscm_arch_builtin_type (self, FUNC_NAME)->builtin_unsigned_char;
  221. return tyscm_scm_from_type (type);
  222. }
  223. /* (arch-ushort-type <gdb:arch>) -> <gdb:type> */
  224. static SCM
  225. gdbscm_arch_ushort_type (SCM self)
  226. {
  227. struct type *type
  228. = gdbscm_arch_builtin_type (self, FUNC_NAME)->builtin_unsigned_short;
  229. return tyscm_scm_from_type (type);
  230. }
  231. /* (arch-uint-type <gdb:arch>) -> <gdb:type> */
  232. static SCM
  233. gdbscm_arch_uint_type (SCM self)
  234. {
  235. struct type *type
  236. = gdbscm_arch_builtin_type (self, FUNC_NAME)->builtin_unsigned_int;
  237. return tyscm_scm_from_type (type);
  238. }
  239. /* (arch-ulong-type <gdb:arch>) -> <gdb:type> */
  240. static SCM
  241. gdbscm_arch_ulong_type (SCM self)
  242. {
  243. struct type *type
  244. = gdbscm_arch_builtin_type (self, FUNC_NAME)->builtin_unsigned_long;
  245. return tyscm_scm_from_type (type);
  246. }
  247. /* (arch-float-type <gdb:arch>) -> <gdb:type> */
  248. static SCM
  249. gdbscm_arch_float_type (SCM self)
  250. {
  251. struct type *type
  252. = gdbscm_arch_builtin_type (self, FUNC_NAME)->builtin_float;
  253. return tyscm_scm_from_type (type);
  254. }
  255. /* (arch-double-type <gdb:arch>) -> <gdb:type> */
  256. static SCM
  257. gdbscm_arch_double_type (SCM self)
  258. {
  259. struct type *type
  260. = gdbscm_arch_builtin_type (self, FUNC_NAME)->builtin_double;
  261. return tyscm_scm_from_type (type);
  262. }
  263. /* (arch-longdouble-type <gdb:arch>) -> <gdb:type> */
  264. static SCM
  265. gdbscm_arch_longdouble_type (SCM self)
  266. {
  267. struct type *type
  268. = gdbscm_arch_builtin_type (self, FUNC_NAME)->builtin_long_double;
  269. return tyscm_scm_from_type (type);
  270. }
  271. /* (arch-bool-type <gdb:arch>) -> <gdb:type> */
  272. static SCM
  273. gdbscm_arch_bool_type (SCM self)
  274. {
  275. struct type *type
  276. = gdbscm_arch_builtin_type (self, FUNC_NAME)->builtin_bool;
  277. return tyscm_scm_from_type (type);
  278. }
  279. /* (arch-longlong-type <gdb:arch>) -> <gdb:type> */
  280. static SCM
  281. gdbscm_arch_longlong_type (SCM self)
  282. {
  283. struct type *type
  284. = gdbscm_arch_builtin_type (self, FUNC_NAME)->builtin_long_long;
  285. return tyscm_scm_from_type (type);
  286. }
  287. /* (arch-ulonglong-type <gdb:arch>) -> <gdb:type> */
  288. static SCM
  289. gdbscm_arch_ulonglong_type (SCM self)
  290. {
  291. struct type *type
  292. = gdbscm_arch_builtin_type (self, FUNC_NAME)->builtin_unsigned_long_long;
  293. return tyscm_scm_from_type (type);
  294. }
  295. /* (arch-int8-type <gdb:arch>) -> <gdb:type> */
  296. static SCM
  297. gdbscm_arch_int8_type (SCM self)
  298. {
  299. struct type *type
  300. = gdbscm_arch_builtin_type (self, FUNC_NAME)->builtin_int8;
  301. return tyscm_scm_from_type (type);
  302. }
  303. /* (arch-uint8-type <gdb:arch>) -> <gdb:type> */
  304. static SCM
  305. gdbscm_arch_uint8_type (SCM self)
  306. {
  307. struct type *type
  308. = gdbscm_arch_builtin_type (self, FUNC_NAME)->builtin_uint8;
  309. return tyscm_scm_from_type (type);
  310. }
  311. /* (arch-int16-type <gdb:arch>) -> <gdb:type> */
  312. static SCM
  313. gdbscm_arch_int16_type (SCM self)
  314. {
  315. struct type *type
  316. = gdbscm_arch_builtin_type (self, FUNC_NAME)->builtin_int16;
  317. return tyscm_scm_from_type (type);
  318. }
  319. /* (arch-uint16-type <gdb:arch>) -> <gdb:type> */
  320. static SCM
  321. gdbscm_arch_uint16_type (SCM self)
  322. {
  323. struct type *type
  324. = gdbscm_arch_builtin_type (self, FUNC_NAME)->builtin_uint16;
  325. return tyscm_scm_from_type (type);
  326. }
  327. /* (arch-int32-type <gdb:arch>) -> <gdb:type> */
  328. static SCM
  329. gdbscm_arch_int32_type (SCM self)
  330. {
  331. struct type *type
  332. = gdbscm_arch_builtin_type (self, FUNC_NAME)->builtin_int32;
  333. return tyscm_scm_from_type (type);
  334. }
  335. /* (arch-uint32-type <gdb:arch>) -> <gdb:type> */
  336. static SCM
  337. gdbscm_arch_uint32_type (SCM self)
  338. {
  339. struct type *type
  340. = gdbscm_arch_builtin_type (self, FUNC_NAME)->builtin_uint32;
  341. return tyscm_scm_from_type (type);
  342. }
  343. /* (arch-int64-type <gdb:arch>) -> <gdb:type> */
  344. static SCM
  345. gdbscm_arch_int64_type (SCM self)
  346. {
  347. struct type *type
  348. = gdbscm_arch_builtin_type (self, FUNC_NAME)->builtin_int64;
  349. return tyscm_scm_from_type (type);
  350. }
  351. /* (arch-uint64-type <gdb:arch>) -> <gdb:type> */
  352. static SCM
  353. gdbscm_arch_uint64_type (SCM self)
  354. {
  355. struct type *type
  356. = gdbscm_arch_builtin_type (self, FUNC_NAME)->builtin_uint64;
  357. return tyscm_scm_from_type (type);
  358. }
  359. /* Initialize the Scheme architecture support. */
  360. static const scheme_function arch_functions[] =
  361. {
  362. { "arch?", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_p),
  363. "\
  364. Return #t if the object is a <gdb:arch> object." },
  365. { "current-arch", 0, 0, 0, as_a_scm_t_subr (gdbscm_current_arch),
  366. "\
  367. Return the <gdb:arch> object representing the architecture of the\n\
  368. currently selected stack frame, if there is one, or the architecture of the\n\
  369. current target if there isn't.\n\
  370. \n\
  371. Arguments: none" },
  372. { "arch-name", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_name),
  373. "\
  374. Return the name of the architecture." },
  375. { "arch-charset", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_charset),
  376. "\
  377. Return name of target character set as a string." },
  378. { "arch-wide-charset", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_wide_charset),
  379. "\
  380. Return name of target wide character set as a string." },
  381. { "arch-void-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_void_type),
  382. "\
  383. Return the <gdb:type> object for the \"void\" type\n\
  384. of the architecture." },
  385. { "arch-char-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_char_type),
  386. "\
  387. Return the <gdb:type> object for the \"char\" type\n\
  388. of the architecture." },
  389. { "arch-short-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_short_type),
  390. "\
  391. Return the <gdb:type> object for the \"short\" type\n\
  392. of the architecture." },
  393. { "arch-int-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_int_type),
  394. "\
  395. Return the <gdb:type> object for the \"int\" type\n\
  396. of the architecture." },
  397. { "arch-long-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_long_type),
  398. "\
  399. Return the <gdb:type> object for the \"long\" type\n\
  400. of the architecture." },
  401. { "arch-schar-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_schar_type),
  402. "\
  403. Return the <gdb:type> object for the \"signed char\" type\n\
  404. of the architecture." },
  405. { "arch-uchar-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_uchar_type),
  406. "\
  407. Return the <gdb:type> object for the \"unsigned char\" type\n\
  408. of the architecture." },
  409. { "arch-ushort-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_ushort_type),
  410. "\
  411. Return the <gdb:type> object for the \"unsigned short\" type\n\
  412. of the architecture." },
  413. { "arch-uint-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_uint_type),
  414. "\
  415. Return the <gdb:type> object for the \"unsigned int\" type\n\
  416. of the architecture." },
  417. { "arch-ulong-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_ulong_type),
  418. "\
  419. Return the <gdb:type> object for the \"unsigned long\" type\n\
  420. of the architecture." },
  421. { "arch-float-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_float_type),
  422. "\
  423. Return the <gdb:type> object for the \"float\" type\n\
  424. of the architecture." },
  425. { "arch-double-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_double_type),
  426. "\
  427. Return the <gdb:type> object for the \"double\" type\n\
  428. of the architecture." },
  429. { "arch-longdouble-type", 1, 0, 0,
  430. as_a_scm_t_subr (gdbscm_arch_longdouble_type),
  431. "\
  432. Return the <gdb:type> object for the \"long double\" type\n\
  433. of the architecture." },
  434. { "arch-bool-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_bool_type),
  435. "\
  436. Return the <gdb:type> object for the \"bool\" type\n\
  437. of the architecture." },
  438. { "arch-longlong-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_longlong_type),
  439. "\
  440. Return the <gdb:type> object for the \"long long\" type\n\
  441. of the architecture." },
  442. { "arch-ulonglong-type", 1, 0, 0,
  443. as_a_scm_t_subr (gdbscm_arch_ulonglong_type),
  444. "\
  445. Return the <gdb:type> object for the \"unsigned long long\" type\n\
  446. of the architecture." },
  447. { "arch-int8-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_int8_type),
  448. "\
  449. Return the <gdb:type> object for the \"int8\" type\n\
  450. of the architecture." },
  451. { "arch-uint8-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_uint8_type),
  452. "\
  453. Return the <gdb:type> object for the \"uint8\" type\n\
  454. of the architecture." },
  455. { "arch-int16-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_int16_type),
  456. "\
  457. Return the <gdb:type> object for the \"int16\" type\n\
  458. of the architecture." },
  459. { "arch-uint16-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_uint16_type),
  460. "\
  461. Return the <gdb:type> object for the \"uint16\" type\n\
  462. of the architecture." },
  463. { "arch-int32-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_int32_type),
  464. "\
  465. Return the <gdb:type> object for the \"int32\" type\n\
  466. of the architecture." },
  467. { "arch-uint32-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_uint32_type),
  468. "\
  469. Return the <gdb:type> object for the \"uint32\" type\n\
  470. of the architecture." },
  471. { "arch-int64-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_int64_type),
  472. "\
  473. Return the <gdb:type> object for the \"int64\" type\n\
  474. of the architecture." },
  475. { "arch-uint64-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_arch_uint64_type),
  476. "\
  477. Return the <gdb:type> object for the \"uint64\" type\n\
  478. of the architecture." },
  479. END_FUNCTIONS
  480. };
  481. void
  482. gdbscm_initialize_arches (void)
  483. {
  484. arch_smob_tag = gdbscm_make_smob_type (arch_smob_name, sizeof (arch_smob));
  485. scm_set_smob_print (arch_smob_tag, arscm_print_arch_smob);
  486. gdbscm_define_functions (arch_functions, 1);
  487. }
  488. void _initialize_scm_arch ();
  489. void
  490. _initialize_scm_arch ()
  491. {
  492. arch_object_data
  493. = gdbarch_data_register_post_init (arscm_object_data_init);
  494. }