blob: 3d59d28a332d457fc212c9755f955dbfdff5aa0d [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>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000013
14#include <string>
15
Greg Clayton41f92322010-06-11 03:25:34 +000016#include "llvm/Support/ELF.h"
Stephen Wilsonfacebfc2011-02-24 19:13:58 +000017#include "llvm/Support/Host.h"
Greg Clayton41f92322010-06-11 03:25:34 +000018#include "llvm/Support/MachO.h"
Greg Clayton514487e2011-02-15 21:59:32 +000019#include "lldb/Host/Endian.h"
20#include "lldb/Host/Host.h"
Greg Clayton41f92322010-06-11 03:25:34 +000021
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022using namespace lldb;
23using namespace lldb_private;
24
Greg Clayton64195a22011-02-23 00:35:02 +000025#define ARCH_SPEC_SEPARATOR_CHAR '-'
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026
Greg Clayton64195a22011-02-23 00:35:02 +000027namespace lldb_private {
Chris Lattner30fdc8d2010-06-08 16:52:24 +000028
Greg Clayton64195a22011-02-23 00:35:02 +000029 struct CoreDefinition
30 {
31 ByteOrder default_byte_order;
32 uint32_t addr_byte_size;
33 llvm::Triple::ArchType machine;
34 ArchSpec::Core core;
35 const char *name;
36 };
37
38}
39
40// This core information can be looked using the ArchSpec::Core as the index
41static const CoreDefinition g_core_definitions[ArchSpec::kNumCores] =
Chris Lattner30fdc8d2010-06-08 16:52:24 +000042{
Greg Clayton64195a22011-02-23 00:35:02 +000043 { eByteOrderLittle, 4, llvm::Triple::alpha , ArchSpec::eCore_alpha_generic , "alpha" },
44
45 { eByteOrderLittle, 4, llvm::Triple::arm , ArchSpec::eCore_arm_generic , "arm" },
46 { eByteOrderLittle, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv4 , "armv4" },
47 { eByteOrderLittle, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv4t , "armv4t" },
48 { eByteOrderLittle, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv5 , "armv5" },
49 { eByteOrderLittle, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv5t , "armv5t" },
50 { eByteOrderLittle, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv6 , "armv6" },
51 { eByteOrderLittle, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7 , "armv7" },
52 { eByteOrderLittle, 4, llvm::Triple::arm , ArchSpec::eCore_arm_xscale , "xscale" },
53
54 { eByteOrderLittle, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_generic , "ppc" },
55 { eByteOrderLittle, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc601 , "ppc601" },
56 { eByteOrderLittle, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc602 , "ppc602" },
57 { eByteOrderLittle, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc603 , "ppc603" },
58 { eByteOrderLittle, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc603e , "ppc603e" },
59 { eByteOrderLittle, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc603ev , "ppc603ev" },
60 { eByteOrderLittle, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc604 , "ppc604" },
61 { eByteOrderLittle, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc604e , "ppc604e" },
62 { eByteOrderLittle, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc620 , "ppc620" },
63 { eByteOrderLittle, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc750 , "ppc750" },
64 { eByteOrderLittle, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc7400 , "ppc7400" },
65 { eByteOrderLittle, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc7450 , "ppc7450" },
66 { eByteOrderLittle, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc970 , "ppc970" },
67
68 { eByteOrderLittle, 8, llvm::Triple::ppc64 , ArchSpec::eCore_ppc64_generic , "ppc64" },
69 { eByteOrderLittle, 8, llvm::Triple::ppc64 , ArchSpec::eCore_ppc64_ppc970_64 , "ppc970-64" },
70
71 { eByteOrderLittle, 4, llvm::Triple::sparc , ArchSpec::eCore_sparc_generic , "sparc" },
72 { eByteOrderLittle, 8, llvm::Triple::sparcv9, ArchSpec::eCore_sparc9_generic , "sparcv9" },
73
74 { eByteOrderLittle, 4, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i386 , "i386" },
75 { eByteOrderLittle, 4, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i486 , "i486" },
76 { eByteOrderLittle, 4, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i486sx , "i486sx" },
77
78 { eByteOrderLittle, 8, llvm::Triple::x86_64 , ArchSpec::eCore_x86_64_x86_64 , "x86_64" }
79};
80
81struct ArchDefinitionEntry
82{
83 ArchSpec::Core core;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000084 uint32_t cpu;
85 uint32_t sub;
Greg Clayton64195a22011-02-23 00:35:02 +000086};
87
88struct ArchDefinition
89{
90 ArchitectureType type;
91 size_t num_entries;
92 const ArchDefinitionEntry *entries;
93 uint32_t cpu_mask;
94 uint32_t sub_mask;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000095 const char *name;
96};
97
Greg Clayton41f92322010-06-11 03:25:34 +000098
Greg Clayton64195a22011-02-23 00:35:02 +000099#define CPU_ANY (UINT32_MAX)
100
101//===----------------------------------------------------------------------===//
102// A table that gets searched linearly for matches. This table is used to
103// convert cpu type and subtypes to architecture names, and to convert
104// architecture names to cpu types and subtypes. The ordering is important and
105// allows the precedence to be set when the table is built.
106static const ArchDefinitionEntry g_macho_arch_entries[] =
Greg Clayton41f92322010-06-11 03:25:34 +0000107{
Greg Clayton64195a22011-02-23 00:35:02 +0000108 { ArchSpec::eCore_arm_generic , llvm::MachO::CPUTypeARM , CPU_ANY },
109 { ArchSpec::eCore_arm_generic , llvm::MachO::CPUTypeARM , 0 },
110 { ArchSpec::eCore_arm_armv4 , llvm::MachO::CPUTypeARM , 5 },
111 { ArchSpec::eCore_arm_armv6 , llvm::MachO::CPUTypeARM , 6 },
112 { ArchSpec::eCore_arm_armv5 , llvm::MachO::CPUTypeARM , 7 },
113 { ArchSpec::eCore_arm_xscale , llvm::MachO::CPUTypeARM , 8 },
114 { ArchSpec::eCore_arm_armv7 , llvm::MachO::CPUTypeARM , 9 },
115 { ArchSpec::eCore_ppc_generic , llvm::MachO::CPUTypePowerPC , CPU_ANY },
116 { ArchSpec::eCore_ppc_generic , llvm::MachO::CPUTypePowerPC , 0 },
117 { ArchSpec::eCore_ppc_ppc601 , llvm::MachO::CPUTypePowerPC , 1 },
118 { ArchSpec::eCore_ppc_ppc602 , llvm::MachO::CPUTypePowerPC , 2 },
119 { ArchSpec::eCore_ppc_ppc603 , llvm::MachO::CPUTypePowerPC , 3 },
120 { ArchSpec::eCore_ppc_ppc603e , llvm::MachO::CPUTypePowerPC , 4 },
121 { ArchSpec::eCore_ppc_ppc603ev , llvm::MachO::CPUTypePowerPC , 5 },
122 { ArchSpec::eCore_ppc_ppc604 , llvm::MachO::CPUTypePowerPC , 6 },
123 { ArchSpec::eCore_ppc_ppc604e , llvm::MachO::CPUTypePowerPC , 7 },
124 { ArchSpec::eCore_ppc_ppc620 , llvm::MachO::CPUTypePowerPC , 8 },
125 { ArchSpec::eCore_ppc_ppc750 , llvm::MachO::CPUTypePowerPC , 9 },
126 { ArchSpec::eCore_ppc_ppc7400 , llvm::MachO::CPUTypePowerPC , 10 },
127 { ArchSpec::eCore_ppc_ppc7450 , llvm::MachO::CPUTypePowerPC , 11 },
128 { ArchSpec::eCore_ppc_ppc970 , llvm::MachO::CPUTypePowerPC , 100 },
129 { ArchSpec::eCore_ppc64_generic , llvm::MachO::CPUTypePowerPC64 , 0 },
130 { ArchSpec::eCore_ppc64_ppc970_64 , llvm::MachO::CPUTypePowerPC64 , 100 },
131 { ArchSpec::eCore_x86_32_i386 , llvm::MachO::CPUTypeI386 , 3 },
132 { ArchSpec::eCore_x86_32_i486 , llvm::MachO::CPUTypeI386 , 4 },
133 { ArchSpec::eCore_x86_32_i486sx , llvm::MachO::CPUTypeI386 , 0x84 },
134 { ArchSpec::eCore_x86_32_i386 , llvm::MachO::CPUTypeI386 , CPU_ANY },
135 { ArchSpec::eCore_x86_64_x86_64 , llvm::MachO::CPUTypeX86_64 , 3 },
136 { ArchSpec::eCore_x86_64_x86_64 , llvm::MachO::CPUTypeX86_64 , CPU_ANY }
137};
138static const ArchDefinition g_macho_arch_def = {
139 eArchTypeMachO,
140 sizeof(g_macho_arch_entries)/sizeof(g_macho_arch_entries[0]),
141 g_macho_arch_entries,
142 UINT32_MAX, // CPU type mask
143 0x00FFFFFFu, // CPU subtype mask
144 "mach-o"
Greg Clayton41f92322010-06-11 03:25:34 +0000145};
146
Greg Clayton64195a22011-02-23 00:35:02 +0000147//===----------------------------------------------------------------------===//
148// A table that gets searched linearly for matches. This table is used to
149// convert cpu type and subtypes to architecture names, and to convert
150// architecture names to cpu types and subtypes. The ordering is important and
151// allows the precedence to be set when the table is built.
152static const ArchDefinitionEntry g_elf_arch_entries[] =
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000153{
Greg Clayton64195a22011-02-23 00:35:02 +0000154 { ArchSpec::eCore_sparc_generic , llvm::ELF::EM_SPARC , LLDB_INVALID_CPUTYPE }, // Sparc
155 { ArchSpec::eCore_x86_32_i386 , llvm::ELF::EM_386 , LLDB_INVALID_CPUTYPE }, // Intel 80386
156 { ArchSpec::eCore_x86_32_i486 , llvm::ELF::EM_486 , LLDB_INVALID_CPUTYPE }, // Intel 486 (deprecated)
157 { ArchSpec::eCore_ppc_generic , llvm::ELF::EM_PPC , LLDB_INVALID_CPUTYPE }, // PowerPC
158 { ArchSpec::eCore_ppc64_generic , llvm::ELF::EM_PPC64 , LLDB_INVALID_CPUTYPE }, // PowerPC64
159 { ArchSpec::eCore_arm_generic , llvm::ELF::EM_ARM , LLDB_INVALID_CPUTYPE }, // ARM
160 { ArchSpec::eCore_alpha_generic , llvm::ELF::EM_ALPHA , LLDB_INVALID_CPUTYPE }, // DEC Alpha
161 { ArchSpec::eCore_sparc9_generic , llvm::ELF::EM_SPARCV9, LLDB_INVALID_CPUTYPE }, // SPARC V9
162 { ArchSpec::eCore_x86_64_x86_64 , llvm::ELF::EM_X86_64 , LLDB_INVALID_CPUTYPE }, // AMD64
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000163};
164
Greg Clayton64195a22011-02-23 00:35:02 +0000165static const ArchDefinition g_elf_arch_def = {
166 eArchTypeELF,
167 sizeof(g_elf_arch_entries)/sizeof(g_elf_arch_entries[0]),
168 g_elf_arch_entries,
169 UINT32_MAX, // CPU type mask
170 UINT32_MAX, // CPU subtype mask
171 "elf",
Greg Clayton41f92322010-06-11 03:25:34 +0000172};
173
Greg Clayton64195a22011-02-23 00:35:02 +0000174//===----------------------------------------------------------------------===//
175// Table of all ArchDefinitions
176static const ArchDefinition *g_arch_definitions[] = {
177 &g_macho_arch_def,
178 &g_elf_arch_def,
179};
Greg Clayton41f92322010-06-11 03:25:34 +0000180
Greg Clayton64195a22011-02-23 00:35:02 +0000181static const size_t k_num_arch_definitions =
182 sizeof(g_arch_definitions) / sizeof(g_arch_definitions[0]);
183
184//===----------------------------------------------------------------------===//
185// Static helper functions.
186
187
188// Get the architecture definition for a given object type.
189static const ArchDefinition *
190FindArchDefinition (ArchitectureType arch_type)
191{
192 for (unsigned int i = 0; i < k_num_arch_definitions; ++i)
193 {
194 const ArchDefinition *def = g_arch_definitions[i];
195 if (def->type == arch_type)
196 return def;
197 }
198 return NULL;
199}
200
201// Get an architecture definition by name.
202static const CoreDefinition *
203FindCoreDefinition (llvm::StringRef name)
204{
205 for (unsigned int i = 0; i < ArchSpec::kNumCores; ++i)
206 {
207 if (name.equals_lower(g_core_definitions[i].name))
208 return &g_core_definitions[i];
209 }
210 return NULL;
211}
212
213static inline const CoreDefinition *
214FindCoreDefinition (ArchSpec::Core core)
215{
216 if (core >= 0 && core < ArchSpec::kNumCores)
217 return &g_core_definitions[core];
218 return NULL;
219}
220
221// Get a definition entry by cpu type and subtype.
222static const ArchDefinitionEntry *
223FindArchDefinitionEntry (const ArchDefinition *def, uint32_t cpu, uint32_t sub)
224{
225 if (def == NULL)
226 return NULL;
227
228 const uint32_t cpu_mask = def->cpu_mask;
229 const uint32_t sub_mask = def->sub_mask;
230 const ArchDefinitionEntry *entries = def->entries;
231 for (size_t i = 0; i < def->num_entries; ++i)
232 {
233 if ((entries[i].cpu == (cpu_mask & cpu)) &&
234 (entries[i].sub == (sub_mask & sub)))
235 return &entries[i];
236 }
237 return NULL;
238}
239
240static const ArchDefinitionEntry *
241FindArchDefinitionEntry (const ArchDefinition *def, ArchSpec::Core core)
242{
243 if (def == NULL)
244 return NULL;
245
246 const ArchDefinitionEntry *entries = def->entries;
247 for (size_t i = 0; i < def->num_entries; ++i)
248 {
249 if (entries[i].core == core)
250 return &entries[i];
251 }
252 return NULL;
253}
254
255//===----------------------------------------------------------------------===//
256// Constructors and destructors.
257
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000258ArchSpec::ArchSpec() :
Greg Clayton514487e2011-02-15 21:59:32 +0000259 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000260 m_core (kCore_invalid),
261 m_byte_order (eByteOrderInvalid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000262{
263}
264
Greg Clayton64195a22011-02-23 00:35:02 +0000265ArchSpec::ArchSpec (const char *triple_cstr) :
Greg Clayton514487e2011-02-15 21:59:32 +0000266 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000267 m_core (kCore_invalid),
268 m_byte_order (eByteOrderInvalid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000269{
Greg Clayton64195a22011-02-23 00:35:02 +0000270 if (triple_cstr)
271 SetTriple(triple_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000272}
273
Greg Clayton64195a22011-02-23 00:35:02 +0000274ArchSpec::ArchSpec(const llvm::Triple &triple) :
Greg Clayton514487e2011-02-15 21:59:32 +0000275 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000276 m_core (kCore_invalid),
277 m_byte_order (eByteOrderInvalid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000278{
Greg Clayton64195a22011-02-23 00:35:02 +0000279 SetTriple(triple);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000280}
281
Greg Clayton64195a22011-02-23 00:35:02 +0000282ArchSpec::ArchSpec (lldb::ArchitectureType arch_type, uint32_t cpu, uint32_t subtype) :
283 m_triple (),
284 m_core (kCore_invalid),
285 m_byte_order (eByteOrderInvalid)
286{
287 SetArchitecture (arch_type, cpu, subtype);
288}
289
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000290ArchSpec::~ArchSpec()
291{
292}
293
Greg Clayton64195a22011-02-23 00:35:02 +0000294//===----------------------------------------------------------------------===//
295// Assignment and initialization.
296
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000297const ArchSpec&
298ArchSpec::operator= (const ArchSpec& rhs)
299{
300 if (this != &rhs)
301 {
Greg Clayton514487e2011-02-15 21:59:32 +0000302 m_triple = rhs.m_triple;
Greg Clayton64195a22011-02-23 00:35:02 +0000303 m_core = rhs.m_core;
Greg Clayton514487e2011-02-15 21:59:32 +0000304 m_byte_order = rhs.m_byte_order;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000305 }
306 return *this;
307}
308
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000309void
310ArchSpec::Clear()
311{
Greg Clayton514487e2011-02-15 21:59:32 +0000312 m_triple = llvm::Triple();
Greg Clayton64195a22011-02-23 00:35:02 +0000313 m_core = kCore_invalid;
314 m_byte_order = eByteOrderInvalid;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000315}
316
Greg Clayton64195a22011-02-23 00:35:02 +0000317//===----------------------------------------------------------------------===//
318// Predicates.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000319
Greg Clayton41f92322010-06-11 03:25:34 +0000320
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000321const char *
Greg Clayton64195a22011-02-23 00:35:02 +0000322ArchSpec::GetArchitectureName () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000323{
Greg Clayton64195a22011-02-23 00:35:02 +0000324 const CoreDefinition *core_def = FindCoreDefinition (m_core);
325 if (core_def)
326 return core_def->name;
327 return "unknown";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000328}
329
Greg Clayton64195a22011-02-23 00:35:02 +0000330uint32_t
331ArchSpec::GetMachOCPUType () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000332{
Greg Clayton64195a22011-02-23 00:35:02 +0000333 const CoreDefinition *core_def = FindCoreDefinition (m_core);
334 if (core_def)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000335 {
Greg Clayton64195a22011-02-23 00:35:02 +0000336 const ArchDefinitionEntry *arch_def = FindArchDefinitionEntry (&g_macho_arch_def, core_def->core);
337 if (arch_def)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000338 {
Greg Clayton64195a22011-02-23 00:35:02 +0000339 return arch_def->cpu;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000340 }
341 }
Greg Clayton64195a22011-02-23 00:35:02 +0000342 return LLDB_INVALID_CPUTYPE;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000343}
344
Greg Clayton64195a22011-02-23 00:35:02 +0000345uint32_t
346ArchSpec::GetMachOCPUSubType () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000347{
Greg Clayton64195a22011-02-23 00:35:02 +0000348 const CoreDefinition *core_def = FindCoreDefinition (m_core);
349 if (core_def)
350 {
351 const ArchDefinitionEntry *arch_def = FindArchDefinitionEntry (&g_macho_arch_def, core_def->core);
352 if (arch_def)
353 {
354 return arch_def->cpu;
355 }
356 }
357 return LLDB_INVALID_CPUTYPE;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000358}
359
Greg Clayton64195a22011-02-23 00:35:02 +0000360llvm::Triple::ArchType
361ArchSpec::GetMachine () const
362{
363 const CoreDefinition *core_def = FindCoreDefinition (m_core);
364 if (core_def)
365 return core_def->machine;
366
367 return llvm::Triple::UnknownArch;
368}
369
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000370uint32_t
371ArchSpec::GetAddressByteSize() const
372{
Greg Clayton64195a22011-02-23 00:35:02 +0000373 const CoreDefinition *core_def = FindCoreDefinition (m_core);
374 if (core_def)
375 return core_def->addr_byte_size;
Greg Clayton41f92322010-06-11 03:25:34 +0000376 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000377}
378
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000379ByteOrder
380ArchSpec::GetDefaultEndian () const
381{
Greg Clayton64195a22011-02-23 00:35:02 +0000382 const CoreDefinition *core_def = FindCoreDefinition (m_core);
383 if (core_def)
384 return core_def->default_byte_order;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000385 return eByteOrderInvalid;
386}
387
Greg Clayton64195a22011-02-23 00:35:02 +0000388lldb::ByteOrder
389ArchSpec::GetByteOrder () const
390{
391 if (m_byte_order == eByteOrderInvalid)
392 return GetDefaultEndian();
393 return m_byte_order;
394}
395
396//===----------------------------------------------------------------------===//
397// Mutators.
398
399bool
400ArchSpec::SetTriple (const llvm::Triple &triple)
401{
402 m_triple = triple;
403
404 llvm::StringRef arch_name (m_triple.getArchName());
405 const CoreDefinition *core_def = FindCoreDefinition (arch_name);
406 if (core_def)
407 {
408 m_core = core_def->core;
409 m_byte_order = core_def->default_byte_order;
410
Stephen Wilsonfacebfc2011-02-24 19:13:58 +0000411 if (m_triple.getVendor() == llvm::Triple::UnknownVendor &&
412 m_triple.getOS() == llvm::Triple::UnknownOS &&
413 m_triple.getEnvironment() == llvm::Triple::UnknownEnvironment)
414 {
415 llvm::Triple host_triple(llvm::sys::getHostTriple());
416
417 m_triple.setVendor(host_triple.getVendor());
418 m_triple.setOS(host_triple.getOS());
419 m_triple.setEnvironment(host_triple.getEnvironment());
420 }
Greg Clayton64195a22011-02-23 00:35:02 +0000421 }
422 else
423 {
424 Clear();
425 }
426
427
428 return IsValid();
429}
430
431bool
432ArchSpec::SetTriple (const char *triple_cstr)
433{
434 if (triple_cstr || triple_cstr[0])
435 {
436 llvm::StringRef triple_stref (triple_cstr);
437 if (triple_stref.startswith (LLDB_ARCH_DEFAULT))
438 {
439 // Special case for the current host default architectures...
440 if (triple_stref.equals (LLDB_ARCH_DEFAULT_32BIT))
441 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture32);
442 else if (triple_stref.equals (LLDB_ARCH_DEFAULT_64BIT))
443 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture64);
444 else if (triple_stref.equals (LLDB_ARCH_DEFAULT))
445 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture);
446 }
447 else
448 {
449 std::string normalized_triple_sstr (llvm::Triple::normalize(triple_stref));
450 triple_stref = normalized_triple_sstr;
451 SetTriple (llvm::Triple (triple_stref));
452 }
453 }
454 else
455 Clear();
456 return IsValid();
457}
458
459//bool
460//ArchSpec::SetArchitecture (const char *arch_name)
461//{
462// return SetArchitecture(llvm::StringRef (arch_name));
463//}
464//
465//bool
466//ArchSpec::SetArchitecture (const llvm::StringRef& arch_name)
467//{
468// // All default architecture names start with LLDB_ARCH_DEFAULT.
469// if (arch_name.startswith (LLDB_ARCH_DEFAULT))
470// {
471// // Special case for the current host default architectures...
472// if (arch_name.equals (LLDB_ARCH_DEFAULT_32BIT))
473// *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture32);
474// else if (arch_name.equals (LLDB_ARCH_DEFAULT_64BIT))
475// *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture64);
476// else
477// *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture);
478// }
479// else
480// {
481// const CoreDefinition *core_def = FindCoreDefinition (arch_name);
482// if (core_def)
483// m_core = core_def->core;
484// CoreUpdated(true);
485// }
486// return IsValid();
487//}
488//
489bool
490ArchSpec::SetArchitecture (lldb::ArchitectureType arch_type, uint32_t cpu, uint32_t sub)
491{
492 m_core = kCore_invalid;
493 bool update_triple = true;
494 const ArchDefinition *arch_def = FindArchDefinition(arch_type);
495 if (arch_def)
496 {
497 const ArchDefinitionEntry *arch_def_entry = FindArchDefinitionEntry (arch_def, cpu, sub);
498 if (arch_def_entry)
499 {
500 const CoreDefinition *core_def = FindCoreDefinition (arch_def_entry->core);
501 if (core_def)
502 {
503 m_core = core_def->core;
504 update_triple = false;
505 m_triple.setArch (core_def->machine);
506 if (arch_type == eArchTypeMachO)
507 {
508 m_triple.setVendor (llvm::Triple::Apple);
509 m_triple.setOS (llvm::Triple::Darwin);
510 }
511 else
512 {
513 m_triple.setVendor (llvm::Triple::UnknownVendor);
514 m_triple.setOS (llvm::Triple::UnknownOS);
515 }
516 }
517 }
518 }
519 CoreUpdated(update_triple);
520 return IsValid();
521}
522
523void
524ArchSpec::SetByteOrder (lldb::ByteOrder byte_order)
525{
526 m_byte_order = byte_order;
527}
528
529//===----------------------------------------------------------------------===//
530// Helper methods.
531
532void
533ArchSpec::CoreUpdated (bool update_triple)
534{
535 const CoreDefinition *core_def = FindCoreDefinition (m_core);
536 if (core_def)
537 {
538 if (update_triple)
539 m_triple = llvm::Triple(core_def->name, "unknown", "unknown");
540 m_byte_order = core_def->default_byte_order;
541 }
542 else
543 {
544 if (update_triple)
545 m_triple = llvm::Triple();
546 m_byte_order = eByteOrderInvalid;
547 }
548}
549
550//===----------------------------------------------------------------------===//
551// Operators.
552
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000553bool
554lldb_private::operator== (const ArchSpec& lhs, const ArchSpec& rhs)
555{
Greg Clayton64195a22011-02-23 00:35:02 +0000556 const ArchSpec::Core lhs_core = lhs.GetCore ();
557 const ArchSpec::Core rhs_core = rhs.GetCore ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000558
Greg Clayton64195a22011-02-23 00:35:02 +0000559 if (lhs_core == rhs_core)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000560 return true;
561
Greg Clayton64195a22011-02-23 00:35:02 +0000562 if (lhs_core == ArchSpec::kCore_any || rhs_core == ArchSpec::kCore_any)
563 return true;
564
565 if (lhs_core == ArchSpec::kCore_arm_any)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000566 {
Greg Clayton64195a22011-02-23 00:35:02 +0000567 if ((rhs_core >= ArchSpec::kCore_arm_first && rhs_core <= ArchSpec::kCore_arm_last) || (rhs_core == ArchSpec::kCore_arm_any))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000568 return true;
Greg Clayton64195a22011-02-23 00:35:02 +0000569 }
570 else if (rhs_core == ArchSpec::kCore_arm_any)
571 {
572 if ((lhs_core >= ArchSpec::kCore_arm_first && lhs_core <= ArchSpec::kCore_arm_last) || (lhs_core == ArchSpec::kCore_arm_any))
573 return true;
574 }
575 else if (lhs_core == ArchSpec::kCore_x86_32_any)
576 {
577 if ((rhs_core >= ArchSpec::kCore_x86_32_first && rhs_core <= ArchSpec::kCore_x86_32_last) || (rhs_core == ArchSpec::kCore_x86_32_any))
578 return true;
579 }
580 else if (rhs_core == ArchSpec::kCore_x86_32_any)
581 {
582 if ((lhs_core >= ArchSpec::kCore_x86_32_first && lhs_core <= ArchSpec::kCore_x86_32_last) || (lhs_core == ArchSpec::kCore_x86_32_any))
583 return true;
584 }
585 else if (lhs_core == ArchSpec::kCore_ppc_any)
586 {
587 if ((rhs_core >= ArchSpec::kCore_ppc_first && rhs_core <= ArchSpec::kCore_ppc_last) || (rhs_core == ArchSpec::kCore_ppc_any))
588 return true;
589 }
590 else if (rhs_core == ArchSpec::kCore_ppc_any)
591 {
592 if ((lhs_core >= ArchSpec::kCore_ppc_first && lhs_core <= ArchSpec::kCore_ppc_last) || (lhs_core == ArchSpec::kCore_ppc_any))
593 return true;
594 }
595 else if (lhs_core == ArchSpec::kCore_ppc64_any)
596 {
597 if ((rhs_core >= ArchSpec::kCore_ppc64_first && rhs_core <= ArchSpec::kCore_ppc64_last) || (rhs_core == ArchSpec::kCore_ppc64_any))
598 return true;
599 }
600 else if (rhs_core == ArchSpec::kCore_ppc64_any)
601 {
602 if ((lhs_core >= ArchSpec::kCore_ppc64_first && lhs_core <= ArchSpec::kCore_ppc64_last) || (lhs_core == ArchSpec::kCore_ppc64_any))
603 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000604 }
605 return false;
606}
607
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000608bool
609lldb_private::operator!= (const ArchSpec& lhs, const ArchSpec& rhs)
610{
611 return !(lhs == rhs);
612}
613
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000614bool
615lldb_private::operator<(const ArchSpec& lhs, const ArchSpec& rhs)
616{
Greg Clayton64195a22011-02-23 00:35:02 +0000617 const ArchSpec::Core lhs_core = lhs.GetCore ();
618 const ArchSpec::Core rhs_core = rhs.GetCore ();
619 return lhs_core < rhs_core;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000620}