Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1 | //===--- TargetInfo.cpp - Information about Target machine ----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the TargetInfo and TargetInfoImpl interfaces. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Basic/TargetInfo.h" |
| 15 | #include "clang/Basic/Diagnostic.h" |
| 16 | #include "clang/AST/Builtins.h" |
| 17 | #include "llvm/ADT/StringMap.h" |
Chris Lattner | 858eece | 2007-09-22 18:29:59 +0000 | [diff] [blame^] | 18 | #include "llvm/ADT/APFloat.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 19 | #include <set> |
| 20 | using namespace clang; |
| 21 | |
| 22 | void TargetInfoImpl::ANCHOR() {} // out-of-line virtual method for class. |
| 23 | |
| 24 | |
Chris Lattner | 858eece | 2007-09-22 18:29:59 +0000 | [diff] [blame^] | 25 | //===----------------------------------------------------------------------===// |
| 26 | // FIXME: These are temporary hacks, they should revector into the |
| 27 | // TargetInfoImpl. |
| 28 | |
| 29 | void TargetInfo::getFloatInfo(uint64_t &Size, unsigned &Align, |
| 30 | const llvm::fltSemantics *&Format, |
| 31 | SourceLocation Loc) { |
| 32 | Align = 32; // FIXME: implement correctly. |
| 33 | Size = 32; |
| 34 | Format = &llvm::APFloat::IEEEsingle; |
| 35 | } |
| 36 | void TargetInfo::getDoubleInfo(uint64_t &Size, unsigned &Align, |
| 37 | const llvm::fltSemantics *&Format, |
| 38 | SourceLocation Loc) { |
| 39 | Size = Align = 64; // FIXME: implement correctly. |
| 40 | Format = &llvm::APFloat::IEEEdouble; |
| 41 | } |
| 42 | void TargetInfo::getLongDoubleInfo(uint64_t &Size, unsigned &Align, |
| 43 | const llvm::fltSemantics *&Format, |
| 44 | SourceLocation Loc) { |
| 45 | Size = 80; Align = 32; // FIXME: implement correctly. |
| 46 | Format = &llvm::APFloat::x87DoubleExtended; |
| 47 | } |
| 48 | |
| 49 | |
| 50 | //===----------------------------------------------------------------------===// |
| 51 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 52 | /// DiagnoseNonPortability - When a use of a non-portable target feature is |
| 53 | /// used, this method emits the diagnostic and marks the translation unit as |
| 54 | /// non-portable. |
| 55 | void TargetInfo::DiagnoseNonPortability(SourceLocation Loc, unsigned DiagKind) { |
| 56 | NonPortable = true; |
| 57 | if (Diag && Loc.isValid()) Diag->Report(Loc, DiagKind); |
| 58 | } |
| 59 | |
| 60 | /// GetTargetDefineMap - Get the set of target #defines in an associative |
| 61 | /// collection for easy lookup. |
| 62 | static void GetTargetDefineMap(const TargetInfoImpl *Target, |
| 63 | llvm::StringMap<std::string> &Map) { |
| 64 | std::vector<std::string> PrimaryDefines; |
| 65 | Target->getTargetDefines(PrimaryDefines); |
| 66 | |
| 67 | while (!PrimaryDefines.empty()) { |
| 68 | std::string &PrimDefineStr = PrimaryDefines.back(); |
| 69 | const char *Str = PrimDefineStr.c_str(); |
| 70 | const char *StrEnd = Str+PrimDefineStr.size(); |
| 71 | |
| 72 | if (const char *Equal = strchr(Str, '=')) { |
| 73 | // Split at the '='. |
| 74 | |
| 75 | std::string &Entry = Map.GetOrCreateValue(Str, Equal).getValue(); |
| 76 | Entry = std::string(Equal+1, StrEnd); |
| 77 | } else { |
| 78 | // Remember "macroname=1". |
| 79 | std::string &Entry = Map.GetOrCreateValue(Str, StrEnd).getValue(); |
| 80 | Entry = "1"; |
| 81 | } |
| 82 | PrimaryDefines.pop_back(); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /// getTargetDefines - Appends the target-specific #define values for this |
| 87 | /// target set to the specified buffer. |
| 88 | void TargetInfo::getTargetDefines(std::vector<char> &Buffer) { |
| 89 | // This is tricky in the face of secondary targets. Specifically, |
| 90 | // target-specific #defines that are present and identical across all |
| 91 | // secondary targets are turned into #defines, #defines that are present in |
| 92 | // the primary target but are missing or different in the secondary targets |
| 93 | // are turned into #define_target, and #defines that are not defined in the |
| 94 | // primary, but are defined in a secondary are turned into |
| 95 | // #define_other_target. This allows the preprocessor to correctly track uses |
| 96 | // of target-specific macros. |
| 97 | |
| 98 | // Get the set of primary #defines. |
| 99 | llvm::StringMap<std::string> PrimaryDefines; |
| 100 | GetTargetDefineMap(PrimaryTarget, PrimaryDefines); |
| 101 | |
| 102 | // If we have no secondary targets, be a bit more efficient. |
| 103 | if (SecondaryTargets.empty()) { |
| 104 | for (llvm::StringMap<std::string>::iterator I = |
| 105 | PrimaryDefines.begin(), E = PrimaryDefines.end(); I != E; ++I) { |
| 106 | // If this define is non-portable, turn it into #define_target, otherwise |
| 107 | // just use #define. |
| 108 | const char *Command = "#define "; |
| 109 | Buffer.insert(Buffer.end(), Command, Command+strlen(Command)); |
| 110 | |
| 111 | // Insert "defname defvalue\n". |
| 112 | const char *KeyStart = I->getKeyData(); |
| 113 | const char *KeyEnd = KeyStart + I->getKeyLength(); |
| 114 | |
| 115 | Buffer.insert(Buffer.end(), KeyStart, KeyEnd); |
| 116 | Buffer.push_back(' '); |
| 117 | Buffer.insert(Buffer.end(), I->getValue().begin(), I->getValue().end()); |
| 118 | Buffer.push_back('\n'); |
| 119 | } |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | // Get the sets of secondary #defines. |
| 124 | llvm::StringMap<std::string> *SecondaryDefines |
| 125 | = new llvm::StringMap<std::string>[SecondaryTargets.size()]; |
| 126 | for (unsigned i = 0, e = SecondaryTargets.size(); i != e; ++i) |
| 127 | GetTargetDefineMap(SecondaryTargets[i], SecondaryDefines[i]); |
| 128 | |
| 129 | // Loop over all defines in the primary target, processing them until we run |
| 130 | // out. |
| 131 | for (llvm::StringMap<std::string>::iterator PDI = |
| 132 | PrimaryDefines.begin(), E = PrimaryDefines.end(); PDI != E; ++PDI) { |
| 133 | std::string DefineName(PDI->getKeyData(), |
| 134 | PDI->getKeyData() + PDI->getKeyLength()); |
| 135 | std::string DefineValue = PDI->getValue(); |
| 136 | |
| 137 | // Check to see whether all secondary targets have this #define and whether |
| 138 | // it is to the same value. Remember if not, but remove the #define from |
| 139 | // their collection in any case if they have it. |
| 140 | bool isPortable = true; |
| 141 | |
| 142 | for (unsigned i = 0, e = SecondaryTargets.size(); i != e; ++i) { |
| 143 | llvm::StringMap<std::string>::iterator I = |
| 144 | SecondaryDefines[i].find(&DefineName[0], |
| 145 | &DefineName[0]+DefineName.size()); |
| 146 | if (I == SecondaryDefines[i].end()) { |
| 147 | // Secondary target doesn't have this #define. |
| 148 | isPortable = false; |
| 149 | } else { |
| 150 | // Secondary target has this define, remember if it disagrees. |
| 151 | if (isPortable) |
| 152 | isPortable = I->getValue() == DefineValue; |
| 153 | // Remove it from the secondary target unconditionally. |
| 154 | SecondaryDefines[i].erase(I); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | // If this define is non-portable, turn it into #define_target, otherwise |
| 159 | // just use #define. |
| 160 | const char *Command = isPortable ? "#define " : "#define_target "; |
| 161 | Buffer.insert(Buffer.end(), Command, Command+strlen(Command)); |
| 162 | |
| 163 | // Insert "defname defvalue\n". |
| 164 | Buffer.insert(Buffer.end(), DefineName.begin(), DefineName.end()); |
| 165 | Buffer.push_back(' '); |
| 166 | Buffer.insert(Buffer.end(), DefineValue.begin(), DefineValue.end()); |
| 167 | Buffer.push_back('\n'); |
| 168 | } |
| 169 | |
| 170 | // Now that all of the primary target's defines have been handled and removed |
| 171 | // from the secondary target's define sets, go through the remaining secondary |
| 172 | // target's #defines and taint them. |
| 173 | for (unsigned i = 0, e = SecondaryTargets.size(); i != e; ++i) { |
| 174 | llvm::StringMap<std::string> &Defs = SecondaryDefines[i]; |
| 175 | while (!Defs.empty()) { |
| 176 | const char *DefStart = Defs.begin()->getKeyData(); |
| 177 | const char *DefEnd = DefStart + Defs.begin()->getKeyLength(); |
| 178 | |
| 179 | // Insert "#define_other_target defname". |
| 180 | const char *Command = "#define_other_target "; |
| 181 | Buffer.insert(Buffer.end(), Command, Command+strlen(Command)); |
| 182 | Buffer.insert(Buffer.end(), DefStart, DefEnd); |
| 183 | Buffer.push_back('\n'); |
| 184 | |
| 185 | // If any other secondary targets have this same define, remove it from |
| 186 | // them to avoid duplicate #define_other_target directives. |
| 187 | for (unsigned j = i+1; j != e; ++j) { |
| 188 | llvm::StringMap<std::string>::iterator I = |
| 189 | SecondaryDefines[j].find(DefStart, DefEnd); |
| 190 | if (I != SecondaryDefines[j].end()) |
| 191 | SecondaryDefines[j].erase(I); |
| 192 | } |
| 193 | Defs.erase(Defs.begin()); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | delete[] SecondaryDefines; |
| 198 | } |
| 199 | |
| 200 | /// ComputeWCharWidth - Determine the width of the wchar_t type for the primary |
| 201 | /// target, diagnosing whether this is non-portable across the secondary |
| 202 | /// targets. |
| 203 | void TargetInfo::ComputeWCharInfo(SourceLocation Loc) { |
| 204 | PrimaryTarget->getWCharInfo(WCharWidth, WCharAlign); |
| 205 | |
| 206 | // Check whether this is portable across the secondary targets if the T-U is |
| 207 | // portable so far. |
| 208 | for (unsigned i = 0, e = SecondaryTargets.size(); i != e; ++i) { |
| 209 | unsigned Width, Align; |
| 210 | SecondaryTargets[i]->getWCharInfo(Width, Align); |
| 211 | if (Width != WCharWidth || Align != WCharAlign) |
| 212 | return DiagnoseNonPortability(Loc, diag::port_wchar_t); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | |
| 217 | /// getTargetBuiltins - Return information about target-specific builtins for |
| 218 | /// the current primary target, and info about which builtins are non-portable |
| 219 | /// across the current set of primary and secondary targets. |
| 220 | void TargetInfo::getTargetBuiltins(const Builtin::Info *&Records, |
| 221 | unsigned &NumRecords, |
| 222 | std::vector<const char *> &NPortable) const { |
| 223 | // Get info about what actual builtins we will expose. |
| 224 | PrimaryTarget->getTargetBuiltins(Records, NumRecords); |
| 225 | if (SecondaryTargets.empty()) return; |
| 226 | |
| 227 | // Compute the set of non-portable builtins. |
| 228 | |
| 229 | // Start by computing a mapping from the primary target's builtins to their |
| 230 | // info records for efficient lookup. |
| 231 | llvm::StringMap<const Builtin::Info*> PrimaryRecs; |
| 232 | for (unsigned i = 0, e = NumRecords; i != e; ++i) { |
| 233 | const char *BIName = Records[i].Name; |
| 234 | PrimaryRecs.GetOrCreateValue(BIName, BIName+strlen(BIName)).getValue() |
| 235 | = Records+i; |
| 236 | } |
| 237 | |
| 238 | for (unsigned i = 0, e = SecondaryTargets.size(); i != e; ++i) { |
| 239 | // Get the builtins for this secondary target. |
| 240 | const Builtin::Info *Records2nd; |
| 241 | unsigned NumRecords2nd; |
| 242 | SecondaryTargets[i]->getTargetBuiltins(Records2nd, NumRecords2nd); |
| 243 | |
| 244 | // Remember all of the secondary builtin names. |
| 245 | std::set<std::string> BuiltinNames2nd; |
| 246 | |
| 247 | for (unsigned j = 0, e = NumRecords2nd; j != e; ++j) { |
| 248 | BuiltinNames2nd.insert(Records2nd[j].Name); |
| 249 | |
| 250 | // Check to see if the primary target has this builtin. |
| 251 | llvm::StringMap<const Builtin::Info*>::iterator I = |
| 252 | PrimaryRecs.find(Records2nd[j].Name, |
| 253 | Records2nd[j].Name+strlen(Records2nd[j].Name)); |
| 254 | if (I != PrimaryRecs.end()) { |
| 255 | const Builtin::Info *PrimBI = I->getValue(); |
| 256 | // If does. If they are not identical, mark the builtin as being |
| 257 | // non-portable. |
| 258 | if (Records2nd[j] != *PrimBI) |
| 259 | NPortable.push_back(PrimBI->Name); |
| 260 | } else { |
| 261 | // The primary target doesn't have this, it is non-portable. |
| 262 | NPortable.push_back(Records2nd[j].Name); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | // Now that we checked all the secondary builtins, check to see if the |
| 267 | // primary target has any builtins that the secondary one doesn't. If so, |
| 268 | // then those are non-portable. |
| 269 | for (unsigned j = 0, e = NumRecords; j != e; ++j) { |
| 270 | if (!BuiltinNames2nd.count(Records[j].Name)) |
| 271 | NPortable.push_back(Records[j].Name); |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | |