blob: e19341bc6a9e92c3911cc3e030bd80cec3bca25a [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" },
Jason Molendaa3a04522013-09-27 23:21:54 +000059 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv6m , "armv6m" },
Greg Clayton357132e2011-03-26 19:14:58 +000060 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7 , "armv7" },
61 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7f , "armv7f" },
Greg Clayton357132e2011-03-26 19:14:58 +000062 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7s , "armv7s" },
Jason Molenda7a1559c2013-03-08 01:20:17 +000063 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7k , "armv7k" },
64 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7m , "armv7m" },
65 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7em , "armv7em" },
Greg Clayton357132e2011-03-26 19:14:58 +000066 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_xscale , "xscale" },
Greg Claytonb5c39fe2011-12-16 18:15:52 +000067 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumb , "thumb" },
68 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv4t , "thumbv4t" },
69 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv5 , "thumbv5" },
70 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv5e , "thumbv5e" },
71 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv6 , "thumbv6" },
Jason Molendaa3a04522013-09-27 23:21:54 +000072 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv6m , "thumbv6m" },
Greg Claytonb5c39fe2011-12-16 18:15:52 +000073 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7 , "thumbv7" },
74 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7f , "thumbv7f" },
Greg Claytonb5c39fe2011-12-16 18:15:52 +000075 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7s , "thumbv7s" },
Jason Molenda7a1559c2013-03-08 01:20:17 +000076 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7k , "thumbv7k" },
77 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7m , "thumbv7m" },
78 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7em , "thumbv7em" },
Greg Claytonb5c39fe2011-12-16 18:15:52 +000079
Greg Clayton64195a22011-02-23 00:35:02 +000080
Greg Clayton83b162d2013-08-12 18:34:04 +000081 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_generic , "ppc" },
82 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc601 , "ppc601" },
83 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc602 , "ppc602" },
84 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc603 , "ppc603" },
85 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc603e , "ppc603e" },
86 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc603ev , "ppc603ev" },
87 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc604 , "ppc604" },
88 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc604e , "ppc604e" },
89 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc620 , "ppc620" },
90 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc750 , "ppc750" },
91 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc7400 , "ppc7400" },
92 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc7450 , "ppc7450" },
93 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc970 , "ppc970" },
Greg Clayton64195a22011-02-23 00:35:02 +000094
Greg Clayton83b162d2013-08-12 18:34:04 +000095 { eByteOrderBig , 8, 4, 4, llvm::Triple::ppc64 , ArchSpec::eCore_ppc64_generic , "ppc64" },
96 { eByteOrderBig , 8, 4, 4, llvm::Triple::ppc64 , ArchSpec::eCore_ppc64_ppc970_64 , "ppc970-64" },
Greg Clayton64195a22011-02-23 00:35:02 +000097
Greg Clayton357132e2011-03-26 19:14:58 +000098 { eByteOrderLittle, 4, 4, 4, llvm::Triple::sparc , ArchSpec::eCore_sparc_generic , "sparc" },
99 { eByteOrderLittle, 8, 4, 4, llvm::Triple::sparcv9, ArchSpec::eCore_sparc9_generic , "sparcv9" },
Greg Clayton64195a22011-02-23 00:35:02 +0000100
Greg Claytonab65b342011-04-13 22:47:15 +0000101 { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i386 , "i386" },
102 { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i486 , "i486" },
103 { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i486sx , "i486sx" },
Greg Clayton64195a22011-02-23 00:35:02 +0000104
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000105 { eByteOrderLittle, 8, 1, 15, llvm::Triple::x86_64 , ArchSpec::eCore_x86_64_x86_64 , "x86_64" },
106 { eByteOrderLittle, 4, 4, 4 , llvm::Triple::UnknownArch , ArchSpec::eCore_uknownMach32 , "unknown-mach-32" },
107 { eByteOrderLittle, 8, 4, 4 , llvm::Triple::UnknownArch , ArchSpec::eCore_uknownMach64 , "unknown-mach-64" }
Greg Clayton64195a22011-02-23 00:35:02 +0000108};
109
110struct ArchDefinitionEntry
111{
112 ArchSpec::Core core;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000113 uint32_t cpu;
114 uint32_t sub;
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000115 uint32_t cpu_mask;
116 uint32_t sub_mask;
Greg Clayton64195a22011-02-23 00:35:02 +0000117};
118
119struct ArchDefinition
120{
121 ArchitectureType type;
122 size_t num_entries;
123 const ArchDefinitionEntry *entries;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000124 const char *name;
125};
126
Greg Clayton41f92322010-06-11 03:25:34 +0000127
Greg Claytonc7bece562013-01-25 18:06:21 +0000128size_t
Greg Claytonab65b342011-04-13 22:47:15 +0000129ArchSpec::AutoComplete (const char *name, StringList &matches)
130{
131 uint32_t i;
132 if (name && name[0])
133 {
134 for (i = 0; i < ArchSpec::kNumCores; ++i)
135 {
136 if (NameMatches(g_core_definitions[i].name, eNameMatchStartsWith, name))
137 matches.AppendString (g_core_definitions[i].name);
138 }
139 }
140 else
141 {
142 for (i = 0; i < ArchSpec::kNumCores; ++i)
143 matches.AppendString (g_core_definitions[i].name);
144 }
145 return matches.GetSize();
146}
147
148
149
Greg Clayton64195a22011-02-23 00:35:02 +0000150#define CPU_ANY (UINT32_MAX)
151
152//===----------------------------------------------------------------------===//
153// A table that gets searched linearly for matches. This table is used to
154// convert cpu type and subtypes to architecture names, and to convert
155// architecture names to cpu types and subtypes. The ordering is important and
156// allows the precedence to be set when the table is built.
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000157#define SUBTYPE_MASK 0x00FFFFFFu
Greg Clayton64195a22011-02-23 00:35:02 +0000158static const ArchDefinitionEntry g_macho_arch_entries[] =
Greg Clayton41f92322010-06-11 03:25:34 +0000159{
Charles Davis510938e2013-08-27 05:04:57 +0000160 { ArchSpec::eCore_arm_generic , llvm::MachO::CPU_TYPE_ARM , CPU_ANY, UINT32_MAX , UINT32_MAX },
161 { ArchSpec::eCore_arm_generic , llvm::MachO::CPU_TYPE_ARM , 0 , UINT32_MAX , SUBTYPE_MASK },
162 { ArchSpec::eCore_arm_armv4 , llvm::MachO::CPU_TYPE_ARM , 5 , UINT32_MAX , SUBTYPE_MASK },
163 { ArchSpec::eCore_arm_armv4t , llvm::MachO::CPU_TYPE_ARM , 5 , UINT32_MAX , SUBTYPE_MASK },
164 { ArchSpec::eCore_arm_armv6 , llvm::MachO::CPU_TYPE_ARM , 6 , UINT32_MAX , SUBTYPE_MASK },
165 { ArchSpec::eCore_arm_armv5 , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
166 { ArchSpec::eCore_arm_armv5e , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
167 { ArchSpec::eCore_arm_armv5t , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
168 { ArchSpec::eCore_arm_xscale , llvm::MachO::CPU_TYPE_ARM , 8 , UINT32_MAX , SUBTYPE_MASK },
169 { ArchSpec::eCore_arm_armv7 , llvm::MachO::CPU_TYPE_ARM , 9 , UINT32_MAX , SUBTYPE_MASK },
170 { ArchSpec::eCore_arm_armv7f , llvm::MachO::CPU_TYPE_ARM , 10 , UINT32_MAX , SUBTYPE_MASK },
171 { ArchSpec::eCore_arm_armv7s , llvm::MachO::CPU_TYPE_ARM , 11 , UINT32_MAX , SUBTYPE_MASK },
172 { ArchSpec::eCore_arm_armv7k , llvm::MachO::CPU_TYPE_ARM , 12 , UINT32_MAX , SUBTYPE_MASK },
173 { ArchSpec::eCore_arm_armv7m , llvm::MachO::CPU_TYPE_ARM , 15 , UINT32_MAX , SUBTYPE_MASK },
174 { ArchSpec::eCore_arm_armv7em , llvm::MachO::CPU_TYPE_ARM , 16 , UINT32_MAX , SUBTYPE_MASK },
175 { ArchSpec::eCore_thumb , llvm::MachO::CPU_TYPE_ARM , 0 , UINT32_MAX , SUBTYPE_MASK },
176 { ArchSpec::eCore_thumbv4t , llvm::MachO::CPU_TYPE_ARM , 5 , UINT32_MAX , SUBTYPE_MASK },
177 { ArchSpec::eCore_thumbv5 , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
178 { ArchSpec::eCore_thumbv5e , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
179 { ArchSpec::eCore_thumbv6 , llvm::MachO::CPU_TYPE_ARM , 6 , UINT32_MAX , SUBTYPE_MASK },
180 { ArchSpec::eCore_thumbv7 , llvm::MachO::CPU_TYPE_ARM , 9 , UINT32_MAX , SUBTYPE_MASK },
181 { ArchSpec::eCore_thumbv7f , llvm::MachO::CPU_TYPE_ARM , 10 , UINT32_MAX , SUBTYPE_MASK },
182 { ArchSpec::eCore_thumbv7s , llvm::MachO::CPU_TYPE_ARM , 11 , UINT32_MAX , SUBTYPE_MASK },
183 { ArchSpec::eCore_thumbv7k , llvm::MachO::CPU_TYPE_ARM , 12 , UINT32_MAX , SUBTYPE_MASK },
184 { ArchSpec::eCore_thumbv7m , llvm::MachO::CPU_TYPE_ARM , 15 , UINT32_MAX , SUBTYPE_MASK },
185 { ArchSpec::eCore_thumbv7em , llvm::MachO::CPU_TYPE_ARM , 16 , UINT32_MAX , SUBTYPE_MASK },
186 { ArchSpec::eCore_ppc_generic , llvm::MachO::CPU_TYPE_POWERPC , CPU_ANY, UINT32_MAX , UINT32_MAX },
187 { ArchSpec::eCore_ppc_generic , llvm::MachO::CPU_TYPE_POWERPC , 0 , UINT32_MAX , SUBTYPE_MASK },
188 { ArchSpec::eCore_ppc_ppc601 , llvm::MachO::CPU_TYPE_POWERPC , 1 , UINT32_MAX , SUBTYPE_MASK },
189 { ArchSpec::eCore_ppc_ppc602 , llvm::MachO::CPU_TYPE_POWERPC , 2 , UINT32_MAX , SUBTYPE_MASK },
190 { ArchSpec::eCore_ppc_ppc603 , llvm::MachO::CPU_TYPE_POWERPC , 3 , UINT32_MAX , SUBTYPE_MASK },
191 { ArchSpec::eCore_ppc_ppc603e , llvm::MachO::CPU_TYPE_POWERPC , 4 , UINT32_MAX , SUBTYPE_MASK },
192 { ArchSpec::eCore_ppc_ppc603ev , llvm::MachO::CPU_TYPE_POWERPC , 5 , UINT32_MAX , SUBTYPE_MASK },
193 { ArchSpec::eCore_ppc_ppc604 , llvm::MachO::CPU_TYPE_POWERPC , 6 , UINT32_MAX , SUBTYPE_MASK },
194 { ArchSpec::eCore_ppc_ppc604e , llvm::MachO::CPU_TYPE_POWERPC , 7 , UINT32_MAX , SUBTYPE_MASK },
195 { ArchSpec::eCore_ppc_ppc620 , llvm::MachO::CPU_TYPE_POWERPC , 8 , UINT32_MAX , SUBTYPE_MASK },
196 { ArchSpec::eCore_ppc_ppc750 , llvm::MachO::CPU_TYPE_POWERPC , 9 , UINT32_MAX , SUBTYPE_MASK },
197 { ArchSpec::eCore_ppc_ppc7400 , llvm::MachO::CPU_TYPE_POWERPC , 10 , UINT32_MAX , SUBTYPE_MASK },
198 { ArchSpec::eCore_ppc_ppc7450 , llvm::MachO::CPU_TYPE_POWERPC , 11 , UINT32_MAX , SUBTYPE_MASK },
199 { ArchSpec::eCore_ppc_ppc970 , llvm::MachO::CPU_TYPE_POWERPC , 100 , UINT32_MAX , SUBTYPE_MASK },
200 { ArchSpec::eCore_ppc64_generic , llvm::MachO::CPU_TYPE_POWERPC64 , 0 , UINT32_MAX , SUBTYPE_MASK },
201 { ArchSpec::eCore_ppc64_ppc970_64 , llvm::MachO::CPU_TYPE_POWERPC64 , 100 , UINT32_MAX , SUBTYPE_MASK },
202 { ArchSpec::eCore_x86_32_i386 , llvm::MachO::CPU_TYPE_I386 , 3 , UINT32_MAX , SUBTYPE_MASK },
203 { ArchSpec::eCore_x86_32_i486 , llvm::MachO::CPU_TYPE_I386 , 4 , UINT32_MAX , SUBTYPE_MASK },
204 { ArchSpec::eCore_x86_32_i486sx , llvm::MachO::CPU_TYPE_I386 , 0x84 , UINT32_MAX , SUBTYPE_MASK },
205 { ArchSpec::eCore_x86_32_i386 , llvm::MachO::CPU_TYPE_I386 , CPU_ANY, UINT32_MAX , UINT32_MAX },
206 { ArchSpec::eCore_x86_64_x86_64 , llvm::MachO::CPU_TYPE_X86_64 , 3 , UINT32_MAX , SUBTYPE_MASK },
207 { ArchSpec::eCore_x86_64_x86_64 , llvm::MachO::CPU_TYPE_X86_64 , 4 , UINT32_MAX , SUBTYPE_MASK },
208 { 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 +0000209 // Catch any unknown mach architectures so we can always use the object and symbol mach-o files
Charles Davis510938e2013-08-27 05:04:57 +0000210 { ArchSpec::eCore_uknownMach32 , 0 , 0 , 0xFF000000u, 0x00000000u },
211 { ArchSpec::eCore_uknownMach64 , llvm::MachO::CPU_ARCH_ABI64 , 0 , 0xFF000000u, 0x00000000u }
Greg Clayton64195a22011-02-23 00:35:02 +0000212};
213static const ArchDefinition g_macho_arch_def = {
214 eArchTypeMachO,
215 sizeof(g_macho_arch_entries)/sizeof(g_macho_arch_entries[0]),
216 g_macho_arch_entries,
Greg Clayton64195a22011-02-23 00:35:02 +0000217 "mach-o"
Greg Clayton41f92322010-06-11 03:25:34 +0000218};
219
Greg Clayton64195a22011-02-23 00:35:02 +0000220//===----------------------------------------------------------------------===//
221// A table that gets searched linearly for matches. This table is used to
222// convert cpu type and subtypes to architecture names, and to convert
223// architecture names to cpu types and subtypes. The ordering is important and
224// allows the precedence to be set when the table is built.
225static const ArchDefinitionEntry g_elf_arch_entries[] =
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000226{
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000227 { ArchSpec::eCore_sparc_generic , llvm::ELF::EM_SPARC , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // Sparc
228 { ArchSpec::eCore_x86_32_i386 , llvm::ELF::EM_386 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // Intel 80386
229 { ArchSpec::eCore_x86_32_i486 , llvm::ELF::EM_486 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // Intel 486 (deprecated)
230 { ArchSpec::eCore_ppc_generic , llvm::ELF::EM_PPC , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // PowerPC
231 { ArchSpec::eCore_ppc64_generic , llvm::ELF::EM_PPC64 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // PowerPC64
232 { ArchSpec::eCore_arm_generic , llvm::ELF::EM_ARM , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // ARM
233 { ArchSpec::eCore_sparc9_generic , llvm::ELF::EM_SPARCV9, LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // SPARC V9
234 { ArchSpec::eCore_x86_64_x86_64 , llvm::ELF::EM_X86_64 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu } // AMD64
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000235};
236
Greg Clayton64195a22011-02-23 00:35:02 +0000237static const ArchDefinition g_elf_arch_def = {
238 eArchTypeELF,
239 sizeof(g_elf_arch_entries)/sizeof(g_elf_arch_entries[0]),
240 g_elf_arch_entries,
Greg Clayton64195a22011-02-23 00:35:02 +0000241 "elf",
Greg Clayton41f92322010-06-11 03:25:34 +0000242};
243
Charles Davis237ad972013-08-27 05:04:33 +0000244static const ArchDefinitionEntry g_coff_arch_entries[] =
245{
246 { ArchSpec::eCore_x86_32_i386 , llvm::COFF::IMAGE_FILE_MACHINE_I386 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // Intel 80386
247 { ArchSpec::eCore_ppc_generic , llvm::COFF::IMAGE_FILE_MACHINE_POWERPC , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // PowerPC
248 { ArchSpec::eCore_ppc_generic , llvm::COFF::IMAGE_FILE_MACHINE_POWERPCFP, LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // PowerPC (with FPU)
249 { ArchSpec::eCore_arm_generic , llvm::COFF::IMAGE_FILE_MACHINE_ARM , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // ARM
250 { ArchSpec::eCore_arm_armv7 , llvm::COFF::IMAGE_FILE_MACHINE_ARMV7 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // ARMv7
251 { ArchSpec::eCore_thumb , llvm::COFF::IMAGE_FILE_MACHINE_THUMB , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // ARMv7
252 { ArchSpec::eCore_x86_64_x86_64, llvm::COFF::IMAGE_FILE_MACHINE_AMD64 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu } // AMD64
253};
254
255static const ArchDefinition g_coff_arch_def = {
256 eArchTypeCOFF,
257 sizeof(g_coff_arch_entries)/sizeof(g_coff_arch_entries[0]),
258 g_coff_arch_entries,
259 "pe-coff",
260};
261
Greg Clayton64195a22011-02-23 00:35:02 +0000262//===----------------------------------------------------------------------===//
263// Table of all ArchDefinitions
264static const ArchDefinition *g_arch_definitions[] = {
265 &g_macho_arch_def,
Charles Davis237ad972013-08-27 05:04:33 +0000266 &g_elf_arch_def,
267 &g_coff_arch_def
Greg Clayton64195a22011-02-23 00:35:02 +0000268};
Greg Clayton41f92322010-06-11 03:25:34 +0000269
Greg Clayton64195a22011-02-23 00:35:02 +0000270static const size_t k_num_arch_definitions =
271 sizeof(g_arch_definitions) / sizeof(g_arch_definitions[0]);
272
273//===----------------------------------------------------------------------===//
274// Static helper functions.
275
276
277// Get the architecture definition for a given object type.
278static const ArchDefinition *
279FindArchDefinition (ArchitectureType arch_type)
280{
281 for (unsigned int i = 0; i < k_num_arch_definitions; ++i)
282 {
283 const ArchDefinition *def = g_arch_definitions[i];
284 if (def->type == arch_type)
285 return def;
286 }
287 return NULL;
288}
289
290// Get an architecture definition by name.
291static const CoreDefinition *
292FindCoreDefinition (llvm::StringRef name)
293{
294 for (unsigned int i = 0; i < ArchSpec::kNumCores; ++i)
295 {
296 if (name.equals_lower(g_core_definitions[i].name))
297 return &g_core_definitions[i];
298 }
299 return NULL;
300}
301
302static inline const CoreDefinition *
303FindCoreDefinition (ArchSpec::Core core)
304{
305 if (core >= 0 && core < ArchSpec::kNumCores)
306 return &g_core_definitions[core];
307 return NULL;
308}
309
310// Get a definition entry by cpu type and subtype.
311static const ArchDefinitionEntry *
312FindArchDefinitionEntry (const ArchDefinition *def, uint32_t cpu, uint32_t sub)
313{
314 if (def == NULL)
315 return NULL;
316
Greg Clayton64195a22011-02-23 00:35:02 +0000317 const ArchDefinitionEntry *entries = def->entries;
318 for (size_t i = 0; i < def->num_entries; ++i)
319 {
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000320 if (entries[i].cpu == (cpu & entries[i].cpu_mask))
321 if (entries[i].sub == (sub & entries[i].sub_mask))
322 return &entries[i];
Greg Clayton64195a22011-02-23 00:35:02 +0000323 }
324 return NULL;
325}
326
327static const ArchDefinitionEntry *
328FindArchDefinitionEntry (const ArchDefinition *def, ArchSpec::Core core)
329{
330 if (def == NULL)
331 return NULL;
332
333 const ArchDefinitionEntry *entries = def->entries;
334 for (size_t i = 0; i < def->num_entries; ++i)
335 {
336 if (entries[i].core == core)
337 return &entries[i];
338 }
339 return NULL;
340}
341
342//===----------------------------------------------------------------------===//
343// Constructors and destructors.
344
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000345ArchSpec::ArchSpec() :
Greg Clayton514487e2011-02-15 21:59:32 +0000346 m_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}
351
Greg Claytoneb0103f2011-04-07 22:46:35 +0000352ArchSpec::ArchSpec (const char *triple_cstr, Platform *platform) :
Greg Clayton514487e2011-02-15 21:59:32 +0000353 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000354 m_core (kCore_invalid),
355 m_byte_order (eByteOrderInvalid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000356{
Greg Clayton64195a22011-02-23 00:35:02 +0000357 if (triple_cstr)
Greg Claytoneb0103f2011-04-07 22:46:35 +0000358 SetTriple(triple_cstr, platform);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000359}
360
Greg Clayton70512312012-05-08 01:45:38 +0000361
362ArchSpec::ArchSpec (const char *triple_cstr) :
363 m_triple (),
364 m_core (kCore_invalid),
365 m_byte_order (eByteOrderInvalid)
366{
367 if (triple_cstr)
368 SetTriple(triple_cstr);
369}
370
Greg Clayton64195a22011-02-23 00:35:02 +0000371ArchSpec::ArchSpec(const llvm::Triple &triple) :
Greg Clayton514487e2011-02-15 21:59:32 +0000372 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000373 m_core (kCore_invalid),
374 m_byte_order (eByteOrderInvalid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000375{
Greg Clayton64195a22011-02-23 00:35:02 +0000376 SetTriple(triple);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000377}
378
Greg Claytone0d378b2011-03-24 21:19:54 +0000379ArchSpec::ArchSpec (ArchitectureType arch_type, uint32_t cpu, uint32_t subtype) :
Greg Clayton64195a22011-02-23 00:35:02 +0000380 m_triple (),
381 m_core (kCore_invalid),
382 m_byte_order (eByteOrderInvalid)
383{
384 SetArchitecture (arch_type, cpu, subtype);
385}
386
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000387ArchSpec::~ArchSpec()
388{
389}
390
Greg Clayton64195a22011-02-23 00:35:02 +0000391//===----------------------------------------------------------------------===//
392// Assignment and initialization.
393
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000394const ArchSpec&
395ArchSpec::operator= (const ArchSpec& rhs)
396{
397 if (this != &rhs)
398 {
Greg Clayton514487e2011-02-15 21:59:32 +0000399 m_triple = rhs.m_triple;
Greg Clayton64195a22011-02-23 00:35:02 +0000400 m_core = rhs.m_core;
Greg Clayton514487e2011-02-15 21:59:32 +0000401 m_byte_order = rhs.m_byte_order;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000402 }
403 return *this;
404}
405
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000406void
407ArchSpec::Clear()
408{
Greg Clayton514487e2011-02-15 21:59:32 +0000409 m_triple = llvm::Triple();
Greg Clayton64195a22011-02-23 00:35:02 +0000410 m_core = kCore_invalid;
411 m_byte_order = eByteOrderInvalid;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000412}
413
Greg Clayton64195a22011-02-23 00:35:02 +0000414//===----------------------------------------------------------------------===//
415// Predicates.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000416
Greg Clayton41f92322010-06-11 03:25:34 +0000417
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000418const char *
Greg Clayton64195a22011-02-23 00:35:02 +0000419ArchSpec::GetArchitectureName () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000420{
Greg Clayton64195a22011-02-23 00:35:02 +0000421 const CoreDefinition *core_def = FindCoreDefinition (m_core);
422 if (core_def)
423 return core_def->name;
424 return "unknown";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000425}
426
Greg Clayton64195a22011-02-23 00:35:02 +0000427uint32_t
428ArchSpec::GetMachOCPUType () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000429{
Greg Clayton64195a22011-02-23 00:35:02 +0000430 const CoreDefinition *core_def = FindCoreDefinition (m_core);
431 if (core_def)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000432 {
Greg Clayton64195a22011-02-23 00:35:02 +0000433 const ArchDefinitionEntry *arch_def = FindArchDefinitionEntry (&g_macho_arch_def, core_def->core);
434 if (arch_def)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000435 {
Greg Clayton64195a22011-02-23 00:35:02 +0000436 return arch_def->cpu;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000437 }
438 }
Greg Clayton64195a22011-02-23 00:35:02 +0000439 return LLDB_INVALID_CPUTYPE;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000440}
441
Greg Clayton64195a22011-02-23 00:35:02 +0000442uint32_t
443ArchSpec::GetMachOCPUSubType () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000444{
Greg Clayton64195a22011-02-23 00:35:02 +0000445 const CoreDefinition *core_def = FindCoreDefinition (m_core);
446 if (core_def)
447 {
448 const ArchDefinitionEntry *arch_def = FindArchDefinitionEntry (&g_macho_arch_def, core_def->core);
449 if (arch_def)
450 {
Greg Clayton1cb64962011-03-24 04:28:38 +0000451 return arch_def->sub;
Greg Clayton64195a22011-02-23 00:35:02 +0000452 }
453 }
454 return LLDB_INVALID_CPUTYPE;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000455}
456
Greg Clayton64195a22011-02-23 00:35:02 +0000457llvm::Triple::ArchType
458ArchSpec::GetMachine () const
459{
460 const CoreDefinition *core_def = FindCoreDefinition (m_core);
461 if (core_def)
462 return core_def->machine;
463
464 return llvm::Triple::UnknownArch;
465}
466
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000467uint32_t
468ArchSpec::GetAddressByteSize() const
469{
Greg Clayton64195a22011-02-23 00:35:02 +0000470 const CoreDefinition *core_def = FindCoreDefinition (m_core);
471 if (core_def)
472 return core_def->addr_byte_size;
Greg Clayton41f92322010-06-11 03:25:34 +0000473 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000474}
475
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000476ByteOrder
477ArchSpec::GetDefaultEndian () const
478{
Greg Clayton64195a22011-02-23 00:35:02 +0000479 const CoreDefinition *core_def = FindCoreDefinition (m_core);
480 if (core_def)
481 return core_def->default_byte_order;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000482 return eByteOrderInvalid;
483}
484
Greg Clayton64195a22011-02-23 00:35:02 +0000485lldb::ByteOrder
486ArchSpec::GetByteOrder () const
487{
488 if (m_byte_order == eByteOrderInvalid)
489 return GetDefaultEndian();
490 return m_byte_order;
491}
492
493//===----------------------------------------------------------------------===//
494// Mutators.
495
496bool
497ArchSpec::SetTriple (const llvm::Triple &triple)
498{
499 m_triple = triple;
500
501 llvm::StringRef arch_name (m_triple.getArchName());
502 const CoreDefinition *core_def = FindCoreDefinition (arch_name);
503 if (core_def)
504 {
505 m_core = core_def->core;
Greg Claytoneb0103f2011-04-07 22:46:35 +0000506 // Set the byte order to the default byte order for an architecture.
507 // This can be modified if needed for cases when cores handle both
508 // big and little endian
509 m_byte_order = core_def->default_byte_order;
Greg Clayton64195a22011-02-23 00:35:02 +0000510 }
511 else
512 {
513 Clear();
514 }
515
516
517 return IsValid();
518}
519
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000520static bool
521ParseMachCPUDashSubtypeTriple (const char *triple_cstr, ArchSpec &arch)
522{
523 // Accept "12-10" or "12.10" as cpu type/subtype
524 if (isdigit(triple_cstr[0]))
525 {
526 char *end = NULL;
527 errno = 0;
Greg Claytonc7bece562013-01-25 18:06:21 +0000528 uint32_t cpu = (uint32_t)::strtoul (triple_cstr, &end, 0);
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000529 if (errno == 0 && cpu != 0 && end && ((*end == '-') || (*end == '.')))
530 {
531 errno = 0;
Greg Claytonc7bece562013-01-25 18:06:21 +0000532 uint32_t sub = (uint32_t)::strtoul (end + 1, &end, 0);
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000533 if (errno == 0 && end && ((*end == '-') || (*end == '.') || (*end == '\0')))
534 {
535 if (arch.SetArchitecture (eArchTypeMachO, cpu, sub))
536 {
537 if (*end == '-')
538 {
539 llvm::StringRef vendor_os (end + 1);
540 size_t dash_pos = vendor_os.find('-');
541 if (dash_pos != llvm::StringRef::npos)
542 {
543 llvm::StringRef vendor_str(vendor_os.substr(0, dash_pos));
544 arch.GetTriple().setVendorName(vendor_str);
545 const size_t vendor_start_pos = dash_pos+1;
Greg Claytonc7bece562013-01-25 18:06:21 +0000546 dash_pos = vendor_os.find('-', vendor_start_pos);
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000547 if (dash_pos == llvm::StringRef::npos)
548 {
549 if (vendor_start_pos < vendor_os.size())
550 arch.GetTriple().setOSName(vendor_os.substr(vendor_start_pos));
551 }
552 else
553 {
554 arch.GetTriple().setOSName(vendor_os.substr(vendor_start_pos, dash_pos - vendor_start_pos));
555 }
556 }
557 }
558 return true;
559 }
560 }
561 }
562 }
563 return false;
564}
Greg Clayton64195a22011-02-23 00:35:02 +0000565bool
Greg Clayton70512312012-05-08 01:45:38 +0000566ArchSpec::SetTriple (const char *triple_cstr)
Greg Clayton64195a22011-02-23 00:35:02 +0000567{
Greg Clayton23aca092011-08-12 23:32:52 +0000568 if (triple_cstr && triple_cstr[0])
Greg Clayton64195a22011-02-23 00:35:02 +0000569 {
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000570 if (ParseMachCPUDashSubtypeTriple (triple_cstr, *this))
571 return true;
572
Greg Clayton64195a22011-02-23 00:35:02 +0000573 llvm::StringRef triple_stref (triple_cstr);
574 if (triple_stref.startswith (LLDB_ARCH_DEFAULT))
575 {
576 // Special case for the current host default architectures...
577 if (triple_stref.equals (LLDB_ARCH_DEFAULT_32BIT))
578 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture32);
579 else if (triple_stref.equals (LLDB_ARCH_DEFAULT_64BIT))
580 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture64);
581 else if (triple_stref.equals (LLDB_ARCH_DEFAULT))
582 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture);
583 }
584 else
585 {
586 std::string normalized_triple_sstr (llvm::Triple::normalize(triple_stref));
587 triple_stref = normalized_triple_sstr;
Greg Clayton70512312012-05-08 01:45:38 +0000588 SetTriple (llvm::Triple (triple_stref));
589 }
590 }
591 else
592 Clear();
593 return IsValid();
594}
595
596bool
597ArchSpec::SetTriple (const char *triple_cstr, Platform *platform)
598{
599 if (triple_cstr && triple_cstr[0])
600 {
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000601 if (ParseMachCPUDashSubtypeTriple (triple_cstr, *this))
602 return true;
603
Greg Clayton70512312012-05-08 01:45:38 +0000604 llvm::StringRef triple_stref (triple_cstr);
605 if (triple_stref.startswith (LLDB_ARCH_DEFAULT))
606 {
607 // Special case for the current host default architectures...
608 if (triple_stref.equals (LLDB_ARCH_DEFAULT_32BIT))
609 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture32);
610 else if (triple_stref.equals (LLDB_ARCH_DEFAULT_64BIT))
611 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture64);
612 else if (triple_stref.equals (LLDB_ARCH_DEFAULT))
613 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture);
614 }
615 else
616 {
617 ArchSpec raw_arch (triple_cstr);
618
619 std::string normalized_triple_sstr (llvm::Triple::normalize(triple_stref));
620 triple_stref = normalized_triple_sstr;
Greg Claytoneb0103f2011-04-07 22:46:35 +0000621 llvm::Triple normalized_triple (triple_stref);
622
623 const bool os_specified = normalized_triple.getOSName().size() > 0;
624 const bool vendor_specified = normalized_triple.getVendorName().size() > 0;
625 const bool env_specified = normalized_triple.getEnvironmentName().size() > 0;
626
627 // If we got an arch only, then default the vendor, os, environment
628 // to match the platform if one is supplied
629 if (!(os_specified || vendor_specified || env_specified))
630 {
631 if (platform)
632 {
633 // If we were given a platform, use the platform's system
634 // architecture. If this is not available (might not be
635 // connected) use the first supported architecture.
Greg Clayton70512312012-05-08 01:45:38 +0000636 ArchSpec compatible_arch;
Greg Clayton1e0c8842013-01-11 20:49:54 +0000637 if (platform->IsCompatibleArchitecture (raw_arch, false, &compatible_arch))
Greg Claytoneb0103f2011-04-07 22:46:35 +0000638 {
Greg Clayton70512312012-05-08 01:45:38 +0000639 if (compatible_arch.IsValid())
640 {
641 const llvm::Triple &compatible_triple = compatible_arch.GetTriple();
642 if (!vendor_specified)
643 normalized_triple.setVendor(compatible_triple.getVendor());
644 if (!os_specified)
645 normalized_triple.setOS(compatible_triple.getOS());
646 if (!env_specified && compatible_triple.getEnvironmentName().size())
647 normalized_triple.setEnvironment(compatible_triple.getEnvironment());
648 }
Greg Claytoneb0103f2011-04-07 22:46:35 +0000649 }
Greg Clayton70512312012-05-08 01:45:38 +0000650 else
Greg Claytoneb0103f2011-04-07 22:46:35 +0000651 {
Greg Clayton70512312012-05-08 01:45:38 +0000652 *this = raw_arch;
653 return IsValid();
Greg Claytoneb0103f2011-04-07 22:46:35 +0000654 }
655 }
656 else
657 {
658 // No platform specified, fall back to the host system for
659 // the default vendor, os, and environment.
Sean Callananbfb237bc2011-11-04 22:46:46 +0000660 llvm::Triple host_triple(llvm::sys::getDefaultTargetTriple());
Greg Clayton70512312012-05-08 01:45:38 +0000661 if (!vendor_specified)
662 normalized_triple.setVendor(host_triple.getVendor());
663 if (!vendor_specified)
664 normalized_triple.setOS(host_triple.getOS());
665 if (!env_specified && host_triple.getEnvironmentName().size())
666 normalized_triple.setEnvironment(host_triple.getEnvironment());
Greg Claytoneb0103f2011-04-07 22:46:35 +0000667 }
668 }
669 SetTriple (normalized_triple);
Greg Clayton64195a22011-02-23 00:35:02 +0000670 }
671 }
672 else
673 Clear();
674 return IsValid();
675}
676
Greg Clayton64195a22011-02-23 00:35:02 +0000677bool
Greg Claytone0d378b2011-03-24 21:19:54 +0000678ArchSpec::SetArchitecture (ArchitectureType arch_type, uint32_t cpu, uint32_t sub)
Greg Clayton64195a22011-02-23 00:35:02 +0000679{
680 m_core = kCore_invalid;
681 bool update_triple = true;
682 const ArchDefinition *arch_def = FindArchDefinition(arch_type);
683 if (arch_def)
684 {
685 const ArchDefinitionEntry *arch_def_entry = FindArchDefinitionEntry (arch_def, cpu, sub);
686 if (arch_def_entry)
687 {
688 const CoreDefinition *core_def = FindCoreDefinition (arch_def_entry->core);
689 if (core_def)
690 {
691 m_core = core_def->core;
692 update_triple = false;
Greg Clayton593577a2011-09-21 03:57:31 +0000693 // Always use the architecture name because it might be more descriptive
694 // than the architecture enum ("armv7" -> llvm::Triple::arm).
695 m_triple.setArchName(llvm::StringRef(core_def->name));
Greg Clayton64195a22011-02-23 00:35:02 +0000696 if (arch_type == eArchTypeMachO)
697 {
698 m_triple.setVendor (llvm::Triple::Apple);
Greg Clayton70512312012-05-08 01:45:38 +0000699
700 switch (core_def->machine)
701 {
702 case llvm::Triple::arm:
703 case llvm::Triple::thumb:
704 m_triple.setOS (llvm::Triple::IOS);
705 break;
706
707 case llvm::Triple::x86:
708 case llvm::Triple::x86_64:
709 default:
710 m_triple.setOS (llvm::Triple::MacOSX);
711 break;
712 }
Greg Clayton64195a22011-02-23 00:35:02 +0000713 }
714 else
715 {
716 m_triple.setVendor (llvm::Triple::UnknownVendor);
717 m_triple.setOS (llvm::Triple::UnknownOS);
718 }
Greg Clayton593577a2011-09-21 03:57:31 +0000719 // Fall back onto setting the machine type if the arch by name failed...
720 if (m_triple.getArch () == llvm::Triple::UnknownArch)
721 m_triple.setArch (core_def->machine);
Greg Clayton64195a22011-02-23 00:35:02 +0000722 }
723 }
724 }
725 CoreUpdated(update_triple);
726 return IsValid();
727}
728
Greg Clayton357132e2011-03-26 19:14:58 +0000729uint32_t
730ArchSpec::GetMinimumOpcodeByteSize() const
Greg Clayton64195a22011-02-23 00:35:02 +0000731{
Greg Clayton357132e2011-03-26 19:14:58 +0000732 const CoreDefinition *core_def = FindCoreDefinition (m_core);
733 if (core_def)
734 return core_def->min_opcode_byte_size;
735 return 0;
736}
737
738uint32_t
739ArchSpec::GetMaximumOpcodeByteSize() const
740{
741 const CoreDefinition *core_def = FindCoreDefinition (m_core);
742 if (core_def)
743 return core_def->max_opcode_byte_size;
744 return 0;
Greg Clayton64195a22011-02-23 00:35:02 +0000745}
746
Jason Molendaba813dc2012-11-04 03:20:05 +0000747bool
748ArchSpec::IsExactMatch (const ArchSpec& rhs) const
749{
Sean Callananbf4b7be2012-12-13 22:07:14 +0000750 return IsEqualTo (rhs, true);
Jason Molendaba813dc2012-11-04 03:20:05 +0000751}
752
753bool
754ArchSpec::IsCompatibleMatch (const ArchSpec& rhs) const
755{
Sean Callananbf4b7be2012-12-13 22:07:14 +0000756 return IsEqualTo (rhs, false);
Jason Molendaba813dc2012-11-04 03:20:05 +0000757}
758
759bool
Sean Callananbf4b7be2012-12-13 22:07:14 +0000760ArchSpec::IsEqualTo (const ArchSpec& rhs, bool exact_match) const
Jason Molendaba813dc2012-11-04 03:20:05 +0000761{
762 if (GetByteOrder() != rhs.GetByteOrder())
763 return false;
764
765 const ArchSpec::Core lhs_core = GetCore ();
766 const ArchSpec::Core rhs_core = rhs.GetCore ();
767
768 const bool core_match = cores_match (lhs_core, rhs_core, true, exact_match);
769
770 if (core_match)
771 {
772 const llvm::Triple &lhs_triple = GetTriple();
773 const llvm::Triple &rhs_triple = rhs.GetTriple();
774
775 const llvm::Triple::VendorType lhs_triple_vendor = lhs_triple.getVendor();
776 const llvm::Triple::VendorType rhs_triple_vendor = rhs_triple.getVendor();
777 if (lhs_triple_vendor != rhs_triple_vendor)
778 {
Sean Callananbf4b7be2012-12-13 22:07:14 +0000779 if (exact_match)
780 {
781 const bool rhs_vendor_specified = rhs.TripleVendorWasSpecified();
782 const bool lhs_vendor_specified = TripleVendorWasSpecified();
783 // Both architectures had the vendor specified, so if they aren't
784 // equal then we return false
785 if (rhs_vendor_specified && lhs_vendor_specified)
786 return false;
787 }
Jason Molendaba813dc2012-11-04 03:20:05 +0000788
789 // Only fail if both vendor types are not unknown
790 if (lhs_triple_vendor != llvm::Triple::UnknownVendor &&
791 rhs_triple_vendor != llvm::Triple::UnknownVendor)
792 return false;
793 }
794
795 const llvm::Triple::OSType lhs_triple_os = lhs_triple.getOS();
796 const llvm::Triple::OSType rhs_triple_os = rhs_triple.getOS();
797 if (lhs_triple_os != rhs_triple_os)
798 {
Sean Callananbf4b7be2012-12-13 22:07:14 +0000799 if (exact_match)
800 {
801 const bool rhs_os_specified = rhs.TripleOSWasSpecified();
802 const bool lhs_os_specified = TripleOSWasSpecified();
803 // Both architectures had the OS specified, so if they aren't
804 // equal then we return false
805 if (rhs_os_specified && lhs_os_specified)
806 return false;
807 }
Jason Molendaba813dc2012-11-04 03:20:05 +0000808 // Only fail if both os types are not unknown
809 if (lhs_triple_os != llvm::Triple::UnknownOS &&
810 rhs_triple_os != llvm::Triple::UnknownOS)
811 return false;
812 }
813
814 const llvm::Triple::EnvironmentType lhs_triple_env = lhs_triple.getEnvironment();
815 const llvm::Triple::EnvironmentType rhs_triple_env = rhs_triple.getEnvironment();
816
817 if (lhs_triple_env != rhs_triple_env)
818 {
819 // Only fail if both environment types are not unknown
820 if (lhs_triple_env != llvm::Triple::UnknownEnvironment &&
821 rhs_triple_env != llvm::Triple::UnknownEnvironment)
822 return false;
823 }
824 return true;
825 }
826 return false;
827}
828
Greg Clayton64195a22011-02-23 00:35:02 +0000829//===----------------------------------------------------------------------===//
830// Helper methods.
831
832void
833ArchSpec::CoreUpdated (bool update_triple)
834{
835 const CoreDefinition *core_def = FindCoreDefinition (m_core);
836 if (core_def)
837 {
838 if (update_triple)
839 m_triple = llvm::Triple(core_def->name, "unknown", "unknown");
840 m_byte_order = core_def->default_byte_order;
841 }
842 else
843 {
844 if (update_triple)
845 m_triple = llvm::Triple();
846 m_byte_order = eByteOrderInvalid;
847 }
848}
849
850//===----------------------------------------------------------------------===//
851// Operators.
852
Greg Clayton70512312012-05-08 01:45:38 +0000853static bool
Jason Molendaba813dc2012-11-04 03:20:05 +0000854cores_match (const ArchSpec::Core core1, const ArchSpec::Core core2, bool try_inverse, bool enforce_exact_match)
Greg Clayton70512312012-05-08 01:45:38 +0000855{
Jason Molendaba813dc2012-11-04 03:20:05 +0000856 if (core1 == core2)
857 return true;
858
Greg Clayton70512312012-05-08 01:45:38 +0000859 switch (core1)
860 {
Greg Clayton70512312012-05-08 01:45:38 +0000861 case ArchSpec::kCore_any:
862 return true;
863
864 case ArchSpec::kCore_arm_any:
865 if (core2 >= ArchSpec::kCore_arm_first && core2 <= ArchSpec::kCore_arm_last)
866 return true;
867 if (core2 >= ArchSpec::kCore_thumb_first && core2 <= ArchSpec::kCore_thumb_last)
868 return true;
869 if (core2 == ArchSpec::kCore_arm_any)
870 return true;
871 break;
872
873 case ArchSpec::kCore_x86_32_any:
874 if ((core2 >= ArchSpec::kCore_x86_32_first && core2 <= ArchSpec::kCore_x86_32_last) || (core2 == ArchSpec::kCore_x86_32_any))
875 return true;
876 break;
877
878 case ArchSpec::kCore_ppc_any:
879 if ((core2 >= ArchSpec::kCore_ppc_first && core2 <= ArchSpec::kCore_ppc_last) || (core2 == ArchSpec::kCore_ppc_any))
880 return true;
881 break;
882
883 case ArchSpec::kCore_ppc64_any:
884 if ((core2 >= ArchSpec::kCore_ppc64_first && core2 <= ArchSpec::kCore_ppc64_last) || (core2 == ArchSpec::kCore_ppc64_any))
885 return true;
886 break;
887
Jason Molendaa3a04522013-09-27 23:21:54 +0000888 case ArchSpec::eCore_arm_armv6m:
889 if (!enforce_exact_match)
890 {
891 try_inverse = false;
892 if (core2 == ArchSpec::eCore_arm_armv6)
893 return true;
894 }
895 break;
896
Jason Molenda7a1559c2013-03-08 01:20:17 +0000897 case ArchSpec::eCore_arm_armv7m:
898 case ArchSpec::eCore_arm_armv7em:
Johnny Chen1083b0d2012-08-28 22:53:40 +0000899 case ArchSpec::eCore_arm_armv7f:
900 case ArchSpec::eCore_arm_armv7k:
901 case ArchSpec::eCore_arm_armv7s:
Jason Molendaba813dc2012-11-04 03:20:05 +0000902 if (!enforce_exact_match)
903 {
904 try_inverse = false;
905 if (core2 == ArchSpec::eCore_arm_armv7)
906 return true;
907 }
Johnny Chen1083b0d2012-08-28 22:53:40 +0000908 break;
909
Greg Clayton70512312012-05-08 01:45:38 +0000910 default:
911 break;
912 }
913 if (try_inverse)
Jason Molendaba813dc2012-11-04 03:20:05 +0000914 return cores_match (core2, core1, false, enforce_exact_match);
Greg Clayton70512312012-05-08 01:45:38 +0000915 return false;
916}
917
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000918bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000919lldb_private::operator<(const ArchSpec& lhs, const ArchSpec& rhs)
920{
Greg Clayton64195a22011-02-23 00:35:02 +0000921 const ArchSpec::Core lhs_core = lhs.GetCore ();
922 const ArchSpec::Core rhs_core = rhs.GetCore ();
923 return lhs_core < rhs_core;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000924}