blob: 24aba81350a62127abd21f67e1f631f3d9f55db3 [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
Ulrich Weigandbb00d0b2016-04-14 14:28:34 +0000133 { eByteOrderBig , 8, 2, 6, llvm::Triple::systemz, ArchSpec::eCore_s390x_generic , "s390x" },
134
Greg Clayton357132e2011-03-26 19:14:58 +0000135 { eByteOrderLittle, 4, 4, 4, llvm::Triple::sparc , ArchSpec::eCore_sparc_generic , "sparc" },
136 { eByteOrderLittle, 8, 4, 4, llvm::Triple::sparcv9, ArchSpec::eCore_sparc9_generic , "sparcv9" },
Greg Clayton64195a22011-02-23 00:35:02 +0000137
Greg Claytonab65b342011-04-13 22:47:15 +0000138 { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i386 , "i386" },
139 { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i486 , "i486" },
140 { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i486sx , "i486sx" },
Virgile Bello97a70e42014-04-08 14:48:48 +0000141 { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i686 , "i686" },
Greg Clayton64195a22011-02-23 00:35:02 +0000142
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000143 { eByteOrderLittle, 8, 1, 15, llvm::Triple::x86_64 , ArchSpec::eCore_x86_64_x86_64 , "x86_64" },
Greg Claytona86dc432014-01-22 23:42:03 +0000144 { eByteOrderLittle, 8, 1, 15, llvm::Triple::x86_64 , ArchSpec::eCore_x86_64_x86_64h , "x86_64h" },
Deepak Panickal6d3df422014-02-19 11:16:46 +0000145 { eByteOrderLittle, 4, 4, 4, llvm::Triple::hexagon , ArchSpec::eCore_hexagon_generic, "hexagon" },
146 { eByteOrderLittle, 4, 4, 4, llvm::Triple::hexagon , ArchSpec::eCore_hexagon_hexagonv4, "hexagonv4" },
147 { eByteOrderLittle, 4, 4, 4, llvm::Triple::hexagon , ArchSpec::eCore_hexagon_hexagonv5, "hexagonv5" },
148
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000149 { eByteOrderLittle, 4, 4, 4 , llvm::Triple::UnknownArch , ArchSpec::eCore_uknownMach32 , "unknown-mach-32" },
Todd Fiala14bbef52014-07-01 23:33:32 +0000150 { eByteOrderLittle, 8, 4, 4 , llvm::Triple::UnknownArch , ArchSpec::eCore_uknownMach64 , "unknown-mach-64" },
151
Matthew Gardiner5f675792014-08-27 12:09:39 +0000152 { eByteOrderBig , 4, 1, 1 , llvm::Triple::kalimba , ArchSpec::eCore_kalimba3 , "kalimba3" },
153 { eByteOrderLittle, 4, 1, 1 , llvm::Triple::kalimba , ArchSpec::eCore_kalimba4 , "kalimba4" },
154 { eByteOrderLittle, 4, 1, 1 , llvm::Triple::kalimba , ArchSpec::eCore_kalimba5 , "kalimba5" }
Greg Clayton64195a22011-02-23 00:35:02 +0000155};
156
Greg Clayton56b79682014-07-23 18:12:06 +0000157// Ensure that we have an entry in the g_core_definitions for each core. If you comment out an entry above,
158// you will need to comment out the corresponding ArchSpec::Core enumeration.
Zachary Turner3b2065f2014-07-28 16:44:28 +0000159static_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 +0000160
Greg Clayton64195a22011-02-23 00:35:02 +0000161struct ArchDefinitionEntry
162{
163 ArchSpec::Core core;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000164 uint32_t cpu;
165 uint32_t sub;
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000166 uint32_t cpu_mask;
167 uint32_t sub_mask;
Greg Clayton64195a22011-02-23 00:35:02 +0000168};
169
170struct ArchDefinition
171{
172 ArchitectureType type;
173 size_t num_entries;
174 const ArchDefinitionEntry *entries;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000175 const char *name;
176};
177
Greg Claytonc7bece562013-01-25 18:06:21 +0000178size_t
Greg Claytonab65b342011-04-13 22:47:15 +0000179ArchSpec::AutoComplete (const char *name, StringList &matches)
180{
Greg Claytonab65b342011-04-13 22:47:15 +0000181 if (name && name[0])
182 {
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000183 for (uint32_t i = 0; i < llvm::array_lengthof(g_core_definitions); ++i)
Greg Claytonab65b342011-04-13 22:47:15 +0000184 {
185 if (NameMatches(g_core_definitions[i].name, eNameMatchStartsWith, name))
186 matches.AppendString (g_core_definitions[i].name);
187 }
188 }
189 else
190 {
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000191 for (uint32_t i = 0; i < llvm::array_lengthof(g_core_definitions); ++i)
Greg Claytonab65b342011-04-13 22:47:15 +0000192 matches.AppendString (g_core_definitions[i].name);
193 }
194 return matches.GetSize();
195}
196
Greg Clayton64195a22011-02-23 00:35:02 +0000197#define CPU_ANY (UINT32_MAX)
198
199//===----------------------------------------------------------------------===//
200// A table that gets searched linearly for matches. This table is used to
201// convert cpu type and subtypes to architecture names, and to convert
202// architecture names to cpu types and subtypes. The ordering is important and
203// allows the precedence to be set when the table is built.
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000204#define SUBTYPE_MASK 0x00FFFFFFu
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000205
Greg Clayton64195a22011-02-23 00:35:02 +0000206static const ArchDefinitionEntry g_macho_arch_entries[] =
Greg Clayton41f92322010-06-11 03:25:34 +0000207{
Charles Davis510938e2013-08-27 05:04:57 +0000208 { ArchSpec::eCore_arm_generic , llvm::MachO::CPU_TYPE_ARM , CPU_ANY, UINT32_MAX , UINT32_MAX },
209 { ArchSpec::eCore_arm_generic , llvm::MachO::CPU_TYPE_ARM , 0 , UINT32_MAX , SUBTYPE_MASK },
210 { ArchSpec::eCore_arm_armv4 , llvm::MachO::CPU_TYPE_ARM , 5 , UINT32_MAX , SUBTYPE_MASK },
211 { ArchSpec::eCore_arm_armv4t , llvm::MachO::CPU_TYPE_ARM , 5 , UINT32_MAX , SUBTYPE_MASK },
212 { ArchSpec::eCore_arm_armv6 , llvm::MachO::CPU_TYPE_ARM , 6 , UINT32_MAX , SUBTYPE_MASK },
Jason Molenda64a11732013-10-08 03:01:08 +0000213 { ArchSpec::eCore_arm_armv6m , llvm::MachO::CPU_TYPE_ARM , 14 , UINT32_MAX , SUBTYPE_MASK },
Charles Davis510938e2013-08-27 05:04:57 +0000214 { ArchSpec::eCore_arm_armv5 , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
215 { ArchSpec::eCore_arm_armv5e , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
216 { ArchSpec::eCore_arm_armv5t , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
217 { ArchSpec::eCore_arm_xscale , llvm::MachO::CPU_TYPE_ARM , 8 , UINT32_MAX , SUBTYPE_MASK },
218 { ArchSpec::eCore_arm_armv7 , llvm::MachO::CPU_TYPE_ARM , 9 , UINT32_MAX , SUBTYPE_MASK },
219 { ArchSpec::eCore_arm_armv7f , llvm::MachO::CPU_TYPE_ARM , 10 , UINT32_MAX , SUBTYPE_MASK },
220 { ArchSpec::eCore_arm_armv7s , llvm::MachO::CPU_TYPE_ARM , 11 , UINT32_MAX , SUBTYPE_MASK },
221 { ArchSpec::eCore_arm_armv7k , llvm::MachO::CPU_TYPE_ARM , 12 , UINT32_MAX , SUBTYPE_MASK },
222 { ArchSpec::eCore_arm_armv7m , llvm::MachO::CPU_TYPE_ARM , 15 , UINT32_MAX , SUBTYPE_MASK },
223 { ArchSpec::eCore_arm_armv7em , llvm::MachO::CPU_TYPE_ARM , 16 , UINT32_MAX , SUBTYPE_MASK },
Jason Molendaa3329782014-03-29 18:54:20 +0000224 { ArchSpec::eCore_arm_arm64 , llvm::MachO::CPU_TYPE_ARM64 , 1 , UINT32_MAX , SUBTYPE_MASK },
Jason Molenda22952582014-11-12 01:11:36 +0000225 { ArchSpec::eCore_arm_arm64 , llvm::MachO::CPU_TYPE_ARM64 , 0 , UINT32_MAX , SUBTYPE_MASK },
Jason Molendaa3329782014-03-29 18:54:20 +0000226 { ArchSpec::eCore_arm_arm64 , llvm::MachO::CPU_TYPE_ARM64 , 13 , UINT32_MAX , SUBTYPE_MASK },
Jason Molenda22952582014-11-12 01:11:36 +0000227 { ArchSpec::eCore_arm_arm64 , llvm::MachO::CPU_TYPE_ARM64 , CPU_ANY, UINT32_MAX , SUBTYPE_MASK },
Charles Davis510938e2013-08-27 05:04:57 +0000228 { ArchSpec::eCore_thumb , llvm::MachO::CPU_TYPE_ARM , 0 , UINT32_MAX , SUBTYPE_MASK },
229 { ArchSpec::eCore_thumbv4t , llvm::MachO::CPU_TYPE_ARM , 5 , UINT32_MAX , SUBTYPE_MASK },
230 { ArchSpec::eCore_thumbv5 , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
231 { ArchSpec::eCore_thumbv5e , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
232 { ArchSpec::eCore_thumbv6 , llvm::MachO::CPU_TYPE_ARM , 6 , UINT32_MAX , SUBTYPE_MASK },
Jason Molenda64a11732013-10-08 03:01:08 +0000233 { ArchSpec::eCore_thumbv6m , llvm::MachO::CPU_TYPE_ARM , 14 , UINT32_MAX , SUBTYPE_MASK },
Charles Davis510938e2013-08-27 05:04:57 +0000234 { ArchSpec::eCore_thumbv7 , llvm::MachO::CPU_TYPE_ARM , 9 , UINT32_MAX , SUBTYPE_MASK },
235 { ArchSpec::eCore_thumbv7f , llvm::MachO::CPU_TYPE_ARM , 10 , UINT32_MAX , SUBTYPE_MASK },
236 { ArchSpec::eCore_thumbv7s , llvm::MachO::CPU_TYPE_ARM , 11 , UINT32_MAX , SUBTYPE_MASK },
237 { ArchSpec::eCore_thumbv7k , llvm::MachO::CPU_TYPE_ARM , 12 , UINT32_MAX , SUBTYPE_MASK },
238 { ArchSpec::eCore_thumbv7m , llvm::MachO::CPU_TYPE_ARM , 15 , UINT32_MAX , SUBTYPE_MASK },
239 { ArchSpec::eCore_thumbv7em , llvm::MachO::CPU_TYPE_ARM , 16 , UINT32_MAX , SUBTYPE_MASK },
240 { ArchSpec::eCore_ppc_generic , llvm::MachO::CPU_TYPE_POWERPC , CPU_ANY, UINT32_MAX , UINT32_MAX },
241 { ArchSpec::eCore_ppc_generic , llvm::MachO::CPU_TYPE_POWERPC , 0 , UINT32_MAX , SUBTYPE_MASK },
242 { ArchSpec::eCore_ppc_ppc601 , llvm::MachO::CPU_TYPE_POWERPC , 1 , UINT32_MAX , SUBTYPE_MASK },
243 { ArchSpec::eCore_ppc_ppc602 , llvm::MachO::CPU_TYPE_POWERPC , 2 , UINT32_MAX , SUBTYPE_MASK },
244 { ArchSpec::eCore_ppc_ppc603 , llvm::MachO::CPU_TYPE_POWERPC , 3 , UINT32_MAX , SUBTYPE_MASK },
245 { ArchSpec::eCore_ppc_ppc603e , llvm::MachO::CPU_TYPE_POWERPC , 4 , UINT32_MAX , SUBTYPE_MASK },
246 { ArchSpec::eCore_ppc_ppc603ev , llvm::MachO::CPU_TYPE_POWERPC , 5 , UINT32_MAX , SUBTYPE_MASK },
247 { ArchSpec::eCore_ppc_ppc604 , llvm::MachO::CPU_TYPE_POWERPC , 6 , UINT32_MAX , SUBTYPE_MASK },
248 { ArchSpec::eCore_ppc_ppc604e , llvm::MachO::CPU_TYPE_POWERPC , 7 , UINT32_MAX , SUBTYPE_MASK },
249 { ArchSpec::eCore_ppc_ppc620 , llvm::MachO::CPU_TYPE_POWERPC , 8 , UINT32_MAX , SUBTYPE_MASK },
250 { ArchSpec::eCore_ppc_ppc750 , llvm::MachO::CPU_TYPE_POWERPC , 9 , UINT32_MAX , SUBTYPE_MASK },
251 { ArchSpec::eCore_ppc_ppc7400 , llvm::MachO::CPU_TYPE_POWERPC , 10 , UINT32_MAX , SUBTYPE_MASK },
252 { ArchSpec::eCore_ppc_ppc7450 , llvm::MachO::CPU_TYPE_POWERPC , 11 , UINT32_MAX , SUBTYPE_MASK },
253 { ArchSpec::eCore_ppc_ppc970 , llvm::MachO::CPU_TYPE_POWERPC , 100 , UINT32_MAX , SUBTYPE_MASK },
254 { ArchSpec::eCore_ppc64_generic , llvm::MachO::CPU_TYPE_POWERPC64 , 0 , UINT32_MAX , SUBTYPE_MASK },
255 { ArchSpec::eCore_ppc64_ppc970_64 , llvm::MachO::CPU_TYPE_POWERPC64 , 100 , UINT32_MAX , SUBTYPE_MASK },
256 { ArchSpec::eCore_x86_32_i386 , llvm::MachO::CPU_TYPE_I386 , 3 , UINT32_MAX , SUBTYPE_MASK },
257 { ArchSpec::eCore_x86_32_i486 , llvm::MachO::CPU_TYPE_I386 , 4 , UINT32_MAX , SUBTYPE_MASK },
258 { ArchSpec::eCore_x86_32_i486sx , llvm::MachO::CPU_TYPE_I386 , 0x84 , UINT32_MAX , SUBTYPE_MASK },
Greg Claytona86dc432014-01-22 23:42:03 +0000259 { ArchSpec::eCore_x86_32_i386 , llvm::MachO::CPU_TYPE_I386 , CPU_ANY, UINT32_MAX , UINT32_MAX },
Charles Davis510938e2013-08-27 05:04:57 +0000260 { ArchSpec::eCore_x86_64_x86_64 , llvm::MachO::CPU_TYPE_X86_64 , 3 , UINT32_MAX , SUBTYPE_MASK },
261 { ArchSpec::eCore_x86_64_x86_64 , llvm::MachO::CPU_TYPE_X86_64 , 4 , UINT32_MAX , SUBTYPE_MASK },
Greg Claytona86dc432014-01-22 23:42:03 +0000262 { ArchSpec::eCore_x86_64_x86_64h , llvm::MachO::CPU_TYPE_X86_64 , 8 , UINT32_MAX , SUBTYPE_MASK },
263 { 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 +0000264 // Catch any unknown mach architectures so we can always use the object and symbol mach-o files
Charles Davis510938e2013-08-27 05:04:57 +0000265 { ArchSpec::eCore_uknownMach32 , 0 , 0 , 0xFF000000u, 0x00000000u },
266 { ArchSpec::eCore_uknownMach64 , llvm::MachO::CPU_ARCH_ABI64 , 0 , 0xFF000000u, 0x00000000u }
Greg Clayton64195a22011-02-23 00:35:02 +0000267};
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000268
Greg Clayton64195a22011-02-23 00:35:02 +0000269static const ArchDefinition g_macho_arch_def = {
270 eArchTypeMachO,
Saleem Abdulrasool28606952014-06-27 05:17:41 +0000271 llvm::array_lengthof(g_macho_arch_entries),
Greg Clayton64195a22011-02-23 00:35:02 +0000272 g_macho_arch_entries,
Greg Clayton64195a22011-02-23 00:35:02 +0000273 "mach-o"
Greg Clayton41f92322010-06-11 03:25:34 +0000274};
275
Greg Clayton64195a22011-02-23 00:35:02 +0000276//===----------------------------------------------------------------------===//
277// A table that gets searched linearly for matches. This table is used to
278// convert cpu type and subtypes to architecture names, and to convert
279// architecture names to cpu types and subtypes. The ordering is important and
280// allows the precedence to be set when the table is built.
281static const ArchDefinitionEntry g_elf_arch_entries[] =
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000282{
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000283 { ArchSpec::eCore_sparc_generic , llvm::ELF::EM_SPARC , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // Sparc
284 { ArchSpec::eCore_x86_32_i386 , llvm::ELF::EM_386 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // Intel 80386
Rafael Espindola86f422e2015-06-19 17:02:25 +0000285 { 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 +0000286 { ArchSpec::eCore_ppc_generic , llvm::ELF::EM_PPC , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // PowerPC
287 { ArchSpec::eCore_ppc64_generic , llvm::ELF::EM_PPC64 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // PowerPC64
288 { ArchSpec::eCore_arm_generic , llvm::ELF::EM_ARM , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // ARM
Todd Fiala02e71812014-08-28 14:32:43 +0000289 { ArchSpec::eCore_arm_aarch64 , llvm::ELF::EM_AARCH64, LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // ARM64
Ulrich Weigandbb00d0b2016-04-14 14:28:34 +0000290 { ArchSpec::eCore_s390x_generic , llvm::ELF::EM_S390 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // SystemZ
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000291 { ArchSpec::eCore_sparc9_generic , llvm::ELF::EM_SPARCV9, LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // SPARC V9
Ed Masteb73f8442013-10-10 00:59:47 +0000292 { 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 +0000293 { ArchSpec::eCore_mips32 , llvm::ELF::EM_MIPS , ArchSpec::eMIPSSubType_mips32, 0xFFFFFFFFu, 0xFFFFFFFFu }, // mips32
294 { ArchSpec::eCore_mips32r2 , llvm::ELF::EM_MIPS , ArchSpec::eMIPSSubType_mips32r2, 0xFFFFFFFFu, 0xFFFFFFFFu }, // mips32r2
295 { ArchSpec::eCore_mips32r6 , llvm::ELF::EM_MIPS , ArchSpec::eMIPSSubType_mips32r6, 0xFFFFFFFFu, 0xFFFFFFFFu }, // mips32r6
296 { ArchSpec::eCore_mips32el , llvm::ELF::EM_MIPS , ArchSpec::eMIPSSubType_mips32el, 0xFFFFFFFFu, 0xFFFFFFFFu }, // mips32el
297 { ArchSpec::eCore_mips32r2el , llvm::ELF::EM_MIPS , ArchSpec::eMIPSSubType_mips32r2el, 0xFFFFFFFFu, 0xFFFFFFFFu }, // mips32r2el
298 { ArchSpec::eCore_mips32r6el , llvm::ELF::EM_MIPS , ArchSpec::eMIPSSubType_mips32r6el, 0xFFFFFFFFu, 0xFFFFFFFFu }, // mips32r6el
299 { ArchSpec::eCore_mips64 , llvm::ELF::EM_MIPS , ArchSpec::eMIPSSubType_mips64, 0xFFFFFFFFu, 0xFFFFFFFFu }, // mips64
300 { ArchSpec::eCore_mips64r2 , llvm::ELF::EM_MIPS , ArchSpec::eMIPSSubType_mips64r2, 0xFFFFFFFFu, 0xFFFFFFFFu }, // mips64r2
301 { ArchSpec::eCore_mips64r6 , llvm::ELF::EM_MIPS , ArchSpec::eMIPSSubType_mips64r6, 0xFFFFFFFFu, 0xFFFFFFFFu }, // mips64r6
302 { ArchSpec::eCore_mips64el , llvm::ELF::EM_MIPS , ArchSpec::eMIPSSubType_mips64el, 0xFFFFFFFFu, 0xFFFFFFFFu }, // mips64el
303 { ArchSpec::eCore_mips64r2el , llvm::ELF::EM_MIPS , ArchSpec::eMIPSSubType_mips64r2el, 0xFFFFFFFFu, 0xFFFFFFFFu }, // mips64r2el
304 { ArchSpec::eCore_mips64r6el , llvm::ELF::EM_MIPS , ArchSpec::eMIPSSubType_mips64r6el, 0xFFFFFFFFu, 0xFFFFFFFFu }, // mips64r6el
Todd Fiala14bbef52014-07-01 23:33:32 +0000305 { ArchSpec::eCore_hexagon_generic , llvm::ELF::EM_HEXAGON, LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // HEXAGON
Matthew Gardinerf03e6d842014-09-29 08:02:24 +0000306 { ArchSpec::eCore_kalimba3 , llvm::ELF::EM_CSR_KALIMBA, llvm::Triple::KalimbaSubArch_v3, 0xFFFFFFFFu, 0xFFFFFFFFu }, // KALIMBA
307 { ArchSpec::eCore_kalimba4 , llvm::ELF::EM_CSR_KALIMBA, llvm::Triple::KalimbaSubArch_v4, 0xFFFFFFFFu, 0xFFFFFFFFu }, // KALIMBA
308 { ArchSpec::eCore_kalimba5 , llvm::ELF::EM_CSR_KALIMBA, llvm::Triple::KalimbaSubArch_v5, 0xFFFFFFFFu, 0xFFFFFFFFu } // KALIMBA
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000309};
310
Greg Clayton64195a22011-02-23 00:35:02 +0000311static const ArchDefinition g_elf_arch_def = {
312 eArchTypeELF,
Saleem Abdulrasool28606952014-06-27 05:17:41 +0000313 llvm::array_lengthof(g_elf_arch_entries),
Greg Clayton64195a22011-02-23 00:35:02 +0000314 g_elf_arch_entries,
Greg Clayton64195a22011-02-23 00:35:02 +0000315 "elf",
Greg Clayton41f92322010-06-11 03:25:34 +0000316};
317
Charles Davis237ad972013-08-27 05:04:33 +0000318static const ArchDefinitionEntry g_coff_arch_entries[] =
319{
Zachary Turnerad587ae42014-07-28 16:44:49 +0000320 { 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 +0000321 { ArchSpec::eCore_ppc_generic , llvm::COFF::IMAGE_FILE_MACHINE_POWERPC , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // PowerPC
322 { ArchSpec::eCore_ppc_generic , llvm::COFF::IMAGE_FILE_MACHINE_POWERPCFP, LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // PowerPC (with FPU)
323 { ArchSpec::eCore_arm_generic , llvm::COFF::IMAGE_FILE_MACHINE_ARM , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // ARM
Saleem Abdulrasool1108cb32014-03-11 03:09:08 +0000324 { ArchSpec::eCore_arm_armv7 , llvm::COFF::IMAGE_FILE_MACHINE_ARMNT , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // ARMv7
Charles Davis237ad972013-08-27 05:04:33 +0000325 { ArchSpec::eCore_thumb , llvm::COFF::IMAGE_FILE_MACHINE_THUMB , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // ARMv7
326 { ArchSpec::eCore_x86_64_x86_64, llvm::COFF::IMAGE_FILE_MACHINE_AMD64 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu } // AMD64
327};
328
329static const ArchDefinition g_coff_arch_def = {
330 eArchTypeCOFF,
Saleem Abdulrasool28606952014-06-27 05:17:41 +0000331 llvm::array_lengthof(g_coff_arch_entries),
Charles Davis237ad972013-08-27 05:04:33 +0000332 g_coff_arch_entries,
333 "pe-coff",
334};
335
Greg Clayton64195a22011-02-23 00:35:02 +0000336//===----------------------------------------------------------------------===//
337// Table of all ArchDefinitions
338static const ArchDefinition *g_arch_definitions[] = {
339 &g_macho_arch_def,
Charles Davis237ad972013-08-27 05:04:33 +0000340 &g_elf_arch_def,
341 &g_coff_arch_def
Greg Clayton64195a22011-02-23 00:35:02 +0000342};
Greg Clayton41f92322010-06-11 03:25:34 +0000343
Saleem Abdulrasool28606952014-06-27 05:17:41 +0000344static const size_t k_num_arch_definitions = llvm::array_lengthof(g_arch_definitions);
Greg Clayton64195a22011-02-23 00:35:02 +0000345
346//===----------------------------------------------------------------------===//
347// Static helper functions.
348
Greg Clayton64195a22011-02-23 00:35:02 +0000349// Get the architecture definition for a given object type.
350static const ArchDefinition *
351FindArchDefinition (ArchitectureType arch_type)
352{
353 for (unsigned int i = 0; i < k_num_arch_definitions; ++i)
354 {
355 const ArchDefinition *def = g_arch_definitions[i];
356 if (def->type == arch_type)
357 return def;
358 }
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000359 return nullptr;
Greg Clayton64195a22011-02-23 00:35:02 +0000360}
361
362// Get an architecture definition by name.
363static const CoreDefinition *
364FindCoreDefinition (llvm::StringRef name)
365{
Greg Clayton56b79682014-07-23 18:12:06 +0000366 for (unsigned int i = 0; i < llvm::array_lengthof(g_core_definitions); ++i)
Greg Clayton64195a22011-02-23 00:35:02 +0000367 {
368 if (name.equals_lower(g_core_definitions[i].name))
369 return &g_core_definitions[i];
370 }
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000371 return nullptr;
Greg Clayton64195a22011-02-23 00:35:02 +0000372}
373
374static inline const CoreDefinition *
375FindCoreDefinition (ArchSpec::Core core)
376{
Greg Clayton56b79682014-07-23 18:12:06 +0000377 if (core >= 0 && core < llvm::array_lengthof(g_core_definitions))
Greg Clayton64195a22011-02-23 00:35:02 +0000378 return &g_core_definitions[core];
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000379 return nullptr;
Greg Clayton64195a22011-02-23 00:35:02 +0000380}
381
382// Get a definition entry by cpu type and subtype.
383static const ArchDefinitionEntry *
384FindArchDefinitionEntry (const ArchDefinition *def, uint32_t cpu, uint32_t sub)
385{
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000386 if (def == nullptr)
387 return nullptr;
Greg Clayton64195a22011-02-23 00:35:02 +0000388
Greg Clayton64195a22011-02-23 00:35:02 +0000389 const ArchDefinitionEntry *entries = def->entries;
390 for (size_t i = 0; i < def->num_entries; ++i)
391 {
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000392 if (entries[i].cpu == (cpu & entries[i].cpu_mask))
393 if (entries[i].sub == (sub & entries[i].sub_mask))
394 return &entries[i];
Greg Clayton64195a22011-02-23 00:35:02 +0000395 }
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000396 return nullptr;
Greg Clayton64195a22011-02-23 00:35:02 +0000397}
398
399static const ArchDefinitionEntry *
400FindArchDefinitionEntry (const ArchDefinition *def, ArchSpec::Core core)
401{
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000402 if (def == nullptr)
403 return nullptr;
Greg Clayton64195a22011-02-23 00:35:02 +0000404
405 const ArchDefinitionEntry *entries = def->entries;
406 for (size_t i = 0; i < def->num_entries; ++i)
407 {
408 if (entries[i].core == core)
409 return &entries[i];
410 }
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000411 return nullptr;
Greg Clayton64195a22011-02-23 00:35:02 +0000412}
413
414//===----------------------------------------------------------------------===//
415// Constructors and destructors.
416
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000417ArchSpec::ArchSpec() :
Greg Clayton514487e2011-02-15 21:59:32 +0000418 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000419 m_core (kCore_invalid),
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000420 m_byte_order (eByteOrderInvalid),
Pavel Labath07695bd2015-07-16 13:11:34 +0000421 m_flags (0),
422 m_distribution_id ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000423{
424}
425
Greg Claytoneb0103f2011-04-07 22:46:35 +0000426ArchSpec::ArchSpec (const char *triple_cstr, Platform *platform) :
Greg Clayton514487e2011-02-15 21:59:32 +0000427 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000428 m_core (kCore_invalid),
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000429 m_byte_order (eByteOrderInvalid),
Pavel Labath07695bd2015-07-16 13:11:34 +0000430 m_flags (0),
431 m_distribution_id ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000432{
Greg Clayton64195a22011-02-23 00:35:02 +0000433 if (triple_cstr)
Greg Claytoneb0103f2011-04-07 22:46:35 +0000434 SetTriple(triple_cstr, platform);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000435}
436
Greg Clayton70512312012-05-08 01:45:38 +0000437
438ArchSpec::ArchSpec (const char *triple_cstr) :
439 m_triple (),
440 m_core (kCore_invalid),
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000441 m_byte_order (eByteOrderInvalid),
Pavel Labath07695bd2015-07-16 13:11:34 +0000442 m_flags (0),
443 m_distribution_id ()
Greg Clayton70512312012-05-08 01:45:38 +0000444{
445 if (triple_cstr)
446 SetTriple(triple_cstr);
447}
448
Greg Clayton64195a22011-02-23 00:35:02 +0000449ArchSpec::ArchSpec(const llvm::Triple &triple) :
Greg Clayton514487e2011-02-15 21:59:32 +0000450 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000451 m_core (kCore_invalid),
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000452 m_byte_order (eByteOrderInvalid),
Pavel Labath07695bd2015-07-16 13:11:34 +0000453 m_flags (0),
454 m_distribution_id ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000455{
Greg Clayton64195a22011-02-23 00:35:02 +0000456 SetTriple(triple);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000457}
458
Greg Claytone0d378b2011-03-24 21:19:54 +0000459ArchSpec::ArchSpec (ArchitectureType arch_type, uint32_t cpu, uint32_t subtype) :
Greg Clayton64195a22011-02-23 00:35:02 +0000460 m_triple (),
461 m_core (kCore_invalid),
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000462 m_byte_order (eByteOrderInvalid),
Pavel Labath07695bd2015-07-16 13:11:34 +0000463 m_flags (0),
464 m_distribution_id ()
Greg Clayton64195a22011-02-23 00:35:02 +0000465{
466 SetArchitecture (arch_type, cpu, subtype);
467}
468
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000469ArchSpec::~ArchSpec() = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000470
Greg Clayton64195a22011-02-23 00:35:02 +0000471//===----------------------------------------------------------------------===//
472// Assignment and initialization.
473
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000474const ArchSpec&
475ArchSpec::operator= (const ArchSpec& rhs)
476{
477 if (this != &rhs)
478 {
Greg Clayton514487e2011-02-15 21:59:32 +0000479 m_triple = rhs.m_triple;
Greg Clayton64195a22011-02-23 00:35:02 +0000480 m_core = rhs.m_core;
Greg Clayton514487e2011-02-15 21:59:32 +0000481 m_byte_order = rhs.m_byte_order;
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000482 m_distribution_id = rhs.m_distribution_id;
Jaydeep Patil501a7812015-07-16 03:51:55 +0000483 m_flags = rhs.m_flags;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000484 }
485 return *this;
486}
487
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000488void
489ArchSpec::Clear()
490{
Greg Clayton514487e2011-02-15 21:59:32 +0000491 m_triple = llvm::Triple();
Greg Clayton64195a22011-02-23 00:35:02 +0000492 m_core = kCore_invalid;
493 m_byte_order = eByteOrderInvalid;
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000494 m_distribution_id.Clear ();
Jaydeep Patil501a7812015-07-16 03:51:55 +0000495 m_flags = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000496}
497
Greg Clayton64195a22011-02-23 00:35:02 +0000498//===----------------------------------------------------------------------===//
499// Predicates.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000500
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000501const char *
Greg Clayton64195a22011-02-23 00:35:02 +0000502ArchSpec::GetArchitectureName () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000503{
Greg Clayton64195a22011-02-23 00:35:02 +0000504 const CoreDefinition *core_def = FindCoreDefinition (m_core);
505 if (core_def)
506 return core_def->name;
507 return "unknown";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000508}
509
Sagar Thakur477eb422016-05-11 13:08:29 +0000510bool
511ArchSpec::IsMIPS() const
512{
513 const llvm::Triple::ArchType machine = GetMachine();
514 if(machine == llvm::Triple::mips ||
515 machine == llvm::Triple::mipsel ||
516 machine == llvm::Triple::mips64 ||
517 machine == llvm::Triple::mips64el)
518 return true;
519 return false;
520}
521
Bhushan D. Attarde3592a6e2016-02-18 11:53:28 +0000522std::string
523ArchSpec::GetClangTargetCPU ()
524{
525 std::string cpu;
526 const llvm::Triple::ArchType machine = GetMachine();
527
528 if (machine == llvm::Triple::mips ||
529 machine == llvm::Triple::mipsel ||
530 machine == llvm::Triple::mips64 ||
531 machine == llvm::Triple::mips64el)
532 {
533 switch (m_core)
534 {
535 case ArchSpec::eCore_mips32:
536 case ArchSpec::eCore_mips32el:
537 cpu = "mips32"; break;
538 case ArchSpec::eCore_mips32r2:
539 case ArchSpec::eCore_mips32r2el:
540 cpu = "mips32r2"; break;
541 case ArchSpec::eCore_mips32r3:
542 case ArchSpec::eCore_mips32r3el:
543 cpu = "mips32r3"; break;
544 case ArchSpec::eCore_mips32r5:
545 case ArchSpec::eCore_mips32r5el:
546 cpu = "mips32r5"; break;
547 case ArchSpec::eCore_mips32r6:
548 case ArchSpec::eCore_mips32r6el:
549 cpu = "mips32r6"; break;
550 case ArchSpec::eCore_mips64:
551 case ArchSpec::eCore_mips64el:
552 cpu = "mips64"; break;
553 case ArchSpec::eCore_mips64r2:
554 case ArchSpec::eCore_mips64r2el:
555 cpu = "mips64r2"; break;
556 case ArchSpec::eCore_mips64r3:
557 case ArchSpec::eCore_mips64r3el:
558 cpu = "mips64r3"; break;
559 case ArchSpec::eCore_mips64r5:
560 case ArchSpec::eCore_mips64r5el:
561 cpu = "mips64r5"; break;
562 case ArchSpec::eCore_mips64r6:
563 case ArchSpec::eCore_mips64r6el:
564 cpu = "mips64r6"; break;
565 default:
566 break;
567 }
568 }
569 return cpu;
570}
571
Greg Clayton64195a22011-02-23 00:35:02 +0000572uint32_t
573ArchSpec::GetMachOCPUType () 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)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000577 {
Greg Clayton64195a22011-02-23 00:35:02 +0000578 const ArchDefinitionEntry *arch_def = FindArchDefinitionEntry (&g_macho_arch_def, core_def->core);
579 if (arch_def)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000580 {
Greg Clayton64195a22011-02-23 00:35:02 +0000581 return arch_def->cpu;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000582 }
583 }
Greg Clayton64195a22011-02-23 00:35:02 +0000584 return LLDB_INVALID_CPUTYPE;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000585}
586
Greg Clayton64195a22011-02-23 00:35:02 +0000587uint32_t
588ArchSpec::GetMachOCPUSubType () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000589{
Greg Clayton64195a22011-02-23 00:35:02 +0000590 const CoreDefinition *core_def = FindCoreDefinition (m_core);
591 if (core_def)
592 {
593 const ArchDefinitionEntry *arch_def = FindArchDefinitionEntry (&g_macho_arch_def, core_def->core);
594 if (arch_def)
595 {
Greg Clayton1cb64962011-03-24 04:28:38 +0000596 return arch_def->sub;
Greg Clayton64195a22011-02-23 00:35:02 +0000597 }
598 }
599 return LLDB_INVALID_CPUTYPE;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000600}
601
Matthew Gardinere77b2942014-09-01 09:06:03 +0000602uint32_t
603ArchSpec::GetDataByteSize () const
604{
605 switch (m_core)
606 {
607 case eCore_kalimba3:
Matthew Gardinerf03e6d842014-09-29 08:02:24 +0000608 return 4;
Matthew Gardinere77b2942014-09-01 09:06:03 +0000609 case eCore_kalimba4:
610 return 1;
611 case eCore_kalimba5:
Matthew Gardinerf03e6d842014-09-29 08:02:24 +0000612 return 4;
Matthew Gardinere77b2942014-09-01 09:06:03 +0000613 default:
614 return 1;
615 }
616 return 1;
617}
618
619uint32_t
620ArchSpec::GetCodeByteSize () const
621{
622 switch (m_core)
623 {
624 case eCore_kalimba3:
625 return 4;
626 case eCore_kalimba4:
627 return 1;
628 case eCore_kalimba5:
629 return 1;
630 default:
631 return 1;
632 }
633 return 1;
634}
635
Greg Clayton64195a22011-02-23 00:35:02 +0000636llvm::Triple::ArchType
637ArchSpec::GetMachine () const
638{
639 const CoreDefinition *core_def = FindCoreDefinition (m_core);
640 if (core_def)
641 return core_def->machine;
642
643 return llvm::Triple::UnknownArch;
644}
645
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000646const ConstString&
647ArchSpec::GetDistributionId () const
648{
649 return m_distribution_id;
650}
651
652void
653ArchSpec::SetDistributionId (const char* distribution_id)
654{
655 m_distribution_id.SetCString (distribution_id);
656}
657
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000658uint32_t
659ArchSpec::GetAddressByteSize() const
660{
Greg Clayton64195a22011-02-23 00:35:02 +0000661 const CoreDefinition *core_def = FindCoreDefinition (m_core);
662 if (core_def)
Mohit K. Bhakkad9514a382015-09-09 10:32:20 +0000663 {
664 if (core_def->machine == llvm::Triple::mips64 || core_def->machine == llvm::Triple::mips64el)
665 {
666 // For N32/O32 applications Address size is 4 bytes.
667 if (m_flags & (eMIPSABI_N32 | eMIPSABI_O32))
668 return 4;
669 }
670 return core_def->addr_byte_size;
671 }
Greg Clayton41f92322010-06-11 03:25:34 +0000672 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000673}
674
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000675ByteOrder
676ArchSpec::GetDefaultEndian () const
677{
Greg Clayton64195a22011-02-23 00:35:02 +0000678 const CoreDefinition *core_def = FindCoreDefinition (m_core);
679 if (core_def)
680 return core_def->default_byte_order;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000681 return eByteOrderInvalid;
682}
683
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +0000684bool
685ArchSpec::CharIsSignedByDefault () const
686{
687 switch (m_triple.getArch()) {
688 default:
689 return true;
690
691 case llvm::Triple::aarch64:
692 case llvm::Triple::aarch64_be:
693 case llvm::Triple::arm:
694 case llvm::Triple::armeb:
695 case llvm::Triple::thumb:
696 case llvm::Triple::thumbeb:
697 return m_triple.isOSDarwin() || m_triple.isOSWindows();
698
699 case llvm::Triple::ppc:
700 case llvm::Triple::ppc64:
701 return m_triple.isOSDarwin();
702
703 case llvm::Triple::ppc64le:
704 case llvm::Triple::systemz:
705 case llvm::Triple::xcore:
706 return false;
707 }
708}
709
Greg Clayton64195a22011-02-23 00:35:02 +0000710lldb::ByteOrder
711ArchSpec::GetByteOrder () const
712{
713 if (m_byte_order == eByteOrderInvalid)
714 return GetDefaultEndian();
715 return m_byte_order;
716}
717
718//===----------------------------------------------------------------------===//
719// Mutators.
720
721bool
722ArchSpec::SetTriple (const llvm::Triple &triple)
723{
724 m_triple = triple;
725
726 llvm::StringRef arch_name (m_triple.getArchName());
727 const CoreDefinition *core_def = FindCoreDefinition (arch_name);
728 if (core_def)
729 {
730 m_core = core_def->core;
Greg Claytoneb0103f2011-04-07 22:46:35 +0000731 // Set the byte order to the default byte order for an architecture.
732 // This can be modified if needed for cases when cores handle both
733 // big and little endian
734 m_byte_order = core_def->default_byte_order;
Greg Clayton64195a22011-02-23 00:35:02 +0000735 }
736 else
737 {
738 Clear();
739 }
740
Greg Clayton64195a22011-02-23 00:35:02 +0000741 return IsValid();
742}
743
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000744static bool
745ParseMachCPUDashSubtypeTriple (const char *triple_cstr, ArchSpec &arch)
746{
747 // Accept "12-10" or "12.10" as cpu type/subtype
748 if (isdigit(triple_cstr[0]))
749 {
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000750 char *end = nullptr;
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000751 errno = 0;
Greg Claytonc7bece562013-01-25 18:06:21 +0000752 uint32_t cpu = (uint32_t)::strtoul (triple_cstr, &end, 0);
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000753 if (errno == 0 && cpu != 0 && end && ((*end == '-') || (*end == '.')))
754 {
755 errno = 0;
Greg Claytonc7bece562013-01-25 18:06:21 +0000756 uint32_t sub = (uint32_t)::strtoul (end + 1, &end, 0);
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000757 if (errno == 0 && end && ((*end == '-') || (*end == '.') || (*end == '\0')))
758 {
759 if (arch.SetArchitecture (eArchTypeMachO, cpu, sub))
760 {
761 if (*end == '-')
762 {
763 llvm::StringRef vendor_os (end + 1);
764 size_t dash_pos = vendor_os.find('-');
765 if (dash_pos != llvm::StringRef::npos)
766 {
767 llvm::StringRef vendor_str(vendor_os.substr(0, dash_pos));
768 arch.GetTriple().setVendorName(vendor_str);
769 const size_t vendor_start_pos = dash_pos+1;
Greg Claytonc7bece562013-01-25 18:06:21 +0000770 dash_pos = vendor_os.find('-', vendor_start_pos);
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000771 if (dash_pos == llvm::StringRef::npos)
772 {
773 if (vendor_start_pos < vendor_os.size())
774 arch.GetTriple().setOSName(vendor_os.substr(vendor_start_pos));
775 }
776 else
777 {
778 arch.GetTriple().setOSName(vendor_os.substr(vendor_start_pos, dash_pos - vendor_start_pos));
779 }
780 }
781 }
782 return true;
783 }
784 }
785 }
786 }
787 return false;
788}
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000789
Greg Clayton64195a22011-02-23 00:35:02 +0000790bool
Greg Clayton70512312012-05-08 01:45:38 +0000791ArchSpec::SetTriple (const char *triple_cstr)
Greg Clayton64195a22011-02-23 00:35:02 +0000792{
Greg Clayton23aca092011-08-12 23:32:52 +0000793 if (triple_cstr && triple_cstr[0])
Greg Clayton64195a22011-02-23 00:35:02 +0000794 {
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000795 if (ParseMachCPUDashSubtypeTriple (triple_cstr, *this))
796 return true;
797
Greg Clayton64195a22011-02-23 00:35:02 +0000798 llvm::StringRef triple_stref (triple_cstr);
799 if (triple_stref.startswith (LLDB_ARCH_DEFAULT))
800 {
801 // Special case for the current host default architectures...
802 if (triple_stref.equals (LLDB_ARCH_DEFAULT_32BIT))
Zachary Turner13b18262014-08-20 16:42:51 +0000803 *this = HostInfo::GetArchitecture(HostInfo::eArchKind32);
Greg Clayton64195a22011-02-23 00:35:02 +0000804 else if (triple_stref.equals (LLDB_ARCH_DEFAULT_64BIT))
Zachary Turner13b18262014-08-20 16:42:51 +0000805 *this = HostInfo::GetArchitecture(HostInfo::eArchKind64);
Greg Clayton64195a22011-02-23 00:35:02 +0000806 else if (triple_stref.equals (LLDB_ARCH_DEFAULT))
Zachary Turner13b18262014-08-20 16:42:51 +0000807 *this = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
Greg Clayton64195a22011-02-23 00:35:02 +0000808 }
809 else
810 {
811 std::string normalized_triple_sstr (llvm::Triple::normalize(triple_stref));
812 triple_stref = normalized_triple_sstr;
Greg Clayton70512312012-05-08 01:45:38 +0000813 SetTriple (llvm::Triple (triple_stref));
814 }
815 }
816 else
817 Clear();
818 return IsValid();
819}
820
821bool
822ArchSpec::SetTriple (const char *triple_cstr, Platform *platform)
823{
824 if (triple_cstr && triple_cstr[0])
825 {
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000826 if (ParseMachCPUDashSubtypeTriple (triple_cstr, *this))
827 return true;
828
Greg Clayton70512312012-05-08 01:45:38 +0000829 llvm::StringRef triple_stref (triple_cstr);
830 if (triple_stref.startswith (LLDB_ARCH_DEFAULT))
831 {
832 // Special case for the current host default architectures...
833 if (triple_stref.equals (LLDB_ARCH_DEFAULT_32BIT))
Zachary Turner13b18262014-08-20 16:42:51 +0000834 *this = HostInfo::GetArchitecture(HostInfo::eArchKind32);
Greg Clayton70512312012-05-08 01:45:38 +0000835 else if (triple_stref.equals (LLDB_ARCH_DEFAULT_64BIT))
Zachary Turner13b18262014-08-20 16:42:51 +0000836 *this = HostInfo::GetArchitecture(HostInfo::eArchKind64);
Greg Clayton70512312012-05-08 01:45:38 +0000837 else if (triple_stref.equals (LLDB_ARCH_DEFAULT))
Zachary Turner13b18262014-08-20 16:42:51 +0000838 *this = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
Greg Clayton70512312012-05-08 01:45:38 +0000839 }
840 else
841 {
842 ArchSpec raw_arch (triple_cstr);
843
844 std::string normalized_triple_sstr (llvm::Triple::normalize(triple_stref));
845 triple_stref = normalized_triple_sstr;
Greg Claytoneb0103f2011-04-07 22:46:35 +0000846 llvm::Triple normalized_triple (triple_stref);
847
848 const bool os_specified = normalized_triple.getOSName().size() > 0;
849 const bool vendor_specified = normalized_triple.getVendorName().size() > 0;
850 const bool env_specified = normalized_triple.getEnvironmentName().size() > 0;
851
852 // If we got an arch only, then default the vendor, os, environment
853 // to match the platform if one is supplied
854 if (!(os_specified || vendor_specified || env_specified))
855 {
856 if (platform)
857 {
858 // If we were given a platform, use the platform's system
859 // architecture. If this is not available (might not be
860 // connected) use the first supported architecture.
Greg Clayton70512312012-05-08 01:45:38 +0000861 ArchSpec compatible_arch;
Greg Clayton1e0c8842013-01-11 20:49:54 +0000862 if (platform->IsCompatibleArchitecture (raw_arch, false, &compatible_arch))
Greg Claytoneb0103f2011-04-07 22:46:35 +0000863 {
Greg Clayton70512312012-05-08 01:45:38 +0000864 if (compatible_arch.IsValid())
865 {
866 const llvm::Triple &compatible_triple = compatible_arch.GetTriple();
867 if (!vendor_specified)
868 normalized_triple.setVendor(compatible_triple.getVendor());
869 if (!os_specified)
870 normalized_triple.setOS(compatible_triple.getOS());
871 if (!env_specified && compatible_triple.getEnvironmentName().size())
872 normalized_triple.setEnvironment(compatible_triple.getEnvironment());
873 }
Greg Claytoneb0103f2011-04-07 22:46:35 +0000874 }
Greg Clayton70512312012-05-08 01:45:38 +0000875 else
Greg Claytoneb0103f2011-04-07 22:46:35 +0000876 {
Greg Clayton70512312012-05-08 01:45:38 +0000877 *this = raw_arch;
878 return IsValid();
Greg Claytoneb0103f2011-04-07 22:46:35 +0000879 }
880 }
881 else
882 {
883 // No platform specified, fall back to the host system for
884 // the default vendor, os, and environment.
Sean Callananbfb237bc2011-11-04 22:46:46 +0000885 llvm::Triple host_triple(llvm::sys::getDefaultTargetTriple());
Greg Clayton70512312012-05-08 01:45:38 +0000886 if (!vendor_specified)
887 normalized_triple.setVendor(host_triple.getVendor());
888 if (!vendor_specified)
889 normalized_triple.setOS(host_triple.getOS());
890 if (!env_specified && host_triple.getEnvironmentName().size())
891 normalized_triple.setEnvironment(host_triple.getEnvironment());
Greg Claytoneb0103f2011-04-07 22:46:35 +0000892 }
893 }
894 SetTriple (normalized_triple);
Greg Clayton64195a22011-02-23 00:35:02 +0000895 }
896 }
897 else
898 Clear();
899 return IsValid();
900}
901
Zachary Turner5e6f4522015-01-22 18:59:05 +0000902void
903ArchSpec::MergeFrom(const ArchSpec &other)
904{
Todd Fiala7df337f2015-10-13 23:41:19 +0000905 if (TripleVendorIsUnspecifiedUnknown() && !other.TripleVendorIsUnspecifiedUnknown())
Zachary Turner5e6f4522015-01-22 18:59:05 +0000906 GetTriple().setVendor(other.GetTriple().getVendor());
Todd Fiala7df337f2015-10-13 23:41:19 +0000907 if (TripleOSIsUnspecifiedUnknown() && !other.TripleOSIsUnspecifiedUnknown())
Zachary Turner5e6f4522015-01-22 18:59:05 +0000908 GetTriple().setOS(other.GetTriple().getOS());
909 if (GetTriple().getArch() == llvm::Triple::UnknownArch)
910 GetTriple().setArch(other.GetTriple().getArch());
Jason Molenda03fe45e2015-11-06 01:43:36 +0000911 if (GetTriple().getEnvironment() == llvm::Triple::UnknownEnvironment && !TripleVendorWasSpecified())
912 {
913 if (other.TripleVendorWasSpecified())
914 GetTriple().setEnvironment(other.GetTriple().getEnvironment());
915 }
Jason Molenda583b1a82016-04-05 05:01:30 +0000916 // If this and other are both arm ArchSpecs and this ArchSpec is a generic "some kind of arm"
917 // spec but the other ArchSpec is a specific arm core, adopt the specific arm core.
918 if (GetTriple().getArch() == llvm::Triple::arm
919 && other.GetTriple().getArch() == llvm::Triple::arm
920 && IsCompatibleMatch (other)
921 && GetCore() == ArchSpec::eCore_arm_generic
922 && other.GetCore() != ArchSpec::eCore_arm_generic)
923 {
924 m_core = other.GetCore();
925 CoreUpdated (true);
926 }
Zachary Turner5e6f4522015-01-22 18:59:05 +0000927}
928
Greg Clayton64195a22011-02-23 00:35:02 +0000929bool
Ed Mastef6a13122015-06-05 13:03:08 +0000930ArchSpec::SetArchitecture (ArchitectureType arch_type, uint32_t cpu, uint32_t sub, uint32_t os)
Greg Clayton64195a22011-02-23 00:35:02 +0000931{
932 m_core = kCore_invalid;
933 bool update_triple = true;
934 const ArchDefinition *arch_def = FindArchDefinition(arch_type);
935 if (arch_def)
936 {
937 const ArchDefinitionEntry *arch_def_entry = FindArchDefinitionEntry (arch_def, cpu, sub);
938 if (arch_def_entry)
939 {
940 const CoreDefinition *core_def = FindCoreDefinition (arch_def_entry->core);
941 if (core_def)
942 {
943 m_core = core_def->core;
944 update_triple = false;
Greg Clayton593577a2011-09-21 03:57:31 +0000945 // Always use the architecture name because it might be more descriptive
946 // than the architecture enum ("armv7" -> llvm::Triple::arm).
947 m_triple.setArchName(llvm::StringRef(core_def->name));
Greg Clayton64195a22011-02-23 00:35:02 +0000948 if (arch_type == eArchTypeMachO)
949 {
950 m_triple.setVendor (llvm::Triple::Apple);
Greg Claytona3a6c122014-07-29 18:04:57 +0000951
Jason Molenda03fe45e2015-11-06 01:43:36 +0000952 // Don't set the OS. It could be simulator, macosx, ios, watchos, tvos. We could
953 // get close with the cpu type - but we can't get it right all of the time. Better
954 // to leave this unset so other sections of code will set it when they have more
955 // information.
956 // NB: don't call m_triple.setOS (llvm::Triple::UnknownOS). That sets the OSName to
957 // "unknown" and the ArchSpec::TripleVendorWasSpecified() method says that any
958 // OSName setting means it was specified.
Greg Clayton64195a22011-02-23 00:35:02 +0000959 }
Ed Mastef6a13122015-06-05 13:03:08 +0000960 else if (arch_type == eArchTypeELF)
961 {
Ed Mastef6a13122015-06-05 13:03:08 +0000962 switch (os)
963 {
Tamas Berghammered1fa202015-07-07 09:11:59 +0000964 case llvm::ELF::ELFOSABI_AIX: m_triple.setOS (llvm::Triple::OSType::AIX); break;
965 case llvm::ELF::ELFOSABI_FREEBSD: m_triple.setOS (llvm::Triple::OSType::FreeBSD); break;
966 case llvm::ELF::ELFOSABI_GNU: m_triple.setOS (llvm::Triple::OSType::Linux); break;
967 case llvm::ELF::ELFOSABI_NETBSD: m_triple.setOS (llvm::Triple::OSType::NetBSD); break;
968 case llvm::ELF::ELFOSABI_OPENBSD: m_triple.setOS (llvm::Triple::OSType::OpenBSD); break;
969 case llvm::ELF::ELFOSABI_SOLARIS: m_triple.setOS (llvm::Triple::OSType::Solaris); break;
Ed Mastef6a13122015-06-05 13:03:08 +0000970 }
Ed Mastef6a13122015-06-05 13:03:08 +0000971 }
Jason Molenda03fe45e2015-11-06 01:43:36 +0000972 else
973 {
974 m_triple.setVendor (llvm::Triple::UnknownVendor);
975 m_triple.setOS (llvm::Triple::UnknownOS);
976 }
Greg Clayton593577a2011-09-21 03:57:31 +0000977 // Fall back onto setting the machine type if the arch by name failed...
978 if (m_triple.getArch () == llvm::Triple::UnknownArch)
979 m_triple.setArch (core_def->machine);
Greg Clayton64195a22011-02-23 00:35:02 +0000980 }
981 }
982 }
983 CoreUpdated(update_triple);
984 return IsValid();
985}
986
Greg Clayton357132e2011-03-26 19:14:58 +0000987uint32_t
988ArchSpec::GetMinimumOpcodeByteSize() const
Greg Clayton64195a22011-02-23 00:35:02 +0000989{
Greg Clayton357132e2011-03-26 19:14:58 +0000990 const CoreDefinition *core_def = FindCoreDefinition (m_core);
991 if (core_def)
992 return core_def->min_opcode_byte_size;
993 return 0;
994}
995
996uint32_t
997ArchSpec::GetMaximumOpcodeByteSize() const
998{
999 const CoreDefinition *core_def = FindCoreDefinition (m_core);
1000 if (core_def)
1001 return core_def->max_opcode_byte_size;
1002 return 0;
Greg Clayton64195a22011-02-23 00:35:02 +00001003}
1004
Jason Molendaba813dc2012-11-04 03:20:05 +00001005bool
1006ArchSpec::IsExactMatch (const ArchSpec& rhs) const
1007{
Sean Callananbf4b7be2012-12-13 22:07:14 +00001008 return IsEqualTo (rhs, true);
Jason Molendaba813dc2012-11-04 03:20:05 +00001009}
1010
1011bool
1012ArchSpec::IsCompatibleMatch (const ArchSpec& rhs) const
1013{
Sean Callananbf4b7be2012-12-13 22:07:14 +00001014 return IsEqualTo (rhs, false);
Jason Molendaba813dc2012-11-04 03:20:05 +00001015}
1016
Tamas Berghammer0237eda2016-04-25 15:51:45 +00001017static bool
1018isCompatibleEnvironment(llvm::Triple::EnvironmentType lhs, llvm::Triple::EnvironmentType rhs)
1019{
1020 if (lhs == rhs)
1021 return true;
1022
1023 // If any of the environment is unknown then they are compatible
1024 if (lhs == llvm::Triple::UnknownEnvironment || rhs == llvm::Triple::UnknownEnvironment)
1025 return true;
1026
1027 // If one of the environment is Android and the other one is EABI then they are considered to
1028 // be compatible. This is required as a workaround for shared libraries compiled for Android
1029 // without the NOTE section indicating that they are using the Android ABI.
1030 if ((lhs == llvm::Triple::Android && rhs == llvm::Triple::EABI) ||
Omair Javaid9a1699c2016-04-26 01:08:59 +00001031 (rhs == llvm::Triple::Android && lhs == llvm::Triple::EABI) ||
1032 (lhs == llvm::Triple::GNUEABI && rhs == llvm::Triple::EABI) ||
1033 (rhs == llvm::Triple::GNUEABI && lhs == llvm::Triple::EABI) ||
1034 (lhs == llvm::Triple::GNUEABIHF && rhs == llvm::Triple::EABIHF) ||
1035 (rhs == llvm::Triple::GNUEABIHF && lhs == llvm::Triple::EABIHF))
Tamas Berghammer0237eda2016-04-25 15:51:45 +00001036 return true;
1037
1038 return false;
1039}
1040
Jason Molendaba813dc2012-11-04 03:20:05 +00001041bool
Sean Callananbf4b7be2012-12-13 22:07:14 +00001042ArchSpec::IsEqualTo (const ArchSpec& rhs, bool exact_match) const
Jason Molendaba813dc2012-11-04 03:20:05 +00001043{
Todd Fialaa9ddb0e2014-01-18 03:02:39 +00001044 // explicitly ignoring m_distribution_id in this method.
1045
Jason Molendaba813dc2012-11-04 03:20:05 +00001046 if (GetByteOrder() != rhs.GetByteOrder())
1047 return false;
1048
1049 const ArchSpec::Core lhs_core = GetCore ();
1050 const ArchSpec::Core rhs_core = rhs.GetCore ();
1051
1052 const bool core_match = cores_match (lhs_core, rhs_core, true, exact_match);
1053
1054 if (core_match)
1055 {
1056 const llvm::Triple &lhs_triple = GetTriple();
1057 const llvm::Triple &rhs_triple = rhs.GetTriple();
1058
1059 const llvm::Triple::VendorType lhs_triple_vendor = lhs_triple.getVendor();
1060 const llvm::Triple::VendorType rhs_triple_vendor = rhs_triple.getVendor();
1061 if (lhs_triple_vendor != rhs_triple_vendor)
1062 {
Jason Molenda03fe45e2015-11-06 01:43:36 +00001063 const bool rhs_vendor_specified = rhs.TripleVendorWasSpecified();
1064 const bool lhs_vendor_specified = TripleVendorWasSpecified();
1065 // Both architectures had the vendor specified, so if they aren't
1066 // equal then we return false
1067 if (rhs_vendor_specified && lhs_vendor_specified)
1068 return false;
Jason Molendaba813dc2012-11-04 03:20:05 +00001069
1070 // Only fail if both vendor types are not unknown
1071 if (lhs_triple_vendor != llvm::Triple::UnknownVendor &&
1072 rhs_triple_vendor != llvm::Triple::UnknownVendor)
1073 return false;
1074 }
1075
1076 const llvm::Triple::OSType lhs_triple_os = lhs_triple.getOS();
1077 const llvm::Triple::OSType rhs_triple_os = rhs_triple.getOS();
1078 if (lhs_triple_os != rhs_triple_os)
1079 {
Jason Molenda03fe45e2015-11-06 01:43:36 +00001080 const bool rhs_os_specified = rhs.TripleOSWasSpecified();
1081 const bool lhs_os_specified = TripleOSWasSpecified();
1082 // Both architectures had the OS specified, so if they aren't
1083 // equal then we return false
1084 if (rhs_os_specified && lhs_os_specified)
1085 return false;
Greg Clayton7ab7f892014-05-29 21:33:45 +00001086
Greg Clayton3f19ada2014-07-10 23:33:37 +00001087 // Only fail if both os types are not unknown
1088 if (lhs_triple_os != llvm::Triple::UnknownOS &&
1089 rhs_triple_os != llvm::Triple::UnknownOS)
1090 return false;
Jason Molendaba813dc2012-11-04 03:20:05 +00001091 }
1092
1093 const llvm::Triple::EnvironmentType lhs_triple_env = lhs_triple.getEnvironment();
1094 const llvm::Triple::EnvironmentType rhs_triple_env = rhs_triple.getEnvironment();
Tamas Berghammer0237eda2016-04-25 15:51:45 +00001095
1096 if (!isCompatibleEnvironment(lhs_triple_env, rhs_triple_env))
1097 return false;
Jason Molendaba813dc2012-11-04 03:20:05 +00001098 return true;
1099 }
1100 return false;
1101}
1102
Greg Clayton64195a22011-02-23 00:35:02 +00001103//===----------------------------------------------------------------------===//
1104// Helper methods.
1105
1106void
1107ArchSpec::CoreUpdated (bool update_triple)
1108{
1109 const CoreDefinition *core_def = FindCoreDefinition (m_core);
1110 if (core_def)
1111 {
1112 if (update_triple)
1113 m_triple = llvm::Triple(core_def->name, "unknown", "unknown");
1114 m_byte_order = core_def->default_byte_order;
1115 }
1116 else
1117 {
1118 if (update_triple)
1119 m_triple = llvm::Triple();
1120 m_byte_order = eByteOrderInvalid;
1121 }
1122}
1123
1124//===----------------------------------------------------------------------===//
1125// Operators.
1126
Greg Clayton70512312012-05-08 01:45:38 +00001127static bool
Jason Molendaba813dc2012-11-04 03:20:05 +00001128cores_match (const ArchSpec::Core core1, const ArchSpec::Core core2, bool try_inverse, bool enforce_exact_match)
Greg Clayton70512312012-05-08 01:45:38 +00001129{
Jason Molendaba813dc2012-11-04 03:20:05 +00001130 if (core1 == core2)
1131 return true;
1132
Greg Clayton70512312012-05-08 01:45:38 +00001133 switch (core1)
1134 {
Greg Clayton70512312012-05-08 01:45:38 +00001135 case ArchSpec::kCore_any:
1136 return true;
1137
Greg Clayton44362e02014-07-12 00:11:34 +00001138 case ArchSpec::eCore_arm_generic:
1139 if (enforce_exact_match)
1140 break;
Jason Molenda62e06812016-02-16 04:14:33 +00001141 LLVM_FALLTHROUGH;
Greg Clayton70512312012-05-08 01:45:38 +00001142 case ArchSpec::kCore_arm_any:
1143 if (core2 >= ArchSpec::kCore_arm_first && core2 <= ArchSpec::kCore_arm_last)
1144 return true;
1145 if (core2 >= ArchSpec::kCore_thumb_first && core2 <= ArchSpec::kCore_thumb_last)
1146 return true;
1147 if (core2 == ArchSpec::kCore_arm_any)
1148 return true;
1149 break;
Sylvestre Ledru1c9e0642014-08-18 14:53:42 +00001150
Greg Clayton70512312012-05-08 01:45:38 +00001151 case ArchSpec::kCore_x86_32_any:
1152 if ((core2 >= ArchSpec::kCore_x86_32_first && core2 <= ArchSpec::kCore_x86_32_last) || (core2 == ArchSpec::kCore_x86_32_any))
1153 return true;
1154 break;
Zachary Turnerad587ae42014-07-28 16:44:49 +00001155
1156 case ArchSpec::kCore_x86_64_any:
1157 if ((core2 >= ArchSpec::kCore_x86_64_first && core2 <= ArchSpec::kCore_x86_64_last) || (core2 == ArchSpec::kCore_x86_64_any))
1158 return true;
Sylvestre Ledru1c9e0642014-08-18 14:53:42 +00001159 break;
Zachary Turnerad587ae42014-07-28 16:44:49 +00001160
Greg Clayton70512312012-05-08 01:45:38 +00001161 case ArchSpec::kCore_ppc_any:
1162 if ((core2 >= ArchSpec::kCore_ppc_first && core2 <= ArchSpec::kCore_ppc_last) || (core2 == ArchSpec::kCore_ppc_any))
1163 return true;
1164 break;
Sylvestre Ledru1c9e0642014-08-18 14:53:42 +00001165
Greg Clayton70512312012-05-08 01:45:38 +00001166 case ArchSpec::kCore_ppc64_any:
1167 if ((core2 >= ArchSpec::kCore_ppc64_first && core2 <= ArchSpec::kCore_ppc64_last) || (core2 == ArchSpec::kCore_ppc64_any))
1168 return true;
1169 break;
1170
Jason Molendaa3a04522013-09-27 23:21:54 +00001171 case ArchSpec::eCore_arm_armv6m:
1172 if (!enforce_exact_match)
1173 {
Greg Clayton44362e02014-07-12 00:11:34 +00001174 if (core2 == ArchSpec::eCore_arm_generic)
1175 return true;
Jason Molendaa3a04522013-09-27 23:21:54 +00001176 try_inverse = false;
Jason Molendac7cda272013-09-27 23:29:10 +00001177 if (core2 == ArchSpec::eCore_arm_armv7)
Jason Molendaa3a04522013-09-27 23:21:54 +00001178 return true;
Jason Molendad607afd2015-06-25 22:37:57 +00001179 if (core2 == ArchSpec::eCore_arm_armv6m)
1180 return true;
Jason Molendaa3a04522013-09-27 23:21:54 +00001181 }
Sylvestre Ledru1c9e0642014-08-18 14:53:42 +00001182 break;
Deepak Panickal6d3df422014-02-19 11:16:46 +00001183
1184 case ArchSpec::kCore_hexagon_any:
1185 if ((core2 >= ArchSpec::kCore_hexagon_first && core2 <= ArchSpec::kCore_hexagon_last) || (core2 == ArchSpec::kCore_hexagon_any))
1186 return true;
Jason Molendaa3a04522013-09-27 23:21:54 +00001187 break;
1188
Jason Molenda8825c5c2015-10-08 21:48:35 +00001189 // v. https://en.wikipedia.org/wiki/ARM_Cortex-M#Silicon_customization
1190 // Cortex-M0 - ARMv6-M - armv6m
1191 // Cortex-M3 - ARMv7-M - armv7m
1192 // Cortex-M4 - ARMv7E-M - armv7em
Jason Molenda7a1559c2013-03-08 01:20:17 +00001193 case ArchSpec::eCore_arm_armv7em:
Jason Molendad607afd2015-06-25 22:37:57 +00001194 if (!enforce_exact_match)
1195 {
1196 if (core2 == ArchSpec::eCore_arm_generic)
1197 return true;
1198 if (core2 == ArchSpec::eCore_arm_armv7m)
1199 return true;
1200 if (core2 == ArchSpec::eCore_arm_armv6m)
1201 return true;
1202 if (core2 == ArchSpec::eCore_arm_armv7)
1203 return true;
1204 try_inverse = true;
1205 }
1206 break;
1207
Jason Molenda8825c5c2015-10-08 21:48:35 +00001208 // v. https://en.wikipedia.org/wiki/ARM_Cortex-M#Silicon_customization
1209 // Cortex-M0 - ARMv6-M - armv6m
1210 // Cortex-M3 - ARMv7-M - armv7m
1211 // Cortex-M4 - ARMv7E-M - armv7em
Jason Molendad607afd2015-06-25 22:37:57 +00001212 case ArchSpec::eCore_arm_armv7m:
1213 if (!enforce_exact_match)
1214 {
1215 if (core2 == ArchSpec::eCore_arm_generic)
1216 return true;
1217 if (core2 == ArchSpec::eCore_arm_armv6m)
1218 return true;
1219 if (core2 == ArchSpec::eCore_arm_armv7)
1220 return true;
1221 if (core2 == ArchSpec::eCore_arm_armv7em)
1222 return true;
1223 try_inverse = true;
1224 }
1225 break;
1226
Johnny Chen1083b0d2012-08-28 22:53:40 +00001227 case ArchSpec::eCore_arm_armv7f:
1228 case ArchSpec::eCore_arm_armv7k:
1229 case ArchSpec::eCore_arm_armv7s:
Jason Molendaba813dc2012-11-04 03:20:05 +00001230 if (!enforce_exact_match)
1231 {
Greg Clayton44362e02014-07-12 00:11:34 +00001232 if (core2 == ArchSpec::eCore_arm_generic)
1233 return true;
Jason Molendaba813dc2012-11-04 03:20:05 +00001234 if (core2 == ArchSpec::eCore_arm_armv7)
1235 return true;
Greg Clayton44362e02014-07-12 00:11:34 +00001236 try_inverse = false;
Jason Molendaba813dc2012-11-04 03:20:05 +00001237 }
Johnny Chen1083b0d2012-08-28 22:53:40 +00001238 break;
Sylvestre Ledru1c9e0642014-08-18 14:53:42 +00001239
Greg Clayton52edb362014-07-14 22:53:02 +00001240 case ArchSpec::eCore_x86_64_x86_64h:
1241 if (!enforce_exact_match)
1242 {
1243 try_inverse = false;
1244 if (core2 == ArchSpec::eCore_x86_64_x86_64)
1245 return true;
1246 }
1247 break;
Johnny Chen1083b0d2012-08-28 22:53:40 +00001248
Todd Fiala02e71812014-08-28 14:32:43 +00001249 case ArchSpec::eCore_arm_armv8:
1250 if (!enforce_exact_match)
1251 {
1252 if (core2 == ArchSpec::eCore_arm_arm64)
1253 return true;
1254 if (core2 == ArchSpec::eCore_arm_aarch64)
1255 return true;
1256 try_inverse = false;
1257 }
1258 break;
1259
1260 case ArchSpec::eCore_arm_aarch64:
1261 if (!enforce_exact_match)
1262 {
1263 if (core2 == ArchSpec::eCore_arm_arm64)
1264 return true;
1265 if (core2 == ArchSpec::eCore_arm_armv8)
1266 return true;
1267 try_inverse = false;
1268 }
1269 break;
1270
1271 case ArchSpec::eCore_arm_arm64:
1272 if (!enforce_exact_match)
1273 {
1274 if (core2 == ArchSpec::eCore_arm_aarch64)
1275 return true;
1276 if (core2 == ArchSpec::eCore_arm_armv8)
1277 return true;
1278 try_inverse = false;
1279 }
1280 break;
1281
Sagar Thakur6bee9612015-07-13 09:52:06 +00001282 case ArchSpec::eCore_mips32:
1283 if (!enforce_exact_match)
1284 {
1285 if (core2 >= ArchSpec::kCore_mips32_first && core2 <= ArchSpec::kCore_mips32_last)
1286 return true;
1287 try_inverse = false;
1288 }
1289 break;
1290
1291 case ArchSpec::eCore_mips32el:
1292 if (!enforce_exact_match)
1293 {
1294 if (core2 >= ArchSpec::kCore_mips32el_first && core2 <= ArchSpec::kCore_mips32el_last)
1295 return true;
1296 try_inverse = false;
1297 }
Greg Claytoncec91ef2016-02-26 01:20:20 +00001298 break;
Sagar Thakur6bee9612015-07-13 09:52:06 +00001299
Sagar Thakurce815e42015-06-03 10:14:24 +00001300 case ArchSpec::eCore_mips64:
Sagar Thakur6bee9612015-07-13 09:52:06 +00001301 if (!enforce_exact_match)
1302 {
1303 if (core2 >= ArchSpec::kCore_mips32_first && core2 <= ArchSpec::kCore_mips32_last)
1304 return true;
1305 if (core2 >= ArchSpec::kCore_mips64_first && core2 <= ArchSpec::kCore_mips64_last)
1306 return true;
1307 try_inverse = false;
1308 }
Greg Claytoncec91ef2016-02-26 01:20:20 +00001309 break;
Sagar Thakur6bee9612015-07-13 09:52:06 +00001310
1311 case ArchSpec::eCore_mips64el:
1312 if (!enforce_exact_match)
1313 {
1314 if (core2 >= ArchSpec::kCore_mips32el_first && core2 <= ArchSpec::kCore_mips32el_last)
1315 return true;
1316 if (core2 >= ArchSpec::kCore_mips64el_first && core2 <= ArchSpec::kCore_mips64el_last)
1317 return true;
1318 try_inverse = false;
1319 }
Greg Claytoncec91ef2016-02-26 01:20:20 +00001320 break;
Sagar Thakur6bee9612015-07-13 09:52:06 +00001321
Sagar Thakurce815e42015-06-03 10:14:24 +00001322 case ArchSpec::eCore_mips64r2:
1323 case ArchSpec::eCore_mips64r3:
1324 case ArchSpec::eCore_mips64r5:
Sagar Thakurce815e42015-06-03 10:14:24 +00001325 if (!enforce_exact_match)
1326 {
1327 if (core2 >= ArchSpec::kCore_mips32_first && core2 <= (core1 - 10))
1328 return true;
1329 if (core2 >= ArchSpec::kCore_mips64_first && core2 <= (core1 - 1))
1330 return true;
1331 try_inverse = false;
1332 }
1333 break;
1334
Sagar Thakurce815e42015-06-03 10:14:24 +00001335 case ArchSpec::eCore_mips64r2el:
1336 case ArchSpec::eCore_mips64r3el:
1337 case ArchSpec::eCore_mips64r5el:
Sagar Thakurce815e42015-06-03 10:14:24 +00001338 if (!enforce_exact_match)
1339 {
1340 if (core2 >= ArchSpec::kCore_mips32el_first && core2 <= (core1 - 10))
1341 return true;
1342 if (core2 >= ArchSpec::kCore_mips64el_first && core2 <= (core1 - 1))
1343 return true;
1344 try_inverse = false;
1345 }
1346 break;
1347
Sagar Thakur6bee9612015-07-13 09:52:06 +00001348 case ArchSpec::eCore_mips32r2:
1349 case ArchSpec::eCore_mips32r3:
1350 case ArchSpec::eCore_mips32r5:
1351 if (!enforce_exact_match)
1352 {
1353 if (core2 >= ArchSpec::kCore_mips32_first && core2 <= core1)
1354 return true;
1355 }
1356 break;
1357
1358 case ArchSpec::eCore_mips32r2el:
1359 case ArchSpec::eCore_mips32r3el:
1360 case ArchSpec::eCore_mips32r5el:
1361 if (!enforce_exact_match)
1362 {
1363 if (core2 >= ArchSpec::kCore_mips32el_first && core2 <= core1)
1364 return true;
1365 }
1366 break;
1367
1368 case ArchSpec::eCore_mips32r6:
1369 if (!enforce_exact_match)
1370 {
1371 if (core2 == ArchSpec::eCore_mips32 || core2 == ArchSpec::eCore_mips32r6)
1372 return true;
1373 }
1374 break;
1375
1376 case ArchSpec::eCore_mips32r6el:
1377 if (!enforce_exact_match)
1378 {
1379 if (core2 == ArchSpec::eCore_mips32el || core2 == ArchSpec::eCore_mips32r6el)
1380 return true;
Sagar Thakur6bee9612015-07-13 09:52:06 +00001381 }
1382 break;
1383
1384 case ArchSpec::eCore_mips64r6:
1385 if (!enforce_exact_match)
1386 {
1387 if (core2 == ArchSpec::eCore_mips32 || core2 == ArchSpec::eCore_mips32r6)
1388 return true;
1389 if (core2 == ArchSpec::eCore_mips64 || core2 == ArchSpec::eCore_mips64r6)
1390 return true;
1391 }
1392 break;
1393
1394 case ArchSpec::eCore_mips64r6el:
1395 if (!enforce_exact_match)
1396 {
1397 if (core2 == ArchSpec::eCore_mips32el || core2 == ArchSpec::eCore_mips32r6el)
1398 return true;
1399 if (core2 == ArchSpec::eCore_mips64el || core2 == ArchSpec::eCore_mips64r6el)
1400 return true;
1401 }
1402 break;
1403
Greg Clayton70512312012-05-08 01:45:38 +00001404 default:
1405 break;
1406 }
1407 if (try_inverse)
Jason Molendaba813dc2012-11-04 03:20:05 +00001408 return cores_match (core2, core1, false, enforce_exact_match);
Greg Clayton70512312012-05-08 01:45:38 +00001409 return false;
1410}
1411
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001412bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001413lldb_private::operator<(const ArchSpec& lhs, const ArchSpec& rhs)
1414{
Greg Clayton64195a22011-02-23 00:35:02 +00001415 const ArchSpec::Core lhs_core = lhs.GetCore ();
1416 const ArchSpec::Core rhs_core = rhs.GetCore ();
1417 return lhs_core < rhs_core;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001418}
Greg Claytona97c4d22014-12-09 23:31:02 +00001419
1420static void
1421StopInfoOverrideCallbackTypeARM(lldb_private::Thread &thread)
1422{
1423 // We need to check if we are stopped in Thumb mode in a IT instruction
1424 // and detect if the condition doesn't pass. If this is the case it means
1425 // we won't actually execute this instruction. If this happens we need to
1426 // clear the stop reason to no thread plans think we are stopped for a
1427 // reason and the plans should keep going.
1428 //
1429 // We do this because when single stepping many ARM processes, debuggers
1430 // often use the BVR/BCR registers that says "stop when the PC is not
1431 // equal to its current value". This method of stepping means we can end
1432 // up stopping on instructions inside an if/then block that wouldn't get
1433 // executed. By fixing this we can stop the debugger from seeming like
1434 // you stepped through both the "if" _and_ the "else" clause when source
1435 // level stepping because the debugger stops regardless due to the BVR/BCR
1436 // triggering a stop.
1437 //
1438 // It also means we can set breakpoints on instructions inside an an
1439 // if/then block and correctly skip them if we use the BKPT instruction.
1440 // The ARM and Thumb BKPT instructions are unconditional even when executed
1441 // in a Thumb IT block.
1442 //
1443 // If your debugger inserts software traps in ARM/Thumb code, it will
1444 // need to use 16 and 32 bit instruction for 16 and 32 bit thumb
1445 // instructions respectively. If your debugger inserts a 16 bit thumb
1446 // trap on top of a 32 bit thumb instruction for an opcode that is inside
1447 // an if/then, it will change the it/then to conditionally execute your
1448 // 16 bit trap and then cause your program to crash if it executes the
1449 // trailing 16 bits (the second half of the 32 bit thumb instruction you
1450 // partially overwrote).
1451
1452 RegisterContextSP reg_ctx_sp (thread.GetRegisterContext());
1453 if (reg_ctx_sp)
1454 {
1455 const uint32_t cpsr = reg_ctx_sp->GetFlags(0);
1456 if (cpsr != 0)
1457 {
1458 // Read the J and T bits to get the ISETSTATE
1459 const uint32_t J = Bit32(cpsr, 24);
1460 const uint32_t T = Bit32(cpsr, 5);
1461 const uint32_t ISETSTATE = J << 1 | T;
1462 if (ISETSTATE == 0)
1463 {
1464 // NOTE: I am pretty sure we want to enable the code below
1465 // that detects when we stop on an instruction in ARM mode
1466 // that is conditional and the condition doesn't pass. This
1467 // can happen if you set a breakpoint on an instruction that
1468 // is conditional. We currently will _always_ stop on the
1469 // instruction which is bad. You can also run into this while
1470 // single stepping and you could appear to run code in the "if"
1471 // and in the "else" clause because it would stop at all of the
1472 // conditional instructions in both.
1473 // In such cases, we really don't want to stop at this location.
1474 // I will check with the lldb-dev list first before I enable this.
1475#if 0
1476 // ARM mode: check for condition on intsruction
1477 const addr_t pc = reg_ctx_sp->GetPC();
1478 Error error;
1479 // If we fail to read the opcode we will get UINT64_MAX as the
1480 // result in "opcode" which we can use to detect if we read a
1481 // valid opcode.
1482 const uint64_t opcode = thread.GetProcess()->ReadUnsignedIntegerFromMemory(pc, 4, UINT64_MAX, error);
1483 if (opcode <= UINT32_MAX)
1484 {
1485 const uint32_t condition = Bits32((uint32_t)opcode, 31, 28);
Eugene Zelenko896ddd02016-03-02 01:09:03 +00001486 if (!ARMConditionPassed(condition, cpsr))
Greg Claytona97c4d22014-12-09 23:31:02 +00001487 {
1488 // We ARE stopped on an ARM instruction whose condition doesn't
1489 // pass so this instruction won't get executed.
1490 // Regardless of why it stopped, we need to clear the stop info
1491 thread.SetStopInfo (StopInfoSP());
1492 }
1493 }
1494#endif
1495 }
1496 else if (ISETSTATE == 1)
1497 {
1498 // Thumb mode
1499 const uint32_t ITSTATE = Bits32 (cpsr, 15, 10) << 2 | Bits32 (cpsr, 26, 25);
1500 if (ITSTATE != 0)
1501 {
1502 const uint32_t condition = Bits32(ITSTATE, 7, 4);
Eugene Zelenko896ddd02016-03-02 01:09:03 +00001503 if (!ARMConditionPassed(condition, cpsr))
Greg Claytona97c4d22014-12-09 23:31:02 +00001504 {
1505 // We ARE stopped in a Thumb IT instruction on an instruction whose
1506 // condition doesn't pass so this instruction won't get executed.
1507 // Regardless of why it stopped, we need to clear the stop info
1508 thread.SetStopInfo (StopInfoSP());
1509 }
1510 }
1511 }
1512 }
1513 }
1514}
1515
1516ArchSpec::StopInfoOverrideCallbackType
1517ArchSpec::GetStopInfoOverrideCallback () const
1518{
1519 const llvm::Triple::ArchType machine = GetMachine();
1520 if (machine == llvm::Triple::arm)
1521 return StopInfoOverrideCallbackTypeARM;
Eugene Zelenko896ddd02016-03-02 01:09:03 +00001522 return nullptr;
Greg Claytona97c4d22014-12-09 23:31:02 +00001523}
Todd Fiala7df337f2015-10-13 23:41:19 +00001524
Jason Molenda03fe45e2015-11-06 01:43:36 +00001525bool
1526ArchSpec::IsFullySpecifiedTriple () const
1527{
1528 const auto& user_specified_triple = GetTriple();
1529
1530 bool user_triple_fully_specified = false;
1531
1532 if ((user_specified_triple.getOS() != llvm::Triple::UnknownOS) || TripleOSWasSpecified())
1533 {
1534 if ((user_specified_triple.getVendor() != llvm::Triple::UnknownVendor) || TripleVendorWasSpecified())
1535 {
1536 const unsigned unspecified = 0;
1537 if (user_specified_triple.getOSMajorVersion() != unspecified)
1538 {
1539 user_triple_fully_specified = true;
1540 }
1541 }
1542 }
1543
1544 return user_triple_fully_specified;
1545}
1546
1547void
1548ArchSpec::PiecewiseTripleCompare (const ArchSpec &other,
1549 bool &arch_different,
1550 bool &vendor_different,
1551 bool &os_different,
1552 bool &os_version_different,
1553 bool &env_different)
1554{
1555 const llvm::Triple &me(GetTriple());
1556 const llvm::Triple &them(other.GetTriple());
1557
1558 arch_different = (me.getArch() != them.getArch());
1559
1560 vendor_different = (me.getVendor() != them.getVendor());
1561
1562 os_different = (me.getOS() != them.getOS());
1563
1564 os_version_different = (me.getOSMajorVersion() != them.getOSMajorVersion());
1565
1566 env_different = (me.getEnvironment() != them.getEnvironment());
1567}
1568
Jason Molenda583b1a82016-04-05 05:01:30 +00001569bool
1570ArchSpec::IsAlwaysThumbInstructions () const
1571{
1572 std::string Error;
1573 if (GetTriple().getArch() == llvm::Triple::arm || GetTriple().getArch() == llvm::Triple::thumb)
1574 {
1575 // v. https://en.wikipedia.org/wiki/ARM_Cortex-M
1576 //
1577 // Cortex-M0 through Cortex-M7 are ARM processor cores which can only
1578 // execute thumb instructions. We map the cores to arch names like this:
1579 //
1580 // Cortex-M0, Cortex-M0+, Cortex-M1: armv6m
1581 // Cortex-M3: armv7m
1582 // Cortex-M4, Cortex-M7: armv7em
1583
1584 if (GetCore() == ArchSpec::Core::eCore_arm_armv7m
1585 || GetCore() == ArchSpec::Core::eCore_arm_armv7em
1586 || GetCore() == ArchSpec::Core::eCore_arm_armv6m)
1587 {
1588 return true;
1589 }
1590 }
1591 return false;
1592}
1593
Todd Fiala7df337f2015-10-13 23:41:19 +00001594void
1595ArchSpec::DumpTriple(Stream &s) const
1596{
1597 const llvm::Triple &triple = GetTriple();
1598 llvm::StringRef arch_str = triple.getArchName();
1599 llvm::StringRef vendor_str = triple.getVendorName();
1600 llvm::StringRef os_str = triple.getOSName();
Stephane Sezerbef0ff82016-04-05 17:29:19 +00001601 llvm::StringRef environ_str = triple.getEnvironmentName();
Todd Fiala7df337f2015-10-13 23:41:19 +00001602
1603 s.Printf("%s-%s-%s",
1604 arch_str.empty() ? "*" : arch_str.str().c_str(),
1605 vendor_str.empty() ? "*" : vendor_str.str().c_str(),
1606 os_str.empty() ? "*" : os_str.str().c_str()
1607 );
Stephane Sezerbef0ff82016-04-05 17:29:19 +00001608
1609 if (!environ_str.empty())
1610 s.Printf("-%s", environ_str.str().c_str());
Todd Fiala7df337f2015-10-13 23:41:19 +00001611}