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