blob: f9db53b92a3bdf4889b6d7fea4eba70fc1faa237 [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
Saleem Abdulrasool28606952014-06-27 05:17:41 +000017#include "llvm/ADT/STLExtras.h"
Charles Davis237ad972013-08-27 05:04:33 +000018#include "llvm/Support/COFF.h"
Greg Clayton41f92322010-06-11 03:25:34 +000019#include "llvm/Support/ELF.h"
Stephen Wilsonfacebfc2011-02-24 19:13:58 +000020#include "llvm/Support/Host.h"
Jim Ingham46d005d2014-04-02 22:53:21 +000021#include "lldb/Utility/SafeMachO.h"
Greg Claytone795f1b2012-08-08 01:19:34 +000022#include "lldb/Core/RegularExpression.h"
Greg Clayton514487e2011-02-15 21:59:32 +000023#include "lldb/Host/Endian.h"
24#include "lldb/Host/Host.h"
Greg Claytoneb0103f2011-04-07 22:46:35 +000025#include "lldb/Target/Platform.h"
Greg Clayton41f92322010-06-11 03:25:34 +000026
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027using namespace lldb;
28using namespace lldb_private;
29
Greg Clayton64195a22011-02-23 00:35:02 +000030#define ARCH_SPEC_SEPARATOR_CHAR '-'
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031
Jason Molendaba813dc2012-11-04 03:20:05 +000032
33static bool cores_match (const ArchSpec::Core core1, const ArchSpec::Core core2, bool try_inverse, bool enforce_exact_match);
34
Greg Clayton64195a22011-02-23 00:35:02 +000035namespace lldb_private {
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036
Greg Clayton64195a22011-02-23 00:35:02 +000037 struct CoreDefinition
38 {
39 ByteOrder default_byte_order;
40 uint32_t addr_byte_size;
Greg Clayton357132e2011-03-26 19:14:58 +000041 uint32_t min_opcode_byte_size;
42 uint32_t max_opcode_byte_size;
Greg Clayton64195a22011-02-23 00:35:02 +000043 llvm::Triple::ArchType machine;
44 ArchSpec::Core core;
45 const char *name;
46 };
47
48}
49
50// This core information can be looked using the ArchSpec::Core as the index
51static const CoreDefinition g_core_definitions[ArchSpec::kNumCores] =
Chris Lattner30fdc8d2010-06-08 16:52:24 +000052{
Greg Clayton357132e2011-03-26 19:14:58 +000053 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_generic , "arm" },
54 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv4 , "armv4" },
55 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv4t , "armv4t" },
56 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv5 , "armv5" },
Greg Claytonb5c39fe2011-12-16 18:15:52 +000057 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv5e , "armv5e" },
Greg Clayton357132e2011-03-26 19:14:58 +000058 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv5t , "armv5t" },
59 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv6 , "armv6" },
Jason Molendaa3a04522013-09-27 23:21:54 +000060 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv6m , "armv6m" },
Greg Clayton357132e2011-03-26 19:14:58 +000061 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7 , "armv7" },
62 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7f , "armv7f" },
Greg Clayton357132e2011-03-26 19:14:58 +000063 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7s , "armv7s" },
Jason Molenda7a1559c2013-03-08 01:20:17 +000064 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7k , "armv7k" },
65 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7m , "armv7m" },
66 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7em , "armv7em" },
Greg Clayton357132e2011-03-26 19:14:58 +000067 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_xscale , "xscale" },
Greg Claytonb5c39fe2011-12-16 18:15:52 +000068 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumb , "thumb" },
69 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv4t , "thumbv4t" },
70 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv5 , "thumbv5" },
71 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv5e , "thumbv5e" },
72 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv6 , "thumbv6" },
Jason Molendaa3a04522013-09-27 23:21:54 +000073 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv6m , "thumbv6m" },
Greg Claytonb5c39fe2011-12-16 18:15:52 +000074 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7 , "thumbv7" },
75 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7f , "thumbv7f" },
Greg Claytonb5c39fe2011-12-16 18:15:52 +000076 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7s , "thumbv7s" },
Jason Molenda7a1559c2013-03-08 01:20:17 +000077 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7k , "thumbv7k" },
78 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7m , "thumbv7m" },
79 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7em , "thumbv7em" },
Jason Molendaa3329782014-03-29 18:54:20 +000080 { eByteOrderLittle, 8, 4, 4, llvm::Triple::arm64 , ArchSpec::eCore_arm_arm64 , "arm64" },
Ed Masteb73f8442013-10-10 00:59:47 +000081
82 { eByteOrderBig , 8, 4, 4, llvm::Triple::mips64 , ArchSpec::eCore_mips64 , "mips64" },
Greg Clayton64195a22011-02-23 00:35:02 +000083
Greg Clayton83b162d2013-08-12 18:34:04 +000084 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_generic , "ppc" },
85 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc601 , "ppc601" },
86 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc602 , "ppc602" },
87 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc603 , "ppc603" },
88 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc603e , "ppc603e" },
89 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc603ev , "ppc603ev" },
90 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc604 , "ppc604" },
91 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc604e , "ppc604e" },
92 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc620 , "ppc620" },
93 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc750 , "ppc750" },
94 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc7400 , "ppc7400" },
95 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc7450 , "ppc7450" },
96 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc970 , "ppc970" },
Greg Clayton64195a22011-02-23 00:35:02 +000097
Greg Clayton83b162d2013-08-12 18:34:04 +000098 { eByteOrderBig , 8, 4, 4, llvm::Triple::ppc64 , ArchSpec::eCore_ppc64_generic , "ppc64" },
99 { eByteOrderBig , 8, 4, 4, llvm::Triple::ppc64 , ArchSpec::eCore_ppc64_ppc970_64 , "ppc970-64" },
Greg Clayton64195a22011-02-23 00:35:02 +0000100
Greg Clayton357132e2011-03-26 19:14:58 +0000101 { eByteOrderLittle, 4, 4, 4, llvm::Triple::sparc , ArchSpec::eCore_sparc_generic , "sparc" },
102 { eByteOrderLittle, 8, 4, 4, llvm::Triple::sparcv9, ArchSpec::eCore_sparc9_generic , "sparcv9" },
Greg Clayton64195a22011-02-23 00:35:02 +0000103
Greg Claytonab65b342011-04-13 22:47:15 +0000104 { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i386 , "i386" },
105 { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i486 , "i486" },
106 { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i486sx , "i486sx" },
Virgile Bello97a70e42014-04-08 14:48:48 +0000107 { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i686 , "i686" },
Greg Clayton64195a22011-02-23 00:35:02 +0000108
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000109 { eByteOrderLittle, 8, 1, 15, llvm::Triple::x86_64 , ArchSpec::eCore_x86_64_x86_64 , "x86_64" },
Greg Claytona86dc432014-01-22 23:42:03 +0000110 { eByteOrderLittle, 8, 1, 15, llvm::Triple::x86_64 , ArchSpec::eCore_x86_64_x86_64h , "x86_64h" },
Deepak Panickal6d3df422014-02-19 11:16:46 +0000111 { eByteOrderLittle, 4, 4, 4, llvm::Triple::hexagon , ArchSpec::eCore_hexagon_generic, "hexagon" },
112 { eByteOrderLittle, 4, 4, 4, llvm::Triple::hexagon , ArchSpec::eCore_hexagon_hexagonv4, "hexagonv4" },
113 { eByteOrderLittle, 4, 4, 4, llvm::Triple::hexagon , ArchSpec::eCore_hexagon_hexagonv5, "hexagonv5" },
114
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000115 { eByteOrderLittle, 4, 4, 4 , llvm::Triple::UnknownArch , ArchSpec::eCore_uknownMach32 , "unknown-mach-32" },
116 { eByteOrderLittle, 8, 4, 4 , llvm::Triple::UnknownArch , ArchSpec::eCore_uknownMach64 , "unknown-mach-64" }
Greg Clayton64195a22011-02-23 00:35:02 +0000117};
118
119struct ArchDefinitionEntry
120{
121 ArchSpec::Core core;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000122 uint32_t cpu;
123 uint32_t sub;
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000124 uint32_t cpu_mask;
125 uint32_t sub_mask;
Greg Clayton64195a22011-02-23 00:35:02 +0000126};
127
128struct ArchDefinition
129{
130 ArchitectureType type;
131 size_t num_entries;
132 const ArchDefinitionEntry *entries;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000133 const char *name;
134};
135
Greg Clayton41f92322010-06-11 03:25:34 +0000136
Greg Claytonc7bece562013-01-25 18:06:21 +0000137size_t
Greg Claytonab65b342011-04-13 22:47:15 +0000138ArchSpec::AutoComplete (const char *name, StringList &matches)
139{
140 uint32_t i;
141 if (name && name[0])
142 {
143 for (i = 0; i < ArchSpec::kNumCores; ++i)
144 {
145 if (NameMatches(g_core_definitions[i].name, eNameMatchStartsWith, name))
146 matches.AppendString (g_core_definitions[i].name);
147 }
148 }
149 else
150 {
151 for (i = 0; i < ArchSpec::kNumCores; ++i)
152 matches.AppendString (g_core_definitions[i].name);
153 }
154 return matches.GetSize();
155}
156
157
158
Greg Clayton64195a22011-02-23 00:35:02 +0000159#define CPU_ANY (UINT32_MAX)
160
161//===----------------------------------------------------------------------===//
162// A table that gets searched linearly for matches. This table is used to
163// convert cpu type and subtypes to architecture names, and to convert
164// architecture names to cpu types and subtypes. The ordering is important and
165// allows the precedence to be set when the table is built.
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000166#define SUBTYPE_MASK 0x00FFFFFFu
Greg Clayton64195a22011-02-23 00:35:02 +0000167static const ArchDefinitionEntry g_macho_arch_entries[] =
Greg Clayton41f92322010-06-11 03:25:34 +0000168{
Charles Davis510938e2013-08-27 05:04:57 +0000169 { ArchSpec::eCore_arm_generic , llvm::MachO::CPU_TYPE_ARM , CPU_ANY, UINT32_MAX , UINT32_MAX },
170 { ArchSpec::eCore_arm_generic , llvm::MachO::CPU_TYPE_ARM , 0 , UINT32_MAX , SUBTYPE_MASK },
171 { ArchSpec::eCore_arm_armv4 , llvm::MachO::CPU_TYPE_ARM , 5 , UINT32_MAX , SUBTYPE_MASK },
172 { ArchSpec::eCore_arm_armv4t , llvm::MachO::CPU_TYPE_ARM , 5 , UINT32_MAX , SUBTYPE_MASK },
173 { ArchSpec::eCore_arm_armv6 , llvm::MachO::CPU_TYPE_ARM , 6 , UINT32_MAX , SUBTYPE_MASK },
Jason Molenda64a11732013-10-08 03:01:08 +0000174 { ArchSpec::eCore_arm_armv6m , llvm::MachO::CPU_TYPE_ARM , 14 , UINT32_MAX , SUBTYPE_MASK },
Charles Davis510938e2013-08-27 05:04:57 +0000175 { ArchSpec::eCore_arm_armv5 , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
176 { ArchSpec::eCore_arm_armv5e , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
177 { ArchSpec::eCore_arm_armv5t , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
178 { ArchSpec::eCore_arm_xscale , llvm::MachO::CPU_TYPE_ARM , 8 , UINT32_MAX , SUBTYPE_MASK },
179 { ArchSpec::eCore_arm_armv7 , llvm::MachO::CPU_TYPE_ARM , 9 , UINT32_MAX , SUBTYPE_MASK },
180 { ArchSpec::eCore_arm_armv7f , llvm::MachO::CPU_TYPE_ARM , 10 , UINT32_MAX , SUBTYPE_MASK },
181 { ArchSpec::eCore_arm_armv7s , llvm::MachO::CPU_TYPE_ARM , 11 , UINT32_MAX , SUBTYPE_MASK },
182 { ArchSpec::eCore_arm_armv7k , llvm::MachO::CPU_TYPE_ARM , 12 , UINT32_MAX , SUBTYPE_MASK },
183 { ArchSpec::eCore_arm_armv7m , llvm::MachO::CPU_TYPE_ARM , 15 , UINT32_MAX , SUBTYPE_MASK },
184 { ArchSpec::eCore_arm_armv7em , llvm::MachO::CPU_TYPE_ARM , 16 , UINT32_MAX , SUBTYPE_MASK },
Jason Molendaa3329782014-03-29 18:54:20 +0000185 { ArchSpec::eCore_arm_arm64 , llvm::MachO::CPU_TYPE_ARM64 , CPU_ANY, UINT32_MAX , SUBTYPE_MASK },
186 { ArchSpec::eCore_arm_arm64 , llvm::MachO::CPU_TYPE_ARM64 , 0 , UINT32_MAX , SUBTYPE_MASK },
187 { ArchSpec::eCore_arm_arm64 , llvm::MachO::CPU_TYPE_ARM64 , 1 , UINT32_MAX , SUBTYPE_MASK },
188 { ArchSpec::eCore_arm_arm64 , llvm::MachO::CPU_TYPE_ARM64 , 13 , UINT32_MAX , SUBTYPE_MASK },
Charles Davis510938e2013-08-27 05:04:57 +0000189 { ArchSpec::eCore_thumb , llvm::MachO::CPU_TYPE_ARM , 0 , UINT32_MAX , SUBTYPE_MASK },
190 { ArchSpec::eCore_thumbv4t , llvm::MachO::CPU_TYPE_ARM , 5 , UINT32_MAX , SUBTYPE_MASK },
191 { ArchSpec::eCore_thumbv5 , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
192 { ArchSpec::eCore_thumbv5e , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
193 { ArchSpec::eCore_thumbv6 , llvm::MachO::CPU_TYPE_ARM , 6 , UINT32_MAX , SUBTYPE_MASK },
Jason Molenda64a11732013-10-08 03:01:08 +0000194 { ArchSpec::eCore_thumbv6m , llvm::MachO::CPU_TYPE_ARM , 14 , UINT32_MAX , SUBTYPE_MASK },
Charles Davis510938e2013-08-27 05:04:57 +0000195 { ArchSpec::eCore_thumbv7 , llvm::MachO::CPU_TYPE_ARM , 9 , UINT32_MAX , SUBTYPE_MASK },
196 { ArchSpec::eCore_thumbv7f , llvm::MachO::CPU_TYPE_ARM , 10 , UINT32_MAX , SUBTYPE_MASK },
197 { ArchSpec::eCore_thumbv7s , llvm::MachO::CPU_TYPE_ARM , 11 , UINT32_MAX , SUBTYPE_MASK },
198 { ArchSpec::eCore_thumbv7k , llvm::MachO::CPU_TYPE_ARM , 12 , UINT32_MAX , SUBTYPE_MASK },
199 { ArchSpec::eCore_thumbv7m , llvm::MachO::CPU_TYPE_ARM , 15 , UINT32_MAX , SUBTYPE_MASK },
200 { ArchSpec::eCore_thumbv7em , llvm::MachO::CPU_TYPE_ARM , 16 , UINT32_MAX , SUBTYPE_MASK },
201 { ArchSpec::eCore_ppc_generic , llvm::MachO::CPU_TYPE_POWERPC , CPU_ANY, UINT32_MAX , UINT32_MAX },
202 { ArchSpec::eCore_ppc_generic , llvm::MachO::CPU_TYPE_POWERPC , 0 , UINT32_MAX , SUBTYPE_MASK },
203 { ArchSpec::eCore_ppc_ppc601 , llvm::MachO::CPU_TYPE_POWERPC , 1 , UINT32_MAX , SUBTYPE_MASK },
204 { ArchSpec::eCore_ppc_ppc602 , llvm::MachO::CPU_TYPE_POWERPC , 2 , UINT32_MAX , SUBTYPE_MASK },
205 { ArchSpec::eCore_ppc_ppc603 , llvm::MachO::CPU_TYPE_POWERPC , 3 , UINT32_MAX , SUBTYPE_MASK },
206 { ArchSpec::eCore_ppc_ppc603e , llvm::MachO::CPU_TYPE_POWERPC , 4 , UINT32_MAX , SUBTYPE_MASK },
207 { ArchSpec::eCore_ppc_ppc603ev , llvm::MachO::CPU_TYPE_POWERPC , 5 , UINT32_MAX , SUBTYPE_MASK },
208 { ArchSpec::eCore_ppc_ppc604 , llvm::MachO::CPU_TYPE_POWERPC , 6 , UINT32_MAX , SUBTYPE_MASK },
209 { ArchSpec::eCore_ppc_ppc604e , llvm::MachO::CPU_TYPE_POWERPC , 7 , UINT32_MAX , SUBTYPE_MASK },
210 { ArchSpec::eCore_ppc_ppc620 , llvm::MachO::CPU_TYPE_POWERPC , 8 , UINT32_MAX , SUBTYPE_MASK },
211 { ArchSpec::eCore_ppc_ppc750 , llvm::MachO::CPU_TYPE_POWERPC , 9 , UINT32_MAX , SUBTYPE_MASK },
212 { ArchSpec::eCore_ppc_ppc7400 , llvm::MachO::CPU_TYPE_POWERPC , 10 , UINT32_MAX , SUBTYPE_MASK },
213 { ArchSpec::eCore_ppc_ppc7450 , llvm::MachO::CPU_TYPE_POWERPC , 11 , UINT32_MAX , SUBTYPE_MASK },
214 { ArchSpec::eCore_ppc_ppc970 , llvm::MachO::CPU_TYPE_POWERPC , 100 , UINT32_MAX , SUBTYPE_MASK },
215 { ArchSpec::eCore_ppc64_generic , llvm::MachO::CPU_TYPE_POWERPC64 , 0 , UINT32_MAX , SUBTYPE_MASK },
216 { ArchSpec::eCore_ppc64_ppc970_64 , llvm::MachO::CPU_TYPE_POWERPC64 , 100 , UINT32_MAX , SUBTYPE_MASK },
217 { ArchSpec::eCore_x86_32_i386 , llvm::MachO::CPU_TYPE_I386 , 3 , UINT32_MAX , SUBTYPE_MASK },
218 { ArchSpec::eCore_x86_32_i486 , llvm::MachO::CPU_TYPE_I386 , 4 , UINT32_MAX , SUBTYPE_MASK },
219 { ArchSpec::eCore_x86_32_i486sx , llvm::MachO::CPU_TYPE_I386 , 0x84 , UINT32_MAX , SUBTYPE_MASK },
Greg Claytona86dc432014-01-22 23:42:03 +0000220 { ArchSpec::eCore_x86_32_i386 , llvm::MachO::CPU_TYPE_I386 , CPU_ANY, UINT32_MAX , UINT32_MAX },
Charles Davis510938e2013-08-27 05:04:57 +0000221 { ArchSpec::eCore_x86_64_x86_64 , llvm::MachO::CPU_TYPE_X86_64 , 3 , UINT32_MAX , SUBTYPE_MASK },
222 { ArchSpec::eCore_x86_64_x86_64 , llvm::MachO::CPU_TYPE_X86_64 , 4 , UINT32_MAX , SUBTYPE_MASK },
Greg Claytona86dc432014-01-22 23:42:03 +0000223 { ArchSpec::eCore_x86_64_x86_64h , llvm::MachO::CPU_TYPE_X86_64 , 8 , UINT32_MAX , SUBTYPE_MASK },
224 { 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 +0000225 // Catch any unknown mach architectures so we can always use the object and symbol mach-o files
Charles Davis510938e2013-08-27 05:04:57 +0000226 { ArchSpec::eCore_uknownMach32 , 0 , 0 , 0xFF000000u, 0x00000000u },
227 { ArchSpec::eCore_uknownMach64 , llvm::MachO::CPU_ARCH_ABI64 , 0 , 0xFF000000u, 0x00000000u }
Greg Clayton64195a22011-02-23 00:35:02 +0000228};
229static const ArchDefinition g_macho_arch_def = {
230 eArchTypeMachO,
Saleem Abdulrasool28606952014-06-27 05:17:41 +0000231 llvm::array_lengthof(g_macho_arch_entries),
Greg Clayton64195a22011-02-23 00:35:02 +0000232 g_macho_arch_entries,
Greg Clayton64195a22011-02-23 00:35:02 +0000233 "mach-o"
Greg Clayton41f92322010-06-11 03:25:34 +0000234};
235
Greg Clayton64195a22011-02-23 00:35:02 +0000236//===----------------------------------------------------------------------===//
237// A table that gets searched linearly for matches. This table is used to
238// convert cpu type and subtypes to architecture names, and to convert
239// architecture names to cpu types and subtypes. The ordering is important and
240// allows the precedence to be set when the table is built.
241static const ArchDefinitionEntry g_elf_arch_entries[] =
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000242{
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000243 { ArchSpec::eCore_sparc_generic , llvm::ELF::EM_SPARC , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // Sparc
244 { ArchSpec::eCore_x86_32_i386 , llvm::ELF::EM_386 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // Intel 80386
245 { ArchSpec::eCore_x86_32_i486 , llvm::ELF::EM_486 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // Intel 486 (deprecated)
246 { ArchSpec::eCore_ppc_generic , llvm::ELF::EM_PPC , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // PowerPC
247 { ArchSpec::eCore_ppc64_generic , llvm::ELF::EM_PPC64 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // PowerPC64
248 { ArchSpec::eCore_arm_generic , llvm::ELF::EM_ARM , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // ARM
249 { ArchSpec::eCore_sparc9_generic , llvm::ELF::EM_SPARCV9, LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // SPARC V9
Ed Masteb73f8442013-10-10 00:59:47 +0000250 { ArchSpec::eCore_x86_64_x86_64 , llvm::ELF::EM_X86_64 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // AMD64
Deepak Panickal6d3df422014-02-19 11:16:46 +0000251 { ArchSpec::eCore_mips64 , llvm::ELF::EM_MIPS , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // MIPS
252 { ArchSpec::eCore_hexagon_generic , llvm::ELF::EM_HEXAGON, LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu } // HEXAGON
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000253};
254
Greg Clayton64195a22011-02-23 00:35:02 +0000255static const ArchDefinition g_elf_arch_def = {
256 eArchTypeELF,
Saleem Abdulrasool28606952014-06-27 05:17:41 +0000257 llvm::array_lengthof(g_elf_arch_entries),
Greg Clayton64195a22011-02-23 00:35:02 +0000258 g_elf_arch_entries,
Greg Clayton64195a22011-02-23 00:35:02 +0000259 "elf",
Greg Clayton41f92322010-06-11 03:25:34 +0000260};
261
Charles Davis237ad972013-08-27 05:04:33 +0000262static const ArchDefinitionEntry g_coff_arch_entries[] =
263{
264 { ArchSpec::eCore_x86_32_i386 , llvm::COFF::IMAGE_FILE_MACHINE_I386 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // Intel 80386
265 { ArchSpec::eCore_ppc_generic , llvm::COFF::IMAGE_FILE_MACHINE_POWERPC , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // PowerPC
266 { ArchSpec::eCore_ppc_generic , llvm::COFF::IMAGE_FILE_MACHINE_POWERPCFP, LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // PowerPC (with FPU)
267 { ArchSpec::eCore_arm_generic , llvm::COFF::IMAGE_FILE_MACHINE_ARM , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // ARM
Saleem Abdulrasool1108cb32014-03-11 03:09:08 +0000268 { ArchSpec::eCore_arm_armv7 , llvm::COFF::IMAGE_FILE_MACHINE_ARMNT , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // ARMv7
Charles Davis237ad972013-08-27 05:04:33 +0000269 { ArchSpec::eCore_thumb , llvm::COFF::IMAGE_FILE_MACHINE_THUMB , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // ARMv7
270 { ArchSpec::eCore_x86_64_x86_64, llvm::COFF::IMAGE_FILE_MACHINE_AMD64 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu } // AMD64
271};
272
273static const ArchDefinition g_coff_arch_def = {
274 eArchTypeCOFF,
Saleem Abdulrasool28606952014-06-27 05:17:41 +0000275 llvm::array_lengthof(g_coff_arch_entries),
Charles Davis237ad972013-08-27 05:04:33 +0000276 g_coff_arch_entries,
277 "pe-coff",
278};
279
Greg Clayton64195a22011-02-23 00:35:02 +0000280//===----------------------------------------------------------------------===//
281// Table of all ArchDefinitions
282static const ArchDefinition *g_arch_definitions[] = {
283 &g_macho_arch_def,
Charles Davis237ad972013-08-27 05:04:33 +0000284 &g_elf_arch_def,
285 &g_coff_arch_def
Greg Clayton64195a22011-02-23 00:35:02 +0000286};
Greg Clayton41f92322010-06-11 03:25:34 +0000287
Saleem Abdulrasool28606952014-06-27 05:17:41 +0000288static const size_t k_num_arch_definitions = llvm::array_lengthof(g_arch_definitions);
Greg Clayton64195a22011-02-23 00:35:02 +0000289
290//===----------------------------------------------------------------------===//
291// Static helper functions.
292
293
294// Get the architecture definition for a given object type.
295static const ArchDefinition *
296FindArchDefinition (ArchitectureType arch_type)
297{
298 for (unsigned int i = 0; i < k_num_arch_definitions; ++i)
299 {
300 const ArchDefinition *def = g_arch_definitions[i];
301 if (def->type == arch_type)
302 return def;
303 }
304 return NULL;
305}
306
307// Get an architecture definition by name.
308static const CoreDefinition *
309FindCoreDefinition (llvm::StringRef name)
310{
311 for (unsigned int i = 0; i < ArchSpec::kNumCores; ++i)
312 {
313 if (name.equals_lower(g_core_definitions[i].name))
314 return &g_core_definitions[i];
315 }
316 return NULL;
317}
318
319static inline const CoreDefinition *
320FindCoreDefinition (ArchSpec::Core core)
321{
322 if (core >= 0 && core < ArchSpec::kNumCores)
323 return &g_core_definitions[core];
324 return NULL;
325}
326
327// Get a definition entry by cpu type and subtype.
328static const ArchDefinitionEntry *
329FindArchDefinitionEntry (const ArchDefinition *def, uint32_t cpu, uint32_t sub)
330{
331 if (def == NULL)
332 return NULL;
333
Greg Clayton64195a22011-02-23 00:35:02 +0000334 const ArchDefinitionEntry *entries = def->entries;
335 for (size_t i = 0; i < def->num_entries; ++i)
336 {
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000337 if (entries[i].cpu == (cpu & entries[i].cpu_mask))
338 if (entries[i].sub == (sub & entries[i].sub_mask))
339 return &entries[i];
Greg Clayton64195a22011-02-23 00:35:02 +0000340 }
341 return NULL;
342}
343
344static const ArchDefinitionEntry *
345FindArchDefinitionEntry (const ArchDefinition *def, ArchSpec::Core core)
346{
347 if (def == NULL)
348 return NULL;
349
350 const ArchDefinitionEntry *entries = def->entries;
351 for (size_t i = 0; i < def->num_entries; ++i)
352 {
353 if (entries[i].core == core)
354 return &entries[i];
355 }
356 return NULL;
357}
358
359//===----------------------------------------------------------------------===//
360// Constructors and destructors.
361
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000362ArchSpec::ArchSpec() :
Greg Clayton514487e2011-02-15 21:59:32 +0000363 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000364 m_core (kCore_invalid),
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000365 m_byte_order (eByteOrderInvalid),
366 m_distribution_id ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000367{
368}
369
Greg Claytoneb0103f2011-04-07 22:46:35 +0000370ArchSpec::ArchSpec (const char *triple_cstr, Platform *platform) :
Greg Clayton514487e2011-02-15 21:59:32 +0000371 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000372 m_core (kCore_invalid),
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000373 m_byte_order (eByteOrderInvalid),
374 m_distribution_id ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000375{
Greg Clayton64195a22011-02-23 00:35:02 +0000376 if (triple_cstr)
Greg Claytoneb0103f2011-04-07 22:46:35 +0000377 SetTriple(triple_cstr, platform);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000378}
379
Greg Clayton70512312012-05-08 01:45:38 +0000380
381ArchSpec::ArchSpec (const char *triple_cstr) :
382 m_triple (),
383 m_core (kCore_invalid),
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000384 m_byte_order (eByteOrderInvalid),
385 m_distribution_id ()
Greg Clayton70512312012-05-08 01:45:38 +0000386{
387 if (triple_cstr)
388 SetTriple(triple_cstr);
389}
390
Greg Clayton64195a22011-02-23 00:35:02 +0000391ArchSpec::ArchSpec(const llvm::Triple &triple) :
Greg Clayton514487e2011-02-15 21:59:32 +0000392 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000393 m_core (kCore_invalid),
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000394 m_byte_order (eByteOrderInvalid),
395 m_distribution_id ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000396{
Greg Clayton64195a22011-02-23 00:35:02 +0000397 SetTriple(triple);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000398}
399
Greg Claytone0d378b2011-03-24 21:19:54 +0000400ArchSpec::ArchSpec (ArchitectureType arch_type, uint32_t cpu, uint32_t subtype) :
Greg Clayton64195a22011-02-23 00:35:02 +0000401 m_triple (),
402 m_core (kCore_invalid),
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000403 m_byte_order (eByteOrderInvalid),
404 m_distribution_id ()
Greg Clayton64195a22011-02-23 00:35:02 +0000405{
406 SetArchitecture (arch_type, cpu, subtype);
407}
408
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000409ArchSpec::~ArchSpec()
410{
411}
412
Greg Clayton64195a22011-02-23 00:35:02 +0000413//===----------------------------------------------------------------------===//
414// Assignment and initialization.
415
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000416const ArchSpec&
417ArchSpec::operator= (const ArchSpec& rhs)
418{
419 if (this != &rhs)
420 {
Greg Clayton514487e2011-02-15 21:59:32 +0000421 m_triple = rhs.m_triple;
Greg Clayton64195a22011-02-23 00:35:02 +0000422 m_core = rhs.m_core;
Greg Clayton514487e2011-02-15 21:59:32 +0000423 m_byte_order = rhs.m_byte_order;
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000424 m_distribution_id = rhs.m_distribution_id;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000425 }
426 return *this;
427}
428
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000429void
430ArchSpec::Clear()
431{
Greg Clayton514487e2011-02-15 21:59:32 +0000432 m_triple = llvm::Triple();
Greg Clayton64195a22011-02-23 00:35:02 +0000433 m_core = kCore_invalid;
434 m_byte_order = eByteOrderInvalid;
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000435 m_distribution_id.Clear ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000436}
437
Greg Clayton64195a22011-02-23 00:35:02 +0000438//===----------------------------------------------------------------------===//
439// Predicates.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000440
Greg Clayton41f92322010-06-11 03:25:34 +0000441
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000442const char *
Greg Clayton64195a22011-02-23 00:35:02 +0000443ArchSpec::GetArchitectureName () 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 return core_def->name;
448 return "unknown";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000449}
450
Greg Clayton64195a22011-02-23 00:35:02 +0000451uint32_t
452ArchSpec::GetMachOCPUType () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000453{
Greg Clayton64195a22011-02-23 00:35:02 +0000454 const CoreDefinition *core_def = FindCoreDefinition (m_core);
455 if (core_def)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000456 {
Greg Clayton64195a22011-02-23 00:35:02 +0000457 const ArchDefinitionEntry *arch_def = FindArchDefinitionEntry (&g_macho_arch_def, core_def->core);
458 if (arch_def)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000459 {
Greg Clayton64195a22011-02-23 00:35:02 +0000460 return arch_def->cpu;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000461 }
462 }
Greg Clayton64195a22011-02-23 00:35:02 +0000463 return LLDB_INVALID_CPUTYPE;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000464}
465
Greg Clayton64195a22011-02-23 00:35:02 +0000466uint32_t
467ArchSpec::GetMachOCPUSubType () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000468{
Greg Clayton64195a22011-02-23 00:35:02 +0000469 const CoreDefinition *core_def = FindCoreDefinition (m_core);
470 if (core_def)
471 {
472 const ArchDefinitionEntry *arch_def = FindArchDefinitionEntry (&g_macho_arch_def, core_def->core);
473 if (arch_def)
474 {
Greg Clayton1cb64962011-03-24 04:28:38 +0000475 return arch_def->sub;
Greg Clayton64195a22011-02-23 00:35:02 +0000476 }
477 }
478 return LLDB_INVALID_CPUTYPE;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000479}
480
Greg Clayton64195a22011-02-23 00:35:02 +0000481llvm::Triple::ArchType
482ArchSpec::GetMachine () const
483{
484 const CoreDefinition *core_def = FindCoreDefinition (m_core);
485 if (core_def)
486 return core_def->machine;
487
488 return llvm::Triple::UnknownArch;
489}
490
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000491const ConstString&
492ArchSpec::GetDistributionId () const
493{
494 return m_distribution_id;
495}
496
497void
498ArchSpec::SetDistributionId (const char* distribution_id)
499{
500 m_distribution_id.SetCString (distribution_id);
501}
502
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000503uint32_t
504ArchSpec::GetAddressByteSize() const
505{
Greg Clayton64195a22011-02-23 00:35:02 +0000506 const CoreDefinition *core_def = FindCoreDefinition (m_core);
507 if (core_def)
508 return core_def->addr_byte_size;
Greg Clayton41f92322010-06-11 03:25:34 +0000509 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000510}
511
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000512ByteOrder
513ArchSpec::GetDefaultEndian () const
514{
Greg Clayton64195a22011-02-23 00:35:02 +0000515 const CoreDefinition *core_def = FindCoreDefinition (m_core);
516 if (core_def)
517 return core_def->default_byte_order;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000518 return eByteOrderInvalid;
519}
520
Greg Clayton64195a22011-02-23 00:35:02 +0000521lldb::ByteOrder
522ArchSpec::GetByteOrder () const
523{
524 if (m_byte_order == eByteOrderInvalid)
525 return GetDefaultEndian();
526 return m_byte_order;
527}
528
529//===----------------------------------------------------------------------===//
530// Mutators.
531
532bool
533ArchSpec::SetTriple (const llvm::Triple &triple)
534{
535 m_triple = triple;
536
537 llvm::StringRef arch_name (m_triple.getArchName());
538 const CoreDefinition *core_def = FindCoreDefinition (arch_name);
539 if (core_def)
540 {
541 m_core = core_def->core;
Greg Claytoneb0103f2011-04-07 22:46:35 +0000542 // Set the byte order to the default byte order for an architecture.
543 // This can be modified if needed for cases when cores handle both
544 // big and little endian
545 m_byte_order = core_def->default_byte_order;
Greg Clayton64195a22011-02-23 00:35:02 +0000546 }
547 else
548 {
549 Clear();
550 }
551
552
553 return IsValid();
554}
555
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000556static bool
557ParseMachCPUDashSubtypeTriple (const char *triple_cstr, ArchSpec &arch)
558{
559 // Accept "12-10" or "12.10" as cpu type/subtype
560 if (isdigit(triple_cstr[0]))
561 {
562 char *end = NULL;
563 errno = 0;
Greg Claytonc7bece562013-01-25 18:06:21 +0000564 uint32_t cpu = (uint32_t)::strtoul (triple_cstr, &end, 0);
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000565 if (errno == 0 && cpu != 0 && end && ((*end == '-') || (*end == '.')))
566 {
567 errno = 0;
Greg Claytonc7bece562013-01-25 18:06:21 +0000568 uint32_t sub = (uint32_t)::strtoul (end + 1, &end, 0);
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000569 if (errno == 0 && end && ((*end == '-') || (*end == '.') || (*end == '\0')))
570 {
571 if (arch.SetArchitecture (eArchTypeMachO, cpu, sub))
572 {
573 if (*end == '-')
574 {
575 llvm::StringRef vendor_os (end + 1);
576 size_t dash_pos = vendor_os.find('-');
577 if (dash_pos != llvm::StringRef::npos)
578 {
579 llvm::StringRef vendor_str(vendor_os.substr(0, dash_pos));
580 arch.GetTriple().setVendorName(vendor_str);
581 const size_t vendor_start_pos = dash_pos+1;
Greg Claytonc7bece562013-01-25 18:06:21 +0000582 dash_pos = vendor_os.find('-', vendor_start_pos);
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000583 if (dash_pos == llvm::StringRef::npos)
584 {
585 if (vendor_start_pos < vendor_os.size())
586 arch.GetTriple().setOSName(vendor_os.substr(vendor_start_pos));
587 }
588 else
589 {
590 arch.GetTriple().setOSName(vendor_os.substr(vendor_start_pos, dash_pos - vendor_start_pos));
591 }
592 }
593 }
594 return true;
595 }
596 }
597 }
598 }
599 return false;
600}
Greg Clayton64195a22011-02-23 00:35:02 +0000601bool
Greg Clayton70512312012-05-08 01:45:38 +0000602ArchSpec::SetTriple (const char *triple_cstr)
Greg Clayton64195a22011-02-23 00:35:02 +0000603{
Greg Clayton23aca092011-08-12 23:32:52 +0000604 if (triple_cstr && triple_cstr[0])
Greg Clayton64195a22011-02-23 00:35:02 +0000605 {
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000606 if (ParseMachCPUDashSubtypeTriple (triple_cstr, *this))
607 return true;
608
Greg Clayton64195a22011-02-23 00:35:02 +0000609 llvm::StringRef triple_stref (triple_cstr);
610 if (triple_stref.startswith (LLDB_ARCH_DEFAULT))
611 {
612 // Special case for the current host default architectures...
613 if (triple_stref.equals (LLDB_ARCH_DEFAULT_32BIT))
614 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture32);
615 else if (triple_stref.equals (LLDB_ARCH_DEFAULT_64BIT))
616 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture64);
617 else if (triple_stref.equals (LLDB_ARCH_DEFAULT))
618 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture);
619 }
620 else
621 {
622 std::string normalized_triple_sstr (llvm::Triple::normalize(triple_stref));
623 triple_stref = normalized_triple_sstr;
Greg Clayton70512312012-05-08 01:45:38 +0000624 SetTriple (llvm::Triple (triple_stref));
625 }
626 }
627 else
628 Clear();
629 return IsValid();
630}
631
632bool
633ArchSpec::SetTriple (const char *triple_cstr, Platform *platform)
634{
635 if (triple_cstr && triple_cstr[0])
636 {
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000637 if (ParseMachCPUDashSubtypeTriple (triple_cstr, *this))
638 return true;
639
Greg Clayton70512312012-05-08 01:45:38 +0000640 llvm::StringRef triple_stref (triple_cstr);
641 if (triple_stref.startswith (LLDB_ARCH_DEFAULT))
642 {
643 // Special case for the current host default architectures...
644 if (triple_stref.equals (LLDB_ARCH_DEFAULT_32BIT))
645 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture32);
646 else if (triple_stref.equals (LLDB_ARCH_DEFAULT_64BIT))
647 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture64);
648 else if (triple_stref.equals (LLDB_ARCH_DEFAULT))
649 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture);
650 }
651 else
652 {
653 ArchSpec raw_arch (triple_cstr);
654
655 std::string normalized_triple_sstr (llvm::Triple::normalize(triple_stref));
656 triple_stref = normalized_triple_sstr;
Greg Claytoneb0103f2011-04-07 22:46:35 +0000657 llvm::Triple normalized_triple (triple_stref);
658
659 const bool os_specified = normalized_triple.getOSName().size() > 0;
660 const bool vendor_specified = normalized_triple.getVendorName().size() > 0;
661 const bool env_specified = normalized_triple.getEnvironmentName().size() > 0;
662
663 // If we got an arch only, then default the vendor, os, environment
664 // to match the platform if one is supplied
665 if (!(os_specified || vendor_specified || env_specified))
666 {
667 if (platform)
668 {
669 // If we were given a platform, use the platform's system
670 // architecture. If this is not available (might not be
671 // connected) use the first supported architecture.
Greg Clayton70512312012-05-08 01:45:38 +0000672 ArchSpec compatible_arch;
Greg Clayton1e0c8842013-01-11 20:49:54 +0000673 if (platform->IsCompatibleArchitecture (raw_arch, false, &compatible_arch))
Greg Claytoneb0103f2011-04-07 22:46:35 +0000674 {
Greg Clayton70512312012-05-08 01:45:38 +0000675 if (compatible_arch.IsValid())
676 {
677 const llvm::Triple &compatible_triple = compatible_arch.GetTriple();
678 if (!vendor_specified)
679 normalized_triple.setVendor(compatible_triple.getVendor());
680 if (!os_specified)
681 normalized_triple.setOS(compatible_triple.getOS());
682 if (!env_specified && compatible_triple.getEnvironmentName().size())
683 normalized_triple.setEnvironment(compatible_triple.getEnvironment());
684 }
Greg Claytoneb0103f2011-04-07 22:46:35 +0000685 }
Greg Clayton70512312012-05-08 01:45:38 +0000686 else
Greg Claytoneb0103f2011-04-07 22:46:35 +0000687 {
Greg Clayton70512312012-05-08 01:45:38 +0000688 *this = raw_arch;
689 return IsValid();
Greg Claytoneb0103f2011-04-07 22:46:35 +0000690 }
691 }
692 else
693 {
694 // No platform specified, fall back to the host system for
695 // the default vendor, os, and environment.
Sean Callananbfb237bc2011-11-04 22:46:46 +0000696 llvm::Triple host_triple(llvm::sys::getDefaultTargetTriple());
Greg Clayton70512312012-05-08 01:45:38 +0000697 if (!vendor_specified)
698 normalized_triple.setVendor(host_triple.getVendor());
699 if (!vendor_specified)
700 normalized_triple.setOS(host_triple.getOS());
701 if (!env_specified && host_triple.getEnvironmentName().size())
702 normalized_triple.setEnvironment(host_triple.getEnvironment());
Greg Claytoneb0103f2011-04-07 22:46:35 +0000703 }
704 }
705 SetTriple (normalized_triple);
Greg Clayton64195a22011-02-23 00:35:02 +0000706 }
707 }
708 else
709 Clear();
710 return IsValid();
711}
712
Greg Clayton64195a22011-02-23 00:35:02 +0000713bool
Greg Claytone0d378b2011-03-24 21:19:54 +0000714ArchSpec::SetArchitecture (ArchitectureType arch_type, uint32_t cpu, uint32_t sub)
Greg Clayton64195a22011-02-23 00:35:02 +0000715{
716 m_core = kCore_invalid;
717 bool update_triple = true;
718 const ArchDefinition *arch_def = FindArchDefinition(arch_type);
719 if (arch_def)
720 {
721 const ArchDefinitionEntry *arch_def_entry = FindArchDefinitionEntry (arch_def, cpu, sub);
722 if (arch_def_entry)
723 {
724 const CoreDefinition *core_def = FindCoreDefinition (arch_def_entry->core);
725 if (core_def)
726 {
727 m_core = core_def->core;
728 update_triple = false;
Greg Clayton593577a2011-09-21 03:57:31 +0000729 // Always use the architecture name because it might be more descriptive
730 // than the architecture enum ("armv7" -> llvm::Triple::arm).
731 m_triple.setArchName(llvm::StringRef(core_def->name));
Greg Clayton64195a22011-02-23 00:35:02 +0000732 if (arch_type == eArchTypeMachO)
733 {
734 m_triple.setVendor (llvm::Triple::Apple);
Greg Clayton70512312012-05-08 01:45:38 +0000735
736 switch (core_def->machine)
737 {
Jason Molendaa3329782014-03-29 18:54:20 +0000738 case llvm::Triple::arm64:
Greg Clayton70512312012-05-08 01:45:38 +0000739 case llvm::Triple::arm:
740 case llvm::Triple::thumb:
741 m_triple.setOS (llvm::Triple::IOS);
742 break;
743
744 case llvm::Triple::x86:
745 case llvm::Triple::x86_64:
746 default:
747 m_triple.setOS (llvm::Triple::MacOSX);
748 break;
749 }
Greg Clayton64195a22011-02-23 00:35:02 +0000750 }
751 else
752 {
753 m_triple.setVendor (llvm::Triple::UnknownVendor);
754 m_triple.setOS (llvm::Triple::UnknownOS);
755 }
Greg Clayton593577a2011-09-21 03:57:31 +0000756 // Fall back onto setting the machine type if the arch by name failed...
757 if (m_triple.getArch () == llvm::Triple::UnknownArch)
758 m_triple.setArch (core_def->machine);
Greg Clayton64195a22011-02-23 00:35:02 +0000759 }
760 }
761 }
762 CoreUpdated(update_triple);
763 return IsValid();
764}
765
Greg Clayton357132e2011-03-26 19:14:58 +0000766uint32_t
767ArchSpec::GetMinimumOpcodeByteSize() const
Greg Clayton64195a22011-02-23 00:35:02 +0000768{
Greg Clayton357132e2011-03-26 19:14:58 +0000769 const CoreDefinition *core_def = FindCoreDefinition (m_core);
770 if (core_def)
771 return core_def->min_opcode_byte_size;
772 return 0;
773}
774
775uint32_t
776ArchSpec::GetMaximumOpcodeByteSize() const
777{
778 const CoreDefinition *core_def = FindCoreDefinition (m_core);
779 if (core_def)
780 return core_def->max_opcode_byte_size;
781 return 0;
Greg Clayton64195a22011-02-23 00:35:02 +0000782}
783
Jason Molendaba813dc2012-11-04 03:20:05 +0000784bool
785ArchSpec::IsExactMatch (const ArchSpec& rhs) const
786{
Sean Callananbf4b7be2012-12-13 22:07:14 +0000787 return IsEqualTo (rhs, true);
Jason Molendaba813dc2012-11-04 03:20:05 +0000788}
789
790bool
791ArchSpec::IsCompatibleMatch (const ArchSpec& rhs) const
792{
Sean Callananbf4b7be2012-12-13 22:07:14 +0000793 return IsEqualTo (rhs, false);
Jason Molendaba813dc2012-11-04 03:20:05 +0000794}
795
796bool
Sean Callananbf4b7be2012-12-13 22:07:14 +0000797ArchSpec::IsEqualTo (const ArchSpec& rhs, bool exact_match) const
Jason Molendaba813dc2012-11-04 03:20:05 +0000798{
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000799 // explicitly ignoring m_distribution_id in this method.
800
Jason Molendaba813dc2012-11-04 03:20:05 +0000801 if (GetByteOrder() != rhs.GetByteOrder())
802 return false;
803
804 const ArchSpec::Core lhs_core = GetCore ();
805 const ArchSpec::Core rhs_core = rhs.GetCore ();
806
807 const bool core_match = cores_match (lhs_core, rhs_core, true, exact_match);
808
809 if (core_match)
810 {
811 const llvm::Triple &lhs_triple = GetTriple();
812 const llvm::Triple &rhs_triple = rhs.GetTriple();
813
814 const llvm::Triple::VendorType lhs_triple_vendor = lhs_triple.getVendor();
815 const llvm::Triple::VendorType rhs_triple_vendor = rhs_triple.getVendor();
816 if (lhs_triple_vendor != rhs_triple_vendor)
817 {
Sean Callananbf4b7be2012-12-13 22:07:14 +0000818 if (exact_match)
819 {
820 const bool rhs_vendor_specified = rhs.TripleVendorWasSpecified();
821 const bool lhs_vendor_specified = TripleVendorWasSpecified();
822 // Both architectures had the vendor specified, so if they aren't
823 // equal then we return false
824 if (rhs_vendor_specified && lhs_vendor_specified)
825 return false;
826 }
Jason Molendaba813dc2012-11-04 03:20:05 +0000827
828 // Only fail if both vendor types are not unknown
829 if (lhs_triple_vendor != llvm::Triple::UnknownVendor &&
830 rhs_triple_vendor != llvm::Triple::UnknownVendor)
831 return false;
832 }
833
834 const llvm::Triple::OSType lhs_triple_os = lhs_triple.getOS();
835 const llvm::Triple::OSType rhs_triple_os = rhs_triple.getOS();
836 if (lhs_triple_os != rhs_triple_os)
837 {
Sean Callananbf4b7be2012-12-13 22:07:14 +0000838 if (exact_match)
839 {
840 const bool rhs_os_specified = rhs.TripleOSWasSpecified();
841 const bool lhs_os_specified = TripleOSWasSpecified();
842 // Both architectures had the OS specified, so if they aren't
843 // equal then we return false
844 if (rhs_os_specified && lhs_os_specified)
845 return false;
846 }
Greg Clayton7ab7f892014-05-29 21:33:45 +0000847
848 bool ios_simulator_compatible = false;
849 // Check for iOS desktop simulator matches where:
850 // x86_64-apple-ios is compatible with x86_64-apple-macosx
851 // i386-apple-ios is compatible with i386-apple-macosx
852 if (lhs_triple_vendor == llvm::Triple::Apple)
853 {
854 const llvm::Triple::ArchType lhs_arch = lhs_triple.getArch();
855 if (lhs_arch == llvm::Triple::x86_64 ||
856 lhs_arch == llvm::Triple::x86)
857 {
858 if ((lhs_triple_os == llvm::Triple::MacOSX && rhs_triple_os == llvm::Triple::IOS ) ||
859 (lhs_triple_os == llvm::Triple::IOS && rhs_triple_os == llvm::Triple::MacOSX ))
860 {
861 ios_simulator_compatible = true;
862 }
863
864 }
865 }
866
867 // Only fail if both os types are not unknown or if we determined the triples
868 // match for x86_64 or x86 iOS simulator
869 if (!ios_simulator_compatible)
870 {
871 if (lhs_triple_os != llvm::Triple::UnknownOS &&
872 rhs_triple_os != llvm::Triple::UnknownOS)
873 return false;
874 }
Jason Molendaba813dc2012-11-04 03:20:05 +0000875 }
876
877 const llvm::Triple::EnvironmentType lhs_triple_env = lhs_triple.getEnvironment();
878 const llvm::Triple::EnvironmentType rhs_triple_env = rhs_triple.getEnvironment();
879
880 if (lhs_triple_env != rhs_triple_env)
881 {
882 // Only fail if both environment types are not unknown
883 if (lhs_triple_env != llvm::Triple::UnknownEnvironment &&
884 rhs_triple_env != llvm::Triple::UnknownEnvironment)
885 return false;
886 }
887 return true;
888 }
889 return false;
890}
891
Greg Clayton64195a22011-02-23 00:35:02 +0000892//===----------------------------------------------------------------------===//
893// Helper methods.
894
895void
896ArchSpec::CoreUpdated (bool update_triple)
897{
898 const CoreDefinition *core_def = FindCoreDefinition (m_core);
899 if (core_def)
900 {
901 if (update_triple)
902 m_triple = llvm::Triple(core_def->name, "unknown", "unknown");
903 m_byte_order = core_def->default_byte_order;
904 }
905 else
906 {
907 if (update_triple)
908 m_triple = llvm::Triple();
909 m_byte_order = eByteOrderInvalid;
910 }
911}
912
913//===----------------------------------------------------------------------===//
914// Operators.
915
Greg Clayton70512312012-05-08 01:45:38 +0000916static bool
Jason Molendaba813dc2012-11-04 03:20:05 +0000917cores_match (const ArchSpec::Core core1, const ArchSpec::Core core2, bool try_inverse, bool enforce_exact_match)
Greg Clayton70512312012-05-08 01:45:38 +0000918{
Jason Molendaba813dc2012-11-04 03:20:05 +0000919 if (core1 == core2)
920 return true;
921
Greg Clayton70512312012-05-08 01:45:38 +0000922 switch (core1)
923 {
Greg Clayton70512312012-05-08 01:45:38 +0000924 case ArchSpec::kCore_any:
925 return true;
926
927 case ArchSpec::kCore_arm_any:
928 if (core2 >= ArchSpec::kCore_arm_first && core2 <= ArchSpec::kCore_arm_last)
929 return true;
930 if (core2 >= ArchSpec::kCore_thumb_first && core2 <= ArchSpec::kCore_thumb_last)
931 return true;
932 if (core2 == ArchSpec::kCore_arm_any)
933 return true;
934 break;
Greg Claytona86dc432014-01-22 23:42:03 +0000935
Greg Clayton70512312012-05-08 01:45:38 +0000936 case ArchSpec::kCore_x86_32_any:
937 if ((core2 >= ArchSpec::kCore_x86_32_first && core2 <= ArchSpec::kCore_x86_32_last) || (core2 == ArchSpec::kCore_x86_32_any))
938 return true;
939 break;
940
941 case ArchSpec::kCore_ppc_any:
942 if ((core2 >= ArchSpec::kCore_ppc_first && core2 <= ArchSpec::kCore_ppc_last) || (core2 == ArchSpec::kCore_ppc_any))
943 return true;
944 break;
945
946 case ArchSpec::kCore_ppc64_any:
947 if ((core2 >= ArchSpec::kCore_ppc64_first && core2 <= ArchSpec::kCore_ppc64_last) || (core2 == ArchSpec::kCore_ppc64_any))
948 return true;
949 break;
950
Jason Molendaa3a04522013-09-27 23:21:54 +0000951 case ArchSpec::eCore_arm_armv6m:
952 if (!enforce_exact_match)
953 {
954 try_inverse = false;
Jason Molendac7cda272013-09-27 23:29:10 +0000955 if (core2 == ArchSpec::eCore_arm_armv7)
Jason Molendaa3a04522013-09-27 23:21:54 +0000956 return true;
957 }
Deepak Panickal6d3df422014-02-19 11:16:46 +0000958
959 case ArchSpec::kCore_hexagon_any:
960 if ((core2 >= ArchSpec::kCore_hexagon_first && core2 <= ArchSpec::kCore_hexagon_last) || (core2 == ArchSpec::kCore_hexagon_any))
961 return true;
Jason Molendaa3a04522013-09-27 23:21:54 +0000962 break;
963
Jason Molenda7a1559c2013-03-08 01:20:17 +0000964 case ArchSpec::eCore_arm_armv7m:
965 case ArchSpec::eCore_arm_armv7em:
Johnny Chen1083b0d2012-08-28 22:53:40 +0000966 case ArchSpec::eCore_arm_armv7f:
967 case ArchSpec::eCore_arm_armv7k:
968 case ArchSpec::eCore_arm_armv7s:
Jason Molendaba813dc2012-11-04 03:20:05 +0000969 if (!enforce_exact_match)
970 {
971 try_inverse = false;
972 if (core2 == ArchSpec::eCore_arm_armv7)
973 return true;
974 }
Johnny Chen1083b0d2012-08-28 22:53:40 +0000975 break;
976
Greg Clayton70512312012-05-08 01:45:38 +0000977 default:
978 break;
979 }
980 if (try_inverse)
Jason Molendaba813dc2012-11-04 03:20:05 +0000981 return cores_match (core2, core1, false, enforce_exact_match);
Greg Clayton70512312012-05-08 01:45:38 +0000982 return false;
983}
984
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000985bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000986lldb_private::operator<(const ArchSpec& lhs, const ArchSpec& rhs)
987{
Greg Clayton64195a22011-02-23 00:35:02 +0000988 const ArchSpec::Core lhs_core = lhs.GetCore ();
989 const ArchSpec::Core rhs_core = rhs.GetCore ();
990 return lhs_core < rhs_core;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000991}