loongarch.c 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* Copyright (C) 2022 Free Software Foundation, Inc.
  2. This file is part of GDB.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. #include "gdbsupport/common-defs.h"
  14. #include "loongarch.h"
  15. #include <stdlib.h>
  16. #include <unordered_map>
  17. /* Target description features. */
  18. #include "../features/loongarch/base32.c"
  19. #include "../features/loongarch/base64.c"
  20. static target_desc_up
  21. loongarch_create_target_description (const struct loongarch_gdbarch_features features)
  22. {
  23. /* Now we should create a new target description. */
  24. target_desc_up tdesc = allocate_target_description ();
  25. std::string arch_name = "loongarch";
  26. if (features.xlen == 4)
  27. arch_name.append ("32");
  28. else if (features.xlen == 8)
  29. arch_name.append ("64");
  30. set_tdesc_architecture (tdesc.get (), arch_name.c_str ());
  31. long regnum = 0;
  32. /* For now we only support creating 32-bit or 64-bit x-registers. */
  33. if (features.xlen == 4)
  34. regnum = create_feature_loongarch_base32 (tdesc.get (), regnum);
  35. else if (features.xlen == 8)
  36. regnum = create_feature_loongarch_base64 (tdesc.get (), regnum);
  37. return tdesc;
  38. }
  39. /* Wrapper used by std::unordered_map to generate hash for feature set. */
  40. struct loongarch_gdbarch_features_hasher
  41. {
  42. std::size_t
  43. operator() (const loongarch_gdbarch_features &features) const noexcept
  44. {
  45. return features.hash ();
  46. }
  47. };
  48. /* Cache of previously seen target descriptions, indexed by the feature set
  49. that created them. */
  50. static std::unordered_map<loongarch_gdbarch_features,
  51. const target_desc_up,
  52. loongarch_gdbarch_features_hasher> loongarch_tdesc_cache;
  53. const target_desc *
  54. loongarch_lookup_target_description (const struct loongarch_gdbarch_features features)
  55. {
  56. /* Lookup in the cache. If we find it then return the pointer out of
  57. the target_desc_up (which is a unique_ptr). This is safe as the
  58. loongarch_tdesc_cache will exist until GDB exits. */
  59. const auto it = loongarch_tdesc_cache.find (features);
  60. if (it != loongarch_tdesc_cache.end ())
  61. return it->second.get ();
  62. target_desc_up tdesc (loongarch_create_target_description (features));
  63. /* Add to the cache, and return a pointer borrowed from the
  64. target_desc_up. This is safe as the cache (and the pointers
  65. contained within it) are not deleted until GDB exits. */
  66. target_desc *ptr = tdesc.get ();
  67. loongarch_tdesc_cache.emplace (features, std::move (tdesc));
  68. return ptr;
  69. }