ArchSpec.h revision 0f577c2ab79604f7300ac66afebcaa4a4b2dceef
1//===-- ArchSpec.h ----------------------------------------------*- 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#ifndef liblldb_ArchSpec_h_ 11#define liblldb_ArchSpec_h_ 12 13#if defined(__cplusplus) 14 15#include "lldb/lldb-private.h" 16 17namespace lldb_private { 18 19//---------------------------------------------------------------------- 20/// @class ArchSpec ArchSpec.h "lldb/Core/ArchSpec.h" 21/// @brief An architecture specification class. 22/// 23/// A class designed to be created from a cpu type and subtype, or a 24/// string representation. Keeping all of the conversions of strings 25/// to architecture enumeration values confined to this class allows 26/// new architecture support to be added easily. 27//---------------------------------------------------------------------- 28class ArchSpec 29{ 30public: 31 // Generic CPU types that each m_type needs to know how to convert 32 // their m_cpu and m_sub to. 33 enum CPU 34 { 35 eCPU_Unknown, 36 eCPU_arm, 37 eCPU_i386, 38 eCPU_x86_64, 39 eCPU_ppc, 40 eCPU_ppc64, 41 eCPU_sparc 42 }; 43 44 //------------------------------------------------------------------ 45 /// Default constructor. 46 /// 47 /// Default constructor that initializes the object with invalid 48 /// cpu type and subtype values. 49 //------------------------------------------------------------------ 50 ArchSpec (); 51 52 //------------------------------------------------------------------ 53 /// Constructor with cpu type and subtype. 54 /// 55 /// Constructor that initializes the object with supplied cpu and 56 /// subtypes. 57 //------------------------------------------------------------------ 58 ArchSpec (lldb::ArchitectureType arch_type, uint32_t cpu, uint32_t sub); 59 60 //------------------------------------------------------------------ 61 /// Construct with architecture name. 62 /// 63 /// Constructor that initializes the object with supplied 64 /// architecture name. There are also predefined values in 65 /// Defines.h: 66 /// @li \c LLDB_ARCH_DEFAULT 67 /// The arch the current system defaults to when a program is 68 /// launched without any extra attributes or settings. 69 /// 70 /// @li \c LLDB_ARCH_DEFAULT_32BIT 71 /// The 32 bit arch the current system defaults to (if any) 72 /// 73 /// @li \c LLDB_ARCH_DEFAULT_32BIT 74 /// The 64 bit arch the current system defaults to (if any) 75 //------------------------------------------------------------------ 76 ArchSpec (const char *arch_name); 77 78 //------------------------------------------------------------------ 79 /// Destructor. 80 /// 81 /// The destructor is virtual in case this class is subclassed. 82 //------------------------------------------------------------------ 83 virtual 84 ~ArchSpec (); 85 86 //------------------------------------------------------------------ 87 /// Assignment operator. 88 /// 89 /// @param[in] rhs another ArchSpec object to copy. 90 /// 91 /// @return a const reference to this object 92 //------------------------------------------------------------------ 93 const ArchSpec& 94 operator= (const ArchSpec& rhs); 95 96 //------------------------------------------------------------------ 97 /// Get a string representation of the contained architecture. 98 /// 99 /// Gets a C string representation of the current architecture. 100 /// If the returned string is a valid architecture name, the string 101 /// came from a constant string values that do not need to be freed. 102 /// If the returned string uses the "N.M" format, the string comes 103 /// from a static buffer that should be copied. 104 /// 105 /// @return a NULL terminated C string that does not need to be 106 /// freed. 107 //------------------------------------------------------------------ 108 const char * 109 AsCString () const; 110 111 //------------------------------------------------------------------ 112 /// Returns a string representation of the supplied architecture. 113 /// 114 /// Class function to get a C string representation given a CPU type 115 /// and subtype. 116 /// 117 /// @param[in] cpu The cpu type of the architecture. 118 /// @param[in] subtype The cpu subtype of the architecture. 119 /// 120 /// @return a NULL terminated C string that does not need to be 121 /// freed. 122 //------------------------------------------------------------------ 123 static const char * 124 AsCString (lldb::ArchitectureType arch_type, uint32_t cpu, uint32_t subtype); 125 126 //------------------------------------------------------------------ 127 /// Clears the object state. 128 /// 129 /// Clears the object state back to a default invalid state. 130 //------------------------------------------------------------------ 131 void 132 Clear (); 133 134 //------------------------------------------------------------------ 135 /// Returns the size in bytes of an address of the current 136 /// architecture. 137 /// 138 /// @return The byte size of an address of the current architecture. 139 //------------------------------------------------------------------ 140 uint32_t 141 GetAddressByteSize () const; 142 143 144 CPU 145 GetGenericCPUType () const; 146 147 //------------------------------------------------------------------ 148 /// CPU subtype get accessor. 149 /// 150 /// @return The current value of the CPU subtype. 151 //------------------------------------------------------------------ 152 uint32_t 153 GetCPUSubtype () const; 154 155 //------------------------------------------------------------------ 156 /// CPU type get accessor. 157 /// 158 /// @return The current value of the CPU type. 159 //------------------------------------------------------------------ 160 uint32_t 161 GetCPUType () const; 162 163 //------------------------------------------------------------------ 164 /// Feature flags get accessor. 165 /// 166 /// @return The current value of the CPU feature flags. 167 //------------------------------------------------------------------ 168 uint32_t 169 GetFeatureFlags () const; 170 171 //------------------------------------------------------------------ 172 /// Get register names of the current architecture. 173 /// 174 /// Get register names of the current architecture given 175 /// a register number, and a flavor for that register number. 176 /// There are many different register numbering schemes used 177 /// on a host: 178 /// @li \c eRegisterKindGCC - gcc compiler register numbering 179 /// @li \c eRegisterKindDWARF - DWARF register numbering 180 /// 181 /// @param[in] reg_num The register number to decode. 182 /// @param[in] flavor The flavor of the \a reg_num. 183 /// 184 /// @return the name of the register as a NULL terminated C string, 185 /// or /c NULL if the \a reg_num is invalid for \a flavor. 186 /// String values that are returned do not need to be freed. 187 //------------------------------------------------------------------ 188 const char * 189 GetRegisterName (uint32_t reg_num, uint32_t flavor) const; 190 191 //------------------------------------------------------------------ 192 /// Get register names for a specified architecture. 193 /// 194 /// Get register names of the specified architecture given 195 /// a register number, and a flavor for that register number. 196 /// There are many different register numbering schemes used 197 /// on a host: 198 /// 199 /// @li compiler register numbers (@see eRegisterKindGCC) 200 /// @li DWARF register numbers (@see eRegisterKindDWARF) 201 /// 202 /// @param[in] cpu The cpu type of the architecture specific 203 /// register 204 /// @param[in] subtype The cpu subtype of the architecture specific 205 /// register 206 /// @param[in] reg_num The register number to decode. 207 /// @param[in] flavor The flavor of the \a reg_num. 208 /// 209 /// @return the name of the register as a NULL terminated C string, 210 /// or /c NULL if the \a reg_num is invalid for \a flavor. 211 /// String values that are returned do not need to be freed. 212 //------------------------------------------------------------------ 213 static const char * 214 GetRegisterName (lldb::ArchitectureType arch_type, uint32_t cpu, uint32_t subtype, uint32_t reg_num, uint32_t flavor); 215 216 //------------------------------------------------------------------ 217 /// Test if the contained architecture is valid. 218 /// 219 /// @return true if the current architecture is valid, false 220 /// otherwise. 221 //------------------------------------------------------------------ 222 bool 223 IsValid () const; 224 225 //------------------------------------------------------------------ 226 /// Get the memory cost of this object. 227 /// 228 /// @return 229 /// The number of bytes that this object occupies in memory. 230 //------------------------------------------------------------------ 231 size_t 232 MemorySize() const; 233 234 //------------------------------------------------------------------ 235 /// Change the CPU type and subtype given an architecture name. 236 /// 237 /// The architecture name supplied can also by one of the generic 238 /// system default values: 239 /// @li \c LLDB_ARCH_DEFAULT - The arch the current system defaults 240 /// to when a program is launched without any extra 241 /// attributes or settings. 242 /// @li \c LLDB_ARCH_DEFAULT_32BIT - The default host architecture 243 /// for 32 bit (if any). 244 /// @li \c LLDB_ARCH_DEFAULT_64BIT - The default host architecture 245 /// for 64 bit (if any). 246 /// 247 /// @param[in] arch_name The name of an architecture. 248 /// 249 /// @return true if \a arch_name was successfully transformed into 250 /// a valid cpu type and subtype. 251 //------------------------------------------------------------------ 252 bool 253 SetArch (const char *arch_name); 254 255 bool 256 SetArchFromTargetTriple (const char *arch_name); 257 //------------------------------------------------------------------ 258 /// Change the CPU type and subtype given new values of the cpu 259 /// type and subtype. 260 /// 261 /// @param[in] cpu The new CPU type 262 /// @param[in] subtype The new CPU subtype 263 //------------------------------------------------------------------ 264 void 265 SetMachOArch (uint32_t cpu, uint32_t sub) 266 { 267 m_type = lldb::eArchTypeMachO; 268 m_cpu = cpu; 269 m_sub = sub; 270 } 271 272 void 273 SetElfArch (uint32_t cpu, uint32_t sub) 274 { 275 m_type = lldb::eArchTypeELF; 276 m_cpu = cpu; 277 m_sub = sub; 278 } 279 280 //------------------------------------------------------------------ 281 /// Returns the default endianness of the architecture. 282 /// 283 /// @return The endian enumeration for the default endianness of 284 /// the architecture. 285 //------------------------------------------------------------------ 286 lldb::ByteOrder 287 GetDefaultEndian () const; 288 289 290 lldb::ArchitectureType 291 GetType() const 292 { 293 return m_type; 294 } 295 296protected: 297 //------------------------------------------------------------------ 298 // Member variables 299 //------------------------------------------------------------------ 300 lldb::ArchitectureType m_type; 301 // m_type => eArchTypeMachO eArchTypeELF 302 uint32_t m_cpu; // cpu type ELF header e_machine 303 uint32_t m_sub; // cpu subtype nothing 304}; 305 306 307//------------------------------------------------------------------ 308/// @fn bool operator== (const ArchSpec& lhs, const ArchSpec& rhs) 309/// @brief Equal to operator. 310/// 311/// Tests two ArchSpec objects to see if they are equal. 312/// 313/// @param[in] lhs The Left Hand Side ArchSpec object to compare. 314/// @param[in] rhs The Left Hand Side ArchSpec object to compare. 315/// 316/// @return true if \a lhs is equal to \a rhs 317//------------------------------------------------------------------ 318bool operator==(const ArchSpec& lhs, const ArchSpec& rhs); 319 320//------------------------------------------------------------------ 321/// @fn bool operator!= (const ArchSpec& lhs, const ArchSpec& rhs) 322/// @brief Not equal to operator. 323/// 324/// Tests two ArchSpec objects to see if they are not equal. 325/// 326/// @param[in] lhs The Left Hand Side ArchSpec object to compare. 327/// @param[in] rhs The Left Hand Side ArchSpec object to compare. 328/// 329/// @return true if \a lhs is not equal to \a rhs 330//------------------------------------------------------------------ 331bool operator!=(const ArchSpec& lhs, const ArchSpec& rhs); 332 333//------------------------------------------------------------------ 334/// @fn bool operator< (const ArchSpec& lhs, const ArchSpec& rhs) 335/// @brief Less than operator. 336/// 337/// Tests two ArchSpec objects to see if \a lhs is less than \a 338/// rhs. 339/// 340/// @param[in] lhs The Left Hand Side ArchSpec object to compare. 341/// @param[in] rhs The Left Hand Side ArchSpec object to compare. 342/// 343/// @return true if \a lhs is less than \a rhs 344//------------------------------------------------------------------ 345bool operator< (const ArchSpec& lhs, const ArchSpec& rhs); 346 347} // namespace lldb_private 348 349#endif // #if defined(__cplusplus) 350#endif // #ifndef liblldb_ArchSpec_h_ 351