blob: 51bea8a95b52c1a4a6b41352e400fc286d6bfee1 [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
Greg Clayton41f92322010-06-11 03:25:34 +000017#include "llvm/Support/ELF.h"
Stephen Wilsonfacebfc2011-02-24 19:13:58 +000018#include "llvm/Support/Host.h"
Greg Clayton41f92322010-06-11 03:25:34 +000019#include "llvm/Support/MachO.h"
Greg Claytone795f1b2012-08-08 01:19:34 +000020#include "lldb/Core/RegularExpression.h"
Greg Clayton514487e2011-02-15 21:59:32 +000021#include "lldb/Host/Endian.h"
22#include "lldb/Host/Host.h"
Greg Claytoneb0103f2011-04-07 22:46:35 +000023#include "lldb/Target/Platform.h"
Greg Clayton41f92322010-06-11 03:25:34 +000024
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025using namespace lldb;
26using namespace lldb_private;
27
Greg Clayton64195a22011-02-23 00:35:02 +000028#define ARCH_SPEC_SEPARATOR_CHAR '-'
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029
Jason Molendaba813dc2012-11-04 03:20:05 +000030
31static bool cores_match (const ArchSpec::Core core1, const ArchSpec::Core core2, bool try_inverse, bool enforce_exact_match);
32
Greg Clayton64195a22011-02-23 00:35:02 +000033namespace lldb_private {
Chris Lattner30fdc8d2010-06-08 16:52:24 +000034
Greg Clayton64195a22011-02-23 00:35:02 +000035 struct CoreDefinition
36 {
37 ByteOrder default_byte_order;
38 uint32_t addr_byte_size;
Greg Clayton357132e2011-03-26 19:14:58 +000039 uint32_t min_opcode_byte_size;
40 uint32_t max_opcode_byte_size;
Greg Clayton64195a22011-02-23 00:35:02 +000041 llvm::Triple::ArchType machine;
42 ArchSpec::Core core;
43 const char *name;
44 };
45
46}
47
48// This core information can be looked using the ArchSpec::Core as the index
49static const CoreDefinition g_core_definitions[ArchSpec::kNumCores] =
Chris Lattner30fdc8d2010-06-08 16:52:24 +000050{
Greg Clayton357132e2011-03-26 19:14:58 +000051 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_generic , "arm" },
52 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv4 , "armv4" },
53 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv4t , "armv4t" },
54 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv5 , "armv5" },
Greg Claytonb5c39fe2011-12-16 18:15:52 +000055 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv5e , "armv5e" },
Greg Clayton357132e2011-03-26 19:14:58 +000056 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv5t , "armv5t" },
57 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv6 , "armv6" },
58 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7 , "armv7" },
59 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7f , "armv7f" },
60 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7k , "armv7k" },
61 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7s , "armv7s" },
62 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_xscale , "xscale" },
Greg Claytonb5c39fe2011-12-16 18:15:52 +000063 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumb , "thumb" },
64 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv4t , "thumbv4t" },
65 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv5 , "thumbv5" },
66 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv5e , "thumbv5e" },
67 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv6 , "thumbv6" },
68 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7 , "thumbv7" },
69 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7f , "thumbv7f" },
70 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7k , "thumbv7k" },
71 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7s , "thumbv7s" },
72
Greg Clayton64195a22011-02-23 00:35:02 +000073
Greg Clayton357132e2011-03-26 19:14:58 +000074 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_generic , "ppc" },
75 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc601 , "ppc601" },
76 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc602 , "ppc602" },
77 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc603 , "ppc603" },
78 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc603e , "ppc603e" },
79 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc603ev , "ppc603ev" },
80 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc604 , "ppc604" },
81 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc604e , "ppc604e" },
82 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc620 , "ppc620" },
83 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc750 , "ppc750" },
84 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc7400 , "ppc7400" },
85 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc7450 , "ppc7450" },
86 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc970 , "ppc970" },
Greg Clayton64195a22011-02-23 00:35:02 +000087
Greg Clayton357132e2011-03-26 19:14:58 +000088 { eByteOrderLittle, 8, 4, 4, llvm::Triple::ppc64 , ArchSpec::eCore_ppc64_generic , "ppc64" },
89 { eByteOrderLittle, 8, 4, 4, llvm::Triple::ppc64 , ArchSpec::eCore_ppc64_ppc970_64 , "ppc970-64" },
Greg Clayton64195a22011-02-23 00:35:02 +000090
Greg Clayton357132e2011-03-26 19:14:58 +000091 { eByteOrderLittle, 4, 4, 4, llvm::Triple::sparc , ArchSpec::eCore_sparc_generic , "sparc" },
92 { eByteOrderLittle, 8, 4, 4, llvm::Triple::sparcv9, ArchSpec::eCore_sparc9_generic , "sparcv9" },
Greg Clayton64195a22011-02-23 00:35:02 +000093
Greg Claytonab65b342011-04-13 22:47:15 +000094 { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i386 , "i386" },
95 { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i486 , "i486" },
96 { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i486sx , "i486sx" },
Greg Clayton64195a22011-02-23 00:35:02 +000097
Greg Clayton9e6cffc2012-09-19 22:25:17 +000098 { eByteOrderLittle, 8, 1, 15, llvm::Triple::x86_64 , ArchSpec::eCore_x86_64_x86_64 , "x86_64" },
99 { eByteOrderLittle, 4, 4, 4 , llvm::Triple::UnknownArch , ArchSpec::eCore_uknownMach32 , "unknown-mach-32" },
100 { eByteOrderLittle, 8, 4, 4 , llvm::Triple::UnknownArch , ArchSpec::eCore_uknownMach64 , "unknown-mach-64" }
Greg Clayton64195a22011-02-23 00:35:02 +0000101};
102
103struct ArchDefinitionEntry
104{
105 ArchSpec::Core core;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000106 uint32_t cpu;
107 uint32_t sub;
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000108 uint32_t cpu_mask;
109 uint32_t sub_mask;
Greg Clayton64195a22011-02-23 00:35:02 +0000110};
111
112struct ArchDefinition
113{
114 ArchitectureType type;
115 size_t num_entries;
116 const ArchDefinitionEntry *entries;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000117 const char *name;
118};
119
Greg Clayton41f92322010-06-11 03:25:34 +0000120
Greg Claytonc7bece562013-01-25 18:06:21 +0000121size_t
Greg Claytonab65b342011-04-13 22:47:15 +0000122ArchSpec::AutoComplete (const char *name, StringList &matches)
123{
124 uint32_t i;
125 if (name && name[0])
126 {
127 for (i = 0; i < ArchSpec::kNumCores; ++i)
128 {
129 if (NameMatches(g_core_definitions[i].name, eNameMatchStartsWith, name))
130 matches.AppendString (g_core_definitions[i].name);
131 }
132 }
133 else
134 {
135 for (i = 0; i < ArchSpec::kNumCores; ++i)
136 matches.AppendString (g_core_definitions[i].name);
137 }
138 return matches.GetSize();
139}
140
141
142
Greg Clayton64195a22011-02-23 00:35:02 +0000143#define CPU_ANY (UINT32_MAX)
144
145//===----------------------------------------------------------------------===//
146// A table that gets searched linearly for matches. This table is used to
147// convert cpu type and subtypes to architecture names, and to convert
148// architecture names to cpu types and subtypes. The ordering is important and
149// allows the precedence to be set when the table is built.
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000150#define SUBTYPE_MASK 0x00FFFFFFu
Greg Clayton64195a22011-02-23 00:35:02 +0000151static const ArchDefinitionEntry g_macho_arch_entries[] =
Greg Clayton41f92322010-06-11 03:25:34 +0000152{
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000153 { ArchSpec::eCore_arm_generic , llvm::MachO::CPUTypeARM , CPU_ANY, UINT32_MAX , UINT32_MAX },
154 { ArchSpec::eCore_arm_generic , llvm::MachO::CPUTypeARM , 0 , UINT32_MAX , SUBTYPE_MASK },
155 { ArchSpec::eCore_arm_armv4 , llvm::MachO::CPUTypeARM , 5 , UINT32_MAX , SUBTYPE_MASK },
156 { ArchSpec::eCore_arm_armv4t , llvm::MachO::CPUTypeARM , 5 , UINT32_MAX , SUBTYPE_MASK },
157 { ArchSpec::eCore_arm_armv6 , llvm::MachO::CPUTypeARM , 6 , UINT32_MAX , SUBTYPE_MASK },
158 { ArchSpec::eCore_arm_armv5 , llvm::MachO::CPUTypeARM , 7 , UINT32_MAX , SUBTYPE_MASK },
159 { ArchSpec::eCore_arm_armv5e , llvm::MachO::CPUTypeARM , 7 , UINT32_MAX , SUBTYPE_MASK },
160 { ArchSpec::eCore_arm_armv5t , llvm::MachO::CPUTypeARM , 7 , UINT32_MAX , SUBTYPE_MASK },
161 { ArchSpec::eCore_arm_xscale , llvm::MachO::CPUTypeARM , 8 , UINT32_MAX , SUBTYPE_MASK },
162 { ArchSpec::eCore_arm_armv7 , llvm::MachO::CPUTypeARM , 9 , UINT32_MAX , SUBTYPE_MASK },
163 { ArchSpec::eCore_arm_armv7f , llvm::MachO::CPUTypeARM , 10 , UINT32_MAX , SUBTYPE_MASK },
164 { ArchSpec::eCore_arm_armv7k , llvm::MachO::CPUTypeARM , 12 , UINT32_MAX , SUBTYPE_MASK },
165 { ArchSpec::eCore_arm_armv7s , llvm::MachO::CPUTypeARM , 11 , UINT32_MAX , SUBTYPE_MASK },
166 { ArchSpec::eCore_thumb , llvm::MachO::CPUTypeARM , 0 , UINT32_MAX , SUBTYPE_MASK },
167 { ArchSpec::eCore_thumbv4t , llvm::MachO::CPUTypeARM , 5 , UINT32_MAX , SUBTYPE_MASK },
168 { ArchSpec::eCore_thumbv5 , llvm::MachO::CPUTypeARM , 7 , UINT32_MAX , SUBTYPE_MASK },
169 { ArchSpec::eCore_thumbv5e , llvm::MachO::CPUTypeARM , 7 , UINT32_MAX , SUBTYPE_MASK },
170 { ArchSpec::eCore_thumbv6 , llvm::MachO::CPUTypeARM , 6 , UINT32_MAX , SUBTYPE_MASK },
171 { ArchSpec::eCore_thumbv7 , llvm::MachO::CPUTypeARM , 9 , UINT32_MAX , SUBTYPE_MASK },
172 { ArchSpec::eCore_thumbv7f , llvm::MachO::CPUTypeARM , 10 , UINT32_MAX , SUBTYPE_MASK },
173 { ArchSpec::eCore_thumbv7k , llvm::MachO::CPUTypeARM , 12 , UINT32_MAX , SUBTYPE_MASK },
174 { ArchSpec::eCore_thumbv7s , llvm::MachO::CPUTypeARM , 11 , UINT32_MAX , SUBTYPE_MASK },
175 { ArchSpec::eCore_ppc_generic , llvm::MachO::CPUTypePowerPC , CPU_ANY, UINT32_MAX , UINT32_MAX },
176 { ArchSpec::eCore_ppc_generic , llvm::MachO::CPUTypePowerPC , 0 , UINT32_MAX , SUBTYPE_MASK },
177 { ArchSpec::eCore_ppc_ppc601 , llvm::MachO::CPUTypePowerPC , 1 , UINT32_MAX , SUBTYPE_MASK },
178 { ArchSpec::eCore_ppc_ppc602 , llvm::MachO::CPUTypePowerPC , 2 , UINT32_MAX , SUBTYPE_MASK },
179 { ArchSpec::eCore_ppc_ppc603 , llvm::MachO::CPUTypePowerPC , 3 , UINT32_MAX , SUBTYPE_MASK },
180 { ArchSpec::eCore_ppc_ppc603e , llvm::MachO::CPUTypePowerPC , 4 , UINT32_MAX , SUBTYPE_MASK },
181 { ArchSpec::eCore_ppc_ppc603ev , llvm::MachO::CPUTypePowerPC , 5 , UINT32_MAX , SUBTYPE_MASK },
182 { ArchSpec::eCore_ppc_ppc604 , llvm::MachO::CPUTypePowerPC , 6 , UINT32_MAX , SUBTYPE_MASK },
183 { ArchSpec::eCore_ppc_ppc604e , llvm::MachO::CPUTypePowerPC , 7 , UINT32_MAX , SUBTYPE_MASK },
184 { ArchSpec::eCore_ppc_ppc620 , llvm::MachO::CPUTypePowerPC , 8 , UINT32_MAX , SUBTYPE_MASK },
185 { ArchSpec::eCore_ppc_ppc750 , llvm::MachO::CPUTypePowerPC , 9 , UINT32_MAX , SUBTYPE_MASK },
186 { ArchSpec::eCore_ppc_ppc7400 , llvm::MachO::CPUTypePowerPC , 10 , UINT32_MAX , SUBTYPE_MASK },
187 { ArchSpec::eCore_ppc_ppc7450 , llvm::MachO::CPUTypePowerPC , 11 , UINT32_MAX , SUBTYPE_MASK },
188 { ArchSpec::eCore_ppc_ppc970 , llvm::MachO::CPUTypePowerPC , 100 , UINT32_MAX , SUBTYPE_MASK },
189 { ArchSpec::eCore_ppc64_generic , llvm::MachO::CPUTypePowerPC64 , 0 , UINT32_MAX , SUBTYPE_MASK },
190 { ArchSpec::eCore_ppc64_ppc970_64 , llvm::MachO::CPUTypePowerPC64 , 100 , UINT32_MAX , SUBTYPE_MASK },
191 { ArchSpec::eCore_x86_32_i386 , llvm::MachO::CPUTypeI386 , 3 , UINT32_MAX , SUBTYPE_MASK },
192 { ArchSpec::eCore_x86_32_i486 , llvm::MachO::CPUTypeI386 , 4 , UINT32_MAX , SUBTYPE_MASK },
193 { ArchSpec::eCore_x86_32_i486sx , llvm::MachO::CPUTypeI386 , 0x84 , UINT32_MAX , SUBTYPE_MASK },
194 { ArchSpec::eCore_x86_32_i386 , llvm::MachO::CPUTypeI386 , CPU_ANY, UINT32_MAX , UINT32_MAX },
195 { ArchSpec::eCore_x86_64_x86_64 , llvm::MachO::CPUTypeX86_64 , 3 , UINT32_MAX , SUBTYPE_MASK },
196 { ArchSpec::eCore_x86_64_x86_64 , llvm::MachO::CPUTypeX86_64 , 4 , UINT32_MAX , SUBTYPE_MASK },
197 { ArchSpec::eCore_x86_64_x86_64 , llvm::MachO::CPUTypeX86_64 , CPU_ANY, UINT32_MAX , UINT32_MAX },
198 // Catch any unknown mach architectures so we can always use the object and symbol mach-o files
199 { ArchSpec::eCore_uknownMach32 , 0 , 0 , 0xFF000000u, 0x00000000u },
200 { ArchSpec::eCore_uknownMach64 , llvm::MachO::CPUArchABI64 , 0 , 0xFF000000u, 0x00000000u }
Greg Clayton64195a22011-02-23 00:35:02 +0000201};
202static const ArchDefinition g_macho_arch_def = {
203 eArchTypeMachO,
204 sizeof(g_macho_arch_entries)/sizeof(g_macho_arch_entries[0]),
205 g_macho_arch_entries,
Greg Clayton64195a22011-02-23 00:35:02 +0000206 "mach-o"
Greg Clayton41f92322010-06-11 03:25:34 +0000207};
208
Greg Clayton64195a22011-02-23 00:35:02 +0000209//===----------------------------------------------------------------------===//
210// A table that gets searched linearly for matches. This table is used to
211// convert cpu type and subtypes to architecture names, and to convert
212// architecture names to cpu types and subtypes. The ordering is important and
213// allows the precedence to be set when the table is built.
214static const ArchDefinitionEntry g_elf_arch_entries[] =
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000215{
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000216 { ArchSpec::eCore_sparc_generic , llvm::ELF::EM_SPARC , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // Sparc
217 { ArchSpec::eCore_x86_32_i386 , llvm::ELF::EM_386 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // Intel 80386
218 { ArchSpec::eCore_x86_32_i486 , llvm::ELF::EM_486 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // Intel 486 (deprecated)
219 { ArchSpec::eCore_ppc_generic , llvm::ELF::EM_PPC , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // PowerPC
220 { ArchSpec::eCore_ppc64_generic , llvm::ELF::EM_PPC64 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // PowerPC64
221 { ArchSpec::eCore_arm_generic , llvm::ELF::EM_ARM , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // ARM
222 { ArchSpec::eCore_sparc9_generic , llvm::ELF::EM_SPARCV9, LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // SPARC V9
223 { ArchSpec::eCore_x86_64_x86_64 , llvm::ELF::EM_X86_64 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu } // AMD64
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000224};
225
Greg Clayton64195a22011-02-23 00:35:02 +0000226static const ArchDefinition g_elf_arch_def = {
227 eArchTypeELF,
228 sizeof(g_elf_arch_entries)/sizeof(g_elf_arch_entries[0]),
229 g_elf_arch_entries,
Greg Clayton64195a22011-02-23 00:35:02 +0000230 "elf",
Greg Clayton41f92322010-06-11 03:25:34 +0000231};
232
Greg Clayton64195a22011-02-23 00:35:02 +0000233//===----------------------------------------------------------------------===//
234// Table of all ArchDefinitions
235static const ArchDefinition *g_arch_definitions[] = {
236 &g_macho_arch_def,
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000237 &g_elf_arch_def
Greg Clayton64195a22011-02-23 00:35:02 +0000238};
Greg Clayton41f92322010-06-11 03:25:34 +0000239
Greg Clayton64195a22011-02-23 00:35:02 +0000240static const size_t k_num_arch_definitions =
241 sizeof(g_arch_definitions) / sizeof(g_arch_definitions[0]);
242
243//===----------------------------------------------------------------------===//
244// Static helper functions.
245
246
247// Get the architecture definition for a given object type.
248static const ArchDefinition *
249FindArchDefinition (ArchitectureType arch_type)
250{
251 for (unsigned int i = 0; i < k_num_arch_definitions; ++i)
252 {
253 const ArchDefinition *def = g_arch_definitions[i];
254 if (def->type == arch_type)
255 return def;
256 }
257 return NULL;
258}
259
260// Get an architecture definition by name.
261static const CoreDefinition *
262FindCoreDefinition (llvm::StringRef name)
263{
264 for (unsigned int i = 0; i < ArchSpec::kNumCores; ++i)
265 {
266 if (name.equals_lower(g_core_definitions[i].name))
267 return &g_core_definitions[i];
268 }
269 return NULL;
270}
271
272static inline const CoreDefinition *
273FindCoreDefinition (ArchSpec::Core core)
274{
275 if (core >= 0 && core < ArchSpec::kNumCores)
276 return &g_core_definitions[core];
277 return NULL;
278}
279
280// Get a definition entry by cpu type and subtype.
281static const ArchDefinitionEntry *
282FindArchDefinitionEntry (const ArchDefinition *def, uint32_t cpu, uint32_t sub)
283{
284 if (def == NULL)
285 return NULL;
286
Greg Clayton64195a22011-02-23 00:35:02 +0000287 const ArchDefinitionEntry *entries = def->entries;
288 for (size_t i = 0; i < def->num_entries; ++i)
289 {
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000290 if (entries[i].cpu == (cpu & entries[i].cpu_mask))
291 if (entries[i].sub == (sub & entries[i].sub_mask))
292 return &entries[i];
Greg Clayton64195a22011-02-23 00:35:02 +0000293 }
294 return NULL;
295}
296
297static const ArchDefinitionEntry *
298FindArchDefinitionEntry (const ArchDefinition *def, ArchSpec::Core core)
299{
300 if (def == NULL)
301 return NULL;
302
303 const ArchDefinitionEntry *entries = def->entries;
304 for (size_t i = 0; i < def->num_entries; ++i)
305 {
306 if (entries[i].core == core)
307 return &entries[i];
308 }
309 return NULL;
310}
311
312//===----------------------------------------------------------------------===//
313// Constructors and destructors.
314
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000315ArchSpec::ArchSpec() :
Greg Clayton514487e2011-02-15 21:59:32 +0000316 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000317 m_core (kCore_invalid),
318 m_byte_order (eByteOrderInvalid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000319{
320}
321
Greg Claytoneb0103f2011-04-07 22:46:35 +0000322ArchSpec::ArchSpec (const char *triple_cstr, Platform *platform) :
Greg Clayton514487e2011-02-15 21:59:32 +0000323 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000324 m_core (kCore_invalid),
325 m_byte_order (eByteOrderInvalid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000326{
Greg Clayton64195a22011-02-23 00:35:02 +0000327 if (triple_cstr)
Greg Claytoneb0103f2011-04-07 22:46:35 +0000328 SetTriple(triple_cstr, platform);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000329}
330
Greg Clayton70512312012-05-08 01:45:38 +0000331
332ArchSpec::ArchSpec (const char *triple_cstr) :
333 m_triple (),
334 m_core (kCore_invalid),
335 m_byte_order (eByteOrderInvalid)
336{
337 if (triple_cstr)
338 SetTriple(triple_cstr);
339}
340
Greg Clayton64195a22011-02-23 00:35:02 +0000341ArchSpec::ArchSpec(const llvm::Triple &triple) :
Greg Clayton514487e2011-02-15 21:59:32 +0000342 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000343 m_core (kCore_invalid),
344 m_byte_order (eByteOrderInvalid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000345{
Greg Clayton64195a22011-02-23 00:35:02 +0000346 SetTriple(triple);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000347}
348
Greg Claytone0d378b2011-03-24 21:19:54 +0000349ArchSpec::ArchSpec (ArchitectureType arch_type, uint32_t cpu, uint32_t subtype) :
Greg Clayton64195a22011-02-23 00:35:02 +0000350 m_triple (),
351 m_core (kCore_invalid),
352 m_byte_order (eByteOrderInvalid)
353{
354 SetArchitecture (arch_type, cpu, subtype);
355}
356
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000357ArchSpec::~ArchSpec()
358{
359}
360
Greg Clayton64195a22011-02-23 00:35:02 +0000361//===----------------------------------------------------------------------===//
362// Assignment and initialization.
363
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000364const ArchSpec&
365ArchSpec::operator= (const ArchSpec& rhs)
366{
367 if (this != &rhs)
368 {
Greg Clayton514487e2011-02-15 21:59:32 +0000369 m_triple = rhs.m_triple;
Greg Clayton64195a22011-02-23 00:35:02 +0000370 m_core = rhs.m_core;
Greg Clayton514487e2011-02-15 21:59:32 +0000371 m_byte_order = rhs.m_byte_order;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000372 }
373 return *this;
374}
375
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000376void
377ArchSpec::Clear()
378{
Greg Clayton514487e2011-02-15 21:59:32 +0000379 m_triple = llvm::Triple();
Greg Clayton64195a22011-02-23 00:35:02 +0000380 m_core = kCore_invalid;
381 m_byte_order = eByteOrderInvalid;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000382}
383
Greg Clayton64195a22011-02-23 00:35:02 +0000384//===----------------------------------------------------------------------===//
385// Predicates.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000386
Greg Clayton41f92322010-06-11 03:25:34 +0000387
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000388const char *
Greg Clayton64195a22011-02-23 00:35:02 +0000389ArchSpec::GetArchitectureName () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000390{
Greg Clayton64195a22011-02-23 00:35:02 +0000391 const CoreDefinition *core_def = FindCoreDefinition (m_core);
392 if (core_def)
393 return core_def->name;
394 return "unknown";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000395}
396
Greg Clayton64195a22011-02-23 00:35:02 +0000397uint32_t
398ArchSpec::GetMachOCPUType () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000399{
Greg Clayton64195a22011-02-23 00:35:02 +0000400 const CoreDefinition *core_def = FindCoreDefinition (m_core);
401 if (core_def)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000402 {
Greg Clayton64195a22011-02-23 00:35:02 +0000403 const ArchDefinitionEntry *arch_def = FindArchDefinitionEntry (&g_macho_arch_def, core_def->core);
404 if (arch_def)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000405 {
Greg Clayton64195a22011-02-23 00:35:02 +0000406 return arch_def->cpu;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000407 }
408 }
Greg Clayton64195a22011-02-23 00:35:02 +0000409 return LLDB_INVALID_CPUTYPE;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000410}
411
Greg Clayton64195a22011-02-23 00:35:02 +0000412uint32_t
413ArchSpec::GetMachOCPUSubType () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000414{
Greg Clayton64195a22011-02-23 00:35:02 +0000415 const CoreDefinition *core_def = FindCoreDefinition (m_core);
416 if (core_def)
417 {
418 const ArchDefinitionEntry *arch_def = FindArchDefinitionEntry (&g_macho_arch_def, core_def->core);
419 if (arch_def)
420 {
Greg Clayton1cb64962011-03-24 04:28:38 +0000421 return arch_def->sub;
Greg Clayton64195a22011-02-23 00:35:02 +0000422 }
423 }
424 return LLDB_INVALID_CPUTYPE;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000425}
426
Greg Clayton64195a22011-02-23 00:35:02 +0000427llvm::Triple::ArchType
428ArchSpec::GetMachine () const
429{
430 const CoreDefinition *core_def = FindCoreDefinition (m_core);
431 if (core_def)
432 return core_def->machine;
433
434 return llvm::Triple::UnknownArch;
435}
436
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000437uint32_t
438ArchSpec::GetAddressByteSize() const
439{
Greg Clayton64195a22011-02-23 00:35:02 +0000440 const CoreDefinition *core_def = FindCoreDefinition (m_core);
441 if (core_def)
442 return core_def->addr_byte_size;
Greg Clayton41f92322010-06-11 03:25:34 +0000443 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000444}
445
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000446ByteOrder
447ArchSpec::GetDefaultEndian () const
448{
Greg Clayton64195a22011-02-23 00:35:02 +0000449 const CoreDefinition *core_def = FindCoreDefinition (m_core);
450 if (core_def)
451 return core_def->default_byte_order;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000452 return eByteOrderInvalid;
453}
454
Greg Clayton64195a22011-02-23 00:35:02 +0000455lldb::ByteOrder
456ArchSpec::GetByteOrder () const
457{
458 if (m_byte_order == eByteOrderInvalid)
459 return GetDefaultEndian();
460 return m_byte_order;
461}
462
463//===----------------------------------------------------------------------===//
464// Mutators.
465
466bool
467ArchSpec::SetTriple (const llvm::Triple &triple)
468{
469 m_triple = triple;
470
471 llvm::StringRef arch_name (m_triple.getArchName());
472 const CoreDefinition *core_def = FindCoreDefinition (arch_name);
473 if (core_def)
474 {
475 m_core = core_def->core;
Greg Claytoneb0103f2011-04-07 22:46:35 +0000476 // Set the byte order to the default byte order for an architecture.
477 // This can be modified if needed for cases when cores handle both
478 // big and little endian
479 m_byte_order = core_def->default_byte_order;
Greg Clayton64195a22011-02-23 00:35:02 +0000480 }
481 else
482 {
483 Clear();
484 }
485
486
487 return IsValid();
488}
489
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000490static bool
491ParseMachCPUDashSubtypeTriple (const char *triple_cstr, ArchSpec &arch)
492{
493 // Accept "12-10" or "12.10" as cpu type/subtype
494 if (isdigit(triple_cstr[0]))
495 {
496 char *end = NULL;
497 errno = 0;
Greg Claytonc7bece562013-01-25 18:06:21 +0000498 uint32_t cpu = (uint32_t)::strtoul (triple_cstr, &end, 0);
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000499 if (errno == 0 && cpu != 0 && end && ((*end == '-') || (*end == '.')))
500 {
501 errno = 0;
Greg Claytonc7bece562013-01-25 18:06:21 +0000502 uint32_t sub = (uint32_t)::strtoul (end + 1, &end, 0);
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000503 if (errno == 0 && end && ((*end == '-') || (*end == '.') || (*end == '\0')))
504 {
505 if (arch.SetArchitecture (eArchTypeMachO, cpu, sub))
506 {
507 if (*end == '-')
508 {
509 llvm::StringRef vendor_os (end + 1);
510 size_t dash_pos = vendor_os.find('-');
511 if (dash_pos != llvm::StringRef::npos)
512 {
513 llvm::StringRef vendor_str(vendor_os.substr(0, dash_pos));
514 arch.GetTriple().setVendorName(vendor_str);
515 const size_t vendor_start_pos = dash_pos+1;
Greg Claytonc7bece562013-01-25 18:06:21 +0000516 dash_pos = vendor_os.find('-', vendor_start_pos);
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000517 if (dash_pos == llvm::StringRef::npos)
518 {
519 if (vendor_start_pos < vendor_os.size())
520 arch.GetTriple().setOSName(vendor_os.substr(vendor_start_pos));
521 }
522 else
523 {
524 arch.GetTriple().setOSName(vendor_os.substr(vendor_start_pos, dash_pos - vendor_start_pos));
525 }
526 }
527 }
528 return true;
529 }
530 }
531 }
532 }
533 return false;
534}
Greg Clayton64195a22011-02-23 00:35:02 +0000535bool
Greg Clayton70512312012-05-08 01:45:38 +0000536ArchSpec::SetTriple (const char *triple_cstr)
Greg Clayton64195a22011-02-23 00:35:02 +0000537{
Greg Clayton23aca092011-08-12 23:32:52 +0000538 if (triple_cstr && triple_cstr[0])
Greg Clayton64195a22011-02-23 00:35:02 +0000539 {
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000540 if (ParseMachCPUDashSubtypeTriple (triple_cstr, *this))
541 return true;
542
Greg Clayton64195a22011-02-23 00:35:02 +0000543 llvm::StringRef triple_stref (triple_cstr);
544 if (triple_stref.startswith (LLDB_ARCH_DEFAULT))
545 {
546 // Special case for the current host default architectures...
547 if (triple_stref.equals (LLDB_ARCH_DEFAULT_32BIT))
548 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture32);
549 else if (triple_stref.equals (LLDB_ARCH_DEFAULT_64BIT))
550 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture64);
551 else if (triple_stref.equals (LLDB_ARCH_DEFAULT))
552 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture);
553 }
554 else
555 {
556 std::string normalized_triple_sstr (llvm::Triple::normalize(triple_stref));
557 triple_stref = normalized_triple_sstr;
Greg Clayton70512312012-05-08 01:45:38 +0000558 SetTriple (llvm::Triple (triple_stref));
559 }
560 }
561 else
562 Clear();
563 return IsValid();
564}
565
566bool
567ArchSpec::SetTriple (const char *triple_cstr, Platform *platform)
568{
569 if (triple_cstr && triple_cstr[0])
570 {
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000571 if (ParseMachCPUDashSubtypeTriple (triple_cstr, *this))
572 return true;
573
Greg Clayton70512312012-05-08 01:45:38 +0000574 llvm::StringRef triple_stref (triple_cstr);
575 if (triple_stref.startswith (LLDB_ARCH_DEFAULT))
576 {
577 // Special case for the current host default architectures...
578 if (triple_stref.equals (LLDB_ARCH_DEFAULT_32BIT))
579 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture32);
580 else if (triple_stref.equals (LLDB_ARCH_DEFAULT_64BIT))
581 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture64);
582 else if (triple_stref.equals (LLDB_ARCH_DEFAULT))
583 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture);
584 }
585 else
586 {
587 ArchSpec raw_arch (triple_cstr);
588
589 std::string normalized_triple_sstr (llvm::Triple::normalize(triple_stref));
590 triple_stref = normalized_triple_sstr;
Greg Claytoneb0103f2011-04-07 22:46:35 +0000591 llvm::Triple normalized_triple (triple_stref);
592
593 const bool os_specified = normalized_triple.getOSName().size() > 0;
594 const bool vendor_specified = normalized_triple.getVendorName().size() > 0;
595 const bool env_specified = normalized_triple.getEnvironmentName().size() > 0;
596
597 // If we got an arch only, then default the vendor, os, environment
598 // to match the platform if one is supplied
599 if (!(os_specified || vendor_specified || env_specified))
600 {
601 if (platform)
602 {
603 // If we were given a platform, use the platform's system
604 // architecture. If this is not available (might not be
605 // connected) use the first supported architecture.
Greg Clayton70512312012-05-08 01:45:38 +0000606 ArchSpec compatible_arch;
Greg Clayton1e0c8842013-01-11 20:49:54 +0000607 if (platform->IsCompatibleArchitecture (raw_arch, false, &compatible_arch))
Greg Claytoneb0103f2011-04-07 22:46:35 +0000608 {
Greg Clayton70512312012-05-08 01:45:38 +0000609 if (compatible_arch.IsValid())
610 {
611 const llvm::Triple &compatible_triple = compatible_arch.GetTriple();
612 if (!vendor_specified)
613 normalized_triple.setVendor(compatible_triple.getVendor());
614 if (!os_specified)
615 normalized_triple.setOS(compatible_triple.getOS());
616 if (!env_specified && compatible_triple.getEnvironmentName().size())
617 normalized_triple.setEnvironment(compatible_triple.getEnvironment());
618 }
Greg Claytoneb0103f2011-04-07 22:46:35 +0000619 }
Greg Clayton70512312012-05-08 01:45:38 +0000620 else
Greg Claytoneb0103f2011-04-07 22:46:35 +0000621 {
Greg Clayton70512312012-05-08 01:45:38 +0000622 *this = raw_arch;
623 return IsValid();
Greg Claytoneb0103f2011-04-07 22:46:35 +0000624 }
625 }
626 else
627 {
628 // No platform specified, fall back to the host system for
629 // the default vendor, os, and environment.
Sean Callananbfb237bc2011-11-04 22:46:46 +0000630 llvm::Triple host_triple(llvm::sys::getDefaultTargetTriple());
Greg Clayton70512312012-05-08 01:45:38 +0000631 if (!vendor_specified)
632 normalized_triple.setVendor(host_triple.getVendor());
633 if (!vendor_specified)
634 normalized_triple.setOS(host_triple.getOS());
635 if (!env_specified && host_triple.getEnvironmentName().size())
636 normalized_triple.setEnvironment(host_triple.getEnvironment());
Greg Claytoneb0103f2011-04-07 22:46:35 +0000637 }
638 }
639 SetTriple (normalized_triple);
Greg Clayton64195a22011-02-23 00:35:02 +0000640 }
641 }
642 else
643 Clear();
644 return IsValid();
645}
646
Greg Clayton64195a22011-02-23 00:35:02 +0000647bool
Greg Claytone0d378b2011-03-24 21:19:54 +0000648ArchSpec::SetArchitecture (ArchitectureType arch_type, uint32_t cpu, uint32_t sub)
Greg Clayton64195a22011-02-23 00:35:02 +0000649{
650 m_core = kCore_invalid;
651 bool update_triple = true;
652 const ArchDefinition *arch_def = FindArchDefinition(arch_type);
653 if (arch_def)
654 {
655 const ArchDefinitionEntry *arch_def_entry = FindArchDefinitionEntry (arch_def, cpu, sub);
656 if (arch_def_entry)
657 {
658 const CoreDefinition *core_def = FindCoreDefinition (arch_def_entry->core);
659 if (core_def)
660 {
661 m_core = core_def->core;
662 update_triple = false;
Greg Clayton593577a2011-09-21 03:57:31 +0000663 // Always use the architecture name because it might be more descriptive
664 // than the architecture enum ("armv7" -> llvm::Triple::arm).
665 m_triple.setArchName(llvm::StringRef(core_def->name));
Greg Clayton64195a22011-02-23 00:35:02 +0000666 if (arch_type == eArchTypeMachO)
667 {
668 m_triple.setVendor (llvm::Triple::Apple);
Greg Clayton70512312012-05-08 01:45:38 +0000669
670 switch (core_def->machine)
671 {
672 case llvm::Triple::arm:
673 case llvm::Triple::thumb:
674 m_triple.setOS (llvm::Triple::IOS);
675 break;
676
677 case llvm::Triple::x86:
678 case llvm::Triple::x86_64:
679 default:
680 m_triple.setOS (llvm::Triple::MacOSX);
681 break;
682 }
Greg Clayton64195a22011-02-23 00:35:02 +0000683 }
684 else
685 {
686 m_triple.setVendor (llvm::Triple::UnknownVendor);
687 m_triple.setOS (llvm::Triple::UnknownOS);
688 }
Greg Clayton593577a2011-09-21 03:57:31 +0000689 // Fall back onto setting the machine type if the arch by name failed...
690 if (m_triple.getArch () == llvm::Triple::UnknownArch)
691 m_triple.setArch (core_def->machine);
Greg Clayton64195a22011-02-23 00:35:02 +0000692 }
693 }
694 }
695 CoreUpdated(update_triple);
696 return IsValid();
697}
698
Greg Clayton357132e2011-03-26 19:14:58 +0000699uint32_t
700ArchSpec::GetMinimumOpcodeByteSize() const
Greg Clayton64195a22011-02-23 00:35:02 +0000701{
Greg Clayton357132e2011-03-26 19:14:58 +0000702 const CoreDefinition *core_def = FindCoreDefinition (m_core);
703 if (core_def)
704 return core_def->min_opcode_byte_size;
705 return 0;
706}
707
708uint32_t
709ArchSpec::GetMaximumOpcodeByteSize() const
710{
711 const CoreDefinition *core_def = FindCoreDefinition (m_core);
712 if (core_def)
713 return core_def->max_opcode_byte_size;
714 return 0;
Greg Clayton64195a22011-02-23 00:35:02 +0000715}
716
Jason Molendaba813dc2012-11-04 03:20:05 +0000717bool
718ArchSpec::IsExactMatch (const ArchSpec& rhs) const
719{
Sean Callananbf4b7be2012-12-13 22:07:14 +0000720 return IsEqualTo (rhs, true);
Jason Molendaba813dc2012-11-04 03:20:05 +0000721}
722
723bool
724ArchSpec::IsCompatibleMatch (const ArchSpec& rhs) const
725{
Sean Callananbf4b7be2012-12-13 22:07:14 +0000726 return IsEqualTo (rhs, false);
Jason Molendaba813dc2012-11-04 03:20:05 +0000727}
728
729bool
Sean Callananbf4b7be2012-12-13 22:07:14 +0000730ArchSpec::IsEqualTo (const ArchSpec& rhs, bool exact_match) const
Jason Molendaba813dc2012-11-04 03:20:05 +0000731{
732 if (GetByteOrder() != rhs.GetByteOrder())
733 return false;
734
735 const ArchSpec::Core lhs_core = GetCore ();
736 const ArchSpec::Core rhs_core = rhs.GetCore ();
737
738 const bool core_match = cores_match (lhs_core, rhs_core, true, exact_match);
739
740 if (core_match)
741 {
742 const llvm::Triple &lhs_triple = GetTriple();
743 const llvm::Triple &rhs_triple = rhs.GetTriple();
744
745 const llvm::Triple::VendorType lhs_triple_vendor = lhs_triple.getVendor();
746 const llvm::Triple::VendorType rhs_triple_vendor = rhs_triple.getVendor();
747 if (lhs_triple_vendor != rhs_triple_vendor)
748 {
Sean Callananbf4b7be2012-12-13 22:07:14 +0000749 if (exact_match)
750 {
751 const bool rhs_vendor_specified = rhs.TripleVendorWasSpecified();
752 const bool lhs_vendor_specified = TripleVendorWasSpecified();
753 // Both architectures had the vendor specified, so if they aren't
754 // equal then we return false
755 if (rhs_vendor_specified && lhs_vendor_specified)
756 return false;
757 }
Jason Molendaba813dc2012-11-04 03:20:05 +0000758
759 // Only fail if both vendor types are not unknown
760 if (lhs_triple_vendor != llvm::Triple::UnknownVendor &&
761 rhs_triple_vendor != llvm::Triple::UnknownVendor)
762 return false;
763 }
764
765 const llvm::Triple::OSType lhs_triple_os = lhs_triple.getOS();
766 const llvm::Triple::OSType rhs_triple_os = rhs_triple.getOS();
767 if (lhs_triple_os != rhs_triple_os)
768 {
Sean Callananbf4b7be2012-12-13 22:07:14 +0000769 if (exact_match)
770 {
771 const bool rhs_os_specified = rhs.TripleOSWasSpecified();
772 const bool lhs_os_specified = TripleOSWasSpecified();
773 // Both architectures had the OS specified, so if they aren't
774 // equal then we return false
775 if (rhs_os_specified && lhs_os_specified)
776 return false;
777 }
Jason Molendaba813dc2012-11-04 03:20:05 +0000778 // Only fail if both os types are not unknown
779 if (lhs_triple_os != llvm::Triple::UnknownOS &&
780 rhs_triple_os != llvm::Triple::UnknownOS)
781 return false;
782 }
783
784 const llvm::Triple::EnvironmentType lhs_triple_env = lhs_triple.getEnvironment();
785 const llvm::Triple::EnvironmentType rhs_triple_env = rhs_triple.getEnvironment();
786
787 if (lhs_triple_env != rhs_triple_env)
788 {
789 // Only fail if both environment types are not unknown
790 if (lhs_triple_env != llvm::Triple::UnknownEnvironment &&
791 rhs_triple_env != llvm::Triple::UnknownEnvironment)
792 return false;
793 }
794 return true;
795 }
796 return false;
797}
798
Greg Clayton64195a22011-02-23 00:35:02 +0000799//===----------------------------------------------------------------------===//
800// Helper methods.
801
802void
803ArchSpec::CoreUpdated (bool update_triple)
804{
805 const CoreDefinition *core_def = FindCoreDefinition (m_core);
806 if (core_def)
807 {
808 if (update_triple)
809 m_triple = llvm::Triple(core_def->name, "unknown", "unknown");
810 m_byte_order = core_def->default_byte_order;
811 }
812 else
813 {
814 if (update_triple)
815 m_triple = llvm::Triple();
816 m_byte_order = eByteOrderInvalid;
817 }
818}
819
820//===----------------------------------------------------------------------===//
821// Operators.
822
Greg Clayton70512312012-05-08 01:45:38 +0000823static bool
Jason Molendaba813dc2012-11-04 03:20:05 +0000824cores_match (const ArchSpec::Core core1, const ArchSpec::Core core2, bool try_inverse, bool enforce_exact_match)
Greg Clayton70512312012-05-08 01:45:38 +0000825{
Jason Molendaba813dc2012-11-04 03:20:05 +0000826 if (core1 == core2)
827 return true;
828
Greg Clayton70512312012-05-08 01:45:38 +0000829 switch (core1)
830 {
Greg Clayton70512312012-05-08 01:45:38 +0000831 case ArchSpec::kCore_any:
832 return true;
833
834 case ArchSpec::kCore_arm_any:
835 if (core2 >= ArchSpec::kCore_arm_first && core2 <= ArchSpec::kCore_arm_last)
836 return true;
837 if (core2 >= ArchSpec::kCore_thumb_first && core2 <= ArchSpec::kCore_thumb_last)
838 return true;
839 if (core2 == ArchSpec::kCore_arm_any)
840 return true;
841 break;
842
843 case ArchSpec::kCore_x86_32_any:
844 if ((core2 >= ArchSpec::kCore_x86_32_first && core2 <= ArchSpec::kCore_x86_32_last) || (core2 == ArchSpec::kCore_x86_32_any))
845 return true;
846 break;
847
848 case ArchSpec::kCore_ppc_any:
849 if ((core2 >= ArchSpec::kCore_ppc_first && core2 <= ArchSpec::kCore_ppc_last) || (core2 == ArchSpec::kCore_ppc_any))
850 return true;
851 break;
852
853 case ArchSpec::kCore_ppc64_any:
854 if ((core2 >= ArchSpec::kCore_ppc64_first && core2 <= ArchSpec::kCore_ppc64_last) || (core2 == ArchSpec::kCore_ppc64_any))
855 return true;
856 break;
857
Johnny Chen1083b0d2012-08-28 22:53:40 +0000858 case ArchSpec::eCore_arm_armv7f:
859 case ArchSpec::eCore_arm_armv7k:
860 case ArchSpec::eCore_arm_armv7s:
Jason Molendaba813dc2012-11-04 03:20:05 +0000861 if (!enforce_exact_match)
862 {
863 try_inverse = false;
864 if (core2 == ArchSpec::eCore_arm_armv7)
865 return true;
866 }
Johnny Chen1083b0d2012-08-28 22:53:40 +0000867 break;
868
Greg Clayton70512312012-05-08 01:45:38 +0000869 default:
870 break;
871 }
872 if (try_inverse)
Jason Molendaba813dc2012-11-04 03:20:05 +0000873 return cores_match (core2, core1, false, enforce_exact_match);
Greg Clayton70512312012-05-08 01:45:38 +0000874 return false;
875}
876
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000877bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000878lldb_private::operator<(const ArchSpec& lhs, const ArchSpec& rhs)
879{
Greg Clayton64195a22011-02-23 00:35:02 +0000880 const ArchSpec::Core lhs_core = lhs.GetCore ();
881 const ArchSpec::Core rhs_core = rhs.GetCore ();
882 return lhs_core < rhs_core;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000883}