blob: 368ed7f45d5680c6704f5b19fdf7268d43947921 [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 },
Jason Molenda311186a2011-08-16 01:23:22 +0000170 { ArchSpec::eCore_x86_64_x86_64 , llvm::MachO::CPUTypeX86_64 , 4 },
Greg Clayton64195a22011-02-23 00:35:02 +0000171 { ArchSpec::eCore_x86_64_x86_64 , llvm::MachO::CPUTypeX86_64 , CPU_ANY }
172};
173static const ArchDefinition g_macho_arch_def = {
174 eArchTypeMachO,
175 sizeof(g_macho_arch_entries)/sizeof(g_macho_arch_entries[0]),
176 g_macho_arch_entries,
177 UINT32_MAX, // CPU type mask
178 0x00FFFFFFu, // CPU subtype mask
179 "mach-o"
Greg Clayton41f92322010-06-11 03:25:34 +0000180};
181
Greg Clayton64195a22011-02-23 00:35:02 +0000182//===----------------------------------------------------------------------===//
183// A table that gets searched linearly for matches. This table is used to
184// convert cpu type and subtypes to architecture names, and to convert
185// architecture names to cpu types and subtypes. The ordering is important and
186// allows the precedence to be set when the table is built.
187static const ArchDefinitionEntry g_elf_arch_entries[] =
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000188{
Greg Clayton64195a22011-02-23 00:35:02 +0000189 { ArchSpec::eCore_sparc_generic , llvm::ELF::EM_SPARC , LLDB_INVALID_CPUTYPE }, // Sparc
190 { ArchSpec::eCore_x86_32_i386 , llvm::ELF::EM_386 , LLDB_INVALID_CPUTYPE }, // Intel 80386
191 { ArchSpec::eCore_x86_32_i486 , llvm::ELF::EM_486 , LLDB_INVALID_CPUTYPE }, // Intel 486 (deprecated)
192 { ArchSpec::eCore_ppc_generic , llvm::ELF::EM_PPC , LLDB_INVALID_CPUTYPE }, // PowerPC
193 { ArchSpec::eCore_ppc64_generic , llvm::ELF::EM_PPC64 , LLDB_INVALID_CPUTYPE }, // PowerPC64
194 { ArchSpec::eCore_arm_generic , llvm::ELF::EM_ARM , LLDB_INVALID_CPUTYPE }, // ARM
195 { ArchSpec::eCore_alpha_generic , llvm::ELF::EM_ALPHA , LLDB_INVALID_CPUTYPE }, // DEC Alpha
196 { ArchSpec::eCore_sparc9_generic , llvm::ELF::EM_SPARCV9, LLDB_INVALID_CPUTYPE }, // SPARC V9
197 { ArchSpec::eCore_x86_64_x86_64 , llvm::ELF::EM_X86_64 , LLDB_INVALID_CPUTYPE }, // AMD64
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000198};
199
Greg Clayton64195a22011-02-23 00:35:02 +0000200static const ArchDefinition g_elf_arch_def = {
201 eArchTypeELF,
202 sizeof(g_elf_arch_entries)/sizeof(g_elf_arch_entries[0]),
203 g_elf_arch_entries,
204 UINT32_MAX, // CPU type mask
205 UINT32_MAX, // CPU subtype mask
206 "elf",
Greg Clayton41f92322010-06-11 03:25:34 +0000207};
208
Greg Clayton64195a22011-02-23 00:35:02 +0000209//===----------------------------------------------------------------------===//
210// Table of all ArchDefinitions
211static const ArchDefinition *g_arch_definitions[] = {
212 &g_macho_arch_def,
213 &g_elf_arch_def,
214};
Greg Clayton41f92322010-06-11 03:25:34 +0000215
Greg Clayton64195a22011-02-23 00:35:02 +0000216static const size_t k_num_arch_definitions =
217 sizeof(g_arch_definitions) / sizeof(g_arch_definitions[0]);
218
219//===----------------------------------------------------------------------===//
220// Static helper functions.
221
222
223// Get the architecture definition for a given object type.
224static const ArchDefinition *
225FindArchDefinition (ArchitectureType arch_type)
226{
227 for (unsigned int i = 0; i < k_num_arch_definitions; ++i)
228 {
229 const ArchDefinition *def = g_arch_definitions[i];
230 if (def->type == arch_type)
231 return def;
232 }
233 return NULL;
234}
235
236// Get an architecture definition by name.
237static const CoreDefinition *
238FindCoreDefinition (llvm::StringRef name)
239{
240 for (unsigned int i = 0; i < ArchSpec::kNumCores; ++i)
241 {
242 if (name.equals_lower(g_core_definitions[i].name))
243 return &g_core_definitions[i];
244 }
245 return NULL;
246}
247
248static inline const CoreDefinition *
249FindCoreDefinition (ArchSpec::Core core)
250{
251 if (core >= 0 && core < ArchSpec::kNumCores)
252 return &g_core_definitions[core];
253 return NULL;
254}
255
256// Get a definition entry by cpu type and subtype.
257static const ArchDefinitionEntry *
258FindArchDefinitionEntry (const ArchDefinition *def, uint32_t cpu, uint32_t sub)
259{
260 if (def == NULL)
261 return NULL;
262
263 const uint32_t cpu_mask = def->cpu_mask;
264 const uint32_t sub_mask = def->sub_mask;
265 const ArchDefinitionEntry *entries = def->entries;
266 for (size_t i = 0; i < def->num_entries; ++i)
267 {
268 if ((entries[i].cpu == (cpu_mask & cpu)) &&
269 (entries[i].sub == (sub_mask & sub)))
270 return &entries[i];
271 }
272 return NULL;
273}
274
275static const ArchDefinitionEntry *
276FindArchDefinitionEntry (const ArchDefinition *def, ArchSpec::Core core)
277{
278 if (def == NULL)
279 return NULL;
280
281 const ArchDefinitionEntry *entries = def->entries;
282 for (size_t i = 0; i < def->num_entries; ++i)
283 {
284 if (entries[i].core == core)
285 return &entries[i];
286 }
287 return NULL;
288}
289
290//===----------------------------------------------------------------------===//
291// Constructors and destructors.
292
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000293ArchSpec::ArchSpec() :
Greg Clayton514487e2011-02-15 21:59:32 +0000294 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000295 m_core (kCore_invalid),
296 m_byte_order (eByteOrderInvalid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000297{
298}
299
Greg Claytoneb0103f2011-04-07 22:46:35 +0000300ArchSpec::ArchSpec (const char *triple_cstr, Platform *platform) :
Greg Clayton514487e2011-02-15 21:59:32 +0000301 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000302 m_core (kCore_invalid),
303 m_byte_order (eByteOrderInvalid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000304{
Greg Clayton64195a22011-02-23 00:35:02 +0000305 if (triple_cstr)
Greg Claytoneb0103f2011-04-07 22:46:35 +0000306 SetTriple(triple_cstr, platform);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000307}
308
Greg Clayton64195a22011-02-23 00:35:02 +0000309ArchSpec::ArchSpec(const llvm::Triple &triple) :
Greg Clayton514487e2011-02-15 21:59:32 +0000310 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000311 m_core (kCore_invalid),
312 m_byte_order (eByteOrderInvalid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000313{
Greg Clayton64195a22011-02-23 00:35:02 +0000314 SetTriple(triple);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000315}
316
Greg Claytone0d378b2011-03-24 21:19:54 +0000317ArchSpec::ArchSpec (ArchitectureType arch_type, uint32_t cpu, uint32_t subtype) :
Greg Clayton64195a22011-02-23 00:35:02 +0000318 m_triple (),
319 m_core (kCore_invalid),
320 m_byte_order (eByteOrderInvalid)
321{
322 SetArchitecture (arch_type, cpu, subtype);
323}
324
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000325ArchSpec::~ArchSpec()
326{
327}
328
Greg Clayton64195a22011-02-23 00:35:02 +0000329//===----------------------------------------------------------------------===//
330// Assignment and initialization.
331
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000332const ArchSpec&
333ArchSpec::operator= (const ArchSpec& rhs)
334{
335 if (this != &rhs)
336 {
Greg Clayton514487e2011-02-15 21:59:32 +0000337 m_triple = rhs.m_triple;
Greg Clayton64195a22011-02-23 00:35:02 +0000338 m_core = rhs.m_core;
Greg Clayton514487e2011-02-15 21:59:32 +0000339 m_byte_order = rhs.m_byte_order;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000340 }
341 return *this;
342}
343
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000344void
345ArchSpec::Clear()
346{
Greg Clayton514487e2011-02-15 21:59:32 +0000347 m_triple = llvm::Triple();
Greg Clayton64195a22011-02-23 00:35:02 +0000348 m_core = kCore_invalid;
349 m_byte_order = eByteOrderInvalid;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000350}
351
Greg Clayton64195a22011-02-23 00:35:02 +0000352//===----------------------------------------------------------------------===//
353// Predicates.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000354
Greg Clayton41f92322010-06-11 03:25:34 +0000355
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000356const char *
Greg Clayton64195a22011-02-23 00:35:02 +0000357ArchSpec::GetArchitectureName () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000358{
Greg Clayton64195a22011-02-23 00:35:02 +0000359 const CoreDefinition *core_def = FindCoreDefinition (m_core);
360 if (core_def)
361 return core_def->name;
362 return "unknown";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000363}
364
Greg Clayton64195a22011-02-23 00:35:02 +0000365uint32_t
366ArchSpec::GetMachOCPUType () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000367{
Greg Clayton64195a22011-02-23 00:35:02 +0000368 const CoreDefinition *core_def = FindCoreDefinition (m_core);
369 if (core_def)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000370 {
Greg Clayton64195a22011-02-23 00:35:02 +0000371 const ArchDefinitionEntry *arch_def = FindArchDefinitionEntry (&g_macho_arch_def, core_def->core);
372 if (arch_def)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000373 {
Greg Clayton64195a22011-02-23 00:35:02 +0000374 return arch_def->cpu;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000375 }
376 }
Greg Clayton64195a22011-02-23 00:35:02 +0000377 return LLDB_INVALID_CPUTYPE;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000378}
379
Greg Clayton64195a22011-02-23 00:35:02 +0000380uint32_t
381ArchSpec::GetMachOCPUSubType () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000382{
Greg Clayton64195a22011-02-23 00:35:02 +0000383 const CoreDefinition *core_def = FindCoreDefinition (m_core);
384 if (core_def)
385 {
386 const ArchDefinitionEntry *arch_def = FindArchDefinitionEntry (&g_macho_arch_def, core_def->core);
387 if (arch_def)
388 {
Greg Clayton1cb64962011-03-24 04:28:38 +0000389 return arch_def->sub;
Greg Clayton64195a22011-02-23 00:35:02 +0000390 }
391 }
392 return LLDB_INVALID_CPUTYPE;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000393}
394
Greg Clayton64195a22011-02-23 00:35:02 +0000395llvm::Triple::ArchType
396ArchSpec::GetMachine () const
397{
398 const CoreDefinition *core_def = FindCoreDefinition (m_core);
399 if (core_def)
400 return core_def->machine;
401
402 return llvm::Triple::UnknownArch;
403}
404
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000405uint32_t
406ArchSpec::GetAddressByteSize() const
407{
Greg Clayton64195a22011-02-23 00:35:02 +0000408 const CoreDefinition *core_def = FindCoreDefinition (m_core);
409 if (core_def)
410 return core_def->addr_byte_size;
Greg Clayton41f92322010-06-11 03:25:34 +0000411 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000412}
413
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000414ByteOrder
415ArchSpec::GetDefaultEndian () const
416{
Greg Clayton64195a22011-02-23 00:35:02 +0000417 const CoreDefinition *core_def = FindCoreDefinition (m_core);
418 if (core_def)
419 return core_def->default_byte_order;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000420 return eByteOrderInvalid;
421}
422
Greg Clayton64195a22011-02-23 00:35:02 +0000423lldb::ByteOrder
424ArchSpec::GetByteOrder () const
425{
426 if (m_byte_order == eByteOrderInvalid)
427 return GetDefaultEndian();
428 return m_byte_order;
429}
430
431//===----------------------------------------------------------------------===//
432// Mutators.
433
434bool
435ArchSpec::SetTriple (const llvm::Triple &triple)
436{
437 m_triple = triple;
438
439 llvm::StringRef arch_name (m_triple.getArchName());
440 const CoreDefinition *core_def = FindCoreDefinition (arch_name);
441 if (core_def)
442 {
443 m_core = core_def->core;
Greg Claytoneb0103f2011-04-07 22:46:35 +0000444 // Set the byte order to the default byte order for an architecture.
445 // This can be modified if needed for cases when cores handle both
446 // big and little endian
447 m_byte_order = core_def->default_byte_order;
Greg Clayton64195a22011-02-23 00:35:02 +0000448 }
449 else
450 {
451 Clear();
452 }
453
454
455 return IsValid();
456}
457
458bool
Greg Claytoneb0103f2011-04-07 22:46:35 +0000459ArchSpec::SetTriple (const char *triple_cstr, Platform *platform)
Greg Clayton64195a22011-02-23 00:35:02 +0000460{
Greg Clayton23aca092011-08-12 23:32:52 +0000461 if (triple_cstr && triple_cstr[0])
Greg Clayton64195a22011-02-23 00:35:02 +0000462 {
463 llvm::StringRef triple_stref (triple_cstr);
464 if (triple_stref.startswith (LLDB_ARCH_DEFAULT))
465 {
466 // Special case for the current host default architectures...
467 if (triple_stref.equals (LLDB_ARCH_DEFAULT_32BIT))
468 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture32);
469 else if (triple_stref.equals (LLDB_ARCH_DEFAULT_64BIT))
470 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture64);
471 else if (triple_stref.equals (LLDB_ARCH_DEFAULT))
472 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture);
473 }
474 else
475 {
476 std::string normalized_triple_sstr (llvm::Triple::normalize(triple_stref));
477 triple_stref = normalized_triple_sstr;
Greg Claytoneb0103f2011-04-07 22:46:35 +0000478 llvm::Triple normalized_triple (triple_stref);
479
480 const bool os_specified = normalized_triple.getOSName().size() > 0;
481 const bool vendor_specified = normalized_triple.getVendorName().size() > 0;
482 const bool env_specified = normalized_triple.getEnvironmentName().size() > 0;
483
484 // If we got an arch only, then default the vendor, os, environment
485 // to match the platform if one is supplied
486 if (!(os_specified || vendor_specified || env_specified))
487 {
488 if (platform)
489 {
490 // If we were given a platform, use the platform's system
491 // architecture. If this is not available (might not be
492 // connected) use the first supported architecture.
493 ArchSpec platform_arch (platform->GetSystemArchitecture());
494 if (!platform_arch.IsValid())
495 {
496 if (!platform->GetSupportedArchitectureAtIndex (0, platform_arch))
497 platform_arch.Clear();
498 }
499
500 if (platform_arch.IsValid())
501 {
502 normalized_triple.setVendor(platform_arch.GetTriple().getVendor());
503 normalized_triple.setOS(platform_arch.GetTriple().getOS());
504 normalized_triple.setEnvironment(platform_arch.GetTriple().getEnvironment());
505 }
506 }
507 else
508 {
509 // No platform specified, fall back to the host system for
510 // the default vendor, os, and environment.
511 llvm::Triple host_triple(llvm::sys::getHostTriple());
512 normalized_triple.setVendor(host_triple.getVendor());
513 normalized_triple.setOS(host_triple.getOS());
514 normalized_triple.setEnvironment(host_triple.getEnvironment());
515 }
516 }
517 SetTriple (normalized_triple);
Greg Clayton64195a22011-02-23 00:35:02 +0000518 }
519 }
520 else
521 Clear();
522 return IsValid();
523}
524
Greg Clayton64195a22011-02-23 00:35:02 +0000525bool
Greg Claytone0d378b2011-03-24 21:19:54 +0000526ArchSpec::SetArchitecture (ArchitectureType arch_type, uint32_t cpu, uint32_t sub)
Greg Clayton64195a22011-02-23 00:35:02 +0000527{
528 m_core = kCore_invalid;
529 bool update_triple = true;
530 const ArchDefinition *arch_def = FindArchDefinition(arch_type);
531 if (arch_def)
532 {
533 const ArchDefinitionEntry *arch_def_entry = FindArchDefinitionEntry (arch_def, cpu, sub);
534 if (arch_def_entry)
535 {
536 const CoreDefinition *core_def = FindCoreDefinition (arch_def_entry->core);
537 if (core_def)
538 {
539 m_core = core_def->core;
540 update_triple = false;
541 m_triple.setArch (core_def->machine);
542 if (arch_type == eArchTypeMachO)
543 {
544 m_triple.setVendor (llvm::Triple::Apple);
545 m_triple.setOS (llvm::Triple::Darwin);
546 }
547 else
548 {
549 m_triple.setVendor (llvm::Triple::UnknownVendor);
550 m_triple.setOS (llvm::Triple::UnknownOS);
551 }
552 }
553 }
554 }
555 CoreUpdated(update_triple);
556 return IsValid();
557}
558
Greg Clayton357132e2011-03-26 19:14:58 +0000559uint32_t
560ArchSpec::GetMinimumOpcodeByteSize() const
Greg Clayton64195a22011-02-23 00:35:02 +0000561{
Greg Clayton357132e2011-03-26 19:14:58 +0000562 const CoreDefinition *core_def = FindCoreDefinition (m_core);
563 if (core_def)
564 return core_def->min_opcode_byte_size;
565 return 0;
566}
567
568uint32_t
569ArchSpec::GetMaximumOpcodeByteSize() const
570{
571 const CoreDefinition *core_def = FindCoreDefinition (m_core);
572 if (core_def)
573 return core_def->max_opcode_byte_size;
574 return 0;
Greg Clayton64195a22011-02-23 00:35:02 +0000575}
576
577//===----------------------------------------------------------------------===//
578// Helper methods.
579
580void
581ArchSpec::CoreUpdated (bool update_triple)
582{
583 const CoreDefinition *core_def = FindCoreDefinition (m_core);
584 if (core_def)
585 {
586 if (update_triple)
587 m_triple = llvm::Triple(core_def->name, "unknown", "unknown");
588 m_byte_order = core_def->default_byte_order;
589 }
590 else
591 {
592 if (update_triple)
593 m_triple = llvm::Triple();
594 m_byte_order = eByteOrderInvalid;
595 }
596}
597
598//===----------------------------------------------------------------------===//
599// Operators.
600
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000601bool
602lldb_private::operator== (const ArchSpec& lhs, const ArchSpec& rhs)
603{
Greg Clayton64195a22011-02-23 00:35:02 +0000604 const ArchSpec::Core lhs_core = lhs.GetCore ();
605 const ArchSpec::Core rhs_core = rhs.GetCore ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000606
Greg Clayton64195a22011-02-23 00:35:02 +0000607 if (lhs_core == rhs_core)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000608 return true;
609
Greg Clayton64195a22011-02-23 00:35:02 +0000610 if (lhs_core == ArchSpec::kCore_any || rhs_core == ArchSpec::kCore_any)
611 return true;
612
613 if (lhs_core == ArchSpec::kCore_arm_any)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000614 {
Greg Clayton64195a22011-02-23 00:35:02 +0000615 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 +0000616 return true;
Greg Clayton64195a22011-02-23 00:35:02 +0000617 }
618 else if (rhs_core == ArchSpec::kCore_arm_any)
619 {
620 if ((lhs_core >= ArchSpec::kCore_arm_first && lhs_core <= ArchSpec::kCore_arm_last) || (lhs_core == ArchSpec::kCore_arm_any))
621 return true;
622 }
623 else if (lhs_core == ArchSpec::kCore_x86_32_any)
624 {
625 if ((rhs_core >= ArchSpec::kCore_x86_32_first && rhs_core <= ArchSpec::kCore_x86_32_last) || (rhs_core == ArchSpec::kCore_x86_32_any))
626 return true;
627 }
628 else if (rhs_core == ArchSpec::kCore_x86_32_any)
629 {
630 if ((lhs_core >= ArchSpec::kCore_x86_32_first && lhs_core <= ArchSpec::kCore_x86_32_last) || (lhs_core == ArchSpec::kCore_x86_32_any))
631 return true;
632 }
633 else if (lhs_core == ArchSpec::kCore_ppc_any)
634 {
635 if ((rhs_core >= ArchSpec::kCore_ppc_first && rhs_core <= ArchSpec::kCore_ppc_last) || (rhs_core == ArchSpec::kCore_ppc_any))
636 return true;
637 }
638 else if (rhs_core == ArchSpec::kCore_ppc_any)
639 {
640 if ((lhs_core >= ArchSpec::kCore_ppc_first && lhs_core <= ArchSpec::kCore_ppc_last) || (lhs_core == ArchSpec::kCore_ppc_any))
641 return true;
642 }
643 else if (lhs_core == ArchSpec::kCore_ppc64_any)
644 {
645 if ((rhs_core >= ArchSpec::kCore_ppc64_first && rhs_core <= ArchSpec::kCore_ppc64_last) || (rhs_core == ArchSpec::kCore_ppc64_any))
646 return true;
647 }
648 else if (rhs_core == ArchSpec::kCore_ppc64_any)
649 {
650 if ((lhs_core >= ArchSpec::kCore_ppc64_first && lhs_core <= ArchSpec::kCore_ppc64_last) || (lhs_core == ArchSpec::kCore_ppc64_any))
651 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000652 }
653 return false;
654}
655
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000656bool
657lldb_private::operator!= (const ArchSpec& lhs, const ArchSpec& rhs)
658{
659 return !(lhs == rhs);
660}
661
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000662bool
663lldb_private::operator<(const ArchSpec& lhs, const ArchSpec& rhs)
664{
Greg Clayton64195a22011-02-23 00:35:02 +0000665 const ArchSpec::Core lhs_core = lhs.GetCore ();
666 const ArchSpec::Core rhs_core = rhs.GetCore ();
667 return lhs_core < rhs_core;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000668}