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