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