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