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