blob: 426f4df6bbd1f64eeea5f98a0668ada2e2507013 [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;
Greg Clayton56b79682014-07-23 18:12:06 +000045 const char * const name;
Greg Clayton64195a22011-02-23 00:35:02 +000046 };
47
48}
49
50// This core information can be looked using the ArchSpec::Core as the index
Greg Clayton56b79682014-07-23 18:12:06 +000051static const CoreDefinition g_core_definitions[] =
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" },
Todd Fialad8eaa172014-07-23 14:37:35 +000080 { eByteOrderLittle, 8, 4, 4, llvm::Triple::aarch64, 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" },
Todd Fiala14bbef52014-07-01 23:33:32 +0000116 { eByteOrderLittle, 8, 4, 4 , llvm::Triple::UnknownArch , ArchSpec::eCore_uknownMach64 , "unknown-mach-64" },
117
Todd Fialacfee9632014-07-16 15:03:10 +0000118 { eByteOrderLittle, 4, 1, 1 , llvm::Triple::kalimba , ArchSpec::eCore_kalimba , "kalimba" }
Greg Clayton64195a22011-02-23 00:35:02 +0000119};
120
Greg Clayton56b79682014-07-23 18:12:06 +0000121// Ensure that we have an entry in the g_core_definitions for each core. If you comment out an entry above,
122// you will need to comment out the corresponding ArchSpec::Core enumeration.
Zachary Turner3b2065f2014-07-28 16:44:28 +0000123static_assert(sizeof(g_core_definitions) / sizeof(CoreDefinition) == ArchSpec::kNumCores, "make sure we have one core definition for each core");
Greg Clayton56b79682014-07-23 18:12:06 +0000124
125
Greg Clayton64195a22011-02-23 00:35:02 +0000126struct ArchDefinitionEntry
127{
128 ArchSpec::Core core;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000129 uint32_t cpu;
130 uint32_t sub;
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000131 uint32_t cpu_mask;
132 uint32_t sub_mask;
Greg Clayton64195a22011-02-23 00:35:02 +0000133};
134
135struct ArchDefinition
136{
137 ArchitectureType type;
138 size_t num_entries;
139 const ArchDefinitionEntry *entries;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000140 const char *name;
141};
142
Greg Clayton41f92322010-06-11 03:25:34 +0000143
Greg Claytonc7bece562013-01-25 18:06:21 +0000144size_t
Greg Claytonab65b342011-04-13 22:47:15 +0000145ArchSpec::AutoComplete (const char *name, StringList &matches)
146{
147 uint32_t i;
148 if (name && name[0])
149 {
Greg Clayton56b79682014-07-23 18:12:06 +0000150 for (i = 0; i < llvm::array_lengthof(g_core_definitions); ++i)
Greg Claytonab65b342011-04-13 22:47:15 +0000151 {
152 if (NameMatches(g_core_definitions[i].name, eNameMatchStartsWith, name))
153 matches.AppendString (g_core_definitions[i].name);
154 }
155 }
156 else
157 {
Greg Clayton56b79682014-07-23 18:12:06 +0000158 for (i = 0; i < llvm::array_lengthof(g_core_definitions); ++i)
Greg Claytonab65b342011-04-13 22:47:15 +0000159 matches.AppendString (g_core_definitions[i].name);
160 }
161 return matches.GetSize();
162}
163
164
165
Greg Clayton64195a22011-02-23 00:35:02 +0000166#define CPU_ANY (UINT32_MAX)
167
168//===----------------------------------------------------------------------===//
169// A table that gets searched linearly for matches. This table is used to
170// convert cpu type and subtypes to architecture names, and to convert
171// architecture names to cpu types and subtypes. The ordering is important and
172// allows the precedence to be set when the table is built.
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000173#define SUBTYPE_MASK 0x00FFFFFFu
Greg Clayton64195a22011-02-23 00:35:02 +0000174static const ArchDefinitionEntry g_macho_arch_entries[] =
Greg Clayton41f92322010-06-11 03:25:34 +0000175{
Charles Davis510938e2013-08-27 05:04:57 +0000176 { ArchSpec::eCore_arm_generic , llvm::MachO::CPU_TYPE_ARM , CPU_ANY, UINT32_MAX , UINT32_MAX },
177 { ArchSpec::eCore_arm_generic , llvm::MachO::CPU_TYPE_ARM , 0 , UINT32_MAX , SUBTYPE_MASK },
178 { ArchSpec::eCore_arm_armv4 , llvm::MachO::CPU_TYPE_ARM , 5 , UINT32_MAX , SUBTYPE_MASK },
179 { ArchSpec::eCore_arm_armv4t , llvm::MachO::CPU_TYPE_ARM , 5 , UINT32_MAX , SUBTYPE_MASK },
180 { ArchSpec::eCore_arm_armv6 , llvm::MachO::CPU_TYPE_ARM , 6 , UINT32_MAX , SUBTYPE_MASK },
Jason Molenda64a11732013-10-08 03:01:08 +0000181 { ArchSpec::eCore_arm_armv6m , llvm::MachO::CPU_TYPE_ARM , 14 , UINT32_MAX , SUBTYPE_MASK },
Charles Davis510938e2013-08-27 05:04:57 +0000182 { ArchSpec::eCore_arm_armv5 , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
183 { ArchSpec::eCore_arm_armv5e , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
184 { ArchSpec::eCore_arm_armv5t , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
185 { ArchSpec::eCore_arm_xscale , llvm::MachO::CPU_TYPE_ARM , 8 , UINT32_MAX , SUBTYPE_MASK },
186 { ArchSpec::eCore_arm_armv7 , llvm::MachO::CPU_TYPE_ARM , 9 , UINT32_MAX , SUBTYPE_MASK },
187 { ArchSpec::eCore_arm_armv7f , llvm::MachO::CPU_TYPE_ARM , 10 , UINT32_MAX , SUBTYPE_MASK },
188 { ArchSpec::eCore_arm_armv7s , llvm::MachO::CPU_TYPE_ARM , 11 , UINT32_MAX , SUBTYPE_MASK },
189 { ArchSpec::eCore_arm_armv7k , llvm::MachO::CPU_TYPE_ARM , 12 , UINT32_MAX , SUBTYPE_MASK },
190 { ArchSpec::eCore_arm_armv7m , llvm::MachO::CPU_TYPE_ARM , 15 , UINT32_MAX , SUBTYPE_MASK },
191 { ArchSpec::eCore_arm_armv7em , llvm::MachO::CPU_TYPE_ARM , 16 , UINT32_MAX , SUBTYPE_MASK },
Jason Molendaa3329782014-03-29 18:54:20 +0000192 { ArchSpec::eCore_arm_arm64 , llvm::MachO::CPU_TYPE_ARM64 , CPU_ANY, UINT32_MAX , SUBTYPE_MASK },
193 { ArchSpec::eCore_arm_arm64 , llvm::MachO::CPU_TYPE_ARM64 , 0 , UINT32_MAX , SUBTYPE_MASK },
194 { ArchSpec::eCore_arm_arm64 , llvm::MachO::CPU_TYPE_ARM64 , 1 , UINT32_MAX , SUBTYPE_MASK },
195 { ArchSpec::eCore_arm_arm64 , llvm::MachO::CPU_TYPE_ARM64 , 13 , UINT32_MAX , SUBTYPE_MASK },
Charles Davis510938e2013-08-27 05:04:57 +0000196 { ArchSpec::eCore_thumb , llvm::MachO::CPU_TYPE_ARM , 0 , UINT32_MAX , SUBTYPE_MASK },
197 { ArchSpec::eCore_thumbv4t , llvm::MachO::CPU_TYPE_ARM , 5 , UINT32_MAX , SUBTYPE_MASK },
198 { ArchSpec::eCore_thumbv5 , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
199 { ArchSpec::eCore_thumbv5e , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
200 { ArchSpec::eCore_thumbv6 , llvm::MachO::CPU_TYPE_ARM , 6 , UINT32_MAX , SUBTYPE_MASK },
Jason Molenda64a11732013-10-08 03:01:08 +0000201 { ArchSpec::eCore_thumbv6m , llvm::MachO::CPU_TYPE_ARM , 14 , UINT32_MAX , SUBTYPE_MASK },
Charles Davis510938e2013-08-27 05:04:57 +0000202 { ArchSpec::eCore_thumbv7 , llvm::MachO::CPU_TYPE_ARM , 9 , UINT32_MAX , SUBTYPE_MASK },
203 { ArchSpec::eCore_thumbv7f , llvm::MachO::CPU_TYPE_ARM , 10 , UINT32_MAX , SUBTYPE_MASK },
204 { ArchSpec::eCore_thumbv7s , llvm::MachO::CPU_TYPE_ARM , 11 , UINT32_MAX , SUBTYPE_MASK },
205 { ArchSpec::eCore_thumbv7k , llvm::MachO::CPU_TYPE_ARM , 12 , UINT32_MAX , SUBTYPE_MASK },
206 { ArchSpec::eCore_thumbv7m , llvm::MachO::CPU_TYPE_ARM , 15 , UINT32_MAX , SUBTYPE_MASK },
207 { ArchSpec::eCore_thumbv7em , llvm::MachO::CPU_TYPE_ARM , 16 , UINT32_MAX , SUBTYPE_MASK },
208 { ArchSpec::eCore_ppc_generic , llvm::MachO::CPU_TYPE_POWERPC , CPU_ANY, UINT32_MAX , UINT32_MAX },
209 { ArchSpec::eCore_ppc_generic , llvm::MachO::CPU_TYPE_POWERPC , 0 , UINT32_MAX , SUBTYPE_MASK },
210 { ArchSpec::eCore_ppc_ppc601 , llvm::MachO::CPU_TYPE_POWERPC , 1 , UINT32_MAX , SUBTYPE_MASK },
211 { ArchSpec::eCore_ppc_ppc602 , llvm::MachO::CPU_TYPE_POWERPC , 2 , UINT32_MAX , SUBTYPE_MASK },
212 { ArchSpec::eCore_ppc_ppc603 , llvm::MachO::CPU_TYPE_POWERPC , 3 , UINT32_MAX , SUBTYPE_MASK },
213 { ArchSpec::eCore_ppc_ppc603e , llvm::MachO::CPU_TYPE_POWERPC , 4 , UINT32_MAX , SUBTYPE_MASK },
214 { ArchSpec::eCore_ppc_ppc603ev , llvm::MachO::CPU_TYPE_POWERPC , 5 , UINT32_MAX , SUBTYPE_MASK },
215 { ArchSpec::eCore_ppc_ppc604 , llvm::MachO::CPU_TYPE_POWERPC , 6 , UINT32_MAX , SUBTYPE_MASK },
216 { ArchSpec::eCore_ppc_ppc604e , llvm::MachO::CPU_TYPE_POWERPC , 7 , UINT32_MAX , SUBTYPE_MASK },
217 { ArchSpec::eCore_ppc_ppc620 , llvm::MachO::CPU_TYPE_POWERPC , 8 , UINT32_MAX , SUBTYPE_MASK },
218 { ArchSpec::eCore_ppc_ppc750 , llvm::MachO::CPU_TYPE_POWERPC , 9 , UINT32_MAX , SUBTYPE_MASK },
219 { ArchSpec::eCore_ppc_ppc7400 , llvm::MachO::CPU_TYPE_POWERPC , 10 , UINT32_MAX , SUBTYPE_MASK },
220 { ArchSpec::eCore_ppc_ppc7450 , llvm::MachO::CPU_TYPE_POWERPC , 11 , UINT32_MAX , SUBTYPE_MASK },
221 { ArchSpec::eCore_ppc_ppc970 , llvm::MachO::CPU_TYPE_POWERPC , 100 , UINT32_MAX , SUBTYPE_MASK },
222 { ArchSpec::eCore_ppc64_generic , llvm::MachO::CPU_TYPE_POWERPC64 , 0 , UINT32_MAX , SUBTYPE_MASK },
223 { ArchSpec::eCore_ppc64_ppc970_64 , llvm::MachO::CPU_TYPE_POWERPC64 , 100 , UINT32_MAX , SUBTYPE_MASK },
224 { ArchSpec::eCore_x86_32_i386 , llvm::MachO::CPU_TYPE_I386 , 3 , UINT32_MAX , SUBTYPE_MASK },
225 { ArchSpec::eCore_x86_32_i486 , llvm::MachO::CPU_TYPE_I386 , 4 , UINT32_MAX , SUBTYPE_MASK },
226 { ArchSpec::eCore_x86_32_i486sx , llvm::MachO::CPU_TYPE_I386 , 0x84 , UINT32_MAX , SUBTYPE_MASK },
Greg Claytona86dc432014-01-22 23:42:03 +0000227 { ArchSpec::eCore_x86_32_i386 , llvm::MachO::CPU_TYPE_I386 , CPU_ANY, UINT32_MAX , UINT32_MAX },
Charles Davis510938e2013-08-27 05:04:57 +0000228 { ArchSpec::eCore_x86_64_x86_64 , llvm::MachO::CPU_TYPE_X86_64 , 3 , UINT32_MAX , SUBTYPE_MASK },
229 { ArchSpec::eCore_x86_64_x86_64 , llvm::MachO::CPU_TYPE_X86_64 , 4 , UINT32_MAX , SUBTYPE_MASK },
Greg Claytona86dc432014-01-22 23:42:03 +0000230 { ArchSpec::eCore_x86_64_x86_64h , llvm::MachO::CPU_TYPE_X86_64 , 8 , UINT32_MAX , SUBTYPE_MASK },
231 { 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 +0000232 // Catch any unknown mach architectures so we can always use the object and symbol mach-o files
Charles Davis510938e2013-08-27 05:04:57 +0000233 { ArchSpec::eCore_uknownMach32 , 0 , 0 , 0xFF000000u, 0x00000000u },
234 { ArchSpec::eCore_uknownMach64 , llvm::MachO::CPU_ARCH_ABI64 , 0 , 0xFF000000u, 0x00000000u }
Greg Clayton64195a22011-02-23 00:35:02 +0000235};
236static const ArchDefinition g_macho_arch_def = {
237 eArchTypeMachO,
Saleem Abdulrasool28606952014-06-27 05:17:41 +0000238 llvm::array_lengthof(g_macho_arch_entries),
Greg Clayton64195a22011-02-23 00:35:02 +0000239 g_macho_arch_entries,
Greg Clayton64195a22011-02-23 00:35:02 +0000240 "mach-o"
Greg Clayton41f92322010-06-11 03:25:34 +0000241};
242
Greg Clayton64195a22011-02-23 00:35:02 +0000243//===----------------------------------------------------------------------===//
244// A table that gets searched linearly for matches. This table is used to
245// convert cpu type and subtypes to architecture names, and to convert
246// architecture names to cpu types and subtypes. The ordering is important and
247// allows the precedence to be set when the table is built.
248static const ArchDefinitionEntry g_elf_arch_entries[] =
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000249{
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000250 { ArchSpec::eCore_sparc_generic , llvm::ELF::EM_SPARC , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // Sparc
251 { ArchSpec::eCore_x86_32_i386 , llvm::ELF::EM_386 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // Intel 80386
252 { ArchSpec::eCore_x86_32_i486 , llvm::ELF::EM_486 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // Intel 486 (deprecated)
253 { ArchSpec::eCore_ppc_generic , llvm::ELF::EM_PPC , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // PowerPC
254 { ArchSpec::eCore_ppc64_generic , llvm::ELF::EM_PPC64 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // PowerPC64
255 { ArchSpec::eCore_arm_generic , llvm::ELF::EM_ARM , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // ARM
256 { ArchSpec::eCore_sparc9_generic , llvm::ELF::EM_SPARCV9, LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // SPARC V9
Ed Masteb73f8442013-10-10 00:59:47 +0000257 { ArchSpec::eCore_x86_64_x86_64 , llvm::ELF::EM_X86_64 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // AMD64
Deepak Panickal6d3df422014-02-19 11:16:46 +0000258 { ArchSpec::eCore_mips64 , llvm::ELF::EM_MIPS , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // MIPS
Todd Fiala14bbef52014-07-01 23:33:32 +0000259 { ArchSpec::eCore_hexagon_generic , llvm::ELF::EM_HEXAGON, LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // HEXAGON
260 { ArchSpec::eCore_kalimba , llvm::ELF::EM_CSR_KALIMBA, LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu } // KALIMBA
261
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000262};
263
Greg Clayton64195a22011-02-23 00:35:02 +0000264static const ArchDefinition g_elf_arch_def = {
265 eArchTypeELF,
Saleem Abdulrasool28606952014-06-27 05:17:41 +0000266 llvm::array_lengthof(g_elf_arch_entries),
Greg Clayton64195a22011-02-23 00:35:02 +0000267 g_elf_arch_entries,
Greg Clayton64195a22011-02-23 00:35:02 +0000268 "elf",
Greg Clayton41f92322010-06-11 03:25:34 +0000269};
270
Charles Davis237ad972013-08-27 05:04:33 +0000271static const ArchDefinitionEntry g_coff_arch_entries[] =
272{
273 { ArchSpec::eCore_x86_32_i386 , llvm::COFF::IMAGE_FILE_MACHINE_I386 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // Intel 80386
274 { ArchSpec::eCore_ppc_generic , llvm::COFF::IMAGE_FILE_MACHINE_POWERPC , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // PowerPC
275 { ArchSpec::eCore_ppc_generic , llvm::COFF::IMAGE_FILE_MACHINE_POWERPCFP, LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // PowerPC (with FPU)
276 { ArchSpec::eCore_arm_generic , llvm::COFF::IMAGE_FILE_MACHINE_ARM , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // ARM
Saleem Abdulrasool1108cb32014-03-11 03:09:08 +0000277 { ArchSpec::eCore_arm_armv7 , llvm::COFF::IMAGE_FILE_MACHINE_ARMNT , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // ARMv7
Charles Davis237ad972013-08-27 05:04:33 +0000278 { ArchSpec::eCore_thumb , llvm::COFF::IMAGE_FILE_MACHINE_THUMB , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // ARMv7
279 { ArchSpec::eCore_x86_64_x86_64, llvm::COFF::IMAGE_FILE_MACHINE_AMD64 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu } // AMD64
280};
281
282static const ArchDefinition g_coff_arch_def = {
283 eArchTypeCOFF,
Saleem Abdulrasool28606952014-06-27 05:17:41 +0000284 llvm::array_lengthof(g_coff_arch_entries),
Charles Davis237ad972013-08-27 05:04:33 +0000285 g_coff_arch_entries,
286 "pe-coff",
287};
288
Greg Clayton64195a22011-02-23 00:35:02 +0000289//===----------------------------------------------------------------------===//
290// Table of all ArchDefinitions
291static const ArchDefinition *g_arch_definitions[] = {
292 &g_macho_arch_def,
Charles Davis237ad972013-08-27 05:04:33 +0000293 &g_elf_arch_def,
294 &g_coff_arch_def
Greg Clayton64195a22011-02-23 00:35:02 +0000295};
Greg Clayton41f92322010-06-11 03:25:34 +0000296
Saleem Abdulrasool28606952014-06-27 05:17:41 +0000297static const size_t k_num_arch_definitions = llvm::array_lengthof(g_arch_definitions);
Greg Clayton64195a22011-02-23 00:35:02 +0000298
299//===----------------------------------------------------------------------===//
300// Static helper functions.
301
302
303// Get the architecture definition for a given object type.
304static const ArchDefinition *
305FindArchDefinition (ArchitectureType arch_type)
306{
307 for (unsigned int i = 0; i < k_num_arch_definitions; ++i)
308 {
309 const ArchDefinition *def = g_arch_definitions[i];
310 if (def->type == arch_type)
311 return def;
312 }
313 return NULL;
314}
315
316// Get an architecture definition by name.
317static const CoreDefinition *
318FindCoreDefinition (llvm::StringRef name)
319{
Greg Clayton56b79682014-07-23 18:12:06 +0000320 for (unsigned int i = 0; i < llvm::array_lengthof(g_core_definitions); ++i)
Greg Clayton64195a22011-02-23 00:35:02 +0000321 {
322 if (name.equals_lower(g_core_definitions[i].name))
323 return &g_core_definitions[i];
324 }
325 return NULL;
326}
327
328static inline const CoreDefinition *
329FindCoreDefinition (ArchSpec::Core core)
330{
Greg Clayton56b79682014-07-23 18:12:06 +0000331 if (core >= 0 && core < llvm::array_lengthof(g_core_definitions))
Greg Clayton64195a22011-02-23 00:35:02 +0000332 return &g_core_definitions[core];
333 return NULL;
334}
335
336// Get a definition entry by cpu type and subtype.
337static const ArchDefinitionEntry *
338FindArchDefinitionEntry (const ArchDefinition *def, uint32_t cpu, uint32_t sub)
339{
340 if (def == NULL)
341 return NULL;
342
Greg Clayton64195a22011-02-23 00:35:02 +0000343 const ArchDefinitionEntry *entries = def->entries;
344 for (size_t i = 0; i < def->num_entries; ++i)
345 {
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000346 if (entries[i].cpu == (cpu & entries[i].cpu_mask))
347 if (entries[i].sub == (sub & entries[i].sub_mask))
348 return &entries[i];
Greg Clayton64195a22011-02-23 00:35:02 +0000349 }
350 return NULL;
351}
352
353static const ArchDefinitionEntry *
354FindArchDefinitionEntry (const ArchDefinition *def, ArchSpec::Core core)
355{
356 if (def == NULL)
357 return NULL;
358
359 const ArchDefinitionEntry *entries = def->entries;
360 for (size_t i = 0; i < def->num_entries; ++i)
361 {
362 if (entries[i].core == core)
363 return &entries[i];
364 }
365 return NULL;
366}
367
368//===----------------------------------------------------------------------===//
369// Constructors and destructors.
370
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000371ArchSpec::ArchSpec() :
Greg Clayton514487e2011-02-15 21:59:32 +0000372 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000373 m_core (kCore_invalid),
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000374 m_byte_order (eByteOrderInvalid),
375 m_distribution_id ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000376{
377}
378
Greg Claytoneb0103f2011-04-07 22:46:35 +0000379ArchSpec::ArchSpec (const char *triple_cstr, Platform *platform) :
Greg Clayton514487e2011-02-15 21:59:32 +0000380 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000381 m_core (kCore_invalid),
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000382 m_byte_order (eByteOrderInvalid),
383 m_distribution_id ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000384{
Greg Clayton64195a22011-02-23 00:35:02 +0000385 if (triple_cstr)
Greg Claytoneb0103f2011-04-07 22:46:35 +0000386 SetTriple(triple_cstr, platform);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000387}
388
Greg Clayton70512312012-05-08 01:45:38 +0000389
390ArchSpec::ArchSpec (const char *triple_cstr) :
391 m_triple (),
392 m_core (kCore_invalid),
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000393 m_byte_order (eByteOrderInvalid),
394 m_distribution_id ()
Greg Clayton70512312012-05-08 01:45:38 +0000395{
396 if (triple_cstr)
397 SetTriple(triple_cstr);
398}
399
Greg Clayton64195a22011-02-23 00:35:02 +0000400ArchSpec::ArchSpec(const llvm::Triple &triple) :
Greg Clayton514487e2011-02-15 21:59:32 +0000401 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000402 m_core (kCore_invalid),
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000403 m_byte_order (eByteOrderInvalid),
404 m_distribution_id ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000405{
Greg Clayton64195a22011-02-23 00:35:02 +0000406 SetTriple(triple);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000407}
408
Greg Claytone0d378b2011-03-24 21:19:54 +0000409ArchSpec::ArchSpec (ArchitectureType arch_type, uint32_t cpu, uint32_t subtype) :
Greg Clayton64195a22011-02-23 00:35:02 +0000410 m_triple (),
411 m_core (kCore_invalid),
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000412 m_byte_order (eByteOrderInvalid),
413 m_distribution_id ()
Greg Clayton64195a22011-02-23 00:35:02 +0000414{
415 SetArchitecture (arch_type, cpu, subtype);
416}
417
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000418ArchSpec::~ArchSpec()
419{
420}
421
Greg Clayton64195a22011-02-23 00:35:02 +0000422//===----------------------------------------------------------------------===//
423// Assignment and initialization.
424
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000425const ArchSpec&
426ArchSpec::operator= (const ArchSpec& rhs)
427{
428 if (this != &rhs)
429 {
Greg Clayton514487e2011-02-15 21:59:32 +0000430 m_triple = rhs.m_triple;
Greg Clayton64195a22011-02-23 00:35:02 +0000431 m_core = rhs.m_core;
Greg Clayton514487e2011-02-15 21:59:32 +0000432 m_byte_order = rhs.m_byte_order;
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000433 m_distribution_id = rhs.m_distribution_id;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000434 }
435 return *this;
436}
437
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000438void
439ArchSpec::Clear()
440{
Greg Clayton514487e2011-02-15 21:59:32 +0000441 m_triple = llvm::Triple();
Greg Clayton64195a22011-02-23 00:35:02 +0000442 m_core = kCore_invalid;
443 m_byte_order = eByteOrderInvalid;
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000444 m_distribution_id.Clear ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000445}
446
Greg Clayton64195a22011-02-23 00:35:02 +0000447//===----------------------------------------------------------------------===//
448// Predicates.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000449
Greg Clayton41f92322010-06-11 03:25:34 +0000450
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000451const char *
Greg Clayton64195a22011-02-23 00:35:02 +0000452ArchSpec::GetArchitectureName () 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)
456 return core_def->name;
457 return "unknown";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000458}
459
Greg Clayton64195a22011-02-23 00:35:02 +0000460uint32_t
461ArchSpec::GetMachOCPUType () 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)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000465 {
Greg Clayton64195a22011-02-23 00:35:02 +0000466 const ArchDefinitionEntry *arch_def = FindArchDefinitionEntry (&g_macho_arch_def, core_def->core);
467 if (arch_def)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000468 {
Greg Clayton64195a22011-02-23 00:35:02 +0000469 return arch_def->cpu;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000470 }
471 }
Greg Clayton64195a22011-02-23 00:35:02 +0000472 return LLDB_INVALID_CPUTYPE;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000473}
474
Greg Clayton64195a22011-02-23 00:35:02 +0000475uint32_t
476ArchSpec::GetMachOCPUSubType () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000477{
Greg Clayton64195a22011-02-23 00:35:02 +0000478 const CoreDefinition *core_def = FindCoreDefinition (m_core);
479 if (core_def)
480 {
481 const ArchDefinitionEntry *arch_def = FindArchDefinitionEntry (&g_macho_arch_def, core_def->core);
482 if (arch_def)
483 {
Greg Clayton1cb64962011-03-24 04:28:38 +0000484 return arch_def->sub;
Greg Clayton64195a22011-02-23 00:35:02 +0000485 }
486 }
487 return LLDB_INVALID_CPUTYPE;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000488}
489
Greg Clayton64195a22011-02-23 00:35:02 +0000490llvm::Triple::ArchType
491ArchSpec::GetMachine () const
492{
493 const CoreDefinition *core_def = FindCoreDefinition (m_core);
494 if (core_def)
495 return core_def->machine;
496
497 return llvm::Triple::UnknownArch;
498}
499
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000500const ConstString&
501ArchSpec::GetDistributionId () const
502{
503 return m_distribution_id;
504}
505
506void
507ArchSpec::SetDistributionId (const char* distribution_id)
508{
509 m_distribution_id.SetCString (distribution_id);
510}
511
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000512uint32_t
513ArchSpec::GetAddressByteSize() 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->addr_byte_size;
Greg Clayton41f92322010-06-11 03:25:34 +0000518 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000519}
520
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000521ByteOrder
522ArchSpec::GetDefaultEndian () const
523{
Greg Clayton64195a22011-02-23 00:35:02 +0000524 const CoreDefinition *core_def = FindCoreDefinition (m_core);
525 if (core_def)
526 return core_def->default_byte_order;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000527 return eByteOrderInvalid;
528}
529
Greg Clayton64195a22011-02-23 00:35:02 +0000530lldb::ByteOrder
531ArchSpec::GetByteOrder () const
532{
533 if (m_byte_order == eByteOrderInvalid)
534 return GetDefaultEndian();
535 return m_byte_order;
536}
537
538//===----------------------------------------------------------------------===//
539// Mutators.
540
541bool
542ArchSpec::SetTriple (const llvm::Triple &triple)
543{
544 m_triple = triple;
545
546 llvm::StringRef arch_name (m_triple.getArchName());
547 const CoreDefinition *core_def = FindCoreDefinition (arch_name);
548 if (core_def)
549 {
550 m_core = core_def->core;
Greg Claytoneb0103f2011-04-07 22:46:35 +0000551 // Set the byte order to the default byte order for an architecture.
552 // This can be modified if needed for cases when cores handle both
553 // big and little endian
554 m_byte_order = core_def->default_byte_order;
Greg Clayton64195a22011-02-23 00:35:02 +0000555 }
556 else
557 {
558 Clear();
559 }
560
561
562 return IsValid();
563}
564
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000565static bool
566ParseMachCPUDashSubtypeTriple (const char *triple_cstr, ArchSpec &arch)
567{
568 // Accept "12-10" or "12.10" as cpu type/subtype
569 if (isdigit(triple_cstr[0]))
570 {
571 char *end = NULL;
572 errno = 0;
Greg Claytonc7bece562013-01-25 18:06:21 +0000573 uint32_t cpu = (uint32_t)::strtoul (triple_cstr, &end, 0);
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000574 if (errno == 0 && cpu != 0 && end && ((*end == '-') || (*end == '.')))
575 {
576 errno = 0;
Greg Claytonc7bece562013-01-25 18:06:21 +0000577 uint32_t sub = (uint32_t)::strtoul (end + 1, &end, 0);
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000578 if (errno == 0 && end && ((*end == '-') || (*end == '.') || (*end == '\0')))
579 {
580 if (arch.SetArchitecture (eArchTypeMachO, cpu, sub))
581 {
582 if (*end == '-')
583 {
584 llvm::StringRef vendor_os (end + 1);
585 size_t dash_pos = vendor_os.find('-');
586 if (dash_pos != llvm::StringRef::npos)
587 {
588 llvm::StringRef vendor_str(vendor_os.substr(0, dash_pos));
589 arch.GetTriple().setVendorName(vendor_str);
590 const size_t vendor_start_pos = dash_pos+1;
Greg Claytonc7bece562013-01-25 18:06:21 +0000591 dash_pos = vendor_os.find('-', vendor_start_pos);
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000592 if (dash_pos == llvm::StringRef::npos)
593 {
594 if (vendor_start_pos < vendor_os.size())
595 arch.GetTriple().setOSName(vendor_os.substr(vendor_start_pos));
596 }
597 else
598 {
599 arch.GetTriple().setOSName(vendor_os.substr(vendor_start_pos, dash_pos - vendor_start_pos));
600 }
601 }
602 }
603 return true;
604 }
605 }
606 }
607 }
608 return false;
609}
Greg Clayton64195a22011-02-23 00:35:02 +0000610bool
Greg Clayton70512312012-05-08 01:45:38 +0000611ArchSpec::SetTriple (const char *triple_cstr)
Greg Clayton64195a22011-02-23 00:35:02 +0000612{
Greg Clayton23aca092011-08-12 23:32:52 +0000613 if (triple_cstr && triple_cstr[0])
Greg Clayton64195a22011-02-23 00:35:02 +0000614 {
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000615 if (ParseMachCPUDashSubtypeTriple (triple_cstr, *this))
616 return true;
617
Greg Clayton64195a22011-02-23 00:35:02 +0000618 llvm::StringRef triple_stref (triple_cstr);
619 if (triple_stref.startswith (LLDB_ARCH_DEFAULT))
620 {
621 // Special case for the current host default architectures...
622 if (triple_stref.equals (LLDB_ARCH_DEFAULT_32BIT))
623 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture32);
624 else if (triple_stref.equals (LLDB_ARCH_DEFAULT_64BIT))
625 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture64);
626 else if (triple_stref.equals (LLDB_ARCH_DEFAULT))
627 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture);
628 }
629 else
630 {
631 std::string normalized_triple_sstr (llvm::Triple::normalize(triple_stref));
632 triple_stref = normalized_triple_sstr;
Greg Clayton70512312012-05-08 01:45:38 +0000633 SetTriple (llvm::Triple (triple_stref));
634 }
635 }
636 else
637 Clear();
638 return IsValid();
639}
640
641bool
642ArchSpec::SetTriple (const char *triple_cstr, Platform *platform)
643{
644 if (triple_cstr && triple_cstr[0])
645 {
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000646 if (ParseMachCPUDashSubtypeTriple (triple_cstr, *this))
647 return true;
648
Greg Clayton70512312012-05-08 01:45:38 +0000649 llvm::StringRef triple_stref (triple_cstr);
650 if (triple_stref.startswith (LLDB_ARCH_DEFAULT))
651 {
652 // Special case for the current host default architectures...
653 if (triple_stref.equals (LLDB_ARCH_DEFAULT_32BIT))
654 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture32);
655 else if (triple_stref.equals (LLDB_ARCH_DEFAULT_64BIT))
656 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture64);
657 else if (triple_stref.equals (LLDB_ARCH_DEFAULT))
658 *this = Host::GetArchitecture (Host::eSystemDefaultArchitecture);
659 }
660 else
661 {
662 ArchSpec raw_arch (triple_cstr);
663
664 std::string normalized_triple_sstr (llvm::Triple::normalize(triple_stref));
665 triple_stref = normalized_triple_sstr;
Greg Claytoneb0103f2011-04-07 22:46:35 +0000666 llvm::Triple normalized_triple (triple_stref);
667
668 const bool os_specified = normalized_triple.getOSName().size() > 0;
669 const bool vendor_specified = normalized_triple.getVendorName().size() > 0;
670 const bool env_specified = normalized_triple.getEnvironmentName().size() > 0;
671
672 // If we got an arch only, then default the vendor, os, environment
673 // to match the platform if one is supplied
674 if (!(os_specified || vendor_specified || env_specified))
675 {
676 if (platform)
677 {
678 // If we were given a platform, use the platform's system
679 // architecture. If this is not available (might not be
680 // connected) use the first supported architecture.
Greg Clayton70512312012-05-08 01:45:38 +0000681 ArchSpec compatible_arch;
Greg Clayton1e0c8842013-01-11 20:49:54 +0000682 if (platform->IsCompatibleArchitecture (raw_arch, false, &compatible_arch))
Greg Claytoneb0103f2011-04-07 22:46:35 +0000683 {
Greg Clayton70512312012-05-08 01:45:38 +0000684 if (compatible_arch.IsValid())
685 {
686 const llvm::Triple &compatible_triple = compatible_arch.GetTriple();
687 if (!vendor_specified)
688 normalized_triple.setVendor(compatible_triple.getVendor());
689 if (!os_specified)
690 normalized_triple.setOS(compatible_triple.getOS());
691 if (!env_specified && compatible_triple.getEnvironmentName().size())
692 normalized_triple.setEnvironment(compatible_triple.getEnvironment());
693 }
Greg Claytoneb0103f2011-04-07 22:46:35 +0000694 }
Greg Clayton70512312012-05-08 01:45:38 +0000695 else
Greg Claytoneb0103f2011-04-07 22:46:35 +0000696 {
Greg Clayton70512312012-05-08 01:45:38 +0000697 *this = raw_arch;
698 return IsValid();
Greg Claytoneb0103f2011-04-07 22:46:35 +0000699 }
700 }
701 else
702 {
703 // No platform specified, fall back to the host system for
704 // the default vendor, os, and environment.
Sean Callananbfb237bc2011-11-04 22:46:46 +0000705 llvm::Triple host_triple(llvm::sys::getDefaultTargetTriple());
Greg Clayton70512312012-05-08 01:45:38 +0000706 if (!vendor_specified)
707 normalized_triple.setVendor(host_triple.getVendor());
708 if (!vendor_specified)
709 normalized_triple.setOS(host_triple.getOS());
710 if (!env_specified && host_triple.getEnvironmentName().size())
711 normalized_triple.setEnvironment(host_triple.getEnvironment());
Greg Claytoneb0103f2011-04-07 22:46:35 +0000712 }
713 }
714 SetTriple (normalized_triple);
Greg Clayton64195a22011-02-23 00:35:02 +0000715 }
716 }
717 else
718 Clear();
719 return IsValid();
720}
721
Greg Clayton64195a22011-02-23 00:35:02 +0000722bool
Greg Claytone0d378b2011-03-24 21:19:54 +0000723ArchSpec::SetArchitecture (ArchitectureType arch_type, uint32_t cpu, uint32_t sub)
Greg Clayton64195a22011-02-23 00:35:02 +0000724{
725 m_core = kCore_invalid;
726 bool update_triple = true;
727 const ArchDefinition *arch_def = FindArchDefinition(arch_type);
728 if (arch_def)
729 {
730 const ArchDefinitionEntry *arch_def_entry = FindArchDefinitionEntry (arch_def, cpu, sub);
731 if (arch_def_entry)
732 {
733 const CoreDefinition *core_def = FindCoreDefinition (arch_def_entry->core);
734 if (core_def)
735 {
736 m_core = core_def->core;
737 update_triple = false;
Greg Clayton593577a2011-09-21 03:57:31 +0000738 // Always use the architecture name because it might be more descriptive
739 // than the architecture enum ("armv7" -> llvm::Triple::arm).
740 m_triple.setArchName(llvm::StringRef(core_def->name));
Greg Clayton64195a22011-02-23 00:35:02 +0000741 if (arch_type == eArchTypeMachO)
742 {
743 m_triple.setVendor (llvm::Triple::Apple);
Greg Clayton70512312012-05-08 01:45:38 +0000744
745 switch (core_def->machine)
746 {
Todd Fialad8eaa172014-07-23 14:37:35 +0000747 case llvm::Triple::aarch64:
Greg Clayton70512312012-05-08 01:45:38 +0000748 case llvm::Triple::arm:
749 case llvm::Triple::thumb:
750 m_triple.setOS (llvm::Triple::IOS);
751 break;
752
753 case llvm::Triple::x86:
754 case llvm::Triple::x86_64:
755 default:
756 m_triple.setOS (llvm::Triple::MacOSX);
757 break;
758 }
Greg Clayton64195a22011-02-23 00:35:02 +0000759 }
760 else
761 {
762 m_triple.setVendor (llvm::Triple::UnknownVendor);
763 m_triple.setOS (llvm::Triple::UnknownOS);
764 }
Greg Clayton593577a2011-09-21 03:57:31 +0000765 // Fall back onto setting the machine type if the arch by name failed...
766 if (m_triple.getArch () == llvm::Triple::UnknownArch)
767 m_triple.setArch (core_def->machine);
Greg Clayton64195a22011-02-23 00:35:02 +0000768 }
769 }
770 }
771 CoreUpdated(update_triple);
772 return IsValid();
773}
774
Greg Clayton357132e2011-03-26 19:14:58 +0000775uint32_t
776ArchSpec::GetMinimumOpcodeByteSize() const
Greg Clayton64195a22011-02-23 00:35:02 +0000777{
Greg Clayton357132e2011-03-26 19:14:58 +0000778 const CoreDefinition *core_def = FindCoreDefinition (m_core);
779 if (core_def)
780 return core_def->min_opcode_byte_size;
781 return 0;
782}
783
784uint32_t
785ArchSpec::GetMaximumOpcodeByteSize() const
786{
787 const CoreDefinition *core_def = FindCoreDefinition (m_core);
788 if (core_def)
789 return core_def->max_opcode_byte_size;
790 return 0;
Greg Clayton64195a22011-02-23 00:35:02 +0000791}
792
Jason Molendaba813dc2012-11-04 03:20:05 +0000793bool
794ArchSpec::IsExactMatch (const ArchSpec& rhs) const
795{
Sean Callananbf4b7be2012-12-13 22:07:14 +0000796 return IsEqualTo (rhs, true);
Jason Molendaba813dc2012-11-04 03:20:05 +0000797}
798
799bool
800ArchSpec::IsCompatibleMatch (const ArchSpec& rhs) const
801{
Sean Callananbf4b7be2012-12-13 22:07:14 +0000802 return IsEqualTo (rhs, false);
Jason Molendaba813dc2012-11-04 03:20:05 +0000803}
804
805bool
Sean Callananbf4b7be2012-12-13 22:07:14 +0000806ArchSpec::IsEqualTo (const ArchSpec& rhs, bool exact_match) const
Jason Molendaba813dc2012-11-04 03:20:05 +0000807{
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000808 // explicitly ignoring m_distribution_id in this method.
809
Jason Molendaba813dc2012-11-04 03:20:05 +0000810 if (GetByteOrder() != rhs.GetByteOrder())
811 return false;
812
813 const ArchSpec::Core lhs_core = GetCore ();
814 const ArchSpec::Core rhs_core = rhs.GetCore ();
815
816 const bool core_match = cores_match (lhs_core, rhs_core, true, exact_match);
817
818 if (core_match)
819 {
820 const llvm::Triple &lhs_triple = GetTriple();
821 const llvm::Triple &rhs_triple = rhs.GetTriple();
822
823 const llvm::Triple::VendorType lhs_triple_vendor = lhs_triple.getVendor();
824 const llvm::Triple::VendorType rhs_triple_vendor = rhs_triple.getVendor();
825 if (lhs_triple_vendor != rhs_triple_vendor)
826 {
Sean Callananbf4b7be2012-12-13 22:07:14 +0000827 if (exact_match)
828 {
829 const bool rhs_vendor_specified = rhs.TripleVendorWasSpecified();
830 const bool lhs_vendor_specified = TripleVendorWasSpecified();
831 // Both architectures had the vendor specified, so if they aren't
832 // equal then we return false
833 if (rhs_vendor_specified && lhs_vendor_specified)
834 return false;
835 }
Jason Molendaba813dc2012-11-04 03:20:05 +0000836
837 // Only fail if both vendor types are not unknown
838 if (lhs_triple_vendor != llvm::Triple::UnknownVendor &&
839 rhs_triple_vendor != llvm::Triple::UnknownVendor)
840 return false;
841 }
842
843 const llvm::Triple::OSType lhs_triple_os = lhs_triple.getOS();
844 const llvm::Triple::OSType rhs_triple_os = rhs_triple.getOS();
845 if (lhs_triple_os != rhs_triple_os)
846 {
Sean Callananbf4b7be2012-12-13 22:07:14 +0000847 if (exact_match)
848 {
849 const bool rhs_os_specified = rhs.TripleOSWasSpecified();
850 const bool lhs_os_specified = TripleOSWasSpecified();
851 // Both architectures had the OS specified, so if they aren't
852 // equal then we return false
853 if (rhs_os_specified && lhs_os_specified)
854 return false;
855 }
Greg Clayton7ab7f892014-05-29 21:33:45 +0000856
Greg Clayton3f19ada2014-07-10 23:33:37 +0000857 // Only fail if both os types are not unknown
858 if (lhs_triple_os != llvm::Triple::UnknownOS &&
859 rhs_triple_os != llvm::Triple::UnknownOS)
860 return false;
Jason Molendaba813dc2012-11-04 03:20:05 +0000861 }
862
863 const llvm::Triple::EnvironmentType lhs_triple_env = lhs_triple.getEnvironment();
864 const llvm::Triple::EnvironmentType rhs_triple_env = rhs_triple.getEnvironment();
865
866 if (lhs_triple_env != rhs_triple_env)
867 {
868 // Only fail if both environment types are not unknown
869 if (lhs_triple_env != llvm::Triple::UnknownEnvironment &&
870 rhs_triple_env != llvm::Triple::UnknownEnvironment)
871 return false;
872 }
873 return true;
874 }
875 return false;
876}
877
Greg Clayton64195a22011-02-23 00:35:02 +0000878//===----------------------------------------------------------------------===//
879// Helper methods.
880
881void
882ArchSpec::CoreUpdated (bool update_triple)
883{
884 const CoreDefinition *core_def = FindCoreDefinition (m_core);
885 if (core_def)
886 {
887 if (update_triple)
888 m_triple = llvm::Triple(core_def->name, "unknown", "unknown");
889 m_byte_order = core_def->default_byte_order;
890 }
891 else
892 {
893 if (update_triple)
894 m_triple = llvm::Triple();
895 m_byte_order = eByteOrderInvalid;
896 }
897}
898
899//===----------------------------------------------------------------------===//
900// Operators.
901
Greg Clayton70512312012-05-08 01:45:38 +0000902static bool
Jason Molendaba813dc2012-11-04 03:20:05 +0000903cores_match (const ArchSpec::Core core1, const ArchSpec::Core core2, bool try_inverse, bool enforce_exact_match)
Greg Clayton70512312012-05-08 01:45:38 +0000904{
Jason Molendaba813dc2012-11-04 03:20:05 +0000905 if (core1 == core2)
906 return true;
907
Greg Clayton70512312012-05-08 01:45:38 +0000908 switch (core1)
909 {
Greg Clayton70512312012-05-08 01:45:38 +0000910 case ArchSpec::kCore_any:
911 return true;
912
Greg Clayton44362e02014-07-12 00:11:34 +0000913 case ArchSpec::eCore_arm_generic:
914 if (enforce_exact_match)
915 break;
916 // Fall through to case below
Greg Clayton70512312012-05-08 01:45:38 +0000917 case ArchSpec::kCore_arm_any:
918 if (core2 >= ArchSpec::kCore_arm_first && core2 <= ArchSpec::kCore_arm_last)
919 return true;
920 if (core2 >= ArchSpec::kCore_thumb_first && core2 <= ArchSpec::kCore_thumb_last)
921 return true;
922 if (core2 == ArchSpec::kCore_arm_any)
923 return true;
924 break;
Greg Claytona86dc432014-01-22 23:42:03 +0000925
Greg Clayton70512312012-05-08 01:45:38 +0000926 case ArchSpec::kCore_x86_32_any:
927 if ((core2 >= ArchSpec::kCore_x86_32_first && core2 <= ArchSpec::kCore_x86_32_last) || (core2 == ArchSpec::kCore_x86_32_any))
928 return true;
929 break;
930
931 case ArchSpec::kCore_ppc_any:
932 if ((core2 >= ArchSpec::kCore_ppc_first && core2 <= ArchSpec::kCore_ppc_last) || (core2 == ArchSpec::kCore_ppc_any))
933 return true;
934 break;
935
936 case ArchSpec::kCore_ppc64_any:
937 if ((core2 >= ArchSpec::kCore_ppc64_first && core2 <= ArchSpec::kCore_ppc64_last) || (core2 == ArchSpec::kCore_ppc64_any))
938 return true;
939 break;
940
Jason Molendaa3a04522013-09-27 23:21:54 +0000941 case ArchSpec::eCore_arm_armv6m:
942 if (!enforce_exact_match)
943 {
Greg Clayton44362e02014-07-12 00:11:34 +0000944 if (core2 == ArchSpec::eCore_arm_generic)
945 return true;
Jason Molendaa3a04522013-09-27 23:21:54 +0000946 try_inverse = false;
Jason Molendac7cda272013-09-27 23:29:10 +0000947 if (core2 == ArchSpec::eCore_arm_armv7)
Jason Molendaa3a04522013-09-27 23:21:54 +0000948 return true;
949 }
Deepak Panickal6d3df422014-02-19 11:16:46 +0000950
951 case ArchSpec::kCore_hexagon_any:
952 if ((core2 >= ArchSpec::kCore_hexagon_first && core2 <= ArchSpec::kCore_hexagon_last) || (core2 == ArchSpec::kCore_hexagon_any))
953 return true;
Jason Molendaa3a04522013-09-27 23:21:54 +0000954 break;
955
Jason Molenda7a1559c2013-03-08 01:20:17 +0000956 case ArchSpec::eCore_arm_armv7m:
957 case ArchSpec::eCore_arm_armv7em:
Johnny Chen1083b0d2012-08-28 22:53:40 +0000958 case ArchSpec::eCore_arm_armv7f:
959 case ArchSpec::eCore_arm_armv7k:
960 case ArchSpec::eCore_arm_armv7s:
Jason Molendaba813dc2012-11-04 03:20:05 +0000961 if (!enforce_exact_match)
962 {
Greg Clayton44362e02014-07-12 00:11:34 +0000963 if (core2 == ArchSpec::eCore_arm_generic)
964 return true;
Jason Molendaba813dc2012-11-04 03:20:05 +0000965 if (core2 == ArchSpec::eCore_arm_armv7)
966 return true;
Greg Clayton44362e02014-07-12 00:11:34 +0000967 try_inverse = false;
Jason Molendaba813dc2012-11-04 03:20:05 +0000968 }
Johnny Chen1083b0d2012-08-28 22:53:40 +0000969 break;
Greg Clayton52edb362014-07-14 22:53:02 +0000970
971 case ArchSpec::eCore_x86_64_x86_64h:
972 if (!enforce_exact_match)
973 {
974 try_inverse = false;
975 if (core2 == ArchSpec::eCore_x86_64_x86_64)
976 return true;
977 }
978 break;
Johnny Chen1083b0d2012-08-28 22:53:40 +0000979
Greg Clayton70512312012-05-08 01:45:38 +0000980 default:
981 break;
982 }
983 if (try_inverse)
Jason Molendaba813dc2012-11-04 03:20:05 +0000984 return cores_match (core2, core1, false, enforce_exact_match);
Greg Clayton70512312012-05-08 01:45:38 +0000985 return false;
986}
987
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000988bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000989lldb_private::operator<(const ArchSpec& lhs, const ArchSpec& rhs)
990{
Greg Clayton64195a22011-02-23 00:35:02 +0000991 const ArchSpec::Core lhs_core = lhs.GetCore ();
992 const ArchSpec::Core rhs_core = rhs.GetCore ();
993 return lhs_core < rhs_core;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000994}