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