blob: 45567328e53f86b5be88e5406590076a140ebbd8 [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 Claytoneb0103f2011-04-07 22:46:35 +000021#include "lldb/Target/Platform.h"
Greg Clayton41f92322010-06-11 03:25:34 +000022
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023using namespace lldb;
24using namespace lldb_private;
25
Greg Clayton64195a22011-02-23 00:35:02 +000026#define ARCH_SPEC_SEPARATOR_CHAR '-'
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027
Greg Clayton64195a22011-02-23 00:35:02 +000028namespace lldb_private {
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029
Greg Clayton64195a22011-02-23 00:35:02 +000030 struct CoreDefinition
31 {
32 ByteOrder default_byte_order;
33 uint32_t addr_byte_size;
Greg Clayton357132e2011-03-26 19:14:58 +000034 uint32_t min_opcode_byte_size;
35 uint32_t max_opcode_byte_size;
Greg Clayton64195a22011-02-23 00:35:02 +000036 llvm::Triple::ArchType machine;
37 ArchSpec::Core core;
38 const char *name;
39 };
40
41}
42
43// This core information can be looked using the ArchSpec::Core as the index
44static const CoreDefinition g_core_definitions[ArchSpec::kNumCores] =
Chris Lattner30fdc8d2010-06-08 16:52:24 +000045{
Greg Clayton357132e2011-03-26 19:14:58 +000046 // TODO: verify alpha has 32 bit fixed instructions
47 { eByteOrderLittle, 4, 4, 4, llvm::Triple::alpha , ArchSpec::eCore_alpha_generic , "alpha" },
Greg Clayton64195a22011-02-23 00:35:02 +000048
Greg Clayton357132e2011-03-26 19:14:58 +000049 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_generic , "arm" },
50 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv4 , "armv4" },
51 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv4t , "armv4t" },
52 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv5 , "armv5" },
53 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv5t , "armv5t" },
54 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv6 , "armv6" },
55 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7 , "armv7" },
56 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7f , "armv7f" },
57 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7k , "armv7k" },
58 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7s , "armv7s" },
59 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_xscale , "xscale" },
60 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumb_generic , "thumb" },
Greg Clayton64195a22011-02-23 00:35:02 +000061
Greg Clayton357132e2011-03-26 19:14:58 +000062 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_generic , "ppc" },
63 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc601 , "ppc601" },
64 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc602 , "ppc602" },
65 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc603 , "ppc603" },
66 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc603e , "ppc603e" },
67 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc603ev , "ppc603ev" },
68 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc604 , "ppc604" },
69 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc604e , "ppc604e" },
70 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc620 , "ppc620" },
71 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc750 , "ppc750" },
72 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc7400 , "ppc7400" },
73 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc7450 , "ppc7450" },
74 { eByteOrderLittle, 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc970 , "ppc970" },
Greg Clayton64195a22011-02-23 00:35:02 +000075
Greg Clayton357132e2011-03-26 19:14:58 +000076 { eByteOrderLittle, 8, 4, 4, llvm::Triple::ppc64 , ArchSpec::eCore_ppc64_generic , "ppc64" },
77 { eByteOrderLittle, 8, 4, 4, llvm::Triple::ppc64 , ArchSpec::eCore_ppc64_ppc970_64 , "ppc970-64" },
Greg Clayton64195a22011-02-23 00:35:02 +000078
Greg Clayton357132e2011-03-26 19:14:58 +000079 { eByteOrderLittle, 4, 4, 4, llvm::Triple::sparc , ArchSpec::eCore_sparc_generic , "sparc" },
80 { eByteOrderLittle, 8, 4, 4, llvm::Triple::sparcv9, ArchSpec::eCore_sparc9_generic , "sparcv9" },
Greg Clayton64195a22011-02-23 00:35:02 +000081
Greg Claytonab65b342011-04-13 22:47:15 +000082 { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i386 , "i386" },
83 { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i486 , "i486" },
84 { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i486sx , "i486sx" },
Greg Clayton64195a22011-02-23 00:35:02 +000085
Greg Claytonab65b342011-04-13 22:47:15 +000086 { eByteOrderLittle, 8, 1, 15, llvm::Triple::x86_64 , ArchSpec::eCore_x86_64_x86_64 , "x86_64" }
Greg Clayton64195a22011-02-23 00:35:02 +000087};
88
89struct ArchDefinitionEntry
90{
91 ArchSpec::Core core;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000092 uint32_t cpu;
93 uint32_t sub;
Greg Clayton64195a22011-02-23 00:35:02 +000094};
95
96struct ArchDefinition
97{
98 ArchitectureType type;
99 size_t num_entries;
100 const ArchDefinitionEntry *entries;
101 uint32_t cpu_mask;
102 uint32_t sub_mask;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000103 const char *name;
104};
105
Greg Clayton41f92322010-06-11 03:25:34 +0000106
Greg Claytonab65b342011-04-13 22:47:15 +0000107uint32_t
108ArchSpec::AutoComplete (const char *name, StringList &matches)
109{
110 uint32_t i;
111 if (name && name[0])
112 {
113 for (i = 0; i < ArchSpec::kNumCores; ++i)
114 {
115 if (NameMatches(g_core_definitions[i].name, eNameMatchStartsWith, name))
116 matches.AppendString (g_core_definitions[i].name);
117 }
118 }
119 else
120 {
121 for (i = 0; i < ArchSpec::kNumCores; ++i)
122 matches.AppendString (g_core_definitions[i].name);
123 }
124 return matches.GetSize();
125}
126
127
128
Greg Clayton64195a22011-02-23 00:35:02 +0000129#define CPU_ANY (UINT32_MAX)
130
131//===----------------------------------------------------------------------===//
132// A table that gets searched linearly for matches. This table is used to
133// convert cpu type and subtypes to architecture names, and to convert
134// architecture names to cpu types and subtypes. The ordering is important and
135// allows the precedence to be set when the table is built.
136static const ArchDefinitionEntry g_macho_arch_entries[] =
Greg Clayton41f92322010-06-11 03:25:34 +0000137{
Greg Clayton64195a22011-02-23 00:35:02 +0000138 { ArchSpec::eCore_arm_generic , llvm::MachO::CPUTypeARM , CPU_ANY },
139 { ArchSpec::eCore_arm_generic , llvm::MachO::CPUTypeARM , 0 },
140 { ArchSpec::eCore_arm_armv4 , llvm::MachO::CPUTypeARM , 5 },
141 { ArchSpec::eCore_arm_armv6 , llvm::MachO::CPUTypeARM , 6 },
142 { ArchSpec::eCore_arm_armv5 , llvm::MachO::CPUTypeARM , 7 },
143 { ArchSpec::eCore_arm_xscale , llvm::MachO::CPUTypeARM , 8 },
144 { ArchSpec::eCore_arm_armv7 , llvm::MachO::CPUTypeARM , 9 },
Greg Claytonded470d2011-03-19 01:12:21 +0000145 { ArchSpec::eCore_arm_armv7f , llvm::MachO::CPUTypeARM , 10 },
146 { ArchSpec::eCore_arm_armv7k , llvm::MachO::CPUTypeARM , 12 },
147 { ArchSpec::eCore_arm_armv7s , llvm::MachO::CPUTypeARM , 11 },
Greg Clayton357132e2011-03-26 19:14:58 +0000148 { ArchSpec::eCore_thumb_generic , llvm::MachO::CPUTypeARM , 0 },
Greg Clayton64195a22011-02-23 00:35:02 +0000149 { ArchSpec::eCore_ppc_generic , llvm::MachO::CPUTypePowerPC , CPU_ANY },
150 { ArchSpec::eCore_ppc_generic , llvm::MachO::CPUTypePowerPC , 0 },
151 { ArchSpec::eCore_ppc_ppc601 , llvm::MachO::CPUTypePowerPC , 1 },
152 { ArchSpec::eCore_ppc_ppc602 , llvm::MachO::CPUTypePowerPC , 2 },
153 { ArchSpec::eCore_ppc_ppc603 , llvm::MachO::CPUTypePowerPC , 3 },
154 { ArchSpec::eCore_ppc_ppc603e , llvm::MachO::CPUTypePowerPC , 4 },
155 { ArchSpec::eCore_ppc_ppc603ev , llvm::MachO::CPUTypePowerPC , 5 },
156 { ArchSpec::eCore_ppc_ppc604 , llvm::MachO::CPUTypePowerPC , 6 },
157 { ArchSpec::eCore_ppc_ppc604e , llvm::MachO::CPUTypePowerPC , 7 },
158 { ArchSpec::eCore_ppc_ppc620 , llvm::MachO::CPUTypePowerPC , 8 },
159 { ArchSpec::eCore_ppc_ppc750 , llvm::MachO::CPUTypePowerPC , 9 },
160 { ArchSpec::eCore_ppc_ppc7400 , llvm::MachO::CPUTypePowerPC , 10 },
161 { ArchSpec::eCore_ppc_ppc7450 , llvm::MachO::CPUTypePowerPC , 11 },
162 { ArchSpec::eCore_ppc_ppc970 , llvm::MachO::CPUTypePowerPC , 100 },
163 { ArchSpec::eCore_ppc64_generic , llvm::MachO::CPUTypePowerPC64 , 0 },
164 { ArchSpec::eCore_ppc64_ppc970_64 , llvm::MachO::CPUTypePowerPC64 , 100 },
165 { ArchSpec::eCore_x86_32_i386 , llvm::MachO::CPUTypeI386 , 3 },
166 { ArchSpec::eCore_x86_32_i486 , llvm::MachO::CPUTypeI386 , 4 },
167 { ArchSpec::eCore_x86_32_i486sx , llvm::MachO::CPUTypeI386 , 0x84 },
168 { ArchSpec::eCore_x86_32_i386 , llvm::MachO::CPUTypeI386 , CPU_ANY },
169 { ArchSpec::eCore_x86_64_x86_64 , llvm::MachO::CPUTypeX86_64 , 3 },
170 { ArchSpec::eCore_x86_64_x86_64 , llvm::MachO::CPUTypeX86_64 , CPU_ANY }
171};
172static const ArchDefinition g_macho_arch_def = {
173 eArchTypeMachO,
174 sizeof(g_macho_arch_entries)/sizeof(g_macho_arch_entries[0]),
175 g_macho_arch_entries,
176 UINT32_MAX, // CPU type mask
177 0x00FFFFFFu, // CPU subtype mask
178 "mach-o"
Greg Clayton41f92322010-06-11 03:25:34 +0000179};
180
Greg Clayton64195a22011-02-23 00:35:02 +0000181//===----------------------------------------------------------------------===//
182// A table that gets searched linearly for matches. This table is used to
183// convert cpu type and subtypes to architecture names, and to convert
184// architecture names to cpu types and subtypes. The ordering is important and
185// allows the precedence to be set when the table is built.
186static const ArchDefinitionEntry g_elf_arch_entries[] =
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000187{
Greg Clayton64195a22011-02-23 00:35:02 +0000188 { ArchSpec::eCore_sparc_generic , llvm::ELF::EM_SPARC , LLDB_INVALID_CPUTYPE }, // Sparc
189 { ArchSpec::eCore_x86_32_i386 , llvm::ELF::EM_386 , LLDB_INVALID_CPUTYPE }, // Intel 80386
190 { ArchSpec::eCore_x86_32_i486 , llvm::ELF::EM_486 , LLDB_INVALID_CPUTYPE }, // Intel 486 (deprecated)
191 { ArchSpec::eCore_ppc_generic , llvm::ELF::EM_PPC , LLDB_INVALID_CPUTYPE }, // PowerPC
192 { ArchSpec::eCore_ppc64_generic , llvm::ELF::EM_PPC64 , LLDB_INVALID_CPUTYPE }, // PowerPC64
193 { ArchSpec::eCore_arm_generic , llvm::ELF::EM_ARM , LLDB_INVALID_CPUTYPE }, // ARM
194 { ArchSpec::eCore_alpha_generic , llvm::ELF::EM_ALPHA , LLDB_INVALID_CPUTYPE }, // DEC Alpha
195 { ArchSpec::eCore_sparc9_generic , llvm::ELF::EM_SPARCV9, LLDB_INVALID_CPUTYPE }, // SPARC V9
196 { ArchSpec::eCore_x86_64_x86_64 , llvm::ELF::EM_X86_64 , LLDB_INVALID_CPUTYPE }, // AMD64
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000197};
198
Greg Clayton64195a22011-02-23 00:35:02 +0000199static const ArchDefinition g_elf_arch_def = {
200 eArchTypeELF,
201 sizeof(g_elf_arch_entries)/sizeof(g_elf_arch_entries[0]),
202 g_elf_arch_entries,
203 UINT32_MAX, // CPU type mask
204 UINT32_MAX, // CPU subtype mask
205 "elf",
Greg Clayton41f92322010-06-11 03:25:34 +0000206};
207
Greg Clayton64195a22011-02-23 00:35:02 +0000208//===----------------------------------------------------------------------===//
209// Table of all ArchDefinitions
210static const ArchDefinition *g_arch_definitions[] = {
211 &g_macho_arch_def,
212 &g_elf_arch_def,
213};
Greg Clayton41f92322010-06-11 03:25:34 +0000214
Greg Clayton64195a22011-02-23 00:35:02 +0000215static const size_t k_num_arch_definitions =
216 sizeof(g_arch_definitions) / sizeof(g_arch_definitions[0]);
217
218//===----------------------------------------------------------------------===//
219// Static helper functions.
220
221
222// Get the architecture definition for a given object type.
223static const ArchDefinition *
224FindArchDefinition (ArchitectureType arch_type)
225{
226 for (unsigned int i = 0; i < k_num_arch_definitions; ++i)
227 {
228 const ArchDefinition *def = g_arch_definitions[i];
229 if (def->type == arch_type)
230 return def;
231 }
232 return NULL;
233}
234
235// Get an architecture definition by name.
236static const CoreDefinition *
237FindCoreDefinition (llvm::StringRef name)
238{
239 for (unsigned int i = 0; i < ArchSpec::kNumCores; ++i)
240 {
241 if (name.equals_lower(g_core_definitions[i].name))
242 return &g_core_definitions[i];
243 }
244 return NULL;
245}
246
247static inline const CoreDefinition *
248FindCoreDefinition (ArchSpec::Core core)
249{
250 if (core >= 0 && core < ArchSpec::kNumCores)
251 return &g_core_definitions[core];
252 return NULL;
253}
254
255// Get a definition entry by cpu type and subtype.
256static const ArchDefinitionEntry *
257FindArchDefinitionEntry (const ArchDefinition *def, uint32_t cpu, uint32_t sub)
258{
259 if (def == NULL)
260 return NULL;
261
262 const uint32_t cpu_mask = def->cpu_mask;
263 const uint32_t sub_mask = def->sub_mask;
264 const ArchDefinitionEntry *entries = def->entries;
265 for (size_t i = 0; i < def->num_entries; ++i)
266 {
267 if ((entries[i].cpu == (cpu_mask & cpu)) &&
268 (entries[i].sub == (sub_mask & sub)))
269 return &entries[i];
270 }
271 return NULL;
272}
273
274static const ArchDefinitionEntry *
275FindArchDefinitionEntry (const ArchDefinition *def, ArchSpec::Core core)
276{
277 if (def == NULL)
278 return NULL;
279
280 const ArchDefinitionEntry *entries = def->entries;
281 for (size_t i = 0; i < def->num_entries; ++i)
282 {
283 if (entries[i].core == core)
284 return &entries[i];
285 }
286 return NULL;
287}
288
289//===----------------------------------------------------------------------===//
290// Constructors and destructors.
291
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000292ArchSpec::ArchSpec() :
Greg Clayton514487e2011-02-15 21:59:32 +0000293 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000294 m_core (kCore_invalid),
295 m_byte_order (eByteOrderInvalid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000296{
297}
298
Greg Claytoneb0103f2011-04-07 22:46:35 +0000299ArchSpec::ArchSpec (const char *triple_cstr, Platform *platform) :
Greg Clayton514487e2011-02-15 21:59:32 +0000300 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000301 m_core (kCore_invalid),
302 m_byte_order (eByteOrderInvalid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000303{
Greg Clayton64195a22011-02-23 00:35:02 +0000304 if (triple_cstr)
Greg Claytoneb0103f2011-04-07 22:46:35 +0000305 SetTriple(triple_cstr, platform);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000306}
307
Greg Clayton64195a22011-02-23 00:35:02 +0000308ArchSpec::ArchSpec(const llvm::Triple &triple) :
Greg Clayton514487e2011-02-15 21:59:32 +0000309 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000310 m_core (kCore_invalid),
311 m_byte_order (eByteOrderInvalid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000312{
Greg Clayton64195a22011-02-23 00:35:02 +0000313 SetTriple(triple);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000314}
315
Greg Claytone0d378b2011-03-24 21:19:54 +0000316ArchSpec::ArchSpec (ArchitectureType arch_type, uint32_t cpu, uint32_t subtype) :
Greg Clayton64195a22011-02-23 00:35:02 +0000317 m_triple (),
318 m_core (kCore_invalid),
319 m_byte_order (eByteOrderInvalid)
320{
321 SetArchitecture (arch_type, cpu, subtype);
322}
323
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000324ArchSpec::~ArchSpec()
325{
326}
327
Greg Clayton64195a22011-02-23 00:35:02 +0000328//===----------------------------------------------------------------------===//
329// Assignment and initialization.
330
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000331const ArchSpec&
332ArchSpec::operator= (const ArchSpec& rhs)
333{
334 if (this != &rhs)
335 {
Greg Clayton514487e2011-02-15 21:59:32 +0000336 m_triple = rhs.m_triple;
Greg Clayton64195a22011-02-23 00:35:02 +0000337 m_core = rhs.m_core;
Greg Clayton514487e2011-02-15 21:59:32 +0000338 m_byte_order = rhs.m_byte_order;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000339 }
340 return *this;
341}
342
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000343void
344ArchSpec::Clear()
345{
Greg Clayton514487e2011-02-15 21:59:32 +0000346 m_triple = llvm::Triple();
Greg Clayton64195a22011-02-23 00:35:02 +0000347 m_core = kCore_invalid;
348 m_byte_order = eByteOrderInvalid;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000349}
350
Greg Clayton64195a22011-02-23 00:35:02 +0000351//===----------------------------------------------------------------------===//
352// Predicates.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000353
Greg Clayton41f92322010-06-11 03:25:34 +0000354
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000355const char *
Greg Clayton64195a22011-02-23 00:35:02 +0000356ArchSpec::GetArchitectureName () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000357{
Greg Clayton64195a22011-02-23 00:35:02 +0000358 const CoreDefinition *core_def = FindCoreDefinition (m_core);
359 if (core_def)
360 return core_def->name;
361 return "unknown";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000362}
363
Greg Clayton64195a22011-02-23 00:35:02 +0000364uint32_t
365ArchSpec::GetMachOCPUType () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000366{
Greg Clayton64195a22011-02-23 00:35:02 +0000367 const CoreDefinition *core_def = FindCoreDefinition (m_core);
368 if (core_def)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000369 {
Greg Clayton64195a22011-02-23 00:35:02 +0000370 const ArchDefinitionEntry *arch_def = FindArchDefinitionEntry (&g_macho_arch_def, core_def->core);
371 if (arch_def)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000372 {
Greg Clayton64195a22011-02-23 00:35:02 +0000373 return arch_def->cpu;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000374 }
375 }
Greg Clayton64195a22011-02-23 00:35:02 +0000376 return LLDB_INVALID_CPUTYPE;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000377}
378
Greg Clayton64195a22011-02-23 00:35:02 +0000379uint32_t
380ArchSpec::GetMachOCPUSubType () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000381{
Greg Clayton64195a22011-02-23 00:35:02 +0000382 const CoreDefinition *core_def = FindCoreDefinition (m_core);
383 if (core_def)
384 {
385 const ArchDefinitionEntry *arch_def = FindArchDefinitionEntry (&g_macho_arch_def, core_def->core);
386 if (arch_def)
387 {
Greg Clayton1cb64962011-03-24 04:28:38 +0000388 return arch_def->sub;
Greg Clayton64195a22011-02-23 00:35:02 +0000389 }
390 }
391 return LLDB_INVALID_CPUTYPE;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000392}
393
Greg Clayton64195a22011-02-23 00:35:02 +0000394llvm::Triple::ArchType
395ArchSpec::GetMachine () const
396{
397 const CoreDefinition *core_def = FindCoreDefinition (m_core);
398 if (core_def)
399 return core_def->machine;
400
401 return llvm::Triple::UnknownArch;
402}
403
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000404uint32_t
405ArchSpec::GetAddressByteSize() const
406{
Greg Clayton64195a22011-02-23 00:35:02 +0000407 const CoreDefinition *core_def = FindCoreDefinition (m_core);
408 if (core_def)
409 return core_def->addr_byte_size;
Greg Clayton41f92322010-06-11 03:25:34 +0000410 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000411}
412
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000413ByteOrder
414ArchSpec::GetDefaultEndian () const
415{
Greg Clayton64195a22011-02-23 00:35:02 +0000416 const CoreDefinition *core_def = FindCoreDefinition (m_core);
417 if (core_def)
418 return core_def->default_byte_order;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000419 return eByteOrderInvalid;
420}
421
Greg Clayton64195a22011-02-23 00:35:02 +0000422lldb::ByteOrder
423ArchSpec::GetByteOrder () const
424{
425 if (m_byte_order == eByteOrderInvalid)
426 return GetDefaultEndian();
427 return m_byte_order;
428}
429
430//===----------------------------------------------------------------------===//
431// Mutators.
432
433bool
434ArchSpec::SetTriple (const llvm::Triple &triple)
435{
436 m_triple = triple;
437
438 llvm::StringRef arch_name (m_triple.getArchName());
439 const CoreDefinition *core_def = FindCoreDefinition (arch_name);
440 if (core_def)
441 {
442 m_core = core_def->core;
Greg Claytoneb0103f2011-04-07 22:46:35 +0000443 // Set the byte order to the default byte order for an architecture.
444 // This can be modified if needed for cases when cores handle both
445 // big and little endian
446 m_byte_order = core_def->default_byte_order;
Greg Clayton64195a22011-02-23 00:35:02 +0000447 }
448 else
449 {
450 Clear();
451 }
452
453
454 return IsValid();
455}
456
457bool
Greg Claytoneb0103f2011-04-07 22:46:35 +0000458ArchSpec::SetTriple (const char *triple_cstr, Platform *platform)
Greg Clayton64195a22011-02-23 00:35:02 +0000459{
460 if (triple_cstr || triple_cstr[0])
461 {
462 llvm::StringRef triple_stref (triple_cstr);
463 if (triple_stref.startswith (LLDB_ARCH_DEFAULT))
464 {
465 // Special case for the current host default architectures...
466 if (triple_stref.equals (LLDB_ARCH_DEFAULT_32BIT))
467 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture32);
468 else if (triple_stref.equals (LLDB_ARCH_DEFAULT_64BIT))
469 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture64);
470 else if (triple_stref.equals (LLDB_ARCH_DEFAULT))
471 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture);
472 }
473 else
474 {
475 std::string normalized_triple_sstr (llvm::Triple::normalize(triple_stref));
476 triple_stref = normalized_triple_sstr;
Greg Claytoneb0103f2011-04-07 22:46:35 +0000477 llvm::Triple normalized_triple (triple_stref);
478
479 const bool os_specified = normalized_triple.getOSName().size() > 0;
480 const bool vendor_specified = normalized_triple.getVendorName().size() > 0;
481 const bool env_specified = normalized_triple.getEnvironmentName().size() > 0;
482
483 // If we got an arch only, then default the vendor, os, environment
484 // to match the platform if one is supplied
485 if (!(os_specified || vendor_specified || env_specified))
486 {
487 if (platform)
488 {
489 // If we were given a platform, use the platform's system
490 // architecture. If this is not available (might not be
491 // connected) use the first supported architecture.
492 ArchSpec platform_arch (platform->GetSystemArchitecture());
493 if (!platform_arch.IsValid())
494 {
495 if (!platform->GetSupportedArchitectureAtIndex (0, platform_arch))
496 platform_arch.Clear();
497 }
498
499 if (platform_arch.IsValid())
500 {
501 normalized_triple.setVendor(platform_arch.GetTriple().getVendor());
502 normalized_triple.setOS(platform_arch.GetTriple().getOS());
503 normalized_triple.setEnvironment(platform_arch.GetTriple().getEnvironment());
504 }
505 }
506 else
507 {
508 // No platform specified, fall back to the host system for
509 // the default vendor, os, and environment.
510 llvm::Triple host_triple(llvm::sys::getHostTriple());
511 normalized_triple.setVendor(host_triple.getVendor());
512 normalized_triple.setOS(host_triple.getOS());
513 normalized_triple.setEnvironment(host_triple.getEnvironment());
514 }
515 }
516 SetTriple (normalized_triple);
Greg Clayton64195a22011-02-23 00:35:02 +0000517 }
518 }
519 else
520 Clear();
521 return IsValid();
522}
523
Greg Clayton64195a22011-02-23 00:35:02 +0000524bool
Greg Claytone0d378b2011-03-24 21:19:54 +0000525ArchSpec::SetArchitecture (ArchitectureType arch_type, uint32_t cpu, uint32_t sub)
Greg Clayton64195a22011-02-23 00:35:02 +0000526{
527 m_core = kCore_invalid;
528 bool update_triple = true;
529 const ArchDefinition *arch_def = FindArchDefinition(arch_type);
530 if (arch_def)
531 {
532 const ArchDefinitionEntry *arch_def_entry = FindArchDefinitionEntry (arch_def, cpu, sub);
533 if (arch_def_entry)
534 {
535 const CoreDefinition *core_def = FindCoreDefinition (arch_def_entry->core);
536 if (core_def)
537 {
538 m_core = core_def->core;
539 update_triple = false;
540 m_triple.setArch (core_def->machine);
541 if (arch_type == eArchTypeMachO)
542 {
543 m_triple.setVendor (llvm::Triple::Apple);
544 m_triple.setOS (llvm::Triple::Darwin);
545 }
546 else
547 {
548 m_triple.setVendor (llvm::Triple::UnknownVendor);
549 m_triple.setOS (llvm::Triple::UnknownOS);
550 }
551 }
552 }
553 }
554 CoreUpdated(update_triple);
555 return IsValid();
556}
557
Greg Clayton357132e2011-03-26 19:14:58 +0000558uint32_t
559ArchSpec::GetMinimumOpcodeByteSize() const
Greg Clayton64195a22011-02-23 00:35:02 +0000560{
Greg Clayton357132e2011-03-26 19:14:58 +0000561 const CoreDefinition *core_def = FindCoreDefinition (m_core);
562 if (core_def)
563 return core_def->min_opcode_byte_size;
564 return 0;
565}
566
567uint32_t
568ArchSpec::GetMaximumOpcodeByteSize() const
569{
570 const CoreDefinition *core_def = FindCoreDefinition (m_core);
571 if (core_def)
572 return core_def->max_opcode_byte_size;
573 return 0;
Greg Clayton64195a22011-02-23 00:35:02 +0000574}
575
576//===----------------------------------------------------------------------===//
577// Helper methods.
578
579void
580ArchSpec::CoreUpdated (bool update_triple)
581{
582 const CoreDefinition *core_def = FindCoreDefinition (m_core);
583 if (core_def)
584 {
585 if (update_triple)
586 m_triple = llvm::Triple(core_def->name, "unknown", "unknown");
587 m_byte_order = core_def->default_byte_order;
588 }
589 else
590 {
591 if (update_triple)
592 m_triple = llvm::Triple();
593 m_byte_order = eByteOrderInvalid;
594 }
595}
596
597//===----------------------------------------------------------------------===//
598// Operators.
599
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000600bool
601lldb_private::operator== (const ArchSpec& lhs, const ArchSpec& rhs)
602{
Greg Clayton64195a22011-02-23 00:35:02 +0000603 const ArchSpec::Core lhs_core = lhs.GetCore ();
604 const ArchSpec::Core rhs_core = rhs.GetCore ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000605
Greg Clayton64195a22011-02-23 00:35:02 +0000606 if (lhs_core == rhs_core)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000607 return true;
608
Greg Clayton64195a22011-02-23 00:35:02 +0000609 if (lhs_core == ArchSpec::kCore_any || rhs_core == ArchSpec::kCore_any)
610 return true;
611
612 if (lhs_core == ArchSpec::kCore_arm_any)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000613 {
Greg Clayton64195a22011-02-23 00:35:02 +0000614 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 +0000615 return true;
Greg Clayton64195a22011-02-23 00:35:02 +0000616 }
617 else if (rhs_core == ArchSpec::kCore_arm_any)
618 {
619 if ((lhs_core >= ArchSpec::kCore_arm_first && lhs_core <= ArchSpec::kCore_arm_last) || (lhs_core == ArchSpec::kCore_arm_any))
620 return true;
621 }
622 else if (lhs_core == ArchSpec::kCore_x86_32_any)
623 {
624 if ((rhs_core >= ArchSpec::kCore_x86_32_first && rhs_core <= ArchSpec::kCore_x86_32_last) || (rhs_core == ArchSpec::kCore_x86_32_any))
625 return true;
626 }
627 else if (rhs_core == ArchSpec::kCore_x86_32_any)
628 {
629 if ((lhs_core >= ArchSpec::kCore_x86_32_first && lhs_core <= ArchSpec::kCore_x86_32_last) || (lhs_core == ArchSpec::kCore_x86_32_any))
630 return true;
631 }
632 else if (lhs_core == ArchSpec::kCore_ppc_any)
633 {
634 if ((rhs_core >= ArchSpec::kCore_ppc_first && rhs_core <= ArchSpec::kCore_ppc_last) || (rhs_core == ArchSpec::kCore_ppc_any))
635 return true;
636 }
637 else if (rhs_core == ArchSpec::kCore_ppc_any)
638 {
639 if ((lhs_core >= ArchSpec::kCore_ppc_first && lhs_core <= ArchSpec::kCore_ppc_last) || (lhs_core == ArchSpec::kCore_ppc_any))
640 return true;
641 }
642 else if (lhs_core == ArchSpec::kCore_ppc64_any)
643 {
644 if ((rhs_core >= ArchSpec::kCore_ppc64_first && rhs_core <= ArchSpec::kCore_ppc64_last) || (rhs_core == ArchSpec::kCore_ppc64_any))
645 return true;
646 }
647 else if (rhs_core == ArchSpec::kCore_ppc64_any)
648 {
649 if ((lhs_core >= ArchSpec::kCore_ppc64_first && lhs_core <= ArchSpec::kCore_ppc64_last) || (lhs_core == ArchSpec::kCore_ppc64_any))
650 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000651 }
652 return false;
653}
654
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000655bool
656lldb_private::operator!= (const ArchSpec& lhs, const ArchSpec& rhs)
657{
658 return !(lhs == rhs);
659}
660
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000661bool
662lldb_private::operator<(const ArchSpec& lhs, const ArchSpec& rhs)
663{
Greg Clayton64195a22011-02-23 00:35:02 +0000664 const ArchSpec::Core lhs_core = lhs.GetCore ();
665 const ArchSpec::Core rhs_core = rhs.GetCore ();
666 return lhs_core < rhs_core;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000667}