blob: 559780225f1eb67172f497316fbfd93cf863bb17 [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
Matthew Gardiner5f675792014-08-27 12:09:39 +0000119 { eByteOrderLittle, 4, 1, 1 , llvm::Triple::kalimba , ArchSpec::eCore_kalimba , "kalimba" },
120 { eByteOrderBig , 4, 1, 1 , llvm::Triple::kalimba , ArchSpec::eCore_kalimba3 , "kalimba3" },
121 { eByteOrderLittle, 4, 1, 1 , llvm::Triple::kalimba , ArchSpec::eCore_kalimba4 , "kalimba4" },
122 { eByteOrderLittle, 4, 1, 1 , llvm::Triple::kalimba , ArchSpec::eCore_kalimba5 , "kalimba5" }
Greg Clayton64195a22011-02-23 00:35:02 +0000123};
124
Greg Clayton56b79682014-07-23 18:12:06 +0000125// Ensure that we have an entry in the g_core_definitions for each core. If you comment out an entry above,
126// you will need to comment out the corresponding ArchSpec::Core enumeration.
Zachary Turner3b2065f2014-07-28 16:44:28 +0000127static_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 +0000128
129
Greg Clayton64195a22011-02-23 00:35:02 +0000130struct ArchDefinitionEntry
131{
132 ArchSpec::Core core;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000133 uint32_t cpu;
134 uint32_t sub;
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000135 uint32_t cpu_mask;
136 uint32_t sub_mask;
Greg Clayton64195a22011-02-23 00:35:02 +0000137};
138
139struct ArchDefinition
140{
141 ArchitectureType type;
142 size_t num_entries;
143 const ArchDefinitionEntry *entries;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000144 const char *name;
145};
146
Greg Clayton41f92322010-06-11 03:25:34 +0000147
Greg Claytonc7bece562013-01-25 18:06:21 +0000148size_t
Greg Claytonab65b342011-04-13 22:47:15 +0000149ArchSpec::AutoComplete (const char *name, StringList &matches)
150{
151 uint32_t i;
152 if (name && name[0])
153 {
Greg Clayton56b79682014-07-23 18:12:06 +0000154 for (i = 0; i < llvm::array_lengthof(g_core_definitions); ++i)
Greg Claytonab65b342011-04-13 22:47:15 +0000155 {
156 if (NameMatches(g_core_definitions[i].name, eNameMatchStartsWith, name))
157 matches.AppendString (g_core_definitions[i].name);
158 }
159 }
160 else
161 {
Greg Clayton56b79682014-07-23 18:12:06 +0000162 for (i = 0; i < llvm::array_lengthof(g_core_definitions); ++i)
Greg Claytonab65b342011-04-13 22:47:15 +0000163 matches.AppendString (g_core_definitions[i].name);
164 }
165 return matches.GetSize();
166}
167
168
169
Greg Clayton64195a22011-02-23 00:35:02 +0000170#define CPU_ANY (UINT32_MAX)
171
172//===----------------------------------------------------------------------===//
173// A table that gets searched linearly for matches. This table is used to
174// convert cpu type and subtypes to architecture names, and to convert
175// architecture names to cpu types and subtypes. The ordering is important and
176// allows the precedence to be set when the table is built.
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000177#define SUBTYPE_MASK 0x00FFFFFFu
Greg Clayton64195a22011-02-23 00:35:02 +0000178static const ArchDefinitionEntry g_macho_arch_entries[] =
Greg Clayton41f92322010-06-11 03:25:34 +0000179{
Charles Davis510938e2013-08-27 05:04:57 +0000180 { ArchSpec::eCore_arm_generic , llvm::MachO::CPU_TYPE_ARM , CPU_ANY, UINT32_MAX , UINT32_MAX },
181 { ArchSpec::eCore_arm_generic , llvm::MachO::CPU_TYPE_ARM , 0 , UINT32_MAX , SUBTYPE_MASK },
182 { ArchSpec::eCore_arm_armv4 , llvm::MachO::CPU_TYPE_ARM , 5 , UINT32_MAX , SUBTYPE_MASK },
183 { ArchSpec::eCore_arm_armv4t , llvm::MachO::CPU_TYPE_ARM , 5 , UINT32_MAX , SUBTYPE_MASK },
184 { ArchSpec::eCore_arm_armv6 , llvm::MachO::CPU_TYPE_ARM , 6 , UINT32_MAX , SUBTYPE_MASK },
Jason Molenda64a11732013-10-08 03:01:08 +0000185 { ArchSpec::eCore_arm_armv6m , llvm::MachO::CPU_TYPE_ARM , 14 , UINT32_MAX , SUBTYPE_MASK },
Charles Davis510938e2013-08-27 05:04:57 +0000186 { ArchSpec::eCore_arm_armv5 , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
187 { ArchSpec::eCore_arm_armv5e , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
188 { ArchSpec::eCore_arm_armv5t , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
189 { ArchSpec::eCore_arm_xscale , llvm::MachO::CPU_TYPE_ARM , 8 , UINT32_MAX , SUBTYPE_MASK },
190 { ArchSpec::eCore_arm_armv7 , llvm::MachO::CPU_TYPE_ARM , 9 , UINT32_MAX , SUBTYPE_MASK },
191 { ArchSpec::eCore_arm_armv7f , llvm::MachO::CPU_TYPE_ARM , 10 , UINT32_MAX , SUBTYPE_MASK },
192 { ArchSpec::eCore_arm_armv7s , llvm::MachO::CPU_TYPE_ARM , 11 , UINT32_MAX , SUBTYPE_MASK },
193 { ArchSpec::eCore_arm_armv7k , llvm::MachO::CPU_TYPE_ARM , 12 , UINT32_MAX , SUBTYPE_MASK },
194 { ArchSpec::eCore_arm_armv7m , llvm::MachO::CPU_TYPE_ARM , 15 , UINT32_MAX , SUBTYPE_MASK },
195 { ArchSpec::eCore_arm_armv7em , llvm::MachO::CPU_TYPE_ARM , 16 , UINT32_MAX , SUBTYPE_MASK },
Jason Molendaa3329782014-03-29 18:54:20 +0000196 { ArchSpec::eCore_arm_arm64 , llvm::MachO::CPU_TYPE_ARM64 , CPU_ANY, UINT32_MAX , SUBTYPE_MASK },
197 { ArchSpec::eCore_arm_arm64 , llvm::MachO::CPU_TYPE_ARM64 , 0 , UINT32_MAX , SUBTYPE_MASK },
198 { ArchSpec::eCore_arm_arm64 , llvm::MachO::CPU_TYPE_ARM64 , 1 , UINT32_MAX , SUBTYPE_MASK },
199 { ArchSpec::eCore_arm_arm64 , llvm::MachO::CPU_TYPE_ARM64 , 13 , UINT32_MAX , SUBTYPE_MASK },
Charles Davis510938e2013-08-27 05:04:57 +0000200 { ArchSpec::eCore_thumb , llvm::MachO::CPU_TYPE_ARM , 0 , UINT32_MAX , SUBTYPE_MASK },
201 { ArchSpec::eCore_thumbv4t , llvm::MachO::CPU_TYPE_ARM , 5 , UINT32_MAX , SUBTYPE_MASK },
202 { ArchSpec::eCore_thumbv5 , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
203 { ArchSpec::eCore_thumbv5e , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
204 { ArchSpec::eCore_thumbv6 , llvm::MachO::CPU_TYPE_ARM , 6 , UINT32_MAX , SUBTYPE_MASK },
Jason Molenda64a11732013-10-08 03:01:08 +0000205 { ArchSpec::eCore_thumbv6m , llvm::MachO::CPU_TYPE_ARM , 14 , UINT32_MAX , SUBTYPE_MASK },
Charles Davis510938e2013-08-27 05:04:57 +0000206 { ArchSpec::eCore_thumbv7 , llvm::MachO::CPU_TYPE_ARM , 9 , UINT32_MAX , SUBTYPE_MASK },
207 { ArchSpec::eCore_thumbv7f , llvm::MachO::CPU_TYPE_ARM , 10 , UINT32_MAX , SUBTYPE_MASK },
208 { ArchSpec::eCore_thumbv7s , llvm::MachO::CPU_TYPE_ARM , 11 , UINT32_MAX , SUBTYPE_MASK },
209 { ArchSpec::eCore_thumbv7k , llvm::MachO::CPU_TYPE_ARM , 12 , UINT32_MAX , SUBTYPE_MASK },
210 { ArchSpec::eCore_thumbv7m , llvm::MachO::CPU_TYPE_ARM , 15 , UINT32_MAX , SUBTYPE_MASK },
211 { ArchSpec::eCore_thumbv7em , llvm::MachO::CPU_TYPE_ARM , 16 , UINT32_MAX , SUBTYPE_MASK },
212 { ArchSpec::eCore_ppc_generic , llvm::MachO::CPU_TYPE_POWERPC , CPU_ANY, UINT32_MAX , UINT32_MAX },
213 { ArchSpec::eCore_ppc_generic , llvm::MachO::CPU_TYPE_POWERPC , 0 , UINT32_MAX , SUBTYPE_MASK },
214 { ArchSpec::eCore_ppc_ppc601 , llvm::MachO::CPU_TYPE_POWERPC , 1 , UINT32_MAX , SUBTYPE_MASK },
215 { ArchSpec::eCore_ppc_ppc602 , llvm::MachO::CPU_TYPE_POWERPC , 2 , UINT32_MAX , SUBTYPE_MASK },
216 { ArchSpec::eCore_ppc_ppc603 , llvm::MachO::CPU_TYPE_POWERPC , 3 , UINT32_MAX , SUBTYPE_MASK },
217 { ArchSpec::eCore_ppc_ppc603e , llvm::MachO::CPU_TYPE_POWERPC , 4 , UINT32_MAX , SUBTYPE_MASK },
218 { ArchSpec::eCore_ppc_ppc603ev , llvm::MachO::CPU_TYPE_POWERPC , 5 , UINT32_MAX , SUBTYPE_MASK },
219 { ArchSpec::eCore_ppc_ppc604 , llvm::MachO::CPU_TYPE_POWERPC , 6 , UINT32_MAX , SUBTYPE_MASK },
220 { ArchSpec::eCore_ppc_ppc604e , llvm::MachO::CPU_TYPE_POWERPC , 7 , UINT32_MAX , SUBTYPE_MASK },
221 { ArchSpec::eCore_ppc_ppc620 , llvm::MachO::CPU_TYPE_POWERPC , 8 , UINT32_MAX , SUBTYPE_MASK },
222 { ArchSpec::eCore_ppc_ppc750 , llvm::MachO::CPU_TYPE_POWERPC , 9 , UINT32_MAX , SUBTYPE_MASK },
223 { ArchSpec::eCore_ppc_ppc7400 , llvm::MachO::CPU_TYPE_POWERPC , 10 , UINT32_MAX , SUBTYPE_MASK },
224 { ArchSpec::eCore_ppc_ppc7450 , llvm::MachO::CPU_TYPE_POWERPC , 11 , UINT32_MAX , SUBTYPE_MASK },
225 { ArchSpec::eCore_ppc_ppc970 , llvm::MachO::CPU_TYPE_POWERPC , 100 , UINT32_MAX , SUBTYPE_MASK },
226 { ArchSpec::eCore_ppc64_generic , llvm::MachO::CPU_TYPE_POWERPC64 , 0 , UINT32_MAX , SUBTYPE_MASK },
227 { ArchSpec::eCore_ppc64_ppc970_64 , llvm::MachO::CPU_TYPE_POWERPC64 , 100 , UINT32_MAX , SUBTYPE_MASK },
228 { ArchSpec::eCore_x86_32_i386 , llvm::MachO::CPU_TYPE_I386 , 3 , UINT32_MAX , SUBTYPE_MASK },
229 { ArchSpec::eCore_x86_32_i486 , llvm::MachO::CPU_TYPE_I386 , 4 , UINT32_MAX , SUBTYPE_MASK },
230 { ArchSpec::eCore_x86_32_i486sx , llvm::MachO::CPU_TYPE_I386 , 0x84 , UINT32_MAX , SUBTYPE_MASK },
Greg Claytona86dc432014-01-22 23:42:03 +0000231 { ArchSpec::eCore_x86_32_i386 , llvm::MachO::CPU_TYPE_I386 , CPU_ANY, UINT32_MAX , UINT32_MAX },
Charles Davis510938e2013-08-27 05:04:57 +0000232 { ArchSpec::eCore_x86_64_x86_64 , llvm::MachO::CPU_TYPE_X86_64 , 3 , UINT32_MAX , SUBTYPE_MASK },
233 { ArchSpec::eCore_x86_64_x86_64 , llvm::MachO::CPU_TYPE_X86_64 , 4 , UINT32_MAX , SUBTYPE_MASK },
Greg Claytona86dc432014-01-22 23:42:03 +0000234 { ArchSpec::eCore_x86_64_x86_64h , llvm::MachO::CPU_TYPE_X86_64 , 8 , UINT32_MAX , SUBTYPE_MASK },
235 { 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 +0000236 // Catch any unknown mach architectures so we can always use the object and symbol mach-o files
Charles Davis510938e2013-08-27 05:04:57 +0000237 { ArchSpec::eCore_uknownMach32 , 0 , 0 , 0xFF000000u, 0x00000000u },
238 { ArchSpec::eCore_uknownMach64 , llvm::MachO::CPU_ARCH_ABI64 , 0 , 0xFF000000u, 0x00000000u }
Greg Clayton64195a22011-02-23 00:35:02 +0000239};
240static const ArchDefinition g_macho_arch_def = {
241 eArchTypeMachO,
Saleem Abdulrasool28606952014-06-27 05:17:41 +0000242 llvm::array_lengthof(g_macho_arch_entries),
Greg Clayton64195a22011-02-23 00:35:02 +0000243 g_macho_arch_entries,
Greg Clayton64195a22011-02-23 00:35:02 +0000244 "mach-o"
Greg Clayton41f92322010-06-11 03:25:34 +0000245};
246
Greg Clayton64195a22011-02-23 00:35:02 +0000247//===----------------------------------------------------------------------===//
248// A table that gets searched linearly for matches. This table is used to
249// convert cpu type and subtypes to architecture names, and to convert
250// architecture names to cpu types and subtypes. The ordering is important and
251// allows the precedence to be set when the table is built.
252static const ArchDefinitionEntry g_elf_arch_entries[] =
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000253{
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000254 { ArchSpec::eCore_sparc_generic , llvm::ELF::EM_SPARC , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // Sparc
255 { ArchSpec::eCore_x86_32_i386 , llvm::ELF::EM_386 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // Intel 80386
256 { ArchSpec::eCore_x86_32_i486 , llvm::ELF::EM_486 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // Intel 486 (deprecated)
257 { ArchSpec::eCore_ppc_generic , llvm::ELF::EM_PPC , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // PowerPC
258 { ArchSpec::eCore_ppc64_generic , llvm::ELF::EM_PPC64 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // PowerPC64
259 { ArchSpec::eCore_arm_generic , llvm::ELF::EM_ARM , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // ARM
260 { ArchSpec::eCore_sparc9_generic , llvm::ELF::EM_SPARCV9, LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // SPARC V9
Ed Masteb73f8442013-10-10 00:59:47 +0000261 { ArchSpec::eCore_x86_64_x86_64 , llvm::ELF::EM_X86_64 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // AMD64
Deepak Panickal6d3df422014-02-19 11:16:46 +0000262 { ArchSpec::eCore_mips64 , llvm::ELF::EM_MIPS , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // MIPS
Todd Fiala14bbef52014-07-01 23:33:32 +0000263 { ArchSpec::eCore_hexagon_generic , llvm::ELF::EM_HEXAGON, LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // HEXAGON
Matthew Gardiner5f675792014-08-27 12:09:39 +0000264 { ArchSpec::eCore_kalimba , llvm::ELF::EM_CSR_KALIMBA, LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // KALIMBA
265 { ArchSpec::eCore_kalimba3 , llvm::ELF::EM_CSR_KALIMBA, 3, 0xFFFFFFFFu, 0xFFFFFFFFu }, // KALIMBA
266 { ArchSpec::eCore_kalimba4 , llvm::ELF::EM_CSR_KALIMBA, 4, 0xFFFFFFFFu, 0xFFFFFFFFu }, // KALIMBA
267 { ArchSpec::eCore_kalimba5 , llvm::ELF::EM_CSR_KALIMBA, 5, 0xFFFFFFFFu, 0xFFFFFFFFu } // KALIMBA
Todd Fiala14bbef52014-07-01 23:33:32 +0000268
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000269};
270
Greg Clayton64195a22011-02-23 00:35:02 +0000271static const ArchDefinition g_elf_arch_def = {
272 eArchTypeELF,
Saleem Abdulrasool28606952014-06-27 05:17:41 +0000273 llvm::array_lengthof(g_elf_arch_entries),
Greg Clayton64195a22011-02-23 00:35:02 +0000274 g_elf_arch_entries,
Greg Clayton64195a22011-02-23 00:35:02 +0000275 "elf",
Greg Clayton41f92322010-06-11 03:25:34 +0000276};
277
Charles Davis237ad972013-08-27 05:04:33 +0000278static const ArchDefinitionEntry g_coff_arch_entries[] =
279{
Zachary Turnerad587ae42014-07-28 16:44:49 +0000280 { 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 +0000281 { ArchSpec::eCore_ppc_generic , llvm::COFF::IMAGE_FILE_MACHINE_POWERPC , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // PowerPC
282 { ArchSpec::eCore_ppc_generic , llvm::COFF::IMAGE_FILE_MACHINE_POWERPCFP, LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // PowerPC (with FPU)
283 { ArchSpec::eCore_arm_generic , llvm::COFF::IMAGE_FILE_MACHINE_ARM , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // ARM
Saleem Abdulrasool1108cb32014-03-11 03:09:08 +0000284 { ArchSpec::eCore_arm_armv7 , llvm::COFF::IMAGE_FILE_MACHINE_ARMNT , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // ARMv7
Charles Davis237ad972013-08-27 05:04:33 +0000285 { ArchSpec::eCore_thumb , llvm::COFF::IMAGE_FILE_MACHINE_THUMB , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // ARMv7
286 { ArchSpec::eCore_x86_64_x86_64, llvm::COFF::IMAGE_FILE_MACHINE_AMD64 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu } // AMD64
287};
288
289static const ArchDefinition g_coff_arch_def = {
290 eArchTypeCOFF,
Saleem Abdulrasool28606952014-06-27 05:17:41 +0000291 llvm::array_lengthof(g_coff_arch_entries),
Charles Davis237ad972013-08-27 05:04:33 +0000292 g_coff_arch_entries,
293 "pe-coff",
294};
295
Greg Clayton64195a22011-02-23 00:35:02 +0000296//===----------------------------------------------------------------------===//
297// Table of all ArchDefinitions
298static const ArchDefinition *g_arch_definitions[] = {
299 &g_macho_arch_def,
Charles Davis237ad972013-08-27 05:04:33 +0000300 &g_elf_arch_def,
301 &g_coff_arch_def
Greg Clayton64195a22011-02-23 00:35:02 +0000302};
Greg Clayton41f92322010-06-11 03:25:34 +0000303
Saleem Abdulrasool28606952014-06-27 05:17:41 +0000304static const size_t k_num_arch_definitions = llvm::array_lengthof(g_arch_definitions);
Greg Clayton64195a22011-02-23 00:35:02 +0000305
306//===----------------------------------------------------------------------===//
307// Static helper functions.
308
309
310// Get the architecture definition for a given object type.
311static const ArchDefinition *
312FindArchDefinition (ArchitectureType arch_type)
313{
314 for (unsigned int i = 0; i < k_num_arch_definitions; ++i)
315 {
316 const ArchDefinition *def = g_arch_definitions[i];
317 if (def->type == arch_type)
318 return def;
319 }
320 return NULL;
321}
322
323// Get an architecture definition by name.
324static const CoreDefinition *
325FindCoreDefinition (llvm::StringRef name)
326{
Greg Clayton56b79682014-07-23 18:12:06 +0000327 for (unsigned int i = 0; i < llvm::array_lengthof(g_core_definitions); ++i)
Greg Clayton64195a22011-02-23 00:35:02 +0000328 {
329 if (name.equals_lower(g_core_definitions[i].name))
330 return &g_core_definitions[i];
331 }
332 return NULL;
333}
334
335static inline const CoreDefinition *
336FindCoreDefinition (ArchSpec::Core core)
337{
Greg Clayton56b79682014-07-23 18:12:06 +0000338 if (core >= 0 && core < llvm::array_lengthof(g_core_definitions))
Greg Clayton64195a22011-02-23 00:35:02 +0000339 return &g_core_definitions[core];
340 return NULL;
341}
342
343// Get a definition entry by cpu type and subtype.
344static const ArchDefinitionEntry *
345FindArchDefinitionEntry (const ArchDefinition *def, uint32_t cpu, uint32_t sub)
346{
347 if (def == NULL)
348 return NULL;
349
Greg Clayton64195a22011-02-23 00:35:02 +0000350 const ArchDefinitionEntry *entries = def->entries;
351 for (size_t i = 0; i < def->num_entries; ++i)
352 {
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000353 if (entries[i].cpu == (cpu & entries[i].cpu_mask))
354 if (entries[i].sub == (sub & entries[i].sub_mask))
355 return &entries[i];
Greg Clayton64195a22011-02-23 00:35:02 +0000356 }
357 return NULL;
358}
359
360static const ArchDefinitionEntry *
361FindArchDefinitionEntry (const ArchDefinition *def, ArchSpec::Core core)
362{
363 if (def == NULL)
364 return NULL;
365
366 const ArchDefinitionEntry *entries = def->entries;
367 for (size_t i = 0; i < def->num_entries; ++i)
368 {
369 if (entries[i].core == core)
370 return &entries[i];
371 }
372 return NULL;
373}
374
375//===----------------------------------------------------------------------===//
376// Constructors and destructors.
377
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000378ArchSpec::ArchSpec() :
Greg Clayton514487e2011-02-15 21:59:32 +0000379 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000380 m_core (kCore_invalid),
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000381 m_byte_order (eByteOrderInvalid),
382 m_distribution_id ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000383{
384}
385
Greg Claytoneb0103f2011-04-07 22:46:35 +0000386ArchSpec::ArchSpec (const char *triple_cstr, Platform *platform) :
Greg Clayton514487e2011-02-15 21:59:32 +0000387 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000388 m_core (kCore_invalid),
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000389 m_byte_order (eByteOrderInvalid),
390 m_distribution_id ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000391{
Greg Clayton64195a22011-02-23 00:35:02 +0000392 if (triple_cstr)
Greg Claytoneb0103f2011-04-07 22:46:35 +0000393 SetTriple(triple_cstr, platform);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000394}
395
Greg Clayton70512312012-05-08 01:45:38 +0000396
397ArchSpec::ArchSpec (const char *triple_cstr) :
398 m_triple (),
399 m_core (kCore_invalid),
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000400 m_byte_order (eByteOrderInvalid),
401 m_distribution_id ()
Greg Clayton70512312012-05-08 01:45:38 +0000402{
403 if (triple_cstr)
404 SetTriple(triple_cstr);
405}
406
Greg Clayton64195a22011-02-23 00:35:02 +0000407ArchSpec::ArchSpec(const llvm::Triple &triple) :
Greg Clayton514487e2011-02-15 21:59:32 +0000408 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000409 m_core (kCore_invalid),
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000410 m_byte_order (eByteOrderInvalid),
411 m_distribution_id ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000412{
Greg Clayton64195a22011-02-23 00:35:02 +0000413 SetTriple(triple);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000414}
415
Greg Claytone0d378b2011-03-24 21:19:54 +0000416ArchSpec::ArchSpec (ArchitectureType arch_type, uint32_t cpu, uint32_t subtype) :
Greg Clayton64195a22011-02-23 00:35:02 +0000417 m_triple (),
418 m_core (kCore_invalid),
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000419 m_byte_order (eByteOrderInvalid),
420 m_distribution_id ()
Greg Clayton64195a22011-02-23 00:35:02 +0000421{
422 SetArchitecture (arch_type, cpu, subtype);
423}
424
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000425ArchSpec::~ArchSpec()
426{
427}
428
Greg Clayton64195a22011-02-23 00:35:02 +0000429//===----------------------------------------------------------------------===//
430// Assignment and initialization.
431
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000432const ArchSpec&
433ArchSpec::operator= (const ArchSpec& rhs)
434{
435 if (this != &rhs)
436 {
Greg Clayton514487e2011-02-15 21:59:32 +0000437 m_triple = rhs.m_triple;
Greg Clayton64195a22011-02-23 00:35:02 +0000438 m_core = rhs.m_core;
Greg Clayton514487e2011-02-15 21:59:32 +0000439 m_byte_order = rhs.m_byte_order;
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000440 m_distribution_id = rhs.m_distribution_id;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000441 }
442 return *this;
443}
444
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000445void
446ArchSpec::Clear()
447{
Greg Clayton514487e2011-02-15 21:59:32 +0000448 m_triple = llvm::Triple();
Greg Clayton64195a22011-02-23 00:35:02 +0000449 m_core = kCore_invalid;
450 m_byte_order = eByteOrderInvalid;
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000451 m_distribution_id.Clear ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000452}
453
Greg Clayton64195a22011-02-23 00:35:02 +0000454//===----------------------------------------------------------------------===//
455// Predicates.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000456
Greg Clayton41f92322010-06-11 03:25:34 +0000457
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000458const char *
Greg Clayton64195a22011-02-23 00:35:02 +0000459ArchSpec::GetArchitectureName () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000460{
Greg Clayton64195a22011-02-23 00:35:02 +0000461 const CoreDefinition *core_def = FindCoreDefinition (m_core);
462 if (core_def)
463 return core_def->name;
464 return "unknown";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000465}
466
Greg Clayton64195a22011-02-23 00:35:02 +0000467uint32_t
468ArchSpec::GetMachOCPUType () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000469{
Greg Clayton64195a22011-02-23 00:35:02 +0000470 const CoreDefinition *core_def = FindCoreDefinition (m_core);
471 if (core_def)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000472 {
Greg Clayton64195a22011-02-23 00:35:02 +0000473 const ArchDefinitionEntry *arch_def = FindArchDefinitionEntry (&g_macho_arch_def, core_def->core);
474 if (arch_def)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000475 {
Greg Clayton64195a22011-02-23 00:35:02 +0000476 return arch_def->cpu;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000477 }
478 }
Greg Clayton64195a22011-02-23 00:35:02 +0000479 return LLDB_INVALID_CPUTYPE;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000480}
481
Greg Clayton64195a22011-02-23 00:35:02 +0000482uint32_t
483ArchSpec::GetMachOCPUSubType () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000484{
Greg Clayton64195a22011-02-23 00:35:02 +0000485 const CoreDefinition *core_def = FindCoreDefinition (m_core);
486 if (core_def)
487 {
488 const ArchDefinitionEntry *arch_def = FindArchDefinitionEntry (&g_macho_arch_def, core_def->core);
489 if (arch_def)
490 {
Greg Clayton1cb64962011-03-24 04:28:38 +0000491 return arch_def->sub;
Greg Clayton64195a22011-02-23 00:35:02 +0000492 }
493 }
494 return LLDB_INVALID_CPUTYPE;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000495}
496
Greg Clayton64195a22011-02-23 00:35:02 +0000497llvm::Triple::ArchType
498ArchSpec::GetMachine () const
499{
500 const CoreDefinition *core_def = FindCoreDefinition (m_core);
501 if (core_def)
502 return core_def->machine;
503
504 return llvm::Triple::UnknownArch;
505}
506
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000507const ConstString&
508ArchSpec::GetDistributionId () const
509{
510 return m_distribution_id;
511}
512
513void
514ArchSpec::SetDistributionId (const char* distribution_id)
515{
516 m_distribution_id.SetCString (distribution_id);
517}
518
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000519uint32_t
520ArchSpec::GetAddressByteSize() const
521{
Greg Clayton64195a22011-02-23 00:35:02 +0000522 const CoreDefinition *core_def = FindCoreDefinition (m_core);
523 if (core_def)
524 return core_def->addr_byte_size;
Greg Clayton41f92322010-06-11 03:25:34 +0000525 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000526}
527
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000528ByteOrder
529ArchSpec::GetDefaultEndian () const
530{
Greg Clayton64195a22011-02-23 00:35:02 +0000531 const CoreDefinition *core_def = FindCoreDefinition (m_core);
532 if (core_def)
533 return core_def->default_byte_order;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000534 return eByteOrderInvalid;
535}
536
Greg Clayton64195a22011-02-23 00:35:02 +0000537lldb::ByteOrder
538ArchSpec::GetByteOrder () const
539{
540 if (m_byte_order == eByteOrderInvalid)
541 return GetDefaultEndian();
542 return m_byte_order;
543}
544
545//===----------------------------------------------------------------------===//
546// Mutators.
547
548bool
549ArchSpec::SetTriple (const llvm::Triple &triple)
550{
551 m_triple = triple;
552
553 llvm::StringRef arch_name (m_triple.getArchName());
554 const CoreDefinition *core_def = FindCoreDefinition (arch_name);
555 if (core_def)
556 {
557 m_core = core_def->core;
Greg Claytoneb0103f2011-04-07 22:46:35 +0000558 // Set the byte order to the default byte order for an architecture.
559 // This can be modified if needed for cases when cores handle both
560 // big and little endian
561 m_byte_order = core_def->default_byte_order;
Greg Clayton64195a22011-02-23 00:35:02 +0000562 }
563 else
564 {
565 Clear();
566 }
567
568
569 return IsValid();
570}
571
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000572static bool
573ParseMachCPUDashSubtypeTriple (const char *triple_cstr, ArchSpec &arch)
574{
575 // Accept "12-10" or "12.10" as cpu type/subtype
576 if (isdigit(triple_cstr[0]))
577 {
578 char *end = NULL;
579 errno = 0;
Greg Claytonc7bece562013-01-25 18:06:21 +0000580 uint32_t cpu = (uint32_t)::strtoul (triple_cstr, &end, 0);
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000581 if (errno == 0 && cpu != 0 && end && ((*end == '-') || (*end == '.')))
582 {
583 errno = 0;
Greg Claytonc7bece562013-01-25 18:06:21 +0000584 uint32_t sub = (uint32_t)::strtoul (end + 1, &end, 0);
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000585 if (errno == 0 && end && ((*end == '-') || (*end == '.') || (*end == '\0')))
586 {
587 if (arch.SetArchitecture (eArchTypeMachO, cpu, sub))
588 {
589 if (*end == '-')
590 {
591 llvm::StringRef vendor_os (end + 1);
592 size_t dash_pos = vendor_os.find('-');
593 if (dash_pos != llvm::StringRef::npos)
594 {
595 llvm::StringRef vendor_str(vendor_os.substr(0, dash_pos));
596 arch.GetTriple().setVendorName(vendor_str);
597 const size_t vendor_start_pos = dash_pos+1;
Greg Claytonc7bece562013-01-25 18:06:21 +0000598 dash_pos = vendor_os.find('-', vendor_start_pos);
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000599 if (dash_pos == llvm::StringRef::npos)
600 {
601 if (vendor_start_pos < vendor_os.size())
602 arch.GetTriple().setOSName(vendor_os.substr(vendor_start_pos));
603 }
604 else
605 {
606 arch.GetTriple().setOSName(vendor_os.substr(vendor_start_pos, dash_pos - vendor_start_pos));
607 }
608 }
609 }
610 return true;
611 }
612 }
613 }
614 }
615 return false;
616}
Greg Clayton64195a22011-02-23 00:35:02 +0000617bool
Greg Clayton70512312012-05-08 01:45:38 +0000618ArchSpec::SetTriple (const char *triple_cstr)
Greg Clayton64195a22011-02-23 00:35:02 +0000619{
Greg Clayton23aca092011-08-12 23:32:52 +0000620 if (triple_cstr && triple_cstr[0])
Greg Clayton64195a22011-02-23 00:35:02 +0000621 {
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000622 if (ParseMachCPUDashSubtypeTriple (triple_cstr, *this))
623 return true;
624
Greg Clayton64195a22011-02-23 00:35:02 +0000625 llvm::StringRef triple_stref (triple_cstr);
626 if (triple_stref.startswith (LLDB_ARCH_DEFAULT))
627 {
628 // Special case for the current host default architectures...
629 if (triple_stref.equals (LLDB_ARCH_DEFAULT_32BIT))
Zachary Turner13b18262014-08-20 16:42:51 +0000630 *this = HostInfo::GetArchitecture(HostInfo::eArchKind32);
Greg Clayton64195a22011-02-23 00:35:02 +0000631 else if (triple_stref.equals (LLDB_ARCH_DEFAULT_64BIT))
Zachary Turner13b18262014-08-20 16:42:51 +0000632 *this = HostInfo::GetArchitecture(HostInfo::eArchKind64);
Greg Clayton64195a22011-02-23 00:35:02 +0000633 else if (triple_stref.equals (LLDB_ARCH_DEFAULT))
Zachary Turner13b18262014-08-20 16:42:51 +0000634 *this = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
Greg Clayton64195a22011-02-23 00:35:02 +0000635 }
636 else
637 {
638 std::string normalized_triple_sstr (llvm::Triple::normalize(triple_stref));
639 triple_stref = normalized_triple_sstr;
Greg Clayton70512312012-05-08 01:45:38 +0000640 SetTriple (llvm::Triple (triple_stref));
641 }
642 }
643 else
644 Clear();
645 return IsValid();
646}
647
648bool
649ArchSpec::SetTriple (const char *triple_cstr, Platform *platform)
650{
651 if (triple_cstr && triple_cstr[0])
652 {
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000653 if (ParseMachCPUDashSubtypeTriple (triple_cstr, *this))
654 return true;
655
Greg Clayton70512312012-05-08 01:45:38 +0000656 llvm::StringRef triple_stref (triple_cstr);
657 if (triple_stref.startswith (LLDB_ARCH_DEFAULT))
658 {
659 // Special case for the current host default architectures...
660 if (triple_stref.equals (LLDB_ARCH_DEFAULT_32BIT))
Zachary Turner13b18262014-08-20 16:42:51 +0000661 *this = HostInfo::GetArchitecture(HostInfo::eArchKind32);
Greg Clayton70512312012-05-08 01:45:38 +0000662 else if (triple_stref.equals (LLDB_ARCH_DEFAULT_64BIT))
Zachary Turner13b18262014-08-20 16:42:51 +0000663 *this = HostInfo::GetArchitecture(HostInfo::eArchKind64);
Greg Clayton70512312012-05-08 01:45:38 +0000664 else if (triple_stref.equals (LLDB_ARCH_DEFAULT))
Zachary Turner13b18262014-08-20 16:42:51 +0000665 *this = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
Greg Clayton70512312012-05-08 01:45:38 +0000666 }
667 else
668 {
669 ArchSpec raw_arch (triple_cstr);
670
671 std::string normalized_triple_sstr (llvm::Triple::normalize(triple_stref));
672 triple_stref = normalized_triple_sstr;
Greg Claytoneb0103f2011-04-07 22:46:35 +0000673 llvm::Triple normalized_triple (triple_stref);
674
675 const bool os_specified = normalized_triple.getOSName().size() > 0;
676 const bool vendor_specified = normalized_triple.getVendorName().size() > 0;
677 const bool env_specified = normalized_triple.getEnvironmentName().size() > 0;
678
679 // If we got an arch only, then default the vendor, os, environment
680 // to match the platform if one is supplied
681 if (!(os_specified || vendor_specified || env_specified))
682 {
683 if (platform)
684 {
685 // If we were given a platform, use the platform's system
686 // architecture. If this is not available (might not be
687 // connected) use the first supported architecture.
Greg Clayton70512312012-05-08 01:45:38 +0000688 ArchSpec compatible_arch;
Greg Clayton1e0c8842013-01-11 20:49:54 +0000689 if (platform->IsCompatibleArchitecture (raw_arch, false, &compatible_arch))
Greg Claytoneb0103f2011-04-07 22:46:35 +0000690 {
Greg Clayton70512312012-05-08 01:45:38 +0000691 if (compatible_arch.IsValid())
692 {
693 const llvm::Triple &compatible_triple = compatible_arch.GetTriple();
694 if (!vendor_specified)
695 normalized_triple.setVendor(compatible_triple.getVendor());
696 if (!os_specified)
697 normalized_triple.setOS(compatible_triple.getOS());
698 if (!env_specified && compatible_triple.getEnvironmentName().size())
699 normalized_triple.setEnvironment(compatible_triple.getEnvironment());
700 }
Greg Claytoneb0103f2011-04-07 22:46:35 +0000701 }
Greg Clayton70512312012-05-08 01:45:38 +0000702 else
Greg Claytoneb0103f2011-04-07 22:46:35 +0000703 {
Greg Clayton70512312012-05-08 01:45:38 +0000704 *this = raw_arch;
705 return IsValid();
Greg Claytoneb0103f2011-04-07 22:46:35 +0000706 }
707 }
708 else
709 {
710 // No platform specified, fall back to the host system for
711 // the default vendor, os, and environment.
Sean Callananbfb237bc2011-11-04 22:46:46 +0000712 llvm::Triple host_triple(llvm::sys::getDefaultTargetTriple());
Greg Clayton70512312012-05-08 01:45:38 +0000713 if (!vendor_specified)
714 normalized_triple.setVendor(host_triple.getVendor());
715 if (!vendor_specified)
716 normalized_triple.setOS(host_triple.getOS());
717 if (!env_specified && host_triple.getEnvironmentName().size())
718 normalized_triple.setEnvironment(host_triple.getEnvironment());
Greg Claytoneb0103f2011-04-07 22:46:35 +0000719 }
720 }
721 SetTriple (normalized_triple);
Greg Clayton64195a22011-02-23 00:35:02 +0000722 }
723 }
724 else
725 Clear();
726 return IsValid();
727}
728
Greg Clayton64195a22011-02-23 00:35:02 +0000729bool
Greg Claytone0d378b2011-03-24 21:19:54 +0000730ArchSpec::SetArchitecture (ArchitectureType arch_type, uint32_t cpu, uint32_t sub)
Greg Clayton64195a22011-02-23 00:35:02 +0000731{
732 m_core = kCore_invalid;
733 bool update_triple = true;
734 const ArchDefinition *arch_def = FindArchDefinition(arch_type);
735 if (arch_def)
736 {
737 const ArchDefinitionEntry *arch_def_entry = FindArchDefinitionEntry (arch_def, cpu, sub);
738 if (arch_def_entry)
739 {
740 const CoreDefinition *core_def = FindCoreDefinition (arch_def_entry->core);
741 if (core_def)
742 {
743 m_core = core_def->core;
744 update_triple = false;
Greg Clayton593577a2011-09-21 03:57:31 +0000745 // Always use the architecture name because it might be more descriptive
746 // than the architecture enum ("armv7" -> llvm::Triple::arm).
747 m_triple.setArchName(llvm::StringRef(core_def->name));
Greg Clayton64195a22011-02-23 00:35:02 +0000748 if (arch_type == eArchTypeMachO)
749 {
750 m_triple.setVendor (llvm::Triple::Apple);
Greg Clayton70512312012-05-08 01:45:38 +0000751
752 switch (core_def->machine)
753 {
Todd Fialad8eaa172014-07-23 14:37:35 +0000754 case llvm::Triple::aarch64:
Greg Clayton70512312012-05-08 01:45:38 +0000755 case llvm::Triple::arm:
756 case llvm::Triple::thumb:
757 m_triple.setOS (llvm::Triple::IOS);
758 break;
759
760 case llvm::Triple::x86:
761 case llvm::Triple::x86_64:
Greg Claytona3a6c122014-07-29 18:04:57 +0000762 // Don't set the OS for x86_64 or for x86 as we want to leave it as an "unspecified unknown"
763 // which means if we ask for the OS from the llvm::Triple we get back llvm::Triple::UnknownOS, but
764 // if we ask for the string value for the OS it will come back empty (unspecified).
765 // We do this because we now have iOS and MacOSX as the OS values for x86 and x86_64 for
766 // normal desktop and simulator binaries. And if we compare a "x86_64-apple-ios" to a "x86_64-apple-"
767 // triple, it will say it is compatible (because the OS is unspecified in the second one and will match
768 // anything in the first
769 break;
770
Greg Clayton70512312012-05-08 01:45:38 +0000771 default:
772 m_triple.setOS (llvm::Triple::MacOSX);
773 break;
774 }
Greg Clayton64195a22011-02-23 00:35:02 +0000775 }
776 else
777 {
778 m_triple.setVendor (llvm::Triple::UnknownVendor);
779 m_triple.setOS (llvm::Triple::UnknownOS);
780 }
Greg Clayton593577a2011-09-21 03:57:31 +0000781 // Fall back onto setting the machine type if the arch by name failed...
782 if (m_triple.getArch () == llvm::Triple::UnknownArch)
783 m_triple.setArch (core_def->machine);
Greg Clayton64195a22011-02-23 00:35:02 +0000784 }
785 }
786 }
787 CoreUpdated(update_triple);
788 return IsValid();
789}
790
Greg Clayton357132e2011-03-26 19:14:58 +0000791uint32_t
792ArchSpec::GetMinimumOpcodeByteSize() const
Greg Clayton64195a22011-02-23 00:35:02 +0000793{
Greg Clayton357132e2011-03-26 19:14:58 +0000794 const CoreDefinition *core_def = FindCoreDefinition (m_core);
795 if (core_def)
796 return core_def->min_opcode_byte_size;
797 return 0;
798}
799
800uint32_t
801ArchSpec::GetMaximumOpcodeByteSize() const
802{
803 const CoreDefinition *core_def = FindCoreDefinition (m_core);
804 if (core_def)
805 return core_def->max_opcode_byte_size;
806 return 0;
Greg Clayton64195a22011-02-23 00:35:02 +0000807}
808
Jason Molendaba813dc2012-11-04 03:20:05 +0000809bool
810ArchSpec::IsExactMatch (const ArchSpec& rhs) const
811{
Sean Callananbf4b7be2012-12-13 22:07:14 +0000812 return IsEqualTo (rhs, true);
Jason Molendaba813dc2012-11-04 03:20:05 +0000813}
814
815bool
816ArchSpec::IsCompatibleMatch (const ArchSpec& rhs) const
817{
Sean Callananbf4b7be2012-12-13 22:07:14 +0000818 return IsEqualTo (rhs, false);
Jason Molendaba813dc2012-11-04 03:20:05 +0000819}
820
821bool
Sean Callananbf4b7be2012-12-13 22:07:14 +0000822ArchSpec::IsEqualTo (const ArchSpec& rhs, bool exact_match) const
Jason Molendaba813dc2012-11-04 03:20:05 +0000823{
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000824 // explicitly ignoring m_distribution_id in this method.
825
Jason Molendaba813dc2012-11-04 03:20:05 +0000826 if (GetByteOrder() != rhs.GetByteOrder())
827 return false;
828
829 const ArchSpec::Core lhs_core = GetCore ();
830 const ArchSpec::Core rhs_core = rhs.GetCore ();
831
832 const bool core_match = cores_match (lhs_core, rhs_core, true, exact_match);
833
834 if (core_match)
835 {
836 const llvm::Triple &lhs_triple = GetTriple();
837 const llvm::Triple &rhs_triple = rhs.GetTriple();
838
839 const llvm::Triple::VendorType lhs_triple_vendor = lhs_triple.getVendor();
840 const llvm::Triple::VendorType rhs_triple_vendor = rhs_triple.getVendor();
841 if (lhs_triple_vendor != rhs_triple_vendor)
842 {
Sean Callananbf4b7be2012-12-13 22:07:14 +0000843 if (exact_match)
844 {
845 const bool rhs_vendor_specified = rhs.TripleVendorWasSpecified();
846 const bool lhs_vendor_specified = TripleVendorWasSpecified();
847 // Both architectures had the vendor specified, so if they aren't
848 // equal then we return false
849 if (rhs_vendor_specified && lhs_vendor_specified)
850 return false;
851 }
Jason Molendaba813dc2012-11-04 03:20:05 +0000852
853 // Only fail if both vendor types are not unknown
854 if (lhs_triple_vendor != llvm::Triple::UnknownVendor &&
855 rhs_triple_vendor != llvm::Triple::UnknownVendor)
856 return false;
857 }
858
859 const llvm::Triple::OSType lhs_triple_os = lhs_triple.getOS();
860 const llvm::Triple::OSType rhs_triple_os = rhs_triple.getOS();
861 if (lhs_triple_os != rhs_triple_os)
862 {
Sean Callananbf4b7be2012-12-13 22:07:14 +0000863 if (exact_match)
864 {
865 const bool rhs_os_specified = rhs.TripleOSWasSpecified();
866 const bool lhs_os_specified = TripleOSWasSpecified();
867 // Both architectures had the OS specified, so if they aren't
868 // equal then we return false
869 if (rhs_os_specified && lhs_os_specified)
870 return false;
871 }
Greg Clayton7ab7f892014-05-29 21:33:45 +0000872
Greg Clayton3f19ada2014-07-10 23:33:37 +0000873 // Only fail if both os types are not unknown
874 if (lhs_triple_os != llvm::Triple::UnknownOS &&
875 rhs_triple_os != llvm::Triple::UnknownOS)
876 return false;
Jason Molendaba813dc2012-11-04 03:20:05 +0000877 }
878
879 const llvm::Triple::EnvironmentType lhs_triple_env = lhs_triple.getEnvironment();
880 const llvm::Triple::EnvironmentType rhs_triple_env = rhs_triple.getEnvironment();
881
882 if (lhs_triple_env != rhs_triple_env)
883 {
884 // Only fail if both environment types are not unknown
885 if (lhs_triple_env != llvm::Triple::UnknownEnvironment &&
886 rhs_triple_env != llvm::Triple::UnknownEnvironment)
887 return false;
888 }
889 return true;
890 }
891 return false;
892}
893
Greg Clayton64195a22011-02-23 00:35:02 +0000894//===----------------------------------------------------------------------===//
895// Helper methods.
896
897void
898ArchSpec::CoreUpdated (bool update_triple)
899{
900 const CoreDefinition *core_def = FindCoreDefinition (m_core);
901 if (core_def)
902 {
903 if (update_triple)
904 m_triple = llvm::Triple(core_def->name, "unknown", "unknown");
905 m_byte_order = core_def->default_byte_order;
906 }
907 else
908 {
909 if (update_triple)
910 m_triple = llvm::Triple();
911 m_byte_order = eByteOrderInvalid;
912 }
913}
914
915//===----------------------------------------------------------------------===//
916// Operators.
917
Greg Clayton70512312012-05-08 01:45:38 +0000918static bool
Jason Molendaba813dc2012-11-04 03:20:05 +0000919cores_match (const ArchSpec::Core core1, const ArchSpec::Core core2, bool try_inverse, bool enforce_exact_match)
Greg Clayton70512312012-05-08 01:45:38 +0000920{
Jason Molendaba813dc2012-11-04 03:20:05 +0000921 if (core1 == core2)
922 return true;
923
Greg Clayton70512312012-05-08 01:45:38 +0000924 switch (core1)
925 {
Greg Clayton70512312012-05-08 01:45:38 +0000926 case ArchSpec::kCore_any:
927 return true;
928
Greg Clayton44362e02014-07-12 00:11:34 +0000929 case ArchSpec::eCore_arm_generic:
930 if (enforce_exact_match)
931 break;
932 // Fall through to case below
Greg Clayton70512312012-05-08 01:45:38 +0000933 case ArchSpec::kCore_arm_any:
934 if (core2 >= ArchSpec::kCore_arm_first && core2 <= ArchSpec::kCore_arm_last)
935 return true;
936 if (core2 >= ArchSpec::kCore_thumb_first && core2 <= ArchSpec::kCore_thumb_last)
937 return true;
938 if (core2 == ArchSpec::kCore_arm_any)
939 return true;
940 break;
Sylvestre Ledru1c9e0642014-08-18 14:53:42 +0000941
Greg Clayton70512312012-05-08 01:45:38 +0000942 case ArchSpec::kCore_x86_32_any:
943 if ((core2 >= ArchSpec::kCore_x86_32_first && core2 <= ArchSpec::kCore_x86_32_last) || (core2 == ArchSpec::kCore_x86_32_any))
944 return true;
945 break;
Zachary Turnerad587ae42014-07-28 16:44:49 +0000946
947 case ArchSpec::kCore_x86_64_any:
948 if ((core2 >= ArchSpec::kCore_x86_64_first && core2 <= ArchSpec::kCore_x86_64_last) || (core2 == ArchSpec::kCore_x86_64_any))
949 return true;
Sylvestre Ledru1c9e0642014-08-18 14:53:42 +0000950 break;
Zachary Turnerad587ae42014-07-28 16:44:49 +0000951
Greg Clayton70512312012-05-08 01:45:38 +0000952 case ArchSpec::kCore_ppc_any:
953 if ((core2 >= ArchSpec::kCore_ppc_first && core2 <= ArchSpec::kCore_ppc_last) || (core2 == ArchSpec::kCore_ppc_any))
954 return true;
955 break;
Sylvestre Ledru1c9e0642014-08-18 14:53:42 +0000956
Greg Clayton70512312012-05-08 01:45:38 +0000957 case ArchSpec::kCore_ppc64_any:
958 if ((core2 >= ArchSpec::kCore_ppc64_first && core2 <= ArchSpec::kCore_ppc64_last) || (core2 == ArchSpec::kCore_ppc64_any))
959 return true;
960 break;
961
Jason Molendaa3a04522013-09-27 23:21:54 +0000962 case ArchSpec::eCore_arm_armv6m:
963 if (!enforce_exact_match)
964 {
Greg Clayton44362e02014-07-12 00:11:34 +0000965 if (core2 == ArchSpec::eCore_arm_generic)
966 return true;
Jason Molendaa3a04522013-09-27 23:21:54 +0000967 try_inverse = false;
Jason Molendac7cda272013-09-27 23:29:10 +0000968 if (core2 == ArchSpec::eCore_arm_armv7)
Jason Molendaa3a04522013-09-27 23:21:54 +0000969 return true;
970 }
Sylvestre Ledru1c9e0642014-08-18 14:53:42 +0000971 break;
Deepak Panickal6d3df422014-02-19 11:16:46 +0000972
973 case ArchSpec::kCore_hexagon_any:
974 if ((core2 >= ArchSpec::kCore_hexagon_first && core2 <= ArchSpec::kCore_hexagon_last) || (core2 == ArchSpec::kCore_hexagon_any))
975 return true;
Jason Molendaa3a04522013-09-27 23:21:54 +0000976 break;
977
Jason Molenda7a1559c2013-03-08 01:20:17 +0000978 case ArchSpec::eCore_arm_armv7m:
979 case ArchSpec::eCore_arm_armv7em:
Johnny Chen1083b0d2012-08-28 22:53:40 +0000980 case ArchSpec::eCore_arm_armv7f:
981 case ArchSpec::eCore_arm_armv7k:
982 case ArchSpec::eCore_arm_armv7s:
Jason Molendaba813dc2012-11-04 03:20:05 +0000983 if (!enforce_exact_match)
984 {
Greg Clayton44362e02014-07-12 00:11:34 +0000985 if (core2 == ArchSpec::eCore_arm_generic)
986 return true;
Jason Molendaba813dc2012-11-04 03:20:05 +0000987 if (core2 == ArchSpec::eCore_arm_armv7)
988 return true;
Greg Clayton44362e02014-07-12 00:11:34 +0000989 try_inverse = false;
Jason Molendaba813dc2012-11-04 03:20:05 +0000990 }
Johnny Chen1083b0d2012-08-28 22:53:40 +0000991 break;
Sylvestre Ledru1c9e0642014-08-18 14:53:42 +0000992
Greg Clayton52edb362014-07-14 22:53:02 +0000993 case ArchSpec::eCore_x86_64_x86_64h:
994 if (!enforce_exact_match)
995 {
996 try_inverse = false;
997 if (core2 == ArchSpec::eCore_x86_64_x86_64)
998 return true;
999 }
1000 break;
Johnny Chen1083b0d2012-08-28 22:53:40 +00001001
Matthew Gardiner5f675792014-08-27 12:09:39 +00001002 case ArchSpec::eCore_kalimba:
1003 case ArchSpec::eCore_kalimba3:
1004 case ArchSpec::eCore_kalimba4:
1005 case ArchSpec::eCore_kalimba5:
1006 if (core2 >= ArchSpec::kCore_kalimba_first && core2 <= ArchSpec::kCore_kalimba_last)
1007 {
1008 return true;
1009 }
1010 break;
1011
Greg Clayton70512312012-05-08 01:45:38 +00001012 default:
1013 break;
1014 }
1015 if (try_inverse)
Jason Molendaba813dc2012-11-04 03:20:05 +00001016 return cores_match (core2, core1, false, enforce_exact_match);
Greg Clayton70512312012-05-08 01:45:38 +00001017 return false;
1018}
1019
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001020bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001021lldb_private::operator<(const ArchSpec& lhs, const ArchSpec& rhs)
1022{
Greg Clayton64195a22011-02-23 00:35:02 +00001023 const ArchSpec::Core lhs_core = lhs.GetCore ();
1024 const ArchSpec::Core rhs_core = rhs.GetCore ();
1025 return lhs_core < rhs_core;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001026}