blob: b1303462572d210d577aef56e282d01ab57f0803 [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
Charles Davis237ad972013-08-27 05:04:33 +000017#include "llvm/Support/COFF.h"
Greg Clayton41f92322010-06-11 03:25:34 +000018#include "llvm/Support/ELF.h"
Stephen Wilsonfacebfc2011-02-24 19:13:58 +000019#include "llvm/Support/Host.h"
Greg Clayton41f92322010-06-11 03:25:34 +000020#include "llvm/Support/MachO.h"
Greg Claytone795f1b2012-08-08 01:19:34 +000021#include "lldb/Core/RegularExpression.h"
Greg Clayton514487e2011-02-15 21:59:32 +000022#include "lldb/Host/Endian.h"
23#include "lldb/Host/Host.h"
Greg Claytoneb0103f2011-04-07 22:46:35 +000024#include "lldb/Target/Platform.h"
Greg Clayton41f92322010-06-11 03:25:34 +000025
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026using namespace lldb;
27using namespace lldb_private;
28
Greg Clayton64195a22011-02-23 00:35:02 +000029#define ARCH_SPEC_SEPARATOR_CHAR '-'
Chris Lattner30fdc8d2010-06-08 16:52:24 +000030
Jason Molendaba813dc2012-11-04 03:20:05 +000031
32static bool cores_match (const ArchSpec::Core core1, const ArchSpec::Core core2, bool try_inverse, bool enforce_exact_match);
33
Greg Clayton64195a22011-02-23 00:35:02 +000034namespace lldb_private {
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035
Greg Clayton64195a22011-02-23 00:35:02 +000036 struct CoreDefinition
37 {
38 ByteOrder default_byte_order;
39 uint32_t addr_byte_size;
Greg Clayton357132e2011-03-26 19:14:58 +000040 uint32_t min_opcode_byte_size;
41 uint32_t max_opcode_byte_size;
Greg Clayton64195a22011-02-23 00:35:02 +000042 llvm::Triple::ArchType machine;
43 ArchSpec::Core core;
44 const char *name;
45 };
46
47}
48
49// This core information can be looked using the ArchSpec::Core as the index
50static const CoreDefinition g_core_definitions[ArchSpec::kNumCores] =
Chris Lattner30fdc8d2010-06-08 16:52:24 +000051{
Greg Clayton357132e2011-03-26 19:14:58 +000052 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_generic , "arm" },
53 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv4 , "armv4" },
54 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv4t , "armv4t" },
55 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv5 , "armv5" },
Greg Claytonb5c39fe2011-12-16 18:15:52 +000056 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv5e , "armv5e" },
Greg Clayton357132e2011-03-26 19:14:58 +000057 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv5t , "armv5t" },
58 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv6 , "armv6" },
Jason Molendaa3a04522013-09-27 23:21:54 +000059 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv6m , "armv6m" },
Greg Clayton357132e2011-03-26 19:14:58 +000060 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7 , "armv7" },
61 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7f , "armv7f" },
Greg Clayton357132e2011-03-26 19:14:58 +000062 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7s , "armv7s" },
Jason Molenda7a1559c2013-03-08 01:20:17 +000063 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7k , "armv7k" },
64 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7m , "armv7m" },
65 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7em , "armv7em" },
Greg Clayton357132e2011-03-26 19:14:58 +000066 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_xscale , "xscale" },
Greg Claytonb5c39fe2011-12-16 18:15:52 +000067 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumb , "thumb" },
68 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv4t , "thumbv4t" },
69 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv5 , "thumbv5" },
70 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv5e , "thumbv5e" },
71 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv6 , "thumbv6" },
Jason Molendaa3a04522013-09-27 23:21:54 +000072 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv6m , "thumbv6m" },
Greg Claytonb5c39fe2011-12-16 18:15:52 +000073 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7 , "thumbv7" },
74 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7f , "thumbv7f" },
Greg Claytonb5c39fe2011-12-16 18:15:52 +000075 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7s , "thumbv7s" },
Jason Molenda7a1559c2013-03-08 01:20:17 +000076 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7k , "thumbv7k" },
77 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7m , "thumbv7m" },
78 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7em , "thumbv7em" },
Jason Molendaa3329782014-03-29 18:54:20 +000079 { eByteOrderLittle, 8, 4, 4, llvm::Triple::arm64 , ArchSpec::eCore_arm_arm64 , "arm64" },
Ed Masteb73f8442013-10-10 00:59:47 +000080
81 { eByteOrderBig , 8, 4, 4, llvm::Triple::mips64 , ArchSpec::eCore_mips64 , "mips64" },
Greg Clayton64195a22011-02-23 00:35:02 +000082
Greg Clayton83b162d2013-08-12 18:34:04 +000083 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_generic , "ppc" },
84 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc601 , "ppc601" },
85 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc602 , "ppc602" },
86 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc603 , "ppc603" },
87 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc603e , "ppc603e" },
88 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc603ev , "ppc603ev" },
89 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc604 , "ppc604" },
90 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc604e , "ppc604e" },
91 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc620 , "ppc620" },
92 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc750 , "ppc750" },
93 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc7400 , "ppc7400" },
94 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc7450 , "ppc7450" },
95 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc970 , "ppc970" },
Greg Clayton64195a22011-02-23 00:35:02 +000096
Greg Clayton83b162d2013-08-12 18:34:04 +000097 { eByteOrderBig , 8, 4, 4, llvm::Triple::ppc64 , ArchSpec::eCore_ppc64_generic , "ppc64" },
98 { eByteOrderBig , 8, 4, 4, llvm::Triple::ppc64 , ArchSpec::eCore_ppc64_ppc970_64 , "ppc970-64" },
Greg Clayton64195a22011-02-23 00:35:02 +000099
Greg Clayton357132e2011-03-26 19:14:58 +0000100 { eByteOrderLittle, 4, 4, 4, llvm::Triple::sparc , ArchSpec::eCore_sparc_generic , "sparc" },
101 { eByteOrderLittle, 8, 4, 4, llvm::Triple::sparcv9, ArchSpec::eCore_sparc9_generic , "sparcv9" },
Greg Clayton64195a22011-02-23 00:35:02 +0000102
Greg Claytonab65b342011-04-13 22:47:15 +0000103 { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i386 , "i386" },
104 { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i486 , "i486" },
105 { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i486sx , "i486sx" },
Greg Clayton64195a22011-02-23 00:35:02 +0000106
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000107 { eByteOrderLittle, 8, 1, 15, llvm::Triple::x86_64 , ArchSpec::eCore_x86_64_x86_64 , "x86_64" },
Greg Claytona86dc432014-01-22 23:42:03 +0000108 { eByteOrderLittle, 8, 1, 15, llvm::Triple::x86_64 , ArchSpec::eCore_x86_64_x86_64h , "x86_64h" },
Deepak Panickal6d3df422014-02-19 11:16:46 +0000109 { eByteOrderLittle, 4, 4, 4, llvm::Triple::hexagon , ArchSpec::eCore_hexagon_generic, "hexagon" },
110 { eByteOrderLittle, 4, 4, 4, llvm::Triple::hexagon , ArchSpec::eCore_hexagon_hexagonv4, "hexagonv4" },
111 { eByteOrderLittle, 4, 4, 4, llvm::Triple::hexagon , ArchSpec::eCore_hexagon_hexagonv5, "hexagonv5" },
112
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000113 { eByteOrderLittle, 4, 4, 4 , llvm::Triple::UnknownArch , ArchSpec::eCore_uknownMach32 , "unknown-mach-32" },
114 { eByteOrderLittle, 8, 4, 4 , llvm::Triple::UnknownArch , ArchSpec::eCore_uknownMach64 , "unknown-mach-64" }
Greg Clayton64195a22011-02-23 00:35:02 +0000115};
116
117struct ArchDefinitionEntry
118{
119 ArchSpec::Core core;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000120 uint32_t cpu;
121 uint32_t sub;
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000122 uint32_t cpu_mask;
123 uint32_t sub_mask;
Greg Clayton64195a22011-02-23 00:35:02 +0000124};
125
126struct ArchDefinition
127{
128 ArchitectureType type;
129 size_t num_entries;
130 const ArchDefinitionEntry *entries;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000131 const char *name;
132};
133
Greg Clayton41f92322010-06-11 03:25:34 +0000134
Greg Claytonc7bece562013-01-25 18:06:21 +0000135size_t
Greg Claytonab65b342011-04-13 22:47:15 +0000136ArchSpec::AutoComplete (const char *name, StringList &matches)
137{
138 uint32_t i;
139 if (name && name[0])
140 {
141 for (i = 0; i < ArchSpec::kNumCores; ++i)
142 {
143 if (NameMatches(g_core_definitions[i].name, eNameMatchStartsWith, name))
144 matches.AppendString (g_core_definitions[i].name);
145 }
146 }
147 else
148 {
149 for (i = 0; i < ArchSpec::kNumCores; ++i)
150 matches.AppendString (g_core_definitions[i].name);
151 }
152 return matches.GetSize();
153}
154
155
156
Greg Clayton64195a22011-02-23 00:35:02 +0000157#define CPU_ANY (UINT32_MAX)
158
159//===----------------------------------------------------------------------===//
160// A table that gets searched linearly for matches. This table is used to
161// convert cpu type and subtypes to architecture names, and to convert
162// architecture names to cpu types and subtypes. The ordering is important and
163// allows the precedence to be set when the table is built.
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000164#define SUBTYPE_MASK 0x00FFFFFFu
Greg Clayton64195a22011-02-23 00:35:02 +0000165static const ArchDefinitionEntry g_macho_arch_entries[] =
Greg Clayton41f92322010-06-11 03:25:34 +0000166{
Charles Davis510938e2013-08-27 05:04:57 +0000167 { ArchSpec::eCore_arm_generic , llvm::MachO::CPU_TYPE_ARM , CPU_ANY, UINT32_MAX , UINT32_MAX },
168 { ArchSpec::eCore_arm_generic , llvm::MachO::CPU_TYPE_ARM , 0 , UINT32_MAX , SUBTYPE_MASK },
169 { ArchSpec::eCore_arm_armv4 , llvm::MachO::CPU_TYPE_ARM , 5 , UINT32_MAX , SUBTYPE_MASK },
170 { ArchSpec::eCore_arm_armv4t , llvm::MachO::CPU_TYPE_ARM , 5 , UINT32_MAX , SUBTYPE_MASK },
171 { ArchSpec::eCore_arm_armv6 , llvm::MachO::CPU_TYPE_ARM , 6 , UINT32_MAX , SUBTYPE_MASK },
Jason Molenda64a11732013-10-08 03:01:08 +0000172 { ArchSpec::eCore_arm_armv6m , llvm::MachO::CPU_TYPE_ARM , 14 , UINT32_MAX , SUBTYPE_MASK },
Charles Davis510938e2013-08-27 05:04:57 +0000173 { ArchSpec::eCore_arm_armv5 , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
174 { ArchSpec::eCore_arm_armv5e , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
175 { ArchSpec::eCore_arm_armv5t , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
176 { ArchSpec::eCore_arm_xscale , llvm::MachO::CPU_TYPE_ARM , 8 , UINT32_MAX , SUBTYPE_MASK },
177 { ArchSpec::eCore_arm_armv7 , llvm::MachO::CPU_TYPE_ARM , 9 , UINT32_MAX , SUBTYPE_MASK },
178 { ArchSpec::eCore_arm_armv7f , llvm::MachO::CPU_TYPE_ARM , 10 , UINT32_MAX , SUBTYPE_MASK },
179 { ArchSpec::eCore_arm_armv7s , llvm::MachO::CPU_TYPE_ARM , 11 , UINT32_MAX , SUBTYPE_MASK },
180 { ArchSpec::eCore_arm_armv7k , llvm::MachO::CPU_TYPE_ARM , 12 , UINT32_MAX , SUBTYPE_MASK },
181 { ArchSpec::eCore_arm_armv7m , llvm::MachO::CPU_TYPE_ARM , 15 , UINT32_MAX , SUBTYPE_MASK },
182 { ArchSpec::eCore_arm_armv7em , llvm::MachO::CPU_TYPE_ARM , 16 , UINT32_MAX , SUBTYPE_MASK },
Jason Molendaa3329782014-03-29 18:54:20 +0000183 { ArchSpec::eCore_arm_arm64 , llvm::MachO::CPU_TYPE_ARM64 , CPU_ANY, UINT32_MAX , SUBTYPE_MASK },
184 { ArchSpec::eCore_arm_arm64 , llvm::MachO::CPU_TYPE_ARM64 , 0 , UINT32_MAX , SUBTYPE_MASK },
185 { ArchSpec::eCore_arm_arm64 , llvm::MachO::CPU_TYPE_ARM64 , 1 , UINT32_MAX , SUBTYPE_MASK },
186 { ArchSpec::eCore_arm_arm64 , llvm::MachO::CPU_TYPE_ARM64 , 13 , UINT32_MAX , SUBTYPE_MASK },
Charles Davis510938e2013-08-27 05:04:57 +0000187 { ArchSpec::eCore_thumb , llvm::MachO::CPU_TYPE_ARM , 0 , UINT32_MAX , SUBTYPE_MASK },
188 { ArchSpec::eCore_thumbv4t , llvm::MachO::CPU_TYPE_ARM , 5 , UINT32_MAX , SUBTYPE_MASK },
189 { ArchSpec::eCore_thumbv5 , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
190 { ArchSpec::eCore_thumbv5e , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
191 { ArchSpec::eCore_thumbv6 , llvm::MachO::CPU_TYPE_ARM , 6 , UINT32_MAX , SUBTYPE_MASK },
Jason Molenda64a11732013-10-08 03:01:08 +0000192 { ArchSpec::eCore_thumbv6m , llvm::MachO::CPU_TYPE_ARM , 14 , UINT32_MAX , SUBTYPE_MASK },
Charles Davis510938e2013-08-27 05:04:57 +0000193 { ArchSpec::eCore_thumbv7 , llvm::MachO::CPU_TYPE_ARM , 9 , UINT32_MAX , SUBTYPE_MASK },
194 { ArchSpec::eCore_thumbv7f , llvm::MachO::CPU_TYPE_ARM , 10 , UINT32_MAX , SUBTYPE_MASK },
195 { ArchSpec::eCore_thumbv7s , llvm::MachO::CPU_TYPE_ARM , 11 , UINT32_MAX , SUBTYPE_MASK },
196 { ArchSpec::eCore_thumbv7k , llvm::MachO::CPU_TYPE_ARM , 12 , UINT32_MAX , SUBTYPE_MASK },
197 { ArchSpec::eCore_thumbv7m , llvm::MachO::CPU_TYPE_ARM , 15 , UINT32_MAX , SUBTYPE_MASK },
198 { ArchSpec::eCore_thumbv7em , llvm::MachO::CPU_TYPE_ARM , 16 , UINT32_MAX , SUBTYPE_MASK },
199 { ArchSpec::eCore_ppc_generic , llvm::MachO::CPU_TYPE_POWERPC , CPU_ANY, UINT32_MAX , UINT32_MAX },
200 { ArchSpec::eCore_ppc_generic , llvm::MachO::CPU_TYPE_POWERPC , 0 , UINT32_MAX , SUBTYPE_MASK },
201 { ArchSpec::eCore_ppc_ppc601 , llvm::MachO::CPU_TYPE_POWERPC , 1 , UINT32_MAX , SUBTYPE_MASK },
202 { ArchSpec::eCore_ppc_ppc602 , llvm::MachO::CPU_TYPE_POWERPC , 2 , UINT32_MAX , SUBTYPE_MASK },
203 { ArchSpec::eCore_ppc_ppc603 , llvm::MachO::CPU_TYPE_POWERPC , 3 , UINT32_MAX , SUBTYPE_MASK },
204 { ArchSpec::eCore_ppc_ppc603e , llvm::MachO::CPU_TYPE_POWERPC , 4 , UINT32_MAX , SUBTYPE_MASK },
205 { ArchSpec::eCore_ppc_ppc603ev , llvm::MachO::CPU_TYPE_POWERPC , 5 , UINT32_MAX , SUBTYPE_MASK },
206 { ArchSpec::eCore_ppc_ppc604 , llvm::MachO::CPU_TYPE_POWERPC , 6 , UINT32_MAX , SUBTYPE_MASK },
207 { ArchSpec::eCore_ppc_ppc604e , llvm::MachO::CPU_TYPE_POWERPC , 7 , UINT32_MAX , SUBTYPE_MASK },
208 { ArchSpec::eCore_ppc_ppc620 , llvm::MachO::CPU_TYPE_POWERPC , 8 , UINT32_MAX , SUBTYPE_MASK },
209 { ArchSpec::eCore_ppc_ppc750 , llvm::MachO::CPU_TYPE_POWERPC , 9 , UINT32_MAX , SUBTYPE_MASK },
210 { ArchSpec::eCore_ppc_ppc7400 , llvm::MachO::CPU_TYPE_POWERPC , 10 , UINT32_MAX , SUBTYPE_MASK },
211 { ArchSpec::eCore_ppc_ppc7450 , llvm::MachO::CPU_TYPE_POWERPC , 11 , UINT32_MAX , SUBTYPE_MASK },
212 { ArchSpec::eCore_ppc_ppc970 , llvm::MachO::CPU_TYPE_POWERPC , 100 , UINT32_MAX , SUBTYPE_MASK },
213 { ArchSpec::eCore_ppc64_generic , llvm::MachO::CPU_TYPE_POWERPC64 , 0 , UINT32_MAX , SUBTYPE_MASK },
214 { ArchSpec::eCore_ppc64_ppc970_64 , llvm::MachO::CPU_TYPE_POWERPC64 , 100 , UINT32_MAX , SUBTYPE_MASK },
215 { ArchSpec::eCore_x86_32_i386 , llvm::MachO::CPU_TYPE_I386 , 3 , UINT32_MAX , SUBTYPE_MASK },
216 { ArchSpec::eCore_x86_32_i486 , llvm::MachO::CPU_TYPE_I386 , 4 , UINT32_MAX , SUBTYPE_MASK },
217 { ArchSpec::eCore_x86_32_i486sx , llvm::MachO::CPU_TYPE_I386 , 0x84 , UINT32_MAX , SUBTYPE_MASK },
Greg Claytona86dc432014-01-22 23:42:03 +0000218 { ArchSpec::eCore_x86_32_i386 , llvm::MachO::CPU_TYPE_I386 , CPU_ANY, UINT32_MAX , UINT32_MAX },
Charles Davis510938e2013-08-27 05:04:57 +0000219 { ArchSpec::eCore_x86_64_x86_64 , llvm::MachO::CPU_TYPE_X86_64 , 3 , UINT32_MAX , SUBTYPE_MASK },
220 { ArchSpec::eCore_x86_64_x86_64 , llvm::MachO::CPU_TYPE_X86_64 , 4 , UINT32_MAX , SUBTYPE_MASK },
Greg Claytona86dc432014-01-22 23:42:03 +0000221 { ArchSpec::eCore_x86_64_x86_64h , llvm::MachO::CPU_TYPE_X86_64 , 8 , UINT32_MAX , SUBTYPE_MASK },
222 { 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 +0000223 // Catch any unknown mach architectures so we can always use the object and symbol mach-o files
Charles Davis510938e2013-08-27 05:04:57 +0000224 { ArchSpec::eCore_uknownMach32 , 0 , 0 , 0xFF000000u, 0x00000000u },
225 { ArchSpec::eCore_uknownMach64 , llvm::MachO::CPU_ARCH_ABI64 , 0 , 0xFF000000u, 0x00000000u }
Greg Clayton64195a22011-02-23 00:35:02 +0000226};
227static const ArchDefinition g_macho_arch_def = {
228 eArchTypeMachO,
229 sizeof(g_macho_arch_entries)/sizeof(g_macho_arch_entries[0]),
230 g_macho_arch_entries,
Greg Clayton64195a22011-02-23 00:35:02 +0000231 "mach-o"
Greg Clayton41f92322010-06-11 03:25:34 +0000232};
233
Greg Clayton64195a22011-02-23 00:35:02 +0000234//===----------------------------------------------------------------------===//
235// A table that gets searched linearly for matches. This table is used to
236// convert cpu type and subtypes to architecture names, and to convert
237// architecture names to cpu types and subtypes. The ordering is important and
238// allows the precedence to be set when the table is built.
239static const ArchDefinitionEntry g_elf_arch_entries[] =
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000240{
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000241 { ArchSpec::eCore_sparc_generic , llvm::ELF::EM_SPARC , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // Sparc
242 { ArchSpec::eCore_x86_32_i386 , llvm::ELF::EM_386 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // Intel 80386
243 { ArchSpec::eCore_x86_32_i486 , llvm::ELF::EM_486 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // Intel 486 (deprecated)
244 { ArchSpec::eCore_ppc_generic , llvm::ELF::EM_PPC , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // PowerPC
245 { ArchSpec::eCore_ppc64_generic , llvm::ELF::EM_PPC64 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // PowerPC64
246 { ArchSpec::eCore_arm_generic , llvm::ELF::EM_ARM , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // ARM
247 { ArchSpec::eCore_sparc9_generic , llvm::ELF::EM_SPARCV9, LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // SPARC V9
Ed Masteb73f8442013-10-10 00:59:47 +0000248 { ArchSpec::eCore_x86_64_x86_64 , llvm::ELF::EM_X86_64 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // AMD64
Deepak Panickal6d3df422014-02-19 11:16:46 +0000249 { ArchSpec::eCore_mips64 , llvm::ELF::EM_MIPS , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // MIPS
250 { ArchSpec::eCore_hexagon_generic , llvm::ELF::EM_HEXAGON, LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu } // HEXAGON
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000251};
252
Greg Clayton64195a22011-02-23 00:35:02 +0000253static const ArchDefinition g_elf_arch_def = {
254 eArchTypeELF,
255 sizeof(g_elf_arch_entries)/sizeof(g_elf_arch_entries[0]),
256 g_elf_arch_entries,
Greg Clayton64195a22011-02-23 00:35:02 +0000257 "elf",
Greg Clayton41f92322010-06-11 03:25:34 +0000258};
259
Charles Davis237ad972013-08-27 05:04:33 +0000260static const ArchDefinitionEntry g_coff_arch_entries[] =
261{
262 { ArchSpec::eCore_x86_32_i386 , llvm::COFF::IMAGE_FILE_MACHINE_I386 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // Intel 80386
263 { ArchSpec::eCore_ppc_generic , llvm::COFF::IMAGE_FILE_MACHINE_POWERPC , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // PowerPC
264 { ArchSpec::eCore_ppc_generic , llvm::COFF::IMAGE_FILE_MACHINE_POWERPCFP, LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // PowerPC (with FPU)
265 { ArchSpec::eCore_arm_generic , llvm::COFF::IMAGE_FILE_MACHINE_ARM , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // ARM
Saleem Abdulrasool1108cb32014-03-11 03:09:08 +0000266 { ArchSpec::eCore_arm_armv7 , llvm::COFF::IMAGE_FILE_MACHINE_ARMNT , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // ARMv7
Charles Davis237ad972013-08-27 05:04:33 +0000267 { ArchSpec::eCore_thumb , llvm::COFF::IMAGE_FILE_MACHINE_THUMB , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // ARMv7
268 { ArchSpec::eCore_x86_64_x86_64, llvm::COFF::IMAGE_FILE_MACHINE_AMD64 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu } // AMD64
269};
270
271static const ArchDefinition g_coff_arch_def = {
272 eArchTypeCOFF,
273 sizeof(g_coff_arch_entries)/sizeof(g_coff_arch_entries[0]),
274 g_coff_arch_entries,
275 "pe-coff",
276};
277
Greg Clayton64195a22011-02-23 00:35:02 +0000278//===----------------------------------------------------------------------===//
279// Table of all ArchDefinitions
280static const ArchDefinition *g_arch_definitions[] = {
281 &g_macho_arch_def,
Charles Davis237ad972013-08-27 05:04:33 +0000282 &g_elf_arch_def,
283 &g_coff_arch_def
Greg Clayton64195a22011-02-23 00:35:02 +0000284};
Greg Clayton41f92322010-06-11 03:25:34 +0000285
Greg Clayton64195a22011-02-23 00:35:02 +0000286static const size_t k_num_arch_definitions =
287 sizeof(g_arch_definitions) / sizeof(g_arch_definitions[0]);
288
289//===----------------------------------------------------------------------===//
290// Static helper functions.
291
292
293// Get the architecture definition for a given object type.
294static const ArchDefinition *
295FindArchDefinition (ArchitectureType arch_type)
296{
297 for (unsigned int i = 0; i < k_num_arch_definitions; ++i)
298 {
299 const ArchDefinition *def = g_arch_definitions[i];
300 if (def->type == arch_type)
301 return def;
302 }
303 return NULL;
304}
305
306// Get an architecture definition by name.
307static const CoreDefinition *
308FindCoreDefinition (llvm::StringRef name)
309{
310 for (unsigned int i = 0; i < ArchSpec::kNumCores; ++i)
311 {
312 if (name.equals_lower(g_core_definitions[i].name))
313 return &g_core_definitions[i];
314 }
315 return NULL;
316}
317
318static inline const CoreDefinition *
319FindCoreDefinition (ArchSpec::Core core)
320{
321 if (core >= 0 && core < ArchSpec::kNumCores)
322 return &g_core_definitions[core];
323 return NULL;
324}
325
326// Get a definition entry by cpu type and subtype.
327static const ArchDefinitionEntry *
328FindArchDefinitionEntry (const ArchDefinition *def, uint32_t cpu, uint32_t sub)
329{
330 if (def == NULL)
331 return NULL;
332
Greg Clayton64195a22011-02-23 00:35:02 +0000333 const ArchDefinitionEntry *entries = def->entries;
334 for (size_t i = 0; i < def->num_entries; ++i)
335 {
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000336 if (entries[i].cpu == (cpu & entries[i].cpu_mask))
337 if (entries[i].sub == (sub & entries[i].sub_mask))
338 return &entries[i];
Greg Clayton64195a22011-02-23 00:35:02 +0000339 }
340 return NULL;
341}
342
343static const ArchDefinitionEntry *
344FindArchDefinitionEntry (const ArchDefinition *def, ArchSpec::Core core)
345{
346 if (def == NULL)
347 return NULL;
348
349 const ArchDefinitionEntry *entries = def->entries;
350 for (size_t i = 0; i < def->num_entries; ++i)
351 {
352 if (entries[i].core == core)
353 return &entries[i];
354 }
355 return NULL;
356}
357
358//===----------------------------------------------------------------------===//
359// Constructors and destructors.
360
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000361ArchSpec::ArchSpec() :
Greg Clayton514487e2011-02-15 21:59:32 +0000362 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000363 m_core (kCore_invalid),
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000364 m_byte_order (eByteOrderInvalid),
365 m_distribution_id ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000366{
367}
368
Greg Claytoneb0103f2011-04-07 22:46:35 +0000369ArchSpec::ArchSpec (const char *triple_cstr, Platform *platform) :
Greg Clayton514487e2011-02-15 21:59:32 +0000370 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000371 m_core (kCore_invalid),
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000372 m_byte_order (eByteOrderInvalid),
373 m_distribution_id ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000374{
Greg Clayton64195a22011-02-23 00:35:02 +0000375 if (triple_cstr)
Greg Claytoneb0103f2011-04-07 22:46:35 +0000376 SetTriple(triple_cstr, platform);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000377}
378
Greg Clayton70512312012-05-08 01:45:38 +0000379
380ArchSpec::ArchSpec (const char *triple_cstr) :
381 m_triple (),
382 m_core (kCore_invalid),
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000383 m_byte_order (eByteOrderInvalid),
384 m_distribution_id ()
Greg Clayton70512312012-05-08 01:45:38 +0000385{
386 if (triple_cstr)
387 SetTriple(triple_cstr);
388}
389
Greg Clayton64195a22011-02-23 00:35:02 +0000390ArchSpec::ArchSpec(const llvm::Triple &triple) :
Greg Clayton514487e2011-02-15 21:59:32 +0000391 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000392 m_core (kCore_invalid),
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000393 m_byte_order (eByteOrderInvalid),
394 m_distribution_id ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000395{
Greg Clayton64195a22011-02-23 00:35:02 +0000396 SetTriple(triple);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000397}
398
Greg Claytone0d378b2011-03-24 21:19:54 +0000399ArchSpec::ArchSpec (ArchitectureType arch_type, uint32_t cpu, uint32_t subtype) :
Greg Clayton64195a22011-02-23 00:35:02 +0000400 m_triple (),
401 m_core (kCore_invalid),
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000402 m_byte_order (eByteOrderInvalid),
403 m_distribution_id ()
Greg Clayton64195a22011-02-23 00:35:02 +0000404{
405 SetArchitecture (arch_type, cpu, subtype);
406}
407
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000408ArchSpec::~ArchSpec()
409{
410}
411
Greg Clayton64195a22011-02-23 00:35:02 +0000412//===----------------------------------------------------------------------===//
413// Assignment and initialization.
414
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000415const ArchSpec&
416ArchSpec::operator= (const ArchSpec& rhs)
417{
418 if (this != &rhs)
419 {
Greg Clayton514487e2011-02-15 21:59:32 +0000420 m_triple = rhs.m_triple;
Greg Clayton64195a22011-02-23 00:35:02 +0000421 m_core = rhs.m_core;
Greg Clayton514487e2011-02-15 21:59:32 +0000422 m_byte_order = rhs.m_byte_order;
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000423 m_distribution_id = rhs.m_distribution_id;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000424 }
425 return *this;
426}
427
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000428void
429ArchSpec::Clear()
430{
Greg Clayton514487e2011-02-15 21:59:32 +0000431 m_triple = llvm::Triple();
Greg Clayton64195a22011-02-23 00:35:02 +0000432 m_core = kCore_invalid;
433 m_byte_order = eByteOrderInvalid;
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000434 m_distribution_id.Clear ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000435}
436
Greg Clayton64195a22011-02-23 00:35:02 +0000437//===----------------------------------------------------------------------===//
438// Predicates.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000439
Greg Clayton41f92322010-06-11 03:25:34 +0000440
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000441const char *
Greg Clayton64195a22011-02-23 00:35:02 +0000442ArchSpec::GetArchitectureName () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000443{
Greg Clayton64195a22011-02-23 00:35:02 +0000444 const CoreDefinition *core_def = FindCoreDefinition (m_core);
445 if (core_def)
446 return core_def->name;
447 return "unknown";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000448}
449
Greg Clayton64195a22011-02-23 00:35:02 +0000450uint32_t
451ArchSpec::GetMachOCPUType () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000452{
Greg Clayton64195a22011-02-23 00:35:02 +0000453 const CoreDefinition *core_def = FindCoreDefinition (m_core);
454 if (core_def)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000455 {
Greg Clayton64195a22011-02-23 00:35:02 +0000456 const ArchDefinitionEntry *arch_def = FindArchDefinitionEntry (&g_macho_arch_def, core_def->core);
457 if (arch_def)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000458 {
Greg Clayton64195a22011-02-23 00:35:02 +0000459 return arch_def->cpu;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000460 }
461 }
Greg Clayton64195a22011-02-23 00:35:02 +0000462 return LLDB_INVALID_CPUTYPE;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000463}
464
Greg Clayton64195a22011-02-23 00:35:02 +0000465uint32_t
466ArchSpec::GetMachOCPUSubType () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000467{
Greg Clayton64195a22011-02-23 00:35:02 +0000468 const CoreDefinition *core_def = FindCoreDefinition (m_core);
469 if (core_def)
470 {
471 const ArchDefinitionEntry *arch_def = FindArchDefinitionEntry (&g_macho_arch_def, core_def->core);
472 if (arch_def)
473 {
Greg Clayton1cb64962011-03-24 04:28:38 +0000474 return arch_def->sub;
Greg Clayton64195a22011-02-23 00:35:02 +0000475 }
476 }
477 return LLDB_INVALID_CPUTYPE;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000478}
479
Greg Clayton64195a22011-02-23 00:35:02 +0000480llvm::Triple::ArchType
481ArchSpec::GetMachine () const
482{
483 const CoreDefinition *core_def = FindCoreDefinition (m_core);
484 if (core_def)
485 return core_def->machine;
486
487 return llvm::Triple::UnknownArch;
488}
489
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000490const ConstString&
491ArchSpec::GetDistributionId () const
492{
493 return m_distribution_id;
494}
495
496void
497ArchSpec::SetDistributionId (const char* distribution_id)
498{
499 m_distribution_id.SetCString (distribution_id);
500}
501
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000502uint32_t
503ArchSpec::GetAddressByteSize() const
504{
Greg Clayton64195a22011-02-23 00:35:02 +0000505 const CoreDefinition *core_def = FindCoreDefinition (m_core);
506 if (core_def)
507 return core_def->addr_byte_size;
Greg Clayton41f92322010-06-11 03:25:34 +0000508 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000509}
510
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000511ByteOrder
512ArchSpec::GetDefaultEndian () const
513{
Greg Clayton64195a22011-02-23 00:35:02 +0000514 const CoreDefinition *core_def = FindCoreDefinition (m_core);
515 if (core_def)
516 return core_def->default_byte_order;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000517 return eByteOrderInvalid;
518}
519
Greg Clayton64195a22011-02-23 00:35:02 +0000520lldb::ByteOrder
521ArchSpec::GetByteOrder () const
522{
523 if (m_byte_order == eByteOrderInvalid)
524 return GetDefaultEndian();
525 return m_byte_order;
526}
527
528//===----------------------------------------------------------------------===//
529// Mutators.
530
531bool
532ArchSpec::SetTriple (const llvm::Triple &triple)
533{
534 m_triple = triple;
535
536 llvm::StringRef arch_name (m_triple.getArchName());
537 const CoreDefinition *core_def = FindCoreDefinition (arch_name);
538 if (core_def)
539 {
540 m_core = core_def->core;
Greg Claytoneb0103f2011-04-07 22:46:35 +0000541 // Set the byte order to the default byte order for an architecture.
542 // This can be modified if needed for cases when cores handle both
543 // big and little endian
544 m_byte_order = core_def->default_byte_order;
Greg Clayton64195a22011-02-23 00:35:02 +0000545 }
546 else
547 {
548 Clear();
549 }
550
551
552 return IsValid();
553}
554
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000555static bool
556ParseMachCPUDashSubtypeTriple (const char *triple_cstr, ArchSpec &arch)
557{
558 // Accept "12-10" or "12.10" as cpu type/subtype
559 if (isdigit(triple_cstr[0]))
560 {
561 char *end = NULL;
562 errno = 0;
Greg Claytonc7bece562013-01-25 18:06:21 +0000563 uint32_t cpu = (uint32_t)::strtoul (triple_cstr, &end, 0);
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000564 if (errno == 0 && cpu != 0 && end && ((*end == '-') || (*end == '.')))
565 {
566 errno = 0;
Greg Claytonc7bece562013-01-25 18:06:21 +0000567 uint32_t sub = (uint32_t)::strtoul (end + 1, &end, 0);
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000568 if (errno == 0 && end && ((*end == '-') || (*end == '.') || (*end == '\0')))
569 {
570 if (arch.SetArchitecture (eArchTypeMachO, cpu, sub))
571 {
572 if (*end == '-')
573 {
574 llvm::StringRef vendor_os (end + 1);
575 size_t dash_pos = vendor_os.find('-');
576 if (dash_pos != llvm::StringRef::npos)
577 {
578 llvm::StringRef vendor_str(vendor_os.substr(0, dash_pos));
579 arch.GetTriple().setVendorName(vendor_str);
580 const size_t vendor_start_pos = dash_pos+1;
Greg Claytonc7bece562013-01-25 18:06:21 +0000581 dash_pos = vendor_os.find('-', vendor_start_pos);
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000582 if (dash_pos == llvm::StringRef::npos)
583 {
584 if (vendor_start_pos < vendor_os.size())
585 arch.GetTriple().setOSName(vendor_os.substr(vendor_start_pos));
586 }
587 else
588 {
589 arch.GetTriple().setOSName(vendor_os.substr(vendor_start_pos, dash_pos - vendor_start_pos));
590 }
591 }
592 }
593 return true;
594 }
595 }
596 }
597 }
598 return false;
599}
Greg Clayton64195a22011-02-23 00:35:02 +0000600bool
Greg Clayton70512312012-05-08 01:45:38 +0000601ArchSpec::SetTriple (const char *triple_cstr)
Greg Clayton64195a22011-02-23 00:35:02 +0000602{
Greg Clayton23aca092011-08-12 23:32:52 +0000603 if (triple_cstr && triple_cstr[0])
Greg Clayton64195a22011-02-23 00:35:02 +0000604 {
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000605 if (ParseMachCPUDashSubtypeTriple (triple_cstr, *this))
606 return true;
607
Greg Clayton64195a22011-02-23 00:35:02 +0000608 llvm::StringRef triple_stref (triple_cstr);
609 if (triple_stref.startswith (LLDB_ARCH_DEFAULT))
610 {
611 // Special case for the current host default architectures...
612 if (triple_stref.equals (LLDB_ARCH_DEFAULT_32BIT))
613 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture32);
614 else if (triple_stref.equals (LLDB_ARCH_DEFAULT_64BIT))
615 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture64);
616 else if (triple_stref.equals (LLDB_ARCH_DEFAULT))
617 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture);
618 }
619 else
620 {
621 std::string normalized_triple_sstr (llvm::Triple::normalize(triple_stref));
622 triple_stref = normalized_triple_sstr;
Greg Clayton70512312012-05-08 01:45:38 +0000623 SetTriple (llvm::Triple (triple_stref));
624 }
625 }
626 else
627 Clear();
628 return IsValid();
629}
630
631bool
632ArchSpec::SetTriple (const char *triple_cstr, Platform *platform)
633{
634 if (triple_cstr && triple_cstr[0])
635 {
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000636 if (ParseMachCPUDashSubtypeTriple (triple_cstr, *this))
637 return true;
638
Greg Clayton70512312012-05-08 01:45:38 +0000639 llvm::StringRef triple_stref (triple_cstr);
640 if (triple_stref.startswith (LLDB_ARCH_DEFAULT))
641 {
642 // Special case for the current host default architectures...
643 if (triple_stref.equals (LLDB_ARCH_DEFAULT_32BIT))
644 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture32);
645 else if (triple_stref.equals (LLDB_ARCH_DEFAULT_64BIT))
646 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture64);
647 else if (triple_stref.equals (LLDB_ARCH_DEFAULT))
648 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture);
649 }
650 else
651 {
652 ArchSpec raw_arch (triple_cstr);
653
654 std::string normalized_triple_sstr (llvm::Triple::normalize(triple_stref));
655 triple_stref = normalized_triple_sstr;
Greg Claytoneb0103f2011-04-07 22:46:35 +0000656 llvm::Triple normalized_triple (triple_stref);
657
658 const bool os_specified = normalized_triple.getOSName().size() > 0;
659 const bool vendor_specified = normalized_triple.getVendorName().size() > 0;
660 const bool env_specified = normalized_triple.getEnvironmentName().size() > 0;
661
662 // If we got an arch only, then default the vendor, os, environment
663 // to match the platform if one is supplied
664 if (!(os_specified || vendor_specified || env_specified))
665 {
666 if (platform)
667 {
668 // If we were given a platform, use the platform's system
669 // architecture. If this is not available (might not be
670 // connected) use the first supported architecture.
Greg Clayton70512312012-05-08 01:45:38 +0000671 ArchSpec compatible_arch;
Greg Clayton1e0c8842013-01-11 20:49:54 +0000672 if (platform->IsCompatibleArchitecture (raw_arch, false, &compatible_arch))
Greg Claytoneb0103f2011-04-07 22:46:35 +0000673 {
Greg Clayton70512312012-05-08 01:45:38 +0000674 if (compatible_arch.IsValid())
675 {
676 const llvm::Triple &compatible_triple = compatible_arch.GetTriple();
677 if (!vendor_specified)
678 normalized_triple.setVendor(compatible_triple.getVendor());
679 if (!os_specified)
680 normalized_triple.setOS(compatible_triple.getOS());
681 if (!env_specified && compatible_triple.getEnvironmentName().size())
682 normalized_triple.setEnvironment(compatible_triple.getEnvironment());
683 }
Greg Claytoneb0103f2011-04-07 22:46:35 +0000684 }
Greg Clayton70512312012-05-08 01:45:38 +0000685 else
Greg Claytoneb0103f2011-04-07 22:46:35 +0000686 {
Greg Clayton70512312012-05-08 01:45:38 +0000687 *this = raw_arch;
688 return IsValid();
Greg Claytoneb0103f2011-04-07 22:46:35 +0000689 }
690 }
691 else
692 {
693 // No platform specified, fall back to the host system for
694 // the default vendor, os, and environment.
Sean Callananbfb237bc2011-11-04 22:46:46 +0000695 llvm::Triple host_triple(llvm::sys::getDefaultTargetTriple());
Greg Clayton70512312012-05-08 01:45:38 +0000696 if (!vendor_specified)
697 normalized_triple.setVendor(host_triple.getVendor());
698 if (!vendor_specified)
699 normalized_triple.setOS(host_triple.getOS());
700 if (!env_specified && host_triple.getEnvironmentName().size())
701 normalized_triple.setEnvironment(host_triple.getEnvironment());
Greg Claytoneb0103f2011-04-07 22:46:35 +0000702 }
703 }
704 SetTriple (normalized_triple);
Greg Clayton64195a22011-02-23 00:35:02 +0000705 }
706 }
707 else
708 Clear();
709 return IsValid();
710}
711
Greg Clayton64195a22011-02-23 00:35:02 +0000712bool
Greg Claytone0d378b2011-03-24 21:19:54 +0000713ArchSpec::SetArchitecture (ArchitectureType arch_type, uint32_t cpu, uint32_t sub)
Greg Clayton64195a22011-02-23 00:35:02 +0000714{
715 m_core = kCore_invalid;
716 bool update_triple = true;
717 const ArchDefinition *arch_def = FindArchDefinition(arch_type);
718 if (arch_def)
719 {
720 const ArchDefinitionEntry *arch_def_entry = FindArchDefinitionEntry (arch_def, cpu, sub);
721 if (arch_def_entry)
722 {
723 const CoreDefinition *core_def = FindCoreDefinition (arch_def_entry->core);
724 if (core_def)
725 {
726 m_core = core_def->core;
727 update_triple = false;
Greg Clayton593577a2011-09-21 03:57:31 +0000728 // Always use the architecture name because it might be more descriptive
729 // than the architecture enum ("armv7" -> llvm::Triple::arm).
730 m_triple.setArchName(llvm::StringRef(core_def->name));
Greg Clayton64195a22011-02-23 00:35:02 +0000731 if (arch_type == eArchTypeMachO)
732 {
733 m_triple.setVendor (llvm::Triple::Apple);
Greg Clayton70512312012-05-08 01:45:38 +0000734
735 switch (core_def->machine)
736 {
Jason Molendaa3329782014-03-29 18:54:20 +0000737 case llvm::Triple::arm64:
Greg Clayton70512312012-05-08 01:45:38 +0000738 case llvm::Triple::arm:
739 case llvm::Triple::thumb:
740 m_triple.setOS (llvm::Triple::IOS);
741 break;
742
743 case llvm::Triple::x86:
744 case llvm::Triple::x86_64:
745 default:
746 m_triple.setOS (llvm::Triple::MacOSX);
747 break;
748 }
Greg Clayton64195a22011-02-23 00:35:02 +0000749 }
750 else
751 {
752 m_triple.setVendor (llvm::Triple::UnknownVendor);
753 m_triple.setOS (llvm::Triple::UnknownOS);
754 }
Greg Clayton593577a2011-09-21 03:57:31 +0000755 // Fall back onto setting the machine type if the arch by name failed...
756 if (m_triple.getArch () == llvm::Triple::UnknownArch)
757 m_triple.setArch (core_def->machine);
Greg Clayton64195a22011-02-23 00:35:02 +0000758 }
759 }
760 }
761 CoreUpdated(update_triple);
762 return IsValid();
763}
764
Greg Clayton357132e2011-03-26 19:14:58 +0000765uint32_t
766ArchSpec::GetMinimumOpcodeByteSize() const
Greg Clayton64195a22011-02-23 00:35:02 +0000767{
Greg Clayton357132e2011-03-26 19:14:58 +0000768 const CoreDefinition *core_def = FindCoreDefinition (m_core);
769 if (core_def)
770 return core_def->min_opcode_byte_size;
771 return 0;
772}
773
774uint32_t
775ArchSpec::GetMaximumOpcodeByteSize() const
776{
777 const CoreDefinition *core_def = FindCoreDefinition (m_core);
778 if (core_def)
779 return core_def->max_opcode_byte_size;
780 return 0;
Greg Clayton64195a22011-02-23 00:35:02 +0000781}
782
Jason Molendaba813dc2012-11-04 03:20:05 +0000783bool
784ArchSpec::IsExactMatch (const ArchSpec& rhs) const
785{
Sean Callananbf4b7be2012-12-13 22:07:14 +0000786 return IsEqualTo (rhs, true);
Jason Molendaba813dc2012-11-04 03:20:05 +0000787}
788
789bool
790ArchSpec::IsCompatibleMatch (const ArchSpec& rhs) const
791{
Sean Callananbf4b7be2012-12-13 22:07:14 +0000792 return IsEqualTo (rhs, false);
Jason Molendaba813dc2012-11-04 03:20:05 +0000793}
794
795bool
Sean Callananbf4b7be2012-12-13 22:07:14 +0000796ArchSpec::IsEqualTo (const ArchSpec& rhs, bool exact_match) const
Jason Molendaba813dc2012-11-04 03:20:05 +0000797{
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000798 // explicitly ignoring m_distribution_id in this method.
799
Jason Molendaba813dc2012-11-04 03:20:05 +0000800 if (GetByteOrder() != rhs.GetByteOrder())
801 return false;
802
803 const ArchSpec::Core lhs_core = GetCore ();
804 const ArchSpec::Core rhs_core = rhs.GetCore ();
805
806 const bool core_match = cores_match (lhs_core, rhs_core, true, exact_match);
807
808 if (core_match)
809 {
810 const llvm::Triple &lhs_triple = GetTriple();
811 const llvm::Triple &rhs_triple = rhs.GetTriple();
812
813 const llvm::Triple::VendorType lhs_triple_vendor = lhs_triple.getVendor();
814 const llvm::Triple::VendorType rhs_triple_vendor = rhs_triple.getVendor();
815 if (lhs_triple_vendor != rhs_triple_vendor)
816 {
Sean Callananbf4b7be2012-12-13 22:07:14 +0000817 if (exact_match)
818 {
819 const bool rhs_vendor_specified = rhs.TripleVendorWasSpecified();
820 const bool lhs_vendor_specified = TripleVendorWasSpecified();
821 // Both architectures had the vendor specified, so if they aren't
822 // equal then we return false
823 if (rhs_vendor_specified && lhs_vendor_specified)
824 return false;
825 }
Jason Molendaba813dc2012-11-04 03:20:05 +0000826
827 // Only fail if both vendor types are not unknown
828 if (lhs_triple_vendor != llvm::Triple::UnknownVendor &&
829 rhs_triple_vendor != llvm::Triple::UnknownVendor)
830 return false;
831 }
832
833 const llvm::Triple::OSType lhs_triple_os = lhs_triple.getOS();
834 const llvm::Triple::OSType rhs_triple_os = rhs_triple.getOS();
835 if (lhs_triple_os != rhs_triple_os)
836 {
Sean Callananbf4b7be2012-12-13 22:07:14 +0000837 if (exact_match)
838 {
839 const bool rhs_os_specified = rhs.TripleOSWasSpecified();
840 const bool lhs_os_specified = TripleOSWasSpecified();
841 // Both architectures had the OS specified, so if they aren't
842 // equal then we return false
843 if (rhs_os_specified && lhs_os_specified)
844 return false;
845 }
Jason Molendaba813dc2012-11-04 03:20:05 +0000846 // Only fail if both os types are not unknown
847 if (lhs_triple_os != llvm::Triple::UnknownOS &&
848 rhs_triple_os != llvm::Triple::UnknownOS)
849 return false;
850 }
851
852 const llvm::Triple::EnvironmentType lhs_triple_env = lhs_triple.getEnvironment();
853 const llvm::Triple::EnvironmentType rhs_triple_env = rhs_triple.getEnvironment();
854
855 if (lhs_triple_env != rhs_triple_env)
856 {
857 // Only fail if both environment types are not unknown
858 if (lhs_triple_env != llvm::Triple::UnknownEnvironment &&
859 rhs_triple_env != llvm::Triple::UnknownEnvironment)
860 return false;
861 }
862 return true;
863 }
864 return false;
865}
866
Greg Clayton64195a22011-02-23 00:35:02 +0000867//===----------------------------------------------------------------------===//
868// Helper methods.
869
870void
871ArchSpec::CoreUpdated (bool update_triple)
872{
873 const CoreDefinition *core_def = FindCoreDefinition (m_core);
874 if (core_def)
875 {
876 if (update_triple)
877 m_triple = llvm::Triple(core_def->name, "unknown", "unknown");
878 m_byte_order = core_def->default_byte_order;
879 }
880 else
881 {
882 if (update_triple)
883 m_triple = llvm::Triple();
884 m_byte_order = eByteOrderInvalid;
885 }
886}
887
888//===----------------------------------------------------------------------===//
889// Operators.
890
Greg Clayton70512312012-05-08 01:45:38 +0000891static bool
Jason Molendaba813dc2012-11-04 03:20:05 +0000892cores_match (const ArchSpec::Core core1, const ArchSpec::Core core2, bool try_inverse, bool enforce_exact_match)
Greg Clayton70512312012-05-08 01:45:38 +0000893{
Jason Molendaba813dc2012-11-04 03:20:05 +0000894 if (core1 == core2)
895 return true;
896
Greg Clayton70512312012-05-08 01:45:38 +0000897 switch (core1)
898 {
Greg Clayton70512312012-05-08 01:45:38 +0000899 case ArchSpec::kCore_any:
900 return true;
901
902 case ArchSpec::kCore_arm_any:
903 if (core2 >= ArchSpec::kCore_arm_first && core2 <= ArchSpec::kCore_arm_last)
904 return true;
905 if (core2 >= ArchSpec::kCore_thumb_first && core2 <= ArchSpec::kCore_thumb_last)
906 return true;
907 if (core2 == ArchSpec::kCore_arm_any)
908 return true;
909 break;
Greg Claytona86dc432014-01-22 23:42:03 +0000910
Greg Clayton70512312012-05-08 01:45:38 +0000911 case ArchSpec::kCore_x86_32_any:
912 if ((core2 >= ArchSpec::kCore_x86_32_first && core2 <= ArchSpec::kCore_x86_32_last) || (core2 == ArchSpec::kCore_x86_32_any))
913 return true;
914 break;
915
916 case ArchSpec::kCore_ppc_any:
917 if ((core2 >= ArchSpec::kCore_ppc_first && core2 <= ArchSpec::kCore_ppc_last) || (core2 == ArchSpec::kCore_ppc_any))
918 return true;
919 break;
920
921 case ArchSpec::kCore_ppc64_any:
922 if ((core2 >= ArchSpec::kCore_ppc64_first && core2 <= ArchSpec::kCore_ppc64_last) || (core2 == ArchSpec::kCore_ppc64_any))
923 return true;
924 break;
925
Jason Molendaa3a04522013-09-27 23:21:54 +0000926 case ArchSpec::eCore_arm_armv6m:
927 if (!enforce_exact_match)
928 {
929 try_inverse = false;
Jason Molendac7cda272013-09-27 23:29:10 +0000930 if (core2 == ArchSpec::eCore_arm_armv7)
Jason Molendaa3a04522013-09-27 23:21:54 +0000931 return true;
932 }
Deepak Panickal6d3df422014-02-19 11:16:46 +0000933
934 case ArchSpec::kCore_hexagon_any:
935 if ((core2 >= ArchSpec::kCore_hexagon_first && core2 <= ArchSpec::kCore_hexagon_last) || (core2 == ArchSpec::kCore_hexagon_any))
936 return true;
Jason Molendaa3a04522013-09-27 23:21:54 +0000937 break;
938
Jason Molenda7a1559c2013-03-08 01:20:17 +0000939 case ArchSpec::eCore_arm_armv7m:
940 case ArchSpec::eCore_arm_armv7em:
Johnny Chen1083b0d2012-08-28 22:53:40 +0000941 case ArchSpec::eCore_arm_armv7f:
942 case ArchSpec::eCore_arm_armv7k:
943 case ArchSpec::eCore_arm_armv7s:
Jason Molendaba813dc2012-11-04 03:20:05 +0000944 if (!enforce_exact_match)
945 {
946 try_inverse = false;
947 if (core2 == ArchSpec::eCore_arm_armv7)
948 return true;
949 }
Johnny Chen1083b0d2012-08-28 22:53:40 +0000950 break;
951
Greg Clayton70512312012-05-08 01:45:38 +0000952 default:
953 break;
954 }
955 if (try_inverse)
Jason Molendaba813dc2012-11-04 03:20:05 +0000956 return cores_match (core2, core1, false, enforce_exact_match);
Greg Clayton70512312012-05-08 01:45:38 +0000957 return false;
958}
959
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000960bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000961lldb_private::operator<(const ArchSpec& lhs, const ArchSpec& rhs)
962{
Greg Clayton64195a22011-02-23 00:35:02 +0000963 const ArchSpec::Core lhs_core = lhs.GetCore ();
964 const ArchSpec::Core rhs_core = rhs.GetCore ();
965 return lhs_core < rhs_core;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000966}