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