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