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