TargetInfo.h revision 98be4943e8dc4f3905629a7102668960873cf863
1//===--- TargetInfo.h - Expose information about the target -----*- 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// This file defines the TargetInfo and TargetInfoImpl interfaces. 11// 12//===----------------------------------------------------------------------===// 13 14#ifndef LLVM_CLANG_BASIC_TARGETINFO_H 15#define LLVM_CLANG_BASIC_TARGETINFO_H 16 17#include "llvm/Support/DataTypes.h" 18#include <vector> 19#include <string> 20 21namespace llvm { struct fltSemantics; } 22 23namespace clang { 24 25class TargetInfoImpl; 26class Diagnostic; 27class SourceManager; 28 29namespace Builtin { struct Info; } 30 31/// TargetInfo - This class exposes information about the current target. 32/// 33class TargetInfo { 34 /// Primary - This tracks the primary target in the target set. 35 /// 36 const TargetInfoImpl *Target; 37 38 /// These are all caches for target values. 39 unsigned WCharWidth, WCharAlign; 40 41 //==----------------------------------------------------------------==/ 42 // TargetInfo Construction. 43 //==----------------------------------------------------------------==/ 44 45 TargetInfo(const TargetInfoImpl *TII) { 46 Target = TII; 47 48 // Initialize Cache values to uncomputed. 49 WCharWidth = 0; 50 } 51 52public: 53 /// CreateTargetInfo - Return the target info object for the specified target 54 /// triple. 55 static TargetInfo* CreateTargetInfo(const std::string &Triple); 56 57 ~TargetInfo(); 58 59 ///===---- Target property query methods --------------------------------===// 60 61 /// getTargetDefines - Appends the target-specific #define values for this 62 /// target set to the specified buffer. 63 void getTargetDefines(std::vector<char> &DefineBuffer); 64 65 /// isCharSigned - Return true if 'char' is 'signed char' or false if it is 66 /// treated as 'unsigned char'. This is implementation defined according to 67 /// C99 6.2.5p15. In our implementation, this is target-specific. 68 bool isCharSigned() { 69 // FIXME: implement correctly. 70 return true; 71 } 72 73 /// getPointerWidth - Return the width of pointers on this target, we 74 /// currently assume one pointer type. 75 void getPointerInfo(uint64_t &Size, unsigned &Align) { 76 Size = 32; // FIXME: implement correctly. 77 Align = 32; 78 } 79 80 /// getBoolInfo - Return the size of '_Bool' and C++ 'bool' for this target, 81 /// in bits. 82 void getBoolInfo(uint64_t &Size, unsigned &Align) { 83 Size = Align = 8; // FIXME: implement correctly: wrong for ppc32. 84 } 85 86 /// getCharInfo - Return the size of 'char', 'signed char' and 87 /// 'unsigned char' for this target, in bits. 88 void getCharInfo(uint64_t &Size, unsigned &Align) { 89 Size = Align = 8; // FIXME: implement correctly. 90 } 91 92 /// getShortInfo - Return the size of 'signed short' and 'unsigned short' for 93 /// this target, in bits. 94 void getShortInfo(uint64_t &Size, unsigned &Align) { 95 Size = Align = 16; // FIXME: implement correctly. 96 } 97 98 /// getIntInfo - Return the size of 'signed int' and 'unsigned int' for this 99 /// target, in bits. 100 void getIntInfo(uint64_t &Size, unsigned &Align) { 101 Size = Align = 32; // FIXME: implement correctly. 102 } 103 104 /// getLongInfo - Return the size of 'signed long' and 'unsigned long' for 105 /// this target, in bits. 106 void getLongInfo(uint64_t &Size, unsigned &Align) { 107 Size = Align = 32; // FIXME: implement correctly: wrong for ppc64/x86-64 108 } 109 110 /// getLongLongInfo - Return the size of 'signed long long' and 111 /// 'unsigned long long' for this target, in bits. 112 void getLongLongInfo(uint64_t &Size, unsigned &Align) { 113 Size = Align = 64; // FIXME: implement correctly. 114 } 115 116 /// getFloatInfo - Characterize 'float' for this target. 117 void getFloatInfo(uint64_t &Size, unsigned &Align, 118 const llvm::fltSemantics *&Format); 119 120 /// getDoubleInfo - Characterize 'double' for this target. 121 void getDoubleInfo(uint64_t &Size, unsigned &Align, 122 const llvm::fltSemantics *&Format); 123 124 /// getLongDoubleInfo - Characterize 'long double' for this target. 125 void getLongDoubleInfo(uint64_t &Size, unsigned &Align, 126 const llvm::fltSemantics *&Format); 127 128 /// getWCharInfo - Return the size of wchar_t in bits. 129 /// 130 void getWCharInfo(uint64_t &Size, unsigned &Align) { 131 if (!WCharWidth) ComputeWCharInfo(); 132 Size = WCharWidth; 133 Align = WCharAlign; 134 } 135 136 /// getIntMaxTWidth - Return the size of intmax_t and uintmax_t for this 137 /// target, in bits. 138 unsigned getIntMaxTWidth() { 139 // FIXME: implement correctly. 140 return 64; 141 } 142 143 /// getTargetBuiltins - Return information about target-specific builtins for 144 /// the current primary target, and info about which builtins are non-portable 145 /// across the current set of primary and secondary targets. 146 void getTargetBuiltins(const Builtin::Info *&Records, 147 unsigned &NumRecords) const; 148 149 /// getVAListDeclaration - Return the declaration to use for 150 /// __builtin_va_list, which is target-specific. 151 const char *getVAListDeclaration() const; 152 153 /// isValidGCCRegisterName - Returns whether the passed in string 154 /// is a valid register name according to GCC. This is used by Sema for 155 /// inline asm statements. 156 bool isValidGCCRegisterName(const char *Name) const; 157 158 // getNormalizedGCCRegisterName - Returns the "normalized" GCC register name. 159 // For example, on x86 it will return "ax" when "eax" is passed in. 160 const char *getNormalizedGCCRegisterName(const char *Name) const; 161 162 enum ConstraintInfo { 163 CI_None = 0x00, 164 CI_AllowsMemory = 0x01, 165 CI_AllowsRegister = 0x02, 166 CI_ReadWrite = 0x04 167 }; 168 169 // validateOutputConstraint, validateInputConstraint - Checks that 170 // a constraint is valid and provides information about it. 171 // FIXME: These should return a real error instead of just true/false. 172 bool validateOutputConstraint(const char *Name, ConstraintInfo &Info) const; 173 bool validateInputConstraint (const char *Name, unsigned NumOutputs, 174 ConstraintInfo &info) const; 175 176 std::string convertConstraint(const char Constraint) const; 177 178 // Returns a string of target-specific clobbers, in LLVM format. 179 const char *getClobbers() const; 180 181 ///===---- Some helper methods ------------------------------------------===// 182 183 unsigned getBoolWidth() { 184 uint64_t Size; unsigned Align; 185 getBoolInfo(Size, Align); 186 return static_cast<unsigned>(Size); 187 } 188 189 unsigned getCharWidth(bool isWide = false) { 190 uint64_t Size; unsigned Align; 191 if (isWide) 192 getWCharInfo(Size, Align); 193 else 194 getCharInfo(Size, Align); 195 return static_cast<unsigned>(Size); 196 } 197 198 unsigned getWCharWidth() { 199 uint64_t Size; unsigned Align; 200 getWCharInfo(Size, Align); 201 return static_cast<unsigned>(Size); 202 } 203 204 unsigned getIntWidth() { 205 uint64_t Size; unsigned Align; 206 getIntInfo(Size, Align); 207 return static_cast<unsigned>(Size); 208 } 209 210 unsigned getLongWidth() { 211 uint64_t Size; unsigned Align; 212 getLongInfo(Size, Align); 213 return static_cast<unsigned>(Size); 214 } 215 216 unsigned getLongLongWidth() { 217 uint64_t Size; unsigned Align; 218 getLongLongInfo(Size, Align); 219 return static_cast<unsigned>(Size); 220 } 221 222 /// getTargetPrefix - Return the target prefix used for identifying 223 /// llvm intrinsics. 224 const char *getTargetPrefix() const; 225 226 /// getTargetTriple - Return the target triple of the primary target. 227 const char *getTargetTriple() const; 228 229 const char *getTargetDescription() const { 230 // FIXME ! 231 // Hard code darwin-x86 for now. 232 return "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:\ 23332-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128"; 234 } 235private: 236 void ComputeWCharInfo(); 237}; 238 239 240 241 242/// TargetInfoImpl - This class is implemented for specific targets and is used 243/// by the TargetInfo class. Target implementations should initialize instance 244/// variables and implement various virtual methods if the default values are 245/// not appropriate for the target. 246class TargetInfoImpl { 247protected: 248 unsigned WCharWidth; /// sizeof(wchar_t) in bits. Default value is 32. 249 unsigned WCharAlign; /// alignof(wchar_t) in bits. Default value is 32. 250 std::string Triple; 251public: 252 TargetInfoImpl(const std::string& triple) 253 : WCharWidth(32), WCharAlign(32), Triple(triple) {} 254 255 virtual ~TargetInfoImpl() {} 256 257 /// getTargetTriple - Return the string representing the target triple this 258 /// TargetInfoImpl object was created from. 259 const char* getTargetTriple() const { return Triple.c_str(); } 260 261 virtual const char *getTargetPrefix() const = 0; 262 263 /// getTargetDefines - Return a list of the target-specific #define values set 264 /// when compiling to this target. Each string should be of the form 265 /// "#define X Y\n". 266 virtual void getTargetDefines(std::vector<char> &Defines) const = 0; 267 268 /// getVAListDeclaration - Return the declaration to use for 269 /// __builtin_va_list, which is target-specific. 270 virtual const char *getVAListDeclaration() const = 0; 271 272 /// getWCharWidth - Return the size of wchar_t in bits. 273 /// 274 void getWCharInfo(unsigned &Size, unsigned &Align) const { 275 Size = WCharWidth; 276 Align = WCharAlign; 277 } 278 279 /// getTargetBuiltins - Return information about target-specific builtins for 280 /// the target. 281 virtual void getTargetBuiltins(const Builtin::Info *&Records, 282 unsigned &NumRecords) const { 283 Records = 0; 284 NumRecords = 0; 285 } 286 287 virtual void getGCCRegNames(const char * const *&Names, 288 unsigned &NumNames) const = 0; 289 290 struct GCCRegAlias { 291 const char * const Aliases[5]; 292 const char * const Register; 293 }; 294 virtual void getGCCRegAliases(const GCCRegAlias *&Aliases, 295 unsigned &NumAliases) const = 0; 296 297 virtual bool validateAsmConstraint(char c, 298 TargetInfo::ConstraintInfo &info) const= 0; 299 300 virtual std::string convertConstraint(const char Constraint) const { 301 return std::string(1, Constraint); 302 } 303 304 virtual const char *getClobbers() const = 0; 305private: 306 virtual void ANCHOR(); // out-of-line virtual method for class. 307}; 308 309} // end namespace clang 310 311#endif 312