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