blob: 97ae4971239b226cca864c00a941f8361f4f5b64 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- ArchSpec.cpp --------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Core/ArchSpec.h"
11
Eli Friedman50fac2f2010-06-11 04:26:08 +000012#include <stdio.h>
Jason Molendadfa424c2012-09-18 23:27:18 +000013#include <errno.h>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000014
15#include <string>
16
Saleem Abdulrasool28606952014-06-27 05:17:41 +000017#include "llvm/ADT/STLExtras.h"
Charles Davis237ad972013-08-27 05:04:33 +000018#include "llvm/Support/COFF.h"
Greg Clayton41f92322010-06-11 03:25:34 +000019#include "llvm/Support/ELF.h"
Stephen Wilsonfacebfc2011-02-24 19:13:58 +000020#include "llvm/Support/Host.h"
Jim Ingham46d005d2014-04-02 22:53:21 +000021#include "lldb/Utility/SafeMachO.h"
Greg Claytone795f1b2012-08-08 01:19:34 +000022#include "lldb/Core/RegularExpression.h"
Greg Clayton514487e2011-02-15 21:59:32 +000023#include "lldb/Host/Endian.h"
24#include "lldb/Host/Host.h"
Greg Claytoneb0103f2011-04-07 22:46:35 +000025#include "lldb/Target/Platform.h"
Greg Clayton41f92322010-06-11 03:25:34 +000026
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027using namespace lldb;
28using namespace lldb_private;
29
Greg Clayton64195a22011-02-23 00:35:02 +000030#define ARCH_SPEC_SEPARATOR_CHAR '-'
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031
Jason Molendaba813dc2012-11-04 03:20:05 +000032
33static bool cores_match (const ArchSpec::Core core1, const ArchSpec::Core core2, bool try_inverse, bool enforce_exact_match);
34
Greg Clayton64195a22011-02-23 00:35:02 +000035namespace lldb_private {
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036
Greg Clayton64195a22011-02-23 00:35:02 +000037 struct CoreDefinition
38 {
39 ByteOrder default_byte_order;
40 uint32_t addr_byte_size;
Greg Clayton357132e2011-03-26 19:14:58 +000041 uint32_t min_opcode_byte_size;
42 uint32_t max_opcode_byte_size;
Greg Clayton64195a22011-02-23 00:35:02 +000043 llvm::Triple::ArchType machine;
44 ArchSpec::Core core;
45 const char *name;
46 };
47
48}
49
50// This core information can be looked using the ArchSpec::Core as the index
51static const CoreDefinition g_core_definitions[ArchSpec::kNumCores] =
Chris Lattner30fdc8d2010-06-08 16:52:24 +000052{
Greg Clayton357132e2011-03-26 19:14:58 +000053 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_generic , "arm" },
54 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv4 , "armv4" },
55 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv4t , "armv4t" },
56 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv5 , "armv5" },
Greg Claytonb5c39fe2011-12-16 18:15:52 +000057 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv5e , "armv5e" },
Greg Clayton357132e2011-03-26 19:14:58 +000058 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv5t , "armv5t" },
59 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv6 , "armv6" },
Jason Molendaa3a04522013-09-27 23:21:54 +000060 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv6m , "armv6m" },
Greg Clayton357132e2011-03-26 19:14:58 +000061 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7 , "armv7" },
62 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7f , "armv7f" },
Greg Clayton357132e2011-03-26 19:14:58 +000063 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7s , "armv7s" },
Jason Molenda7a1559c2013-03-08 01:20:17 +000064 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7k , "armv7k" },
65 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7m , "armv7m" },
66 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7em , "armv7em" },
Greg Clayton357132e2011-03-26 19:14:58 +000067 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_xscale , "xscale" },
Greg Claytonb5c39fe2011-12-16 18:15:52 +000068 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumb , "thumb" },
69 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv4t , "thumbv4t" },
70 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv5 , "thumbv5" },
71 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv5e , "thumbv5e" },
72 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv6 , "thumbv6" },
Jason Molendaa3a04522013-09-27 23:21:54 +000073 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv6m , "thumbv6m" },
Greg Claytonb5c39fe2011-12-16 18:15:52 +000074 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7 , "thumbv7" },
75 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7f , "thumbv7f" },
Greg Claytonb5c39fe2011-12-16 18:15:52 +000076 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7s , "thumbv7s" },
Jason Molenda7a1559c2013-03-08 01:20:17 +000077 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7k , "thumbv7k" },
78 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7m , "thumbv7m" },
79 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7em , "thumbv7em" },
Jason Molendaa3329782014-03-29 18:54:20 +000080 { eByteOrderLittle, 8, 4, 4, llvm::Triple::arm64 , ArchSpec::eCore_arm_arm64 , "arm64" },
Ed Masteb73f8442013-10-10 00:59:47 +000081
82 { eByteOrderBig , 8, 4, 4, llvm::Triple::mips64 , ArchSpec::eCore_mips64 , "mips64" },
Greg Clayton64195a22011-02-23 00:35:02 +000083
Greg Clayton83b162d2013-08-12 18:34:04 +000084 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_generic , "ppc" },
85 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc601 , "ppc601" },
86 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc602 , "ppc602" },
87 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc603 , "ppc603" },
88 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc603e , "ppc603e" },
89 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc603ev , "ppc603ev" },
90 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc604 , "ppc604" },
91 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc604e , "ppc604e" },
92 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc620 , "ppc620" },
93 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc750 , "ppc750" },
94 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc7400 , "ppc7400" },
95 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc7450 , "ppc7450" },
96 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc970 , "ppc970" },
Greg Clayton64195a22011-02-23 00:35:02 +000097
Greg Clayton83b162d2013-08-12 18:34:04 +000098 { eByteOrderBig , 8, 4, 4, llvm::Triple::ppc64 , ArchSpec::eCore_ppc64_generic , "ppc64" },
99 { eByteOrderBig , 8, 4, 4, llvm::Triple::ppc64 , ArchSpec::eCore_ppc64_ppc970_64 , "ppc970-64" },
Greg Clayton64195a22011-02-23 00:35:02 +0000100
Greg Clayton357132e2011-03-26 19:14:58 +0000101 { eByteOrderLittle, 4, 4, 4, llvm::Triple::sparc , ArchSpec::eCore_sparc_generic , "sparc" },
102 { eByteOrderLittle, 8, 4, 4, llvm::Triple::sparcv9, ArchSpec::eCore_sparc9_generic , "sparcv9" },
Greg Clayton64195a22011-02-23 00:35:02 +0000103
Greg Claytonab65b342011-04-13 22:47:15 +0000104 { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i386 , "i386" },
105 { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i486 , "i486" },
106 { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i486sx , "i486sx" },
Virgile Bello97a70e42014-04-08 14:48:48 +0000107 { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i686 , "i686" },
Greg Clayton64195a22011-02-23 00:35:02 +0000108
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000109 { eByteOrderLittle, 8, 1, 15, llvm::Triple::x86_64 , ArchSpec::eCore_x86_64_x86_64 , "x86_64" },
Greg Claytona86dc432014-01-22 23:42:03 +0000110 { eByteOrderLittle, 8, 1, 15, llvm::Triple::x86_64 , ArchSpec::eCore_x86_64_x86_64h , "x86_64h" },
Deepak Panickal6d3df422014-02-19 11:16:46 +0000111 { eByteOrderLittle, 4, 4, 4, llvm::Triple::hexagon , ArchSpec::eCore_hexagon_generic, "hexagon" },
112 { eByteOrderLittle, 4, 4, 4, llvm::Triple::hexagon , ArchSpec::eCore_hexagon_hexagonv4, "hexagonv4" },
113 { eByteOrderLittle, 4, 4, 4, llvm::Triple::hexagon , ArchSpec::eCore_hexagon_hexagonv5, "hexagonv5" },
114
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000115 { eByteOrderLittle, 4, 4, 4 , llvm::Triple::UnknownArch , ArchSpec::eCore_uknownMach32 , "unknown-mach-32" },
Todd Fiala14bbef52014-07-01 23:33:32 +0000116 { eByteOrderLittle, 8, 4, 4 , llvm::Triple::UnknownArch , ArchSpec::eCore_uknownMach64 , "unknown-mach-64" },
117
118 { eByteOrderLittle, 4, 1, 1 , llvm::Triple::UnknownArch , ArchSpec::eCore_kalimba , "kalimba" }
Greg Clayton64195a22011-02-23 00:35:02 +0000119};
120
121struct ArchDefinitionEntry
122{
123 ArchSpec::Core core;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000124 uint32_t cpu;
125 uint32_t sub;
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000126 uint32_t cpu_mask;
127 uint32_t sub_mask;
Greg Clayton64195a22011-02-23 00:35:02 +0000128};
129
130struct ArchDefinition
131{
132 ArchitectureType type;
133 size_t num_entries;
134 const ArchDefinitionEntry *entries;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000135 const char *name;
136};
137
Greg Clayton41f92322010-06-11 03:25:34 +0000138
Greg Claytonc7bece562013-01-25 18:06:21 +0000139size_t
Greg Claytonab65b342011-04-13 22:47:15 +0000140ArchSpec::AutoComplete (const char *name, StringList &matches)
141{
142 uint32_t i;
143 if (name && name[0])
144 {
145 for (i = 0; i < ArchSpec::kNumCores; ++i)
146 {
147 if (NameMatches(g_core_definitions[i].name, eNameMatchStartsWith, name))
148 matches.AppendString (g_core_definitions[i].name);
149 }
150 }
151 else
152 {
153 for (i = 0; i < ArchSpec::kNumCores; ++i)
154 matches.AppendString (g_core_definitions[i].name);
155 }
156 return matches.GetSize();
157}
158
159
160
Greg Clayton64195a22011-02-23 00:35:02 +0000161#define CPU_ANY (UINT32_MAX)
162
163//===----------------------------------------------------------------------===//
164// A table that gets searched linearly for matches. This table is used to
165// convert cpu type and subtypes to architecture names, and to convert
166// architecture names to cpu types and subtypes. The ordering is important and
167// allows the precedence to be set when the table is built.
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000168#define SUBTYPE_MASK 0x00FFFFFFu
Greg Clayton64195a22011-02-23 00:35:02 +0000169static const ArchDefinitionEntry g_macho_arch_entries[] =
Greg Clayton41f92322010-06-11 03:25:34 +0000170{
Charles Davis510938e2013-08-27 05:04:57 +0000171 { ArchSpec::eCore_arm_generic , llvm::MachO::CPU_TYPE_ARM , CPU_ANY, UINT32_MAX , UINT32_MAX },
172 { ArchSpec::eCore_arm_generic , llvm::MachO::CPU_TYPE_ARM , 0 , UINT32_MAX , SUBTYPE_MASK },
173 { ArchSpec::eCore_arm_armv4 , llvm::MachO::CPU_TYPE_ARM , 5 , UINT32_MAX , SUBTYPE_MASK },
174 { ArchSpec::eCore_arm_armv4t , llvm::MachO::CPU_TYPE_ARM , 5 , UINT32_MAX , SUBTYPE_MASK },
175 { ArchSpec::eCore_arm_armv6 , llvm::MachO::CPU_TYPE_ARM , 6 , UINT32_MAX , SUBTYPE_MASK },
Jason Molenda64a11732013-10-08 03:01:08 +0000176 { ArchSpec::eCore_arm_armv6m , llvm::MachO::CPU_TYPE_ARM , 14 , UINT32_MAX , SUBTYPE_MASK },
Charles Davis510938e2013-08-27 05:04:57 +0000177 { ArchSpec::eCore_arm_armv5 , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
178 { ArchSpec::eCore_arm_armv5e , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
179 { ArchSpec::eCore_arm_armv5t , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
180 { ArchSpec::eCore_arm_xscale , llvm::MachO::CPU_TYPE_ARM , 8 , UINT32_MAX , SUBTYPE_MASK },
181 { ArchSpec::eCore_arm_armv7 , llvm::MachO::CPU_TYPE_ARM , 9 , UINT32_MAX , SUBTYPE_MASK },
182 { ArchSpec::eCore_arm_armv7f , llvm::MachO::CPU_TYPE_ARM , 10 , UINT32_MAX , SUBTYPE_MASK },
183 { ArchSpec::eCore_arm_armv7s , llvm::MachO::CPU_TYPE_ARM , 11 , UINT32_MAX , SUBTYPE_MASK },
184 { ArchSpec::eCore_arm_armv7k , llvm::MachO::CPU_TYPE_ARM , 12 , UINT32_MAX , SUBTYPE_MASK },
185 { ArchSpec::eCore_arm_armv7m , llvm::MachO::CPU_TYPE_ARM , 15 , UINT32_MAX , SUBTYPE_MASK },
186 { ArchSpec::eCore_arm_armv7em , llvm::MachO::CPU_TYPE_ARM , 16 , UINT32_MAX , SUBTYPE_MASK },
Jason Molendaa3329782014-03-29 18:54:20 +0000187 { ArchSpec::eCore_arm_arm64 , llvm::MachO::CPU_TYPE_ARM64 , CPU_ANY, UINT32_MAX , SUBTYPE_MASK },
188 { ArchSpec::eCore_arm_arm64 , llvm::MachO::CPU_TYPE_ARM64 , 0 , UINT32_MAX , SUBTYPE_MASK },
189 { ArchSpec::eCore_arm_arm64 , llvm::MachO::CPU_TYPE_ARM64 , 1 , UINT32_MAX , SUBTYPE_MASK },
190 { ArchSpec::eCore_arm_arm64 , llvm::MachO::CPU_TYPE_ARM64 , 13 , UINT32_MAX , SUBTYPE_MASK },
Charles Davis510938e2013-08-27 05:04:57 +0000191 { ArchSpec::eCore_thumb , llvm::MachO::CPU_TYPE_ARM , 0 , UINT32_MAX , SUBTYPE_MASK },
192 { ArchSpec::eCore_thumbv4t , llvm::MachO::CPU_TYPE_ARM , 5 , UINT32_MAX , SUBTYPE_MASK },
193 { ArchSpec::eCore_thumbv5 , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
194 { ArchSpec::eCore_thumbv5e , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
195 { ArchSpec::eCore_thumbv6 , llvm::MachO::CPU_TYPE_ARM , 6 , UINT32_MAX , SUBTYPE_MASK },
Jason Molenda64a11732013-10-08 03:01:08 +0000196 { ArchSpec::eCore_thumbv6m , llvm::MachO::CPU_TYPE_ARM , 14 , UINT32_MAX , SUBTYPE_MASK },
Charles Davis510938e2013-08-27 05:04:57 +0000197 { ArchSpec::eCore_thumbv7 , llvm::MachO::CPU_TYPE_ARM , 9 , UINT32_MAX , SUBTYPE_MASK },
198 { ArchSpec::eCore_thumbv7f , llvm::MachO::CPU_TYPE_ARM , 10 , UINT32_MAX , SUBTYPE_MASK },
199 { ArchSpec::eCore_thumbv7s , llvm::MachO::CPU_TYPE_ARM , 11 , UINT32_MAX , SUBTYPE_MASK },
200 { ArchSpec::eCore_thumbv7k , llvm::MachO::CPU_TYPE_ARM , 12 , UINT32_MAX , SUBTYPE_MASK },
201 { ArchSpec::eCore_thumbv7m , llvm::MachO::CPU_TYPE_ARM , 15 , UINT32_MAX , SUBTYPE_MASK },
202 { ArchSpec::eCore_thumbv7em , llvm::MachO::CPU_TYPE_ARM , 16 , UINT32_MAX , SUBTYPE_MASK },
203 { ArchSpec::eCore_ppc_generic , llvm::MachO::CPU_TYPE_POWERPC , CPU_ANY, UINT32_MAX , UINT32_MAX },
204 { ArchSpec::eCore_ppc_generic , llvm::MachO::CPU_TYPE_POWERPC , 0 , UINT32_MAX , SUBTYPE_MASK },
205 { ArchSpec::eCore_ppc_ppc601 , llvm::MachO::CPU_TYPE_POWERPC , 1 , UINT32_MAX , SUBTYPE_MASK },
206 { ArchSpec::eCore_ppc_ppc602 , llvm::MachO::CPU_TYPE_POWERPC , 2 , UINT32_MAX , SUBTYPE_MASK },
207 { ArchSpec::eCore_ppc_ppc603 , llvm::MachO::CPU_TYPE_POWERPC , 3 , UINT32_MAX , SUBTYPE_MASK },
208 { ArchSpec::eCore_ppc_ppc603e , llvm::MachO::CPU_TYPE_POWERPC , 4 , UINT32_MAX , SUBTYPE_MASK },
209 { ArchSpec::eCore_ppc_ppc603ev , llvm::MachO::CPU_TYPE_POWERPC , 5 , UINT32_MAX , SUBTYPE_MASK },
210 { ArchSpec::eCore_ppc_ppc604 , llvm::MachO::CPU_TYPE_POWERPC , 6 , UINT32_MAX , SUBTYPE_MASK },
211 { ArchSpec::eCore_ppc_ppc604e , llvm::MachO::CPU_TYPE_POWERPC , 7 , UINT32_MAX , SUBTYPE_MASK },
212 { ArchSpec::eCore_ppc_ppc620 , llvm::MachO::CPU_TYPE_POWERPC , 8 , UINT32_MAX , SUBTYPE_MASK },
213 { ArchSpec::eCore_ppc_ppc750 , llvm::MachO::CPU_TYPE_POWERPC , 9 , UINT32_MAX , SUBTYPE_MASK },
214 { ArchSpec::eCore_ppc_ppc7400 , llvm::MachO::CPU_TYPE_POWERPC , 10 , UINT32_MAX , SUBTYPE_MASK },
215 { ArchSpec::eCore_ppc_ppc7450 , llvm::MachO::CPU_TYPE_POWERPC , 11 , UINT32_MAX , SUBTYPE_MASK },
216 { ArchSpec::eCore_ppc_ppc970 , llvm::MachO::CPU_TYPE_POWERPC , 100 , UINT32_MAX , SUBTYPE_MASK },
217 { ArchSpec::eCore_ppc64_generic , llvm::MachO::CPU_TYPE_POWERPC64 , 0 , UINT32_MAX , SUBTYPE_MASK },
218 { ArchSpec::eCore_ppc64_ppc970_64 , llvm::MachO::CPU_TYPE_POWERPC64 , 100 , UINT32_MAX , SUBTYPE_MASK },
219 { ArchSpec::eCore_x86_32_i386 , llvm::MachO::CPU_TYPE_I386 , 3 , UINT32_MAX , SUBTYPE_MASK },
220 { ArchSpec::eCore_x86_32_i486 , llvm::MachO::CPU_TYPE_I386 , 4 , UINT32_MAX , SUBTYPE_MASK },
221 { ArchSpec::eCore_x86_32_i486sx , llvm::MachO::CPU_TYPE_I386 , 0x84 , UINT32_MAX , SUBTYPE_MASK },
Greg Claytona86dc432014-01-22 23:42:03 +0000222 { ArchSpec::eCore_x86_32_i386 , llvm::MachO::CPU_TYPE_I386 , CPU_ANY, UINT32_MAX , UINT32_MAX },
Charles Davis510938e2013-08-27 05:04:57 +0000223 { ArchSpec::eCore_x86_64_x86_64 , llvm::MachO::CPU_TYPE_X86_64 , 3 , UINT32_MAX , SUBTYPE_MASK },
224 { ArchSpec::eCore_x86_64_x86_64 , llvm::MachO::CPU_TYPE_X86_64 , 4 , UINT32_MAX , SUBTYPE_MASK },
Greg Claytona86dc432014-01-22 23:42:03 +0000225 { ArchSpec::eCore_x86_64_x86_64h , llvm::MachO::CPU_TYPE_X86_64 , 8 , UINT32_MAX , SUBTYPE_MASK },
226 { 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 +0000227 // Catch any unknown mach architectures so we can always use the object and symbol mach-o files
Charles Davis510938e2013-08-27 05:04:57 +0000228 { ArchSpec::eCore_uknownMach32 , 0 , 0 , 0xFF000000u, 0x00000000u },
229 { ArchSpec::eCore_uknownMach64 , llvm::MachO::CPU_ARCH_ABI64 , 0 , 0xFF000000u, 0x00000000u }
Greg Clayton64195a22011-02-23 00:35:02 +0000230};
231static const ArchDefinition g_macho_arch_def = {
232 eArchTypeMachO,
Saleem Abdulrasool28606952014-06-27 05:17:41 +0000233 llvm::array_lengthof(g_macho_arch_entries),
Greg Clayton64195a22011-02-23 00:35:02 +0000234 g_macho_arch_entries,
Greg Clayton64195a22011-02-23 00:35:02 +0000235 "mach-o"
Greg Clayton41f92322010-06-11 03:25:34 +0000236};
237
Greg Clayton64195a22011-02-23 00:35:02 +0000238//===----------------------------------------------------------------------===//
239// A table that gets searched linearly for matches. This table is used to
240// convert cpu type and subtypes to architecture names, and to convert
241// architecture names to cpu types and subtypes. The ordering is important and
242// allows the precedence to be set when the table is built.
243static const ArchDefinitionEntry g_elf_arch_entries[] =
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000244{
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000245 { ArchSpec::eCore_sparc_generic , llvm::ELF::EM_SPARC , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // Sparc
246 { ArchSpec::eCore_x86_32_i386 , llvm::ELF::EM_386 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // Intel 80386
247 { ArchSpec::eCore_x86_32_i486 , llvm::ELF::EM_486 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // Intel 486 (deprecated)
248 { ArchSpec::eCore_ppc_generic , llvm::ELF::EM_PPC , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // PowerPC
249 { ArchSpec::eCore_ppc64_generic , llvm::ELF::EM_PPC64 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // PowerPC64
250 { ArchSpec::eCore_arm_generic , llvm::ELF::EM_ARM , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // ARM
251 { ArchSpec::eCore_sparc9_generic , llvm::ELF::EM_SPARCV9, LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // SPARC V9
Ed Masteb73f8442013-10-10 00:59:47 +0000252 { ArchSpec::eCore_x86_64_x86_64 , llvm::ELF::EM_X86_64 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // AMD64
Deepak Panickal6d3df422014-02-19 11:16:46 +0000253 { ArchSpec::eCore_mips64 , llvm::ELF::EM_MIPS , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // MIPS
Todd Fiala14bbef52014-07-01 23:33:32 +0000254 { ArchSpec::eCore_hexagon_generic , llvm::ELF::EM_HEXAGON, LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // HEXAGON
255 { ArchSpec::eCore_kalimba , llvm::ELF::EM_CSR_KALIMBA, LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu } // KALIMBA
256
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000257};
258
Greg Clayton64195a22011-02-23 00:35:02 +0000259static const ArchDefinition g_elf_arch_def = {
260 eArchTypeELF,
Saleem Abdulrasool28606952014-06-27 05:17:41 +0000261 llvm::array_lengthof(g_elf_arch_entries),
Greg Clayton64195a22011-02-23 00:35:02 +0000262 g_elf_arch_entries,
Greg Clayton64195a22011-02-23 00:35:02 +0000263 "elf",
Greg Clayton41f92322010-06-11 03:25:34 +0000264};
265
Charles Davis237ad972013-08-27 05:04:33 +0000266static const ArchDefinitionEntry g_coff_arch_entries[] =
267{
268 { ArchSpec::eCore_x86_32_i386 , llvm::COFF::IMAGE_FILE_MACHINE_I386 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // Intel 80386
269 { ArchSpec::eCore_ppc_generic , llvm::COFF::IMAGE_FILE_MACHINE_POWERPC , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // PowerPC
270 { ArchSpec::eCore_ppc_generic , llvm::COFF::IMAGE_FILE_MACHINE_POWERPCFP, LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // PowerPC (with FPU)
271 { ArchSpec::eCore_arm_generic , llvm::COFF::IMAGE_FILE_MACHINE_ARM , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // ARM
Saleem Abdulrasool1108cb32014-03-11 03:09:08 +0000272 { ArchSpec::eCore_arm_armv7 , llvm::COFF::IMAGE_FILE_MACHINE_ARMNT , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // ARMv7
Charles Davis237ad972013-08-27 05:04:33 +0000273 { ArchSpec::eCore_thumb , llvm::COFF::IMAGE_FILE_MACHINE_THUMB , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // ARMv7
274 { ArchSpec::eCore_x86_64_x86_64, llvm::COFF::IMAGE_FILE_MACHINE_AMD64 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu } // AMD64
275};
276
277static const ArchDefinition g_coff_arch_def = {
278 eArchTypeCOFF,
Saleem Abdulrasool28606952014-06-27 05:17:41 +0000279 llvm::array_lengthof(g_coff_arch_entries),
Charles Davis237ad972013-08-27 05:04:33 +0000280 g_coff_arch_entries,
281 "pe-coff",
282};
283
Greg Clayton64195a22011-02-23 00:35:02 +0000284//===----------------------------------------------------------------------===//
285// Table of all ArchDefinitions
286static const ArchDefinition *g_arch_definitions[] = {
287 &g_macho_arch_def,
Charles Davis237ad972013-08-27 05:04:33 +0000288 &g_elf_arch_def,
289 &g_coff_arch_def
Greg Clayton64195a22011-02-23 00:35:02 +0000290};
Greg Clayton41f92322010-06-11 03:25:34 +0000291
Saleem Abdulrasool28606952014-06-27 05:17:41 +0000292static const size_t k_num_arch_definitions = llvm::array_lengthof(g_arch_definitions);
Greg Clayton64195a22011-02-23 00:35:02 +0000293
294//===----------------------------------------------------------------------===//
295// Static helper functions.
296
297
298// Get the architecture definition for a given object type.
299static const ArchDefinition *
300FindArchDefinition (ArchitectureType arch_type)
301{
302 for (unsigned int i = 0; i < k_num_arch_definitions; ++i)
303 {
304 const ArchDefinition *def = g_arch_definitions[i];
305 if (def->type == arch_type)
306 return def;
307 }
308 return NULL;
309}
310
311// Get an architecture definition by name.
312static const CoreDefinition *
313FindCoreDefinition (llvm::StringRef name)
314{
315 for (unsigned int i = 0; i < ArchSpec::kNumCores; ++i)
316 {
317 if (name.equals_lower(g_core_definitions[i].name))
318 return &g_core_definitions[i];
319 }
320 return NULL;
321}
322
323static inline const CoreDefinition *
324FindCoreDefinition (ArchSpec::Core core)
325{
326 if (core >= 0 && core < ArchSpec::kNumCores)
327 return &g_core_definitions[core];
328 return NULL;
329}
330
331// Get a definition entry by cpu type and subtype.
332static const ArchDefinitionEntry *
333FindArchDefinitionEntry (const ArchDefinition *def, uint32_t cpu, uint32_t sub)
334{
335 if (def == NULL)
336 return NULL;
337
Greg Clayton64195a22011-02-23 00:35:02 +0000338 const ArchDefinitionEntry *entries = def->entries;
339 for (size_t i = 0; i < def->num_entries; ++i)
340 {
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000341 if (entries[i].cpu == (cpu & entries[i].cpu_mask))
342 if (entries[i].sub == (sub & entries[i].sub_mask))
343 return &entries[i];
Greg Clayton64195a22011-02-23 00:35:02 +0000344 }
345 return NULL;
346}
347
348static const ArchDefinitionEntry *
349FindArchDefinitionEntry (const ArchDefinition *def, ArchSpec::Core core)
350{
351 if (def == NULL)
352 return NULL;
353
354 const ArchDefinitionEntry *entries = def->entries;
355 for (size_t i = 0; i < def->num_entries; ++i)
356 {
357 if (entries[i].core == core)
358 return &entries[i];
359 }
360 return NULL;
361}
362
363//===----------------------------------------------------------------------===//
364// Constructors and destructors.
365
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000366ArchSpec::ArchSpec() :
Greg Clayton514487e2011-02-15 21:59:32 +0000367 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000368 m_core (kCore_invalid),
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000369 m_byte_order (eByteOrderInvalid),
370 m_distribution_id ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000371{
372}
373
Greg Claytoneb0103f2011-04-07 22:46:35 +0000374ArchSpec::ArchSpec (const char *triple_cstr, Platform *platform) :
Greg Clayton514487e2011-02-15 21:59:32 +0000375 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000376 m_core (kCore_invalid),
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000377 m_byte_order (eByteOrderInvalid),
378 m_distribution_id ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000379{
Greg Clayton64195a22011-02-23 00:35:02 +0000380 if (triple_cstr)
Greg Claytoneb0103f2011-04-07 22:46:35 +0000381 SetTriple(triple_cstr, platform);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000382}
383
Greg Clayton70512312012-05-08 01:45:38 +0000384
385ArchSpec::ArchSpec (const char *triple_cstr) :
386 m_triple (),
387 m_core (kCore_invalid),
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000388 m_byte_order (eByteOrderInvalid),
389 m_distribution_id ()
Greg Clayton70512312012-05-08 01:45:38 +0000390{
391 if (triple_cstr)
392 SetTriple(triple_cstr);
393}
394
Greg Clayton64195a22011-02-23 00:35:02 +0000395ArchSpec::ArchSpec(const llvm::Triple &triple) :
Greg Clayton514487e2011-02-15 21:59:32 +0000396 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000397 m_core (kCore_invalid),
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000398 m_byte_order (eByteOrderInvalid),
399 m_distribution_id ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000400{
Greg Clayton64195a22011-02-23 00:35:02 +0000401 SetTriple(triple);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000402}
403
Greg Claytone0d378b2011-03-24 21:19:54 +0000404ArchSpec::ArchSpec (ArchitectureType arch_type, uint32_t cpu, uint32_t subtype) :
Greg Clayton64195a22011-02-23 00:35:02 +0000405 m_triple (),
406 m_core (kCore_invalid),
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000407 m_byte_order (eByteOrderInvalid),
408 m_distribution_id ()
Greg Clayton64195a22011-02-23 00:35:02 +0000409{
410 SetArchitecture (arch_type, cpu, subtype);
411}
412
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000413ArchSpec::~ArchSpec()
414{
415}
416
Greg Clayton64195a22011-02-23 00:35:02 +0000417//===----------------------------------------------------------------------===//
418// Assignment and initialization.
419
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000420const ArchSpec&
421ArchSpec::operator= (const ArchSpec& rhs)
422{
423 if (this != &rhs)
424 {
Greg Clayton514487e2011-02-15 21:59:32 +0000425 m_triple = rhs.m_triple;
Greg Clayton64195a22011-02-23 00:35:02 +0000426 m_core = rhs.m_core;
Greg Clayton514487e2011-02-15 21:59:32 +0000427 m_byte_order = rhs.m_byte_order;
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000428 m_distribution_id = rhs.m_distribution_id;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000429 }
430 return *this;
431}
432
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000433void
434ArchSpec::Clear()
435{
Greg Clayton514487e2011-02-15 21:59:32 +0000436 m_triple = llvm::Triple();
Greg Clayton64195a22011-02-23 00:35:02 +0000437 m_core = kCore_invalid;
438 m_byte_order = eByteOrderInvalid;
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000439 m_distribution_id.Clear ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000440}
441
Greg Clayton64195a22011-02-23 00:35:02 +0000442//===----------------------------------------------------------------------===//
443// Predicates.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000444
Greg Clayton41f92322010-06-11 03:25:34 +0000445
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000446const char *
Greg Clayton64195a22011-02-23 00:35:02 +0000447ArchSpec::GetArchitectureName () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000448{
Greg Clayton64195a22011-02-23 00:35:02 +0000449 const CoreDefinition *core_def = FindCoreDefinition (m_core);
450 if (core_def)
451 return core_def->name;
452 return "unknown";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000453}
454
Greg Clayton64195a22011-02-23 00:35:02 +0000455uint32_t
456ArchSpec::GetMachOCPUType () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000457{
Greg Clayton64195a22011-02-23 00:35:02 +0000458 const CoreDefinition *core_def = FindCoreDefinition (m_core);
459 if (core_def)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000460 {
Greg Clayton64195a22011-02-23 00:35:02 +0000461 const ArchDefinitionEntry *arch_def = FindArchDefinitionEntry (&g_macho_arch_def, core_def->core);
462 if (arch_def)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000463 {
Greg Clayton64195a22011-02-23 00:35:02 +0000464 return arch_def->cpu;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000465 }
466 }
Greg Clayton64195a22011-02-23 00:35:02 +0000467 return LLDB_INVALID_CPUTYPE;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000468}
469
Greg Clayton64195a22011-02-23 00:35:02 +0000470uint32_t
471ArchSpec::GetMachOCPUSubType () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000472{
Greg Clayton64195a22011-02-23 00:35:02 +0000473 const CoreDefinition *core_def = FindCoreDefinition (m_core);
474 if (core_def)
475 {
476 const ArchDefinitionEntry *arch_def = FindArchDefinitionEntry (&g_macho_arch_def, core_def->core);
477 if (arch_def)
478 {
Greg Clayton1cb64962011-03-24 04:28:38 +0000479 return arch_def->sub;
Greg Clayton64195a22011-02-23 00:35:02 +0000480 }
481 }
482 return LLDB_INVALID_CPUTYPE;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000483}
484
Greg Clayton64195a22011-02-23 00:35:02 +0000485llvm::Triple::ArchType
486ArchSpec::GetMachine () const
487{
488 const CoreDefinition *core_def = FindCoreDefinition (m_core);
489 if (core_def)
490 return core_def->machine;
491
492 return llvm::Triple::UnknownArch;
493}
494
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000495const ConstString&
496ArchSpec::GetDistributionId () const
497{
498 return m_distribution_id;
499}
500
501void
502ArchSpec::SetDistributionId (const char* distribution_id)
503{
504 m_distribution_id.SetCString (distribution_id);
505}
506
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000507uint32_t
508ArchSpec::GetAddressByteSize() const
509{
Greg Clayton64195a22011-02-23 00:35:02 +0000510 const CoreDefinition *core_def = FindCoreDefinition (m_core);
511 if (core_def)
512 return core_def->addr_byte_size;
Greg Clayton41f92322010-06-11 03:25:34 +0000513 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000514}
515
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000516ByteOrder
517ArchSpec::GetDefaultEndian () const
518{
Greg Clayton64195a22011-02-23 00:35:02 +0000519 const CoreDefinition *core_def = FindCoreDefinition (m_core);
520 if (core_def)
521 return core_def->default_byte_order;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000522 return eByteOrderInvalid;
523}
524
Greg Clayton64195a22011-02-23 00:35:02 +0000525lldb::ByteOrder
526ArchSpec::GetByteOrder () const
527{
528 if (m_byte_order == eByteOrderInvalid)
529 return GetDefaultEndian();
530 return m_byte_order;
531}
532
533//===----------------------------------------------------------------------===//
534// Mutators.
535
536bool
537ArchSpec::SetTriple (const llvm::Triple &triple)
538{
539 m_triple = triple;
540
541 llvm::StringRef arch_name (m_triple.getArchName());
542 const CoreDefinition *core_def = FindCoreDefinition (arch_name);
543 if (core_def)
544 {
545 m_core = core_def->core;
Greg Claytoneb0103f2011-04-07 22:46:35 +0000546 // Set the byte order to the default byte order for an architecture.
547 // This can be modified if needed for cases when cores handle both
548 // big and little endian
549 m_byte_order = core_def->default_byte_order;
Greg Clayton64195a22011-02-23 00:35:02 +0000550 }
551 else
552 {
553 Clear();
554 }
555
556
557 return IsValid();
558}
559
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000560static bool
561ParseMachCPUDashSubtypeTriple (const char *triple_cstr, ArchSpec &arch)
562{
563 // Accept "12-10" or "12.10" as cpu type/subtype
564 if (isdigit(triple_cstr[0]))
565 {
566 char *end = NULL;
567 errno = 0;
Greg Claytonc7bece562013-01-25 18:06:21 +0000568 uint32_t cpu = (uint32_t)::strtoul (triple_cstr, &end, 0);
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000569 if (errno == 0 && cpu != 0 && end && ((*end == '-') || (*end == '.')))
570 {
571 errno = 0;
Greg Claytonc7bece562013-01-25 18:06:21 +0000572 uint32_t sub = (uint32_t)::strtoul (end + 1, &end, 0);
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000573 if (errno == 0 && end && ((*end == '-') || (*end == '.') || (*end == '\0')))
574 {
575 if (arch.SetArchitecture (eArchTypeMachO, cpu, sub))
576 {
577 if (*end == '-')
578 {
579 llvm::StringRef vendor_os (end + 1);
580 size_t dash_pos = vendor_os.find('-');
581 if (dash_pos != llvm::StringRef::npos)
582 {
583 llvm::StringRef vendor_str(vendor_os.substr(0, dash_pos));
584 arch.GetTriple().setVendorName(vendor_str);
585 const size_t vendor_start_pos = dash_pos+1;
Greg Claytonc7bece562013-01-25 18:06:21 +0000586 dash_pos = vendor_os.find('-', vendor_start_pos);
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000587 if (dash_pos == llvm::StringRef::npos)
588 {
589 if (vendor_start_pos < vendor_os.size())
590 arch.GetTriple().setOSName(vendor_os.substr(vendor_start_pos));
591 }
592 else
593 {
594 arch.GetTriple().setOSName(vendor_os.substr(vendor_start_pos, dash_pos - vendor_start_pos));
595 }
596 }
597 }
598 return true;
599 }
600 }
601 }
602 }
603 return false;
604}
Greg Clayton64195a22011-02-23 00:35:02 +0000605bool
Greg Clayton70512312012-05-08 01:45:38 +0000606ArchSpec::SetTriple (const char *triple_cstr)
Greg Clayton64195a22011-02-23 00:35:02 +0000607{
Greg Clayton23aca092011-08-12 23:32:52 +0000608 if (triple_cstr && triple_cstr[0])
Greg Clayton64195a22011-02-23 00:35:02 +0000609 {
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000610 if (ParseMachCPUDashSubtypeTriple (triple_cstr, *this))
611 return true;
612
Greg Clayton64195a22011-02-23 00:35:02 +0000613 llvm::StringRef triple_stref (triple_cstr);
614 if (triple_stref.startswith (LLDB_ARCH_DEFAULT))
615 {
616 // Special case for the current host default architectures...
617 if (triple_stref.equals (LLDB_ARCH_DEFAULT_32BIT))
618 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture32);
619 else if (triple_stref.equals (LLDB_ARCH_DEFAULT_64BIT))
620 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture64);
621 else if (triple_stref.equals (LLDB_ARCH_DEFAULT))
622 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture);
623 }
624 else
625 {
626 std::string normalized_triple_sstr (llvm::Triple::normalize(triple_stref));
627 triple_stref = normalized_triple_sstr;
Greg Clayton70512312012-05-08 01:45:38 +0000628 SetTriple (llvm::Triple (triple_stref));
629 }
630 }
631 else
632 Clear();
633 return IsValid();
634}
635
636bool
637ArchSpec::SetTriple (const char *triple_cstr, Platform *platform)
638{
639 if (triple_cstr && triple_cstr[0])
640 {
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000641 if (ParseMachCPUDashSubtypeTriple (triple_cstr, *this))
642 return true;
643
Greg Clayton70512312012-05-08 01:45:38 +0000644 llvm::StringRef triple_stref (triple_cstr);
645 if (triple_stref.startswith (LLDB_ARCH_DEFAULT))
646 {
647 // Special case for the current host default architectures...
648 if (triple_stref.equals (LLDB_ARCH_DEFAULT_32BIT))
649 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture32);
650 else if (triple_stref.equals (LLDB_ARCH_DEFAULT_64BIT))
651 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture64);
652 else if (triple_stref.equals (LLDB_ARCH_DEFAULT))
653 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture);
654 }
655 else
656 {
657 ArchSpec raw_arch (triple_cstr);
658
659 std::string normalized_triple_sstr (llvm::Triple::normalize(triple_stref));
660 triple_stref = normalized_triple_sstr;
Greg Claytoneb0103f2011-04-07 22:46:35 +0000661 llvm::Triple normalized_triple (triple_stref);
662
663 const bool os_specified = normalized_triple.getOSName().size() > 0;
664 const bool vendor_specified = normalized_triple.getVendorName().size() > 0;
665 const bool env_specified = normalized_triple.getEnvironmentName().size() > 0;
666
667 // If we got an arch only, then default the vendor, os, environment
668 // to match the platform if one is supplied
669 if (!(os_specified || vendor_specified || env_specified))
670 {
671 if (platform)
672 {
673 // If we were given a platform, use the platform's system
674 // architecture. If this is not available (might not be
675 // connected) use the first supported architecture.
Greg Clayton70512312012-05-08 01:45:38 +0000676 ArchSpec compatible_arch;
Greg Clayton1e0c8842013-01-11 20:49:54 +0000677 if (platform->IsCompatibleArchitecture (raw_arch, false, &compatible_arch))
Greg Claytoneb0103f2011-04-07 22:46:35 +0000678 {
Greg Clayton70512312012-05-08 01:45:38 +0000679 if (compatible_arch.IsValid())
680 {
681 const llvm::Triple &compatible_triple = compatible_arch.GetTriple();
682 if (!vendor_specified)
683 normalized_triple.setVendor(compatible_triple.getVendor());
684 if (!os_specified)
685 normalized_triple.setOS(compatible_triple.getOS());
686 if (!env_specified && compatible_triple.getEnvironmentName().size())
687 normalized_triple.setEnvironment(compatible_triple.getEnvironment());
688 }
Greg Claytoneb0103f2011-04-07 22:46:35 +0000689 }
Greg Clayton70512312012-05-08 01:45:38 +0000690 else
Greg Claytoneb0103f2011-04-07 22:46:35 +0000691 {
Greg Clayton70512312012-05-08 01:45:38 +0000692 *this = raw_arch;
693 return IsValid();
Greg Claytoneb0103f2011-04-07 22:46:35 +0000694 }
695 }
696 else
697 {
698 // No platform specified, fall back to the host system for
699 // the default vendor, os, and environment.
Sean Callananbfb237bc2011-11-04 22:46:46 +0000700 llvm::Triple host_triple(llvm::sys::getDefaultTargetTriple());
Greg Clayton70512312012-05-08 01:45:38 +0000701 if (!vendor_specified)
702 normalized_triple.setVendor(host_triple.getVendor());
703 if (!vendor_specified)
704 normalized_triple.setOS(host_triple.getOS());
705 if (!env_specified && host_triple.getEnvironmentName().size())
706 normalized_triple.setEnvironment(host_triple.getEnvironment());
Greg Claytoneb0103f2011-04-07 22:46:35 +0000707 }
708 }
709 SetTriple (normalized_triple);
Greg Clayton64195a22011-02-23 00:35:02 +0000710 }
711 }
712 else
713 Clear();
714 return IsValid();
715}
716
Greg Clayton64195a22011-02-23 00:35:02 +0000717bool
Greg Claytone0d378b2011-03-24 21:19:54 +0000718ArchSpec::SetArchitecture (ArchitectureType arch_type, uint32_t cpu, uint32_t sub)
Greg Clayton64195a22011-02-23 00:35:02 +0000719{
720 m_core = kCore_invalid;
721 bool update_triple = true;
722 const ArchDefinition *arch_def = FindArchDefinition(arch_type);
723 if (arch_def)
724 {
725 const ArchDefinitionEntry *arch_def_entry = FindArchDefinitionEntry (arch_def, cpu, sub);
726 if (arch_def_entry)
727 {
728 const CoreDefinition *core_def = FindCoreDefinition (arch_def_entry->core);
729 if (core_def)
730 {
731 m_core = core_def->core;
732 update_triple = false;
Greg Clayton593577a2011-09-21 03:57:31 +0000733 // Always use the architecture name because it might be more descriptive
734 // than the architecture enum ("armv7" -> llvm::Triple::arm).
735 m_triple.setArchName(llvm::StringRef(core_def->name));
Greg Clayton64195a22011-02-23 00:35:02 +0000736 if (arch_type == eArchTypeMachO)
737 {
738 m_triple.setVendor (llvm::Triple::Apple);
Greg Clayton70512312012-05-08 01:45:38 +0000739
740 switch (core_def->machine)
741 {
Jason Molendaa3329782014-03-29 18:54:20 +0000742 case llvm::Triple::arm64:
Greg Clayton70512312012-05-08 01:45:38 +0000743 case llvm::Triple::arm:
744 case llvm::Triple::thumb:
745 m_triple.setOS (llvm::Triple::IOS);
746 break;
747
748 case llvm::Triple::x86:
749 case llvm::Triple::x86_64:
750 default:
751 m_triple.setOS (llvm::Triple::MacOSX);
752 break;
753 }
Greg Clayton64195a22011-02-23 00:35:02 +0000754 }
755 else
756 {
757 m_triple.setVendor (llvm::Triple::UnknownVendor);
758 m_triple.setOS (llvm::Triple::UnknownOS);
759 }
Greg Clayton593577a2011-09-21 03:57:31 +0000760 // Fall back onto setting the machine type if the arch by name failed...
761 if (m_triple.getArch () == llvm::Triple::UnknownArch)
762 m_triple.setArch (core_def->machine);
Greg Clayton64195a22011-02-23 00:35:02 +0000763 }
764 }
765 }
766 CoreUpdated(update_triple);
767 return IsValid();
768}
769
Greg Clayton357132e2011-03-26 19:14:58 +0000770uint32_t
771ArchSpec::GetMinimumOpcodeByteSize() const
Greg Clayton64195a22011-02-23 00:35:02 +0000772{
Greg Clayton357132e2011-03-26 19:14:58 +0000773 const CoreDefinition *core_def = FindCoreDefinition (m_core);
774 if (core_def)
775 return core_def->min_opcode_byte_size;
776 return 0;
777}
778
779uint32_t
780ArchSpec::GetMaximumOpcodeByteSize() const
781{
782 const CoreDefinition *core_def = FindCoreDefinition (m_core);
783 if (core_def)
784 return core_def->max_opcode_byte_size;
785 return 0;
Greg Clayton64195a22011-02-23 00:35:02 +0000786}
787
Jason Molendaba813dc2012-11-04 03:20:05 +0000788bool
789ArchSpec::IsExactMatch (const ArchSpec& rhs) const
790{
Sean Callananbf4b7be2012-12-13 22:07:14 +0000791 return IsEqualTo (rhs, true);
Jason Molendaba813dc2012-11-04 03:20:05 +0000792}
793
794bool
795ArchSpec::IsCompatibleMatch (const ArchSpec& rhs) const
796{
Sean Callananbf4b7be2012-12-13 22:07:14 +0000797 return IsEqualTo (rhs, false);
Jason Molendaba813dc2012-11-04 03:20:05 +0000798}
799
800bool
Sean Callananbf4b7be2012-12-13 22:07:14 +0000801ArchSpec::IsEqualTo (const ArchSpec& rhs, bool exact_match) const
Jason Molendaba813dc2012-11-04 03:20:05 +0000802{
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000803 // explicitly ignoring m_distribution_id in this method.
804
Jason Molendaba813dc2012-11-04 03:20:05 +0000805 if (GetByteOrder() != rhs.GetByteOrder())
806 return false;
807
808 const ArchSpec::Core lhs_core = GetCore ();
809 const ArchSpec::Core rhs_core = rhs.GetCore ();
810
811 const bool core_match = cores_match (lhs_core, rhs_core, true, exact_match);
812
813 if (core_match)
814 {
815 const llvm::Triple &lhs_triple = GetTriple();
816 const llvm::Triple &rhs_triple = rhs.GetTriple();
817
818 const llvm::Triple::VendorType lhs_triple_vendor = lhs_triple.getVendor();
819 const llvm::Triple::VendorType rhs_triple_vendor = rhs_triple.getVendor();
820 if (lhs_triple_vendor != rhs_triple_vendor)
821 {
Sean Callananbf4b7be2012-12-13 22:07:14 +0000822 if (exact_match)
823 {
824 const bool rhs_vendor_specified = rhs.TripleVendorWasSpecified();
825 const bool lhs_vendor_specified = TripleVendorWasSpecified();
826 // Both architectures had the vendor specified, so if they aren't
827 // equal then we return false
828 if (rhs_vendor_specified && lhs_vendor_specified)
829 return false;
830 }
Jason Molendaba813dc2012-11-04 03:20:05 +0000831
832 // Only fail if both vendor types are not unknown
833 if (lhs_triple_vendor != llvm::Triple::UnknownVendor &&
834 rhs_triple_vendor != llvm::Triple::UnknownVendor)
835 return false;
836 }
837
838 const llvm::Triple::OSType lhs_triple_os = lhs_triple.getOS();
839 const llvm::Triple::OSType rhs_triple_os = rhs_triple.getOS();
840 if (lhs_triple_os != rhs_triple_os)
841 {
Sean Callananbf4b7be2012-12-13 22:07:14 +0000842 if (exact_match)
843 {
844 const bool rhs_os_specified = rhs.TripleOSWasSpecified();
845 const bool lhs_os_specified = TripleOSWasSpecified();
846 // Both architectures had the OS specified, so if they aren't
847 // equal then we return false
848 if (rhs_os_specified && lhs_os_specified)
849 return false;
850 }
Greg Clayton7ab7f892014-05-29 21:33:45 +0000851
Greg Clayton3f19ada2014-07-10 23:33:37 +0000852 // Only fail if both os types are not unknown
853 if (lhs_triple_os != llvm::Triple::UnknownOS &&
854 rhs_triple_os != llvm::Triple::UnknownOS)
855 return false;
Jason Molendaba813dc2012-11-04 03:20:05 +0000856 }
857
858 const llvm::Triple::EnvironmentType lhs_triple_env = lhs_triple.getEnvironment();
859 const llvm::Triple::EnvironmentType rhs_triple_env = rhs_triple.getEnvironment();
860
861 if (lhs_triple_env != rhs_triple_env)
862 {
863 // Only fail if both environment types are not unknown
864 if (lhs_triple_env != llvm::Triple::UnknownEnvironment &&
865 rhs_triple_env != llvm::Triple::UnknownEnvironment)
866 return false;
867 }
868 return true;
869 }
870 return false;
871}
872
Greg Clayton64195a22011-02-23 00:35:02 +0000873//===----------------------------------------------------------------------===//
874// Helper methods.
875
876void
877ArchSpec::CoreUpdated (bool update_triple)
878{
879 const CoreDefinition *core_def = FindCoreDefinition (m_core);
880 if (core_def)
881 {
882 if (update_triple)
883 m_triple = llvm::Triple(core_def->name, "unknown", "unknown");
884 m_byte_order = core_def->default_byte_order;
885 }
886 else
887 {
888 if (update_triple)
889 m_triple = llvm::Triple();
890 m_byte_order = eByteOrderInvalid;
891 }
892}
893
894//===----------------------------------------------------------------------===//
895// Operators.
896
Greg Clayton70512312012-05-08 01:45:38 +0000897static bool
Jason Molendaba813dc2012-11-04 03:20:05 +0000898cores_match (const ArchSpec::Core core1, const ArchSpec::Core core2, bool try_inverse, bool enforce_exact_match)
Greg Clayton70512312012-05-08 01:45:38 +0000899{
Jason Molendaba813dc2012-11-04 03:20:05 +0000900 if (core1 == core2)
901 return true;
902
Greg Clayton70512312012-05-08 01:45:38 +0000903 switch (core1)
904 {
Greg Clayton70512312012-05-08 01:45:38 +0000905 case ArchSpec::kCore_any:
906 return true;
907
908 case ArchSpec::kCore_arm_any:
909 if (core2 >= ArchSpec::kCore_arm_first && core2 <= ArchSpec::kCore_arm_last)
910 return true;
911 if (core2 >= ArchSpec::kCore_thumb_first && core2 <= ArchSpec::kCore_thumb_last)
912 return true;
913 if (core2 == ArchSpec::kCore_arm_any)
914 return true;
915 break;
Greg Claytona86dc432014-01-22 23:42:03 +0000916
Greg Clayton70512312012-05-08 01:45:38 +0000917 case ArchSpec::kCore_x86_32_any:
918 if ((core2 >= ArchSpec::kCore_x86_32_first && core2 <= ArchSpec::kCore_x86_32_last) || (core2 == ArchSpec::kCore_x86_32_any))
919 return true;
920 break;
921
922 case ArchSpec::kCore_ppc_any:
923 if ((core2 >= ArchSpec::kCore_ppc_first && core2 <= ArchSpec::kCore_ppc_last) || (core2 == ArchSpec::kCore_ppc_any))
924 return true;
925 break;
926
927 case ArchSpec::kCore_ppc64_any:
928 if ((core2 >= ArchSpec::kCore_ppc64_first && core2 <= ArchSpec::kCore_ppc64_last) || (core2 == ArchSpec::kCore_ppc64_any))
929 return true;
930 break;
931
Jason Molendaa3a04522013-09-27 23:21:54 +0000932 case ArchSpec::eCore_arm_armv6m:
933 if (!enforce_exact_match)
934 {
935 try_inverse = false;
Jason Molendac7cda272013-09-27 23:29:10 +0000936 if (core2 == ArchSpec::eCore_arm_armv7)
Jason Molendaa3a04522013-09-27 23:21:54 +0000937 return true;
938 }
Deepak Panickal6d3df422014-02-19 11:16:46 +0000939
940 case ArchSpec::kCore_hexagon_any:
941 if ((core2 >= ArchSpec::kCore_hexagon_first && core2 <= ArchSpec::kCore_hexagon_last) || (core2 == ArchSpec::kCore_hexagon_any))
942 return true;
Jason Molendaa3a04522013-09-27 23:21:54 +0000943 break;
944
Jason Molenda7a1559c2013-03-08 01:20:17 +0000945 case ArchSpec::eCore_arm_armv7m:
946 case ArchSpec::eCore_arm_armv7em:
Johnny Chen1083b0d2012-08-28 22:53:40 +0000947 case ArchSpec::eCore_arm_armv7f:
948 case ArchSpec::eCore_arm_armv7k:
949 case ArchSpec::eCore_arm_armv7s:
Jason Molendaba813dc2012-11-04 03:20:05 +0000950 if (!enforce_exact_match)
951 {
952 try_inverse = false;
953 if (core2 == ArchSpec::eCore_arm_armv7)
954 return true;
955 }
Johnny Chen1083b0d2012-08-28 22:53:40 +0000956 break;
957
Greg Clayton70512312012-05-08 01:45:38 +0000958 default:
959 break;
960 }
961 if (try_inverse)
Jason Molendaba813dc2012-11-04 03:20:05 +0000962 return cores_match (core2, core1, false, enforce_exact_match);
Greg Clayton70512312012-05-08 01:45:38 +0000963 return false;
964}
965
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000966bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000967lldb_private::operator<(const ArchSpec& lhs, const ArchSpec& rhs)
968{
Greg Clayton64195a22011-02-23 00:35:02 +0000969 const ArchSpec::Core lhs_core = lhs.GetCore ();
970 const ArchSpec::Core rhs_core = rhs.GetCore ();
971 return lhs_core < rhs_core;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000972}