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