blob: d3641150630572e00a6a6a5a0b90f5072533fab1 [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"
Zachary Turner13b18262014-08-20 16:42:51 +000023#include "lldb/Core/StringList.h"
Greg Clayton514487e2011-02-15 21:59:32 +000024#include "lldb/Host/Endian.h"
Zachary Turner13b18262014-08-20 16:42:51 +000025#include "lldb/Host/HostInfo.h"
Greg Claytoneb0103f2011-04-07 22:46:35 +000026#include "lldb/Target/Platform.h"
Greg Clayton41f92322010-06-11 03:25:34 +000027
Chris Lattner30fdc8d2010-06-08 16:52:24 +000028using namespace lldb;
29using namespace lldb_private;
30
Greg Clayton64195a22011-02-23 00:35:02 +000031#define ARCH_SPEC_SEPARATOR_CHAR '-'
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032
Jason Molendaba813dc2012-11-04 03:20:05 +000033
34static bool cores_match (const ArchSpec::Core core1, const ArchSpec::Core core2, bool try_inverse, bool enforce_exact_match);
35
Greg Clayton64195a22011-02-23 00:35:02 +000036namespace lldb_private {
Chris Lattner30fdc8d2010-06-08 16:52:24 +000037
Greg Clayton64195a22011-02-23 00:35:02 +000038 struct CoreDefinition
39 {
40 ByteOrder default_byte_order;
41 uint32_t addr_byte_size;
Greg Clayton357132e2011-03-26 19:14:58 +000042 uint32_t min_opcode_byte_size;
43 uint32_t max_opcode_byte_size;
Greg Clayton64195a22011-02-23 00:35:02 +000044 llvm::Triple::ArchType machine;
45 ArchSpec::Core core;
Greg Clayton56b79682014-07-23 18:12:06 +000046 const char * const name;
Greg Clayton64195a22011-02-23 00:35:02 +000047 };
48
49}
50
51// This core information can be looked using the ArchSpec::Core as the index
Greg Clayton56b79682014-07-23 18:12:06 +000052static const CoreDefinition g_core_definitions[] =
Chris Lattner30fdc8d2010-06-08 16:52:24 +000053{
Greg Clayton357132e2011-03-26 19:14:58 +000054 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_generic , "arm" },
55 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv4 , "armv4" },
56 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv4t , "armv4t" },
57 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv5 , "armv5" },
Greg Claytonb5c39fe2011-12-16 18:15:52 +000058 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv5e , "armv5e" },
Greg Clayton357132e2011-03-26 19:14:58 +000059 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv5t , "armv5t" },
60 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv6 , "armv6" },
Jason Molendaa3a04522013-09-27 23:21:54 +000061 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv6m , "armv6m" },
Greg Clayton357132e2011-03-26 19:14:58 +000062 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7 , "armv7" },
63 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7f , "armv7f" },
Greg Clayton357132e2011-03-26 19:14:58 +000064 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7s , "armv7s" },
Jason Molenda7a1559c2013-03-08 01:20:17 +000065 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7k , "armv7k" },
66 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7m , "armv7m" },
67 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7em , "armv7em" },
Greg Clayton357132e2011-03-26 19:14:58 +000068 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_xscale , "xscale" },
Greg Claytonb5c39fe2011-12-16 18:15:52 +000069 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumb , "thumb" },
70 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv4t , "thumbv4t" },
71 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv5 , "thumbv5" },
72 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv5e , "thumbv5e" },
73 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv6 , "thumbv6" },
Jason Molendaa3a04522013-09-27 23:21:54 +000074 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv6m , "thumbv6m" },
Greg Claytonb5c39fe2011-12-16 18:15:52 +000075 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7 , "thumbv7" },
76 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7f , "thumbv7f" },
Greg Claytonb5c39fe2011-12-16 18:15:52 +000077 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7s , "thumbv7s" },
Jason Molenda7a1559c2013-03-08 01:20:17 +000078 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7k , "thumbv7k" },
79 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7m , "thumbv7m" },
80 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7em , "thumbv7em" },
Todd Fialad8eaa172014-07-23 14:37:35 +000081 { eByteOrderLittle, 8, 4, 4, llvm::Triple::aarch64, ArchSpec::eCore_arm_arm64 , "arm64" },
Ed Masteb73f8442013-10-10 00:59:47 +000082
83 { eByteOrderBig , 8, 4, 4, llvm::Triple::mips64 , ArchSpec::eCore_mips64 , "mips64" },
Greg Clayton64195a22011-02-23 00:35:02 +000084
Greg Clayton83b162d2013-08-12 18:34:04 +000085 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_generic , "ppc" },
86 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc601 , "ppc601" },
87 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc602 , "ppc602" },
88 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc603 , "ppc603" },
89 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc603e , "ppc603e" },
90 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc603ev , "ppc603ev" },
91 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc604 , "ppc604" },
92 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc604e , "ppc604e" },
93 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc620 , "ppc620" },
94 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc750 , "ppc750" },
95 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc7400 , "ppc7400" },
96 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc7450 , "ppc7450" },
97 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc970 , "ppc970" },
Greg Clayton64195a22011-02-23 00:35:02 +000098
Greg Clayton83b162d2013-08-12 18:34:04 +000099 { eByteOrderBig , 8, 4, 4, llvm::Triple::ppc64 , ArchSpec::eCore_ppc64_generic , "ppc64" },
100 { eByteOrderBig , 8, 4, 4, llvm::Triple::ppc64 , ArchSpec::eCore_ppc64_ppc970_64 , "ppc970-64" },
Greg Clayton64195a22011-02-23 00:35:02 +0000101
Greg Clayton357132e2011-03-26 19:14:58 +0000102 { eByteOrderLittle, 4, 4, 4, llvm::Triple::sparc , ArchSpec::eCore_sparc_generic , "sparc" },
103 { eByteOrderLittle, 8, 4, 4, llvm::Triple::sparcv9, ArchSpec::eCore_sparc9_generic , "sparcv9" },
Greg Clayton64195a22011-02-23 00:35:02 +0000104
Greg Claytonab65b342011-04-13 22:47:15 +0000105 { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i386 , "i386" },
106 { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i486 , "i486" },
107 { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i486sx , "i486sx" },
Virgile Bello97a70e42014-04-08 14:48:48 +0000108 { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i686 , "i686" },
Greg Clayton64195a22011-02-23 00:35:02 +0000109
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000110 { eByteOrderLittle, 8, 1, 15, llvm::Triple::x86_64 , ArchSpec::eCore_x86_64_x86_64 , "x86_64" },
Greg Claytona86dc432014-01-22 23:42:03 +0000111 { eByteOrderLittle, 8, 1, 15, llvm::Triple::x86_64 , ArchSpec::eCore_x86_64_x86_64h , "x86_64h" },
Deepak Panickal6d3df422014-02-19 11:16:46 +0000112 { eByteOrderLittle, 4, 4, 4, llvm::Triple::hexagon , ArchSpec::eCore_hexagon_generic, "hexagon" },
113 { eByteOrderLittle, 4, 4, 4, llvm::Triple::hexagon , ArchSpec::eCore_hexagon_hexagonv4, "hexagonv4" },
114 { eByteOrderLittle, 4, 4, 4, llvm::Triple::hexagon , ArchSpec::eCore_hexagon_hexagonv5, "hexagonv5" },
115
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000116 { eByteOrderLittle, 4, 4, 4 , llvm::Triple::UnknownArch , ArchSpec::eCore_uknownMach32 , "unknown-mach-32" },
Todd Fiala14bbef52014-07-01 23:33:32 +0000117 { eByteOrderLittle, 8, 4, 4 , llvm::Triple::UnknownArch , ArchSpec::eCore_uknownMach64 , "unknown-mach-64" },
118
Todd Fialacfee9632014-07-16 15:03:10 +0000119 { eByteOrderLittle, 4, 1, 1 , llvm::Triple::kalimba , ArchSpec::eCore_kalimba , "kalimba" }
Greg Clayton64195a22011-02-23 00:35:02 +0000120};
121
Greg Clayton56b79682014-07-23 18:12:06 +0000122// Ensure that we have an entry in the g_core_definitions for each core. If you comment out an entry above,
123// you will need to comment out the corresponding ArchSpec::Core enumeration.
Zachary Turner3b2065f2014-07-28 16:44:28 +0000124static_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 +0000125
126
Greg Clayton64195a22011-02-23 00:35:02 +0000127struct ArchDefinitionEntry
128{
129 ArchSpec::Core core;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000130 uint32_t cpu;
131 uint32_t sub;
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000132 uint32_t cpu_mask;
133 uint32_t sub_mask;
Greg Clayton64195a22011-02-23 00:35:02 +0000134};
135
136struct ArchDefinition
137{
138 ArchitectureType type;
139 size_t num_entries;
140 const ArchDefinitionEntry *entries;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000141 const char *name;
142};
143
Greg Clayton41f92322010-06-11 03:25:34 +0000144
Greg Claytonc7bece562013-01-25 18:06:21 +0000145size_t
Greg Claytonab65b342011-04-13 22:47:15 +0000146ArchSpec::AutoComplete (const char *name, StringList &matches)
147{
148 uint32_t i;
149 if (name && name[0])
150 {
Greg Clayton56b79682014-07-23 18:12:06 +0000151 for (i = 0; i < llvm::array_lengthof(g_core_definitions); ++i)
Greg Claytonab65b342011-04-13 22:47:15 +0000152 {
153 if (NameMatches(g_core_definitions[i].name, eNameMatchStartsWith, name))
154 matches.AppendString (g_core_definitions[i].name);
155 }
156 }
157 else
158 {
Greg Clayton56b79682014-07-23 18:12:06 +0000159 for (i = 0; i < llvm::array_lengthof(g_core_definitions); ++i)
Greg Claytonab65b342011-04-13 22:47:15 +0000160 matches.AppendString (g_core_definitions[i].name);
161 }
162 return matches.GetSize();
163}
164
165
166
Greg Clayton64195a22011-02-23 00:35:02 +0000167#define CPU_ANY (UINT32_MAX)
168
169//===----------------------------------------------------------------------===//
170// A table that gets searched linearly for matches. This table is used to
171// convert cpu type and subtypes to architecture names, and to convert
172// architecture names to cpu types and subtypes. The ordering is important and
173// allows the precedence to be set when the table is built.
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000174#define SUBTYPE_MASK 0x00FFFFFFu
Greg Clayton64195a22011-02-23 00:35:02 +0000175static const ArchDefinitionEntry g_macho_arch_entries[] =
Greg Clayton41f92322010-06-11 03:25:34 +0000176{
Charles Davis510938e2013-08-27 05:04:57 +0000177 { ArchSpec::eCore_arm_generic , llvm::MachO::CPU_TYPE_ARM , CPU_ANY, UINT32_MAX , UINT32_MAX },
178 { ArchSpec::eCore_arm_generic , llvm::MachO::CPU_TYPE_ARM , 0 , UINT32_MAX , SUBTYPE_MASK },
179 { ArchSpec::eCore_arm_armv4 , llvm::MachO::CPU_TYPE_ARM , 5 , UINT32_MAX , SUBTYPE_MASK },
180 { ArchSpec::eCore_arm_armv4t , llvm::MachO::CPU_TYPE_ARM , 5 , UINT32_MAX , SUBTYPE_MASK },
181 { ArchSpec::eCore_arm_armv6 , llvm::MachO::CPU_TYPE_ARM , 6 , UINT32_MAX , SUBTYPE_MASK },
Jason Molenda64a11732013-10-08 03:01:08 +0000182 { ArchSpec::eCore_arm_armv6m , llvm::MachO::CPU_TYPE_ARM , 14 , UINT32_MAX , SUBTYPE_MASK },
Charles Davis510938e2013-08-27 05:04:57 +0000183 { ArchSpec::eCore_arm_armv5 , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
184 { ArchSpec::eCore_arm_armv5e , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
185 { ArchSpec::eCore_arm_armv5t , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
186 { ArchSpec::eCore_arm_xscale , llvm::MachO::CPU_TYPE_ARM , 8 , UINT32_MAX , SUBTYPE_MASK },
187 { ArchSpec::eCore_arm_armv7 , llvm::MachO::CPU_TYPE_ARM , 9 , UINT32_MAX , SUBTYPE_MASK },
188 { ArchSpec::eCore_arm_armv7f , llvm::MachO::CPU_TYPE_ARM , 10 , UINT32_MAX , SUBTYPE_MASK },
189 { ArchSpec::eCore_arm_armv7s , llvm::MachO::CPU_TYPE_ARM , 11 , UINT32_MAX , SUBTYPE_MASK },
190 { ArchSpec::eCore_arm_armv7k , llvm::MachO::CPU_TYPE_ARM , 12 , UINT32_MAX , SUBTYPE_MASK },
191 { ArchSpec::eCore_arm_armv7m , llvm::MachO::CPU_TYPE_ARM , 15 , UINT32_MAX , SUBTYPE_MASK },
192 { ArchSpec::eCore_arm_armv7em , llvm::MachO::CPU_TYPE_ARM , 16 , UINT32_MAX , SUBTYPE_MASK },
Jason Molendaa3329782014-03-29 18:54:20 +0000193 { ArchSpec::eCore_arm_arm64 , llvm::MachO::CPU_TYPE_ARM64 , CPU_ANY, UINT32_MAX , SUBTYPE_MASK },
194 { ArchSpec::eCore_arm_arm64 , llvm::MachO::CPU_TYPE_ARM64 , 0 , UINT32_MAX , SUBTYPE_MASK },
195 { ArchSpec::eCore_arm_arm64 , llvm::MachO::CPU_TYPE_ARM64 , 1 , UINT32_MAX , SUBTYPE_MASK },
196 { ArchSpec::eCore_arm_arm64 , llvm::MachO::CPU_TYPE_ARM64 , 13 , UINT32_MAX , SUBTYPE_MASK },
Charles Davis510938e2013-08-27 05:04:57 +0000197 { ArchSpec::eCore_thumb , llvm::MachO::CPU_TYPE_ARM , 0 , UINT32_MAX , SUBTYPE_MASK },
198 { ArchSpec::eCore_thumbv4t , llvm::MachO::CPU_TYPE_ARM , 5 , UINT32_MAX , SUBTYPE_MASK },
199 { ArchSpec::eCore_thumbv5 , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
200 { ArchSpec::eCore_thumbv5e , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
201 { ArchSpec::eCore_thumbv6 , llvm::MachO::CPU_TYPE_ARM , 6 , UINT32_MAX , SUBTYPE_MASK },
Jason Molenda64a11732013-10-08 03:01:08 +0000202 { ArchSpec::eCore_thumbv6m , llvm::MachO::CPU_TYPE_ARM , 14 , UINT32_MAX , SUBTYPE_MASK },
Charles Davis510938e2013-08-27 05:04:57 +0000203 { ArchSpec::eCore_thumbv7 , llvm::MachO::CPU_TYPE_ARM , 9 , UINT32_MAX , SUBTYPE_MASK },
204 { ArchSpec::eCore_thumbv7f , llvm::MachO::CPU_TYPE_ARM , 10 , UINT32_MAX , SUBTYPE_MASK },
205 { ArchSpec::eCore_thumbv7s , llvm::MachO::CPU_TYPE_ARM , 11 , UINT32_MAX , SUBTYPE_MASK },
206 { ArchSpec::eCore_thumbv7k , llvm::MachO::CPU_TYPE_ARM , 12 , UINT32_MAX , SUBTYPE_MASK },
207 { ArchSpec::eCore_thumbv7m , llvm::MachO::CPU_TYPE_ARM , 15 , UINT32_MAX , SUBTYPE_MASK },
208 { ArchSpec::eCore_thumbv7em , llvm::MachO::CPU_TYPE_ARM , 16 , UINT32_MAX , SUBTYPE_MASK },
209 { ArchSpec::eCore_ppc_generic , llvm::MachO::CPU_TYPE_POWERPC , CPU_ANY, UINT32_MAX , UINT32_MAX },
210 { ArchSpec::eCore_ppc_generic , llvm::MachO::CPU_TYPE_POWERPC , 0 , UINT32_MAX , SUBTYPE_MASK },
211 { ArchSpec::eCore_ppc_ppc601 , llvm::MachO::CPU_TYPE_POWERPC , 1 , UINT32_MAX , SUBTYPE_MASK },
212 { ArchSpec::eCore_ppc_ppc602 , llvm::MachO::CPU_TYPE_POWERPC , 2 , UINT32_MAX , SUBTYPE_MASK },
213 { ArchSpec::eCore_ppc_ppc603 , llvm::MachO::CPU_TYPE_POWERPC , 3 , UINT32_MAX , SUBTYPE_MASK },
214 { ArchSpec::eCore_ppc_ppc603e , llvm::MachO::CPU_TYPE_POWERPC , 4 , UINT32_MAX , SUBTYPE_MASK },
215 { ArchSpec::eCore_ppc_ppc603ev , llvm::MachO::CPU_TYPE_POWERPC , 5 , UINT32_MAX , SUBTYPE_MASK },
216 { ArchSpec::eCore_ppc_ppc604 , llvm::MachO::CPU_TYPE_POWERPC , 6 , UINT32_MAX , SUBTYPE_MASK },
217 { ArchSpec::eCore_ppc_ppc604e , llvm::MachO::CPU_TYPE_POWERPC , 7 , UINT32_MAX , SUBTYPE_MASK },
218 { ArchSpec::eCore_ppc_ppc620 , llvm::MachO::CPU_TYPE_POWERPC , 8 , UINT32_MAX , SUBTYPE_MASK },
219 { ArchSpec::eCore_ppc_ppc750 , llvm::MachO::CPU_TYPE_POWERPC , 9 , UINT32_MAX , SUBTYPE_MASK },
220 { ArchSpec::eCore_ppc_ppc7400 , llvm::MachO::CPU_TYPE_POWERPC , 10 , UINT32_MAX , SUBTYPE_MASK },
221 { ArchSpec::eCore_ppc_ppc7450 , llvm::MachO::CPU_TYPE_POWERPC , 11 , UINT32_MAX , SUBTYPE_MASK },
222 { ArchSpec::eCore_ppc_ppc970 , llvm::MachO::CPU_TYPE_POWERPC , 100 , UINT32_MAX , SUBTYPE_MASK },
223 { ArchSpec::eCore_ppc64_generic , llvm::MachO::CPU_TYPE_POWERPC64 , 0 , UINT32_MAX , SUBTYPE_MASK },
224 { ArchSpec::eCore_ppc64_ppc970_64 , llvm::MachO::CPU_TYPE_POWERPC64 , 100 , UINT32_MAX , SUBTYPE_MASK },
225 { ArchSpec::eCore_x86_32_i386 , llvm::MachO::CPU_TYPE_I386 , 3 , UINT32_MAX , SUBTYPE_MASK },
226 { ArchSpec::eCore_x86_32_i486 , llvm::MachO::CPU_TYPE_I386 , 4 , UINT32_MAX , SUBTYPE_MASK },
227 { ArchSpec::eCore_x86_32_i486sx , llvm::MachO::CPU_TYPE_I386 , 0x84 , UINT32_MAX , SUBTYPE_MASK },
Greg Claytona86dc432014-01-22 23:42:03 +0000228 { ArchSpec::eCore_x86_32_i386 , llvm::MachO::CPU_TYPE_I386 , CPU_ANY, UINT32_MAX , UINT32_MAX },
Charles Davis510938e2013-08-27 05:04:57 +0000229 { ArchSpec::eCore_x86_64_x86_64 , llvm::MachO::CPU_TYPE_X86_64 , 3 , UINT32_MAX , SUBTYPE_MASK },
230 { ArchSpec::eCore_x86_64_x86_64 , llvm::MachO::CPU_TYPE_X86_64 , 4 , UINT32_MAX , SUBTYPE_MASK },
Greg Claytona86dc432014-01-22 23:42:03 +0000231 { ArchSpec::eCore_x86_64_x86_64h , llvm::MachO::CPU_TYPE_X86_64 , 8 , UINT32_MAX , SUBTYPE_MASK },
232 { 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 +0000233 // Catch any unknown mach architectures so we can always use the object and symbol mach-o files
Charles Davis510938e2013-08-27 05:04:57 +0000234 { ArchSpec::eCore_uknownMach32 , 0 , 0 , 0xFF000000u, 0x00000000u },
235 { ArchSpec::eCore_uknownMach64 , llvm::MachO::CPU_ARCH_ABI64 , 0 , 0xFF000000u, 0x00000000u }
Greg Clayton64195a22011-02-23 00:35:02 +0000236};
237static const ArchDefinition g_macho_arch_def = {
238 eArchTypeMachO,
Saleem Abdulrasool28606952014-06-27 05:17:41 +0000239 llvm::array_lengthof(g_macho_arch_entries),
Greg Clayton64195a22011-02-23 00:35:02 +0000240 g_macho_arch_entries,
Greg Clayton64195a22011-02-23 00:35:02 +0000241 "mach-o"
Greg Clayton41f92322010-06-11 03:25:34 +0000242};
243
Greg Clayton64195a22011-02-23 00:35:02 +0000244//===----------------------------------------------------------------------===//
245// A table that gets searched linearly for matches. This table is used to
246// convert cpu type and subtypes to architecture names, and to convert
247// architecture names to cpu types and subtypes. The ordering is important and
248// allows the precedence to be set when the table is built.
249static const ArchDefinitionEntry g_elf_arch_entries[] =
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000250{
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000251 { ArchSpec::eCore_sparc_generic , llvm::ELF::EM_SPARC , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // Sparc
252 { ArchSpec::eCore_x86_32_i386 , llvm::ELF::EM_386 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // Intel 80386
253 { ArchSpec::eCore_x86_32_i486 , llvm::ELF::EM_486 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // Intel 486 (deprecated)
254 { ArchSpec::eCore_ppc_generic , llvm::ELF::EM_PPC , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // PowerPC
255 { ArchSpec::eCore_ppc64_generic , llvm::ELF::EM_PPC64 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // PowerPC64
256 { ArchSpec::eCore_arm_generic , llvm::ELF::EM_ARM , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // ARM
257 { ArchSpec::eCore_sparc9_generic , llvm::ELF::EM_SPARCV9, LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // SPARC V9
Ed Masteb73f8442013-10-10 00:59:47 +0000258 { ArchSpec::eCore_x86_64_x86_64 , llvm::ELF::EM_X86_64 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // AMD64
Deepak Panickal6d3df422014-02-19 11:16:46 +0000259 { ArchSpec::eCore_mips64 , llvm::ELF::EM_MIPS , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // MIPS
Todd Fiala14bbef52014-07-01 23:33:32 +0000260 { ArchSpec::eCore_hexagon_generic , llvm::ELF::EM_HEXAGON, LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // HEXAGON
261 { ArchSpec::eCore_kalimba , llvm::ELF::EM_CSR_KALIMBA, LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu } // KALIMBA
262
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000263};
264
Greg Clayton64195a22011-02-23 00:35:02 +0000265static const ArchDefinition g_elf_arch_def = {
266 eArchTypeELF,
Saleem Abdulrasool28606952014-06-27 05:17:41 +0000267 llvm::array_lengthof(g_elf_arch_entries),
Greg Clayton64195a22011-02-23 00:35:02 +0000268 g_elf_arch_entries,
Greg Clayton64195a22011-02-23 00:35:02 +0000269 "elf",
Greg Clayton41f92322010-06-11 03:25:34 +0000270};
271
Charles Davis237ad972013-08-27 05:04:33 +0000272static const ArchDefinitionEntry g_coff_arch_entries[] =
273{
Zachary Turnerad587ae42014-07-28 16:44:49 +0000274 { ArchSpec::eCore_x86_32_i386 , llvm::COFF::IMAGE_FILE_MACHINE_I386 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // Intel 80x86
Charles Davis237ad972013-08-27 05:04:33 +0000275 { ArchSpec::eCore_ppc_generic , llvm::COFF::IMAGE_FILE_MACHINE_POWERPC , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // PowerPC
276 { ArchSpec::eCore_ppc_generic , llvm::COFF::IMAGE_FILE_MACHINE_POWERPCFP, LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // PowerPC (with FPU)
277 { ArchSpec::eCore_arm_generic , llvm::COFF::IMAGE_FILE_MACHINE_ARM , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // ARM
Saleem Abdulrasool1108cb32014-03-11 03:09:08 +0000278 { ArchSpec::eCore_arm_armv7 , llvm::COFF::IMAGE_FILE_MACHINE_ARMNT , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // ARMv7
Charles Davis237ad972013-08-27 05:04:33 +0000279 { ArchSpec::eCore_thumb , llvm::COFF::IMAGE_FILE_MACHINE_THUMB , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // ARMv7
280 { ArchSpec::eCore_x86_64_x86_64, llvm::COFF::IMAGE_FILE_MACHINE_AMD64 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu } // AMD64
281};
282
283static const ArchDefinition g_coff_arch_def = {
284 eArchTypeCOFF,
Saleem Abdulrasool28606952014-06-27 05:17:41 +0000285 llvm::array_lengthof(g_coff_arch_entries),
Charles Davis237ad972013-08-27 05:04:33 +0000286 g_coff_arch_entries,
287 "pe-coff",
288};
289
Greg Clayton64195a22011-02-23 00:35:02 +0000290//===----------------------------------------------------------------------===//
291// Table of all ArchDefinitions
292static const ArchDefinition *g_arch_definitions[] = {
293 &g_macho_arch_def,
Charles Davis237ad972013-08-27 05:04:33 +0000294 &g_elf_arch_def,
295 &g_coff_arch_def
Greg Clayton64195a22011-02-23 00:35:02 +0000296};
Greg Clayton41f92322010-06-11 03:25:34 +0000297
Saleem Abdulrasool28606952014-06-27 05:17:41 +0000298static const size_t k_num_arch_definitions = llvm::array_lengthof(g_arch_definitions);
Greg Clayton64195a22011-02-23 00:35:02 +0000299
300//===----------------------------------------------------------------------===//
301// Static helper functions.
302
303
304// Get the architecture definition for a given object type.
305static const ArchDefinition *
306FindArchDefinition (ArchitectureType arch_type)
307{
308 for (unsigned int i = 0; i < k_num_arch_definitions; ++i)
309 {
310 const ArchDefinition *def = g_arch_definitions[i];
311 if (def->type == arch_type)
312 return def;
313 }
314 return NULL;
315}
316
317// Get an architecture definition by name.
318static const CoreDefinition *
319FindCoreDefinition (llvm::StringRef name)
320{
Greg Clayton56b79682014-07-23 18:12:06 +0000321 for (unsigned int i = 0; i < llvm::array_lengthof(g_core_definitions); ++i)
Greg Clayton64195a22011-02-23 00:35:02 +0000322 {
323 if (name.equals_lower(g_core_definitions[i].name))
324 return &g_core_definitions[i];
325 }
326 return NULL;
327}
328
329static inline const CoreDefinition *
330FindCoreDefinition (ArchSpec::Core core)
331{
Greg Clayton56b79682014-07-23 18:12:06 +0000332 if (core >= 0 && core < llvm::array_lengthof(g_core_definitions))
Greg Clayton64195a22011-02-23 00:35:02 +0000333 return &g_core_definitions[core];
334 return NULL;
335}
336
337// Get a definition entry by cpu type and subtype.
338static const ArchDefinitionEntry *
339FindArchDefinitionEntry (const ArchDefinition *def, uint32_t cpu, uint32_t sub)
340{
341 if (def == NULL)
342 return NULL;
343
Greg Clayton64195a22011-02-23 00:35:02 +0000344 const ArchDefinitionEntry *entries = def->entries;
345 for (size_t i = 0; i < def->num_entries; ++i)
346 {
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000347 if (entries[i].cpu == (cpu & entries[i].cpu_mask))
348 if (entries[i].sub == (sub & entries[i].sub_mask))
349 return &entries[i];
Greg Clayton64195a22011-02-23 00:35:02 +0000350 }
351 return NULL;
352}
353
354static const ArchDefinitionEntry *
355FindArchDefinitionEntry (const ArchDefinition *def, ArchSpec::Core core)
356{
357 if (def == NULL)
358 return NULL;
359
360 const ArchDefinitionEntry *entries = def->entries;
361 for (size_t i = 0; i < def->num_entries; ++i)
362 {
363 if (entries[i].core == core)
364 return &entries[i];
365 }
366 return NULL;
367}
368
369//===----------------------------------------------------------------------===//
370// Constructors and destructors.
371
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000372ArchSpec::ArchSpec() :
Greg Clayton514487e2011-02-15 21:59:32 +0000373 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000374 m_core (kCore_invalid),
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000375 m_byte_order (eByteOrderInvalid),
376 m_distribution_id ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000377{
378}
379
Greg Claytoneb0103f2011-04-07 22:46:35 +0000380ArchSpec::ArchSpec (const char *triple_cstr, Platform *platform) :
Greg Clayton514487e2011-02-15 21:59:32 +0000381 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000382 m_core (kCore_invalid),
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000383 m_byte_order (eByteOrderInvalid),
384 m_distribution_id ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000385{
Greg Clayton64195a22011-02-23 00:35:02 +0000386 if (triple_cstr)
Greg Claytoneb0103f2011-04-07 22:46:35 +0000387 SetTriple(triple_cstr, platform);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000388}
389
Greg Clayton70512312012-05-08 01:45:38 +0000390
391ArchSpec::ArchSpec (const char *triple_cstr) :
392 m_triple (),
393 m_core (kCore_invalid),
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000394 m_byte_order (eByteOrderInvalid),
395 m_distribution_id ()
Greg Clayton70512312012-05-08 01:45:38 +0000396{
397 if (triple_cstr)
398 SetTriple(triple_cstr);
399}
400
Greg Clayton64195a22011-02-23 00:35:02 +0000401ArchSpec::ArchSpec(const llvm::Triple &triple) :
Greg Clayton514487e2011-02-15 21:59:32 +0000402 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000403 m_core (kCore_invalid),
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000404 m_byte_order (eByteOrderInvalid),
405 m_distribution_id ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000406{
Greg Clayton64195a22011-02-23 00:35:02 +0000407 SetTriple(triple);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000408}
409
Greg Claytone0d378b2011-03-24 21:19:54 +0000410ArchSpec::ArchSpec (ArchitectureType arch_type, uint32_t cpu, uint32_t subtype) :
Greg Clayton64195a22011-02-23 00:35:02 +0000411 m_triple (),
412 m_core (kCore_invalid),
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000413 m_byte_order (eByteOrderInvalid),
414 m_distribution_id ()
Greg Clayton64195a22011-02-23 00:35:02 +0000415{
416 SetArchitecture (arch_type, cpu, subtype);
417}
418
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000419ArchSpec::~ArchSpec()
420{
421}
422
Greg Clayton64195a22011-02-23 00:35:02 +0000423//===----------------------------------------------------------------------===//
424// Assignment and initialization.
425
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000426const ArchSpec&
427ArchSpec::operator= (const ArchSpec& rhs)
428{
429 if (this != &rhs)
430 {
Greg Clayton514487e2011-02-15 21:59:32 +0000431 m_triple = rhs.m_triple;
Greg Clayton64195a22011-02-23 00:35:02 +0000432 m_core = rhs.m_core;
Greg Clayton514487e2011-02-15 21:59:32 +0000433 m_byte_order = rhs.m_byte_order;
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000434 m_distribution_id = rhs.m_distribution_id;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000435 }
436 return *this;
437}
438
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000439void
440ArchSpec::Clear()
441{
Greg Clayton514487e2011-02-15 21:59:32 +0000442 m_triple = llvm::Triple();
Greg Clayton64195a22011-02-23 00:35:02 +0000443 m_core = kCore_invalid;
444 m_byte_order = eByteOrderInvalid;
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000445 m_distribution_id.Clear ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000446}
447
Greg Clayton64195a22011-02-23 00:35:02 +0000448//===----------------------------------------------------------------------===//
449// Predicates.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000450
Greg Clayton41f92322010-06-11 03:25:34 +0000451
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000452const char *
Greg Clayton64195a22011-02-23 00:35:02 +0000453ArchSpec::GetArchitectureName () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000454{
Greg Clayton64195a22011-02-23 00:35:02 +0000455 const CoreDefinition *core_def = FindCoreDefinition (m_core);
456 if (core_def)
457 return core_def->name;
458 return "unknown";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000459}
460
Greg Clayton64195a22011-02-23 00:35:02 +0000461uint32_t
462ArchSpec::GetMachOCPUType () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000463{
Greg Clayton64195a22011-02-23 00:35:02 +0000464 const CoreDefinition *core_def = FindCoreDefinition (m_core);
465 if (core_def)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000466 {
Greg Clayton64195a22011-02-23 00:35:02 +0000467 const ArchDefinitionEntry *arch_def = FindArchDefinitionEntry (&g_macho_arch_def, core_def->core);
468 if (arch_def)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000469 {
Greg Clayton64195a22011-02-23 00:35:02 +0000470 return arch_def->cpu;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000471 }
472 }
Greg Clayton64195a22011-02-23 00:35:02 +0000473 return LLDB_INVALID_CPUTYPE;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000474}
475
Greg Clayton64195a22011-02-23 00:35:02 +0000476uint32_t
477ArchSpec::GetMachOCPUSubType () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000478{
Greg Clayton64195a22011-02-23 00:35:02 +0000479 const CoreDefinition *core_def = FindCoreDefinition (m_core);
480 if (core_def)
481 {
482 const ArchDefinitionEntry *arch_def = FindArchDefinitionEntry (&g_macho_arch_def, core_def->core);
483 if (arch_def)
484 {
Greg Clayton1cb64962011-03-24 04:28:38 +0000485 return arch_def->sub;
Greg Clayton64195a22011-02-23 00:35:02 +0000486 }
487 }
488 return LLDB_INVALID_CPUTYPE;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000489}
490
Greg Clayton64195a22011-02-23 00:35:02 +0000491llvm::Triple::ArchType
492ArchSpec::GetMachine () const
493{
494 const CoreDefinition *core_def = FindCoreDefinition (m_core);
495 if (core_def)
496 return core_def->machine;
497
498 return llvm::Triple::UnknownArch;
499}
500
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000501const ConstString&
502ArchSpec::GetDistributionId () const
503{
504 return m_distribution_id;
505}
506
507void
508ArchSpec::SetDistributionId (const char* distribution_id)
509{
510 m_distribution_id.SetCString (distribution_id);
511}
512
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000513uint32_t
514ArchSpec::GetAddressByteSize() const
515{
Greg Clayton64195a22011-02-23 00:35:02 +0000516 const CoreDefinition *core_def = FindCoreDefinition (m_core);
517 if (core_def)
518 return core_def->addr_byte_size;
Greg Clayton41f92322010-06-11 03:25:34 +0000519 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000520}
521
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000522ByteOrder
523ArchSpec::GetDefaultEndian () const
524{
Greg Clayton64195a22011-02-23 00:35:02 +0000525 const CoreDefinition *core_def = FindCoreDefinition (m_core);
526 if (core_def)
527 return core_def->default_byte_order;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000528 return eByteOrderInvalid;
529}
530
Greg Clayton64195a22011-02-23 00:35:02 +0000531lldb::ByteOrder
532ArchSpec::GetByteOrder () const
533{
534 if (m_byte_order == eByteOrderInvalid)
535 return GetDefaultEndian();
536 return m_byte_order;
537}
538
539//===----------------------------------------------------------------------===//
540// Mutators.
541
542bool
543ArchSpec::SetTriple (const llvm::Triple &triple)
544{
545 m_triple = triple;
546
547 llvm::StringRef arch_name (m_triple.getArchName());
548 const CoreDefinition *core_def = FindCoreDefinition (arch_name);
549 if (core_def)
550 {
551 m_core = core_def->core;
Greg Claytoneb0103f2011-04-07 22:46:35 +0000552 // Set the byte order to the default byte order for an architecture.
553 // This can be modified if needed for cases when cores handle both
554 // big and little endian
555 m_byte_order = core_def->default_byte_order;
Greg Clayton64195a22011-02-23 00:35:02 +0000556 }
557 else
558 {
559 Clear();
560 }
561
562
563 return IsValid();
564}
565
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000566static bool
567ParseMachCPUDashSubtypeTriple (const char *triple_cstr, ArchSpec &arch)
568{
569 // Accept "12-10" or "12.10" as cpu type/subtype
570 if (isdigit(triple_cstr[0]))
571 {
572 char *end = NULL;
573 errno = 0;
Greg Claytonc7bece562013-01-25 18:06:21 +0000574 uint32_t cpu = (uint32_t)::strtoul (triple_cstr, &end, 0);
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000575 if (errno == 0 && cpu != 0 && end && ((*end == '-') || (*end == '.')))
576 {
577 errno = 0;
Greg Claytonc7bece562013-01-25 18:06:21 +0000578 uint32_t sub = (uint32_t)::strtoul (end + 1, &end, 0);
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000579 if (errno == 0 && end && ((*end == '-') || (*end == '.') || (*end == '\0')))
580 {
581 if (arch.SetArchitecture (eArchTypeMachO, cpu, sub))
582 {
583 if (*end == '-')
584 {
585 llvm::StringRef vendor_os (end + 1);
586 size_t dash_pos = vendor_os.find('-');
587 if (dash_pos != llvm::StringRef::npos)
588 {
589 llvm::StringRef vendor_str(vendor_os.substr(0, dash_pos));
590 arch.GetTriple().setVendorName(vendor_str);
591 const size_t vendor_start_pos = dash_pos+1;
Greg Claytonc7bece562013-01-25 18:06:21 +0000592 dash_pos = vendor_os.find('-', vendor_start_pos);
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000593 if (dash_pos == llvm::StringRef::npos)
594 {
595 if (vendor_start_pos < vendor_os.size())
596 arch.GetTriple().setOSName(vendor_os.substr(vendor_start_pos));
597 }
598 else
599 {
600 arch.GetTriple().setOSName(vendor_os.substr(vendor_start_pos, dash_pos - vendor_start_pos));
601 }
602 }
603 }
604 return true;
605 }
606 }
607 }
608 }
609 return false;
610}
Greg Clayton64195a22011-02-23 00:35:02 +0000611bool
Greg Clayton70512312012-05-08 01:45:38 +0000612ArchSpec::SetTriple (const char *triple_cstr)
Greg Clayton64195a22011-02-23 00:35:02 +0000613{
Greg Clayton23aca092011-08-12 23:32:52 +0000614 if (triple_cstr && triple_cstr[0])
Greg Clayton64195a22011-02-23 00:35:02 +0000615 {
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000616 if (ParseMachCPUDashSubtypeTriple (triple_cstr, *this))
617 return true;
618
Greg Clayton64195a22011-02-23 00:35:02 +0000619 llvm::StringRef triple_stref (triple_cstr);
620 if (triple_stref.startswith (LLDB_ARCH_DEFAULT))
621 {
622 // Special case for the current host default architectures...
623 if (triple_stref.equals (LLDB_ARCH_DEFAULT_32BIT))
Zachary Turner13b18262014-08-20 16:42:51 +0000624 *this = HostInfo::GetArchitecture(HostInfo::eArchKind32);
Greg Clayton64195a22011-02-23 00:35:02 +0000625 else if (triple_stref.equals (LLDB_ARCH_DEFAULT_64BIT))
Zachary Turner13b18262014-08-20 16:42:51 +0000626 *this = HostInfo::GetArchitecture(HostInfo::eArchKind64);
Greg Clayton64195a22011-02-23 00:35:02 +0000627 else if (triple_stref.equals (LLDB_ARCH_DEFAULT))
Zachary Turner13b18262014-08-20 16:42:51 +0000628 *this = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
Greg Clayton64195a22011-02-23 00:35:02 +0000629 }
630 else
631 {
632 std::string normalized_triple_sstr (llvm::Triple::normalize(triple_stref));
633 triple_stref = normalized_triple_sstr;
Greg Clayton70512312012-05-08 01:45:38 +0000634 SetTriple (llvm::Triple (triple_stref));
635 }
636 }
637 else
638 Clear();
639 return IsValid();
640}
641
642bool
643ArchSpec::SetTriple (const char *triple_cstr, Platform *platform)
644{
645 if (triple_cstr && triple_cstr[0])
646 {
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000647 if (ParseMachCPUDashSubtypeTriple (triple_cstr, *this))
648 return true;
649
Greg Clayton70512312012-05-08 01:45:38 +0000650 llvm::StringRef triple_stref (triple_cstr);
651 if (triple_stref.startswith (LLDB_ARCH_DEFAULT))
652 {
653 // Special case for the current host default architectures...
654 if (triple_stref.equals (LLDB_ARCH_DEFAULT_32BIT))
Zachary Turner13b18262014-08-20 16:42:51 +0000655 *this = HostInfo::GetArchitecture(HostInfo::eArchKind32);
Greg Clayton70512312012-05-08 01:45:38 +0000656 else if (triple_stref.equals (LLDB_ARCH_DEFAULT_64BIT))
Zachary Turner13b18262014-08-20 16:42:51 +0000657 *this = HostInfo::GetArchitecture(HostInfo::eArchKind64);
Greg Clayton70512312012-05-08 01:45:38 +0000658 else if (triple_stref.equals (LLDB_ARCH_DEFAULT))
Zachary Turner13b18262014-08-20 16:42:51 +0000659 *this = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
Greg Clayton70512312012-05-08 01:45:38 +0000660 }
661 else
662 {
663 ArchSpec raw_arch (triple_cstr);
664
665 std::string normalized_triple_sstr (llvm::Triple::normalize(triple_stref));
666 triple_stref = normalized_triple_sstr;
Greg Claytoneb0103f2011-04-07 22:46:35 +0000667 llvm::Triple normalized_triple (triple_stref);
668
669 const bool os_specified = normalized_triple.getOSName().size() > 0;
670 const bool vendor_specified = normalized_triple.getVendorName().size() > 0;
671 const bool env_specified = normalized_triple.getEnvironmentName().size() > 0;
672
673 // If we got an arch only, then default the vendor, os, environment
674 // to match the platform if one is supplied
675 if (!(os_specified || vendor_specified || env_specified))
676 {
677 if (platform)
678 {
679 // If we were given a platform, use the platform's system
680 // architecture. If this is not available (might not be
681 // connected) use the first supported architecture.
Greg Clayton70512312012-05-08 01:45:38 +0000682 ArchSpec compatible_arch;
Greg Clayton1e0c8842013-01-11 20:49:54 +0000683 if (platform->IsCompatibleArchitecture (raw_arch, false, &compatible_arch))
Greg Claytoneb0103f2011-04-07 22:46:35 +0000684 {
Greg Clayton70512312012-05-08 01:45:38 +0000685 if (compatible_arch.IsValid())
686 {
687 const llvm::Triple &compatible_triple = compatible_arch.GetTriple();
688 if (!vendor_specified)
689 normalized_triple.setVendor(compatible_triple.getVendor());
690 if (!os_specified)
691 normalized_triple.setOS(compatible_triple.getOS());
692 if (!env_specified && compatible_triple.getEnvironmentName().size())
693 normalized_triple.setEnvironment(compatible_triple.getEnvironment());
694 }
Greg Claytoneb0103f2011-04-07 22:46:35 +0000695 }
Greg Clayton70512312012-05-08 01:45:38 +0000696 else
Greg Claytoneb0103f2011-04-07 22:46:35 +0000697 {
Greg Clayton70512312012-05-08 01:45:38 +0000698 *this = raw_arch;
699 return IsValid();
Greg Claytoneb0103f2011-04-07 22:46:35 +0000700 }
701 }
702 else
703 {
704 // No platform specified, fall back to the host system for
705 // the default vendor, os, and environment.
Sean Callananbfb237bc2011-11-04 22:46:46 +0000706 llvm::Triple host_triple(llvm::sys::getDefaultTargetTriple());
Greg Clayton70512312012-05-08 01:45:38 +0000707 if (!vendor_specified)
708 normalized_triple.setVendor(host_triple.getVendor());
709 if (!vendor_specified)
710 normalized_triple.setOS(host_triple.getOS());
711 if (!env_specified && host_triple.getEnvironmentName().size())
712 normalized_triple.setEnvironment(host_triple.getEnvironment());
Greg Claytoneb0103f2011-04-07 22:46:35 +0000713 }
714 }
715 SetTriple (normalized_triple);
Greg Clayton64195a22011-02-23 00:35:02 +0000716 }
717 }
718 else
719 Clear();
720 return IsValid();
721}
722
Greg Clayton64195a22011-02-23 00:35:02 +0000723bool
Greg Claytone0d378b2011-03-24 21:19:54 +0000724ArchSpec::SetArchitecture (ArchitectureType arch_type, uint32_t cpu, uint32_t sub)
Greg Clayton64195a22011-02-23 00:35:02 +0000725{
726 m_core = kCore_invalid;
727 bool update_triple = true;
728 const ArchDefinition *arch_def = FindArchDefinition(arch_type);
729 if (arch_def)
730 {
731 const ArchDefinitionEntry *arch_def_entry = FindArchDefinitionEntry (arch_def, cpu, sub);
732 if (arch_def_entry)
733 {
734 const CoreDefinition *core_def = FindCoreDefinition (arch_def_entry->core);
735 if (core_def)
736 {
737 m_core = core_def->core;
738 update_triple = false;
Greg Clayton593577a2011-09-21 03:57:31 +0000739 // Always use the architecture name because it might be more descriptive
740 // than the architecture enum ("armv7" -> llvm::Triple::arm).
741 m_triple.setArchName(llvm::StringRef(core_def->name));
Greg Clayton64195a22011-02-23 00:35:02 +0000742 if (arch_type == eArchTypeMachO)
743 {
744 m_triple.setVendor (llvm::Triple::Apple);
Greg Clayton70512312012-05-08 01:45:38 +0000745
746 switch (core_def->machine)
747 {
Todd Fialad8eaa172014-07-23 14:37:35 +0000748 case llvm::Triple::aarch64:
Greg Clayton70512312012-05-08 01:45:38 +0000749 case llvm::Triple::arm:
750 case llvm::Triple::thumb:
751 m_triple.setOS (llvm::Triple::IOS);
752 break;
753
754 case llvm::Triple::x86:
755 case llvm::Triple::x86_64:
Greg Claytona3a6c122014-07-29 18:04:57 +0000756 // Don't set the OS for x86_64 or for x86 as we want to leave it as an "unspecified unknown"
757 // which means if we ask for the OS from the llvm::Triple we get back llvm::Triple::UnknownOS, but
758 // if we ask for the string value for the OS it will come back empty (unspecified).
759 // We do this because we now have iOS and MacOSX as the OS values for x86 and x86_64 for
760 // normal desktop and simulator binaries. And if we compare a "x86_64-apple-ios" to a "x86_64-apple-"
761 // triple, it will say it is compatible (because the OS is unspecified in the second one and will match
762 // anything in the first
763 break;
764
Greg Clayton70512312012-05-08 01:45:38 +0000765 default:
766 m_triple.setOS (llvm::Triple::MacOSX);
767 break;
768 }
Greg Clayton64195a22011-02-23 00:35:02 +0000769 }
770 else
771 {
772 m_triple.setVendor (llvm::Triple::UnknownVendor);
773 m_triple.setOS (llvm::Triple::UnknownOS);
774 }
Greg Clayton593577a2011-09-21 03:57:31 +0000775 // Fall back onto setting the machine type if the arch by name failed...
776 if (m_triple.getArch () == llvm::Triple::UnknownArch)
777 m_triple.setArch (core_def->machine);
Greg Clayton64195a22011-02-23 00:35:02 +0000778 }
779 }
780 }
781 CoreUpdated(update_triple);
782 return IsValid();
783}
784
Greg Clayton357132e2011-03-26 19:14:58 +0000785uint32_t
786ArchSpec::GetMinimumOpcodeByteSize() const
Greg Clayton64195a22011-02-23 00:35:02 +0000787{
Greg Clayton357132e2011-03-26 19:14:58 +0000788 const CoreDefinition *core_def = FindCoreDefinition (m_core);
789 if (core_def)
790 return core_def->min_opcode_byte_size;
791 return 0;
792}
793
794uint32_t
795ArchSpec::GetMaximumOpcodeByteSize() const
796{
797 const CoreDefinition *core_def = FindCoreDefinition (m_core);
798 if (core_def)
799 return core_def->max_opcode_byte_size;
800 return 0;
Greg Clayton64195a22011-02-23 00:35:02 +0000801}
802
Jason Molendaba813dc2012-11-04 03:20:05 +0000803bool
804ArchSpec::IsExactMatch (const ArchSpec& rhs) const
805{
Sean Callananbf4b7be2012-12-13 22:07:14 +0000806 return IsEqualTo (rhs, true);
Jason Molendaba813dc2012-11-04 03:20:05 +0000807}
808
809bool
810ArchSpec::IsCompatibleMatch (const ArchSpec& rhs) const
811{
Sean Callananbf4b7be2012-12-13 22:07:14 +0000812 return IsEqualTo (rhs, false);
Jason Molendaba813dc2012-11-04 03:20:05 +0000813}
814
815bool
Sean Callananbf4b7be2012-12-13 22:07:14 +0000816ArchSpec::IsEqualTo (const ArchSpec& rhs, bool exact_match) const
Jason Molendaba813dc2012-11-04 03:20:05 +0000817{
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000818 // explicitly ignoring m_distribution_id in this method.
819
Jason Molendaba813dc2012-11-04 03:20:05 +0000820 if (GetByteOrder() != rhs.GetByteOrder())
821 return false;
822
823 const ArchSpec::Core lhs_core = GetCore ();
824 const ArchSpec::Core rhs_core = rhs.GetCore ();
825
826 const bool core_match = cores_match (lhs_core, rhs_core, true, exact_match);
827
828 if (core_match)
829 {
830 const llvm::Triple &lhs_triple = GetTriple();
831 const llvm::Triple &rhs_triple = rhs.GetTriple();
832
833 const llvm::Triple::VendorType lhs_triple_vendor = lhs_triple.getVendor();
834 const llvm::Triple::VendorType rhs_triple_vendor = rhs_triple.getVendor();
835 if (lhs_triple_vendor != rhs_triple_vendor)
836 {
Sean Callananbf4b7be2012-12-13 22:07:14 +0000837 if (exact_match)
838 {
839 const bool rhs_vendor_specified = rhs.TripleVendorWasSpecified();
840 const bool lhs_vendor_specified = TripleVendorWasSpecified();
841 // Both architectures had the vendor specified, so if they aren't
842 // equal then we return false
843 if (rhs_vendor_specified && lhs_vendor_specified)
844 return false;
845 }
Jason Molendaba813dc2012-11-04 03:20:05 +0000846
847 // Only fail if both vendor types are not unknown
848 if (lhs_triple_vendor != llvm::Triple::UnknownVendor &&
849 rhs_triple_vendor != llvm::Triple::UnknownVendor)
850 return false;
851 }
852
853 const llvm::Triple::OSType lhs_triple_os = lhs_triple.getOS();
854 const llvm::Triple::OSType rhs_triple_os = rhs_triple.getOS();
855 if (lhs_triple_os != rhs_triple_os)
856 {
Sean Callananbf4b7be2012-12-13 22:07:14 +0000857 if (exact_match)
858 {
859 const bool rhs_os_specified = rhs.TripleOSWasSpecified();
860 const bool lhs_os_specified = TripleOSWasSpecified();
861 // Both architectures had the OS specified, so if they aren't
862 // equal then we return false
863 if (rhs_os_specified && lhs_os_specified)
864 return false;
865 }
Greg Clayton7ab7f892014-05-29 21:33:45 +0000866
Greg Clayton3f19ada2014-07-10 23:33:37 +0000867 // Only fail if both os types are not unknown
868 if (lhs_triple_os != llvm::Triple::UnknownOS &&
869 rhs_triple_os != llvm::Triple::UnknownOS)
870 return false;
Jason Molendaba813dc2012-11-04 03:20:05 +0000871 }
872
873 const llvm::Triple::EnvironmentType lhs_triple_env = lhs_triple.getEnvironment();
874 const llvm::Triple::EnvironmentType rhs_triple_env = rhs_triple.getEnvironment();
875
876 if (lhs_triple_env != rhs_triple_env)
877 {
878 // Only fail if both environment types are not unknown
879 if (lhs_triple_env != llvm::Triple::UnknownEnvironment &&
880 rhs_triple_env != llvm::Triple::UnknownEnvironment)
881 return false;
882 }
883 return true;
884 }
885 return false;
886}
887
Greg Clayton64195a22011-02-23 00:35:02 +0000888//===----------------------------------------------------------------------===//
889// Helper methods.
890
891void
892ArchSpec::CoreUpdated (bool update_triple)
893{
894 const CoreDefinition *core_def = FindCoreDefinition (m_core);
895 if (core_def)
896 {
897 if (update_triple)
898 m_triple = llvm::Triple(core_def->name, "unknown", "unknown");
899 m_byte_order = core_def->default_byte_order;
900 }
901 else
902 {
903 if (update_triple)
904 m_triple = llvm::Triple();
905 m_byte_order = eByteOrderInvalid;
906 }
907}
908
909//===----------------------------------------------------------------------===//
910// Operators.
911
Greg Clayton70512312012-05-08 01:45:38 +0000912static bool
Jason Molendaba813dc2012-11-04 03:20:05 +0000913cores_match (const ArchSpec::Core core1, const ArchSpec::Core core2, bool try_inverse, bool enforce_exact_match)
Greg Clayton70512312012-05-08 01:45:38 +0000914{
Jason Molendaba813dc2012-11-04 03:20:05 +0000915 if (core1 == core2)
916 return true;
917
Greg Clayton70512312012-05-08 01:45:38 +0000918 switch (core1)
919 {
Greg Clayton70512312012-05-08 01:45:38 +0000920 case ArchSpec::kCore_any:
921 return true;
922
Greg Clayton44362e02014-07-12 00:11:34 +0000923 case ArchSpec::eCore_arm_generic:
924 if (enforce_exact_match)
925 break;
926 // Fall through to case below
Greg Clayton70512312012-05-08 01:45:38 +0000927 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;
Sylvestre Ledru1c9e0642014-08-18 14:53:42 +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;
Zachary Turnerad587ae42014-07-28 16:44:49 +0000940
941 case ArchSpec::kCore_x86_64_any:
942 if ((core2 >= ArchSpec::kCore_x86_64_first && core2 <= ArchSpec::kCore_x86_64_last) || (core2 == ArchSpec::kCore_x86_64_any))
943 return true;
Sylvestre Ledru1c9e0642014-08-18 14:53:42 +0000944 break;
Zachary Turnerad587ae42014-07-28 16:44:49 +0000945
Greg Clayton70512312012-05-08 01:45:38 +0000946 case ArchSpec::kCore_ppc_any:
947 if ((core2 >= ArchSpec::kCore_ppc_first && core2 <= ArchSpec::kCore_ppc_last) || (core2 == ArchSpec::kCore_ppc_any))
948 return true;
949 break;
Sylvestre Ledru1c9e0642014-08-18 14:53:42 +0000950
Greg Clayton70512312012-05-08 01:45:38 +0000951 case ArchSpec::kCore_ppc64_any:
952 if ((core2 >= ArchSpec::kCore_ppc64_first && core2 <= ArchSpec::kCore_ppc64_last) || (core2 == ArchSpec::kCore_ppc64_any))
953 return true;
954 break;
955
Jason Molendaa3a04522013-09-27 23:21:54 +0000956 case ArchSpec::eCore_arm_armv6m:
957 if (!enforce_exact_match)
958 {
Greg Clayton44362e02014-07-12 00:11:34 +0000959 if (core2 == ArchSpec::eCore_arm_generic)
960 return true;
Jason Molendaa3a04522013-09-27 23:21:54 +0000961 try_inverse = false;
Jason Molendac7cda272013-09-27 23:29:10 +0000962 if (core2 == ArchSpec::eCore_arm_armv7)
Jason Molendaa3a04522013-09-27 23:21:54 +0000963 return true;
964 }
Sylvestre Ledru1c9e0642014-08-18 14:53:42 +0000965 break;
Deepak Panickal6d3df422014-02-19 11:16:46 +0000966
967 case ArchSpec::kCore_hexagon_any:
968 if ((core2 >= ArchSpec::kCore_hexagon_first && core2 <= ArchSpec::kCore_hexagon_last) || (core2 == ArchSpec::kCore_hexagon_any))
969 return true;
Jason Molendaa3a04522013-09-27 23:21:54 +0000970 break;
971
Jason Molenda7a1559c2013-03-08 01:20:17 +0000972 case ArchSpec::eCore_arm_armv7m:
973 case ArchSpec::eCore_arm_armv7em:
Johnny Chen1083b0d2012-08-28 22:53:40 +0000974 case ArchSpec::eCore_arm_armv7f:
975 case ArchSpec::eCore_arm_armv7k:
976 case ArchSpec::eCore_arm_armv7s:
Jason Molendaba813dc2012-11-04 03:20:05 +0000977 if (!enforce_exact_match)
978 {
Greg Clayton44362e02014-07-12 00:11:34 +0000979 if (core2 == ArchSpec::eCore_arm_generic)
980 return true;
Jason Molendaba813dc2012-11-04 03:20:05 +0000981 if (core2 == ArchSpec::eCore_arm_armv7)
982 return true;
Greg Clayton44362e02014-07-12 00:11:34 +0000983 try_inverse = false;
Jason Molendaba813dc2012-11-04 03:20:05 +0000984 }
Johnny Chen1083b0d2012-08-28 22:53:40 +0000985 break;
Sylvestre Ledru1c9e0642014-08-18 14:53:42 +0000986
Greg Clayton52edb362014-07-14 22:53:02 +0000987 case ArchSpec::eCore_x86_64_x86_64h:
988 if (!enforce_exact_match)
989 {
990 try_inverse = false;
991 if (core2 == ArchSpec::eCore_x86_64_x86_64)
992 return true;
993 }
994 break;
Johnny Chen1083b0d2012-08-28 22:53:40 +0000995
Greg Clayton70512312012-05-08 01:45:38 +0000996 default:
997 break;
998 }
999 if (try_inverse)
Jason Molendaba813dc2012-11-04 03:20:05 +00001000 return cores_match (core2, core1, false, enforce_exact_match);
Greg Clayton70512312012-05-08 01:45:38 +00001001 return false;
1002}
1003
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001004bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001005lldb_private::operator<(const ArchSpec& lhs, const ArchSpec& rhs)
1006{
Greg Clayton64195a22011-02-23 00:35:02 +00001007 const ArchSpec::Core lhs_core = lhs.GetCore ();
1008 const ArchSpec::Core rhs_core = rhs.GetCore ();
1009 return lhs_core < rhs_core;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001010}