Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1 | //===-- llvmAsmParser.y - Parser for llvm assembly files --------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the bison parser for LLVM assembly languages files. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | %{ |
| 15 | #include "ParserInternals.h" |
| 16 | #include "llvm/CallingConv.h" |
| 17 | #include "llvm/InlineAsm.h" |
| 18 | #include "llvm/Instructions.h" |
| 19 | #include "llvm/Module.h" |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 20 | #include "llvm/ValueSymbolTable.h" |
Chandler Carruth | 0220219 | 2007-08-04 01:56:21 +0000 | [diff] [blame] | 21 | #include "llvm/AutoUpgrade.h" |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 22 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 23 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | f7469af | 2007-01-31 04:44:08 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/STLExtras.h" |
| 26 | #include "llvm/Support/MathExtras.h" |
Reid Spencer | 481169e | 2006-12-01 00:33:46 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Streams.h" |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 28 | #include <algorithm> |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 29 | #include <list> |
Chris Lattner | 8adde28 | 2007-02-11 21:40:10 +0000 | [diff] [blame] | 30 | #include <map> |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 31 | #include <utility> |
| 32 | |
Reid Spencer | e4f4759 | 2006-08-18 17:32:55 +0000 | [diff] [blame] | 33 | // The following is a gross hack. In order to rid the libAsmParser library of |
| 34 | // exceptions, we have to have a way of getting the yyparse function to go into |
| 35 | // an error situation. So, whenever we want an error to occur, the GenerateError |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 36 | // function (see bottom of file) sets TriggerError. Then, at the end of each |
| 37 | // production in the grammer we use CHECK_FOR_ERROR which will invoke YYERROR |
| 38 | // (a goto) to put YACC in error state. Furthermore, several calls to |
Reid Spencer | e4f4759 | 2006-08-18 17:32:55 +0000 | [diff] [blame] | 39 | // GenerateError are made from inside productions and they must simulate the |
| 40 | // previous exception behavior by exiting the production immediately. We have |
| 41 | // replaced these with the GEN_ERROR macro which calls GeneratError and then |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 42 | // immediately invokes YYERROR. This would be so much cleaner if it was a |
Reid Spencer | e4f4759 | 2006-08-18 17:32:55 +0000 | [diff] [blame] | 43 | // recursive descent parser. |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 44 | static bool TriggerError = false; |
Reid Spencer | f63697d | 2006-10-09 17:36:59 +0000 | [diff] [blame] | 45 | #define CHECK_FOR_ERROR { if (TriggerError) { TriggerError = false; YYABORT; } } |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 46 | #define GEN_ERROR(msg) { GenerateError(msg); YYERROR; } |
| 47 | |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 48 | int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit |
| 49 | int yylex(); // declaration" of xxx warnings. |
| 50 | int yyparse(); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 51 | using namespace llvm; |
| 52 | |
| 53 | static Module *ParserResult; |
| 54 | |
| 55 | // DEBUG_UPREFS - Define this symbol if you want to enable debugging output |
| 56 | // relating to upreferences in the input stream. |
| 57 | // |
| 58 | //#define DEBUG_UPREFS 1 |
| 59 | #ifdef DEBUG_UPREFS |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 60 | #define UR_OUT(X) cerr << X |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 61 | #else |
| 62 | #define UR_OUT(X) |
| 63 | #endif |
| 64 | |
| 65 | #define YYERROR_VERBOSE 1 |
| 66 | |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 67 | static GlobalVariable *CurGV; |
| 68 | |
| 69 | |
| 70 | // This contains info used when building the body of a function. It is |
| 71 | // destroyed when the function is completed. |
| 72 | // |
| 73 | typedef std::vector<Value *> ValueList; // Numbered defs |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 74 | |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 75 | static void |
Reid Spencer | 93c4003 | 2007-03-19 18:40:50 +0000 | [diff] [blame] | 76 | ResolveDefinitions(ValueList &LateResolvers, ValueList *FutureLateResolvers=0); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 77 | |
| 78 | static struct PerModuleInfo { |
| 79 | Module *CurrentModule; |
Reid Spencer | 93c4003 | 2007-03-19 18:40:50 +0000 | [diff] [blame] | 80 | ValueList Values; // Module level numbered definitions |
| 81 | ValueList LateResolveValues; |
Reid Spencer | 861d9d6 | 2006-11-28 07:29:44 +0000 | [diff] [blame] | 82 | std::vector<PATypeHolder> Types; |
| 83 | std::map<ValID, PATypeHolder> LateResolveTypes; |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 84 | |
| 85 | /// PlaceHolderInfo - When temporary placeholder objects are created, remember |
Chris Lattner | 0ad1970 | 2006-06-21 16:53:00 +0000 | [diff] [blame] | 86 | /// how they were referenced and on which line of the input they came from so |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 87 | /// that we can resolve them later and print error messages as appropriate. |
| 88 | std::map<Value*, std::pair<ValID, int> > PlaceHolderInfo; |
| 89 | |
| 90 | // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward |
| 91 | // references to global values. Global values may be referenced before they |
| 92 | // are defined, and if so, the temporary object that they represent is held |
| 93 | // here. This is used for forward references of GlobalValues. |
| 94 | // |
| 95 | typedef std::map<std::pair<const PointerType *, |
| 96 | ValID>, GlobalValue*> GlobalRefsType; |
| 97 | GlobalRefsType GlobalRefs; |
| 98 | |
| 99 | void ModuleDone() { |
| 100 | // If we could not resolve some functions at function compilation time |
| 101 | // (calls to functions before they are defined), resolve them now... Types |
| 102 | // are resolved when the constant pool has been completely parsed. |
| 103 | // |
| 104 | ResolveDefinitions(LateResolveValues); |
Reid Spencer | 5b7e753 | 2006-09-28 19:28:24 +0000 | [diff] [blame] | 105 | if (TriggerError) |
| 106 | return; |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 107 | |
| 108 | // Check to make sure that all global value forward references have been |
| 109 | // resolved! |
| 110 | // |
| 111 | if (!GlobalRefs.empty()) { |
| 112 | std::string UndefinedReferences = "Unresolved global references exist:\n"; |
| 113 | |
| 114 | for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end(); |
| 115 | I != E; ++I) { |
| 116 | UndefinedReferences += " " + I->first.first->getDescription() + " " + |
| 117 | I->first.second.getName() + "\n"; |
| 118 | } |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 119 | GenerateError(UndefinedReferences); |
Reid Spencer | 5b7e753 | 2006-09-28 19:28:24 +0000 | [diff] [blame] | 120 | return; |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Chandler Carruth | 0220219 | 2007-08-04 01:56:21 +0000 | [diff] [blame] | 123 | // Look for intrinsic functions and CallInst that need to be upgraded |
| 124 | for (Module::iterator FI = CurrentModule->begin(), |
| 125 | FE = CurrentModule->end(); FI != FE; ) |
| 126 | UpgradeCallsToIntrinsic(FI++); // must be post-increment, as we remove |
| 127 | |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 128 | Values.clear(); // Clear out function local definitions |
| 129 | Types.clear(); |
| 130 | CurrentModule = 0; |
| 131 | } |
| 132 | |
| 133 | // GetForwardRefForGlobal - Check to see if there is a forward reference |
| 134 | // for this global. If so, remove it from the GlobalRefs map and return it. |
| 135 | // If not, just return null. |
| 136 | GlobalValue *GetForwardRefForGlobal(const PointerType *PTy, ValID ID) { |
| 137 | // Check to see if there is a forward reference to this global variable... |
| 138 | // if there is, eliminate it and patch the reference to use the new def'n. |
| 139 | GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PTy, ID)); |
| 140 | GlobalValue *Ret = 0; |
| 141 | if (I != GlobalRefs.end()) { |
| 142 | Ret = I->second; |
Nuno Lopes | 8a5f347 | 2008-10-15 12:05:02 +0000 | [diff] [blame] | 143 | I->first.second.destroy(); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 144 | GlobalRefs.erase(I); |
| 145 | } |
| 146 | return Ret; |
| 147 | } |
Reid Spencer | 8c8a2dc | 2007-01-02 21:54:12 +0000 | [diff] [blame] | 148 | |
| 149 | bool TypeIsUnresolved(PATypeHolder* PATy) { |
| 150 | // If it isn't abstract, its resolved |
| 151 | const Type* Ty = PATy->get(); |
| 152 | if (!Ty->isAbstract()) |
| 153 | return false; |
| 154 | // Traverse the type looking for abstract types. If it isn't abstract then |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 155 | // we don't need to traverse that leg of the type. |
Reid Spencer | 8c8a2dc | 2007-01-02 21:54:12 +0000 | [diff] [blame] | 156 | std::vector<const Type*> WorkList, SeenList; |
| 157 | WorkList.push_back(Ty); |
| 158 | while (!WorkList.empty()) { |
| 159 | const Type* Ty = WorkList.back(); |
| 160 | SeenList.push_back(Ty); |
| 161 | WorkList.pop_back(); |
| 162 | if (const OpaqueType* OpTy = dyn_cast<OpaqueType>(Ty)) { |
| 163 | // Check to see if this is an unresolved type |
| 164 | std::map<ValID, PATypeHolder>::iterator I = LateResolveTypes.begin(); |
| 165 | std::map<ValID, PATypeHolder>::iterator E = LateResolveTypes.end(); |
| 166 | for ( ; I != E; ++I) { |
| 167 | if (I->second.get() == OpTy) |
| 168 | return true; |
| 169 | } |
| 170 | } else if (const SequentialType* SeqTy = dyn_cast<SequentialType>(Ty)) { |
| 171 | const Type* TheTy = SeqTy->getElementType(); |
| 172 | if (TheTy->isAbstract() && TheTy != Ty) { |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 173 | std::vector<const Type*>::iterator I = SeenList.begin(), |
Reid Spencer | 8c8a2dc | 2007-01-02 21:54:12 +0000 | [diff] [blame] | 174 | E = SeenList.end(); |
| 175 | for ( ; I != E; ++I) |
| 176 | if (*I == TheTy) |
| 177 | break; |
| 178 | if (I == E) |
| 179 | WorkList.push_back(TheTy); |
| 180 | } |
| 181 | } else if (const StructType* StrTy = dyn_cast<StructType>(Ty)) { |
| 182 | for (unsigned i = 0; i < StrTy->getNumElements(); ++i) { |
| 183 | const Type* TheTy = StrTy->getElementType(i); |
| 184 | if (TheTy->isAbstract() && TheTy != Ty) { |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 185 | std::vector<const Type*>::iterator I = SeenList.begin(), |
Reid Spencer | 8c8a2dc | 2007-01-02 21:54:12 +0000 | [diff] [blame] | 186 | E = SeenList.end(); |
| 187 | for ( ; I != E; ++I) |
| 188 | if (*I == TheTy) |
| 189 | break; |
| 190 | if (I == E) |
| 191 | WorkList.push_back(TheTy); |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | return false; |
| 197 | } |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 198 | } CurModule; |
| 199 | |
| 200 | static struct PerFunctionInfo { |
| 201 | Function *CurrentFunction; // Pointer to current function being created |
| 202 | |
Reid Spencer | 93c4003 | 2007-03-19 18:40:50 +0000 | [diff] [blame] | 203 | ValueList Values; // Keep track of #'d definitions |
| 204 | unsigned NextValNum; |
| 205 | ValueList LateResolveValues; |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 206 | bool isDeclare; // Is this function a forward declararation? |
Anton Korobeynikov | b74ed07 | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 207 | GlobalValue::LinkageTypes Linkage; // Linkage for forward declaration. |
Anton Korobeynikov | 7f70559 | 2007-01-12 19:20:47 +0000 | [diff] [blame] | 208 | GlobalValue::VisibilityTypes Visibility; |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 209 | |
| 210 | /// BBForwardRefs - When we see forward references to basic blocks, keep |
| 211 | /// track of them here. |
Reid Spencer | 93c4003 | 2007-03-19 18:40:50 +0000 | [diff] [blame] | 212 | std::map<ValID, BasicBlock*> BBForwardRefs; |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 213 | |
| 214 | inline PerFunctionInfo() { |
| 215 | CurrentFunction = 0; |
| 216 | isDeclare = false; |
Anton Korobeynikov | 7f70559 | 2007-01-12 19:20:47 +0000 | [diff] [blame] | 217 | Linkage = GlobalValue::ExternalLinkage; |
| 218 | Visibility = GlobalValue::DefaultVisibility; |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | inline void FunctionStart(Function *M) { |
| 222 | CurrentFunction = M; |
Reid Spencer | 93c4003 | 2007-03-19 18:40:50 +0000 | [diff] [blame] | 223 | NextValNum = 0; |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | void FunctionDone() { |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 227 | // Any forward referenced blocks left? |
Reid Spencer | 5b7e753 | 2006-09-28 19:28:24 +0000 | [diff] [blame] | 228 | if (!BBForwardRefs.empty()) { |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 229 | GenerateError("Undefined reference to label " + |
Reid Spencer | 93c4003 | 2007-03-19 18:40:50 +0000 | [diff] [blame] | 230 | BBForwardRefs.begin()->second->getName()); |
Reid Spencer | 5b7e753 | 2006-09-28 19:28:24 +0000 | [diff] [blame] | 231 | return; |
| 232 | } |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 233 | |
| 234 | // Resolve all forward references now. |
| 235 | ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues); |
| 236 | |
| 237 | Values.clear(); // Clear out function local definitions |
Reid Spencer | 93c4003 | 2007-03-19 18:40:50 +0000 | [diff] [blame] | 238 | BBForwardRefs.clear(); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 239 | CurrentFunction = 0; |
| 240 | isDeclare = false; |
Anton Korobeynikov | b74ed07 | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 241 | Linkage = GlobalValue::ExternalLinkage; |
Anton Korobeynikov | 7f70559 | 2007-01-12 19:20:47 +0000 | [diff] [blame] | 242 | Visibility = GlobalValue::DefaultVisibility; |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 243 | } |
| 244 | } CurFun; // Info for the current function... |
| 245 | |
| 246 | static bool inFunctionScope() { return CurFun.CurrentFunction != 0; } |
| 247 | |
| 248 | |
| 249 | //===----------------------------------------------------------------------===// |
| 250 | // Code to handle definitions of all the types |
| 251 | //===----------------------------------------------------------------------===// |
| 252 | |
Chris Lattner | 15bd095 | 2008-08-29 17:20:18 +0000 | [diff] [blame] | 253 | /// InsertValue - Insert a value into the value table. If it is named, this |
| 254 | /// returns -1, otherwise it returns the slot number for the value. |
| 255 | static int InsertValue(Value *V, ValueList &ValueTab = CurFun.Values) { |
Reid Spencer | 93c4003 | 2007-03-19 18:40:50 +0000 | [diff] [blame] | 256 | // Things that have names or are void typed don't get slot numbers |
| 257 | if (V->hasName() || (V->getType() == Type::VoidTy)) |
Chris Lattner | 15bd095 | 2008-08-29 17:20:18 +0000 | [diff] [blame] | 258 | return -1; |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 259 | |
Reid Spencer | 93c4003 | 2007-03-19 18:40:50 +0000 | [diff] [blame] | 260 | // In the case of function values, we have to allow for the forward reference |
| 261 | // of basic blocks, which are included in the numbering. Consequently, we keep |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 262 | // track of the next insertion location with NextValNum. When a BB gets |
Reid Spencer | 93c4003 | 2007-03-19 18:40:50 +0000 | [diff] [blame] | 263 | // inserted, it could change the size of the CurFun.Values vector. |
| 264 | if (&ValueTab == &CurFun.Values) { |
| 265 | if (ValueTab.size() <= CurFun.NextValNum) |
| 266 | ValueTab.resize(CurFun.NextValNum+1); |
| 267 | ValueTab[CurFun.NextValNum++] = V; |
Chris Lattner | 15bd095 | 2008-08-29 17:20:18 +0000 | [diff] [blame] | 268 | return CurFun.NextValNum-1; |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 269 | } |
Reid Spencer | 93c4003 | 2007-03-19 18:40:50 +0000 | [diff] [blame] | 270 | // For all other lists, its okay to just tack it on the back of the vector. |
| 271 | ValueTab.push_back(V); |
Chris Lattner | 15bd095 | 2008-08-29 17:20:18 +0000 | [diff] [blame] | 272 | return ValueTab.size()-1; |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) { |
| 276 | switch (D.Type) { |
Reid Spencer | 41dff5e | 2007-01-26 08:05:27 +0000 | [diff] [blame] | 277 | case ValID::LocalID: // Is it a numbered definition? |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 278 | // Module constants occupy the lowest numbered slots... |
Reid Spencer | 41dff5e | 2007-01-26 08:05:27 +0000 | [diff] [blame] | 279 | if (D.Num < CurModule.Types.size()) |
| 280 | return CurModule.Types[D.Num]; |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 281 | break; |
Reid Spencer | 41dff5e | 2007-01-26 08:05:27 +0000 | [diff] [blame] | 282 | case ValID::LocalName: // Is it a named definition? |
Reid Spencer | 0a8a16b | 2007-05-22 18:52:55 +0000 | [diff] [blame] | 283 | if (const Type *N = CurModule.CurrentModule->getTypeByName(D.getName())) { |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 284 | D.destroy(); // Free old strdup'd memory... |
| 285 | return N; |
| 286 | } |
| 287 | break; |
| 288 | default: |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 289 | GenerateError("Internal parser error: Invalid symbol type reference"); |
Reid Spencer | 5b7e753 | 2006-09-28 19:28:24 +0000 | [diff] [blame] | 290 | return 0; |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | // If we reached here, we referenced either a symbol that we don't know about |
| 294 | // or an id number that hasn't been read yet. We may be referencing something |
| 295 | // forward, so just create an entry to be resolved later and get to it... |
| 296 | // |
| 297 | if (DoNotImprovise) return 0; // Do we just want a null to be returned? |
| 298 | |
| 299 | |
| 300 | if (inFunctionScope()) { |
Reid Spencer | 41dff5e | 2007-01-26 08:05:27 +0000 | [diff] [blame] | 301 | if (D.Type == ValID::LocalName) { |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 302 | GenerateError("Reference to an undefined type: '" + D.getName() + "'"); |
Reid Spencer | 5b7e753 | 2006-09-28 19:28:24 +0000 | [diff] [blame] | 303 | return 0; |
| 304 | } else { |
Reid Spencer | 41dff5e | 2007-01-26 08:05:27 +0000 | [diff] [blame] | 305 | GenerateError("Reference to an undefined type: #" + utostr(D.Num)); |
Reid Spencer | 5b7e753 | 2006-09-28 19:28:24 +0000 | [diff] [blame] | 306 | return 0; |
| 307 | } |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Reid Spencer | 861d9d6 | 2006-11-28 07:29:44 +0000 | [diff] [blame] | 310 | std::map<ValID, PATypeHolder>::iterator I =CurModule.LateResolveTypes.find(D); |
Nuno Lopes | f05ff66 | 2008-10-15 11:20:21 +0000 | [diff] [blame] | 311 | if (I != CurModule.LateResolveTypes.end()) { |
| 312 | D.destroy(); |
Reid Spencer | 861d9d6 | 2006-11-28 07:29:44 +0000 | [diff] [blame] | 313 | return I->second; |
Nuno Lopes | f05ff66 | 2008-10-15 11:20:21 +0000 | [diff] [blame] | 314 | } |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 315 | |
Reid Spencer | 861d9d6 | 2006-11-28 07:29:44 +0000 | [diff] [blame] | 316 | Type *Typ = OpaqueType::get(); |
| 317 | CurModule.LateResolveTypes.insert(std::make_pair(D, Typ)); |
| 318 | return Typ; |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 319 | } |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 320 | |
Reid Spencer | 93c4003 | 2007-03-19 18:40:50 +0000 | [diff] [blame] | 321 | // getExistingVal - Look up the value specified by the provided type and |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 322 | // the provided ValID. If the value exists and has already been defined, return |
| 323 | // it. Otherwise return null. |
| 324 | // |
Reid Spencer | 93c4003 | 2007-03-19 18:40:50 +0000 | [diff] [blame] | 325 | static Value *getExistingVal(const Type *Ty, const ValID &D) { |
Reid Spencer | 5b7e753 | 2006-09-28 19:28:24 +0000 | [diff] [blame] | 326 | if (isa<FunctionType>(Ty)) { |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 327 | GenerateError("Functions are not values and " |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 328 | "must be referenced as pointers"); |
Reid Spencer | 5b7e753 | 2006-09-28 19:28:24 +0000 | [diff] [blame] | 329 | return 0; |
| 330 | } |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 331 | |
| 332 | switch (D.Type) { |
Reid Spencer | 41dff5e | 2007-01-26 08:05:27 +0000 | [diff] [blame] | 333 | case ValID::LocalID: { // Is it a numbered definition? |
Reid Spencer | 41dff5e | 2007-01-26 08:05:27 +0000 | [diff] [blame] | 334 | // Check that the number is within bounds. |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 335 | if (D.Num >= CurFun.Values.size()) |
Reid Spencer | 93c4003 | 2007-03-19 18:40:50 +0000 | [diff] [blame] | 336 | return 0; |
| 337 | Value *Result = CurFun.Values[D.Num]; |
| 338 | if (Ty != Result->getType()) { |
| 339 | GenerateError("Numbered value (%" + utostr(D.Num) + ") of type '" + |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 340 | Result->getType()->getDescription() + "' does not match " |
Reid Spencer | 93c4003 | 2007-03-19 18:40:50 +0000 | [diff] [blame] | 341 | "expected type, '" + Ty->getDescription() + "'"); |
| 342 | return 0; |
| 343 | } |
| 344 | return Result; |
Reid Spencer | 41dff5e | 2007-01-26 08:05:27 +0000 | [diff] [blame] | 345 | } |
| 346 | case ValID::GlobalID: { // Is it a numbered definition? |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 347 | if (D.Num >= CurModule.Values.size()) |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 348 | return 0; |
Reid Spencer | 93c4003 | 2007-03-19 18:40:50 +0000 | [diff] [blame] | 349 | Value *Result = CurModule.Values[D.Num]; |
| 350 | if (Ty != Result->getType()) { |
| 351 | GenerateError("Numbered value (@" + utostr(D.Num) + ") of type '" + |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 352 | Result->getType()->getDescription() + "' does not match " |
Reid Spencer | 93c4003 | 2007-03-19 18:40:50 +0000 | [diff] [blame] | 353 | "expected type, '" + Ty->getDescription() + "'"); |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 354 | return 0; |
Reid Spencer | 93c4003 | 2007-03-19 18:40:50 +0000 | [diff] [blame] | 355 | } |
| 356 | return Result; |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 357 | } |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 358 | |
Reid Spencer | 41dff5e | 2007-01-26 08:05:27 +0000 | [diff] [blame] | 359 | case ValID::LocalName: { // Is it a named definition? |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 360 | if (!inFunctionScope()) |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 361 | return 0; |
| 362 | ValueSymbolTable &SymTab = CurFun.CurrentFunction->getValueSymbolTable(); |
Reid Spencer | 0a8a16b | 2007-05-22 18:52:55 +0000 | [diff] [blame] | 363 | Value *N = SymTab.lookup(D.getName()); |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 364 | if (N == 0) |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 365 | return 0; |
| 366 | if (N->getType() != Ty) |
| 367 | return 0; |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 368 | |
Reid Spencer | 41dff5e | 2007-01-26 08:05:27 +0000 | [diff] [blame] | 369 | D.destroy(); // Free old strdup'd memory... |
| 370 | return N; |
| 371 | } |
| 372 | case ValID::GlobalName: { // Is it a named definition? |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 373 | ValueSymbolTable &SymTab = CurModule.CurrentModule->getValueSymbolTable(); |
Reid Spencer | 0a8a16b | 2007-05-22 18:52:55 +0000 | [diff] [blame] | 374 | Value *N = SymTab.lookup(D.getName()); |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 375 | if (N == 0) |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 376 | return 0; |
| 377 | if (N->getType() != Ty) |
| 378 | return 0; |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 379 | |
| 380 | D.destroy(); // Free old strdup'd memory... |
| 381 | return N; |
| 382 | } |
| 383 | |
| 384 | // Check to make sure that "Ty" is an integral type, and that our |
| 385 | // value will fit into the specified type... |
| 386 | case ValID::ConstSIntVal: // Is it a constant pool reference?? |
Chris Lattner | 3890561 | 2008-02-19 04:36:25 +0000 | [diff] [blame] | 387 | if (!isa<IntegerType>(Ty) || |
| 388 | !ConstantInt::isValueValidForType(Ty, D.ConstPool64)) { |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 389 | GenerateError("Signed integral constant '" + |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 390 | itostr(D.ConstPool64) + "' is invalid for type '" + |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 391 | Ty->getDescription() + "'"); |
Reid Spencer | 5b7e753 | 2006-09-28 19:28:24 +0000 | [diff] [blame] | 392 | return 0; |
| 393 | } |
Reid Spencer | 49d273e | 2007-03-19 20:40:51 +0000 | [diff] [blame] | 394 | return ConstantInt::get(Ty, D.ConstPool64, true); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 395 | |
| 396 | case ValID::ConstUIntVal: // Is it an unsigned const pool reference? |
Chris Lattner | 3890561 | 2008-02-19 04:36:25 +0000 | [diff] [blame] | 397 | if (isa<IntegerType>(Ty) && |
| 398 | ConstantInt::isValueValidForType(Ty, D.UConstPool64)) |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 399 | return ConstantInt::get(Ty, D.UConstPool64); |
Chris Lattner | 3890561 | 2008-02-19 04:36:25 +0000 | [diff] [blame] | 400 | |
| 401 | if (!isa<IntegerType>(Ty) || |
| 402 | !ConstantInt::isValueValidForType(Ty, D.ConstPool64)) { |
| 403 | GenerateError("Integral constant '" + utostr(D.UConstPool64) + |
| 404 | "' is invalid or out of range for type '" + |
| 405 | Ty->getDescription() + "'"); |
| 406 | return 0; |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 407 | } |
Chris Lattner | 3890561 | 2008-02-19 04:36:25 +0000 | [diff] [blame] | 408 | // This is really a signed reference. Transmogrify. |
| 409 | return ConstantInt::get(Ty, D.ConstPool64, true); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 410 | |
Chris Lattner | 1913b94 | 2008-07-11 00:30:39 +0000 | [diff] [blame] | 411 | case ValID::ConstAPInt: // Is it an unsigned const pool reference? |
| 412 | if (!isa<IntegerType>(Ty)) { |
| 413 | GenerateError("Integral constant '" + D.getName() + |
| 414 | "' is invalid or out of range for type '" + |
| 415 | Ty->getDescription() + "'"); |
| 416 | return 0; |
| 417 | } |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 418 | |
Chris Lattner | 1913b94 | 2008-07-11 00:30:39 +0000 | [diff] [blame] | 419 | { |
| 420 | APSInt Tmp = *D.ConstPoolInt; |
Nuno Lopes | 1983033 | 2008-11-04 14:28:33 +0000 | [diff] [blame] | 421 | D.destroy(); |
Chris Lattner | 1913b94 | 2008-07-11 00:30:39 +0000 | [diff] [blame] | 422 | Tmp.extOrTrunc(Ty->getPrimitiveSizeInBits()); |
| 423 | return ConstantInt::get(Tmp); |
| 424 | } |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 425 | |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 426 | case ValID::ConstFPVal: // Is it a floating point const pool reference? |
Chris Lattner | 3890561 | 2008-02-19 04:36:25 +0000 | [diff] [blame] | 427 | if (!Ty->isFloatingPoint() || |
| 428 | !ConstantFP::isValueValidForType(Ty, *D.ConstPoolFP)) { |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 429 | GenerateError("FP constant invalid for type"); |
Reid Spencer | 5b7e753 | 2006-09-28 19:28:24 +0000 | [diff] [blame] | 430 | return 0; |
| 431 | } |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 432 | // Lexer has no type info, so builds all float and double FP constants |
Dale Johannesen | c72cd7e | 2007-09-11 18:33:39 +0000 | [diff] [blame] | 433 | // as double. Fix this here. Long double does not need this. |
| 434 | if (&D.ConstPoolFP->getSemantics() == &APFloat::IEEEdouble && |
Dale Johannesen | 236bbd4 | 2008-10-09 23:01:34 +0000 | [diff] [blame] | 435 | Ty==Type::FloatTy) { |
| 436 | bool ignored; |
| 437 | D.ConstPoolFP->convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, |
| 438 | &ignored); |
| 439 | } |
Nuno Lopes | aa386d0 | 2008-11-04 14:43:20 +0000 | [diff] [blame] | 440 | { |
| 441 | ConstantFP *tmp = ConstantFP::get(*D.ConstPoolFP); |
| 442 | D.destroy(); |
| 443 | return tmp; |
| 444 | } |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 445 | |
| 446 | case ValID::ConstNullVal: // Is it a null value? |
Reid Spencer | 5b7e753 | 2006-09-28 19:28:24 +0000 | [diff] [blame] | 447 | if (!isa<PointerType>(Ty)) { |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 448 | GenerateError("Cannot create a a non pointer null"); |
Reid Spencer | 5b7e753 | 2006-09-28 19:28:24 +0000 | [diff] [blame] | 449 | return 0; |
| 450 | } |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 451 | return ConstantPointerNull::get(cast<PointerType>(Ty)); |
| 452 | |
| 453 | case ValID::ConstUndefVal: // Is it an undef value? |
| 454 | return UndefValue::get(Ty); |
| 455 | |
| 456 | case ValID::ConstZeroVal: // Is it a zero value? |
| 457 | return Constant::getNullValue(Ty); |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 458 | |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 459 | case ValID::ConstantVal: // Fully resolved constant? |
Reid Spencer | 5b7e753 | 2006-09-28 19:28:24 +0000 | [diff] [blame] | 460 | if (D.ConstantValue->getType() != Ty) { |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 461 | GenerateError("Constant expression type different from required type"); |
Reid Spencer | 5b7e753 | 2006-09-28 19:28:24 +0000 | [diff] [blame] | 462 | return 0; |
| 463 | } |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 464 | return D.ConstantValue; |
| 465 | |
| 466 | case ValID::InlineAsmVal: { // Inline asm expression |
| 467 | const PointerType *PTy = dyn_cast<PointerType>(Ty); |
| 468 | const FunctionType *FTy = |
| 469 | PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0; |
Reid Spencer | 5b7e753 | 2006-09-28 19:28:24 +0000 | [diff] [blame] | 470 | if (!FTy || !InlineAsm::Verify(FTy, D.IAD->Constraints)) { |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 471 | GenerateError("Invalid type for asm constraint string"); |
Reid Spencer | 5b7e753 | 2006-09-28 19:28:24 +0000 | [diff] [blame] | 472 | return 0; |
| 473 | } |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 474 | InlineAsm *IA = InlineAsm::get(FTy, D.IAD->AsmString, D.IAD->Constraints, |
| 475 | D.IAD->HasSideEffects); |
| 476 | D.destroy(); // Free InlineAsmDescriptor. |
| 477 | return IA; |
| 478 | } |
| 479 | default: |
Reid Spencer | a9720f5 | 2007-02-05 17:04:00 +0000 | [diff] [blame] | 480 | assert(0 && "Unhandled case!"); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 481 | return 0; |
| 482 | } // End of switch |
| 483 | |
Reid Spencer | a9720f5 | 2007-02-05 17:04:00 +0000 | [diff] [blame] | 484 | assert(0 && "Unhandled case!"); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 485 | return 0; |
| 486 | } |
| 487 | |
Reid Spencer | 93c4003 | 2007-03-19 18:40:50 +0000 | [diff] [blame] | 488 | // getVal - This function is identical to getExistingVal, except that if a |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 489 | // value is not already defined, it "improvises" by creating a placeholder var |
| 490 | // that looks and acts just like the requested variable. When the value is |
| 491 | // defined later, all uses of the placeholder variable are replaced with the |
| 492 | // real thing. |
| 493 | // |
| 494 | static Value *getVal(const Type *Ty, const ValID &ID) { |
Reid Spencer | 5b7e753 | 2006-09-28 19:28:24 +0000 | [diff] [blame] | 495 | if (Ty == Type::LabelTy) { |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 496 | GenerateError("Cannot use a basic block here"); |
Reid Spencer | 5b7e753 | 2006-09-28 19:28:24 +0000 | [diff] [blame] | 497 | return 0; |
| 498 | } |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 499 | |
| 500 | // See if the value has already been defined. |
Reid Spencer | 93c4003 | 2007-03-19 18:40:50 +0000 | [diff] [blame] | 501 | Value *V = getExistingVal(Ty, ID); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 502 | if (V) return V; |
Reid Spencer | 5b7e753 | 2006-09-28 19:28:24 +0000 | [diff] [blame] | 503 | if (TriggerError) return 0; |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 504 | |
Reid Spencer | 5b7e753 | 2006-09-28 19:28:24 +0000 | [diff] [blame] | 505 | if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty)) { |
Dan Gohman | e4977cf | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 506 | GenerateError("Invalid use of a non-first-class type"); |
Reid Spencer | 5b7e753 | 2006-09-28 19:28:24 +0000 | [diff] [blame] | 507 | return 0; |
| 508 | } |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 509 | |
| 510 | // If we reached here, we referenced either a symbol that we don't know about |
| 511 | // or an id number that hasn't been read yet. We may be referencing something |
| 512 | // forward, so just create an entry to be resolved later and get to it... |
| 513 | // |
Anton Korobeynikov | 38e0980 | 2007-04-28 13:48:45 +0000 | [diff] [blame] | 514 | switch (ID.Type) { |
| 515 | case ValID::GlobalName: |
Reid Spencer | 9c9b63a | 2007-04-28 16:07:31 +0000 | [diff] [blame] | 516 | case ValID::GlobalID: { |
Anton Korobeynikov | 38e0980 | 2007-04-28 13:48:45 +0000 | [diff] [blame] | 517 | const PointerType *PTy = dyn_cast<PointerType>(Ty); |
| 518 | if (!PTy) { |
| 519 | GenerateError("Invalid type for reference to global" ); |
| 520 | return 0; |
| 521 | } |
| 522 | const Type* ElTy = PTy->getElementType(); |
| 523 | if (const FunctionType *FTy = dyn_cast<FunctionType>(ElTy)) |
Gabor Greif | e64d248 | 2008-04-06 23:07:54 +0000 | [diff] [blame] | 524 | V = Function::Create(FTy, GlobalValue::ExternalLinkage); |
Anton Korobeynikov | 38e0980 | 2007-04-28 13:48:45 +0000 | [diff] [blame] | 525 | else |
Christopher Lamb | a8ed9bf | 2007-12-11 09:02:08 +0000 | [diff] [blame] | 526 | V = new GlobalVariable(ElTy, false, GlobalValue::ExternalLinkage, 0, "", |
| 527 | (Module*)0, false, PTy->getAddressSpace()); |
Anton Korobeynikov | 38e0980 | 2007-04-28 13:48:45 +0000 | [diff] [blame] | 528 | break; |
Reid Spencer | 9c9b63a | 2007-04-28 16:07:31 +0000 | [diff] [blame] | 529 | } |
Anton Korobeynikov | 38e0980 | 2007-04-28 13:48:45 +0000 | [diff] [blame] | 530 | default: |
| 531 | V = new Argument(Ty); |
| 532 | } |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 533 | |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 534 | // Remember where this forward reference came from. FIXME, shouldn't we try |
| 535 | // to recycle these things?? |
| 536 | CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID, |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 537 | LLLgetLineNo()))); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 538 | |
| 539 | if (inFunctionScope()) |
| 540 | InsertValue(V, CurFun.LateResolveValues); |
| 541 | else |
| 542 | InsertValue(V, CurModule.LateResolveValues); |
| 543 | return V; |
| 544 | } |
| 545 | |
Reid Spencer | 93c4003 | 2007-03-19 18:40:50 +0000 | [diff] [blame] | 546 | /// defineBBVal - This is a definition of a new basic block with the specified |
| 547 | /// identifier which must be the same as CurFun.NextValNum, if its numeric. |
Nick Lewycky | 280a6e6 | 2008-04-25 16:53:59 +0000 | [diff] [blame] | 548 | static BasicBlock *defineBBVal(const ValID &ID) { |
Reid Spencer | a9720f5 | 2007-02-05 17:04:00 +0000 | [diff] [blame] | 549 | assert(inFunctionScope() && "Can't get basic block at global scope!"); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 550 | |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 551 | BasicBlock *BB = 0; |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 552 | |
Reid Spencer | 93c4003 | 2007-03-19 18:40:50 +0000 | [diff] [blame] | 553 | // First, see if this was forward referenced |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 554 | |
Reid Spencer | 93c4003 | 2007-03-19 18:40:50 +0000 | [diff] [blame] | 555 | std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID); |
| 556 | if (BBI != CurFun.BBForwardRefs.end()) { |
| 557 | BB = BBI->second; |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 558 | // The forward declaration could have been inserted anywhere in the |
| 559 | // function: insert it into the correct place now. |
| 560 | CurFun.CurrentFunction->getBasicBlockList().remove(BB); |
| 561 | CurFun.CurrentFunction->getBasicBlockList().push_back(BB); |
Reid Spencer | 93c4003 | 2007-03-19 18:40:50 +0000 | [diff] [blame] | 562 | |
Reid Spencer | 66728ef | 2007-03-20 01:13:36 +0000 | [diff] [blame] | 563 | // We're about to erase the entry, save the key so we can clean it up. |
| 564 | ValID Tmp = BBI->first; |
| 565 | |
Reid Spencer | 93c4003 | 2007-03-19 18:40:50 +0000 | [diff] [blame] | 566 | // Erase the forward ref from the map as its no longer "forward" |
| 567 | CurFun.BBForwardRefs.erase(ID); |
| 568 | |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 569 | // The key has been removed from the map but so we don't want to leave |
Reid Spencer | 66728ef | 2007-03-20 01:13:36 +0000 | [diff] [blame] | 570 | // strdup'd memory around so destroy it too. |
| 571 | Tmp.destroy(); |
| 572 | |
Reid Spencer | 93c4003 | 2007-03-19 18:40:50 +0000 | [diff] [blame] | 573 | // If its a numbered definition, bump the number and set the BB value. |
| 574 | if (ID.Type == ValID::LocalID) { |
| 575 | assert(ID.Num == CurFun.NextValNum && "Invalid new block number"); |
| 576 | InsertValue(BB); |
| 577 | } |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 578 | } else { |
| 579 | // We haven't seen this BB before and its first mention is a definition. |
Devang Patel | 6790943 | 2008-03-03 18:58:47 +0000 | [diff] [blame] | 580 | // Just create it and return it. |
| 581 | std::string Name (ID.Type == ValID::LocalName ? ID.getName() : ""); |
Gabor Greif | e64d248 | 2008-04-06 23:07:54 +0000 | [diff] [blame] | 582 | BB = BasicBlock::Create(Name, CurFun.CurrentFunction); |
Devang Patel | 6790943 | 2008-03-03 18:58:47 +0000 | [diff] [blame] | 583 | if (ID.Type == ValID::LocalID) { |
| 584 | assert(ID.Num == CurFun.NextValNum && "Invalid new block number"); |
| 585 | InsertValue(BB); |
| 586 | } |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 587 | } |
Reid Spencer | 93c4003 | 2007-03-19 18:40:50 +0000 | [diff] [blame] | 588 | |
Devang Patel | 6790943 | 2008-03-03 18:58:47 +0000 | [diff] [blame] | 589 | ID.destroy(); |
Reid Spencer | 93c4003 | 2007-03-19 18:40:50 +0000 | [diff] [blame] | 590 | return BB; |
| 591 | } |
| 592 | |
| 593 | /// getBBVal - get an existing BB value or create a forward reference for it. |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 594 | /// |
Reid Spencer | 93c4003 | 2007-03-19 18:40:50 +0000 | [diff] [blame] | 595 | static BasicBlock *getBBVal(const ValID &ID) { |
| 596 | assert(inFunctionScope() && "Can't get basic block at global scope!"); |
| 597 | |
| 598 | BasicBlock *BB = 0; |
| 599 | |
| 600 | std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID); |
| 601 | if (BBI != CurFun.BBForwardRefs.end()) { |
| 602 | BB = BBI->second; |
| 603 | } if (ID.Type == ValID::LocalName) { |
Reid Spencer | 0a8a16b | 2007-05-22 18:52:55 +0000 | [diff] [blame] | 604 | std::string Name = ID.getName(); |
Reid Spencer | 93c4003 | 2007-03-19 18:40:50 +0000 | [diff] [blame] | 605 | Value *N = CurFun.CurrentFunction->getValueSymbolTable().lookup(Name); |
Anton Korobeynikov | 4aefd6b | 2008-02-20 12:07:57 +0000 | [diff] [blame] | 606 | if (N) { |
Reid Spencer | 93c4003 | 2007-03-19 18:40:50 +0000 | [diff] [blame] | 607 | if (N->getType()->getTypeID() == Type::LabelTyID) |
| 608 | BB = cast<BasicBlock>(N); |
| 609 | else |
| 610 | GenerateError("Reference to label '" + Name + "' is actually of type '"+ |
| 611 | N->getType()->getDescription() + "'"); |
Anton Korobeynikov | 4aefd6b | 2008-02-20 12:07:57 +0000 | [diff] [blame] | 612 | } |
Reid Spencer | 93c4003 | 2007-03-19 18:40:50 +0000 | [diff] [blame] | 613 | } else if (ID.Type == ValID::LocalID) { |
| 614 | if (ID.Num < CurFun.NextValNum && ID.Num < CurFun.Values.size()) { |
| 615 | if (CurFun.Values[ID.Num]->getType()->getTypeID() == Type::LabelTyID) |
| 616 | BB = cast<BasicBlock>(CurFun.Values[ID.Num]); |
| 617 | else |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 618 | GenerateError("Reference to label '%" + utostr(ID.Num) + |
| 619 | "' is actually of type '"+ |
Reid Spencer | 93c4003 | 2007-03-19 18:40:50 +0000 | [diff] [blame] | 620 | CurFun.Values[ID.Num]->getType()->getDescription() + "'"); |
| 621 | } |
| 622 | } else { |
| 623 | GenerateError("Illegal label reference " + ID.getName()); |
| 624 | return 0; |
| 625 | } |
| 626 | |
| 627 | // If its already been defined, return it now. |
| 628 | if (BB) { |
| 629 | ID.destroy(); // Free strdup'd memory. |
| 630 | return BB; |
| 631 | } |
| 632 | |
| 633 | // Otherwise, this block has not been seen before, create it. |
| 634 | std::string Name; |
| 635 | if (ID.Type == ValID::LocalName) |
Reid Spencer | 0a8a16b | 2007-05-22 18:52:55 +0000 | [diff] [blame] | 636 | Name = ID.getName(); |
Gabor Greif | e64d248 | 2008-04-06 23:07:54 +0000 | [diff] [blame] | 637 | BB = BasicBlock::Create(Name, CurFun.CurrentFunction); |
Reid Spencer | 93c4003 | 2007-03-19 18:40:50 +0000 | [diff] [blame] | 638 | |
| 639 | // Insert it in the forward refs map. |
| 640 | CurFun.BBForwardRefs[ID] = BB; |
| 641 | |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 642 | return BB; |
| 643 | } |
| 644 | |
| 645 | |
| 646 | //===----------------------------------------------------------------------===// |
| 647 | // Code to handle forward references in instructions |
| 648 | //===----------------------------------------------------------------------===// |
| 649 | // |
| 650 | // This code handles the late binding needed with statements that reference |
| 651 | // values not defined yet... for example, a forward branch, or the PHI node for |
| 652 | // a loop body. |
| 653 | // |
| 654 | // This keeps a table (CurFun.LateResolveValues) of all such forward references |
| 655 | // and back patchs after we are done. |
| 656 | // |
| 657 | |
| 658 | // ResolveDefinitions - If we could not resolve some defs at parsing |
| 659 | // time (forward branches, phi functions for loops, etc...) resolve the |
| 660 | // defs now... |
| 661 | // |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 662 | static void |
Reid Spencer | 93c4003 | 2007-03-19 18:40:50 +0000 | [diff] [blame] | 663 | ResolveDefinitions(ValueList &LateResolvers, ValueList *FutureLateResolvers) { |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 664 | // Loop over LateResolveDefs fixing up stuff that couldn't be resolved |
Reid Spencer | 93c4003 | 2007-03-19 18:40:50 +0000 | [diff] [blame] | 665 | while (!LateResolvers.empty()) { |
| 666 | Value *V = LateResolvers.back(); |
| 667 | LateResolvers.pop_back(); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 668 | |
Reid Spencer | 93c4003 | 2007-03-19 18:40:50 +0000 | [diff] [blame] | 669 | std::map<Value*, std::pair<ValID, int> >::iterator PHI = |
| 670 | CurModule.PlaceHolderInfo.find(V); |
| 671 | assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!"); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 672 | |
Reid Spencer | 93c4003 | 2007-03-19 18:40:50 +0000 | [diff] [blame] | 673 | ValID &DID = PHI->second.first; |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 674 | |
Reid Spencer | 93c4003 | 2007-03-19 18:40:50 +0000 | [diff] [blame] | 675 | Value *TheRealValue = getExistingVal(V->getType(), DID); |
| 676 | if (TriggerError) |
| 677 | return; |
| 678 | if (TheRealValue) { |
| 679 | V->replaceAllUsesWith(TheRealValue); |
| 680 | delete V; |
| 681 | CurModule.PlaceHolderInfo.erase(PHI); |
| 682 | } else if (FutureLateResolvers) { |
| 683 | // Functions have their unresolved items forwarded to the module late |
| 684 | // resolver table |
| 685 | InsertValue(V, *FutureLateResolvers); |
| 686 | } else { |
| 687 | if (DID.Type == ValID::LocalName || DID.Type == ValID::GlobalName) { |
| 688 | GenerateError("Reference to an invalid definition: '" +DID.getName()+ |
| 689 | "' of type '" + V->getType()->getDescription() + "'", |
| 690 | PHI->second.second); |
Reid Spencer | 5b7e753 | 2006-09-28 19:28:24 +0000 | [diff] [blame] | 691 | return; |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 692 | } else { |
Reid Spencer | 93c4003 | 2007-03-19 18:40:50 +0000 | [diff] [blame] | 693 | GenerateError("Reference to an invalid definition: #" + |
| 694 | itostr(DID.Num) + " of type '" + |
| 695 | V->getType()->getDescription() + "'", |
| 696 | PHI->second.second); |
| 697 | return; |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 698 | } |
| 699 | } |
| 700 | } |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 701 | LateResolvers.clear(); |
| 702 | } |
| 703 | |
| 704 | // ResolveTypeTo - A brand new type was just declared. This means that (if |
| 705 | // name is not null) things referencing Name can be resolved. Otherwise, things |
| 706 | // refering to the number can be resolved. Do this now. |
| 707 | // |
Reid Spencer | 0a8a16b | 2007-05-22 18:52:55 +0000 | [diff] [blame] | 708 | static void ResolveTypeTo(std::string *Name, const Type *ToTy) { |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 709 | ValID D; |
Reid Spencer | 0a8a16b | 2007-05-22 18:52:55 +0000 | [diff] [blame] | 710 | if (Name) |
| 711 | D = ValID::createLocalName(*Name); |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 712 | else |
Reid Spencer | 0a8a16b | 2007-05-22 18:52:55 +0000 | [diff] [blame] | 713 | D = ValID::createLocalID(CurModule.Types.size()); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 714 | |
Reid Spencer | 861d9d6 | 2006-11-28 07:29:44 +0000 | [diff] [blame] | 715 | std::map<ValID, PATypeHolder>::iterator I = |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 716 | CurModule.LateResolveTypes.find(D); |
| 717 | if (I != CurModule.LateResolveTypes.end()) { |
Reid Spencer | 861d9d6 | 2006-11-28 07:29:44 +0000 | [diff] [blame] | 718 | ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy); |
Nuno Lopes | 6ec8a25 | 2008-10-15 11:11:12 +0000 | [diff] [blame] | 719 | I->first.destroy(); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 720 | CurModule.LateResolveTypes.erase(I); |
| 721 | } |
Nuno Lopes | 191dfb9 | 2008-10-03 15:52:39 +0000 | [diff] [blame] | 722 | D.destroy(); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | // setValueName - Set the specified value to the name given. The name may be |
| 726 | // null potentially, in which case this is a noop. The string passed in is |
| 727 | // assumed to be a malloc'd string buffer, and is free'd by this function. |
| 728 | // |
Reid Spencer | 0a8a16b | 2007-05-22 18:52:55 +0000 | [diff] [blame] | 729 | static void setValueName(Value *V, std::string *NameStr) { |
Reid Spencer | 41dff5e | 2007-01-26 08:05:27 +0000 | [diff] [blame] | 730 | if (!NameStr) return; |
Reid Spencer | 0a8a16b | 2007-05-22 18:52:55 +0000 | [diff] [blame] | 731 | std::string Name(*NameStr); // Copy string |
| 732 | delete NameStr; // Free old string |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 733 | |
Reid Spencer | 41dff5e | 2007-01-26 08:05:27 +0000 | [diff] [blame] | 734 | if (V->getType() == Type::VoidTy) { |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 735 | GenerateError("Can't assign name '" + Name+"' to value with void type"); |
Reid Spencer | 41dff5e | 2007-01-26 08:05:27 +0000 | [diff] [blame] | 736 | return; |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 737 | } |
Reid Spencer | 41dff5e | 2007-01-26 08:05:27 +0000 | [diff] [blame] | 738 | |
Reid Spencer | a9720f5 | 2007-02-05 17:04:00 +0000 | [diff] [blame] | 739 | assert(inFunctionScope() && "Must be in function scope!"); |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 740 | ValueSymbolTable &ST = CurFun.CurrentFunction->getValueSymbolTable(); |
| 741 | if (ST.lookup(Name)) { |
Reid Spencer | 41dff5e | 2007-01-26 08:05:27 +0000 | [diff] [blame] | 742 | GenerateError("Redefinition of value '" + Name + "' of type '" + |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 743 | V->getType()->getDescription() + "'"); |
Reid Spencer | 41dff5e | 2007-01-26 08:05:27 +0000 | [diff] [blame] | 744 | return; |
| 745 | } |
| 746 | |
| 747 | // Set the name. |
| 748 | V->setName(Name); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | /// ParseGlobalVariable - Handle parsing of a global. If Initializer is null, |
| 752 | /// this is a declaration, otherwise it is a definition. |
| 753 | static GlobalVariable * |
Reid Spencer | 0a8a16b | 2007-05-22 18:52:55 +0000 | [diff] [blame] | 754 | ParseGlobalVariable(std::string *NameStr, |
Anton Korobeynikov | 7f70559 | 2007-01-12 19:20:47 +0000 | [diff] [blame] | 755 | GlobalValue::LinkageTypes Linkage, |
| 756 | GlobalValue::VisibilityTypes Visibility, |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 757 | bool isConstantGlobal, const Type *Ty, |
Christopher Lamb | a8ed9bf | 2007-12-11 09:02:08 +0000 | [diff] [blame] | 758 | Constant *Initializer, bool IsThreadLocal, |
| 759 | unsigned AddressSpace = 0) { |
Reid Spencer | 5b7e753 | 2006-09-28 19:28:24 +0000 | [diff] [blame] | 760 | if (isa<FunctionType>(Ty)) { |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 761 | GenerateError("Cannot declare global vars of function type"); |
Reid Spencer | 5b7e753 | 2006-09-28 19:28:24 +0000 | [diff] [blame] | 762 | return 0; |
| 763 | } |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 764 | if (Ty == Type::LabelTy) { |
| 765 | GenerateError("Cannot declare global vars of label type"); |
| 766 | return 0; |
| 767 | } |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 768 | |
Christopher Lamb | a8ed9bf | 2007-12-11 09:02:08 +0000 | [diff] [blame] | 769 | const PointerType *PTy = PointerType::get(Ty, AddressSpace); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 770 | |
| 771 | std::string Name; |
| 772 | if (NameStr) { |
Reid Spencer | 0a8a16b | 2007-05-22 18:52:55 +0000 | [diff] [blame] | 773 | Name = *NameStr; // Copy string |
| 774 | delete NameStr; // Free old string |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | // See if this global value was forward referenced. If so, recycle the |
| 778 | // object. |
| 779 | ValID ID; |
| 780 | if (!Name.empty()) { |
Reid Spencer | 0a8a16b | 2007-05-22 18:52:55 +0000 | [diff] [blame] | 781 | ID = ValID::createGlobalName(Name); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 782 | } else { |
Reid Spencer | 93c4003 | 2007-03-19 18:40:50 +0000 | [diff] [blame] | 783 | ID = ValID::createGlobalID(CurModule.Values.size()); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) { |
| 787 | // Move the global to the end of the list, from whereever it was |
| 788 | // previously inserted. |
| 789 | GlobalVariable *GV = cast<GlobalVariable>(FWGV); |
| 790 | CurModule.CurrentModule->getGlobalList().remove(GV); |
| 791 | CurModule.CurrentModule->getGlobalList().push_back(GV); |
| 792 | GV->setInitializer(Initializer); |
| 793 | GV->setLinkage(Linkage); |
Anton Korobeynikov | 7f70559 | 2007-01-12 19:20:47 +0000 | [diff] [blame] | 794 | GV->setVisibility(Visibility); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 795 | GV->setConstant(isConstantGlobal); |
Lauro Ramos Venancio | c763552 | 2007-04-12 18:32:50 +0000 | [diff] [blame] | 796 | GV->setThreadLocal(IsThreadLocal); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 797 | InsertValue(GV, CurModule.Values); |
Nuno Lopes | 191dfb9 | 2008-10-03 15:52:39 +0000 | [diff] [blame] | 798 | ID.destroy(); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 799 | return GV; |
| 800 | } |
| 801 | |
Nuno Lopes | 191dfb9 | 2008-10-03 15:52:39 +0000 | [diff] [blame] | 802 | ID.destroy(); |
| 803 | |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 804 | // If this global has a name |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 805 | if (!Name.empty()) { |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 806 | // if the global we're parsing has an initializer (is a definition) and |
| 807 | // has external linkage. |
| 808 | if (Initializer && Linkage != GlobalValue::InternalLinkage) |
| 809 | // If there is already a global with external linkage with this name |
| 810 | if (CurModule.CurrentModule->getGlobalVariable(Name, false)) { |
| 811 | // If we allow this GVar to get created, it will be renamed in the |
| 812 | // symbol table because it conflicts with an existing GVar. We can't |
| 813 | // allow redefinition of GVars whose linking indicates that their name |
| 814 | // must stay the same. Issue the error. |
| 815 | GenerateError("Redefinition of global variable named '" + Name + |
| 816 | "' of type '" + Ty->getDescription() + "'"); |
| 817 | return 0; |
| 818 | } |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | // Otherwise there is no existing GV to use, create one now. |
| 822 | GlobalVariable *GV = |
| 823 | new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name, |
Christopher Lamb | a8ed9bf | 2007-12-11 09:02:08 +0000 | [diff] [blame] | 824 | CurModule.CurrentModule, IsThreadLocal, AddressSpace); |
Anton Korobeynikov | 7f70559 | 2007-01-12 19:20:47 +0000 | [diff] [blame] | 825 | GV->setVisibility(Visibility); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 826 | InsertValue(GV, CurModule.Values); |
| 827 | return GV; |
| 828 | } |
| 829 | |
| 830 | // setTypeName - Set the specified type to the name given. The name may be |
| 831 | // null potentially, in which case this is a noop. The string passed in is |
| 832 | // assumed to be a malloc'd string buffer, and is freed by this function. |
| 833 | // |
| 834 | // This function returns true if the type has already been defined, but is |
| 835 | // allowed to be redefined in the specified context. If the name is a new name |
| 836 | // for the type plane, it is inserted and false is returned. |
Reid Spencer | 0a8a16b | 2007-05-22 18:52:55 +0000 | [diff] [blame] | 837 | static bool setTypeName(const Type *T, std::string *NameStr) { |
Reid Spencer | a9720f5 | 2007-02-05 17:04:00 +0000 | [diff] [blame] | 838 | assert(!inFunctionScope() && "Can't give types function-local names!"); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 839 | if (NameStr == 0) return false; |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 840 | |
Reid Spencer | 0a8a16b | 2007-05-22 18:52:55 +0000 | [diff] [blame] | 841 | std::string Name(*NameStr); // Copy string |
| 842 | delete NameStr; // Free old string |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 843 | |
| 844 | // We don't allow assigning names to void type |
Reid Spencer | 5b7e753 | 2006-09-28 19:28:24 +0000 | [diff] [blame] | 845 | if (T == Type::VoidTy) { |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 846 | GenerateError("Can't assign name '" + Name + "' to the void type"); |
Reid Spencer | 5b7e753 | 2006-09-28 19:28:24 +0000 | [diff] [blame] | 847 | return false; |
| 848 | } |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 849 | |
| 850 | // Set the type name, checking for conflicts as we do so. |
| 851 | bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T); |
| 852 | |
| 853 | if (AlreadyExists) { // Inserting a name that is already defined??? |
| 854 | const Type *Existing = CurModule.CurrentModule->getTypeByName(Name); |
Reid Spencer | a9720f5 | 2007-02-05 17:04:00 +0000 | [diff] [blame] | 855 | assert(Existing && "Conflict but no matching type?!"); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 856 | |
| 857 | // There is only one case where this is allowed: when we are refining an |
| 858 | // opaque type. In this case, Existing will be an opaque type. |
| 859 | if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) { |
| 860 | // We ARE replacing an opaque type! |
| 861 | const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T); |
| 862 | return true; |
| 863 | } |
| 864 | |
| 865 | // Otherwise, this is an attempt to redefine a type. That's okay if |
| 866 | // the redefinition is identical to the original. This will be so if |
| 867 | // Existing and T point to the same Type object. In this one case we |
| 868 | // allow the equivalent redefinition. |
| 869 | if (Existing == T) return true; // Yes, it's equal. |
| 870 | |
| 871 | // Any other kind of (non-equivalent) redefinition is an error. |
Reid Spencer | 63c3445 | 2007-01-05 21:51:07 +0000 | [diff] [blame] | 872 | GenerateError("Redefinition of type named '" + Name + "' of type '" + |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 873 | T->getDescription() + "'"); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | return false; |
| 877 | } |
| 878 | |
| 879 | //===----------------------------------------------------------------------===// |
| 880 | // Code for handling upreferences in type names... |
| 881 | // |
| 882 | |
| 883 | // TypeContains - Returns true if Ty directly contains E in it. |
| 884 | // |
| 885 | static bool TypeContains(const Type *Ty, const Type *E) { |
| 886 | return std::find(Ty->subtype_begin(), Ty->subtype_end(), |
| 887 | E) != Ty->subtype_end(); |
| 888 | } |
| 889 | |
| 890 | namespace { |
| 891 | struct UpRefRecord { |
| 892 | // NestingLevel - The number of nesting levels that need to be popped before |
| 893 | // this type is resolved. |
| 894 | unsigned NestingLevel; |
| 895 | |
| 896 | // LastContainedTy - This is the type at the current binding level for the |
| 897 | // type. Every time we reduce the nesting level, this gets updated. |
| 898 | const Type *LastContainedTy; |
| 899 | |
| 900 | // UpRefTy - This is the actual opaque type that the upreference is |
| 901 | // represented with. |
| 902 | OpaqueType *UpRefTy; |
| 903 | |
| 904 | UpRefRecord(unsigned NL, OpaqueType *URTy) |
| 905 | : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {} |
| 906 | }; |
| 907 | } |
| 908 | |
| 909 | // UpRefs - A list of the outstanding upreferences that need to be resolved. |
| 910 | static std::vector<UpRefRecord> UpRefs; |
| 911 | |
| 912 | /// HandleUpRefs - Every time we finish a new layer of types, this function is |
| 913 | /// called. It loops through the UpRefs vector, which is a list of the |
| 914 | /// currently active types. For each type, if the up reference is contained in |
| 915 | /// the newly completed type, we decrement the level count. When the level |
| 916 | /// count reaches zero, the upreferenced type is the type that is passed in: |
| 917 | /// thus we can complete the cycle. |
| 918 | /// |
| 919 | static PATypeHolder HandleUpRefs(const Type *ty) { |
Chris Lattner | 224f84f | 2006-08-18 17:34:45 +0000 | [diff] [blame] | 920 | // If Ty isn't abstract, or if there are no up-references in it, then there is |
| 921 | // nothing to resolve here. |
| 922 | if (!ty->isAbstract() || UpRefs.empty()) return ty; |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 923 | |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 924 | PATypeHolder Ty(ty); |
| 925 | UR_OUT("Type '" << Ty->getDescription() << |
| 926 | "' newly formed. Resolving upreferences.\n" << |
| 927 | UpRefs.size() << " upreferences active!\n"); |
| 928 | |
| 929 | // If we find any resolvable upreferences (i.e., those whose NestingLevel goes |
| 930 | // to zero), we resolve them all together before we resolve them to Ty. At |
| 931 | // the end of the loop, if there is anything to resolve to Ty, it will be in |
| 932 | // this variable. |
| 933 | OpaqueType *TypeToResolve = 0; |
| 934 | |
| 935 | for (unsigned i = 0; i != UpRefs.size(); ++i) { |
| 936 | UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", " |
| 937 | << UpRefs[i].second->getDescription() << ") = " |
| 938 | << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n"); |
| 939 | if (TypeContains(Ty, UpRefs[i].LastContainedTy)) { |
| 940 | // Decrement level of upreference |
| 941 | unsigned Level = --UpRefs[i].NestingLevel; |
| 942 | UpRefs[i].LastContainedTy = Ty; |
| 943 | UR_OUT(" Uplevel Ref Level = " << Level << "\n"); |
| 944 | if (Level == 0) { // Upreference should be resolved! |
| 945 | if (!TypeToResolve) { |
| 946 | TypeToResolve = UpRefs[i].UpRefTy; |
| 947 | } else { |
| 948 | UR_OUT(" * Resolving upreference for " |
| 949 | << UpRefs[i].second->getDescription() << "\n"; |
| 950 | std::string OldName = UpRefs[i].UpRefTy->getDescription()); |
| 951 | UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve); |
| 952 | UR_OUT(" * Type '" << OldName << "' refined upreference to: " |
| 953 | << (const void*)Ty << ", " << Ty->getDescription() << "\n"); |
| 954 | } |
| 955 | UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list... |
| 956 | --i; // Do not skip the next element... |
| 957 | } |
| 958 | } |
| 959 | } |
| 960 | |
| 961 | if (TypeToResolve) { |
| 962 | UR_OUT(" * Resolving upreference for " |
| 963 | << UpRefs[i].second->getDescription() << "\n"; |
| 964 | std::string OldName = TypeToResolve->getDescription()); |
| 965 | TypeToResolve->refineAbstractTypeTo(Ty); |
| 966 | } |
| 967 | |
| 968 | return Ty; |
| 969 | } |
| 970 | |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 971 | //===----------------------------------------------------------------------===// |
| 972 | // RunVMAsmParser - Define an interface to this parser |
| 973 | //===----------------------------------------------------------------------===// |
| 974 | // |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 975 | static Module* RunParser(Module * M); |
| 976 | |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 977 | Module *llvm::RunVMAsmParser(llvm::MemoryBuffer *MB) { |
| 978 | InitLLLexer(MB); |
| 979 | Module *M = RunParser(new Module(LLLgetFilename())); |
| 980 | FreeLexer(); |
| 981 | return M; |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | %} |
| 985 | |
| 986 | %union { |
| 987 | llvm::Module *ModuleVal; |
| 988 | llvm::Function *FunctionVal; |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 989 | llvm::BasicBlock *BasicBlockVal; |
| 990 | llvm::TerminatorInst *TermInstVal; |
| 991 | llvm::Instruction *InstVal; |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 992 | llvm::Constant *ConstVal; |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 993 | |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 994 | const llvm::Type *PrimType; |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 995 | std::list<llvm::PATypeHolder> *TypeList; |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 996 | llvm::PATypeHolder *TypeVal; |
| 997 | llvm::Value *ValueVal; |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 998 | std::vector<llvm::Value*> *ValueList; |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 999 | std::vector<unsigned> *ConstantList; |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 1000 | llvm::ArgListType *ArgList; |
| 1001 | llvm::TypeWithAttrs TypeWithAttrs; |
| 1002 | llvm::TypeWithAttrsList *TypeWithAttrsList; |
Dale Johannesen | eb57ea7 | 2007-11-05 21:20:28 +0000 | [diff] [blame] | 1003 | llvm::ParamList *ParamList; |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 1004 | |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1005 | // Represent the RHS of PHI node |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 1006 | std::list<std::pair<llvm::Value*, |
| 1007 | llvm::BasicBlock*> > *PHIList; |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1008 | std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable; |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 1009 | std::vector<llvm::Constant*> *ConstVector; |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1010 | |
| 1011 | llvm::GlobalValue::LinkageTypes Linkage; |
Anton Korobeynikov | 7f70559 | 2007-01-12 19:20:47 +0000 | [diff] [blame] | 1012 | llvm::GlobalValue::VisibilityTypes Visibility; |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 1013 | llvm::Attributes Attributes; |
Reid Spencer | 38c91a9 | 2007-02-28 02:24:54 +0000 | [diff] [blame] | 1014 | llvm::APInt *APIntVal; |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1015 | int64_t SInt64Val; |
| 1016 | uint64_t UInt64Val; |
| 1017 | int SIntVal; |
| 1018 | unsigned UIntVal; |
Dale Johannesen | 43421b3 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 1019 | llvm::APFloat *FPVal; |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1020 | bool BoolVal; |
| 1021 | |
Reid Spencer | 0a8a16b | 2007-05-22 18:52:55 +0000 | [diff] [blame] | 1022 | std::string *StrVal; // This memory must be deleted |
| 1023 | llvm::ValID ValIDVal; |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1024 | |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 1025 | llvm::Instruction::BinaryOps BinaryOpVal; |
| 1026 | llvm::Instruction::TermOps TermOpVal; |
| 1027 | llvm::Instruction::MemoryOps MemOpVal; |
| 1028 | llvm::Instruction::CastOps CastOpVal; |
| 1029 | llvm::Instruction::OtherOps OtherOpVal; |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 1030 | llvm::ICmpInst::Predicate IPredicate; |
| 1031 | llvm::FCmpInst::Predicate FPredicate; |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1032 | } |
| 1033 | |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 1034 | %type <ModuleVal> Module |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1035 | %type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList |
| 1036 | %type <BasicBlockVal> BasicBlock InstructionList |
| 1037 | %type <TermInstVal> BBTerminatorInst |
| 1038 | %type <InstVal> Inst InstVal MemoryInst |
Anton Korobeynikov | 38e0980 | 2007-04-28 13:48:45 +0000 | [diff] [blame] | 1039 | %type <ConstVal> ConstVal ConstExpr AliaseeRef |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1040 | %type <ConstVector> ConstVector |
| 1041 | %type <ArgList> ArgList ArgListH |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1042 | %type <PHIList> PHIList |
Dale Johannesen | eb57ea7 | 2007-11-05 21:20:28 +0000 | [diff] [blame] | 1043 | %type <ParamList> ParamList // For call param lists & GEP indices |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 1044 | %type <ValueList> IndexList // For GEP indices |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1045 | %type <ConstantList> ConstantIndexList // For insertvalue/extractvalue indices |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 1046 | %type <TypeList> TypeListI |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 1047 | %type <TypeWithAttrsList> ArgTypeList ArgTypeListI |
Reid Spencer | 218ded2 | 2007-01-05 17:07:23 +0000 | [diff] [blame] | 1048 | %type <TypeWithAttrs> ArgType |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1049 | %type <JumpTable> JumpTable |
| 1050 | %type <BoolVal> GlobalType // GLOBAL or CONSTANT? |
Lauro Ramos Venancio | c763552 | 2007-04-12 18:32:50 +0000 | [diff] [blame] | 1051 | %type <BoolVal> ThreadLocal // 'thread_local' or not |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1052 | %type <BoolVal> OptVolatile // 'volatile' or not |
| 1053 | %type <BoolVal> OptTailCall // TAIL CALL or plain CALL. |
| 1054 | %type <BoolVal> OptSideEffect // 'sideeffect' or not. |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 1055 | %type <Linkage> GVInternalLinkage GVExternalLinkage |
| 1056 | %type <Linkage> FunctionDefineLinkage FunctionDeclareLinkage |
Anton Korobeynikov | 77d0f97 | 2007-04-25 14:29:12 +0000 | [diff] [blame] | 1057 | %type <Linkage> AliasLinkage |
Anton Korobeynikov | 7f70559 | 2007-01-12 19:20:47 +0000 | [diff] [blame] | 1058 | %type <Visibility> GVVisibilityStyle |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1059 | |
| 1060 | // ValueRef - Unresolved reference to a definition or BB |
| 1061 | %type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef |
| 1062 | %type <ValueVal> ResolvedVal // <type> <valref> pair |
Devang Patel | 7990dc7 | 2008-02-20 22:40:23 +0000 | [diff] [blame] | 1063 | %type <ValueList> ReturnedVal |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1064 | // Tokens and types for handling constant integer values |
| 1065 | // |
| 1066 | // ESINT64VAL - A negative number within long long range |
| 1067 | %token <SInt64Val> ESINT64VAL |
| 1068 | |
| 1069 | // EUINT64VAL - A positive number within uns. long long range |
| 1070 | %token <UInt64Val> EUINT64VAL |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1071 | |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 1072 | // ESAPINTVAL - A negative number with arbitrary precision |
Reid Spencer | 38c91a9 | 2007-02-28 02:24:54 +0000 | [diff] [blame] | 1073 | %token <APIntVal> ESAPINTVAL |
| 1074 | |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 1075 | // EUAPINTVAL - A positive number with arbitrary precision |
Reid Spencer | 38c91a9 | 2007-02-28 02:24:54 +0000 | [diff] [blame] | 1076 | %token <APIntVal> EUAPINTVAL |
| 1077 | |
Reid Spencer | 41dff5e | 2007-01-26 08:05:27 +0000 | [diff] [blame] | 1078 | %token <UIntVal> LOCALVAL_ID GLOBALVAL_ID // %123 @123 |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1079 | %token <FPVal> FPVAL // Float or Double constant |
| 1080 | |
| 1081 | // Built in types... |
Reid Spencer | 218ded2 | 2007-01-05 17:07:23 +0000 | [diff] [blame] | 1082 | %type <TypeVal> Types ResultTypes |
Chris Lattner | 740e709 | 2008-10-15 06:16:57 +0000 | [diff] [blame] | 1083 | %type <PrimType> PrimType // Classifications |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 1084 | %token <PrimType> VOID INTTYPE |
Dale Johannesen | 320fc8a | 2007-08-03 01:03:46 +0000 | [diff] [blame] | 1085 | %token <PrimType> FLOAT DOUBLE X86_FP80 FP128 PPC_FP128 LABEL |
Reid Spencer | 6fd36ab | 2006-12-29 20:35:03 +0000 | [diff] [blame] | 1086 | %token TYPE |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1087 | |
Reid Spencer | 0a8a16b | 2007-05-22 18:52:55 +0000 | [diff] [blame] | 1088 | |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 1089 | %token<StrVal> LOCALVAR GLOBALVAR LABELSTR |
Reid Spencer | ed951ea | 2007-05-19 07:22:10 +0000 | [diff] [blame] | 1090 | %token<StrVal> STRINGCONSTANT ATSTRINGCONSTANT PCTSTRINGCONSTANT |
Reid Spencer | 41dff5e | 2007-01-26 08:05:27 +0000 | [diff] [blame] | 1091 | %type <StrVal> LocalName OptLocalName OptLocalAssign |
Anton Korobeynikov | 77d0f97 | 2007-04-25 14:29:12 +0000 | [diff] [blame] | 1092 | %type <StrVal> GlobalName OptGlobalAssign GlobalAssign |
Gordon Henriksen | 80a75bf | 2007-12-10 03:18:06 +0000 | [diff] [blame] | 1093 | %type <StrVal> OptSection SectionString OptGC |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1094 | |
Christopher Lamb | bf3348d | 2007-12-12 08:45:45 +0000 | [diff] [blame] | 1095 | %type <UIntVal> OptAlign OptCAlign OptAddrSpace |
Reid Spencer | 0a8a16b | 2007-05-22 18:52:55 +0000 | [diff] [blame] | 1096 | |
Reid Spencer | 3d6b71e | 2007-04-09 01:56:05 +0000 | [diff] [blame] | 1097 | %token ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK |
Anton Korobeynikov | 77d0f97 | 2007-04-25 14:29:12 +0000 | [diff] [blame] | 1098 | %token DECLARE DEFINE GLOBAL CONSTANT SECTION ALIAS VOLATILE THREAD_LOCAL |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 1099 | %token TO DOTDOTDOT NULL_TOK UNDEF INTERNAL LINKONCE WEAK APPENDING |
Dale Johannesen | c7071cc | 2008-05-14 20:13:36 +0000 | [diff] [blame] | 1100 | %token DLLIMPORT DLLEXPORT EXTERN_WEAK COMMON |
Christopher Lamb | a8ed9bf | 2007-12-11 09:02:08 +0000 | [diff] [blame] | 1101 | %token OPAQUE EXTERNAL TARGET TRIPLE ALIGN ADDRSPACE |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1102 | %token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 1103 | %token CC_TOK CCC_TOK FASTCC_TOK COLDCC_TOK X86_STDCALLCC_TOK X86_FASTCALLCC_TOK |
Nick Lewycky | 280a6e6 | 2008-04-25 16:53:59 +0000 | [diff] [blame] | 1104 | %token DATALAYOUT |
Chris Lattner | 15bd095 | 2008-08-29 17:20:18 +0000 | [diff] [blame] | 1105 | %type <UIntVal> OptCallingConv LocalNumber |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 1106 | %type <Attributes> OptAttributes Attribute |
| 1107 | %type <Attributes> OptFuncAttrs FuncAttr |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 1108 | %type <Attributes> OptRetAttrs RetAttr |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1109 | |
| 1110 | // Basic Block Terminating Operators |
| 1111 | %token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE |
| 1112 | |
| 1113 | // Binary Operators |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1114 | %type <BinaryOpVal> ArithmeticOps LogicalOps // Binops Subcatagories |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 1115 | %token <BinaryOpVal> ADD SUB MUL UDIV SDIV FDIV UREM SREM FREM AND OR XOR |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1116 | %token <BinaryOpVal> SHL LSHR ASHR |
| 1117 | |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 1118 | %token <OtherOpVal> ICMP FCMP VICMP VFCMP |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 1119 | %type <IPredicate> IPredicates |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 1120 | %type <FPredicate> FPredicates |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 1121 | %token EQ NE SLT SGT SLE SGE ULT UGT ULE UGE |
Reid Spencer | 6e18b7d | 2006-12-03 06:59:29 +0000 | [diff] [blame] | 1122 | %token OEQ ONE OLT OGT OLE OGE ORD UNO UEQ UNE |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1123 | |
| 1124 | // Memory Instructions |
| 1125 | %token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR |
| 1126 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1127 | // Cast Operators |
| 1128 | %type <CastOpVal> CastOps |
| 1129 | %token <CastOpVal> TRUNC ZEXT SEXT FPTRUNC FPEXT BITCAST |
| 1130 | %token <CastOpVal> UITOFP SITOFP FPTOUI FPTOSI INTTOPTR PTRTOINT |
| 1131 | |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1132 | // Other Operators |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1133 | %token <OtherOpVal> PHI_TOK SELECT VAARG |
Chris Lattner | d5efe84 | 2006-04-08 01:18:56 +0000 | [diff] [blame] | 1134 | %token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR |
Devang Patel | 5a97097 | 2008-02-19 22:27:01 +0000 | [diff] [blame] | 1135 | %token <OtherOpVal> GETRESULT |
Dan Gohman | e4977cf | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 1136 | %token <OtherOpVal> EXTRACTVALUE INSERTVALUE |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1137 | |
Reid Spencer | 218ded2 | 2007-01-05 17:07:23 +0000 | [diff] [blame] | 1138 | // Function Attributes |
Nick Lewycky | d802be3 | 2008-12-19 09:41:54 +0000 | [diff] [blame] | 1139 | %token SIGNEXT ZEROEXT NORETURN INREG SRET NOUNWIND NOALIAS NOCAPTURE BYVAL |
| 1140 | %token READNONE READONLY GC OPTSIZE NOINLINE ALWAYSINLINE SSP SSPREQ NEST |
Devang Patel | d498081 | 2008-09-02 20:52:40 +0000 | [diff] [blame] | 1141 | |
Anton Korobeynikov | 7f70559 | 2007-01-12 19:20:47 +0000 | [diff] [blame] | 1142 | // Visibility Styles |
Anton Korobeynikov | 6f9896f | 2007-04-29 18:35:00 +0000 | [diff] [blame] | 1143 | %token DEFAULT HIDDEN PROTECTED |
Anton Korobeynikov | 7f70559 | 2007-01-12 19:20:47 +0000 | [diff] [blame] | 1144 | |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1145 | %start Module |
| 1146 | %% |
| 1147 | |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1148 | |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1149 | // Operations that are notably excluded from this list include: |
| 1150 | // RET, BR, & SWITCH because they end basic blocks and are treated specially. |
| 1151 | // |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 1152 | ArithmeticOps: ADD | SUB | MUL | UDIV | SDIV | FDIV | UREM | SREM | FREM; |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1153 | LogicalOps : SHL | LSHR | ASHR | AND | OR | XOR; |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 1154 | CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | BITCAST | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1155 | UITOFP | SITOFP | FPTOUI | FPTOSI | INTTOPTR | PTRTOINT; |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1156 | |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 1157 | IPredicates |
Reid Spencer | 4012e83 | 2006-12-04 05:24:24 +0000 | [diff] [blame] | 1158 | : EQ { $$ = ICmpInst::ICMP_EQ; } | NE { $$ = ICmpInst::ICMP_NE; } |
Reid Spencer | 6e18b7d | 2006-12-03 06:59:29 +0000 | [diff] [blame] | 1159 | | SLT { $$ = ICmpInst::ICMP_SLT; } | SGT { $$ = ICmpInst::ICMP_SGT; } |
| 1160 | | SLE { $$ = ICmpInst::ICMP_SLE; } | SGE { $$ = ICmpInst::ICMP_SGE; } |
| 1161 | | ULT { $$ = ICmpInst::ICMP_ULT; } | UGT { $$ = ICmpInst::ICMP_UGT; } |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 1162 | | ULE { $$ = ICmpInst::ICMP_ULE; } | UGE { $$ = ICmpInst::ICMP_UGE; } |
Reid Spencer | 6e18b7d | 2006-12-03 06:59:29 +0000 | [diff] [blame] | 1163 | ; |
| 1164 | |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 1165 | FPredicates |
Reid Spencer | 6e18b7d | 2006-12-03 06:59:29 +0000 | [diff] [blame] | 1166 | : OEQ { $$ = FCmpInst::FCMP_OEQ; } | ONE { $$ = FCmpInst::FCMP_ONE; } |
| 1167 | | OLT { $$ = FCmpInst::FCMP_OLT; } | OGT { $$ = FCmpInst::FCMP_OGT; } |
| 1168 | | OLE { $$ = FCmpInst::FCMP_OLE; } | OGE { $$ = FCmpInst::FCMP_OGE; } |
| 1169 | | ORD { $$ = FCmpInst::FCMP_ORD; } | UNO { $$ = FCmpInst::FCMP_UNO; } |
| 1170 | | UEQ { $$ = FCmpInst::FCMP_UEQ; } | UNE { $$ = FCmpInst::FCMP_UNE; } |
| 1171 | | ULT { $$ = FCmpInst::FCMP_ULT; } | UGT { $$ = FCmpInst::FCMP_UGT; } |
| 1172 | | ULE { $$ = FCmpInst::FCMP_ULE; } | UGE { $$ = FCmpInst::FCMP_UGE; } |
| 1173 | | TRUETOK { $$ = FCmpInst::FCMP_TRUE; } |
| 1174 | | FALSETOK { $$ = FCmpInst::FCMP_FALSE; } |
| 1175 | ; |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1176 | |
Reid Spencer | 0a8a16b | 2007-05-22 18:52:55 +0000 | [diff] [blame] | 1177 | LocalName : LOCALVAR | STRINGCONSTANT | PCTSTRINGCONSTANT ; |
Reid Spencer | 41dff5e | 2007-01-26 08:05:27 +0000 | [diff] [blame] | 1178 | OptLocalName : LocalName | /*empty*/ { $$ = 0; }; |
| 1179 | |
Christopher Lamb | bf3348d | 2007-12-12 08:45:45 +0000 | [diff] [blame] | 1180 | OptAddrSpace : ADDRSPACE '(' EUINT64VAL ')' { $$=$3; } |
| 1181 | | /*empty*/ { $$=0; }; |
| 1182 | |
Reid Spencer | 41dff5e | 2007-01-26 08:05:27 +0000 | [diff] [blame] | 1183 | /// OptLocalAssign - Value producing statements have an optional assignment |
| 1184 | /// component. |
| 1185 | OptLocalAssign : LocalName '=' { |
| 1186 | $$ = $1; |
| 1187 | CHECK_FOR_ERROR |
| 1188 | } |
| 1189 | | /*empty*/ { |
| 1190 | $$ = 0; |
| 1191 | CHECK_FOR_ERROR |
| 1192 | }; |
| 1193 | |
Chris Lattner | 15bd095 | 2008-08-29 17:20:18 +0000 | [diff] [blame] | 1194 | LocalNumber : LOCALVAL_ID '=' { |
| 1195 | $$ = $1; |
| 1196 | CHECK_FOR_ERROR |
| 1197 | }; |
| 1198 | |
| 1199 | |
Reid Spencer | 0a8a16b | 2007-05-22 18:52:55 +0000 | [diff] [blame] | 1200 | GlobalName : GLOBALVAR | ATSTRINGCONSTANT ; |
Reid Spencer | 41dff5e | 2007-01-26 08:05:27 +0000 | [diff] [blame] | 1201 | |
Anton Korobeynikov | 77d0f97 | 2007-04-25 14:29:12 +0000 | [diff] [blame] | 1202 | OptGlobalAssign : GlobalAssign |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1203 | | /*empty*/ { |
| 1204 | $$ = 0; |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 1205 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1206 | }; |
| 1207 | |
Anton Korobeynikov | 77d0f97 | 2007-04-25 14:29:12 +0000 | [diff] [blame] | 1208 | GlobalAssign : GlobalName '=' { |
| 1209 | $$ = $1; |
| 1210 | CHECK_FOR_ERROR |
Chris Lattner | 6cdc682 | 2007-04-26 05:31:05 +0000 | [diff] [blame] | 1211 | }; |
Anton Korobeynikov | 77d0f97 | 2007-04-25 14:29:12 +0000 | [diff] [blame] | 1212 | |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 1213 | GVInternalLinkage |
| 1214 | : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
| 1215 | | WEAK { $$ = GlobalValue::WeakLinkage; } |
Reid Spencer | 6fd36ab | 2006-12-29 20:35:03 +0000 | [diff] [blame] | 1216 | | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
| 1217 | | APPENDING { $$ = GlobalValue::AppendingLinkage; } |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 1218 | | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; } |
Dale Johannesen | c7071cc | 2008-05-14 20:13:36 +0000 | [diff] [blame] | 1219 | | COMMON { $$ = GlobalValue::CommonLinkage; } |
Reid Spencer | 6fd36ab | 2006-12-29 20:35:03 +0000 | [diff] [blame] | 1220 | ; |
| 1221 | |
| 1222 | GVExternalLinkage |
| 1223 | : DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; } |
| 1224 | | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; } |
| 1225 | | EXTERNAL { $$ = GlobalValue::ExternalLinkage; } |
| 1226 | ; |
| 1227 | |
Anton Korobeynikov | 7f70559 | 2007-01-12 19:20:47 +0000 | [diff] [blame] | 1228 | GVVisibilityStyle |
Anton Korobeynikov | 6f9896f | 2007-04-29 18:35:00 +0000 | [diff] [blame] | 1229 | : /*empty*/ { $$ = GlobalValue::DefaultVisibility; } |
| 1230 | | DEFAULT { $$ = GlobalValue::DefaultVisibility; } |
| 1231 | | HIDDEN { $$ = GlobalValue::HiddenVisibility; } |
| 1232 | | PROTECTED { $$ = GlobalValue::ProtectedVisibility; } |
Anton Korobeynikov | 7f70559 | 2007-01-12 19:20:47 +0000 | [diff] [blame] | 1233 | ; |
| 1234 | |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 1235 | FunctionDeclareLinkage |
| 1236 | : /*empty*/ { $$ = GlobalValue::ExternalLinkage; } |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 1237 | | DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; } |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 1238 | | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; } |
Reid Spencer | 6fd36ab | 2006-12-29 20:35:03 +0000 | [diff] [blame] | 1239 | ; |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 1240 | |
Anton Korobeynikov | 77d0f97 | 2007-04-25 14:29:12 +0000 | [diff] [blame] | 1241 | FunctionDefineLinkage |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 1242 | : /*empty*/ { $$ = GlobalValue::ExternalLinkage; } |
| 1243 | | INTERNAL { $$ = GlobalValue::InternalLinkage; } |
Reid Spencer | 6fd36ab | 2006-12-29 20:35:03 +0000 | [diff] [blame] | 1244 | | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
| 1245 | | WEAK { $$ = GlobalValue::WeakLinkage; } |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 1246 | | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; } |
| 1247 | ; |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1248 | |
Anton Korobeynikov | 77d0f97 | 2007-04-25 14:29:12 +0000 | [diff] [blame] | 1249 | AliasLinkage |
| 1250 | : /*empty*/ { $$ = GlobalValue::ExternalLinkage; } |
| 1251 | | WEAK { $$ = GlobalValue::WeakLinkage; } |
| 1252 | | INTERNAL { $$ = GlobalValue::InternalLinkage; } |
| 1253 | ; |
| 1254 | |
Anton Korobeynikov | bcb9770 | 2006-09-17 20:25:45 +0000 | [diff] [blame] | 1255 | OptCallingConv : /*empty*/ { $$ = CallingConv::C; } | |
| 1256 | CCC_TOK { $$ = CallingConv::C; } | |
Anton Korobeynikov | bcb9770 | 2006-09-17 20:25:45 +0000 | [diff] [blame] | 1257 | FASTCC_TOK { $$ = CallingConv::Fast; } | |
| 1258 | COLDCC_TOK { $$ = CallingConv::Cold; } | |
| 1259 | X86_STDCALLCC_TOK { $$ = CallingConv::X86_StdCall; } | |
| 1260 | X86_FASTCALLCC_TOK { $$ = CallingConv::X86_FastCall; } | |
| 1261 | CC_TOK EUINT64VAL { |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1262 | if ((unsigned)$2 != $2) |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 1263 | GEN_ERROR("Calling conv too large"); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1264 | $$ = $2; |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 1265 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1266 | }; |
| 1267 | |
Nick Lewycky | d802be3 | 2008-12-19 09:41:54 +0000 | [diff] [blame] | 1268 | Attribute : ZEROEXT { $$ = Attribute::ZExt; } |
| 1269 | | ZEXT { $$ = Attribute::ZExt; } |
| 1270 | | SIGNEXT { $$ = Attribute::SExt; } |
| 1271 | | SEXT { $$ = Attribute::SExt; } |
| 1272 | | INREG { $$ = Attribute::InReg; } |
| 1273 | | SRET { $$ = Attribute::StructRet; } |
| 1274 | | NOALIAS { $$ = Attribute::NoAlias; } |
| 1275 | | NOCAPTURE { $$ = Attribute::NoCapture; } |
| 1276 | | BYVAL { $$ = Attribute::ByVal; } |
| 1277 | | NEST { $$ = Attribute::Nest; } |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 1278 | | ALIGN EUINT64VAL { $$ = |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 1279 | Attribute::constructAlignmentFromInt($2); } |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 1280 | ; |
| 1281 | |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 1282 | OptAttributes : /* empty */ { $$ = Attribute::None; } |
| 1283 | | OptAttributes Attribute { |
Reid Spencer | 7b5d466 | 2007-04-09 06:16:21 +0000 | [diff] [blame] | 1284 | $$ = $1 | $2; |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 1285 | } |
| 1286 | ; |
| 1287 | |
Nick Lewycky | d802be3 | 2008-12-19 09:41:54 +0000 | [diff] [blame] | 1288 | RetAttr : INREG { $$ = Attribute::InReg; } |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 1289 | | ZEROEXT { $$ = Attribute::ZExt; } |
| 1290 | | SIGNEXT { $$ = Attribute::SExt; } |
Nick Lewycky | d802be3 | 2008-12-19 09:41:54 +0000 | [diff] [blame] | 1291 | | NOALIAS { $$ = Attribute::NoAlias; } |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 1292 | ; |
| 1293 | |
| 1294 | OptRetAttrs : /* empty */ { $$ = Attribute::None; } |
| 1295 | | OptRetAttrs RetAttr { |
| 1296 | $$ = $1 | $2; |
| 1297 | } |
| 1298 | ; |
| 1299 | |
| 1300 | |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 1301 | FuncAttr : NORETURN { $$ = Attribute::NoReturn; } |
| 1302 | | NOUNWIND { $$ = Attribute::NoUnwind; } |
| 1303 | | INREG { $$ = Attribute::InReg; } |
| 1304 | | ZEROEXT { $$ = Attribute::ZExt; } |
| 1305 | | SIGNEXT { $$ = Attribute::SExt; } |
| 1306 | | READNONE { $$ = Attribute::ReadNone; } |
| 1307 | | READONLY { $$ = Attribute::ReadOnly; } |
Chris Lattner | 9fc4da4 | 2008-10-08 06:44:45 +0000 | [diff] [blame] | 1308 | | NOINLINE { $$ = Attribute::NoInline; } |
| 1309 | | ALWAYSINLINE { $$ = Attribute::AlwaysInline; } |
Bill Wendling | 6ff4bfe | 2008-11-13 01:03:00 +0000 | [diff] [blame] | 1310 | | OPTSIZE { $$ = Attribute::OptimizeForSize; } |
| 1311 | | SSP { $$ = Attribute::StackProtect; } |
| 1312 | | SSPREQ { $$ = Attribute::StackProtectReq; } |
Reid Spencer | 218ded2 | 2007-01-05 17:07:23 +0000 | [diff] [blame] | 1313 | ; |
| 1314 | |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 1315 | OptFuncAttrs : /* empty */ { $$ = Attribute::None; } |
Reid Spencer | 218ded2 | 2007-01-05 17:07:23 +0000 | [diff] [blame] | 1316 | | OptFuncAttrs FuncAttr { |
Reid Spencer | 7b5d466 | 2007-04-09 06:16:21 +0000 | [diff] [blame] | 1317 | $$ = $1 | $2; |
Reid Spencer | 218ded2 | 2007-01-05 17:07:23 +0000 | [diff] [blame] | 1318 | } |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 1319 | ; |
| 1320 | |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 1321 | |
Gordon Henriksen | 80a75bf | 2007-12-10 03:18:06 +0000 | [diff] [blame] | 1322 | OptGC : /* empty */ { $$ = 0; } |
| 1323 | | GC STRINGCONSTANT { |
| 1324 | $$ = $2; |
| 1325 | } |
| 1326 | ; |
| 1327 | |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1328 | // OptAlign/OptCAlign - An optional alignment, and an optional alignment with |
| 1329 | // a comma before it. |
| 1330 | OptAlign : /*empty*/ { $$ = 0; } | |
| 1331 | ALIGN EUINT64VAL { |
| 1332 | $$ = $2; |
| 1333 | if ($$ != 0 && !isPowerOf2_32($$)) |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 1334 | GEN_ERROR("Alignment must be a power of two"); |
Nick Lewycky | d802be3 | 2008-12-19 09:41:54 +0000 | [diff] [blame] | 1335 | if ($$ > 0x40000000) |
| 1336 | GEN_ERROR("Alignment too large"); |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 1337 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1338 | }; |
| 1339 | OptCAlign : /*empty*/ { $$ = 0; } | |
| 1340 | ',' ALIGN EUINT64VAL { |
| 1341 | $$ = $3; |
| 1342 | if ($$ != 0 && !isPowerOf2_32($$)) |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 1343 | GEN_ERROR("Alignment must be a power of two"); |
Nick Lewycky | d802be3 | 2008-12-19 09:41:54 +0000 | [diff] [blame] | 1344 | if ($$ > 0x40000000) |
| 1345 | GEN_ERROR("Alignment too large"); |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 1346 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1347 | }; |
| 1348 | |
| 1349 | |
Christopher Lamb | a8ed9bf | 2007-12-11 09:02:08 +0000 | [diff] [blame] | 1350 | |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1351 | SectionString : SECTION STRINGCONSTANT { |
Reid Spencer | 0a8a16b | 2007-05-22 18:52:55 +0000 | [diff] [blame] | 1352 | for (unsigned i = 0, e = $2->length(); i != e; ++i) |
| 1353 | if ((*$2)[i] == '"' || (*$2)[i] == '\\') |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 1354 | GEN_ERROR("Invalid character in section name"); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1355 | $$ = $2; |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 1356 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1357 | }; |
| 1358 | |
| 1359 | OptSection : /*empty*/ { $$ = 0; } | |
| 1360 | SectionString { $$ = $1; }; |
| 1361 | |
| 1362 | // GlobalVarAttributes - Used to pass the attributes string on a global. CurGV |
| 1363 | // is set to be the global we are processing. |
| 1364 | // |
| 1365 | GlobalVarAttributes : /* empty */ {} | |
| 1366 | ',' GlobalVarAttribute GlobalVarAttributes {}; |
| 1367 | GlobalVarAttribute : SectionString { |
Reid Spencer | 0a8a16b | 2007-05-22 18:52:55 +0000 | [diff] [blame] | 1368 | CurGV->setSection(*$1); |
| 1369 | delete $1; |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 1370 | CHECK_FOR_ERROR |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 1371 | } |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1372 | | ALIGN EUINT64VAL { |
| 1373 | if ($2 != 0 && !isPowerOf2_32($2)) |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 1374 | GEN_ERROR("Alignment must be a power of two"); |
Nick Lewycky | d802be3 | 2008-12-19 09:41:54 +0000 | [diff] [blame] | 1375 | if ($2 > 0x40000000) |
| 1376 | GEN_ERROR("Alignment too large"); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1377 | CurGV->setAlignment($2); |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 1378 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1379 | }; |
| 1380 | |
| 1381 | //===----------------------------------------------------------------------===// |
| 1382 | // Types includes all predefined types... except void, because it can only be |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 1383 | // used in specific contexts (function returning void for example). |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1384 | |
| 1385 | // Derived types are added later... |
| 1386 | // |
Dale Johannesen | 320fc8a | 2007-08-03 01:03:46 +0000 | [diff] [blame] | 1387 | PrimType : INTTYPE | FLOAT | DOUBLE | PPC_FP128 | FP128 | X86_FP80 | LABEL ; |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 1388 | |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 1389 | Types |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 1390 | : OPAQUE { |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 1391 | $$ = new PATypeHolder(OpaqueType::get()); |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 1392 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1393 | } |
| 1394 | | PrimType { |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 1395 | $$ = new PATypeHolder($1); |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 1396 | CHECK_FOR_ERROR |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 1397 | } |
Christopher Lamb | bf3348d | 2007-12-12 08:45:45 +0000 | [diff] [blame] | 1398 | | Types OptAddrSpace '*' { // Pointer type? |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 1399 | if (*$1 == Type::LabelTy) |
| 1400 | GEN_ERROR("Cannot form a pointer to a basic block"); |
Christopher Lamb | bf3348d | 2007-12-12 08:45:45 +0000 | [diff] [blame] | 1401 | $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1, $2))); |
Christopher Lamb | a8ed9bf | 2007-12-11 09:02:08 +0000 | [diff] [blame] | 1402 | delete $1; |
| 1403 | CHECK_FOR_ERROR |
| 1404 | } |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 1405 | | SymbolicValueRef { // Named types are also simple types... |
| 1406 | const Type* tmp = getTypeVal($1); |
| 1407 | CHECK_FOR_ERROR |
| 1408 | $$ = new PATypeHolder(tmp); |
| 1409 | } |
| 1410 | | '\\' EUINT64VAL { // Type UpReference |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 1411 | if ($2 > (uint64_t)~0U) GEN_ERROR("Value out of range"); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1412 | OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder |
| 1413 | UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector... |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 1414 | $$ = new PATypeHolder(OT); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1415 | UR_OUT("New Upreference!\n"); |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 1416 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1417 | } |
Reid Spencer | 218ded2 | 2007-01-05 17:07:23 +0000 | [diff] [blame] | 1418 | | Types '(' ArgTypeListI ')' OptFuncAttrs { |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 1419 | // Allow but ignore attributes on function types; this permits auto-upgrade. |
| 1420 | // FIXME: remove in LLVM 3.0. |
Chris Lattner | a925a14 | 2008-04-23 05:37:08 +0000 | [diff] [blame] | 1421 | const Type *RetTy = *$1; |
| 1422 | if (!FunctionType::isValidReturnType(RetTy)) |
| 1423 | GEN_ERROR("Invalid result type for LLVM function"); |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 1424 | |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1425 | std::vector<const Type*> Params; |
Reid Spencer | 7b5d466 | 2007-04-09 06:16:21 +0000 | [diff] [blame] | 1426 | TypeWithAttrsList::iterator I = $3->begin(), E = $3->end(); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 1427 | for (; I != E; ++I ) { |
Reid Spencer | 66728ef | 2007-03-20 01:13:36 +0000 | [diff] [blame] | 1428 | const Type *Ty = I->Ty->get(); |
Reid Spencer | 66728ef | 2007-03-20 01:13:36 +0000 | [diff] [blame] | 1429 | Params.push_back(Ty); |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 1430 | } |
Anton Korobeynikov | c1d848d | 2007-12-03 19:16:54 +0000 | [diff] [blame] | 1431 | |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1432 | bool isVarArg = Params.size() && Params.back() == Type::VoidTy; |
| 1433 | if (isVarArg) Params.pop_back(); |
| 1434 | |
Anton Korobeynikov | 05e5a74 | 2007-12-03 21:01:29 +0000 | [diff] [blame] | 1435 | for (unsigned i = 0; i != Params.size(); ++i) |
| 1436 | if (!(Params[i]->isFirstClassType() || isa<OpaqueType>(Params[i]))) |
| 1437 | GEN_ERROR("Function arguments must be value types!"); |
| 1438 | |
| 1439 | CHECK_FOR_ERROR |
| 1440 | |
Anton Korobeynikov | c1d848d | 2007-12-03 19:16:54 +0000 | [diff] [blame] | 1441 | FunctionType *FT = FunctionType::get(RetTy, Params, isVarArg); |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 1442 | delete $1; // Delete the return type handle |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 1443 | $$ = new PATypeHolder(HandleUpRefs(FT)); |
Nuno Lopes | 8a88a37 | 2008-10-05 16:49:34 +0000 | [diff] [blame] | 1444 | |
| 1445 | // Delete the argument list |
| 1446 | for (I = $3->begin() ; I != E; ++I ) { |
| 1447 | delete I->Ty; |
| 1448 | } |
| 1449 | delete $3; |
| 1450 | |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 1451 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1452 | } |
Reid Spencer | 218ded2 | 2007-01-05 17:07:23 +0000 | [diff] [blame] | 1453 | | VOID '(' ArgTypeListI ')' OptFuncAttrs { |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 1454 | // Allow but ignore attributes on function types; this permits auto-upgrade. |
| 1455 | // FIXME: remove in LLVM 3.0. |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 1456 | std::vector<const Type*> Params; |
Reid Spencer | 7b5d466 | 2007-04-09 06:16:21 +0000 | [diff] [blame] | 1457 | TypeWithAttrsList::iterator I = $3->begin(), E = $3->end(); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 1458 | for ( ; I != E; ++I ) { |
Reid Spencer | 66728ef | 2007-03-20 01:13:36 +0000 | [diff] [blame] | 1459 | const Type* Ty = I->Ty->get(); |
Reid Spencer | 66728ef | 2007-03-20 01:13:36 +0000 | [diff] [blame] | 1460 | Params.push_back(Ty); |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 1461 | } |
Anton Korobeynikov | c1d848d | 2007-12-03 19:16:54 +0000 | [diff] [blame] | 1462 | |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 1463 | bool isVarArg = Params.size() && Params.back() == Type::VoidTy; |
| 1464 | if (isVarArg) Params.pop_back(); |
| 1465 | |
Anton Korobeynikov | 05e5a74 | 2007-12-03 21:01:29 +0000 | [diff] [blame] | 1466 | for (unsigned i = 0; i != Params.size(); ++i) |
| 1467 | if (!(Params[i]->isFirstClassType() || isa<OpaqueType>(Params[i]))) |
| 1468 | GEN_ERROR("Function arguments must be value types!"); |
| 1469 | |
| 1470 | CHECK_FOR_ERROR |
| 1471 | |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 1472 | FunctionType *FT = FunctionType::get($1, Params, isVarArg); |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 1473 | $$ = new PATypeHolder(HandleUpRefs(FT)); |
Nuno Lopes | 8a88a37 | 2008-10-05 16:49:34 +0000 | [diff] [blame] | 1474 | |
| 1475 | // Delete the argument list |
| 1476 | for (I = $3->begin() ; I != E; ++I ) { |
| 1477 | delete I->Ty; |
| 1478 | } |
| 1479 | delete $3; |
| 1480 | |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 1481 | CHECK_FOR_ERROR |
| 1482 | } |
| 1483 | |
| 1484 | | '[' EUINT64VAL 'x' Types ']' { // Sized array type? |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1485 | $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, $2))); |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 1486 | delete $4; |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 1487 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1488 | } |
Chris Lattner | 3298069 | 2007-02-19 07:44:24 +0000 | [diff] [blame] | 1489 | | '<' EUINT64VAL 'x' Types '>' { // Vector type? |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 1490 | const llvm::Type* ElemTy = $4->get(); |
| 1491 | if ((unsigned)$2 != $2) |
| 1492 | GEN_ERROR("Unsigned result not equal to signed result"); |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1493 | if (!ElemTy->isFloatingPoint() && !ElemTy->isInteger()) |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1494 | GEN_ERROR("Element type of a VectorType must be primitive"); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1495 | $$ = new PATypeHolder(HandleUpRefs(VectorType::get(*$4, (unsigned)$2))); |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 1496 | delete $4; |
| 1497 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1498 | } |
| 1499 | | '{' TypeListI '}' { // Structure type? |
| 1500 | std::vector<const Type*> Elements; |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 1501 | for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(), |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1502 | E = $2->end(); I != E; ++I) |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 1503 | Elements.push_back(*I); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1504 | |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 1505 | $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements))); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1506 | delete $2; |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 1507 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1508 | } |
| 1509 | | '{' '}' { // Empty structure type? |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 1510 | $$ = new PATypeHolder(StructType::get(std::vector<const Type*>())); |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 1511 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1512 | } |
Andrew Lenharth | 6353e05 | 2006-12-08 18:07:09 +0000 | [diff] [blame] | 1513 | | '<' '{' TypeListI '}' '>' { |
| 1514 | std::vector<const Type*> Elements; |
| 1515 | for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(), |
| 1516 | E = $3->end(); I != E; ++I) |
| 1517 | Elements.push_back(*I); |
| 1518 | |
| 1519 | $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements, true))); |
| 1520 | delete $3; |
| 1521 | CHECK_FOR_ERROR |
| 1522 | } |
| 1523 | | '<' '{' '}' '>' { // Empty structure type? |
| 1524 | $$ = new PATypeHolder(StructType::get(std::vector<const Type*>(), true)); |
| 1525 | CHECK_FOR_ERROR |
| 1526 | } |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 1527 | ; |
| 1528 | |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 1529 | ArgType |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 1530 | : Types OptAttributes { |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 1531 | // Allow but ignore attributes on function types; this permits auto-upgrade. |
| 1532 | // FIXME: remove in LLVM 3.0. |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 1533 | $$.Ty = $1; |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 1534 | $$.Attrs = Attribute::None; |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 1535 | } |
| 1536 | ; |
| 1537 | |
Reid Spencer | 218ded2 | 2007-01-05 17:07:23 +0000 | [diff] [blame] | 1538 | ResultTypes |
| 1539 | : Types { |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 1540 | if (!UpRefs.empty()) |
| 1541 | GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription()); |
Devang Patel | 2007173 | 2008-02-23 01:17:37 +0000 | [diff] [blame] | 1542 | if (!(*$1)->isFirstClassType() && !isa<StructType>($1->get())) |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 1543 | GEN_ERROR("LLVM functions cannot return aggregate types"); |
Reid Spencer | 218ded2 | 2007-01-05 17:07:23 +0000 | [diff] [blame] | 1544 | $$ = $1; |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 1545 | } |
Reid Spencer | 218ded2 | 2007-01-05 17:07:23 +0000 | [diff] [blame] | 1546 | | VOID { |
| 1547 | $$ = new PATypeHolder(Type::VoidTy); |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 1548 | } |
| 1549 | ; |
| 1550 | |
| 1551 | ArgTypeList : ArgType { |
| 1552 | $$ = new TypeWithAttrsList(); |
| 1553 | $$->push_back($1); |
| 1554 | CHECK_FOR_ERROR |
| 1555 | } |
| 1556 | | ArgTypeList ',' ArgType { |
| 1557 | ($$=$1)->push_back($3); |
| 1558 | CHECK_FOR_ERROR |
| 1559 | } |
| 1560 | ; |
| 1561 | |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 1562 | ArgTypeListI |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 1563 | : ArgTypeList |
| 1564 | | ArgTypeList ',' DOTDOTDOT { |
| 1565 | $$=$1; |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 1566 | TypeWithAttrs TWA; TWA.Attrs = Attribute::None; |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 1567 | TWA.Ty = new PATypeHolder(Type::VoidTy); |
| 1568 | $$->push_back(TWA); |
| 1569 | CHECK_FOR_ERROR |
| 1570 | } |
| 1571 | | DOTDOTDOT { |
| 1572 | $$ = new TypeWithAttrsList; |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 1573 | TypeWithAttrs TWA; TWA.Attrs = Attribute::None; |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 1574 | TWA.Ty = new PATypeHolder(Type::VoidTy); |
| 1575 | $$->push_back(TWA); |
| 1576 | CHECK_FOR_ERROR |
| 1577 | } |
| 1578 | | /*empty*/ { |
| 1579 | $$ = new TypeWithAttrsList(); |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 1580 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1581 | }; |
| 1582 | |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 1583 | // TypeList - Used for struct declarations and as a basis for function type |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1584 | // declaration type lists |
| 1585 | // |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 1586 | TypeListI : Types { |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 1587 | $$ = new std::list<PATypeHolder>(); |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 1588 | $$->push_back(*$1); |
Reid Spencer | 66728ef | 2007-03-20 01:13:36 +0000 | [diff] [blame] | 1589 | delete $1; |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 1590 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1591 | } |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 1592 | | TypeListI ',' Types { |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 1593 | ($$=$1)->push_back(*$3); |
Reid Spencer | 66728ef | 2007-03-20 01:13:36 +0000 | [diff] [blame] | 1594 | delete $3; |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 1595 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1596 | }; |
| 1597 | |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1598 | // ConstVal - The various declarations that go into the constant pool. This |
| 1599 | // production is used ONLY to represent constants that show up AFTER a 'const', |
| 1600 | // 'constant' or 'global' token at global scope. Constants that can be inlined |
| 1601 | // into other expressions (such as integers and constexprs) are handled by the |
| 1602 | // ResolvedVal, ValueRef and ConstValueRef productions. |
| 1603 | // |
| 1604 | ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 1605 | if (!UpRefs.empty()) |
| 1606 | GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription()); |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 1607 | const ArrayType *ATy = dyn_cast<ArrayType>($1->get()); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1608 | if (ATy == 0) |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 1609 | GEN_ERROR("Cannot make array constant with type: '" + |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 1610 | (*$1)->getDescription() + "'"); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1611 | const Type *ETy = ATy->getElementType(); |
Dan Gohman | 180c169 | 2008-06-23 18:43:26 +0000 | [diff] [blame] | 1612 | uint64_t NumElements = ATy->getNumElements(); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1613 | |
| 1614 | // Verify that we have the correct size... |
Mon P Wang | 2887310 | 2008-06-25 08:15:39 +0000 | [diff] [blame] | 1615 | if (NumElements != uint64_t(-1) && NumElements != $3->size()) |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 1616 | GEN_ERROR("Type mismatch: constant sized array initialized with " + |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 1617 | utostr($3->size()) + " arguments, but has size of " + |
Mon P Wang | 2887310 | 2008-06-25 08:15:39 +0000 | [diff] [blame] | 1618 | utostr(NumElements) + ""); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1619 | |
| 1620 | // Verify all elements are correct type! |
| 1621 | for (unsigned i = 0; i < $3->size(); i++) { |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 1622 | if (ETy != (*$3)[i]->getType()) |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 1623 | GEN_ERROR("Element #" + utostr(i) + " is not of type '" + |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1624 | ETy->getDescription() +"' as required!\nIt is of type '"+ |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 1625 | (*$3)[i]->getType()->getDescription() + "'."); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1626 | } |
| 1627 | |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 1628 | $$ = ConstantArray::get(ATy, *$3); |
| 1629 | delete $1; delete $3; |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 1630 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1631 | } |
| 1632 | | Types '[' ']' { |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 1633 | if (!UpRefs.empty()) |
| 1634 | GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription()); |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 1635 | const ArrayType *ATy = dyn_cast<ArrayType>($1->get()); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1636 | if (ATy == 0) |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 1637 | GEN_ERROR("Cannot make array constant with type: '" + |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 1638 | (*$1)->getDescription() + "'"); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1639 | |
Dan Gohman | 180c169 | 2008-06-23 18:43:26 +0000 | [diff] [blame] | 1640 | uint64_t NumElements = ATy->getNumElements(); |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 1641 | if (NumElements != uint64_t(-1) && NumElements != 0) |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 1642 | GEN_ERROR("Type mismatch: constant sized array initialized with 0" |
Mon P Wang | 2887310 | 2008-06-25 08:15:39 +0000 | [diff] [blame] | 1643 | " arguments, but has size of " + utostr(NumElements) +""); |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 1644 | $$ = ConstantArray::get(ATy, std::vector<Constant*>()); |
| 1645 | delete $1; |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 1646 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1647 | } |
| 1648 | | Types 'c' STRINGCONSTANT { |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 1649 | if (!UpRefs.empty()) |
| 1650 | GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription()); |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 1651 | const ArrayType *ATy = dyn_cast<ArrayType>($1->get()); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1652 | if (ATy == 0) |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 1653 | GEN_ERROR("Cannot make array constant with type: '" + |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 1654 | (*$1)->getDescription() + "'"); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1655 | |
Dan Gohman | 180c169 | 2008-06-23 18:43:26 +0000 | [diff] [blame] | 1656 | uint64_t NumElements = ATy->getNumElements(); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1657 | const Type *ETy = ATy->getElementType(); |
Mon P Wang | 2887310 | 2008-06-25 08:15:39 +0000 | [diff] [blame] | 1658 | if (NumElements != uint64_t(-1) && NumElements != $3->length()) |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 1659 | GEN_ERROR("Can't build string constant of size " + |
Mon P Wang | 2887310 | 2008-06-25 08:15:39 +0000 | [diff] [blame] | 1660 | utostr($3->length()) + |
| 1661 | " when array has size " + utostr(NumElements) + ""); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1662 | std::vector<Constant*> Vals; |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 1663 | if (ETy == Type::Int8Ty) { |
Mon P Wang | 2887310 | 2008-06-25 08:15:39 +0000 | [diff] [blame] | 1664 | for (uint64_t i = 0; i < $3->length(); ++i) |
Reid Spencer | 0a8a16b | 2007-05-22 18:52:55 +0000 | [diff] [blame] | 1665 | Vals.push_back(ConstantInt::get(ETy, (*$3)[i])); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1666 | } else { |
Reid Spencer | 0a8a16b | 2007-05-22 18:52:55 +0000 | [diff] [blame] | 1667 | delete $3; |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 1668 | GEN_ERROR("Cannot build string arrays of non byte sized elements"); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1669 | } |
Reid Spencer | 0a8a16b | 2007-05-22 18:52:55 +0000 | [diff] [blame] | 1670 | delete $3; |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 1671 | $$ = ConstantArray::get(ATy, Vals); |
| 1672 | delete $1; |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 1673 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1674 | } |
| 1675 | | Types '<' ConstVector '>' { // Nonempty unsized arr |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 1676 | if (!UpRefs.empty()) |
| 1677 | GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription()); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1678 | const VectorType *PTy = dyn_cast<VectorType>($1->get()); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1679 | if (PTy == 0) |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 1680 | GEN_ERROR("Cannot make packed constant with type: '" + |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 1681 | (*$1)->getDescription() + "'"); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1682 | const Type *ETy = PTy->getElementType(); |
Dan Gohman | 180c169 | 2008-06-23 18:43:26 +0000 | [diff] [blame] | 1683 | unsigned NumElements = PTy->getNumElements(); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1684 | |
| 1685 | // Verify that we have the correct size... |
Mon P Wang | 2887310 | 2008-06-25 08:15:39 +0000 | [diff] [blame] | 1686 | if (NumElements != unsigned(-1) && NumElements != (unsigned)$3->size()) |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 1687 | GEN_ERROR("Type mismatch: constant sized packed initialized with " + |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 1688 | utostr($3->size()) + " arguments, but has size of " + |
Mon P Wang | 2887310 | 2008-06-25 08:15:39 +0000 | [diff] [blame] | 1689 | utostr(NumElements) + ""); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1690 | |
| 1691 | // Verify all elements are correct type! |
| 1692 | for (unsigned i = 0; i < $3->size(); i++) { |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 1693 | if (ETy != (*$3)[i]->getType()) |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 1694 | GEN_ERROR("Element #" + utostr(i) + " is not of type '" + |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1695 | ETy->getDescription() +"' as required!\nIt is of type '"+ |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 1696 | (*$3)[i]->getType()->getDescription() + "'."); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1697 | } |
| 1698 | |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1699 | $$ = ConstantVector::get(PTy, *$3); |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 1700 | delete $1; delete $3; |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 1701 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1702 | } |
| 1703 | | Types '{' ConstVector '}' { |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 1704 | const StructType *STy = dyn_cast<StructType>($1->get()); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1705 | if (STy == 0) |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 1706 | GEN_ERROR("Cannot make struct constant with type: '" + |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 1707 | (*$1)->getDescription() + "'"); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1708 | |
| 1709 | if ($3->size() != STy->getNumContainedTypes()) |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 1710 | GEN_ERROR("Illegal number of initializers for structure type"); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1711 | |
| 1712 | // Check to ensure that constants are compatible with the type initializer! |
| 1713 | for (unsigned i = 0, e = $3->size(); i != e; ++i) |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 1714 | if ((*$3)[i]->getType() != STy->getElementType(i)) |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 1715 | GEN_ERROR("Expected type '" + |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1716 | STy->getElementType(i)->getDescription() + |
| 1717 | "' for element #" + utostr(i) + |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 1718 | " of structure initializer"); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1719 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1720 | // Check to ensure that Type is not packed |
| 1721 | if (STy->isPacked()) |
Chris Lattner | 6cdc682 | 2007-04-26 05:31:05 +0000 | [diff] [blame] | 1722 | GEN_ERROR("Unpacked Initializer to vector type '" + |
| 1723 | STy->getDescription() + "'"); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1724 | |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 1725 | $$ = ConstantStruct::get(STy, *$3); |
| 1726 | delete $1; delete $3; |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 1727 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1728 | } |
| 1729 | | Types '{' '}' { |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 1730 | if (!UpRefs.empty()) |
| 1731 | GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription()); |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 1732 | const StructType *STy = dyn_cast<StructType>($1->get()); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1733 | if (STy == 0) |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 1734 | GEN_ERROR("Cannot make struct constant with type: '" + |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 1735 | (*$1)->getDescription() + "'"); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1736 | |
| 1737 | if (STy->getNumContainedTypes() != 0) |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 1738 | GEN_ERROR("Illegal number of initializers for structure type"); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1739 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1740 | // Check to ensure that Type is not packed |
| 1741 | if (STy->isPacked()) |
Chris Lattner | 6cdc682 | 2007-04-26 05:31:05 +0000 | [diff] [blame] | 1742 | GEN_ERROR("Unpacked Initializer to vector type '" + |
| 1743 | STy->getDescription() + "'"); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1744 | |
| 1745 | $$ = ConstantStruct::get(STy, std::vector<Constant*>()); |
| 1746 | delete $1; |
| 1747 | CHECK_FOR_ERROR |
| 1748 | } |
| 1749 | | Types '<' '{' ConstVector '}' '>' { |
| 1750 | const StructType *STy = dyn_cast<StructType>($1->get()); |
| 1751 | if (STy == 0) |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 1752 | GEN_ERROR("Cannot make struct constant with type: '" + |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 1753 | (*$1)->getDescription() + "'"); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1754 | |
| 1755 | if ($4->size() != STy->getNumContainedTypes()) |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 1756 | GEN_ERROR("Illegal number of initializers for structure type"); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1757 | |
| 1758 | // Check to ensure that constants are compatible with the type initializer! |
| 1759 | for (unsigned i = 0, e = $4->size(); i != e; ++i) |
| 1760 | if ((*$4)[i]->getType() != STy->getElementType(i)) |
| 1761 | GEN_ERROR("Expected type '" + |
| 1762 | STy->getElementType(i)->getDescription() + |
| 1763 | "' for element #" + utostr(i) + |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 1764 | " of structure initializer"); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1765 | |
| 1766 | // Check to ensure that Type is packed |
| 1767 | if (!STy->isPacked()) |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 1768 | GEN_ERROR("Vector initializer to non-vector type '" + |
Chris Lattner | 3298069 | 2007-02-19 07:44:24 +0000 | [diff] [blame] | 1769 | STy->getDescription() + "'"); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1770 | |
| 1771 | $$ = ConstantStruct::get(STy, *$4); |
| 1772 | delete $1; delete $4; |
| 1773 | CHECK_FOR_ERROR |
| 1774 | } |
| 1775 | | Types '<' '{' '}' '>' { |
| 1776 | if (!UpRefs.empty()) |
| 1777 | GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription()); |
| 1778 | const StructType *STy = dyn_cast<StructType>($1->get()); |
| 1779 | if (STy == 0) |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 1780 | GEN_ERROR("Cannot make struct constant with type: '" + |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 1781 | (*$1)->getDescription() + "'"); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1782 | |
| 1783 | if (STy->getNumContainedTypes() != 0) |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 1784 | GEN_ERROR("Illegal number of initializers for structure type"); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1785 | |
| 1786 | // Check to ensure that Type is packed |
| 1787 | if (!STy->isPacked()) |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 1788 | GEN_ERROR("Vector initializer to non-vector type '" + |
Chris Lattner | 3298069 | 2007-02-19 07:44:24 +0000 | [diff] [blame] | 1789 | STy->getDescription() + "'"); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1790 | |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 1791 | $$ = ConstantStruct::get(STy, std::vector<Constant*>()); |
| 1792 | delete $1; |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 1793 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1794 | } |
| 1795 | | Types NULL_TOK { |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 1796 | if (!UpRefs.empty()) |
| 1797 | GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription()); |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 1798 | const PointerType *PTy = dyn_cast<PointerType>($1->get()); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1799 | if (PTy == 0) |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 1800 | GEN_ERROR("Cannot make null pointer constant with type: '" + |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 1801 | (*$1)->getDescription() + "'"); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1802 | |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 1803 | $$ = ConstantPointerNull::get(PTy); |
| 1804 | delete $1; |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 1805 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1806 | } |
| 1807 | | Types UNDEF { |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 1808 | if (!UpRefs.empty()) |
| 1809 | GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription()); |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 1810 | $$ = UndefValue::get($1->get()); |
| 1811 | delete $1; |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 1812 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1813 | } |
| 1814 | | Types SymbolicValueRef { |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 1815 | if (!UpRefs.empty()) |
| 1816 | GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription()); |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 1817 | const PointerType *Ty = dyn_cast<PointerType>($1->get()); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1818 | if (Ty == 0) |
Devang Patel | 5a97097 | 2008-02-19 22:27:01 +0000 | [diff] [blame] | 1819 | GEN_ERROR("Global const reference must be a pointer type " + (*$1)->getDescription()); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1820 | |
| 1821 | // ConstExprs can exist in the body of a function, thus creating |
| 1822 | // GlobalValues whenever they refer to a variable. Because we are in |
Reid Spencer | 93c4003 | 2007-03-19 18:40:50 +0000 | [diff] [blame] | 1823 | // the context of a function, getExistingVal will search the functions |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1824 | // symbol table instead of the module symbol table for the global symbol, |
| 1825 | // which throws things all off. To get around this, we just tell |
Reid Spencer | 93c4003 | 2007-03-19 18:40:50 +0000 | [diff] [blame] | 1826 | // getExistingVal that we are at global scope here. |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1827 | // |
| 1828 | Function *SavedCurFn = CurFun.CurrentFunction; |
| 1829 | CurFun.CurrentFunction = 0; |
| 1830 | |
Reid Spencer | 93c4003 | 2007-03-19 18:40:50 +0000 | [diff] [blame] | 1831 | Value *V = getExistingVal(Ty, $2); |
Reid Spencer | 5b7e753 | 2006-09-28 19:28:24 +0000 | [diff] [blame] | 1832 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1833 | |
| 1834 | CurFun.CurrentFunction = SavedCurFn; |
| 1835 | |
| 1836 | // If this is an initializer for a constant pointer, which is referencing a |
| 1837 | // (currently) undefined variable, create a stub now that shall be replaced |
| 1838 | // in the future with the right type of variable. |
| 1839 | // |
| 1840 | if (V == 0) { |
Reid Spencer | a9720f5 | 2007-02-05 17:04:00 +0000 | [diff] [blame] | 1841 | assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!"); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1842 | const PointerType *PT = cast<PointerType>(Ty); |
| 1843 | |
| 1844 | // First check to see if the forward references value is already created! |
| 1845 | PerModuleInfo::GlobalRefsType::iterator I = |
| 1846 | CurModule.GlobalRefs.find(std::make_pair(PT, $2)); |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 1847 | |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1848 | if (I != CurModule.GlobalRefs.end()) { |
| 1849 | V = I->second; // Placeholder already exists, use it... |
| 1850 | $2.destroy(); |
| 1851 | } else { |
| 1852 | std::string Name; |
Reid Spencer | 41dff5e | 2007-01-26 08:05:27 +0000 | [diff] [blame] | 1853 | if ($2.Type == ValID::GlobalName) |
Reid Spencer | 0a8a16b | 2007-05-22 18:52:55 +0000 | [diff] [blame] | 1854 | Name = $2.getName(); |
Reid Spencer | 41dff5e | 2007-01-26 08:05:27 +0000 | [diff] [blame] | 1855 | else if ($2.Type != ValID::GlobalID) |
| 1856 | GEN_ERROR("Invalid reference to global"); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1857 | |
| 1858 | // Create the forward referenced global. |
| 1859 | GlobalValue *GV; |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 1860 | if (const FunctionType *FTy = |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1861 | dyn_cast<FunctionType>(PT->getElementType())) { |
Gabor Greif | e64d248 | 2008-04-06 23:07:54 +0000 | [diff] [blame] | 1862 | GV = Function::Create(FTy, GlobalValue::ExternalWeakLinkage, Name, |
| 1863 | CurModule.CurrentModule); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1864 | } else { |
| 1865 | GV = new GlobalVariable(PT->getElementType(), false, |
Chris Lattner | 6cdc682 | 2007-04-26 05:31:05 +0000 | [diff] [blame] | 1866 | GlobalValue::ExternalWeakLinkage, 0, |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1867 | Name, CurModule.CurrentModule); |
| 1868 | } |
| 1869 | |
| 1870 | // Keep track of the fact that we have a forward ref to recycle it |
| 1871 | CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV)); |
| 1872 | V = GV; |
| 1873 | } |
| 1874 | } |
| 1875 | |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 1876 | $$ = cast<GlobalValue>(V); |
| 1877 | delete $1; // Free the type handle |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 1878 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1879 | } |
| 1880 | | Types ConstExpr { |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 1881 | if (!UpRefs.empty()) |
| 1882 | GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription()); |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 1883 | if ($1->get() != $2->getType()) |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 1884 | GEN_ERROR("Mismatched types for constant expression: " + |
Reid Spencer | e68853b | 2007-01-04 00:06:14 +0000 | [diff] [blame] | 1885 | (*$1)->getDescription() + " and " + $2->getType()->getDescription()); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1886 | $$ = $2; |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 1887 | delete $1; |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 1888 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1889 | } |
| 1890 | | Types ZEROINITIALIZER { |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 1891 | if (!UpRefs.empty()) |
| 1892 | GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription()); |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 1893 | const Type *Ty = $1->get(); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1894 | if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty)) |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 1895 | GEN_ERROR("Cannot create a null initialized value of this type"); |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 1896 | $$ = Constant::getNullValue(Ty); |
| 1897 | delete $1; |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 1898 | CHECK_FOR_ERROR |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 1899 | } |
Chris Lattner | 740e709 | 2008-10-15 06:16:57 +0000 | [diff] [blame] | 1900 | | Types ESINT64VAL { // integral constants |
| 1901 | if (IntegerType *IT = dyn_cast<IntegerType>($1->get())) { |
| 1902 | if (!ConstantInt::isValueValidForType(IT, $2)) |
| 1903 | GEN_ERROR("Constant value doesn't fit in type"); |
| 1904 | $$ = ConstantInt::get(IT, $2, true); |
| 1905 | } else { |
| 1906 | GEN_ERROR("integer constant must have integer type"); |
| 1907 | } |
| 1908 | delete $1; |
Reid Spencer | 38c91a9 | 2007-02-28 02:24:54 +0000 | [diff] [blame] | 1909 | CHECK_FOR_ERROR |
| 1910 | } |
Chris Lattner | 740e709 | 2008-10-15 06:16:57 +0000 | [diff] [blame] | 1911 | | Types ESAPINTVAL { // arbitrary precision integer constants |
| 1912 | if (IntegerType *IT = dyn_cast<IntegerType>($1->get())) { |
| 1913 | if ($2->getBitWidth() > IT->getBitWidth()) |
| 1914 | GEN_ERROR("Constant value does not fit in type"); |
| 1915 | $2->sextOrTrunc(IT->getBitWidth()); |
| 1916 | $$ = ConstantInt::get(*$2); |
| 1917 | } else { |
| 1918 | GEN_ERROR("integer constant must have integer type"); |
Reid Spencer | 1079427 | 2007-03-01 19:41:47 +0000 | [diff] [blame] | 1919 | } |
Chris Lattner | 740e709 | 2008-10-15 06:16:57 +0000 | [diff] [blame] | 1920 | delete $1; |
Reid Spencer | 38c91a9 | 2007-02-28 02:24:54 +0000 | [diff] [blame] | 1921 | delete $2; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1922 | CHECK_FOR_ERROR |
| 1923 | } |
Chris Lattner | 740e709 | 2008-10-15 06:16:57 +0000 | [diff] [blame] | 1924 | | Types EUINT64VAL { // integral constants |
| 1925 | if (IntegerType *IT = dyn_cast<IntegerType>($1->get())) { |
| 1926 | if (!ConstantInt::isValueValidForType(IT, $2)) |
| 1927 | GEN_ERROR("Constant value doesn't fit in type"); |
| 1928 | $$ = ConstantInt::get(IT, $2, false); |
| 1929 | } else { |
| 1930 | GEN_ERROR("integer constant must have integer type"); |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 1931 | } |
Chris Lattner | 740e709 | 2008-10-15 06:16:57 +0000 | [diff] [blame] | 1932 | delete $1; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1933 | CHECK_FOR_ERROR |
| 1934 | } |
Chris Lattner | 740e709 | 2008-10-15 06:16:57 +0000 | [diff] [blame] | 1935 | | Types EUAPINTVAL { // arbitrary precision integer constants |
| 1936 | if (IntegerType *IT = dyn_cast<IntegerType>($1->get())) { |
| 1937 | if ($2->getBitWidth() > IT->getBitWidth()) |
| 1938 | GEN_ERROR("Constant value does not fit in type"); |
| 1939 | $2->zextOrTrunc(IT->getBitWidth()); |
| 1940 | $$ = ConstantInt::get(*$2); |
| 1941 | } else { |
| 1942 | GEN_ERROR("integer constant must have integer type"); |
| 1943 | } |
| 1944 | |
| 1945 | delete $2; |
| 1946 | delete $1; |
| 1947 | CHECK_FOR_ERROR |
| 1948 | } |
| 1949 | | Types TRUETOK { // Boolean constants |
| 1950 | if ($1->get() != Type::Int1Ty) |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1951 | GEN_ERROR("Constant true must have type i1"); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1952 | $$ = ConstantInt::getTrue(); |
Chris Lattner | 740e709 | 2008-10-15 06:16:57 +0000 | [diff] [blame] | 1953 | delete $1; |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 1954 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1955 | } |
Chris Lattner | 740e709 | 2008-10-15 06:16:57 +0000 | [diff] [blame] | 1956 | | Types FALSETOK { // Boolean constants |
| 1957 | if ($1->get() != Type::Int1Ty) |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1958 | GEN_ERROR("Constant false must have type i1"); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1959 | $$ = ConstantInt::getFalse(); |
Chris Lattner | 740e709 | 2008-10-15 06:16:57 +0000 | [diff] [blame] | 1960 | delete $1; |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 1961 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1962 | } |
Chris Lattner | 740e709 | 2008-10-15 06:16:57 +0000 | [diff] [blame] | 1963 | | Types FPVAL { // Floating point constants |
| 1964 | if (!ConstantFP::isValueValidForType($1->get(), *$2)) |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 1965 | GEN_ERROR("Floating point constant invalid for type"); |
Chris Lattner | 740e709 | 2008-10-15 06:16:57 +0000 | [diff] [blame] | 1966 | |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 1967 | // Lexer has no type info, so builds all float and double FP constants |
Dale Johannesen | c72cd7e | 2007-09-11 18:33:39 +0000 | [diff] [blame] | 1968 | // as double. Fix this here. Long double is done right. |
Chris Lattner | 740e709 | 2008-10-15 06:16:57 +0000 | [diff] [blame] | 1969 | if (&$2->getSemantics()==&APFloat::IEEEdouble && $1->get()==Type::FloatTy) { |
Dale Johannesen | 236bbd4 | 2008-10-09 23:01:34 +0000 | [diff] [blame] | 1970 | bool ignored; |
| 1971 | $2->convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, |
| 1972 | &ignored); |
| 1973 | } |
Chris Lattner | d8eb63f | 2008-04-20 00:41:19 +0000 | [diff] [blame] | 1974 | $$ = ConstantFP::get(*$2); |
Chris Lattner | 740e709 | 2008-10-15 06:16:57 +0000 | [diff] [blame] | 1975 | delete $1; |
Dale Johannesen | cdd509a | 2007-09-07 21:07:57 +0000 | [diff] [blame] | 1976 | delete $2; |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 1977 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1978 | }; |
| 1979 | |
| 1980 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1981 | ConstExpr: CastOps '(' ConstVal TO Types ')' { |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 1982 | if (!UpRefs.empty()) |
| 1983 | GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription()); |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 1984 | Constant *Val = $3; |
Reid Spencer | b0fcf8f | 2007-01-17 02:48:45 +0000 | [diff] [blame] | 1985 | const Type *DestTy = $5->get(); |
| 1986 | if (!CastInst::castIsValid($1, $3, DestTy)) |
| 1987 | GEN_ERROR("invalid cast opcode for cast from '" + |
| 1988 | Val->getType()->getDescription() + "' to '" + |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 1989 | DestTy->getDescription() + "'"); |
Reid Spencer | b0fcf8f | 2007-01-17 02:48:45 +0000 | [diff] [blame] | 1990 | $$ = ConstantExpr::getCast($1, $3, DestTy); |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 1991 | delete $5; |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1992 | } |
| 1993 | | GETELEMENTPTR '(' ConstVal IndexList ')' { |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 1994 | if (!isa<PointerType>($3->getType())) |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 1995 | GEN_ERROR("GetElementPtr requires a pointer operand"); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 1996 | |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 1997 | const Type *IdxTy = |
Dan Gohman | 041e2eb | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1998 | GetElementPtrInst::getIndexedType($3->getType(), $4->begin(), $4->end()); |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 1999 | if (!IdxTy) |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 2000 | GEN_ERROR("Index list invalid for constant getelementptr"); |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 2001 | |
Chris Lattner | f7469af | 2007-01-31 04:44:08 +0000 | [diff] [blame] | 2002 | SmallVector<Constant*, 8> IdxVec; |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 2003 | for (unsigned i = 0, e = $4->size(); i != e; ++i) |
| 2004 | if (Constant *C = dyn_cast<Constant>((*$4)[i])) |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2005 | IdxVec.push_back(C); |
| 2006 | else |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 2007 | GEN_ERROR("Indices to constant getelementptr must be constants"); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2008 | |
| 2009 | delete $4; |
| 2010 | |
Chris Lattner | f7469af | 2007-01-31 04:44:08 +0000 | [diff] [blame] | 2011 | $$ = ConstantExpr::getGetElementPtr($3, &IdxVec[0], IdxVec.size()); |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 2012 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2013 | } |
| 2014 | | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' { |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 2015 | if ($3->getType() != Type::Int1Ty) |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 2016 | GEN_ERROR("Select condition must be of boolean type"); |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 2017 | if ($5->getType() != $7->getType()) |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 2018 | GEN_ERROR("Select operand types must match"); |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 2019 | $$ = ConstantExpr::getSelect($3, $5, $7); |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 2020 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2021 | } |
| 2022 | | ArithmeticOps '(' ConstVal ',' ConstVal ')' { |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 2023 | if ($3->getType() != $5->getType()) |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 2024 | GEN_ERROR("Binary operator types must match"); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2025 | CHECK_FOR_ERROR; |
Reid Spencer | 9eef56f | 2006-12-05 19:16:11 +0000 | [diff] [blame] | 2026 | $$ = ConstantExpr::get($1, $3, $5); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2027 | } |
| 2028 | | LogicalOps '(' ConstVal ',' ConstVal ')' { |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 2029 | if ($3->getType() != $5->getType()) |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 2030 | GEN_ERROR("Logical operator types must match"); |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 2031 | if (!$3->getType()->isInteger()) { |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 2032 | if (!isa<VectorType>($3->getType()) || |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 2033 | !cast<VectorType>($3->getType())->getElementType()->isInteger()) |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 2034 | GEN_ERROR("Logical operator requires integral operands"); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2035 | } |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 2036 | $$ = ConstantExpr::get($1, $3, $5); |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 2037 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2038 | } |
Reid Spencer | 4012e83 | 2006-12-04 05:24:24 +0000 | [diff] [blame] | 2039 | | ICMP IPredicates '(' ConstVal ',' ConstVal ')' { |
| 2040 | if ($4->getType() != $6->getType()) |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 2041 | GEN_ERROR("icmp operand types must match"); |
Reid Spencer | 4012e83 | 2006-12-04 05:24:24 +0000 | [diff] [blame] | 2042 | $$ = ConstantExpr::getICmp($2, $4, $6); |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 2043 | } |
Reid Spencer | 4012e83 | 2006-12-04 05:24:24 +0000 | [diff] [blame] | 2044 | | FCMP FPredicates '(' ConstVal ',' ConstVal ')' { |
| 2045 | if ($4->getType() != $6->getType()) |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 2046 | GEN_ERROR("fcmp operand types must match"); |
Reid Spencer | 4012e83 | 2006-12-04 05:24:24 +0000 | [diff] [blame] | 2047 | $$ = ConstantExpr::getFCmp($2, $4, $6); |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 2048 | } |
Nate Begeman | ac80ade | 2008-05-12 19:01:56 +0000 | [diff] [blame] | 2049 | | VICMP IPredicates '(' ConstVal ',' ConstVal ')' { |
| 2050 | if ($4->getType() != $6->getType()) |
| 2051 | GEN_ERROR("vicmp operand types must match"); |
| 2052 | $$ = ConstantExpr::getVICmp($2, $4, $6); |
| 2053 | } |
| 2054 | | VFCMP FPredicates '(' ConstVal ',' ConstVal ')' { |
| 2055 | if ($4->getType() != $6->getType()) |
| 2056 | GEN_ERROR("vfcmp operand types must match"); |
| 2057 | $$ = ConstantExpr::getVFCmp($2, $4, $6); |
| 2058 | } |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2059 | | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' { |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 2060 | if (!ExtractElementInst::isValidOperands($3, $5)) |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 2061 | GEN_ERROR("Invalid extractelement operands"); |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 2062 | $$ = ConstantExpr::getExtractElement($3, $5); |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 2063 | CHECK_FOR_ERROR |
Chris Lattner | d25db20 | 2006-04-08 03:55:17 +0000 | [diff] [blame] | 2064 | } |
| 2065 | | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' { |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 2066 | if (!InsertElementInst::isValidOperands($3, $5, $7)) |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 2067 | GEN_ERROR("Invalid insertelement operands"); |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 2068 | $$ = ConstantExpr::getInsertElement($3, $5, $7); |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 2069 | CHECK_FOR_ERROR |
Chris Lattner | d25db20 | 2006-04-08 03:55:17 +0000 | [diff] [blame] | 2070 | } |
| 2071 | | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' { |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 2072 | if (!ShuffleVectorInst::isValidOperands($3, $5, $7)) |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 2073 | GEN_ERROR("Invalid shufflevector operands"); |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 2074 | $$ = ConstantExpr::getShuffleVector($3, $5, $7); |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 2075 | CHECK_FOR_ERROR |
Dan Gohman | e4977cf | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 2076 | } |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 2077 | | EXTRACTVALUE '(' ConstVal ConstantIndexList ')' { |
Dan Gohman | e4977cf | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 2078 | if (!isa<StructType>($3->getType()) && !isa<ArrayType>($3->getType())) |
| 2079 | GEN_ERROR("ExtractValue requires an aggregate operand"); |
| 2080 | |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 2081 | $$ = ConstantExpr::getExtractValue($3, &(*$4)[0], $4->size()); |
Dan Gohman | e4977cf | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 2082 | delete $4; |
Dan Gohman | e4977cf | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 2083 | CHECK_FOR_ERROR |
| 2084 | } |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 2085 | | INSERTVALUE '(' ConstVal ',' ConstVal ConstantIndexList ')' { |
Dan Gohman | e4977cf | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 2086 | if (!isa<StructType>($3->getType()) && !isa<ArrayType>($3->getType())) |
| 2087 | GEN_ERROR("InsertValue requires an aggregate operand"); |
| 2088 | |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 2089 | $$ = ConstantExpr::getInsertValue($3, $5, &(*$6)[0], $6->size()); |
Dan Gohman | e4977cf | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 2090 | delete $6; |
Dan Gohman | e4977cf | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 2091 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2092 | }; |
| 2093 | |
Chris Lattner | d25db20 | 2006-04-08 03:55:17 +0000 | [diff] [blame] | 2094 | |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2095 | // ConstVector - A list of comma separated constants. |
| 2096 | ConstVector : ConstVector ',' ConstVal { |
| 2097 | ($$ = $1)->push_back($3); |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 2098 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2099 | } |
| 2100 | | ConstVal { |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 2101 | $$ = new std::vector<Constant*>(); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2102 | $$->push_back($1); |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 2103 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2104 | }; |
| 2105 | |
| 2106 | |
| 2107 | // GlobalType - Match either GLOBAL or CONSTANT for global declarations... |
| 2108 | GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; }; |
| 2109 | |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 2110 | // ThreadLocal |
Lauro Ramos Venancio | c763552 | 2007-04-12 18:32:50 +0000 | [diff] [blame] | 2111 | ThreadLocal : THREAD_LOCAL { $$ = true; } | { $$ = false; }; |
| 2112 | |
Anton Korobeynikov | 38e0980 | 2007-04-28 13:48:45 +0000 | [diff] [blame] | 2113 | // AliaseeRef - Match either GlobalValue or bitcast to GlobalValue. |
| 2114 | AliaseeRef : ResultTypes SymbolicValueRef { |
| 2115 | const Type* VTy = $1->get(); |
| 2116 | Value *V = getVal(VTy, $2); |
Chris Lattner | 0275cff | 2007-08-06 21:00:46 +0000 | [diff] [blame] | 2117 | CHECK_FOR_ERROR |
Anton Korobeynikov | 38e0980 | 2007-04-28 13:48:45 +0000 | [diff] [blame] | 2118 | GlobalValue* Aliasee = dyn_cast<GlobalValue>(V); |
| 2119 | if (!Aliasee) |
| 2120 | GEN_ERROR("Aliases can be created only to global values"); |
| 2121 | |
| 2122 | $$ = Aliasee; |
| 2123 | CHECK_FOR_ERROR |
| 2124 | delete $1; |
| 2125 | } |
| 2126 | | BITCAST '(' AliaseeRef TO Types ')' { |
| 2127 | Constant *Val = $3; |
| 2128 | const Type *DestTy = $5->get(); |
| 2129 | if (!CastInst::castIsValid($1, $3, DestTy)) |
| 2130 | GEN_ERROR("invalid cast opcode for cast from '" + |
| 2131 | Val->getType()->getDescription() + "' to '" + |
| 2132 | DestTy->getDescription() + "'"); |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 2133 | |
Anton Korobeynikov | 38e0980 | 2007-04-28 13:48:45 +0000 | [diff] [blame] | 2134 | $$ = ConstantExpr::getCast($1, $3, DestTy); |
| 2135 | CHECK_FOR_ERROR |
| 2136 | delete $5; |
| 2137 | }; |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2138 | |
| 2139 | //===----------------------------------------------------------------------===// |
| 2140 | // Rules to match Modules |
| 2141 | //===----------------------------------------------------------------------===// |
| 2142 | |
| 2143 | // Module rule: Capture the result of parsing the whole file into a result |
| 2144 | // variable... |
| 2145 | // |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 2146 | Module |
Reid Spencer | 6fd36ab | 2006-12-29 20:35:03 +0000 | [diff] [blame] | 2147 | : DefinitionList { |
| 2148 | $$ = ParserResult = CurModule.CurrentModule; |
| 2149 | CurModule.ModuleDone(); |
| 2150 | CHECK_FOR_ERROR; |
| 2151 | } |
| 2152 | | /*empty*/ { |
| 2153 | $$ = ParserResult = CurModule.CurrentModule; |
| 2154 | CurModule.ModuleDone(); |
| 2155 | CHECK_FOR_ERROR; |
| 2156 | } |
| 2157 | ; |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2158 | |
Reid Spencer | 6fd36ab | 2006-12-29 20:35:03 +0000 | [diff] [blame] | 2159 | DefinitionList |
| 2160 | : Definition |
| 2161 | | DefinitionList Definition |
| 2162 | ; |
| 2163 | |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 2164 | Definition |
Jeff Cohen | 361c3ef | 2007-01-21 19:19:31 +0000 | [diff] [blame] | 2165 | : DEFINE { CurFun.isDeclare = false; } Function { |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2166 | CurFun.FunctionDone(); |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 2167 | CHECK_FOR_ERROR |
Reid Spencer | 6fd36ab | 2006-12-29 20:35:03 +0000 | [diff] [blame] | 2168 | } |
| 2169 | | DECLARE { CurFun.isDeclare = true; } FunctionProto { |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 2170 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2171 | } |
Reid Spencer | 6fd36ab | 2006-12-29 20:35:03 +0000 | [diff] [blame] | 2172 | | MODULE ASM_TOK AsmBlock { |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 2173 | CHECK_FOR_ERROR |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 2174 | } |
Reid Spencer | 41dff5e | 2007-01-26 08:05:27 +0000 | [diff] [blame] | 2175 | | OptLocalAssign TYPE Types { |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 2176 | if (!UpRefs.empty()) |
| 2177 | GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription()); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2178 | // Eagerly resolve types. This is not an optimization, this is a |
| 2179 | // requirement that is due to the fact that we could have this: |
| 2180 | // |
| 2181 | // %list = type { %list * } |
| 2182 | // %list = type { %list * } ; repeated type decl |
| 2183 | // |
| 2184 | // If types are not resolved eagerly, then the two types will not be |
| 2185 | // determined to be the same type! |
| 2186 | // |
Reid Spencer | 6fd36ab | 2006-12-29 20:35:03 +0000 | [diff] [blame] | 2187 | ResolveTypeTo($1, *$3); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2188 | |
Reid Spencer | 6fd36ab | 2006-12-29 20:35:03 +0000 | [diff] [blame] | 2189 | if (!setTypeName(*$3, $1) && !$1) { |
Reid Spencer | 5b7e753 | 2006-09-28 19:28:24 +0000 | [diff] [blame] | 2190 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2191 | // If this is a named type that is not a redefinition, add it to the slot |
| 2192 | // table. |
Reid Spencer | 6fd36ab | 2006-12-29 20:35:03 +0000 | [diff] [blame] | 2193 | CurModule.Types.push_back(*$3); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2194 | } |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 2195 | |
Reid Spencer | 6fd36ab | 2006-12-29 20:35:03 +0000 | [diff] [blame] | 2196 | delete $3; |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 2197 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2198 | } |
Reid Spencer | 41dff5e | 2007-01-26 08:05:27 +0000 | [diff] [blame] | 2199 | | OptLocalAssign TYPE VOID { |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 2200 | ResolveTypeTo($1, $3); |
| 2201 | |
| 2202 | if (!setTypeName($3, $1) && !$1) { |
| 2203 | CHECK_FOR_ERROR |
| 2204 | // If this is a named type that is not a redefinition, add it to the slot |
| 2205 | // table. |
| 2206 | CurModule.Types.push_back($3); |
| 2207 | } |
| 2208 | CHECK_FOR_ERROR |
| 2209 | } |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 2210 | | OptGlobalAssign GVVisibilityStyle ThreadLocal GlobalType ConstVal |
| 2211 | OptAddrSpace { |
Reid Spencer | 41dff5e | 2007-01-26 08:05:27 +0000 | [diff] [blame] | 2212 | /* "Externally Visible" Linkage */ |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 2213 | if ($5 == 0) |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 2214 | GEN_ERROR("Global value initializer is not a constant"); |
Lauro Ramos Venancio | c763552 | 2007-04-12 18:32:50 +0000 | [diff] [blame] | 2215 | CurGV = ParseGlobalVariable($1, GlobalValue::ExternalLinkage, |
Christopher Lamb | bf3348d | 2007-12-12 08:45:45 +0000 | [diff] [blame] | 2216 | $2, $4, $5->getType(), $5, $3, $6); |
Christopher Lamb | a8ed9bf | 2007-12-11 09:02:08 +0000 | [diff] [blame] | 2217 | CHECK_FOR_ERROR |
| 2218 | } GlobalVarAttributes { |
| 2219 | CurGV = 0; |
| 2220 | } |
Chris Lattner | 6cdc682 | 2007-04-26 05:31:05 +0000 | [diff] [blame] | 2221 | | OptGlobalAssign GVInternalLinkage GVVisibilityStyle ThreadLocal GlobalType |
Christopher Lamb | bf3348d | 2007-12-12 08:45:45 +0000 | [diff] [blame] | 2222 | ConstVal OptAddrSpace { |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 2223 | if ($6 == 0) |
Lauro Ramos Venancio | c763552 | 2007-04-12 18:32:50 +0000 | [diff] [blame] | 2224 | GEN_ERROR("Global value initializer is not a constant"); |
Christopher Lamb | bf3348d | 2007-12-12 08:45:45 +0000 | [diff] [blame] | 2225 | CurGV = ParseGlobalVariable($1, $2, $3, $5, $6->getType(), $6, $4, $7); |
Anton Korobeynikov | 7f70559 | 2007-01-12 19:20:47 +0000 | [diff] [blame] | 2226 | CHECK_FOR_ERROR |
Lauro Ramos Venancio | c763552 | 2007-04-12 18:32:50 +0000 | [diff] [blame] | 2227 | } GlobalVarAttributes { |
| 2228 | CurGV = 0; |
| 2229 | } |
Chris Lattner | 6cdc682 | 2007-04-26 05:31:05 +0000 | [diff] [blame] | 2230 | | OptGlobalAssign GVExternalLinkage GVVisibilityStyle ThreadLocal GlobalType |
Christopher Lamb | bf3348d | 2007-12-12 08:45:45 +0000 | [diff] [blame] | 2231 | Types OptAddrSpace { |
Lauro Ramos Venancio | c763552 | 2007-04-12 18:32:50 +0000 | [diff] [blame] | 2232 | if (!UpRefs.empty()) |
| 2233 | GEN_ERROR("Invalid upreference in type: " + (*$6)->getDescription()); |
Christopher Lamb | bf3348d | 2007-12-12 08:45:45 +0000 | [diff] [blame] | 2234 | CurGV = ParseGlobalVariable($1, $2, $3, $5, *$6, 0, $4, $7); |
Lauro Ramos Venancio | c763552 | 2007-04-12 18:32:50 +0000 | [diff] [blame] | 2235 | CHECK_FOR_ERROR |
| 2236 | delete $6; |
Reid Spencer | 5b7e753 | 2006-09-28 19:28:24 +0000 | [diff] [blame] | 2237 | } GlobalVarAttributes { |
Anton Korobeynikov | b74ed07 | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 2238 | CurGV = 0; |
| 2239 | CHECK_FOR_ERROR |
| 2240 | } |
Anton Korobeynikov | 38e0980 | 2007-04-28 13:48:45 +0000 | [diff] [blame] | 2241 | | OptGlobalAssign GVVisibilityStyle ALIAS AliasLinkage AliaseeRef { |
Reid Spencer | 0a8a16b | 2007-05-22 18:52:55 +0000 | [diff] [blame] | 2242 | std::string Name; |
| 2243 | if ($1) { |
| 2244 | Name = *$1; |
| 2245 | delete $1; |
| 2246 | } |
Anton Korobeynikov | 77d0f97 | 2007-04-25 14:29:12 +0000 | [diff] [blame] | 2247 | if (Name.empty()) |
Anton Korobeynikov | 38e0980 | 2007-04-28 13:48:45 +0000 | [diff] [blame] | 2248 | GEN_ERROR("Alias name cannot be empty"); |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 2249 | |
Anton Korobeynikov | 38e0980 | 2007-04-28 13:48:45 +0000 | [diff] [blame] | 2250 | Constant* Aliasee = $5; |
| 2251 | if (Aliasee == 0) |
Reid Spencer | 0a8a16b | 2007-05-22 18:52:55 +0000 | [diff] [blame] | 2252 | GEN_ERROR(std::string("Invalid aliasee for alias: ") + Name); |
Anton Korobeynikov | 38e0980 | 2007-04-28 13:48:45 +0000 | [diff] [blame] | 2253 | |
| 2254 | GlobalAlias* GA = new GlobalAlias(Aliasee->getType(), $4, Name, Aliasee, |
| 2255 | CurModule.CurrentModule); |
| 2256 | GA->setVisibility($2); |
| 2257 | InsertValue(GA, CurModule.Values); |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 2258 | |
| 2259 | |
Chris Lattner | 569f737 | 2007-09-10 23:24:14 +0000 | [diff] [blame] | 2260 | // If there was a forward reference of this alias, resolve it now. |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 2261 | |
Chris Lattner | 569f737 | 2007-09-10 23:24:14 +0000 | [diff] [blame] | 2262 | ValID ID; |
| 2263 | if (!Name.empty()) |
| 2264 | ID = ValID::createGlobalName(Name); |
| 2265 | else |
| 2266 | ID = ValID::createGlobalID(CurModule.Values.size()-1); |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 2267 | |
Chris Lattner | 569f737 | 2007-09-10 23:24:14 +0000 | [diff] [blame] | 2268 | if (GlobalValue *FWGV = |
| 2269 | CurModule.GetForwardRefForGlobal(GA->getType(), ID)) { |
| 2270 | // Replace uses of the fwdref with the actual alias. |
| 2271 | FWGV->replaceAllUsesWith(GA); |
| 2272 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(FWGV)) |
| 2273 | GV->eraseFromParent(); |
| 2274 | else |
| 2275 | cast<Function>(FWGV)->eraseFromParent(); |
| 2276 | } |
| 2277 | ID.destroy(); |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 2278 | |
Anton Korobeynikov | 77d0f97 | 2007-04-25 14:29:12 +0000 | [diff] [blame] | 2279 | CHECK_FOR_ERROR |
Anton Korobeynikov | 77d0f97 | 2007-04-25 14:29:12 +0000 | [diff] [blame] | 2280 | } |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 2281 | | TARGET TargetDefinition { |
Anton Korobeynikov | b74ed07 | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 2282 | CHECK_FOR_ERROR |
| 2283 | } |
Reid Spencer | 6fd36ab | 2006-12-29 20:35:03 +0000 | [diff] [blame] | 2284 | | DEPLIBS '=' LibrariesDefinition { |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 2285 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2286 | } |
Reid Spencer | 6fd36ab | 2006-12-29 20:35:03 +0000 | [diff] [blame] | 2287 | ; |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2288 | |
| 2289 | |
| 2290 | AsmBlock : STRINGCONSTANT { |
| 2291 | const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm(); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2292 | if (AsmSoFar.empty()) |
Reid Spencer | 0a8a16b | 2007-05-22 18:52:55 +0000 | [diff] [blame] | 2293 | CurModule.CurrentModule->setModuleInlineAsm(*$1); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2294 | else |
Reid Spencer | 0a8a16b | 2007-05-22 18:52:55 +0000 | [diff] [blame] | 2295 | CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+*$1); |
| 2296 | delete $1; |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 2297 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2298 | }; |
| 2299 | |
Reid Spencer | 41dff5e | 2007-01-26 08:05:27 +0000 | [diff] [blame] | 2300 | TargetDefinition : TRIPLE '=' STRINGCONSTANT { |
Reid Spencer | 0a8a16b | 2007-05-22 18:52:55 +0000 | [diff] [blame] | 2301 | CurModule.CurrentModule->setTargetTriple(*$3); |
| 2302 | delete $3; |
John Criswell | 2f6a8b1 | 2006-10-24 19:09:48 +0000 | [diff] [blame] | 2303 | } |
Chris Lattner | 1ae022f | 2006-10-22 06:08:13 +0000 | [diff] [blame] | 2304 | | DATALAYOUT '=' STRINGCONSTANT { |
Reid Spencer | 0a8a16b | 2007-05-22 18:52:55 +0000 | [diff] [blame] | 2305 | CurModule.CurrentModule->setDataLayout(*$3); |
| 2306 | delete $3; |
Owen Anderson | 1dc6969 | 2006-10-18 02:21:48 +0000 | [diff] [blame] | 2307 | }; |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2308 | |
| 2309 | LibrariesDefinition : '[' LibList ']'; |
| 2310 | |
| 2311 | LibList : LibList ',' STRINGCONSTANT { |
Reid Spencer | 0a8a16b | 2007-05-22 18:52:55 +0000 | [diff] [blame] | 2312 | CurModule.CurrentModule->addLibrary(*$3); |
| 2313 | delete $3; |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 2314 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2315 | } |
| 2316 | | STRINGCONSTANT { |
Reid Spencer | 0a8a16b | 2007-05-22 18:52:55 +0000 | [diff] [blame] | 2317 | CurModule.CurrentModule->addLibrary(*$1); |
| 2318 | delete $1; |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 2319 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2320 | } |
| 2321 | | /* empty: end of list */ { |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 2322 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2323 | } |
| 2324 | ; |
| 2325 | |
| 2326 | //===----------------------------------------------------------------------===// |
| 2327 | // Rules to match Function Headers |
| 2328 | //===----------------------------------------------------------------------===// |
| 2329 | |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 2330 | ArgListH : ArgListH ',' Types OptAttributes OptLocalName { |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 2331 | if (!UpRefs.empty()) |
| 2332 | GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription()); |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 2333 | if (!(*$3)->isFirstClassType()) |
| 2334 | GEN_ERROR("Argument types must be first-class"); |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 2335 | ArgListEntry E; E.Attrs = $4; E.Ty = $3; E.Name = $5; |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2336 | $$ = $1; |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 2337 | $1->push_back(E); |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 2338 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2339 | } |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 2340 | | Types OptAttributes OptLocalName { |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 2341 | if (!UpRefs.empty()) |
| 2342 | GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription()); |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 2343 | if (!(*$1)->isFirstClassType()) |
| 2344 | GEN_ERROR("Argument types must be first-class"); |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 2345 | ArgListEntry E; E.Attrs = $2; E.Ty = $1; E.Name = $3; |
| 2346 | $$ = new ArgListType; |
| 2347 | $$->push_back(E); |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 2348 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2349 | }; |
| 2350 | |
| 2351 | ArgList : ArgListH { |
| 2352 | $$ = $1; |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 2353 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2354 | } |
| 2355 | | ArgListH ',' DOTDOTDOT { |
| 2356 | $$ = $1; |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 2357 | struct ArgListEntry E; |
| 2358 | E.Ty = new PATypeHolder(Type::VoidTy); |
| 2359 | E.Name = 0; |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 2360 | E.Attrs = Attribute::None; |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 2361 | $$->push_back(E); |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 2362 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2363 | } |
| 2364 | | DOTDOTDOT { |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 2365 | $$ = new ArgListType; |
| 2366 | struct ArgListEntry E; |
| 2367 | E.Ty = new PATypeHolder(Type::VoidTy); |
| 2368 | E.Name = 0; |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 2369 | E.Attrs = Attribute::None; |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 2370 | $$->push_back(E); |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 2371 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2372 | } |
| 2373 | | /* empty */ { |
| 2374 | $$ = 0; |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 2375 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2376 | }; |
| 2377 | |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 2378 | FunctionHeaderH : OptCallingConv OptRetAttrs ResultTypes GlobalName '(' ArgList ')' |
Devang Patel | 2c9c3e7 | 2008-09-26 23:51:19 +0000 | [diff] [blame] | 2379 | OptFuncAttrs OptSection OptAlign OptGC { |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 2380 | std::string FunctionName(*$4); |
| 2381 | delete $4; // Free strdup'd memory! |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 2382 | |
Reid Spencer | 8c8a2dc | 2007-01-02 21:54:12 +0000 | [diff] [blame] | 2383 | // Check the function result for abstractness if this is a define. We should |
| 2384 | // have no abstract types at this point |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 2385 | if (!CurFun.isDeclare && CurModule.TypeIsUnresolved($3)) |
| 2386 | GEN_ERROR("Reference to abstract result: "+ $3->get()->getDescription()); |
Reid Spencer | 8c8a2dc | 2007-01-02 21:54:12 +0000 | [diff] [blame] | 2387 | |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 2388 | if (!FunctionType::isValidReturnType(*$3)) |
Chris Lattner | a925a14 | 2008-04-23 05:37:08 +0000 | [diff] [blame] | 2389 | GEN_ERROR("Invalid result type for LLVM function"); |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 2390 | |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2391 | std::vector<const Type*> ParamTypeList; |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 2392 | SmallVector<AttributeWithIndex, 8> Attrs; |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 2393 | //FIXME : In 3.0, stop accepting zext, sext and inreg as optional function |
| 2394 | //attributes. |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 2395 | Attributes RetAttrs = $2; |
| 2396 | if ($8 != Attribute::None) { |
| 2397 | if ($8 & Attribute::ZExt) { |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 2398 | RetAttrs = RetAttrs | Attribute::ZExt; |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 2399 | $8 = $8 ^ Attribute::ZExt; |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 2400 | } |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 2401 | if ($8 & Attribute::SExt) { |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 2402 | RetAttrs = RetAttrs | Attribute::SExt; |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 2403 | $8 = $8 ^ Attribute::SExt; |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 2404 | } |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 2405 | if ($8 & Attribute::InReg) { |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 2406 | RetAttrs = RetAttrs | Attribute::InReg; |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 2407 | $8 = $8 ^ Attribute::InReg; |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 2408 | } |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 2409 | } |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 2410 | if (RetAttrs != Attribute::None) |
| 2411 | Attrs.push_back(AttributeWithIndex::get(0, RetAttrs)); |
| 2412 | if ($6) { // If there are arguments... |
Reid Spencer | 7b5d466 | 2007-04-09 06:16:21 +0000 | [diff] [blame] | 2413 | unsigned index = 1; |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 2414 | for (ArgListType::iterator I = $6->begin(); I != $6->end(); ++I, ++index) { |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 2415 | const Type* Ty = I->Ty->get(); |
Reid Spencer | 8c8a2dc | 2007-01-02 21:54:12 +0000 | [diff] [blame] | 2416 | if (!CurFun.isDeclare && CurModule.TypeIsUnresolved(I->Ty)) |
| 2417 | GEN_ERROR("Reference to abstract argument: " + Ty->getDescription()); |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 2418 | ParamTypeList.push_back(Ty); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 2419 | if (Ty != Type::VoidTy && I->Attrs != Attribute::None) |
| 2420 | Attrs.push_back(AttributeWithIndex::get(index, I->Attrs)); |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 2421 | } |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2422 | } |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 2423 | if ($8 != Attribute::None) |
| 2424 | Attrs.push_back(AttributeWithIndex::get(~0, $8)); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2425 | |
| 2426 | bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy; |
| 2427 | if (isVarArg) ParamTypeList.pop_back(); |
| 2428 | |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 2429 | AttrListPtr PAL; |
Christopher Lamb | 5c10424 | 2007-04-22 20:09:11 +0000 | [diff] [blame] | 2430 | if (!Attrs.empty()) |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 2431 | PAL = AttrListPtr::get(Attrs.begin(), Attrs.end()); |
Reid Spencer | 7b5d466 | 2007-04-09 06:16:21 +0000 | [diff] [blame] | 2432 | |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 2433 | FunctionType *FT = FunctionType::get(*$3, ParamTypeList, isVarArg); |
Christopher Lamb | 4374f8e | 2007-12-17 01:17:35 +0000 | [diff] [blame] | 2434 | const PointerType *PFT = PointerType::getUnqual(FT); |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 2435 | delete $3; |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2436 | |
| 2437 | ValID ID; |
| 2438 | if (!FunctionName.empty()) { |
Reid Spencer | 41dff5e | 2007-01-26 08:05:27 +0000 | [diff] [blame] | 2439 | ID = ValID::createGlobalName((char*)FunctionName.c_str()); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2440 | } else { |
Reid Spencer | 93c4003 | 2007-03-19 18:40:50 +0000 | [diff] [blame] | 2441 | ID = ValID::createGlobalID(CurModule.Values.size()); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2442 | } |
| 2443 | |
| 2444 | Function *Fn = 0; |
| 2445 | // See if this function was forward referenced. If so, recycle the object. |
| 2446 | if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) { |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 2447 | // Move the function to the end of the list, from whereever it was |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2448 | // previously inserted. |
| 2449 | Fn = cast<Function>(FWRef); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 2450 | assert(Fn->getAttributes().isEmpty() && |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 2451 | "Forward reference has parameter attributes!"); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2452 | CurModule.CurrentModule->getFunctionList().remove(Fn); |
| 2453 | CurModule.CurrentModule->getFunctionList().push_back(Fn); |
| 2454 | } else if (!FunctionName.empty() && // Merge with an earlier prototype? |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 2455 | (Fn = CurModule.CurrentModule->getFunction(FunctionName))) { |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 2456 | if (Fn->getFunctionType() != FT ) { |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 2457 | // The existing function doesn't have the same type. This is an overload |
| 2458 | // error. |
| 2459 | GEN_ERROR("Overload of function '" + FunctionName + "' not permitted."); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 2460 | } else if (Fn->getAttributes() != PAL) { |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 2461 | // The existing function doesn't have the same parameter attributes. |
| 2462 | // This is an overload error. |
| 2463 | GEN_ERROR("Overload of function '" + FunctionName + "' not permitted."); |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 2464 | } else if (!CurFun.isDeclare && !Fn->isDeclaration()) { |
Chris Lattner | 6cdc682 | 2007-04-26 05:31:05 +0000 | [diff] [blame] | 2465 | // Neither the existing or the current function is a declaration and they |
| 2466 | // have the same name and same type. Clearly this is a redefinition. |
| 2467 | GEN_ERROR("Redefinition of function '" + FunctionName + "'"); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 2468 | } else if (Fn->isDeclaration()) { |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 2469 | // Make sure to strip off any argument names so we can't get conflicts. |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2470 | for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end(); |
| 2471 | AI != AE; ++AI) |
| 2472 | AI->setName(""); |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 2473 | } |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2474 | } else { // Not already defined? |
Gabor Greif | e64d248 | 2008-04-06 23:07:54 +0000 | [diff] [blame] | 2475 | Fn = Function::Create(FT, GlobalValue::ExternalWeakLinkage, FunctionName, |
| 2476 | CurModule.CurrentModule); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2477 | InsertValue(Fn, CurModule.Values); |
| 2478 | } |
| 2479 | |
Nuno Lopes | 9e9631d | 2008-10-03 15:45:58 +0000 | [diff] [blame] | 2480 | ID.destroy(); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2481 | CurFun.FunctionStart(Fn); |
Anton Korobeynikov | 93c2b37 | 2006-09-17 13:06:18 +0000 | [diff] [blame] | 2482 | |
| 2483 | if (CurFun.isDeclare) { |
| 2484 | // If we have declaration, always overwrite linkage. This will allow us to |
| 2485 | // correctly handle cases, when pointer to function is passed as argument to |
| 2486 | // another function. |
| 2487 | Fn->setLinkage(CurFun.Linkage); |
Anton Korobeynikov | 7f70559 | 2007-01-12 19:20:47 +0000 | [diff] [blame] | 2488 | Fn->setVisibility(CurFun.Visibility); |
Anton Korobeynikov | 93c2b37 | 2006-09-17 13:06:18 +0000 | [diff] [blame] | 2489 | } |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2490 | Fn->setCallingConv($1); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 2491 | Fn->setAttributes(PAL); |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 2492 | Fn->setAlignment($10); |
| 2493 | if ($9) { |
| 2494 | Fn->setSection(*$9); |
| 2495 | delete $9; |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2496 | } |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 2497 | if ($11) { |
| 2498 | Fn->setGC($11->c_str()); |
| 2499 | delete $11; |
Gordon Henriksen | 80a75bf | 2007-12-10 03:18:06 +0000 | [diff] [blame] | 2500 | } |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2501 | |
| 2502 | // Add all of the arguments we parsed to the function... |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 2503 | if ($6) { // Is null if empty... |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2504 | if (isVarArg) { // Nuke the last entry |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 2505 | assert($6->back().Ty->get() == Type::VoidTy && $6->back().Name == 0 && |
Reid Spencer | a9720f5 | 2007-02-05 17:04:00 +0000 | [diff] [blame] | 2506 | "Not a varargs marker!"); |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 2507 | delete $6->back().Ty; |
| 2508 | $6->pop_back(); // Delete the last entry |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2509 | } |
| 2510 | Function::arg_iterator ArgIt = Fn->arg_begin(); |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 2511 | Function::arg_iterator ArgEnd = Fn->arg_end(); |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 2512 | unsigned Idx = 1; |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 2513 | for (ArgListType::iterator I = $6->begin(); |
| 2514 | I != $6->end() && ArgIt != ArgEnd; ++I, ++ArgIt) { |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 2515 | delete I->Ty; // Delete the typeholder... |
Reid Spencer | 0a8a16b | 2007-05-22 18:52:55 +0000 | [diff] [blame] | 2516 | setValueName(ArgIt, I->Name); // Insert arg into symtab... |
Reid Spencer | 5b7e753 | 2006-09-28 19:28:24 +0000 | [diff] [blame] | 2517 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2518 | InsertValue(ArgIt); |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 2519 | Idx++; |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2520 | } |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 2521 | |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 2522 | delete $6; // We're now done with the argument list |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2523 | } |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 2524 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2525 | }; |
| 2526 | |
| 2527 | BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function |
| 2528 | |
Anton Korobeynikov | 7f70559 | 2007-01-12 19:20:47 +0000 | [diff] [blame] | 2529 | FunctionHeader : FunctionDefineLinkage GVVisibilityStyle FunctionHeaderH BEGIN { |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2530 | $$ = CurFun.CurrentFunction; |
| 2531 | |
| 2532 | // Make sure that we keep track of the linkage type even if there was a |
| 2533 | // previous "declare". |
| 2534 | $$->setLinkage($1); |
Anton Korobeynikov | 7f70559 | 2007-01-12 19:20:47 +0000 | [diff] [blame] | 2535 | $$->setVisibility($2); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2536 | }; |
| 2537 | |
| 2538 | END : ENDTOK | '}'; // Allow end of '}' to end a function |
| 2539 | |
| 2540 | Function : BasicBlockList END { |
| 2541 | $$ = $1; |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 2542 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2543 | }; |
| 2544 | |
Anton Korobeynikov | 7f70559 | 2007-01-12 19:20:47 +0000 | [diff] [blame] | 2545 | FunctionProto : FunctionDeclareLinkage GVVisibilityStyle FunctionHeaderH { |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 2546 | CurFun.CurrentFunction->setLinkage($1); |
Anton Korobeynikov | 7f70559 | 2007-01-12 19:20:47 +0000 | [diff] [blame] | 2547 | CurFun.CurrentFunction->setVisibility($2); |
Anton Korobeynikov | b74ed07 | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 2548 | $$ = CurFun.CurrentFunction; |
| 2549 | CurFun.FunctionDone(); |
| 2550 | CHECK_FOR_ERROR |
| 2551 | }; |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2552 | |
| 2553 | //===----------------------------------------------------------------------===// |
| 2554 | // Rules to match Basic Blocks |
| 2555 | //===----------------------------------------------------------------------===// |
| 2556 | |
| 2557 | OptSideEffect : /* empty */ { |
| 2558 | $$ = false; |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 2559 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2560 | } |
| 2561 | | SIDEEFFECT { |
| 2562 | $$ = true; |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 2563 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2564 | }; |
| 2565 | |
| 2566 | ConstValueRef : ESINT64VAL { // A reference to a direct constant |
| 2567 | $$ = ValID::create($1); |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 2568 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2569 | } |
| 2570 | | EUINT64VAL { |
| 2571 | $$ = ValID::create($1); |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 2572 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2573 | } |
Chris Lattner | 1913b94 | 2008-07-11 00:30:39 +0000 | [diff] [blame] | 2574 | | ESAPINTVAL { // arbitrary precision integer constants |
| 2575 | $$ = ValID::create(*$1, true); |
| 2576 | delete $1; |
| 2577 | CHECK_FOR_ERROR |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 2578 | } |
Chris Lattner | 1913b94 | 2008-07-11 00:30:39 +0000 | [diff] [blame] | 2579 | | EUAPINTVAL { // arbitrary precision integer constants |
| 2580 | $$ = ValID::create(*$1, false); |
| 2581 | delete $1; |
| 2582 | CHECK_FOR_ERROR |
| 2583 | } |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2584 | | FPVAL { // Perhaps it's an FP constant? |
| 2585 | $$ = ValID::create($1); |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 2586 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2587 | } |
| 2588 | | TRUETOK { |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2589 | $$ = ValID::create(ConstantInt::getTrue()); |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 2590 | CHECK_FOR_ERROR |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 2591 | } |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2592 | | FALSETOK { |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2593 | $$ = ValID::create(ConstantInt::getFalse()); |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 2594 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2595 | } |
| 2596 | | NULL_TOK { |
| 2597 | $$ = ValID::createNull(); |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 2598 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2599 | } |
| 2600 | | UNDEF { |
| 2601 | $$ = ValID::createUndef(); |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 2602 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2603 | } |
| 2604 | | ZEROINITIALIZER { // A vector zero constant. |
| 2605 | $$ = ValID::createZeroInit(); |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 2606 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2607 | } |
| 2608 | | '<' ConstVector '>' { // Nonempty unsized packed vector |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 2609 | const Type *ETy = (*$2)[0]->getType(); |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 2610 | unsigned NumElements = $2->size(); |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 2611 | |
| 2612 | if (!ETy->isInteger() && !ETy->isFloatingPoint()) |
| 2613 | GEN_ERROR("Invalid vector element type: " + ETy->getDescription()); |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 2614 | |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 2615 | VectorType* pt = VectorType::get(ETy, NumElements); |
Dan Gohman | f910eaa | 2008-06-09 14:45:02 +0000 | [diff] [blame] | 2616 | PATypeHolder* PTy = new PATypeHolder(HandleUpRefs(pt)); |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 2617 | |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2618 | // Verify all elements are correct type! |
| 2619 | for (unsigned i = 0; i < $2->size(); i++) { |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 2620 | if (ETy != (*$2)[i]->getType()) |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 2621 | GEN_ERROR("Element #" + utostr(i) + " is not of type '" + |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2622 | ETy->getDescription() +"' as required!\nIt is of type '" + |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 2623 | (*$2)[i]->getType()->getDescription() + "'."); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2624 | } |
| 2625 | |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 2626 | $$ = ValID::create(ConstantVector::get(pt, *$2)); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2627 | delete PTy; delete $2; |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 2628 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2629 | } |
Dan Gohman | f910eaa | 2008-06-09 14:45:02 +0000 | [diff] [blame] | 2630 | | '[' ConstVector ']' { // Nonempty unsized arr |
| 2631 | const Type *ETy = (*$2)[0]->getType(); |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 2632 | uint64_t NumElements = $2->size(); |
Dan Gohman | f910eaa | 2008-06-09 14:45:02 +0000 | [diff] [blame] | 2633 | |
| 2634 | if (!ETy->isFirstClassType()) |
| 2635 | GEN_ERROR("Invalid array element type: " + ETy->getDescription()); |
| 2636 | |
| 2637 | ArrayType *ATy = ArrayType::get(ETy, NumElements); |
| 2638 | PATypeHolder* PTy = new PATypeHolder(HandleUpRefs(ATy)); |
| 2639 | |
| 2640 | // Verify all elements are correct type! |
| 2641 | for (unsigned i = 0; i < $2->size(); i++) { |
| 2642 | if (ETy != (*$2)[i]->getType()) |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 2643 | GEN_ERROR("Element #" + utostr(i) + " is not of type '" + |
Dan Gohman | f910eaa | 2008-06-09 14:45:02 +0000 | [diff] [blame] | 2644 | ETy->getDescription() +"' as required!\nIt is of type '"+ |
| 2645 | (*$2)[i]->getType()->getDescription() + "'."); |
| 2646 | } |
| 2647 | |
| 2648 | $$ = ValID::create(ConstantArray::get(ATy, *$2)); |
| 2649 | delete PTy; delete $2; |
| 2650 | CHECK_FOR_ERROR |
| 2651 | } |
| 2652 | | '[' ']' { |
Dan Gohman | 180c169 | 2008-06-23 18:43:26 +0000 | [diff] [blame] | 2653 | // Use undef instead of an array because it's inconvenient to determine |
| 2654 | // the element type at this point, there being no elements to examine. |
Dan Gohman | f910eaa | 2008-06-09 14:45:02 +0000 | [diff] [blame] | 2655 | $$ = ValID::createUndef(); |
| 2656 | CHECK_FOR_ERROR |
| 2657 | } |
| 2658 | | 'c' STRINGCONSTANT { |
Dan Gohman | 180c169 | 2008-06-23 18:43:26 +0000 | [diff] [blame] | 2659 | uint64_t NumElements = $2->length(); |
Dan Gohman | f910eaa | 2008-06-09 14:45:02 +0000 | [diff] [blame] | 2660 | const Type *ETy = Type::Int8Ty; |
| 2661 | |
| 2662 | ArrayType *ATy = ArrayType::get(ETy, NumElements); |
| 2663 | |
| 2664 | std::vector<Constant*> Vals; |
| 2665 | for (unsigned i = 0; i < $2->length(); ++i) |
| 2666 | Vals.push_back(ConstantInt::get(ETy, (*$2)[i])); |
| 2667 | delete $2; |
| 2668 | $$ = ValID::create(ConstantArray::get(ATy, Vals)); |
| 2669 | CHECK_FOR_ERROR |
| 2670 | } |
| 2671 | | '{' ConstVector '}' { |
| 2672 | std::vector<const Type*> Elements($2->size()); |
| 2673 | for (unsigned i = 0, e = $2->size(); i != e; ++i) |
| 2674 | Elements[i] = (*$2)[i]->getType(); |
| 2675 | |
| 2676 | const StructType *STy = StructType::get(Elements); |
| 2677 | PATypeHolder* PTy = new PATypeHolder(HandleUpRefs(STy)); |
| 2678 | |
| 2679 | $$ = ValID::create(ConstantStruct::get(STy, *$2)); |
| 2680 | delete PTy; delete $2; |
| 2681 | CHECK_FOR_ERROR |
| 2682 | } |
| 2683 | | '{' '}' { |
| 2684 | const StructType *STy = StructType::get(std::vector<const Type*>()); |
| 2685 | $$ = ValID::create(ConstantStruct::get(STy, std::vector<Constant*>())); |
| 2686 | CHECK_FOR_ERROR |
| 2687 | } |
| 2688 | | '<' '{' ConstVector '}' '>' { |
| 2689 | std::vector<const Type*> Elements($3->size()); |
| 2690 | for (unsigned i = 0, e = $3->size(); i != e; ++i) |
| 2691 | Elements[i] = (*$3)[i]->getType(); |
| 2692 | |
| 2693 | const StructType *STy = StructType::get(Elements, /*isPacked=*/true); |
| 2694 | PATypeHolder* PTy = new PATypeHolder(HandleUpRefs(STy)); |
| 2695 | |
| 2696 | $$ = ValID::create(ConstantStruct::get(STy, *$3)); |
| 2697 | delete PTy; delete $3; |
| 2698 | CHECK_FOR_ERROR |
| 2699 | } |
| 2700 | | '<' '{' '}' '>' { |
| 2701 | const StructType *STy = StructType::get(std::vector<const Type*>(), |
| 2702 | /*isPacked=*/true); |
| 2703 | $$ = ValID::create(ConstantStruct::get(STy, std::vector<Constant*>())); |
| 2704 | CHECK_FOR_ERROR |
| 2705 | } |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2706 | | ConstExpr { |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 2707 | $$ = ValID::create($1); |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 2708 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2709 | } |
| 2710 | | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT { |
Reid Spencer | 0a8a16b | 2007-05-22 18:52:55 +0000 | [diff] [blame] | 2711 | $$ = ValID::createInlineAsm(*$3, *$5, $2); |
| 2712 | delete $3; |
| 2713 | delete $5; |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 2714 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2715 | }; |
| 2716 | |
| 2717 | // SymbolicValueRef - Reference to one of two ways of symbolically refering to |
| 2718 | // another value. |
| 2719 | // |
Reid Spencer | 41dff5e | 2007-01-26 08:05:27 +0000 | [diff] [blame] | 2720 | SymbolicValueRef : LOCALVAL_ID { // Is it an integer reference...? |
| 2721 | $$ = ValID::createLocalID($1); |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 2722 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2723 | } |
Reid Spencer | 41dff5e | 2007-01-26 08:05:27 +0000 | [diff] [blame] | 2724 | | GLOBALVAL_ID { |
| 2725 | $$ = ValID::createGlobalID($1); |
| 2726 | CHECK_FOR_ERROR |
| 2727 | } |
| 2728 | | LocalName { // Is it a named reference...? |
Reid Spencer | 0a8a16b | 2007-05-22 18:52:55 +0000 | [diff] [blame] | 2729 | $$ = ValID::createLocalName(*$1); |
| 2730 | delete $1; |
Reid Spencer | 41dff5e | 2007-01-26 08:05:27 +0000 | [diff] [blame] | 2731 | CHECK_FOR_ERROR |
| 2732 | } |
| 2733 | | GlobalName { // Is it a named reference...? |
Reid Spencer | 0a8a16b | 2007-05-22 18:52:55 +0000 | [diff] [blame] | 2734 | $$ = ValID::createGlobalName(*$1); |
| 2735 | delete $1; |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 2736 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2737 | }; |
| 2738 | |
| 2739 | // ValueRef - A reference to a definition... either constant or symbolic |
| 2740 | ValueRef : SymbolicValueRef | ConstValueRef; |
| 2741 | |
| 2742 | |
| 2743 | // ResolvedVal - a <type> <value> pair. This is used only in cases where the |
| 2744 | // type immediately preceeds the value reference, and allows complex constant |
| 2745 | // pool references (for things like: 'ret [2 x int] [ int 12, int 42]') |
| 2746 | ResolvedVal : Types ValueRef { |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 2747 | if (!UpRefs.empty()) |
| 2748 | GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription()); |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 2749 | $$ = getVal(*$1, $2); |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 2750 | delete $1; |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 2751 | CHECK_FOR_ERROR |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 2752 | } |
| 2753 | ; |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2754 | |
Devang Patel | 7990dc7 | 2008-02-20 22:40:23 +0000 | [diff] [blame] | 2755 | ReturnedVal : ResolvedVal { |
| 2756 | $$ = new std::vector<Value *>(); |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 2757 | $$->push_back($1); |
Devang Patel | 7990dc7 | 2008-02-20 22:40:23 +0000 | [diff] [blame] | 2758 | CHECK_FOR_ERROR |
| 2759 | } |
Devang Patel | 6bfc63b | 2008-02-23 00:38:56 +0000 | [diff] [blame] | 2760 | | ReturnedVal ',' ResolvedVal { |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 2761 | ($$=$1)->push_back($3); |
Devang Patel | 7990dc7 | 2008-02-20 22:40:23 +0000 | [diff] [blame] | 2762 | CHECK_FOR_ERROR |
| 2763 | }; |
| 2764 | |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2765 | BasicBlockList : BasicBlockList BasicBlock { |
| 2766 | $$ = $1; |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 2767 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2768 | } |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 2769 | | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2770 | $$ = $1; |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 2771 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2772 | }; |
| 2773 | |
| 2774 | |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 2775 | // Basic blocks are terminated by branching instructions: |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2776 | // br, br/cc, switch, ret |
| 2777 | // |
Chris Lattner | 15bd095 | 2008-08-29 17:20:18 +0000 | [diff] [blame] | 2778 | BasicBlock : InstructionList OptLocalAssign BBTerminatorInst { |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2779 | setValueName($3, $2); |
Reid Spencer | 5b7e753 | 2006-09-28 19:28:24 +0000 | [diff] [blame] | 2780 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2781 | InsertValue($3); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2782 | $1->getInstList().push_back($3); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2783 | $$ = $1; |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 2784 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2785 | }; |
| 2786 | |
Chris Lattner | 15bd095 | 2008-08-29 17:20:18 +0000 | [diff] [blame] | 2787 | BasicBlock : InstructionList LocalNumber BBTerminatorInst { |
| 2788 | CHECK_FOR_ERROR |
| 2789 | int ValNum = InsertValue($3); |
| 2790 | if (ValNum != (int)$2) |
| 2791 | GEN_ERROR("Result value number %" + utostr($2) + |
| 2792 | " is incorrect, expected %" + utostr((unsigned)ValNum)); |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 2793 | |
Chris Lattner | 15bd095 | 2008-08-29 17:20:18 +0000 | [diff] [blame] | 2794 | $1->getInstList().push_back($3); |
| 2795 | $$ = $1; |
| 2796 | CHECK_FOR_ERROR |
| 2797 | }; |
| 2798 | |
| 2799 | |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2800 | InstructionList : InstructionList Inst { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2801 | if (CastInst *CI1 = dyn_cast<CastInst>($2)) |
| 2802 | if (CastInst *CI2 = dyn_cast<CastInst>(CI1->getOperand(0))) |
| 2803 | if (CI2->getParent() == 0) |
| 2804 | $1->getInstList().push_back(CI2); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2805 | $1->getInstList().push_back($2); |
| 2806 | $$ = $1; |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 2807 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2808 | } |
Reid Spencer | 93c4003 | 2007-03-19 18:40:50 +0000 | [diff] [blame] | 2809 | | /* empty */ { // Empty space between instruction lists |
Nick Lewycky | 280a6e6 | 2008-04-25 16:53:59 +0000 | [diff] [blame] | 2810 | $$ = defineBBVal(ValID::createLocalID(CurFun.NextValNum)); |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 2811 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2812 | } |
Reid Spencer | 93c4003 | 2007-03-19 18:40:50 +0000 | [diff] [blame] | 2813 | | LABELSTR { // Labelled (named) basic block |
Nick Lewycky | 280a6e6 | 2008-04-25 16:53:59 +0000 | [diff] [blame] | 2814 | $$ = defineBBVal(ValID::createLocalName(*$1)); |
Reid Spencer | 0a8a16b | 2007-05-22 18:52:55 +0000 | [diff] [blame] | 2815 | delete $1; |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 2816 | CHECK_FOR_ERROR |
Nick Lewycky | 280a6e6 | 2008-04-25 16:53:59 +0000 | [diff] [blame] | 2817 | |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2818 | }; |
| 2819 | |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 2820 | BBTerminatorInst : |
Devang Patel | 7990dc7 | 2008-02-20 22:40:23 +0000 | [diff] [blame] | 2821 | RET ReturnedVal { // Return with a result... |
Devang Patel | b82b7f2 | 2008-02-26 22:17:48 +0000 | [diff] [blame] | 2822 | ValueList &VL = *$2; |
Devang Patel | 13b823c | 2008-02-26 23:19:08 +0000 | [diff] [blame] | 2823 | assert(!VL.empty() && "Invalid ret operands!"); |
Dan Gohman | 1a57024 | 2008-07-23 00:54:54 +0000 | [diff] [blame] | 2824 | const Type *ReturnType = CurFun.CurrentFunction->getReturnType(); |
| 2825 | if (VL.size() > 1 || |
| 2826 | (isa<StructType>(ReturnType) && |
| 2827 | (VL.empty() || VL[0]->getType() != ReturnType))) { |
| 2828 | Value *RV = UndefValue::get(ReturnType); |
| 2829 | for (unsigned i = 0, e = VL.size(); i != e; ++i) { |
| 2830 | Instruction *I = InsertValueInst::Create(RV, VL[i], i, "mrv"); |
| 2831 | ($<BasicBlockVal>-1)->getInstList().push_back(I); |
| 2832 | RV = I; |
| 2833 | } |
| 2834 | $$ = ReturnInst::Create(RV); |
| 2835 | } else { |
| 2836 | $$ = ReturnInst::Create(VL[0]); |
| 2837 | } |
Devang Patel | 7990dc7 | 2008-02-20 22:40:23 +0000 | [diff] [blame] | 2838 | delete $2; |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 2839 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2840 | } |
Reid Spencer | 93c4003 | 2007-03-19 18:40:50 +0000 | [diff] [blame] | 2841 | | RET VOID { // Return with no result... |
Gabor Greif | e64d248 | 2008-04-06 23:07:54 +0000 | [diff] [blame] | 2842 | $$ = ReturnInst::Create(); |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 2843 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2844 | } |
Reid Spencer | 93c4003 | 2007-03-19 18:40:50 +0000 | [diff] [blame] | 2845 | | BR LABEL ValueRef { // Unconditional Branch... |
Reid Spencer | 5b7e753 | 2006-09-28 19:28:24 +0000 | [diff] [blame] | 2846 | BasicBlock* tmpBB = getBBVal($3); |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 2847 | CHECK_FOR_ERROR |
Gabor Greif | e64d248 | 2008-04-06 23:07:54 +0000 | [diff] [blame] | 2848 | $$ = BranchInst::Create(tmpBB); |
Reid Spencer | 93c4003 | 2007-03-19 18:40:50 +0000 | [diff] [blame] | 2849 | } // Conditional Branch... |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 2850 | | BR INTTYPE ValueRef ',' LABEL ValueRef ',' LABEL ValueRef { |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 2851 | if (cast<IntegerType>($2)->getBitWidth() != 1) |
| 2852 | GEN_ERROR("Branch condition must have type i1"); |
Reid Spencer | 5b7e753 | 2006-09-28 19:28:24 +0000 | [diff] [blame] | 2853 | BasicBlock* tmpBBA = getBBVal($6); |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 2854 | CHECK_FOR_ERROR |
Reid Spencer | 5b7e753 | 2006-09-28 19:28:24 +0000 | [diff] [blame] | 2855 | BasicBlock* tmpBBB = getBBVal($9); |
| 2856 | CHECK_FOR_ERROR |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 2857 | Value* tmpVal = getVal(Type::Int1Ty, $3); |
Reid Spencer | 5b7e753 | 2006-09-28 19:28:24 +0000 | [diff] [blame] | 2858 | CHECK_FOR_ERROR |
Gabor Greif | e64d248 | 2008-04-06 23:07:54 +0000 | [diff] [blame] | 2859 | $$ = BranchInst::Create(tmpBBA, tmpBBB, tmpVal); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2860 | } |
Chris Lattner | f9078f9 | 2008-10-15 06:03:48 +0000 | [diff] [blame] | 2861 | | SWITCH INTTYPE ValueRef ',' LABEL ValueRef '[' JumpTable ']' { |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 2862 | Value* tmpVal = getVal($2, $3); |
Reid Spencer | 5b7e753 | 2006-09-28 19:28:24 +0000 | [diff] [blame] | 2863 | CHECK_FOR_ERROR |
| 2864 | BasicBlock* tmpBB = getBBVal($6); |
| 2865 | CHECK_FOR_ERROR |
Gabor Greif | e64d248 | 2008-04-06 23:07:54 +0000 | [diff] [blame] | 2866 | SwitchInst *S = SwitchInst::Create(tmpVal, tmpBB, $8->size()); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2867 | $$ = S; |
| 2868 | |
| 2869 | std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(), |
| 2870 | E = $8->end(); |
| 2871 | for (; I != E; ++I) { |
| 2872 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first)) |
| 2873 | S->addCase(CI, I->second); |
| 2874 | else |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 2875 | GEN_ERROR("Switch case is constant, but not a simple integer"); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2876 | } |
| 2877 | delete $8; |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 2878 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2879 | } |
Chris Lattner | f9078f9 | 2008-10-15 06:03:48 +0000 | [diff] [blame] | 2880 | | SWITCH INTTYPE ValueRef ',' LABEL ValueRef '[' ']' { |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 2881 | Value* tmpVal = getVal($2, $3); |
Reid Spencer | 5b7e753 | 2006-09-28 19:28:24 +0000 | [diff] [blame] | 2882 | CHECK_FOR_ERROR |
| 2883 | BasicBlock* tmpBB = getBBVal($6); |
| 2884 | CHECK_FOR_ERROR |
Gabor Greif | e64d248 | 2008-04-06 23:07:54 +0000 | [diff] [blame] | 2885 | SwitchInst *S = SwitchInst::Create(tmpVal, tmpBB, 0); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2886 | $$ = S; |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 2887 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2888 | } |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 2889 | | INVOKE OptCallingConv OptRetAttrs ResultTypes ValueRef '(' ParamList ')' |
| 2890 | OptFuncAttrs TO LABEL ValueRef UNWIND LABEL ValueRef { |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2891 | |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 2892 | // Handle the short syntax |
| 2893 | const PointerType *PFTy = 0; |
| 2894 | const FunctionType *Ty = 0; |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 2895 | if (!(PFTy = dyn_cast<PointerType>($4->get())) || |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2896 | !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) { |
| 2897 | // Pull out the types of all of the arguments... |
| 2898 | std::vector<const Type*> ParamTypes; |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 2899 | ParamList::iterator I = $7->begin(), E = $7->end(); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 2900 | for (; I != E; ++I) { |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 2901 | const Type *Ty = I->Val->getType(); |
| 2902 | if (Ty == Type::VoidTy) |
| 2903 | GEN_ERROR("Short call syntax cannot be used with varargs"); |
| 2904 | ParamTypes.push_back(Ty); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2905 | } |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 2906 | |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 2907 | if (!FunctionType::isValidReturnType(*$4)) |
Chris Lattner | a925a14 | 2008-04-23 05:37:08 +0000 | [diff] [blame] | 2908 | GEN_ERROR("Invalid result type for LLVM function"); |
| 2909 | |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 2910 | Ty = FunctionType::get($4->get(), ParamTypes, false); |
Christopher Lamb | 4374f8e | 2007-12-17 01:17:35 +0000 | [diff] [blame] | 2911 | PFTy = PointerType::getUnqual(Ty); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2912 | } |
| 2913 | |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 2914 | delete $4; |
Reid Spencer | 66728ef | 2007-03-20 01:13:36 +0000 | [diff] [blame] | 2915 | |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 2916 | Value *V = getVal(PFTy, $5); // Get the function we're calling... |
Reid Spencer | 5b7e753 | 2006-09-28 19:28:24 +0000 | [diff] [blame] | 2917 | CHECK_FOR_ERROR |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 2918 | BasicBlock *Normal = getBBVal($12); |
Reid Spencer | 5b7e753 | 2006-09-28 19:28:24 +0000 | [diff] [blame] | 2919 | CHECK_FOR_ERROR |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 2920 | BasicBlock *Except = getBBVal($15); |
Reid Spencer | 5b7e753 | 2006-09-28 19:28:24 +0000 | [diff] [blame] | 2921 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2922 | |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 2923 | SmallVector<AttributeWithIndex, 8> Attrs; |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 2924 | //FIXME : In 3.0, stop accepting zext, sext and inreg as optional function |
| 2925 | //attributes. |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 2926 | Attributes RetAttrs = $3; |
| 2927 | if ($9 != Attribute::None) { |
| 2928 | if ($9 & Attribute::ZExt) { |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 2929 | RetAttrs = RetAttrs | Attribute::ZExt; |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 2930 | $9 = $9 ^ Attribute::ZExt; |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 2931 | } |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 2932 | if ($9 & Attribute::SExt) { |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 2933 | RetAttrs = RetAttrs | Attribute::SExt; |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 2934 | $9 = $9 ^ Attribute::SExt; |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 2935 | } |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 2936 | if ($9 & Attribute::InReg) { |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 2937 | RetAttrs = RetAttrs | Attribute::InReg; |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 2938 | $9 = $9 ^ Attribute::InReg; |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 2939 | } |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 2940 | } |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 2941 | if (RetAttrs != Attribute::None) |
| 2942 | Attrs.push_back(AttributeWithIndex::get(0, RetAttrs)); |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 2943 | |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 2944 | // Check the arguments |
| 2945 | ValueList Args; |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 2946 | if ($7->empty()) { // Has no arguments? |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 2947 | // Make sure no arguments is a good thing! |
| 2948 | if (Ty->getNumParams() != 0) |
| 2949 | GEN_ERROR("No arguments passed to a function that " |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 2950 | "expects arguments"); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2951 | } else { // Has arguments? |
| 2952 | // Loop through FunctionType's arguments and ensure they are specified |
| 2953 | // correctly! |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2954 | FunctionType::param_iterator I = Ty->param_begin(); |
| 2955 | FunctionType::param_iterator E = Ty->param_end(); |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 2956 | ParamList::iterator ArgI = $7->begin(), ArgE = $7->end(); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 2957 | unsigned index = 1; |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2958 | |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 2959 | for (; ArgI != ArgE && I != E; ++ArgI, ++I, ++index) { |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 2960 | if (ArgI->Val->getType() != *I) |
| 2961 | GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" + |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 2962 | (*I)->getDescription() + "'"); |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 2963 | Args.push_back(ArgI->Val); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 2964 | if (ArgI->Attrs != Attribute::None) |
| 2965 | Attrs.push_back(AttributeWithIndex::get(index, ArgI->Attrs)); |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 2966 | } |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 2967 | |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 2968 | if (Ty->isVarArg()) { |
| 2969 | if (I == E) |
Chris Lattner | 3890561 | 2008-02-19 04:36:25 +0000 | [diff] [blame] | 2970 | for (; ArgI != ArgE; ++ArgI, ++index) { |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 2971 | Args.push_back(ArgI->Val); // push the remaining varargs |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 2972 | if (ArgI->Attrs != Attribute::None) |
| 2973 | Attrs.push_back(AttributeWithIndex::get(index, ArgI->Attrs)); |
Chris Lattner | 3890561 | 2008-02-19 04:36:25 +0000 | [diff] [blame] | 2974 | } |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 2975 | } else if (I != E || ArgI != ArgE) |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 2976 | GEN_ERROR("Invalid number of parameters detected"); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2977 | } |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 2978 | if ($9 != Attribute::None) |
| 2979 | Attrs.push_back(AttributeWithIndex::get(~0, $9)); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 2980 | AttrListPtr PAL; |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 2981 | if (!Attrs.empty()) |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 2982 | PAL = AttrListPtr::get(Attrs.begin(), Attrs.end()); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 2983 | |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 2984 | // Create the InvokeInst |
Dan Gohman | 041e2eb | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2985 | InvokeInst *II = InvokeInst::Create(V, Normal, Except, |
| 2986 | Args.begin(), Args.end()); |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 2987 | II->setCallingConv($2); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 2988 | II->setAttributes(PAL); |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 2989 | $$ = II; |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 2990 | delete $7; |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 2991 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2992 | } |
| 2993 | | UNWIND { |
| 2994 | $$ = new UnwindInst(); |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 2995 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 2996 | } |
| 2997 | | UNREACHABLE { |
| 2998 | $$ = new UnreachableInst(); |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 2999 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3000 | }; |
| 3001 | |
| 3002 | |
| 3003 | |
Chris Lattner | f9078f9 | 2008-10-15 06:03:48 +0000 | [diff] [blame] | 3004 | JumpTable : JumpTable INTTYPE ConstValueRef ',' LABEL ValueRef { |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3005 | $$ = $1; |
Reid Spencer | 93c4003 | 2007-03-19 18:40:50 +0000 | [diff] [blame] | 3006 | Constant *V = cast<Constant>(getExistingVal($2, $3)); |
Reid Spencer | 5b7e753 | 2006-09-28 19:28:24 +0000 | [diff] [blame] | 3007 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3008 | if (V == 0) |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 3009 | GEN_ERROR("May only switch on a constant pool value"); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3010 | |
Reid Spencer | 5b7e753 | 2006-09-28 19:28:24 +0000 | [diff] [blame] | 3011 | BasicBlock* tmpBB = getBBVal($6); |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 3012 | CHECK_FOR_ERROR |
Reid Spencer | 5b7e753 | 2006-09-28 19:28:24 +0000 | [diff] [blame] | 3013 | $$->push_back(std::make_pair(V, tmpBB)); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3014 | } |
Chris Lattner | f9078f9 | 2008-10-15 06:03:48 +0000 | [diff] [blame] | 3015 | | INTTYPE ConstValueRef ',' LABEL ValueRef { |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3016 | $$ = new std::vector<std::pair<Constant*, BasicBlock*> >(); |
Reid Spencer | 93c4003 | 2007-03-19 18:40:50 +0000 | [diff] [blame] | 3017 | Constant *V = cast<Constant>(getExistingVal($1, $2)); |
Reid Spencer | 5b7e753 | 2006-09-28 19:28:24 +0000 | [diff] [blame] | 3018 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3019 | |
| 3020 | if (V == 0) |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 3021 | GEN_ERROR("May only switch on a constant pool value"); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3022 | |
Reid Spencer | 5b7e753 | 2006-09-28 19:28:24 +0000 | [diff] [blame] | 3023 | BasicBlock* tmpBB = getBBVal($5); |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 3024 | CHECK_FOR_ERROR |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 3025 | $$->push_back(std::make_pair(V, tmpBB)); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3026 | }; |
| 3027 | |
Reid Spencer | 41dff5e | 2007-01-26 08:05:27 +0000 | [diff] [blame] | 3028 | Inst : OptLocalAssign InstVal { |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 3029 | // Is this definition named?? if so, assign the name... |
| 3030 | setValueName($2, $1); |
| 3031 | CHECK_FOR_ERROR |
| 3032 | InsertValue($2); |
| 3033 | $$ = $2; |
| 3034 | CHECK_FOR_ERROR |
| 3035 | }; |
| 3036 | |
Chris Lattner | 15bd095 | 2008-08-29 17:20:18 +0000 | [diff] [blame] | 3037 | Inst : LocalNumber InstVal { |
| 3038 | CHECK_FOR_ERROR |
| 3039 | int ValNum = InsertValue($2); |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 3040 | |
Chris Lattner | 15bd095 | 2008-08-29 17:20:18 +0000 | [diff] [blame] | 3041 | if (ValNum != (int)$1) |
| 3042 | GEN_ERROR("Result value number %" + utostr($1) + |
| 3043 | " is incorrect, expected %" + utostr((unsigned)ValNum)); |
| 3044 | |
| 3045 | $$ = $2; |
| 3046 | CHECK_FOR_ERROR |
| 3047 | }; |
| 3048 | |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3049 | |
| 3050 | PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 3051 | if (!UpRefs.empty()) |
| 3052 | GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription()); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3053 | $$ = new std::list<std::pair<Value*, BasicBlock*> >(); |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 3054 | Value* tmpVal = getVal(*$1, $3); |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 3055 | CHECK_FOR_ERROR |
Reid Spencer | 5b7e753 | 2006-09-28 19:28:24 +0000 | [diff] [blame] | 3056 | BasicBlock* tmpBB = getBBVal($5); |
| 3057 | CHECK_FOR_ERROR |
| 3058 | $$->push_back(std::make_pair(tmpVal, tmpBB)); |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 3059 | delete $1; |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3060 | } |
| 3061 | | PHIList ',' '[' ValueRef ',' ValueRef ']' { |
| 3062 | $$ = $1; |
Reid Spencer | 5b7e753 | 2006-09-28 19:28:24 +0000 | [diff] [blame] | 3063 | Value* tmpVal = getVal($1->front().first->getType(), $4); |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 3064 | CHECK_FOR_ERROR |
Reid Spencer | 5b7e753 | 2006-09-28 19:28:24 +0000 | [diff] [blame] | 3065 | BasicBlock* tmpBB = getBBVal($6); |
| 3066 | CHECK_FOR_ERROR |
| 3067 | $1->push_back(std::make_pair(tmpVal, tmpBB)); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3068 | }; |
| 3069 | |
| 3070 | |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 3071 | ParamList : Types OptAttributes ValueRef OptAttributes { |
| 3072 | // FIXME: Remove trailing OptAttributes in LLVM 3.0, it was a mistake in 2.0 |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 3073 | if (!UpRefs.empty()) |
| 3074 | GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription()); |
| 3075 | // Used for call and invoke instructions |
Dale Johannesen | eb57ea7 | 2007-11-05 21:20:28 +0000 | [diff] [blame] | 3076 | $$ = new ParamList(); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 3077 | ParamListEntry E; E.Attrs = $2 | $4; E.Val = getVal($1->get(), $3); |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 3078 | $$->push_back(E); |
Reid Spencer | 66728ef | 2007-03-20 01:13:36 +0000 | [diff] [blame] | 3079 | delete $1; |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 3080 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3081 | } |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 3082 | | LABEL OptAttributes ValueRef OptAttributes { |
| 3083 | // FIXME: Remove trailing OptAttributes in LLVM 3.0, it was a mistake in 2.0 |
Dale Johannesen | eb57ea7 | 2007-11-05 21:20:28 +0000 | [diff] [blame] | 3084 | // Labels are only valid in ASMs |
| 3085 | $$ = new ParamList(); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 3086 | ParamListEntry E; E.Attrs = $2 | $4; E.Val = getBBVal($3); |
Dale Johannesen | eb57ea7 | 2007-11-05 21:20:28 +0000 | [diff] [blame] | 3087 | $$->push_back(E); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 3088 | CHECK_FOR_ERROR |
Dale Johannesen | eb57ea7 | 2007-11-05 21:20:28 +0000 | [diff] [blame] | 3089 | } |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 3090 | | ParamList ',' Types OptAttributes ValueRef OptAttributes { |
| 3091 | // FIXME: Remove trailing OptAttributes in LLVM 3.0, it was a mistake in 2.0 |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 3092 | if (!UpRefs.empty()) |
| 3093 | GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription()); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3094 | $$ = $1; |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 3095 | ParamListEntry E; E.Attrs = $4 | $6; E.Val = getVal($3->get(), $5); |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 3096 | $$->push_back(E); |
Reid Spencer | 66728ef | 2007-03-20 01:13:36 +0000 | [diff] [blame] | 3097 | delete $3; |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 3098 | CHECK_FOR_ERROR |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 3099 | } |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 3100 | | ParamList ',' LABEL OptAttributes ValueRef OptAttributes { |
| 3101 | // FIXME: Remove trailing OptAttributes in LLVM 3.0, it was a mistake in 2.0 |
Dale Johannesen | eb57ea7 | 2007-11-05 21:20:28 +0000 | [diff] [blame] | 3102 | $$ = $1; |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 3103 | ParamListEntry E; E.Attrs = $4 | $6; E.Val = getBBVal($5); |
Dale Johannesen | eb57ea7 | 2007-11-05 21:20:28 +0000 | [diff] [blame] | 3104 | $$->push_back(E); |
| 3105 | CHECK_FOR_ERROR |
| 3106 | } |
| 3107 | | /*empty*/ { $$ = new ParamList(); }; |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3108 | |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 3109 | IndexList // Used for gep instructions and constant expressions |
Reid Spencer | c6c59fd | 2006-12-31 21:47:02 +0000 | [diff] [blame] | 3110 | : /*empty*/ { $$ = new std::vector<Value*>(); } |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 3111 | | IndexList ',' ResolvedVal { |
| 3112 | $$ = $1; |
| 3113 | $$->push_back($3); |
| 3114 | CHECK_FOR_ERROR |
| 3115 | } |
Reid Spencer | c6c59fd | 2006-12-31 21:47:02 +0000 | [diff] [blame] | 3116 | ; |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3117 | |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 3118 | ConstantIndexList // Used for insertvalue and extractvalue instructions |
| 3119 | : ',' EUINT64VAL { |
| 3120 | $$ = new std::vector<unsigned>(); |
| 3121 | if ((unsigned)$2 != $2) |
| 3122 | GEN_ERROR("Index " + utostr($2) + " is not valid for insertvalue or extractvalue."); |
| 3123 | $$->push_back($2); |
| 3124 | } |
| 3125 | | ConstantIndexList ',' EUINT64VAL { |
| 3126 | $$ = $1; |
| 3127 | if ((unsigned)$3 != $3) |
| 3128 | GEN_ERROR("Index " + utostr($3) + " is not valid for insertvalue or extractvalue."); |
| 3129 | $$->push_back($3); |
| 3130 | CHECK_FOR_ERROR |
| 3131 | } |
| 3132 | ; |
| 3133 | |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3134 | OptTailCall : TAIL CALL { |
| 3135 | $$ = true; |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 3136 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3137 | } |
| 3138 | | CALL { |
| 3139 | $$ = false; |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 3140 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3141 | }; |
| 3142 | |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3143 | InstVal : ArithmeticOps Types ValueRef ',' ValueRef { |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 3144 | if (!UpRefs.empty()) |
| 3145 | GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription()); |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 3146 | if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() && |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 3147 | !isa<VectorType>((*$2).get())) |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 3148 | GEN_ERROR( |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 3149 | "Arithmetic operator requires integer, FP, or packed operands"); |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 3150 | Value* val1 = getVal(*$2, $3); |
Reid Spencer | 5b7e753 | 2006-09-28 19:28:24 +0000 | [diff] [blame] | 3151 | CHECK_FOR_ERROR |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 3152 | Value* val2 = getVal(*$2, $5); |
Reid Spencer | 5b7e753 | 2006-09-28 19:28:24 +0000 | [diff] [blame] | 3153 | CHECK_FOR_ERROR |
Dan Gohman | e4977cf | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 3154 | $$ = BinaryOperator::Create($1, val1, val2); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3155 | if ($$ == 0) |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 3156 | GEN_ERROR("binary operator returned null"); |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 3157 | delete $2; |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3158 | } |
| 3159 | | LogicalOps Types ValueRef ',' ValueRef { |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 3160 | if (!UpRefs.empty()) |
| 3161 | GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription()); |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 3162 | if (!(*$2)->isInteger()) { |
Nate Begeman | 5bc1ea0 | 2008-07-29 15:49:41 +0000 | [diff] [blame] | 3163 | if (!isa<VectorType>($2->get()) || |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 3164 | !cast<VectorType>($2->get())->getElementType()->isInteger()) |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 3165 | GEN_ERROR("Logical operator requires integral operands"); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3166 | } |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 3167 | Value* tmpVal1 = getVal(*$2, $3); |
Reid Spencer | 5b7e753 | 2006-09-28 19:28:24 +0000 | [diff] [blame] | 3168 | CHECK_FOR_ERROR |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 3169 | Value* tmpVal2 = getVal(*$2, $5); |
Reid Spencer | 5b7e753 | 2006-09-28 19:28:24 +0000 | [diff] [blame] | 3170 | CHECK_FOR_ERROR |
Dan Gohman | e4977cf | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 3171 | $$ = BinaryOperator::Create($1, tmpVal1, tmpVal2); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3172 | if ($$ == 0) |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 3173 | GEN_ERROR("binary operator returned null"); |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 3174 | delete $2; |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3175 | } |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 3176 | | ICMP IPredicates Types ValueRef ',' ValueRef { |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 3177 | if (!UpRefs.empty()) |
| 3178 | GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription()); |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 3179 | Value* tmpVal1 = getVal(*$3, $4); |
| 3180 | CHECK_FOR_ERROR |
| 3181 | Value* tmpVal2 = getVal(*$3, $6); |
| 3182 | CHECK_FOR_ERROR |
Dan Gohman | e4977cf | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 3183 | $$ = CmpInst::Create($1, $2, tmpVal1, tmpVal2); |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 3184 | if ($$ == 0) |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 3185 | GEN_ERROR("icmp operator returned null"); |
Reid Spencer | 66728ef | 2007-03-20 01:13:36 +0000 | [diff] [blame] | 3186 | delete $3; |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 3187 | } |
| 3188 | | FCMP FPredicates Types ValueRef ',' ValueRef { |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 3189 | if (!UpRefs.empty()) |
| 3190 | GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription()); |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 3191 | Value* tmpVal1 = getVal(*$3, $4); |
| 3192 | CHECK_FOR_ERROR |
| 3193 | Value* tmpVal2 = getVal(*$3, $6); |
| 3194 | CHECK_FOR_ERROR |
Dan Gohman | e4977cf | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 3195 | $$ = CmpInst::Create($1, $2, tmpVal1, tmpVal2); |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 3196 | if ($$ == 0) |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 3197 | GEN_ERROR("fcmp operator returned null"); |
Reid Spencer | 66728ef | 2007-03-20 01:13:36 +0000 | [diff] [blame] | 3198 | delete $3; |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3199 | } |
Nate Begeman | ac80ade | 2008-05-12 19:01:56 +0000 | [diff] [blame] | 3200 | | VICMP IPredicates Types ValueRef ',' ValueRef { |
| 3201 | if (!UpRefs.empty()) |
| 3202 | GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription()); |
| 3203 | if (!isa<VectorType>((*$3).get())) |
| 3204 | GEN_ERROR("Scalar types not supported by vicmp instruction"); |
| 3205 | Value* tmpVal1 = getVal(*$3, $4); |
| 3206 | CHECK_FOR_ERROR |
| 3207 | Value* tmpVal2 = getVal(*$3, $6); |
| 3208 | CHECK_FOR_ERROR |
Dan Gohman | e4977cf | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 3209 | $$ = CmpInst::Create($1, $2, tmpVal1, tmpVal2); |
Nate Begeman | ac80ade | 2008-05-12 19:01:56 +0000 | [diff] [blame] | 3210 | if ($$ == 0) |
Dan Gohman | d8ee59b | 2008-09-09 01:13:24 +0000 | [diff] [blame] | 3211 | GEN_ERROR("vicmp operator returned null"); |
Nate Begeman | ac80ade | 2008-05-12 19:01:56 +0000 | [diff] [blame] | 3212 | delete $3; |
| 3213 | } |
| 3214 | | VFCMP FPredicates Types ValueRef ',' ValueRef { |
| 3215 | if (!UpRefs.empty()) |
| 3216 | GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription()); |
| 3217 | if (!isa<VectorType>((*$3).get())) |
| 3218 | GEN_ERROR("Scalar types not supported by vfcmp instruction"); |
| 3219 | Value* tmpVal1 = getVal(*$3, $4); |
| 3220 | CHECK_FOR_ERROR |
| 3221 | Value* tmpVal2 = getVal(*$3, $6); |
| 3222 | CHECK_FOR_ERROR |
Dan Gohman | e4977cf | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 3223 | $$ = CmpInst::Create($1, $2, tmpVal1, tmpVal2); |
Nate Begeman | ac80ade | 2008-05-12 19:01:56 +0000 | [diff] [blame] | 3224 | if ($$ == 0) |
Dan Gohman | d8ee59b | 2008-09-09 01:13:24 +0000 | [diff] [blame] | 3225 | GEN_ERROR("vfcmp operator returned null"); |
Nate Begeman | ac80ade | 2008-05-12 19:01:56 +0000 | [diff] [blame] | 3226 | delete $3; |
| 3227 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3228 | | CastOps ResolvedVal TO Types { |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 3229 | if (!UpRefs.empty()) |
| 3230 | GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription()); |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 3231 | Value* Val = $2; |
Reid Spencer | b0fcf8f | 2007-01-17 02:48:45 +0000 | [diff] [blame] | 3232 | const Type* DestTy = $4->get(); |
| 3233 | if (!CastInst::castIsValid($1, Val, DestTy)) |
| 3234 | GEN_ERROR("invalid cast opcode for cast from '" + |
| 3235 | Val->getType()->getDescription() + "' to '" + |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 3236 | DestTy->getDescription() + "'"); |
Dan Gohman | e4977cf | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 3237 | $$ = CastInst::Create($1, Val, DestTy); |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 3238 | delete $4; |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3239 | } |
| 3240 | | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal { |
Dan Gohman | d8ee59b | 2008-09-09 01:13:24 +0000 | [diff] [blame] | 3241 | if (isa<VectorType>($2->getType())) { |
| 3242 | // vector select |
| 3243 | if (!isa<VectorType>($4->getType()) |
| 3244 | || !isa<VectorType>($6->getType()) ) |
| 3245 | GEN_ERROR("vector select value types must be vector types"); |
| 3246 | const VectorType* cond_type = cast<VectorType>($2->getType()); |
| 3247 | const VectorType* select_type = cast<VectorType>($4->getType()); |
| 3248 | if (cond_type->getElementType() != Type::Int1Ty) |
| 3249 | GEN_ERROR("vector select condition element type must be boolean"); |
| 3250 | if (cond_type->getNumElements() != select_type->getNumElements()) |
| 3251 | GEN_ERROR("vector select number of elements must be the same"); |
| 3252 | } else { |
| 3253 | if ($2->getType() != Type::Int1Ty) |
| 3254 | GEN_ERROR("select condition must be boolean"); |
| 3255 | } |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 3256 | if ($4->getType() != $6->getType()) |
Dan Gohman | d8ee59b | 2008-09-09 01:13:24 +0000 | [diff] [blame] | 3257 | GEN_ERROR("select value types must match"); |
Gabor Greif | e64d248 | 2008-04-06 23:07:54 +0000 | [diff] [blame] | 3258 | $$ = SelectInst::Create($2, $4, $6); |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 3259 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3260 | } |
| 3261 | | VAARG ResolvedVal ',' Types { |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 3262 | if (!UpRefs.empty()) |
| 3263 | GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription()); |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 3264 | $$ = new VAArgInst($2, *$4); |
| 3265 | delete $4; |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 3266 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3267 | } |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3268 | | EXTRACTELEMENT ResolvedVal ',' ResolvedVal { |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 3269 | if (!ExtractElementInst::isValidOperands($2, $4)) |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 3270 | GEN_ERROR("Invalid extractelement operands"); |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 3271 | $$ = new ExtractElementInst($2, $4); |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 3272 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3273 | } |
| 3274 | | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal { |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 3275 | if (!InsertElementInst::isValidOperands($2, $4, $6)) |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 3276 | GEN_ERROR("Invalid insertelement operands"); |
Gabor Greif | e64d248 | 2008-04-06 23:07:54 +0000 | [diff] [blame] | 3277 | $$ = InsertElementInst::Create($2, $4, $6); |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 3278 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3279 | } |
Chris Lattner | d5efe84 | 2006-04-08 01:18:56 +0000 | [diff] [blame] | 3280 | | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal { |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 3281 | if (!ShuffleVectorInst::isValidOperands($2, $4, $6)) |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 3282 | GEN_ERROR("Invalid shufflevector operands"); |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 3283 | $$ = new ShuffleVectorInst($2, $4, $6); |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 3284 | CHECK_FOR_ERROR |
Chris Lattner | d5efe84 | 2006-04-08 01:18:56 +0000 | [diff] [blame] | 3285 | } |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3286 | | PHI_TOK PHIList { |
| 3287 | const Type *Ty = $2->front().first->getType(); |
| 3288 | if (!Ty->isFirstClassType()) |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 3289 | GEN_ERROR("PHI node operands must be of first class type"); |
Gabor Greif | e64d248 | 2008-04-06 23:07:54 +0000 | [diff] [blame] | 3290 | $$ = PHINode::Create(Ty); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3291 | ((PHINode*)$$)->reserveOperandSpace($2->size()); |
| 3292 | while ($2->begin() != $2->end()) { |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 3293 | if ($2->front().first->getType() != Ty) |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 3294 | GEN_ERROR("All elements of a PHI node must be of the same type"); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3295 | cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second); |
| 3296 | $2->pop_front(); |
| 3297 | } |
| 3298 | delete $2; // Free the list... |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 3299 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3300 | } |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 3301 | | OptTailCall OptCallingConv OptRetAttrs ResultTypes ValueRef '(' ParamList ')' |
Reid Spencer | 218ded2 | 2007-01-05 17:07:23 +0000 | [diff] [blame] | 3302 | OptFuncAttrs { |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 3303 | |
| 3304 | // Handle the short syntax |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3305 | const PointerType *PFTy = 0; |
| 3306 | const FunctionType *Ty = 0; |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 3307 | if (!(PFTy = dyn_cast<PointerType>($4->get())) || |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3308 | !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) { |
| 3309 | // Pull out the types of all of the arguments... |
| 3310 | std::vector<const Type*> ParamTypes; |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 3311 | ParamList::iterator I = $7->begin(), E = $7->end(); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 3312 | for (; I != E; ++I) { |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 3313 | const Type *Ty = I->Val->getType(); |
| 3314 | if (Ty == Type::VoidTy) |
| 3315 | GEN_ERROR("Short call syntax cannot be used with varargs"); |
| 3316 | ParamTypes.push_back(Ty); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3317 | } |
Chris Lattner | a925a14 | 2008-04-23 05:37:08 +0000 | [diff] [blame] | 3318 | |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 3319 | if (!FunctionType::isValidReturnType(*$4)) |
Chris Lattner | a925a14 | 2008-04-23 05:37:08 +0000 | [diff] [blame] | 3320 | GEN_ERROR("Invalid result type for LLVM function"); |
| 3321 | |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 3322 | Ty = FunctionType::get($4->get(), ParamTypes, false); |
Christopher Lamb | 4374f8e | 2007-12-17 01:17:35 +0000 | [diff] [blame] | 3323 | PFTy = PointerType::getUnqual(Ty); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3324 | } |
Chris Lattner | 6cdc682 | 2007-04-26 05:31:05 +0000 | [diff] [blame] | 3325 | |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 3326 | Value *V = getVal(PFTy, $5); // Get the function we're calling... |
Reid Spencer | 5b7e753 | 2006-09-28 19:28:24 +0000 | [diff] [blame] | 3327 | CHECK_FOR_ERROR |
Chris Lattner | 6cdc682 | 2007-04-26 05:31:05 +0000 | [diff] [blame] | 3328 | |
Reid Spencer | 7780acb | 2007-04-16 06:56:07 +0000 | [diff] [blame] | 3329 | // Check for call to invalid intrinsic to avoid crashing later. |
| 3330 | if (Function *theF = dyn_cast<Function>(V)) { |
Reid Spencer | ed48de2 | 2007-04-16 22:02:23 +0000 | [diff] [blame] | 3331 | if (theF->hasName() && (theF->getValueName()->getKeyLength() >= 5) && |
Reid Spencer | 36fdde1 | 2007-04-16 20:35:38 +0000 | [diff] [blame] | 3332 | (0 == strncmp(theF->getValueName()->getKeyData(), "llvm.", 5)) && |
| 3333 | !theF->getIntrinsicID(true)) |
Reid Spencer | 7780acb | 2007-04-16 06:56:07 +0000 | [diff] [blame] | 3334 | GEN_ERROR("Call to invalid LLVM intrinsic function '" + |
| 3335 | theF->getName() + "'"); |
| 3336 | } |
| 3337 | |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 3338 | // Set up the Attributes for the function |
| 3339 | SmallVector<AttributeWithIndex, 8> Attrs; |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 3340 | //FIXME : In 3.0, stop accepting zext, sext and inreg as optional function |
| 3341 | //attributes. |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 3342 | Attributes RetAttrs = $3; |
| 3343 | if ($9 != Attribute::None) { |
| 3344 | if ($9 & Attribute::ZExt) { |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 3345 | RetAttrs = RetAttrs | Attribute::ZExt; |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 3346 | $9 = $9 ^ Attribute::ZExt; |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 3347 | } |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 3348 | if ($9 & Attribute::SExt) { |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 3349 | RetAttrs = RetAttrs | Attribute::SExt; |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 3350 | $9 = $9 ^ Attribute::SExt; |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 3351 | } |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 3352 | if ($9 & Attribute::InReg) { |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 3353 | RetAttrs = RetAttrs | Attribute::InReg; |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 3354 | $9 = $9 ^ Attribute::InReg; |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 3355 | } |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 3356 | } |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 3357 | if (RetAttrs != Attribute::None) |
| 3358 | Attrs.push_back(AttributeWithIndex::get(0, RetAttrs)); |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 3359 | |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 3360 | // Check the arguments |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 3361 | ValueList Args; |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 3362 | if ($7->empty()) { // Has no arguments? |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3363 | // Make sure no arguments is a good thing! |
| 3364 | if (Ty->getNumParams() != 0) |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 3365 | GEN_ERROR("No arguments passed to a function that " |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 3366 | "expects arguments"); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3367 | } else { // Has arguments? |
| 3368 | // Loop through FunctionType's arguments and ensure they are specified |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 3369 | // correctly. Also, gather any parameter attributes. |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3370 | FunctionType::param_iterator I = Ty->param_begin(); |
| 3371 | FunctionType::param_iterator E = Ty->param_end(); |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 3372 | ParamList::iterator ArgI = $7->begin(), ArgE = $7->end(); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 3373 | unsigned index = 1; |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3374 | |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 3375 | for (; ArgI != ArgE && I != E; ++ArgI, ++I, ++index) { |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 3376 | if (ArgI->Val->getType() != *I) |
| 3377 | GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" + |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 3378 | (*I)->getDescription() + "'"); |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 3379 | Args.push_back(ArgI->Val); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 3380 | if (ArgI->Attrs != Attribute::None) |
| 3381 | Attrs.push_back(AttributeWithIndex::get(index, ArgI->Attrs)); |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 3382 | } |
| 3383 | if (Ty->isVarArg()) { |
| 3384 | if (I == E) |
Chris Lattner | 3890561 | 2008-02-19 04:36:25 +0000 | [diff] [blame] | 3385 | for (; ArgI != ArgE; ++ArgI, ++index) { |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 3386 | Args.push_back(ArgI->Val); // push the remaining varargs |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 3387 | if (ArgI->Attrs != Attribute::None) |
| 3388 | Attrs.push_back(AttributeWithIndex::get(index, ArgI->Attrs)); |
Chris Lattner | 3890561 | 2008-02-19 04:36:25 +0000 | [diff] [blame] | 3389 | } |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 3390 | } else if (I != E || ArgI != ArgE) |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 3391 | GEN_ERROR("Invalid number of parameters detected"); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3392 | } |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 3393 | if ($9 != Attribute::None) |
| 3394 | Attrs.push_back(AttributeWithIndex::get(~0, $9)); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 3395 | |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 3396 | // Finish off the Attributes and check them |
| 3397 | AttrListPtr PAL; |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 3398 | if (!Attrs.empty()) |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 3399 | PAL = AttrListPtr::get(Attrs.begin(), Attrs.end()); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 3400 | |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 3401 | // Create the call node |
Gabor Greif | e64d248 | 2008-04-06 23:07:54 +0000 | [diff] [blame] | 3402 | CallInst *CI = CallInst::Create(V, Args.begin(), Args.end()); |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 3403 | CI->setTailCall($1); |
| 3404 | CI->setCallingConv($2); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 3405 | CI->setAttributes(PAL); |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 3406 | $$ = CI; |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 3407 | delete $7; |
| 3408 | delete $4; |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 3409 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3410 | } |
| 3411 | | MemoryInst { |
| 3412 | $$ = $1; |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 3413 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3414 | }; |
| 3415 | |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3416 | OptVolatile : VOLATILE { |
| 3417 | $$ = true; |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 3418 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3419 | } |
| 3420 | | /* empty */ { |
| 3421 | $$ = false; |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 3422 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3423 | }; |
| 3424 | |
| 3425 | |
| 3426 | |
| 3427 | MemoryInst : MALLOC Types OptCAlign { |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 3428 | if (!UpRefs.empty()) |
| 3429 | GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription()); |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 3430 | $$ = new MallocInst(*$2, 0, $3); |
| 3431 | delete $2; |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 3432 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3433 | } |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 3434 | | MALLOC Types ',' INTTYPE ValueRef OptCAlign { |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 3435 | if (!UpRefs.empty()) |
| 3436 | GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription()); |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 3437 | if ($4 != Type::Int32Ty) |
| 3438 | GEN_ERROR("Malloc array size is not a 32-bit integer!"); |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 3439 | Value* tmpVal = getVal($4, $5); |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 3440 | CHECK_FOR_ERROR |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 3441 | $$ = new MallocInst(*$2, tmpVal, $6); |
| 3442 | delete $2; |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3443 | } |
| 3444 | | ALLOCA Types OptCAlign { |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 3445 | if (!UpRefs.empty()) |
| 3446 | GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription()); |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 3447 | $$ = new AllocaInst(*$2, 0, $3); |
| 3448 | delete $2; |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 3449 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3450 | } |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 3451 | | ALLOCA Types ',' INTTYPE ValueRef OptCAlign { |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 3452 | if (!UpRefs.empty()) |
| 3453 | GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription()); |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 3454 | if ($4 != Type::Int32Ty) |
| 3455 | GEN_ERROR("Alloca array size is not a 32-bit integer!"); |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 3456 | Value* tmpVal = getVal($4, $5); |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 3457 | CHECK_FOR_ERROR |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 3458 | $$ = new AllocaInst(*$2, tmpVal, $6); |
| 3459 | delete $2; |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3460 | } |
| 3461 | | FREE ResolvedVal { |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 3462 | if (!isa<PointerType>($2->getType())) |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 3463 | GEN_ERROR("Trying to free nonpointer type " + |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 3464 | $2->getType()->getDescription() + ""); |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 3465 | $$ = new FreeInst($2); |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 3466 | CHECK_FOR_ERROR |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3467 | } |
| 3468 | |
Christopher Lamb | 5c10424 | 2007-04-22 20:09:11 +0000 | [diff] [blame] | 3469 | | OptVolatile LOAD Types ValueRef OptCAlign { |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 3470 | if (!UpRefs.empty()) |
| 3471 | GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription()); |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 3472 | if (!isa<PointerType>($3->get())) |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 3473 | GEN_ERROR("Can't load from nonpointer type: " + |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 3474 | (*$3)->getDescription()); |
| 3475 | if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType()) |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 3476 | GEN_ERROR("Can't load from pointer of non-first-class type: " + |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 3477 | (*$3)->getDescription()); |
| 3478 | Value* tmpVal = getVal(*$3, $4); |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 3479 | CHECK_FOR_ERROR |
Christopher Lamb | 5c10424 | 2007-04-22 20:09:11 +0000 | [diff] [blame] | 3480 | $$ = new LoadInst(tmpVal, "", $1, $5); |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 3481 | delete $3; |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3482 | } |
Christopher Lamb | 5c10424 | 2007-04-22 20:09:11 +0000 | [diff] [blame] | 3483 | | OptVolatile STORE ResolvedVal ',' Types ValueRef OptCAlign { |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 3484 | if (!UpRefs.empty()) |
| 3485 | GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription()); |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 3486 | const PointerType *PT = dyn_cast<PointerType>($5->get()); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3487 | if (!PT) |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 3488 | GEN_ERROR("Can't store to a nonpointer type: " + |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 3489 | (*$5)->getDescription()); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3490 | const Type *ElTy = PT->getElementType(); |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 3491 | if (ElTy != $3->getType()) |
| 3492 | GEN_ERROR("Can't store '" + $3->getType()->getDescription() + |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 3493 | "' into space of type '" + ElTy->getDescription() + "'"); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3494 | |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 3495 | Value* tmpVal = getVal(*$5, $6); |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 3496 | CHECK_FOR_ERROR |
Christopher Lamb | 5c10424 | 2007-04-22 20:09:11 +0000 | [diff] [blame] | 3497 | $$ = new StoreInst($3, tmpVal, $1, $7); |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 3498 | delete $5; |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3499 | } |
Dan Gohman | e4977cf | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 3500 | | GETRESULT Types ValueRef ',' EUINT64VAL { |
Dan Gohman | 1a57024 | 2008-07-23 00:54:54 +0000 | [diff] [blame] | 3501 | if (!UpRefs.empty()) |
| 3502 | GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription()); |
| 3503 | if (!isa<StructType>($2->get()) && !isa<ArrayType>($2->get())) |
| 3504 | GEN_ERROR("getresult insn requires an aggregate operand"); |
| 3505 | if (!ExtractValueInst::getIndexedType(*$2, $5)) |
| 3506 | GEN_ERROR("Invalid getresult index for type '" + |
| 3507 | (*$2)->getDescription()+ "'"); |
| 3508 | |
| 3509 | Value *tmpVal = getVal(*$2, $3); |
Devang Patel | 5a97097 | 2008-02-19 22:27:01 +0000 | [diff] [blame] | 3510 | CHECK_FOR_ERROR |
Dan Gohman | 1a57024 | 2008-07-23 00:54:54 +0000 | [diff] [blame] | 3511 | $$ = ExtractValueInst::Create(tmpVal, $5); |
| 3512 | delete $2; |
Devang Patel | 5a97097 | 2008-02-19 22:27:01 +0000 | [diff] [blame] | 3513 | } |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3514 | | GETELEMENTPTR Types ValueRef IndexList { |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 3515 | if (!UpRefs.empty()) |
| 3516 | GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription()); |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 3517 | if (!isa<PointerType>($2->get())) |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 3518 | GEN_ERROR("getelementptr insn requires pointer operand"); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3519 | |
Dan Gohman | 041e2eb | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 3520 | if (!GetElementPtrInst::getIndexedType(*$2, $4->begin(), $4->end())) |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 3521 | GEN_ERROR("Invalid getelementptr indices for type '" + |
Reid Spencer | b5334b0 | 2007-02-05 10:18:06 +0000 | [diff] [blame] | 3522 | (*$2)->getDescription()+ "'"); |
Reid Spencer | a132e04 | 2006-12-03 05:46:11 +0000 | [diff] [blame] | 3523 | Value* tmpVal = getVal(*$2, $3); |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 3524 | CHECK_FOR_ERROR |
Gabor Greif | e64d248 | 2008-04-06 23:07:54 +0000 | [diff] [blame] | 3525 | $$ = GetElementPtrInst::Create(tmpVal, $4->begin(), $4->end()); |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 3526 | delete $2; |
Reid Spencer | 5b7e753 | 2006-09-28 19:28:24 +0000 | [diff] [blame] | 3527 | delete $4; |
Dan Gohman | e4977cf | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 3528 | } |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 3529 | | EXTRACTVALUE Types ValueRef ConstantIndexList { |
Dan Gohman | e4977cf | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 3530 | if (!UpRefs.empty()) |
| 3531 | GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription()); |
| 3532 | if (!isa<StructType>($2->get()) && !isa<ArrayType>($2->get())) |
| 3533 | GEN_ERROR("extractvalue insn requires an aggregate operand"); |
| 3534 | |
| 3535 | if (!ExtractValueInst::getIndexedType(*$2, $4->begin(), $4->end())) |
| 3536 | GEN_ERROR("Invalid extractvalue indices for type '" + |
| 3537 | (*$2)->getDescription()+ "'"); |
| 3538 | Value* tmpVal = getVal(*$2, $3); |
| 3539 | CHECK_FOR_ERROR |
| 3540 | $$ = ExtractValueInst::Create(tmpVal, $4->begin(), $4->end()); |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 3541 | delete $2; |
Dan Gohman | e4977cf | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 3542 | delete $4; |
| 3543 | } |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 3544 | | INSERTVALUE Types ValueRef ',' Types ValueRef ConstantIndexList { |
Dan Gohman | e4977cf | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 3545 | if (!UpRefs.empty()) |
| 3546 | GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription()); |
| 3547 | if (!isa<StructType>($2->get()) && !isa<ArrayType>($2->get())) |
| 3548 | GEN_ERROR("extractvalue insn requires an aggregate operand"); |
| 3549 | |
| 3550 | if (ExtractValueInst::getIndexedType(*$2, $7->begin(), $7->end()) != $5->get()) |
| 3551 | GEN_ERROR("Invalid insertvalue indices for type '" + |
| 3552 | (*$2)->getDescription()+ "'"); |
| 3553 | Value* aggVal = getVal(*$2, $3); |
| 3554 | Value* tmpVal = getVal(*$5, $6); |
| 3555 | CHECK_FOR_ERROR |
| 3556 | $$ = InsertValueInst::Create(aggVal, tmpVal, $7->begin(), $7->end()); |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 3557 | delete $2; |
Dan Gohman | e4977cf | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 3558 | delete $5; |
| 3559 | delete $7; |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3560 | }; |
| 3561 | |
| 3562 | |
| 3563 | %% |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 3564 | |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 3565 | // common code from the two 'RunVMAsmParser' functions |
| 3566 | static Module* RunParser(Module * M) { |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 3567 | CurModule.CurrentModule = M; |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 3568 | // Check to make sure the parser succeeded |
| 3569 | if (yyparse()) { |
| 3570 | if (ParserResult) |
| 3571 | delete ParserResult; |
| 3572 | return 0; |
| 3573 | } |
| 3574 | |
Reid Spencer | 0d60b5a | 2007-03-30 01:37:39 +0000 | [diff] [blame] | 3575 | // Emit an error if there are any unresolved types left. |
| 3576 | if (!CurModule.LateResolveTypes.empty()) { |
| 3577 | const ValID &DID = CurModule.LateResolveTypes.begin()->first; |
| 3578 | if (DID.Type == ValID::LocalName) { |
| 3579 | GenerateError("Undefined type remains at eof: '"+DID.getName() + "'"); |
| 3580 | } else { |
| 3581 | GenerateError("Undefined type remains at eof: #" + itostr(DID.Num)); |
| 3582 | } |
| 3583 | if (ParserResult) |
| 3584 | delete ParserResult; |
| 3585 | return 0; |
| 3586 | } |
| 3587 | |
| 3588 | // Emit an error if there are any unresolved values left. |
| 3589 | if (!CurModule.LateResolveValues.empty()) { |
| 3590 | Value *V = CurModule.LateResolveValues.back(); |
| 3591 | std::map<Value*, std::pair<ValID, int> >::iterator I = |
| 3592 | CurModule.PlaceHolderInfo.find(V); |
| 3593 | |
| 3594 | if (I != CurModule.PlaceHolderInfo.end()) { |
| 3595 | ValID &DID = I->second.first; |
| 3596 | if (DID.Type == ValID::LocalName) { |
| 3597 | GenerateError("Undefined value remains at eof: "+DID.getName() + "'"); |
| 3598 | } else { |
| 3599 | GenerateError("Undefined value remains at eof: #" + itostr(DID.Num)); |
| 3600 | } |
| 3601 | if (ParserResult) |
| 3602 | delete ParserResult; |
| 3603 | return 0; |
| 3604 | } |
| 3605 | } |
| 3606 | |
Reid Spencer | 1431061 | 2006-12-31 05:40:51 +0000 | [diff] [blame] | 3607 | // Check to make sure that parsing produced a result |
| 3608 | if (!ParserResult) |
| 3609 | return 0; |
| 3610 | |
| 3611 | // Reset ParserResult variable while saving its value for the result. |
| 3612 | Module *Result = ParserResult; |
| 3613 | ParserResult = 0; |
| 3614 | |
| 3615 | return Result; |
| 3616 | } |
| 3617 | |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 3618 | void llvm::GenerateError(const std::string &message, int LineNo) { |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 3619 | if (LineNo == -1) LineNo = LLLgetLineNo(); |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 3620 | // TODO: column number in exception |
| 3621 | if (TheParseError) |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 3622 | TheParseError->setError(LLLgetFilename(), message, LineNo); |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 3623 | TriggerError = 1; |
| 3624 | } |
| 3625 | |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3626 | int yyerror(const char *ErrorMsg) { |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 3627 | std::string where = LLLgetFilename() + ":" + utostr(LLLgetLineNo()) + ": "; |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 3628 | std::string errMsg = where + "error: " + std::string(ErrorMsg); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 3629 | if (yychar != YYEMPTY && yychar != 0) { |
| 3630 | errMsg += " while reading token: '"; |
Eric Christopher | 2a5196f | 2008-09-24 04:55:49 +0000 | [diff] [blame] | 3631 | errMsg += std::string(LLLgetTokenStart(), |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 3632 | LLLgetTokenStart()+LLLgetTokenLength()) + "'"; |
| 3633 | } |
Reid Spencer | 61c83e0 | 2006-08-18 08:43:06 +0000 | [diff] [blame] | 3634 | GenerateError(errMsg); |
Chris Lattner | 58af2a1 | 2006-02-15 07:22:58 +0000 | [diff] [blame] | 3635 | return 0; |
| 3636 | } |