blob: 015f76bffbb5312006e7263eb98f53f93322bf66 [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 Claytona97c4d22014-12-09 23:31:02 +000027#include "lldb/Target/Process.h"
28#include "lldb/Target/RegisterContext.h"
29#include "lldb/Target/Thread.h"
30#include "Plugins/Process/Utility/ARMDefines.h"
31#include "Plugins/Process/Utility/InstructionUtils.h"
Greg Clayton41f92322010-06-11 03:25:34 +000032
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033using namespace lldb;
34using namespace lldb_private;
35
Greg Clayton64195a22011-02-23 00:35:02 +000036#define ARCH_SPEC_SEPARATOR_CHAR '-'
Chris Lattner30fdc8d2010-06-08 16:52:24 +000037
Jason Molendaba813dc2012-11-04 03:20:05 +000038
39static bool cores_match (const ArchSpec::Core core1, const ArchSpec::Core core2, bool try_inverse, bool enforce_exact_match);
40
Greg Clayton64195a22011-02-23 00:35:02 +000041namespace lldb_private {
Chris Lattner30fdc8d2010-06-08 16:52:24 +000042
Greg Clayton64195a22011-02-23 00:35:02 +000043 struct CoreDefinition
44 {
45 ByteOrder default_byte_order;
46 uint32_t addr_byte_size;
Greg Clayton357132e2011-03-26 19:14:58 +000047 uint32_t min_opcode_byte_size;
48 uint32_t max_opcode_byte_size;
Greg Clayton64195a22011-02-23 00:35:02 +000049 llvm::Triple::ArchType machine;
50 ArchSpec::Core core;
Greg Clayton56b79682014-07-23 18:12:06 +000051 const char * const name;
Greg Clayton64195a22011-02-23 00:35:02 +000052 };
53
54}
55
56// This core information can be looked using the ArchSpec::Core as the index
Greg Clayton56b79682014-07-23 18:12:06 +000057static const CoreDefinition g_core_definitions[] =
Chris Lattner30fdc8d2010-06-08 16:52:24 +000058{
Greg Clayton357132e2011-03-26 19:14:58 +000059 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_generic , "arm" },
60 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv4 , "armv4" },
61 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv4t , "armv4t" },
62 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv5 , "armv5" },
Greg Claytonb5c39fe2011-12-16 18:15:52 +000063 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv5e , "armv5e" },
Greg Clayton357132e2011-03-26 19:14:58 +000064 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv5t , "armv5t" },
65 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv6 , "armv6" },
Jason Molendaa3a04522013-09-27 23:21:54 +000066 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv6m , "armv6m" },
Greg Clayton357132e2011-03-26 19:14:58 +000067 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7 , "armv7" },
68 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7f , "armv7f" },
Greg Clayton357132e2011-03-26 19:14:58 +000069 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7s , "armv7s" },
Jason Molenda7a1559c2013-03-08 01:20:17 +000070 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7k , "armv7k" },
71 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7m , "armv7m" },
72 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_armv7em , "armv7em" },
Greg Clayton357132e2011-03-26 19:14:58 +000073 { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm , ArchSpec::eCore_arm_xscale , "xscale" },
Greg Claytonb5c39fe2011-12-16 18:15:52 +000074 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumb , "thumb" },
75 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv4t , "thumbv4t" },
76 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv5 , "thumbv5" },
77 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv5e , "thumbv5e" },
78 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv6 , "thumbv6" },
Jason Molendaa3a04522013-09-27 23:21:54 +000079 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv6m , "thumbv6m" },
Greg Claytonb5c39fe2011-12-16 18:15:52 +000080 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7 , "thumbv7" },
81 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7f , "thumbv7f" },
Greg Claytonb5c39fe2011-12-16 18:15:52 +000082 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7s , "thumbv7s" },
Jason Molenda7a1559c2013-03-08 01:20:17 +000083 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7k , "thumbv7k" },
84 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7m , "thumbv7m" },
85 { eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb , ArchSpec::eCore_thumbv7em , "thumbv7em" },
Todd Fialad8eaa172014-07-23 14:37:35 +000086 { eByteOrderLittle, 8, 4, 4, llvm::Triple::aarch64, ArchSpec::eCore_arm_arm64 , "arm64" },
Todd Fiala02e71812014-08-28 14:32:43 +000087 { eByteOrderLittle, 8, 4, 4, llvm::Triple::aarch64, ArchSpec::eCore_arm_armv8 , "armv8" },
88 { eByteOrderLittle, 8, 4, 4, llvm::Triple::aarch64, ArchSpec::eCore_arm_aarch64 , "aarch64" },
Ed Masteb73f8442013-10-10 00:59:47 +000089
90 { eByteOrderBig , 8, 4, 4, llvm::Triple::mips64 , ArchSpec::eCore_mips64 , "mips64" },
Greg Clayton64195a22011-02-23 00:35:02 +000091
Justin Hibbits6256a0e2014-10-31 02:34:28 +000092 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_generic , "powerpc" },
Greg Clayton83b162d2013-08-12 18:34:04 +000093 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc601 , "ppc601" },
94 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc602 , "ppc602" },
95 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc603 , "ppc603" },
96 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc603e , "ppc603e" },
97 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc603ev , "ppc603ev" },
98 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc604 , "ppc604" },
99 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc604e , "ppc604e" },
100 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc620 , "ppc620" },
101 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc750 , "ppc750" },
102 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc7400 , "ppc7400" },
103 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc7450 , "ppc7450" },
104 { eByteOrderBig , 4, 4, 4, llvm::Triple::ppc , ArchSpec::eCore_ppc_ppc970 , "ppc970" },
Greg Clayton64195a22011-02-23 00:35:02 +0000105
Justin Hibbits6256a0e2014-10-31 02:34:28 +0000106 { eByteOrderBig , 8, 4, 4, llvm::Triple::ppc64 , ArchSpec::eCore_ppc64_generic , "powerpc64" },
Greg Clayton83b162d2013-08-12 18:34:04 +0000107 { eByteOrderBig , 8, 4, 4, llvm::Triple::ppc64 , ArchSpec::eCore_ppc64_ppc970_64 , "ppc970-64" },
Greg Clayton64195a22011-02-23 00:35:02 +0000108
Greg Clayton357132e2011-03-26 19:14:58 +0000109 { eByteOrderLittle, 4, 4, 4, llvm::Triple::sparc , ArchSpec::eCore_sparc_generic , "sparc" },
110 { eByteOrderLittle, 8, 4, 4, llvm::Triple::sparcv9, ArchSpec::eCore_sparc9_generic , "sparcv9" },
Greg Clayton64195a22011-02-23 00:35:02 +0000111
Greg Claytonab65b342011-04-13 22:47:15 +0000112 { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i386 , "i386" },
113 { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i486 , "i486" },
114 { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i486sx , "i486sx" },
Virgile Bello97a70e42014-04-08 14:48:48 +0000115 { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86 , ArchSpec::eCore_x86_32_i686 , "i686" },
Greg Clayton64195a22011-02-23 00:35:02 +0000116
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000117 { eByteOrderLittle, 8, 1, 15, llvm::Triple::x86_64 , ArchSpec::eCore_x86_64_x86_64 , "x86_64" },
Greg Claytona86dc432014-01-22 23:42:03 +0000118 { eByteOrderLittle, 8, 1, 15, llvm::Triple::x86_64 , ArchSpec::eCore_x86_64_x86_64h , "x86_64h" },
Deepak Panickal6d3df422014-02-19 11:16:46 +0000119 { eByteOrderLittle, 4, 4, 4, llvm::Triple::hexagon , ArchSpec::eCore_hexagon_generic, "hexagon" },
120 { eByteOrderLittle, 4, 4, 4, llvm::Triple::hexagon , ArchSpec::eCore_hexagon_hexagonv4, "hexagonv4" },
121 { eByteOrderLittle, 4, 4, 4, llvm::Triple::hexagon , ArchSpec::eCore_hexagon_hexagonv5, "hexagonv5" },
122
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000123 { eByteOrderLittle, 4, 4, 4 , llvm::Triple::UnknownArch , ArchSpec::eCore_uknownMach32 , "unknown-mach-32" },
Todd Fiala14bbef52014-07-01 23:33:32 +0000124 { eByteOrderLittle, 8, 4, 4 , llvm::Triple::UnknownArch , ArchSpec::eCore_uknownMach64 , "unknown-mach-64" },
125
Matthew Gardiner5f675792014-08-27 12:09:39 +0000126 { eByteOrderBig , 4, 1, 1 , llvm::Triple::kalimba , ArchSpec::eCore_kalimba3 , "kalimba3" },
127 { eByteOrderLittle, 4, 1, 1 , llvm::Triple::kalimba , ArchSpec::eCore_kalimba4 , "kalimba4" },
128 { eByteOrderLittle, 4, 1, 1 , llvm::Triple::kalimba , ArchSpec::eCore_kalimba5 , "kalimba5" }
Greg Clayton64195a22011-02-23 00:35:02 +0000129};
130
Greg Clayton56b79682014-07-23 18:12:06 +0000131// Ensure that we have an entry in the g_core_definitions for each core. If you comment out an entry above,
132// you will need to comment out the corresponding ArchSpec::Core enumeration.
Zachary Turner3b2065f2014-07-28 16:44:28 +0000133static_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 +0000134
135
Greg Clayton64195a22011-02-23 00:35:02 +0000136struct ArchDefinitionEntry
137{
138 ArchSpec::Core core;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000139 uint32_t cpu;
140 uint32_t sub;
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000141 uint32_t cpu_mask;
142 uint32_t sub_mask;
Greg Clayton64195a22011-02-23 00:35:02 +0000143};
144
145struct ArchDefinition
146{
147 ArchitectureType type;
148 size_t num_entries;
149 const ArchDefinitionEntry *entries;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000150 const char *name;
151};
152
Greg Clayton41f92322010-06-11 03:25:34 +0000153
Greg Claytonc7bece562013-01-25 18:06:21 +0000154size_t
Greg Claytonab65b342011-04-13 22:47:15 +0000155ArchSpec::AutoComplete (const char *name, StringList &matches)
156{
157 uint32_t i;
158 if (name && name[0])
159 {
Greg Clayton56b79682014-07-23 18:12:06 +0000160 for (i = 0; i < llvm::array_lengthof(g_core_definitions); ++i)
Greg Claytonab65b342011-04-13 22:47:15 +0000161 {
162 if (NameMatches(g_core_definitions[i].name, eNameMatchStartsWith, name))
163 matches.AppendString (g_core_definitions[i].name);
164 }
165 }
166 else
167 {
Greg Clayton56b79682014-07-23 18:12:06 +0000168 for (i = 0; i < llvm::array_lengthof(g_core_definitions); ++i)
Greg Claytonab65b342011-04-13 22:47:15 +0000169 matches.AppendString (g_core_definitions[i].name);
170 }
171 return matches.GetSize();
172}
173
174
175
Greg Clayton64195a22011-02-23 00:35:02 +0000176#define CPU_ANY (UINT32_MAX)
177
178//===----------------------------------------------------------------------===//
179// A table that gets searched linearly for matches. This table is used to
180// convert cpu type and subtypes to architecture names, and to convert
181// architecture names to cpu types and subtypes. The ordering is important and
182// allows the precedence to be set when the table is built.
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000183#define SUBTYPE_MASK 0x00FFFFFFu
Greg Clayton64195a22011-02-23 00:35:02 +0000184static const ArchDefinitionEntry g_macho_arch_entries[] =
Greg Clayton41f92322010-06-11 03:25:34 +0000185{
Charles Davis510938e2013-08-27 05:04:57 +0000186 { ArchSpec::eCore_arm_generic , llvm::MachO::CPU_TYPE_ARM , CPU_ANY, UINT32_MAX , UINT32_MAX },
187 { ArchSpec::eCore_arm_generic , llvm::MachO::CPU_TYPE_ARM , 0 , UINT32_MAX , SUBTYPE_MASK },
188 { ArchSpec::eCore_arm_armv4 , llvm::MachO::CPU_TYPE_ARM , 5 , UINT32_MAX , SUBTYPE_MASK },
189 { ArchSpec::eCore_arm_armv4t , llvm::MachO::CPU_TYPE_ARM , 5 , UINT32_MAX , SUBTYPE_MASK },
190 { ArchSpec::eCore_arm_armv6 , llvm::MachO::CPU_TYPE_ARM , 6 , UINT32_MAX , SUBTYPE_MASK },
Jason Molenda64a11732013-10-08 03:01:08 +0000191 { ArchSpec::eCore_arm_armv6m , llvm::MachO::CPU_TYPE_ARM , 14 , UINT32_MAX , SUBTYPE_MASK },
Charles Davis510938e2013-08-27 05:04:57 +0000192 { ArchSpec::eCore_arm_armv5 , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
193 { ArchSpec::eCore_arm_armv5e , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
194 { ArchSpec::eCore_arm_armv5t , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
195 { ArchSpec::eCore_arm_xscale , llvm::MachO::CPU_TYPE_ARM , 8 , UINT32_MAX , SUBTYPE_MASK },
196 { ArchSpec::eCore_arm_armv7 , llvm::MachO::CPU_TYPE_ARM , 9 , UINT32_MAX , SUBTYPE_MASK },
197 { ArchSpec::eCore_arm_armv7f , llvm::MachO::CPU_TYPE_ARM , 10 , UINT32_MAX , SUBTYPE_MASK },
198 { ArchSpec::eCore_arm_armv7s , llvm::MachO::CPU_TYPE_ARM , 11 , UINT32_MAX , SUBTYPE_MASK },
199 { ArchSpec::eCore_arm_armv7k , llvm::MachO::CPU_TYPE_ARM , 12 , UINT32_MAX , SUBTYPE_MASK },
200 { ArchSpec::eCore_arm_armv7m , llvm::MachO::CPU_TYPE_ARM , 15 , UINT32_MAX , SUBTYPE_MASK },
201 { ArchSpec::eCore_arm_armv7em , llvm::MachO::CPU_TYPE_ARM , 16 , UINT32_MAX , SUBTYPE_MASK },
Jason Molendaa3329782014-03-29 18:54:20 +0000202 { ArchSpec::eCore_arm_arm64 , llvm::MachO::CPU_TYPE_ARM64 , 1 , UINT32_MAX , SUBTYPE_MASK },
Jason Molenda22952582014-11-12 01:11:36 +0000203 { ArchSpec::eCore_arm_arm64 , llvm::MachO::CPU_TYPE_ARM64 , 0 , UINT32_MAX , SUBTYPE_MASK },
Jason Molendaa3329782014-03-29 18:54:20 +0000204 { ArchSpec::eCore_arm_arm64 , llvm::MachO::CPU_TYPE_ARM64 , 13 , UINT32_MAX , SUBTYPE_MASK },
Jason Molenda22952582014-11-12 01:11:36 +0000205 { ArchSpec::eCore_arm_arm64 , llvm::MachO::CPU_TYPE_ARM64 , CPU_ANY, UINT32_MAX , SUBTYPE_MASK },
Charles Davis510938e2013-08-27 05:04:57 +0000206 { ArchSpec::eCore_thumb , llvm::MachO::CPU_TYPE_ARM , 0 , UINT32_MAX , SUBTYPE_MASK },
207 { ArchSpec::eCore_thumbv4t , llvm::MachO::CPU_TYPE_ARM , 5 , UINT32_MAX , SUBTYPE_MASK },
208 { ArchSpec::eCore_thumbv5 , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
209 { ArchSpec::eCore_thumbv5e , llvm::MachO::CPU_TYPE_ARM , 7 , UINT32_MAX , SUBTYPE_MASK },
210 { ArchSpec::eCore_thumbv6 , llvm::MachO::CPU_TYPE_ARM , 6 , UINT32_MAX , SUBTYPE_MASK },
Jason Molenda64a11732013-10-08 03:01:08 +0000211 { ArchSpec::eCore_thumbv6m , llvm::MachO::CPU_TYPE_ARM , 14 , UINT32_MAX , SUBTYPE_MASK },
Charles Davis510938e2013-08-27 05:04:57 +0000212 { ArchSpec::eCore_thumbv7 , llvm::MachO::CPU_TYPE_ARM , 9 , UINT32_MAX , SUBTYPE_MASK },
213 { ArchSpec::eCore_thumbv7f , llvm::MachO::CPU_TYPE_ARM , 10 , UINT32_MAX , SUBTYPE_MASK },
214 { ArchSpec::eCore_thumbv7s , llvm::MachO::CPU_TYPE_ARM , 11 , UINT32_MAX , SUBTYPE_MASK },
215 { ArchSpec::eCore_thumbv7k , llvm::MachO::CPU_TYPE_ARM , 12 , UINT32_MAX , SUBTYPE_MASK },
216 { ArchSpec::eCore_thumbv7m , llvm::MachO::CPU_TYPE_ARM , 15 , UINT32_MAX , SUBTYPE_MASK },
217 { ArchSpec::eCore_thumbv7em , llvm::MachO::CPU_TYPE_ARM , 16 , UINT32_MAX , SUBTYPE_MASK },
218 { ArchSpec::eCore_ppc_generic , llvm::MachO::CPU_TYPE_POWERPC , CPU_ANY, UINT32_MAX , UINT32_MAX },
219 { ArchSpec::eCore_ppc_generic , llvm::MachO::CPU_TYPE_POWERPC , 0 , UINT32_MAX , SUBTYPE_MASK },
220 { ArchSpec::eCore_ppc_ppc601 , llvm::MachO::CPU_TYPE_POWERPC , 1 , UINT32_MAX , SUBTYPE_MASK },
221 { ArchSpec::eCore_ppc_ppc602 , llvm::MachO::CPU_TYPE_POWERPC , 2 , UINT32_MAX , SUBTYPE_MASK },
222 { ArchSpec::eCore_ppc_ppc603 , llvm::MachO::CPU_TYPE_POWERPC , 3 , UINT32_MAX , SUBTYPE_MASK },
223 { ArchSpec::eCore_ppc_ppc603e , llvm::MachO::CPU_TYPE_POWERPC , 4 , UINT32_MAX , SUBTYPE_MASK },
224 { ArchSpec::eCore_ppc_ppc603ev , llvm::MachO::CPU_TYPE_POWERPC , 5 , UINT32_MAX , SUBTYPE_MASK },
225 { ArchSpec::eCore_ppc_ppc604 , llvm::MachO::CPU_TYPE_POWERPC , 6 , UINT32_MAX , SUBTYPE_MASK },
226 { ArchSpec::eCore_ppc_ppc604e , llvm::MachO::CPU_TYPE_POWERPC , 7 , UINT32_MAX , SUBTYPE_MASK },
227 { ArchSpec::eCore_ppc_ppc620 , llvm::MachO::CPU_TYPE_POWERPC , 8 , UINT32_MAX , SUBTYPE_MASK },
228 { ArchSpec::eCore_ppc_ppc750 , llvm::MachO::CPU_TYPE_POWERPC , 9 , UINT32_MAX , SUBTYPE_MASK },
229 { ArchSpec::eCore_ppc_ppc7400 , llvm::MachO::CPU_TYPE_POWERPC , 10 , UINT32_MAX , SUBTYPE_MASK },
230 { ArchSpec::eCore_ppc_ppc7450 , llvm::MachO::CPU_TYPE_POWERPC , 11 , UINT32_MAX , SUBTYPE_MASK },
231 { ArchSpec::eCore_ppc_ppc970 , llvm::MachO::CPU_TYPE_POWERPC , 100 , UINT32_MAX , SUBTYPE_MASK },
232 { ArchSpec::eCore_ppc64_generic , llvm::MachO::CPU_TYPE_POWERPC64 , 0 , UINT32_MAX , SUBTYPE_MASK },
233 { ArchSpec::eCore_ppc64_ppc970_64 , llvm::MachO::CPU_TYPE_POWERPC64 , 100 , UINT32_MAX , SUBTYPE_MASK },
234 { ArchSpec::eCore_x86_32_i386 , llvm::MachO::CPU_TYPE_I386 , 3 , UINT32_MAX , SUBTYPE_MASK },
235 { ArchSpec::eCore_x86_32_i486 , llvm::MachO::CPU_TYPE_I386 , 4 , UINT32_MAX , SUBTYPE_MASK },
236 { ArchSpec::eCore_x86_32_i486sx , llvm::MachO::CPU_TYPE_I386 , 0x84 , UINT32_MAX , SUBTYPE_MASK },
Greg Claytona86dc432014-01-22 23:42:03 +0000237 { ArchSpec::eCore_x86_32_i386 , llvm::MachO::CPU_TYPE_I386 , CPU_ANY, UINT32_MAX , UINT32_MAX },
Charles Davis510938e2013-08-27 05:04:57 +0000238 { ArchSpec::eCore_x86_64_x86_64 , llvm::MachO::CPU_TYPE_X86_64 , 3 , UINT32_MAX , SUBTYPE_MASK },
239 { ArchSpec::eCore_x86_64_x86_64 , llvm::MachO::CPU_TYPE_X86_64 , 4 , UINT32_MAX , SUBTYPE_MASK },
Greg Claytona86dc432014-01-22 23:42:03 +0000240 { ArchSpec::eCore_x86_64_x86_64h , llvm::MachO::CPU_TYPE_X86_64 , 8 , UINT32_MAX , SUBTYPE_MASK },
241 { 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 +0000242 // Catch any unknown mach architectures so we can always use the object and symbol mach-o files
Charles Davis510938e2013-08-27 05:04:57 +0000243 { ArchSpec::eCore_uknownMach32 , 0 , 0 , 0xFF000000u, 0x00000000u },
244 { ArchSpec::eCore_uknownMach64 , llvm::MachO::CPU_ARCH_ABI64 , 0 , 0xFF000000u, 0x00000000u }
Greg Clayton64195a22011-02-23 00:35:02 +0000245};
246static const ArchDefinition g_macho_arch_def = {
247 eArchTypeMachO,
Saleem Abdulrasool28606952014-06-27 05:17:41 +0000248 llvm::array_lengthof(g_macho_arch_entries),
Greg Clayton64195a22011-02-23 00:35:02 +0000249 g_macho_arch_entries,
Greg Clayton64195a22011-02-23 00:35:02 +0000250 "mach-o"
Greg Clayton41f92322010-06-11 03:25:34 +0000251};
252
Greg Clayton64195a22011-02-23 00:35:02 +0000253//===----------------------------------------------------------------------===//
254// A table that gets searched linearly for matches. This table is used to
255// convert cpu type and subtypes to architecture names, and to convert
256// architecture names to cpu types and subtypes. The ordering is important and
257// allows the precedence to be set when the table is built.
258static const ArchDefinitionEntry g_elf_arch_entries[] =
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000259{
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000260 { ArchSpec::eCore_sparc_generic , llvm::ELF::EM_SPARC , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // Sparc
261 { ArchSpec::eCore_x86_32_i386 , llvm::ELF::EM_386 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // Intel 80386
262 { ArchSpec::eCore_x86_32_i486 , llvm::ELF::EM_486 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // Intel 486 (deprecated)
263 { ArchSpec::eCore_ppc_generic , llvm::ELF::EM_PPC , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // PowerPC
264 { ArchSpec::eCore_ppc64_generic , llvm::ELF::EM_PPC64 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // PowerPC64
265 { ArchSpec::eCore_arm_generic , llvm::ELF::EM_ARM , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // ARM
Todd Fiala02e71812014-08-28 14:32:43 +0000266 { ArchSpec::eCore_arm_aarch64 , llvm::ELF::EM_AARCH64, LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // ARM64
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000267 { ArchSpec::eCore_sparc9_generic , llvm::ELF::EM_SPARCV9, LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // SPARC V9
Ed Masteb73f8442013-10-10 00:59:47 +0000268 { ArchSpec::eCore_x86_64_x86_64 , llvm::ELF::EM_X86_64 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // AMD64
Deepak Panickal6d3df422014-02-19 11:16:46 +0000269 { ArchSpec::eCore_mips64 , llvm::ELF::EM_MIPS , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // MIPS
Todd Fiala14bbef52014-07-01 23:33:32 +0000270 { ArchSpec::eCore_hexagon_generic , llvm::ELF::EM_HEXAGON, LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // HEXAGON
Matthew Gardinerf03e6d842014-09-29 08:02:24 +0000271 { ArchSpec::eCore_kalimba3 , llvm::ELF::EM_CSR_KALIMBA, llvm::Triple::KalimbaSubArch_v3, 0xFFFFFFFFu, 0xFFFFFFFFu }, // KALIMBA
272 { ArchSpec::eCore_kalimba4 , llvm::ELF::EM_CSR_KALIMBA, llvm::Triple::KalimbaSubArch_v4, 0xFFFFFFFFu, 0xFFFFFFFFu }, // KALIMBA
273 { ArchSpec::eCore_kalimba5 , llvm::ELF::EM_CSR_KALIMBA, llvm::Triple::KalimbaSubArch_v5, 0xFFFFFFFFu, 0xFFFFFFFFu } // KALIMBA
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000274};
275
Greg Clayton64195a22011-02-23 00:35:02 +0000276static const ArchDefinition g_elf_arch_def = {
277 eArchTypeELF,
Saleem Abdulrasool28606952014-06-27 05:17:41 +0000278 llvm::array_lengthof(g_elf_arch_entries),
Greg Clayton64195a22011-02-23 00:35:02 +0000279 g_elf_arch_entries,
Greg Clayton64195a22011-02-23 00:35:02 +0000280 "elf",
Greg Clayton41f92322010-06-11 03:25:34 +0000281};
282
Charles Davis237ad972013-08-27 05:04:33 +0000283static const ArchDefinitionEntry g_coff_arch_entries[] =
284{
Zachary Turnerad587ae42014-07-28 16:44:49 +0000285 { 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 +0000286 { ArchSpec::eCore_ppc_generic , llvm::COFF::IMAGE_FILE_MACHINE_POWERPC , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // PowerPC
287 { ArchSpec::eCore_ppc_generic , llvm::COFF::IMAGE_FILE_MACHINE_POWERPCFP, LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // PowerPC (with FPU)
288 { ArchSpec::eCore_arm_generic , llvm::COFF::IMAGE_FILE_MACHINE_ARM , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // ARM
Saleem Abdulrasool1108cb32014-03-11 03:09:08 +0000289 { ArchSpec::eCore_arm_armv7 , llvm::COFF::IMAGE_FILE_MACHINE_ARMNT , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // ARMv7
Charles Davis237ad972013-08-27 05:04:33 +0000290 { ArchSpec::eCore_thumb , llvm::COFF::IMAGE_FILE_MACHINE_THUMB , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu }, // ARMv7
291 { ArchSpec::eCore_x86_64_x86_64, llvm::COFF::IMAGE_FILE_MACHINE_AMD64 , LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu } // AMD64
292};
293
294static const ArchDefinition g_coff_arch_def = {
295 eArchTypeCOFF,
Saleem Abdulrasool28606952014-06-27 05:17:41 +0000296 llvm::array_lengthof(g_coff_arch_entries),
Charles Davis237ad972013-08-27 05:04:33 +0000297 g_coff_arch_entries,
298 "pe-coff",
299};
300
Greg Clayton64195a22011-02-23 00:35:02 +0000301//===----------------------------------------------------------------------===//
302// Table of all ArchDefinitions
303static const ArchDefinition *g_arch_definitions[] = {
304 &g_macho_arch_def,
Charles Davis237ad972013-08-27 05:04:33 +0000305 &g_elf_arch_def,
306 &g_coff_arch_def
Greg Clayton64195a22011-02-23 00:35:02 +0000307};
Greg Clayton41f92322010-06-11 03:25:34 +0000308
Saleem Abdulrasool28606952014-06-27 05:17:41 +0000309static const size_t k_num_arch_definitions = llvm::array_lengthof(g_arch_definitions);
Greg Clayton64195a22011-02-23 00:35:02 +0000310
311//===----------------------------------------------------------------------===//
312// Static helper functions.
313
314
315// Get the architecture definition for a given object type.
316static const ArchDefinition *
317FindArchDefinition (ArchitectureType arch_type)
318{
319 for (unsigned int i = 0; i < k_num_arch_definitions; ++i)
320 {
321 const ArchDefinition *def = g_arch_definitions[i];
322 if (def->type == arch_type)
323 return def;
324 }
325 return NULL;
326}
327
328// Get an architecture definition by name.
329static const CoreDefinition *
330FindCoreDefinition (llvm::StringRef name)
331{
Greg Clayton56b79682014-07-23 18:12:06 +0000332 for (unsigned int i = 0; i < llvm::array_lengthof(g_core_definitions); ++i)
Greg Clayton64195a22011-02-23 00:35:02 +0000333 {
334 if (name.equals_lower(g_core_definitions[i].name))
335 return &g_core_definitions[i];
336 }
337 return NULL;
338}
339
340static inline const CoreDefinition *
341FindCoreDefinition (ArchSpec::Core core)
342{
Greg Clayton56b79682014-07-23 18:12:06 +0000343 if (core >= 0 && core < llvm::array_lengthof(g_core_definitions))
Greg Clayton64195a22011-02-23 00:35:02 +0000344 return &g_core_definitions[core];
345 return NULL;
346}
347
348// Get a definition entry by cpu type and subtype.
349static const ArchDefinitionEntry *
350FindArchDefinitionEntry (const ArchDefinition *def, uint32_t cpu, uint32_t sub)
351{
352 if (def == NULL)
353 return NULL;
354
Greg Clayton64195a22011-02-23 00:35:02 +0000355 const ArchDefinitionEntry *entries = def->entries;
356 for (size_t i = 0; i < def->num_entries; ++i)
357 {
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000358 if (entries[i].cpu == (cpu & entries[i].cpu_mask))
359 if (entries[i].sub == (sub & entries[i].sub_mask))
360 return &entries[i];
Greg Clayton64195a22011-02-23 00:35:02 +0000361 }
362 return NULL;
363}
364
365static const ArchDefinitionEntry *
366FindArchDefinitionEntry (const ArchDefinition *def, ArchSpec::Core core)
367{
368 if (def == NULL)
369 return NULL;
370
371 const ArchDefinitionEntry *entries = def->entries;
372 for (size_t i = 0; i < def->num_entries; ++i)
373 {
374 if (entries[i].core == core)
375 return &entries[i];
376 }
377 return NULL;
378}
379
380//===----------------------------------------------------------------------===//
381// Constructors and destructors.
382
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000383ArchSpec::ArchSpec() :
Greg Clayton514487e2011-02-15 21:59:32 +0000384 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000385 m_core (kCore_invalid),
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000386 m_byte_order (eByteOrderInvalid),
387 m_distribution_id ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000388{
389}
390
Greg Claytoneb0103f2011-04-07 22:46:35 +0000391ArchSpec::ArchSpec (const char *triple_cstr, Platform *platform) :
Greg Clayton514487e2011-02-15 21:59:32 +0000392 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000393 m_core (kCore_invalid),
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000394 m_byte_order (eByteOrderInvalid),
395 m_distribution_id ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000396{
Greg Clayton64195a22011-02-23 00:35:02 +0000397 if (triple_cstr)
Greg Claytoneb0103f2011-04-07 22:46:35 +0000398 SetTriple(triple_cstr, platform);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000399}
400
Greg Clayton70512312012-05-08 01:45:38 +0000401
402ArchSpec::ArchSpec (const char *triple_cstr) :
403 m_triple (),
404 m_core (kCore_invalid),
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000405 m_byte_order (eByteOrderInvalid),
406 m_distribution_id ()
Greg Clayton70512312012-05-08 01:45:38 +0000407{
408 if (triple_cstr)
409 SetTriple(triple_cstr);
410}
411
Greg Clayton64195a22011-02-23 00:35:02 +0000412ArchSpec::ArchSpec(const llvm::Triple &triple) :
Greg Clayton514487e2011-02-15 21:59:32 +0000413 m_triple (),
Greg Clayton64195a22011-02-23 00:35:02 +0000414 m_core (kCore_invalid),
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000415 m_byte_order (eByteOrderInvalid),
416 m_distribution_id ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000417{
Greg Clayton64195a22011-02-23 00:35:02 +0000418 SetTriple(triple);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000419}
420
Greg Claytone0d378b2011-03-24 21:19:54 +0000421ArchSpec::ArchSpec (ArchitectureType arch_type, uint32_t cpu, uint32_t subtype) :
Greg Clayton64195a22011-02-23 00:35:02 +0000422 m_triple (),
423 m_core (kCore_invalid),
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000424 m_byte_order (eByteOrderInvalid),
425 m_distribution_id ()
Greg Clayton64195a22011-02-23 00:35:02 +0000426{
427 SetArchitecture (arch_type, cpu, subtype);
428}
429
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000430ArchSpec::~ArchSpec()
431{
432}
433
Greg Clayton64195a22011-02-23 00:35:02 +0000434//===----------------------------------------------------------------------===//
435// Assignment and initialization.
436
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000437const ArchSpec&
438ArchSpec::operator= (const ArchSpec& rhs)
439{
440 if (this != &rhs)
441 {
Greg Clayton514487e2011-02-15 21:59:32 +0000442 m_triple = rhs.m_triple;
Greg Clayton64195a22011-02-23 00:35:02 +0000443 m_core = rhs.m_core;
Greg Clayton514487e2011-02-15 21:59:32 +0000444 m_byte_order = rhs.m_byte_order;
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000445 m_distribution_id = rhs.m_distribution_id;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000446 }
447 return *this;
448}
449
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000450void
451ArchSpec::Clear()
452{
Greg Clayton514487e2011-02-15 21:59:32 +0000453 m_triple = llvm::Triple();
Greg Clayton64195a22011-02-23 00:35:02 +0000454 m_core = kCore_invalid;
455 m_byte_order = eByteOrderInvalid;
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000456 m_distribution_id.Clear ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000457}
458
Greg Clayton64195a22011-02-23 00:35:02 +0000459//===----------------------------------------------------------------------===//
460// Predicates.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000461
Greg Clayton41f92322010-06-11 03:25:34 +0000462
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000463const char *
Greg Clayton64195a22011-02-23 00:35:02 +0000464ArchSpec::GetArchitectureName () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000465{
Greg Clayton64195a22011-02-23 00:35:02 +0000466 const CoreDefinition *core_def = FindCoreDefinition (m_core);
467 if (core_def)
468 return core_def->name;
469 return "unknown";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000470}
471
Greg Clayton64195a22011-02-23 00:35:02 +0000472uint32_t
473ArchSpec::GetMachOCPUType () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000474{
Greg Clayton64195a22011-02-23 00:35:02 +0000475 const CoreDefinition *core_def = FindCoreDefinition (m_core);
476 if (core_def)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000477 {
Greg Clayton64195a22011-02-23 00:35:02 +0000478 const ArchDefinitionEntry *arch_def = FindArchDefinitionEntry (&g_macho_arch_def, core_def->core);
479 if (arch_def)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000480 {
Greg Clayton64195a22011-02-23 00:35:02 +0000481 return arch_def->cpu;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000482 }
483 }
Greg Clayton64195a22011-02-23 00:35:02 +0000484 return LLDB_INVALID_CPUTYPE;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000485}
486
Greg Clayton64195a22011-02-23 00:35:02 +0000487uint32_t
488ArchSpec::GetMachOCPUSubType () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000489{
Greg Clayton64195a22011-02-23 00:35:02 +0000490 const CoreDefinition *core_def = FindCoreDefinition (m_core);
491 if (core_def)
492 {
493 const ArchDefinitionEntry *arch_def = FindArchDefinitionEntry (&g_macho_arch_def, core_def->core);
494 if (arch_def)
495 {
Greg Clayton1cb64962011-03-24 04:28:38 +0000496 return arch_def->sub;
Greg Clayton64195a22011-02-23 00:35:02 +0000497 }
498 }
499 return LLDB_INVALID_CPUTYPE;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000500}
501
Matthew Gardinere77b2942014-09-01 09:06:03 +0000502uint32_t
503ArchSpec::GetDataByteSize () const
504{
505 switch (m_core)
506 {
507 case eCore_kalimba3:
Matthew Gardinerf03e6d842014-09-29 08:02:24 +0000508 return 4;
Matthew Gardinere77b2942014-09-01 09:06:03 +0000509 case eCore_kalimba4:
510 return 1;
511 case eCore_kalimba5:
Matthew Gardinerf03e6d842014-09-29 08:02:24 +0000512 return 4;
Matthew Gardinere77b2942014-09-01 09:06:03 +0000513 default:
514 return 1;
515 }
516 return 1;
517}
518
519uint32_t
520ArchSpec::GetCodeByteSize () const
521{
522 switch (m_core)
523 {
524 case eCore_kalimba3:
525 return 4;
526 case eCore_kalimba4:
527 return 1;
528 case eCore_kalimba5:
529 return 1;
530 default:
531 return 1;
532 }
533 return 1;
534}
535
Greg Clayton64195a22011-02-23 00:35:02 +0000536llvm::Triple::ArchType
537ArchSpec::GetMachine () const
538{
539 const CoreDefinition *core_def = FindCoreDefinition (m_core);
540 if (core_def)
541 return core_def->machine;
542
543 return llvm::Triple::UnknownArch;
544}
545
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000546const ConstString&
547ArchSpec::GetDistributionId () const
548{
549 return m_distribution_id;
550}
551
552void
553ArchSpec::SetDistributionId (const char* distribution_id)
554{
555 m_distribution_id.SetCString (distribution_id);
556}
557
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000558uint32_t
559ArchSpec::GetAddressByteSize() const
560{
Greg Clayton64195a22011-02-23 00:35:02 +0000561 const CoreDefinition *core_def = FindCoreDefinition (m_core);
562 if (core_def)
563 return core_def->addr_byte_size;
Greg Clayton41f92322010-06-11 03:25:34 +0000564 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000565}
566
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000567ByteOrder
568ArchSpec::GetDefaultEndian () const
569{
Greg Clayton64195a22011-02-23 00:35:02 +0000570 const CoreDefinition *core_def = FindCoreDefinition (m_core);
571 if (core_def)
572 return core_def->default_byte_order;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000573 return eByteOrderInvalid;
574}
575
Greg Clayton64195a22011-02-23 00:35:02 +0000576lldb::ByteOrder
577ArchSpec::GetByteOrder () const
578{
579 if (m_byte_order == eByteOrderInvalid)
580 return GetDefaultEndian();
581 return m_byte_order;
582}
583
584//===----------------------------------------------------------------------===//
585// Mutators.
586
587bool
588ArchSpec::SetTriple (const llvm::Triple &triple)
589{
590 m_triple = triple;
591
592 llvm::StringRef arch_name (m_triple.getArchName());
593 const CoreDefinition *core_def = FindCoreDefinition (arch_name);
594 if (core_def)
595 {
596 m_core = core_def->core;
Greg Claytoneb0103f2011-04-07 22:46:35 +0000597 // Set the byte order to the default byte order for an architecture.
598 // This can be modified if needed for cases when cores handle both
599 // big and little endian
600 m_byte_order = core_def->default_byte_order;
Greg Clayton64195a22011-02-23 00:35:02 +0000601 }
602 else
603 {
604 Clear();
605 }
606
607
608 return IsValid();
609}
610
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000611static bool
612ParseMachCPUDashSubtypeTriple (const char *triple_cstr, ArchSpec &arch)
613{
614 // Accept "12-10" or "12.10" as cpu type/subtype
615 if (isdigit(triple_cstr[0]))
616 {
617 char *end = NULL;
618 errno = 0;
Greg Claytonc7bece562013-01-25 18:06:21 +0000619 uint32_t cpu = (uint32_t)::strtoul (triple_cstr, &end, 0);
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000620 if (errno == 0 && cpu != 0 && end && ((*end == '-') || (*end == '.')))
621 {
622 errno = 0;
Greg Claytonc7bece562013-01-25 18:06:21 +0000623 uint32_t sub = (uint32_t)::strtoul (end + 1, &end, 0);
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000624 if (errno == 0 && end && ((*end == '-') || (*end == '.') || (*end == '\0')))
625 {
626 if (arch.SetArchitecture (eArchTypeMachO, cpu, sub))
627 {
628 if (*end == '-')
629 {
630 llvm::StringRef vendor_os (end + 1);
631 size_t dash_pos = vendor_os.find('-');
632 if (dash_pos != llvm::StringRef::npos)
633 {
634 llvm::StringRef vendor_str(vendor_os.substr(0, dash_pos));
635 arch.GetTriple().setVendorName(vendor_str);
636 const size_t vendor_start_pos = dash_pos+1;
Greg Claytonc7bece562013-01-25 18:06:21 +0000637 dash_pos = vendor_os.find('-', vendor_start_pos);
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000638 if (dash_pos == llvm::StringRef::npos)
639 {
640 if (vendor_start_pos < vendor_os.size())
641 arch.GetTriple().setOSName(vendor_os.substr(vendor_start_pos));
642 }
643 else
644 {
645 arch.GetTriple().setOSName(vendor_os.substr(vendor_start_pos, dash_pos - vendor_start_pos));
646 }
647 }
648 }
649 return true;
650 }
651 }
652 }
653 }
654 return false;
655}
Greg Clayton64195a22011-02-23 00:35:02 +0000656bool
Greg Clayton70512312012-05-08 01:45:38 +0000657ArchSpec::SetTriple (const char *triple_cstr)
Greg Clayton64195a22011-02-23 00:35:02 +0000658{
Greg Clayton23aca092011-08-12 23:32:52 +0000659 if (triple_cstr && triple_cstr[0])
Greg Clayton64195a22011-02-23 00:35:02 +0000660 {
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000661 if (ParseMachCPUDashSubtypeTriple (triple_cstr, *this))
662 return true;
663
Greg Clayton64195a22011-02-23 00:35:02 +0000664 llvm::StringRef triple_stref (triple_cstr);
665 if (triple_stref.startswith (LLDB_ARCH_DEFAULT))
666 {
667 // Special case for the current host default architectures...
668 if (triple_stref.equals (LLDB_ARCH_DEFAULT_32BIT))
Zachary Turner13b18262014-08-20 16:42:51 +0000669 *this = HostInfo::GetArchitecture(HostInfo::eArchKind32);
Greg Clayton64195a22011-02-23 00:35:02 +0000670 else if (triple_stref.equals (LLDB_ARCH_DEFAULT_64BIT))
Zachary Turner13b18262014-08-20 16:42:51 +0000671 *this = HostInfo::GetArchitecture(HostInfo::eArchKind64);
Greg Clayton64195a22011-02-23 00:35:02 +0000672 else if (triple_stref.equals (LLDB_ARCH_DEFAULT))
Zachary Turner13b18262014-08-20 16:42:51 +0000673 *this = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
Greg Clayton64195a22011-02-23 00:35:02 +0000674 }
675 else
676 {
677 std::string normalized_triple_sstr (llvm::Triple::normalize(triple_stref));
678 triple_stref = normalized_triple_sstr;
Greg Clayton70512312012-05-08 01:45:38 +0000679 SetTriple (llvm::Triple (triple_stref));
680 }
681 }
682 else
683 Clear();
684 return IsValid();
685}
686
687bool
688ArchSpec::SetTriple (const char *triple_cstr, Platform *platform)
689{
690 if (triple_cstr && triple_cstr[0])
691 {
Greg Clayton9e6cffc2012-09-19 22:25:17 +0000692 if (ParseMachCPUDashSubtypeTriple (triple_cstr, *this))
693 return true;
694
Greg Clayton70512312012-05-08 01:45:38 +0000695 llvm::StringRef triple_stref (triple_cstr);
696 if (triple_stref.startswith (LLDB_ARCH_DEFAULT))
697 {
698 // Special case for the current host default architectures...
699 if (triple_stref.equals (LLDB_ARCH_DEFAULT_32BIT))
Zachary Turner13b18262014-08-20 16:42:51 +0000700 *this = HostInfo::GetArchitecture(HostInfo::eArchKind32);
Greg Clayton70512312012-05-08 01:45:38 +0000701 else if (triple_stref.equals (LLDB_ARCH_DEFAULT_64BIT))
Zachary Turner13b18262014-08-20 16:42:51 +0000702 *this = HostInfo::GetArchitecture(HostInfo::eArchKind64);
Greg Clayton70512312012-05-08 01:45:38 +0000703 else if (triple_stref.equals (LLDB_ARCH_DEFAULT))
Zachary Turner13b18262014-08-20 16:42:51 +0000704 *this = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
Greg Clayton70512312012-05-08 01:45:38 +0000705 }
706 else
707 {
708 ArchSpec raw_arch (triple_cstr);
709
710 std::string normalized_triple_sstr (llvm::Triple::normalize(triple_stref));
711 triple_stref = normalized_triple_sstr;
Greg Claytoneb0103f2011-04-07 22:46:35 +0000712 llvm::Triple normalized_triple (triple_stref);
713
714 const bool os_specified = normalized_triple.getOSName().size() > 0;
715 const bool vendor_specified = normalized_triple.getVendorName().size() > 0;
716 const bool env_specified = normalized_triple.getEnvironmentName().size() > 0;
717
718 // If we got an arch only, then default the vendor, os, environment
719 // to match the platform if one is supplied
720 if (!(os_specified || vendor_specified || env_specified))
721 {
722 if (platform)
723 {
724 // If we were given a platform, use the platform's system
725 // architecture. If this is not available (might not be
726 // connected) use the first supported architecture.
Greg Clayton70512312012-05-08 01:45:38 +0000727 ArchSpec compatible_arch;
Greg Clayton1e0c8842013-01-11 20:49:54 +0000728 if (platform->IsCompatibleArchitecture (raw_arch, false, &compatible_arch))
Greg Claytoneb0103f2011-04-07 22:46:35 +0000729 {
Greg Clayton70512312012-05-08 01:45:38 +0000730 if (compatible_arch.IsValid())
731 {
732 const llvm::Triple &compatible_triple = compatible_arch.GetTriple();
733 if (!vendor_specified)
734 normalized_triple.setVendor(compatible_triple.getVendor());
735 if (!os_specified)
736 normalized_triple.setOS(compatible_triple.getOS());
737 if (!env_specified && compatible_triple.getEnvironmentName().size())
738 normalized_triple.setEnvironment(compatible_triple.getEnvironment());
739 }
Greg Claytoneb0103f2011-04-07 22:46:35 +0000740 }
Greg Clayton70512312012-05-08 01:45:38 +0000741 else
Greg Claytoneb0103f2011-04-07 22:46:35 +0000742 {
Greg Clayton70512312012-05-08 01:45:38 +0000743 *this = raw_arch;
744 return IsValid();
Greg Claytoneb0103f2011-04-07 22:46:35 +0000745 }
746 }
747 else
748 {
749 // No platform specified, fall back to the host system for
750 // the default vendor, os, and environment.
Sean Callananbfb237bc2011-11-04 22:46:46 +0000751 llvm::Triple host_triple(llvm::sys::getDefaultTargetTriple());
Greg Clayton70512312012-05-08 01:45:38 +0000752 if (!vendor_specified)
753 normalized_triple.setVendor(host_triple.getVendor());
754 if (!vendor_specified)
755 normalized_triple.setOS(host_triple.getOS());
756 if (!env_specified && host_triple.getEnvironmentName().size())
757 normalized_triple.setEnvironment(host_triple.getEnvironment());
Greg Claytoneb0103f2011-04-07 22:46:35 +0000758 }
759 }
760 SetTriple (normalized_triple);
Greg Clayton64195a22011-02-23 00:35:02 +0000761 }
762 }
763 else
764 Clear();
765 return IsValid();
766}
767
Zachary Turner5e6f4522015-01-22 18:59:05 +0000768void
769ArchSpec::MergeFrom(const ArchSpec &other)
770{
771 if (GetTriple().getVendor() == llvm::Triple::UnknownVendor && !TripleVendorWasSpecified())
772 GetTriple().setVendor(other.GetTriple().getVendor());
773 if (GetTriple().getOS() == llvm::Triple::UnknownOS && !TripleOSWasSpecified())
774 GetTriple().setOS(other.GetTriple().getOS());
775 if (GetTriple().getArch() == llvm::Triple::UnknownArch)
776 GetTriple().setArch(other.GetTriple().getArch());
777 if (GetTriple().getEnvironment() == llvm::Triple::UnknownEnvironment)
778 GetTriple().setEnvironment(other.GetTriple().getEnvironment());
779}
780
Greg Clayton64195a22011-02-23 00:35:02 +0000781bool
Greg Claytone0d378b2011-03-24 21:19:54 +0000782ArchSpec::SetArchitecture (ArchitectureType arch_type, uint32_t cpu, uint32_t sub)
Greg Clayton64195a22011-02-23 00:35:02 +0000783{
784 m_core = kCore_invalid;
785 bool update_triple = true;
786 const ArchDefinition *arch_def = FindArchDefinition(arch_type);
787 if (arch_def)
788 {
789 const ArchDefinitionEntry *arch_def_entry = FindArchDefinitionEntry (arch_def, cpu, sub);
790 if (arch_def_entry)
791 {
792 const CoreDefinition *core_def = FindCoreDefinition (arch_def_entry->core);
793 if (core_def)
794 {
795 m_core = core_def->core;
796 update_triple = false;
Greg Clayton593577a2011-09-21 03:57:31 +0000797 // Always use the architecture name because it might be more descriptive
798 // than the architecture enum ("armv7" -> llvm::Triple::arm).
799 m_triple.setArchName(llvm::StringRef(core_def->name));
Greg Clayton64195a22011-02-23 00:35:02 +0000800 if (arch_type == eArchTypeMachO)
801 {
802 m_triple.setVendor (llvm::Triple::Apple);
Greg Clayton70512312012-05-08 01:45:38 +0000803
804 switch (core_def->machine)
805 {
Todd Fialad8eaa172014-07-23 14:37:35 +0000806 case llvm::Triple::aarch64:
Greg Clayton70512312012-05-08 01:45:38 +0000807 case llvm::Triple::arm:
808 case llvm::Triple::thumb:
809 m_triple.setOS (llvm::Triple::IOS);
810 break;
811
812 case llvm::Triple::x86:
813 case llvm::Triple::x86_64:
Greg Claytona3a6c122014-07-29 18:04:57 +0000814 // Don't set the OS for x86_64 or for x86 as we want to leave it as an "unspecified unknown"
815 // which means if we ask for the OS from the llvm::Triple we get back llvm::Triple::UnknownOS, but
816 // if we ask for the string value for the OS it will come back empty (unspecified).
817 // We do this because we now have iOS and MacOSX as the OS values for x86 and x86_64 for
818 // normal desktop and simulator binaries. And if we compare a "x86_64-apple-ios" to a "x86_64-apple-"
819 // triple, it will say it is compatible (because the OS is unspecified in the second one and will match
820 // anything in the first
821 break;
822
Greg Clayton70512312012-05-08 01:45:38 +0000823 default:
824 m_triple.setOS (llvm::Triple::MacOSX);
825 break;
826 }
Greg Clayton64195a22011-02-23 00:35:02 +0000827 }
828 else
829 {
830 m_triple.setVendor (llvm::Triple::UnknownVendor);
831 m_triple.setOS (llvm::Triple::UnknownOS);
832 }
Greg Clayton593577a2011-09-21 03:57:31 +0000833 // Fall back onto setting the machine type if the arch by name failed...
834 if (m_triple.getArch () == llvm::Triple::UnknownArch)
835 m_triple.setArch (core_def->machine);
Greg Clayton64195a22011-02-23 00:35:02 +0000836 }
837 }
838 }
839 CoreUpdated(update_triple);
840 return IsValid();
841}
842
Greg Clayton357132e2011-03-26 19:14:58 +0000843uint32_t
844ArchSpec::GetMinimumOpcodeByteSize() const
Greg Clayton64195a22011-02-23 00:35:02 +0000845{
Greg Clayton357132e2011-03-26 19:14:58 +0000846 const CoreDefinition *core_def = FindCoreDefinition (m_core);
847 if (core_def)
848 return core_def->min_opcode_byte_size;
849 return 0;
850}
851
852uint32_t
853ArchSpec::GetMaximumOpcodeByteSize() const
854{
855 const CoreDefinition *core_def = FindCoreDefinition (m_core);
856 if (core_def)
857 return core_def->max_opcode_byte_size;
858 return 0;
Greg Clayton64195a22011-02-23 00:35:02 +0000859}
860
Jason Molendaba813dc2012-11-04 03:20:05 +0000861bool
862ArchSpec::IsExactMatch (const ArchSpec& rhs) const
863{
Sean Callananbf4b7be2012-12-13 22:07:14 +0000864 return IsEqualTo (rhs, true);
Jason Molendaba813dc2012-11-04 03:20:05 +0000865}
866
867bool
868ArchSpec::IsCompatibleMatch (const ArchSpec& rhs) const
869{
Sean Callananbf4b7be2012-12-13 22:07:14 +0000870 return IsEqualTo (rhs, false);
Jason Molendaba813dc2012-11-04 03:20:05 +0000871}
872
873bool
Sean Callananbf4b7be2012-12-13 22:07:14 +0000874ArchSpec::IsEqualTo (const ArchSpec& rhs, bool exact_match) const
Jason Molendaba813dc2012-11-04 03:20:05 +0000875{
Todd Fialaa9ddb0e2014-01-18 03:02:39 +0000876 // explicitly ignoring m_distribution_id in this method.
877
Jason Molendaba813dc2012-11-04 03:20:05 +0000878 if (GetByteOrder() != rhs.GetByteOrder())
879 return false;
880
881 const ArchSpec::Core lhs_core = GetCore ();
882 const ArchSpec::Core rhs_core = rhs.GetCore ();
883
884 const bool core_match = cores_match (lhs_core, rhs_core, true, exact_match);
885
886 if (core_match)
887 {
888 const llvm::Triple &lhs_triple = GetTriple();
889 const llvm::Triple &rhs_triple = rhs.GetTriple();
890
891 const llvm::Triple::VendorType lhs_triple_vendor = lhs_triple.getVendor();
892 const llvm::Triple::VendorType rhs_triple_vendor = rhs_triple.getVendor();
893 if (lhs_triple_vendor != rhs_triple_vendor)
894 {
Sean Callananbf4b7be2012-12-13 22:07:14 +0000895 if (exact_match)
896 {
897 const bool rhs_vendor_specified = rhs.TripleVendorWasSpecified();
898 const bool lhs_vendor_specified = TripleVendorWasSpecified();
899 // Both architectures had the vendor specified, so if they aren't
900 // equal then we return false
901 if (rhs_vendor_specified && lhs_vendor_specified)
902 return false;
903 }
Jason Molendaba813dc2012-11-04 03:20:05 +0000904
905 // Only fail if both vendor types are not unknown
906 if (lhs_triple_vendor != llvm::Triple::UnknownVendor &&
907 rhs_triple_vendor != llvm::Triple::UnknownVendor)
908 return false;
909 }
910
911 const llvm::Triple::OSType lhs_triple_os = lhs_triple.getOS();
912 const llvm::Triple::OSType rhs_triple_os = rhs_triple.getOS();
913 if (lhs_triple_os != rhs_triple_os)
914 {
Sean Callananbf4b7be2012-12-13 22:07:14 +0000915 if (exact_match)
916 {
917 const bool rhs_os_specified = rhs.TripleOSWasSpecified();
918 const bool lhs_os_specified = TripleOSWasSpecified();
919 // Both architectures had the OS specified, so if they aren't
920 // equal then we return false
921 if (rhs_os_specified && lhs_os_specified)
922 return false;
923 }
Greg Clayton7ab7f892014-05-29 21:33:45 +0000924
Greg Clayton3f19ada2014-07-10 23:33:37 +0000925 // Only fail if both os types are not unknown
926 if (lhs_triple_os != llvm::Triple::UnknownOS &&
927 rhs_triple_os != llvm::Triple::UnknownOS)
928 return false;
Jason Molendaba813dc2012-11-04 03:20:05 +0000929 }
930
931 const llvm::Triple::EnvironmentType lhs_triple_env = lhs_triple.getEnvironment();
932 const llvm::Triple::EnvironmentType rhs_triple_env = rhs_triple.getEnvironment();
933
934 if (lhs_triple_env != rhs_triple_env)
935 {
936 // Only fail if both environment types are not unknown
937 if (lhs_triple_env != llvm::Triple::UnknownEnvironment &&
938 rhs_triple_env != llvm::Triple::UnknownEnvironment)
939 return false;
940 }
941 return true;
942 }
943 return false;
944}
945
Greg Clayton64195a22011-02-23 00:35:02 +0000946//===----------------------------------------------------------------------===//
947// Helper methods.
948
949void
950ArchSpec::CoreUpdated (bool update_triple)
951{
952 const CoreDefinition *core_def = FindCoreDefinition (m_core);
953 if (core_def)
954 {
955 if (update_triple)
956 m_triple = llvm::Triple(core_def->name, "unknown", "unknown");
957 m_byte_order = core_def->default_byte_order;
958 }
959 else
960 {
961 if (update_triple)
962 m_triple = llvm::Triple();
963 m_byte_order = eByteOrderInvalid;
964 }
965}
966
967//===----------------------------------------------------------------------===//
968// Operators.
969
Greg Clayton70512312012-05-08 01:45:38 +0000970static bool
Jason Molendaba813dc2012-11-04 03:20:05 +0000971cores_match (const ArchSpec::Core core1, const ArchSpec::Core core2, bool try_inverse, bool enforce_exact_match)
Greg Clayton70512312012-05-08 01:45:38 +0000972{
Jason Molendaba813dc2012-11-04 03:20:05 +0000973 if (core1 == core2)
974 return true;
975
Greg Clayton70512312012-05-08 01:45:38 +0000976 switch (core1)
977 {
Greg Clayton70512312012-05-08 01:45:38 +0000978 case ArchSpec::kCore_any:
979 return true;
980
Greg Clayton44362e02014-07-12 00:11:34 +0000981 case ArchSpec::eCore_arm_generic:
982 if (enforce_exact_match)
983 break;
984 // Fall through to case below
Greg Clayton70512312012-05-08 01:45:38 +0000985 case ArchSpec::kCore_arm_any:
986 if (core2 >= ArchSpec::kCore_arm_first && core2 <= ArchSpec::kCore_arm_last)
987 return true;
988 if (core2 >= ArchSpec::kCore_thumb_first && core2 <= ArchSpec::kCore_thumb_last)
989 return true;
990 if (core2 == ArchSpec::kCore_arm_any)
991 return true;
992 break;
Sylvestre Ledru1c9e0642014-08-18 14:53:42 +0000993
Greg Clayton70512312012-05-08 01:45:38 +0000994 case ArchSpec::kCore_x86_32_any:
995 if ((core2 >= ArchSpec::kCore_x86_32_first && core2 <= ArchSpec::kCore_x86_32_last) || (core2 == ArchSpec::kCore_x86_32_any))
996 return true;
997 break;
Zachary Turnerad587ae42014-07-28 16:44:49 +0000998
999 case ArchSpec::kCore_x86_64_any:
1000 if ((core2 >= ArchSpec::kCore_x86_64_first && core2 <= ArchSpec::kCore_x86_64_last) || (core2 == ArchSpec::kCore_x86_64_any))
1001 return true;
Sylvestre Ledru1c9e0642014-08-18 14:53:42 +00001002 break;
Zachary Turnerad587ae42014-07-28 16:44:49 +00001003
Greg Clayton70512312012-05-08 01:45:38 +00001004 case ArchSpec::kCore_ppc_any:
1005 if ((core2 >= ArchSpec::kCore_ppc_first && core2 <= ArchSpec::kCore_ppc_last) || (core2 == ArchSpec::kCore_ppc_any))
1006 return true;
1007 break;
Sylvestre Ledru1c9e0642014-08-18 14:53:42 +00001008
Greg Clayton70512312012-05-08 01:45:38 +00001009 case ArchSpec::kCore_ppc64_any:
1010 if ((core2 >= ArchSpec::kCore_ppc64_first && core2 <= ArchSpec::kCore_ppc64_last) || (core2 == ArchSpec::kCore_ppc64_any))
1011 return true;
1012 break;
1013
Jason Molendaa3a04522013-09-27 23:21:54 +00001014 case ArchSpec::eCore_arm_armv6m:
1015 if (!enforce_exact_match)
1016 {
Greg Clayton44362e02014-07-12 00:11:34 +00001017 if (core2 == ArchSpec::eCore_arm_generic)
1018 return true;
Jason Molendaa3a04522013-09-27 23:21:54 +00001019 try_inverse = false;
Jason Molendac7cda272013-09-27 23:29:10 +00001020 if (core2 == ArchSpec::eCore_arm_armv7)
Jason Molendaa3a04522013-09-27 23:21:54 +00001021 return true;
1022 }
Sylvestre Ledru1c9e0642014-08-18 14:53:42 +00001023 break;
Deepak Panickal6d3df422014-02-19 11:16:46 +00001024
1025 case ArchSpec::kCore_hexagon_any:
1026 if ((core2 >= ArchSpec::kCore_hexagon_first && core2 <= ArchSpec::kCore_hexagon_last) || (core2 == ArchSpec::kCore_hexagon_any))
1027 return true;
Jason Molendaa3a04522013-09-27 23:21:54 +00001028 break;
1029
Jason Molenda7a1559c2013-03-08 01:20:17 +00001030 case ArchSpec::eCore_arm_armv7m:
1031 case ArchSpec::eCore_arm_armv7em:
Johnny Chen1083b0d2012-08-28 22:53:40 +00001032 case ArchSpec::eCore_arm_armv7f:
1033 case ArchSpec::eCore_arm_armv7k:
1034 case ArchSpec::eCore_arm_armv7s:
Jason Molendaba813dc2012-11-04 03:20:05 +00001035 if (!enforce_exact_match)
1036 {
Greg Clayton44362e02014-07-12 00:11:34 +00001037 if (core2 == ArchSpec::eCore_arm_generic)
1038 return true;
Jason Molendaba813dc2012-11-04 03:20:05 +00001039 if (core2 == ArchSpec::eCore_arm_armv7)
1040 return true;
Greg Clayton44362e02014-07-12 00:11:34 +00001041 try_inverse = false;
Jason Molendaba813dc2012-11-04 03:20:05 +00001042 }
Johnny Chen1083b0d2012-08-28 22:53:40 +00001043 break;
Sylvestre Ledru1c9e0642014-08-18 14:53:42 +00001044
Greg Clayton52edb362014-07-14 22:53:02 +00001045 case ArchSpec::eCore_x86_64_x86_64h:
1046 if (!enforce_exact_match)
1047 {
1048 try_inverse = false;
1049 if (core2 == ArchSpec::eCore_x86_64_x86_64)
1050 return true;
1051 }
1052 break;
Johnny Chen1083b0d2012-08-28 22:53:40 +00001053
Todd Fiala02e71812014-08-28 14:32:43 +00001054 case ArchSpec::eCore_arm_armv8:
1055 if (!enforce_exact_match)
1056 {
1057 if (core2 == ArchSpec::eCore_arm_arm64)
1058 return true;
1059 if (core2 == ArchSpec::eCore_arm_aarch64)
1060 return true;
1061 try_inverse = false;
1062 }
1063 break;
1064
1065 case ArchSpec::eCore_arm_aarch64:
1066 if (!enforce_exact_match)
1067 {
1068 if (core2 == ArchSpec::eCore_arm_arm64)
1069 return true;
1070 if (core2 == ArchSpec::eCore_arm_armv8)
1071 return true;
1072 try_inverse = false;
1073 }
1074 break;
1075
1076 case ArchSpec::eCore_arm_arm64:
1077 if (!enforce_exact_match)
1078 {
1079 if (core2 == ArchSpec::eCore_arm_aarch64)
1080 return true;
1081 if (core2 == ArchSpec::eCore_arm_armv8)
1082 return true;
1083 try_inverse = false;
1084 }
1085 break;
1086
Greg Clayton70512312012-05-08 01:45:38 +00001087 default:
1088 break;
1089 }
1090 if (try_inverse)
Jason Molendaba813dc2012-11-04 03:20:05 +00001091 return cores_match (core2, core1, false, enforce_exact_match);
Greg Clayton70512312012-05-08 01:45:38 +00001092 return false;
1093}
1094
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001095bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001096lldb_private::operator<(const ArchSpec& lhs, const ArchSpec& rhs)
1097{
Greg Clayton64195a22011-02-23 00:35:02 +00001098 const ArchSpec::Core lhs_core = lhs.GetCore ();
1099 const ArchSpec::Core rhs_core = rhs.GetCore ();
1100 return lhs_core < rhs_core;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001101}
Greg Claytona97c4d22014-12-09 23:31:02 +00001102
1103static void
1104StopInfoOverrideCallbackTypeARM(lldb_private::Thread &thread)
1105{
1106 // We need to check if we are stopped in Thumb mode in a IT instruction
1107 // and detect if the condition doesn't pass. If this is the case it means
1108 // we won't actually execute this instruction. If this happens we need to
1109 // clear the stop reason to no thread plans think we are stopped for a
1110 // reason and the plans should keep going.
1111 //
1112 // We do this because when single stepping many ARM processes, debuggers
1113 // often use the BVR/BCR registers that says "stop when the PC is not
1114 // equal to its current value". This method of stepping means we can end
1115 // up stopping on instructions inside an if/then block that wouldn't get
1116 // executed. By fixing this we can stop the debugger from seeming like
1117 // you stepped through both the "if" _and_ the "else" clause when source
1118 // level stepping because the debugger stops regardless due to the BVR/BCR
1119 // triggering a stop.
1120 //
1121 // It also means we can set breakpoints on instructions inside an an
1122 // if/then block and correctly skip them if we use the BKPT instruction.
1123 // The ARM and Thumb BKPT instructions are unconditional even when executed
1124 // in a Thumb IT block.
1125 //
1126 // If your debugger inserts software traps in ARM/Thumb code, it will
1127 // need to use 16 and 32 bit instruction for 16 and 32 bit thumb
1128 // instructions respectively. If your debugger inserts a 16 bit thumb
1129 // trap on top of a 32 bit thumb instruction for an opcode that is inside
1130 // an if/then, it will change the it/then to conditionally execute your
1131 // 16 bit trap and then cause your program to crash if it executes the
1132 // trailing 16 bits (the second half of the 32 bit thumb instruction you
1133 // partially overwrote).
1134
1135 RegisterContextSP reg_ctx_sp (thread.GetRegisterContext());
1136 if (reg_ctx_sp)
1137 {
1138 const uint32_t cpsr = reg_ctx_sp->GetFlags(0);
1139 if (cpsr != 0)
1140 {
1141 // Read the J and T bits to get the ISETSTATE
1142 const uint32_t J = Bit32(cpsr, 24);
1143 const uint32_t T = Bit32(cpsr, 5);
1144 const uint32_t ISETSTATE = J << 1 | T;
1145 if (ISETSTATE == 0)
1146 {
1147 // NOTE: I am pretty sure we want to enable the code below
1148 // that detects when we stop on an instruction in ARM mode
1149 // that is conditional and the condition doesn't pass. This
1150 // can happen if you set a breakpoint on an instruction that
1151 // is conditional. We currently will _always_ stop on the
1152 // instruction which is bad. You can also run into this while
1153 // single stepping and you could appear to run code in the "if"
1154 // and in the "else" clause because it would stop at all of the
1155 // conditional instructions in both.
1156 // In such cases, we really don't want to stop at this location.
1157 // I will check with the lldb-dev list first before I enable this.
1158#if 0
1159 // ARM mode: check for condition on intsruction
1160 const addr_t pc = reg_ctx_sp->GetPC();
1161 Error error;
1162 // If we fail to read the opcode we will get UINT64_MAX as the
1163 // result in "opcode" which we can use to detect if we read a
1164 // valid opcode.
1165 const uint64_t opcode = thread.GetProcess()->ReadUnsignedIntegerFromMemory(pc, 4, UINT64_MAX, error);
1166 if (opcode <= UINT32_MAX)
1167 {
1168 const uint32_t condition = Bits32((uint32_t)opcode, 31, 28);
1169 if (ARMConditionPassed(condition, cpsr) == false)
1170 {
1171 // We ARE stopped on an ARM instruction whose condition doesn't
1172 // pass so this instruction won't get executed.
1173 // Regardless of why it stopped, we need to clear the stop info
1174 thread.SetStopInfo (StopInfoSP());
1175 }
1176 }
1177#endif
1178 }
1179 else if (ISETSTATE == 1)
1180 {
1181 // Thumb mode
1182 const uint32_t ITSTATE = Bits32 (cpsr, 15, 10) << 2 | Bits32 (cpsr, 26, 25);
1183 if (ITSTATE != 0)
1184 {
1185 const uint32_t condition = Bits32(ITSTATE, 7, 4);
1186 if (ARMConditionPassed(condition, cpsr) == false)
1187 {
1188 // We ARE stopped in a Thumb IT instruction on an instruction whose
1189 // condition doesn't pass so this instruction won't get executed.
1190 // Regardless of why it stopped, we need to clear the stop info
1191 thread.SetStopInfo (StopInfoSP());
1192 }
1193 }
1194 }
1195 }
1196 }
1197}
1198
1199ArchSpec::StopInfoOverrideCallbackType
1200ArchSpec::GetStopInfoOverrideCallback () const
1201{
1202 const llvm::Triple::ArchType machine = GetMachine();
1203 if (machine == llvm::Triple::arm)
1204 return StopInfoOverrideCallbackTypeARM;
1205 return NULL;
1206}