blob: 4c5971935ef2a0f37a3341abe0c495850d030d27 [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
Bhushan D. Attarde3592a6e2016-02-18 11:53:28 +0000510std::string
511ArchSpec::GetClangTargetCPU ()
512{
513 std::string cpu;
514 const llvm::Triple::ArchType machine = GetMachine();
515
516 if (machine == llvm::Triple::mips ||
517 machine == llvm::Triple::mipsel ||
518 machine == llvm::Triple::mips64 ||
519 machine == llvm::Triple::mips64el)
520 {
521 switch (m_core)
522 {
523 case ArchSpec::eCore_mips32:
524 case ArchSpec::eCore_mips32el:
525 cpu = "mips32"; break;
526 case ArchSpec::eCore_mips32r2:
527 case ArchSpec::eCore_mips32r2el:
528 cpu = "mips32r2"; break;
529 case ArchSpec::eCore_mips32r3:
530 case ArchSpec::eCore_mips32r3el:
531 cpu = "mips32r3"; break;
532 case ArchSpec::eCore_mips32r5:
533 case ArchSpec::eCore_mips32r5el:
534 cpu = "mips32r5"; break;
535 case ArchSpec::eCore_mips32r6:
536 case ArchSpec::eCore_mips32r6el:
537 cpu = "mips32r6"; break;
538 case ArchSpec::eCore_mips64:
539 case ArchSpec::eCore_mips64el:
540 cpu = "mips64"; break;
541 case ArchSpec::eCore_mips64r2:
542 case ArchSpec::eCore_mips64r2el:
543 cpu = "mips64r2"; break;
544 case ArchSpec::eCore_mips64r3:
545 case ArchSpec::eCore_mips64r3el:
546 cpu = "mips64r3"; break;
547 case ArchSpec::eCore_mips64r5:
548 case ArchSpec::eCore_mips64r5el:
549 cpu = "mips64r5"; break;
550 case ArchSpec::eCore_mips64r6:
551 case ArchSpec::eCore_mips64r6el:
552 cpu = "mips64r6"; break;
553 default:
554 break;
555 }
556 }
557 return cpu;
558}
559
Greg Clayton64195a22011-02-23 00:35:02 +0000560uint32_t
561ArchSpec::GetMachOCPUType () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000562{
Greg Clayton64195a22011-02-23 00:35:02 +0000563 const CoreDefinition *core_def = FindCoreDefinition (m_core);
564 if (core_def)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000565 {
Greg Clayton64195a22011-02-23 00:35:02 +0000566 const ArchDefinitionEntry *arch_def = FindArchDefinitionEntry (&g_macho_arch_def, core_def->core);
567 if (arch_def)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000568 {
Greg Clayton64195a22011-02-23 00:35:02 +0000569 return arch_def->cpu;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000570 }
571 }
Greg Clayton64195a22011-02-23 00:35:02 +0000572 return LLDB_INVALID_CPUTYPE;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000573}
574
Greg Clayton64195a22011-02-23 00:35:02 +0000575uint32_t
576ArchSpec::GetMachOCPUSubType () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000577{
Greg Clayton64195a22011-02-23 00:35:02 +0000578 const CoreDefinition *core_def = FindCoreDefinition (m_core);
579 if (core_def)
580 {
581 const ArchDefinitionEntry *arch_def = FindArchDefinitionEntry (&g_macho_arch_def, core_def->core);
582 if (arch_def)
583 {
Greg Clayton1cb64962011-03-24 04:28:38 +0000584 return arch_def->sub;
Greg Clayton64195a22011-02-23 00:35:02 +0000585 }
586 }
587 return LLDB_INVALID_CPUTYPE;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000588}
589
Matthew Gardinere77b2942014-09-01 09:06:03 +0000590uint32_t
591ArchSpec::GetDataByteSize () const
592{
593 switch (m_core)
594 {
595 case eCore_kalimba3:
Matthew Gardinerf03e6d842014-09-29 08:02:24 +0000596 return 4;
Matthew Gardinere77b2942014-09-01 09:06:03 +0000597 case eCore_kalimba4:
598 return 1;
599 case eCore_kalimba5:
Matthew Gardinerf03e6d842014-09-29 08:02:24 +0000600 return 4;
Matthew Gardinere77b2942014-09-01 09:06:03 +0000601 default:
602 return 1;
603 }
604 return 1;
605}
606
607uint32_t
608ArchSpec::GetCodeByteSize () const
609{
610 switch (m_core)
611 {
612 case eCore_kalimba3:
613 return 4;
614 case eCore_kalimba4:
615 return 1;
616 case eCore_kalimba5:
617 return 1;
618 default:
619 return 1;
620 }
621 return 1;
622}
623
Greg Clayton64195a22011-02-23 00:35:02 +0000624llvm::Triple::ArchType
625ArchSpec::GetMachine () const
626{
627 const CoreDefinition *core_def = FindCoreDefinition (m_core);
628 if (core_def)
629 return core_def->machine;
630
631 return llvm::Triple::UnknownArch;
632}
633
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000634const ConstString&
635ArchSpec::GetDistributionId () const
636{
637 return m_distribution_id;
638}
639
640void
641ArchSpec::SetDistributionId (const char* distribution_id)
642{
643 m_distribution_id.SetCString (distribution_id);
644}
645
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000646uint32_t
647ArchSpec::GetAddressByteSize() const
648{
Greg Clayton64195a22011-02-23 00:35:02 +0000649 const CoreDefinition *core_def = FindCoreDefinition (m_core);
650 if (core_def)
Mohit K. Bhakkad9514a382015-09-09 10:32:20 +0000651 {
652 if (core_def->machine == llvm::Triple::mips64 || core_def->machine == llvm::Triple::mips64el)
653 {
654 // For N32/O32 applications Address size is 4 bytes.
655 if (m_flags & (eMIPSABI_N32 | eMIPSABI_O32))
656 return 4;
657 }
658 return core_def->addr_byte_size;
659 }
Greg Clayton41f92322010-06-11 03:25:34 +0000660 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000661}
662
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000663ByteOrder
664ArchSpec::GetDefaultEndian () const
665{
Greg Clayton64195a22011-02-23 00:35:02 +0000666 const CoreDefinition *core_def = FindCoreDefinition (m_core);
667 if (core_def)
668 return core_def->default_byte_order;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000669 return eByteOrderInvalid;
670}
671
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +0000672bool
673ArchSpec::CharIsSignedByDefault () const
674{
675 switch (m_triple.getArch()) {
676 default:
677 return true;
678
679 case llvm::Triple::aarch64:
680 case llvm::Triple::aarch64_be:
681 case llvm::Triple::arm:
682 case llvm::Triple::armeb:
683 case llvm::Triple::thumb:
684 case llvm::Triple::thumbeb:
685 return m_triple.isOSDarwin() || m_triple.isOSWindows();
686
687 case llvm::Triple::ppc:
688 case llvm::Triple::ppc64:
689 return m_triple.isOSDarwin();
690
691 case llvm::Triple::ppc64le:
692 case llvm::Triple::systemz:
693 case llvm::Triple::xcore:
694 return false;
695 }
696}
697
Greg Clayton64195a22011-02-23 00:35:02 +0000698lldb::ByteOrder
699ArchSpec::GetByteOrder () const
700{
701 if (m_byte_order == eByteOrderInvalid)
702 return GetDefaultEndian();
703 return m_byte_order;
704}
705
706//===----------------------------------------------------------------------===//
707// Mutators.
708
709bool
710ArchSpec::SetTriple (const llvm::Triple &triple)
711{
712 m_triple = triple;
713
714 llvm::StringRef arch_name (m_triple.getArchName());
715 const CoreDefinition *core_def = FindCoreDefinition (arch_name);
716 if (core_def)
717 {
718 m_core = core_def->core;
Greg Claytoneb0103f2011-04-07 22:46:35 +0000719 // Set the byte order to the default byte order for an architecture.
720 // This can be modified if needed for cases when cores handle both
721 // big and little endian
722 m_byte_order = core_def->default_byte_order;
Greg Clayton64195a22011-02-23 00:35:02 +0000723 }
724 else
725 {
726 Clear();
727 }
728
Greg Clayton64195a22011-02-23 00:35:02 +0000729 return IsValid();
730}
731
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000732static bool
733ParseMachCPUDashSubtypeTriple (const char *triple_cstr, ArchSpec &arch)
734{
735 // Accept "12-10" or "12.10" as cpu type/subtype
736 if (isdigit(triple_cstr[0]))
737 {
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000738 char *end = nullptr;
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000739 errno = 0;
Greg Claytonc7bece562013-01-25 18:06:21 +0000740 uint32_t cpu = (uint32_t)::strtoul (triple_cstr, &end, 0);
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000741 if (errno == 0 && cpu != 0 && end && ((*end == '-') || (*end == '.')))
742 {
743 errno = 0;
Greg Claytonc7bece562013-01-25 18:06:21 +0000744 uint32_t sub = (uint32_t)::strtoul (end + 1, &end, 0);
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000745 if (errno == 0 && end && ((*end == '-') || (*end == '.') || (*end == '\0')))
746 {
747 if (arch.SetArchitecture (eArchTypeMachO, cpu, sub))
748 {
749 if (*end == '-')
750 {
751 llvm::StringRef vendor_os (end + 1);
752 size_t dash_pos = vendor_os.find('-');
753 if (dash_pos != llvm::StringRef::npos)
754 {
755 llvm::StringRef vendor_str(vendor_os.substr(0, dash_pos));
756 arch.GetTriple().setVendorName(vendor_str);
757 const size_t vendor_start_pos = dash_pos+1;
Greg Claytonc7bece562013-01-25 18:06:21 +0000758 dash_pos = vendor_os.find('-', vendor_start_pos);
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000759 if (dash_pos == llvm::StringRef::npos)
760 {
761 if (vendor_start_pos < vendor_os.size())
762 arch.GetTriple().setOSName(vendor_os.substr(vendor_start_pos));
763 }
764 else
765 {
766 arch.GetTriple().setOSName(vendor_os.substr(vendor_start_pos, dash_pos - vendor_start_pos));
767 }
768 }
769 }
770 return true;
771 }
772 }
773 }
774 }
775 return false;
776}
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000777
Greg Clayton64195a22011-02-23 00:35:02 +0000778bool
Greg Clayton70512312012-05-08 01:45:38 +0000779ArchSpec::SetTriple (const char *triple_cstr)
Greg Clayton64195a22011-02-23 00:35:02 +0000780{
Greg Clayton23aca092011-08-12 23:32:52 +0000781 if (triple_cstr && triple_cstr[0])
Greg Clayton64195a22011-02-23 00:35:02 +0000782 {
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000783 if (ParseMachCPUDashSubtypeTriple (triple_cstr, *this))
784 return true;
785
Greg Clayton64195a22011-02-23 00:35:02 +0000786 llvm::StringRef triple_stref (triple_cstr);
787 if (triple_stref.startswith (LLDB_ARCH_DEFAULT))
788 {
789 // Special case for the current host default architectures...
790 if (triple_stref.equals (LLDB_ARCH_DEFAULT_32BIT))
Zachary Turner13b18262014-08-20 16:42:51 +0000791 *this = HostInfo::GetArchitecture(HostInfo::eArchKind32);
Greg Clayton64195a22011-02-23 00:35:02 +0000792 else if (triple_stref.equals (LLDB_ARCH_DEFAULT_64BIT))
Zachary Turner13b18262014-08-20 16:42:51 +0000793 *this = HostInfo::GetArchitecture(HostInfo::eArchKind64);
Greg Clayton64195a22011-02-23 00:35:02 +0000794 else if (triple_stref.equals (LLDB_ARCH_DEFAULT))
Zachary Turner13b18262014-08-20 16:42:51 +0000795 *this = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
Greg Clayton64195a22011-02-23 00:35:02 +0000796 }
797 else
798 {
799 std::string normalized_triple_sstr (llvm::Triple::normalize(triple_stref));
800 triple_stref = normalized_triple_sstr;
Greg Clayton70512312012-05-08 01:45:38 +0000801 SetTriple (llvm::Triple (triple_stref));
802 }
803 }
804 else
805 Clear();
806 return IsValid();
807}
808
809bool
810ArchSpec::SetTriple (const char *triple_cstr, Platform *platform)
811{
812 if (triple_cstr && triple_cstr[0])
813 {
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000814 if (ParseMachCPUDashSubtypeTriple (triple_cstr, *this))
815 return true;
816
Greg Clayton70512312012-05-08 01:45:38 +0000817 llvm::StringRef triple_stref (triple_cstr);
818 if (triple_stref.startswith (LLDB_ARCH_DEFAULT))
819 {
820 // Special case for the current host default architectures...
821 if (triple_stref.equals (LLDB_ARCH_DEFAULT_32BIT))
Zachary Turner13b18262014-08-20 16:42:51 +0000822 *this = HostInfo::GetArchitecture(HostInfo::eArchKind32);
Greg Clayton70512312012-05-08 01:45:38 +0000823 else if (triple_stref.equals (LLDB_ARCH_DEFAULT_64BIT))
Zachary Turner13b18262014-08-20 16:42:51 +0000824 *this = HostInfo::GetArchitecture(HostInfo::eArchKind64);
Greg Clayton70512312012-05-08 01:45:38 +0000825 else if (triple_stref.equals (LLDB_ARCH_DEFAULT))
Zachary Turner13b18262014-08-20 16:42:51 +0000826 *this = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
Greg Clayton70512312012-05-08 01:45:38 +0000827 }
828 else
829 {
830 ArchSpec raw_arch (triple_cstr);
831
832 std::string normalized_triple_sstr (llvm::Triple::normalize(triple_stref));
833 triple_stref = normalized_triple_sstr;
Greg Claytoneb0103f2011-04-07 22:46:35 +0000834 llvm::Triple normalized_triple (triple_stref);
835
836 const bool os_specified = normalized_triple.getOSName().size() > 0;
837 const bool vendor_specified = normalized_triple.getVendorName().size() > 0;
838 const bool env_specified = normalized_triple.getEnvironmentName().size() > 0;
839
840 // If we got an arch only, then default the vendor, os, environment
841 // to match the platform if one is supplied
842 if (!(os_specified || vendor_specified || env_specified))
843 {
844 if (platform)
845 {
846 // If we were given a platform, use the platform's system
847 // architecture. If this is not available (might not be
848 // connected) use the first supported architecture.
Greg Clayton70512312012-05-08 01:45:38 +0000849 ArchSpec compatible_arch;
Greg Clayton1e0c8842013-01-11 20:49:54 +0000850 if (platform->IsCompatibleArchitecture (raw_arch, false, &compatible_arch))
Greg Claytoneb0103f2011-04-07 22:46:35 +0000851 {
Greg Clayton70512312012-05-08 01:45:38 +0000852 if (compatible_arch.IsValid())
853 {
854 const llvm::Triple &compatible_triple = compatible_arch.GetTriple();
855 if (!vendor_specified)
856 normalized_triple.setVendor(compatible_triple.getVendor());
857 if (!os_specified)
858 normalized_triple.setOS(compatible_triple.getOS());
859 if (!env_specified && compatible_triple.getEnvironmentName().size())
860 normalized_triple.setEnvironment(compatible_triple.getEnvironment());
861 }
Greg Claytoneb0103f2011-04-07 22:46:35 +0000862 }
Greg Clayton70512312012-05-08 01:45:38 +0000863 else
Greg Claytoneb0103f2011-04-07 22:46:35 +0000864 {
Greg Clayton70512312012-05-08 01:45:38 +0000865 *this = raw_arch;
866 return IsValid();
Greg Claytoneb0103f2011-04-07 22:46:35 +0000867 }
868 }
869 else
870 {
871 // No platform specified, fall back to the host system for
872 // the default vendor, os, and environment.
Sean Callananbfb237bc2011-11-04 22:46:46 +0000873 llvm::Triple host_triple(llvm::sys::getDefaultTargetTriple());
Greg Clayton70512312012-05-08 01:45:38 +0000874 if (!vendor_specified)
875 normalized_triple.setVendor(host_triple.getVendor());
876 if (!vendor_specified)
877 normalized_triple.setOS(host_triple.getOS());
878 if (!env_specified && host_triple.getEnvironmentName().size())
879 normalized_triple.setEnvironment(host_triple.getEnvironment());
Greg Claytoneb0103f2011-04-07 22:46:35 +0000880 }
881 }
882 SetTriple (normalized_triple);
Greg Clayton64195a22011-02-23 00:35:02 +0000883 }
884 }
885 else
886 Clear();
887 return IsValid();
888}
889
Zachary Turner5e6f4522015-01-22 18:59:05 +0000890void
891ArchSpec::MergeFrom(const ArchSpec &other)
892{
Todd Fiala7df337f2015-10-13 23:41:19 +0000893 if (TripleVendorIsUnspecifiedUnknown() && !other.TripleVendorIsUnspecifiedUnknown())
Zachary Turner5e6f4522015-01-22 18:59:05 +0000894 GetTriple().setVendor(other.GetTriple().getVendor());
Todd Fiala7df337f2015-10-13 23:41:19 +0000895 if (TripleOSIsUnspecifiedUnknown() && !other.TripleOSIsUnspecifiedUnknown())
Zachary Turner5e6f4522015-01-22 18:59:05 +0000896 GetTriple().setOS(other.GetTriple().getOS());
897 if (GetTriple().getArch() == llvm::Triple::UnknownArch)
898 GetTriple().setArch(other.GetTriple().getArch());
Jason Molenda03fe45e2015-11-06 01:43:36 +0000899 if (GetTriple().getEnvironment() == llvm::Triple::UnknownEnvironment && !TripleVendorWasSpecified())
900 {
901 if (other.TripleVendorWasSpecified())
902 GetTriple().setEnvironment(other.GetTriple().getEnvironment());
903 }
Jason Molenda583b1a82016-04-05 05:01:30 +0000904 // If this and other are both arm ArchSpecs and this ArchSpec is a generic "some kind of arm"
905 // spec but the other ArchSpec is a specific arm core, adopt the specific arm core.
906 if (GetTriple().getArch() == llvm::Triple::arm
907 && other.GetTriple().getArch() == llvm::Triple::arm
908 && IsCompatibleMatch (other)
909 && GetCore() == ArchSpec::eCore_arm_generic
910 && other.GetCore() != ArchSpec::eCore_arm_generic)
911 {
912 m_core = other.GetCore();
913 CoreUpdated (true);
914 }
Zachary Turner5e6f4522015-01-22 18:59:05 +0000915}
916
Greg Clayton64195a22011-02-23 00:35:02 +0000917bool
Ed Mastef6a13122015-06-05 13:03:08 +0000918ArchSpec::SetArchitecture (ArchitectureType arch_type, uint32_t cpu, uint32_t sub, uint32_t os)
Greg Clayton64195a22011-02-23 00:35:02 +0000919{
920 m_core = kCore_invalid;
921 bool update_triple = true;
922 const ArchDefinition *arch_def = FindArchDefinition(arch_type);
923 if (arch_def)
924 {
925 const ArchDefinitionEntry *arch_def_entry = FindArchDefinitionEntry (arch_def, cpu, sub);
926 if (arch_def_entry)
927 {
928 const CoreDefinition *core_def = FindCoreDefinition (arch_def_entry->core);
929 if (core_def)
930 {
931 m_core = core_def->core;
932 update_triple = false;
Greg Clayton593577a2011-09-21 03:57:31 +0000933 // Always use the architecture name because it might be more descriptive
934 // than the architecture enum ("armv7" -> llvm::Triple::arm).
935 m_triple.setArchName(llvm::StringRef(core_def->name));
Greg Clayton64195a22011-02-23 00:35:02 +0000936 if (arch_type == eArchTypeMachO)
937 {
938 m_triple.setVendor (llvm::Triple::Apple);
Greg Claytona3a6c122014-07-29 18:04:57 +0000939
Jason Molenda03fe45e2015-11-06 01:43:36 +0000940 // Don't set the OS. It could be simulator, macosx, ios, watchos, tvos. We could
941 // get close with the cpu type - but we can't get it right all of the time. Better
942 // to leave this unset so other sections of code will set it when they have more
943 // information.
944 // NB: don't call m_triple.setOS (llvm::Triple::UnknownOS). That sets the OSName to
945 // "unknown" and the ArchSpec::TripleVendorWasSpecified() method says that any
946 // OSName setting means it was specified.
Greg Clayton64195a22011-02-23 00:35:02 +0000947 }
Ed Mastef6a13122015-06-05 13:03:08 +0000948 else if (arch_type == eArchTypeELF)
949 {
Ed Mastef6a13122015-06-05 13:03:08 +0000950 switch (os)
951 {
Tamas Berghammered1fa202015-07-07 09:11:59 +0000952 case llvm::ELF::ELFOSABI_AIX: m_triple.setOS (llvm::Triple::OSType::AIX); break;
953 case llvm::ELF::ELFOSABI_FREEBSD: m_triple.setOS (llvm::Triple::OSType::FreeBSD); break;
954 case llvm::ELF::ELFOSABI_GNU: m_triple.setOS (llvm::Triple::OSType::Linux); break;
955 case llvm::ELF::ELFOSABI_NETBSD: m_triple.setOS (llvm::Triple::OSType::NetBSD); break;
956 case llvm::ELF::ELFOSABI_OPENBSD: m_triple.setOS (llvm::Triple::OSType::OpenBSD); break;
957 case llvm::ELF::ELFOSABI_SOLARIS: m_triple.setOS (llvm::Triple::OSType::Solaris); break;
Ed Mastef6a13122015-06-05 13:03:08 +0000958 }
Ed Mastef6a13122015-06-05 13:03:08 +0000959 }
Jason Molenda03fe45e2015-11-06 01:43:36 +0000960 else
961 {
962 m_triple.setVendor (llvm::Triple::UnknownVendor);
963 m_triple.setOS (llvm::Triple::UnknownOS);
964 }
Greg Clayton593577a2011-09-21 03:57:31 +0000965 // Fall back onto setting the machine type if the arch by name failed...
966 if (m_triple.getArch () == llvm::Triple::UnknownArch)
967 m_triple.setArch (core_def->machine);
Greg Clayton64195a22011-02-23 00:35:02 +0000968 }
969 }
970 }
971 CoreUpdated(update_triple);
972 return IsValid();
973}
974
Greg Clayton357132e2011-03-26 19:14:58 +0000975uint32_t
976ArchSpec::GetMinimumOpcodeByteSize() const
Greg Clayton64195a22011-02-23 00:35:02 +0000977{
Greg Clayton357132e2011-03-26 19:14:58 +0000978 const CoreDefinition *core_def = FindCoreDefinition (m_core);
979 if (core_def)
980 return core_def->min_opcode_byte_size;
981 return 0;
982}
983
984uint32_t
985ArchSpec::GetMaximumOpcodeByteSize() const
986{
987 const CoreDefinition *core_def = FindCoreDefinition (m_core);
988 if (core_def)
989 return core_def->max_opcode_byte_size;
990 return 0;
Greg Clayton64195a22011-02-23 00:35:02 +0000991}
992
Jason Molendaba813dc2012-11-04 03:20:05 +0000993bool
994ArchSpec::IsExactMatch (const ArchSpec& rhs) const
995{
Sean Callananbf4b7be2012-12-13 22:07:14 +0000996 return IsEqualTo (rhs, true);
Jason Molendaba813dc2012-11-04 03:20:05 +0000997}
998
999bool
1000ArchSpec::IsCompatibleMatch (const ArchSpec& rhs) const
1001{
Sean Callananbf4b7be2012-12-13 22:07:14 +00001002 return IsEqualTo (rhs, false);
Jason Molendaba813dc2012-11-04 03:20:05 +00001003}
1004
Tamas Berghammer0237eda2016-04-25 15:51:45 +00001005static bool
1006isCompatibleEnvironment(llvm::Triple::EnvironmentType lhs, llvm::Triple::EnvironmentType rhs)
1007{
1008 if (lhs == rhs)
1009 return true;
1010
1011 // If any of the environment is unknown then they are compatible
1012 if (lhs == llvm::Triple::UnknownEnvironment || rhs == llvm::Triple::UnknownEnvironment)
1013 return true;
1014
1015 // If one of the environment is Android and the other one is EABI then they are considered to
1016 // be compatible. This is required as a workaround for shared libraries compiled for Android
1017 // without the NOTE section indicating that they are using the Android ABI.
1018 if ((lhs == llvm::Triple::Android && rhs == llvm::Triple::EABI) ||
Omair Javaid9a1699c2016-04-26 01:08:59 +00001019 (rhs == llvm::Triple::Android && lhs == llvm::Triple::EABI) ||
1020 (lhs == llvm::Triple::GNUEABI && rhs == llvm::Triple::EABI) ||
1021 (rhs == llvm::Triple::GNUEABI && lhs == llvm::Triple::EABI) ||
1022 (lhs == llvm::Triple::GNUEABIHF && rhs == llvm::Triple::EABIHF) ||
1023 (rhs == llvm::Triple::GNUEABIHF && lhs == llvm::Triple::EABIHF))
Tamas Berghammer0237eda2016-04-25 15:51:45 +00001024 return true;
1025
1026 return false;
1027}
1028
Jason Molendaba813dc2012-11-04 03:20:05 +00001029bool
Sean Callananbf4b7be2012-12-13 22:07:14 +00001030ArchSpec::IsEqualTo (const ArchSpec& rhs, bool exact_match) const
Jason Molendaba813dc2012-11-04 03:20:05 +00001031{
Todd Fialaa9ddb0e2014-01-18 03:02:39 +00001032 // explicitly ignoring m_distribution_id in this method.
1033
Jason Molendaba813dc2012-11-04 03:20:05 +00001034 if (GetByteOrder() != rhs.GetByteOrder())
1035 return false;
1036
1037 const ArchSpec::Core lhs_core = GetCore ();
1038 const ArchSpec::Core rhs_core = rhs.GetCore ();
1039
1040 const bool core_match = cores_match (lhs_core, rhs_core, true, exact_match);
1041
1042 if (core_match)
1043 {
1044 const llvm::Triple &lhs_triple = GetTriple();
1045 const llvm::Triple &rhs_triple = rhs.GetTriple();
1046
1047 const llvm::Triple::VendorType lhs_triple_vendor = lhs_triple.getVendor();
1048 const llvm::Triple::VendorType rhs_triple_vendor = rhs_triple.getVendor();
1049 if (lhs_triple_vendor != rhs_triple_vendor)
1050 {
Jason Molenda03fe45e2015-11-06 01:43:36 +00001051 const bool rhs_vendor_specified = rhs.TripleVendorWasSpecified();
1052 const bool lhs_vendor_specified = TripleVendorWasSpecified();
1053 // Both architectures had the vendor specified, so if they aren't
1054 // equal then we return false
1055 if (rhs_vendor_specified && lhs_vendor_specified)
1056 return false;
Jason Molendaba813dc2012-11-04 03:20:05 +00001057
1058 // Only fail if both vendor types are not unknown
1059 if (lhs_triple_vendor != llvm::Triple::UnknownVendor &&
1060 rhs_triple_vendor != llvm::Triple::UnknownVendor)
1061 return false;
1062 }
1063
1064 const llvm::Triple::OSType lhs_triple_os = lhs_triple.getOS();
1065 const llvm::Triple::OSType rhs_triple_os = rhs_triple.getOS();
1066 if (lhs_triple_os != rhs_triple_os)
1067 {
Jason Molenda03fe45e2015-11-06 01:43:36 +00001068 const bool rhs_os_specified = rhs.TripleOSWasSpecified();
1069 const bool lhs_os_specified = TripleOSWasSpecified();
1070 // Both architectures had the OS specified, so if they aren't
1071 // equal then we return false
1072 if (rhs_os_specified && lhs_os_specified)
1073 return false;
Greg Clayton7ab7f892014-05-29 21:33:45 +00001074
Greg Clayton3f19ada2014-07-10 23:33:37 +00001075 // Only fail if both os types are not unknown
1076 if (lhs_triple_os != llvm::Triple::UnknownOS &&
1077 rhs_triple_os != llvm::Triple::UnknownOS)
1078 return false;
Jason Molendaba813dc2012-11-04 03:20:05 +00001079 }
1080
1081 const llvm::Triple::EnvironmentType lhs_triple_env = lhs_triple.getEnvironment();
1082 const llvm::Triple::EnvironmentType rhs_triple_env = rhs_triple.getEnvironment();
Tamas Berghammer0237eda2016-04-25 15:51:45 +00001083
1084 if (!isCompatibleEnvironment(lhs_triple_env, rhs_triple_env))
1085 return false;
Jason Molendaba813dc2012-11-04 03:20:05 +00001086 return true;
1087 }
1088 return false;
1089}
1090
Greg Clayton64195a22011-02-23 00:35:02 +00001091//===----------------------------------------------------------------------===//
1092// Helper methods.
1093
1094void
1095ArchSpec::CoreUpdated (bool update_triple)
1096{
1097 const CoreDefinition *core_def = FindCoreDefinition (m_core);
1098 if (core_def)
1099 {
1100 if (update_triple)
1101 m_triple = llvm::Triple(core_def->name, "unknown", "unknown");
1102 m_byte_order = core_def->default_byte_order;
1103 }
1104 else
1105 {
1106 if (update_triple)
1107 m_triple = llvm::Triple();
1108 m_byte_order = eByteOrderInvalid;
1109 }
1110}
1111
1112//===----------------------------------------------------------------------===//
1113// Operators.
1114
Greg Clayton70512312012-05-08 01:45:38 +00001115static bool
Jason Molendaba813dc2012-11-04 03:20:05 +00001116cores_match (const ArchSpec::Core core1, const ArchSpec::Core core2, bool try_inverse, bool enforce_exact_match)
Greg Clayton70512312012-05-08 01:45:38 +00001117{
Jason Molendaba813dc2012-11-04 03:20:05 +00001118 if (core1 == core2)
1119 return true;
1120
Greg Clayton70512312012-05-08 01:45:38 +00001121 switch (core1)
1122 {
Greg Clayton70512312012-05-08 01:45:38 +00001123 case ArchSpec::kCore_any:
1124 return true;
1125
Greg Clayton44362e02014-07-12 00:11:34 +00001126 case ArchSpec::eCore_arm_generic:
1127 if (enforce_exact_match)
1128 break;
Jason Molenda62e06812016-02-16 04:14:33 +00001129 LLVM_FALLTHROUGH;
Greg Clayton70512312012-05-08 01:45:38 +00001130 case ArchSpec::kCore_arm_any:
1131 if (core2 >= ArchSpec::kCore_arm_first && core2 <= ArchSpec::kCore_arm_last)
1132 return true;
1133 if (core2 >= ArchSpec::kCore_thumb_first && core2 <= ArchSpec::kCore_thumb_last)
1134 return true;
1135 if (core2 == ArchSpec::kCore_arm_any)
1136 return true;
1137 break;
Sylvestre Ledru1c9e0642014-08-18 14:53:42 +00001138
Greg Clayton70512312012-05-08 01:45:38 +00001139 case ArchSpec::kCore_x86_32_any:
1140 if ((core2 >= ArchSpec::kCore_x86_32_first && core2 <= ArchSpec::kCore_x86_32_last) || (core2 == ArchSpec::kCore_x86_32_any))
1141 return true;
1142 break;
Zachary Turnerad587ae42014-07-28 16:44:49 +00001143
1144 case ArchSpec::kCore_x86_64_any:
1145 if ((core2 >= ArchSpec::kCore_x86_64_first && core2 <= ArchSpec::kCore_x86_64_last) || (core2 == ArchSpec::kCore_x86_64_any))
1146 return true;
Sylvestre Ledru1c9e0642014-08-18 14:53:42 +00001147 break;
Zachary Turnerad587ae42014-07-28 16:44:49 +00001148
Greg Clayton70512312012-05-08 01:45:38 +00001149 case ArchSpec::kCore_ppc_any:
1150 if ((core2 >= ArchSpec::kCore_ppc_first && core2 <= ArchSpec::kCore_ppc_last) || (core2 == ArchSpec::kCore_ppc_any))
1151 return true;
1152 break;
Sylvestre Ledru1c9e0642014-08-18 14:53:42 +00001153
Greg Clayton70512312012-05-08 01:45:38 +00001154 case ArchSpec::kCore_ppc64_any:
1155 if ((core2 >= ArchSpec::kCore_ppc64_first && core2 <= ArchSpec::kCore_ppc64_last) || (core2 == ArchSpec::kCore_ppc64_any))
1156 return true;
1157 break;
1158
Jason Molendaa3a04522013-09-27 23:21:54 +00001159 case ArchSpec::eCore_arm_armv6m:
1160 if (!enforce_exact_match)
1161 {
Greg Clayton44362e02014-07-12 00:11:34 +00001162 if (core2 == ArchSpec::eCore_arm_generic)
1163 return true;
Jason Molendaa3a04522013-09-27 23:21:54 +00001164 try_inverse = false;
Jason Molendac7cda272013-09-27 23:29:10 +00001165 if (core2 == ArchSpec::eCore_arm_armv7)
Jason Molendaa3a04522013-09-27 23:21:54 +00001166 return true;
Jason Molendad607afd2015-06-25 22:37:57 +00001167 if (core2 == ArchSpec::eCore_arm_armv6m)
1168 return true;
Jason Molendaa3a04522013-09-27 23:21:54 +00001169 }
Sylvestre Ledru1c9e0642014-08-18 14:53:42 +00001170 break;
Deepak Panickal6d3df422014-02-19 11:16:46 +00001171
1172 case ArchSpec::kCore_hexagon_any:
1173 if ((core2 >= ArchSpec::kCore_hexagon_first && core2 <= ArchSpec::kCore_hexagon_last) || (core2 == ArchSpec::kCore_hexagon_any))
1174 return true;
Jason Molendaa3a04522013-09-27 23:21:54 +00001175 break;
1176
Jason Molenda8825c5c2015-10-08 21:48:35 +00001177 // v. https://en.wikipedia.org/wiki/ARM_Cortex-M#Silicon_customization
1178 // Cortex-M0 - ARMv6-M - armv6m
1179 // Cortex-M3 - ARMv7-M - armv7m
1180 // Cortex-M4 - ARMv7E-M - armv7em
Jason Molenda7a1559c2013-03-08 01:20:17 +00001181 case ArchSpec::eCore_arm_armv7em:
Jason Molendad607afd2015-06-25 22:37:57 +00001182 if (!enforce_exact_match)
1183 {
1184 if (core2 == ArchSpec::eCore_arm_generic)
1185 return true;
1186 if (core2 == ArchSpec::eCore_arm_armv7m)
1187 return true;
1188 if (core2 == ArchSpec::eCore_arm_armv6m)
1189 return true;
1190 if (core2 == ArchSpec::eCore_arm_armv7)
1191 return true;
1192 try_inverse = true;
1193 }
1194 break;
1195
Jason Molenda8825c5c2015-10-08 21:48:35 +00001196 // v. https://en.wikipedia.org/wiki/ARM_Cortex-M#Silicon_customization
1197 // Cortex-M0 - ARMv6-M - armv6m
1198 // Cortex-M3 - ARMv7-M - armv7m
1199 // Cortex-M4 - ARMv7E-M - armv7em
Jason Molendad607afd2015-06-25 22:37:57 +00001200 case ArchSpec::eCore_arm_armv7m:
1201 if (!enforce_exact_match)
1202 {
1203 if (core2 == ArchSpec::eCore_arm_generic)
1204 return true;
1205 if (core2 == ArchSpec::eCore_arm_armv6m)
1206 return true;
1207 if (core2 == ArchSpec::eCore_arm_armv7)
1208 return true;
1209 if (core2 == ArchSpec::eCore_arm_armv7em)
1210 return true;
1211 try_inverse = true;
1212 }
1213 break;
1214
Johnny Chen1083b0d2012-08-28 22:53:40 +00001215 case ArchSpec::eCore_arm_armv7f:
1216 case ArchSpec::eCore_arm_armv7k:
1217 case ArchSpec::eCore_arm_armv7s:
Jason Molendaba813dc2012-11-04 03:20:05 +00001218 if (!enforce_exact_match)
1219 {
Greg Clayton44362e02014-07-12 00:11:34 +00001220 if (core2 == ArchSpec::eCore_arm_generic)
1221 return true;
Jason Molendaba813dc2012-11-04 03:20:05 +00001222 if (core2 == ArchSpec::eCore_arm_armv7)
1223 return true;
Greg Clayton44362e02014-07-12 00:11:34 +00001224 try_inverse = false;
Jason Molendaba813dc2012-11-04 03:20:05 +00001225 }
Johnny Chen1083b0d2012-08-28 22:53:40 +00001226 break;
Sylvestre Ledru1c9e0642014-08-18 14:53:42 +00001227
Greg Clayton52edb362014-07-14 22:53:02 +00001228 case ArchSpec::eCore_x86_64_x86_64h:
1229 if (!enforce_exact_match)
1230 {
1231 try_inverse = false;
1232 if (core2 == ArchSpec::eCore_x86_64_x86_64)
1233 return true;
1234 }
1235 break;
Johnny Chen1083b0d2012-08-28 22:53:40 +00001236
Todd Fiala02e71812014-08-28 14:32:43 +00001237 case ArchSpec::eCore_arm_armv8:
1238 if (!enforce_exact_match)
1239 {
1240 if (core2 == ArchSpec::eCore_arm_arm64)
1241 return true;
1242 if (core2 == ArchSpec::eCore_arm_aarch64)
1243 return true;
1244 try_inverse = false;
1245 }
1246 break;
1247
1248 case ArchSpec::eCore_arm_aarch64:
1249 if (!enforce_exact_match)
1250 {
1251 if (core2 == ArchSpec::eCore_arm_arm64)
1252 return true;
1253 if (core2 == ArchSpec::eCore_arm_armv8)
1254 return true;
1255 try_inverse = false;
1256 }
1257 break;
1258
1259 case ArchSpec::eCore_arm_arm64:
1260 if (!enforce_exact_match)
1261 {
1262 if (core2 == ArchSpec::eCore_arm_aarch64)
1263 return true;
1264 if (core2 == ArchSpec::eCore_arm_armv8)
1265 return true;
1266 try_inverse = false;
1267 }
1268 break;
1269
Sagar Thakur6bee9612015-07-13 09:52:06 +00001270 case ArchSpec::eCore_mips32:
1271 if (!enforce_exact_match)
1272 {
1273 if (core2 >= ArchSpec::kCore_mips32_first && core2 <= ArchSpec::kCore_mips32_last)
1274 return true;
1275 try_inverse = false;
1276 }
1277 break;
1278
1279 case ArchSpec::eCore_mips32el:
1280 if (!enforce_exact_match)
1281 {
1282 if (core2 >= ArchSpec::kCore_mips32el_first && core2 <= ArchSpec::kCore_mips32el_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_mips64:
Sagar Thakur6bee9612015-07-13 09:52:06 +00001289 if (!enforce_exact_match)
1290 {
1291 if (core2 >= ArchSpec::kCore_mips32_first && core2 <= ArchSpec::kCore_mips32_last)
1292 return true;
1293 if (core2 >= ArchSpec::kCore_mips64_first && core2 <= ArchSpec::kCore_mips64_last)
1294 return true;
1295 try_inverse = false;
1296 }
Greg Claytoncec91ef2016-02-26 01:20:20 +00001297 break;
Sagar Thakur6bee9612015-07-13 09:52:06 +00001298
1299 case ArchSpec::eCore_mips64el:
1300 if (!enforce_exact_match)
1301 {
1302 if (core2 >= ArchSpec::kCore_mips32el_first && core2 <= ArchSpec::kCore_mips32el_last)
1303 return true;
1304 if (core2 >= ArchSpec::kCore_mips64el_first && core2 <= ArchSpec::kCore_mips64el_last)
1305 return true;
1306 try_inverse = false;
1307 }
Greg Claytoncec91ef2016-02-26 01:20:20 +00001308 break;
Sagar Thakur6bee9612015-07-13 09:52:06 +00001309
Sagar Thakurce815e42015-06-03 10:14:24 +00001310 case ArchSpec::eCore_mips64r2:
1311 case ArchSpec::eCore_mips64r3:
1312 case ArchSpec::eCore_mips64r5:
Sagar Thakurce815e42015-06-03 10:14:24 +00001313 if (!enforce_exact_match)
1314 {
1315 if (core2 >= ArchSpec::kCore_mips32_first && core2 <= (core1 - 10))
1316 return true;
1317 if (core2 >= ArchSpec::kCore_mips64_first && core2 <= (core1 - 1))
1318 return true;
1319 try_inverse = false;
1320 }
1321 break;
1322
Sagar Thakurce815e42015-06-03 10:14:24 +00001323 case ArchSpec::eCore_mips64r2el:
1324 case ArchSpec::eCore_mips64r3el:
1325 case ArchSpec::eCore_mips64r5el:
Sagar Thakurce815e42015-06-03 10:14:24 +00001326 if (!enforce_exact_match)
1327 {
1328 if (core2 >= ArchSpec::kCore_mips32el_first && core2 <= (core1 - 10))
1329 return true;
1330 if (core2 >= ArchSpec::kCore_mips64el_first && core2 <= (core1 - 1))
1331 return true;
1332 try_inverse = false;
1333 }
1334 break;
1335
Sagar Thakur6bee9612015-07-13 09:52:06 +00001336 case ArchSpec::eCore_mips32r2:
1337 case ArchSpec::eCore_mips32r3:
1338 case ArchSpec::eCore_mips32r5:
1339 if (!enforce_exact_match)
1340 {
1341 if (core2 >= ArchSpec::kCore_mips32_first && core2 <= core1)
1342 return true;
1343 }
1344 break;
1345
1346 case ArchSpec::eCore_mips32r2el:
1347 case ArchSpec::eCore_mips32r3el:
1348 case ArchSpec::eCore_mips32r5el:
1349 if (!enforce_exact_match)
1350 {
1351 if (core2 >= ArchSpec::kCore_mips32el_first && core2 <= core1)
1352 return true;
1353 }
1354 break;
1355
1356 case ArchSpec::eCore_mips32r6:
1357 if (!enforce_exact_match)
1358 {
1359 if (core2 == ArchSpec::eCore_mips32 || core2 == ArchSpec::eCore_mips32r6)
1360 return true;
1361 }
1362 break;
1363
1364 case ArchSpec::eCore_mips32r6el:
1365 if (!enforce_exact_match)
1366 {
1367 if (core2 == ArchSpec::eCore_mips32el || core2 == ArchSpec::eCore_mips32r6el)
1368 return true;
Sagar Thakur6bee9612015-07-13 09:52:06 +00001369 }
1370 break;
1371
1372 case ArchSpec::eCore_mips64r6:
1373 if (!enforce_exact_match)
1374 {
1375 if (core2 == ArchSpec::eCore_mips32 || core2 == ArchSpec::eCore_mips32r6)
1376 return true;
1377 if (core2 == ArchSpec::eCore_mips64 || core2 == ArchSpec::eCore_mips64r6)
1378 return true;
1379 }
1380 break;
1381
1382 case ArchSpec::eCore_mips64r6el:
1383 if (!enforce_exact_match)
1384 {
1385 if (core2 == ArchSpec::eCore_mips32el || core2 == ArchSpec::eCore_mips32r6el)
1386 return true;
1387 if (core2 == ArchSpec::eCore_mips64el || core2 == ArchSpec::eCore_mips64r6el)
1388 return true;
1389 }
1390 break;
1391
Greg Clayton70512312012-05-08 01:45:38 +00001392 default:
1393 break;
1394 }
1395 if (try_inverse)
Jason Molendaba813dc2012-11-04 03:20:05 +00001396 return cores_match (core2, core1, false, enforce_exact_match);
Greg Clayton70512312012-05-08 01:45:38 +00001397 return false;
1398}
1399
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001400bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001401lldb_private::operator<(const ArchSpec& lhs, const ArchSpec& rhs)
1402{
Greg Clayton64195a22011-02-23 00:35:02 +00001403 const ArchSpec::Core lhs_core = lhs.GetCore ();
1404 const ArchSpec::Core rhs_core = rhs.GetCore ();
1405 return lhs_core < rhs_core;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001406}
Greg Claytona97c4d22014-12-09 23:31:02 +00001407
1408static void
1409StopInfoOverrideCallbackTypeARM(lldb_private::Thread &thread)
1410{
1411 // We need to check if we are stopped in Thumb mode in a IT instruction
1412 // and detect if the condition doesn't pass. If this is the case it means
1413 // we won't actually execute this instruction. If this happens we need to
1414 // clear the stop reason to no thread plans think we are stopped for a
1415 // reason and the plans should keep going.
1416 //
1417 // We do this because when single stepping many ARM processes, debuggers
1418 // often use the BVR/BCR registers that says "stop when the PC is not
1419 // equal to its current value". This method of stepping means we can end
1420 // up stopping on instructions inside an if/then block that wouldn't get
1421 // executed. By fixing this we can stop the debugger from seeming like
1422 // you stepped through both the "if" _and_ the "else" clause when source
1423 // level stepping because the debugger stops regardless due to the BVR/BCR
1424 // triggering a stop.
1425 //
1426 // It also means we can set breakpoints on instructions inside an an
1427 // if/then block and correctly skip them if we use the BKPT instruction.
1428 // The ARM and Thumb BKPT instructions are unconditional even when executed
1429 // in a Thumb IT block.
1430 //
1431 // If your debugger inserts software traps in ARM/Thumb code, it will
1432 // need to use 16 and 32 bit instruction for 16 and 32 bit thumb
1433 // instructions respectively. If your debugger inserts a 16 bit thumb
1434 // trap on top of a 32 bit thumb instruction for an opcode that is inside
1435 // an if/then, it will change the it/then to conditionally execute your
1436 // 16 bit trap and then cause your program to crash if it executes the
1437 // trailing 16 bits (the second half of the 32 bit thumb instruction you
1438 // partially overwrote).
1439
1440 RegisterContextSP reg_ctx_sp (thread.GetRegisterContext());
1441 if (reg_ctx_sp)
1442 {
1443 const uint32_t cpsr = reg_ctx_sp->GetFlags(0);
1444 if (cpsr != 0)
1445 {
1446 // Read the J and T bits to get the ISETSTATE
1447 const uint32_t J = Bit32(cpsr, 24);
1448 const uint32_t T = Bit32(cpsr, 5);
1449 const uint32_t ISETSTATE = J << 1 | T;
1450 if (ISETSTATE == 0)
1451 {
1452 // NOTE: I am pretty sure we want to enable the code below
1453 // that detects when we stop on an instruction in ARM mode
1454 // that is conditional and the condition doesn't pass. This
1455 // can happen if you set a breakpoint on an instruction that
1456 // is conditional. We currently will _always_ stop on the
1457 // instruction which is bad. You can also run into this while
1458 // single stepping and you could appear to run code in the "if"
1459 // and in the "else" clause because it would stop at all of the
1460 // conditional instructions in both.
1461 // In such cases, we really don't want to stop at this location.
1462 // I will check with the lldb-dev list first before I enable this.
1463#if 0
1464 // ARM mode: check for condition on intsruction
1465 const addr_t pc = reg_ctx_sp->GetPC();
1466 Error error;
1467 // If we fail to read the opcode we will get UINT64_MAX as the
1468 // result in "opcode" which we can use to detect if we read a
1469 // valid opcode.
1470 const uint64_t opcode = thread.GetProcess()->ReadUnsignedIntegerFromMemory(pc, 4, UINT64_MAX, error);
1471 if (opcode <= UINT32_MAX)
1472 {
1473 const uint32_t condition = Bits32((uint32_t)opcode, 31, 28);
Eugene Zelenko896ddd02016-03-02 01:09:03 +00001474 if (!ARMConditionPassed(condition, cpsr))
Greg Claytona97c4d22014-12-09 23:31:02 +00001475 {
1476 // We ARE stopped on an ARM instruction whose condition doesn't
1477 // pass so this instruction won't get executed.
1478 // Regardless of why it stopped, we need to clear the stop info
1479 thread.SetStopInfo (StopInfoSP());
1480 }
1481 }
1482#endif
1483 }
1484 else if (ISETSTATE == 1)
1485 {
1486 // Thumb mode
1487 const uint32_t ITSTATE = Bits32 (cpsr, 15, 10) << 2 | Bits32 (cpsr, 26, 25);
1488 if (ITSTATE != 0)
1489 {
1490 const uint32_t condition = Bits32(ITSTATE, 7, 4);
Eugene Zelenko896ddd02016-03-02 01:09:03 +00001491 if (!ARMConditionPassed(condition, cpsr))
Greg Claytona97c4d22014-12-09 23:31:02 +00001492 {
1493 // We ARE stopped in a Thumb IT instruction on an instruction whose
1494 // condition doesn't pass so this instruction won't get executed.
1495 // Regardless of why it stopped, we need to clear the stop info
1496 thread.SetStopInfo (StopInfoSP());
1497 }
1498 }
1499 }
1500 }
1501 }
1502}
1503
1504ArchSpec::StopInfoOverrideCallbackType
1505ArchSpec::GetStopInfoOverrideCallback () const
1506{
1507 const llvm::Triple::ArchType machine = GetMachine();
1508 if (machine == llvm::Triple::arm)
1509 return StopInfoOverrideCallbackTypeARM;
Eugene Zelenko896ddd02016-03-02 01:09:03 +00001510 return nullptr;
Greg Claytona97c4d22014-12-09 23:31:02 +00001511}
Todd Fiala7df337f2015-10-13 23:41:19 +00001512
Jason Molenda03fe45e2015-11-06 01:43:36 +00001513bool
1514ArchSpec::IsFullySpecifiedTriple () const
1515{
1516 const auto& user_specified_triple = GetTriple();
1517
1518 bool user_triple_fully_specified = false;
1519
1520 if ((user_specified_triple.getOS() != llvm::Triple::UnknownOS) || TripleOSWasSpecified())
1521 {
1522 if ((user_specified_triple.getVendor() != llvm::Triple::UnknownVendor) || TripleVendorWasSpecified())
1523 {
1524 const unsigned unspecified = 0;
1525 if (user_specified_triple.getOSMajorVersion() != unspecified)
1526 {
1527 user_triple_fully_specified = true;
1528 }
1529 }
1530 }
1531
1532 return user_triple_fully_specified;
1533}
1534
1535void
1536ArchSpec::PiecewiseTripleCompare (const ArchSpec &other,
1537 bool &arch_different,
1538 bool &vendor_different,
1539 bool &os_different,
1540 bool &os_version_different,
1541 bool &env_different)
1542{
1543 const llvm::Triple &me(GetTriple());
1544 const llvm::Triple &them(other.GetTriple());
1545
1546 arch_different = (me.getArch() != them.getArch());
1547
1548 vendor_different = (me.getVendor() != them.getVendor());
1549
1550 os_different = (me.getOS() != them.getOS());
1551
1552 os_version_different = (me.getOSMajorVersion() != them.getOSMajorVersion());
1553
1554 env_different = (me.getEnvironment() != them.getEnvironment());
1555}
1556
Jason Molenda583b1a82016-04-05 05:01:30 +00001557bool
1558ArchSpec::IsAlwaysThumbInstructions () const
1559{
1560 std::string Error;
1561 if (GetTriple().getArch() == llvm::Triple::arm || GetTriple().getArch() == llvm::Triple::thumb)
1562 {
1563 // v. https://en.wikipedia.org/wiki/ARM_Cortex-M
1564 //
1565 // Cortex-M0 through Cortex-M7 are ARM processor cores which can only
1566 // execute thumb instructions. We map the cores to arch names like this:
1567 //
1568 // Cortex-M0, Cortex-M0+, Cortex-M1: armv6m
1569 // Cortex-M3: armv7m
1570 // Cortex-M4, Cortex-M7: armv7em
1571
1572 if (GetCore() == ArchSpec::Core::eCore_arm_armv7m
1573 || GetCore() == ArchSpec::Core::eCore_arm_armv7em
1574 || GetCore() == ArchSpec::Core::eCore_arm_armv6m)
1575 {
1576 return true;
1577 }
1578 }
1579 return false;
1580}
1581
Todd Fiala7df337f2015-10-13 23:41:19 +00001582void
1583ArchSpec::DumpTriple(Stream &s) const
1584{
1585 const llvm::Triple &triple = GetTriple();
1586 llvm::StringRef arch_str = triple.getArchName();
1587 llvm::StringRef vendor_str = triple.getVendorName();
1588 llvm::StringRef os_str = triple.getOSName();
Stephane Sezerbef0ff82016-04-05 17:29:19 +00001589 llvm::StringRef environ_str = triple.getEnvironmentName();
Todd Fiala7df337f2015-10-13 23:41:19 +00001590
1591 s.Printf("%s-%s-%s",
1592 arch_str.empty() ? "*" : arch_str.str().c_str(),
1593 vendor_str.empty() ? "*" : vendor_str.str().c_str(),
1594 os_str.empty() ? "*" : os_str.str().c_str()
1595 );
Stephane Sezerbef0ff82016-04-05 17:29:19 +00001596
1597 if (!environ_str.empty())
1598 s.Printf("-%s", environ_str.str().c_str());
Todd Fiala7df337f2015-10-13 23:41:19 +00001599}