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