blob: be3f84b06aa6cee7b473bfbdd875c40f246be446 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- ArchSpec.cpp --------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Core/ArchSpec.h"
11
Eli Friedman50fac2f2010-06-11 04:26:08 +000012#include <stdio.h>
Jason Molendadfa424c2012-09-18 23:27:18 +000013#include <errno.h>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000014
15#include <string>
16
Saleem Abdulrasool28606952014-06-27 05:17:41 +000017#include "llvm/ADT/STLExtras.h"
Charles Davis237ad972013-08-27 05:04:33 +000018#include "llvm/Support/COFF.h"
Greg Clayton41f92322010-06-11 03:25:34 +000019#include "llvm/Support/ELF.h"
Stephen Wilsonfacebfc2011-02-24 19:13:58 +000020#include "llvm/Support/Host.h"
Zachary Turner50232572015-03-18 21:31:45 +000021
Greg Claytone795f1b2012-08-08 01:19:34 +000022#include "lldb/Core/RegularExpression.h"
Zachary Turner13b18262014-08-20 16:42:51 +000023#include "lldb/Core/StringList.h"
Greg Clayton514487e2011-02-15 21:59:32 +000024#include "lldb/Host/Endian.h"
Zachary Turner13b18262014-08-20 16:42:51 +000025#include "lldb/Host/HostInfo.h"
Greg Claytoneb0103f2011-04-07 22:46:35 +000026#include "lldb/Target/Platform.h"
Greg Claytona97c4d22014-12-09 23:31:02 +000027#include "lldb/Target/Process.h"
28#include "lldb/Target/RegisterContext.h"
29#include "lldb/Target/Thread.h"
Zachary Turner50232572015-03-18 21:31:45 +000030#include "lldb/Utility/NameMatches.h"
31#include "lldb/Utility/SafeMachO.h"
Greg Claytona97c4d22014-12-09 23:31:02 +000032#include "Plugins/Process/Utility/ARMDefines.h"
33#include "Plugins/Process/Utility/InstructionUtils.h"
Greg Clayton41f92322010-06-11 03:25:34 +000034
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035using namespace lldb;
36using namespace lldb_private;
37
Greg Clayton64195a22011-02-23 00:35:02 +000038#define ARCH_SPEC_SEPARATOR_CHAR '-'
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039
Jason Molendaba813dc2012-11-04 03:20:05 +000040
41static bool cores_match (const ArchSpec::Core core1, const ArchSpec::Core core2, bool try_inverse, bool enforce_exact_match);
42
Greg Clayton64195a22011-02-23 00:35:02 +000043namespace lldb_private {
Chris Lattner30fdc8d2010-06-08 16:52:24 +000044
Greg Clayton64195a22011-02-23 00:35:02 +000045 struct CoreDefinition
46 {
47 ByteOrder default_byte_order;
48 uint32_t addr_byte_size;
Greg Clayton357132e2011-03-26 19:14:58 +000049 uint32_t min_opcode_byte_size;
50 uint32_t max_opcode_byte_size;
Greg Clayton64195a22011-02-23 00:35:02 +000051 llvm::Triple::ArchType machine;
52 ArchSpec::Core core;
Greg Clayton56b79682014-07-23 18:12:06 +000053 const char * const name;
Greg Clayton64195a22011-02-23 00:35:02 +000054 };
55
56}
57
58// This core information can be looked using the ArchSpec::Core as the index
Greg Clayton56b79682014-07-23 18:12:06 +000059static const CoreDefinition g_core_definitions[] =
Chris Lattner30fdc8d2010-06-08 16:52:24 +000060{
Greg Clayton357132e2011-03-26 19:14:58 +000061 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_generic , "arm" },
62 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv4 , "armv4" },
63 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv4t , "armv4t" },
64 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv5 , "armv5" },
Greg Claytonb5c39fe2011-12-16 18:15:52 +000065 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv5e , "armv5e" },
Greg Clayton357132e2011-03-26 19:14:58 +000066 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv5t , "armv5t" },
67 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv6 , "armv6" },
Jason Molendaa3a04522013-09-27 23:21:54 +000068 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv6m , "armv6m" },
Greg Clayton357132e2011-03-26 19:14:58 +000069 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7 , "armv7" },
70 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7f , "armv7f" },
Greg Clayton357132e2011-03-26 19:14:58 +000071 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7s , "armv7s" },
Jason Molenda7a1559c2013-03-08 01:20:17 +000072 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7k , "armv7k" },
73 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7m , "armv7m" },
74 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7em , "armv7em" },
Greg Clayton357132e2011-03-26 19:14:58 +000075 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_xscale , "xscale" },
Greg Claytonb5c39fe2011-12-16 18:15:52 +000076 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumb , "thumb" },
77 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv4t , "thumbv4t" },
78 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv5 , "thumbv5" },
79 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv5e , "thumbv5e" },
80 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv6 , "thumbv6" },
Jason Molendaa3a04522013-09-27 23:21:54 +000081 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv6m , "thumbv6m" },
Greg Claytonb5c39fe2011-12-16 18:15:52 +000082 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7 , "thumbv7" },
83 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7f , "thumbv7f" },
Greg Claytonb5c39fe2011-12-16 18:15:52 +000084 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7s , "thumbv7s" },
Jason Molenda7a1559c2013-03-08 01:20:17 +000085 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7k , "thumbv7k" },
86 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7m , "thumbv7m" },
87 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7em , "thumbv7em" },
Todd Fialad8eaa172014-07-23 14:37:35 +000088 { eByteOrderLittle, 8, 4, 4, llvm::Triple::aarch64, ArchSpec::eCore_arm_arm64 , "arm64" },
Todd Fiala02e71812014-08-28 14:32:43 +000089 { eByteOrderLittle, 8, 4, 4, llvm::Triple::aarch64, ArchSpec::eCore_arm_armv8 , "armv8" },
90 { eByteOrderLittle, 8, 4, 4, llvm::Triple::aarch64, ArchSpec::eCore_arm_aarch64 , "aarch64" },
Ed Masteb73f8442013-10-10 00:59:47 +000091
92 { eByteOrderBig , 8, 4, 4, llvm::Triple::mips64 , ArchSpec::eCore_mips64 , "mips64" },
Mohit K. Bhakkad3df471c2015-03-17 11:43:56 +000093 { eByteOrderLittle, 8, 4, 4, llvm::Triple::mips64el, ArchSpec::eCore_mips64el , "mips64el" },
Greg Clayton64195a22011-02-23 00:35:02 +000094
Justin Hibbits6256a0e2014-10-31 02:34:28 +000095 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_generic , "powerpc" },
Greg Clayton83b162d2013-08-12 18:34:04 +000096 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc601 , "ppc601" },
97 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc602 , "ppc602" },
98 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc603 , "ppc603" },
99 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc603e , "ppc603e" },
100 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc603ev , "ppc603ev" },
101 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc604 , "ppc604" },
102 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc604e , "ppc604e" },
103 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc620 , "ppc620" },
104 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc750 , "ppc750" },
105 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc7400 , "ppc7400" },
106 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc7450 , "ppc7450" },
107 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc970 , "ppc970" },
Greg Clayton64195a22011-02-23 00:35:02 +0000108
Justin Hibbits6256a0e2014-10-31 02:34:28 +0000109 { eByteOrderBig , 8, 4, 4, llvm::Triple::ppc64 , ArchSpec::eCore_ppc64_generic , "powerpc64" },
Greg Clayton83b162d2013-08-12 18:34:04 +0000110 { eByteOrderBig , 8, 4, 4, llvm::Triple::ppc64 , ArchSpec::eCore_ppc64_ppc970_64 , "ppc970-64" },
Greg Clayton64195a22011-02-23 00:35:02 +0000111
Greg Clayton357132e2011-03-26 19:14:58 +0000112 { eByteOrderLittle, 4, 4, 4, llvm::Triple::sparc , ArchSpec::eCore_sparc_generic , "sparc" },
113 { eByteOrderLittle, 8, 4, 4, llvm::Triple::sparcv9, ArchSpec::eCore_sparc9_generic , "sparcv9" },
Greg Clayton64195a22011-02-23 00:35:02 +0000114
Greg Claytonab65b342011-04-13 22:47:15 +0000115 { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i386 , "i386" },
116 { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i486 , "i486" },
117 { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i486sx , "i486sx" },
Virgile Bello97a70e42014-04-08 14:48:48 +0000118 { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i686 , "i686" },
Greg Clayton64195a22011-02-23 00:35:02 +0000119
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000120 { eByteOrderLittle, 8, 1, 15, llvm::Triple::x86_64 , ArchSpec::eCore_x86_64_x86_64 , "x86_64" },
Greg Claytona86dc432014-01-22 23:42:03 +0000121 { eByteOrderLittle, 8, 1, 15, llvm::Triple::x86_64 , ArchSpec::eCore_x86_64_x86_64h , "x86_64h" },
Deepak Panickal6d3df422014-02-19 11:16:46 +0000122 { eByteOrderLittle, 4, 4, 4, llvm::Triple::hexagon , ArchSpec::eCore_hexagon_generic, "hexagon" },
123 { eByteOrderLittle, 4, 4, 4, llvm::Triple::hexagon , ArchSpec::eCore_hexagon_hexagonv4, "hexagonv4" },
124 { eByteOrderLittle, 4, 4, 4, llvm::Triple::hexagon , ArchSpec::eCore_hexagon_hexagonv5, "hexagonv5" },
125
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000126 { eByteOrderLittle, 4, 4, 4 , llvm::Triple::UnknownArch , ArchSpec::eCore_uknownMach32 , "unknown-mach-32" },
Todd Fiala14bbef52014-07-01 23:33:32 +0000127 { eByteOrderLittle, 8, 4, 4 , llvm::Triple::UnknownArch , ArchSpec::eCore_uknownMach64 , "unknown-mach-64" },
128
Matthew Gardiner5f675792014-08-27 12:09:39 +0000129 { eByteOrderBig , 4, 1, 1 , llvm::Triple::kalimba , ArchSpec::eCore_kalimba3 , "kalimba3" },
130 { eByteOrderLittle, 4, 1, 1 , llvm::Triple::kalimba , ArchSpec::eCore_kalimba4 , "kalimba4" },
131 { eByteOrderLittle, 4, 1, 1 , llvm::Triple::kalimba , ArchSpec::eCore_kalimba5 , "kalimba5" }
Greg Clayton64195a22011-02-23 00:35:02 +0000132};
133
Greg Clayton56b79682014-07-23 18:12:06 +0000134// Ensure that we have an entry in the g_core_definitions for each core. If you comment out an entry above,
135// you will need to comment out the corresponding ArchSpec::Core enumeration.
Zachary Turner3b2065f2014-07-28 16:44:28 +0000136static_assert(sizeof(g_core_definitions) / sizeof(CoreDefinition) == ArchSpec::kNumCores, "make sure we have one core definition for each core");
Greg Clayton56b79682014-07-23 18:12:06 +0000137
138
Greg Clayton64195a22011-02-23 00:35:02 +0000139struct ArchDefinitionEntry
140{
141 ArchSpec::Core core;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000142 uint32_t cpu;
143 uint32_t sub;
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000144 uint32_t cpu_mask;
145 uint32_t sub_mask;
Greg Clayton64195a22011-02-23 00:35:02 +0000146};
147
148struct ArchDefinition
149{
150 ArchitectureType type;
151 size_t num_entries;
152 const ArchDefinitionEntry *entries;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000153 const char *name;
154};
155
Greg Clayton41f92322010-06-11 03:25:34 +0000156
Greg Claytonc7bece562013-01-25 18:06:21 +0000157size_t
Greg Claytonab65b342011-04-13 22:47:15 +0000158ArchSpec::AutoComplete (const char *name, StringList &matches)
159{
160 uint32_t i;
161 if (name && name[0])
162 {
Greg Clayton56b79682014-07-23 18:12:06 +0000163 for (i = 0; i < llvm::array_lengthof(g_core_definitions); ++i)
Greg Claytonab65b342011-04-13 22:47:15 +0000164 {
165 if (NameMatches(g_core_definitions[i].name, eNameMatchStartsWith, name))
166 matches.AppendString (g_core_definitions[i].name);
167 }
168 }
169 else
170 {
Greg Clayton56b79682014-07-23 18:12:06 +0000171 for (i = 0; i < llvm::array_lengthof(g_core_definitions); ++i)
Greg Claytonab65b342011-04-13 22:47:15 +0000172 matches.AppendString (g_core_definitions[i].name);
173 }
174 return matches.GetSize();
175}
176
177
178
Greg Clayton64195a22011-02-23 00:35:02 +0000179#define CPU_ANY (UINT32_MAX)
180
181//===----------------------------------------------------------------------===//
182// A table that gets searched linearly for matches. This table is used to
183// convert cpu type and subtypes to architecture names, and to convert
184// architecture names to cpu types and subtypes. The ordering is important and
185// allows the precedence to be set when the table is built.
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000186#define SUBTYPE_MASK 0x00FFFFFFu
Greg Clayton64195a22011-02-23 00:35:02 +0000187static const ArchDefinitionEntry g_macho_arch_entries[] =
Greg Clayton41f92322010-06-11 03:25:34 +0000188{
Charles Davis510938e2013-08-27 05:04:57 +0000189 { ArchSpec::eCore_arm_generic , llvm::MachO::CPU_TYPE_ARM , CPU_ANY, UINT32_MAX , UINT32_MAX },
190 { ArchSpec::eCore_arm_generic , llvm::MachO::CPU_TYPE_ARM , 0 , UINT32_MAX , SUBTYPE_MASK },
191 { ArchSpec::eCore_arm_armv4 , llvm::MachO::CPU_TYPE_ARM , 5 , UINT32_MAX , SUBTYPE_MASK },
192 { ArchSpec::eCore_arm_armv4t , llvm::MachO::CPU_TYPE_ARM , 5 , UINT32_MAX , SUBTYPE_MASK },
193 { ArchSpec::eCore_arm_armv6 , llvm::MachO::CPU_TYPE_ARM , 6 , UINT32_MAX , SUBTYPE_MASK },
Jason Molenda64a11732013-10-08 03:01:08 +0000194 { ArchSpec::eCore_arm_armv6m , llvm::MachO::CPU_TYPE_ARM , 14 , UINT32_MAX , SUBTYPE_MASK },
Charles Davis510938e2013-08-27 05:04:57 +0000195 { ArchSpec::eCore_arm_armv5 , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
196 { ArchSpec::eCore_arm_armv5e , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
197 { ArchSpec::eCore_arm_armv5t , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
198 { ArchSpec::eCore_arm_xscale , llvm::MachO::CPU_TYPE_ARM , 8 , UINT32_MAX , SUBTYPE_MASK },
199 { ArchSpec::eCore_arm_armv7 , llvm::MachO::CPU_TYPE_ARM , 9 , UINT32_MAX , SUBTYPE_MASK },
200 { ArchSpec::eCore_arm_armv7f , llvm::MachO::CPU_TYPE_ARM , 10 , UINT32_MAX , SUBTYPE_MASK },
201 { ArchSpec::eCore_arm_armv7s , llvm::MachO::CPU_TYPE_ARM , 11 , UINT32_MAX , SUBTYPE_MASK },
202 { ArchSpec::eCore_arm_armv7k , llvm::MachO::CPU_TYPE_ARM , 12 , UINT32_MAX , SUBTYPE_MASK },
203 { ArchSpec::eCore_arm_armv7m , llvm::MachO::CPU_TYPE_ARM , 15 , UINT32_MAX , SUBTYPE_MASK },
204 { ArchSpec::eCore_arm_armv7em , llvm::MachO::CPU_TYPE_ARM , 16 , UINT32_MAX , SUBTYPE_MASK },
Jason Molendaa3329782014-03-29 18:54:20 +0000205 { ArchSpec::eCore_arm_arm64 , llvm::MachO::CPU_TYPE_ARM64 , 1 , UINT32_MAX , SUBTYPE_MASK },
Jason Molenda22952582014-11-12 01:11:36 +0000206 { ArchSpec::eCore_arm_arm64 , llvm::MachO::CPU_TYPE_ARM64 , 0 , UINT32_MAX , SUBTYPE_MASK },
Jason Molendaa3329782014-03-29 18:54:20 +0000207 { ArchSpec::eCore_arm_arm64 , llvm::MachO::CPU_TYPE_ARM64 , 13 , UINT32_MAX , SUBTYPE_MASK },
Jason Molenda22952582014-11-12 01:11:36 +0000208 { ArchSpec::eCore_arm_arm64 , llvm::MachO::CPU_TYPE_ARM64 , CPU_ANY, UINT32_MAX , SUBTYPE_MASK },
Charles Davis510938e2013-08-27 05:04:57 +0000209 { ArchSpec::eCore_thumb , llvm::MachO::CPU_TYPE_ARM , 0 , UINT32_MAX , SUBTYPE_MASK },
210 { ArchSpec::eCore_thumbv4t , llvm::MachO::CPU_TYPE_ARM , 5 , UINT32_MAX , SUBTYPE_MASK },
211 { ArchSpec::eCore_thumbv5 , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
212 { ArchSpec::eCore_thumbv5e , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
213 { ArchSpec::eCore_thumbv6 , llvm::MachO::CPU_TYPE_ARM , 6 , UINT32_MAX , SUBTYPE_MASK },
Jason Molenda64a11732013-10-08 03:01:08 +0000214 { ArchSpec::eCore_thumbv6m , llvm::MachO::CPU_TYPE_ARM , 14 , UINT32_MAX , SUBTYPE_MASK },
Charles Davis510938e2013-08-27 05:04:57 +0000215 { ArchSpec::eCore_thumbv7 , llvm::MachO::CPU_TYPE_ARM , 9 , UINT32_MAX , SUBTYPE_MASK },
216 { ArchSpec::eCore_thumbv7f , llvm::MachO::CPU_TYPE_ARM , 10 , UINT32_MAX , SUBTYPE_MASK },
217 { ArchSpec::eCore_thumbv7s , llvm::MachO::CPU_TYPE_ARM , 11 , UINT32_MAX , SUBTYPE_MASK },
218 { ArchSpec::eCore_thumbv7k , llvm::MachO::CPU_TYPE_ARM , 12 , UINT32_MAX , SUBTYPE_MASK },
219 { ArchSpec::eCore_thumbv7m , llvm::MachO::CPU_TYPE_ARM , 15 , UINT32_MAX , SUBTYPE_MASK },
220 { ArchSpec::eCore_thumbv7em , llvm::MachO::CPU_TYPE_ARM , 16 , UINT32_MAX , SUBTYPE_MASK },
221 { ArchSpec::eCore_ppc_generic , llvm::MachO::CPU_TYPE_POWERPC , CPU_ANY, UINT32_MAX , UINT32_MAX },
222 { ArchSpec::eCore_ppc_generic , llvm::MachO::CPU_TYPE_POWERPC , 0 , UINT32_MAX , SUBTYPE_MASK },
223 { ArchSpec::eCore_ppc_ppc601 , llvm::MachO::CPU_TYPE_POWERPC , 1 , UINT32_MAX , SUBTYPE_MASK },
224 { ArchSpec::eCore_ppc_ppc602 , llvm::MachO::CPU_TYPE_POWERPC , 2 , UINT32_MAX , SUBTYPE_MASK },
225 { ArchSpec::eCore_ppc_ppc603 , llvm::MachO::CPU_TYPE_POWERPC , 3 , UINT32_MAX , SUBTYPE_MASK },
226 { ArchSpec::eCore_ppc_ppc603e , llvm::MachO::CPU_TYPE_POWERPC , 4 , UINT32_MAX , SUBTYPE_MASK },
227 { ArchSpec::eCore_ppc_ppc603ev , llvm::MachO::CPU_TYPE_POWERPC , 5 , UINT32_MAX , SUBTYPE_MASK },
228 { ArchSpec::eCore_ppc_ppc604 , llvm::MachO::CPU_TYPE_POWERPC , 6 , UINT32_MAX , SUBTYPE_MASK },
229 { ArchSpec::eCore_ppc_ppc604e , llvm::MachO::CPU_TYPE_POWERPC , 7 , UINT32_MAX , SUBTYPE_MASK },
230 { ArchSpec::eCore_ppc_ppc620 , llvm::MachO::CPU_TYPE_POWERPC , 8 , UINT32_MAX , SUBTYPE_MASK },
231 { ArchSpec::eCore_ppc_ppc750 , llvm::MachO::CPU_TYPE_POWERPC , 9 , UINT32_MAX , SUBTYPE_MASK },
232 { ArchSpec::eCore_ppc_ppc7400 , llvm::MachO::CPU_TYPE_POWERPC , 10 , UINT32_MAX , SUBTYPE_MASK },
233 { ArchSpec::eCore_ppc_ppc7450 , llvm::MachO::CPU_TYPE_POWERPC , 11 , UINT32_MAX , SUBTYPE_MASK },
234 { ArchSpec::eCore_ppc_ppc970 , llvm::MachO::CPU_TYPE_POWERPC , 100 , UINT32_MAX , SUBTYPE_MASK },
235 { ArchSpec::eCore_ppc64_generic , llvm::MachO::CPU_TYPE_POWERPC64 , 0 , UINT32_MAX , SUBTYPE_MASK },
236 { ArchSpec::eCore_ppc64_ppc970_64 , llvm::MachO::CPU_TYPE_POWERPC64 , 100 , UINT32_MAX , SUBTYPE_MASK },
237 { ArchSpec::eCore_x86_32_i386 , llvm::MachO::CPU_TYPE_I386 , 3 , UINT32_MAX , SUBTYPE_MASK },
238 { ArchSpec::eCore_x86_32_i486 , llvm::MachO::CPU_TYPE_I386 , 4 , UINT32_MAX , SUBTYPE_MASK },
239 { ArchSpec::eCore_x86_32_i486sx , llvm::MachO::CPU_TYPE_I386 , 0x84 , UINT32_MAX , SUBTYPE_MASK },
Greg Claytona86dc432014-01-22 23:42:03 +0000240 { ArchSpec::eCore_x86_32_i386 , llvm::MachO::CPU_TYPE_I386 , CPU_ANY, UINT32_MAX , UINT32_MAX },
Charles Davis510938e2013-08-27 05:04:57 +0000241 { ArchSpec::eCore_x86_64_x86_64 , llvm::MachO::CPU_TYPE_X86_64 , 3 , UINT32_MAX , SUBTYPE_MASK },
242 { ArchSpec::eCore_x86_64_x86_64 , llvm::MachO::CPU_TYPE_X86_64 , 4 , UINT32_MAX , SUBTYPE_MASK },
Greg Claytona86dc432014-01-22 23:42:03 +0000243 { ArchSpec::eCore_x86_64_x86_64h , llvm::MachO::CPU_TYPE_X86_64 , 8 , UINT32_MAX , SUBTYPE_MASK },
244 { ArchSpec::eCore_x86_64_x86_64 , llvm::MachO::CPU_TYPE_X86_64 , CPU_ANY, UINT32_MAX , UINT32_MAX },
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000245 // Catch any unknown mach architectures so we can always use the object and symbol mach-o files
Charles Davis510938e2013-08-27 05:04:57 +0000246 { ArchSpec::eCore_uknownMach32 , 0 , 0 , 0xFF000000u, 0x00000000u },
247 { ArchSpec::eCore_uknownMach64 , llvm::MachO::CPU_ARCH_ABI64 , 0 , 0xFF000000u, 0x00000000u }
Greg Clayton64195a22011-02-23 00:35:02 +0000248};
249static const ArchDefinition g_macho_arch_def = {
250 eArchTypeMachO,
Saleem Abdulrasool28606952014-06-27 05:17:41 +0000251 llvm::array_lengthof(g_macho_arch_entries),
Greg Clayton64195a22011-02-23 00:35:02 +0000252 g_macho_arch_entries,
Greg Clayton64195a22011-02-23 00:35:02 +0000253 "mach-o"
Greg Clayton41f92322010-06-11 03:25:34 +0000254};
255
Greg Clayton64195a22011-02-23 00:35:02 +0000256//===----------------------------------------------------------------------===//
257// A table that gets searched linearly for matches. This table is used to
258// convert cpu type and subtypes to architecture names, and to convert
259// architecture names to cpu types and subtypes. The ordering is important and
260// allows the precedence to be set when the table is built.
261static const ArchDefinitionEntry g_elf_arch_entries[] =
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000262{
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000263 { ArchSpec::eCore_sparc_generic , llvm::ELF::EM_SPARC , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // Sparc
264 { ArchSpec::eCore_x86_32_i386 , llvm::ELF::EM_386 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // Intel 80386
265 { ArchSpec::eCore_x86_32_i486 , llvm::ELF::EM_486 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // Intel 486 (deprecated)
266 { ArchSpec::eCore_ppc_generic , llvm::ELF::EM_PPC , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // PowerPC
267 { ArchSpec::eCore_ppc64_generic , llvm::ELF::EM_PPC64 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // PowerPC64
268 { ArchSpec::eCore_arm_generic , llvm::ELF::EM_ARM , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // ARM
Todd Fiala02e71812014-08-28 14:32:43 +0000269 { ArchSpec::eCore_arm_aarch64 , llvm::ELF::EM_AARCH64, LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // ARM64
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000270 { ArchSpec::eCore_sparc9_generic , llvm::ELF::EM_SPARCV9, LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // SPARC V9
Ed Masteb73f8442013-10-10 00:59:47 +0000271 { ArchSpec::eCore_x86_64_x86_64 , llvm::ELF::EM_X86_64 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // AMD64
Mohit K. Bhakkad3df471c2015-03-17 11:43:56 +0000272 { ArchSpec::eCore_mips64 , llvm::ELF::EM_MIPS , llvm::Triple::mips64, 0xFFFFFFFFu, 0xFFFFFFFFu }, // mips64
273 { ArchSpec::eCore_mips64el , llvm::ELF::EM_MIPS , llvm::Triple::mips64el, 0xFFFFFFFFu, 0xFFFFFFFFu }, // mips64el
Todd Fiala14bbef52014-07-01 23:33:32 +0000274 { ArchSpec::eCore_hexagon_generic , llvm::ELF::EM_HEXAGON, LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // HEXAGON
Matthew Gardinerf03e6d842014-09-29 08:02:24 +0000275 { ArchSpec::eCore_kalimba3 , llvm::ELF::EM_CSR_KALIMBA, llvm::Triple::KalimbaSubArch_v3, 0xFFFFFFFFu, 0xFFFFFFFFu }, // KALIMBA
276 { ArchSpec::eCore_kalimba4 , llvm::ELF::EM_CSR_KALIMBA, llvm::Triple::KalimbaSubArch_v4, 0xFFFFFFFFu, 0xFFFFFFFFu }, // KALIMBA
277 { ArchSpec::eCore_kalimba5 , llvm::ELF::EM_CSR_KALIMBA, llvm::Triple::KalimbaSubArch_v5, 0xFFFFFFFFu, 0xFFFFFFFFu } // KALIMBA
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000278};
279
Greg Clayton64195a22011-02-23 00:35:02 +0000280static const ArchDefinition g_elf_arch_def = {
281 eArchTypeELF,
Saleem Abdulrasool28606952014-06-27 05:17:41 +0000282 llvm::array_lengthof(g_elf_arch_entries),
Greg Clayton64195a22011-02-23 00:35:02 +0000283 g_elf_arch_entries,
Greg Clayton64195a22011-02-23 00:35:02 +0000284 "elf",
Greg Clayton41f92322010-06-11 03:25:34 +0000285};
286
Charles Davis237ad972013-08-27 05:04:33 +0000287static const ArchDefinitionEntry g_coff_arch_entries[] =
288{
Zachary Turnerad587ae42014-07-28 16:44:49 +0000289 { ArchSpec::eCore_x86_32_i386 , llvm::COFF::IMAGE_FILE_MACHINE_I386 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // Intel 80x86
Charles Davis237ad972013-08-27 05:04:33 +0000290 { ArchSpec::eCore_ppc_generic , llvm::COFF::IMAGE_FILE_MACHINE_POWERPC , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // PowerPC
291 { ArchSpec::eCore_ppc_generic , llvm::COFF::IMAGE_FILE_MACHINE_POWERPCFP, LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // PowerPC (with FPU)
292 { ArchSpec::eCore_arm_generic , llvm::COFF::IMAGE_FILE_MACHINE_ARM , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // ARM
Saleem Abdulrasool1108cb32014-03-11 03:09:08 +0000293 { ArchSpec::eCore_arm_armv7 , llvm::COFF::IMAGE_FILE_MACHINE_ARMNT , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // ARMv7
Charles Davis237ad972013-08-27 05:04:33 +0000294 { ArchSpec::eCore_thumb , llvm::COFF::IMAGE_FILE_MACHINE_THUMB , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // ARMv7
295 { ArchSpec::eCore_x86_64_x86_64, llvm::COFF::IMAGE_FILE_MACHINE_AMD64 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu } // AMD64
296};
297
298static const ArchDefinition g_coff_arch_def = {
299 eArchTypeCOFF,
Saleem Abdulrasool28606952014-06-27 05:17:41 +0000300 llvm::array_lengthof(g_coff_arch_entries),
Charles Davis237ad972013-08-27 05:04:33 +0000301 g_coff_arch_entries,
302 "pe-coff",
303};
304
Greg Clayton64195a22011-02-23 00:35:02 +0000305//===----------------------------------------------------------------------===//
306// Table of all ArchDefinitions
307static const ArchDefinition *g_arch_definitions[] = {
308 &g_macho_arch_def,
Charles Davis237ad972013-08-27 05:04:33 +0000309 &g_elf_arch_def,
310 &g_coff_arch_def
Greg Clayton64195a22011-02-23 00:35:02 +0000311};
Greg Clayton41f92322010-06-11 03:25:34 +0000312
Saleem Abdulrasool28606952014-06-27 05:17:41 +0000313static const size_t k_num_arch_definitions = llvm::array_lengthof(g_arch_definitions);
Greg Clayton64195a22011-02-23 00:35:02 +0000314
315//===----------------------------------------------------------------------===//
316// Static helper functions.
317
318
319// Get the architecture definition for a given object type.
320static const ArchDefinition *
321FindArchDefinition (ArchitectureType arch_type)
322{
323 for (unsigned int i = 0; i < k_num_arch_definitions; ++i)
324 {
325 const ArchDefinition *def = g_arch_definitions[i];
326 if (def->type == arch_type)
327 return def;
328 }
329 return NULL;
330}
331
332// Get an architecture definition by name.
333static const CoreDefinition *
334FindCoreDefinition (llvm::StringRef name)
335{
Greg Clayton56b79682014-07-23 18:12:06 +0000336 for (unsigned int i = 0; i < llvm::array_lengthof(g_core_definitions); ++i)
Greg Clayton64195a22011-02-23 00:35:02 +0000337 {
338 if (name.equals_lower(g_core_definitions[i].name))
339 return &g_core_definitions[i];
340 }
341 return NULL;
342}
343
344static inline const CoreDefinition *
345FindCoreDefinition (ArchSpec::Core core)
346{
Greg Clayton56b79682014-07-23 18:12:06 +0000347 if (core >= 0 && core < llvm::array_lengthof(g_core_definitions))
Greg Clayton64195a22011-02-23 00:35:02 +0000348 return &g_core_definitions[core];
349 return NULL;
350}
351
352// Get a definition entry by cpu type and subtype.
353static const ArchDefinitionEntry *
354FindArchDefinitionEntry (const ArchDefinition *def, uint32_t cpu, uint32_t sub)
355{
356 if (def == NULL)
357 return NULL;
358
Greg Clayton64195a22011-02-23 00:35:02 +0000359 const ArchDefinitionEntry *entries = def->entries;
360 for (size_t i = 0; i < def->num_entries; ++i)
361 {
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000362 if (entries[i].cpu == (cpu & entries[i].cpu_mask))
363 if (entries[i].sub == (sub & entries[i].sub_mask))
364 return &entries[i];
Greg Clayton64195a22011-02-23 00:35:02 +0000365 }
366 return NULL;
367}
368
369static const ArchDefinitionEntry *
370FindArchDefinitionEntry (const ArchDefinition *def, ArchSpec::Core core)
371{
372 if (def == NULL)
373 return NULL;
374
375 const ArchDefinitionEntry *entries = def->entries;
376 for (size_t i = 0; i < def->num_entries; ++i)
377 {
378 if (entries[i].core == core)
379 return &entries[i];
380 }
381 return NULL;
382}
383
384//===----------------------------------------------------------------------===//
385// Constructors and destructors.
386
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000387ArchSpec::ArchSpec() :
Greg Clayton514487e2011-02-15 21:59:32 +0000388 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000389 m_core (kCore_invalid),
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000390 m_byte_order (eByteOrderInvalid),
391 m_distribution_id ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000392{
393}
394
Greg Claytoneb0103f2011-04-07 22:46:35 +0000395ArchSpec::ArchSpec (const char *triple_cstr, Platform *platform) :
Greg Clayton514487e2011-02-15 21:59:32 +0000396 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000397 m_core (kCore_invalid),
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000398 m_byte_order (eByteOrderInvalid),
399 m_distribution_id ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000400{
Greg Clayton64195a22011-02-23 00:35:02 +0000401 if (triple_cstr)
Greg Claytoneb0103f2011-04-07 22:46:35 +0000402 SetTriple(triple_cstr, platform);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000403}
404
Greg Clayton70512312012-05-08 01:45:38 +0000405
406ArchSpec::ArchSpec (const char *triple_cstr) :
407 m_triple (),
408 m_core (kCore_invalid),
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000409 m_byte_order (eByteOrderInvalid),
410 m_distribution_id ()
Greg Clayton70512312012-05-08 01:45:38 +0000411{
412 if (triple_cstr)
413 SetTriple(triple_cstr);
414}
415
Greg Clayton64195a22011-02-23 00:35:02 +0000416ArchSpec::ArchSpec(const llvm::Triple &triple) :
Greg Clayton514487e2011-02-15 21:59:32 +0000417 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000418 m_core (kCore_invalid),
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000419 m_byte_order (eByteOrderInvalid),
420 m_distribution_id ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000421{
Greg Clayton64195a22011-02-23 00:35:02 +0000422 SetTriple(triple);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000423}
424
Greg Claytone0d378b2011-03-24 21:19:54 +0000425ArchSpec::ArchSpec (ArchitectureType arch_type, uint32_t cpu, uint32_t subtype) :
Greg Clayton64195a22011-02-23 00:35:02 +0000426 m_triple (),
427 m_core (kCore_invalid),
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000428 m_byte_order (eByteOrderInvalid),
429 m_distribution_id ()
Greg Clayton64195a22011-02-23 00:35:02 +0000430{
431 SetArchitecture (arch_type, cpu, subtype);
432}
433
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000434ArchSpec::~ArchSpec()
435{
436}
437
Greg Clayton64195a22011-02-23 00:35:02 +0000438//===----------------------------------------------------------------------===//
439// Assignment and initialization.
440
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000441const ArchSpec&
442ArchSpec::operator= (const ArchSpec& rhs)
443{
444 if (this != &rhs)
445 {
Greg Clayton514487e2011-02-15 21:59:32 +0000446 m_triple = rhs.m_triple;
Greg Clayton64195a22011-02-23 00:35:02 +0000447 m_core = rhs.m_core;
Greg Clayton514487e2011-02-15 21:59:32 +0000448 m_byte_order = rhs.m_byte_order;
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000449 m_distribution_id = rhs.m_distribution_id;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000450 }
451 return *this;
452}
453
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000454void
455ArchSpec::Clear()
456{
Greg Clayton514487e2011-02-15 21:59:32 +0000457 m_triple = llvm::Triple();
Greg Clayton64195a22011-02-23 00:35:02 +0000458 m_core = kCore_invalid;
459 m_byte_order = eByteOrderInvalid;
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000460 m_distribution_id.Clear ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000461}
462
Greg Clayton64195a22011-02-23 00:35:02 +0000463//===----------------------------------------------------------------------===//
464// Predicates.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000465
Greg Clayton41f92322010-06-11 03:25:34 +0000466
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000467const char *
Greg Clayton64195a22011-02-23 00:35:02 +0000468ArchSpec::GetArchitectureName () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000469{
Greg Clayton64195a22011-02-23 00:35:02 +0000470 const CoreDefinition *core_def = FindCoreDefinition (m_core);
471 if (core_def)
472 return core_def->name;
473 return "unknown";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000474}
475
Greg Clayton64195a22011-02-23 00:35:02 +0000476uint32_t
477ArchSpec::GetMachOCPUType () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000478{
Greg Clayton64195a22011-02-23 00:35:02 +0000479 const CoreDefinition *core_def = FindCoreDefinition (m_core);
480 if (core_def)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000481 {
Greg Clayton64195a22011-02-23 00:35:02 +0000482 const ArchDefinitionEntry *arch_def = FindArchDefinitionEntry (&g_macho_arch_def, core_def->core);
483 if (arch_def)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000484 {
Greg Clayton64195a22011-02-23 00:35:02 +0000485 return arch_def->cpu;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000486 }
487 }
Greg Clayton64195a22011-02-23 00:35:02 +0000488 return LLDB_INVALID_CPUTYPE;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000489}
490
Greg Clayton64195a22011-02-23 00:35:02 +0000491uint32_t
492ArchSpec::GetMachOCPUSubType () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000493{
Greg Clayton64195a22011-02-23 00:35:02 +0000494 const CoreDefinition *core_def = FindCoreDefinition (m_core);
495 if (core_def)
496 {
497 const ArchDefinitionEntry *arch_def = FindArchDefinitionEntry (&g_macho_arch_def, core_def->core);
498 if (arch_def)
499 {
Greg Clayton1cb64962011-03-24 04:28:38 +0000500 return arch_def->sub;
Greg Clayton64195a22011-02-23 00:35:02 +0000501 }
502 }
503 return LLDB_INVALID_CPUTYPE;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000504}
505
Matthew Gardinere77b2942014-09-01 09:06:03 +0000506uint32_t
507ArchSpec::GetDataByteSize () const
508{
509 switch (m_core)
510 {
511 case eCore_kalimba3:
Matthew Gardinerf03e6d842014-09-29 08:02:24 +0000512 return 4;
Matthew Gardinere77b2942014-09-01 09:06:03 +0000513 case eCore_kalimba4:
514 return 1;
515 case eCore_kalimba5:
Matthew Gardinerf03e6d842014-09-29 08:02:24 +0000516 return 4;
Matthew Gardinere77b2942014-09-01 09:06:03 +0000517 default:
518 return 1;
519 }
520 return 1;
521}
522
523uint32_t
524ArchSpec::GetCodeByteSize () const
525{
526 switch (m_core)
527 {
528 case eCore_kalimba3:
529 return 4;
530 case eCore_kalimba4:
531 return 1;
532 case eCore_kalimba5:
533 return 1;
534 default:
535 return 1;
536 }
537 return 1;
538}
539
Greg Clayton64195a22011-02-23 00:35:02 +0000540llvm::Triple::ArchType
541ArchSpec::GetMachine () const
542{
543 const CoreDefinition *core_def = FindCoreDefinition (m_core);
544 if (core_def)
545 return core_def->machine;
546
547 return llvm::Triple::UnknownArch;
548}
549
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000550const ConstString&
551ArchSpec::GetDistributionId () const
552{
553 return m_distribution_id;
554}
555
556void
557ArchSpec::SetDistributionId (const char* distribution_id)
558{
559 m_distribution_id.SetCString (distribution_id);
560}
561
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000562uint32_t
563ArchSpec::GetAddressByteSize() const
564{
Greg Clayton64195a22011-02-23 00:35:02 +0000565 const CoreDefinition *core_def = FindCoreDefinition (m_core);
566 if (core_def)
567 return core_def->addr_byte_size;
Greg Clayton41f92322010-06-11 03:25:34 +0000568 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000569}
570
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000571ByteOrder
572ArchSpec::GetDefaultEndian () const
573{
Greg Clayton64195a22011-02-23 00:35:02 +0000574 const CoreDefinition *core_def = FindCoreDefinition (m_core);
575 if (core_def)
576 return core_def->default_byte_order;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000577 return eByteOrderInvalid;
578}
579
Greg Clayton64195a22011-02-23 00:35:02 +0000580lldb::ByteOrder
581ArchSpec::GetByteOrder () const
582{
583 if (m_byte_order == eByteOrderInvalid)
584 return GetDefaultEndian();
585 return m_byte_order;
586}
587
588//===----------------------------------------------------------------------===//
589// Mutators.
590
591bool
592ArchSpec::SetTriple (const llvm::Triple &triple)
593{
594 m_triple = triple;
595
596 llvm::StringRef arch_name (m_triple.getArchName());
597 const CoreDefinition *core_def = FindCoreDefinition (arch_name);
598 if (core_def)
599 {
600 m_core = core_def->core;
Greg Claytoneb0103f2011-04-07 22:46:35 +0000601 // Set the byte order to the default byte order for an architecture.
602 // This can be modified if needed for cases when cores handle both
603 // big and little endian
604 m_byte_order = core_def->default_byte_order;
Greg Clayton64195a22011-02-23 00:35:02 +0000605 }
606 else
607 {
608 Clear();
609 }
610
611
612 return IsValid();
613}
614
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000615static bool
616ParseMachCPUDashSubtypeTriple (const char *triple_cstr, ArchSpec &arch)
617{
618 // Accept "12-10" or "12.10" as cpu type/subtype
619 if (isdigit(triple_cstr[0]))
620 {
621 char *end = NULL;
622 errno = 0;
Greg Claytonc7bece562013-01-25 18:06:21 +0000623 uint32_t cpu = (uint32_t)::strtoul (triple_cstr, &end, 0);
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000624 if (errno == 0 && cpu != 0 && end && ((*end == '-') || (*end == '.')))
625 {
626 errno = 0;
Greg Claytonc7bece562013-01-25 18:06:21 +0000627 uint32_t sub = (uint32_t)::strtoul (end + 1, &end, 0);
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000628 if (errno == 0 && end && ((*end == '-') || (*end == '.') || (*end == '\0')))
629 {
630 if (arch.SetArchitecture (eArchTypeMachO, cpu, sub))
631 {
632 if (*end == '-')
633 {
634 llvm::StringRef vendor_os (end + 1);
635 size_t dash_pos = vendor_os.find('-');
636 if (dash_pos != llvm::StringRef::npos)
637 {
638 llvm::StringRef vendor_str(vendor_os.substr(0, dash_pos));
639 arch.GetTriple().setVendorName(vendor_str);
640 const size_t vendor_start_pos = dash_pos+1;
Greg Claytonc7bece562013-01-25 18:06:21 +0000641 dash_pos = vendor_os.find('-', vendor_start_pos);
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000642 if (dash_pos == llvm::StringRef::npos)
643 {
644 if (vendor_start_pos < vendor_os.size())
645 arch.GetTriple().setOSName(vendor_os.substr(vendor_start_pos));
646 }
647 else
648 {
649 arch.GetTriple().setOSName(vendor_os.substr(vendor_start_pos, dash_pos - vendor_start_pos));
650 }
651 }
652 }
653 return true;
654 }
655 }
656 }
657 }
658 return false;
659}
Greg Clayton64195a22011-02-23 00:35:02 +0000660bool
Greg Clayton70512312012-05-08 01:45:38 +0000661ArchSpec::SetTriple (const char *triple_cstr)
Greg Clayton64195a22011-02-23 00:35:02 +0000662{
Greg Clayton23aca092011-08-12 23:32:52 +0000663 if (triple_cstr && triple_cstr[0])
Greg Clayton64195a22011-02-23 00:35:02 +0000664 {
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000665 if (ParseMachCPUDashSubtypeTriple (triple_cstr, *this))
666 return true;
667
Greg Clayton64195a22011-02-23 00:35:02 +0000668 llvm::StringRef triple_stref (triple_cstr);
669 if (triple_stref.startswith (LLDB_ARCH_DEFAULT))
670 {
671 // Special case for the current host default architectures...
672 if (triple_stref.equals (LLDB_ARCH_DEFAULT_32BIT))
Zachary Turner13b18262014-08-20 16:42:51 +0000673 *this = HostInfo::GetArchitecture(HostInfo::eArchKind32);
Greg Clayton64195a22011-02-23 00:35:02 +0000674 else if (triple_stref.equals (LLDB_ARCH_DEFAULT_64BIT))
Zachary Turner13b18262014-08-20 16:42:51 +0000675 *this = HostInfo::GetArchitecture(HostInfo::eArchKind64);
Greg Clayton64195a22011-02-23 00:35:02 +0000676 else if (triple_stref.equals (LLDB_ARCH_DEFAULT))
Zachary Turner13b18262014-08-20 16:42:51 +0000677 *this = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
Greg Clayton64195a22011-02-23 00:35:02 +0000678 }
679 else
680 {
681 std::string normalized_triple_sstr (llvm::Triple::normalize(triple_stref));
682 triple_stref = normalized_triple_sstr;
Greg Clayton70512312012-05-08 01:45:38 +0000683 SetTriple (llvm::Triple (triple_stref));
684 }
685 }
686 else
687 Clear();
688 return IsValid();
689}
690
691bool
692ArchSpec::SetTriple (const char *triple_cstr, Platform *platform)
693{
694 if (triple_cstr && triple_cstr[0])
695 {
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000696 if (ParseMachCPUDashSubtypeTriple (triple_cstr, *this))
697 return true;
698
Greg Clayton70512312012-05-08 01:45:38 +0000699 llvm::StringRef triple_stref (triple_cstr);
700 if (triple_stref.startswith (LLDB_ARCH_DEFAULT))
701 {
702 // Special case for the current host default architectures...
703 if (triple_stref.equals (LLDB_ARCH_DEFAULT_32BIT))
Zachary Turner13b18262014-08-20 16:42:51 +0000704 *this = HostInfo::GetArchitecture(HostInfo::eArchKind32);
Greg Clayton70512312012-05-08 01:45:38 +0000705 else if (triple_stref.equals (LLDB_ARCH_DEFAULT_64BIT))
Zachary Turner13b18262014-08-20 16:42:51 +0000706 *this = HostInfo::GetArchitecture(HostInfo::eArchKind64);
Greg Clayton70512312012-05-08 01:45:38 +0000707 else if (triple_stref.equals (LLDB_ARCH_DEFAULT))
Zachary Turner13b18262014-08-20 16:42:51 +0000708 *this = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
Greg Clayton70512312012-05-08 01:45:38 +0000709 }
710 else
711 {
712 ArchSpec raw_arch (triple_cstr);
713
714 std::string normalized_triple_sstr (llvm::Triple::normalize(triple_stref));
715 triple_stref = normalized_triple_sstr;
Greg Claytoneb0103f2011-04-07 22:46:35 +0000716 llvm::Triple normalized_triple (triple_stref);
717
718 const bool os_specified = normalized_triple.getOSName().size() > 0;
719 const bool vendor_specified = normalized_triple.getVendorName().size() > 0;
720 const bool env_specified = normalized_triple.getEnvironmentName().size() > 0;
721
722 // If we got an arch only, then default the vendor, os, environment
723 // to match the platform if one is supplied
724 if (!(os_specified || vendor_specified || env_specified))
725 {
726 if (platform)
727 {
728 // If we were given a platform, use the platform's system
729 // architecture. If this is not available (might not be
730 // connected) use the first supported architecture.
Greg Clayton70512312012-05-08 01:45:38 +0000731 ArchSpec compatible_arch;
Greg Clayton1e0c8842013-01-11 20:49:54 +0000732 if (platform->IsCompatibleArchitecture (raw_arch, false, &compatible_arch))
Greg Claytoneb0103f2011-04-07 22:46:35 +0000733 {
Greg Clayton70512312012-05-08 01:45:38 +0000734 if (compatible_arch.IsValid())
735 {
736 const llvm::Triple &compatible_triple = compatible_arch.GetTriple();
737 if (!vendor_specified)
738 normalized_triple.setVendor(compatible_triple.getVendor());
739 if (!os_specified)
740 normalized_triple.setOS(compatible_triple.getOS());
741 if (!env_specified && compatible_triple.getEnvironmentName().size())
742 normalized_triple.setEnvironment(compatible_triple.getEnvironment());
743 }
Greg Claytoneb0103f2011-04-07 22:46:35 +0000744 }
Greg Clayton70512312012-05-08 01:45:38 +0000745 else
Greg Claytoneb0103f2011-04-07 22:46:35 +0000746 {
Greg Clayton70512312012-05-08 01:45:38 +0000747 *this = raw_arch;
748 return IsValid();
Greg Claytoneb0103f2011-04-07 22:46:35 +0000749 }
750 }
751 else
752 {
753 // No platform specified, fall back to the host system for
754 // the default vendor, os, and environment.
Sean Callananbfb237bc2011-11-04 22:46:46 +0000755 llvm::Triple host_triple(llvm::sys::getDefaultTargetTriple());
Greg Clayton70512312012-05-08 01:45:38 +0000756 if (!vendor_specified)
757 normalized_triple.setVendor(host_triple.getVendor());
758 if (!vendor_specified)
759 normalized_triple.setOS(host_triple.getOS());
760 if (!env_specified && host_triple.getEnvironmentName().size())
761 normalized_triple.setEnvironment(host_triple.getEnvironment());
Greg Claytoneb0103f2011-04-07 22:46:35 +0000762 }
763 }
764 SetTriple (normalized_triple);
Greg Clayton64195a22011-02-23 00:35:02 +0000765 }
766 }
767 else
768 Clear();
769 return IsValid();
770}
771
Zachary Turner5e6f4522015-01-22 18:59:05 +0000772void
773ArchSpec::MergeFrom(const ArchSpec &other)
774{
775 if (GetTriple().getVendor() == llvm::Triple::UnknownVendor && !TripleVendorWasSpecified())
776 GetTriple().setVendor(other.GetTriple().getVendor());
777 if (GetTriple().getOS() == llvm::Triple::UnknownOS && !TripleOSWasSpecified())
778 GetTriple().setOS(other.GetTriple().getOS());
779 if (GetTriple().getArch() == llvm::Triple::UnknownArch)
780 GetTriple().setArch(other.GetTriple().getArch());
781 if (GetTriple().getEnvironment() == llvm::Triple::UnknownEnvironment)
782 GetTriple().setEnvironment(other.GetTriple().getEnvironment());
783}
784
Greg Clayton64195a22011-02-23 00:35:02 +0000785bool
Greg Claytone0d378b2011-03-24 21:19:54 +0000786ArchSpec::SetArchitecture (ArchitectureType arch_type, uint32_t cpu, uint32_t sub)
Greg Clayton64195a22011-02-23 00:35:02 +0000787{
788 m_core = kCore_invalid;
789 bool update_triple = true;
790 const ArchDefinition *arch_def = FindArchDefinition(arch_type);
791 if (arch_def)
792 {
793 const ArchDefinitionEntry *arch_def_entry = FindArchDefinitionEntry (arch_def, cpu, sub);
794 if (arch_def_entry)
795 {
796 const CoreDefinition *core_def = FindCoreDefinition (arch_def_entry->core);
797 if (core_def)
798 {
799 m_core = core_def->core;
800 update_triple = false;
Greg Clayton593577a2011-09-21 03:57:31 +0000801 // Always use the architecture name because it might be more descriptive
802 // than the architecture enum ("armv7" -> llvm::Triple::arm).
803 m_triple.setArchName(llvm::StringRef(core_def->name));
Greg Clayton64195a22011-02-23 00:35:02 +0000804 if (arch_type == eArchTypeMachO)
805 {
806 m_triple.setVendor (llvm::Triple::Apple);
Greg Clayton70512312012-05-08 01:45:38 +0000807
808 switch (core_def->machine)
809 {
Todd Fialad8eaa172014-07-23 14:37:35 +0000810 case llvm::Triple::aarch64:
Greg Clayton70512312012-05-08 01:45:38 +0000811 case llvm::Triple::arm:
812 case llvm::Triple::thumb:
813 m_triple.setOS (llvm::Triple::IOS);
814 break;
815
816 case llvm::Triple::x86:
817 case llvm::Triple::x86_64:
Greg Claytona3a6c122014-07-29 18:04:57 +0000818 // Don't set the OS for x86_64 or for x86 as we want to leave it as an "unspecified unknown"
819 // which means if we ask for the OS from the llvm::Triple we get back llvm::Triple::UnknownOS, but
820 // if we ask for the string value for the OS it will come back empty (unspecified).
821 // We do this because we now have iOS and MacOSX as the OS values for x86 and x86_64 for
822 // normal desktop and simulator binaries. And if we compare a "x86_64-apple-ios" to a "x86_64-apple-"
823 // triple, it will say it is compatible (because the OS is unspecified in the second one and will match
824 // anything in the first
825 break;
826
Greg Clayton70512312012-05-08 01:45:38 +0000827 default:
828 m_triple.setOS (llvm::Triple::MacOSX);
829 break;
830 }
Greg Clayton64195a22011-02-23 00:35:02 +0000831 }
832 else
833 {
834 m_triple.setVendor (llvm::Triple::UnknownVendor);
835 m_triple.setOS (llvm::Triple::UnknownOS);
836 }
Greg Clayton593577a2011-09-21 03:57:31 +0000837 // Fall back onto setting the machine type if the arch by name failed...
838 if (m_triple.getArch () == llvm::Triple::UnknownArch)
839 m_triple.setArch (core_def->machine);
Greg Clayton64195a22011-02-23 00:35:02 +0000840 }
841 }
842 }
843 CoreUpdated(update_triple);
844 return IsValid();
845}
846
Greg Clayton357132e2011-03-26 19:14:58 +0000847uint32_t
848ArchSpec::GetMinimumOpcodeByteSize() const
Greg Clayton64195a22011-02-23 00:35:02 +0000849{
Greg Clayton357132e2011-03-26 19:14:58 +0000850 const CoreDefinition *core_def = FindCoreDefinition (m_core);
851 if (core_def)
852 return core_def->min_opcode_byte_size;
853 return 0;
854}
855
856uint32_t
857ArchSpec::GetMaximumOpcodeByteSize() const
858{
859 const CoreDefinition *core_def = FindCoreDefinition (m_core);
860 if (core_def)
861 return core_def->max_opcode_byte_size;
862 return 0;
Greg Clayton64195a22011-02-23 00:35:02 +0000863}
864
Jason Molendaba813dc2012-11-04 03:20:05 +0000865bool
866ArchSpec::IsExactMatch (const ArchSpec& rhs) const
867{
Sean Callananbf4b7be2012-12-13 22:07:14 +0000868 return IsEqualTo (rhs, true);
Jason Molendaba813dc2012-11-04 03:20:05 +0000869}
870
871bool
872ArchSpec::IsCompatibleMatch (const ArchSpec& rhs) const
873{
Sean Callananbf4b7be2012-12-13 22:07:14 +0000874 return IsEqualTo (rhs, false);
Jason Molendaba813dc2012-11-04 03:20:05 +0000875}
876
877bool
Sean Callananbf4b7be2012-12-13 22:07:14 +0000878ArchSpec::IsEqualTo (const ArchSpec& rhs, bool exact_match) const
Jason Molendaba813dc2012-11-04 03:20:05 +0000879{
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000880 // explicitly ignoring m_distribution_id in this method.
881
Jason Molendaba813dc2012-11-04 03:20:05 +0000882 if (GetByteOrder() != rhs.GetByteOrder())
883 return false;
884
885 const ArchSpec::Core lhs_core = GetCore ();
886 const ArchSpec::Core rhs_core = rhs.GetCore ();
887
888 const bool core_match = cores_match (lhs_core, rhs_core, true, exact_match);
889
890 if (core_match)
891 {
892 const llvm::Triple &lhs_triple = GetTriple();
893 const llvm::Triple &rhs_triple = rhs.GetTriple();
894
895 const llvm::Triple::VendorType lhs_triple_vendor = lhs_triple.getVendor();
896 const llvm::Triple::VendorType rhs_triple_vendor = rhs_triple.getVendor();
897 if (lhs_triple_vendor != rhs_triple_vendor)
898 {
Sean Callananbf4b7be2012-12-13 22:07:14 +0000899 if (exact_match)
900 {
901 const bool rhs_vendor_specified = rhs.TripleVendorWasSpecified();
902 const bool lhs_vendor_specified = TripleVendorWasSpecified();
903 // Both architectures had the vendor specified, so if they aren't
904 // equal then we return false
905 if (rhs_vendor_specified && lhs_vendor_specified)
906 return false;
907 }
Jason Molendaba813dc2012-11-04 03:20:05 +0000908
909 // Only fail if both vendor types are not unknown
910 if (lhs_triple_vendor != llvm::Triple::UnknownVendor &&
911 rhs_triple_vendor != llvm::Triple::UnknownVendor)
912 return false;
913 }
914
915 const llvm::Triple::OSType lhs_triple_os = lhs_triple.getOS();
916 const llvm::Triple::OSType rhs_triple_os = rhs_triple.getOS();
917 if (lhs_triple_os != rhs_triple_os)
918 {
Sean Callananbf4b7be2012-12-13 22:07:14 +0000919 if (exact_match)
920 {
921 const bool rhs_os_specified = rhs.TripleOSWasSpecified();
922 const bool lhs_os_specified = TripleOSWasSpecified();
923 // Both architectures had the OS specified, so if they aren't
924 // equal then we return false
925 if (rhs_os_specified && lhs_os_specified)
926 return false;
927 }
Greg Clayton7ab7f892014-05-29 21:33:45 +0000928
Greg Clayton3f19ada2014-07-10 23:33:37 +0000929 // Only fail if both os types are not unknown
930 if (lhs_triple_os != llvm::Triple::UnknownOS &&
931 rhs_triple_os != llvm::Triple::UnknownOS)
932 return false;
Jason Molendaba813dc2012-11-04 03:20:05 +0000933 }
934
935 const llvm::Triple::EnvironmentType lhs_triple_env = lhs_triple.getEnvironment();
936 const llvm::Triple::EnvironmentType rhs_triple_env = rhs_triple.getEnvironment();
937
938 if (lhs_triple_env != rhs_triple_env)
939 {
940 // Only fail if both environment types are not unknown
941 if (lhs_triple_env != llvm::Triple::UnknownEnvironment &&
942 rhs_triple_env != llvm::Triple::UnknownEnvironment)
943 return false;
944 }
945 return true;
946 }
947 return false;
948}
949
Greg Clayton64195a22011-02-23 00:35:02 +0000950//===----------------------------------------------------------------------===//
951// Helper methods.
952
953void
954ArchSpec::CoreUpdated (bool update_triple)
955{
956 const CoreDefinition *core_def = FindCoreDefinition (m_core);
957 if (core_def)
958 {
959 if (update_triple)
960 m_triple = llvm::Triple(core_def->name, "unknown", "unknown");
961 m_byte_order = core_def->default_byte_order;
962 }
963 else
964 {
965 if (update_triple)
966 m_triple = llvm::Triple();
967 m_byte_order = eByteOrderInvalid;
968 }
969}
970
971//===----------------------------------------------------------------------===//
972// Operators.
973
Greg Clayton70512312012-05-08 01:45:38 +0000974static bool
Jason Molendaba813dc2012-11-04 03:20:05 +0000975cores_match (const ArchSpec::Core core1, const ArchSpec::Core core2, bool try_inverse, bool enforce_exact_match)
Greg Clayton70512312012-05-08 01:45:38 +0000976{
Jason Molendaba813dc2012-11-04 03:20:05 +0000977 if (core1 == core2)
978 return true;
979
Greg Clayton70512312012-05-08 01:45:38 +0000980 switch (core1)
981 {
Greg Clayton70512312012-05-08 01:45:38 +0000982 case ArchSpec::kCore_any:
983 return true;
984
Greg Clayton44362e02014-07-12 00:11:34 +0000985 case ArchSpec::eCore_arm_generic:
986 if (enforce_exact_match)
987 break;
988 // Fall through to case below
Greg Clayton70512312012-05-08 01:45:38 +0000989 case ArchSpec::kCore_arm_any:
990 if (core2 >= ArchSpec::kCore_arm_first && core2 <= ArchSpec::kCore_arm_last)
991 return true;
992 if (core2 >= ArchSpec::kCore_thumb_first && core2 <= ArchSpec::kCore_thumb_last)
993 return true;
994 if (core2 == ArchSpec::kCore_arm_any)
995 return true;
996 break;
Sylvestre Ledru1c9e0642014-08-18 14:53:42 +0000997
Greg Clayton70512312012-05-08 01:45:38 +0000998 case ArchSpec::kCore_x86_32_any:
999 if ((core2 >= ArchSpec::kCore_x86_32_first && core2 <= ArchSpec::kCore_x86_32_last) || (core2 == ArchSpec::kCore_x86_32_any))
1000 return true;
1001 break;
Zachary Turnerad587ae42014-07-28 16:44:49 +00001002
1003 case ArchSpec::kCore_x86_64_any:
1004 if ((core2 >= ArchSpec::kCore_x86_64_first && core2 <= ArchSpec::kCore_x86_64_last) || (core2 == ArchSpec::kCore_x86_64_any))
1005 return true;
Sylvestre Ledru1c9e0642014-08-18 14:53:42 +00001006 break;
Zachary Turnerad587ae42014-07-28 16:44:49 +00001007
Greg Clayton70512312012-05-08 01:45:38 +00001008 case ArchSpec::kCore_ppc_any:
1009 if ((core2 >= ArchSpec::kCore_ppc_first && core2 <= ArchSpec::kCore_ppc_last) || (core2 == ArchSpec::kCore_ppc_any))
1010 return true;
1011 break;
Sylvestre Ledru1c9e0642014-08-18 14:53:42 +00001012
Greg Clayton70512312012-05-08 01:45:38 +00001013 case ArchSpec::kCore_ppc64_any:
1014 if ((core2 >= ArchSpec::kCore_ppc64_first && core2 <= ArchSpec::kCore_ppc64_last) || (core2 == ArchSpec::kCore_ppc64_any))
1015 return true;
1016 break;
1017
Jason Molendaa3a04522013-09-27 23:21:54 +00001018 case ArchSpec::eCore_arm_armv6m:
1019 if (!enforce_exact_match)
1020 {
Greg Clayton44362e02014-07-12 00:11:34 +00001021 if (core2 == ArchSpec::eCore_arm_generic)
1022 return true;
Jason Molendaa3a04522013-09-27 23:21:54 +00001023 try_inverse = false;
Jason Molendac7cda272013-09-27 23:29:10 +00001024 if (core2 == ArchSpec::eCore_arm_armv7)
Jason Molendaa3a04522013-09-27 23:21:54 +00001025 return true;
1026 }
Sylvestre Ledru1c9e0642014-08-18 14:53:42 +00001027 break;
Deepak Panickal6d3df422014-02-19 11:16:46 +00001028
1029 case ArchSpec::kCore_hexagon_any:
1030 if ((core2 >= ArchSpec::kCore_hexagon_first && core2 <= ArchSpec::kCore_hexagon_last) || (core2 == ArchSpec::kCore_hexagon_any))
1031 return true;
Jason Molendaa3a04522013-09-27 23:21:54 +00001032 break;
1033
Jason Molenda7a1559c2013-03-08 01:20:17 +00001034 case ArchSpec::eCore_arm_armv7m:
1035 case ArchSpec::eCore_arm_armv7em:
Johnny Chen1083b0d2012-08-28 22:53:40 +00001036 case ArchSpec::eCore_arm_armv7f:
1037 case ArchSpec::eCore_arm_armv7k:
1038 case ArchSpec::eCore_arm_armv7s:
Jason Molendaba813dc2012-11-04 03:20:05 +00001039 if (!enforce_exact_match)
1040 {
Greg Clayton44362e02014-07-12 00:11:34 +00001041 if (core2 == ArchSpec::eCore_arm_generic)
1042 return true;
Jason Molendaba813dc2012-11-04 03:20:05 +00001043 if (core2 == ArchSpec::eCore_arm_armv7)
1044 return true;
Greg Clayton44362e02014-07-12 00:11:34 +00001045 try_inverse = false;
Jason Molendaba813dc2012-11-04 03:20:05 +00001046 }
Johnny Chen1083b0d2012-08-28 22:53:40 +00001047 break;
Sylvestre Ledru1c9e0642014-08-18 14:53:42 +00001048
Greg Clayton52edb362014-07-14 22:53:02 +00001049 case ArchSpec::eCore_x86_64_x86_64h:
1050 if (!enforce_exact_match)
1051 {
1052 try_inverse = false;
1053 if (core2 == ArchSpec::eCore_x86_64_x86_64)
1054 return true;
1055 }
1056 break;
Johnny Chen1083b0d2012-08-28 22:53:40 +00001057
Todd Fiala02e71812014-08-28 14:32:43 +00001058 case ArchSpec::eCore_arm_armv8:
1059 if (!enforce_exact_match)
1060 {
1061 if (core2 == ArchSpec::eCore_arm_arm64)
1062 return true;
1063 if (core2 == ArchSpec::eCore_arm_aarch64)
1064 return true;
1065 try_inverse = false;
1066 }
1067 break;
1068
1069 case ArchSpec::eCore_arm_aarch64:
1070 if (!enforce_exact_match)
1071 {
1072 if (core2 == ArchSpec::eCore_arm_arm64)
1073 return true;
1074 if (core2 == ArchSpec::eCore_arm_armv8)
1075 return true;
1076 try_inverse = false;
1077 }
1078 break;
1079
1080 case ArchSpec::eCore_arm_arm64:
1081 if (!enforce_exact_match)
1082 {
1083 if (core2 == ArchSpec::eCore_arm_aarch64)
1084 return true;
1085 if (core2 == ArchSpec::eCore_arm_armv8)
1086 return true;
1087 try_inverse = false;
1088 }
1089 break;
1090
Greg Clayton70512312012-05-08 01:45:38 +00001091 default:
1092 break;
1093 }
1094 if (try_inverse)
Jason Molendaba813dc2012-11-04 03:20:05 +00001095 return cores_match (core2, core1, false, enforce_exact_match);
Greg Clayton70512312012-05-08 01:45:38 +00001096 return false;
1097}
1098
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001099bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001100lldb_private::operator<(const ArchSpec& lhs, const ArchSpec& rhs)
1101{
Greg Clayton64195a22011-02-23 00:35:02 +00001102 const ArchSpec::Core lhs_core = lhs.GetCore ();
1103 const ArchSpec::Core rhs_core = rhs.GetCore ();
1104 return lhs_core < rhs_core;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001105}
Greg Claytona97c4d22014-12-09 23:31:02 +00001106
1107static void
1108StopInfoOverrideCallbackTypeARM(lldb_private::Thread &thread)
1109{
1110 // We need to check if we are stopped in Thumb mode in a IT instruction
1111 // and detect if the condition doesn't pass. If this is the case it means
1112 // we won't actually execute this instruction. If this happens we need to
1113 // clear the stop reason to no thread plans think we are stopped for a
1114 // reason and the plans should keep going.
1115 //
1116 // We do this because when single stepping many ARM processes, debuggers
1117 // often use the BVR/BCR registers that says "stop when the PC is not
1118 // equal to its current value". This method of stepping means we can end
1119 // up stopping on instructions inside an if/then block that wouldn't get
1120 // executed. By fixing this we can stop the debugger from seeming like
1121 // you stepped through both the "if" _and_ the "else" clause when source
1122 // level stepping because the debugger stops regardless due to the BVR/BCR
1123 // triggering a stop.
1124 //
1125 // It also means we can set breakpoints on instructions inside an an
1126 // if/then block and correctly skip them if we use the BKPT instruction.
1127 // The ARM and Thumb BKPT instructions are unconditional even when executed
1128 // in a Thumb IT block.
1129 //
1130 // If your debugger inserts software traps in ARM/Thumb code, it will
1131 // need to use 16 and 32 bit instruction for 16 and 32 bit thumb
1132 // instructions respectively. If your debugger inserts a 16 bit thumb
1133 // trap on top of a 32 bit thumb instruction for an opcode that is inside
1134 // an if/then, it will change the it/then to conditionally execute your
1135 // 16 bit trap and then cause your program to crash if it executes the
1136 // trailing 16 bits (the second half of the 32 bit thumb instruction you
1137 // partially overwrote).
1138
1139 RegisterContextSP reg_ctx_sp (thread.GetRegisterContext());
1140 if (reg_ctx_sp)
1141 {
1142 const uint32_t cpsr = reg_ctx_sp->GetFlags(0);
1143 if (cpsr != 0)
1144 {
1145 // Read the J and T bits to get the ISETSTATE
1146 const uint32_t J = Bit32(cpsr, 24);
1147 const uint32_t T = Bit32(cpsr, 5);
1148 const uint32_t ISETSTATE = J << 1 | T;
1149 if (ISETSTATE == 0)
1150 {
1151 // NOTE: I am pretty sure we want to enable the code below
1152 // that detects when we stop on an instruction in ARM mode
1153 // that is conditional and the condition doesn't pass. This
1154 // can happen if you set a breakpoint on an instruction that
1155 // is conditional. We currently will _always_ stop on the
1156 // instruction which is bad. You can also run into this while
1157 // single stepping and you could appear to run code in the "if"
1158 // and in the "else" clause because it would stop at all of the
1159 // conditional instructions in both.
1160 // In such cases, we really don't want to stop at this location.
1161 // I will check with the lldb-dev list first before I enable this.
1162#if 0
1163 // ARM mode: check for condition on intsruction
1164 const addr_t pc = reg_ctx_sp->GetPC();
1165 Error error;
1166 // If we fail to read the opcode we will get UINT64_MAX as the
1167 // result in "opcode" which we can use to detect if we read a
1168 // valid opcode.
1169 const uint64_t opcode = thread.GetProcess()->ReadUnsignedIntegerFromMemory(pc, 4, UINT64_MAX, error);
1170 if (opcode <= UINT32_MAX)
1171 {
1172 const uint32_t condition = Bits32((uint32_t)opcode, 31, 28);
1173 if (ARMConditionPassed(condition, cpsr) == false)
1174 {
1175 // We ARE stopped on an ARM instruction whose condition doesn't
1176 // pass so this instruction won't get executed.
1177 // Regardless of why it stopped, we need to clear the stop info
1178 thread.SetStopInfo (StopInfoSP());
1179 }
1180 }
1181#endif
1182 }
1183 else if (ISETSTATE == 1)
1184 {
1185 // Thumb mode
1186 const uint32_t ITSTATE = Bits32 (cpsr, 15, 10) << 2 | Bits32 (cpsr, 26, 25);
1187 if (ITSTATE != 0)
1188 {
1189 const uint32_t condition = Bits32(ITSTATE, 7, 4);
1190 if (ARMConditionPassed(condition, cpsr) == false)
1191 {
1192 // We ARE stopped in a Thumb IT instruction on an instruction whose
1193 // condition doesn't pass so this instruction won't get executed.
1194 // Regardless of why it stopped, we need to clear the stop info
1195 thread.SetStopInfo (StopInfoSP());
1196 }
1197 }
1198 }
1199 }
1200 }
1201}
1202
1203ArchSpec::StopInfoOverrideCallbackType
1204ArchSpec::GetStopInfoOverrideCallback () const
1205{
1206 const llvm::Triple::ArchType machine = GetMachine();
1207 if (machine == llvm::Triple::arm)
1208 return StopInfoOverrideCallbackTypeARM;
1209 return NULL;
1210}