blob: 4cf2789480361c71f68e3ebb64fa1a07bfac44bf [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" },
Ed Masteb73f8442013-10-10 00:59:47 +000079
80 { eByteOrderBig , 8, 4, 4, llvm::Triple::mips64 , ArchSpec::eCore_mips64 , "mips64" },
Greg Clayton64195a22011-02-23 00:35:02 +000081
Greg Clayton83b162d2013-08-12 18:34:04 +000082 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_generic , "ppc" },
83 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc601 , "ppc601" },
84 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc602 , "ppc602" },
85 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc603 , "ppc603" },
86 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc603e , "ppc603e" },
87 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc603ev , "ppc603ev" },
88 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc604 , "ppc604" },
89 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc604e , "ppc604e" },
90 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc620 , "ppc620" },
91 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc750 , "ppc750" },
92 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc7400 , "ppc7400" },
93 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc7450 , "ppc7450" },
94 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc970 , "ppc970" },
Greg Clayton64195a22011-02-23 00:35:02 +000095
Greg Clayton83b162d2013-08-12 18:34:04 +000096 { eByteOrderBig , 8, 4, 4, llvm::Triple::ppc64 , ArchSpec::eCore_ppc64_generic , "ppc64" },
97 { eByteOrderBig , 8, 4, 4, llvm::Triple::ppc64 , ArchSpec::eCore_ppc64_ppc970_64 , "ppc970-64" },
Greg Clayton64195a22011-02-23 00:35:02 +000098
Greg Clayton357132e2011-03-26 19:14:58 +000099 { eByteOrderLittle, 4, 4, 4, llvm::Triple::sparc , ArchSpec::eCore_sparc_generic , "sparc" },
100 { eByteOrderLittle, 8, 4, 4, llvm::Triple::sparcv9, ArchSpec::eCore_sparc9_generic , "sparcv9" },
Greg Clayton64195a22011-02-23 00:35:02 +0000101
Greg Claytonab65b342011-04-13 22:47:15 +0000102 { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i386 , "i386" },
103 { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i486 , "i486" },
104 { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i486sx , "i486sx" },
Greg Clayton64195a22011-02-23 00:35:02 +0000105
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000106 { eByteOrderLittle, 8, 1, 15, llvm::Triple::x86_64 , ArchSpec::eCore_x86_64_x86_64 , "x86_64" },
Greg Claytona86dc432014-01-22 23:42:03 +0000107 { eByteOrderLittle, 8, 1, 15, llvm::Triple::x86_64 , ArchSpec::eCore_x86_64_x86_64h , "x86_64h" },
Deepak Panickal6d3df422014-02-19 11:16:46 +0000108 { eByteOrderLittle, 4, 4, 4, llvm::Triple::hexagon , ArchSpec::eCore_hexagon_generic, "hexagon" },
109 { eByteOrderLittle, 4, 4, 4, llvm::Triple::hexagon , ArchSpec::eCore_hexagon_hexagonv4, "hexagonv4" },
110 { eByteOrderLittle, 4, 4, 4, llvm::Triple::hexagon , ArchSpec::eCore_hexagon_hexagonv5, "hexagonv5" },
111
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000112 { eByteOrderLittle, 4, 4, 4 , llvm::Triple::UnknownArch , ArchSpec::eCore_uknownMach32 , "unknown-mach-32" },
113 { eByteOrderLittle, 8, 4, 4 , llvm::Triple::UnknownArch , ArchSpec::eCore_uknownMach64 , "unknown-mach-64" }
Greg Clayton64195a22011-02-23 00:35:02 +0000114};
115
116struct ArchDefinitionEntry
117{
118 ArchSpec::Core core;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000119 uint32_t cpu;
120 uint32_t sub;
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000121 uint32_t cpu_mask;
122 uint32_t sub_mask;
Greg Clayton64195a22011-02-23 00:35:02 +0000123};
124
125struct ArchDefinition
126{
127 ArchitectureType type;
128 size_t num_entries;
129 const ArchDefinitionEntry *entries;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000130 const char *name;
131};
132
Greg Clayton41f92322010-06-11 03:25:34 +0000133
Greg Claytonc7bece562013-01-25 18:06:21 +0000134size_t
Greg Claytonab65b342011-04-13 22:47:15 +0000135ArchSpec::AutoComplete (const char *name, StringList &matches)
136{
137 uint32_t i;
138 if (name && name[0])
139 {
140 for (i = 0; i < ArchSpec::kNumCores; ++i)
141 {
142 if (NameMatches(g_core_definitions[i].name, eNameMatchStartsWith, name))
143 matches.AppendString (g_core_definitions[i].name);
144 }
145 }
146 else
147 {
148 for (i = 0; i < ArchSpec::kNumCores; ++i)
149 matches.AppendString (g_core_definitions[i].name);
150 }
151 return matches.GetSize();
152}
153
154
155
Greg Clayton64195a22011-02-23 00:35:02 +0000156#define CPU_ANY (UINT32_MAX)
157
158//===----------------------------------------------------------------------===//
159// A table that gets searched linearly for matches. This table is used to
160// convert cpu type and subtypes to architecture names, and to convert
161// architecture names to cpu types and subtypes. The ordering is important and
162// allows the precedence to be set when the table is built.
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000163#define SUBTYPE_MASK 0x00FFFFFFu
Greg Clayton64195a22011-02-23 00:35:02 +0000164static const ArchDefinitionEntry g_macho_arch_entries[] =
Greg Clayton41f92322010-06-11 03:25:34 +0000165{
Charles Davis510938e2013-08-27 05:04:57 +0000166 { ArchSpec::eCore_arm_generic , llvm::MachO::CPU_TYPE_ARM , CPU_ANY, UINT32_MAX , UINT32_MAX },
167 { ArchSpec::eCore_arm_generic , llvm::MachO::CPU_TYPE_ARM , 0 , UINT32_MAX , SUBTYPE_MASK },
168 { ArchSpec::eCore_arm_armv4 , llvm::MachO::CPU_TYPE_ARM , 5 , UINT32_MAX , SUBTYPE_MASK },
169 { ArchSpec::eCore_arm_armv4t , llvm::MachO::CPU_TYPE_ARM , 5 , UINT32_MAX , SUBTYPE_MASK },
170 { ArchSpec::eCore_arm_armv6 , llvm::MachO::CPU_TYPE_ARM , 6 , UINT32_MAX , SUBTYPE_MASK },
Jason Molenda64a11732013-10-08 03:01:08 +0000171 { ArchSpec::eCore_arm_armv6m , llvm::MachO::CPU_TYPE_ARM , 14 , UINT32_MAX , SUBTYPE_MASK },
Charles Davis510938e2013-08-27 05:04:57 +0000172 { ArchSpec::eCore_arm_armv5 , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
173 { ArchSpec::eCore_arm_armv5e , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
174 { ArchSpec::eCore_arm_armv5t , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
175 { ArchSpec::eCore_arm_xscale , llvm::MachO::CPU_TYPE_ARM , 8 , UINT32_MAX , SUBTYPE_MASK },
176 { ArchSpec::eCore_arm_armv7 , llvm::MachO::CPU_TYPE_ARM , 9 , UINT32_MAX , SUBTYPE_MASK },
177 { ArchSpec::eCore_arm_armv7f , llvm::MachO::CPU_TYPE_ARM , 10 , UINT32_MAX , SUBTYPE_MASK },
178 { ArchSpec::eCore_arm_armv7s , llvm::MachO::CPU_TYPE_ARM , 11 , UINT32_MAX , SUBTYPE_MASK },
179 { ArchSpec::eCore_arm_armv7k , llvm::MachO::CPU_TYPE_ARM , 12 , UINT32_MAX , SUBTYPE_MASK },
180 { ArchSpec::eCore_arm_armv7m , llvm::MachO::CPU_TYPE_ARM , 15 , UINT32_MAX , SUBTYPE_MASK },
181 { ArchSpec::eCore_arm_armv7em , llvm::MachO::CPU_TYPE_ARM , 16 , UINT32_MAX , SUBTYPE_MASK },
182 { ArchSpec::eCore_thumb , llvm::MachO::CPU_TYPE_ARM , 0 , UINT32_MAX , SUBTYPE_MASK },
183 { ArchSpec::eCore_thumbv4t , llvm::MachO::CPU_TYPE_ARM , 5 , UINT32_MAX , SUBTYPE_MASK },
184 { ArchSpec::eCore_thumbv5 , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
185 { ArchSpec::eCore_thumbv5e , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
186 { ArchSpec::eCore_thumbv6 , llvm::MachO::CPU_TYPE_ARM , 6 , UINT32_MAX , SUBTYPE_MASK },
Jason Molenda64a11732013-10-08 03:01:08 +0000187 { ArchSpec::eCore_thumbv6m , llvm::MachO::CPU_TYPE_ARM , 14 , UINT32_MAX , SUBTYPE_MASK },
Charles Davis510938e2013-08-27 05:04:57 +0000188 { ArchSpec::eCore_thumbv7 , llvm::MachO::CPU_TYPE_ARM , 9 , UINT32_MAX , SUBTYPE_MASK },
189 { ArchSpec::eCore_thumbv7f , llvm::MachO::CPU_TYPE_ARM , 10 , UINT32_MAX , SUBTYPE_MASK },
190 { ArchSpec::eCore_thumbv7s , llvm::MachO::CPU_TYPE_ARM , 11 , UINT32_MAX , SUBTYPE_MASK },
191 { ArchSpec::eCore_thumbv7k , llvm::MachO::CPU_TYPE_ARM , 12 , UINT32_MAX , SUBTYPE_MASK },
192 { ArchSpec::eCore_thumbv7m , llvm::MachO::CPU_TYPE_ARM , 15 , UINT32_MAX , SUBTYPE_MASK },
193 { ArchSpec::eCore_thumbv7em , llvm::MachO::CPU_TYPE_ARM , 16 , UINT32_MAX , SUBTYPE_MASK },
194 { ArchSpec::eCore_ppc_generic , llvm::MachO::CPU_TYPE_POWERPC , CPU_ANY, UINT32_MAX , UINT32_MAX },
195 { ArchSpec::eCore_ppc_generic , llvm::MachO::CPU_TYPE_POWERPC , 0 , UINT32_MAX , SUBTYPE_MASK },
196 { ArchSpec::eCore_ppc_ppc601 , llvm::MachO::CPU_TYPE_POWERPC , 1 , UINT32_MAX , SUBTYPE_MASK },
197 { ArchSpec::eCore_ppc_ppc602 , llvm::MachO::CPU_TYPE_POWERPC , 2 , UINT32_MAX , SUBTYPE_MASK },
198 { ArchSpec::eCore_ppc_ppc603 , llvm::MachO::CPU_TYPE_POWERPC , 3 , UINT32_MAX , SUBTYPE_MASK },
199 { ArchSpec::eCore_ppc_ppc603e , llvm::MachO::CPU_TYPE_POWERPC , 4 , UINT32_MAX , SUBTYPE_MASK },
200 { ArchSpec::eCore_ppc_ppc603ev , llvm::MachO::CPU_TYPE_POWERPC , 5 , UINT32_MAX , SUBTYPE_MASK },
201 { ArchSpec::eCore_ppc_ppc604 , llvm::MachO::CPU_TYPE_POWERPC , 6 , UINT32_MAX , SUBTYPE_MASK },
202 { ArchSpec::eCore_ppc_ppc604e , llvm::MachO::CPU_TYPE_POWERPC , 7 , UINT32_MAX , SUBTYPE_MASK },
203 { ArchSpec::eCore_ppc_ppc620 , llvm::MachO::CPU_TYPE_POWERPC , 8 , UINT32_MAX , SUBTYPE_MASK },
204 { ArchSpec::eCore_ppc_ppc750 , llvm::MachO::CPU_TYPE_POWERPC , 9 , UINT32_MAX , SUBTYPE_MASK },
205 { ArchSpec::eCore_ppc_ppc7400 , llvm::MachO::CPU_TYPE_POWERPC , 10 , UINT32_MAX , SUBTYPE_MASK },
206 { ArchSpec::eCore_ppc_ppc7450 , llvm::MachO::CPU_TYPE_POWERPC , 11 , UINT32_MAX , SUBTYPE_MASK },
207 { ArchSpec::eCore_ppc_ppc970 , llvm::MachO::CPU_TYPE_POWERPC , 100 , UINT32_MAX , SUBTYPE_MASK },
208 { ArchSpec::eCore_ppc64_generic , llvm::MachO::CPU_TYPE_POWERPC64 , 0 , UINT32_MAX , SUBTYPE_MASK },
209 { ArchSpec::eCore_ppc64_ppc970_64 , llvm::MachO::CPU_TYPE_POWERPC64 , 100 , UINT32_MAX , SUBTYPE_MASK },
210 { ArchSpec::eCore_x86_32_i386 , llvm::MachO::CPU_TYPE_I386 , 3 , UINT32_MAX , SUBTYPE_MASK },
211 { ArchSpec::eCore_x86_32_i486 , llvm::MachO::CPU_TYPE_I386 , 4 , UINT32_MAX , SUBTYPE_MASK },
212 { ArchSpec::eCore_x86_32_i486sx , llvm::MachO::CPU_TYPE_I386 , 0x84 , UINT32_MAX , SUBTYPE_MASK },
Greg Claytona86dc432014-01-22 23:42:03 +0000213 { ArchSpec::eCore_x86_32_i386 , llvm::MachO::CPU_TYPE_I386 , CPU_ANY, UINT32_MAX , UINT32_MAX },
Charles Davis510938e2013-08-27 05:04:57 +0000214 { ArchSpec::eCore_x86_64_x86_64 , llvm::MachO::CPU_TYPE_X86_64 , 3 , UINT32_MAX , SUBTYPE_MASK },
215 { ArchSpec::eCore_x86_64_x86_64 , llvm::MachO::CPU_TYPE_X86_64 , 4 , UINT32_MAX , SUBTYPE_MASK },
Greg Claytona86dc432014-01-22 23:42:03 +0000216 { ArchSpec::eCore_x86_64_x86_64h , llvm::MachO::CPU_TYPE_X86_64 , 8 , UINT32_MAX , SUBTYPE_MASK },
217 { 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 +0000218 // Catch any unknown mach architectures so we can always use the object and symbol mach-o files
Charles Davis510938e2013-08-27 05:04:57 +0000219 { ArchSpec::eCore_uknownMach32 , 0 , 0 , 0xFF000000u, 0x00000000u },
220 { ArchSpec::eCore_uknownMach64 , llvm::MachO::CPU_ARCH_ABI64 , 0 , 0xFF000000u, 0x00000000u }
Greg Clayton64195a22011-02-23 00:35:02 +0000221};
222static const ArchDefinition g_macho_arch_def = {
223 eArchTypeMachO,
224 sizeof(g_macho_arch_entries)/sizeof(g_macho_arch_entries[0]),
225 g_macho_arch_entries,
Greg Clayton64195a22011-02-23 00:35:02 +0000226 "mach-o"
Greg Clayton41f92322010-06-11 03:25:34 +0000227};
228
Greg Clayton64195a22011-02-23 00:35:02 +0000229//===----------------------------------------------------------------------===//
230// A table that gets searched linearly for matches. This table is used to
231// convert cpu type and subtypes to architecture names, and to convert
232// architecture names to cpu types and subtypes. The ordering is important and
233// allows the precedence to be set when the table is built.
234static const ArchDefinitionEntry g_elf_arch_entries[] =
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000235{
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000236 { ArchSpec::eCore_sparc_generic , llvm::ELF::EM_SPARC , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // Sparc
237 { ArchSpec::eCore_x86_32_i386 , llvm::ELF::EM_386 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // Intel 80386
238 { ArchSpec::eCore_x86_32_i486 , llvm::ELF::EM_486 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // Intel 486 (deprecated)
239 { ArchSpec::eCore_ppc_generic , llvm::ELF::EM_PPC , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // PowerPC
240 { ArchSpec::eCore_ppc64_generic , llvm::ELF::EM_PPC64 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // PowerPC64
241 { ArchSpec::eCore_arm_generic , llvm::ELF::EM_ARM , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // ARM
242 { ArchSpec::eCore_sparc9_generic , llvm::ELF::EM_SPARCV9, LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // SPARC V9
Ed Masteb73f8442013-10-10 00:59:47 +0000243 { ArchSpec::eCore_x86_64_x86_64 , llvm::ELF::EM_X86_64 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // AMD64
Deepak Panickal6d3df422014-02-19 11:16:46 +0000244 { ArchSpec::eCore_mips64 , llvm::ELF::EM_MIPS , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // MIPS
245 { ArchSpec::eCore_hexagon_generic , llvm::ELF::EM_HEXAGON, LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu } // HEXAGON
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000246};
247
Greg Clayton64195a22011-02-23 00:35:02 +0000248static const ArchDefinition g_elf_arch_def = {
249 eArchTypeELF,
250 sizeof(g_elf_arch_entries)/sizeof(g_elf_arch_entries[0]),
251 g_elf_arch_entries,
Greg Clayton64195a22011-02-23 00:35:02 +0000252 "elf",
Greg Clayton41f92322010-06-11 03:25:34 +0000253};
254
Charles Davis237ad972013-08-27 05:04:33 +0000255static const ArchDefinitionEntry g_coff_arch_entries[] =
256{
257 { ArchSpec::eCore_x86_32_i386 , llvm::COFF::IMAGE_FILE_MACHINE_I386 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // Intel 80386
258 { ArchSpec::eCore_ppc_generic , llvm::COFF::IMAGE_FILE_MACHINE_POWERPC , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // PowerPC
259 { ArchSpec::eCore_ppc_generic , llvm::COFF::IMAGE_FILE_MACHINE_POWERPCFP, LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // PowerPC (with FPU)
260 { ArchSpec::eCore_arm_generic , llvm::COFF::IMAGE_FILE_MACHINE_ARM , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // ARM
Saleem Abdulrasool1108cb32014-03-11 03:09:08 +0000261 { ArchSpec::eCore_arm_armv7 , llvm::COFF::IMAGE_FILE_MACHINE_ARMNT , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // ARMv7
Charles Davis237ad972013-08-27 05:04:33 +0000262 { ArchSpec::eCore_thumb , llvm::COFF::IMAGE_FILE_MACHINE_THUMB , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // ARMv7
263 { ArchSpec::eCore_x86_64_x86_64, llvm::COFF::IMAGE_FILE_MACHINE_AMD64 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu } // AMD64
264};
265
266static const ArchDefinition g_coff_arch_def = {
267 eArchTypeCOFF,
268 sizeof(g_coff_arch_entries)/sizeof(g_coff_arch_entries[0]),
269 g_coff_arch_entries,
270 "pe-coff",
271};
272
Greg Clayton64195a22011-02-23 00:35:02 +0000273//===----------------------------------------------------------------------===//
274// Table of all ArchDefinitions
275static const ArchDefinition *g_arch_definitions[] = {
276 &g_macho_arch_def,
Charles Davis237ad972013-08-27 05:04:33 +0000277 &g_elf_arch_def,
278 &g_coff_arch_def
Greg Clayton64195a22011-02-23 00:35:02 +0000279};
Greg Clayton41f92322010-06-11 03:25:34 +0000280
Greg Clayton64195a22011-02-23 00:35:02 +0000281static const size_t k_num_arch_definitions =
282 sizeof(g_arch_definitions) / sizeof(g_arch_definitions[0]);
283
284//===----------------------------------------------------------------------===//
285// Static helper functions.
286
287
288// Get the architecture definition for a given object type.
289static const ArchDefinition *
290FindArchDefinition (ArchitectureType arch_type)
291{
292 for (unsigned int i = 0; i < k_num_arch_definitions; ++i)
293 {
294 const ArchDefinition *def = g_arch_definitions[i];
295 if (def->type == arch_type)
296 return def;
297 }
298 return NULL;
299}
300
301// Get an architecture definition by name.
302static const CoreDefinition *
303FindCoreDefinition (llvm::StringRef name)
304{
305 for (unsigned int i = 0; i < ArchSpec::kNumCores; ++i)
306 {
307 if (name.equals_lower(g_core_definitions[i].name))
308 return &g_core_definitions[i];
309 }
310 return NULL;
311}
312
313static inline const CoreDefinition *
314FindCoreDefinition (ArchSpec::Core core)
315{
316 if (core >= 0 && core < ArchSpec::kNumCores)
317 return &g_core_definitions[core];
318 return NULL;
319}
320
321// Get a definition entry by cpu type and subtype.
322static const ArchDefinitionEntry *
323FindArchDefinitionEntry (const ArchDefinition *def, uint32_t cpu, uint32_t sub)
324{
325 if (def == NULL)
326 return NULL;
327
Greg Clayton64195a22011-02-23 00:35:02 +0000328 const ArchDefinitionEntry *entries = def->entries;
329 for (size_t i = 0; i < def->num_entries; ++i)
330 {
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000331 if (entries[i].cpu == (cpu & entries[i].cpu_mask))
332 if (entries[i].sub == (sub & entries[i].sub_mask))
333 return &entries[i];
Greg Clayton64195a22011-02-23 00:35:02 +0000334 }
335 return NULL;
336}
337
338static const ArchDefinitionEntry *
339FindArchDefinitionEntry (const ArchDefinition *def, ArchSpec::Core core)
340{
341 if (def == NULL)
342 return NULL;
343
344 const ArchDefinitionEntry *entries = def->entries;
345 for (size_t i = 0; i < def->num_entries; ++i)
346 {
347 if (entries[i].core == core)
348 return &entries[i];
349 }
350 return NULL;
351}
352
353//===----------------------------------------------------------------------===//
354// Constructors and destructors.
355
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000356ArchSpec::ArchSpec() :
Greg Clayton514487e2011-02-15 21:59:32 +0000357 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000358 m_core (kCore_invalid),
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000359 m_byte_order (eByteOrderInvalid),
360 m_distribution_id ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000361{
362}
363
Greg Claytoneb0103f2011-04-07 22:46:35 +0000364ArchSpec::ArchSpec (const char *triple_cstr, Platform *platform) :
Greg Clayton514487e2011-02-15 21:59:32 +0000365 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000366 m_core (kCore_invalid),
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000367 m_byte_order (eByteOrderInvalid),
368 m_distribution_id ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000369{
Greg Clayton64195a22011-02-23 00:35:02 +0000370 if (triple_cstr)
Greg Claytoneb0103f2011-04-07 22:46:35 +0000371 SetTriple(triple_cstr, platform);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000372}
373
Greg Clayton70512312012-05-08 01:45:38 +0000374
375ArchSpec::ArchSpec (const char *triple_cstr) :
376 m_triple (),
377 m_core (kCore_invalid),
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000378 m_byte_order (eByteOrderInvalid),
379 m_distribution_id ()
Greg Clayton70512312012-05-08 01:45:38 +0000380{
381 if (triple_cstr)
382 SetTriple(triple_cstr);
383}
384
Greg Clayton64195a22011-02-23 00:35:02 +0000385ArchSpec::ArchSpec(const llvm::Triple &triple) :
Greg Clayton514487e2011-02-15 21:59:32 +0000386 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000387 m_core (kCore_invalid),
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000388 m_byte_order (eByteOrderInvalid),
389 m_distribution_id ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000390{
Greg Clayton64195a22011-02-23 00:35:02 +0000391 SetTriple(triple);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000392}
393
Greg Claytone0d378b2011-03-24 21:19:54 +0000394ArchSpec::ArchSpec (ArchitectureType arch_type, uint32_t cpu, uint32_t subtype) :
Greg Clayton64195a22011-02-23 00:35:02 +0000395 m_triple (),
396 m_core (kCore_invalid),
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000397 m_byte_order (eByteOrderInvalid),
398 m_distribution_id ()
Greg Clayton64195a22011-02-23 00:35:02 +0000399{
400 SetArchitecture (arch_type, cpu, subtype);
401}
402
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000403ArchSpec::~ArchSpec()
404{
405}
406
Greg Clayton64195a22011-02-23 00:35:02 +0000407//===----------------------------------------------------------------------===//
408// Assignment and initialization.
409
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000410const ArchSpec&
411ArchSpec::operator= (const ArchSpec& rhs)
412{
413 if (this != &rhs)
414 {
Greg Clayton514487e2011-02-15 21:59:32 +0000415 m_triple = rhs.m_triple;
Greg Clayton64195a22011-02-23 00:35:02 +0000416 m_core = rhs.m_core;
Greg Clayton514487e2011-02-15 21:59:32 +0000417 m_byte_order = rhs.m_byte_order;
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000418 m_distribution_id = rhs.m_distribution_id;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000419 }
420 return *this;
421}
422
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000423void
424ArchSpec::Clear()
425{
Greg Clayton514487e2011-02-15 21:59:32 +0000426 m_triple = llvm::Triple();
Greg Clayton64195a22011-02-23 00:35:02 +0000427 m_core = kCore_invalid;
428 m_byte_order = eByteOrderInvalid;
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000429 m_distribution_id.Clear ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000430}
431
Greg Clayton64195a22011-02-23 00:35:02 +0000432//===----------------------------------------------------------------------===//
433// Predicates.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000434
Greg Clayton41f92322010-06-11 03:25:34 +0000435
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000436const char *
Greg Clayton64195a22011-02-23 00:35:02 +0000437ArchSpec::GetArchitectureName () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000438{
Greg Clayton64195a22011-02-23 00:35:02 +0000439 const CoreDefinition *core_def = FindCoreDefinition (m_core);
440 if (core_def)
441 return core_def->name;
442 return "unknown";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000443}
444
Greg Clayton64195a22011-02-23 00:35:02 +0000445uint32_t
446ArchSpec::GetMachOCPUType () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000447{
Greg Clayton64195a22011-02-23 00:35:02 +0000448 const CoreDefinition *core_def = FindCoreDefinition (m_core);
449 if (core_def)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000450 {
Greg Clayton64195a22011-02-23 00:35:02 +0000451 const ArchDefinitionEntry *arch_def = FindArchDefinitionEntry (&g_macho_arch_def, core_def->core);
452 if (arch_def)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000453 {
Greg Clayton64195a22011-02-23 00:35:02 +0000454 return arch_def->cpu;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000455 }
456 }
Greg Clayton64195a22011-02-23 00:35:02 +0000457 return LLDB_INVALID_CPUTYPE;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000458}
459
Greg Clayton64195a22011-02-23 00:35:02 +0000460uint32_t
461ArchSpec::GetMachOCPUSubType () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000462{
Greg Clayton64195a22011-02-23 00:35:02 +0000463 const CoreDefinition *core_def = FindCoreDefinition (m_core);
464 if (core_def)
465 {
466 const ArchDefinitionEntry *arch_def = FindArchDefinitionEntry (&g_macho_arch_def, core_def->core);
467 if (arch_def)
468 {
Greg Clayton1cb64962011-03-24 04:28:38 +0000469 return arch_def->sub;
Greg Clayton64195a22011-02-23 00:35:02 +0000470 }
471 }
472 return LLDB_INVALID_CPUTYPE;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000473}
474
Greg Clayton64195a22011-02-23 00:35:02 +0000475llvm::Triple::ArchType
476ArchSpec::GetMachine () const
477{
478 const CoreDefinition *core_def = FindCoreDefinition (m_core);
479 if (core_def)
480 return core_def->machine;
481
482 return llvm::Triple::UnknownArch;
483}
484
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000485const ConstString&
486ArchSpec::GetDistributionId () const
487{
488 return m_distribution_id;
489}
490
491void
492ArchSpec::SetDistributionId (const char* distribution_id)
493{
494 m_distribution_id.SetCString (distribution_id);
495}
496
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000497uint32_t
498ArchSpec::GetAddressByteSize() const
499{
Greg Clayton64195a22011-02-23 00:35:02 +0000500 const CoreDefinition *core_def = FindCoreDefinition (m_core);
501 if (core_def)
502 return core_def->addr_byte_size;
Greg Clayton41f92322010-06-11 03:25:34 +0000503 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000504}
505
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000506ByteOrder
507ArchSpec::GetDefaultEndian () const
508{
Greg Clayton64195a22011-02-23 00:35:02 +0000509 const CoreDefinition *core_def = FindCoreDefinition (m_core);
510 if (core_def)
511 return core_def->default_byte_order;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000512 return eByteOrderInvalid;
513}
514
Greg Clayton64195a22011-02-23 00:35:02 +0000515lldb::ByteOrder
516ArchSpec::GetByteOrder () const
517{
518 if (m_byte_order == eByteOrderInvalid)
519 return GetDefaultEndian();
520 return m_byte_order;
521}
522
523//===----------------------------------------------------------------------===//
524// Mutators.
525
526bool
527ArchSpec::SetTriple (const llvm::Triple &triple)
528{
529 m_triple = triple;
530
531 llvm::StringRef arch_name (m_triple.getArchName());
532 const CoreDefinition *core_def = FindCoreDefinition (arch_name);
533 if (core_def)
534 {
535 m_core = core_def->core;
Greg Claytoneb0103f2011-04-07 22:46:35 +0000536 // Set the byte order to the default byte order for an architecture.
537 // This can be modified if needed for cases when cores handle both
538 // big and little endian
539 m_byte_order = core_def->default_byte_order;
Greg Clayton64195a22011-02-23 00:35:02 +0000540 }
541 else
542 {
543 Clear();
544 }
545
546
547 return IsValid();
548}
549
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000550static bool
551ParseMachCPUDashSubtypeTriple (const char *triple_cstr, ArchSpec &arch)
552{
553 // Accept "12-10" or "12.10" as cpu type/subtype
554 if (isdigit(triple_cstr[0]))
555 {
556 char *end = NULL;
557 errno = 0;
Greg Claytonc7bece562013-01-25 18:06:21 +0000558 uint32_t cpu = (uint32_t)::strtoul (triple_cstr, &end, 0);
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000559 if (errno == 0 && cpu != 0 && end && ((*end == '-') || (*end == '.')))
560 {
561 errno = 0;
Greg Claytonc7bece562013-01-25 18:06:21 +0000562 uint32_t sub = (uint32_t)::strtoul (end + 1, &end, 0);
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000563 if (errno == 0 && end && ((*end == '-') || (*end == '.') || (*end == '\0')))
564 {
565 if (arch.SetArchitecture (eArchTypeMachO, cpu, sub))
566 {
567 if (*end == '-')
568 {
569 llvm::StringRef vendor_os (end + 1);
570 size_t dash_pos = vendor_os.find('-');
571 if (dash_pos != llvm::StringRef::npos)
572 {
573 llvm::StringRef vendor_str(vendor_os.substr(0, dash_pos));
574 arch.GetTriple().setVendorName(vendor_str);
575 const size_t vendor_start_pos = dash_pos+1;
Greg Claytonc7bece562013-01-25 18:06:21 +0000576 dash_pos = vendor_os.find('-', vendor_start_pos);
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000577 if (dash_pos == llvm::StringRef::npos)
578 {
579 if (vendor_start_pos < vendor_os.size())
580 arch.GetTriple().setOSName(vendor_os.substr(vendor_start_pos));
581 }
582 else
583 {
584 arch.GetTriple().setOSName(vendor_os.substr(vendor_start_pos, dash_pos - vendor_start_pos));
585 }
586 }
587 }
588 return true;
589 }
590 }
591 }
592 }
593 return false;
594}
Greg Clayton64195a22011-02-23 00:35:02 +0000595bool
Greg Clayton70512312012-05-08 01:45:38 +0000596ArchSpec::SetTriple (const char *triple_cstr)
Greg Clayton64195a22011-02-23 00:35:02 +0000597{
Greg Clayton23aca092011-08-12 23:32:52 +0000598 if (triple_cstr && triple_cstr[0])
Greg Clayton64195a22011-02-23 00:35:02 +0000599 {
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000600 if (ParseMachCPUDashSubtypeTriple (triple_cstr, *this))
601 return true;
602
Greg Clayton64195a22011-02-23 00:35:02 +0000603 llvm::StringRef triple_stref (triple_cstr);
604 if (triple_stref.startswith (LLDB_ARCH_DEFAULT))
605 {
606 // Special case for the current host default architectures...
607 if (triple_stref.equals (LLDB_ARCH_DEFAULT_32BIT))
608 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture32);
609 else if (triple_stref.equals (LLDB_ARCH_DEFAULT_64BIT))
610 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture64);
611 else if (triple_stref.equals (LLDB_ARCH_DEFAULT))
612 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture);
613 }
614 else
615 {
616 std::string normalized_triple_sstr (llvm::Triple::normalize(triple_stref));
617 triple_stref = normalized_triple_sstr;
Greg Clayton70512312012-05-08 01:45:38 +0000618 SetTriple (llvm::Triple (triple_stref));
619 }
620 }
621 else
622 Clear();
623 return IsValid();
624}
625
626bool
627ArchSpec::SetTriple (const char *triple_cstr, Platform *platform)
628{
629 if (triple_cstr && triple_cstr[0])
630 {
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000631 if (ParseMachCPUDashSubtypeTriple (triple_cstr, *this))
632 return true;
633
Greg Clayton70512312012-05-08 01:45:38 +0000634 llvm::StringRef triple_stref (triple_cstr);
635 if (triple_stref.startswith (LLDB_ARCH_DEFAULT))
636 {
637 // Special case for the current host default architectures...
638 if (triple_stref.equals (LLDB_ARCH_DEFAULT_32BIT))
639 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture32);
640 else if (triple_stref.equals (LLDB_ARCH_DEFAULT_64BIT))
641 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture64);
642 else if (triple_stref.equals (LLDB_ARCH_DEFAULT))
643 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture);
644 }
645 else
646 {
647 ArchSpec raw_arch (triple_cstr);
648
649 std::string normalized_triple_sstr (llvm::Triple::normalize(triple_stref));
650 triple_stref = normalized_triple_sstr;
Greg Claytoneb0103f2011-04-07 22:46:35 +0000651 llvm::Triple normalized_triple (triple_stref);
652
653 const bool os_specified = normalized_triple.getOSName().size() > 0;
654 const bool vendor_specified = normalized_triple.getVendorName().size() > 0;
655 const bool env_specified = normalized_triple.getEnvironmentName().size() > 0;
656
657 // If we got an arch only, then default the vendor, os, environment
658 // to match the platform if one is supplied
659 if (!(os_specified || vendor_specified || env_specified))
660 {
661 if (platform)
662 {
663 // If we were given a platform, use the platform's system
664 // architecture. If this is not available (might not be
665 // connected) use the first supported architecture.
Greg Clayton70512312012-05-08 01:45:38 +0000666 ArchSpec compatible_arch;
Greg Clayton1e0c8842013-01-11 20:49:54 +0000667 if (platform->IsCompatibleArchitecture (raw_arch, false, &compatible_arch))
Greg Claytoneb0103f2011-04-07 22:46:35 +0000668 {
Greg Clayton70512312012-05-08 01:45:38 +0000669 if (compatible_arch.IsValid())
670 {
671 const llvm::Triple &compatible_triple = compatible_arch.GetTriple();
672 if (!vendor_specified)
673 normalized_triple.setVendor(compatible_triple.getVendor());
674 if (!os_specified)
675 normalized_triple.setOS(compatible_triple.getOS());
676 if (!env_specified && compatible_triple.getEnvironmentName().size())
677 normalized_triple.setEnvironment(compatible_triple.getEnvironment());
678 }
Greg Claytoneb0103f2011-04-07 22:46:35 +0000679 }
Greg Clayton70512312012-05-08 01:45:38 +0000680 else
Greg Claytoneb0103f2011-04-07 22:46:35 +0000681 {
Greg Clayton70512312012-05-08 01:45:38 +0000682 *this = raw_arch;
683 return IsValid();
Greg Claytoneb0103f2011-04-07 22:46:35 +0000684 }
685 }
686 else
687 {
688 // No platform specified, fall back to the host system for
689 // the default vendor, os, and environment.
Sean Callananbfb237bc2011-11-04 22:46:46 +0000690 llvm::Triple host_triple(llvm::sys::getDefaultTargetTriple());
Greg Clayton70512312012-05-08 01:45:38 +0000691 if (!vendor_specified)
692 normalized_triple.setVendor(host_triple.getVendor());
693 if (!vendor_specified)
694 normalized_triple.setOS(host_triple.getOS());
695 if (!env_specified && host_triple.getEnvironmentName().size())
696 normalized_triple.setEnvironment(host_triple.getEnvironment());
Greg Claytoneb0103f2011-04-07 22:46:35 +0000697 }
698 }
699 SetTriple (normalized_triple);
Greg Clayton64195a22011-02-23 00:35:02 +0000700 }
701 }
702 else
703 Clear();
704 return IsValid();
705}
706
Greg Clayton64195a22011-02-23 00:35:02 +0000707bool
Greg Claytone0d378b2011-03-24 21:19:54 +0000708ArchSpec::SetArchitecture (ArchitectureType arch_type, uint32_t cpu, uint32_t sub)
Greg Clayton64195a22011-02-23 00:35:02 +0000709{
710 m_core = kCore_invalid;
711 bool update_triple = true;
712 const ArchDefinition *arch_def = FindArchDefinition(arch_type);
713 if (arch_def)
714 {
715 const ArchDefinitionEntry *arch_def_entry = FindArchDefinitionEntry (arch_def, cpu, sub);
716 if (arch_def_entry)
717 {
718 const CoreDefinition *core_def = FindCoreDefinition (arch_def_entry->core);
719 if (core_def)
720 {
721 m_core = core_def->core;
722 update_triple = false;
Greg Clayton593577a2011-09-21 03:57:31 +0000723 // Always use the architecture name because it might be more descriptive
724 // than the architecture enum ("armv7" -> llvm::Triple::arm).
725 m_triple.setArchName(llvm::StringRef(core_def->name));
Greg Clayton64195a22011-02-23 00:35:02 +0000726 if (arch_type == eArchTypeMachO)
727 {
728 m_triple.setVendor (llvm::Triple::Apple);
Greg Clayton70512312012-05-08 01:45:38 +0000729
730 switch (core_def->machine)
731 {
732 case llvm::Triple::arm:
733 case llvm::Triple::thumb:
734 m_triple.setOS (llvm::Triple::IOS);
735 break;
736
737 case llvm::Triple::x86:
738 case llvm::Triple::x86_64:
739 default:
740 m_triple.setOS (llvm::Triple::MacOSX);
741 break;
742 }
Greg Clayton64195a22011-02-23 00:35:02 +0000743 }
744 else
745 {
746 m_triple.setVendor (llvm::Triple::UnknownVendor);
747 m_triple.setOS (llvm::Triple::UnknownOS);
748 }
Greg Clayton593577a2011-09-21 03:57:31 +0000749 // Fall back onto setting the machine type if the arch by name failed...
750 if (m_triple.getArch () == llvm::Triple::UnknownArch)
751 m_triple.setArch (core_def->machine);
Greg Clayton64195a22011-02-23 00:35:02 +0000752 }
753 }
754 }
755 CoreUpdated(update_triple);
756 return IsValid();
757}
758
Greg Clayton357132e2011-03-26 19:14:58 +0000759uint32_t
760ArchSpec::GetMinimumOpcodeByteSize() const
Greg Clayton64195a22011-02-23 00:35:02 +0000761{
Greg Clayton357132e2011-03-26 19:14:58 +0000762 const CoreDefinition *core_def = FindCoreDefinition (m_core);
763 if (core_def)
764 return core_def->min_opcode_byte_size;
765 return 0;
766}
767
768uint32_t
769ArchSpec::GetMaximumOpcodeByteSize() const
770{
771 const CoreDefinition *core_def = FindCoreDefinition (m_core);
772 if (core_def)
773 return core_def->max_opcode_byte_size;
774 return 0;
Greg Clayton64195a22011-02-23 00:35:02 +0000775}
776
Jason Molendaba813dc2012-11-04 03:20:05 +0000777bool
778ArchSpec::IsExactMatch (const ArchSpec& rhs) const
779{
Sean Callananbf4b7be2012-12-13 22:07:14 +0000780 return IsEqualTo (rhs, true);
Jason Molendaba813dc2012-11-04 03:20:05 +0000781}
782
783bool
784ArchSpec::IsCompatibleMatch (const ArchSpec& rhs) const
785{
Sean Callananbf4b7be2012-12-13 22:07:14 +0000786 return IsEqualTo (rhs, false);
Jason Molendaba813dc2012-11-04 03:20:05 +0000787}
788
789bool
Sean Callananbf4b7be2012-12-13 22:07:14 +0000790ArchSpec::IsEqualTo (const ArchSpec& rhs, bool exact_match) const
Jason Molendaba813dc2012-11-04 03:20:05 +0000791{
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000792 // explicitly ignoring m_distribution_id in this method.
793
Jason Molendaba813dc2012-11-04 03:20:05 +0000794 if (GetByteOrder() != rhs.GetByteOrder())
795 return false;
796
797 const ArchSpec::Core lhs_core = GetCore ();
798 const ArchSpec::Core rhs_core = rhs.GetCore ();
799
800 const bool core_match = cores_match (lhs_core, rhs_core, true, exact_match);
801
802 if (core_match)
803 {
804 const llvm::Triple &lhs_triple = GetTriple();
805 const llvm::Triple &rhs_triple = rhs.GetTriple();
806
807 const llvm::Triple::VendorType lhs_triple_vendor = lhs_triple.getVendor();
808 const llvm::Triple::VendorType rhs_triple_vendor = rhs_triple.getVendor();
809 if (lhs_triple_vendor != rhs_triple_vendor)
810 {
Sean Callananbf4b7be2012-12-13 22:07:14 +0000811 if (exact_match)
812 {
813 const bool rhs_vendor_specified = rhs.TripleVendorWasSpecified();
814 const bool lhs_vendor_specified = TripleVendorWasSpecified();
815 // Both architectures had the vendor specified, so if they aren't
816 // equal then we return false
817 if (rhs_vendor_specified && lhs_vendor_specified)
818 return false;
819 }
Jason Molendaba813dc2012-11-04 03:20:05 +0000820
821 // Only fail if both vendor types are not unknown
822 if (lhs_triple_vendor != llvm::Triple::UnknownVendor &&
823 rhs_triple_vendor != llvm::Triple::UnknownVendor)
824 return false;
825 }
826
827 const llvm::Triple::OSType lhs_triple_os = lhs_triple.getOS();
828 const llvm::Triple::OSType rhs_triple_os = rhs_triple.getOS();
829 if (lhs_triple_os != rhs_triple_os)
830 {
Sean Callananbf4b7be2012-12-13 22:07:14 +0000831 if (exact_match)
832 {
833 const bool rhs_os_specified = rhs.TripleOSWasSpecified();
834 const bool lhs_os_specified = TripleOSWasSpecified();
835 // Both architectures had the OS specified, so if they aren't
836 // equal then we return false
837 if (rhs_os_specified && lhs_os_specified)
838 return false;
839 }
Jason Molendaba813dc2012-11-04 03:20:05 +0000840 // Only fail if both os types are not unknown
841 if (lhs_triple_os != llvm::Triple::UnknownOS &&
842 rhs_triple_os != llvm::Triple::UnknownOS)
843 return false;
844 }
845
846 const llvm::Triple::EnvironmentType lhs_triple_env = lhs_triple.getEnvironment();
847 const llvm::Triple::EnvironmentType rhs_triple_env = rhs_triple.getEnvironment();
848
849 if (lhs_triple_env != rhs_triple_env)
850 {
851 // Only fail if both environment types are not unknown
852 if (lhs_triple_env != llvm::Triple::UnknownEnvironment &&
853 rhs_triple_env != llvm::Triple::UnknownEnvironment)
854 return false;
855 }
856 return true;
857 }
858 return false;
859}
860
Greg Clayton64195a22011-02-23 00:35:02 +0000861//===----------------------------------------------------------------------===//
862// Helper methods.
863
864void
865ArchSpec::CoreUpdated (bool update_triple)
866{
867 const CoreDefinition *core_def = FindCoreDefinition (m_core);
868 if (core_def)
869 {
870 if (update_triple)
871 m_triple = llvm::Triple(core_def->name, "unknown", "unknown");
872 m_byte_order = core_def->default_byte_order;
873 }
874 else
875 {
876 if (update_triple)
877 m_triple = llvm::Triple();
878 m_byte_order = eByteOrderInvalid;
879 }
880}
881
882//===----------------------------------------------------------------------===//
883// Operators.
884
Greg Clayton70512312012-05-08 01:45:38 +0000885static bool
Jason Molendaba813dc2012-11-04 03:20:05 +0000886cores_match (const ArchSpec::Core core1, const ArchSpec::Core core2, bool try_inverse, bool enforce_exact_match)
Greg Clayton70512312012-05-08 01:45:38 +0000887{
Jason Molendaba813dc2012-11-04 03:20:05 +0000888 if (core1 == core2)
889 return true;
890
Greg Clayton70512312012-05-08 01:45:38 +0000891 switch (core1)
892 {
Greg Clayton70512312012-05-08 01:45:38 +0000893 case ArchSpec::kCore_any:
894 return true;
895
896 case ArchSpec::kCore_arm_any:
897 if (core2 >= ArchSpec::kCore_arm_first && core2 <= ArchSpec::kCore_arm_last)
898 return true;
899 if (core2 >= ArchSpec::kCore_thumb_first && core2 <= ArchSpec::kCore_thumb_last)
900 return true;
901 if (core2 == ArchSpec::kCore_arm_any)
902 return true;
903 break;
Greg Claytona86dc432014-01-22 23:42:03 +0000904
Greg Clayton70512312012-05-08 01:45:38 +0000905 case ArchSpec::kCore_x86_32_any:
906 if ((core2 >= ArchSpec::kCore_x86_32_first && core2 <= ArchSpec::kCore_x86_32_last) || (core2 == ArchSpec::kCore_x86_32_any))
907 return true;
908 break;
909
910 case ArchSpec::kCore_ppc_any:
911 if ((core2 >= ArchSpec::kCore_ppc_first && core2 <= ArchSpec::kCore_ppc_last) || (core2 == ArchSpec::kCore_ppc_any))
912 return true;
913 break;
914
915 case ArchSpec::kCore_ppc64_any:
916 if ((core2 >= ArchSpec::kCore_ppc64_first && core2 <= ArchSpec::kCore_ppc64_last) || (core2 == ArchSpec::kCore_ppc64_any))
917 return true;
918 break;
919
Jason Molendaa3a04522013-09-27 23:21:54 +0000920 case ArchSpec::eCore_arm_armv6m:
921 if (!enforce_exact_match)
922 {
923 try_inverse = false;
Jason Molendac7cda272013-09-27 23:29:10 +0000924 if (core2 == ArchSpec::eCore_arm_armv7)
Jason Molendaa3a04522013-09-27 23:21:54 +0000925 return true;
926 }
Deepak Panickal6d3df422014-02-19 11:16:46 +0000927
928 case ArchSpec::kCore_hexagon_any:
929 if ((core2 >= ArchSpec::kCore_hexagon_first && core2 <= ArchSpec::kCore_hexagon_last) || (core2 == ArchSpec::kCore_hexagon_any))
930 return true;
Jason Molendaa3a04522013-09-27 23:21:54 +0000931 break;
932
Jason Molenda7a1559c2013-03-08 01:20:17 +0000933 case ArchSpec::eCore_arm_armv7m:
934 case ArchSpec::eCore_arm_armv7em:
Johnny Chen1083b0d2012-08-28 22:53:40 +0000935 case ArchSpec::eCore_arm_armv7f:
936 case ArchSpec::eCore_arm_armv7k:
937 case ArchSpec::eCore_arm_armv7s:
Jason Molendaba813dc2012-11-04 03:20:05 +0000938 if (!enforce_exact_match)
939 {
940 try_inverse = false;
941 if (core2 == ArchSpec::eCore_arm_armv7)
942 return true;
943 }
Johnny Chen1083b0d2012-08-28 22:53:40 +0000944 break;
945
Greg Clayton70512312012-05-08 01:45:38 +0000946 default:
947 break;
948 }
949 if (try_inverse)
Jason Molendaba813dc2012-11-04 03:20:05 +0000950 return cores_match (core2, core1, false, enforce_exact_match);
Greg Clayton70512312012-05-08 01:45:38 +0000951 return false;
952}
953
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000954bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000955lldb_private::operator<(const ArchSpec& lhs, const ArchSpec& rhs)
956{
Greg Clayton64195a22011-02-23 00:35:02 +0000957 const ArchSpec::Core lhs_core = lhs.GetCore ();
958 const ArchSpec::Core rhs_core = rhs.GetCore ();
959 return lhs_core < rhs_core;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000960}