blob: ecbd884ec52fc7b7c9c2dbc952bb4e05c274b4b4 [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
Eugene Zelenko896ddd02016-03-02 01:09:03 +000012// C Includes
13// C++ Includes
14#include <cstdio>
15#include <cerrno>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016#include <string>
17
Eugene Zelenko896ddd02016-03-02 01:09:03 +000018// Other libraries and framework includes
Saleem Abdulrasool28606952014-06-27 05:17:41 +000019#include "llvm/ADT/STLExtras.h"
Charles Davis237ad972013-08-27 05:04:33 +000020#include "llvm/Support/COFF.h"
Greg Clayton41f92322010-06-11 03:25:34 +000021#include "llvm/Support/ELF.h"
Stephen Wilsonfacebfc2011-02-24 19:13:58 +000022#include "llvm/Support/Host.h"
Zachary Turner50232572015-03-18 21:31:45 +000023
Eugene Zelenko896ddd02016-03-02 01:09:03 +000024// Project includes
Greg Claytone795f1b2012-08-08 01:19:34 +000025#include "lldb/Core/RegularExpression.h"
Zachary Turner13b18262014-08-20 16:42:51 +000026#include "lldb/Core/StringList.h"
Greg Clayton514487e2011-02-15 21:59:32 +000027#include "lldb/Host/Endian.h"
Zachary Turner13b18262014-08-20 16:42:51 +000028#include "lldb/Host/HostInfo.h"
Greg Claytoneb0103f2011-04-07 22:46:35 +000029#include "lldb/Target/Platform.h"
Greg Claytona97c4d22014-12-09 23:31:02 +000030#include "lldb/Target/Process.h"
31#include "lldb/Target/RegisterContext.h"
32#include "lldb/Target/Thread.h"
Zachary Turner50232572015-03-18 21:31:45 +000033#include "lldb/Utility/NameMatches.h"
34#include "lldb/Utility/SafeMachO.h"
Greg Claytona97c4d22014-12-09 23:31:02 +000035#include "Plugins/Process/Utility/ARMDefines.h"
36#include "Plugins/Process/Utility/InstructionUtils.h"
Greg Clayton41f92322010-06-11 03:25:34 +000037
Chris Lattner30fdc8d2010-06-08 16:52:24 +000038using namespace lldb;
39using namespace lldb_private;
40
Jason Molendaba813dc2012-11-04 03:20:05 +000041static 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
Eugene Zelenko896ddd02016-03-02 01:09:03 +000056} // namespace lldb_private
Greg Clayton64195a22011-02-23 00:35:02 +000057
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
Mohit K. Bhakkade8659b52015-04-23 06:36:20 +000092 // mips32, mips32r2, mips32r3, mips32r5, mips32r6
Jaydeep Patil501a7812015-07-16 03:51:55 +000093 { eByteOrderBig , 4, 2, 4, llvm::Triple::mips , ArchSpec::eCore_mips32 , "mips" },
94 { eByteOrderBig , 4, 2, 4, llvm::Triple::mips , ArchSpec::eCore_mips32r2 , "mipsr2" },
95 { eByteOrderBig , 4, 2, 4, llvm::Triple::mips , ArchSpec::eCore_mips32r3 , "mipsr3" },
96 { eByteOrderBig , 4, 2, 4, llvm::Triple::mips , ArchSpec::eCore_mips32r5 , "mipsr5" },
97 { eByteOrderBig , 4, 2, 4, llvm::Triple::mips , ArchSpec::eCore_mips32r6 , "mipsr6" },
98 { eByteOrderLittle, 4, 2, 4, llvm::Triple::mipsel, ArchSpec::eCore_mips32el , "mipsel" },
99 { eByteOrderLittle, 4, 2, 4, llvm::Triple::mipsel, ArchSpec::eCore_mips32r2el , "mipsr2el" },
100 { eByteOrderLittle, 4, 2, 4, llvm::Triple::mipsel, ArchSpec::eCore_mips32r3el , "mipsr3el" },
101 { eByteOrderLittle, 4, 2, 4, llvm::Triple::mipsel, ArchSpec::eCore_mips32r5el , "mipsr5el" },
102 { eByteOrderLittle, 4, 2, 4, llvm::Triple::mipsel, ArchSpec::eCore_mips32r6el , "mipsr6el" },
Mohit K. Bhakkade8659b52015-04-23 06:36:20 +0000103
104 // mips64, mips64r2, mips64r3, mips64r5, mips64r6
Jaydeep Patil501a7812015-07-16 03:51:55 +0000105 { eByteOrderBig , 8, 2, 4, llvm::Triple::mips64 , ArchSpec::eCore_mips64 , "mips64" },
106 { eByteOrderBig , 8, 2, 4, llvm::Triple::mips64 , ArchSpec::eCore_mips64r2 , "mips64r2" },
107 { eByteOrderBig , 8, 2, 4, llvm::Triple::mips64 , ArchSpec::eCore_mips64r3 , "mips64r3" },
108 { eByteOrderBig , 8, 2, 4, llvm::Triple::mips64 , ArchSpec::eCore_mips64r5 , "mips64r5" },
109 { eByteOrderBig , 8, 2, 4, llvm::Triple::mips64 , ArchSpec::eCore_mips64r6 , "mips64r6" },
110 { eByteOrderLittle, 8, 2, 4, llvm::Triple::mips64el, ArchSpec::eCore_mips64el , "mips64el" },
111 { eByteOrderLittle, 8, 2, 4, llvm::Triple::mips64el, ArchSpec::eCore_mips64r2el , "mips64r2el" },
112 { eByteOrderLittle, 8, 2, 4, llvm::Triple::mips64el, ArchSpec::eCore_mips64r3el , "mips64r3el" },
113 { eByteOrderLittle, 8, 2, 4, llvm::Triple::mips64el, ArchSpec::eCore_mips64r5el , "mips64r5el" },
114 { eByteOrderLittle, 8, 2, 4, llvm::Triple::mips64el, ArchSpec::eCore_mips64r6el , "mips64r6el" },
Greg Clayton64195a22011-02-23 00:35:02 +0000115
Justin Hibbits6256a0e2014-10-31 02:34:28 +0000116 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_generic , "powerpc" },
Greg Clayton83b162d2013-08-12 18:34:04 +0000117 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc601 , "ppc601" },
118 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc602 , "ppc602" },
119 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc603 , "ppc603" },
120 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc603e , "ppc603e" },
121 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc603ev , "ppc603ev" },
122 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc604 , "ppc604" },
123 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc604e , "ppc604e" },
124 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc620 , "ppc620" },
125 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc750 , "ppc750" },
126 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc7400 , "ppc7400" },
127 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc7450 , "ppc7450" },
128 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc970 , "ppc970" },
Greg Clayton64195a22011-02-23 00:35:02 +0000129
Justin Hibbits6256a0e2014-10-31 02:34:28 +0000130 { eByteOrderBig , 8, 4, 4, llvm::Triple::ppc64 , ArchSpec::eCore_ppc64_generic , "powerpc64" },
Greg Clayton83b162d2013-08-12 18:34:04 +0000131 { eByteOrderBig , 8, 4, 4, llvm::Triple::ppc64 , ArchSpec::eCore_ppc64_ppc970_64 , "ppc970-64" },
Greg Clayton64195a22011-02-23 00:35:02 +0000132
Greg Clayton357132e2011-03-26 19:14:58 +0000133 { eByteOrderLittle, 4, 4, 4, llvm::Triple::sparc , ArchSpec::eCore_sparc_generic , "sparc" },
134 { eByteOrderLittle, 8, 4, 4, llvm::Triple::sparcv9, ArchSpec::eCore_sparc9_generic , "sparcv9" },
Greg Clayton64195a22011-02-23 00:35:02 +0000135
Greg Claytonab65b342011-04-13 22:47:15 +0000136 { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i386 , "i386" },
137 { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i486 , "i486" },
138 { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i486sx , "i486sx" },
Virgile Bello97a70e42014-04-08 14:48:48 +0000139 { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i686 , "i686" },
Greg Clayton64195a22011-02-23 00:35:02 +0000140
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000141 { eByteOrderLittle, 8, 1, 15, llvm::Triple::x86_64 , ArchSpec::eCore_x86_64_x86_64 , "x86_64" },
Greg Claytona86dc432014-01-22 23:42:03 +0000142 { eByteOrderLittle, 8, 1, 15, llvm::Triple::x86_64 , ArchSpec::eCore_x86_64_x86_64h , "x86_64h" },
Deepak Panickal6d3df422014-02-19 11:16:46 +0000143 { eByteOrderLittle, 4, 4, 4, llvm::Triple::hexagon , ArchSpec::eCore_hexagon_generic, "hexagon" },
144 { eByteOrderLittle, 4, 4, 4, llvm::Triple::hexagon , ArchSpec::eCore_hexagon_hexagonv4, "hexagonv4" },
145 { eByteOrderLittle, 4, 4, 4, llvm::Triple::hexagon , ArchSpec::eCore_hexagon_hexagonv5, "hexagonv5" },
146
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000147 { eByteOrderLittle, 4, 4, 4 , llvm::Triple::UnknownArch , ArchSpec::eCore_uknownMach32 , "unknown-mach-32" },
Todd Fiala14bbef52014-07-01 23:33:32 +0000148 { eByteOrderLittle, 8, 4, 4 , llvm::Triple::UnknownArch , ArchSpec::eCore_uknownMach64 , "unknown-mach-64" },
149
Matthew Gardiner5f675792014-08-27 12:09:39 +0000150 { eByteOrderBig , 4, 1, 1 , llvm::Triple::kalimba , ArchSpec::eCore_kalimba3 , "kalimba3" },
151 { eByteOrderLittle, 4, 1, 1 , llvm::Triple::kalimba , ArchSpec::eCore_kalimba4 , "kalimba4" },
152 { eByteOrderLittle, 4, 1, 1 , llvm::Triple::kalimba , ArchSpec::eCore_kalimba5 , "kalimba5" }
Greg Clayton64195a22011-02-23 00:35:02 +0000153};
154
Greg Clayton56b79682014-07-23 18:12:06 +0000155// Ensure that we have an entry in the g_core_definitions for each core. If you comment out an entry above,
156// you will need to comment out the corresponding ArchSpec::Core enumeration.
Zachary Turner3b2065f2014-07-28 16:44:28 +0000157static_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 +0000158
Greg Clayton64195a22011-02-23 00:35:02 +0000159struct ArchDefinitionEntry
160{
161 ArchSpec::Core core;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000162 uint32_t cpu;
163 uint32_t sub;
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000164 uint32_t cpu_mask;
165 uint32_t sub_mask;
Greg Clayton64195a22011-02-23 00:35:02 +0000166};
167
168struct ArchDefinition
169{
170 ArchitectureType type;
171 size_t num_entries;
172 const ArchDefinitionEntry *entries;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000173 const char *name;
174};
175
Greg Claytonc7bece562013-01-25 18:06:21 +0000176size_t
Greg Claytonab65b342011-04-13 22:47:15 +0000177ArchSpec::AutoComplete (const char *name, StringList &matches)
178{
Greg Claytonab65b342011-04-13 22:47:15 +0000179 if (name && name[0])
180 {
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000181 for (uint32_t i = 0; i < llvm::array_lengthof(g_core_definitions); ++i)
Greg Claytonab65b342011-04-13 22:47:15 +0000182 {
183 if (NameMatches(g_core_definitions[i].name, eNameMatchStartsWith, name))
184 matches.AppendString (g_core_definitions[i].name);
185 }
186 }
187 else
188 {
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000189 for (uint32_t i = 0; i < llvm::array_lengthof(g_core_definitions); ++i)
Greg Claytonab65b342011-04-13 22:47:15 +0000190 matches.AppendString (g_core_definitions[i].name);
191 }
192 return matches.GetSize();
193}
194
Greg Clayton64195a22011-02-23 00:35:02 +0000195#define CPU_ANY (UINT32_MAX)
196
197//===----------------------------------------------------------------------===//
198// A table that gets searched linearly for matches. This table is used to
199// convert cpu type and subtypes to architecture names, and to convert
200// architecture names to cpu types and subtypes. The ordering is important and
201// allows the precedence to be set when the table is built.
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000202#define SUBTYPE_MASK 0x00FFFFFFu
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000203
Greg Clayton64195a22011-02-23 00:35:02 +0000204static const ArchDefinitionEntry g_macho_arch_entries[] =
Greg Clayton41f92322010-06-11 03:25:34 +0000205{
Charles Davis510938e2013-08-27 05:04:57 +0000206 { ArchSpec::eCore_arm_generic , llvm::MachO::CPU_TYPE_ARM , CPU_ANY, UINT32_MAX , UINT32_MAX },
207 { ArchSpec::eCore_arm_generic , llvm::MachO::CPU_TYPE_ARM , 0 , UINT32_MAX , SUBTYPE_MASK },
208 { ArchSpec::eCore_arm_armv4 , llvm::MachO::CPU_TYPE_ARM , 5 , UINT32_MAX , SUBTYPE_MASK },
209 { ArchSpec::eCore_arm_armv4t , llvm::MachO::CPU_TYPE_ARM , 5 , UINT32_MAX , SUBTYPE_MASK },
210 { ArchSpec::eCore_arm_armv6 , llvm::MachO::CPU_TYPE_ARM , 6 , UINT32_MAX , SUBTYPE_MASK },
Jason Molenda64a11732013-10-08 03:01:08 +0000211 { ArchSpec::eCore_arm_armv6m , llvm::MachO::CPU_TYPE_ARM , 14 , UINT32_MAX , SUBTYPE_MASK },
Charles Davis510938e2013-08-27 05:04:57 +0000212 { ArchSpec::eCore_arm_armv5 , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
213 { ArchSpec::eCore_arm_armv5e , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
214 { ArchSpec::eCore_arm_armv5t , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
215 { ArchSpec::eCore_arm_xscale , llvm::MachO::CPU_TYPE_ARM , 8 , UINT32_MAX , SUBTYPE_MASK },
216 { ArchSpec::eCore_arm_armv7 , llvm::MachO::CPU_TYPE_ARM , 9 , UINT32_MAX , SUBTYPE_MASK },
217 { ArchSpec::eCore_arm_armv7f , llvm::MachO::CPU_TYPE_ARM , 10 , UINT32_MAX , SUBTYPE_MASK },
218 { ArchSpec::eCore_arm_armv7s , llvm::MachO::CPU_TYPE_ARM , 11 , UINT32_MAX , SUBTYPE_MASK },
219 { ArchSpec::eCore_arm_armv7k , llvm::MachO::CPU_TYPE_ARM , 12 , UINT32_MAX , SUBTYPE_MASK },
220 { ArchSpec::eCore_arm_armv7m , llvm::MachO::CPU_TYPE_ARM , 15 , UINT32_MAX , SUBTYPE_MASK },
221 { ArchSpec::eCore_arm_armv7em , llvm::MachO::CPU_TYPE_ARM , 16 , UINT32_MAX , SUBTYPE_MASK },
Jason Molendaa3329782014-03-29 18:54:20 +0000222 { ArchSpec::eCore_arm_arm64 , llvm::MachO::CPU_TYPE_ARM64 , 1 , UINT32_MAX , SUBTYPE_MASK },
Jason Molenda22952582014-11-12 01:11:36 +0000223 { ArchSpec::eCore_arm_arm64 , llvm::MachO::CPU_TYPE_ARM64 , 0 , UINT32_MAX , SUBTYPE_MASK },
Jason Molendaa3329782014-03-29 18:54:20 +0000224 { ArchSpec::eCore_arm_arm64 , llvm::MachO::CPU_TYPE_ARM64 , 13 , UINT32_MAX , SUBTYPE_MASK },
Jason Molenda22952582014-11-12 01:11:36 +0000225 { ArchSpec::eCore_arm_arm64 , llvm::MachO::CPU_TYPE_ARM64 , CPU_ANY, UINT32_MAX , SUBTYPE_MASK },
Charles Davis510938e2013-08-27 05:04:57 +0000226 { ArchSpec::eCore_thumb , llvm::MachO::CPU_TYPE_ARM , 0 , UINT32_MAX , SUBTYPE_MASK },
227 { ArchSpec::eCore_thumbv4t , llvm::MachO::CPU_TYPE_ARM , 5 , UINT32_MAX , SUBTYPE_MASK },
228 { ArchSpec::eCore_thumbv5 , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
229 { ArchSpec::eCore_thumbv5e , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
230 { ArchSpec::eCore_thumbv6 , llvm::MachO::CPU_TYPE_ARM , 6 , UINT32_MAX , SUBTYPE_MASK },
Jason Molenda64a11732013-10-08 03:01:08 +0000231 { ArchSpec::eCore_thumbv6m , llvm::MachO::CPU_TYPE_ARM , 14 , UINT32_MAX , SUBTYPE_MASK },
Charles Davis510938e2013-08-27 05:04:57 +0000232 { ArchSpec::eCore_thumbv7 , llvm::MachO::CPU_TYPE_ARM , 9 , UINT32_MAX , SUBTYPE_MASK },
233 { ArchSpec::eCore_thumbv7f , llvm::MachO::CPU_TYPE_ARM , 10 , UINT32_MAX , SUBTYPE_MASK },
234 { ArchSpec::eCore_thumbv7s , llvm::MachO::CPU_TYPE_ARM , 11 , UINT32_MAX , SUBTYPE_MASK },
235 { ArchSpec::eCore_thumbv7k , llvm::MachO::CPU_TYPE_ARM , 12 , UINT32_MAX , SUBTYPE_MASK },
236 { ArchSpec::eCore_thumbv7m , llvm::MachO::CPU_TYPE_ARM , 15 , UINT32_MAX , SUBTYPE_MASK },
237 { ArchSpec::eCore_thumbv7em , llvm::MachO::CPU_TYPE_ARM , 16 , UINT32_MAX , SUBTYPE_MASK },
238 { ArchSpec::eCore_ppc_generic , llvm::MachO::CPU_TYPE_POWERPC , CPU_ANY, UINT32_MAX , UINT32_MAX },
239 { ArchSpec::eCore_ppc_generic , llvm::MachO::CPU_TYPE_POWERPC , 0 , UINT32_MAX , SUBTYPE_MASK },
240 { ArchSpec::eCore_ppc_ppc601 , llvm::MachO::CPU_TYPE_POWERPC , 1 , UINT32_MAX , SUBTYPE_MASK },
241 { ArchSpec::eCore_ppc_ppc602 , llvm::MachO::CPU_TYPE_POWERPC , 2 , UINT32_MAX , SUBTYPE_MASK },
242 { ArchSpec::eCore_ppc_ppc603 , llvm::MachO::CPU_TYPE_POWERPC , 3 , UINT32_MAX , SUBTYPE_MASK },
243 { ArchSpec::eCore_ppc_ppc603e , llvm::MachO::CPU_TYPE_POWERPC , 4 , UINT32_MAX , SUBTYPE_MASK },
244 { ArchSpec::eCore_ppc_ppc603ev , llvm::MachO::CPU_TYPE_POWERPC , 5 , UINT32_MAX , SUBTYPE_MASK },
245 { ArchSpec::eCore_ppc_ppc604 , llvm::MachO::CPU_TYPE_POWERPC , 6 , UINT32_MAX , SUBTYPE_MASK },
246 { ArchSpec::eCore_ppc_ppc604e , llvm::MachO::CPU_TYPE_POWERPC , 7 , UINT32_MAX , SUBTYPE_MASK },
247 { ArchSpec::eCore_ppc_ppc620 , llvm::MachO::CPU_TYPE_POWERPC , 8 , UINT32_MAX , SUBTYPE_MASK },
248 { ArchSpec::eCore_ppc_ppc750 , llvm::MachO::CPU_TYPE_POWERPC , 9 , UINT32_MAX , SUBTYPE_MASK },
249 { ArchSpec::eCore_ppc_ppc7400 , llvm::MachO::CPU_TYPE_POWERPC , 10 , UINT32_MAX , SUBTYPE_MASK },
250 { ArchSpec::eCore_ppc_ppc7450 , llvm::MachO::CPU_TYPE_POWERPC , 11 , UINT32_MAX , SUBTYPE_MASK },
251 { ArchSpec::eCore_ppc_ppc970 , llvm::MachO::CPU_TYPE_POWERPC , 100 , UINT32_MAX , SUBTYPE_MASK },
252 { ArchSpec::eCore_ppc64_generic , llvm::MachO::CPU_TYPE_POWERPC64 , 0 , UINT32_MAX , SUBTYPE_MASK },
253 { ArchSpec::eCore_ppc64_ppc970_64 , llvm::MachO::CPU_TYPE_POWERPC64 , 100 , UINT32_MAX , SUBTYPE_MASK },
254 { ArchSpec::eCore_x86_32_i386 , llvm::MachO::CPU_TYPE_I386 , 3 , UINT32_MAX , SUBTYPE_MASK },
255 { ArchSpec::eCore_x86_32_i486 , llvm::MachO::CPU_TYPE_I386 , 4 , UINT32_MAX , SUBTYPE_MASK },
256 { ArchSpec::eCore_x86_32_i486sx , llvm::MachO::CPU_TYPE_I386 , 0x84 , UINT32_MAX , SUBTYPE_MASK },
Greg Claytona86dc432014-01-22 23:42:03 +0000257 { ArchSpec::eCore_x86_32_i386 , llvm::MachO::CPU_TYPE_I386 , CPU_ANY, UINT32_MAX , UINT32_MAX },
Charles Davis510938e2013-08-27 05:04:57 +0000258 { ArchSpec::eCore_x86_64_x86_64 , llvm::MachO::CPU_TYPE_X86_64 , 3 , UINT32_MAX , SUBTYPE_MASK },
259 { ArchSpec::eCore_x86_64_x86_64 , llvm::MachO::CPU_TYPE_X86_64 , 4 , UINT32_MAX , SUBTYPE_MASK },
Greg Claytona86dc432014-01-22 23:42:03 +0000260 { ArchSpec::eCore_x86_64_x86_64h , llvm::MachO::CPU_TYPE_X86_64 , 8 , UINT32_MAX , SUBTYPE_MASK },
261 { 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 +0000262 // Catch any unknown mach architectures so we can always use the object and symbol mach-o files
Charles Davis510938e2013-08-27 05:04:57 +0000263 { ArchSpec::eCore_uknownMach32 , 0 , 0 , 0xFF000000u, 0x00000000u },
264 { ArchSpec::eCore_uknownMach64 , llvm::MachO::CPU_ARCH_ABI64 , 0 , 0xFF000000u, 0x00000000u }
Greg Clayton64195a22011-02-23 00:35:02 +0000265};
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000266
Greg Clayton64195a22011-02-23 00:35:02 +0000267static const ArchDefinition g_macho_arch_def = {
268 eArchTypeMachO,
Saleem Abdulrasool28606952014-06-27 05:17:41 +0000269 llvm::array_lengthof(g_macho_arch_entries),
Greg Clayton64195a22011-02-23 00:35:02 +0000270 g_macho_arch_entries,
Greg Clayton64195a22011-02-23 00:35:02 +0000271 "mach-o"
Greg Clayton41f92322010-06-11 03:25:34 +0000272};
273
Greg Clayton64195a22011-02-23 00:35:02 +0000274//===----------------------------------------------------------------------===//
275// A table that gets searched linearly for matches. This table is used to
276// convert cpu type and subtypes to architecture names, and to convert
277// architecture names to cpu types and subtypes. The ordering is important and
278// allows the precedence to be set when the table is built.
279static const ArchDefinitionEntry g_elf_arch_entries[] =
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000280{
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000281 { ArchSpec::eCore_sparc_generic , llvm::ELF::EM_SPARC , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // Sparc
282 { ArchSpec::eCore_x86_32_i386 , llvm::ELF::EM_386 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // Intel 80386
Rafael Espindola86f422e2015-06-19 17:02:25 +0000283 { ArchSpec::eCore_x86_32_i486 , llvm::ELF::EM_IAMCU , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // Intel MCU // FIXME: is this correct?
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000284 { ArchSpec::eCore_ppc_generic , llvm::ELF::EM_PPC , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // PowerPC
285 { ArchSpec::eCore_ppc64_generic , llvm::ELF::EM_PPC64 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // PowerPC64
286 { ArchSpec::eCore_arm_generic , llvm::ELF::EM_ARM , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // ARM
Todd Fiala02e71812014-08-28 14:32:43 +0000287 { ArchSpec::eCore_arm_aarch64 , llvm::ELF::EM_AARCH64, LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // ARM64
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000288 { ArchSpec::eCore_sparc9_generic , llvm::ELF::EM_SPARCV9, LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // SPARC V9
Ed Masteb73f8442013-10-10 00:59:47 +0000289 { ArchSpec::eCore_x86_64_x86_64 , llvm::ELF::EM_X86_64 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // AMD64
Mohit K. Bhakkade8659b52015-04-23 06:36:20 +0000290 { ArchSpec::eCore_mips32 , llvm::ELF::EM_MIPS , ArchSpec::eMIPSSubType_mips32, 0xFFFFFFFFu, 0xFFFFFFFFu }, // mips32
291 { ArchSpec::eCore_mips32r2 , llvm::ELF::EM_MIPS , ArchSpec::eMIPSSubType_mips32r2, 0xFFFFFFFFu, 0xFFFFFFFFu }, // mips32r2
292 { ArchSpec::eCore_mips32r6 , llvm::ELF::EM_MIPS , ArchSpec::eMIPSSubType_mips32r6, 0xFFFFFFFFu, 0xFFFFFFFFu }, // mips32r6
293 { ArchSpec::eCore_mips32el , llvm::ELF::EM_MIPS , ArchSpec::eMIPSSubType_mips32el, 0xFFFFFFFFu, 0xFFFFFFFFu }, // mips32el
294 { ArchSpec::eCore_mips32r2el , llvm::ELF::EM_MIPS , ArchSpec::eMIPSSubType_mips32r2el, 0xFFFFFFFFu, 0xFFFFFFFFu }, // mips32r2el
295 { ArchSpec::eCore_mips32r6el , llvm::ELF::EM_MIPS , ArchSpec::eMIPSSubType_mips32r6el, 0xFFFFFFFFu, 0xFFFFFFFFu }, // mips32r6el
296 { ArchSpec::eCore_mips64 , llvm::ELF::EM_MIPS , ArchSpec::eMIPSSubType_mips64, 0xFFFFFFFFu, 0xFFFFFFFFu }, // mips64
297 { ArchSpec::eCore_mips64r2 , llvm::ELF::EM_MIPS , ArchSpec::eMIPSSubType_mips64r2, 0xFFFFFFFFu, 0xFFFFFFFFu }, // mips64r2
298 { ArchSpec::eCore_mips64r6 , llvm::ELF::EM_MIPS , ArchSpec::eMIPSSubType_mips64r6, 0xFFFFFFFFu, 0xFFFFFFFFu }, // mips64r6
299 { ArchSpec::eCore_mips64el , llvm::ELF::EM_MIPS , ArchSpec::eMIPSSubType_mips64el, 0xFFFFFFFFu, 0xFFFFFFFFu }, // mips64el
300 { ArchSpec::eCore_mips64r2el , llvm::ELF::EM_MIPS , ArchSpec::eMIPSSubType_mips64r2el, 0xFFFFFFFFu, 0xFFFFFFFFu }, // mips64r2el
301 { ArchSpec::eCore_mips64r6el , llvm::ELF::EM_MIPS , ArchSpec::eMIPSSubType_mips64r6el, 0xFFFFFFFFu, 0xFFFFFFFFu }, // mips64r6el
Todd Fiala14bbef52014-07-01 23:33:32 +0000302 { ArchSpec::eCore_hexagon_generic , llvm::ELF::EM_HEXAGON, LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // HEXAGON
Matthew Gardinerf03e6d842014-09-29 08:02:24 +0000303 { ArchSpec::eCore_kalimba3 , llvm::ELF::EM_CSR_KALIMBA, llvm::Triple::KalimbaSubArch_v3, 0xFFFFFFFFu, 0xFFFFFFFFu }, // KALIMBA
304 { ArchSpec::eCore_kalimba4 , llvm::ELF::EM_CSR_KALIMBA, llvm::Triple::KalimbaSubArch_v4, 0xFFFFFFFFu, 0xFFFFFFFFu }, // KALIMBA
305 { ArchSpec::eCore_kalimba5 , llvm::ELF::EM_CSR_KALIMBA, llvm::Triple::KalimbaSubArch_v5, 0xFFFFFFFFu, 0xFFFFFFFFu } // KALIMBA
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000306};
307
Greg Clayton64195a22011-02-23 00:35:02 +0000308static const ArchDefinition g_elf_arch_def = {
309 eArchTypeELF,
Saleem Abdulrasool28606952014-06-27 05:17:41 +0000310 llvm::array_lengthof(g_elf_arch_entries),
Greg Clayton64195a22011-02-23 00:35:02 +0000311 g_elf_arch_entries,
Greg Clayton64195a22011-02-23 00:35:02 +0000312 "elf",
Greg Clayton41f92322010-06-11 03:25:34 +0000313};
314
Charles Davis237ad972013-08-27 05:04:33 +0000315static const ArchDefinitionEntry g_coff_arch_entries[] =
316{
Zachary Turnerad587ae42014-07-28 16:44:49 +0000317 { 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 +0000318 { ArchSpec::eCore_ppc_generic , llvm::COFF::IMAGE_FILE_MACHINE_POWERPC , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // PowerPC
319 { ArchSpec::eCore_ppc_generic , llvm::COFF::IMAGE_FILE_MACHINE_POWERPCFP, LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // PowerPC (with FPU)
320 { ArchSpec::eCore_arm_generic , llvm::COFF::IMAGE_FILE_MACHINE_ARM , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // ARM
Saleem Abdulrasool1108cb32014-03-11 03:09:08 +0000321 { ArchSpec::eCore_arm_armv7 , llvm::COFF::IMAGE_FILE_MACHINE_ARMNT , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // ARMv7
Charles Davis237ad972013-08-27 05:04:33 +0000322 { ArchSpec::eCore_thumb , llvm::COFF::IMAGE_FILE_MACHINE_THUMB , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // ARMv7
323 { ArchSpec::eCore_x86_64_x86_64, llvm::COFF::IMAGE_FILE_MACHINE_AMD64 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu } // AMD64
324};
325
326static const ArchDefinition g_coff_arch_def = {
327 eArchTypeCOFF,
Saleem Abdulrasool28606952014-06-27 05:17:41 +0000328 llvm::array_lengthof(g_coff_arch_entries),
Charles Davis237ad972013-08-27 05:04:33 +0000329 g_coff_arch_entries,
330 "pe-coff",
331};
332
Greg Clayton64195a22011-02-23 00:35:02 +0000333//===----------------------------------------------------------------------===//
334// Table of all ArchDefinitions
335static const ArchDefinition *g_arch_definitions[] = {
336 &g_macho_arch_def,
Charles Davis237ad972013-08-27 05:04:33 +0000337 &g_elf_arch_def,
338 &g_coff_arch_def
Greg Clayton64195a22011-02-23 00:35:02 +0000339};
Greg Clayton41f92322010-06-11 03:25:34 +0000340
Saleem Abdulrasool28606952014-06-27 05:17:41 +0000341static const size_t k_num_arch_definitions = llvm::array_lengthof(g_arch_definitions);
Greg Clayton64195a22011-02-23 00:35:02 +0000342
343//===----------------------------------------------------------------------===//
344// Static helper functions.
345
Greg Clayton64195a22011-02-23 00:35:02 +0000346// Get the architecture definition for a given object type.
347static const ArchDefinition *
348FindArchDefinition (ArchitectureType arch_type)
349{
350 for (unsigned int i = 0; i < k_num_arch_definitions; ++i)
351 {
352 const ArchDefinition *def = g_arch_definitions[i];
353 if (def->type == arch_type)
354 return def;
355 }
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000356 return nullptr;
Greg Clayton64195a22011-02-23 00:35:02 +0000357}
358
359// Get an architecture definition by name.
360static const CoreDefinition *
361FindCoreDefinition (llvm::StringRef name)
362{
Greg Clayton56b79682014-07-23 18:12:06 +0000363 for (unsigned int i = 0; i < llvm::array_lengthof(g_core_definitions); ++i)
Greg Clayton64195a22011-02-23 00:35:02 +0000364 {
365 if (name.equals_lower(g_core_definitions[i].name))
366 return &g_core_definitions[i];
367 }
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000368 return nullptr;
Greg Clayton64195a22011-02-23 00:35:02 +0000369}
370
371static inline const CoreDefinition *
372FindCoreDefinition (ArchSpec::Core core)
373{
Greg Clayton56b79682014-07-23 18:12:06 +0000374 if (core >= 0 && core < llvm::array_lengthof(g_core_definitions))
Greg Clayton64195a22011-02-23 00:35:02 +0000375 return &g_core_definitions[core];
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000376 return nullptr;
Greg Clayton64195a22011-02-23 00:35:02 +0000377}
378
379// Get a definition entry by cpu type and subtype.
380static const ArchDefinitionEntry *
381FindArchDefinitionEntry (const ArchDefinition *def, uint32_t cpu, uint32_t sub)
382{
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000383 if (def == nullptr)
384 return nullptr;
Greg Clayton64195a22011-02-23 00:35:02 +0000385
Greg Clayton64195a22011-02-23 00:35:02 +0000386 const ArchDefinitionEntry *entries = def->entries;
387 for (size_t i = 0; i < def->num_entries; ++i)
388 {
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000389 if (entries[i].cpu == (cpu & entries[i].cpu_mask))
390 if (entries[i].sub == (sub & entries[i].sub_mask))
391 return &entries[i];
Greg Clayton64195a22011-02-23 00:35:02 +0000392 }
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000393 return nullptr;
Greg Clayton64195a22011-02-23 00:35:02 +0000394}
395
396static const ArchDefinitionEntry *
397FindArchDefinitionEntry (const ArchDefinition *def, ArchSpec::Core core)
398{
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000399 if (def == nullptr)
400 return nullptr;
Greg Clayton64195a22011-02-23 00:35:02 +0000401
402 const ArchDefinitionEntry *entries = def->entries;
403 for (size_t i = 0; i < def->num_entries; ++i)
404 {
405 if (entries[i].core == core)
406 return &entries[i];
407 }
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000408 return nullptr;
Greg Clayton64195a22011-02-23 00:35:02 +0000409}
410
411//===----------------------------------------------------------------------===//
412// Constructors and destructors.
413
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000414ArchSpec::ArchSpec() :
Greg Clayton514487e2011-02-15 21:59:32 +0000415 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000416 m_core (kCore_invalid),
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000417 m_byte_order (eByteOrderInvalid),
Pavel Labath07695bd2015-07-16 13:11:34 +0000418 m_flags (0),
419 m_distribution_id ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000420{
421}
422
Greg Claytoneb0103f2011-04-07 22:46:35 +0000423ArchSpec::ArchSpec (const char *triple_cstr, Platform *platform) :
Greg Clayton514487e2011-02-15 21:59:32 +0000424 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000425 m_core (kCore_invalid),
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000426 m_byte_order (eByteOrderInvalid),
Pavel Labath07695bd2015-07-16 13:11:34 +0000427 m_flags (0),
428 m_distribution_id ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000429{
Greg Clayton64195a22011-02-23 00:35:02 +0000430 if (triple_cstr)
Greg Claytoneb0103f2011-04-07 22:46:35 +0000431 SetTriple(triple_cstr, platform);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000432}
433
Greg Clayton70512312012-05-08 01:45:38 +0000434
435ArchSpec::ArchSpec (const char *triple_cstr) :
436 m_triple (),
437 m_core (kCore_invalid),
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000438 m_byte_order (eByteOrderInvalid),
Pavel Labath07695bd2015-07-16 13:11:34 +0000439 m_flags (0),
440 m_distribution_id ()
Greg Clayton70512312012-05-08 01:45:38 +0000441{
442 if (triple_cstr)
443 SetTriple(triple_cstr);
444}
445
Greg Clayton64195a22011-02-23 00:35:02 +0000446ArchSpec::ArchSpec(const llvm::Triple &triple) :
Greg Clayton514487e2011-02-15 21:59:32 +0000447 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000448 m_core (kCore_invalid),
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000449 m_byte_order (eByteOrderInvalid),
Pavel Labath07695bd2015-07-16 13:11:34 +0000450 m_flags (0),
451 m_distribution_id ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000452{
Greg Clayton64195a22011-02-23 00:35:02 +0000453 SetTriple(triple);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000454}
455
Greg Claytone0d378b2011-03-24 21:19:54 +0000456ArchSpec::ArchSpec (ArchitectureType arch_type, uint32_t cpu, uint32_t subtype) :
Greg Clayton64195a22011-02-23 00:35:02 +0000457 m_triple (),
458 m_core (kCore_invalid),
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000459 m_byte_order (eByteOrderInvalid),
Pavel Labath07695bd2015-07-16 13:11:34 +0000460 m_flags (0),
461 m_distribution_id ()
Greg Clayton64195a22011-02-23 00:35:02 +0000462{
463 SetArchitecture (arch_type, cpu, subtype);
464}
465
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000466ArchSpec::~ArchSpec() = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000467
Greg Clayton64195a22011-02-23 00:35:02 +0000468//===----------------------------------------------------------------------===//
469// Assignment and initialization.
470
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000471const ArchSpec&
472ArchSpec::operator= (const ArchSpec& rhs)
473{
474 if (this != &rhs)
475 {
Greg Clayton514487e2011-02-15 21:59:32 +0000476 m_triple = rhs.m_triple;
Greg Clayton64195a22011-02-23 00:35:02 +0000477 m_core = rhs.m_core;
Greg Clayton514487e2011-02-15 21:59:32 +0000478 m_byte_order = rhs.m_byte_order;
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000479 m_distribution_id = rhs.m_distribution_id;
Jaydeep Patil501a7812015-07-16 03:51:55 +0000480 m_flags = rhs.m_flags;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000481 }
482 return *this;
483}
484
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000485void
486ArchSpec::Clear()
487{
Greg Clayton514487e2011-02-15 21:59:32 +0000488 m_triple = llvm::Triple();
Greg Clayton64195a22011-02-23 00:35:02 +0000489 m_core = kCore_invalid;
490 m_byte_order = eByteOrderInvalid;
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000491 m_distribution_id.Clear ();
Jaydeep Patil501a7812015-07-16 03:51:55 +0000492 m_flags = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000493}
494
Greg Clayton64195a22011-02-23 00:35:02 +0000495//===----------------------------------------------------------------------===//
496// Predicates.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000497
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000498const char *
Greg Clayton64195a22011-02-23 00:35:02 +0000499ArchSpec::GetArchitectureName () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000500{
Greg Clayton64195a22011-02-23 00:35:02 +0000501 const CoreDefinition *core_def = FindCoreDefinition (m_core);
502 if (core_def)
503 return core_def->name;
504 return "unknown";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000505}
506
Bhushan D. Attarde3592a6e2016-02-18 11:53:28 +0000507std::string
508ArchSpec::GetClangTargetCPU ()
509{
510 std::string cpu;
511 const llvm::Triple::ArchType machine = GetMachine();
512
513 if (machine == llvm::Triple::mips ||
514 machine == llvm::Triple::mipsel ||
515 machine == llvm::Triple::mips64 ||
516 machine == llvm::Triple::mips64el)
517 {
518 switch (m_core)
519 {
520 case ArchSpec::eCore_mips32:
521 case ArchSpec::eCore_mips32el:
522 cpu = "mips32"; break;
523 case ArchSpec::eCore_mips32r2:
524 case ArchSpec::eCore_mips32r2el:
525 cpu = "mips32r2"; break;
526 case ArchSpec::eCore_mips32r3:
527 case ArchSpec::eCore_mips32r3el:
528 cpu = "mips32r3"; break;
529 case ArchSpec::eCore_mips32r5:
530 case ArchSpec::eCore_mips32r5el:
531 cpu = "mips32r5"; break;
532 case ArchSpec::eCore_mips32r6:
533 case ArchSpec::eCore_mips32r6el:
534 cpu = "mips32r6"; break;
535 case ArchSpec::eCore_mips64:
536 case ArchSpec::eCore_mips64el:
537 cpu = "mips64"; break;
538 case ArchSpec::eCore_mips64r2:
539 case ArchSpec::eCore_mips64r2el:
540 cpu = "mips64r2"; break;
541 case ArchSpec::eCore_mips64r3:
542 case ArchSpec::eCore_mips64r3el:
543 cpu = "mips64r3"; break;
544 case ArchSpec::eCore_mips64r5:
545 case ArchSpec::eCore_mips64r5el:
546 cpu = "mips64r5"; break;
547 case ArchSpec::eCore_mips64r6:
548 case ArchSpec::eCore_mips64r6el:
549 cpu = "mips64r6"; break;
550 default:
551 break;
552 }
553 }
554 return cpu;
555}
556
Greg Clayton64195a22011-02-23 00:35:02 +0000557uint32_t
558ArchSpec::GetMachOCPUType () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000559{
Greg Clayton64195a22011-02-23 00:35:02 +0000560 const CoreDefinition *core_def = FindCoreDefinition (m_core);
561 if (core_def)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000562 {
Greg Clayton64195a22011-02-23 00:35:02 +0000563 const ArchDefinitionEntry *arch_def = FindArchDefinitionEntry (&g_macho_arch_def, core_def->core);
564 if (arch_def)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000565 {
Greg Clayton64195a22011-02-23 00:35:02 +0000566 return arch_def->cpu;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000567 }
568 }
Greg Clayton64195a22011-02-23 00:35:02 +0000569 return LLDB_INVALID_CPUTYPE;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000570}
571
Greg Clayton64195a22011-02-23 00:35:02 +0000572uint32_t
573ArchSpec::GetMachOCPUSubType () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000574{
Greg Clayton64195a22011-02-23 00:35:02 +0000575 const CoreDefinition *core_def = FindCoreDefinition (m_core);
576 if (core_def)
577 {
578 const ArchDefinitionEntry *arch_def = FindArchDefinitionEntry (&g_macho_arch_def, core_def->core);
579 if (arch_def)
580 {
Greg Clayton1cb64962011-03-24 04:28:38 +0000581 return arch_def->sub;
Greg Clayton64195a22011-02-23 00:35:02 +0000582 }
583 }
584 return LLDB_INVALID_CPUTYPE;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000585}
586
Matthew Gardinere77b2942014-09-01 09:06:03 +0000587uint32_t
588ArchSpec::GetDataByteSize () const
589{
590 switch (m_core)
591 {
592 case eCore_kalimba3:
Matthew Gardinerf03e6d842014-09-29 08:02:24 +0000593 return 4;
Matthew Gardinere77b2942014-09-01 09:06:03 +0000594 case eCore_kalimba4:
595 return 1;
596 case eCore_kalimba5:
Matthew Gardinerf03e6d842014-09-29 08:02:24 +0000597 return 4;
Matthew Gardinere77b2942014-09-01 09:06:03 +0000598 default:
599 return 1;
600 }
601 return 1;
602}
603
604uint32_t
605ArchSpec::GetCodeByteSize () const
606{
607 switch (m_core)
608 {
609 case eCore_kalimba3:
610 return 4;
611 case eCore_kalimba4:
612 return 1;
613 case eCore_kalimba5:
614 return 1;
615 default:
616 return 1;
617 }
618 return 1;
619}
620
Greg Clayton64195a22011-02-23 00:35:02 +0000621llvm::Triple::ArchType
622ArchSpec::GetMachine () const
623{
624 const CoreDefinition *core_def = FindCoreDefinition (m_core);
625 if (core_def)
626 return core_def->machine;
627
628 return llvm::Triple::UnknownArch;
629}
630
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000631const ConstString&
632ArchSpec::GetDistributionId () const
633{
634 return m_distribution_id;
635}
636
637void
638ArchSpec::SetDistributionId (const char* distribution_id)
639{
640 m_distribution_id.SetCString (distribution_id);
641}
642
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000643uint32_t
644ArchSpec::GetAddressByteSize() const
645{
Greg Clayton64195a22011-02-23 00:35:02 +0000646 const CoreDefinition *core_def = FindCoreDefinition (m_core);
647 if (core_def)
Mohit K. Bhakkad9514a382015-09-09 10:32:20 +0000648 {
649 if (core_def->machine == llvm::Triple::mips64 || core_def->machine == llvm::Triple::mips64el)
650 {
651 // For N32/O32 applications Address size is 4 bytes.
652 if (m_flags & (eMIPSABI_N32 | eMIPSABI_O32))
653 return 4;
654 }
655 return core_def->addr_byte_size;
656 }
Greg Clayton41f92322010-06-11 03:25:34 +0000657 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000658}
659
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000660ByteOrder
661ArchSpec::GetDefaultEndian () const
662{
Greg Clayton64195a22011-02-23 00:35:02 +0000663 const CoreDefinition *core_def = FindCoreDefinition (m_core);
664 if (core_def)
665 return core_def->default_byte_order;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000666 return eByteOrderInvalid;
667}
668
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +0000669bool
670ArchSpec::CharIsSignedByDefault () const
671{
672 switch (m_triple.getArch()) {
673 default:
674 return true;
675
676 case llvm::Triple::aarch64:
677 case llvm::Triple::aarch64_be:
678 case llvm::Triple::arm:
679 case llvm::Triple::armeb:
680 case llvm::Triple::thumb:
681 case llvm::Triple::thumbeb:
682 return m_triple.isOSDarwin() || m_triple.isOSWindows();
683
684 case llvm::Triple::ppc:
685 case llvm::Triple::ppc64:
686 return m_triple.isOSDarwin();
687
688 case llvm::Triple::ppc64le:
689 case llvm::Triple::systemz:
690 case llvm::Triple::xcore:
691 return false;
692 }
693}
694
Greg Clayton64195a22011-02-23 00:35:02 +0000695lldb::ByteOrder
696ArchSpec::GetByteOrder () const
697{
698 if (m_byte_order == eByteOrderInvalid)
699 return GetDefaultEndian();
700 return m_byte_order;
701}
702
703//===----------------------------------------------------------------------===//
704// Mutators.
705
706bool
707ArchSpec::SetTriple (const llvm::Triple &triple)
708{
709 m_triple = triple;
710
711 llvm::StringRef arch_name (m_triple.getArchName());
712 const CoreDefinition *core_def = FindCoreDefinition (arch_name);
713 if (core_def)
714 {
715 m_core = core_def->core;
Greg Claytoneb0103f2011-04-07 22:46:35 +0000716 // Set the byte order to the default byte order for an architecture.
717 // This can be modified if needed for cases when cores handle both
718 // big and little endian
719 m_byte_order = core_def->default_byte_order;
Greg Clayton64195a22011-02-23 00:35:02 +0000720 }
721 else
722 {
723 Clear();
724 }
725
Greg Clayton64195a22011-02-23 00:35:02 +0000726 return IsValid();
727}
728
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000729static bool
730ParseMachCPUDashSubtypeTriple (const char *triple_cstr, ArchSpec &arch)
731{
732 // Accept "12-10" or "12.10" as cpu type/subtype
733 if (isdigit(triple_cstr[0]))
734 {
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000735 char *end = nullptr;
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000736 errno = 0;
Greg Claytonc7bece562013-01-25 18:06:21 +0000737 uint32_t cpu = (uint32_t)::strtoul (triple_cstr, &end, 0);
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000738 if (errno == 0 && cpu != 0 && end && ((*end == '-') || (*end == '.')))
739 {
740 errno = 0;
Greg Claytonc7bece562013-01-25 18:06:21 +0000741 uint32_t sub = (uint32_t)::strtoul (end + 1, &end, 0);
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000742 if (errno == 0 && end && ((*end == '-') || (*end == '.') || (*end == '\0')))
743 {
744 if (arch.SetArchitecture (eArchTypeMachO, cpu, sub))
745 {
746 if (*end == '-')
747 {
748 llvm::StringRef vendor_os (end + 1);
749 size_t dash_pos = vendor_os.find('-');
750 if (dash_pos != llvm::StringRef::npos)
751 {
752 llvm::StringRef vendor_str(vendor_os.substr(0, dash_pos));
753 arch.GetTriple().setVendorName(vendor_str);
754 const size_t vendor_start_pos = dash_pos+1;
Greg Claytonc7bece562013-01-25 18:06:21 +0000755 dash_pos = vendor_os.find('-', vendor_start_pos);
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000756 if (dash_pos == llvm::StringRef::npos)
757 {
758 if (vendor_start_pos < vendor_os.size())
759 arch.GetTriple().setOSName(vendor_os.substr(vendor_start_pos));
760 }
761 else
762 {
763 arch.GetTriple().setOSName(vendor_os.substr(vendor_start_pos, dash_pos - vendor_start_pos));
764 }
765 }
766 }
767 return true;
768 }
769 }
770 }
771 }
772 return false;
773}
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000774
Greg Clayton64195a22011-02-23 00:35:02 +0000775bool
Greg Clayton70512312012-05-08 01:45:38 +0000776ArchSpec::SetTriple (const char *triple_cstr)
Greg Clayton64195a22011-02-23 00:35:02 +0000777{
Greg Clayton23aca092011-08-12 23:32:52 +0000778 if (triple_cstr && triple_cstr[0])
Greg Clayton64195a22011-02-23 00:35:02 +0000779 {
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000780 if (ParseMachCPUDashSubtypeTriple (triple_cstr, *this))
781 return true;
782
Greg Clayton64195a22011-02-23 00:35:02 +0000783 llvm::StringRef triple_stref (triple_cstr);
784 if (triple_stref.startswith (LLDB_ARCH_DEFAULT))
785 {
786 // Special case for the current host default architectures...
787 if (triple_stref.equals (LLDB_ARCH_DEFAULT_32BIT))
Zachary Turner13b18262014-08-20 16:42:51 +0000788 *this = HostInfo::GetArchitecture(HostInfo::eArchKind32);
Greg Clayton64195a22011-02-23 00:35:02 +0000789 else if (triple_stref.equals (LLDB_ARCH_DEFAULT_64BIT))
Zachary Turner13b18262014-08-20 16:42:51 +0000790 *this = HostInfo::GetArchitecture(HostInfo::eArchKind64);
Greg Clayton64195a22011-02-23 00:35:02 +0000791 else if (triple_stref.equals (LLDB_ARCH_DEFAULT))
Zachary Turner13b18262014-08-20 16:42:51 +0000792 *this = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
Greg Clayton64195a22011-02-23 00:35:02 +0000793 }
794 else
795 {
796 std::string normalized_triple_sstr (llvm::Triple::normalize(triple_stref));
797 triple_stref = normalized_triple_sstr;
Greg Clayton70512312012-05-08 01:45:38 +0000798 SetTriple (llvm::Triple (triple_stref));
799 }
800 }
801 else
802 Clear();
803 return IsValid();
804}
805
806bool
807ArchSpec::SetTriple (const char *triple_cstr, Platform *platform)
808{
809 if (triple_cstr && triple_cstr[0])
810 {
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000811 if (ParseMachCPUDashSubtypeTriple (triple_cstr, *this))
812 return true;
813
Greg Clayton70512312012-05-08 01:45:38 +0000814 llvm::StringRef triple_stref (triple_cstr);
815 if (triple_stref.startswith (LLDB_ARCH_DEFAULT))
816 {
817 // Special case for the current host default architectures...
818 if (triple_stref.equals (LLDB_ARCH_DEFAULT_32BIT))
Zachary Turner13b18262014-08-20 16:42:51 +0000819 *this = HostInfo::GetArchitecture(HostInfo::eArchKind32);
Greg Clayton70512312012-05-08 01:45:38 +0000820 else if (triple_stref.equals (LLDB_ARCH_DEFAULT_64BIT))
Zachary Turner13b18262014-08-20 16:42:51 +0000821 *this = HostInfo::GetArchitecture(HostInfo::eArchKind64);
Greg Clayton70512312012-05-08 01:45:38 +0000822 else if (triple_stref.equals (LLDB_ARCH_DEFAULT))
Zachary Turner13b18262014-08-20 16:42:51 +0000823 *this = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
Greg Clayton70512312012-05-08 01:45:38 +0000824 }
825 else
826 {
827 ArchSpec raw_arch (triple_cstr);
828
829 std::string normalized_triple_sstr (llvm::Triple::normalize(triple_stref));
830 triple_stref = normalized_triple_sstr;
Greg Claytoneb0103f2011-04-07 22:46:35 +0000831 llvm::Triple normalized_triple (triple_stref);
832
833 const bool os_specified = normalized_triple.getOSName().size() > 0;
834 const bool vendor_specified = normalized_triple.getVendorName().size() > 0;
835 const bool env_specified = normalized_triple.getEnvironmentName().size() > 0;
836
837 // If we got an arch only, then default the vendor, os, environment
838 // to match the platform if one is supplied
839 if (!(os_specified || vendor_specified || env_specified))
840 {
841 if (platform)
842 {
843 // If we were given a platform, use the platform's system
844 // architecture. If this is not available (might not be
845 // connected) use the first supported architecture.
Greg Clayton70512312012-05-08 01:45:38 +0000846 ArchSpec compatible_arch;
Greg Clayton1e0c8842013-01-11 20:49:54 +0000847 if (platform->IsCompatibleArchitecture (raw_arch, false, &compatible_arch))
Greg Claytoneb0103f2011-04-07 22:46:35 +0000848 {
Greg Clayton70512312012-05-08 01:45:38 +0000849 if (compatible_arch.IsValid())
850 {
851 const llvm::Triple &compatible_triple = compatible_arch.GetTriple();
852 if (!vendor_specified)
853 normalized_triple.setVendor(compatible_triple.getVendor());
854 if (!os_specified)
855 normalized_triple.setOS(compatible_triple.getOS());
856 if (!env_specified && compatible_triple.getEnvironmentName().size())
857 normalized_triple.setEnvironment(compatible_triple.getEnvironment());
858 }
Greg Claytoneb0103f2011-04-07 22:46:35 +0000859 }
Greg Clayton70512312012-05-08 01:45:38 +0000860 else
Greg Claytoneb0103f2011-04-07 22:46:35 +0000861 {
Greg Clayton70512312012-05-08 01:45:38 +0000862 *this = raw_arch;
863 return IsValid();
Greg Claytoneb0103f2011-04-07 22:46:35 +0000864 }
865 }
866 else
867 {
868 // No platform specified, fall back to the host system for
869 // the default vendor, os, and environment.
Sean Callananbfb237bc2011-11-04 22:46:46 +0000870 llvm::Triple host_triple(llvm::sys::getDefaultTargetTriple());
Greg Clayton70512312012-05-08 01:45:38 +0000871 if (!vendor_specified)
872 normalized_triple.setVendor(host_triple.getVendor());
873 if (!vendor_specified)
874 normalized_triple.setOS(host_triple.getOS());
875 if (!env_specified && host_triple.getEnvironmentName().size())
876 normalized_triple.setEnvironment(host_triple.getEnvironment());
Greg Claytoneb0103f2011-04-07 22:46:35 +0000877 }
878 }
879 SetTriple (normalized_triple);
Greg Clayton64195a22011-02-23 00:35:02 +0000880 }
881 }
882 else
883 Clear();
884 return IsValid();
885}
886
Zachary Turner5e6f4522015-01-22 18:59:05 +0000887void
888ArchSpec::MergeFrom(const ArchSpec &other)
889{
Todd Fiala7df337f2015-10-13 23:41:19 +0000890 if (TripleVendorIsUnspecifiedUnknown() && !other.TripleVendorIsUnspecifiedUnknown())
Zachary Turner5e6f4522015-01-22 18:59:05 +0000891 GetTriple().setVendor(other.GetTriple().getVendor());
Todd Fiala7df337f2015-10-13 23:41:19 +0000892 if (TripleOSIsUnspecifiedUnknown() && !other.TripleOSIsUnspecifiedUnknown())
Zachary Turner5e6f4522015-01-22 18:59:05 +0000893 GetTriple().setOS(other.GetTriple().getOS());
894 if (GetTriple().getArch() == llvm::Triple::UnknownArch)
895 GetTriple().setArch(other.GetTriple().getArch());
Jason Molenda03fe45e2015-11-06 01:43:36 +0000896 if (GetTriple().getEnvironment() == llvm::Triple::UnknownEnvironment && !TripleVendorWasSpecified())
897 {
898 if (other.TripleVendorWasSpecified())
899 GetTriple().setEnvironment(other.GetTriple().getEnvironment());
900 }
Jason Molenda583b1a82016-04-05 05:01:30 +0000901 // If this and other are both arm ArchSpecs and this ArchSpec is a generic "some kind of arm"
902 // spec but the other ArchSpec is a specific arm core, adopt the specific arm core.
903 if (GetTriple().getArch() == llvm::Triple::arm
904 && other.GetTriple().getArch() == llvm::Triple::arm
905 && IsCompatibleMatch (other)
906 && GetCore() == ArchSpec::eCore_arm_generic
907 && other.GetCore() != ArchSpec::eCore_arm_generic)
908 {
909 m_core = other.GetCore();
910 CoreUpdated (true);
911 }
Zachary Turner5e6f4522015-01-22 18:59:05 +0000912}
913
Greg Clayton64195a22011-02-23 00:35:02 +0000914bool
Ed Mastef6a13122015-06-05 13:03:08 +0000915ArchSpec::SetArchitecture (ArchitectureType arch_type, uint32_t cpu, uint32_t sub, uint32_t os)
Greg Clayton64195a22011-02-23 00:35:02 +0000916{
917 m_core = kCore_invalid;
918 bool update_triple = true;
919 const ArchDefinition *arch_def = FindArchDefinition(arch_type);
920 if (arch_def)
921 {
922 const ArchDefinitionEntry *arch_def_entry = FindArchDefinitionEntry (arch_def, cpu, sub);
923 if (arch_def_entry)
924 {
925 const CoreDefinition *core_def = FindCoreDefinition (arch_def_entry->core);
926 if (core_def)
927 {
928 m_core = core_def->core;
929 update_triple = false;
Greg Clayton593577a2011-09-21 03:57:31 +0000930 // Always use the architecture name because it might be more descriptive
931 // than the architecture enum ("armv7" -> llvm::Triple::arm).
932 m_triple.setArchName(llvm::StringRef(core_def->name));
Greg Clayton64195a22011-02-23 00:35:02 +0000933 if (arch_type == eArchTypeMachO)
934 {
935 m_triple.setVendor (llvm::Triple::Apple);
Greg Claytona3a6c122014-07-29 18:04:57 +0000936
Jason Molenda03fe45e2015-11-06 01:43:36 +0000937 // Don't set the OS. It could be simulator, macosx, ios, watchos, tvos. We could
938 // get close with the cpu type - but we can't get it right all of the time. Better
939 // to leave this unset so other sections of code will set it when they have more
940 // information.
941 // NB: don't call m_triple.setOS (llvm::Triple::UnknownOS). That sets the OSName to
942 // "unknown" and the ArchSpec::TripleVendorWasSpecified() method says that any
943 // OSName setting means it was specified.
Greg Clayton64195a22011-02-23 00:35:02 +0000944 }
Ed Mastef6a13122015-06-05 13:03:08 +0000945 else if (arch_type == eArchTypeELF)
946 {
Ed Mastef6a13122015-06-05 13:03:08 +0000947 switch (os)
948 {
Tamas Berghammered1fa202015-07-07 09:11:59 +0000949 case llvm::ELF::ELFOSABI_AIX: m_triple.setOS (llvm::Triple::OSType::AIX); break;
950 case llvm::ELF::ELFOSABI_FREEBSD: m_triple.setOS (llvm::Triple::OSType::FreeBSD); break;
951 case llvm::ELF::ELFOSABI_GNU: m_triple.setOS (llvm::Triple::OSType::Linux); break;
952 case llvm::ELF::ELFOSABI_NETBSD: m_triple.setOS (llvm::Triple::OSType::NetBSD); break;
953 case llvm::ELF::ELFOSABI_OPENBSD: m_triple.setOS (llvm::Triple::OSType::OpenBSD); break;
954 case llvm::ELF::ELFOSABI_SOLARIS: m_triple.setOS (llvm::Triple::OSType::Solaris); break;
Ed Mastef6a13122015-06-05 13:03:08 +0000955 }
Ed Mastef6a13122015-06-05 13:03:08 +0000956 }
Jason Molenda03fe45e2015-11-06 01:43:36 +0000957 else
958 {
959 m_triple.setVendor (llvm::Triple::UnknownVendor);
960 m_triple.setOS (llvm::Triple::UnknownOS);
961 }
Greg Clayton593577a2011-09-21 03:57:31 +0000962 // Fall back onto setting the machine type if the arch by name failed...
963 if (m_triple.getArch () == llvm::Triple::UnknownArch)
964 m_triple.setArch (core_def->machine);
Greg Clayton64195a22011-02-23 00:35:02 +0000965 }
966 }
967 }
968 CoreUpdated(update_triple);
969 return IsValid();
970}
971
Greg Clayton357132e2011-03-26 19:14:58 +0000972uint32_t
973ArchSpec::GetMinimumOpcodeByteSize() const
Greg Clayton64195a22011-02-23 00:35:02 +0000974{
Greg Clayton357132e2011-03-26 19:14:58 +0000975 const CoreDefinition *core_def = FindCoreDefinition (m_core);
976 if (core_def)
977 return core_def->min_opcode_byte_size;
978 return 0;
979}
980
981uint32_t
982ArchSpec::GetMaximumOpcodeByteSize() const
983{
984 const CoreDefinition *core_def = FindCoreDefinition (m_core);
985 if (core_def)
986 return core_def->max_opcode_byte_size;
987 return 0;
Greg Clayton64195a22011-02-23 00:35:02 +0000988}
989
Jason Molendaba813dc2012-11-04 03:20:05 +0000990bool
991ArchSpec::IsExactMatch (const ArchSpec& rhs) const
992{
Sean Callananbf4b7be2012-12-13 22:07:14 +0000993 return IsEqualTo (rhs, true);
Jason Molendaba813dc2012-11-04 03:20:05 +0000994}
995
996bool
997ArchSpec::IsCompatibleMatch (const ArchSpec& rhs) const
998{
Sean Callananbf4b7be2012-12-13 22:07:14 +0000999 return IsEqualTo (rhs, false);
Jason Molendaba813dc2012-11-04 03:20:05 +00001000}
1001
1002bool
Sean Callananbf4b7be2012-12-13 22:07:14 +00001003ArchSpec::IsEqualTo (const ArchSpec& rhs, bool exact_match) const
Jason Molendaba813dc2012-11-04 03:20:05 +00001004{
Todd Fialaa9ddb0e2014-01-18 03:02:39 +00001005 // explicitly ignoring m_distribution_id in this method.
1006
Jason Molendaba813dc2012-11-04 03:20:05 +00001007 if (GetByteOrder() != rhs.GetByteOrder())
1008 return false;
1009
1010 const ArchSpec::Core lhs_core = GetCore ();
1011 const ArchSpec::Core rhs_core = rhs.GetCore ();
1012
1013 const bool core_match = cores_match (lhs_core, rhs_core, true, exact_match);
1014
1015 if (core_match)
1016 {
1017 const llvm::Triple &lhs_triple = GetTriple();
1018 const llvm::Triple &rhs_triple = rhs.GetTriple();
1019
1020 const llvm::Triple::VendorType lhs_triple_vendor = lhs_triple.getVendor();
1021 const llvm::Triple::VendorType rhs_triple_vendor = rhs_triple.getVendor();
1022 if (lhs_triple_vendor != rhs_triple_vendor)
1023 {
Jason Molenda03fe45e2015-11-06 01:43:36 +00001024 const bool rhs_vendor_specified = rhs.TripleVendorWasSpecified();
1025 const bool lhs_vendor_specified = TripleVendorWasSpecified();
1026 // Both architectures had the vendor specified, so if they aren't
1027 // equal then we return false
1028 if (rhs_vendor_specified && lhs_vendor_specified)
1029 return false;
Jason Molendaba813dc2012-11-04 03:20:05 +00001030
1031 // Only fail if both vendor types are not unknown
1032 if (lhs_triple_vendor != llvm::Triple::UnknownVendor &&
1033 rhs_triple_vendor != llvm::Triple::UnknownVendor)
1034 return false;
1035 }
1036
1037 const llvm::Triple::OSType lhs_triple_os = lhs_triple.getOS();
1038 const llvm::Triple::OSType rhs_triple_os = rhs_triple.getOS();
1039 if (lhs_triple_os != rhs_triple_os)
1040 {
Jason Molenda03fe45e2015-11-06 01:43:36 +00001041 const bool rhs_os_specified = rhs.TripleOSWasSpecified();
1042 const bool lhs_os_specified = TripleOSWasSpecified();
1043 // Both architectures had the OS specified, so if they aren't
1044 // equal then we return false
1045 if (rhs_os_specified && lhs_os_specified)
1046 return false;
Greg Clayton7ab7f892014-05-29 21:33:45 +00001047
Greg Clayton3f19ada2014-07-10 23:33:37 +00001048 // Only fail if both os types are not unknown
1049 if (lhs_triple_os != llvm::Triple::UnknownOS &&
1050 rhs_triple_os != llvm::Triple::UnknownOS)
1051 return false;
Jason Molendaba813dc2012-11-04 03:20:05 +00001052 }
1053
1054 const llvm::Triple::EnvironmentType lhs_triple_env = lhs_triple.getEnvironment();
1055 const llvm::Triple::EnvironmentType rhs_triple_env = rhs_triple.getEnvironment();
1056
1057 if (lhs_triple_env != rhs_triple_env)
1058 {
1059 // Only fail if both environment types are not unknown
1060 if (lhs_triple_env != llvm::Triple::UnknownEnvironment &&
1061 rhs_triple_env != llvm::Triple::UnknownEnvironment)
1062 return false;
1063 }
1064 return true;
1065 }
1066 return false;
1067}
1068
Greg Clayton64195a22011-02-23 00:35:02 +00001069//===----------------------------------------------------------------------===//
1070// Helper methods.
1071
1072void
1073ArchSpec::CoreUpdated (bool update_triple)
1074{
1075 const CoreDefinition *core_def = FindCoreDefinition (m_core);
1076 if (core_def)
1077 {
1078 if (update_triple)
1079 m_triple = llvm::Triple(core_def->name, "unknown", "unknown");
1080 m_byte_order = core_def->default_byte_order;
1081 }
1082 else
1083 {
1084 if (update_triple)
1085 m_triple = llvm::Triple();
1086 m_byte_order = eByteOrderInvalid;
1087 }
1088}
1089
1090//===----------------------------------------------------------------------===//
1091// Operators.
1092
Greg Clayton70512312012-05-08 01:45:38 +00001093static bool
Jason Molendaba813dc2012-11-04 03:20:05 +00001094cores_match (const ArchSpec::Core core1, const ArchSpec::Core core2, bool try_inverse, bool enforce_exact_match)
Greg Clayton70512312012-05-08 01:45:38 +00001095{
Jason Molendaba813dc2012-11-04 03:20:05 +00001096 if (core1 == core2)
1097 return true;
1098
Greg Clayton70512312012-05-08 01:45:38 +00001099 switch (core1)
1100 {
Greg Clayton70512312012-05-08 01:45:38 +00001101 case ArchSpec::kCore_any:
1102 return true;
1103
Greg Clayton44362e02014-07-12 00:11:34 +00001104 case ArchSpec::eCore_arm_generic:
1105 if (enforce_exact_match)
1106 break;
Jason Molenda62e06812016-02-16 04:14:33 +00001107 LLVM_FALLTHROUGH;
Greg Clayton70512312012-05-08 01:45:38 +00001108 case ArchSpec::kCore_arm_any:
1109 if (core2 >= ArchSpec::kCore_arm_first && core2 <= ArchSpec::kCore_arm_last)
1110 return true;
1111 if (core2 >= ArchSpec::kCore_thumb_first && core2 <= ArchSpec::kCore_thumb_last)
1112 return true;
1113 if (core2 == ArchSpec::kCore_arm_any)
1114 return true;
1115 break;
Sylvestre Ledru1c9e0642014-08-18 14:53:42 +00001116
Greg Clayton70512312012-05-08 01:45:38 +00001117 case ArchSpec::kCore_x86_32_any:
1118 if ((core2 >= ArchSpec::kCore_x86_32_first && core2 <= ArchSpec::kCore_x86_32_last) || (core2 == ArchSpec::kCore_x86_32_any))
1119 return true;
1120 break;
Zachary Turnerad587ae42014-07-28 16:44:49 +00001121
1122 case ArchSpec::kCore_x86_64_any:
1123 if ((core2 >= ArchSpec::kCore_x86_64_first && core2 <= ArchSpec::kCore_x86_64_last) || (core2 == ArchSpec::kCore_x86_64_any))
1124 return true;
Sylvestre Ledru1c9e0642014-08-18 14:53:42 +00001125 break;
Zachary Turnerad587ae42014-07-28 16:44:49 +00001126
Greg Clayton70512312012-05-08 01:45:38 +00001127 case ArchSpec::kCore_ppc_any:
1128 if ((core2 >= ArchSpec::kCore_ppc_first && core2 <= ArchSpec::kCore_ppc_last) || (core2 == ArchSpec::kCore_ppc_any))
1129 return true;
1130 break;
Sylvestre Ledru1c9e0642014-08-18 14:53:42 +00001131
Greg Clayton70512312012-05-08 01:45:38 +00001132 case ArchSpec::kCore_ppc64_any:
1133 if ((core2 >= ArchSpec::kCore_ppc64_first && core2 <= ArchSpec::kCore_ppc64_last) || (core2 == ArchSpec::kCore_ppc64_any))
1134 return true;
1135 break;
1136
Jason Molendaa3a04522013-09-27 23:21:54 +00001137 case ArchSpec::eCore_arm_armv6m:
1138 if (!enforce_exact_match)
1139 {
Greg Clayton44362e02014-07-12 00:11:34 +00001140 if (core2 == ArchSpec::eCore_arm_generic)
1141 return true;
Jason Molendaa3a04522013-09-27 23:21:54 +00001142 try_inverse = false;
Jason Molendac7cda272013-09-27 23:29:10 +00001143 if (core2 == ArchSpec::eCore_arm_armv7)
Jason Molendaa3a04522013-09-27 23:21:54 +00001144 return true;
Jason Molendad607afd2015-06-25 22:37:57 +00001145 if (core2 == ArchSpec::eCore_arm_armv6m)
1146 return true;
Jason Molendaa3a04522013-09-27 23:21:54 +00001147 }
Sylvestre Ledru1c9e0642014-08-18 14:53:42 +00001148 break;
Deepak Panickal6d3df422014-02-19 11:16:46 +00001149
1150 case ArchSpec::kCore_hexagon_any:
1151 if ((core2 >= ArchSpec::kCore_hexagon_first && core2 <= ArchSpec::kCore_hexagon_last) || (core2 == ArchSpec::kCore_hexagon_any))
1152 return true;
Jason Molendaa3a04522013-09-27 23:21:54 +00001153 break;
1154
Jason Molenda8825c5c2015-10-08 21:48:35 +00001155 // v. https://en.wikipedia.org/wiki/ARM_Cortex-M#Silicon_customization
1156 // Cortex-M0 - ARMv6-M - armv6m
1157 // Cortex-M3 - ARMv7-M - armv7m
1158 // Cortex-M4 - ARMv7E-M - armv7em
Jason Molenda7a1559c2013-03-08 01:20:17 +00001159 case ArchSpec::eCore_arm_armv7em:
Jason Molendad607afd2015-06-25 22:37:57 +00001160 if (!enforce_exact_match)
1161 {
1162 if (core2 == ArchSpec::eCore_arm_generic)
1163 return true;
1164 if (core2 == ArchSpec::eCore_arm_armv7m)
1165 return true;
1166 if (core2 == ArchSpec::eCore_arm_armv6m)
1167 return true;
1168 if (core2 == ArchSpec::eCore_arm_armv7)
1169 return true;
1170 try_inverse = true;
1171 }
1172 break;
1173
Jason Molenda8825c5c2015-10-08 21:48:35 +00001174 // v. https://en.wikipedia.org/wiki/ARM_Cortex-M#Silicon_customization
1175 // Cortex-M0 - ARMv6-M - armv6m
1176 // Cortex-M3 - ARMv7-M - armv7m
1177 // Cortex-M4 - ARMv7E-M - armv7em
Jason Molendad607afd2015-06-25 22:37:57 +00001178 case ArchSpec::eCore_arm_armv7m:
1179 if (!enforce_exact_match)
1180 {
1181 if (core2 == ArchSpec::eCore_arm_generic)
1182 return true;
1183 if (core2 == ArchSpec::eCore_arm_armv6m)
1184 return true;
1185 if (core2 == ArchSpec::eCore_arm_armv7)
1186 return true;
1187 if (core2 == ArchSpec::eCore_arm_armv7em)
1188 return true;
1189 try_inverse = true;
1190 }
1191 break;
1192
Johnny Chen1083b0d2012-08-28 22:53:40 +00001193 case ArchSpec::eCore_arm_armv7f:
1194 case ArchSpec::eCore_arm_armv7k:
1195 case ArchSpec::eCore_arm_armv7s:
Jason Molendaba813dc2012-11-04 03:20:05 +00001196 if (!enforce_exact_match)
1197 {
Greg Clayton44362e02014-07-12 00:11:34 +00001198 if (core2 == ArchSpec::eCore_arm_generic)
1199 return true;
Jason Molendaba813dc2012-11-04 03:20:05 +00001200 if (core2 == ArchSpec::eCore_arm_armv7)
1201 return true;
Greg Clayton44362e02014-07-12 00:11:34 +00001202 try_inverse = false;
Jason Molendaba813dc2012-11-04 03:20:05 +00001203 }
Johnny Chen1083b0d2012-08-28 22:53:40 +00001204 break;
Sylvestre Ledru1c9e0642014-08-18 14:53:42 +00001205
Greg Clayton52edb362014-07-14 22:53:02 +00001206 case ArchSpec::eCore_x86_64_x86_64h:
1207 if (!enforce_exact_match)
1208 {
1209 try_inverse = false;
1210 if (core2 == ArchSpec::eCore_x86_64_x86_64)
1211 return true;
1212 }
1213 break;
Johnny Chen1083b0d2012-08-28 22:53:40 +00001214
Todd Fiala02e71812014-08-28 14:32:43 +00001215 case ArchSpec::eCore_arm_armv8:
1216 if (!enforce_exact_match)
1217 {
1218 if (core2 == ArchSpec::eCore_arm_arm64)
1219 return true;
1220 if (core2 == ArchSpec::eCore_arm_aarch64)
1221 return true;
1222 try_inverse = false;
1223 }
1224 break;
1225
1226 case ArchSpec::eCore_arm_aarch64:
1227 if (!enforce_exact_match)
1228 {
1229 if (core2 == ArchSpec::eCore_arm_arm64)
1230 return true;
1231 if (core2 == ArchSpec::eCore_arm_armv8)
1232 return true;
1233 try_inverse = false;
1234 }
1235 break;
1236
1237 case ArchSpec::eCore_arm_arm64:
1238 if (!enforce_exact_match)
1239 {
1240 if (core2 == ArchSpec::eCore_arm_aarch64)
1241 return true;
1242 if (core2 == ArchSpec::eCore_arm_armv8)
1243 return true;
1244 try_inverse = false;
1245 }
1246 break;
1247
Sagar Thakur6bee9612015-07-13 09:52:06 +00001248 case ArchSpec::eCore_mips32:
1249 if (!enforce_exact_match)
1250 {
1251 if (core2 >= ArchSpec::kCore_mips32_first && core2 <= ArchSpec::kCore_mips32_last)
1252 return true;
1253 try_inverse = false;
1254 }
1255 break;
1256
1257 case ArchSpec::eCore_mips32el:
1258 if (!enforce_exact_match)
1259 {
1260 if (core2 >= ArchSpec::kCore_mips32el_first && core2 <= ArchSpec::kCore_mips32el_last)
1261 return true;
1262 try_inverse = false;
1263 }
Greg Claytoncec91ef2016-02-26 01:20:20 +00001264 break;
Sagar Thakur6bee9612015-07-13 09:52:06 +00001265
Sagar Thakurce815e42015-06-03 10:14:24 +00001266 case ArchSpec::eCore_mips64:
Sagar Thakur6bee9612015-07-13 09:52:06 +00001267 if (!enforce_exact_match)
1268 {
1269 if (core2 >= ArchSpec::kCore_mips32_first && core2 <= ArchSpec::kCore_mips32_last)
1270 return true;
1271 if (core2 >= ArchSpec::kCore_mips64_first && core2 <= ArchSpec::kCore_mips64_last)
1272 return true;
1273 try_inverse = false;
1274 }
Greg Claytoncec91ef2016-02-26 01:20:20 +00001275 break;
Sagar Thakur6bee9612015-07-13 09:52:06 +00001276
1277 case ArchSpec::eCore_mips64el:
1278 if (!enforce_exact_match)
1279 {
1280 if (core2 >= ArchSpec::kCore_mips32el_first && core2 <= ArchSpec::kCore_mips32el_last)
1281 return true;
1282 if (core2 >= ArchSpec::kCore_mips64el_first && core2 <= ArchSpec::kCore_mips64el_last)
1283 return true;
1284 try_inverse = false;
1285 }
Greg Claytoncec91ef2016-02-26 01:20:20 +00001286 break;
Sagar Thakur6bee9612015-07-13 09:52:06 +00001287
Sagar Thakurce815e42015-06-03 10:14:24 +00001288 case ArchSpec::eCore_mips64r2:
1289 case ArchSpec::eCore_mips64r3:
1290 case ArchSpec::eCore_mips64r5:
Sagar Thakurce815e42015-06-03 10:14:24 +00001291 if (!enforce_exact_match)
1292 {
1293 if (core2 >= ArchSpec::kCore_mips32_first && core2 <= (core1 - 10))
1294 return true;
1295 if (core2 >= ArchSpec::kCore_mips64_first && core2 <= (core1 - 1))
1296 return true;
1297 try_inverse = false;
1298 }
1299 break;
1300
Sagar Thakurce815e42015-06-03 10:14:24 +00001301 case ArchSpec::eCore_mips64r2el:
1302 case ArchSpec::eCore_mips64r3el:
1303 case ArchSpec::eCore_mips64r5el:
Sagar Thakurce815e42015-06-03 10:14:24 +00001304 if (!enforce_exact_match)
1305 {
1306 if (core2 >= ArchSpec::kCore_mips32el_first && core2 <= (core1 - 10))
1307 return true;
1308 if (core2 >= ArchSpec::kCore_mips64el_first && core2 <= (core1 - 1))
1309 return true;
1310 try_inverse = false;
1311 }
1312 break;
1313
Sagar Thakur6bee9612015-07-13 09:52:06 +00001314 case ArchSpec::eCore_mips32r2:
1315 case ArchSpec::eCore_mips32r3:
1316 case ArchSpec::eCore_mips32r5:
1317 if (!enforce_exact_match)
1318 {
1319 if (core2 >= ArchSpec::kCore_mips32_first && core2 <= core1)
1320 return true;
1321 }
1322 break;
1323
1324 case ArchSpec::eCore_mips32r2el:
1325 case ArchSpec::eCore_mips32r3el:
1326 case ArchSpec::eCore_mips32r5el:
1327 if (!enforce_exact_match)
1328 {
1329 if (core2 >= ArchSpec::kCore_mips32el_first && core2 <= core1)
1330 return true;
1331 }
1332 break;
1333
1334 case ArchSpec::eCore_mips32r6:
1335 if (!enforce_exact_match)
1336 {
1337 if (core2 == ArchSpec::eCore_mips32 || core2 == ArchSpec::eCore_mips32r6)
1338 return true;
1339 }
1340 break;
1341
1342 case ArchSpec::eCore_mips32r6el:
1343 if (!enforce_exact_match)
1344 {
1345 if (core2 == ArchSpec::eCore_mips32el || core2 == ArchSpec::eCore_mips32r6el)
1346 return true;
1347 return true;
1348 }
1349 break;
1350
1351 case ArchSpec::eCore_mips64r6:
1352 if (!enforce_exact_match)
1353 {
1354 if (core2 == ArchSpec::eCore_mips32 || core2 == ArchSpec::eCore_mips32r6)
1355 return true;
1356 if (core2 == ArchSpec::eCore_mips64 || core2 == ArchSpec::eCore_mips64r6)
1357 return true;
1358 }
1359 break;
1360
1361 case ArchSpec::eCore_mips64r6el:
1362 if (!enforce_exact_match)
1363 {
1364 if (core2 == ArchSpec::eCore_mips32el || core2 == ArchSpec::eCore_mips32r6el)
1365 return true;
1366 if (core2 == ArchSpec::eCore_mips64el || core2 == ArchSpec::eCore_mips64r6el)
1367 return true;
1368 }
1369 break;
1370
Greg Clayton70512312012-05-08 01:45:38 +00001371 default:
1372 break;
1373 }
1374 if (try_inverse)
Jason Molendaba813dc2012-11-04 03:20:05 +00001375 return cores_match (core2, core1, false, enforce_exact_match);
Greg Clayton70512312012-05-08 01:45:38 +00001376 return false;
1377}
1378
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001379bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001380lldb_private::operator<(const ArchSpec& lhs, const ArchSpec& rhs)
1381{
Greg Clayton64195a22011-02-23 00:35:02 +00001382 const ArchSpec::Core lhs_core = lhs.GetCore ();
1383 const ArchSpec::Core rhs_core = rhs.GetCore ();
1384 return lhs_core < rhs_core;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001385}
Greg Claytona97c4d22014-12-09 23:31:02 +00001386
1387static void
1388StopInfoOverrideCallbackTypeARM(lldb_private::Thread &thread)
1389{
1390 // We need to check if we are stopped in Thumb mode in a IT instruction
1391 // and detect if the condition doesn't pass. If this is the case it means
1392 // we won't actually execute this instruction. If this happens we need to
1393 // clear the stop reason to no thread plans think we are stopped for a
1394 // reason and the plans should keep going.
1395 //
1396 // We do this because when single stepping many ARM processes, debuggers
1397 // often use the BVR/BCR registers that says "stop when the PC is not
1398 // equal to its current value". This method of stepping means we can end
1399 // up stopping on instructions inside an if/then block that wouldn't get
1400 // executed. By fixing this we can stop the debugger from seeming like
1401 // you stepped through both the "if" _and_ the "else" clause when source
1402 // level stepping because the debugger stops regardless due to the BVR/BCR
1403 // triggering a stop.
1404 //
1405 // It also means we can set breakpoints on instructions inside an an
1406 // if/then block and correctly skip them if we use the BKPT instruction.
1407 // The ARM and Thumb BKPT instructions are unconditional even when executed
1408 // in a Thumb IT block.
1409 //
1410 // If your debugger inserts software traps in ARM/Thumb code, it will
1411 // need to use 16 and 32 bit instruction for 16 and 32 bit thumb
1412 // instructions respectively. If your debugger inserts a 16 bit thumb
1413 // trap on top of a 32 bit thumb instruction for an opcode that is inside
1414 // an if/then, it will change the it/then to conditionally execute your
1415 // 16 bit trap and then cause your program to crash if it executes the
1416 // trailing 16 bits (the second half of the 32 bit thumb instruction you
1417 // partially overwrote).
1418
1419 RegisterContextSP reg_ctx_sp (thread.GetRegisterContext());
1420 if (reg_ctx_sp)
1421 {
1422 const uint32_t cpsr = reg_ctx_sp->GetFlags(0);
1423 if (cpsr != 0)
1424 {
1425 // Read the J and T bits to get the ISETSTATE
1426 const uint32_t J = Bit32(cpsr, 24);
1427 const uint32_t T = Bit32(cpsr, 5);
1428 const uint32_t ISETSTATE = J << 1 | T;
1429 if (ISETSTATE == 0)
1430 {
1431 // NOTE: I am pretty sure we want to enable the code below
1432 // that detects when we stop on an instruction in ARM mode
1433 // that is conditional and the condition doesn't pass. This
1434 // can happen if you set a breakpoint on an instruction that
1435 // is conditional. We currently will _always_ stop on the
1436 // instruction which is bad. You can also run into this while
1437 // single stepping and you could appear to run code in the "if"
1438 // and in the "else" clause because it would stop at all of the
1439 // conditional instructions in both.
1440 // In such cases, we really don't want to stop at this location.
1441 // I will check with the lldb-dev list first before I enable this.
1442#if 0
1443 // ARM mode: check for condition on intsruction
1444 const addr_t pc = reg_ctx_sp->GetPC();
1445 Error error;
1446 // If we fail to read the opcode we will get UINT64_MAX as the
1447 // result in "opcode" which we can use to detect if we read a
1448 // valid opcode.
1449 const uint64_t opcode = thread.GetProcess()->ReadUnsignedIntegerFromMemory(pc, 4, UINT64_MAX, error);
1450 if (opcode <= UINT32_MAX)
1451 {
1452 const uint32_t condition = Bits32((uint32_t)opcode, 31, 28);
Eugene Zelenko896ddd02016-03-02 01:09:03 +00001453 if (!ARMConditionPassed(condition, cpsr))
Greg Claytona97c4d22014-12-09 23:31:02 +00001454 {
1455 // We ARE stopped on an ARM instruction whose condition doesn't
1456 // pass so this instruction won't get executed.
1457 // Regardless of why it stopped, we need to clear the stop info
1458 thread.SetStopInfo (StopInfoSP());
1459 }
1460 }
1461#endif
1462 }
1463 else if (ISETSTATE == 1)
1464 {
1465 // Thumb mode
1466 const uint32_t ITSTATE = Bits32 (cpsr, 15, 10) << 2 | Bits32 (cpsr, 26, 25);
1467 if (ITSTATE != 0)
1468 {
1469 const uint32_t condition = Bits32(ITSTATE, 7, 4);
Eugene Zelenko896ddd02016-03-02 01:09:03 +00001470 if (!ARMConditionPassed(condition, cpsr))
Greg Claytona97c4d22014-12-09 23:31:02 +00001471 {
1472 // We ARE stopped in a Thumb IT instruction on an instruction whose
1473 // condition doesn't pass so this instruction won't get executed.
1474 // Regardless of why it stopped, we need to clear the stop info
1475 thread.SetStopInfo (StopInfoSP());
1476 }
1477 }
1478 }
1479 }
1480 }
1481}
1482
1483ArchSpec::StopInfoOverrideCallbackType
1484ArchSpec::GetStopInfoOverrideCallback () const
1485{
1486 const llvm::Triple::ArchType machine = GetMachine();
1487 if (machine == llvm::Triple::arm)
1488 return StopInfoOverrideCallbackTypeARM;
Eugene Zelenko896ddd02016-03-02 01:09:03 +00001489 return nullptr;
Greg Claytona97c4d22014-12-09 23:31:02 +00001490}
Todd Fiala7df337f2015-10-13 23:41:19 +00001491
Jason Molenda03fe45e2015-11-06 01:43:36 +00001492bool
1493ArchSpec::IsFullySpecifiedTriple () const
1494{
1495 const auto& user_specified_triple = GetTriple();
1496
1497 bool user_triple_fully_specified = false;
1498
1499 if ((user_specified_triple.getOS() != llvm::Triple::UnknownOS) || TripleOSWasSpecified())
1500 {
1501 if ((user_specified_triple.getVendor() != llvm::Triple::UnknownVendor) || TripleVendorWasSpecified())
1502 {
1503 const unsigned unspecified = 0;
1504 if (user_specified_triple.getOSMajorVersion() != unspecified)
1505 {
1506 user_triple_fully_specified = true;
1507 }
1508 }
1509 }
1510
1511 return user_triple_fully_specified;
1512}
1513
1514void
1515ArchSpec::PiecewiseTripleCompare (const ArchSpec &other,
1516 bool &arch_different,
1517 bool &vendor_different,
1518 bool &os_different,
1519 bool &os_version_different,
1520 bool &env_different)
1521{
1522 const llvm::Triple &me(GetTriple());
1523 const llvm::Triple &them(other.GetTriple());
1524
1525 arch_different = (me.getArch() != them.getArch());
1526
1527 vendor_different = (me.getVendor() != them.getVendor());
1528
1529 os_different = (me.getOS() != them.getOS());
1530
1531 os_version_different = (me.getOSMajorVersion() != them.getOSMajorVersion());
1532
1533 env_different = (me.getEnvironment() != them.getEnvironment());
1534}
1535
Jason Molenda583b1a82016-04-05 05:01:30 +00001536bool
1537ArchSpec::IsAlwaysThumbInstructions () const
1538{
1539 std::string Error;
1540 if (GetTriple().getArch() == llvm::Triple::arm || GetTriple().getArch() == llvm::Triple::thumb)
1541 {
1542 // v. https://en.wikipedia.org/wiki/ARM_Cortex-M
1543 //
1544 // Cortex-M0 through Cortex-M7 are ARM processor cores which can only
1545 // execute thumb instructions. We map the cores to arch names like this:
1546 //
1547 // Cortex-M0, Cortex-M0+, Cortex-M1: armv6m
1548 // Cortex-M3: armv7m
1549 // Cortex-M4, Cortex-M7: armv7em
1550
1551 if (GetCore() == ArchSpec::Core::eCore_arm_armv7m
1552 || GetCore() == ArchSpec::Core::eCore_arm_armv7em
1553 || GetCore() == ArchSpec::Core::eCore_arm_armv6m)
1554 {
1555 return true;
1556 }
1557 }
1558 return false;
1559}
1560
Todd Fiala7df337f2015-10-13 23:41:19 +00001561void
1562ArchSpec::DumpTriple(Stream &s) const
1563{
1564 const llvm::Triple &triple = GetTriple();
1565 llvm::StringRef arch_str = triple.getArchName();
1566 llvm::StringRef vendor_str = triple.getVendorName();
1567 llvm::StringRef os_str = triple.getOSName();
1568
1569 s.Printf("%s-%s-%s",
1570 arch_str.empty() ? "*" : arch_str.str().c_str(),
1571 vendor_str.empty() ? "*" : vendor_str.str().c_str(),
1572 os_str.empty() ? "*" : os_str.str().c_str()
1573 );
1574}