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