blob: 836a0efa890aa89de3e1cc6c9368f0013bfe6833 [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>
Jason Molendadfa424c2012-09-18 23:27:18 +000013#include <errno.h>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000014
15#include <string>
16
Charles Davis237ad972013-08-27 05:04:33 +000017#include "llvm/Support/COFF.h"
Greg Clayton41f92322010-06-11 03:25:34 +000018#include "llvm/Support/ELF.h"
Stephen Wilsonfacebfc2011-02-24 19:13:58 +000019#include "llvm/Support/Host.h"
Greg Clayton41f92322010-06-11 03:25:34 +000020#include "llvm/Support/MachO.h"
Greg Claytone795f1b2012-08-08 01:19:34 +000021#include "lldb/Core/RegularExpression.h"
Greg Clayton514487e2011-02-15 21:59:32 +000022#include "lldb/Host/Endian.h"
23#include "lldb/Host/Host.h"
Greg Claytoneb0103f2011-04-07 22:46:35 +000024#include "lldb/Target/Platform.h"
Greg Clayton41f92322010-06-11 03:25:34 +000025
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026using namespace lldb;
27using namespace lldb_private;
28
Greg Clayton64195a22011-02-23 00:35:02 +000029#define ARCH_SPEC_SEPARATOR_CHAR '-'
Chris Lattner30fdc8d2010-06-08 16:52:24 +000030
Jason Molendaba813dc2012-11-04 03:20:05 +000031
32static bool cores_match (const ArchSpec::Core core1, const ArchSpec::Core core2, bool try_inverse, bool enforce_exact_match);
33
Greg Clayton64195a22011-02-23 00:35:02 +000034namespace lldb_private {
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035
Greg Clayton64195a22011-02-23 00:35:02 +000036 struct CoreDefinition
37 {
38 ByteOrder default_byte_order;
39 uint32_t addr_byte_size;
Greg Clayton357132e2011-03-26 19:14:58 +000040 uint32_t min_opcode_byte_size;
41 uint32_t max_opcode_byte_size;
Greg Clayton64195a22011-02-23 00:35:02 +000042 llvm::Triple::ArchType machine;
43 ArchSpec::Core core;
44 const char *name;
45 };
46
47}
48
49// This core information can be looked using the ArchSpec::Core as the index
50static const CoreDefinition g_core_definitions[ArchSpec::kNumCores] =
Chris Lattner30fdc8d2010-06-08 16:52:24 +000051{
Greg Clayton357132e2011-03-26 19:14:58 +000052 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_generic , "arm" },
53 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv4 , "armv4" },
54 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv4t , "armv4t" },
55 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv5 , "armv5" },
Greg Claytonb5c39fe2011-12-16 18:15:52 +000056 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv5e , "armv5e" },
Greg Clayton357132e2011-03-26 19:14:58 +000057 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv5t , "armv5t" },
58 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv6 , "armv6" },
59 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7 , "armv7" },
60 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7f , "armv7f" },
Greg Clayton357132e2011-03-26 19:14:58 +000061 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7s , "armv7s" },
Jason Molenda7a1559c2013-03-08 01:20:17 +000062 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7k , "armv7k" },
63 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7m , "armv7m" },
64 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7em , "armv7em" },
Greg Clayton357132e2011-03-26 19:14:58 +000065 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_xscale , "xscale" },
Greg Claytonb5c39fe2011-12-16 18:15:52 +000066 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumb , "thumb" },
67 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv4t , "thumbv4t" },
68 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv5 , "thumbv5" },
69 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv5e , "thumbv5e" },
70 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv6 , "thumbv6" },
71 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7 , "thumbv7" },
72 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7f , "thumbv7f" },
Greg Claytonb5c39fe2011-12-16 18:15:52 +000073 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7s , "thumbv7s" },
Jason Molenda7a1559c2013-03-08 01:20:17 +000074 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7k , "thumbv7k" },
75 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7m , "thumbv7m" },
76 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7em , "thumbv7em" },
Greg Claytonb5c39fe2011-12-16 18:15:52 +000077
Greg Clayton64195a22011-02-23 00:35:02 +000078
Greg Clayton83b162d2013-08-12 18:34:04 +000079 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_generic , "ppc" },
80 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc601 , "ppc601" },
81 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc602 , "ppc602" },
82 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc603 , "ppc603" },
83 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc603e , "ppc603e" },
84 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc603ev , "ppc603ev" },
85 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc604 , "ppc604" },
86 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc604e , "ppc604e" },
87 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc620 , "ppc620" },
88 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc750 , "ppc750" },
89 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc7400 , "ppc7400" },
90 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc7450 , "ppc7450" },
91 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc970 , "ppc970" },
Greg Clayton64195a22011-02-23 00:35:02 +000092
Greg Clayton83b162d2013-08-12 18:34:04 +000093 { eByteOrderBig , 8, 4, 4, llvm::Triple::ppc64 , ArchSpec::eCore_ppc64_generic , "ppc64" },
94 { eByteOrderBig , 8, 4, 4, llvm::Triple::ppc64 , ArchSpec::eCore_ppc64_ppc970_64 , "ppc970-64" },
Greg Clayton64195a22011-02-23 00:35:02 +000095
Greg Clayton357132e2011-03-26 19:14:58 +000096 { eByteOrderLittle, 4, 4, 4, llvm::Triple::sparc , ArchSpec::eCore_sparc_generic , "sparc" },
97 { eByteOrderLittle, 8, 4, 4, llvm::Triple::sparcv9, ArchSpec::eCore_sparc9_generic , "sparcv9" },
Greg Clayton64195a22011-02-23 00:35:02 +000098
Greg Claytonab65b342011-04-13 22:47:15 +000099 { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i386 , "i386" },
100 { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i486 , "i486" },
101 { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i486sx , "i486sx" },
Greg Clayton64195a22011-02-23 00:35:02 +0000102
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000103 { eByteOrderLittle, 8, 1, 15, llvm::Triple::x86_64 , ArchSpec::eCore_x86_64_x86_64 , "x86_64" },
104 { eByteOrderLittle, 4, 4, 4 , llvm::Triple::UnknownArch , ArchSpec::eCore_uknownMach32 , "unknown-mach-32" },
105 { eByteOrderLittle, 8, 4, 4 , llvm::Triple::UnknownArch , ArchSpec::eCore_uknownMach64 , "unknown-mach-64" }
Greg Clayton64195a22011-02-23 00:35:02 +0000106};
107
108struct ArchDefinitionEntry
109{
110 ArchSpec::Core core;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000111 uint32_t cpu;
112 uint32_t sub;
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000113 uint32_t cpu_mask;
114 uint32_t sub_mask;
Greg Clayton64195a22011-02-23 00:35:02 +0000115};
116
117struct ArchDefinition
118{
119 ArchitectureType type;
120 size_t num_entries;
121 const ArchDefinitionEntry *entries;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000122 const char *name;
123};
124
Greg Clayton41f92322010-06-11 03:25:34 +0000125
Greg Claytonc7bece562013-01-25 18:06:21 +0000126size_t
Greg Claytonab65b342011-04-13 22:47:15 +0000127ArchSpec::AutoComplete (const char *name, StringList &matches)
128{
129 uint32_t i;
130 if (name && name[0])
131 {
132 for (i = 0; i < ArchSpec::kNumCores; ++i)
133 {
134 if (NameMatches(g_core_definitions[i].name, eNameMatchStartsWith, name))
135 matches.AppendString (g_core_definitions[i].name);
136 }
137 }
138 else
139 {
140 for (i = 0; i < ArchSpec::kNumCores; ++i)
141 matches.AppendString (g_core_definitions[i].name);
142 }
143 return matches.GetSize();
144}
145
146
147
Greg Clayton64195a22011-02-23 00:35:02 +0000148#define CPU_ANY (UINT32_MAX)
149
150//===----------------------------------------------------------------------===//
151// A table that gets searched linearly for matches. This table is used to
152// convert cpu type and subtypes to architecture names, and to convert
153// architecture names to cpu types and subtypes. The ordering is important and
154// allows the precedence to be set when the table is built.
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000155#define SUBTYPE_MASK 0x00FFFFFFu
Greg Clayton64195a22011-02-23 00:35:02 +0000156static const ArchDefinitionEntry g_macho_arch_entries[] =
Greg Clayton41f92322010-06-11 03:25:34 +0000157{
Charles Davis510938e2013-08-27 05:04:57 +0000158 { ArchSpec::eCore_arm_generic , llvm::MachO::CPU_TYPE_ARM , CPU_ANY, UINT32_MAX , UINT32_MAX },
159 { ArchSpec::eCore_arm_generic , llvm::MachO::CPU_TYPE_ARM , 0 , UINT32_MAX , SUBTYPE_MASK },
160 { ArchSpec::eCore_arm_armv4 , llvm::MachO::CPU_TYPE_ARM , 5 , UINT32_MAX , SUBTYPE_MASK },
161 { ArchSpec::eCore_arm_armv4t , llvm::MachO::CPU_TYPE_ARM , 5 , UINT32_MAX , SUBTYPE_MASK },
162 { ArchSpec::eCore_arm_armv6 , llvm::MachO::CPU_TYPE_ARM , 6 , UINT32_MAX , SUBTYPE_MASK },
163 { ArchSpec::eCore_arm_armv5 , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
164 { ArchSpec::eCore_arm_armv5e , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
165 { ArchSpec::eCore_arm_armv5t , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
166 { ArchSpec::eCore_arm_xscale , llvm::MachO::CPU_TYPE_ARM , 8 , UINT32_MAX , SUBTYPE_MASK },
167 { ArchSpec::eCore_arm_armv7 , llvm::MachO::CPU_TYPE_ARM , 9 , UINT32_MAX , SUBTYPE_MASK },
168 { ArchSpec::eCore_arm_armv7f , llvm::MachO::CPU_TYPE_ARM , 10 , UINT32_MAX , SUBTYPE_MASK },
169 { ArchSpec::eCore_arm_armv7s , llvm::MachO::CPU_TYPE_ARM , 11 , UINT32_MAX , SUBTYPE_MASK },
170 { ArchSpec::eCore_arm_armv7k , llvm::MachO::CPU_TYPE_ARM , 12 , UINT32_MAX , SUBTYPE_MASK },
171 { ArchSpec::eCore_arm_armv7m , llvm::MachO::CPU_TYPE_ARM , 15 , UINT32_MAX , SUBTYPE_MASK },
172 { ArchSpec::eCore_arm_armv7em , llvm::MachO::CPU_TYPE_ARM , 16 , UINT32_MAX , SUBTYPE_MASK },
173 { ArchSpec::eCore_thumb , llvm::MachO::CPU_TYPE_ARM , 0 , UINT32_MAX , SUBTYPE_MASK },
174 { ArchSpec::eCore_thumbv4t , llvm::MachO::CPU_TYPE_ARM , 5 , UINT32_MAX , SUBTYPE_MASK },
175 { ArchSpec::eCore_thumbv5 , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
176 { ArchSpec::eCore_thumbv5e , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
177 { ArchSpec::eCore_thumbv6 , llvm::MachO::CPU_TYPE_ARM , 6 , UINT32_MAX , SUBTYPE_MASK },
178 { ArchSpec::eCore_thumbv7 , llvm::MachO::CPU_TYPE_ARM , 9 , UINT32_MAX , SUBTYPE_MASK },
179 { ArchSpec::eCore_thumbv7f , llvm::MachO::CPU_TYPE_ARM , 10 , UINT32_MAX , SUBTYPE_MASK },
180 { ArchSpec::eCore_thumbv7s , llvm::MachO::CPU_TYPE_ARM , 11 , UINT32_MAX , SUBTYPE_MASK },
181 { ArchSpec::eCore_thumbv7k , llvm::MachO::CPU_TYPE_ARM , 12 , UINT32_MAX , SUBTYPE_MASK },
182 { ArchSpec::eCore_thumbv7m , llvm::MachO::CPU_TYPE_ARM , 15 , UINT32_MAX , SUBTYPE_MASK },
183 { ArchSpec::eCore_thumbv7em , llvm::MachO::CPU_TYPE_ARM , 16 , UINT32_MAX , SUBTYPE_MASK },
184 { ArchSpec::eCore_ppc_generic , llvm::MachO::CPU_TYPE_POWERPC , CPU_ANY, UINT32_MAX , UINT32_MAX },
185 { ArchSpec::eCore_ppc_generic , llvm::MachO::CPU_TYPE_POWERPC , 0 , UINT32_MAX , SUBTYPE_MASK },
186 { ArchSpec::eCore_ppc_ppc601 , llvm::MachO::CPU_TYPE_POWERPC , 1 , UINT32_MAX , SUBTYPE_MASK },
187 { ArchSpec::eCore_ppc_ppc602 , llvm::MachO::CPU_TYPE_POWERPC , 2 , UINT32_MAX , SUBTYPE_MASK },
188 { ArchSpec::eCore_ppc_ppc603 , llvm::MachO::CPU_TYPE_POWERPC , 3 , UINT32_MAX , SUBTYPE_MASK },
189 { ArchSpec::eCore_ppc_ppc603e , llvm::MachO::CPU_TYPE_POWERPC , 4 , UINT32_MAX , SUBTYPE_MASK },
190 { ArchSpec::eCore_ppc_ppc603ev , llvm::MachO::CPU_TYPE_POWERPC , 5 , UINT32_MAX , SUBTYPE_MASK },
191 { ArchSpec::eCore_ppc_ppc604 , llvm::MachO::CPU_TYPE_POWERPC , 6 , UINT32_MAX , SUBTYPE_MASK },
192 { ArchSpec::eCore_ppc_ppc604e , llvm::MachO::CPU_TYPE_POWERPC , 7 , UINT32_MAX , SUBTYPE_MASK },
193 { ArchSpec::eCore_ppc_ppc620 , llvm::MachO::CPU_TYPE_POWERPC , 8 , UINT32_MAX , SUBTYPE_MASK },
194 { ArchSpec::eCore_ppc_ppc750 , llvm::MachO::CPU_TYPE_POWERPC , 9 , UINT32_MAX , SUBTYPE_MASK },
195 { ArchSpec::eCore_ppc_ppc7400 , llvm::MachO::CPU_TYPE_POWERPC , 10 , UINT32_MAX , SUBTYPE_MASK },
196 { ArchSpec::eCore_ppc_ppc7450 , llvm::MachO::CPU_TYPE_POWERPC , 11 , UINT32_MAX , SUBTYPE_MASK },
197 { ArchSpec::eCore_ppc_ppc970 , llvm::MachO::CPU_TYPE_POWERPC , 100 , UINT32_MAX , SUBTYPE_MASK },
198 { ArchSpec::eCore_ppc64_generic , llvm::MachO::CPU_TYPE_POWERPC64 , 0 , UINT32_MAX , SUBTYPE_MASK },
199 { ArchSpec::eCore_ppc64_ppc970_64 , llvm::MachO::CPU_TYPE_POWERPC64 , 100 , UINT32_MAX , SUBTYPE_MASK },
200 { ArchSpec::eCore_x86_32_i386 , llvm::MachO::CPU_TYPE_I386 , 3 , UINT32_MAX , SUBTYPE_MASK },
201 { ArchSpec::eCore_x86_32_i486 , llvm::MachO::CPU_TYPE_I386 , 4 , UINT32_MAX , SUBTYPE_MASK },
202 { ArchSpec::eCore_x86_32_i486sx , llvm::MachO::CPU_TYPE_I386 , 0x84 , UINT32_MAX , SUBTYPE_MASK },
203 { ArchSpec::eCore_x86_32_i386 , llvm::MachO::CPU_TYPE_I386 , CPU_ANY, UINT32_MAX , UINT32_MAX },
204 { ArchSpec::eCore_x86_64_x86_64 , llvm::MachO::CPU_TYPE_X86_64 , 3 , UINT32_MAX , SUBTYPE_MASK },
205 { ArchSpec::eCore_x86_64_x86_64 , llvm::MachO::CPU_TYPE_X86_64 , 4 , UINT32_MAX , SUBTYPE_MASK },
206 { ArchSpec::eCore_x86_64_x86_64 , llvm::MachO::CPU_TYPE_X86_64 , CPU_ANY, UINT32_MAX , UINT32_MAX },
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000207 // Catch any unknown mach architectures so we can always use the object and symbol mach-o files
Charles Davis510938e2013-08-27 05:04:57 +0000208 { ArchSpec::eCore_uknownMach32 , 0 , 0 , 0xFF000000u, 0x00000000u },
209 { ArchSpec::eCore_uknownMach64 , llvm::MachO::CPU_ARCH_ABI64 , 0 , 0xFF000000u, 0x00000000u }
Greg Clayton64195a22011-02-23 00:35:02 +0000210};
211static const ArchDefinition g_macho_arch_def = {
212 eArchTypeMachO,
213 sizeof(g_macho_arch_entries)/sizeof(g_macho_arch_entries[0]),
214 g_macho_arch_entries,
Greg Clayton64195a22011-02-23 00:35:02 +0000215 "mach-o"
Greg Clayton41f92322010-06-11 03:25:34 +0000216};
217
Greg Clayton64195a22011-02-23 00:35:02 +0000218//===----------------------------------------------------------------------===//
219// A table that gets searched linearly for matches. This table is used to
220// convert cpu type and subtypes to architecture names, and to convert
221// architecture names to cpu types and subtypes. The ordering is important and
222// allows the precedence to be set when the table is built.
223static const ArchDefinitionEntry g_elf_arch_entries[] =
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000224{
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000225 { ArchSpec::eCore_sparc_generic , llvm::ELF::EM_SPARC , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // Sparc
226 { ArchSpec::eCore_x86_32_i386 , llvm::ELF::EM_386 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // Intel 80386
227 { ArchSpec::eCore_x86_32_i486 , llvm::ELF::EM_486 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // Intel 486 (deprecated)
228 { ArchSpec::eCore_ppc_generic , llvm::ELF::EM_PPC , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // PowerPC
229 { ArchSpec::eCore_ppc64_generic , llvm::ELF::EM_PPC64 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // PowerPC64
230 { ArchSpec::eCore_arm_generic , llvm::ELF::EM_ARM , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // ARM
231 { ArchSpec::eCore_sparc9_generic , llvm::ELF::EM_SPARCV9, LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // SPARC V9
232 { ArchSpec::eCore_x86_64_x86_64 , llvm::ELF::EM_X86_64 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu } // AMD64
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000233};
234
Greg Clayton64195a22011-02-23 00:35:02 +0000235static const ArchDefinition g_elf_arch_def = {
236 eArchTypeELF,
237 sizeof(g_elf_arch_entries)/sizeof(g_elf_arch_entries[0]),
238 g_elf_arch_entries,
Greg Clayton64195a22011-02-23 00:35:02 +0000239 "elf",
Greg Clayton41f92322010-06-11 03:25:34 +0000240};
241
Charles Davis237ad972013-08-27 05:04:33 +0000242static const ArchDefinitionEntry g_coff_arch_entries[] =
243{
244 { ArchSpec::eCore_x86_32_i386 , llvm::COFF::IMAGE_FILE_MACHINE_I386 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // Intel 80386
245 { ArchSpec::eCore_ppc_generic , llvm::COFF::IMAGE_FILE_MACHINE_POWERPC , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // PowerPC
246 { ArchSpec::eCore_ppc_generic , llvm::COFF::IMAGE_FILE_MACHINE_POWERPCFP, LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // PowerPC (with FPU)
247 { ArchSpec::eCore_arm_generic , llvm::COFF::IMAGE_FILE_MACHINE_ARM , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // ARM
248 { ArchSpec::eCore_arm_armv7 , llvm::COFF::IMAGE_FILE_MACHINE_ARMV7 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // ARMv7
249 { ArchSpec::eCore_thumb , llvm::COFF::IMAGE_FILE_MACHINE_THUMB , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // ARMv7
250 { ArchSpec::eCore_x86_64_x86_64, llvm::COFF::IMAGE_FILE_MACHINE_AMD64 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu } // AMD64
251};
252
253static const ArchDefinition g_coff_arch_def = {
254 eArchTypeCOFF,
255 sizeof(g_coff_arch_entries)/sizeof(g_coff_arch_entries[0]),
256 g_coff_arch_entries,
257 "pe-coff",
258};
259
Greg Clayton64195a22011-02-23 00:35:02 +0000260//===----------------------------------------------------------------------===//
261// Table of all ArchDefinitions
262static const ArchDefinition *g_arch_definitions[] = {
263 &g_macho_arch_def,
Charles Davis237ad972013-08-27 05:04:33 +0000264 &g_elf_arch_def,
265 &g_coff_arch_def
Greg Clayton64195a22011-02-23 00:35:02 +0000266};
Greg Clayton41f92322010-06-11 03:25:34 +0000267
Greg Clayton64195a22011-02-23 00:35:02 +0000268static const size_t k_num_arch_definitions =
269 sizeof(g_arch_definitions) / sizeof(g_arch_definitions[0]);
270
271//===----------------------------------------------------------------------===//
272// Static helper functions.
273
274
275// Get the architecture definition for a given object type.
276static const ArchDefinition *
277FindArchDefinition (ArchitectureType arch_type)
278{
279 for (unsigned int i = 0; i < k_num_arch_definitions; ++i)
280 {
281 const ArchDefinition *def = g_arch_definitions[i];
282 if (def->type == arch_type)
283 return def;
284 }
285 return NULL;
286}
287
288// Get an architecture definition by name.
289static const CoreDefinition *
290FindCoreDefinition (llvm::StringRef name)
291{
292 for (unsigned int i = 0; i < ArchSpec::kNumCores; ++i)
293 {
294 if (name.equals_lower(g_core_definitions[i].name))
295 return &g_core_definitions[i];
296 }
297 return NULL;
298}
299
300static inline const CoreDefinition *
301FindCoreDefinition (ArchSpec::Core core)
302{
303 if (core >= 0 && core < ArchSpec::kNumCores)
304 return &g_core_definitions[core];
305 return NULL;
306}
307
308// Get a definition entry by cpu type and subtype.
309static const ArchDefinitionEntry *
310FindArchDefinitionEntry (const ArchDefinition *def, uint32_t cpu, uint32_t sub)
311{
312 if (def == NULL)
313 return NULL;
314
Greg Clayton64195a22011-02-23 00:35:02 +0000315 const ArchDefinitionEntry *entries = def->entries;
316 for (size_t i = 0; i < def->num_entries; ++i)
317 {
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000318 if (entries[i].cpu == (cpu & entries[i].cpu_mask))
319 if (entries[i].sub == (sub & entries[i].sub_mask))
320 return &entries[i];
Greg Clayton64195a22011-02-23 00:35:02 +0000321 }
322 return NULL;
323}
324
325static const ArchDefinitionEntry *
326FindArchDefinitionEntry (const ArchDefinition *def, ArchSpec::Core core)
327{
328 if (def == NULL)
329 return NULL;
330
331 const ArchDefinitionEntry *entries = def->entries;
332 for (size_t i = 0; i < def->num_entries; ++i)
333 {
334 if (entries[i].core == core)
335 return &entries[i];
336 }
337 return NULL;
338}
339
340//===----------------------------------------------------------------------===//
341// Constructors and destructors.
342
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000343ArchSpec::ArchSpec() :
Greg Clayton514487e2011-02-15 21:59:32 +0000344 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000345 m_core (kCore_invalid),
346 m_byte_order (eByteOrderInvalid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000347{
348}
349
Greg Claytoneb0103f2011-04-07 22:46:35 +0000350ArchSpec::ArchSpec (const char *triple_cstr, Platform *platform) :
Greg Clayton514487e2011-02-15 21:59:32 +0000351 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000352 m_core (kCore_invalid),
353 m_byte_order (eByteOrderInvalid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000354{
Greg Clayton64195a22011-02-23 00:35:02 +0000355 if (triple_cstr)
Greg Claytoneb0103f2011-04-07 22:46:35 +0000356 SetTriple(triple_cstr, platform);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000357}
358
Greg Clayton70512312012-05-08 01:45:38 +0000359
360ArchSpec::ArchSpec (const char *triple_cstr) :
361 m_triple (),
362 m_core (kCore_invalid),
363 m_byte_order (eByteOrderInvalid)
364{
365 if (triple_cstr)
366 SetTriple(triple_cstr);
367}
368
Greg Clayton64195a22011-02-23 00:35:02 +0000369ArchSpec::ArchSpec(const llvm::Triple &triple) :
Greg Clayton514487e2011-02-15 21:59:32 +0000370 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000371 m_core (kCore_invalid),
372 m_byte_order (eByteOrderInvalid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000373{
Greg Clayton64195a22011-02-23 00:35:02 +0000374 SetTriple(triple);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000375}
376
Greg Claytone0d378b2011-03-24 21:19:54 +0000377ArchSpec::ArchSpec (ArchitectureType arch_type, uint32_t cpu, uint32_t subtype) :
Greg Clayton64195a22011-02-23 00:35:02 +0000378 m_triple (),
379 m_core (kCore_invalid),
380 m_byte_order (eByteOrderInvalid)
381{
382 SetArchitecture (arch_type, cpu, subtype);
383}
384
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000385ArchSpec::~ArchSpec()
386{
387}
388
Greg Clayton64195a22011-02-23 00:35:02 +0000389//===----------------------------------------------------------------------===//
390// Assignment and initialization.
391
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000392const ArchSpec&
393ArchSpec::operator= (const ArchSpec& rhs)
394{
395 if (this != &rhs)
396 {
Greg Clayton514487e2011-02-15 21:59:32 +0000397 m_triple = rhs.m_triple;
Greg Clayton64195a22011-02-23 00:35:02 +0000398 m_core = rhs.m_core;
Greg Clayton514487e2011-02-15 21:59:32 +0000399 m_byte_order = rhs.m_byte_order;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000400 }
401 return *this;
402}
403
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000404void
405ArchSpec::Clear()
406{
Greg Clayton514487e2011-02-15 21:59:32 +0000407 m_triple = llvm::Triple();
Greg Clayton64195a22011-02-23 00:35:02 +0000408 m_core = kCore_invalid;
409 m_byte_order = eByteOrderInvalid;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000410}
411
Greg Clayton64195a22011-02-23 00:35:02 +0000412//===----------------------------------------------------------------------===//
413// Predicates.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000414
Greg Clayton41f92322010-06-11 03:25:34 +0000415
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000416const char *
Greg Clayton64195a22011-02-23 00:35:02 +0000417ArchSpec::GetArchitectureName () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000418{
Greg Clayton64195a22011-02-23 00:35:02 +0000419 const CoreDefinition *core_def = FindCoreDefinition (m_core);
420 if (core_def)
421 return core_def->name;
422 return "unknown";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000423}
424
Greg Clayton64195a22011-02-23 00:35:02 +0000425uint32_t
426ArchSpec::GetMachOCPUType () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000427{
Greg Clayton64195a22011-02-23 00:35:02 +0000428 const CoreDefinition *core_def = FindCoreDefinition (m_core);
429 if (core_def)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000430 {
Greg Clayton64195a22011-02-23 00:35:02 +0000431 const ArchDefinitionEntry *arch_def = FindArchDefinitionEntry (&g_macho_arch_def, core_def->core);
432 if (arch_def)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000433 {
Greg Clayton64195a22011-02-23 00:35:02 +0000434 return arch_def->cpu;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000435 }
436 }
Greg Clayton64195a22011-02-23 00:35:02 +0000437 return LLDB_INVALID_CPUTYPE;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000438}
439
Greg Clayton64195a22011-02-23 00:35:02 +0000440uint32_t
441ArchSpec::GetMachOCPUSubType () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000442{
Greg Clayton64195a22011-02-23 00:35:02 +0000443 const CoreDefinition *core_def = FindCoreDefinition (m_core);
444 if (core_def)
445 {
446 const ArchDefinitionEntry *arch_def = FindArchDefinitionEntry (&g_macho_arch_def, core_def->core);
447 if (arch_def)
448 {
Greg Clayton1cb64962011-03-24 04:28:38 +0000449 return arch_def->sub;
Greg Clayton64195a22011-02-23 00:35:02 +0000450 }
451 }
452 return LLDB_INVALID_CPUTYPE;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000453}
454
Greg Clayton64195a22011-02-23 00:35:02 +0000455llvm::Triple::ArchType
456ArchSpec::GetMachine () const
457{
458 const CoreDefinition *core_def = FindCoreDefinition (m_core);
459 if (core_def)
460 return core_def->machine;
461
462 return llvm::Triple::UnknownArch;
463}
464
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000465uint32_t
466ArchSpec::GetAddressByteSize() const
467{
Greg Clayton64195a22011-02-23 00:35:02 +0000468 const CoreDefinition *core_def = FindCoreDefinition (m_core);
469 if (core_def)
470 return core_def->addr_byte_size;
Greg Clayton41f92322010-06-11 03:25:34 +0000471 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000472}
473
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000474ByteOrder
475ArchSpec::GetDefaultEndian () const
476{
Greg Clayton64195a22011-02-23 00:35:02 +0000477 const CoreDefinition *core_def = FindCoreDefinition (m_core);
478 if (core_def)
479 return core_def->default_byte_order;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000480 return eByteOrderInvalid;
481}
482
Greg Clayton64195a22011-02-23 00:35:02 +0000483lldb::ByteOrder
484ArchSpec::GetByteOrder () const
485{
486 if (m_byte_order == eByteOrderInvalid)
487 return GetDefaultEndian();
488 return m_byte_order;
489}
490
491//===----------------------------------------------------------------------===//
492// Mutators.
493
494bool
495ArchSpec::SetTriple (const llvm::Triple &triple)
496{
497 m_triple = triple;
498
499 llvm::StringRef arch_name (m_triple.getArchName());
500 const CoreDefinition *core_def = FindCoreDefinition (arch_name);
501 if (core_def)
502 {
503 m_core = core_def->core;
Greg Claytoneb0103f2011-04-07 22:46:35 +0000504 // Set the byte order to the default byte order for an architecture.
505 // This can be modified if needed for cases when cores handle both
506 // big and little endian
507 m_byte_order = core_def->default_byte_order;
Greg Clayton64195a22011-02-23 00:35:02 +0000508 }
509 else
510 {
511 Clear();
512 }
513
514
515 return IsValid();
516}
517
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000518static bool
519ParseMachCPUDashSubtypeTriple (const char *triple_cstr, ArchSpec &arch)
520{
521 // Accept "12-10" or "12.10" as cpu type/subtype
522 if (isdigit(triple_cstr[0]))
523 {
524 char *end = NULL;
525 errno = 0;
Greg Claytonc7bece562013-01-25 18:06:21 +0000526 uint32_t cpu = (uint32_t)::strtoul (triple_cstr, &end, 0);
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000527 if (errno == 0 && cpu != 0 && end && ((*end == '-') || (*end == '.')))
528 {
529 errno = 0;
Greg Claytonc7bece562013-01-25 18:06:21 +0000530 uint32_t sub = (uint32_t)::strtoul (end + 1, &end, 0);
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000531 if (errno == 0 && end && ((*end == '-') || (*end == '.') || (*end == '\0')))
532 {
533 if (arch.SetArchitecture (eArchTypeMachO, cpu, sub))
534 {
535 if (*end == '-')
536 {
537 llvm::StringRef vendor_os (end + 1);
538 size_t dash_pos = vendor_os.find('-');
539 if (dash_pos != llvm::StringRef::npos)
540 {
541 llvm::StringRef vendor_str(vendor_os.substr(0, dash_pos));
542 arch.GetTriple().setVendorName(vendor_str);
543 const size_t vendor_start_pos = dash_pos+1;
Greg Claytonc7bece562013-01-25 18:06:21 +0000544 dash_pos = vendor_os.find('-', vendor_start_pos);
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000545 if (dash_pos == llvm::StringRef::npos)
546 {
547 if (vendor_start_pos < vendor_os.size())
548 arch.GetTriple().setOSName(vendor_os.substr(vendor_start_pos));
549 }
550 else
551 {
552 arch.GetTriple().setOSName(vendor_os.substr(vendor_start_pos, dash_pos - vendor_start_pos));
553 }
554 }
555 }
556 return true;
557 }
558 }
559 }
560 }
561 return false;
562}
Greg Clayton64195a22011-02-23 00:35:02 +0000563bool
Greg Clayton70512312012-05-08 01:45:38 +0000564ArchSpec::SetTriple (const char *triple_cstr)
Greg Clayton64195a22011-02-23 00:35:02 +0000565{
Greg Clayton23aca092011-08-12 23:32:52 +0000566 if (triple_cstr && triple_cstr[0])
Greg Clayton64195a22011-02-23 00:35:02 +0000567 {
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000568 if (ParseMachCPUDashSubtypeTriple (triple_cstr, *this))
569 return true;
570
Greg Clayton64195a22011-02-23 00:35:02 +0000571 llvm::StringRef triple_stref (triple_cstr);
572 if (triple_stref.startswith (LLDB_ARCH_DEFAULT))
573 {
574 // Special case for the current host default architectures...
575 if (triple_stref.equals (LLDB_ARCH_DEFAULT_32BIT))
576 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture32);
577 else if (triple_stref.equals (LLDB_ARCH_DEFAULT_64BIT))
578 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture64);
579 else if (triple_stref.equals (LLDB_ARCH_DEFAULT))
580 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture);
581 }
582 else
583 {
584 std::string normalized_triple_sstr (llvm::Triple::normalize(triple_stref));
585 triple_stref = normalized_triple_sstr;
Greg Clayton70512312012-05-08 01:45:38 +0000586 SetTriple (llvm::Triple (triple_stref));
587 }
588 }
589 else
590 Clear();
591 return IsValid();
592}
593
594bool
595ArchSpec::SetTriple (const char *triple_cstr, Platform *platform)
596{
597 if (triple_cstr && triple_cstr[0])
598 {
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000599 if (ParseMachCPUDashSubtypeTriple (triple_cstr, *this))
600 return true;
601
Greg Clayton70512312012-05-08 01:45:38 +0000602 llvm::StringRef triple_stref (triple_cstr);
603 if (triple_stref.startswith (LLDB_ARCH_DEFAULT))
604 {
605 // Special case for the current host default architectures...
606 if (triple_stref.equals (LLDB_ARCH_DEFAULT_32BIT))
607 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture32);
608 else if (triple_stref.equals (LLDB_ARCH_DEFAULT_64BIT))
609 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture64);
610 else if (triple_stref.equals (LLDB_ARCH_DEFAULT))
611 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture);
612 }
613 else
614 {
615 ArchSpec raw_arch (triple_cstr);
616
617 std::string normalized_triple_sstr (llvm::Triple::normalize(triple_stref));
618 triple_stref = normalized_triple_sstr;
Greg Claytoneb0103f2011-04-07 22:46:35 +0000619 llvm::Triple normalized_triple (triple_stref);
620
621 const bool os_specified = normalized_triple.getOSName().size() > 0;
622 const bool vendor_specified = normalized_triple.getVendorName().size() > 0;
623 const bool env_specified = normalized_triple.getEnvironmentName().size() > 0;
624
625 // If we got an arch only, then default the vendor, os, environment
626 // to match the platform if one is supplied
627 if (!(os_specified || vendor_specified || env_specified))
628 {
629 if (platform)
630 {
631 // If we were given a platform, use the platform's system
632 // architecture. If this is not available (might not be
633 // connected) use the first supported architecture.
Greg Clayton70512312012-05-08 01:45:38 +0000634 ArchSpec compatible_arch;
Greg Clayton1e0c8842013-01-11 20:49:54 +0000635 if (platform->IsCompatibleArchitecture (raw_arch, false, &compatible_arch))
Greg Claytoneb0103f2011-04-07 22:46:35 +0000636 {
Greg Clayton70512312012-05-08 01:45:38 +0000637 if (compatible_arch.IsValid())
638 {
639 const llvm::Triple &compatible_triple = compatible_arch.GetTriple();
640 if (!vendor_specified)
641 normalized_triple.setVendor(compatible_triple.getVendor());
642 if (!os_specified)
643 normalized_triple.setOS(compatible_triple.getOS());
644 if (!env_specified && compatible_triple.getEnvironmentName().size())
645 normalized_triple.setEnvironment(compatible_triple.getEnvironment());
646 }
Greg Claytoneb0103f2011-04-07 22:46:35 +0000647 }
Greg Clayton70512312012-05-08 01:45:38 +0000648 else
Greg Claytoneb0103f2011-04-07 22:46:35 +0000649 {
Greg Clayton70512312012-05-08 01:45:38 +0000650 *this = raw_arch;
651 return IsValid();
Greg Claytoneb0103f2011-04-07 22:46:35 +0000652 }
653 }
654 else
655 {
656 // No platform specified, fall back to the host system for
657 // the default vendor, os, and environment.
Sean Callananbfb237bc2011-11-04 22:46:46 +0000658 llvm::Triple host_triple(llvm::sys::getDefaultTargetTriple());
Greg Clayton70512312012-05-08 01:45:38 +0000659 if (!vendor_specified)
660 normalized_triple.setVendor(host_triple.getVendor());
661 if (!vendor_specified)
662 normalized_triple.setOS(host_triple.getOS());
663 if (!env_specified && host_triple.getEnvironmentName().size())
664 normalized_triple.setEnvironment(host_triple.getEnvironment());
Greg Claytoneb0103f2011-04-07 22:46:35 +0000665 }
666 }
667 SetTriple (normalized_triple);
Greg Clayton64195a22011-02-23 00:35:02 +0000668 }
669 }
670 else
671 Clear();
672 return IsValid();
673}
674
Greg Clayton64195a22011-02-23 00:35:02 +0000675bool
Greg Claytone0d378b2011-03-24 21:19:54 +0000676ArchSpec::SetArchitecture (ArchitectureType arch_type, uint32_t cpu, uint32_t sub)
Greg Clayton64195a22011-02-23 00:35:02 +0000677{
678 m_core = kCore_invalid;
679 bool update_triple = true;
680 const ArchDefinition *arch_def = FindArchDefinition(arch_type);
681 if (arch_def)
682 {
683 const ArchDefinitionEntry *arch_def_entry = FindArchDefinitionEntry (arch_def, cpu, sub);
684 if (arch_def_entry)
685 {
686 const CoreDefinition *core_def = FindCoreDefinition (arch_def_entry->core);
687 if (core_def)
688 {
689 m_core = core_def->core;
690 update_triple = false;
Greg Clayton593577a2011-09-21 03:57:31 +0000691 // Always use the architecture name because it might be more descriptive
692 // than the architecture enum ("armv7" -> llvm::Triple::arm).
693 m_triple.setArchName(llvm::StringRef(core_def->name));
Greg Clayton64195a22011-02-23 00:35:02 +0000694 if (arch_type == eArchTypeMachO)
695 {
696 m_triple.setVendor (llvm::Triple::Apple);
Greg Clayton70512312012-05-08 01:45:38 +0000697
698 switch (core_def->machine)
699 {
700 case llvm::Triple::arm:
701 case llvm::Triple::thumb:
702 m_triple.setOS (llvm::Triple::IOS);
703 break;
704
705 case llvm::Triple::x86:
706 case llvm::Triple::x86_64:
707 default:
708 m_triple.setOS (llvm::Triple::MacOSX);
709 break;
710 }
Greg Clayton64195a22011-02-23 00:35:02 +0000711 }
712 else
713 {
714 m_triple.setVendor (llvm::Triple::UnknownVendor);
715 m_triple.setOS (llvm::Triple::UnknownOS);
716 }
Greg Clayton593577a2011-09-21 03:57:31 +0000717 // Fall back onto setting the machine type if the arch by name failed...
718 if (m_triple.getArch () == llvm::Triple::UnknownArch)
719 m_triple.setArch (core_def->machine);
Greg Clayton64195a22011-02-23 00:35:02 +0000720 }
721 }
722 }
723 CoreUpdated(update_triple);
724 return IsValid();
725}
726
Greg Clayton357132e2011-03-26 19:14:58 +0000727uint32_t
728ArchSpec::GetMinimumOpcodeByteSize() const
Greg Clayton64195a22011-02-23 00:35:02 +0000729{
Greg Clayton357132e2011-03-26 19:14:58 +0000730 const CoreDefinition *core_def = FindCoreDefinition (m_core);
731 if (core_def)
732 return core_def->min_opcode_byte_size;
733 return 0;
734}
735
736uint32_t
737ArchSpec::GetMaximumOpcodeByteSize() const
738{
739 const CoreDefinition *core_def = FindCoreDefinition (m_core);
740 if (core_def)
741 return core_def->max_opcode_byte_size;
742 return 0;
Greg Clayton64195a22011-02-23 00:35:02 +0000743}
744
Jason Molendaba813dc2012-11-04 03:20:05 +0000745bool
746ArchSpec::IsExactMatch (const ArchSpec& rhs) const
747{
Sean Callananbf4b7be2012-12-13 22:07:14 +0000748 return IsEqualTo (rhs, true);
Jason Molendaba813dc2012-11-04 03:20:05 +0000749}
750
751bool
752ArchSpec::IsCompatibleMatch (const ArchSpec& rhs) const
753{
Sean Callananbf4b7be2012-12-13 22:07:14 +0000754 return IsEqualTo (rhs, false);
Jason Molendaba813dc2012-11-04 03:20:05 +0000755}
756
757bool
Sean Callananbf4b7be2012-12-13 22:07:14 +0000758ArchSpec::IsEqualTo (const ArchSpec& rhs, bool exact_match) const
Jason Molendaba813dc2012-11-04 03:20:05 +0000759{
760 if (GetByteOrder() != rhs.GetByteOrder())
761 return false;
762
763 const ArchSpec::Core lhs_core = GetCore ();
764 const ArchSpec::Core rhs_core = rhs.GetCore ();
765
766 const bool core_match = cores_match (lhs_core, rhs_core, true, exact_match);
767
768 if (core_match)
769 {
770 const llvm::Triple &lhs_triple = GetTriple();
771 const llvm::Triple &rhs_triple = rhs.GetTriple();
772
773 const llvm::Triple::VendorType lhs_triple_vendor = lhs_triple.getVendor();
774 const llvm::Triple::VendorType rhs_triple_vendor = rhs_triple.getVendor();
775 if (lhs_triple_vendor != rhs_triple_vendor)
776 {
Sean Callananbf4b7be2012-12-13 22:07:14 +0000777 if (exact_match)
778 {
779 const bool rhs_vendor_specified = rhs.TripleVendorWasSpecified();
780 const bool lhs_vendor_specified = TripleVendorWasSpecified();
781 // Both architectures had the vendor specified, so if they aren't
782 // equal then we return false
783 if (rhs_vendor_specified && lhs_vendor_specified)
784 return false;
785 }
Jason Molendaba813dc2012-11-04 03:20:05 +0000786
787 // Only fail if both vendor types are not unknown
788 if (lhs_triple_vendor != llvm::Triple::UnknownVendor &&
789 rhs_triple_vendor != llvm::Triple::UnknownVendor)
790 return false;
791 }
792
793 const llvm::Triple::OSType lhs_triple_os = lhs_triple.getOS();
794 const llvm::Triple::OSType rhs_triple_os = rhs_triple.getOS();
795 if (lhs_triple_os != rhs_triple_os)
796 {
Sean Callananbf4b7be2012-12-13 22:07:14 +0000797 if (exact_match)
798 {
799 const bool rhs_os_specified = rhs.TripleOSWasSpecified();
800 const bool lhs_os_specified = TripleOSWasSpecified();
801 // Both architectures had the OS specified, so if they aren't
802 // equal then we return false
803 if (rhs_os_specified && lhs_os_specified)
804 return false;
805 }
Jason Molendaba813dc2012-11-04 03:20:05 +0000806 // Only fail if both os types are not unknown
807 if (lhs_triple_os != llvm::Triple::UnknownOS &&
808 rhs_triple_os != llvm::Triple::UnknownOS)
809 return false;
810 }
811
812 const llvm::Triple::EnvironmentType lhs_triple_env = lhs_triple.getEnvironment();
813 const llvm::Triple::EnvironmentType rhs_triple_env = rhs_triple.getEnvironment();
814
815 if (lhs_triple_env != rhs_triple_env)
816 {
817 // Only fail if both environment types are not unknown
818 if (lhs_triple_env != llvm::Triple::UnknownEnvironment &&
819 rhs_triple_env != llvm::Triple::UnknownEnvironment)
820 return false;
821 }
822 return true;
823 }
824 return false;
825}
826
Greg Clayton64195a22011-02-23 00:35:02 +0000827//===----------------------------------------------------------------------===//
828// Helper methods.
829
830void
831ArchSpec::CoreUpdated (bool update_triple)
832{
833 const CoreDefinition *core_def = FindCoreDefinition (m_core);
834 if (core_def)
835 {
836 if (update_triple)
837 m_triple = llvm::Triple(core_def->name, "unknown", "unknown");
838 m_byte_order = core_def->default_byte_order;
839 }
840 else
841 {
842 if (update_triple)
843 m_triple = llvm::Triple();
844 m_byte_order = eByteOrderInvalid;
845 }
846}
847
848//===----------------------------------------------------------------------===//
849// Operators.
850
Greg Clayton70512312012-05-08 01:45:38 +0000851static bool
Jason Molendaba813dc2012-11-04 03:20:05 +0000852cores_match (const ArchSpec::Core core1, const ArchSpec::Core core2, bool try_inverse, bool enforce_exact_match)
Greg Clayton70512312012-05-08 01:45:38 +0000853{
Jason Molendaba813dc2012-11-04 03:20:05 +0000854 if (core1 == core2)
855 return true;
856
Greg Clayton70512312012-05-08 01:45:38 +0000857 switch (core1)
858 {
Greg Clayton70512312012-05-08 01:45:38 +0000859 case ArchSpec::kCore_any:
860 return true;
861
862 case ArchSpec::kCore_arm_any:
863 if (core2 >= ArchSpec::kCore_arm_first && core2 <= ArchSpec::kCore_arm_last)
864 return true;
865 if (core2 >= ArchSpec::kCore_thumb_first && core2 <= ArchSpec::kCore_thumb_last)
866 return true;
867 if (core2 == ArchSpec::kCore_arm_any)
868 return true;
869 break;
870
871 case ArchSpec::kCore_x86_32_any:
872 if ((core2 >= ArchSpec::kCore_x86_32_first && core2 <= ArchSpec::kCore_x86_32_last) || (core2 == ArchSpec::kCore_x86_32_any))
873 return true;
874 break;
875
876 case ArchSpec::kCore_ppc_any:
877 if ((core2 >= ArchSpec::kCore_ppc_first && core2 <= ArchSpec::kCore_ppc_last) || (core2 == ArchSpec::kCore_ppc_any))
878 return true;
879 break;
880
881 case ArchSpec::kCore_ppc64_any:
882 if ((core2 >= ArchSpec::kCore_ppc64_first && core2 <= ArchSpec::kCore_ppc64_last) || (core2 == ArchSpec::kCore_ppc64_any))
883 return true;
884 break;
885
Jason Molenda7a1559c2013-03-08 01:20:17 +0000886 case ArchSpec::eCore_arm_armv7m:
887 case ArchSpec::eCore_arm_armv7em:
Johnny Chen1083b0d2012-08-28 22:53:40 +0000888 case ArchSpec::eCore_arm_armv7f:
889 case ArchSpec::eCore_arm_armv7k:
890 case ArchSpec::eCore_arm_armv7s:
Jason Molendaba813dc2012-11-04 03:20:05 +0000891 if (!enforce_exact_match)
892 {
893 try_inverse = false;
894 if (core2 == ArchSpec::eCore_arm_armv7)
895 return true;
896 }
Johnny Chen1083b0d2012-08-28 22:53:40 +0000897 break;
898
Greg Clayton70512312012-05-08 01:45:38 +0000899 default:
900 break;
901 }
902 if (try_inverse)
Jason Molendaba813dc2012-11-04 03:20:05 +0000903 return cores_match (core2, core1, false, enforce_exact_match);
Greg Clayton70512312012-05-08 01:45:38 +0000904 return false;
905}
906
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000907bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000908lldb_private::operator<(const ArchSpec& lhs, const ArchSpec& rhs)
909{
Greg Clayton64195a22011-02-23 00:35:02 +0000910 const ArchSpec::Core lhs_core = lhs.GetCore ();
911 const ArchSpec::Core rhs_core = rhs.GetCore ();
912 return lhs_core < rhs_core;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000913}