blob: a5493b3954d4514d285b825c3d0c790bb27a8bce [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"
17#include "llvm/Support/MachO.h"
Greg Clayton514487e2011-02-15 21:59:32 +000018#include "lldb/Host/Endian.h"
19#include "lldb/Host/Host.h"
Greg Clayton41f92322010-06-11 03:25:34 +000020
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021using namespace lldb;
22using namespace lldb_private;
23
Greg Clayton64195a22011-02-23 00:35:02 +000024#define ARCH_SPEC_SEPARATOR_CHAR '-'
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025
Greg Clayton64195a22011-02-23 00:35:02 +000026namespace lldb_private {
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027
Greg Clayton64195a22011-02-23 00:35:02 +000028 struct CoreDefinition
29 {
30 ByteOrder default_byte_order;
31 uint32_t addr_byte_size;
32 llvm::Triple::ArchType machine;
33 ArchSpec::Core core;
34 const char *name;
35 };
36
37}
38
39// This core information can be looked using the ArchSpec::Core as the index
40static const CoreDefinition g_core_definitions[ArchSpec::kNumCores] =
Chris Lattner30fdc8d2010-06-08 16:52:24 +000041{
Greg Clayton64195a22011-02-23 00:35:02 +000042 { eByteOrderLittle, 4, llvm::Triple::alpha , ArchSpec::eCore_alpha_generic , "alpha" },
43
44 { eByteOrderLittle, 4, llvm::Triple::arm , ArchSpec::eCore_arm_generic , "arm" },
45 { eByteOrderLittle, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv4 , "armv4" },
46 { eByteOrderLittle, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv4t , "armv4t" },
47 { eByteOrderLittle, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv5 , "armv5" },
48 { eByteOrderLittle, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv5t , "armv5t" },
49 { eByteOrderLittle, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv6 , "armv6" },
50 { eByteOrderLittle, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7 , "armv7" },
51 { eByteOrderLittle, 4, llvm::Triple::arm , ArchSpec::eCore_arm_xscale , "xscale" },
52
53 { eByteOrderLittle, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_generic , "ppc" },
54 { eByteOrderLittle, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc601 , "ppc601" },
55 { eByteOrderLittle, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc602 , "ppc602" },
56 { eByteOrderLittle, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc603 , "ppc603" },
57 { eByteOrderLittle, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc603e , "ppc603e" },
58 { eByteOrderLittle, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc603ev , "ppc603ev" },
59 { eByteOrderLittle, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc604 , "ppc604" },
60 { eByteOrderLittle, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc604e , "ppc604e" },
61 { eByteOrderLittle, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc620 , "ppc620" },
62 { eByteOrderLittle, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc750 , "ppc750" },
63 { eByteOrderLittle, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc7400 , "ppc7400" },
64 { eByteOrderLittle, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc7450 , "ppc7450" },
65 { eByteOrderLittle, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc970 , "ppc970" },
66
67 { eByteOrderLittle, 8, llvm::Triple::ppc64 , ArchSpec::eCore_ppc64_generic , "ppc64" },
68 { eByteOrderLittle, 8, llvm::Triple::ppc64 , ArchSpec::eCore_ppc64_ppc970_64 , "ppc970-64" },
69
70 { eByteOrderLittle, 4, llvm::Triple::sparc , ArchSpec::eCore_sparc_generic , "sparc" },
71 { eByteOrderLittle, 8, llvm::Triple::sparcv9, ArchSpec::eCore_sparc9_generic , "sparcv9" },
72
73 { eByteOrderLittle, 4, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i386 , "i386" },
74 { eByteOrderLittle, 4, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i486 , "i486" },
75 { eByteOrderLittle, 4, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i486sx , "i486sx" },
76
77 { eByteOrderLittle, 8, llvm::Triple::x86_64 , ArchSpec::eCore_x86_64_x86_64 , "x86_64" }
78};
79
80struct ArchDefinitionEntry
81{
82 ArchSpec::Core core;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000083 uint32_t cpu;
84 uint32_t sub;
Greg Clayton64195a22011-02-23 00:35:02 +000085};
86
87struct ArchDefinition
88{
89 ArchitectureType type;
90 size_t num_entries;
91 const ArchDefinitionEntry *entries;
92 uint32_t cpu_mask;
93 uint32_t sub_mask;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000094 const char *name;
95};
96
Greg Clayton41f92322010-06-11 03:25:34 +000097
Greg Clayton64195a22011-02-23 00:35:02 +000098#define CPU_ANY (UINT32_MAX)
99
100//===----------------------------------------------------------------------===//
101// A table that gets searched linearly for matches. This table is used to
102// convert cpu type and subtypes to architecture names, and to convert
103// architecture names to cpu types and subtypes. The ordering is important and
104// allows the precedence to be set when the table is built.
105static const ArchDefinitionEntry g_macho_arch_entries[] =
Greg Clayton41f92322010-06-11 03:25:34 +0000106{
Greg Clayton64195a22011-02-23 00:35:02 +0000107 { ArchSpec::eCore_arm_generic , llvm::MachO::CPUTypeARM , CPU_ANY },
108 { ArchSpec::eCore_arm_generic , llvm::MachO::CPUTypeARM , 0 },
109 { ArchSpec::eCore_arm_armv4 , llvm::MachO::CPUTypeARM , 5 },
110 { ArchSpec::eCore_arm_armv6 , llvm::MachO::CPUTypeARM , 6 },
111 { ArchSpec::eCore_arm_armv5 , llvm::MachO::CPUTypeARM , 7 },
112 { ArchSpec::eCore_arm_xscale , llvm::MachO::CPUTypeARM , 8 },
113 { ArchSpec::eCore_arm_armv7 , llvm::MachO::CPUTypeARM , 9 },
114 { ArchSpec::eCore_ppc_generic , llvm::MachO::CPUTypePowerPC , CPU_ANY },
115 { ArchSpec::eCore_ppc_generic , llvm::MachO::CPUTypePowerPC , 0 },
116 { ArchSpec::eCore_ppc_ppc601 , llvm::MachO::CPUTypePowerPC , 1 },
117 { ArchSpec::eCore_ppc_ppc602 , llvm::MachO::CPUTypePowerPC , 2 },
118 { ArchSpec::eCore_ppc_ppc603 , llvm::MachO::CPUTypePowerPC , 3 },
119 { ArchSpec::eCore_ppc_ppc603e , llvm::MachO::CPUTypePowerPC , 4 },
120 { ArchSpec::eCore_ppc_ppc603ev , llvm::MachO::CPUTypePowerPC , 5 },
121 { ArchSpec::eCore_ppc_ppc604 , llvm::MachO::CPUTypePowerPC , 6 },
122 { ArchSpec::eCore_ppc_ppc604e , llvm::MachO::CPUTypePowerPC , 7 },
123 { ArchSpec::eCore_ppc_ppc620 , llvm::MachO::CPUTypePowerPC , 8 },
124 { ArchSpec::eCore_ppc_ppc750 , llvm::MachO::CPUTypePowerPC , 9 },
125 { ArchSpec::eCore_ppc_ppc7400 , llvm::MachO::CPUTypePowerPC , 10 },
126 { ArchSpec::eCore_ppc_ppc7450 , llvm::MachO::CPUTypePowerPC , 11 },
127 { ArchSpec::eCore_ppc_ppc970 , llvm::MachO::CPUTypePowerPC , 100 },
128 { ArchSpec::eCore_ppc64_generic , llvm::MachO::CPUTypePowerPC64 , 0 },
129 { ArchSpec::eCore_ppc64_ppc970_64 , llvm::MachO::CPUTypePowerPC64 , 100 },
130 { ArchSpec::eCore_x86_32_i386 , llvm::MachO::CPUTypeI386 , 3 },
131 { ArchSpec::eCore_x86_32_i486 , llvm::MachO::CPUTypeI386 , 4 },
132 { ArchSpec::eCore_x86_32_i486sx , llvm::MachO::CPUTypeI386 , 0x84 },
133 { ArchSpec::eCore_x86_32_i386 , llvm::MachO::CPUTypeI386 , CPU_ANY },
134 { ArchSpec::eCore_x86_64_x86_64 , llvm::MachO::CPUTypeX86_64 , 3 },
135 { ArchSpec::eCore_x86_64_x86_64 , llvm::MachO::CPUTypeX86_64 , CPU_ANY }
136};
137static const ArchDefinition g_macho_arch_def = {
138 eArchTypeMachO,
139 sizeof(g_macho_arch_entries)/sizeof(g_macho_arch_entries[0]),
140 g_macho_arch_entries,
141 UINT32_MAX, // CPU type mask
142 0x00FFFFFFu, // CPU subtype mask
143 "mach-o"
Greg Clayton41f92322010-06-11 03:25:34 +0000144};
145
Greg Clayton64195a22011-02-23 00:35:02 +0000146//===----------------------------------------------------------------------===//
147// A table that gets searched linearly for matches. This table is used to
148// convert cpu type and subtypes to architecture names, and to convert
149// architecture names to cpu types and subtypes. The ordering is important and
150// allows the precedence to be set when the table is built.
151static const ArchDefinitionEntry g_elf_arch_entries[] =
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000152{
Greg Clayton64195a22011-02-23 00:35:02 +0000153 { ArchSpec::eCore_sparc_generic , llvm::ELF::EM_SPARC , LLDB_INVALID_CPUTYPE }, // Sparc
154 { ArchSpec::eCore_x86_32_i386 , llvm::ELF::EM_386 , LLDB_INVALID_CPUTYPE }, // Intel 80386
155 { ArchSpec::eCore_x86_32_i486 , llvm::ELF::EM_486 , LLDB_INVALID_CPUTYPE }, // Intel 486 (deprecated)
156 { ArchSpec::eCore_ppc_generic , llvm::ELF::EM_PPC , LLDB_INVALID_CPUTYPE }, // PowerPC
157 { ArchSpec::eCore_ppc64_generic , llvm::ELF::EM_PPC64 , LLDB_INVALID_CPUTYPE }, // PowerPC64
158 { ArchSpec::eCore_arm_generic , llvm::ELF::EM_ARM , LLDB_INVALID_CPUTYPE }, // ARM
159 { ArchSpec::eCore_alpha_generic , llvm::ELF::EM_ALPHA , LLDB_INVALID_CPUTYPE }, // DEC Alpha
160 { ArchSpec::eCore_sparc9_generic , llvm::ELF::EM_SPARCV9, LLDB_INVALID_CPUTYPE }, // SPARC V9
161 { ArchSpec::eCore_x86_64_x86_64 , llvm::ELF::EM_X86_64 , LLDB_INVALID_CPUTYPE }, // AMD64
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000162};
163
Greg Clayton64195a22011-02-23 00:35:02 +0000164static const ArchDefinition g_elf_arch_def = {
165 eArchTypeELF,
166 sizeof(g_elf_arch_entries)/sizeof(g_elf_arch_entries[0]),
167 g_elf_arch_entries,
168 UINT32_MAX, // CPU type mask
169 UINT32_MAX, // CPU subtype mask
170 "elf",
Greg Clayton41f92322010-06-11 03:25:34 +0000171};
172
Greg Clayton64195a22011-02-23 00:35:02 +0000173//===----------------------------------------------------------------------===//
174// Table of all ArchDefinitions
175static const ArchDefinition *g_arch_definitions[] = {
176 &g_macho_arch_def,
177 &g_elf_arch_def,
178};
Greg Clayton41f92322010-06-11 03:25:34 +0000179
Greg Clayton64195a22011-02-23 00:35:02 +0000180static const size_t k_num_arch_definitions =
181 sizeof(g_arch_definitions) / sizeof(g_arch_definitions[0]);
182
183//===----------------------------------------------------------------------===//
184// Static helper functions.
185
186
187// Get the architecture definition for a given object type.
188static const ArchDefinition *
189FindArchDefinition (ArchitectureType arch_type)
190{
191 for (unsigned int i = 0; i < k_num_arch_definitions; ++i)
192 {
193 const ArchDefinition *def = g_arch_definitions[i];
194 if (def->type == arch_type)
195 return def;
196 }
197 return NULL;
198}
199
200// Get an architecture definition by name.
201static const CoreDefinition *
202FindCoreDefinition (llvm::StringRef name)
203{
204 for (unsigned int i = 0; i < ArchSpec::kNumCores; ++i)
205 {
206 if (name.equals_lower(g_core_definitions[i].name))
207 return &g_core_definitions[i];
208 }
209 return NULL;
210}
211
212static inline const CoreDefinition *
213FindCoreDefinition (ArchSpec::Core core)
214{
215 if (core >= 0 && core < ArchSpec::kNumCores)
216 return &g_core_definitions[core];
217 return NULL;
218}
219
220// Get a definition entry by cpu type and subtype.
221static const ArchDefinitionEntry *
222FindArchDefinitionEntry (const ArchDefinition *def, uint32_t cpu, uint32_t sub)
223{
224 if (def == NULL)
225 return NULL;
226
227 const uint32_t cpu_mask = def->cpu_mask;
228 const uint32_t sub_mask = def->sub_mask;
229 const ArchDefinitionEntry *entries = def->entries;
230 for (size_t i = 0; i < def->num_entries; ++i)
231 {
232 if ((entries[i].cpu == (cpu_mask & cpu)) &&
233 (entries[i].sub == (sub_mask & sub)))
234 return &entries[i];
235 }
236 return NULL;
237}
238
239static const ArchDefinitionEntry *
240FindArchDefinitionEntry (const ArchDefinition *def, ArchSpec::Core core)
241{
242 if (def == NULL)
243 return NULL;
244
245 const ArchDefinitionEntry *entries = def->entries;
246 for (size_t i = 0; i < def->num_entries; ++i)
247 {
248 if (entries[i].core == core)
249 return &entries[i];
250 }
251 return NULL;
252}
253
254//===----------------------------------------------------------------------===//
255// Constructors and destructors.
256
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000257ArchSpec::ArchSpec() :
Greg Clayton514487e2011-02-15 21:59:32 +0000258 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000259 m_core (kCore_invalid),
260 m_byte_order (eByteOrderInvalid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000261{
262}
263
Greg Clayton64195a22011-02-23 00:35:02 +0000264ArchSpec::ArchSpec (const char *triple_cstr) :
Greg Clayton514487e2011-02-15 21:59:32 +0000265 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000266 m_core (kCore_invalid),
267 m_byte_order (eByteOrderInvalid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000268{
Greg Clayton64195a22011-02-23 00:35:02 +0000269 if (triple_cstr)
270 SetTriple(triple_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000271}
272
Greg Clayton64195a22011-02-23 00:35:02 +0000273ArchSpec::ArchSpec(const llvm::Triple &triple) :
Greg Clayton514487e2011-02-15 21:59:32 +0000274 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000275 m_core (kCore_invalid),
276 m_byte_order (eByteOrderInvalid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000277{
Greg Clayton64195a22011-02-23 00:35:02 +0000278 SetTriple(triple);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000279}
280
Greg Clayton64195a22011-02-23 00:35:02 +0000281ArchSpec::ArchSpec (lldb::ArchitectureType arch_type, uint32_t cpu, uint32_t subtype) :
282 m_triple (),
283 m_core (kCore_invalid),
284 m_byte_order (eByteOrderInvalid)
285{
286 SetArchitecture (arch_type, cpu, subtype);
287}
288
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000289ArchSpec::~ArchSpec()
290{
291}
292
Greg Clayton64195a22011-02-23 00:35:02 +0000293//===----------------------------------------------------------------------===//
294// Assignment and initialization.
295
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000296const ArchSpec&
297ArchSpec::operator= (const ArchSpec& rhs)
298{
299 if (this != &rhs)
300 {
Greg Clayton514487e2011-02-15 21:59:32 +0000301 m_triple = rhs.m_triple;
Greg Clayton64195a22011-02-23 00:35:02 +0000302 m_core = rhs.m_core;
Greg Clayton514487e2011-02-15 21:59:32 +0000303 m_byte_order = rhs.m_byte_order;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000304 }
305 return *this;
306}
307
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000308void
309ArchSpec::Clear()
310{
Greg Clayton514487e2011-02-15 21:59:32 +0000311 m_triple = llvm::Triple();
Greg Clayton64195a22011-02-23 00:35:02 +0000312 m_core = kCore_invalid;
313 m_byte_order = eByteOrderInvalid;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000314}
315
Greg Clayton64195a22011-02-23 00:35:02 +0000316//===----------------------------------------------------------------------===//
317// Predicates.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000318
Greg Clayton41f92322010-06-11 03:25:34 +0000319
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000320const char *
Greg Clayton64195a22011-02-23 00:35:02 +0000321ArchSpec::GetArchitectureName () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000322{
Greg Clayton64195a22011-02-23 00:35:02 +0000323 const CoreDefinition *core_def = FindCoreDefinition (m_core);
324 if (core_def)
325 return core_def->name;
326 return "unknown";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000327}
328
Greg Clayton64195a22011-02-23 00:35:02 +0000329uint32_t
330ArchSpec::GetMachOCPUType () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000331{
Greg Clayton64195a22011-02-23 00:35:02 +0000332 const CoreDefinition *core_def = FindCoreDefinition (m_core);
333 if (core_def)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000334 {
Greg Clayton64195a22011-02-23 00:35:02 +0000335 const ArchDefinitionEntry *arch_def = FindArchDefinitionEntry (&g_macho_arch_def, core_def->core);
336 if (arch_def)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000337 {
Greg Clayton64195a22011-02-23 00:35:02 +0000338 return arch_def->cpu;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000339 }
340 }
Greg Clayton64195a22011-02-23 00:35:02 +0000341 return LLDB_INVALID_CPUTYPE;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000342}
343
Greg Clayton64195a22011-02-23 00:35:02 +0000344uint32_t
345ArchSpec::GetMachOCPUSubType () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000346{
Greg Clayton64195a22011-02-23 00:35:02 +0000347 const CoreDefinition *core_def = FindCoreDefinition (m_core);
348 if (core_def)
349 {
350 const ArchDefinitionEntry *arch_def = FindArchDefinitionEntry (&g_macho_arch_def, core_def->core);
351 if (arch_def)
352 {
353 return arch_def->cpu;
354 }
355 }
356 return LLDB_INVALID_CPUTYPE;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000357}
358
Greg Clayton64195a22011-02-23 00:35:02 +0000359llvm::Triple::ArchType
360ArchSpec::GetMachine () const
361{
362 const CoreDefinition *core_def = FindCoreDefinition (m_core);
363 if (core_def)
364 return core_def->machine;
365
366 return llvm::Triple::UnknownArch;
367}
368
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000369uint32_t
370ArchSpec::GetAddressByteSize() const
371{
Greg Clayton64195a22011-02-23 00:35:02 +0000372 const CoreDefinition *core_def = FindCoreDefinition (m_core);
373 if (core_def)
374 return core_def->addr_byte_size;
Greg Clayton41f92322010-06-11 03:25:34 +0000375 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000376}
377
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000378ByteOrder
379ArchSpec::GetDefaultEndian () const
380{
Greg Clayton64195a22011-02-23 00:35:02 +0000381 const CoreDefinition *core_def = FindCoreDefinition (m_core);
382 if (core_def)
383 return core_def->default_byte_order;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000384 return eByteOrderInvalid;
385}
386
Greg Clayton64195a22011-02-23 00:35:02 +0000387lldb::ByteOrder
388ArchSpec::GetByteOrder () const
389{
390 if (m_byte_order == eByteOrderInvalid)
391 return GetDefaultEndian();
392 return m_byte_order;
393}
394
395//===----------------------------------------------------------------------===//
396// Mutators.
397
398bool
399ArchSpec::SetTriple (const llvm::Triple &triple)
400{
401 m_triple = triple;
402
403 llvm::StringRef arch_name (m_triple.getArchName());
404 const CoreDefinition *core_def = FindCoreDefinition (arch_name);
405 if (core_def)
406 {
407 m_core = core_def->core;
408 m_byte_order = core_def->default_byte_order;
409
410 // If the vendor, OS or environment aren't specified, default to the system?
411 const ArchSpec &host_arch_ref = Host::GetArchitecture (Host::eSystemDefaultArchitecture);
412 if (m_triple.getVendor() == llvm::Triple::UnknownVendor)
413 m_triple.setVendor(host_arch_ref.GetTriple().getVendor());
414 if (m_triple.getOS() == llvm::Triple::UnknownOS)
415 m_triple.setOS(host_arch_ref.GetTriple().getOS());
416 if (m_triple.getEnvironment() == llvm::Triple::UnknownEnvironment)
417 m_triple.setEnvironment(host_arch_ref.GetTriple().getEnvironment());
418 }
419 else
420 {
421 Clear();
422 }
423
424
425 return IsValid();
426}
427
428bool
429ArchSpec::SetTriple (const char *triple_cstr)
430{
431 if (triple_cstr || triple_cstr[0])
432 {
433 llvm::StringRef triple_stref (triple_cstr);
434 if (triple_stref.startswith (LLDB_ARCH_DEFAULT))
435 {
436 // Special case for the current host default architectures...
437 if (triple_stref.equals (LLDB_ARCH_DEFAULT_32BIT))
438 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture32);
439 else if (triple_stref.equals (LLDB_ARCH_DEFAULT_64BIT))
440 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture64);
441 else if (triple_stref.equals (LLDB_ARCH_DEFAULT))
442 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture);
443 }
444 else
445 {
446 std::string normalized_triple_sstr (llvm::Triple::normalize(triple_stref));
447 triple_stref = normalized_triple_sstr;
448 SetTriple (llvm::Triple (triple_stref));
449 }
450 }
451 else
452 Clear();
453 return IsValid();
454}
455
456//bool
457//ArchSpec::SetArchitecture (const char *arch_name)
458//{
459// return SetArchitecture(llvm::StringRef (arch_name));
460//}
461//
462//bool
463//ArchSpec::SetArchitecture (const llvm::StringRef& arch_name)
464//{
465// // All default architecture names start with LLDB_ARCH_DEFAULT.
466// if (arch_name.startswith (LLDB_ARCH_DEFAULT))
467// {
468// // Special case for the current host default architectures...
469// if (arch_name.equals (LLDB_ARCH_DEFAULT_32BIT))
470// *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture32);
471// else if (arch_name.equals (LLDB_ARCH_DEFAULT_64BIT))
472// *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture64);
473// else
474// *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture);
475// }
476// else
477// {
478// const CoreDefinition *core_def = FindCoreDefinition (arch_name);
479// if (core_def)
480// m_core = core_def->core;
481// CoreUpdated(true);
482// }
483// return IsValid();
484//}
485//
486bool
487ArchSpec::SetArchitecture (lldb::ArchitectureType arch_type, uint32_t cpu, uint32_t sub)
488{
489 m_core = kCore_invalid;
490 bool update_triple = true;
491 const ArchDefinition *arch_def = FindArchDefinition(arch_type);
492 if (arch_def)
493 {
494 const ArchDefinitionEntry *arch_def_entry = FindArchDefinitionEntry (arch_def, cpu, sub);
495 if (arch_def_entry)
496 {
497 const CoreDefinition *core_def = FindCoreDefinition (arch_def_entry->core);
498 if (core_def)
499 {
500 m_core = core_def->core;
501 update_triple = false;
502 m_triple.setArch (core_def->machine);
503 if (arch_type == eArchTypeMachO)
504 {
505 m_triple.setVendor (llvm::Triple::Apple);
506 m_triple.setOS (llvm::Triple::Darwin);
507 }
508 else
509 {
510 m_triple.setVendor (llvm::Triple::UnknownVendor);
511 m_triple.setOS (llvm::Triple::UnknownOS);
512 }
513 }
514 }
515 }
516 CoreUpdated(update_triple);
517 return IsValid();
518}
519
520void
521ArchSpec::SetByteOrder (lldb::ByteOrder byte_order)
522{
523 m_byte_order = byte_order;
524}
525
526//===----------------------------------------------------------------------===//
527// Helper methods.
528
529void
530ArchSpec::CoreUpdated (bool update_triple)
531{
532 const CoreDefinition *core_def = FindCoreDefinition (m_core);
533 if (core_def)
534 {
535 if (update_triple)
536 m_triple = llvm::Triple(core_def->name, "unknown", "unknown");
537 m_byte_order = core_def->default_byte_order;
538 }
539 else
540 {
541 if (update_triple)
542 m_triple = llvm::Triple();
543 m_byte_order = eByteOrderInvalid;
544 }
545}
546
547//===----------------------------------------------------------------------===//
548// Operators.
549
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000550bool
551lldb_private::operator== (const ArchSpec& lhs, const ArchSpec& rhs)
552{
Greg Clayton64195a22011-02-23 00:35:02 +0000553 const ArchSpec::Core lhs_core = lhs.GetCore ();
554 const ArchSpec::Core rhs_core = rhs.GetCore ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000555
Greg Clayton64195a22011-02-23 00:35:02 +0000556 if (lhs_core == rhs_core)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000557 return true;
558
Greg Clayton64195a22011-02-23 00:35:02 +0000559 if (lhs_core == ArchSpec::kCore_any || rhs_core == ArchSpec::kCore_any)
560 return true;
561
562 if (lhs_core == ArchSpec::kCore_arm_any)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000563 {
Greg Clayton64195a22011-02-23 00:35:02 +0000564 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 +0000565 return true;
Greg Clayton64195a22011-02-23 00:35:02 +0000566 }
567 else if (rhs_core == ArchSpec::kCore_arm_any)
568 {
569 if ((lhs_core >= ArchSpec::kCore_arm_first && lhs_core <= ArchSpec::kCore_arm_last) || (lhs_core == ArchSpec::kCore_arm_any))
570 return true;
571 }
572 else if (lhs_core == ArchSpec::kCore_x86_32_any)
573 {
574 if ((rhs_core >= ArchSpec::kCore_x86_32_first && rhs_core <= ArchSpec::kCore_x86_32_last) || (rhs_core == ArchSpec::kCore_x86_32_any))
575 return true;
576 }
577 else if (rhs_core == ArchSpec::kCore_x86_32_any)
578 {
579 if ((lhs_core >= ArchSpec::kCore_x86_32_first && lhs_core <= ArchSpec::kCore_x86_32_last) || (lhs_core == ArchSpec::kCore_x86_32_any))
580 return true;
581 }
582 else if (lhs_core == ArchSpec::kCore_ppc_any)
583 {
584 if ((rhs_core >= ArchSpec::kCore_ppc_first && rhs_core <= ArchSpec::kCore_ppc_last) || (rhs_core == ArchSpec::kCore_ppc_any))
585 return true;
586 }
587 else if (rhs_core == ArchSpec::kCore_ppc_any)
588 {
589 if ((lhs_core >= ArchSpec::kCore_ppc_first && lhs_core <= ArchSpec::kCore_ppc_last) || (lhs_core == ArchSpec::kCore_ppc_any))
590 return true;
591 }
592 else if (lhs_core == ArchSpec::kCore_ppc64_any)
593 {
594 if ((rhs_core >= ArchSpec::kCore_ppc64_first && rhs_core <= ArchSpec::kCore_ppc64_last) || (rhs_core == ArchSpec::kCore_ppc64_any))
595 return true;
596 }
597 else if (rhs_core == ArchSpec::kCore_ppc64_any)
598 {
599 if ((lhs_core >= ArchSpec::kCore_ppc64_first && lhs_core <= ArchSpec::kCore_ppc64_last) || (lhs_core == ArchSpec::kCore_ppc64_any))
600 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000601 }
602 return false;
603}
604
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000605bool
606lldb_private::operator!= (const ArchSpec& lhs, const ArchSpec& rhs)
607{
608 return !(lhs == rhs);
609}
610
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000611bool
612lldb_private::operator<(const ArchSpec& lhs, const ArchSpec& rhs)
613{
Greg Clayton64195a22011-02-23 00:35:02 +0000614 const ArchSpec::Core lhs_core = lhs.GetCore ();
615 const ArchSpec::Core rhs_core = rhs.GetCore ();
616 return lhs_core < rhs_core;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000617}