blob: 17134938d09ddbac5b2954fad51212e83f83e8f9 [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" },
Greg Claytonded470d2011-03-19 01:12:21 +000052 { eByteOrderLittle, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7f , "armv7f" },
53 { eByteOrderLittle, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7k , "armv7k" },
54 { eByteOrderLittle, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7s , "armv7s" },
Greg Clayton64195a22011-02-23 00:35:02 +000055 { eByteOrderLittle, 4, llvm::Triple::arm , ArchSpec::eCore_arm_xscale , "xscale" },
56
57 { eByteOrderLittle, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_generic , "ppc" },
58 { eByteOrderLittle, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc601 , "ppc601" },
59 { eByteOrderLittle, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc602 , "ppc602" },
60 { eByteOrderLittle, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc603 , "ppc603" },
61 { eByteOrderLittle, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc603e , "ppc603e" },
62 { eByteOrderLittle, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc603ev , "ppc603ev" },
63 { eByteOrderLittle, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc604 , "ppc604" },
64 { eByteOrderLittle, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc604e , "ppc604e" },
65 { eByteOrderLittle, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc620 , "ppc620" },
66 { eByteOrderLittle, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc750 , "ppc750" },
67 { eByteOrderLittle, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc7400 , "ppc7400" },
68 { eByteOrderLittle, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc7450 , "ppc7450" },
69 { eByteOrderLittle, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc970 , "ppc970" },
70
71 { eByteOrderLittle, 8, llvm::Triple::ppc64 , ArchSpec::eCore_ppc64_generic , "ppc64" },
72 { eByteOrderLittle, 8, llvm::Triple::ppc64 , ArchSpec::eCore_ppc64_ppc970_64 , "ppc970-64" },
73
74 { eByteOrderLittle, 4, llvm::Triple::sparc , ArchSpec::eCore_sparc_generic , "sparc" },
75 { eByteOrderLittle, 8, llvm::Triple::sparcv9, ArchSpec::eCore_sparc9_generic , "sparcv9" },
76
77 { eByteOrderLittle, 4, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i386 , "i386" },
78 { eByteOrderLittle, 4, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i486 , "i486" },
79 { eByteOrderLittle, 4, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i486sx , "i486sx" },
80
81 { eByteOrderLittle, 8, llvm::Triple::x86_64 , ArchSpec::eCore_x86_64_x86_64 , "x86_64" }
82};
83
84struct ArchDefinitionEntry
85{
86 ArchSpec::Core core;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000087 uint32_t cpu;
88 uint32_t sub;
Greg Clayton64195a22011-02-23 00:35:02 +000089};
90
91struct ArchDefinition
92{
93 ArchitectureType type;
94 size_t num_entries;
95 const ArchDefinitionEntry *entries;
96 uint32_t cpu_mask;
97 uint32_t sub_mask;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000098 const char *name;
99};
100
Greg Clayton41f92322010-06-11 03:25:34 +0000101
Greg Clayton64195a22011-02-23 00:35:02 +0000102#define CPU_ANY (UINT32_MAX)
103
104//===----------------------------------------------------------------------===//
105// A table that gets searched linearly for matches. This table is used to
106// convert cpu type and subtypes to architecture names, and to convert
107// architecture names to cpu types and subtypes. The ordering is important and
108// allows the precedence to be set when the table is built.
109static const ArchDefinitionEntry g_macho_arch_entries[] =
Greg Clayton41f92322010-06-11 03:25:34 +0000110{
Greg Clayton64195a22011-02-23 00:35:02 +0000111 { ArchSpec::eCore_arm_generic , llvm::MachO::CPUTypeARM , CPU_ANY },
112 { ArchSpec::eCore_arm_generic , llvm::MachO::CPUTypeARM , 0 },
113 { ArchSpec::eCore_arm_armv4 , llvm::MachO::CPUTypeARM , 5 },
114 { ArchSpec::eCore_arm_armv6 , llvm::MachO::CPUTypeARM , 6 },
115 { ArchSpec::eCore_arm_armv5 , llvm::MachO::CPUTypeARM , 7 },
116 { ArchSpec::eCore_arm_xscale , llvm::MachO::CPUTypeARM , 8 },
117 { ArchSpec::eCore_arm_armv7 , llvm::MachO::CPUTypeARM , 9 },
Greg Claytonded470d2011-03-19 01:12:21 +0000118 { ArchSpec::eCore_arm_armv7f , llvm::MachO::CPUTypeARM , 10 },
119 { ArchSpec::eCore_arm_armv7k , llvm::MachO::CPUTypeARM , 12 },
120 { ArchSpec::eCore_arm_armv7s , llvm::MachO::CPUTypeARM , 11 },
Greg Clayton64195a22011-02-23 00:35:02 +0000121 { ArchSpec::eCore_ppc_generic , llvm::MachO::CPUTypePowerPC , CPU_ANY },
122 { ArchSpec::eCore_ppc_generic , llvm::MachO::CPUTypePowerPC , 0 },
123 { ArchSpec::eCore_ppc_ppc601 , llvm::MachO::CPUTypePowerPC , 1 },
124 { ArchSpec::eCore_ppc_ppc602 , llvm::MachO::CPUTypePowerPC , 2 },
125 { ArchSpec::eCore_ppc_ppc603 , llvm::MachO::CPUTypePowerPC , 3 },
126 { ArchSpec::eCore_ppc_ppc603e , llvm::MachO::CPUTypePowerPC , 4 },
127 { ArchSpec::eCore_ppc_ppc603ev , llvm::MachO::CPUTypePowerPC , 5 },
128 { ArchSpec::eCore_ppc_ppc604 , llvm::MachO::CPUTypePowerPC , 6 },
129 { ArchSpec::eCore_ppc_ppc604e , llvm::MachO::CPUTypePowerPC , 7 },
130 { ArchSpec::eCore_ppc_ppc620 , llvm::MachO::CPUTypePowerPC , 8 },
131 { ArchSpec::eCore_ppc_ppc750 , llvm::MachO::CPUTypePowerPC , 9 },
132 { ArchSpec::eCore_ppc_ppc7400 , llvm::MachO::CPUTypePowerPC , 10 },
133 { ArchSpec::eCore_ppc_ppc7450 , llvm::MachO::CPUTypePowerPC , 11 },
134 { ArchSpec::eCore_ppc_ppc970 , llvm::MachO::CPUTypePowerPC , 100 },
135 { ArchSpec::eCore_ppc64_generic , llvm::MachO::CPUTypePowerPC64 , 0 },
136 { ArchSpec::eCore_ppc64_ppc970_64 , llvm::MachO::CPUTypePowerPC64 , 100 },
137 { ArchSpec::eCore_x86_32_i386 , llvm::MachO::CPUTypeI386 , 3 },
138 { ArchSpec::eCore_x86_32_i486 , llvm::MachO::CPUTypeI386 , 4 },
139 { ArchSpec::eCore_x86_32_i486sx , llvm::MachO::CPUTypeI386 , 0x84 },
140 { ArchSpec::eCore_x86_32_i386 , llvm::MachO::CPUTypeI386 , CPU_ANY },
141 { ArchSpec::eCore_x86_64_x86_64 , llvm::MachO::CPUTypeX86_64 , 3 },
142 { ArchSpec::eCore_x86_64_x86_64 , llvm::MachO::CPUTypeX86_64 , CPU_ANY }
143};
144static const ArchDefinition g_macho_arch_def = {
145 eArchTypeMachO,
146 sizeof(g_macho_arch_entries)/sizeof(g_macho_arch_entries[0]),
147 g_macho_arch_entries,
148 UINT32_MAX, // CPU type mask
149 0x00FFFFFFu, // CPU subtype mask
150 "mach-o"
Greg Clayton41f92322010-06-11 03:25:34 +0000151};
152
Greg Clayton64195a22011-02-23 00:35:02 +0000153//===----------------------------------------------------------------------===//
154// A table that gets searched linearly for matches. This table is used to
155// convert cpu type and subtypes to architecture names, and to convert
156// architecture names to cpu types and subtypes. The ordering is important and
157// allows the precedence to be set when the table is built.
158static const ArchDefinitionEntry g_elf_arch_entries[] =
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000159{
Greg Clayton64195a22011-02-23 00:35:02 +0000160 { ArchSpec::eCore_sparc_generic , llvm::ELF::EM_SPARC , LLDB_INVALID_CPUTYPE }, // Sparc
161 { ArchSpec::eCore_x86_32_i386 , llvm::ELF::EM_386 , LLDB_INVALID_CPUTYPE }, // Intel 80386
162 { ArchSpec::eCore_x86_32_i486 , llvm::ELF::EM_486 , LLDB_INVALID_CPUTYPE }, // Intel 486 (deprecated)
163 { ArchSpec::eCore_ppc_generic , llvm::ELF::EM_PPC , LLDB_INVALID_CPUTYPE }, // PowerPC
164 { ArchSpec::eCore_ppc64_generic , llvm::ELF::EM_PPC64 , LLDB_INVALID_CPUTYPE }, // PowerPC64
165 { ArchSpec::eCore_arm_generic , llvm::ELF::EM_ARM , LLDB_INVALID_CPUTYPE }, // ARM
166 { ArchSpec::eCore_alpha_generic , llvm::ELF::EM_ALPHA , LLDB_INVALID_CPUTYPE }, // DEC Alpha
167 { ArchSpec::eCore_sparc9_generic , llvm::ELF::EM_SPARCV9, LLDB_INVALID_CPUTYPE }, // SPARC V9
168 { ArchSpec::eCore_x86_64_x86_64 , llvm::ELF::EM_X86_64 , LLDB_INVALID_CPUTYPE }, // AMD64
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000169};
170
Greg Clayton64195a22011-02-23 00:35:02 +0000171static const ArchDefinition g_elf_arch_def = {
172 eArchTypeELF,
173 sizeof(g_elf_arch_entries)/sizeof(g_elf_arch_entries[0]),
174 g_elf_arch_entries,
175 UINT32_MAX, // CPU type mask
176 UINT32_MAX, // CPU subtype mask
177 "elf",
Greg Clayton41f92322010-06-11 03:25:34 +0000178};
179
Greg Clayton64195a22011-02-23 00:35:02 +0000180//===----------------------------------------------------------------------===//
181// Table of all ArchDefinitions
182static const ArchDefinition *g_arch_definitions[] = {
183 &g_macho_arch_def,
184 &g_elf_arch_def,
185};
Greg Clayton41f92322010-06-11 03:25:34 +0000186
Greg Clayton64195a22011-02-23 00:35:02 +0000187static const size_t k_num_arch_definitions =
188 sizeof(g_arch_definitions) / sizeof(g_arch_definitions[0]);
189
190//===----------------------------------------------------------------------===//
191// Static helper functions.
192
193
194// Get the architecture definition for a given object type.
195static const ArchDefinition *
196FindArchDefinition (ArchitectureType arch_type)
197{
198 for (unsigned int i = 0; i < k_num_arch_definitions; ++i)
199 {
200 const ArchDefinition *def = g_arch_definitions[i];
201 if (def->type == arch_type)
202 return def;
203 }
204 return NULL;
205}
206
207// Get an architecture definition by name.
208static const CoreDefinition *
209FindCoreDefinition (llvm::StringRef name)
210{
211 for (unsigned int i = 0; i < ArchSpec::kNumCores; ++i)
212 {
213 if (name.equals_lower(g_core_definitions[i].name))
214 return &g_core_definitions[i];
215 }
216 return NULL;
217}
218
219static inline const CoreDefinition *
220FindCoreDefinition (ArchSpec::Core core)
221{
222 if (core >= 0 && core < ArchSpec::kNumCores)
223 return &g_core_definitions[core];
224 return NULL;
225}
226
227// Get a definition entry by cpu type and subtype.
228static const ArchDefinitionEntry *
229FindArchDefinitionEntry (const ArchDefinition *def, uint32_t cpu, uint32_t sub)
230{
231 if (def == NULL)
232 return NULL;
233
234 const uint32_t cpu_mask = def->cpu_mask;
235 const uint32_t sub_mask = def->sub_mask;
236 const ArchDefinitionEntry *entries = def->entries;
237 for (size_t i = 0; i < def->num_entries; ++i)
238 {
239 if ((entries[i].cpu == (cpu_mask & cpu)) &&
240 (entries[i].sub == (sub_mask & sub)))
241 return &entries[i];
242 }
243 return NULL;
244}
245
246static const ArchDefinitionEntry *
247FindArchDefinitionEntry (const ArchDefinition *def, ArchSpec::Core core)
248{
249 if (def == NULL)
250 return NULL;
251
252 const ArchDefinitionEntry *entries = def->entries;
253 for (size_t i = 0; i < def->num_entries; ++i)
254 {
255 if (entries[i].core == core)
256 return &entries[i];
257 }
258 return NULL;
259}
260
261//===----------------------------------------------------------------------===//
262// Constructors and destructors.
263
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000264ArchSpec::ArchSpec() :
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{
269}
270
Greg Clayton64195a22011-02-23 00:35:02 +0000271ArchSpec::ArchSpec (const char *triple_cstr) :
Greg Clayton514487e2011-02-15 21:59:32 +0000272 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000273 m_core (kCore_invalid),
274 m_byte_order (eByteOrderInvalid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000275{
Greg Clayton64195a22011-02-23 00:35:02 +0000276 if (triple_cstr)
277 SetTriple(triple_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000278}
279
Greg Clayton64195a22011-02-23 00:35:02 +0000280ArchSpec::ArchSpec(const llvm::Triple &triple) :
Greg Clayton514487e2011-02-15 21:59:32 +0000281 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000282 m_core (kCore_invalid),
283 m_byte_order (eByteOrderInvalid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000284{
Greg Clayton64195a22011-02-23 00:35:02 +0000285 SetTriple(triple);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000286}
287
Greg Claytone0d378b2011-03-24 21:19:54 +0000288ArchSpec::ArchSpec (ArchitectureType arch_type, uint32_t cpu, uint32_t subtype) :
Greg Clayton64195a22011-02-23 00:35:02 +0000289 m_triple (),
290 m_core (kCore_invalid),
291 m_byte_order (eByteOrderInvalid)
292{
293 SetArchitecture (arch_type, cpu, subtype);
294}
295
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000296ArchSpec::~ArchSpec()
297{
298}
299
Greg Clayton64195a22011-02-23 00:35:02 +0000300//===----------------------------------------------------------------------===//
301// Assignment and initialization.
302
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000303const ArchSpec&
304ArchSpec::operator= (const ArchSpec& rhs)
305{
306 if (this != &rhs)
307 {
Greg Clayton514487e2011-02-15 21:59:32 +0000308 m_triple = rhs.m_triple;
Greg Clayton64195a22011-02-23 00:35:02 +0000309 m_core = rhs.m_core;
Greg Clayton514487e2011-02-15 21:59:32 +0000310 m_byte_order = rhs.m_byte_order;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000311 }
312 return *this;
313}
314
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000315void
316ArchSpec::Clear()
317{
Greg Clayton514487e2011-02-15 21:59:32 +0000318 m_triple = llvm::Triple();
Greg Clayton64195a22011-02-23 00:35:02 +0000319 m_core = kCore_invalid;
320 m_byte_order = eByteOrderInvalid;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000321}
322
Greg Clayton64195a22011-02-23 00:35:02 +0000323//===----------------------------------------------------------------------===//
324// Predicates.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000325
Greg Clayton41f92322010-06-11 03:25:34 +0000326
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000327const char *
Greg Clayton64195a22011-02-23 00:35:02 +0000328ArchSpec::GetArchitectureName () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000329{
Greg Clayton64195a22011-02-23 00:35:02 +0000330 const CoreDefinition *core_def = FindCoreDefinition (m_core);
331 if (core_def)
332 return core_def->name;
333 return "unknown";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000334}
335
Greg Clayton64195a22011-02-23 00:35:02 +0000336uint32_t
337ArchSpec::GetMachOCPUType () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000338{
Greg Clayton64195a22011-02-23 00:35:02 +0000339 const CoreDefinition *core_def = FindCoreDefinition (m_core);
340 if (core_def)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000341 {
Greg Clayton64195a22011-02-23 00:35:02 +0000342 const ArchDefinitionEntry *arch_def = FindArchDefinitionEntry (&g_macho_arch_def, core_def->core);
343 if (arch_def)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000344 {
Greg Clayton64195a22011-02-23 00:35:02 +0000345 return arch_def->cpu;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000346 }
347 }
Greg Clayton64195a22011-02-23 00:35:02 +0000348 return LLDB_INVALID_CPUTYPE;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000349}
350
Greg Clayton64195a22011-02-23 00:35:02 +0000351uint32_t
352ArchSpec::GetMachOCPUSubType () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000353{
Greg Clayton64195a22011-02-23 00:35:02 +0000354 const CoreDefinition *core_def = FindCoreDefinition (m_core);
355 if (core_def)
356 {
357 const ArchDefinitionEntry *arch_def = FindArchDefinitionEntry (&g_macho_arch_def, core_def->core);
358 if (arch_def)
359 {
Greg Clayton1cb64962011-03-24 04:28:38 +0000360 return arch_def->sub;
Greg Clayton64195a22011-02-23 00:35:02 +0000361 }
362 }
363 return LLDB_INVALID_CPUTYPE;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000364}
365
Greg Clayton64195a22011-02-23 00:35:02 +0000366llvm::Triple::ArchType
367ArchSpec::GetMachine () const
368{
369 const CoreDefinition *core_def = FindCoreDefinition (m_core);
370 if (core_def)
371 return core_def->machine;
372
373 return llvm::Triple::UnknownArch;
374}
375
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000376uint32_t
377ArchSpec::GetAddressByteSize() const
378{
Greg Clayton64195a22011-02-23 00:35:02 +0000379 const CoreDefinition *core_def = FindCoreDefinition (m_core);
380 if (core_def)
381 return core_def->addr_byte_size;
Greg Clayton41f92322010-06-11 03:25:34 +0000382 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000383}
384
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000385ByteOrder
386ArchSpec::GetDefaultEndian () const
387{
Greg Clayton64195a22011-02-23 00:35:02 +0000388 const CoreDefinition *core_def = FindCoreDefinition (m_core);
389 if (core_def)
390 return core_def->default_byte_order;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000391 return eByteOrderInvalid;
392}
393
Greg Clayton64195a22011-02-23 00:35:02 +0000394lldb::ByteOrder
395ArchSpec::GetByteOrder () const
396{
397 if (m_byte_order == eByteOrderInvalid)
398 return GetDefaultEndian();
399 return m_byte_order;
400}
401
402//===----------------------------------------------------------------------===//
403// Mutators.
404
405bool
406ArchSpec::SetTriple (const llvm::Triple &triple)
407{
408 m_triple = triple;
409
410 llvm::StringRef arch_name (m_triple.getArchName());
411 const CoreDefinition *core_def = FindCoreDefinition (arch_name);
412 if (core_def)
413 {
414 m_core = core_def->core;
415 m_byte_order = core_def->default_byte_order;
416
Stephen Wilsonfacebfc2011-02-24 19:13:58 +0000417 if (m_triple.getVendor() == llvm::Triple::UnknownVendor &&
418 m_triple.getOS() == llvm::Triple::UnknownOS &&
419 m_triple.getEnvironment() == llvm::Triple::UnknownEnvironment)
420 {
421 llvm::Triple host_triple(llvm::sys::getHostTriple());
422
423 m_triple.setVendor(host_triple.getVendor());
424 m_triple.setOS(host_triple.getOS());
425 m_triple.setEnvironment(host_triple.getEnvironment());
426 }
Greg Clayton64195a22011-02-23 00:35:02 +0000427 }
428 else
429 {
430 Clear();
431 }
432
433
434 return IsValid();
435}
436
437bool
438ArchSpec::SetTriple (const char *triple_cstr)
439{
440 if (triple_cstr || triple_cstr[0])
441 {
442 llvm::StringRef triple_stref (triple_cstr);
443 if (triple_stref.startswith (LLDB_ARCH_DEFAULT))
444 {
445 // Special case for the current host default architectures...
446 if (triple_stref.equals (LLDB_ARCH_DEFAULT_32BIT))
447 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture32);
448 else if (triple_stref.equals (LLDB_ARCH_DEFAULT_64BIT))
449 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture64);
450 else if (triple_stref.equals (LLDB_ARCH_DEFAULT))
451 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture);
452 }
453 else
454 {
455 std::string normalized_triple_sstr (llvm::Triple::normalize(triple_stref));
456 triple_stref = normalized_triple_sstr;
457 SetTriple (llvm::Triple (triple_stref));
458 }
459 }
460 else
461 Clear();
462 return IsValid();
463}
464
Greg Clayton64195a22011-02-23 00:35:02 +0000465bool
Greg Claytone0d378b2011-03-24 21:19:54 +0000466ArchSpec::SetArchitecture (ArchitectureType arch_type, uint32_t cpu, uint32_t sub)
Greg Clayton64195a22011-02-23 00:35:02 +0000467{
468 m_core = kCore_invalid;
469 bool update_triple = true;
470 const ArchDefinition *arch_def = FindArchDefinition(arch_type);
471 if (arch_def)
472 {
473 const ArchDefinitionEntry *arch_def_entry = FindArchDefinitionEntry (arch_def, cpu, sub);
474 if (arch_def_entry)
475 {
476 const CoreDefinition *core_def = FindCoreDefinition (arch_def_entry->core);
477 if (core_def)
478 {
479 m_core = core_def->core;
480 update_triple = false;
481 m_triple.setArch (core_def->machine);
482 if (arch_type == eArchTypeMachO)
483 {
484 m_triple.setVendor (llvm::Triple::Apple);
485 m_triple.setOS (llvm::Triple::Darwin);
486 }
487 else
488 {
489 m_triple.setVendor (llvm::Triple::UnknownVendor);
490 m_triple.setOS (llvm::Triple::UnknownOS);
491 }
492 }
493 }
494 }
495 CoreUpdated(update_triple);
496 return IsValid();
497}
498
499void
500ArchSpec::SetByteOrder (lldb::ByteOrder byte_order)
501{
502 m_byte_order = byte_order;
503}
504
505//===----------------------------------------------------------------------===//
506// Helper methods.
507
508void
509ArchSpec::CoreUpdated (bool update_triple)
510{
511 const CoreDefinition *core_def = FindCoreDefinition (m_core);
512 if (core_def)
513 {
514 if (update_triple)
515 m_triple = llvm::Triple(core_def->name, "unknown", "unknown");
516 m_byte_order = core_def->default_byte_order;
517 }
518 else
519 {
520 if (update_triple)
521 m_triple = llvm::Triple();
522 m_byte_order = eByteOrderInvalid;
523 }
524}
525
526//===----------------------------------------------------------------------===//
527// Operators.
528
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000529bool
530lldb_private::operator== (const ArchSpec& lhs, const ArchSpec& rhs)
531{
Greg Clayton64195a22011-02-23 00:35:02 +0000532 const ArchSpec::Core lhs_core = lhs.GetCore ();
533 const ArchSpec::Core rhs_core = rhs.GetCore ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000534
Greg Clayton64195a22011-02-23 00:35:02 +0000535 if (lhs_core == rhs_core)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000536 return true;
537
Greg Clayton64195a22011-02-23 00:35:02 +0000538 if (lhs_core == ArchSpec::kCore_any || rhs_core == ArchSpec::kCore_any)
539 return true;
540
541 if (lhs_core == ArchSpec::kCore_arm_any)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000542 {
Greg Clayton64195a22011-02-23 00:35:02 +0000543 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 +0000544 return true;
Greg Clayton64195a22011-02-23 00:35:02 +0000545 }
546 else if (rhs_core == ArchSpec::kCore_arm_any)
547 {
548 if ((lhs_core >= ArchSpec::kCore_arm_first && lhs_core <= ArchSpec::kCore_arm_last) || (lhs_core == ArchSpec::kCore_arm_any))
549 return true;
550 }
551 else if (lhs_core == ArchSpec::kCore_x86_32_any)
552 {
553 if ((rhs_core >= ArchSpec::kCore_x86_32_first && rhs_core <= ArchSpec::kCore_x86_32_last) || (rhs_core == ArchSpec::kCore_x86_32_any))
554 return true;
555 }
556 else if (rhs_core == ArchSpec::kCore_x86_32_any)
557 {
558 if ((lhs_core >= ArchSpec::kCore_x86_32_first && lhs_core <= ArchSpec::kCore_x86_32_last) || (lhs_core == ArchSpec::kCore_x86_32_any))
559 return true;
560 }
561 else if (lhs_core == ArchSpec::kCore_ppc_any)
562 {
563 if ((rhs_core >= ArchSpec::kCore_ppc_first && rhs_core <= ArchSpec::kCore_ppc_last) || (rhs_core == ArchSpec::kCore_ppc_any))
564 return true;
565 }
566 else if (rhs_core == ArchSpec::kCore_ppc_any)
567 {
568 if ((lhs_core >= ArchSpec::kCore_ppc_first && lhs_core <= ArchSpec::kCore_ppc_last) || (lhs_core == ArchSpec::kCore_ppc_any))
569 return true;
570 }
571 else if (lhs_core == ArchSpec::kCore_ppc64_any)
572 {
573 if ((rhs_core >= ArchSpec::kCore_ppc64_first && rhs_core <= ArchSpec::kCore_ppc64_last) || (rhs_core == ArchSpec::kCore_ppc64_any))
574 return true;
575 }
576 else if (rhs_core == ArchSpec::kCore_ppc64_any)
577 {
578 if ((lhs_core >= ArchSpec::kCore_ppc64_first && lhs_core <= ArchSpec::kCore_ppc64_last) || (lhs_core == ArchSpec::kCore_ppc64_any))
579 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000580 }
581 return false;
582}
583
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000584bool
585lldb_private::operator!= (const ArchSpec& lhs, const ArchSpec& rhs)
586{
587 return !(lhs == rhs);
588}
589
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000590bool
591lldb_private::operator<(const ArchSpec& lhs, const ArchSpec& rhs)
592{
Greg Clayton64195a22011-02-23 00:35:02 +0000593 const ArchSpec::Core lhs_core = lhs.GetCore ();
594 const ArchSpec::Core rhs_core = rhs.GetCore ();
595 return lhs_core < rhs_core;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000596}