Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1 | //===-- llvmAsmParser.y - Parser for llvm assembly files --------*- C++ -*-===// |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4c06e0d | 2007-12-29 20:46:15 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 10 | // This file implements the bison parser for LLVM assembly languages files. |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | %{ |
Reid Spencer | 90eb4d6 | 2007-01-05 17:18:58 +0000 | [diff] [blame] | 15 | #include "UpgradeInternals.h" |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 16 | #include "llvm/CallingConv.h" |
| 17 | #include "llvm/InlineAsm.h" |
| 18 | #include "llvm/Instructions.h" |
| 19 | #include "llvm/Module.h" |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 20 | #include "llvm/ValueSymbolTable.h" |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 21 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
| 22 | #include "llvm/ADT/STLExtras.h" |
| 23 | #include "llvm/Support/MathExtras.h" |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 24 | #include <algorithm> |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 25 | #include <iostream> |
Chris Lattner | c02659f | 2007-02-11 21:39:35 +0000 | [diff] [blame] | 26 | #include <map> |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 27 | #include <list> |
| 28 | #include <utility> |
| 29 | |
| 30 | // DEBUG_UPREFS - Define this symbol if you want to enable debugging output |
| 31 | // relating to upreferences in the input stream. |
| 32 | // |
| 33 | //#define DEBUG_UPREFS 1 |
| 34 | #ifdef DEBUG_UPREFS |
| 35 | #define UR_OUT(X) std::cerr << X |
| 36 | #else |
| 37 | #define UR_OUT(X) |
| 38 | #endif |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 39 | |
Reid Spencer | e77e35e | 2006-12-01 20:26:20 +0000 | [diff] [blame] | 40 | #define YYERROR_VERBOSE 1 |
Reid Spencer | 96839be | 2006-11-30 16:50:26 +0000 | [diff] [blame] | 41 | #define YYINCLUDED_STDLIB_H |
Reid Spencer | e77e35e | 2006-12-01 20:26:20 +0000 | [diff] [blame] | 42 | #define YYDEBUG 1 |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 43 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 44 | int yylex(); |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 45 | int yyparse(); |
| 46 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 47 | int yyerror(const char*); |
| 48 | static void warning(const std::string& WarningMsg); |
| 49 | |
| 50 | namespace llvm { |
| 51 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 52 | std::istream* LexInput; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 53 | static std::string CurFilename; |
Reid Spencer | 96839be | 2006-11-30 16:50:26 +0000 | [diff] [blame] | 54 | |
Reid Spencer | 05e52a1 | 2006-12-31 05:45:57 +0000 | [diff] [blame] | 55 | // This bool controls whether attributes are ever added to function declarations |
| 56 | // definitions and calls. |
| 57 | static bool AddAttributes = false; |
| 58 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 59 | static Module *ParserResult; |
| 60 | static bool ObsoleteVarArgs; |
| 61 | static bool NewVarArgs; |
| 62 | static BasicBlock *CurBB; |
| 63 | static GlobalVariable *CurGV; |
Reid Spencer | 4f859aa | 2007-04-22 05:46:44 +0000 | [diff] [blame] | 64 | static unsigned lastCallingConv; |
Reid Spencer | a50d596 | 2006-12-02 04:11:07 +0000 | [diff] [blame] | 65 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 66 | // This contains info used when building the body of a function. It is |
| 67 | // destroyed when the function is completed. |
| 68 | // |
| 69 | typedef std::vector<Value *> ValueList; // Numbered defs |
| 70 | |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 71 | typedef std::pair<std::string,TypeInfo> RenameMapKey; |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 72 | typedef std::map<RenameMapKey,std::string> RenameMapType; |
| 73 | |
| 74 | static void |
| 75 | ResolveDefinitions(std::map<const Type *,ValueList> &LateResolvers, |
| 76 | std::map<const Type *,ValueList> *FutureLateResolvers = 0); |
| 77 | |
| 78 | static struct PerModuleInfo { |
| 79 | Module *CurrentModule; |
| 80 | std::map<const Type *, ValueList> Values; // Module level numbered definitions |
| 81 | std::map<const Type *,ValueList> LateResolveValues; |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 82 | std::vector<PATypeHolder> Types; |
| 83 | std::vector<Signedness> TypeSigns; |
| 84 | std::map<std::string,Signedness> NamedTypeSigns; |
| 85 | std::map<std::string,Signedness> NamedValueSigns; |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 86 | std::map<ValID, PATypeHolder> LateResolveTypes; |
| 87 | static Module::Endianness Endian; |
| 88 | static Module::PointerSize PointerSize; |
| 89 | RenameMapType RenameMap; |
| 90 | |
| 91 | /// PlaceHolderInfo - When temporary placeholder objects are created, remember |
| 92 | /// how they were referenced and on which line of the input they came from so |
| 93 | /// that we can resolve them later and print error messages as appropriate. |
| 94 | std::map<Value*, std::pair<ValID, int> > PlaceHolderInfo; |
| 95 | |
| 96 | // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward |
| 97 | // references to global values. Global values may be referenced before they |
| 98 | // are defined, and if so, the temporary object that they represent is held |
| 99 | // here. This is used for forward references of GlobalValues. |
| 100 | // |
| 101 | typedef std::map<std::pair<const PointerType *, ValID>, GlobalValue*> |
| 102 | GlobalRefsType; |
| 103 | GlobalRefsType GlobalRefs; |
| 104 | |
| 105 | void ModuleDone() { |
| 106 | // If we could not resolve some functions at function compilation time |
| 107 | // (calls to functions before they are defined), resolve them now... Types |
| 108 | // are resolved when the constant pool has been completely parsed. |
| 109 | // |
| 110 | ResolveDefinitions(LateResolveValues); |
| 111 | |
| 112 | // Check to make sure that all global value forward references have been |
| 113 | // resolved! |
| 114 | // |
| 115 | if (!GlobalRefs.empty()) { |
| 116 | std::string UndefinedReferences = "Unresolved global references exist:\n"; |
| 117 | |
| 118 | for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end(); |
| 119 | I != E; ++I) { |
| 120 | UndefinedReferences += " " + I->first.first->getDescription() + " " + |
| 121 | I->first.second.getName() + "\n"; |
| 122 | } |
| 123 | error(UndefinedReferences); |
| 124 | return; |
| 125 | } |
| 126 | |
| 127 | if (CurrentModule->getDataLayout().empty()) { |
| 128 | std::string dataLayout; |
| 129 | if (Endian != Module::AnyEndianness) |
| 130 | dataLayout.append(Endian == Module::BigEndian ? "E" : "e"); |
| 131 | if (PointerSize != Module::AnyPointerSize) { |
| 132 | if (!dataLayout.empty()) |
| 133 | dataLayout += "-"; |
| 134 | dataLayout.append(PointerSize == Module::Pointer64 ? |
| 135 | "p:64:64" : "p:32:32"); |
| 136 | } |
| 137 | CurrentModule->setDataLayout(dataLayout); |
| 138 | } |
| 139 | |
| 140 | Values.clear(); // Clear out function local definitions |
| 141 | Types.clear(); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 142 | TypeSigns.clear(); |
| 143 | NamedTypeSigns.clear(); |
| 144 | NamedValueSigns.clear(); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 145 | CurrentModule = 0; |
| 146 | } |
| 147 | |
| 148 | // GetForwardRefForGlobal - Check to see if there is a forward reference |
| 149 | // for this global. If so, remove it from the GlobalRefs map and return it. |
| 150 | // If not, just return null. |
| 151 | GlobalValue *GetForwardRefForGlobal(const PointerType *PTy, ValID ID) { |
| 152 | // Check to see if there is a forward reference to this global variable... |
| 153 | // if there is, eliminate it and patch the reference to use the new def'n. |
| 154 | GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PTy, ID)); |
| 155 | GlobalValue *Ret = 0; |
| 156 | if (I != GlobalRefs.end()) { |
| 157 | Ret = I->second; |
| 158 | GlobalRefs.erase(I); |
| 159 | } |
| 160 | return Ret; |
| 161 | } |
| 162 | void setEndianness(Module::Endianness E) { Endian = E; } |
| 163 | void setPointerSize(Module::PointerSize sz) { PointerSize = sz; } |
| 164 | } CurModule; |
| 165 | |
| 166 | Module::Endianness PerModuleInfo::Endian = Module::AnyEndianness; |
| 167 | Module::PointerSize PerModuleInfo::PointerSize = Module::AnyPointerSize; |
| 168 | |
| 169 | static struct PerFunctionInfo { |
| 170 | Function *CurrentFunction; // Pointer to current function being created |
| 171 | |
| 172 | std::map<const Type*, ValueList> Values; // Keep track of #'d definitions |
| 173 | std::map<const Type*, ValueList> LateResolveValues; |
| 174 | bool isDeclare; // Is this function a forward declararation? |
| 175 | GlobalValue::LinkageTypes Linkage;// Linkage for forward declaration. |
| 176 | |
| 177 | /// BBForwardRefs - When we see forward references to basic blocks, keep |
| 178 | /// track of them here. |
| 179 | std::map<BasicBlock*, std::pair<ValID, int> > BBForwardRefs; |
| 180 | std::vector<BasicBlock*> NumberedBlocks; |
| 181 | RenameMapType RenameMap; |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 182 | unsigned NextBBNum; |
| 183 | |
| 184 | inline PerFunctionInfo() { |
| 185 | CurrentFunction = 0; |
| 186 | isDeclare = false; |
| 187 | Linkage = GlobalValue::ExternalLinkage; |
| 188 | } |
| 189 | |
| 190 | inline void FunctionStart(Function *M) { |
| 191 | CurrentFunction = M; |
| 192 | NextBBNum = 0; |
| 193 | } |
| 194 | |
| 195 | void FunctionDone() { |
| 196 | NumberedBlocks.clear(); |
| 197 | |
| 198 | // Any forward referenced blocks left? |
| 199 | if (!BBForwardRefs.empty()) { |
| 200 | error("Undefined reference to label " + |
| 201 | BBForwardRefs.begin()->first->getName()); |
| 202 | return; |
| 203 | } |
| 204 | |
| 205 | // Resolve all forward references now. |
| 206 | ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues); |
| 207 | |
| 208 | Values.clear(); // Clear out function local definitions |
| 209 | RenameMap.clear(); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 210 | CurrentFunction = 0; |
| 211 | isDeclare = false; |
| 212 | Linkage = GlobalValue::ExternalLinkage; |
| 213 | } |
| 214 | } CurFun; // Info for the current function... |
| 215 | |
| 216 | static bool inFunctionScope() { return CurFun.CurrentFunction != 0; } |
| 217 | |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 218 | /// This function is just a utility to make a Key value for the rename map. |
| 219 | /// The Key is a combination of the name, type, Signedness of the original |
| 220 | /// value (global/function). This just constructs the key and ensures that |
| 221 | /// named Signedness values are resolved to the actual Signedness. |
| 222 | /// @brief Make a key for the RenameMaps |
| 223 | static RenameMapKey makeRenameMapKey(const std::string &Name, const Type* Ty, |
| 224 | const Signedness &Sign) { |
| 225 | TypeInfo TI; |
| 226 | TI.T = Ty; |
| 227 | if (Sign.isNamed()) |
| 228 | // Don't allow Named Signedness nodes because they won't match. The actual |
| 229 | // Signedness must be looked up in the NamedTypeSigns map. |
| 230 | TI.S.copy(CurModule.NamedTypeSigns[Sign.getName()]); |
| 231 | else |
| 232 | TI.S.copy(Sign); |
| 233 | return std::make_pair(Name, TI); |
| 234 | } |
| 235 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 236 | |
| 237 | //===----------------------------------------------------------------------===// |
| 238 | // Code to handle definitions of all the types |
| 239 | //===----------------------------------------------------------------------===// |
| 240 | |
| 241 | static int InsertValue(Value *V, |
| 242 | std::map<const Type*,ValueList> &ValueTab = CurFun.Values) { |
| 243 | if (V->hasName()) return -1; // Is this a numbered definition? |
| 244 | |
| 245 | // Yes, insert the value into the value table... |
| 246 | ValueList &List = ValueTab[V->getType()]; |
| 247 | List.push_back(V); |
| 248 | return List.size()-1; |
| 249 | } |
| 250 | |
Reid Spencer | 9373d27 | 2007-01-26 17:13:53 +0000 | [diff] [blame] | 251 | static const Type *getType(const ValID &D, bool DoNotImprovise = false) { |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 252 | switch (D.Type) { |
| 253 | case ValID::NumberVal: // Is it a numbered definition? |
| 254 | // Module constants occupy the lowest numbered slots... |
| 255 | if ((unsigned)D.Num < CurModule.Types.size()) { |
| 256 | return CurModule.Types[(unsigned)D.Num]; |
| 257 | } |
| 258 | break; |
| 259 | case ValID::NameVal: // Is it a named definition? |
| 260 | if (const Type *N = CurModule.CurrentModule->getTypeByName(D.Name)) { |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 261 | return N; |
| 262 | } |
| 263 | break; |
| 264 | default: |
| 265 | error("Internal parser error: Invalid symbol type reference"); |
| 266 | return 0; |
| 267 | } |
| 268 | |
| 269 | // If we reached here, we referenced either a symbol that we don't know about |
| 270 | // or an id number that hasn't been read yet. We may be referencing something |
| 271 | // forward, so just create an entry to be resolved later and get to it... |
| 272 | // |
| 273 | if (DoNotImprovise) return 0; // Do we just want a null to be returned? |
| 274 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 275 | if (inFunctionScope()) { |
| 276 | if (D.Type == ValID::NameVal) { |
| 277 | error("Reference to an undefined type: '" + D.getName() + "'"); |
| 278 | return 0; |
| 279 | } else { |
| 280 | error("Reference to an undefined type: #" + itostr(D.Num)); |
| 281 | return 0; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | std::map<ValID, PATypeHolder>::iterator I =CurModule.LateResolveTypes.find(D); |
| 286 | if (I != CurModule.LateResolveTypes.end()) |
| 287 | return I->second; |
| 288 | |
| 289 | Type *Typ = OpaqueType::get(); |
| 290 | CurModule.LateResolveTypes.insert(std::make_pair(D, Typ)); |
| 291 | return Typ; |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | /// This is like the getType method except that instead of looking up the type |
| 295 | /// for a given ID, it looks up that type's sign. |
| 296 | /// @brief Get the signedness of a referenced type |
| 297 | static Signedness getTypeSign(const ValID &D) { |
| 298 | switch (D.Type) { |
| 299 | case ValID::NumberVal: // Is it a numbered definition? |
| 300 | // Module constants occupy the lowest numbered slots... |
| 301 | if ((unsigned)D.Num < CurModule.TypeSigns.size()) { |
| 302 | return CurModule.TypeSigns[(unsigned)D.Num]; |
| 303 | } |
| 304 | break; |
| 305 | case ValID::NameVal: { // Is it a named definition? |
| 306 | std::map<std::string,Signedness>::const_iterator I = |
| 307 | CurModule.NamedTypeSigns.find(D.Name); |
| 308 | if (I != CurModule.NamedTypeSigns.end()) |
| 309 | return I->second; |
| 310 | // Perhaps its a named forward .. just cache the name |
| 311 | Signedness S; |
| 312 | S.makeNamed(D.Name); |
| 313 | return S; |
| 314 | } |
| 315 | default: |
| 316 | break; |
| 317 | } |
| 318 | // If we don't find it, its signless |
| 319 | Signedness S; |
| 320 | S.makeSignless(); |
| 321 | return S; |
| 322 | } |
| 323 | |
| 324 | /// This function is analagous to getElementType in LLVM. It provides the same |
| 325 | /// function except that it looks up the Signedness instead of the type. This is |
| 326 | /// used when processing GEP instructions that need to extract the type of an |
| 327 | /// indexed struct/array/ptr member. |
| 328 | /// @brief Look up an element's sign. |
| 329 | static Signedness getElementSign(const ValueInfo& VI, |
| 330 | const std::vector<Value*> &Indices) { |
| 331 | const Type *Ptr = VI.V->getType(); |
| 332 | assert(isa<PointerType>(Ptr) && "Need pointer type"); |
| 333 | |
| 334 | unsigned CurIdx = 0; |
| 335 | Signedness S(VI.S); |
| 336 | while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) { |
| 337 | if (CurIdx == Indices.size()) |
| 338 | break; |
| 339 | |
| 340 | Value *Index = Indices[CurIdx++]; |
| 341 | assert(!isa<PointerType>(CT) || CurIdx == 1 && "Invalid type"); |
| 342 | Ptr = CT->getTypeAtIndex(Index); |
| 343 | if (const Type* Ty = Ptr->getForwardedType()) |
| 344 | Ptr = Ty; |
| 345 | assert(S.isComposite() && "Bad Signedness type"); |
| 346 | if (isa<StructType>(CT)) { |
| 347 | S = S.get(cast<ConstantInt>(Index)->getZExtValue()); |
| 348 | } else { |
| 349 | S = S.get(0UL); |
| 350 | } |
| 351 | if (S.isNamed()) |
| 352 | S = CurModule.NamedTypeSigns[S.getName()]; |
| 353 | } |
| 354 | Signedness Result; |
| 355 | Result.makeComposite(S); |
| 356 | return Result; |
| 357 | } |
| 358 | |
| 359 | /// This function just translates a ConstantInfo into a ValueInfo and calls |
| 360 | /// getElementSign(ValueInfo,...). Its just a convenience. |
| 361 | /// @brief ConstantInfo version of getElementSign. |
| 362 | static Signedness getElementSign(const ConstInfo& CI, |
| 363 | const std::vector<Constant*> &Indices) { |
| 364 | ValueInfo VI; |
| 365 | VI.V = CI.C; |
| 366 | VI.S.copy(CI.S); |
| 367 | std::vector<Value*> Idx; |
| 368 | for (unsigned i = 0; i < Indices.size(); ++i) |
| 369 | Idx.push_back(Indices[i]); |
| 370 | Signedness result = getElementSign(VI, Idx); |
| 371 | VI.destroy(); |
| 372 | return result; |
| 373 | } |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 374 | |
| 375 | // getExistingValue - Look up the value specified by the provided type and |
| 376 | // the provided ValID. If the value exists and has already been defined, return |
| 377 | // it. Otherwise return null. |
| 378 | // |
| 379 | static Value *getExistingValue(const Type *Ty, const ValID &D) { |
| 380 | if (isa<FunctionType>(Ty)) { |
| 381 | error("Functions are not values and must be referenced as pointers"); |
| 382 | } |
| 383 | |
| 384 | switch (D.Type) { |
| 385 | case ValID::NumberVal: { // Is it a numbered definition? |
| 386 | unsigned Num = (unsigned)D.Num; |
| 387 | |
| 388 | // Module constants occupy the lowest numbered slots... |
| 389 | std::map<const Type*,ValueList>::iterator VI = CurModule.Values.find(Ty); |
| 390 | if (VI != CurModule.Values.end()) { |
| 391 | if (Num < VI->second.size()) |
| 392 | return VI->second[Num]; |
| 393 | Num -= VI->second.size(); |
| 394 | } |
| 395 | |
| 396 | // Make sure that our type is within bounds |
| 397 | VI = CurFun.Values.find(Ty); |
| 398 | if (VI == CurFun.Values.end()) return 0; |
| 399 | |
| 400 | // Check that the number is within bounds... |
| 401 | if (VI->second.size() <= Num) return 0; |
| 402 | |
| 403 | return VI->second[Num]; |
| 404 | } |
| 405 | |
| 406 | case ValID::NameVal: { // Is it a named definition? |
| 407 | // Get the name out of the ID |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 408 | RenameMapKey Key = makeRenameMapKey(D.Name, Ty, D.S); |
| 409 | Value *V = 0; |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 410 | if (inFunctionScope()) { |
| 411 | // See if the name was renamed |
| 412 | RenameMapType::const_iterator I = CurFun.RenameMap.find(Key); |
| 413 | std::string LookupName; |
| 414 | if (I != CurFun.RenameMap.end()) |
| 415 | LookupName = I->second; |
| 416 | else |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 417 | LookupName = D.Name; |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 418 | ValueSymbolTable &SymTab = CurFun.CurrentFunction->getValueSymbolTable(); |
| 419 | V = SymTab.lookup(LookupName); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 420 | if (V && V->getType() != Ty) |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 421 | V = 0; |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 422 | } |
| 423 | if (!V) { |
| 424 | RenameMapType::const_iterator I = CurModule.RenameMap.find(Key); |
| 425 | std::string LookupName; |
| 426 | if (I != CurModule.RenameMap.end()) |
| 427 | LookupName = I->second; |
| 428 | else |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 429 | LookupName = D.Name; |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 430 | V = CurModule.CurrentModule->getValueSymbolTable().lookup(LookupName); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 431 | if (V && V->getType() != Ty) |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 432 | V = 0; |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 433 | } |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 434 | if (!V) |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 435 | return 0; |
| 436 | |
| 437 | D.destroy(); // Free old strdup'd memory... |
| 438 | return V; |
| 439 | } |
| 440 | |
| 441 | // Check to make sure that "Ty" is an integral type, and that our |
| 442 | // value will fit into the specified type... |
| 443 | case ValID::ConstSIntVal: // Is it a constant pool reference?? |
| 444 | if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64)) { |
| 445 | error("Signed integral constant '" + itostr(D.ConstPool64) + |
| 446 | "' is invalid for type '" + Ty->getDescription() + "'"); |
| 447 | } |
| 448 | return ConstantInt::get(Ty, D.ConstPool64); |
| 449 | |
| 450 | case ValID::ConstUIntVal: // Is it an unsigned const pool reference? |
| 451 | if (!ConstantInt::isValueValidForType(Ty, D.UConstPool64)) { |
| 452 | if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64)) |
| 453 | error("Integral constant '" + utostr(D.UConstPool64) + |
| 454 | "' is invalid or out of range"); |
| 455 | else // This is really a signed reference. Transmogrify. |
| 456 | return ConstantInt::get(Ty, D.ConstPool64); |
| 457 | } else |
| 458 | return ConstantInt::get(Ty, D.UConstPool64); |
| 459 | |
| 460 | case ValID::ConstFPVal: // Is it a floating point const pool reference? |
Dale Johannesen | 43421b3 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 461 | if (!ConstantFP::isValueValidForType(Ty, *D.ConstPoolFP)) |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 462 | error("FP constant invalid for type"); |
Dale Johannesen | 43421b3 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 463 | // Lexer has no type info, so builds all FP constants as double. |
| 464 | // Fix this here. |
| 465 | if (Ty==Type::FloatTy) |
| 466 | D.ConstPoolFP->convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven); |
| 467 | return ConstantFP::get(Ty, *D.ConstPoolFP); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 468 | |
| 469 | case ValID::ConstNullVal: // Is it a null value? |
| 470 | if (!isa<PointerType>(Ty)) |
| 471 | error("Cannot create a a non pointer null"); |
| 472 | return ConstantPointerNull::get(cast<PointerType>(Ty)); |
| 473 | |
| 474 | case ValID::ConstUndefVal: // Is it an undef value? |
| 475 | return UndefValue::get(Ty); |
| 476 | |
| 477 | case ValID::ConstZeroVal: // Is it a zero value? |
| 478 | return Constant::getNullValue(Ty); |
| 479 | |
| 480 | case ValID::ConstantVal: // Fully resolved constant? |
| 481 | if (D.ConstantValue->getType() != Ty) |
| 482 | error("Constant expression type different from required type"); |
| 483 | return D.ConstantValue; |
| 484 | |
| 485 | case ValID::InlineAsmVal: { // Inline asm expression |
| 486 | const PointerType *PTy = dyn_cast<PointerType>(Ty); |
| 487 | const FunctionType *FTy = |
| 488 | PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0; |
| 489 | if (!FTy || !InlineAsm::Verify(FTy, D.IAD->Constraints)) |
| 490 | error("Invalid type for asm constraint string"); |
| 491 | InlineAsm *IA = InlineAsm::get(FTy, D.IAD->AsmString, D.IAD->Constraints, |
| 492 | D.IAD->HasSideEffects); |
| 493 | D.destroy(); // Free InlineAsmDescriptor. |
| 494 | return IA; |
| 495 | } |
| 496 | default: |
| 497 | assert(0 && "Unhandled case"); |
| 498 | return 0; |
| 499 | } // End of switch |
| 500 | |
| 501 | assert(0 && "Unhandled case"); |
| 502 | return 0; |
| 503 | } |
| 504 | |
| 505 | // getVal - This function is identical to getExistingValue, except that if a |
| 506 | // value is not already defined, it "improvises" by creating a placeholder var |
| 507 | // that looks and acts just like the requested variable. When the value is |
| 508 | // defined later, all uses of the placeholder variable are replaced with the |
| 509 | // real thing. |
| 510 | // |
| 511 | static Value *getVal(const Type *Ty, const ValID &ID) { |
| 512 | if (Ty == Type::LabelTy) |
| 513 | error("Cannot use a basic block here"); |
| 514 | |
| 515 | // See if the value has already been defined. |
| 516 | Value *V = getExistingValue(Ty, ID); |
| 517 | if (V) return V; |
| 518 | |
| 519 | if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty)) |
| 520 | error("Invalid use of a composite type"); |
| 521 | |
| 522 | // If we reached here, we referenced either a symbol that we don't know about |
| 523 | // or an id number that hasn't been read yet. We may be referencing something |
| 524 | // forward, so just create an entry to be resolved later and get to it... |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 525 | V = new Argument(Ty); |
| 526 | |
| 527 | // Remember where this forward reference came from. FIXME, shouldn't we try |
| 528 | // to recycle these things?? |
| 529 | CurModule.PlaceHolderInfo.insert( |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 530 | std::make_pair(V, std::make_pair(ID, Upgradelineno))); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 531 | |
| 532 | if (inFunctionScope()) |
| 533 | InsertValue(V, CurFun.LateResolveValues); |
| 534 | else |
| 535 | InsertValue(V, CurModule.LateResolveValues); |
| 536 | return V; |
| 537 | } |
| 538 | |
Reid Spencer | ac6d608 | 2007-02-08 09:07:25 +0000 | [diff] [blame] | 539 | /// @brief This just makes any name given to it unique, up to MAX_UINT times. |
| 540 | static std::string makeNameUnique(const std::string& Name) { |
| 541 | static unsigned UniqueNameCounter = 1; |
| 542 | std::string Result(Name); |
| 543 | Result += ".upgrd." + llvm::utostr(UniqueNameCounter++); |
| 544 | return Result; |
| 545 | } |
| 546 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 547 | /// getBBVal - This is used for two purposes: |
| 548 | /// * If isDefinition is true, a new basic block with the specified ID is being |
| 549 | /// defined. |
| 550 | /// * If isDefinition is true, this is a reference to a basic block, which may |
| 551 | /// or may not be a forward reference. |
| 552 | /// |
| 553 | static BasicBlock *getBBVal(const ValID &ID, bool isDefinition = false) { |
| 554 | assert(inFunctionScope() && "Can't get basic block at global scope"); |
| 555 | |
| 556 | std::string Name; |
| 557 | BasicBlock *BB = 0; |
| 558 | switch (ID.Type) { |
| 559 | default: |
| 560 | error("Illegal label reference " + ID.getName()); |
| 561 | break; |
| 562 | case ValID::NumberVal: // Is it a numbered definition? |
| 563 | if (unsigned(ID.Num) >= CurFun.NumberedBlocks.size()) |
| 564 | CurFun.NumberedBlocks.resize(ID.Num+1); |
| 565 | BB = CurFun.NumberedBlocks[ID.Num]; |
| 566 | break; |
| 567 | case ValID::NameVal: // Is it a named definition? |
| 568 | Name = ID.Name; |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 569 | if (Value *N = CurFun.CurrentFunction->getValueSymbolTable().lookup(Name)) { |
Reid Spencer | ac6d608 | 2007-02-08 09:07:25 +0000 | [diff] [blame] | 570 | if (N->getType() != Type::LabelTy) { |
| 571 | // Register names didn't use to conflict with basic block names |
| 572 | // because of type planes. Now they all have to be unique. So, we just |
| 573 | // rename the register and treat this name as if no basic block |
| 574 | // had been found. |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 575 | RenameMapKey Key = makeRenameMapKey(ID.Name, N->getType(), ID.S); |
Reid Spencer | ac6d608 | 2007-02-08 09:07:25 +0000 | [diff] [blame] | 576 | N->setName(makeNameUnique(N->getName())); |
| 577 | CurModule.RenameMap[Key] = N->getName(); |
| 578 | BB = 0; |
| 579 | } else { |
| 580 | BB = cast<BasicBlock>(N); |
| 581 | } |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 582 | } |
| 583 | break; |
| 584 | } |
| 585 | |
| 586 | // See if the block has already been defined. |
| 587 | if (BB) { |
| 588 | // If this is the definition of the block, make sure the existing value was |
| 589 | // just a forward reference. If it was a forward reference, there will be |
| 590 | // an entry for it in the PlaceHolderInfo map. |
| 591 | if (isDefinition && !CurFun.BBForwardRefs.erase(BB)) |
| 592 | // The existing value was a definition, not a forward reference. |
| 593 | error("Redefinition of label " + ID.getName()); |
| 594 | |
| 595 | ID.destroy(); // Free strdup'd memory. |
| 596 | return BB; |
| 597 | } |
| 598 | |
| 599 | // Otherwise this block has not been seen before. |
| 600 | BB = new BasicBlock("", CurFun.CurrentFunction); |
| 601 | if (ID.Type == ValID::NameVal) { |
| 602 | BB->setName(ID.Name); |
| 603 | } else { |
| 604 | CurFun.NumberedBlocks[ID.Num] = BB; |
| 605 | } |
| 606 | |
| 607 | // If this is not a definition, keep track of it so we can use it as a forward |
| 608 | // reference. |
| 609 | if (!isDefinition) { |
| 610 | // Remember where this forward reference came from. |
| 611 | CurFun.BBForwardRefs[BB] = std::make_pair(ID, Upgradelineno); |
| 612 | } else { |
| 613 | // The forward declaration could have been inserted anywhere in the |
| 614 | // function: insert it into the correct place now. |
| 615 | CurFun.CurrentFunction->getBasicBlockList().remove(BB); |
| 616 | CurFun.CurrentFunction->getBasicBlockList().push_back(BB); |
| 617 | } |
| 618 | ID.destroy(); |
| 619 | return BB; |
| 620 | } |
| 621 | |
| 622 | |
| 623 | //===----------------------------------------------------------------------===// |
| 624 | // Code to handle forward references in instructions |
| 625 | //===----------------------------------------------------------------------===// |
| 626 | // |
| 627 | // This code handles the late binding needed with statements that reference |
| 628 | // values not defined yet... for example, a forward branch, or the PHI node for |
| 629 | // a loop body. |
| 630 | // |
| 631 | // This keeps a table (CurFun.LateResolveValues) of all such forward references |
| 632 | // and back patchs after we are done. |
| 633 | // |
| 634 | |
| 635 | // ResolveDefinitions - If we could not resolve some defs at parsing |
| 636 | // time (forward branches, phi functions for loops, etc...) resolve the |
| 637 | // defs now... |
| 638 | // |
| 639 | static void |
| 640 | ResolveDefinitions(std::map<const Type*,ValueList> &LateResolvers, |
| 641 | std::map<const Type*,ValueList> *FutureLateResolvers) { |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 642 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 643 | // Loop over LateResolveDefs fixing up stuff that couldn't be resolved |
| 644 | for (std::map<const Type*,ValueList>::iterator LRI = LateResolvers.begin(), |
| 645 | E = LateResolvers.end(); LRI != E; ++LRI) { |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 646 | const Type* Ty = LRI->first; |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 647 | ValueList &List = LRI->second; |
| 648 | while (!List.empty()) { |
| 649 | Value *V = List.back(); |
| 650 | List.pop_back(); |
| 651 | |
| 652 | std::map<Value*, std::pair<ValID, int> >::iterator PHI = |
| 653 | CurModule.PlaceHolderInfo.find(V); |
| 654 | assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error"); |
| 655 | |
| 656 | ValID &DID = PHI->second.first; |
| 657 | |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 658 | Value *TheRealValue = getExistingValue(Ty, DID); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 659 | if (TheRealValue) { |
| 660 | V->replaceAllUsesWith(TheRealValue); |
| 661 | delete V; |
| 662 | CurModule.PlaceHolderInfo.erase(PHI); |
| 663 | } else if (FutureLateResolvers) { |
| 664 | // Functions have their unresolved items forwarded to the module late |
| 665 | // resolver table |
| 666 | InsertValue(V, *FutureLateResolvers); |
| 667 | } else { |
| 668 | if (DID.Type == ValID::NameVal) { |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 669 | error("Reference to an invalid definition: '" + DID.getName() + |
| 670 | "' of type '" + V->getType()->getDescription() + "'", |
| 671 | PHI->second.second); |
Reid Spencer | f049c67 | 2007-01-29 19:07:18 +0000 | [diff] [blame] | 672 | return; |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 673 | } else { |
| 674 | error("Reference to an invalid definition: #" + |
| 675 | itostr(DID.Num) + " of type '" + |
| 676 | V->getType()->getDescription() + "'", PHI->second.second); |
| 677 | return; |
| 678 | } |
| 679 | } |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | LateResolvers.clear(); |
| 684 | } |
| 685 | |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 686 | /// This function is used for type resolution and upref handling. When a type |
| 687 | /// becomes concrete, this function is called to adjust the signedness for the |
| 688 | /// concrete type. |
| 689 | static void ResolveTypeSign(const Type* oldTy, const Signedness &Sign) { |
| 690 | std::string TyName = CurModule.CurrentModule->getTypeName(oldTy); |
| 691 | if (!TyName.empty()) |
| 692 | CurModule.NamedTypeSigns[TyName] = Sign; |
| 693 | } |
| 694 | |
| 695 | /// ResolveTypeTo - A brand new type was just declared. This means that (if |
| 696 | /// name is not null) things referencing Name can be resolved. Otherwise, |
| 697 | /// things refering to the number can be resolved. Do this now. |
| 698 | static void ResolveTypeTo(char *Name, const Type *ToTy, const Signedness& Sign){ |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 699 | ValID D; |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 700 | if (Name) |
| 701 | D = ValID::create(Name); |
| 702 | else |
| 703 | D = ValID::create((int)CurModule.Types.size()); |
| 704 | D.S.copy(Sign); |
| 705 | |
Reid Spencer | 8486744 | 2007-04-11 12:10:08 +0000 | [diff] [blame] | 706 | if (Name) |
| 707 | CurModule.NamedTypeSigns[Name] = Sign; |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 708 | |
| 709 | std::map<ValID, PATypeHolder>::iterator I = |
| 710 | CurModule.LateResolveTypes.find(D); |
| 711 | if (I != CurModule.LateResolveTypes.end()) { |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 712 | const Type *OldTy = I->second.get(); |
| 713 | ((DerivedType*)OldTy)->refineAbstractTypeTo(ToTy); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 714 | CurModule.LateResolveTypes.erase(I); |
| 715 | } |
| 716 | } |
| 717 | |
Anton Korobeynikov | ce13b85 | 2007-01-28 15:25:24 +0000 | [diff] [blame] | 718 | /// This is the implementation portion of TypeHasInteger. It traverses the |
| 719 | /// type given, avoiding recursive types, and returns true as soon as it finds |
| 720 | /// an integer type. If no integer type is found, it returns false. |
| 721 | static bool TypeHasIntegerI(const Type *Ty, std::vector<const Type*> Stack) { |
| 722 | // Handle some easy cases |
| 723 | if (Ty->isPrimitiveType() || (Ty->getTypeID() == Type::OpaqueTyID)) |
| 724 | return false; |
| 725 | if (Ty->isInteger()) |
| 726 | return true; |
| 727 | if (const SequentialType *STy = dyn_cast<SequentialType>(Ty)) |
| 728 | return STy->getElementType()->isInteger(); |
| 729 | |
| 730 | // Avoid type structure recursion |
| 731 | for (std::vector<const Type*>::iterator I = Stack.begin(), E = Stack.end(); |
| 732 | I != E; ++I) |
| 733 | if (Ty == *I) |
| 734 | return false; |
| 735 | |
| 736 | // Push us on the type stack |
| 737 | Stack.push_back(Ty); |
| 738 | |
| 739 | if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) { |
| 740 | if (TypeHasIntegerI(FTy->getReturnType(), Stack)) |
| 741 | return true; |
| 742 | FunctionType::param_iterator I = FTy->param_begin(); |
| 743 | FunctionType::param_iterator E = FTy->param_end(); |
| 744 | for (; I != E; ++I) |
| 745 | if (TypeHasIntegerI(*I, Stack)) |
| 746 | return true; |
| 747 | return false; |
| 748 | } else if (const StructType *STy = dyn_cast<StructType>(Ty)) { |
| 749 | StructType::element_iterator I = STy->element_begin(); |
| 750 | StructType::element_iterator E = STy->element_end(); |
| 751 | for (; I != E; ++I) { |
| 752 | if (TypeHasIntegerI(*I, Stack)) |
| 753 | return true; |
| 754 | } |
| 755 | return false; |
| 756 | } |
| 757 | // There shouldn't be anything else, but its definitely not integer |
| 758 | assert(0 && "What type is this?"); |
| 759 | return false; |
| 760 | } |
| 761 | |
| 762 | /// This is the interface to TypeHasIntegerI. It just provides the type stack, |
| 763 | /// to avoid recursion, and then calls TypeHasIntegerI. |
| 764 | static inline bool TypeHasInteger(const Type *Ty) { |
| 765 | std::vector<const Type*> TyStack; |
| 766 | return TypeHasIntegerI(Ty, TyStack); |
| 767 | } |
| 768 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 769 | // setValueName - Set the specified value to the name given. The name may be |
| 770 | // null potentially, in which case this is a noop. The string passed in is |
| 771 | // assumed to be a malloc'd string buffer, and is free'd by this function. |
| 772 | // |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 773 | static void setValueName(const ValueInfo &V, char *NameStr) { |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 774 | if (NameStr) { |
| 775 | std::string Name(NameStr); // Copy string |
| 776 | free(NameStr); // Free old string |
| 777 | |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 778 | if (V.V->getType() == Type::VoidTy) { |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 779 | error("Can't assign name '" + Name + "' to value with void type"); |
| 780 | return; |
| 781 | } |
| 782 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 783 | assert(inFunctionScope() && "Must be in function scope"); |
| 784 | |
| 785 | // Search the function's symbol table for an existing value of this name |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 786 | ValueSymbolTable &ST = CurFun.CurrentFunction->getValueSymbolTable(); |
| 787 | Value* Existing = ST.lookup(Name); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 788 | if (Existing) { |
Anton Korobeynikov | ce13b85 | 2007-01-28 15:25:24 +0000 | [diff] [blame] | 789 | // An existing value of the same name was found. This might have happened |
| 790 | // because of the integer type planes collapsing in LLVM 2.0. |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 791 | if (Existing->getType() == V.V->getType() && |
Anton Korobeynikov | ce13b85 | 2007-01-28 15:25:24 +0000 | [diff] [blame] | 792 | !TypeHasInteger(Existing->getType())) { |
| 793 | // If the type does not contain any integers in them then this can't be |
| 794 | // a type plane collapsing issue. It truly is a redefinition and we |
| 795 | // should error out as the assembly is invalid. |
| 796 | error("Redefinition of value named '" + Name + "' of type '" + |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 797 | V.V->getType()->getDescription() + "'"); |
Anton Korobeynikov | ce13b85 | 2007-01-28 15:25:24 +0000 | [diff] [blame] | 798 | return; |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 799 | } |
| 800 | // In LLVM 2.0 we don't allow names to be re-used for any values in a |
| 801 | // function, regardless of Type. Previously re-use of names was okay as |
| 802 | // long as they were distinct types. With type planes collapsing because |
| 803 | // of the signedness change and because of PR411, this can no longer be |
| 804 | // supported. We must search the entire symbol table for a conflicting |
| 805 | // name and make the name unique. No warning is needed as this can't |
| 806 | // cause a problem. |
| 807 | std::string NewName = makeNameUnique(Name); |
| 808 | // We're changing the name but it will probably be used by other |
| 809 | // instructions as operands later on. Consequently we have to retain |
| 810 | // a mapping of the renaming that we're doing. |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 811 | RenameMapKey Key = makeRenameMapKey(Name, V.V->getType(), V.S); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 812 | CurFun.RenameMap[Key] = NewName; |
| 813 | Name = NewName; |
| 814 | } |
| 815 | |
| 816 | // Set the name. |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 817 | V.V->setName(Name); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 818 | } |
| 819 | } |
| 820 | |
| 821 | /// ParseGlobalVariable - Handle parsing of a global. If Initializer is null, |
| 822 | /// this is a declaration, otherwise it is a definition. |
| 823 | static GlobalVariable * |
| 824 | ParseGlobalVariable(char *NameStr,GlobalValue::LinkageTypes Linkage, |
| 825 | bool isConstantGlobal, const Type *Ty, |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 826 | Constant *Initializer, |
| 827 | const Signedness &Sign) { |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 828 | if (isa<FunctionType>(Ty)) |
| 829 | error("Cannot declare global vars of function type"); |
| 830 | |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 831 | const PointerType *PTy = PointerType::getUnqual(Ty); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 832 | |
| 833 | std::string Name; |
| 834 | if (NameStr) { |
| 835 | Name = NameStr; // Copy string |
| 836 | free(NameStr); // Free old string |
| 837 | } |
| 838 | |
| 839 | // See if this global value was forward referenced. If so, recycle the |
| 840 | // object. |
| 841 | ValID ID; |
| 842 | if (!Name.empty()) { |
Reid Spencer | 44f87ee | 2007-03-15 03:25:34 +0000 | [diff] [blame] | 843 | ID = ValID::create((char*)Name.c_str()); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 844 | } else { |
Reid Spencer | 44f87ee | 2007-03-15 03:25:34 +0000 | [diff] [blame] | 845 | ID = ValID::create((int)CurModule.Values[PTy].size()); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 846 | } |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 847 | ID.S.makeComposite(Sign); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 848 | |
| 849 | if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) { |
| 850 | // Move the global to the end of the list, from whereever it was |
| 851 | // previously inserted. |
| 852 | GlobalVariable *GV = cast<GlobalVariable>(FWGV); |
| 853 | CurModule.CurrentModule->getGlobalList().remove(GV); |
| 854 | CurModule.CurrentModule->getGlobalList().push_back(GV); |
| 855 | GV->setInitializer(Initializer); |
| 856 | GV->setLinkage(Linkage); |
| 857 | GV->setConstant(isConstantGlobal); |
| 858 | InsertValue(GV, CurModule.Values); |
| 859 | return GV; |
| 860 | } |
| 861 | |
| 862 | // If this global has a name, check to see if there is already a definition |
| 863 | // of this global in the module and emit warnings if there are conflicts. |
| 864 | if (!Name.empty()) { |
| 865 | // The global has a name. See if there's an existing one of the same name. |
Reid Spencer | 53bd704 | 2007-04-16 02:56:33 +0000 | [diff] [blame] | 866 | if (CurModule.CurrentModule->getNamedGlobal(Name) || |
| 867 | CurModule.CurrentModule->getFunction(Name)) { |
| 868 | // We found an existing global of the same name. This isn't allowed |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 869 | // in LLVM 2.0. Consequently, we must alter the name of the global so it |
| 870 | // can at least compile. This can happen because of type planes |
| 871 | // There is alread a global of the same name which means there is a |
| 872 | // conflict. Let's see what we can do about it. |
| 873 | std::string NewName(makeNameUnique(Name)); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 874 | if (Linkage != GlobalValue::InternalLinkage) { |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 875 | // The linkage of this gval is external so we can't reliably rename |
| 876 | // it because it could potentially create a linking problem. |
| 877 | // However, we can't leave the name conflict in the output either or |
| 878 | // it won't assemble with LLVM 2.0. So, all we can do is rename |
| 879 | // this one to something unique and emit a warning about the problem. |
| 880 | warning("Renaming global variable '" + Name + "' to '" + NewName + |
| 881 | "' may cause linkage errors"); |
| 882 | } |
| 883 | |
| 884 | // Put the renaming in the global rename map |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 885 | RenameMapKey Key = |
| 886 | makeRenameMapKey(Name, PointerType::getUnqual(Ty), ID.S); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 887 | CurModule.RenameMap[Key] = NewName; |
| 888 | |
| 889 | // Rename it |
| 890 | Name = NewName; |
| 891 | } |
| 892 | } |
| 893 | |
| 894 | // Otherwise there is no existing GV to use, create one now. |
| 895 | GlobalVariable *GV = |
| 896 | new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name, |
| 897 | CurModule.CurrentModule); |
| 898 | InsertValue(GV, CurModule.Values); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 899 | // Remember the sign of this global. |
| 900 | CurModule.NamedValueSigns[Name] = ID.S; |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 901 | return GV; |
| 902 | } |
| 903 | |
| 904 | // setTypeName - Set the specified type to the name given. The name may be |
| 905 | // null potentially, in which case this is a noop. The string passed in is |
| 906 | // assumed to be a malloc'd string buffer, and is freed by this function. |
| 907 | // |
| 908 | // This function returns true if the type has already been defined, but is |
| 909 | // allowed to be redefined in the specified context. If the name is a new name |
| 910 | // for the type plane, it is inserted and false is returned. |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 911 | static bool setTypeName(const PATypeInfo& TI, char *NameStr) { |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 912 | assert(!inFunctionScope() && "Can't give types function-local names"); |
| 913 | if (NameStr == 0) return false; |
| 914 | |
| 915 | std::string Name(NameStr); // Copy string |
| 916 | free(NameStr); // Free old string |
| 917 | |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 918 | const Type* Ty = TI.PAT->get(); |
| 919 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 920 | // We don't allow assigning names to void type |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 921 | if (Ty == Type::VoidTy) { |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 922 | error("Can't assign name '" + Name + "' to the void type"); |
| 923 | return false; |
| 924 | } |
| 925 | |
| 926 | // Set the type name, checking for conflicts as we do so. |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 927 | bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, Ty); |
| 928 | |
| 929 | // Save the sign information for later use |
| 930 | CurModule.NamedTypeSigns[Name] = TI.S; |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 931 | |
| 932 | if (AlreadyExists) { // Inserting a name that is already defined??? |
| 933 | const Type *Existing = CurModule.CurrentModule->getTypeByName(Name); |
| 934 | assert(Existing && "Conflict but no matching type?"); |
| 935 | |
| 936 | // There is only one case where this is allowed: when we are refining an |
| 937 | // opaque type. In this case, Existing will be an opaque type. |
| 938 | if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) { |
| 939 | // We ARE replacing an opaque type! |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 940 | const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(Ty); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 941 | return true; |
| 942 | } |
| 943 | |
| 944 | // Otherwise, this is an attempt to redefine a type. That's okay if |
| 945 | // the redefinition is identical to the original. This will be so if |
| 946 | // Existing and T point to the same Type object. In this one case we |
| 947 | // allow the equivalent redefinition. |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 948 | if (Existing == Ty) return true; // Yes, it's equal. |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 949 | |
| 950 | // Any other kind of (non-equivalent) redefinition is an error. |
| 951 | error("Redefinition of type named '" + Name + "' in the '" + |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 952 | Ty->getDescription() + "' type plane"); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 953 | } |
| 954 | |
| 955 | return false; |
| 956 | } |
| 957 | |
| 958 | //===----------------------------------------------------------------------===// |
| 959 | // Code for handling upreferences in type names... |
| 960 | // |
| 961 | |
| 962 | // TypeContains - Returns true if Ty directly contains E in it. |
| 963 | // |
| 964 | static bool TypeContains(const Type *Ty, const Type *E) { |
| 965 | return std::find(Ty->subtype_begin(), Ty->subtype_end(), |
| 966 | E) != Ty->subtype_end(); |
| 967 | } |
| 968 | |
| 969 | namespace { |
| 970 | struct UpRefRecord { |
| 971 | // NestingLevel - The number of nesting levels that need to be popped before |
| 972 | // this type is resolved. |
| 973 | unsigned NestingLevel; |
| 974 | |
| 975 | // LastContainedTy - This is the type at the current binding level for the |
| 976 | // type. Every time we reduce the nesting level, this gets updated. |
| 977 | const Type *LastContainedTy; |
| 978 | |
| 979 | // UpRefTy - This is the actual opaque type that the upreference is |
| 980 | // represented with. |
| 981 | OpaqueType *UpRefTy; |
| 982 | |
| 983 | UpRefRecord(unsigned NL, OpaqueType *URTy) |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 984 | : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) { } |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 985 | }; |
| 986 | } |
| 987 | |
| 988 | // UpRefs - A list of the outstanding upreferences that need to be resolved. |
| 989 | static std::vector<UpRefRecord> UpRefs; |
| 990 | |
| 991 | /// HandleUpRefs - Every time we finish a new layer of types, this function is |
| 992 | /// called. It loops through the UpRefs vector, which is a list of the |
| 993 | /// currently active types. For each type, if the up reference is contained in |
| 994 | /// the newly completed type, we decrement the level count. When the level |
| 995 | /// count reaches zero, the upreferenced type is the type that is passed in: |
| 996 | /// thus we can complete the cycle. |
| 997 | /// |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 998 | static PATypeHolder HandleUpRefs(const Type *ty, const Signedness& Sign) { |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 999 | // If Ty isn't abstract, or if there are no up-references in it, then there is |
| 1000 | // nothing to resolve here. |
| 1001 | if (!ty->isAbstract() || UpRefs.empty()) return ty; |
| 1002 | |
| 1003 | PATypeHolder Ty(ty); |
| 1004 | UR_OUT("Type '" << Ty->getDescription() << |
| 1005 | "' newly formed. Resolving upreferences.\n" << |
| 1006 | UpRefs.size() << " upreferences active!\n"); |
| 1007 | |
| 1008 | // If we find any resolvable upreferences (i.e., those whose NestingLevel goes |
| 1009 | // to zero), we resolve them all together before we resolve them to Ty. At |
| 1010 | // the end of the loop, if there is anything to resolve to Ty, it will be in |
| 1011 | // this variable. |
| 1012 | OpaqueType *TypeToResolve = 0; |
| 1013 | |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 1014 | unsigned i = 0; |
| 1015 | for (; i != UpRefs.size(); ++i) { |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1016 | UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", " |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 1017 | << UpRefs[i].UpRefTy->getDescription() << ") = " |
| 1018 | << (TypeContains(Ty, UpRefs[i].UpRefTy) ? "true" : "false") << "\n"); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1019 | if (TypeContains(Ty, UpRefs[i].LastContainedTy)) { |
| 1020 | // Decrement level of upreference |
| 1021 | unsigned Level = --UpRefs[i].NestingLevel; |
| 1022 | UpRefs[i].LastContainedTy = Ty; |
| 1023 | UR_OUT(" Uplevel Ref Level = " << Level << "\n"); |
| 1024 | if (Level == 0) { // Upreference should be resolved! |
| 1025 | if (!TypeToResolve) { |
| 1026 | TypeToResolve = UpRefs[i].UpRefTy; |
| 1027 | } else { |
| 1028 | UR_OUT(" * Resolving upreference for " |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 1029 | << UpRefs[i].UpRefTy->getDescription() << "\n"; |
| 1030 | std::string OldName = UpRefs[i].UpRefTy->getDescription()); |
| 1031 | ResolveTypeSign(UpRefs[i].UpRefTy, Sign); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1032 | UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve); |
| 1033 | UR_OUT(" * Type '" << OldName << "' refined upreference to: " |
| 1034 | << (const void*)Ty << ", " << Ty->getDescription() << "\n"); |
| 1035 | } |
| 1036 | UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list... |
| 1037 | --i; // Do not skip the next element... |
| 1038 | } |
| 1039 | } |
| 1040 | } |
| 1041 | |
| 1042 | if (TypeToResolve) { |
| 1043 | UR_OUT(" * Resolving upreference for " |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 1044 | << UpRefs[i].UpRefTy->getDescription() << "\n"; |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1045 | std::string OldName = TypeToResolve->getDescription()); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 1046 | ResolveTypeSign(TypeToResolve, Sign); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1047 | TypeToResolve->refineAbstractTypeTo(Ty); |
| 1048 | } |
| 1049 | |
| 1050 | return Ty; |
| 1051 | } |
| 1052 | |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 1053 | bool Signedness::operator<(const Signedness &that) const { |
| 1054 | if (isNamed()) { |
| 1055 | if (that.isNamed()) |
| 1056 | return *(this->name) < *(that.name); |
| 1057 | else |
| 1058 | return CurModule.NamedTypeSigns[*name] < that; |
| 1059 | } else if (that.isNamed()) { |
| 1060 | return *this < CurModule.NamedTypeSigns[*that.name]; |
| 1061 | } |
| 1062 | |
| 1063 | if (isComposite() && that.isComposite()) { |
| 1064 | if (sv->size() == that.sv->size()) { |
| 1065 | SignVector::const_iterator thisI = sv->begin(), thisE = sv->end(); |
| 1066 | SignVector::const_iterator thatI = that.sv->begin(), |
| 1067 | thatE = that.sv->end(); |
| 1068 | for (; thisI != thisE; ++thisI, ++thatI) { |
| 1069 | if (*thisI < *thatI) |
| 1070 | return true; |
| 1071 | else if (!(*thisI == *thatI)) |
| 1072 | return false; |
| 1073 | } |
| 1074 | return false; |
| 1075 | } |
| 1076 | return sv->size() < that.sv->size(); |
| 1077 | } |
| 1078 | return kind < that.kind; |
| 1079 | } |
| 1080 | |
| 1081 | bool Signedness::operator==(const Signedness &that) const { |
| 1082 | if (isNamed()) |
| 1083 | if (that.isNamed()) |
| 1084 | return *(this->name) == *(that.name); |
| 1085 | else |
| 1086 | return CurModule.NamedTypeSigns[*(this->name)] == that; |
| 1087 | else if (that.isNamed()) |
| 1088 | return *this == CurModule.NamedTypeSigns[*(that.name)]; |
| 1089 | if (isComposite() && that.isComposite()) { |
| 1090 | if (sv->size() == that.sv->size()) { |
| 1091 | SignVector::const_iterator thisI = sv->begin(), thisE = sv->end(); |
| 1092 | SignVector::const_iterator thatI = that.sv->begin(), |
| 1093 | thatE = that.sv->end(); |
| 1094 | for (; thisI != thisE; ++thisI, ++thatI) { |
| 1095 | if (!(*thisI == *thatI)) |
| 1096 | return false; |
| 1097 | } |
| 1098 | return true; |
| 1099 | } |
| 1100 | return false; |
| 1101 | } |
| 1102 | return kind == that.kind; |
| 1103 | } |
| 1104 | |
| 1105 | void Signedness::copy(const Signedness &that) { |
| 1106 | if (that.isNamed()) { |
| 1107 | kind = Named; |
| 1108 | name = new std::string(*that.name); |
| 1109 | } else if (that.isComposite()) { |
| 1110 | kind = Composite; |
| 1111 | sv = new SignVector(); |
| 1112 | *sv = *that.sv; |
| 1113 | } else { |
| 1114 | kind = that.kind; |
| 1115 | sv = 0; |
| 1116 | } |
| 1117 | } |
| 1118 | |
| 1119 | void Signedness::destroy() { |
| 1120 | if (isNamed()) { |
| 1121 | delete name; |
| 1122 | } else if (isComposite()) { |
| 1123 | delete sv; |
| 1124 | } |
| 1125 | } |
| 1126 | |
Evan Cheng | 2b48420 | 2007-03-22 07:43:51 +0000 | [diff] [blame] | 1127 | #ifndef NDEBUG |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 1128 | void Signedness::dump() const { |
| 1129 | if (isComposite()) { |
| 1130 | if (sv->size() == 1) { |
| 1131 | (*sv)[0].dump(); |
| 1132 | std::cerr << "*"; |
| 1133 | } else { |
| 1134 | std::cerr << "{ " ; |
| 1135 | for (unsigned i = 0; i < sv->size(); ++i) { |
| 1136 | if (i != 0) |
| 1137 | std::cerr << ", "; |
| 1138 | (*sv)[i].dump(); |
| 1139 | } |
| 1140 | std::cerr << "} " ; |
| 1141 | } |
| 1142 | } else if (isNamed()) { |
| 1143 | std::cerr << *name; |
| 1144 | } else if (isSigned()) { |
| 1145 | std::cerr << "S"; |
| 1146 | } else if (isUnsigned()) { |
| 1147 | std::cerr << "U"; |
| 1148 | } else |
| 1149 | std::cerr << "."; |
| 1150 | } |
Evan Cheng | 2b48420 | 2007-03-22 07:43:51 +0000 | [diff] [blame] | 1151 | #endif |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 1152 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1153 | static inline Instruction::TermOps |
| 1154 | getTermOp(TermOps op) { |
| 1155 | switch (op) { |
| 1156 | default : assert(0 && "Invalid OldTermOp"); |
| 1157 | case RetOp : return Instruction::Ret; |
| 1158 | case BrOp : return Instruction::Br; |
| 1159 | case SwitchOp : return Instruction::Switch; |
| 1160 | case InvokeOp : return Instruction::Invoke; |
| 1161 | case UnwindOp : return Instruction::Unwind; |
| 1162 | case UnreachableOp: return Instruction::Unreachable; |
| 1163 | } |
| 1164 | } |
| 1165 | |
| 1166 | static inline Instruction::BinaryOps |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 1167 | getBinaryOp(BinaryOps op, const Type *Ty, const Signedness& Sign) { |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1168 | switch (op) { |
| 1169 | default : assert(0 && "Invalid OldBinaryOps"); |
| 1170 | case SetEQ : |
| 1171 | case SetNE : |
| 1172 | case SetLE : |
| 1173 | case SetGE : |
| 1174 | case SetLT : |
| 1175 | case SetGT : assert(0 && "Should use getCompareOp"); |
| 1176 | case AddOp : return Instruction::Add; |
| 1177 | case SubOp : return Instruction::Sub; |
| 1178 | case MulOp : return Instruction::Mul; |
| 1179 | case DivOp : { |
| 1180 | // This is an obsolete instruction so we must upgrade it based on the |
| 1181 | // types of its operands. |
| 1182 | bool isFP = Ty->isFloatingPoint(); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1183 | if (const VectorType* PTy = dyn_cast<VectorType>(Ty)) |
Reid Spencer | ac9dcb9 | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 1184 | // If its a vector type we want to use the element type |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1185 | isFP = PTy->getElementType()->isFloatingPoint(); |
| 1186 | if (isFP) |
| 1187 | return Instruction::FDiv; |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 1188 | else if (Sign.isSigned()) |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1189 | return Instruction::SDiv; |
| 1190 | return Instruction::UDiv; |
| 1191 | } |
| 1192 | case UDivOp : return Instruction::UDiv; |
| 1193 | case SDivOp : return Instruction::SDiv; |
| 1194 | case FDivOp : return Instruction::FDiv; |
| 1195 | case RemOp : { |
| 1196 | // This is an obsolete instruction so we must upgrade it based on the |
| 1197 | // types of its operands. |
| 1198 | bool isFP = Ty->isFloatingPoint(); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1199 | if (const VectorType* PTy = dyn_cast<VectorType>(Ty)) |
Reid Spencer | ac9dcb9 | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 1200 | // If its a vector type we want to use the element type |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1201 | isFP = PTy->getElementType()->isFloatingPoint(); |
| 1202 | // Select correct opcode |
| 1203 | if (isFP) |
| 1204 | return Instruction::FRem; |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 1205 | else if (Sign.isSigned()) |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1206 | return Instruction::SRem; |
| 1207 | return Instruction::URem; |
| 1208 | } |
| 1209 | case URemOp : return Instruction::URem; |
| 1210 | case SRemOp : return Instruction::SRem; |
| 1211 | case FRemOp : return Instruction::FRem; |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1212 | case LShrOp : return Instruction::LShr; |
| 1213 | case AShrOp : return Instruction::AShr; |
| 1214 | case ShlOp : return Instruction::Shl; |
| 1215 | case ShrOp : |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 1216 | if (Sign.isSigned()) |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1217 | return Instruction::AShr; |
| 1218 | return Instruction::LShr; |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1219 | case AndOp : return Instruction::And; |
| 1220 | case OrOp : return Instruction::Or; |
| 1221 | case XorOp : return Instruction::Xor; |
| 1222 | } |
| 1223 | } |
| 1224 | |
| 1225 | static inline Instruction::OtherOps |
| 1226 | getCompareOp(BinaryOps op, unsigned short &predicate, const Type* &Ty, |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 1227 | const Signedness &Sign) { |
| 1228 | bool isSigned = Sign.isSigned(); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1229 | bool isFP = Ty->isFloatingPoint(); |
| 1230 | switch (op) { |
| 1231 | default : assert(0 && "Invalid OldSetCC"); |
| 1232 | case SetEQ : |
| 1233 | if (isFP) { |
| 1234 | predicate = FCmpInst::FCMP_OEQ; |
| 1235 | return Instruction::FCmp; |
| 1236 | } else { |
| 1237 | predicate = ICmpInst::ICMP_EQ; |
| 1238 | return Instruction::ICmp; |
| 1239 | } |
| 1240 | case SetNE : |
| 1241 | if (isFP) { |
| 1242 | predicate = FCmpInst::FCMP_UNE; |
| 1243 | return Instruction::FCmp; |
| 1244 | } else { |
| 1245 | predicate = ICmpInst::ICMP_NE; |
| 1246 | return Instruction::ICmp; |
| 1247 | } |
| 1248 | case SetLE : |
| 1249 | if (isFP) { |
| 1250 | predicate = FCmpInst::FCMP_OLE; |
| 1251 | return Instruction::FCmp; |
| 1252 | } else { |
| 1253 | if (isSigned) |
| 1254 | predicate = ICmpInst::ICMP_SLE; |
| 1255 | else |
| 1256 | predicate = ICmpInst::ICMP_ULE; |
| 1257 | return Instruction::ICmp; |
| 1258 | } |
| 1259 | case SetGE : |
| 1260 | if (isFP) { |
| 1261 | predicate = FCmpInst::FCMP_OGE; |
| 1262 | return Instruction::FCmp; |
| 1263 | } else { |
| 1264 | if (isSigned) |
| 1265 | predicate = ICmpInst::ICMP_SGE; |
| 1266 | else |
| 1267 | predicate = ICmpInst::ICMP_UGE; |
| 1268 | return Instruction::ICmp; |
| 1269 | } |
| 1270 | case SetLT : |
| 1271 | if (isFP) { |
| 1272 | predicate = FCmpInst::FCMP_OLT; |
| 1273 | return Instruction::FCmp; |
| 1274 | } else { |
| 1275 | if (isSigned) |
| 1276 | predicate = ICmpInst::ICMP_SLT; |
| 1277 | else |
| 1278 | predicate = ICmpInst::ICMP_ULT; |
| 1279 | return Instruction::ICmp; |
| 1280 | } |
| 1281 | case SetGT : |
| 1282 | if (isFP) { |
| 1283 | predicate = FCmpInst::FCMP_OGT; |
| 1284 | return Instruction::FCmp; |
| 1285 | } else { |
| 1286 | if (isSigned) |
| 1287 | predicate = ICmpInst::ICMP_SGT; |
| 1288 | else |
| 1289 | predicate = ICmpInst::ICMP_UGT; |
| 1290 | return Instruction::ICmp; |
| 1291 | } |
| 1292 | } |
| 1293 | } |
| 1294 | |
| 1295 | static inline Instruction::MemoryOps getMemoryOp(MemoryOps op) { |
| 1296 | switch (op) { |
| 1297 | default : assert(0 && "Invalid OldMemoryOps"); |
| 1298 | case MallocOp : return Instruction::Malloc; |
| 1299 | case FreeOp : return Instruction::Free; |
| 1300 | case AllocaOp : return Instruction::Alloca; |
| 1301 | case LoadOp : return Instruction::Load; |
| 1302 | case StoreOp : return Instruction::Store; |
| 1303 | case GetElementPtrOp : return Instruction::GetElementPtr; |
| 1304 | } |
| 1305 | } |
| 1306 | |
| 1307 | static inline Instruction::OtherOps |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 1308 | getOtherOp(OtherOps op, const Signedness &Sign) { |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1309 | switch (op) { |
| 1310 | default : assert(0 && "Invalid OldOtherOps"); |
| 1311 | case PHIOp : return Instruction::PHI; |
| 1312 | case CallOp : return Instruction::Call; |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1313 | case SelectOp : return Instruction::Select; |
| 1314 | case UserOp1 : return Instruction::UserOp1; |
| 1315 | case UserOp2 : return Instruction::UserOp2; |
| 1316 | case VAArg : return Instruction::VAArg; |
| 1317 | case ExtractElementOp : return Instruction::ExtractElement; |
| 1318 | case InsertElementOp : return Instruction::InsertElement; |
| 1319 | case ShuffleVectorOp : return Instruction::ShuffleVector; |
| 1320 | case ICmpOp : return Instruction::ICmp; |
| 1321 | case FCmpOp : return Instruction::FCmp; |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1322 | }; |
| 1323 | } |
| 1324 | |
| 1325 | static inline Value* |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 1326 | getCast(CastOps op, Value *Src, const Signedness &SrcSign, const Type *DstTy, |
| 1327 | const Signedness &DstSign, bool ForceInstruction = false) { |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1328 | Instruction::CastOps Opcode; |
| 1329 | const Type* SrcTy = Src->getType(); |
| 1330 | if (op == CastOp) { |
| 1331 | if (SrcTy->isFloatingPoint() && isa<PointerType>(DstTy)) { |
| 1332 | // fp -> ptr cast is no longer supported but we must upgrade this |
| 1333 | // by doing a double cast: fp -> int -> ptr |
| 1334 | SrcTy = Type::Int64Ty; |
| 1335 | Opcode = Instruction::IntToPtr; |
| 1336 | if (isa<Constant>(Src)) { |
| 1337 | Src = ConstantExpr::getCast(Instruction::FPToUI, |
| 1338 | cast<Constant>(Src), SrcTy); |
| 1339 | } else { |
| 1340 | std::string NewName(makeNameUnique(Src->getName())); |
| 1341 | Src = new FPToUIInst(Src, SrcTy, NewName, CurBB); |
| 1342 | } |
| 1343 | } else if (isa<IntegerType>(DstTy) && |
| 1344 | cast<IntegerType>(DstTy)->getBitWidth() == 1) { |
| 1345 | // cast type %x to bool was previously defined as setne type %x, null |
| 1346 | // The cast semantic is now to truncate, not compare so we must retain |
| 1347 | // the original intent by replacing the cast with a setne |
| 1348 | Constant* Null = Constant::getNullValue(SrcTy); |
| 1349 | Instruction::OtherOps Opcode = Instruction::ICmp; |
| 1350 | unsigned short predicate = ICmpInst::ICMP_NE; |
| 1351 | if (SrcTy->isFloatingPoint()) { |
| 1352 | Opcode = Instruction::FCmp; |
| 1353 | predicate = FCmpInst::FCMP_ONE; |
| 1354 | } else if (!SrcTy->isInteger() && !isa<PointerType>(SrcTy)) { |
| 1355 | error("Invalid cast to bool"); |
| 1356 | } |
| 1357 | if (isa<Constant>(Src) && !ForceInstruction) |
| 1358 | return ConstantExpr::getCompare(predicate, cast<Constant>(Src), Null); |
| 1359 | else |
| 1360 | return CmpInst::create(Opcode, predicate, Src, Null); |
| 1361 | } |
| 1362 | // Determine the opcode to use by calling CastInst::getCastOpcode |
| 1363 | Opcode = |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 1364 | CastInst::getCastOpcode(Src, SrcSign.isSigned(), DstTy, |
| 1365 | DstSign.isSigned()); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1366 | |
| 1367 | } else switch (op) { |
| 1368 | default: assert(0 && "Invalid cast token"); |
| 1369 | case TruncOp: Opcode = Instruction::Trunc; break; |
| 1370 | case ZExtOp: Opcode = Instruction::ZExt; break; |
| 1371 | case SExtOp: Opcode = Instruction::SExt; break; |
| 1372 | case FPTruncOp: Opcode = Instruction::FPTrunc; break; |
| 1373 | case FPExtOp: Opcode = Instruction::FPExt; break; |
| 1374 | case FPToUIOp: Opcode = Instruction::FPToUI; break; |
| 1375 | case FPToSIOp: Opcode = Instruction::FPToSI; break; |
| 1376 | case UIToFPOp: Opcode = Instruction::UIToFP; break; |
| 1377 | case SIToFPOp: Opcode = Instruction::SIToFP; break; |
| 1378 | case PtrToIntOp: Opcode = Instruction::PtrToInt; break; |
| 1379 | case IntToPtrOp: Opcode = Instruction::IntToPtr; break; |
| 1380 | case BitCastOp: Opcode = Instruction::BitCast; break; |
| 1381 | } |
| 1382 | |
| 1383 | if (isa<Constant>(Src) && !ForceInstruction) |
| 1384 | return ConstantExpr::getCast(Opcode, cast<Constant>(Src), DstTy); |
| 1385 | return CastInst::create(Opcode, Src, DstTy); |
| 1386 | } |
| 1387 | |
| 1388 | static Instruction * |
| 1389 | upgradeIntrinsicCall(const Type* RetTy, const ValID &ID, |
| 1390 | std::vector<Value*>& Args) { |
| 1391 | |
| 1392 | std::string Name = ID.Type == ValID::NameVal ? ID.Name : ""; |
Reid Spencer | 4f859aa | 2007-04-22 05:46:44 +0000 | [diff] [blame] | 1393 | if (Name.length() <= 5 || Name[0] != 'l' || Name[1] != 'l' || |
| 1394 | Name[2] != 'v' || Name[3] != 'm' || Name[4] != '.') |
| 1395 | return 0; |
| 1396 | |
Reid Spencer | 1e70bb6 | 2007-04-02 00:50:28 +0000 | [diff] [blame] | 1397 | switch (Name[5]) { |
| 1398 | case 'i': |
| 1399 | if (Name == "llvm.isunordered.f32" || Name == "llvm.isunordered.f64") { |
| 1400 | if (Args.size() != 2) |
| 1401 | error("Invalid prototype for " + Name); |
| 1402 | return new FCmpInst(FCmpInst::FCMP_UNO, Args[0], Args[1]); |
| 1403 | } |
| 1404 | break; |
Reid Spencer | 8918cb4 | 2007-04-02 02:08:05 +0000 | [diff] [blame] | 1405 | |
Reid Spencer | 1e70bb6 | 2007-04-02 00:50:28 +0000 | [diff] [blame] | 1406 | case 'v' : { |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 1407 | const Type* PtrTy = PointerType::getUnqual(Type::Int8Ty); |
Reid Spencer | 1e70bb6 | 2007-04-02 00:50:28 +0000 | [diff] [blame] | 1408 | std::vector<const Type*> Params; |
| 1409 | if (Name == "llvm.va_start" || Name == "llvm.va_end") { |
| 1410 | if (Args.size() != 1) |
| 1411 | error("Invalid prototype for " + Name + " prototype"); |
| 1412 | Params.push_back(PtrTy); |
| 1413 | const FunctionType *FTy = |
| 1414 | FunctionType::get(Type::VoidTy, Params, false); |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 1415 | const PointerType *PFTy = PointerType::getUnqual(FTy); |
Reid Spencer | 1e70bb6 | 2007-04-02 00:50:28 +0000 | [diff] [blame] | 1416 | Value* Func = getVal(PFTy, ID); |
| 1417 | Args[0] = new BitCastInst(Args[0], PtrTy, makeNameUnique("va"), CurBB); |
David Greene | 52eec54 | 2007-08-01 03:43:44 +0000 | [diff] [blame] | 1418 | return new CallInst(Func, Args.begin(), Args.end()); |
Reid Spencer | 1e70bb6 | 2007-04-02 00:50:28 +0000 | [diff] [blame] | 1419 | } else if (Name == "llvm.va_copy") { |
| 1420 | if (Args.size() != 2) |
| 1421 | error("Invalid prototype for " + Name + " prototype"); |
| 1422 | Params.push_back(PtrTy); |
| 1423 | Params.push_back(PtrTy); |
| 1424 | const FunctionType *FTy = |
| 1425 | FunctionType::get(Type::VoidTy, Params, false); |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 1426 | const PointerType *PFTy = PointerType::getUnqual(FTy); |
Reid Spencer | 1e70bb6 | 2007-04-02 00:50:28 +0000 | [diff] [blame] | 1427 | Value* Func = getVal(PFTy, ID); |
| 1428 | std::string InstName0(makeNameUnique("va0")); |
| 1429 | std::string InstName1(makeNameUnique("va1")); |
| 1430 | Args[0] = new BitCastInst(Args[0], PtrTy, InstName0, CurBB); |
| 1431 | Args[1] = new BitCastInst(Args[1], PtrTy, InstName1, CurBB); |
David Greene | 52eec54 | 2007-08-01 03:43:44 +0000 | [diff] [blame] | 1432 | return new CallInst(Func, Args.begin(), Args.end()); |
Reid Spencer | 1e70bb6 | 2007-04-02 00:50:28 +0000 | [diff] [blame] | 1433 | } |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1434 | } |
| 1435 | } |
| 1436 | return 0; |
| 1437 | } |
| 1438 | |
Reid Spencer | d23c4dd | 2007-04-16 00:39:39 +0000 | [diff] [blame] | 1439 | const Type* upgradeGEPCEIndices(const Type* PTy, |
| 1440 | std::vector<ValueInfo> *Indices, |
| 1441 | std::vector<Constant*> &Result) { |
| 1442 | const Type *Ty = PTy; |
| 1443 | Result.clear(); |
| 1444 | for (unsigned i = 0, e = Indices->size(); i != e ; ++i) { |
| 1445 | Constant *Index = cast<Constant>((*Indices)[i].V); |
| 1446 | |
| 1447 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Index)) { |
| 1448 | // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte |
| 1449 | // struct indices to i32 struct indices with ZExt for compatibility. |
| 1450 | if (CI->getBitWidth() < 32) |
| 1451 | Index = ConstantExpr::getCast(Instruction::ZExt, CI, Type::Int32Ty); |
| 1452 | } |
| 1453 | |
| 1454 | if (isa<SequentialType>(Ty)) { |
| 1455 | // Make sure that unsigned SequentialType indices are zext'd to |
| 1456 | // 64-bits if they were smaller than that because LLVM 2.0 will sext |
| 1457 | // all indices for SequentialType elements. We must retain the same |
| 1458 | // semantic (zext) for unsigned types. |
| 1459 | if (const IntegerType *Ity = dyn_cast<IntegerType>(Index->getType())) { |
| 1460 | if (Ity->getBitWidth() < 64 && (*Indices)[i].S.isUnsigned()) { |
| 1461 | Index = ConstantExpr::getCast(Instruction::ZExt, Index,Type::Int64Ty); |
| 1462 | } |
| 1463 | } |
| 1464 | } |
| 1465 | Result.push_back(Index); |
David Greene | b8f7479 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 1466 | Ty = GetElementPtrInst::getIndexedType(PTy, Result.begin(), |
| 1467 | Result.end(),true); |
Reid Spencer | d23c4dd | 2007-04-16 00:39:39 +0000 | [diff] [blame] | 1468 | if (!Ty) |
| 1469 | error("Index list invalid for constant getelementptr"); |
| 1470 | } |
| 1471 | return Ty; |
| 1472 | } |
| 1473 | |
| 1474 | const Type* upgradeGEPInstIndices(const Type* PTy, |
| 1475 | std::vector<ValueInfo> *Indices, |
| 1476 | std::vector<Value*> &Result) { |
| 1477 | const Type *Ty = PTy; |
| 1478 | Result.clear(); |
| 1479 | for (unsigned i = 0, e = Indices->size(); i != e ; ++i) { |
| 1480 | Value *Index = (*Indices)[i].V; |
| 1481 | |
| 1482 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Index)) { |
| 1483 | // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte |
| 1484 | // struct indices to i32 struct indices with ZExt for compatibility. |
| 1485 | if (CI->getBitWidth() < 32) |
| 1486 | Index = ConstantExpr::getCast(Instruction::ZExt, CI, Type::Int32Ty); |
| 1487 | } |
| 1488 | |
| 1489 | |
| 1490 | if (isa<StructType>(Ty)) { // Only change struct indices |
| 1491 | if (!isa<Constant>(Index)) { |
| 1492 | error("Invalid non-constant structure index"); |
| 1493 | return 0; |
| 1494 | } |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1495 | } else { |
| 1496 | // Make sure that unsigned SequentialType indices are zext'd to |
| 1497 | // 64-bits if they were smaller than that because LLVM 2.0 will sext |
| 1498 | // all indices for SequentialType elements. We must retain the same |
| 1499 | // semantic (zext) for unsigned types. |
Reid Spencer | d23c4dd | 2007-04-16 00:39:39 +0000 | [diff] [blame] | 1500 | if (const IntegerType *Ity = dyn_cast<IntegerType>(Index->getType())) { |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 1501 | if (Ity->getBitWidth() < 64 && (*Indices)[i].S.isUnsigned()) { |
Reid Spencer | d23c4dd | 2007-04-16 00:39:39 +0000 | [diff] [blame] | 1502 | if (isa<Constant>(Index)) |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1503 | Index = ConstantExpr::getCast(Instruction::ZExt, |
| 1504 | cast<Constant>(Index), Type::Int64Ty); |
| 1505 | else |
| 1506 | Index = CastInst::create(Instruction::ZExt, Index, Type::Int64Ty, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1507 | makeNameUnique("gep"), CurBB); |
Reid Spencer | 8f78af9 | 2007-01-26 20:29:52 +0000 | [diff] [blame] | 1508 | } |
Reid Spencer | d23c4dd | 2007-04-16 00:39:39 +0000 | [diff] [blame] | 1509 | } |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1510 | } |
Reid Spencer | d23c4dd | 2007-04-16 00:39:39 +0000 | [diff] [blame] | 1511 | Result.push_back(Index); |
David Greene | b8f7479 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 1512 | Ty = GetElementPtrInst::getIndexedType(PTy, Result.begin(), |
| 1513 | Result.end(),true); |
Reid Spencer | d23c4dd | 2007-04-16 00:39:39 +0000 | [diff] [blame] | 1514 | if (!Ty) |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1515 | error("Index list invalid for constant getelementptr"); |
Reid Spencer | d23c4dd | 2007-04-16 00:39:39 +0000 | [diff] [blame] | 1516 | } |
| 1517 | return Ty; |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1518 | } |
| 1519 | |
Reid Spencer | 43f76c9 | 2007-01-29 05:41:09 +0000 | [diff] [blame] | 1520 | unsigned upgradeCallingConv(unsigned CC) { |
| 1521 | switch (CC) { |
| 1522 | case OldCallingConv::C : return CallingConv::C; |
| 1523 | case OldCallingConv::CSRet : return CallingConv::C; |
| 1524 | case OldCallingConv::Fast : return CallingConv::Fast; |
| 1525 | case OldCallingConv::Cold : return CallingConv::Cold; |
| 1526 | case OldCallingConv::X86_StdCall : return CallingConv::X86_StdCall; |
| 1527 | case OldCallingConv::X86_FastCall: return CallingConv::X86_FastCall; |
| 1528 | default: |
| 1529 | return CC; |
| 1530 | } |
| 1531 | } |
| 1532 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1533 | Module* UpgradeAssembly(const std::string &infile, std::istream& in, |
| 1534 | bool debug, bool addAttrs) |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 1535 | { |
| 1536 | Upgradelineno = 1; |
| 1537 | CurFilename = infile; |
Reid Spencer | 96839be | 2006-11-30 16:50:26 +0000 | [diff] [blame] | 1538 | LexInput = ∈ |
Reid Spencer | e77e35e | 2006-12-01 20:26:20 +0000 | [diff] [blame] | 1539 | yydebug = debug; |
Reid Spencer | 05e52a1 | 2006-12-31 05:45:57 +0000 | [diff] [blame] | 1540 | AddAttributes = addAttrs; |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1541 | ObsoleteVarArgs = false; |
| 1542 | NewVarArgs = false; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 1543 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1544 | CurModule.CurrentModule = new Module(CurFilename); |
| 1545 | |
| 1546 | // Check to make sure the parser succeeded |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 1547 | if (yyparse()) { |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1548 | if (ParserResult) |
| 1549 | delete ParserResult; |
Reid Spencer | e0a15bb | 2007-01-15 00:25:53 +0000 | [diff] [blame] | 1550 | std::cerr << "llvm-upgrade: parse failed.\n"; |
Reid Spencer | e0a15bb | 2007-01-15 00:25:53 +0000 | [diff] [blame] | 1551 | return 0; |
| 1552 | } |
| 1553 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1554 | // Check to make sure that parsing produced a result |
| 1555 | if (!ParserResult) { |
| 1556 | std::cerr << "llvm-upgrade: no parse result.\n"; |
| 1557 | return 0; |
Reid Spencer | e0a15bb | 2007-01-15 00:25:53 +0000 | [diff] [blame] | 1558 | } |
| 1559 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1560 | // Reset ParserResult variable while saving its value for the result. |
| 1561 | Module *Result = ParserResult; |
| 1562 | ParserResult = 0; |
Reid Spencer | e0a15bb | 2007-01-15 00:25:53 +0000 | [diff] [blame] | 1563 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1564 | //Not all functions use vaarg, so make a second check for ObsoleteVarArgs |
Reid Spencer | e0a15bb | 2007-01-15 00:25:53 +0000 | [diff] [blame] | 1565 | { |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1566 | Function* F; |
Reid Spencer | 688b049 | 2007-02-05 21:19:13 +0000 | [diff] [blame] | 1567 | if ((F = Result->getFunction("llvm.va_start")) |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1568 | && F->getFunctionType()->getNumParams() == 0) |
| 1569 | ObsoleteVarArgs = true; |
Reid Spencer | 688b049 | 2007-02-05 21:19:13 +0000 | [diff] [blame] | 1570 | if((F = Result->getFunction("llvm.va_copy")) |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1571 | && F->getFunctionType()->getNumParams() == 1) |
| 1572 | ObsoleteVarArgs = true; |
Reid Spencer | 280d801 | 2006-12-01 23:40:53 +0000 | [diff] [blame] | 1573 | } |
Reid Spencer | 90eb4d6 | 2007-01-05 17:18:58 +0000 | [diff] [blame] | 1574 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1575 | if (ObsoleteVarArgs && NewVarArgs) { |
| 1576 | error("This file is corrupt: it uses both new and old style varargs"); |
| 1577 | return 0; |
Reid Spencer | 90eb4d6 | 2007-01-05 17:18:58 +0000 | [diff] [blame] | 1578 | } |
Reid Spencer | 90eb4d6 | 2007-01-05 17:18:58 +0000 | [diff] [blame] | 1579 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1580 | if(ObsoleteVarArgs) { |
Reid Spencer | 688b049 | 2007-02-05 21:19:13 +0000 | [diff] [blame] | 1581 | if(Function* F = Result->getFunction("llvm.va_start")) { |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1582 | if (F->arg_size() != 0) { |
| 1583 | error("Obsolete va_start takes 0 argument"); |
Reid Spencer | 90eb4d6 | 2007-01-05 17:18:58 +0000 | [diff] [blame] | 1584 | return 0; |
| 1585 | } |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1586 | |
| 1587 | //foo = va_start() |
| 1588 | // -> |
| 1589 | //bar = alloca typeof(foo) |
| 1590 | //va_start(bar) |
| 1591 | //foo = load bar |
Reid Spencer | 90eb4d6 | 2007-01-05 17:18:58 +0000 | [diff] [blame] | 1592 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1593 | const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID); |
| 1594 | const Type* ArgTy = F->getFunctionType()->getReturnType(); |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 1595 | const Type* ArgTyPtr = PointerType::getUnqual(ArgTy); |
Duncan Sands | e2c4304 | 2008-04-07 13:45:04 +0000 | [diff] [blame] | 1596 | Function* NF = Intrinsic::getDeclaration(Result, Intrinsic::va_start); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1597 | |
| 1598 | while (!F->use_empty()) { |
| 1599 | CallInst* CI = cast<CallInst>(F->use_back()); |
| 1600 | AllocaInst* bar = new AllocaInst(ArgTy, 0, "vastart.fix.1", CI); |
| 1601 | new CallInst(NF, bar, "", CI); |
| 1602 | Value* foo = new LoadInst(bar, "vastart.fix.2", CI); |
| 1603 | CI->replaceAllUsesWith(foo); |
| 1604 | CI->getParent()->getInstList().erase(CI); |
Reid Spencer | cb03b5a | 2007-01-06 06:03:09 +0000 | [diff] [blame] | 1605 | } |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1606 | Result->getFunctionList().erase(F); |
Reid Spencer | cb03b5a | 2007-01-06 06:03:09 +0000 | [diff] [blame] | 1607 | } |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1608 | |
Reid Spencer | 688b049 | 2007-02-05 21:19:13 +0000 | [diff] [blame] | 1609 | if(Function* F = Result->getFunction("llvm.va_end")) { |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1610 | if(F->arg_size() != 1) { |
| 1611 | error("Obsolete va_end takes 1 argument"); |
| 1612 | return 0; |
Reid Spencer | cb03b5a | 2007-01-06 06:03:09 +0000 | [diff] [blame] | 1613 | } |
Reid Spencer | cb03b5a | 2007-01-06 06:03:09 +0000 | [diff] [blame] | 1614 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1615 | //vaend foo |
| 1616 | // -> |
| 1617 | //bar = alloca 1 of typeof(foo) |
| 1618 | //vaend bar |
| 1619 | const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID); |
| 1620 | const Type* ArgTy = F->getFunctionType()->getParamType(0); |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 1621 | const Type* ArgTyPtr = PointerType::getUnqual(ArgTy); |
Duncan Sands | e2c4304 | 2008-04-07 13:45:04 +0000 | [diff] [blame] | 1622 | Function* NF = Intrinsic::getDeclaration(Result, Intrinsic::va_end); |
Reid Spencer | cb03b5a | 2007-01-06 06:03:09 +0000 | [diff] [blame] | 1623 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1624 | while (!F->use_empty()) { |
| 1625 | CallInst* CI = cast<CallInst>(F->use_back()); |
| 1626 | AllocaInst* bar = new AllocaInst(ArgTy, 0, "vaend.fix.1", CI); |
| 1627 | new StoreInst(CI->getOperand(1), bar, CI); |
| 1628 | new CallInst(NF, bar, "", CI); |
| 1629 | CI->getParent()->getInstList().erase(CI); |
Reid Spencer | e77e35e | 2006-12-01 20:26:20 +0000 | [diff] [blame] | 1630 | } |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1631 | Result->getFunctionList().erase(F); |
Reid Spencer | e77e35e | 2006-12-01 20:26:20 +0000 | [diff] [blame] | 1632 | } |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1633 | |
Reid Spencer | 688b049 | 2007-02-05 21:19:13 +0000 | [diff] [blame] | 1634 | if(Function* F = Result->getFunction("llvm.va_copy")) { |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1635 | if(F->arg_size() != 1) { |
| 1636 | error("Obsolete va_copy takes 1 argument"); |
| 1637 | return 0; |
Reid Spencer | e77e35e | 2006-12-01 20:26:20 +0000 | [diff] [blame] | 1638 | } |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1639 | //foo = vacopy(bar) |
| 1640 | // -> |
| 1641 | //a = alloca 1 of typeof(foo) |
| 1642 | //b = alloca 1 of typeof(foo) |
| 1643 | //store bar -> b |
| 1644 | //vacopy(a, b) |
| 1645 | //foo = load a |
| 1646 | |
| 1647 | const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID); |
| 1648 | const Type* ArgTy = F->getFunctionType()->getReturnType(); |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 1649 | const Type* ArgTyPtr = PointerType::getUnqual(ArgTy); |
Duncan Sands | e2c4304 | 2008-04-07 13:45:04 +0000 | [diff] [blame] | 1650 | Function* NF = Intrinsic::getDeclaration(Result, Intrinsic::va_copy); |
Reid Spencer | e77e35e | 2006-12-01 20:26:20 +0000 | [diff] [blame] | 1651 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1652 | while (!F->use_empty()) { |
| 1653 | CallInst* CI = cast<CallInst>(F->use_back()); |
David Greene | ba1a750 | 2007-08-07 16:57:55 +0000 | [diff] [blame] | 1654 | Value *Args[2] = { |
| 1655 | new AllocaInst(ArgTy, 0, "vacopy.fix.1", CI), |
| 1656 | new AllocaInst(ArgTy, 0, "vacopy.fix.2", CI) |
| 1657 | }; |
David Greene | 52eec54 | 2007-08-01 03:43:44 +0000 | [diff] [blame] | 1658 | new StoreInst(CI->getOperand(1), Args[1], CI); |
David Greene | ba1a750 | 2007-08-07 16:57:55 +0000 | [diff] [blame] | 1659 | new CallInst(NF, Args, Args + 2, "", CI); |
David Greene | 52eec54 | 2007-08-01 03:43:44 +0000 | [diff] [blame] | 1660 | Value* foo = new LoadInst(Args[0], "vacopy.fix.3", CI); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1661 | CI->replaceAllUsesWith(foo); |
| 1662 | CI->getParent()->getInstList().erase(CI); |
| 1663 | } |
| 1664 | Result->getFunctionList().erase(F); |
Reid Spencer | 90eb4d6 | 2007-01-05 17:18:58 +0000 | [diff] [blame] | 1665 | } |
| 1666 | } |
| 1667 | |
Reid Spencer | 2b8036e | 2007-01-02 05:44:33 +0000 | [diff] [blame] | 1668 | return Result; |
| 1669 | } |
| 1670 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1671 | } // end llvm namespace |
Reid Spencer | 90eb4d6 | 2007-01-05 17:18:58 +0000 | [diff] [blame] | 1672 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1673 | using namespace llvm; |
Reid Spencer | e0a15bb | 2007-01-15 00:25:53 +0000 | [diff] [blame] | 1674 | |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 1675 | %} |
| 1676 | |
Reid Spencer | e77e35e | 2006-12-01 20:26:20 +0000 | [diff] [blame] | 1677 | %union { |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1678 | llvm::Module *ModuleVal; |
| 1679 | llvm::Function *FunctionVal; |
| 1680 | std::pair<llvm::PATypeInfo, char*> *ArgVal; |
| 1681 | llvm::BasicBlock *BasicBlockVal; |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 1682 | llvm::TermInstInfo TermInstVal; |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1683 | llvm::InstrInfo InstVal; |
| 1684 | llvm::ConstInfo ConstVal; |
| 1685 | llvm::ValueInfo ValueVal; |
| 1686 | llvm::PATypeInfo TypeVal; |
| 1687 | llvm::TypeInfo PrimType; |
| 1688 | llvm::PHIListInfo PHIList; |
| 1689 | std::list<llvm::PATypeInfo> *TypeList; |
| 1690 | std::vector<llvm::ValueInfo> *ValueList; |
| 1691 | std::vector<llvm::ConstInfo> *ConstVector; |
| 1692 | |
| 1693 | |
| 1694 | std::vector<std::pair<llvm::PATypeInfo,char*> > *ArgList; |
| 1695 | // Represent the RHS of PHI node |
| 1696 | std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable; |
| 1697 | |
| 1698 | llvm::GlobalValue::LinkageTypes Linkage; |
| 1699 | int64_t SInt64Val; |
| 1700 | uint64_t UInt64Val; |
| 1701 | int SIntVal; |
| 1702 | unsigned UIntVal; |
Dale Johannesen | 43421b3 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 1703 | llvm::APFloat *FPVal; |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1704 | bool BoolVal; |
| 1705 | |
| 1706 | char *StrVal; // This memory is strdup'd! |
| 1707 | llvm::ValID ValIDVal; // strdup'd memory maybe! |
| 1708 | |
| 1709 | llvm::BinaryOps BinaryOpVal; |
| 1710 | llvm::TermOps TermOpVal; |
| 1711 | llvm::MemoryOps MemOpVal; |
| 1712 | llvm::OtherOps OtherOpVal; |
| 1713 | llvm::CastOps CastOpVal; |
| 1714 | llvm::ICmpInst::Predicate IPred; |
| 1715 | llvm::FCmpInst::Predicate FPred; |
| 1716 | llvm::Module::Endianness Endianness; |
Reid Spencer | e77e35e | 2006-12-01 20:26:20 +0000 | [diff] [blame] | 1717 | } |
| 1718 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1719 | %type <ModuleVal> Module FunctionList |
| 1720 | %type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList |
| 1721 | %type <BasicBlockVal> BasicBlock InstructionList |
| 1722 | %type <TermInstVal> BBTerminatorInst |
| 1723 | %type <InstVal> Inst InstVal MemoryInst |
| 1724 | %type <ConstVal> ConstVal ConstExpr |
| 1725 | %type <ConstVector> ConstVector |
| 1726 | %type <ArgList> ArgList ArgListH |
| 1727 | %type <ArgVal> ArgVal |
| 1728 | %type <PHIList> PHIList |
| 1729 | %type <ValueList> ValueRefList ValueRefListE // For call param lists |
| 1730 | %type <ValueList> IndexList // For GEP derived indices |
| 1731 | %type <TypeList> TypeListI ArgTypeListI |
| 1732 | %type <JumpTable> JumpTable |
| 1733 | %type <BoolVal> GlobalType // GLOBAL or CONSTANT? |
| 1734 | %type <BoolVal> OptVolatile // 'volatile' or not |
| 1735 | %type <BoolVal> OptTailCall // TAIL CALL or plain CALL. |
| 1736 | %type <BoolVal> OptSideEffect // 'sideeffect' or not. |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 1737 | %type <Linkage> OptLinkage FnDeclareLinkage |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1738 | %type <Endianness> BigOrLittle |
Reid Spencer | e77e35e | 2006-12-01 20:26:20 +0000 | [diff] [blame] | 1739 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1740 | // ValueRef - Unresolved reference to a definition or BB |
| 1741 | %type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef |
| 1742 | %type <ValueVal> ResolvedVal // <type> <valref> pair |
Reid Spencer | 9373d27 | 2007-01-26 17:13:53 +0000 | [diff] [blame] | 1743 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1744 | // Tokens and types for handling constant integer values |
| 1745 | // |
| 1746 | // ESINT64VAL - A negative number within long long range |
| 1747 | %token <SInt64Val> ESINT64VAL |
Reid Spencer | e77e35e | 2006-12-01 20:26:20 +0000 | [diff] [blame] | 1748 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1749 | // EUINT64VAL - A positive number within uns. long long range |
| 1750 | %token <UInt64Val> EUINT64VAL |
| 1751 | %type <SInt64Val> EINT64VAL |
Reid Spencer | e77e35e | 2006-12-01 20:26:20 +0000 | [diff] [blame] | 1752 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1753 | %token <SIntVal> SINTVAL // Signed 32 bit ints... |
| 1754 | %token <UIntVal> UINTVAL // Unsigned 32 bit ints... |
| 1755 | %type <SIntVal> INTVAL |
| 1756 | %token <FPVal> FPVAL // Float or Double constant |
Reid Spencer | e77e35e | 2006-12-01 20:26:20 +0000 | [diff] [blame] | 1757 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1758 | // Built in types... |
| 1759 | %type <TypeVal> Types TypesV UpRTypes UpRTypesV |
| 1760 | %type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications |
| 1761 | %token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG |
| 1762 | %token <PrimType> FLOAT DOUBLE TYPE LABEL |
Reid Spencer | e77e35e | 2006-12-01 20:26:20 +0000 | [diff] [blame] | 1763 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1764 | %token <StrVal> VAR_ID LABELSTR STRINGCONSTANT |
| 1765 | %type <StrVal> Name OptName OptAssign |
| 1766 | %type <UIntVal> OptAlign OptCAlign |
| 1767 | %type <StrVal> OptSection SectionString |
| 1768 | |
| 1769 | %token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK |
| 1770 | %token DECLARE GLOBAL CONSTANT SECTION VOLATILE |
| 1771 | %token TO DOTDOTDOT NULL_TOK UNDEF CONST INTERNAL LINKONCE WEAK APPENDING |
| 1772 | %token DLLIMPORT DLLEXPORT EXTERN_WEAK |
| 1773 | %token OPAQUE NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG ALIGN |
| 1774 | %token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT |
| 1775 | %token CC_TOK CCC_TOK CSRETCC_TOK FASTCC_TOK COLDCC_TOK |
| 1776 | %token X86_STDCALLCC_TOK X86_FASTCALLCC_TOK |
| 1777 | %token DATALAYOUT |
| 1778 | %type <UIntVal> OptCallingConv |
| 1779 | |
| 1780 | // Basic Block Terminating Operators |
| 1781 | %token <TermOpVal> RET BR SWITCH INVOKE UNREACHABLE |
| 1782 | %token UNWIND EXCEPT |
| 1783 | |
| 1784 | // Binary Operators |
| 1785 | %type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1786 | %type <BinaryOpVal> ShiftOps |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1787 | %token <BinaryOpVal> ADD SUB MUL DIV UDIV SDIV FDIV REM UREM SREM FREM |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1788 | %token <BinaryOpVal> AND OR XOR SHL SHR ASHR LSHR |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1789 | %token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comparators |
| 1790 | %token <OtherOpVal> ICMP FCMP |
| 1791 | |
| 1792 | // Memory Instructions |
| 1793 | %token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR |
| 1794 | |
| 1795 | // Other Operators |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1796 | %token <OtherOpVal> PHI_TOK SELECT VAARG |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1797 | %token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR |
| 1798 | %token VAARG_old VANEXT_old //OBSOLETE |
| 1799 | |
Reid Spencer | 9373d27 | 2007-01-26 17:13:53 +0000 | [diff] [blame] | 1800 | // Support for ICmp/FCmp Predicates, which is 1.9++ but not 2.0 |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1801 | %type <IPred> IPredicates |
| 1802 | %type <FPred> FPredicates |
| 1803 | %token EQ NE SLT SGT SLE SGE ULT UGT ULE UGE |
| 1804 | %token OEQ ONE OLT OGT OLE OGE ORD UNO UEQ UNE |
| 1805 | |
| 1806 | %token <CastOpVal> CAST TRUNC ZEXT SEXT FPTRUNC FPEXT FPTOUI FPTOSI |
| 1807 | %token <CastOpVal> UITOFP SITOFP PTRTOINT INTTOPTR BITCAST |
| 1808 | %type <CastOpVal> CastOps |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 1809 | |
| 1810 | %start Module |
| 1811 | |
| 1812 | %% |
| 1813 | |
| 1814 | // Handle constant integer size restriction and conversion... |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1815 | // |
| 1816 | INTVAL |
Reid Spencer | 7e97288 | 2007-01-26 18:26:23 +0000 | [diff] [blame] | 1817 | : SINTVAL |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1818 | | UINTVAL { |
| 1819 | if ($1 > (uint32_t)INT32_MAX) // Outside of my range! |
| 1820 | error("Value too large for type"); |
| 1821 | $$ = (int32_t)$1; |
| 1822 | } |
| 1823 | ; |
| 1824 | |
| 1825 | EINT64VAL |
Reid Spencer | 7e97288 | 2007-01-26 18:26:23 +0000 | [diff] [blame] | 1826 | : ESINT64VAL // These have same type and can't cause problems... |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1827 | | EUINT64VAL { |
| 1828 | if ($1 > (uint64_t)INT64_MAX) // Outside of my range! |
| 1829 | error("Value too large for type"); |
| 1830 | $$ = (int64_t)$1; |
| 1831 | }; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 1832 | |
| 1833 | // Operations that are notably excluded from this list include: |
| 1834 | // RET, BR, & SWITCH because they end basic blocks and are treated specially. |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1835 | // |
| 1836 | ArithmeticOps |
| 1837 | : ADD | SUB | MUL | DIV | UDIV | SDIV | FDIV | REM | UREM | SREM | FREM |
| 1838 | ; |
| 1839 | |
| 1840 | LogicalOps |
| 1841 | : AND | OR | XOR |
| 1842 | ; |
| 1843 | |
| 1844 | SetCondOps |
| 1845 | : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE |
| 1846 | ; |
| 1847 | |
| 1848 | IPredicates |
| 1849 | : EQ { $$ = ICmpInst::ICMP_EQ; } | NE { $$ = ICmpInst::ICMP_NE; } |
| 1850 | | SLT { $$ = ICmpInst::ICMP_SLT; } | SGT { $$ = ICmpInst::ICMP_SGT; } |
| 1851 | | SLE { $$ = ICmpInst::ICMP_SLE; } | SGE { $$ = ICmpInst::ICMP_SGE; } |
| 1852 | | ULT { $$ = ICmpInst::ICMP_ULT; } | UGT { $$ = ICmpInst::ICMP_UGT; } |
| 1853 | | ULE { $$ = ICmpInst::ICMP_ULE; } | UGE { $$ = ICmpInst::ICMP_UGE; } |
| 1854 | ; |
| 1855 | |
| 1856 | FPredicates |
| 1857 | : OEQ { $$ = FCmpInst::FCMP_OEQ; } | ONE { $$ = FCmpInst::FCMP_ONE; } |
| 1858 | | OLT { $$ = FCmpInst::FCMP_OLT; } | OGT { $$ = FCmpInst::FCMP_OGT; } |
| 1859 | | OLE { $$ = FCmpInst::FCMP_OLE; } | OGE { $$ = FCmpInst::FCMP_OGE; } |
| 1860 | | ORD { $$ = FCmpInst::FCMP_ORD; } | UNO { $$ = FCmpInst::FCMP_UNO; } |
| 1861 | | UEQ { $$ = FCmpInst::FCMP_UEQ; } | UNE { $$ = FCmpInst::FCMP_UNE; } |
| 1862 | | ULT { $$ = FCmpInst::FCMP_ULT; } | UGT { $$ = FCmpInst::FCMP_UGT; } |
| 1863 | | ULE { $$ = FCmpInst::FCMP_ULE; } | UGE { $$ = FCmpInst::FCMP_UGE; } |
| 1864 | | TRUETOK { $$ = FCmpInst::FCMP_TRUE; } |
| 1865 | | FALSETOK { $$ = FCmpInst::FCMP_FALSE; } |
| 1866 | ; |
| 1867 | ShiftOps |
| 1868 | : SHL | SHR | ASHR | LSHR |
| 1869 | ; |
| 1870 | |
| 1871 | CastOps |
| 1872 | : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | FPTOUI | FPTOSI |
| 1873 | | UITOFP | SITOFP | PTRTOINT | INTTOPTR | BITCAST | CAST |
| 1874 | ; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 1875 | |
| 1876 | // These are some types that allow classification if we only want a particular |
| 1877 | // thing... for example, only a signed, unsigned, or integral type. |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1878 | SIntType |
| 1879 | : LONG | INT | SHORT | SBYTE |
| 1880 | ; |
| 1881 | |
| 1882 | UIntType |
| 1883 | : ULONG | UINT | USHORT | UBYTE |
| 1884 | ; |
| 1885 | |
| 1886 | IntType |
| 1887 | : SIntType | UIntType |
| 1888 | ; |
| 1889 | |
| 1890 | FPType |
| 1891 | : FLOAT | DOUBLE |
| 1892 | ; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 1893 | |
| 1894 | // OptAssign - Value producing statements have an optional assignment component |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1895 | OptAssign |
| 1896 | : Name '=' { |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 1897 | $$ = $1; |
| 1898 | } |
| 1899 | | /*empty*/ { |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1900 | $$ = 0; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 1901 | }; |
| 1902 | |
| 1903 | OptLinkage |
Reid Spencer | 0ec8200 | 2007-02-08 00:21:06 +0000 | [diff] [blame] | 1904 | : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1905 | | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
| 1906 | | WEAK { $$ = GlobalValue::WeakLinkage; } |
| 1907 | | APPENDING { $$ = GlobalValue::AppendingLinkage; } |
| 1908 | | DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; } |
| 1909 | | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; } |
Reid Spencer | 0ec8200 | 2007-02-08 00:21:06 +0000 | [diff] [blame] | 1910 | | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; } |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1911 | | /*empty*/ { $$ = GlobalValue::ExternalLinkage; } |
| 1912 | ; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 1913 | |
| 1914 | OptCallingConv |
Reid Spencer | 4f859aa | 2007-04-22 05:46:44 +0000 | [diff] [blame] | 1915 | : /*empty*/ { $$ = lastCallingConv = OldCallingConv::C; } |
| 1916 | | CCC_TOK { $$ = lastCallingConv = OldCallingConv::C; } |
| 1917 | | CSRETCC_TOK { $$ = lastCallingConv = OldCallingConv::CSRet; } |
| 1918 | | FASTCC_TOK { $$ = lastCallingConv = OldCallingConv::Fast; } |
| 1919 | | COLDCC_TOK { $$ = lastCallingConv = OldCallingConv::Cold; } |
| 1920 | | X86_STDCALLCC_TOK { $$ = lastCallingConv = OldCallingConv::X86_StdCall; } |
| 1921 | | X86_FASTCALLCC_TOK { $$ = lastCallingConv = OldCallingConv::X86_FastCall; } |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1922 | | CC_TOK EUINT64VAL { |
| 1923 | if ((unsigned)$2 != $2) |
| 1924 | error("Calling conv too large"); |
Reid Spencer | 4f859aa | 2007-04-22 05:46:44 +0000 | [diff] [blame] | 1925 | $$ = lastCallingConv = $2; |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1926 | } |
| 1927 | ; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 1928 | |
| 1929 | // OptAlign/OptCAlign - An optional alignment, and an optional alignment with |
| 1930 | // a comma before it. |
| 1931 | OptAlign |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1932 | : /*empty*/ { $$ = 0; } |
| 1933 | | ALIGN EUINT64VAL { |
| 1934 | $$ = $2; |
| 1935 | if ($$ != 0 && !isPowerOf2_32($$)) |
| 1936 | error("Alignment must be a power of two"); |
| 1937 | } |
| 1938 | ; |
Jim Laskey | 98ba588 | 2006-12-06 10:57:33 +0000 | [diff] [blame] | 1939 | |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 1940 | OptCAlign |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1941 | : /*empty*/ { $$ = 0; } |
| 1942 | | ',' ALIGN EUINT64VAL { |
| 1943 | $$ = $3; |
| 1944 | if ($$ != 0 && !isPowerOf2_32($$)) |
| 1945 | error("Alignment must be a power of two"); |
| 1946 | } |
| 1947 | ; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 1948 | |
| 1949 | SectionString |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1950 | : SECTION STRINGCONSTANT { |
| 1951 | for (unsigned i = 0, e = strlen($2); i != e; ++i) |
| 1952 | if ($2[i] == '"' || $2[i] == '\\') |
| 1953 | error("Invalid character in section name"); |
| 1954 | $$ = $2; |
| 1955 | } |
| 1956 | ; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 1957 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1958 | OptSection |
| 1959 | : /*empty*/ { $$ = 0; } |
| 1960 | | SectionString { $$ = $1; } |
| 1961 | ; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 1962 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1963 | // GlobalVarAttributes - Used to pass the attributes string on a global. CurGV |
| 1964 | // is set to be the global we are processing. |
| 1965 | // |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 1966 | GlobalVarAttributes |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1967 | : /* empty */ {} |
| 1968 | | ',' GlobalVarAttribute GlobalVarAttributes {} |
| 1969 | ; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 1970 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1971 | GlobalVarAttribute |
| 1972 | : SectionString { |
| 1973 | CurGV->setSection($1); |
| 1974 | free($1); |
| 1975 | } |
| 1976 | | ALIGN EUINT64VAL { |
| 1977 | if ($2 != 0 && !isPowerOf2_32($2)) |
| 1978 | error("Alignment must be a power of two"); |
| 1979 | CurGV->setAlignment($2); |
| 1980 | |
| 1981 | } |
| 1982 | ; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 1983 | |
| 1984 | //===----------------------------------------------------------------------===// |
| 1985 | // Types includes all predefined types... except void, because it can only be |
| 1986 | // used in specific contexts (function returning void for example). To have |
| 1987 | // access to it, a user must explicitly use TypesV. |
| 1988 | // |
| 1989 | |
| 1990 | // TypesV includes all of 'Types', but it also includes the void type. |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1991 | TypesV |
| 1992 | : Types |
| 1993 | | VOID { |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 1994 | $$.PAT = new PATypeHolder($1.T); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 1995 | $$.S.makeSignless(); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1996 | } |
| 1997 | ; |
| 1998 | |
| 1999 | UpRTypesV |
| 2000 | : UpRTypes |
| 2001 | | VOID { |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2002 | $$.PAT = new PATypeHolder($1.T); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2003 | $$.S.makeSignless(); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2004 | } |
| 2005 | ; |
| 2006 | |
| 2007 | Types |
| 2008 | : UpRTypes { |
| 2009 | if (!UpRefs.empty()) |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2010 | error("Invalid upreference in type: " + (*$1.PAT)->getDescription()); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2011 | $$ = $1; |
| 2012 | } |
| 2013 | ; |
| 2014 | |
| 2015 | PrimType |
| 2016 | : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT |
| 2017 | | LONG | ULONG | FLOAT | DOUBLE | LABEL |
| 2018 | ; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2019 | |
| 2020 | // Derived types are added later... |
Reid Spencer | a50d596 | 2006-12-02 04:11:07 +0000 | [diff] [blame] | 2021 | UpRTypes |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2022 | : PrimType { |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2023 | $$.PAT = new PATypeHolder($1.T); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2024 | $$.S.copy($1.S); |
Reid Spencer | a50d596 | 2006-12-02 04:11:07 +0000 | [diff] [blame] | 2025 | } |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2026 | | OPAQUE { |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2027 | $$.PAT = new PATypeHolder(OpaqueType::get()); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2028 | $$.S.makeSignless(); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2029 | } |
| 2030 | | SymbolicValueRef { // Named types are also simple types... |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2031 | $$.S.copy(getTypeSign($1)); |
Reid Spencer | 9373d27 | 2007-01-26 17:13:53 +0000 | [diff] [blame] | 2032 | const Type* tmp = getType($1); |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2033 | $$.PAT = new PATypeHolder(tmp); |
Reid Spencer | a8ca090 | 2006-12-02 20:19:56 +0000 | [diff] [blame] | 2034 | } |
| 2035 | | '\\' EUINT64VAL { // Type UpReference |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2036 | if ($2 > (uint64_t)~0U) |
| 2037 | error("Value out of range"); |
| 2038 | OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder |
| 2039 | UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector... |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2040 | $$.PAT = new PATypeHolder(OT); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2041 | $$.S.makeSignless(); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2042 | UR_OUT("New Upreference!\n"); |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2043 | } |
| 2044 | | UpRTypesV '(' ArgTypeListI ')' { // Function derived type? |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2045 | $$.S.makeComposite($1.S); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2046 | std::vector<const Type*> Params; |
| 2047 | for (std::list<llvm::PATypeInfo>::iterator I = $3->begin(), |
| 2048 | E = $3->end(); I != E; ++I) { |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2049 | Params.push_back(I->PAT->get()); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2050 | $$.S.add(I->S); |
Reid Spencer | 2b8036e | 2007-01-02 05:44:33 +0000 | [diff] [blame] | 2051 | } |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2052 | bool isVarArg = Params.size() && Params.back() == Type::VoidTy; |
| 2053 | if (isVarArg) Params.pop_back(); |
| 2054 | |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 2055 | PAListPtr PAL; |
Reid Spencer | 4f859aa | 2007-04-22 05:46:44 +0000 | [diff] [blame] | 2056 | if (lastCallingConv == OldCallingConv::CSRet) { |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 2057 | ParamAttrsWithIndex PAWI = |
| 2058 | ParamAttrsWithIndex::get(1, ParamAttr::StructRet); |
| 2059 | PAL = PAListPtr::get(&PAWI, 1); |
Reid Spencer | 4f859aa | 2007-04-22 05:46:44 +0000 | [diff] [blame] | 2060 | } |
| 2061 | |
Reid Spencer | 460dd9b | 2007-04-09 06:15:59 +0000 | [diff] [blame] | 2062 | const FunctionType *FTy = |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 2063 | FunctionType::get($1.PAT->get(), Params, isVarArg); |
Reid Spencer | 460dd9b | 2007-04-09 06:15:59 +0000 | [diff] [blame] | 2064 | |
| 2065 | $$.PAT = new PATypeHolder( HandleUpRefs(FTy, $$.S) ); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2066 | delete $1.PAT; // Delete the return type handle |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2067 | delete $3; // Delete the argument list |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2068 | } |
| 2069 | | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type? |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2070 | $$.S.makeComposite($4.S); |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2071 | $$.PAT = new PATypeHolder(HandleUpRefs(ArrayType::get($4.PAT->get(), |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2072 | (unsigned)$2), $$.S)); |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2073 | delete $4.PAT; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2074 | } |
Reid Spencer | ac9dcb9 | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 2075 | | '<' EUINT64VAL 'x' UpRTypes '>' { // Vector type? |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2076 | const llvm::Type* ElemTy = $4.PAT->get(); |
| 2077 | if ((unsigned)$2 != $2) |
| 2078 | error("Unsigned result not equal to signed result"); |
| 2079 | if (!(ElemTy->isInteger() || ElemTy->isFloatingPoint())) |
| 2080 | error("Elements of a VectorType must be integer or floating point"); |
| 2081 | if (!isPowerOf2_32($2)) |
| 2082 | error("VectorType length should be a power of 2"); |
| 2083 | $$.S.makeComposite($4.S); |
| 2084 | $$.PAT = new PATypeHolder(HandleUpRefs(VectorType::get(ElemTy, |
| 2085 | (unsigned)$2), $$.S)); |
| 2086 | delete $4.PAT; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2087 | } |
| 2088 | | '{' TypeListI '}' { // Structure type? |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2089 | std::vector<const Type*> Elements; |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2090 | $$.S.makeComposite(); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2091 | for (std::list<llvm::PATypeInfo>::iterator I = $2->begin(), |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2092 | E = $2->end(); I != E; ++I) { |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2093 | Elements.push_back(I->PAT->get()); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2094 | $$.S.add(I->S); |
| 2095 | } |
| 2096 | $$.PAT = new PATypeHolder(HandleUpRefs(StructType::get(Elements), $$.S)); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2097 | delete $2; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2098 | } |
| 2099 | | '{' '}' { // Empty structure type? |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2100 | $$.PAT = new PATypeHolder(StructType::get(std::vector<const Type*>())); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2101 | $$.S.makeComposite(); |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2102 | } |
Reid Spencer | a9d7e89 | 2006-12-29 20:33:37 +0000 | [diff] [blame] | 2103 | | '<' '{' TypeListI '}' '>' { // Packed Structure type? |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2104 | $$.S.makeComposite(); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2105 | std::vector<const Type*> Elements; |
| 2106 | for (std::list<llvm::PATypeInfo>::iterator I = $3->begin(), |
| 2107 | E = $3->end(); I != E; ++I) { |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2108 | Elements.push_back(I->PAT->get()); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2109 | $$.S.add(I->S); |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2110 | delete I->PAT; |
Reid Spencer | 2b8036e | 2007-01-02 05:44:33 +0000 | [diff] [blame] | 2111 | } |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2112 | $$.PAT = new PATypeHolder(HandleUpRefs(StructType::get(Elements, true), |
| 2113 | $$.S)); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2114 | delete $3; |
Reid Spencer | a9d7e89 | 2006-12-29 20:33:37 +0000 | [diff] [blame] | 2115 | } |
| 2116 | | '<' '{' '}' '>' { // Empty packed structure type? |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2117 | $$.PAT = new PATypeHolder(StructType::get(std::vector<const Type*>(),true)); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2118 | $$.S.makeComposite(); |
Reid Spencer | a9d7e89 | 2006-12-29 20:33:37 +0000 | [diff] [blame] | 2119 | } |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2120 | | UpRTypes '*' { // Pointer type? |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2121 | if ($1.PAT->get() == Type::LabelTy) |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2122 | error("Cannot form a pointer to a basic block"); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2123 | $$.S.makeComposite($1.S); |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 2124 | $$.PAT = new |
| 2125 | PATypeHolder(HandleUpRefs(PointerType::getUnqual($1.PAT->get()), |
| 2126 | $$.S)); |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2127 | delete $1.PAT; |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2128 | } |
| 2129 | ; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2130 | |
| 2131 | // TypeList - Used for struct declarations and as a basis for function type |
| 2132 | // declaration type lists |
| 2133 | // |
Reid Spencer | e77e35e | 2006-12-01 20:26:20 +0000 | [diff] [blame] | 2134 | TypeListI |
| 2135 | : UpRTypes { |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2136 | $$ = new std::list<PATypeInfo>(); |
| 2137 | $$->push_back($1); |
Reid Spencer | e77e35e | 2006-12-01 20:26:20 +0000 | [diff] [blame] | 2138 | } |
| 2139 | | TypeListI ',' UpRTypes { |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2140 | ($$=$1)->push_back($3); |
| 2141 | } |
| 2142 | ; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2143 | |
| 2144 | // ArgTypeList - List of types for a function type declaration... |
Reid Spencer | e77e35e | 2006-12-01 20:26:20 +0000 | [diff] [blame] | 2145 | ArgTypeListI |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2146 | : TypeListI |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2147 | | TypeListI ',' DOTDOTDOT { |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2148 | PATypeInfo VoidTI; |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2149 | VoidTI.PAT = new PATypeHolder(Type::VoidTy); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2150 | VoidTI.S.makeSignless(); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2151 | ($$=$1)->push_back(VoidTI); |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2152 | } |
| 2153 | | DOTDOTDOT { |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2154 | $$ = new std::list<PATypeInfo>(); |
| 2155 | PATypeInfo VoidTI; |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2156 | VoidTI.PAT = new PATypeHolder(Type::VoidTy); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2157 | VoidTI.S.makeSignless(); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2158 | $$->push_back(VoidTI); |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2159 | } |
| 2160 | | /*empty*/ { |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2161 | $$ = new std::list<PATypeInfo>(); |
| 2162 | } |
| 2163 | ; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2164 | |
| 2165 | // ConstVal - The various declarations that go into the constant pool. This |
| 2166 | // production is used ONLY to represent constants that show up AFTER a 'const', |
| 2167 | // 'constant' or 'global' token at global scope. Constants that can be inlined |
| 2168 | // into other expressions (such as integers and constexprs) are handled by the |
| 2169 | // ResolvedVal, ValueRef and ConstValueRef productions. |
| 2170 | // |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2171 | ConstVal |
| 2172 | : Types '[' ConstVector ']' { // Nonempty unsized arr |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2173 | const ArrayType *ATy = dyn_cast<ArrayType>($1.PAT->get()); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2174 | if (ATy == 0) |
| 2175 | error("Cannot make array constant with type: '" + |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2176 | $1.PAT->get()->getDescription() + "'"); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2177 | const Type *ETy = ATy->getElementType(); |
| 2178 | int NumElements = ATy->getNumElements(); |
| 2179 | |
| 2180 | // Verify that we have the correct size... |
| 2181 | if (NumElements != -1 && NumElements != (int)$3->size()) |
| 2182 | error("Type mismatch: constant sized array initialized with " + |
| 2183 | utostr($3->size()) + " arguments, but has size of " + |
| 2184 | itostr(NumElements) + ""); |
| 2185 | |
| 2186 | // Verify all elements are correct type! |
| 2187 | std::vector<Constant*> Elems; |
| 2188 | for (unsigned i = 0; i < $3->size(); i++) { |
| 2189 | Constant *C = (*$3)[i].C; |
| 2190 | const Type* ValTy = C->getType(); |
| 2191 | if (ETy != ValTy) |
| 2192 | error("Element #" + utostr(i) + " is not of type '" + |
| 2193 | ETy->getDescription() +"' as required!\nIt is of type '"+ |
| 2194 | ValTy->getDescription() + "'"); |
| 2195 | Elems.push_back(C); |
| 2196 | } |
| 2197 | $$.C = ConstantArray::get(ATy, Elems); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2198 | $$.S.copy($1.S); |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2199 | delete $1.PAT; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2200 | delete $3; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2201 | } |
| 2202 | | Types '[' ']' { |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2203 | const ArrayType *ATy = dyn_cast<ArrayType>($1.PAT->get()); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2204 | if (ATy == 0) |
| 2205 | error("Cannot make array constant with type: '" + |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2206 | $1.PAT->get()->getDescription() + "'"); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2207 | int NumElements = ATy->getNumElements(); |
| 2208 | if (NumElements != -1 && NumElements != 0) |
| 2209 | error("Type mismatch: constant sized array initialized with 0" |
| 2210 | " arguments, but has size of " + itostr(NumElements) +""); |
| 2211 | $$.C = ConstantArray::get(ATy, std::vector<Constant*>()); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2212 | $$.S.copy($1.S); |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2213 | delete $1.PAT; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2214 | } |
| 2215 | | Types 'c' STRINGCONSTANT { |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2216 | const ArrayType *ATy = dyn_cast<ArrayType>($1.PAT->get()); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2217 | if (ATy == 0) |
| 2218 | error("Cannot make array constant with type: '" + |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2219 | $1.PAT->get()->getDescription() + "'"); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2220 | int NumElements = ATy->getNumElements(); |
| 2221 | const Type *ETy = dyn_cast<IntegerType>(ATy->getElementType()); |
| 2222 | if (!ETy || cast<IntegerType>(ETy)->getBitWidth() != 8) |
| 2223 | error("String arrays require type i8, not '" + ETy->getDescription() + |
| 2224 | "'"); |
| 2225 | char *EndStr = UnEscapeLexed($3, true); |
| 2226 | if (NumElements != -1 && NumElements != (EndStr-$3)) |
| 2227 | error("Can't build string constant of size " + |
| 2228 | itostr((int)(EndStr-$3)) + " when array has size " + |
| 2229 | itostr(NumElements) + ""); |
| 2230 | std::vector<Constant*> Vals; |
| 2231 | for (char *C = (char *)$3; C != (char *)EndStr; ++C) |
| 2232 | Vals.push_back(ConstantInt::get(ETy, *C)); |
| 2233 | free($3); |
| 2234 | $$.C = ConstantArray::get(ATy, Vals); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2235 | $$.S.copy($1.S); |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2236 | delete $1.PAT; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2237 | } |
| 2238 | | Types '<' ConstVector '>' { // Nonempty unsized arr |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 2239 | const VectorType *PTy = dyn_cast<VectorType>($1.PAT->get()); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2240 | if (PTy == 0) |
| 2241 | error("Cannot make packed constant with type: '" + |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2242 | $1.PAT->get()->getDescription() + "'"); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2243 | const Type *ETy = PTy->getElementType(); |
| 2244 | int NumElements = PTy->getNumElements(); |
| 2245 | // Verify that we have the correct size... |
| 2246 | if (NumElements != -1 && NumElements != (int)$3->size()) |
| 2247 | error("Type mismatch: constant sized packed initialized with " + |
| 2248 | utostr($3->size()) + " arguments, but has size of " + |
| 2249 | itostr(NumElements) + ""); |
| 2250 | // Verify all elements are correct type! |
| 2251 | std::vector<Constant*> Elems; |
| 2252 | for (unsigned i = 0; i < $3->size(); i++) { |
| 2253 | Constant *C = (*$3)[i].C; |
| 2254 | const Type* ValTy = C->getType(); |
| 2255 | if (ETy != ValTy) |
| 2256 | error("Element #" + utostr(i) + " is not of type '" + |
| 2257 | ETy->getDescription() +"' as required!\nIt is of type '"+ |
| 2258 | ValTy->getDescription() + "'"); |
| 2259 | Elems.push_back(C); |
| 2260 | } |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 2261 | $$.C = ConstantVector::get(PTy, Elems); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2262 | $$.S.copy($1.S); |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2263 | delete $1.PAT; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2264 | delete $3; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2265 | } |
| 2266 | | Types '{' ConstVector '}' { |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2267 | const StructType *STy = dyn_cast<StructType>($1.PAT->get()); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2268 | if (STy == 0) |
| 2269 | error("Cannot make struct constant with type: '" + |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2270 | $1.PAT->get()->getDescription() + "'"); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2271 | if ($3->size() != STy->getNumContainedTypes()) |
| 2272 | error("Illegal number of initializers for structure type"); |
| 2273 | |
| 2274 | // Check to ensure that constants are compatible with the type initializer! |
| 2275 | std::vector<Constant*> Fields; |
| 2276 | for (unsigned i = 0, e = $3->size(); i != e; ++i) { |
| 2277 | Constant *C = (*$3)[i].C; |
| 2278 | if (C->getType() != STy->getElementType(i)) |
| 2279 | error("Expected type '" + STy->getElementType(i)->getDescription() + |
| 2280 | "' for element #" + utostr(i) + " of structure initializer"); |
| 2281 | Fields.push_back(C); |
| 2282 | } |
| 2283 | $$.C = ConstantStruct::get(STy, Fields); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2284 | $$.S.copy($1.S); |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2285 | delete $1.PAT; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2286 | delete $3; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2287 | } |
| 2288 | | Types '{' '}' { |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2289 | const StructType *STy = dyn_cast<StructType>($1.PAT->get()); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2290 | if (STy == 0) |
| 2291 | error("Cannot make struct constant with type: '" + |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2292 | $1.PAT->get()->getDescription() + "'"); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2293 | if (STy->getNumContainedTypes() != 0) |
| 2294 | error("Illegal number of initializers for structure type"); |
| 2295 | $$.C = ConstantStruct::get(STy, std::vector<Constant*>()); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2296 | $$.S.copy($1.S); |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2297 | delete $1.PAT; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2298 | } |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2299 | | Types '<' '{' ConstVector '}' '>' { |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2300 | const StructType *STy = dyn_cast<StructType>($1.PAT->get()); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2301 | if (STy == 0) |
| 2302 | error("Cannot make packed struct constant with type: '" + |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2303 | $1.PAT->get()->getDescription() + "'"); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2304 | if ($4->size() != STy->getNumContainedTypes()) |
| 2305 | error("Illegal number of initializers for packed structure type"); |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2306 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2307 | // Check to ensure that constants are compatible with the type initializer! |
| 2308 | std::vector<Constant*> Fields; |
| 2309 | for (unsigned i = 0, e = $4->size(); i != e; ++i) { |
| 2310 | Constant *C = (*$4)[i].C; |
| 2311 | if (C->getType() != STy->getElementType(i)) |
| 2312 | error("Expected type '" + STy->getElementType(i)->getDescription() + |
| 2313 | "' for element #" + utostr(i) + " of packed struct initializer"); |
| 2314 | Fields.push_back(C); |
Reid Spencer | 280d801 | 2006-12-01 23:40:53 +0000 | [diff] [blame] | 2315 | } |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2316 | $$.C = ConstantStruct::get(STy, Fields); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2317 | $$.S.copy($1.S); |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2318 | delete $1.PAT; |
Reid Spencer | e77e35e | 2006-12-01 20:26:20 +0000 | [diff] [blame] | 2319 | delete $4; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2320 | } |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2321 | | Types '<' '{' '}' '>' { |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2322 | const StructType *STy = dyn_cast<StructType>($1.PAT->get()); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2323 | if (STy == 0) |
| 2324 | error("Cannot make packed struct constant with type: '" + |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2325 | $1.PAT->get()->getDescription() + "'"); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2326 | if (STy->getNumContainedTypes() != 0) |
| 2327 | error("Illegal number of initializers for packed structure type"); |
| 2328 | $$.C = ConstantStruct::get(STy, std::vector<Constant*>()); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2329 | $$.S.copy($1.S); |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2330 | delete $1.PAT; |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2331 | } |
| 2332 | | Types NULL_TOK { |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2333 | const PointerType *PTy = dyn_cast<PointerType>($1.PAT->get()); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2334 | if (PTy == 0) |
| 2335 | error("Cannot make null pointer constant with type: '" + |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2336 | $1.PAT->get()->getDescription() + "'"); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2337 | $$.C = ConstantPointerNull::get(PTy); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2338 | $$.S.copy($1.S); |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2339 | delete $1.PAT; |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2340 | } |
| 2341 | | Types UNDEF { |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2342 | $$.C = UndefValue::get($1.PAT->get()); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2343 | $$.S.copy($1.S); |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2344 | delete $1.PAT; |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2345 | } |
| 2346 | | Types SymbolicValueRef { |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2347 | const PointerType *Ty = dyn_cast<PointerType>($1.PAT->get()); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2348 | if (Ty == 0) |
| 2349 | error("Global const reference must be a pointer type, not" + |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2350 | $1.PAT->get()->getDescription()); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2351 | |
| 2352 | // ConstExprs can exist in the body of a function, thus creating |
| 2353 | // GlobalValues whenever they refer to a variable. Because we are in |
| 2354 | // the context of a function, getExistingValue will search the functions |
| 2355 | // symbol table instead of the module symbol table for the global symbol, |
| 2356 | // which throws things all off. To get around this, we just tell |
| 2357 | // getExistingValue that we are at global scope here. |
| 2358 | // |
| 2359 | Function *SavedCurFn = CurFun.CurrentFunction; |
| 2360 | CurFun.CurrentFunction = 0; |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2361 | $2.S.copy($1.S); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2362 | Value *V = getExistingValue(Ty, $2); |
| 2363 | CurFun.CurrentFunction = SavedCurFn; |
| 2364 | |
| 2365 | // If this is an initializer for a constant pointer, which is referencing a |
| 2366 | // (currently) undefined variable, create a stub now that shall be replaced |
| 2367 | // in the future with the right type of variable. |
| 2368 | // |
| 2369 | if (V == 0) { |
| 2370 | assert(isa<PointerType>(Ty) && "Globals may only be used as pointers"); |
| 2371 | const PointerType *PT = cast<PointerType>(Ty); |
| 2372 | |
| 2373 | // First check to see if the forward references value is already created! |
| 2374 | PerModuleInfo::GlobalRefsType::iterator I = |
| 2375 | CurModule.GlobalRefs.find(std::make_pair(PT, $2)); |
| 2376 | |
| 2377 | if (I != CurModule.GlobalRefs.end()) { |
| 2378 | V = I->second; // Placeholder already exists, use it... |
| 2379 | $2.destroy(); |
| 2380 | } else { |
| 2381 | std::string Name; |
| 2382 | if ($2.Type == ValID::NameVal) Name = $2.Name; |
| 2383 | |
| 2384 | // Create the forward referenced global. |
| 2385 | GlobalValue *GV; |
| 2386 | if (const FunctionType *FTy = |
| 2387 | dyn_cast<FunctionType>(PT->getElementType())) { |
| 2388 | GV = new Function(FTy, GlobalValue::ExternalLinkage, Name, |
| 2389 | CurModule.CurrentModule); |
| 2390 | } else { |
| 2391 | GV = new GlobalVariable(PT->getElementType(), false, |
| 2392 | GlobalValue::ExternalLinkage, 0, |
| 2393 | Name, CurModule.CurrentModule); |
| 2394 | } |
| 2395 | |
| 2396 | // Keep track of the fact that we have a forward ref to recycle it |
| 2397 | CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV)); |
| 2398 | V = GV; |
| 2399 | } |
| 2400 | } |
| 2401 | $$.C = cast<GlobalValue>(V); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2402 | $$.S.copy($1.S); |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2403 | delete $1.PAT; // Free the type handle |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2404 | } |
| 2405 | | Types ConstExpr { |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2406 | if ($1.PAT->get() != $2.C->getType()) |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2407 | error("Mismatched types for constant expression"); |
| 2408 | $$ = $2; |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2409 | $$.S.copy($1.S); |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2410 | delete $1.PAT; |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2411 | } |
| 2412 | | Types ZEROINITIALIZER { |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2413 | const Type *Ty = $1.PAT->get(); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2414 | if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty)) |
| 2415 | error("Cannot create a null initialized value of this type"); |
| 2416 | $$.C = Constant::getNullValue(Ty); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2417 | $$.S.copy($1.S); |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2418 | delete $1.PAT; |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2419 | } |
| 2420 | | SIntType EINT64VAL { // integral constants |
| 2421 | const Type *Ty = $1.T; |
| 2422 | if (!ConstantInt::isValueValidForType(Ty, $2)) |
| 2423 | error("Constant value doesn't fit in type"); |
| 2424 | $$.C = ConstantInt::get(Ty, $2); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2425 | $$.S.makeSigned(); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2426 | } |
| 2427 | | UIntType EUINT64VAL { // integral constants |
| 2428 | const Type *Ty = $1.T; |
| 2429 | if (!ConstantInt::isValueValidForType(Ty, $2)) |
| 2430 | error("Constant value doesn't fit in type"); |
| 2431 | $$.C = ConstantInt::get(Ty, $2); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2432 | $$.S.makeUnsigned(); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2433 | } |
| 2434 | | BOOL TRUETOK { // Boolean constants |
| 2435 | $$.C = ConstantInt::get(Type::Int1Ty, true); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2436 | $$.S.makeUnsigned(); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2437 | } |
| 2438 | | BOOL FALSETOK { // Boolean constants |
| 2439 | $$.C = ConstantInt::get(Type::Int1Ty, false); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2440 | $$.S.makeUnsigned(); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2441 | } |
| 2442 | | FPType FPVAL { // Float & Double constants |
Dale Johannesen | 43421b3 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 2443 | if (!ConstantFP::isValueValidForType($1.T, *$2)) |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2444 | error("Floating point constant invalid for type"); |
Dale Johannesen | 43421b3 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 2445 | // Lexer has no type info, so builds all FP constants as double. |
| 2446 | // Fix this here. |
| 2447 | if ($1.T==Type::FloatTy) |
| 2448 | $2->convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven); |
| 2449 | $$.C = ConstantFP::get($1.T, *$2); |
Dale Johannesen | cdd509a | 2007-09-07 21:07:57 +0000 | [diff] [blame] | 2450 | delete $2; |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2451 | $$.S.makeSignless(); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2452 | } |
| 2453 | ; |
| 2454 | |
| 2455 | ConstExpr |
| 2456 | : CastOps '(' ConstVal TO Types ')' { |
| 2457 | const Type* SrcTy = $3.C->getType(); |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2458 | const Type* DstTy = $5.PAT->get(); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2459 | Signedness SrcSign($3.S); |
| 2460 | Signedness DstSign($5.S); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2461 | if (!SrcTy->isFirstClassType()) |
| 2462 | error("cast constant expression from a non-primitive type: '" + |
| 2463 | SrcTy->getDescription() + "'"); |
| 2464 | if (!DstTy->isFirstClassType()) |
| 2465 | error("cast constant expression to a non-primitive type: '" + |
| 2466 | DstTy->getDescription() + "'"); |
| 2467 | $$.C = cast<Constant>(getCast($1, $3.C, SrcSign, DstTy, DstSign)); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2468 | $$.S.copy(DstSign); |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2469 | delete $5.PAT; |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2470 | } |
| 2471 | | GETELEMENTPTR '(' ConstVal IndexList ')' { |
| 2472 | const Type *Ty = $3.C->getType(); |
| 2473 | if (!isa<PointerType>(Ty)) |
| 2474 | error("GetElementPtr requires a pointer operand"); |
| 2475 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2476 | std::vector<Constant*> CIndices; |
Reid Spencer | d23c4dd | 2007-04-16 00:39:39 +0000 | [diff] [blame] | 2477 | upgradeGEPCEIndices($3.C->getType(), $4, CIndices); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2478 | |
| 2479 | delete $4; |
Chris Lattner | 4227bdb | 2007-02-19 07:34:02 +0000 | [diff] [blame] | 2480 | $$.C = ConstantExpr::getGetElementPtr($3.C, &CIndices[0], CIndices.size()); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2481 | $$.S.copy(getElementSign($3, CIndices)); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2482 | } |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2483 | | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' { |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2484 | if (!$3.C->getType()->isInteger() || |
| 2485 | cast<IntegerType>($3.C->getType())->getBitWidth() != 1) |
| 2486 | error("Select condition must be bool type"); |
| 2487 | if ($5.C->getType() != $7.C->getType()) |
| 2488 | error("Select operand types must match"); |
| 2489 | $$.C = ConstantExpr::getSelect($3.C, $5.C, $7.C); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2490 | $$.S.copy($5.S); |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2491 | } |
| 2492 | | ArithmeticOps '(' ConstVal ',' ConstVal ')' { |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2493 | const Type *Ty = $3.C->getType(); |
| 2494 | if (Ty != $5.C->getType()) |
| 2495 | error("Binary operator types must match"); |
| 2496 | // First, make sure we're dealing with the right opcode by upgrading from |
| 2497 | // obsolete versions. |
| 2498 | Instruction::BinaryOps Opcode = getBinaryOp($1, Ty, $3.S); |
| 2499 | |
| 2500 | // HACK: llvm 1.3 and earlier used to emit invalid pointer constant exprs. |
| 2501 | // To retain backward compatibility with these early compilers, we emit a |
| 2502 | // cast to the appropriate integer type automatically if we are in the |
| 2503 | // broken case. See PR424 for more information. |
| 2504 | if (!isa<PointerType>(Ty)) { |
| 2505 | $$.C = ConstantExpr::get(Opcode, $3.C, $5.C); |
| 2506 | } else { |
| 2507 | const Type *IntPtrTy = 0; |
| 2508 | switch (CurModule.CurrentModule->getPointerSize()) { |
| 2509 | case Module::Pointer32: IntPtrTy = Type::Int32Ty; break; |
| 2510 | case Module::Pointer64: IntPtrTy = Type::Int64Ty; break; |
| 2511 | default: error("invalid pointer binary constant expr"); |
| 2512 | } |
| 2513 | $$.C = ConstantExpr::get(Opcode, |
| 2514 | ConstantExpr::getCast(Instruction::PtrToInt, $3.C, IntPtrTy), |
| 2515 | ConstantExpr::getCast(Instruction::PtrToInt, $5.C, IntPtrTy)); |
| 2516 | $$.C = ConstantExpr::getCast(Instruction::IntToPtr, $$.C, Ty); |
| 2517 | } |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2518 | $$.S.copy($3.S); |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2519 | } |
| 2520 | | LogicalOps '(' ConstVal ',' ConstVal ')' { |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2521 | const Type* Ty = $3.C->getType(); |
| 2522 | if (Ty != $5.C->getType()) |
| 2523 | error("Logical operator types must match"); |
| 2524 | if (!Ty->isInteger()) { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 2525 | if (!isa<VectorType>(Ty) || |
| 2526 | !cast<VectorType>(Ty)->getElementType()->isInteger()) |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2527 | error("Logical operator requires integer operands"); |
| 2528 | } |
| 2529 | Instruction::BinaryOps Opcode = getBinaryOp($1, Ty, $3.S); |
| 2530 | $$.C = ConstantExpr::get(Opcode, $3.C, $5.C); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2531 | $$.S.copy($3.S); |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2532 | } |
| 2533 | | SetCondOps '(' ConstVal ',' ConstVal ')' { |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2534 | const Type* Ty = $3.C->getType(); |
| 2535 | if (Ty != $5.C->getType()) |
| 2536 | error("setcc operand types must match"); |
| 2537 | unsigned short pred; |
| 2538 | Instruction::OtherOps Opcode = getCompareOp($1, pred, Ty, $3.S); |
| 2539 | $$.C = ConstantExpr::getCompare(Opcode, $3.C, $5.C); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2540 | $$.S.makeUnsigned(); |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2541 | } |
Reid Spencer | 57f28f9 | 2006-12-03 07:10:26 +0000 | [diff] [blame] | 2542 | | ICMP IPredicates '(' ConstVal ',' ConstVal ')' { |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2543 | if ($4.C->getType() != $6.C->getType()) |
| 2544 | error("icmp operand types must match"); |
| 2545 | $$.C = ConstantExpr::getCompare($2, $4.C, $6.C); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2546 | $$.S.makeUnsigned(); |
Reid Spencer | 57f28f9 | 2006-12-03 07:10:26 +0000 | [diff] [blame] | 2547 | } |
| 2548 | | FCMP FPredicates '(' ConstVal ',' ConstVal ')' { |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2549 | if ($4.C->getType() != $6.C->getType()) |
| 2550 | error("fcmp operand types must match"); |
| 2551 | $$.C = ConstantExpr::getCompare($2, $4.C, $6.C); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2552 | $$.S.makeUnsigned(); |
Reid Spencer | 2b40438 | 2006-12-02 22:09:27 +0000 | [diff] [blame] | 2553 | } |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2554 | | ShiftOps '(' ConstVal ',' ConstVal ')' { |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2555 | if (!$5.C->getType()->isInteger() || |
| 2556 | cast<IntegerType>($5.C->getType())->getBitWidth() != 8) |
| 2557 | error("Shift count for shift constant must be unsigned byte"); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2558 | const Type* Ty = $3.C->getType(); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2559 | if (!$3.C->getType()->isInteger()) |
| 2560 | error("Shift constant expression requires integer operand"); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2561 | Constant *ShiftAmt = ConstantExpr::getZExt($5.C, Ty); |
| 2562 | $$.C = ConstantExpr::get(getBinaryOp($1, Ty, $3.S), $3.C, ShiftAmt); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2563 | $$.S.copy($3.S); |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2564 | } |
| 2565 | | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' { |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2566 | if (!ExtractElementInst::isValidOperands($3.C, $5.C)) |
| 2567 | error("Invalid extractelement operands"); |
| 2568 | $$.C = ConstantExpr::getExtractElement($3.C, $5.C); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2569 | $$.S.copy($3.S.get(0)); |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2570 | } |
| 2571 | | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' { |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2572 | if (!InsertElementInst::isValidOperands($3.C, $5.C, $7.C)) |
| 2573 | error("Invalid insertelement operands"); |
| 2574 | $$.C = ConstantExpr::getInsertElement($3.C, $5.C, $7.C); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2575 | $$.S.copy($3.S); |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2576 | } |
| 2577 | | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' { |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2578 | if (!ShuffleVectorInst::isValidOperands($3.C, $5.C, $7.C)) |
| 2579 | error("Invalid shufflevector operands"); |
| 2580 | $$.C = ConstantExpr::getShuffleVector($3.C, $5.C, $7.C); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2581 | $$.S.copy($3.S); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2582 | } |
| 2583 | ; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2584 | |
| 2585 | |
| 2586 | // ConstVector - A list of comma separated constants. |
Reid Spencer | e77e35e | 2006-12-01 20:26:20 +0000 | [diff] [blame] | 2587 | ConstVector |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2588 | : ConstVector ',' ConstVal { ($$ = $1)->push_back($3); } |
| 2589 | | ConstVal { |
| 2590 | $$ = new std::vector<ConstInfo>(); |
| 2591 | $$->push_back($1); |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2592 | } |
Reid Spencer | e77e35e | 2006-12-01 20:26:20 +0000 | [diff] [blame] | 2593 | ; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2594 | |
| 2595 | |
| 2596 | // GlobalType - Match either GLOBAL or CONSTANT for global declarations... |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2597 | GlobalType |
| 2598 | : GLOBAL { $$ = false; } |
| 2599 | | CONSTANT { $$ = true; } |
| 2600 | ; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2601 | |
| 2602 | |
| 2603 | //===----------------------------------------------------------------------===// |
| 2604 | // Rules to match Modules |
| 2605 | //===----------------------------------------------------------------------===// |
| 2606 | |
| 2607 | // Module rule: Capture the result of parsing the whole file into a result |
| 2608 | // variable... |
| 2609 | // |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2610 | Module |
| 2611 | : FunctionList { |
| 2612 | $$ = ParserResult = $1; |
| 2613 | CurModule.ModuleDone(); |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2614 | } |
Jeff Cohen | ac2dca9 | 2007-01-21 19:30:52 +0000 | [diff] [blame] | 2615 | ; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2616 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2617 | // FunctionList - A list of functions, preceeded by a constant pool. |
| 2618 | // |
| 2619 | FunctionList |
| 2620 | : FunctionList Function { $$ = $1; CurFun.FunctionDone(); } |
| 2621 | | FunctionList FunctionProto { $$ = $1; } |
| 2622 | | FunctionList MODULE ASM_TOK AsmBlock { $$ = $1; } |
| 2623 | | FunctionList IMPLEMENTATION { $$ = $1; } |
| 2624 | | ConstPool { |
| 2625 | $$ = CurModule.CurrentModule; |
| 2626 | // Emit an error if there are any unresolved types left. |
| 2627 | if (!CurModule.LateResolveTypes.empty()) { |
| 2628 | const ValID &DID = CurModule.LateResolveTypes.begin()->first; |
| 2629 | if (DID.Type == ValID::NameVal) { |
| 2630 | error("Reference to an undefined type: '"+DID.getName() + "'"); |
| 2631 | } else { |
| 2632 | error("Reference to an undefined type: #" + itostr(DID.Num)); |
| 2633 | } |
| 2634 | } |
| 2635 | } |
| 2636 | ; |
Reid Spencer | a8ca090 | 2006-12-02 20:19:56 +0000 | [diff] [blame] | 2637 | |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2638 | // ConstPool - Constants with optional names assigned to them. |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2639 | ConstPool |
| 2640 | : ConstPool OptAssign TYPE TypesV { |
| 2641 | // Eagerly resolve types. This is not an optimization, this is a |
| 2642 | // requirement that is due to the fact that we could have this: |
| 2643 | // |
| 2644 | // %list = type { %list * } |
| 2645 | // %list = type { %list * } ; repeated type decl |
| 2646 | // |
| 2647 | // If types are not resolved eagerly, then the two types will not be |
| 2648 | // determined to be the same type! |
| 2649 | // |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2650 | ResolveTypeTo($2, $4.PAT->get(), $4.S); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2651 | |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2652 | if (!setTypeName($4, $2) && !$2) { |
| 2653 | // If this is a numbered type that is not a redefinition, add it to the |
| 2654 | // slot table. |
| 2655 | CurModule.Types.push_back($4.PAT->get()); |
| 2656 | CurModule.TypeSigns.push_back($4.S); |
Reid Spencer | a50d596 | 2006-12-02 04:11:07 +0000 | [diff] [blame] | 2657 | } |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2658 | delete $4.PAT; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2659 | } |
| 2660 | | ConstPool FunctionProto { // Function prototypes can be in const pool |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2661 | } |
| 2662 | | ConstPool MODULE ASM_TOK AsmBlock { // Asm blocks can be in the const pool |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2663 | } |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2664 | | ConstPool OptAssign OptLinkage GlobalType ConstVal { |
| 2665 | if ($5.C == 0) |
| 2666 | error("Global value initializer is not a constant"); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2667 | CurGV = ParseGlobalVariable($2, $3, $4, $5.C->getType(), $5.C, $5.S); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2668 | } GlobalVarAttributes { |
| 2669 | CurGV = 0; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2670 | } |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2671 | | ConstPool OptAssign EXTERNAL GlobalType Types { |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2672 | const Type *Ty = $5.PAT->get(); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2673 | CurGV = ParseGlobalVariable($2, GlobalValue::ExternalLinkage, $4, Ty, 0, |
| 2674 | $5.S); |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2675 | delete $5.PAT; |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2676 | } GlobalVarAttributes { |
| 2677 | CurGV = 0; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2678 | } |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2679 | | ConstPool OptAssign DLLIMPORT GlobalType Types { |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2680 | const Type *Ty = $5.PAT->get(); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2681 | CurGV = ParseGlobalVariable($2, GlobalValue::DLLImportLinkage, $4, Ty, 0, |
| 2682 | $5.S); |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2683 | delete $5.PAT; |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2684 | } GlobalVarAttributes { |
| 2685 | CurGV = 0; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2686 | } |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2687 | | ConstPool OptAssign EXTERN_WEAK GlobalType Types { |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2688 | const Type *Ty = $5.PAT->get(); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2689 | CurGV = |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2690 | ParseGlobalVariable($2, GlobalValue::ExternalWeakLinkage, $4, Ty, 0, |
| 2691 | $5.S); |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2692 | delete $5.PAT; |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2693 | } GlobalVarAttributes { |
| 2694 | CurGV = 0; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2695 | } |
| 2696 | | ConstPool TARGET TargetDefinition { |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2697 | } |
| 2698 | | ConstPool DEPLIBS '=' LibrariesDefinition { |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2699 | } |
| 2700 | | /* empty: end of list */ { |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2701 | } |
| 2702 | ; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2703 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2704 | AsmBlock |
| 2705 | : STRINGCONSTANT { |
| 2706 | const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm(); |
| 2707 | char *EndStr = UnEscapeLexed($1, true); |
| 2708 | std::string NewAsm($1, EndStr); |
| 2709 | free($1); |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2710 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2711 | if (AsmSoFar.empty()) |
| 2712 | CurModule.CurrentModule->setModuleInlineAsm(NewAsm); |
| 2713 | else |
| 2714 | CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+NewAsm); |
| 2715 | } |
| 2716 | ; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2717 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2718 | BigOrLittle |
Reid Spencer | 7e97288 | 2007-01-26 18:26:23 +0000 | [diff] [blame] | 2719 | : BIG { $$ = Module::BigEndian; } |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2720 | | LITTLE { $$ = Module::LittleEndian; } |
| 2721 | ; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2722 | |
| 2723 | TargetDefinition |
| 2724 | : ENDIAN '=' BigOrLittle { |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2725 | CurModule.setEndianness($3); |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2726 | } |
| 2727 | | POINTERSIZE '=' EUINT64VAL { |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2728 | if ($3 == 32) |
| 2729 | CurModule.setPointerSize(Module::Pointer32); |
| 2730 | else if ($3 == 64) |
| 2731 | CurModule.setPointerSize(Module::Pointer64); |
| 2732 | else |
| 2733 | error("Invalid pointer size: '" + utostr($3) + "'"); |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2734 | } |
| 2735 | | TRIPLE '=' STRINGCONSTANT { |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2736 | CurModule.CurrentModule->setTargetTriple($3); |
| 2737 | free($3); |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2738 | } |
| 2739 | | DATALAYOUT '=' STRINGCONSTANT { |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2740 | CurModule.CurrentModule->setDataLayout($3); |
| 2741 | free($3); |
| 2742 | } |
| 2743 | ; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2744 | |
| 2745 | LibrariesDefinition |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2746 | : '[' LibList ']' |
| 2747 | ; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2748 | |
| 2749 | LibList |
| 2750 | : LibList ',' STRINGCONSTANT { |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2751 | CurModule.CurrentModule->addLibrary($3); |
| 2752 | free($3); |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2753 | } |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2754 | | STRINGCONSTANT { |
| 2755 | CurModule.CurrentModule->addLibrary($1); |
| 2756 | free($1); |
| 2757 | } |
| 2758 | | /* empty: end of list */ { } |
| 2759 | ; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2760 | |
| 2761 | //===----------------------------------------------------------------------===// |
| 2762 | // Rules to match Function Headers |
| 2763 | //===----------------------------------------------------------------------===// |
| 2764 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2765 | Name |
| 2766 | : VAR_ID | STRINGCONSTANT |
| 2767 | ; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2768 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2769 | OptName |
| 2770 | : Name |
| 2771 | | /*empty*/ { $$ = 0; } |
| 2772 | ; |
| 2773 | |
| 2774 | ArgVal |
| 2775 | : Types OptName { |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2776 | if ($1.PAT->get() == Type::VoidTy) |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2777 | error("void typed arguments are invalid"); |
| 2778 | $$ = new std::pair<PATypeInfo, char*>($1, $2); |
Reid Spencer | 2b8036e | 2007-01-02 05:44:33 +0000 | [diff] [blame] | 2779 | } |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2780 | ; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2781 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2782 | ArgListH |
| 2783 | : ArgListH ',' ArgVal { |
| 2784 | $$ = $1; |
| 2785 | $$->push_back(*$3); |
Reid Spencer | e77e35e | 2006-12-01 20:26:20 +0000 | [diff] [blame] | 2786 | delete $3; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2787 | } |
| 2788 | | ArgVal { |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2789 | $$ = new std::vector<std::pair<PATypeInfo,char*> >(); |
| 2790 | $$->push_back(*$1); |
| 2791 | delete $1; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2792 | } |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2793 | ; |
| 2794 | |
| 2795 | ArgList |
| 2796 | : ArgListH { $$ = $1; } |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2797 | | ArgListH ',' DOTDOTDOT { |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2798 | $$ = $1; |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2799 | PATypeInfo VoidTI; |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2800 | VoidTI.PAT = new PATypeHolder(Type::VoidTy); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2801 | VoidTI.S.makeSignless(); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2802 | $$->push_back(std::pair<PATypeInfo, char*>(VoidTI, 0)); |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2803 | } |
| 2804 | | DOTDOTDOT { |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2805 | $$ = new std::vector<std::pair<PATypeInfo,char*> >(); |
| 2806 | PATypeInfo VoidTI; |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2807 | VoidTI.PAT = new PATypeHolder(Type::VoidTy); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2808 | VoidTI.S.makeSignless(); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2809 | $$->push_back(std::pair<PATypeInfo, char*>(VoidTI, 0)); |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2810 | } |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2811 | | /* empty */ { $$ = 0; } |
| 2812 | ; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2813 | |
Reid Spencer | 05e52a1 | 2006-12-31 05:45:57 +0000 | [diff] [blame] | 2814 | FunctionHeaderH |
| 2815 | : OptCallingConv TypesV Name '(' ArgList ')' OptSection OptAlign { |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2816 | UnEscapeLexed($3); |
| 2817 | std::string FunctionName($3); |
| 2818 | free($3); // Free strdup'd memory! |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 2819 | |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2820 | const Type* RetTy = $2.PAT->get(); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2821 | |
| 2822 | if (!RetTy->isFirstClassType() && RetTy != Type::VoidTy) |
| 2823 | error("LLVM functions cannot return aggregate types"); |
| 2824 | |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2825 | Signedness FTySign; |
| 2826 | FTySign.makeComposite($2.S); |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 2827 | std::vector<const Type*> ParamTyList; |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2828 | |
| 2829 | // In LLVM 2.0 the signatures of three varargs intrinsics changed to take |
| 2830 | // i8*. We check here for those names and override the parameter list |
| 2831 | // types to ensure the prototype is correct. |
| 2832 | if (FunctionName == "llvm.va_start" || FunctionName == "llvm.va_end") { |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 2833 | ParamTyList.push_back(PointerType::getUnqual(Type::Int8Ty)); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2834 | } else if (FunctionName == "llvm.va_copy") { |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 2835 | ParamTyList.push_back(PointerType::getUnqual(Type::Int8Ty)); |
| 2836 | ParamTyList.push_back(PointerType::getUnqual(Type::Int8Ty)); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2837 | } else if ($5) { // If there are arguments... |
| 2838 | for (std::vector<std::pair<PATypeInfo,char*> >::iterator |
| 2839 | I = $5->begin(), E = $5->end(); I != E; ++I) { |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2840 | const Type *Ty = I->first.PAT->get(); |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 2841 | ParamTyList.push_back(Ty); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2842 | FTySign.add(I->first.S); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2843 | } |
| 2844 | } |
| 2845 | |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 2846 | bool isVarArg = ParamTyList.size() && ParamTyList.back() == Type::VoidTy; |
| 2847 | if (isVarArg) |
| 2848 | ParamTyList.pop_back(); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2849 | |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 2850 | const FunctionType *FT = FunctionType::get(RetTy, ParamTyList, isVarArg); |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 2851 | const PointerType *PFT = PointerType::getUnqual(FT); |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2852 | delete $2.PAT; |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2853 | |
| 2854 | ValID ID; |
| 2855 | if (!FunctionName.empty()) { |
Reid Spencer | 44f87ee | 2007-03-15 03:25:34 +0000 | [diff] [blame] | 2856 | ID = ValID::create((char*)FunctionName.c_str()); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2857 | } else { |
Reid Spencer | 44f87ee | 2007-03-15 03:25:34 +0000 | [diff] [blame] | 2858 | ID = ValID::create((int)CurModule.Values[PFT].size()); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2859 | } |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2860 | ID.S.makeComposite(FTySign); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2861 | |
| 2862 | Function *Fn = 0; |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2863 | Module* M = CurModule.CurrentModule; |
| 2864 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2865 | // See if this function was forward referenced. If so, recycle the object. |
| 2866 | if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) { |
| 2867 | // Move the function to the end of the list, from whereever it was |
| 2868 | // previously inserted. |
| 2869 | Fn = cast<Function>(FWRef); |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2870 | M->getFunctionList().remove(Fn); |
| 2871 | M->getFunctionList().push_back(Fn); |
| 2872 | } else if (!FunctionName.empty()) { |
| 2873 | GlobalValue *Conflict = M->getFunction(FunctionName); |
| 2874 | if (!Conflict) |
| 2875 | Conflict = M->getNamedGlobal(FunctionName); |
| 2876 | if (Conflict && PFT == Conflict->getType()) { |
| 2877 | if (!CurFun.isDeclare && !Conflict->isDeclaration()) { |
| 2878 | // We have two function definitions that conflict, same type, same |
Reid Spencer | 39a9792 | 2007-02-08 08:47:38 +0000 | [diff] [blame] | 2879 | // name. We should really check to make sure that this is the result |
| 2880 | // of integer type planes collapsing and generate an error if it is |
| 2881 | // not, but we'll just rename on the assumption that it is. However, |
| 2882 | // let's do it intelligently and rename the internal linkage one |
| 2883 | // if there is one. |
| 2884 | std::string NewName(makeNameUnique(FunctionName)); |
| 2885 | if (Conflict->hasInternalLinkage()) { |
| 2886 | Conflict->setName(NewName); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2887 | RenameMapKey Key = |
| 2888 | makeRenameMapKey(FunctionName, Conflict->getType(), ID.S); |
Reid Spencer | 39a9792 | 2007-02-08 08:47:38 +0000 | [diff] [blame] | 2889 | CurModule.RenameMap[Key] = NewName; |
| 2890 | Fn = new Function(FT, CurFun.Linkage, FunctionName, M); |
| 2891 | InsertValue(Fn, CurModule.Values); |
| 2892 | } else { |
| 2893 | Fn = new Function(FT, CurFun.Linkage, NewName, M); |
| 2894 | InsertValue(Fn, CurModule.Values); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2895 | RenameMapKey Key = |
| 2896 | makeRenameMapKey(FunctionName, PFT, ID.S); |
Reid Spencer | 39a9792 | 2007-02-08 08:47:38 +0000 | [diff] [blame] | 2897 | CurModule.RenameMap[Key] = NewName; |
| 2898 | } |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2899 | } else { |
| 2900 | // If they are not both definitions, then just use the function we |
| 2901 | // found since the types are the same. |
| 2902 | Fn = cast<Function>(Conflict); |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 2903 | |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2904 | // Make sure to strip off any argument names so we can't get |
| 2905 | // conflicts. |
| 2906 | if (Fn->isDeclaration()) |
| 2907 | for (Function::arg_iterator AI = Fn->arg_begin(), |
| 2908 | AE = Fn->arg_end(); AI != AE; ++AI) |
| 2909 | AI->setName(""); |
| 2910 | } |
| 2911 | } else if (Conflict) { |
Reid Spencer | 53bd704 | 2007-04-16 02:56:33 +0000 | [diff] [blame] | 2912 | // We have two globals with the same name and different types. |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2913 | // Previously, this was permitted because the symbol table had |
| 2914 | // "type planes" and names only needed to be distinct within a |
| 2915 | // type plane. After PR411 was fixed, this is no loner the case. |
| 2916 | // To resolve this we must rename one of the two. |
| 2917 | if (Conflict->hasInternalLinkage()) { |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2918 | // We can safely rename the Conflict. |
| 2919 | RenameMapKey Key = |
| 2920 | makeRenameMapKey(Conflict->getName(), Conflict->getType(), |
| 2921 | CurModule.NamedValueSigns[Conflict->getName()]); |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2922 | Conflict->setName(makeNameUnique(Conflict->getName())); |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2923 | CurModule.RenameMap[Key] = Conflict->getName(); |
| 2924 | Fn = new Function(FT, CurFun.Linkage, FunctionName, M); |
| 2925 | InsertValue(Fn, CurModule.Values); |
Reid Spencer | 91a9d54 | 2007-03-21 17:26:41 +0000 | [diff] [blame] | 2926 | } else { |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2927 | // We can't quietly rename either of these things, but we must |
Reid Spencer | 91a9d54 | 2007-03-21 17:26:41 +0000 | [diff] [blame] | 2928 | // rename one of them. Only if the function's linkage is internal can |
| 2929 | // we forgo a warning message about the renamed function. |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2930 | std::string NewName = makeNameUnique(FunctionName); |
Reid Spencer | 91a9d54 | 2007-03-21 17:26:41 +0000 | [diff] [blame] | 2931 | if (CurFun.Linkage != GlobalValue::InternalLinkage) { |
| 2932 | warning("Renaming function '" + FunctionName + "' as '" + NewName + |
| 2933 | "' may cause linkage errors"); |
| 2934 | } |
| 2935 | // Elect to rename the thing we're now defining. |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2936 | Fn = new Function(FT, CurFun.Linkage, NewName, M); |
| 2937 | InsertValue(Fn, CurModule.Values); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2938 | RenameMapKey Key = makeRenameMapKey(FunctionName, PFT, ID.S); |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2939 | CurModule.RenameMap[Key] = NewName; |
Reid Spencer | 91a9d54 | 2007-03-21 17:26:41 +0000 | [diff] [blame] | 2940 | } |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 2941 | } else { |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2942 | // There's no conflict, just define the function |
| 2943 | Fn = new Function(FT, CurFun.Linkage, FunctionName, M); |
| 2944 | InsertValue(Fn, CurModule.Values); |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 2945 | } |
Reid Spencer | 7055daf | 2007-04-16 03:04:13 +0000 | [diff] [blame] | 2946 | } else { |
| 2947 | // There's no conflict, just define the function |
| 2948 | Fn = new Function(FT, CurFun.Linkage, FunctionName, M); |
| 2949 | InsertValue(Fn, CurModule.Values); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2950 | } |
| 2951 | |
Reid Spencer | 7055daf | 2007-04-16 03:04:13 +0000 | [diff] [blame] | 2952 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2953 | CurFun.FunctionStart(Fn); |
| 2954 | |
| 2955 | if (CurFun.isDeclare) { |
| 2956 | // If we have declaration, always overwrite linkage. This will allow us |
| 2957 | // to correctly handle cases, when pointer to function is passed as |
| 2958 | // argument to another function. |
| 2959 | Fn->setLinkage(CurFun.Linkage); |
| 2960 | } |
Reid Spencer | 43f76c9 | 2007-01-29 05:41:09 +0000 | [diff] [blame] | 2961 | Fn->setCallingConv(upgradeCallingConv($1)); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2962 | Fn->setAlignment($8); |
| 2963 | if ($7) { |
| 2964 | Fn->setSection($7); |
| 2965 | free($7); |
| 2966 | } |
| 2967 | |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 2968 | // Convert the CSRet calling convention into the corresponding parameter |
| 2969 | // attribute. |
| 2970 | if ($1 == OldCallingConv::CSRet) { |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 2971 | ParamAttrsWithIndex PAWI = |
| 2972 | ParamAttrsWithIndex::get(1, ParamAttr::StructRet); // first arg |
| 2973 | Fn->setParamAttrs(PAListPtr::get(&PAWI, 1)); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 2974 | } |
| 2975 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2976 | // Add all of the arguments we parsed to the function... |
| 2977 | if ($5) { // Is null if empty... |
| 2978 | if (isVarArg) { // Nuke the last entry |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2979 | assert($5->back().first.PAT->get() == Type::VoidTy && |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2980 | $5->back().second == 0 && "Not a varargs marker"); |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2981 | delete $5->back().first.PAT; |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2982 | $5->pop_back(); // Delete the last entry |
| 2983 | } |
| 2984 | Function::arg_iterator ArgIt = Fn->arg_begin(); |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 2985 | Function::arg_iterator ArgEnd = Fn->arg_end(); |
| 2986 | std::vector<std::pair<PATypeInfo,char*> >::iterator I = $5->begin(); |
| 2987 | std::vector<std::pair<PATypeInfo,char*> >::iterator E = $5->end(); |
| 2988 | for ( ; I != E && ArgIt != ArgEnd; ++I, ++ArgIt) { |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 2989 | delete I->first.PAT; // Delete the typeholder... |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 2990 | ValueInfo VI; VI.V = ArgIt; VI.S.copy(I->first.S); |
| 2991 | setValueName(VI, I->second); // Insert arg into symtab... |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2992 | InsertValue(ArgIt); |
| 2993 | } |
| 2994 | delete $5; // We're now done with the argument list |
| 2995 | } |
Reid Spencer | 4f859aa | 2007-04-22 05:46:44 +0000 | [diff] [blame] | 2996 | lastCallingConv = OldCallingConv::C; |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 2997 | } |
| 2998 | ; |
| 2999 | |
| 3000 | BEGIN |
| 3001 | : BEGINTOK | '{' // Allow BEGIN or '{' to start a function |
Jeff Cohen | ac2dca9 | 2007-01-21 19:30:52 +0000 | [diff] [blame] | 3002 | ; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 3003 | |
Reid Spencer | a9d7e89 | 2006-12-29 20:33:37 +0000 | [diff] [blame] | 3004 | FunctionHeader |
Reid Spencer | 91a9d54 | 2007-03-21 17:26:41 +0000 | [diff] [blame] | 3005 | : OptLinkage { CurFun.Linkage = $1; } FunctionHeaderH BEGIN { |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3006 | $$ = CurFun.CurrentFunction; |
| 3007 | |
| 3008 | // Make sure that we keep track of the linkage type even if there was a |
| 3009 | // previous "declare". |
| 3010 | $$->setLinkage($1); |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 3011 | } |
Reid Spencer | a9d7e89 | 2006-12-29 20:33:37 +0000 | [diff] [blame] | 3012 | ; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 3013 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3014 | END |
| 3015 | : ENDTOK | '}' // Allow end of '}' to end a function |
| 3016 | ; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 3017 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3018 | Function |
| 3019 | : BasicBlockList END { |
| 3020 | $$ = $1; |
| 3021 | }; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 3022 | |
Reid Spencer | e77e35e | 2006-12-01 20:26:20 +0000 | [diff] [blame] | 3023 | FnDeclareLinkage |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 3024 | : /*default*/ { $$ = GlobalValue::ExternalLinkage; } |
| 3025 | | DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; } |
| 3026 | | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; } |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 3027 | ; |
| 3028 | |
| 3029 | FunctionProto |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 3030 | : DECLARE { CurFun.isDeclare = true; } |
| 3031 | FnDeclareLinkage { CurFun.Linkage = $3; } FunctionHeaderH { |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3032 | $$ = CurFun.CurrentFunction; |
| 3033 | CurFun.FunctionDone(); |
| 3034 | |
| 3035 | } |
| 3036 | ; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 3037 | |
| 3038 | //===----------------------------------------------------------------------===// |
| 3039 | // Rules to match Basic Blocks |
| 3040 | //===----------------------------------------------------------------------===// |
| 3041 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3042 | OptSideEffect |
| 3043 | : /* empty */ { $$ = false; } |
| 3044 | | SIDEEFFECT { $$ = true; } |
| 3045 | ; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 3046 | |
Reid Spencer | e77e35e | 2006-12-01 20:26:20 +0000 | [diff] [blame] | 3047 | ConstValueRef |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3048 | // A reference to a direct constant |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3049 | : ESINT64VAL { $$ = ValID::create($1); } |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3050 | | EUINT64VAL { $$ = ValID::create($1); } |
| 3051 | | FPVAL { $$ = ValID::create($1); } |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3052 | | TRUETOK { |
| 3053 | $$ = ValID::create(ConstantInt::get(Type::Int1Ty, true)); |
| 3054 | $$.S.makeUnsigned(); |
| 3055 | } |
| 3056 | | FALSETOK { |
| 3057 | $$ = ValID::create(ConstantInt::get(Type::Int1Ty, false)); |
| 3058 | $$.S.makeUnsigned(); |
| 3059 | } |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3060 | | NULL_TOK { $$ = ValID::createNull(); } |
| 3061 | | UNDEF { $$ = ValID::createUndef(); } |
| 3062 | | ZEROINITIALIZER { $$ = ValID::createZeroInit(); } |
| 3063 | | '<' ConstVector '>' { // Nonempty unsized packed vector |
| 3064 | const Type *ETy = (*$2)[0].C->getType(); |
| 3065 | int NumElements = $2->size(); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 3066 | VectorType* pt = VectorType::get(ETy, NumElements); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3067 | $$.S.makeComposite((*$2)[0].S); |
| 3068 | PATypeHolder* PTy = new PATypeHolder(HandleUpRefs(pt, $$.S)); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3069 | |
| 3070 | // Verify all elements are correct type! |
| 3071 | std::vector<Constant*> Elems; |
| 3072 | for (unsigned i = 0; i < $2->size(); i++) { |
| 3073 | Constant *C = (*$2)[i].C; |
| 3074 | const Type *CTy = C->getType(); |
| 3075 | if (ETy != CTy) |
| 3076 | error("Element #" + utostr(i) + " is not of type '" + |
| 3077 | ETy->getDescription() +"' as required!\nIt is of type '" + |
| 3078 | CTy->getDescription() + "'"); |
| 3079 | Elems.push_back(C); |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 3080 | } |
Reid Spencer | 44f87ee | 2007-03-15 03:25:34 +0000 | [diff] [blame] | 3081 | $$ = ValID::create(ConstantVector::get(pt, Elems)); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3082 | delete PTy; delete $2; |
| 3083 | } |
| 3084 | | ConstExpr { |
Reid Spencer | 44f87ee | 2007-03-15 03:25:34 +0000 | [diff] [blame] | 3085 | $$ = ValID::create($1.C); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3086 | $$.S.copy($1.S); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3087 | } |
| 3088 | | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT { |
| 3089 | char *End = UnEscapeLexed($3, true); |
| 3090 | std::string AsmStr = std::string($3, End); |
| 3091 | End = UnEscapeLexed($5, true); |
| 3092 | std::string Constraints = std::string($5, End); |
| 3093 | $$ = ValID::createInlineAsm(AsmStr, Constraints, $2); |
| 3094 | free($3); |
| 3095 | free($5); |
| 3096 | } |
| 3097 | ; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 3098 | |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 3099 | // SymbolicValueRef - Reference to one of two ways of symbolically refering to |
| 3100 | // another value. |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3101 | // |
| 3102 | SymbolicValueRef |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3103 | : INTVAL { $$ = ValID::create($1); $$.S.makeSignless(); } |
| 3104 | | Name { $$ = ValID::create($1); $$.S.makeSignless(); } |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3105 | ; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 3106 | |
| 3107 | // ValueRef - A reference to a definition... either constant or symbolic |
Reid Spencer | 1d64a6c | 2006-12-02 16:19:28 +0000 | [diff] [blame] | 3108 | ValueRef |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3109 | : SymbolicValueRef | ConstValueRef |
Reid Spencer | 1d64a6c | 2006-12-02 16:19:28 +0000 | [diff] [blame] | 3110 | ; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 3111 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3112 | |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 3113 | // ResolvedVal - a <type> <value> pair. This is used only in cases where the |
| 3114 | // type immediately preceeds the value reference, and allows complex constant |
| 3115 | // pool references (for things like: 'ret [2 x int] [ int 12, int 42]') |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3116 | ResolvedVal |
| 3117 | : Types ValueRef { |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 3118 | const Type *Ty = $1.PAT->get(); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3119 | $2.S.copy($1.S); |
Reid Spencer | 44f87ee | 2007-03-15 03:25:34 +0000 | [diff] [blame] | 3120 | $$.V = getVal(Ty, $2); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3121 | $$.S.copy($1.S); |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 3122 | delete $1.PAT; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 3123 | } |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3124 | ; |
| 3125 | |
| 3126 | BasicBlockList |
| 3127 | : BasicBlockList BasicBlock { |
| 3128 | $$ = $1; |
| 3129 | } |
| 3130 | | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks |
| 3131 | $$ = $1; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 3132 | }; |
| 3133 | |
| 3134 | |
| 3135 | // Basic blocks are terminated by branching instructions: |
| 3136 | // br, br/cc, switch, ret |
| 3137 | // |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3138 | BasicBlock |
| 3139 | : InstructionList OptAssign BBTerminatorInst { |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3140 | ValueInfo VI; VI.V = $3.TI; VI.S.copy($3.S); |
| 3141 | setValueName(VI, $2); |
| 3142 | InsertValue($3.TI); |
| 3143 | $1->getInstList().push_back($3.TI); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3144 | InsertValue($1); |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 3145 | $$ = $1; |
| 3146 | } |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3147 | ; |
| 3148 | |
| 3149 | InstructionList |
| 3150 | : InstructionList Inst { |
| 3151 | if ($2.I) |
| 3152 | $1->getInstList().push_back($2.I); |
| 3153 | $$ = $1; |
| 3154 | } |
| 3155 | | /* empty */ { |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3156 | $$ = CurBB = getBBVal(ValID::create((int)CurFun.NextBBNum++),true); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3157 | // Make sure to move the basic block to the correct location in the |
| 3158 | // function, instead of leaving it inserted wherever it was first |
| 3159 | // referenced. |
| 3160 | Function::BasicBlockListType &BBL = |
| 3161 | CurFun.CurrentFunction->getBasicBlockList(); |
| 3162 | BBL.splice(BBL.end(), BBL, $$); |
| 3163 | } |
| 3164 | | LABELSTR { |
Reid Spencer | 44f87ee | 2007-03-15 03:25:34 +0000 | [diff] [blame] | 3165 | $$ = CurBB = getBBVal(ValID::create($1), true); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3166 | // Make sure to move the basic block to the correct location in the |
| 3167 | // function, instead of leaving it inserted wherever it was first |
| 3168 | // referenced. |
| 3169 | Function::BasicBlockListType &BBL = |
| 3170 | CurFun.CurrentFunction->getBasicBlockList(); |
| 3171 | BBL.splice(BBL.end(), BBL, $$); |
| 3172 | } |
| 3173 | ; |
| 3174 | |
| 3175 | Unwind : UNWIND | EXCEPT; |
| 3176 | |
| 3177 | BBTerminatorInst |
| 3178 | : RET ResolvedVal { // Return with a result... |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3179 | $$.TI = new ReturnInst($2.V); |
| 3180 | $$.S.makeSignless(); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3181 | } |
| 3182 | | RET VOID { // Return with no result... |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3183 | $$.TI = new ReturnInst(); |
| 3184 | $$.S.makeSignless(); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3185 | } |
| 3186 | | BR LABEL ValueRef { // Unconditional Branch... |
| 3187 | BasicBlock* tmpBB = getBBVal($3); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3188 | $$.TI = new BranchInst(tmpBB); |
| 3189 | $$.S.makeSignless(); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3190 | } // Conditional Branch... |
| 3191 | | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef { |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3192 | $6.S.makeSignless(); |
| 3193 | $9.S.makeSignless(); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3194 | BasicBlock* tmpBBA = getBBVal($6); |
| 3195 | BasicBlock* tmpBBB = getBBVal($9); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3196 | $3.S.makeUnsigned(); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3197 | Value* tmpVal = getVal(Type::Int1Ty, $3); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3198 | $$.TI = new BranchInst(tmpBBA, tmpBBB, tmpVal); |
| 3199 | $$.S.makeSignless(); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3200 | } |
| 3201 | | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' { |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3202 | $3.S.copy($2.S); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3203 | Value* tmpVal = getVal($2.T, $3); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3204 | $6.S.makeSignless(); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3205 | BasicBlock* tmpBB = getBBVal($6); |
| 3206 | SwitchInst *S = new SwitchInst(tmpVal, tmpBB, $8->size()); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3207 | $$.TI = S; |
| 3208 | $$.S.makeSignless(); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3209 | std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(), |
| 3210 | E = $8->end(); |
| 3211 | for (; I != E; ++I) { |
| 3212 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first)) |
| 3213 | S->addCase(CI, I->second); |
| 3214 | else |
| 3215 | error("Switch case is constant, but not a simple integer"); |
| 3216 | } |
| 3217 | delete $8; |
| 3218 | } |
| 3219 | | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' { |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3220 | $3.S.copy($2.S); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3221 | Value* tmpVal = getVal($2.T, $3); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3222 | $6.S.makeSignless(); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3223 | BasicBlock* tmpBB = getBBVal($6); |
| 3224 | SwitchInst *S = new SwitchInst(tmpVal, tmpBB, 0); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3225 | $$.TI = S; |
| 3226 | $$.S.makeSignless(); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3227 | } |
| 3228 | | INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')' |
| 3229 | TO LABEL ValueRef Unwind LABEL ValueRef { |
| 3230 | const PointerType *PFTy; |
| 3231 | const FunctionType *Ty; |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3232 | Signedness FTySign; |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3233 | |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 3234 | if (!(PFTy = dyn_cast<PointerType>($3.PAT->get())) || |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3235 | !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) { |
| 3236 | // Pull out the types of all of the arguments... |
| 3237 | std::vector<const Type*> ParamTypes; |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3238 | FTySign.makeComposite($3.S); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3239 | if ($6) { |
| 3240 | for (std::vector<ValueInfo>::iterator I = $6->begin(), E = $6->end(); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3241 | I != E; ++I) { |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3242 | ParamTypes.push_back((*I).V->getType()); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3243 | FTySign.add(I->S); |
| 3244 | } |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3245 | } |
| 3246 | bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy; |
| 3247 | if (isVarArg) ParamTypes.pop_back(); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 3248 | Ty = FunctionType::get($3.PAT->get(), ParamTypes, isVarArg); |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 3249 | PFTy = PointerType::getUnqual(Ty); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3250 | $$.S.copy($3.S); |
| 3251 | } else { |
| 3252 | FTySign = $3.S; |
Reid Spencer | b289f19 | 2007-04-07 16:10:37 +0000 | [diff] [blame] | 3253 | // Get the signedness of the result type. $3 is the pointer to the |
| 3254 | // function type so we get the 0th element to extract the function type, |
| 3255 | // and then the 0th element again to get the result type. |
| 3256 | $$.S.copy($3.S.get(0).get(0)); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3257 | } |
Reid Spencer | 460dd9b | 2007-04-09 06:15:59 +0000 | [diff] [blame] | 3258 | |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3259 | $4.S.makeComposite(FTySign); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3260 | Value *V = getVal(PFTy, $4); // Get the function we're calling... |
| 3261 | BasicBlock *Normal = getBBVal($10); |
| 3262 | BasicBlock *Except = getBBVal($13); |
| 3263 | |
| 3264 | // Create the call node... |
| 3265 | if (!$6) { // Has no arguments? |
David Greene | f1355a5 | 2007-08-27 19:04:21 +0000 | [diff] [blame] | 3266 | std::vector<Value*> Args; |
| 3267 | $$.TI = new InvokeInst(V, Normal, Except, Args.begin(), Args.end()); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3268 | } else { // Has arguments? |
| 3269 | // Loop through FunctionType's arguments and ensure they are specified |
| 3270 | // correctly! |
| 3271 | // |
| 3272 | FunctionType::param_iterator I = Ty->param_begin(); |
| 3273 | FunctionType::param_iterator E = Ty->param_end(); |
| 3274 | std::vector<ValueInfo>::iterator ArgI = $6->begin(), ArgE = $6->end(); |
| 3275 | |
| 3276 | std::vector<Value*> Args; |
| 3277 | for (; ArgI != ArgE && I != E; ++ArgI, ++I) { |
| 3278 | if ((*ArgI).V->getType() != *I) |
| 3279 | error("Parameter " +(*ArgI).V->getName()+ " is not of type '" + |
| 3280 | (*I)->getDescription() + "'"); |
| 3281 | Args.push_back((*ArgI).V); |
| 3282 | } |
| 3283 | |
| 3284 | if (I != E || (ArgI != ArgE && !Ty->isVarArg())) |
| 3285 | error("Invalid number of parameters detected"); |
| 3286 | |
David Greene | f1355a5 | 2007-08-27 19:04:21 +0000 | [diff] [blame] | 3287 | $$.TI = new InvokeInst(V, Normal, Except, Args.begin(), Args.end()); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3288 | } |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3289 | cast<InvokeInst>($$.TI)->setCallingConv(upgradeCallingConv($2)); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 3290 | if ($2 == OldCallingConv::CSRet) { |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 3291 | ParamAttrsWithIndex PAWI = |
| 3292 | ParamAttrsWithIndex::get(1, ParamAttr::StructRet); // first arg |
| 3293 | cast<InvokeInst>($$.TI)->setParamAttrs(PAListPtr::get(&PAWI, 1)); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 3294 | } |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 3295 | delete $3.PAT; |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3296 | delete $6; |
Reid Spencer | 4f859aa | 2007-04-22 05:46:44 +0000 | [diff] [blame] | 3297 | lastCallingConv = OldCallingConv::C; |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3298 | } |
| 3299 | | Unwind { |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3300 | $$.TI = new UnwindInst(); |
| 3301 | $$.S.makeSignless(); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3302 | } |
| 3303 | | UNREACHABLE { |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3304 | $$.TI = new UnreachableInst(); |
| 3305 | $$.S.makeSignless(); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3306 | } |
| 3307 | ; |
| 3308 | |
| 3309 | JumpTable |
| 3310 | : JumpTable IntType ConstValueRef ',' LABEL ValueRef { |
| 3311 | $$ = $1; |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3312 | $3.S.copy($2.S); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3313 | Constant *V = cast<Constant>(getExistingValue($2.T, $3)); |
| 3314 | |
| 3315 | if (V == 0) |
| 3316 | error("May only switch on a constant pool value"); |
| 3317 | |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3318 | $6.S.makeSignless(); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3319 | BasicBlock* tmpBB = getBBVal($6); |
| 3320 | $$->push_back(std::make_pair(V, tmpBB)); |
| 3321 | } |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 3322 | | IntType ConstValueRef ',' LABEL ValueRef { |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3323 | $$ = new std::vector<std::pair<Constant*, BasicBlock*> >(); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3324 | $2.S.copy($1.S); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3325 | Constant *V = cast<Constant>(getExistingValue($1.T, $2)); |
| 3326 | |
| 3327 | if (V == 0) |
| 3328 | error("May only switch on a constant pool value"); |
| 3329 | |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3330 | $5.S.makeSignless(); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3331 | BasicBlock* tmpBB = getBBVal($5); |
| 3332 | $$->push_back(std::make_pair(V, tmpBB)); |
| 3333 | } |
| 3334 | ; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 3335 | |
| 3336 | Inst |
| 3337 | : OptAssign InstVal { |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3338 | bool omit = false; |
| 3339 | if ($1) |
| 3340 | if (BitCastInst *BCI = dyn_cast<BitCastInst>($2.I)) |
| 3341 | if (BCI->getSrcTy() == BCI->getDestTy() && |
| 3342 | BCI->getOperand(0)->getName() == $1) |
| 3343 | // This is a useless bit cast causing a name redefinition. It is |
| 3344 | // a bit cast from a type to the same type of an operand with the |
| 3345 | // same name as the name we would give this instruction. Since this |
| 3346 | // instruction results in no code generation, it is safe to omit |
| 3347 | // the instruction. This situation can occur because of collapsed |
| 3348 | // type planes. For example: |
| 3349 | // %X = add int %Y, %Z |
| 3350 | // %X = cast int %Y to uint |
| 3351 | // After upgrade, this looks like: |
| 3352 | // %X = add i32 %Y, %Z |
| 3353 | // %X = bitcast i32 to i32 |
| 3354 | // The bitcast is clearly useless so we omit it. |
| 3355 | omit = true; |
| 3356 | if (omit) { |
| 3357 | $$.I = 0; |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3358 | $$.S.makeSignless(); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3359 | } else { |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3360 | ValueInfo VI; VI.V = $2.I; VI.S.copy($2.S); |
| 3361 | setValueName(VI, $1); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3362 | InsertValue($2.I); |
| 3363 | $$ = $2; |
Reid Spencer | 16222c0 | 2007-01-01 01:20:16 +0000 | [diff] [blame] | 3364 | } |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 3365 | }; |
| 3366 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3367 | PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes |
| 3368 | $$.P = new std::list<std::pair<Value*, BasicBlock*> >(); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3369 | $$.S.copy($1.S); |
| 3370 | $3.S.copy($1.S); |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 3371 | Value* tmpVal = getVal($1.PAT->get(), $3); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3372 | $5.S.makeSignless(); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3373 | BasicBlock* tmpBB = getBBVal($5); |
| 3374 | $$.P->push_back(std::make_pair(tmpVal, tmpBB)); |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 3375 | delete $1.PAT; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 3376 | } |
| 3377 | | PHIList ',' '[' ValueRef ',' ValueRef ']' { |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 3378 | $$ = $1; |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3379 | $4.S.copy($1.S); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3380 | Value* tmpVal = getVal($1.P->front().first->getType(), $4); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3381 | $6.S.makeSignless(); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3382 | BasicBlock* tmpBB = getBBVal($6); |
| 3383 | $1.P->push_back(std::make_pair(tmpVal, tmpBB)); |
| 3384 | } |
| 3385 | ; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 3386 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3387 | ValueRefList : ResolvedVal { // Used for call statements, and memory insts... |
| 3388 | $$ = new std::vector<ValueInfo>(); |
Reid Spencer | f848365 | 2006-12-02 15:16:01 +0000 | [diff] [blame] | 3389 | $$->push_back($1); |
| 3390 | } |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 3391 | | ValueRefList ',' ResolvedVal { |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 3392 | $$ = $1; |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3393 | $1->push_back($3); |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 3394 | }; |
| 3395 | |
| 3396 | // ValueRefListE - Just like ValueRefList, except that it may also be empty! |
| 3397 | ValueRefListE |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3398 | : ValueRefList |
| 3399 | | /*empty*/ { $$ = 0; } |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 3400 | ; |
| 3401 | |
| 3402 | OptTailCall |
| 3403 | : TAIL CALL { |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3404 | $$ = true; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 3405 | } |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3406 | | CALL { |
| 3407 | $$ = false; |
| 3408 | } |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 3409 | ; |
| 3410 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3411 | InstVal |
| 3412 | : ArithmeticOps Types ValueRef ',' ValueRef { |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3413 | $3.S.copy($2.S); |
| 3414 | $5.S.copy($2.S); |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 3415 | const Type* Ty = $2.PAT->get(); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 3416 | if (!Ty->isInteger() && !Ty->isFloatingPoint() && !isa<VectorType>(Ty)) |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3417 | error("Arithmetic operator requires integer, FP, or packed operands"); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 3418 | if (isa<VectorType>(Ty) && |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3419 | ($1 == URemOp || $1 == SRemOp || $1 == FRemOp || $1 == RemOp)) |
Reid Spencer | ac9dcb9 | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 3420 | error("Remainder not supported on vector types"); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3421 | // Upgrade the opcode from obsolete versions before we do anything with it. |
| 3422 | Instruction::BinaryOps Opcode = getBinaryOp($1, Ty, $2.S); |
| 3423 | Value* val1 = getVal(Ty, $3); |
| 3424 | Value* val2 = getVal(Ty, $5); |
| 3425 | $$.I = BinaryOperator::create(Opcode, val1, val2); |
| 3426 | if ($$.I == 0) |
| 3427 | error("binary operator returned null"); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3428 | $$.S.copy($2.S); |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 3429 | delete $2.PAT; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 3430 | } |
| 3431 | | LogicalOps Types ValueRef ',' ValueRef { |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3432 | $3.S.copy($2.S); |
| 3433 | $5.S.copy($2.S); |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 3434 | const Type *Ty = $2.PAT->get(); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3435 | if (!Ty->isInteger()) { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 3436 | if (!isa<VectorType>(Ty) || |
| 3437 | !cast<VectorType>(Ty)->getElementType()->isInteger()) |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3438 | error("Logical operator requires integral operands"); |
| 3439 | } |
| 3440 | Instruction::BinaryOps Opcode = getBinaryOp($1, Ty, $2.S); |
| 3441 | Value* tmpVal1 = getVal(Ty, $3); |
| 3442 | Value* tmpVal2 = getVal(Ty, $5); |
| 3443 | $$.I = BinaryOperator::create(Opcode, tmpVal1, tmpVal2); |
| 3444 | if ($$.I == 0) |
| 3445 | error("binary operator returned null"); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3446 | $$.S.copy($2.S); |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 3447 | delete $2.PAT; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 3448 | } |
| 3449 | | SetCondOps Types ValueRef ',' ValueRef { |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3450 | $3.S.copy($2.S); |
| 3451 | $5.S.copy($2.S); |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 3452 | const Type* Ty = $2.PAT->get(); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 3453 | if(isa<VectorType>(Ty)) |
| 3454 | error("VectorTypes currently not supported in setcc instructions"); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3455 | unsigned short pred; |
| 3456 | Instruction::OtherOps Opcode = getCompareOp($1, pred, Ty, $2.S); |
| 3457 | Value* tmpVal1 = getVal(Ty, $3); |
| 3458 | Value* tmpVal2 = getVal(Ty, $5); |
| 3459 | $$.I = CmpInst::create(Opcode, pred, tmpVal1, tmpVal2); |
| 3460 | if ($$.I == 0) |
| 3461 | error("binary operator returned null"); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3462 | $$.S.makeUnsigned(); |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 3463 | delete $2.PAT; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 3464 | } |
Reid Spencer | a9d7e89 | 2006-12-29 20:33:37 +0000 | [diff] [blame] | 3465 | | ICMP IPredicates Types ValueRef ',' ValueRef { |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3466 | $4.S.copy($3.S); |
| 3467 | $6.S.copy($3.S); |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 3468 | const Type *Ty = $3.PAT->get(); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 3469 | if (isa<VectorType>(Ty)) |
| 3470 | error("VectorTypes currently not supported in icmp instructions"); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3471 | else if (!Ty->isInteger() && !isa<PointerType>(Ty)) |
| 3472 | error("icmp requires integer or pointer typed operands"); |
| 3473 | Value* tmpVal1 = getVal(Ty, $4); |
| 3474 | Value* tmpVal2 = getVal(Ty, $6); |
| 3475 | $$.I = new ICmpInst($2, tmpVal1, tmpVal2); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3476 | $$.S.makeUnsigned(); |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 3477 | delete $3.PAT; |
Reid Spencer | 57f28f9 | 2006-12-03 07:10:26 +0000 | [diff] [blame] | 3478 | } |
Reid Spencer | a9d7e89 | 2006-12-29 20:33:37 +0000 | [diff] [blame] | 3479 | | FCMP FPredicates Types ValueRef ',' ValueRef { |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3480 | $4.S.copy($3.S); |
| 3481 | $6.S.copy($3.S); |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 3482 | const Type *Ty = $3.PAT->get(); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 3483 | if (isa<VectorType>(Ty)) |
| 3484 | error("VectorTypes currently not supported in fcmp instructions"); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3485 | else if (!Ty->isFloatingPoint()) |
| 3486 | error("fcmp instruction requires floating point operands"); |
| 3487 | Value* tmpVal1 = getVal(Ty, $4); |
| 3488 | Value* tmpVal2 = getVal(Ty, $6); |
| 3489 | $$.I = new FCmpInst($2, tmpVal1, tmpVal2); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3490 | $$.S.makeUnsigned(); |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 3491 | delete $3.PAT; |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3492 | } |
| 3493 | | NOT ResolvedVal { |
| 3494 | warning("Use of obsolete 'not' instruction: Replacing with 'xor"); |
| 3495 | const Type *Ty = $2.V->getType(); |
| 3496 | Value *Ones = ConstantInt::getAllOnesValue(Ty); |
| 3497 | if (Ones == 0) |
| 3498 | error("Expected integral type for not instruction"); |
| 3499 | $$.I = BinaryOperator::create(Instruction::Xor, $2.V, Ones); |
| 3500 | if ($$.I == 0) |
| 3501 | error("Could not create a xor instruction"); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3502 | $$.S.copy($2.S); |
Reid Spencer | 2b40438 | 2006-12-02 22:09:27 +0000 | [diff] [blame] | 3503 | } |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 3504 | | ShiftOps ResolvedVal ',' ResolvedVal { |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3505 | if (!$4.V->getType()->isInteger() || |
| 3506 | cast<IntegerType>($4.V->getType())->getBitWidth() != 8) |
| 3507 | error("Shift amount must be int8"); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3508 | const Type* Ty = $2.V->getType(); |
| 3509 | if (!Ty->isInteger()) |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3510 | error("Shift constant expression requires integer operand"); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3511 | Value* ShiftAmt = 0; |
| 3512 | if (cast<IntegerType>(Ty)->getBitWidth() > Type::Int8Ty->getBitWidth()) |
| 3513 | if (Constant *C = dyn_cast<Constant>($4.V)) |
| 3514 | ShiftAmt = ConstantExpr::getZExt(C, Ty); |
| 3515 | else |
| 3516 | ShiftAmt = new ZExtInst($4.V, Ty, makeNameUnique("shift"), CurBB); |
| 3517 | else |
| 3518 | ShiftAmt = $4.V; |
| 3519 | $$.I = BinaryOperator::create(getBinaryOp($1, Ty, $2.S), $2.V, ShiftAmt); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3520 | $$.S.copy($2.S); |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 3521 | } |
Reid Spencer | fcb5df8 | 2006-12-01 22:34:43 +0000 | [diff] [blame] | 3522 | | CastOps ResolvedVal TO Types { |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 3523 | const Type *DstTy = $4.PAT->get(); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3524 | if (!DstTy->isFirstClassType()) |
| 3525 | error("cast instruction to a non-primitive type: '" + |
| 3526 | DstTy->getDescription() + "'"); |
| 3527 | $$.I = cast<Instruction>(getCast($1, $2.V, $2.S, DstTy, $4.S, true)); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3528 | $$.S.copy($4.S); |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 3529 | delete $4.PAT; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 3530 | } |
| 3531 | | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal { |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3532 | if (!$2.V->getType()->isInteger() || |
| 3533 | cast<IntegerType>($2.V->getType())->getBitWidth() != 1) |
| 3534 | error("select condition must be bool"); |
| 3535 | if ($4.V->getType() != $6.V->getType()) |
| 3536 | error("select value types should match"); |
| 3537 | $$.I = new SelectInst($2.V, $4.V, $6.V); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3538 | $$.S.copy($4.S); |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 3539 | } |
| 3540 | | VAARG ResolvedVal ',' Types { |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 3541 | const Type *Ty = $4.PAT->get(); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3542 | NewVarArgs = true; |
| 3543 | $$.I = new VAArgInst($2.V, Ty); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3544 | $$.S.copy($4.S); |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 3545 | delete $4.PAT; |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3546 | } |
| 3547 | | VAARG_old ResolvedVal ',' Types { |
| 3548 | const Type* ArgTy = $2.V->getType(); |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 3549 | const Type* DstTy = $4.PAT->get(); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3550 | ObsoleteVarArgs = true; |
Duncan Sands | e2c4304 | 2008-04-07 13:45:04 +0000 | [diff] [blame] | 3551 | Function* NF = Intrinsic::getDeclaration(CurModule.CurrentModule, |
| 3552 | Intrinsic::va_copy); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3553 | |
| 3554 | //b = vaarg a, t -> |
| 3555 | //foo = alloca 1 of t |
| 3556 | //bar = vacopy a |
| 3557 | //store bar -> foo |
| 3558 | //b = vaarg foo, t |
| 3559 | AllocaInst* foo = new AllocaInst(ArgTy, 0, "vaarg.fix"); |
| 3560 | CurBB->getInstList().push_back(foo); |
| 3561 | CallInst* bar = new CallInst(NF, $2.V); |
| 3562 | CurBB->getInstList().push_back(bar); |
| 3563 | CurBB->getInstList().push_back(new StoreInst(bar, foo)); |
| 3564 | $$.I = new VAArgInst(foo, DstTy); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3565 | $$.S.copy($4.S); |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 3566 | delete $4.PAT; |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3567 | } |
| 3568 | | VANEXT_old ResolvedVal ',' Types { |
| 3569 | const Type* ArgTy = $2.V->getType(); |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 3570 | const Type* DstTy = $4.PAT->get(); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3571 | ObsoleteVarArgs = true; |
Duncan Sands | e2c4304 | 2008-04-07 13:45:04 +0000 | [diff] [blame] | 3572 | Function* NF = Intrinsic::getDeclaration(CurModule.CurrentModule, |
| 3573 | Intrinsic::va_copy); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3574 | |
| 3575 | //b = vanext a, t -> |
| 3576 | //foo = alloca 1 of t |
| 3577 | //bar = vacopy a |
| 3578 | //store bar -> foo |
| 3579 | //tmp = vaarg foo, t |
| 3580 | //b = load foo |
| 3581 | AllocaInst* foo = new AllocaInst(ArgTy, 0, "vanext.fix"); |
| 3582 | CurBB->getInstList().push_back(foo); |
| 3583 | CallInst* bar = new CallInst(NF, $2.V); |
| 3584 | CurBB->getInstList().push_back(bar); |
| 3585 | CurBB->getInstList().push_back(new StoreInst(bar, foo)); |
| 3586 | Instruction* tmp = new VAArgInst(foo, DstTy); |
| 3587 | CurBB->getInstList().push_back(tmp); |
| 3588 | $$.I = new LoadInst(foo); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3589 | $$.S.copy($4.S); |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 3590 | delete $4.PAT; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 3591 | } |
| 3592 | | EXTRACTELEMENT ResolvedVal ',' ResolvedVal { |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3593 | if (!ExtractElementInst::isValidOperands($2.V, $4.V)) |
| 3594 | error("Invalid extractelement operands"); |
| 3595 | $$.I = new ExtractElementInst($2.V, $4.V); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3596 | $$.S.copy($2.S.get(0)); |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 3597 | } |
| 3598 | | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal { |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3599 | if (!InsertElementInst::isValidOperands($2.V, $4.V, $6.V)) |
| 3600 | error("Invalid insertelement operands"); |
| 3601 | $$.I = new InsertElementInst($2.V, $4.V, $6.V); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3602 | $$.S.copy($2.S); |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 3603 | } |
| 3604 | | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal { |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3605 | if (!ShuffleVectorInst::isValidOperands($2.V, $4.V, $6.V)) |
| 3606 | error("Invalid shufflevector operands"); |
| 3607 | $$.I = new ShuffleVectorInst($2.V, $4.V, $6.V); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3608 | $$.S.copy($2.S); |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 3609 | } |
| 3610 | | PHI_TOK PHIList { |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3611 | const Type *Ty = $2.P->front().first->getType(); |
| 3612 | if (!Ty->isFirstClassType()) |
| 3613 | error("PHI node operands must be of first class type"); |
| 3614 | PHINode *PHI = new PHINode(Ty); |
| 3615 | PHI->reserveOperandSpace($2.P->size()); |
| 3616 | while ($2.P->begin() != $2.P->end()) { |
| 3617 | if ($2.P->front().first->getType() != Ty) |
| 3618 | error("All elements of a PHI node must be of the same type"); |
| 3619 | PHI->addIncoming($2.P->front().first, $2.P->front().second); |
| 3620 | $2.P->pop_front(); |
| 3621 | } |
| 3622 | $$.I = PHI; |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3623 | $$.S.copy($2.S); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3624 | delete $2.P; // Free the list... |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 3625 | } |
Reid Spencer | 460dd9b | 2007-04-09 06:15:59 +0000 | [diff] [blame] | 3626 | | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' { |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3627 | // Handle the short call syntax |
| 3628 | const PointerType *PFTy; |
| 3629 | const FunctionType *FTy; |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3630 | Signedness FTySign; |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 3631 | if (!(PFTy = dyn_cast<PointerType>($3.PAT->get())) || |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3632 | !(FTy = dyn_cast<FunctionType>(PFTy->getElementType()))) { |
| 3633 | // Pull out the types of all of the arguments... |
| 3634 | std::vector<const Type*> ParamTypes; |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3635 | FTySign.makeComposite($3.S); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3636 | if ($6) { |
| 3637 | for (std::vector<ValueInfo>::iterator I = $6->begin(), E = $6->end(); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3638 | I != E; ++I) { |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3639 | ParamTypes.push_back((*I).V->getType()); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3640 | FTySign.add(I->S); |
| 3641 | } |
Reid Spencer | fbb7b69 | 2007-01-13 00:02:00 +0000 | [diff] [blame] | 3642 | } |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3643 | |
| 3644 | bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy; |
| 3645 | if (isVarArg) ParamTypes.pop_back(); |
| 3646 | |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 3647 | const Type *RetTy = $3.PAT->get(); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3648 | if (!RetTy->isFirstClassType() && RetTy != Type::VoidTy) |
| 3649 | error("Functions cannot return aggregate types"); |
| 3650 | |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 3651 | FTy = FunctionType::get(RetTy, ParamTypes, isVarArg); |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 3652 | PFTy = PointerType::getUnqual(FTy); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3653 | $$.S.copy($3.S); |
| 3654 | } else { |
| 3655 | FTySign = $3.S; |
Reid Spencer | b289f19 | 2007-04-07 16:10:37 +0000 | [diff] [blame] | 3656 | // Get the signedness of the result type. $3 is the pointer to the |
| 3657 | // function type so we get the 0th element to extract the function type, |
| 3658 | // and then the 0th element again to get the result type. |
| 3659 | $$.S.copy($3.S.get(0).get(0)); |
Reid Spencer | f848365 | 2006-12-02 15:16:01 +0000 | [diff] [blame] | 3660 | } |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3661 | $4.S.makeComposite(FTySign); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3662 | |
| 3663 | // First upgrade any intrinsic calls. |
| 3664 | std::vector<Value*> Args; |
| 3665 | if ($6) |
| 3666 | for (unsigned i = 0, e = $6->size(); i < e; ++i) |
| 3667 | Args.push_back((*$6)[i].V); |
Reid Spencer | 1e70bb6 | 2007-04-02 00:50:28 +0000 | [diff] [blame] | 3668 | Instruction *Inst = upgradeIntrinsicCall(FTy->getReturnType(), $4, Args); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3669 | |
| 3670 | // If we got an upgraded intrinsic |
| 3671 | if (Inst) { |
| 3672 | $$.I = Inst; |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3673 | } else { |
| 3674 | // Get the function we're calling |
| 3675 | Value *V = getVal(PFTy, $4); |
| 3676 | |
| 3677 | // Check the argument values match |
| 3678 | if (!$6) { // Has no arguments? |
| 3679 | // Make sure no arguments is a good thing! |
| 3680 | if (FTy->getNumParams() != 0) |
| 3681 | error("No arguments passed to a function that expects arguments"); |
| 3682 | } else { // Has arguments? |
| 3683 | // Loop through FunctionType's arguments and ensure they are specified |
| 3684 | // correctly! |
| 3685 | // |
| 3686 | FunctionType::param_iterator I = FTy->param_begin(); |
| 3687 | FunctionType::param_iterator E = FTy->param_end(); |
| 3688 | std::vector<ValueInfo>::iterator ArgI = $6->begin(), ArgE = $6->end(); |
| 3689 | |
| 3690 | for (; ArgI != ArgE && I != E; ++ArgI, ++I) |
| 3691 | if ((*ArgI).V->getType() != *I) |
| 3692 | error("Parameter " +(*ArgI).V->getName()+ " is not of type '" + |
| 3693 | (*I)->getDescription() + "'"); |
| 3694 | |
| 3695 | if (I != E || (ArgI != ArgE && !FTy->isVarArg())) |
| 3696 | error("Invalid number of parameters detected"); |
| 3697 | } |
| 3698 | |
| 3699 | // Create the call instruction |
David Greene | 52eec54 | 2007-08-01 03:43:44 +0000 | [diff] [blame] | 3700 | CallInst *CI = new CallInst(V, Args.begin(), Args.end()); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3701 | CI->setTailCall($1); |
Reid Spencer | 43f76c9 | 2007-01-29 05:41:09 +0000 | [diff] [blame] | 3702 | CI->setCallingConv(upgradeCallingConv($2)); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 3703 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3704 | $$.I = CI; |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3705 | } |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 3706 | // Deal with CSRetCC |
| 3707 | if ($2 == OldCallingConv::CSRet) { |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 3708 | ParamAttrsWithIndex PAWI = |
| 3709 | ParamAttrsWithIndex::get(1, ParamAttr::StructRet); // first arg |
| 3710 | cast<CallInst>($$.I)->setParamAttrs(PAListPtr::get(&PAWI, 1)); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 3711 | } |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 3712 | delete $3.PAT; |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3713 | delete $6; |
Reid Spencer | 4f859aa | 2007-04-22 05:46:44 +0000 | [diff] [blame] | 3714 | lastCallingConv = OldCallingConv::C; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 3715 | } |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3716 | | MemoryInst { |
| 3717 | $$ = $1; |
| 3718 | } |
| 3719 | ; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 3720 | |
| 3721 | |
| 3722 | // IndexList - List of indices for GEP based instructions... |
| 3723 | IndexList |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3724 | : ',' ValueRefList { $$ = $2; } |
| 3725 | | /* empty */ { $$ = new std::vector<ValueInfo>(); } |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 3726 | ; |
| 3727 | |
| 3728 | OptVolatile |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3729 | : VOLATILE { $$ = true; } |
| 3730 | | /* empty */ { $$ = false; } |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 3731 | ; |
| 3732 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3733 | MemoryInst |
| 3734 | : MALLOC Types OptCAlign { |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 3735 | const Type *Ty = $2.PAT->get(); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3736 | $$.S.makeComposite($2.S); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3737 | $$.I = new MallocInst(Ty, 0, $3); |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 3738 | delete $2.PAT; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 3739 | } |
| 3740 | | MALLOC Types ',' UINT ValueRef OptCAlign { |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 3741 | const Type *Ty = $2.PAT->get(); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3742 | $5.S.makeUnsigned(); |
| 3743 | $$.S.makeComposite($2.S); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3744 | $$.I = new MallocInst(Ty, getVal($4.T, $5), $6); |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 3745 | delete $2.PAT; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 3746 | } |
| 3747 | | ALLOCA Types OptCAlign { |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 3748 | const Type *Ty = $2.PAT->get(); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3749 | $$.S.makeComposite($2.S); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3750 | $$.I = new AllocaInst(Ty, 0, $3); |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 3751 | delete $2.PAT; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 3752 | } |
| 3753 | | ALLOCA Types ',' UINT ValueRef OptCAlign { |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 3754 | const Type *Ty = $2.PAT->get(); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3755 | $5.S.makeUnsigned(); |
| 3756 | $$.S.makeComposite($4.S); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3757 | $$.I = new AllocaInst(Ty, getVal($4.T, $5), $6); |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 3758 | delete $2.PAT; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 3759 | } |
| 3760 | | FREE ResolvedVal { |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3761 | const Type *PTy = $2.V->getType(); |
| 3762 | if (!isa<PointerType>(PTy)) |
| 3763 | error("Trying to free nonpointer type '" + PTy->getDescription() + "'"); |
| 3764 | $$.I = new FreeInst($2.V); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3765 | $$.S.makeSignless(); |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 3766 | } |
| 3767 | | OptVolatile LOAD Types ValueRef { |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 3768 | const Type* Ty = $3.PAT->get(); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3769 | $4.S.copy($3.S); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3770 | if (!isa<PointerType>(Ty)) |
| 3771 | error("Can't load from nonpointer type: " + Ty->getDescription()); |
| 3772 | if (!cast<PointerType>(Ty)->getElementType()->isFirstClassType()) |
| 3773 | error("Can't load from pointer of non-first-class type: " + |
| 3774 | Ty->getDescription()); |
| 3775 | Value* tmpVal = getVal(Ty, $4); |
| 3776 | $$.I = new LoadInst(tmpVal, "", $1); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3777 | $$.S.copy($3.S.get(0)); |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 3778 | delete $3.PAT; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 3779 | } |
| 3780 | | OptVolatile STORE ResolvedVal ',' Types ValueRef { |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3781 | $6.S.copy($5.S); |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 3782 | const PointerType *PTy = dyn_cast<PointerType>($5.PAT->get()); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3783 | if (!PTy) |
| 3784 | error("Can't store to a nonpointer type: " + |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 3785 | $5.PAT->get()->getDescription()); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3786 | const Type *ElTy = PTy->getElementType(); |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 3787 | Value *StoreVal = $3.V; |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3788 | Value* tmpVal = getVal(PTy, $6); |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 3789 | if (ElTy != $3.V->getType()) { |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 3790 | PTy = PointerType::getUnqual(StoreVal->getType()); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 3791 | if (Constant *C = dyn_cast<Constant>(tmpVal)) |
| 3792 | tmpVal = ConstantExpr::getBitCast(C, PTy); |
| 3793 | else |
| 3794 | tmpVal = new BitCastInst(tmpVal, PTy, "upgrd.cast", CurBB); |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 3795 | } |
| 3796 | $$.I = new StoreInst(StoreVal, tmpVal, $1); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3797 | $$.S.makeSignless(); |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 3798 | delete $5.PAT; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 3799 | } |
| 3800 | | GETELEMENTPTR Types ValueRef IndexList { |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3801 | $3.S.copy($2.S); |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 3802 | const Type* Ty = $2.PAT->get(); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3803 | if (!isa<PointerType>(Ty)) |
| 3804 | error("getelementptr insn requires pointer operand"); |
| 3805 | |
| 3806 | std::vector<Value*> VIndices; |
Reid Spencer | d23c4dd | 2007-04-16 00:39:39 +0000 | [diff] [blame] | 3807 | upgradeGEPInstIndices(Ty, $4, VIndices); |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3808 | |
| 3809 | Value* tmpVal = getVal(Ty, $3); |
David Greene | b8f7479 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 3810 | $$.I = new GetElementPtrInst(tmpVal, VIndices.begin(), VIndices.end()); |
Reid Spencer | 3e5affd | 2007-03-21 17:14:36 +0000 | [diff] [blame] | 3811 | ValueInfo VI; VI.V = tmpVal; VI.S.copy($2.S); |
| 3812 | $$.S.copy(getElementSign(VI, VIndices)); |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 3813 | delete $2.PAT; |
Reid Spencer | e0a15bb | 2007-01-15 00:25:53 +0000 | [diff] [blame] | 3814 | delete $4; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 3815 | }; |
| 3816 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3817 | |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 3818 | %% |
| 3819 | |
| 3820 | int yyerror(const char *ErrorMsg) { |
| 3821 | std::string where |
| 3822 | = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename) |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 3823 | + ":" + llvm::utostr((unsigned) Upgradelineno) + ": "; |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3824 | std::string errMsg = where + "error: " + std::string(ErrorMsg); |
| 3825 | if (yychar != YYEMPTY && yychar != 0) |
| 3826 | errMsg += " while reading token '" + std::string(Upgradetext, Upgradeleng) + |
| 3827 | "'."; |
Reid Spencer | 05e52a1 | 2006-12-31 05:45:57 +0000 | [diff] [blame] | 3828 | std::cerr << "llvm-upgrade: " << errMsg << '\n'; |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3829 | std::cout << "llvm-upgrade: parse failed.\n"; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 3830 | exit(1); |
| 3831 | } |
Reid Spencer | 90eb4d6 | 2007-01-05 17:18:58 +0000 | [diff] [blame] | 3832 | |
Reid Spencer | e0a15bb | 2007-01-15 00:25:53 +0000 | [diff] [blame] | 3833 | void warning(const std::string& ErrorMsg) { |
Reid Spencer | 90eb4d6 | 2007-01-05 17:18:58 +0000 | [diff] [blame] | 3834 | std::string where |
| 3835 | = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename) |
Reid Spencer | e7c326b | 2007-02-08 08:09:36 +0000 | [diff] [blame] | 3836 | + ":" + llvm::utostr((unsigned) Upgradelineno) + ": "; |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3837 | std::string errMsg = where + "warning: " + std::string(ErrorMsg); |
| 3838 | if (yychar != YYEMPTY && yychar != 0) |
| 3839 | errMsg += " while reading token '" + std::string(Upgradetext, Upgradeleng) + |
| 3840 | "'."; |
Reid Spencer | 90eb4d6 | 2007-01-05 17:18:58 +0000 | [diff] [blame] | 3841 | std::cerr << "llvm-upgrade: " << errMsg << '\n'; |
| 3842 | } |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 3843 | |
| 3844 | void error(const std::string& ErrorMsg, int LineNo) { |
| 3845 | if (LineNo == -1) LineNo = Upgradelineno; |
| 3846 | Upgradelineno = LineNo; |
| 3847 | yyerror(ErrorMsg.c_str()); |
| 3848 | } |
| 3849 | |