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