blob: 34e0f70b3c1186865beaef53bc19ece22711731e [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===-- llvmAsmParser.y - Parser for llvm assembly files --------*- C++ -*-===//
John Criswell856ba762003-10-21 15:17:13 +00002//
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//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
10// This file implements the bison parser for LLVM assembly languages files.
11//
Chris Lattnercf3056d2003-10-13 03:32:08 +000012//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +000013
Chris Lattner00950542001-06-06 20:29:01 +000014%{
15#include "ParserInternals.h"
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000016#include "llvm/Instructions.h"
Chris Lattner00950542001-06-06 20:29:01 +000017#include "llvm/Module.h"
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000018#include "llvm/SymbolTable.h"
Chris Lattner830b6f92004-04-05 01:30:04 +000019#include "llvm/Support/GetElementPtrTypeIterator.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000020#include "llvm/ADT/STLExtras.h"
Reid Spencer8ce1da72004-07-04 12:19:05 +000021#include <algorithm>
22#include <iostream>
Chris Lattner00950542001-06-06 20:29:01 +000023#include <list>
Chris Lattnerc188eeb2002-07-30 18:54:25 +000024#include <utility>
Chris Lattner00950542001-06-06 20:29:01 +000025
Chris Lattner386a3b72001-10-16 19:54:17 +000026int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
Chris Lattner09083092001-07-08 04:57:15 +000027int yylex(); // declaration" of xxx warnings.
Chris Lattner00950542001-06-06 20:29:01 +000028int yyparse();
29
Brian Gaeked0fde302003-11-11 22:41:34 +000030namespace llvm {
Chris Lattner86427632004-07-14 06:39:48 +000031 std::string CurFilename;
32}
33using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000034
Chris Lattner00950542001-06-06 20:29:01 +000035static Module *ParserResult;
Chris Lattner00950542001-06-06 20:29:01 +000036
Chris Lattner30c89792001-09-07 16:35:17 +000037// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
38// relating to upreferences in the input stream.
39//
40//#define DEBUG_UPREFS 1
41#ifdef DEBUG_UPREFS
Chris Lattner699f1eb2002-08-14 17:12:33 +000042#define UR_OUT(X) std::cerr << X
Chris Lattner30c89792001-09-07 16:35:17 +000043#else
44#define UR_OUT(X)
45#endif
46
Vikram S. Adved3f7eb02002-07-14 22:59:28 +000047#define YYERROR_VERBOSE 1
48
Chris Lattner7e708292002-06-25 16:13:24 +000049// This contains info used when building the body of a function. It is
50// destroyed when the function is completed.
Chris Lattner00950542001-06-06 20:29:01 +000051//
Chris Lattner9232b992003-04-22 18:42:41 +000052typedef std::vector<Value *> ValueList; // Numbered defs
Chris Lattner735f2702004-07-08 22:30:50 +000053static void ResolveDefinitions(std::map<const Type *,ValueList> &LateResolvers,
54 std::map<const Type *,ValueList> *FutureLateResolvers = 0);
Chris Lattner00950542001-06-06 20:29:01 +000055
56static struct PerModuleInfo {
57 Module *CurrentModule;
Chris Lattner735f2702004-07-08 22:30:50 +000058 std::map<const Type *, ValueList> Values; // Module level numbered definitions
59 std::map<const Type *,ValueList> LateResolveValues;
Chris Lattner16af11d2004-02-09 21:03:38 +000060 std::vector<PATypeHolder> Types;
Chris Lattner9232b992003-04-22 18:42:41 +000061 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner00950542001-06-06 20:29:01 +000062
Chris Lattnerbc721ed2004-07-13 08:28:21 +000063 /// PlaceHolderInfo - When temporary placeholder objects are created, remember
64 /// how they were referenced and one which line of the input they came from so
Chris Lattner22ae2322004-07-13 08:39:15 +000065 /// that we can resolve them later and print error messages as appropriate.
Chris Lattnerbc721ed2004-07-13 08:28:21 +000066 std::map<Value*, std::pair<ValID, int> > PlaceHolderInfo;
67
Chris Lattner2079fde2001-10-13 06:41:08 +000068 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
69 // references to global values. Global values may be referenced before they
70 // are defined, and if so, the temporary object that they represent is held
Reid Spencer3271ed52004-07-18 00:08:11 +000071 // here. This is used for forward references of GlobalValues.
Chris Lattner2079fde2001-10-13 06:41:08 +000072 //
Chris Lattner9232b992003-04-22 18:42:41 +000073 typedef std::map<std::pair<const PointerType *,
Chris Lattnerbc207592004-03-08 06:09:57 +000074 ValID>, GlobalValue*> GlobalRefsType;
Chris Lattner2079fde2001-10-13 06:41:08 +000075 GlobalRefsType GlobalRefs;
76
Chris Lattner00950542001-06-06 20:29:01 +000077 void ModuleDone() {
Chris Lattner7e708292002-06-25 16:13:24 +000078 // If we could not resolve some functions at function compilation time
79 // (calls to functions before they are defined), resolve them now... Types
80 // are resolved when the constant pool has been completely parsed.
Chris Lattner30c89792001-09-07 16:35:17 +000081 //
Chris Lattner00950542001-06-06 20:29:01 +000082 ResolveDefinitions(LateResolveValues);
83
Chris Lattner2079fde2001-10-13 06:41:08 +000084 // Check to make sure that all global value forward references have been
85 // resolved!
86 //
87 if (!GlobalRefs.empty()) {
Chris Lattner9232b992003-04-22 18:42:41 +000088 std::string UndefinedReferences = "Unresolved global references exist:\n";
Chris Lattner749ce032002-03-11 22:12:39 +000089
90 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
91 I != E; ++I) {
92 UndefinedReferences += " " + I->first.first->getDescription() + " " +
93 I->first.second.getName() + "\n";
94 }
95 ThrowException(UndefinedReferences);
Chris Lattner2079fde2001-10-13 06:41:08 +000096 }
97
Chris Lattner7e708292002-06-25 16:13:24 +000098 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +000099 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +0000100 CurrentModule = 0;
101 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000102
103
Chris Lattner8a327842004-07-14 21:44:00 +0000104 // GetForwardRefForGlobal - Check to see if there is a forward reference
105 // for this global. If so, remove it from the GlobalRefs map and return it.
106 // If not, just return null.
107 GlobalValue *GetForwardRefForGlobal(const PointerType *PTy, ValID ID) {
108 // Check to see if there is a forward reference to this global variable...
109 // if there is, eliminate it and patch the reference to use the new def'n.
110 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PTy, ID));
111 GlobalValue *Ret = 0;
112 if (I != GlobalRefs.end()) {
113 Ret = I->second;
114 GlobalRefs.erase(I);
115 }
116 return Ret;
117 }
Chris Lattner00950542001-06-06 20:29:01 +0000118} CurModule;
119
Chris Lattner79df7c02002-03-26 18:01:55 +0000120static struct PerFunctionInfo {
Chris Lattner7e708292002-06-25 16:13:24 +0000121 Function *CurrentFunction; // Pointer to current function being created
Chris Lattner00950542001-06-06 20:29:01 +0000122
Chris Lattner735f2702004-07-08 22:30:50 +0000123 std::map<const Type*, ValueList> Values; // Keep track of #'d definitions
124 std::map<const Type*, ValueList> LateResolveValues;
Chris Lattner7e708292002-06-25 16:13:24 +0000125 bool isDeclare; // Is this function a forward declararation?
Chris Lattner00950542001-06-06 20:29:01 +0000126
Chris Lattner2e2ed292004-07-14 06:28:35 +0000127 /// BBForwardRefs - When we see forward references to basic blocks, keep
128 /// track of them here.
129 std::map<BasicBlock*, std::pair<ValID, int> > BBForwardRefs;
130 std::vector<BasicBlock*> NumberedBlocks;
131 unsigned NextBBNum;
132
Chris Lattner79df7c02002-03-26 18:01:55 +0000133 inline PerFunctionInfo() {
134 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000135 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000136 }
137
Chris Lattner79df7c02002-03-26 18:01:55 +0000138 inline void FunctionStart(Function *M) {
139 CurrentFunction = M;
Chris Lattner2e2ed292004-07-14 06:28:35 +0000140 NextBBNum = 0;
Chris Lattner00950542001-06-06 20:29:01 +0000141 }
142
Chris Lattner79df7c02002-03-26 18:01:55 +0000143 void FunctionDone() {
Chris Lattner2e2ed292004-07-14 06:28:35 +0000144 NumberedBlocks.clear();
145
146 // Any forward referenced blocks left?
147 if (!BBForwardRefs.empty())
148 ThrowException("Undefined reference to label " +
Chris Lattner83beacd2005-02-24 04:59:49 +0000149 BBForwardRefs.begin()->first->getName());
Chris Lattner2e2ed292004-07-14 06:28:35 +0000150
151 // Resolve all forward references now.
Chris Lattner386a3b72001-10-16 19:54:17 +0000152 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000153
Chris Lattner7e708292002-06-25 16:13:24 +0000154 Values.clear(); // Clear out function local definitions
Chris Lattner79df7c02002-03-26 18:01:55 +0000155 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000156 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000157 }
Chris Lattner394f2eb2003-10-16 20:12:13 +0000158} CurFun; // Info for the current function...
Chris Lattner00950542001-06-06 20:29:01 +0000159
Chris Lattner394f2eb2003-10-16 20:12:13 +0000160static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000161
Chris Lattner00950542001-06-06 20:29:01 +0000162
163//===----------------------------------------------------------------------===//
164// Code to handle definitions of all the types
165//===----------------------------------------------------------------------===//
166
Chris Lattner735f2702004-07-08 22:30:50 +0000167static int InsertValue(Value *V,
168 std::map<const Type*,ValueList> &ValueTab = CurFun.Values) {
169 if (V->hasName()) return -1; // Is this a numbered definition?
Chris Lattner2079fde2001-10-13 06:41:08 +0000170
171 // Yes, insert the value into the value table...
Chris Lattner735f2702004-07-08 22:30:50 +0000172 ValueList &List = ValueTab[V->getType()];
173 List.push_back(V);
Chris Lattner16af11d2004-02-09 21:03:38 +0000174 return List.size()-1;
Chris Lattner00950542001-06-06 20:29:01 +0000175}
176
Chris Lattner30c89792001-09-07 16:35:17 +0000177static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000178 switch (D.Type) {
Chris Lattnera09000d2004-07-14 23:03:46 +0000179 case ValID::NumberVal: // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000180 // Module constants occupy the lowest numbered slots...
Chris Lattnera09000d2004-07-14 23:03:46 +0000181 if ((unsigned)D.Num < CurModule.Types.size())
182 return CurModule.Types[(unsigned)D.Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000183 break;
Chris Lattnera09000d2004-07-14 23:03:46 +0000184 case ValID::NameVal: // Is it a named definition?
185 if (const Type *N = CurModule.CurrentModule->getTypeByName(D.Name)) {
186 D.destroy(); // Free old strdup'd memory...
187 return N;
Chris Lattner6e6026b2002-11-20 18:36:02 +0000188 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000189 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000190 default:
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000191 ThrowException("Internal parser error: Invalid symbol type reference!");
Chris Lattner30c89792001-09-07 16:35:17 +0000192 }
193
194 // If we reached here, we referenced either a symbol that we don't know about
195 // or an id number that hasn't been read yet. We may be referencing something
196 // forward, so just create an entry to be resolved later and get to it...
197 //
198 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
199
Chris Lattner355ad1f2005-03-21 06:27:42 +0000200
201 if (inFunctionScope()) {
202 if (D.Type == ValID::NameVal)
203 ThrowException("Reference to an undefined type: '" + D.getName() + "'");
204 else
205 ThrowException("Reference to an undefined type: #" + itostr(D.Num));
Chris Lattner4a42e902001-10-22 05:56:09 +0000206 }
Chris Lattner30c89792001-09-07 16:35:17 +0000207
Chris Lattner355ad1f2005-03-21 06:27:42 +0000208 std::map<ValID, PATypeHolder>::iterator I =CurModule.LateResolveTypes.find(D);
209 if (I != CurModule.LateResolveTypes.end())
210 return I->second;
211
Chris Lattner82269592001-10-22 06:01:08 +0000212 Type *Typ = OpaqueType::get();
Chris Lattner355ad1f2005-03-21 06:27:42 +0000213 CurModule.LateResolveTypes.insert(std::make_pair(D, Typ));
Chris Lattner30c89792001-09-07 16:35:17 +0000214 return Typ;
Chris Lattner355ad1f2005-03-21 06:27:42 +0000215 }
Chris Lattner30c89792001-09-07 16:35:17 +0000216
Chris Lattner9232b992003-04-22 18:42:41 +0000217static Value *lookupInSymbolTable(const Type *Ty, const std::string &Name) {
Chris Lattner6e6026b2002-11-20 18:36:02 +0000218 SymbolTable &SymTab =
Chris Lattner394f2eb2003-10-16 20:12:13 +0000219 inFunctionScope() ? CurFun.CurrentFunction->getSymbolTable() :
Chris Lattner9705a152002-05-02 19:27:42 +0000220 CurModule.CurrentModule->getSymbolTable();
Chris Lattner6e6026b2002-11-20 18:36:02 +0000221 return SymTab.lookup(Ty, Name);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000222}
223
Chris Lattner2079fde2001-10-13 06:41:08 +0000224// getValNonImprovising - Look up the value specified by the provided type and
225// the provided ValID. If the value exists and has already been defined, return
226// it. Otherwise return null.
227//
228static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000229 if (isa<FunctionType>(Ty))
230 ThrowException("Functions are not values and "
231 "must be referenced as pointers");
Chris Lattner386a3b72001-10-16 19:54:17 +0000232
Chris Lattner30c89792001-09-07 16:35:17 +0000233 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000234 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner00950542001-06-06 20:29:01 +0000235 unsigned Num = (unsigned)D.Num;
236
237 // Module constants occupy the lowest numbered slots...
Chris Lattner735f2702004-07-08 22:30:50 +0000238 std::map<const Type*,ValueList>::iterator VI = CurModule.Values.find(Ty);
Chris Lattner16af11d2004-02-09 21:03:38 +0000239 if (VI != CurModule.Values.end()) {
240 if (Num < VI->second.size())
241 return VI->second[Num];
242 Num -= VI->second.size();
Chris Lattner00950542001-06-06 20:29:01 +0000243 }
244
245 // Make sure that our type is within bounds
Chris Lattner735f2702004-07-08 22:30:50 +0000246 VI = CurFun.Values.find(Ty);
Chris Lattner16af11d2004-02-09 21:03:38 +0000247 if (VI == CurFun.Values.end()) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000248
249 // Check that the number is within bounds...
Chris Lattner16af11d2004-02-09 21:03:38 +0000250 if (VI->second.size() <= Num) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000251
Chris Lattner16af11d2004-02-09 21:03:38 +0000252 return VI->second[Num];
Chris Lattner00950542001-06-06 20:29:01 +0000253 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000254
Chris Lattner1a1cb112001-09-30 22:46:54 +0000255 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000256 Value *N = lookupInSymbolTable(Ty, std::string(D.Name));
Chris Lattner2079fde2001-10-13 06:41:08 +0000257 if (N == 0) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000258
259 D.destroy(); // Free old strdup'd memory...
260 return N;
261 }
262
Chris Lattner2079fde2001-10-13 06:41:08 +0000263 // Check to make sure that "Ty" is an integral type, and that our
264 // value will fit into the specified type...
265 case ValID::ConstSIntVal: // Is it a constant pool reference??
Chris Lattnerd78700d2002-08-16 21:14:40 +0000266 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
267 ThrowException("Signed integral constant '" +
268 itostr(D.ConstPool64) + "' is invalid for type '" +
269 Ty->getDescription() + "'!");
270 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000271
272 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000273 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
274 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer3271ed52004-07-18 00:08:11 +0000275 ThrowException("Integral constant '" + utostr(D.UConstPool64) +
Chris Lattnerf8dff732002-07-18 05:18:37 +0000276 "' is invalid or out of range!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000277 } else { // This is really a signed reference. Transmogrify.
Reid Spencer3271ed52004-07-18 00:08:11 +0000278 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000279 }
280 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000281 return ConstantUInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000282 }
283
Chris Lattner2079fde2001-10-13 06:41:08 +0000284 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000285 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattner2079fde2001-10-13 06:41:08 +0000286 ThrowException("FP constant invalid for type!!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000287 return ConstantFP::get(Ty, D.ConstPoolFP);
Chris Lattner2079fde2001-10-13 06:41:08 +0000288
289 case ValID::ConstNullVal: // Is it a null value?
Chris Lattner9b625032002-05-06 16:15:30 +0000290 if (!isa<PointerType>(Ty))
Chris Lattner2079fde2001-10-13 06:41:08 +0000291 ThrowException("Cannot create a a non pointer null!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000292 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner2079fde2001-10-13 06:41:08 +0000293
Chris Lattner16710e92004-10-16 18:17:13 +0000294 case ValID::ConstUndefVal: // Is it an undef value?
295 return UndefValue::get(Ty);
296
Chris Lattnerd78700d2002-08-16 21:14:40 +0000297 case ValID::ConstantVal: // Fully resolved constant?
298 if (D.ConstantValue->getType() != Ty)
299 ThrowException("Constant expression type different from required type!");
300 return D.ConstantValue;
301
Chris Lattner30c89792001-09-07 16:35:17 +0000302 default:
303 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000304 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000305 } // End of switch
306
Chris Lattner2079fde2001-10-13 06:41:08 +0000307 assert(0 && "Unhandled case!");
308 return 0;
309}
310
Chris Lattner2079fde2001-10-13 06:41:08 +0000311// getVal - This function is identical to getValNonImprovising, except that if a
312// value is not already defined, it "improvises" by creating a placeholder var
313// that looks and acts just like the requested variable. When the value is
314// defined later, all uses of the placeholder variable are replaced with the
315// real thing.
316//
Chris Lattner64116f92004-07-14 01:33:11 +0000317static Value *getVal(const Type *Ty, const ValID &ID) {
318 if (Ty == Type::LabelTy)
319 ThrowException("Cannot use a basic block here");
Chris Lattner2079fde2001-10-13 06:41:08 +0000320
Chris Lattner64116f92004-07-14 01:33:11 +0000321 // See if the value has already been defined.
322 Value *V = getValNonImprovising(Ty, ID);
Chris Lattner2079fde2001-10-13 06:41:08 +0000323 if (V) return V;
Chris Lattner00950542001-06-06 20:29:01 +0000324
Chris Lattner60cd9552005-03-23 01:29:26 +0000325 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty))
326 ThrowException("Invalid use of a composite type!");
327
Chris Lattner00950542001-06-06 20:29:01 +0000328 // If we reached here, we referenced either a symbol that we don't know about
329 // or an id number that hasn't been read yet. We may be referencing something
330 // forward, so just create an entry to be resolved later and get to it...
331 //
Chris Lattner64116f92004-07-14 01:33:11 +0000332 V = new Argument(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000333
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000334 // Remember where this forward reference came from. FIXME, shouldn't we try
335 // to recycle these things??
Chris Lattner64116f92004-07-14 01:33:11 +0000336 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000337 llvmAsmlineno)));
338
Chris Lattner79df7c02002-03-26 18:01:55 +0000339 if (inFunctionScope())
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000340 InsertValue(V, CurFun.LateResolveValues);
Chris Lattner386a3b72001-10-16 19:54:17 +0000341 else
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000342 InsertValue(V, CurModule.LateResolveValues);
343 return V;
Chris Lattner00950542001-06-06 20:29:01 +0000344}
345
Chris Lattner2e2ed292004-07-14 06:28:35 +0000346/// getBBVal - This is used for two purposes:
347/// * If isDefinition is true, a new basic block with the specified ID is being
348/// defined.
349/// * If isDefinition is true, this is a reference to a basic block, which may
350/// or may not be a forward reference.
351///
352static BasicBlock *getBBVal(const ValID &ID, bool isDefinition = false) {
Chris Lattner64116f92004-07-14 01:33:11 +0000353 assert(inFunctionScope() && "Can't get basic block at global scope!");
354
Chris Lattner2e2ed292004-07-14 06:28:35 +0000355 std::string Name;
356 BasicBlock *BB = 0;
357 switch (ID.Type) {
358 default: ThrowException("Illegal label reference " + ID.getName());
359 case ValID::NumberVal: // Is it a numbered definition?
360 if (unsigned(ID.Num) >= CurFun.NumberedBlocks.size())
361 CurFun.NumberedBlocks.resize(ID.Num+1);
362 BB = CurFun.NumberedBlocks[ID.Num];
363 break;
364 case ValID::NameVal: // Is it a named definition?
365 Name = ID.Name;
Chris Lattnera09000d2004-07-14 23:03:46 +0000366 if (Value *N = CurFun.CurrentFunction->
367 getSymbolTable().lookup(Type::LabelTy, Name))
Chris Lattner2e2ed292004-07-14 06:28:35 +0000368 BB = cast<BasicBlock>(N);
369 break;
370 }
Chris Lattner64116f92004-07-14 01:33:11 +0000371
Chris Lattner2e2ed292004-07-14 06:28:35 +0000372 // See if the block has already been defined.
373 if (BB) {
374 // If this is the definition of the block, make sure the existing value was
375 // just a forward reference. If it was a forward reference, there will be
376 // an entry for it in the PlaceHolderInfo map.
377 if (isDefinition && !CurFun.BBForwardRefs.erase(BB))
378 // The existing value was a definition, not a forward reference.
379 ThrowException("Redefinition of label " + ID.getName());
Chris Lattner64116f92004-07-14 01:33:11 +0000380
Chris Lattner2e2ed292004-07-14 06:28:35 +0000381 ID.destroy(); // Free strdup'd memory.
382 return BB;
383 }
384
385 // Otherwise this block has not been seen before.
386 BB = new BasicBlock("", CurFun.CurrentFunction);
387 if (ID.Type == ValID::NameVal) {
388 BB->setName(ID.Name);
389 } else {
390 CurFun.NumberedBlocks[ID.Num] = BB;
391 }
392
393 // If this is not a definition, keep track of it so we can use it as a forward
394 // reference.
395 if (!isDefinition) {
396 // Remember where this forward reference came from.
397 CurFun.BBForwardRefs[BB] = std::make_pair(ID, llvmAsmlineno);
398 } else {
399 // The forward declaration could have been inserted anywhere in the
400 // function: insert it into the correct place now.
401 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
402 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
403 }
Chris Lattnere8343a52004-10-26 18:26:14 +0000404 ID.destroy();
Chris Lattner64116f92004-07-14 01:33:11 +0000405 return BB;
406}
407
Chris Lattner00950542001-06-06 20:29:01 +0000408
409//===----------------------------------------------------------------------===//
410// Code to handle forward references in instructions
411//===----------------------------------------------------------------------===//
412//
413// This code handles the late binding needed with statements that reference
414// values not defined yet... for example, a forward branch, or the PHI node for
415// a loop body.
416//
Chris Lattner394f2eb2003-10-16 20:12:13 +0000417// This keeps a table (CurFun.LateResolveValues) of all such forward references
Chris Lattner00950542001-06-06 20:29:01 +0000418// and back patchs after we are done.
419//
420
421// ResolveDefinitions - If we could not resolve some defs at parsing
422// time (forward branches, phi functions for loops, etc...) resolve the
423// defs now...
424//
Chris Lattner735f2702004-07-08 22:30:50 +0000425static void ResolveDefinitions(std::map<const Type*,ValueList> &LateResolvers,
426 std::map<const Type*,ValueList> *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000427 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Chris Lattner735f2702004-07-08 22:30:50 +0000428 for (std::map<const Type*,ValueList>::iterator LRI = LateResolvers.begin(),
Chris Lattner16af11d2004-02-09 21:03:38 +0000429 E = LateResolvers.end(); LRI != E; ++LRI) {
430 ValueList &List = LRI->second;
431 while (!List.empty()) {
432 Value *V = List.back();
433 List.pop_back();
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000434
435 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
436 CurModule.PlaceHolderInfo.find(V);
437 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
438
439 ValID &DID = PHI->second.first;
Chris Lattner00950542001-06-06 20:29:01 +0000440
Chris Lattner735f2702004-07-08 22:30:50 +0000441 Value *TheRealValue = getValNonImprovising(LRI->first, DID);
Chris Lattner386a3b72001-10-16 19:54:17 +0000442 if (TheRealValue) {
443 V->replaceAllUsesWith(TheRealValue);
444 delete V;
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000445 CurModule.PlaceHolderInfo.erase(PHI);
Chris Lattner386a3b72001-10-16 19:54:17 +0000446 } else if (FutureLateResolvers) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000447 // Functions have their unresolved items forwarded to the module late
Chris Lattner386a3b72001-10-16 19:54:17 +0000448 // resolver table
449 InsertValue(V, *FutureLateResolvers);
450 } else {
Reid Spencer3271ed52004-07-18 00:08:11 +0000451 if (DID.Type == ValID::NameVal)
452 ThrowException("Reference to an invalid definition: '" +DID.getName()+
453 "' of type '" + V->getType()->getDescription() + "'",
454 PHI->second.second);
455 else
456 ThrowException("Reference to an invalid definition: #" +
457 itostr(DID.Num) + " of type '" +
458 V->getType()->getDescription() + "'",
459 PHI->second.second);
Chris Lattner30c89792001-09-07 16:35:17 +0000460 }
Chris Lattner00950542001-06-06 20:29:01 +0000461 }
462 }
463
464 LateResolvers.clear();
465}
466
Chris Lattner4a42e902001-10-22 05:56:09 +0000467// ResolveTypeTo - A brand new type was just declared. This means that (if
468// name is not null) things referencing Name can be resolved. Otherwise, things
469// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000470//
Chris Lattner4a42e902001-10-22 05:56:09 +0000471static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner355ad1f2005-03-21 06:27:42 +0000472 ValID D;
473 if (Name) D = ValID::create(Name);
474 else D = ValID::create((int)CurModule.Types.size());
Chris Lattner00950542001-06-06 20:29:01 +0000475
Chris Lattner355ad1f2005-03-21 06:27:42 +0000476 std::map<ValID, PATypeHolder>::iterator I =
477 CurModule.LateResolveTypes.find(D);
478 if (I != CurModule.LateResolveTypes.end()) {
479 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
480 CurModule.LateResolveTypes.erase(I);
Chris Lattner30c89792001-09-07 16:35:17 +0000481 }
482}
483
Chris Lattner1781aca2001-09-18 04:00:54 +0000484// setValueName - Set the specified value to the name given. The name may be
485// null potentially, in which case this is a noop. The string passed in is
Chris Lattner8ab406d2004-07-13 07:59:27 +0000486// assumed to be a malloc'd string buffer, and is free'd by this function.
487//
488static void setValueName(Value *V, char *NameStr) {
489 if (NameStr) {
490 std::string Name(NameStr); // Copy string
491 free(NameStr); // Free old string
Chris Lattnerc9aea522004-07-13 08:12:39 +0000492
493 if (V->getType() == Type::VoidTy)
494 ThrowException("Can't assign name '" + Name+"' to value with void type!");
Chris Lattner8ab406d2004-07-13 07:59:27 +0000495
496 assert(inFunctionScope() && "Must be in function scope!");
497 SymbolTable &ST = CurFun.CurrentFunction->getSymbolTable();
498 if (ST.lookup(V->getType(), Name))
499 ThrowException("Redefinition of value named '" + Name + "' in the '" +
500 V->getType()->getDescription() + "' type plane!");
501
Chris Lattnerc9aea522004-07-13 08:12:39 +0000502 // Set the name.
Chris Lattner262bb9a2005-03-05 19:04:07 +0000503 V->setName(Name);
Chris Lattner8ab406d2004-07-13 07:59:27 +0000504 }
505}
506
Chris Lattnerd88998d2004-07-14 08:23:52 +0000507/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
508/// this is a declaration, otherwise it is a definition.
509static void ParseGlobalVariable(char *NameStr,GlobalValue::LinkageTypes Linkage,
510 bool isConstantGlobal, const Type *Ty,
511 Constant *Initializer) {
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000512 if (isa<FunctionType>(Ty))
513 ThrowException("Cannot declare global vars of function type!");
514
Chris Lattnera09000d2004-07-14 23:03:46 +0000515 const PointerType *PTy = PointerType::get(Ty);
516
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000517 std::string Name;
518 if (NameStr) {
519 Name = NameStr; // Copy string
520 free(NameStr); // Free old string
Chris Lattnerd88998d2004-07-14 08:23:52 +0000521 }
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000522
Chris Lattner8a327842004-07-14 21:44:00 +0000523 // See if this global value was forward referenced. If so, recycle the
524 // object.
525 ValID ID;
526 if (!Name.empty()) {
527 ID = ValID::create((char*)Name.c_str());
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000528 } else {
Chris Lattner8a327842004-07-14 21:44:00 +0000529 ID = ValID::create((int)CurModule.Values[PTy].size());
530 }
531
532 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
533 // Move the global to the end of the list, from whereever it was
534 // previously inserted.
535 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
536 CurModule.CurrentModule->getGlobalList().remove(GV);
537 CurModule.CurrentModule->getGlobalList().push_back(GV);
538 GV->setInitializer(Initializer);
539 GV->setLinkage(Linkage);
540 GV->setConstant(isConstantGlobal);
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000541 InsertValue(GV, CurModule.Values);
Chris Lattnera09000d2004-07-14 23:03:46 +0000542 return;
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000543 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000544
545 // If this global has a name, check to see if there is already a definition
546 // of this global in the module. If so, merge as appropriate. Note that
547 // this is really just a hack around problems in the CFE. :(
548 if (!Name.empty()) {
549 // We are a simple redefinition of a value, check to see if it is defined
550 // the same as the old one.
551 if (GlobalVariable *EGV =
552 CurModule.CurrentModule->getGlobalVariable(Name, Ty)) {
553 // We are allowed to redefine a global variable in two circumstances:
554 // 1. If at least one of the globals is uninitialized or
555 // 2. If both initializers have the same value.
556 //
557 if (!EGV->hasInitializer() || !Initializer ||
558 EGV->getInitializer() == Initializer) {
559
560 // Make sure the existing global version gets the initializer! Make
561 // sure that it also gets marked const if the new version is.
562 if (Initializer && !EGV->hasInitializer())
563 EGV->setInitializer(Initializer);
564 if (isConstantGlobal)
565 EGV->setConstant(true);
566 EGV->setLinkage(Linkage);
567 return;
568 }
569
570 ThrowException("Redefinition of global variable named '" + Name +
571 "' in the '" + Ty->getDescription() + "' type plane!");
572 }
573 }
574
575 // Otherwise there is no existing GV to use, create one now.
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000576 GlobalVariable *GV =
577 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
578 CurModule.CurrentModule);
579 InsertValue(GV, CurModule.Values);
Chris Lattnerd88998d2004-07-14 08:23:52 +0000580}
Chris Lattner8ab406d2004-07-13 07:59:27 +0000581
Reid Spencer75719bf2004-05-26 21:48:31 +0000582// setTypeName - Set the specified type to the name given. The name may be
583// null potentially, in which case this is a noop. The string passed in is
584// assumed to be a malloc'd string buffer, and is freed by this function.
585//
586// This function returns true if the type has already been defined, but is
587// allowed to be redefined in the specified context. If the name is a new name
588// for the type plane, it is inserted and false is returned.
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000589static bool setTypeName(const Type *T, char *NameStr) {
Chris Lattnera09000d2004-07-14 23:03:46 +0000590 assert(!inFunctionScope() && "Can't give types function-local names!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000591 if (NameStr == 0) return false;
592
593 std::string Name(NameStr); // Copy string
594 free(NameStr); // Free old string
595
596 // We don't allow assigning names to void type
597 if (T == Type::VoidTy)
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000598 ThrowException("Can't assign name '" + Name + "' to the void type!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000599
Chris Lattnera09000d2004-07-14 23:03:46 +0000600 // Set the type name, checking for conflicts as we do so.
601 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000602
Chris Lattnera09000d2004-07-14 23:03:46 +0000603 if (AlreadyExists) { // Inserting a name that is already defined???
604 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
605 assert(Existing && "Conflict but no matching type?");
606
Reid Spencer75719bf2004-05-26 21:48:31 +0000607 // There is only one case where this is allowed: when we are refining an
608 // opaque type. In this case, Existing will be an opaque type.
609 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
610 // We ARE replacing an opaque type!
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000611 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000612 return true;
613 }
614
615 // Otherwise, this is an attempt to redefine a type. That's okay if
616 // the redefinition is identical to the original. This will be so if
617 // Existing and T point to the same Type object. In this one case we
618 // allow the equivalent redefinition.
619 if (Existing == T) return true; // Yes, it's equal.
620
621 // Any other kind of (non-equivalent) redefinition is an error.
622 ThrowException("Redefinition of type named '" + Name + "' in the '" +
Reid Spencer3271ed52004-07-18 00:08:11 +0000623 T->getDescription() + "' type plane!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000624 }
625
Reid Spencer75719bf2004-05-26 21:48:31 +0000626 return false;
627}
Chris Lattner8896eda2001-07-09 19:38:36 +0000628
Chris Lattner30c89792001-09-07 16:35:17 +0000629//===----------------------------------------------------------------------===//
630// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000631//
Chris Lattner8896eda2001-07-09 19:38:36 +0000632
Chris Lattner3b073862004-02-09 03:19:29 +0000633// TypeContains - Returns true if Ty directly contains E in it.
Chris Lattner30c89792001-09-07 16:35:17 +0000634//
635static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner6d8dbec2004-12-08 16:13:53 +0000636 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
637 E) != Ty->subtype_end();
Chris Lattner3b073862004-02-09 03:19:29 +0000638}
639
640namespace {
641 struct UpRefRecord {
642 // NestingLevel - The number of nesting levels that need to be popped before
643 // this type is resolved.
644 unsigned NestingLevel;
645
646 // LastContainedTy - This is the type at the current binding level for the
647 // type. Every time we reduce the nesting level, this gets updated.
648 const Type *LastContainedTy;
649
650 // UpRefTy - This is the actual opaque type that the upreference is
651 // represented with.
652 OpaqueType *UpRefTy;
653
654 UpRefRecord(unsigned NL, OpaqueType *URTy)
655 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
656 };
Chris Lattner30c89792001-09-07 16:35:17 +0000657}
Chris Lattner698b56e2001-07-20 19:15:08 +0000658
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000659// UpRefs - A list of the outstanding upreferences that need to be resolved.
Chris Lattner3b073862004-02-09 03:19:29 +0000660static std::vector<UpRefRecord> UpRefs;
Chris Lattner30c89792001-09-07 16:35:17 +0000661
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000662/// HandleUpRefs - Every time we finish a new layer of types, this function is
663/// called. It loops through the UpRefs vector, which is a list of the
664/// currently active types. For each type, if the up reference is contained in
665/// the newly completed type, we decrement the level count. When the level
666/// count reaches zero, the upreferenced type is the type that is passed in:
667/// thus we can complete the cycle.
668///
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000669static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner2c37c182004-02-09 03:03:10 +0000670 if (!ty->isAbstract()) return ty;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000671 PATypeHolder Ty(ty);
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000672 UR_OUT("Type '" << Ty->getDescription() <<
Chris Lattner5084d032001-11-02 07:46:26 +0000673 "' newly formed. Resolving upreferences.\n" <<
674 UpRefs.size() << " upreferences active!\n");
Chris Lattner026a8ce2004-02-09 18:53:54 +0000675
676 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
677 // to zero), we resolve them all together before we resolve them to Ty. At
678 // the end of the loop, if there is anything to resolve to Ty, it will be in
679 // this variable.
680 OpaqueType *TypeToResolve = 0;
681
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000682 for (unsigned i = 0; i != UpRefs.size(); ++i) {
Chris Lattner5084d032001-11-02 07:46:26 +0000683 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Reid Spencer3271ed52004-07-18 00:08:11 +0000684 << UpRefs[i].second->getDescription() << ") = "
685 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
Chris Lattner3b073862004-02-09 03:19:29 +0000686 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
687 // Decrement level of upreference
688 unsigned Level = --UpRefs[i].NestingLevel;
689 UpRefs[i].LastContainedTy = Ty;
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000690 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
Chris Lattner30c89792001-09-07 16:35:17 +0000691 if (Level == 0) { // Upreference should be resolved!
Chris Lattner026a8ce2004-02-09 18:53:54 +0000692 if (!TypeToResolve) {
693 TypeToResolve = UpRefs[i].UpRefTy;
694 } else {
695 UR_OUT(" * Resolving upreference for "
696 << UpRefs[i].second->getDescription() << "\n";
697 std::string OldName = UpRefs[i].UpRefTy->getDescription());
698 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
699 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
700 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
701 }
Reid Spencer3271ed52004-07-18 00:08:11 +0000702 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000703 --i; // Do not skip the next element...
Chris Lattner30c89792001-09-07 16:35:17 +0000704 }
705 }
Chris Lattner8896eda2001-07-09 19:38:36 +0000706 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000707
Chris Lattner026a8ce2004-02-09 18:53:54 +0000708 if (TypeToResolve) {
709 UR_OUT(" * Resolving upreference for "
710 << UpRefs[i].second->getDescription() << "\n";
Chris Lattner16af11d2004-02-09 21:03:38 +0000711 std::string OldName = TypeToResolve->getDescription());
Chris Lattner026a8ce2004-02-09 18:53:54 +0000712 TypeToResolve->refineAbstractTypeTo(Ty);
713 }
714
Chris Lattner8896eda2001-07-09 19:38:36 +0000715 return Ty;
716}
717
Chris Lattner30c89792001-09-07 16:35:17 +0000718
Chris Lattner00950542001-06-06 20:29:01 +0000719//===----------------------------------------------------------------------===//
720// RunVMAsmParser - Define an interface to this parser
721//===----------------------------------------------------------------------===//
722//
Chris Lattner86427632004-07-14 06:39:48 +0000723Module *llvm::RunVMAsmParser(const std::string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000724 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000725 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000726 llvmAsmlineno = 1; // Reset the current line number...
727
Chris Lattner75f20532003-04-22 18:02:52 +0000728 // Allocate a new module to read
729 CurModule.CurrentModule = new Module(Filename);
Chris Lattner42570982003-11-26 07:24:58 +0000730
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000731 yyparse(); // Parse the file, potentially throwing exception
Chris Lattner99e7ab72003-10-18 05:53:13 +0000732
Chris Lattner00950542001-06-06 20:29:01 +0000733 Module *Result = ParserResult;
Chris Lattner99e7ab72003-10-18 05:53:13 +0000734
Chris Lattner00950542001-06-06 20:29:01 +0000735 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
736 ParserResult = 0;
737
738 return Result;
739}
740
741%}
742
743%union {
Brian Gaeked0fde302003-11-11 22:41:34 +0000744 llvm::Module *ModuleVal;
745 llvm::Function *FunctionVal;
746 std::pair<llvm::PATypeHolder*, char*> *ArgVal;
747 llvm::BasicBlock *BasicBlockVal;
748 llvm::TerminatorInst *TermInstVal;
749 llvm::Instruction *InstVal;
750 llvm::Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000751
Brian Gaeked0fde302003-11-11 22:41:34 +0000752 const llvm::Type *PrimType;
753 llvm::PATypeHolder *TypeVal;
754 llvm::Value *ValueVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000755
Brian Gaeked0fde302003-11-11 22:41:34 +0000756 std::vector<std::pair<llvm::PATypeHolder*,char*> > *ArgList;
757 std::vector<llvm::Value*> *ValueList;
758 std::list<llvm::PATypeHolder> *TypeList;
759 std::list<std::pair<llvm::Value*,
760 llvm::BasicBlock*> > *PHIList; // Represent the RHS of PHI node
761 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
762 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000763
Brian Gaeked0fde302003-11-11 22:41:34 +0000764 llvm::GlobalValue::LinkageTypes Linkage;
Chris Lattner30c89792001-09-07 16:35:17 +0000765 int64_t SInt64Val;
766 uint64_t UInt64Val;
767 int SIntVal;
768 unsigned UIntVal;
769 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000770 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000771
Chris Lattner30c89792001-09-07 16:35:17 +0000772 char *StrVal; // This memory is strdup'd!
Brian Gaeked0fde302003-11-11 22:41:34 +0000773 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000774
Brian Gaeked0fde302003-11-11 22:41:34 +0000775 llvm::Instruction::BinaryOps BinaryOpVal;
776 llvm::Instruction::TermOps TermOpVal;
777 llvm::Instruction::MemoryOps MemOpVal;
778 llvm::Instruction::OtherOps OtherOpVal;
779 llvm::Module::Endianness Endianness;
Chris Lattner00950542001-06-06 20:29:01 +0000780}
781
Chris Lattner79df7c02002-03-26 18:01:55 +0000782%type <ModuleVal> Module FunctionList
783%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000784%type <BasicBlockVal> BasicBlock InstructionList
785%type <TermInstVal> BBTerminatorInst
786%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000787%type <ConstVal> ConstVal ConstExpr
Chris Lattner6cdb0112001-11-26 16:54:11 +0000788%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000789%type <ArgList> ArgList ArgListH
790%type <ArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000791%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000792%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000793%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000794%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000795%type <JumpTable> JumpTable
Chris Lattner4ad02e72003-04-16 20:28:45 +0000796%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner15c9c032003-09-08 18:20:29 +0000797%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattnerddb6db42005-05-06 05:51:46 +0000798%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
Chris Lattner4ad02e72003-04-16 20:28:45 +0000799%type <Linkage> OptLinkage
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000800%type <Endianness> BigOrLittle
Chris Lattner00950542001-06-06 20:29:01 +0000801
Chris Lattner2079fde2001-10-13 06:41:08 +0000802// ValueRef - Unresolved reference to a definition or BB
803%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000804%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000805// Tokens and types for handling constant integer values
806//
807// ESINT64VAL - A negative number within long long range
808%token <SInt64Val> ESINT64VAL
809
810// EUINT64VAL - A positive number within uns. long long range
811%token <UInt64Val> EUINT64VAL
812%type <SInt64Val> EINT64VAL
813
814%token <SIntVal> SINTVAL // Signed 32 bit ints...
815%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
816%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000817%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000818
819// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000820%type <TypeVal> Types TypesV UpRTypes UpRTypesV
821%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattner30c89792001-09-07 16:35:17 +0000822%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
823%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000824
Chris Lattner6c23f572003-08-22 05:42:10 +0000825%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
826%type <StrVal> Name OptName OptAssign
Chris Lattner00950542001-06-06 20:29:01 +0000827
828
Chris Lattner91ef4602004-03-31 03:49:47 +0000829%token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Chris Lattner15c9c032003-09-08 18:20:29 +0000830%token DECLARE GLOBAL CONSTANT VOLATILE
Chris Lattner16710e92004-10-16 18:17:13 +0000831%token TO DOTDOTDOT NULL_TOK UNDEF CONST INTERNAL LINKONCE WEAK APPENDING
Reid Spencer0be13e72004-07-25 17:58:28 +0000832%token OPAQUE NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG
Chris Lattnerddb6db42005-05-06 05:51:46 +0000833%token DEPLIBS CALL TAIL
Chris Lattner00950542001-06-06 20:29:01 +0000834
835// Basic Block Terminating Operators
Chris Lattner16710e92004-10-16 18:17:13 +0000836%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
Chris Lattner00950542001-06-06 20:29:01 +0000837
Chris Lattner00950542001-06-06 20:29:01 +0000838// Binary Operators
Chris Lattner4a6482b2002-09-10 19:57:26 +0000839%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Chris Lattner42c9e772001-10-20 09:32:59 +0000840%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000841%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000842
843// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000844%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000845
Chris Lattner027dcc52001-07-08 21:10:27 +0000846// Other Operators
847%type <OtherOpVal> ShiftOps
Chris Lattnerddb6db42005-05-06 05:51:46 +0000848%token <OtherOpVal> PHI_TOK CAST SELECT SHL SHR VAARG VANEXT
849
Chris Lattner027dcc52001-07-08 21:10:27 +0000850
Chris Lattner00950542001-06-06 20:29:01 +0000851%start Module
852%%
853
854// Handle constant integer size restriction and conversion...
855//
Chris Lattner51727be2002-06-04 21:58:56 +0000856INTVAL : SINTVAL;
Chris Lattner00950542001-06-06 20:29:01 +0000857INTVAL : UINTVAL {
858 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
859 ThrowException("Value too large for type!");
860 $$ = (int32_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000861};
Chris Lattner00950542001-06-06 20:29:01 +0000862
863
Chris Lattner51727be2002-06-04 21:58:56 +0000864EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner00950542001-06-06 20:29:01 +0000865EINT64VAL : EUINT64VAL {
866 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
867 ThrowException("Value too large for type!");
868 $$ = (int64_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000869};
Chris Lattner00950542001-06-06 20:29:01 +0000870
Chris Lattner00950542001-06-06 20:29:01 +0000871// Operations that are notably excluded from this list include:
872// RET, BR, & SWITCH because they end basic blocks and are treated specially.
873//
Chris Lattner4a6482b2002-09-10 19:57:26 +0000874ArithmeticOps: ADD | SUB | MUL | DIV | REM;
875LogicalOps : AND | OR | XOR;
876SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
Chris Lattner4a6482b2002-09-10 19:57:26 +0000877
Chris Lattner51727be2002-06-04 21:58:56 +0000878ShiftOps : SHL | SHR;
Chris Lattner00950542001-06-06 20:29:01 +0000879
Chris Lattnerddb6db42005-05-06 05:51:46 +0000880
881
882
Chris Lattnere98dda62001-07-14 06:10:16 +0000883// These are some types that allow classification if we only want a particular
884// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner51727be2002-06-04 21:58:56 +0000885SIntType : LONG | INT | SHORT | SBYTE;
886UIntType : ULONG | UINT | USHORT | UBYTE;
887IntType : SIntType | UIntType;
888FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +0000889
Chris Lattnere98dda62001-07-14 06:10:16 +0000890// OptAssign - Value producing statements have an optional assignment component
Chris Lattner6c23f572003-08-22 05:42:10 +0000891OptAssign : Name '=' {
Chris Lattner00950542001-06-06 20:29:01 +0000892 $$ = $1;
893 }
894 | /*empty*/ {
895 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +0000896 };
Chris Lattner00950542001-06-06 20:29:01 +0000897
Chris Lattner4ad02e72003-04-16 20:28:45 +0000898OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
899 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
Chris Lattner72ac148d2003-10-16 18:29:00 +0000900 WEAK { $$ = GlobalValue::WeakLinkage; } |
Chris Lattner4ad02e72003-04-16 20:28:45 +0000901 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
902 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
Chris Lattner30c89792001-09-07 16:35:17 +0000903
904//===----------------------------------------------------------------------===//
905// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +0000906// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +0000907// access to it, a user must explicitly use TypesV.
908//
909
910// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +0000911TypesV : Types | VOID { $$ = new PATypeHolder($1); };
912UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +0000913
914Types : UpRTypes {
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000915 if (!UpRefs.empty())
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000916 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
917 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000918 };
Chris Lattner30c89792001-09-07 16:35:17 +0000919
920
921// Derived types are added later...
922//
Chris Lattner51727be2002-06-04 21:58:56 +0000923PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
924PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000925UpRTypes : OPAQUE {
926 $$ = new PATypeHolder(OpaqueType::get());
927 }
928 | PrimType {
929 $$ = new PATypeHolder($1);
Chris Lattner51727be2002-06-04 21:58:56 +0000930 };
Chris Lattnerd78700d2002-08-16 21:14:40 +0000931UpRTypes : SymbolicValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000932 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner51727be2002-06-04 21:58:56 +0000933};
Chris Lattner30c89792001-09-07 16:35:17 +0000934
Chris Lattner30c89792001-09-07 16:35:17 +0000935// Include derived types in the Types production.
936//
937UpRTypes : '\\' EUINT64VAL { // Type UpReference
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000938 if ($2 > (uint64_t)~0U) ThrowException("Value out of range!");
Chris Lattner30c89792001-09-07 16:35:17 +0000939 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner3b073862004-02-09 03:19:29 +0000940 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000941 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +0000942 UR_OUT("New Upreference!\n");
943 }
Chris Lattner79df7c02002-03-26 18:01:55 +0000944 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner9232b992003-04-22 18:42:41 +0000945 std::vector<const Type*> Params;
Chris Lattnera08976b2005-02-22 23:13:58 +0000946 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
947 E = $3->end(); I != E; ++I)
948 Params.push_back(*I);
Chris Lattner2079fde2001-10-13 06:41:08 +0000949 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
950 if (isVarArg) Params.pop_back();
951
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000952 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +0000953 delete $3; // Delete the argument list
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000954 delete $1; // Delete the return type handle
Chris Lattner30c89792001-09-07 16:35:17 +0000955 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000956 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000957 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000958 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +0000959 }
Brian Gaeke715c90b2004-08-20 06:00:58 +0000960 | '<' EUINT64VAL 'x' UpRTypes '>' { // Packed array type?
961 const llvm::Type* ElemTy = $4->get();
962 if ((unsigned)$2 != $2) {
963 ThrowException("Unsigned result not equal to signed result");
964 }
965 if(!ElemTy->isPrimitiveType()) {
966 ThrowException("Elemental type of a PackedType must be primitive");
967 }
968 $$ = new PATypeHolder(HandleUpRefs(PackedType::get(*$4, (unsigned)$2)));
969 delete $4;
970 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000971 | '{' TypeListI '}' { // Structure type?
Chris Lattner9232b992003-04-22 18:42:41 +0000972 std::vector<const Type*> Elements;
Chris Lattnera08976b2005-02-22 23:13:58 +0000973 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
974 E = $2->end(); I != E; ++I)
975 Elements.push_back(*I);
976
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000977 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000978 delete $2;
979 }
980 | '{' '}' { // Empty structure type?
Chris Lattner9232b992003-04-22 18:42:41 +0000981 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000982 }
983 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000984 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000985 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000986 };
Chris Lattner30c89792001-09-07 16:35:17 +0000987
Chris Lattner7e708292002-06-25 16:13:24 +0000988// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +0000989// declaration type lists
990//
991TypeListI : UpRTypes {
Chris Lattner9232b992003-04-22 18:42:41 +0000992 $$ = new std::list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +0000993 $$->push_back(*$1); delete $1;
994 }
995 | TypeListI ',' UpRTypes {
996 ($$=$1)->push_back(*$3); delete $3;
Chris Lattner51727be2002-06-04 21:58:56 +0000997 };
Chris Lattner30c89792001-09-07 16:35:17 +0000998
Chris Lattner7e708292002-06-25 16:13:24 +0000999// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +00001000ArgTypeListI : TypeListI
1001 | TypeListI ',' DOTDOTDOT {
1002 ($$=$1)->push_back(Type::VoidTy);
1003 }
1004 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001005 ($$ = new std::list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattner30c89792001-09-07 16:35:17 +00001006 }
1007 | /*empty*/ {
Chris Lattner9232b992003-04-22 18:42:41 +00001008 $$ = new std::list<PATypeHolder>();
Chris Lattner51727be2002-06-04 21:58:56 +00001009 };
Chris Lattner30c89792001-09-07 16:35:17 +00001010
Chris Lattnere98dda62001-07-14 06:10:16 +00001011// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +00001012// production is used ONLY to represent constants that show up AFTER a 'const',
1013// 'constant' or 'global' token at global scope. Constants that can be inlined
1014// into other expressions (such as integers and constexprs) are handled by the
1015// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +00001016//
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001017ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Chris Lattner949a3622003-07-23 15:30:06 +00001018 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001019 if (ATy == 0)
1020 ThrowException("Cannot make array constant with type: '" +
1021 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001022 const Type *ETy = ATy->getElementType();
1023 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +00001024
Chris Lattner30c89792001-09-07 16:35:17 +00001025 // Verify that we have the correct size...
1026 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +00001027 ThrowException("Type mismatch: constant sized array initialized with " +
Reid Spencer3271ed52004-07-18 00:08:11 +00001028 utostr($3->size()) + " arguments, but has size of " +
1029 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +00001030
Chris Lattner30c89792001-09-07 16:35:17 +00001031 // Verify all elements are correct type!
1032 for (unsigned i = 0; i < $3->size(); i++) {
1033 if (ETy != (*$3)[i]->getType())
Reid Spencer3271ed52004-07-18 00:08:11 +00001034 ThrowException("Element #" + utostr(i) + " is not of type '" +
1035 ETy->getDescription() +"' as required!\nIt is of type '"+
1036 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +00001037 }
1038
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001039 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001040 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001041 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001042 | Types '[' ']' {
Chris Lattner949a3622003-07-23 15:30:06 +00001043 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001044 if (ATy == 0)
1045 ThrowException("Cannot make array constant with type: '" +
1046 (*$1)->getDescription() + "'!");
1047
1048 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +00001049 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +00001050 ThrowException("Type mismatch: constant sized array initialized with 0"
Reid Spencer3271ed52004-07-18 00:08:11 +00001051 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattner9232b992003-04-22 18:42:41 +00001052 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +00001053 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001054 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001055 | Types 'c' STRINGCONSTANT {
Chris Lattner949a3622003-07-23 15:30:06 +00001056 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001057 if (ATy == 0)
1058 ThrowException("Cannot make array constant with type: '" +
1059 (*$1)->getDescription() + "'!");
1060
Chris Lattner30c89792001-09-07 16:35:17 +00001061 int NumElements = ATy->getNumElements();
1062 const Type *ETy = ATy->getElementType();
1063 char *EndStr = UnEscapeLexed($3, true);
1064 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +00001065 ThrowException("Can't build string constant of size " +
Reid Spencer3271ed52004-07-18 00:08:11 +00001066 itostr((int)(EndStr-$3)) +
1067 " when array has size " + itostr(NumElements) + "!");
Chris Lattner9232b992003-04-22 18:42:41 +00001068 std::vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +00001069 if (ETy == Type::SByteTy) {
1070 for (char *C = $3; C != EndStr; ++C)
Reid Spencer3271ed52004-07-18 00:08:11 +00001071 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +00001072 } else if (ETy == Type::UByteTy) {
1073 for (char *C = $3; C != EndStr; ++C)
Reid Spencer3271ed52004-07-18 00:08:11 +00001074 Vals.push_back(ConstantUInt::get(ETy, (unsigned char)*C));
Chris Lattner93750fa2001-07-28 17:48:55 +00001075 } else {
Chris Lattner30c89792001-09-07 16:35:17 +00001076 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +00001077 ThrowException("Cannot build string arrays of non byte sized elements!");
1078 }
Chris Lattner30c89792001-09-07 16:35:17 +00001079 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001080 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +00001081 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +00001082 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001083 | Types '<' ConstVector '>' { // Nonempty unsized arr
1084 const PackedType *PTy = dyn_cast<PackedType>($1->get());
1085 if (PTy == 0)
1086 ThrowException("Cannot make packed constant with type: '" +
1087 (*$1)->getDescription() + "'!");
1088 const Type *ETy = PTy->getElementType();
1089 int NumElements = PTy->getNumElements();
1090
1091 // Verify that we have the correct size...
1092 if (NumElements != -1 && NumElements != (int)$3->size())
1093 ThrowException("Type mismatch: constant sized packed initialized with " +
1094 utostr($3->size()) + " arguments, but has size of " +
1095 itostr(NumElements) + "!");
1096
1097 // Verify all elements are correct type!
1098 for (unsigned i = 0; i < $3->size(); i++) {
1099 if (ETy != (*$3)[i]->getType())
1100 ThrowException("Element #" + utostr(i) + " is not of type '" +
1101 ETy->getDescription() +"' as required!\nIt is of type '"+
1102 (*$3)[i]->getType()->getDescription() + "'.");
1103 }
1104
1105 $$ = ConstantPacked::get(PTy, *$3);
1106 delete $1; delete $3;
1107 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001108 | Types '{' ConstVector '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001109 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001110 if (STy == 0)
1111 ThrowException("Cannot make struct constant with type: '" +
1112 (*$1)->getDescription() + "'!");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001113
Chris Lattnerc6212f12003-05-21 16:06:56 +00001114 if ($3->size() != STy->getNumContainedTypes())
1115 ThrowException("Illegal number of initializers for structure type!");
1116
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001117 // Check to ensure that constants are compatible with the type initializer!
1118 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Chris Lattnerd21cd802004-02-09 04:37:31 +00001119 if ((*$3)[i]->getType() != STy->getElementType(i))
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001120 ThrowException("Expected type '" +
Chris Lattnerd21cd802004-02-09 04:37:31 +00001121 STy->getElementType(i)->getDescription() +
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001122 "' for element #" + utostr(i) +
1123 " of structure initializer!");
1124
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001125 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001126 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001127 }
Chris Lattnerc6212f12003-05-21 16:06:56 +00001128 | Types '{' '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001129 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerc6212f12003-05-21 16:06:56 +00001130 if (STy == 0)
1131 ThrowException("Cannot make struct constant with type: '" +
1132 (*$1)->getDescription() + "'!");
1133
1134 if (STy->getNumContainedTypes() != 0)
1135 ThrowException("Illegal number of initializers for structure type!");
1136
1137 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1138 delete $1;
1139 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001140 | Types NULL_TOK {
Chris Lattner949a3622003-07-23 15:30:06 +00001141 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001142 if (PTy == 0)
1143 ThrowException("Cannot make null pointer constant with type: '" +
1144 (*$1)->getDescription() + "'!");
1145
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001146 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001147 delete $1;
1148 }
Chris Lattner16710e92004-10-16 18:17:13 +00001149 | Types UNDEF {
1150 $$ = UndefValue::get($1->get());
1151 delete $1;
1152 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001153 | Types SymbolicValueRef {
Chris Lattner949a3622003-07-23 15:30:06 +00001154 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001155 if (Ty == 0)
1156 ThrowException("Global const reference must be a pointer type!");
1157
Chris Lattner3101c252002-08-15 17:58:33 +00001158 // ConstExprs can exist in the body of a function, thus creating
Reid Spencer3271ed52004-07-18 00:08:11 +00001159 // GlobalValues whenever they refer to a variable. Because we are in
Chris Lattner3101c252002-08-15 17:58:33 +00001160 // the context of a function, getValNonImprovising will search the functions
1161 // symbol table instead of the module symbol table for the global symbol,
1162 // which throws things all off. To get around this, we just tell
1163 // getValNonImprovising that we are at global scope here.
1164 //
Chris Lattner394f2eb2003-10-16 20:12:13 +00001165 Function *SavedCurFn = CurFun.CurrentFunction;
1166 CurFun.CurrentFunction = 0;
Chris Lattner3101c252002-08-15 17:58:33 +00001167
Chris Lattner2079fde2001-10-13 06:41:08 +00001168 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001169
Chris Lattner394f2eb2003-10-16 20:12:13 +00001170 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner3101c252002-08-15 17:58:33 +00001171
Chris Lattner2079fde2001-10-13 06:41:08 +00001172 // If this is an initializer for a constant pointer, which is referencing a
1173 // (currently) undefined variable, create a stub now that shall be replaced
1174 // in the future with the right type of variable.
1175 //
1176 if (V == 0) {
1177 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1178 const PointerType *PT = cast<PointerType>(Ty);
1179
1180 // First check to see if the forward references value is already created!
1181 PerModuleInfo::GlobalRefsType::iterator I =
Reid Spencer3271ed52004-07-18 00:08:11 +00001182 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattner2079fde2001-10-13 06:41:08 +00001183
1184 if (I != CurModule.GlobalRefs.end()) {
Reid Spencer3271ed52004-07-18 00:08:11 +00001185 V = I->second; // Placeholder already exists, use it...
Chris Lattnera989b232003-12-23 20:05:15 +00001186 $2.destroy();
Chris Lattner2079fde2001-10-13 06:41:08 +00001187 } else {
Chris Lattner8a327842004-07-14 21:44:00 +00001188 std::string Name;
Chris Lattnera09000d2004-07-14 23:03:46 +00001189 if ($2.Type == ValID::NameVal) Name = $2.Name;
Chris Lattner8a327842004-07-14 21:44:00 +00001190
Reid Spencer3271ed52004-07-18 00:08:11 +00001191 // Create the forward referenced global.
Chris Lattnera09000d2004-07-14 23:03:46 +00001192 GlobalValue *GV;
1193 if (const FunctionType *FTy =
1194 dyn_cast<FunctionType>(PT->getElementType())) {
1195 GV = new Function(FTy, GlobalValue::ExternalLinkage, Name,
1196 CurModule.CurrentModule);
1197 } else {
Reid Spencer3271ed52004-07-18 00:08:11 +00001198 GV = new GlobalVariable(PT->getElementType(), false,
Chris Lattnera09000d2004-07-14 23:03:46 +00001199 GlobalValue::ExternalLinkage, 0,
1200 Name, CurModule.CurrentModule);
1201 }
Chris Lattner8a327842004-07-14 21:44:00 +00001202
Reid Spencer3271ed52004-07-18 00:08:11 +00001203 // Keep track of the fact that we have a forward ref to recycle it
1204 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1205 V = GV;
Chris Lattner2079fde2001-10-13 06:41:08 +00001206 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001207 }
1208
Reid Spencer3271ed52004-07-18 00:08:11 +00001209 $$ = cast<GlobalValue>(V);
Chris Lattner2079fde2001-10-13 06:41:08 +00001210 delete $1; // Free the type handle
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001211 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001212 | Types ConstExpr {
1213 if ($1->get() != $2->getType())
1214 ThrowException("Mismatched types for constant expression!");
1215 $$ = $2;
1216 delete $1;
Chris Lattnerf4600482003-06-28 20:01:34 +00001217 }
1218 | Types ZEROINITIALIZER {
Chris Lattner78915932004-11-28 16:45:45 +00001219 const Type *Ty = $1->get();
1220 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
1221 ThrowException("Cannot create a null initialized value of this type!");
1222 $$ = Constant::getNullValue(Ty);
Chris Lattnerf4600482003-06-28 20:01:34 +00001223 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001224 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001225
Chris Lattnere43f40b2002-10-09 00:25:32 +00001226ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001227 if (!ConstantSInt::isValueValidForType($1, $2))
1228 ThrowException("Constant value doesn't fit in type!");
1229 $$ = ConstantSInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001230 }
1231 | UIntType EUINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001232 if (!ConstantUInt::isValueValidForType($1, $2))
1233 ThrowException("Constant value doesn't fit in type!");
1234 $$ = ConstantUInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001235 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001236 | BOOL TRUETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001237 $$ = ConstantBool::True;
1238 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001239 | BOOL FALSETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001240 $$ = ConstantBool::False;
1241 }
1242 | FPType FPVAL { // Float & Double constants
Reid Spencer9f9b3ac2004-12-06 22:18:25 +00001243 if (!ConstantFP::isValueValidForType($1, $2))
1244 ThrowException("Floating point constant invalid for type!!");
Chris Lattnerd05e3592002-08-15 18:17:28 +00001245 $$ = ConstantFP::get($1, $2);
1246 };
1247
Chris Lattner00950542001-06-06 20:29:01 +00001248
Chris Lattnerd78700d2002-08-16 21:14:40 +00001249ConstExpr: CAST '(' ConstVal TO Types ')' {
Chris Lattnerae711a82003-11-21 20:27:35 +00001250 if (!$3->getType()->isFirstClassType())
1251 ThrowException("cast constant expression from a non-primitive type: '" +
1252 $3->getType()->getDescription() + "'!");
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001253 if (!$5->get()->isFirstClassType())
1254 ThrowException("cast constant expression to a non-primitive type: '" +
1255 $5->get()->getDescription() + "'!");
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001256 $$ = ConstantExpr::getCast($3, $5->get());
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001257 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001258 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001259 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1260 if (!isa<PointerType>($3->getType()))
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001261 ThrowException("GetElementPtr requires a pointer operand!");
1262
Chris Lattner830b6f92004-04-05 01:30:04 +00001263 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
1264 // indices to uint struct indices for compatibility.
1265 generic_gep_type_iterator<std::vector<Value*>::iterator>
1266 GTI = gep_type_begin($3->getType(), $4->begin(), $4->end()),
1267 GTE = gep_type_end($3->getType(), $4->begin(), $4->end());
1268 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
1269 if (isa<StructType>(*GTI)) // Only change struct indices
1270 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
1271 if (CUI->getType() == Type::UByteTy)
1272 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
1273
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001274 const Type *IdxTy =
Chris Lattnerd78700d2002-08-16 21:14:40 +00001275 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001276 if (!IdxTy)
1277 ThrowException("Index list invalid for constant getelementptr!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001278
Chris Lattner9232b992003-04-22 18:42:41 +00001279 std::vector<Constant*> IdxVec;
Chris Lattnerd78700d2002-08-16 21:14:40 +00001280 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1281 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001282 IdxVec.push_back(C);
1283 else
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001284 ThrowException("Indices to constant getelementptr must be constants!");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001285
Chris Lattnerd78700d2002-08-16 21:14:40 +00001286 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001287
Chris Lattnerd78700d2002-08-16 21:14:40 +00001288 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001289 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001290 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1291 if ($3->getType() != Type::BoolTy)
1292 ThrowException("Select condition must be of boolean type!");
1293 if ($5->getType() != $7->getType())
1294 ThrowException("Select operand types must match!");
1295 $$ = ConstantExpr::getSelect($3, $5, $7);
1296 }
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001297 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001298 if ($3->getType() != $5->getType())
1299 ThrowException("Binary operator types must match!");
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001300 // HACK: llvm 1.3 and earlier used to emit invalid pointer constant exprs.
1301 // To retain backward compatibility with these early compilers, we emit a
1302 // cast to the appropriate integer type automatically if we are in the
1303 // broken case. See PR424 for more information.
1304 if (!isa<PointerType>($3->getType())) {
1305 $$ = ConstantExpr::get($1, $3, $5);
1306 } else {
Chris Lattner42db1a02004-08-20 18:07:39 +00001307 const Type *IntPtrTy = 0;
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001308 switch (CurModule.CurrentModule->getPointerSize()) {
1309 case Module::Pointer32: IntPtrTy = Type::IntTy; break;
1310 case Module::Pointer64: IntPtrTy = Type::LongTy; break;
1311 default: ThrowException("invalid pointer binary constant expr!");
1312 }
1313 $$ = ConstantExpr::get($1, ConstantExpr::getCast($3, IntPtrTy),
1314 ConstantExpr::getCast($5, IntPtrTy));
1315 $$ = ConstantExpr::getCast($$, $3->getType());
1316 }
1317 }
1318 | LogicalOps '(' ConstVal ',' ConstVal ')' {
1319 if ($3->getType() != $5->getType())
1320 ThrowException("Logical operator types must match!");
1321 if (!$3->getType()->isIntegral())
1322 ThrowException("Logical operands must have integral types!");
1323 $$ = ConstantExpr::get($1, $3, $5);
1324 }
1325 | SetCondOps '(' ConstVal ',' ConstVal ')' {
1326 if ($3->getType() != $5->getType())
1327 ThrowException("setcc operand types must match!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001328 $$ = ConstantExpr::get($1, $3, $5);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001329 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001330 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001331 if ($5->getType() != Type::UByteTy)
1332 ThrowException("Shift count for shift constant must be unsigned byte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001333 if (!$3->getType()->isInteger())
1334 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerb16689b2004-01-12 19:08:43 +00001335 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattner699f1eb2002-08-14 17:12:33 +00001336 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001337
1338
Misha Brukmanbc0e9982003-07-14 17:20:40 +00001339// ConstVector - A list of comma separated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001340ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001341 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001342 }
1343 | ConstVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001344 $$ = new std::vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001345 $$->push_back($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001346 };
Chris Lattner00950542001-06-06 20:29:01 +00001347
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001348
Chris Lattner1781aca2001-09-18 04:00:54 +00001349// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001350GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001351
Chris Lattner00950542001-06-06 20:29:01 +00001352
Chris Lattner0e73ce62002-05-02 19:11:13 +00001353//===----------------------------------------------------------------------===//
1354// Rules to match Modules
1355//===----------------------------------------------------------------------===//
1356
1357// Module rule: Capture the result of parsing the whole file into a result
1358// variable...
1359//
1360Module : FunctionList {
1361 $$ = ParserResult = $1;
1362 CurModule.ModuleDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001363};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001364
Chris Lattner7e708292002-06-25 16:13:24 +00001365// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001366//
1367FunctionList : FunctionList Function {
1368 $$ = $1;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001369 CurFun.FunctionDone();
Chris Lattner0e73ce62002-05-02 19:11:13 +00001370 }
1371 | FunctionList FunctionProto {
1372 $$ = $1;
1373 }
1374 | FunctionList IMPLEMENTATION {
1375 $$ = $1;
1376 }
1377 | ConstPool {
1378 $$ = CurModule.CurrentModule;
Chris Lattner355ad1f2005-03-21 06:27:42 +00001379 // Emit an error if there are any unresolved types left.
1380 if (!CurModule.LateResolveTypes.empty()) {
1381 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
1382 if (DID.Type == ValID::NameVal)
1383 ThrowException("Reference to an undefined type: '"+DID.getName() + "'");
1384 else
1385 ThrowException("Reference to an undefined type: #" + itostr(DID.Num));
1386 }
Chris Lattner51727be2002-06-04 21:58:56 +00001387 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001388
Chris Lattnere98dda62001-07-14 06:10:16 +00001389// ConstPool - Constants with optional names assigned to them.
Chris Lattner355ad1f2005-03-21 06:27:42 +00001390ConstPool : ConstPool OptAssign TYPE TypesV {
Chris Lattner4a42e902001-10-22 05:56:09 +00001391 // Eagerly resolve types. This is not an optimization, this is a
1392 // requirement that is due to the fact that we could have this:
1393 //
1394 // %list = type { %list * }
1395 // %list = type { %list * } ; repeated type decl
1396 //
1397 // If types are not resolved eagerly, then the two types will not be
1398 // determined to be the same type!
1399 //
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001400 ResolveTypeTo($2, *$4);
Chris Lattner4a42e902001-10-22 05:56:09 +00001401
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001402 if (!setTypeName(*$4, $2) && !$2) {
1403 // If this is a named type that is not a redefinition, add it to the slot
1404 // table.
Chris Lattner355ad1f2005-03-21 06:27:42 +00001405 CurModule.Types.push_back(*$4);
Chris Lattner30c89792001-09-07 16:35:17 +00001406 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001407
1408 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001409 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001410 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001411 }
Chris Lattner4ad02e72003-04-16 20:28:45 +00001412 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
Chris Lattnerd88998d2004-07-14 08:23:52 +00001413 if ($5 == 0) ThrowException("Global value initializer is not a constant!");
1414 ParseGlobalVariable($2, $3, $4, $5->getType(), $5);
Chris Lattner1781aca2001-09-18 04:00:54 +00001415 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001416 | ConstPool OptAssign EXTERNAL GlobalType Types {
Chris Lattnerd88998d2004-07-14 08:23:52 +00001417 ParseGlobalVariable($2, GlobalValue::ExternalLinkage, $4, *$5, 0);
Chris Lattner1f862af2003-04-16 18:13:57 +00001418 delete $5;
Chris Lattnere98dda62001-07-14 06:10:16 +00001419 }
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001420 | ConstPool TARGET TargetDefinition {
1421 }
Reid Spencer0be13e72004-07-25 17:58:28 +00001422 | ConstPool DEPLIBS '=' LibrariesDefinition {
1423 }
Chris Lattner00950542001-06-06 20:29:01 +00001424 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001425 };
Chris Lattner00950542001-06-06 20:29:01 +00001426
1427
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001428
1429BigOrLittle : BIG { $$ = Module::BigEndian; };
1430BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1431
1432TargetDefinition : ENDIAN '=' BigOrLittle {
1433 CurModule.CurrentModule->setEndianness($3);
1434 }
1435 | POINTERSIZE '=' EUINT64VAL {
1436 if ($3 == 32)
1437 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1438 else if ($3 == 64)
1439 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1440 else
1441 ThrowException("Invalid pointer size: '" + utostr($3) + "'!");
Reid Spencer0be13e72004-07-25 17:58:28 +00001442 }
1443 | TRIPLE '=' STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001444 CurModule.CurrentModule->setTargetTriple($3);
1445 free($3);
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001446 };
1447
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001448LibrariesDefinition : '[' LibList ']';
Reid Spencer0be13e72004-07-25 17:58:28 +00001449
1450LibList : LibList ',' STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001451 CurModule.CurrentModule->addLibrary($3);
1452 free($3);
Reid Spencer0be13e72004-07-25 17:58:28 +00001453 }
1454 | STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001455 CurModule.CurrentModule->addLibrary($1);
1456 free($1);
Reid Spencer0be13e72004-07-25 17:58:28 +00001457 }
1458 | /* empty: end of list */ {
1459 }
1460 ;
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001461
Chris Lattner00950542001-06-06 20:29:01 +00001462//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001463// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001464//===----------------------------------------------------------------------===//
1465
Chris Lattner6c23f572003-08-22 05:42:10 +00001466Name : VAR_ID | STRINGCONSTANT;
1467OptName : Name | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001468
Chris Lattner6c23f572003-08-22 05:42:10 +00001469ArgVal : Types OptName {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001470 if (*$1 == Type::VoidTy)
1471 ThrowException("void typed arguments are invalid!");
Chris Lattner9232b992003-04-22 18:42:41 +00001472 $$ = new std::pair<PATypeHolder*, char*>($1, $2);
Chris Lattner51727be2002-06-04 21:58:56 +00001473};
Chris Lattner00950542001-06-06 20:29:01 +00001474
Chris Lattner69da5cf2002-10-13 20:57:00 +00001475ArgListH : ArgListH ',' ArgVal {
1476 $$ = $1;
1477 $1->push_back(*$3);
1478 delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001479 }
1480 | ArgVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001481 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
Chris Lattner69da5cf2002-10-13 20:57:00 +00001482 $$->push_back(*$1);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001483 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001484 };
Chris Lattner00950542001-06-06 20:29:01 +00001485
1486ArgList : ArgListH {
1487 $$ = $1;
1488 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001489 | ArgListH ',' DOTDOTDOT {
1490 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001491 $$->push_back(std::pair<PATypeHolder*,
1492 char*>(new PATypeHolder(Type::VoidTy), 0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001493 }
1494 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001495 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1496 $$->push_back(std::make_pair(new PATypeHolder(Type::VoidTy), (char*)0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001497 }
Chris Lattner00950542001-06-06 20:29:01 +00001498 | /* empty */ {
1499 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001500 };
Chris Lattner00950542001-06-06 20:29:01 +00001501
Chris Lattner6c23f572003-08-22 05:42:10 +00001502FunctionHeaderH : TypesV Name '(' ArgList ')' {
Chris Lattner1f862af2003-04-16 18:13:57 +00001503 UnEscapeLexed($2);
Chris Lattner9232b992003-04-22 18:42:41 +00001504 std::string FunctionName($2);
Chris Lattnera09000d2004-07-14 23:03:46 +00001505 free($2); // Free strdup'd memory!
Chris Lattnerdda71962001-11-26 18:54:16 +00001506
Chris Lattnerc282f5a2003-11-21 22:32:23 +00001507 if (!(*$1)->isFirstClassType() && *$1 != Type::VoidTy)
1508 ThrowException("LLVM functions cannot return aggregate types!");
1509
Chris Lattner9232b992003-04-22 18:42:41 +00001510 std::vector<const Type*> ParamTypeList;
Chris Lattner1f862af2003-04-16 18:13:57 +00001511 if ($4) { // If there are arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001512 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001513 I != $4->end(); ++I)
Chris Lattner69da5cf2002-10-13 20:57:00 +00001514 ParamTypeList.push_back(I->first->get());
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001515 }
Chris Lattner00950542001-06-06 20:29:01 +00001516
Chris Lattner2079fde2001-10-13 06:41:08 +00001517 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1518 if (isVarArg) ParamTypeList.pop_back();
1519
Chris Lattner1f862af2003-04-16 18:13:57 +00001520 const FunctionType *FT = FunctionType::get(*$1, ParamTypeList, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001521 const PointerType *PFT = PointerType::get(FT);
Chris Lattner1f862af2003-04-16 18:13:57 +00001522 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001523
Chris Lattnera09000d2004-07-14 23:03:46 +00001524 ValID ID;
1525 if (!FunctionName.empty()) {
1526 ID = ValID::create((char*)FunctionName.c_str());
1527 } else {
1528 ID = ValID::create((int)CurModule.Values[PFT].size());
1529 }
1530
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001531 Function *Fn = 0;
Chris Lattnera09000d2004-07-14 23:03:46 +00001532 // See if this function was forward referenced. If so, recycle the object.
1533 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
1534 // Move the function to the end of the list, from whereever it was
1535 // previously inserted.
1536 Fn = cast<Function>(FWRef);
1537 CurModule.CurrentModule->getFunctionList().remove(Fn);
1538 CurModule.CurrentModule->getFunctionList().push_back(Fn);
1539 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
1540 (Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1541 // If this is the case, either we need to be a forward decl, or it needs
1542 // to be.
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001543 if (!CurFun.isDeclare && !Fn->isExternal())
1544 ThrowException("Redefinition of function '" + FunctionName + "'!");
1545
Chris Lattnera09000d2004-07-14 23:03:46 +00001546 // Make sure to strip off any argument names so we can't get conflicts.
1547 if (Fn->isExternal())
Chris Lattnere4d5c442005-03-15 04:54:21 +00001548 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
Chris Lattnera09000d2004-07-14 23:03:46 +00001549 AI != AE; ++AI)
1550 AI->setName("");
Chris Lattner5659dd12002-07-15 00:10:33 +00001551
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001552 } else { // Not already defined?
1553 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName,
1554 CurModule.CurrentModule);
1555 InsertValue(Fn, CurModule.Values);
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001556 }
Chris Lattner00950542001-06-06 20:29:01 +00001557
Chris Lattner394f2eb2003-10-16 20:12:13 +00001558 CurFun.FunctionStart(Fn);
Chris Lattner00950542001-06-06 20:29:01 +00001559
Chris Lattner7e708292002-06-25 16:13:24 +00001560 // Add all of the arguments we parsed to the function...
Chris Lattner1f862af2003-04-16 18:13:57 +00001561 if ($4) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001562 if (isVarArg) { // Nuke the last entry
Chris Lattner1f862af2003-04-16 18:13:57 +00001563 assert($4->back().first->get() == Type::VoidTy && $4->back().second == 0&&
Chris Lattner69da5cf2002-10-13 20:57:00 +00001564 "Not a varargs marker!");
Chris Lattner1f862af2003-04-16 18:13:57 +00001565 delete $4->back().first;
1566 $4->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00001567 }
Chris Lattnere4d5c442005-03-15 04:54:21 +00001568 Function::arg_iterator ArgIt = Fn->arg_begin();
Chris Lattner9232b992003-04-22 18:42:41 +00001569 for (std::vector<std::pair<PATypeHolder*, char*> >::iterator I =$4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001570 I != $4->end(); ++I, ++ArgIt) {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001571 delete I->first; // Delete the typeholder...
1572
Chris Lattner8ab406d2004-07-13 07:59:27 +00001573 setValueName(ArgIt, I->second); // Insert arg into symtab...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001574 InsertValue(ArgIt);
Chris Lattner00950542001-06-06 20:29:01 +00001575 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001576
Chris Lattner1f862af2003-04-16 18:13:57 +00001577 delete $4; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00001578 }
Chris Lattner51727be2002-06-04 21:58:56 +00001579};
Chris Lattner00950542001-06-06 20:29:01 +00001580
Chris Lattner9b02cc32002-05-03 18:23:48 +00001581BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1582
Chris Lattner4ad02e72003-04-16 20:28:45 +00001583FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
Chris Lattner394f2eb2003-10-16 20:12:13 +00001584 $$ = CurFun.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001585
Chris Lattner4ad02e72003-04-16 20:28:45 +00001586 // Make sure that we keep track of the linkage type even if there was a
1587 // previous "declare".
1588 $$->setLinkage($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001589};
Chris Lattner00950542001-06-06 20:29:01 +00001590
Chris Lattner9b02cc32002-05-03 18:23:48 +00001591END : ENDTOK | '}'; // Allow end of '}' to end a function
1592
Chris Lattner79df7c02002-03-26 18:01:55 +00001593Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001594 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001595};
Chris Lattner00950542001-06-06 20:29:01 +00001596
Chris Lattner394f2eb2003-10-16 20:12:13 +00001597FunctionProto : DECLARE { CurFun.isDeclare = true; } FunctionHeaderH {
1598 $$ = CurFun.CurrentFunction;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001599 CurFun.FunctionDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001600};
Chris Lattner00950542001-06-06 20:29:01 +00001601
1602//===----------------------------------------------------------------------===//
1603// Rules to match Basic Blocks
1604//===----------------------------------------------------------------------===//
1605
1606ConstValueRef : ESINT64VAL { // A reference to a direct constant
1607 $$ = ValID::create($1);
1608 }
1609 | EUINT64VAL {
1610 $$ = ValID::create($1);
1611 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001612 | FPVAL { // Perhaps it's an FP constant?
1613 $$ = ValID::create($1);
1614 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001615 | TRUETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001616 $$ = ValID::create(ConstantBool::True);
Chris Lattner00950542001-06-06 20:29:01 +00001617 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001618 | FALSETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001619 $$ = ValID::create(ConstantBool::False);
Chris Lattner00950542001-06-06 20:29:01 +00001620 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001621 | NULL_TOK {
1622 $$ = ValID::createNull();
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001623 }
Chris Lattner16710e92004-10-16 18:17:13 +00001624 | UNDEF {
1625 $$ = ValID::createUndef();
1626 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001627 | '<' ConstVector '>' { // Nonempty unsized packed vector
1628 const Type *ETy = (*$2)[0]->getType();
1629 int NumElements = $2->size();
1630
1631 PackedType* pt = PackedType::get(ETy, NumElements);
1632 PATypeHolder* PTy = new PATypeHolder(
1633 HandleUpRefs(
1634 PackedType::get(
1635 ETy,
1636 NumElements)
1637 )
1638 );
1639
1640 // Verify all elements are correct type!
1641 for (unsigned i = 0; i < $2->size(); i++) {
1642 if (ETy != (*$2)[i]->getType())
1643 ThrowException("Element #" + utostr(i) + " is not of type '" +
1644 ETy->getDescription() +"' as required!\nIt is of type '" +
1645 (*$2)[i]->getType()->getDescription() + "'.");
1646 }
1647
1648 $$ = ValID::create(ConstantPacked::get(pt, *$2));
1649 delete PTy; delete $2;
1650 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001651 | ConstExpr {
1652 $$ = ValID::create($1);
1653 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00001654
Chris Lattner2079fde2001-10-13 06:41:08 +00001655// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1656// another value.
1657//
1658SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001659 $$ = ValID::create($1);
1660 }
Chris Lattner6c23f572003-08-22 05:42:10 +00001661 | Name { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001662 $$ = ValID::create($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001663 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001664
1665// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00001666ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00001667
Chris Lattner00950542001-06-06 20:29:01 +00001668
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001669// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1670// type immediately preceeds the value reference, and allows complex constant
1671// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001672ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001673 $$ = getVal(*$1, $2); delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001674 };
Chris Lattner8b81bf52001-07-25 22:47:46 +00001675
Chris Lattner00950542001-06-06 20:29:01 +00001676BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001677 $$ = $1;
Chris Lattner00950542001-06-06 20:29:01 +00001678 }
Chris Lattner7e708292002-06-25 16:13:24 +00001679 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
Chris Lattner8ab406d2004-07-13 07:59:27 +00001680 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001681 };
Chris Lattner00950542001-06-06 20:29:01 +00001682
1683
1684// Basic blocks are terminated by branching instructions:
1685// br, br/cc, switch, ret
1686//
Chris Lattner2079fde2001-10-13 06:41:08 +00001687BasicBlock : InstructionList OptAssign BBTerminatorInst {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001688 setValueName($3, $2);
Chris Lattner2079fde2001-10-13 06:41:08 +00001689 InsertValue($3);
1690
1691 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001692 InsertValue($1);
1693 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001694 };
Chris Lattner00950542001-06-06 20:29:01 +00001695
1696InstructionList : InstructionList Inst {
1697 $1->getInstList().push_back($2);
1698 $$ = $1;
1699 }
1700 | /* empty */ {
Chris Lattner1f640252005-05-06 19:49:51 +00001701 $$ = getBBVal(ValID::create((int)CurFun.NextBBNum++), true);
Chris Lattner8d65a062004-07-26 01:40:20 +00001702
1703 // Make sure to move the basic block to the correct location in the
1704 // function, instead of leaving it inserted wherever it was first
1705 // referenced.
Chris Lattnerf924a4c2005-05-06 19:58:35 +00001706 Function::BasicBlockListType &BBL =
1707 CurFun.CurrentFunction->getBasicBlockList();
1708 BBL.splice(BBL.end(), BBL, $$);
Chris Lattner22ae2322004-07-13 08:39:15 +00001709 }
1710 | LABELSTR {
Chris Lattner1f640252005-05-06 19:49:51 +00001711 $$ = getBBVal(ValID::create($1), true);
Chris Lattner8d65a062004-07-26 01:40:20 +00001712
1713 // Make sure to move the basic block to the correct location in the
1714 // function, instead of leaving it inserted wherever it was first
1715 // referenced.
Chris Lattnerf924a4c2005-05-06 19:58:35 +00001716 Function::BasicBlockListType &BBL =
1717 CurFun.CurrentFunction->getBasicBlockList();
1718 BBL.splice(BBL.end(), BBL, $$);
Chris Lattner51727be2002-06-04 21:58:56 +00001719 };
Chris Lattner00950542001-06-06 20:29:01 +00001720
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001721BBTerminatorInst : RET ResolvedVal { // Return with a result...
1722 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001723 }
1724 | RET VOID { // Return with no result...
1725 $$ = new ReturnInst();
1726 }
1727 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner64116f92004-07-14 01:33:11 +00001728 $$ = new BranchInst(getBBVal($3));
Chris Lattner00950542001-06-06 20:29:01 +00001729 } // Conditional Branch...
1730 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner64116f92004-07-14 01:33:11 +00001731 $$ = new BranchInst(getBBVal($6), getBBVal($9), getVal(Type::BoolTy, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001732 }
1733 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Chris Lattnerce1df9e2005-01-29 00:35:55 +00001734 SwitchInst *S = new SwitchInst(getVal($2, $3), getBBVal($6), $8->size());
Chris Lattner00950542001-06-06 20:29:01 +00001735 $$ = S;
1736
Chris Lattner9232b992003-04-22 18:42:41 +00001737 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner46748042002-04-09 19:41:42 +00001738 E = $8->end();
Chris Lattner69331f52005-02-24 05:25:17 +00001739 for (; I != E; ++I) {
1740 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
1741 S->addCase(CI, I->second);
1742 else
1743 ThrowException("Switch case is constant, but not a simple integer!");
1744 }
Chris Lattnerf57a43d2004-04-17 23:49:15 +00001745 delete $8;
Chris Lattner00950542001-06-06 20:29:01 +00001746 }
Chris Lattner196850c2003-05-15 21:30:00 +00001747 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Chris Lattnerce1df9e2005-01-29 00:35:55 +00001748 SwitchInst *S = new SwitchInst(getVal($2, $3), getBBVal($6), 0);
Chris Lattner196850c2003-05-15 21:30:00 +00001749 $$ = S;
1750 }
Chris Lattner64116f92004-07-14 01:33:11 +00001751 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO LABEL ValueRef
1752 UNWIND LABEL ValueRef {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001753 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001754 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00001755
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001756 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1757 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00001758 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001759 std::vector<const Type*> ParamTypes;
Chris Lattner2079fde2001-10-13 06:41:08 +00001760 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001761 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1762 I != E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00001763 ParamTypes.push_back((*I)->getType());
1764 }
1765
1766 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1767 if (isVarArg) ParamTypes.pop_back();
1768
Chris Lattner79df7c02002-03-26 18:01:55 +00001769 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001770 PFTy = PointerType::get(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00001771 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001772
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001773 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner2079fde2001-10-13 06:41:08 +00001774
Chris Lattner64116f92004-07-14 01:33:11 +00001775 BasicBlock *Normal = getBBVal($9);
1776 BasicBlock *Except = getBBVal($12);
Chris Lattner2079fde2001-10-13 06:41:08 +00001777
1778 // Create the call node...
1779 if (!$5) { // Has no arguments?
Chris Lattner9232b992003-04-22 18:42:41 +00001780 $$ = new InvokeInst(V, Normal, Except, std::vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001781 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001782 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00001783 // correctly!
1784 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00001785 FunctionType::param_iterator I = Ty->param_begin();
1786 FunctionType::param_iterator E = Ty->param_end();
Chris Lattner9232b992003-04-22 18:42:41 +00001787 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00001788
1789 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
Reid Spencer3271ed52004-07-18 00:08:11 +00001790 if ((*ArgI)->getType() != *I)
1791 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
1792 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001793
1794 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Reid Spencer3271ed52004-07-18 00:08:11 +00001795 ThrowException("Invalid number of parameters detected!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001796
Chris Lattner6cdb0112001-11-26 16:54:11 +00001797 $$ = new InvokeInst(V, Normal, Except, *$5);
Chris Lattner2079fde2001-10-13 06:41:08 +00001798 }
Chris Lattner9cd42572003-12-23 22:18:36 +00001799 delete $2;
Chris Lattner2079fde2001-10-13 06:41:08 +00001800 delete $5;
Chris Lattner36143fc2003-09-08 18:54:55 +00001801 }
1802 | UNWIND {
1803 $$ = new UnwindInst();
Chris Lattner16710e92004-10-16 18:17:13 +00001804 }
1805 | UNREACHABLE {
1806 $$ = new UnreachableInst();
Chris Lattner51727be2002-06-04 21:58:56 +00001807 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001808
1809
Chris Lattner00950542001-06-06 20:29:01 +00001810
1811JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1812 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001813 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001814 if (V == 0)
1815 ThrowException("May only switch on a constant pool value!");
1816
Chris Lattner64116f92004-07-14 01:33:11 +00001817 $$->push_back(std::make_pair(V, getBBVal($6)));
Chris Lattner00950542001-06-06 20:29:01 +00001818 }
1819 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner9232b992003-04-22 18:42:41 +00001820 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001821 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00001822
1823 if (V == 0)
1824 ThrowException("May only switch on a constant pool value!");
1825
Chris Lattner64116f92004-07-14 01:33:11 +00001826 $$->push_back(std::make_pair(V, getBBVal($5)));
Chris Lattner51727be2002-06-04 21:58:56 +00001827 };
Chris Lattner00950542001-06-06 20:29:01 +00001828
1829Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001830 // Is this definition named?? if so, assign the name...
Chris Lattner8ab406d2004-07-13 07:59:27 +00001831 setValueName($2, $1);
Chris Lattner00950542001-06-06 20:29:01 +00001832 InsertValue($2);
1833 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001834};
Chris Lattner00950542001-06-06 20:29:01 +00001835
Chris Lattnerc24d2082001-06-11 15:04:20 +00001836PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Chris Lattner9232b992003-04-22 18:42:41 +00001837 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Chris Lattner64116f92004-07-14 01:33:11 +00001838 $$->push_back(std::make_pair(getVal(*$1, $3), getBBVal($5)));
Chris Lattner30c89792001-09-07 16:35:17 +00001839 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001840 }
1841 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1842 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001843 $1->push_back(std::make_pair(getVal($1->front().first->getType(), $4),
Chris Lattner64116f92004-07-14 01:33:11 +00001844 getBBVal($6)));
Chris Lattner51727be2002-06-04 21:58:56 +00001845 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00001846
1847
Chris Lattner30c89792001-09-07 16:35:17 +00001848ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner9232b992003-04-22 18:42:41 +00001849 $$ = new std::vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001850 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001851 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001852 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001853 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001854 $1->push_back($3);
Chris Lattner51727be2002-06-04 21:58:56 +00001855 };
Chris Lattner00950542001-06-06 20:29:01 +00001856
1857// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner51727be2002-06-04 21:58:56 +00001858ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001859
Chris Lattnerddb6db42005-05-06 05:51:46 +00001860OptTailCall : TAIL CALL {
1861 $$ = true;
1862 }
1863 | CALL {
1864 $$ = false;
1865 };
1866
1867
1868
Chris Lattner4a6482b2002-09-10 19:57:26 +00001869InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Brian Gaeke715c90b2004-08-20 06:00:58 +00001870 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
1871 !isa<PackedType>((*$2).get()))
1872 ThrowException(
1873 "Arithmetic operator requires integer, FP, or packed operands!");
1874 if(isa<PackedType>((*$2).get()) && $1 == Instruction::Rem) {
1875 ThrowException(
1876 "Rem not supported on packed types!");
1877 }
Chris Lattner4a6482b2002-09-10 19:57:26 +00001878 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1879 if ($$ == 0)
1880 ThrowException("binary operator returned null!");
1881 delete $2;
1882 }
1883 | LogicalOps Types ValueRef ',' ValueRef {
1884 if (!(*$2)->isIntegral())
1885 ThrowException("Logical operator requires integral operands!");
1886 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1887 if ($$ == 0)
1888 ThrowException("binary operator returned null!");
1889 delete $2;
1890 }
1891 | SetCondOps Types ValueRef ',' ValueRef {
Reid Spencer57b6eec2004-08-21 16:11:02 +00001892 if(isa<PackedType>((*$2).get())) {
1893 ThrowException(
1894 "PackedTypes currently not supported in setcc instructions!");
1895 }
Chris Lattner1cff96a2002-09-10 22:37:46 +00001896 $$ = new SetCondInst($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001897 if ($$ == 0)
1898 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001899 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001900 }
Chris Lattner699f1eb2002-08-14 17:12:33 +00001901 | NOT ResolvedVal {
1902 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
1903 << " Replacing with 'xor'.\n";
1904
1905 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
1906 if (Ones == 0)
1907 ThrowException("Expected integral type for not instruction!");
1908
1909 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner00950542001-06-06 20:29:01 +00001910 if ($$ == 0)
Chris Lattner699f1eb2002-08-14 17:12:33 +00001911 ThrowException("Could not create a xor instruction!");
Chris Lattner09083092001-07-08 04:57:15 +00001912 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001913 | ShiftOps ResolvedVal ',' ResolvedVal {
1914 if ($4->getType() != Type::UByteTy)
1915 ThrowException("Shift amount must be ubyte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001916 if (!$2->getType()->isInteger())
1917 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001918 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001919 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001920 | CAST ResolvedVal TO Types {
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001921 if (!$4->get()->isFirstClassType())
1922 ThrowException("cast instruction to a non-primitive type: '" +
1923 $4->get()->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001924 $$ = new CastInst($2, *$4);
1925 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001926 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001927 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1928 if ($2->getType() != Type::BoolTy)
1929 ThrowException("select condition must be boolean!");
1930 if ($4->getType() != $6->getType())
1931 ThrowException("select value types should match!");
1932 $$ = new SelectInst($2, $4, $6);
1933 }
Chris Lattner99e7ab72003-10-18 05:53:13 +00001934 | VAARG ResolvedVal ',' Types {
1935 $$ = new VAArgInst($2, *$4);
1936 delete $4;
1937 }
1938 | VANEXT ResolvedVal ',' Types {
1939 $$ = new VANextInst($2, *$4);
Chris Lattner8f77dae2003-05-08 02:44:12 +00001940 delete $4;
1941 }
Chris Lattner3b237fc2003-10-19 21:34:28 +00001942 | PHI_TOK PHIList {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001943 const Type *Ty = $2->front().first->getType();
Chris Lattner8ea8f362003-10-30 01:38:18 +00001944 if (!Ty->isFirstClassType())
1945 ThrowException("PHI node operands must be of first class type!");
Chris Lattnerc24d2082001-06-11 15:04:20 +00001946 $$ = new PHINode(Ty);
Chris Lattnerce1df9e2005-01-29 00:35:55 +00001947 ((PHINode*)$$)->reserveOperandSpace($2->size());
Chris Lattner00950542001-06-06 20:29:01 +00001948 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001949 if ($2->front().first->getType() != Ty)
Reid Spencer3271ed52004-07-18 00:08:11 +00001950 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001951 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001952 $2->pop_front();
1953 }
1954 delete $2; // Free the list...
Chris Lattnerddb6db42005-05-06 05:51:46 +00001955 }
1956 | OptTailCall TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001957 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001958 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001959
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001960 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1961 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001962 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001963 std::vector<const Type*> ParamTypes;
Chris Lattneref9c23f2001-10-03 14:53:21 +00001964 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001965 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1966 I != E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00001967 ParamTypes.push_back((*I)->getType());
1968 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001969
1970 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1971 if (isVarArg) ParamTypes.pop_back();
1972
Chris Lattner595f06c2005-02-01 01:47:42 +00001973 if (!(*$2)->isFirstClassType() && *$2 != Type::VoidTy)
1974 ThrowException("LLVM functions cannot return aggregate types!");
1975
Chris Lattner79df7c02002-03-26 18:01:55 +00001976 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001977 PFTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001978 }
Chris Lattner00950542001-06-06 20:29:01 +00001979
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001980 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001981
Chris Lattner8b81bf52001-07-25 22:47:46 +00001982 // Create the call node...
1983 if (!$5) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00001984 // Make sure no arguments is a good thing!
1985 if (Ty->getNumParams() != 0)
1986 ThrowException("No arguments passed to a function that "
1987 "expects arguments!");
1988
Chris Lattner9232b992003-04-22 18:42:41 +00001989 $$ = new CallInst(V, std::vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001990 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001991 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00001992 // correctly!
1993 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00001994 FunctionType::param_iterator I = Ty->param_begin();
1995 FunctionType::param_iterator E = Ty->param_end();
Chris Lattner9232b992003-04-22 18:42:41 +00001996 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001997
1998 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
Reid Spencer3271ed52004-07-18 00:08:11 +00001999 if ((*ArgI)->getType() != *I)
2000 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
2001 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00002002
Chris Lattner8b81bf52001-07-25 22:47:46 +00002003 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Reid Spencer3271ed52004-07-18 00:08:11 +00002004 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00002005
Chris Lattner6cdb0112001-11-26 16:54:11 +00002006 $$ = new CallInst(V, *$5);
Chris Lattner8b81bf52001-07-25 22:47:46 +00002007 }
Chris Lattnerddb6db42005-05-06 05:51:46 +00002008 cast<CallInst>($$)->setTailCall($1);
Chris Lattnerf64d57a2003-12-23 20:39:17 +00002009 delete $2;
Chris Lattner8b81bf52001-07-25 22:47:46 +00002010 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00002011 }
2012 | MemoryInst {
2013 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00002014 };
Chris Lattner00950542001-06-06 20:29:01 +00002015
Chris Lattner6cdb0112001-11-26 16:54:11 +00002016
2017// IndexList - List of indices for GEP based instructions...
2018IndexList : ',' ValueRefList {
Chris Lattner15c9c032003-09-08 18:20:29 +00002019 $$ = $2;
2020 } | /* empty */ {
2021 $$ = new std::vector<Value*>();
2022 };
2023
2024OptVolatile : VOLATILE {
2025 $$ = true;
2026 }
2027 | /* empty */ {
2028 $$ = false;
2029 };
2030
Chris Lattner027dcc52001-07-08 21:10:27 +00002031
Chris Lattnerddb6db42005-05-06 05:51:46 +00002032
Chris Lattner00950542001-06-06 20:29:01 +00002033MemoryInst : MALLOC Types {
Chris Lattner05804b72002-09-13 22:28:45 +00002034 $$ = new MallocInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00002035 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002036 }
2037 | MALLOC Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00002038 $$ = new MallocInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00002039 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002040 }
2041 | ALLOCA Types {
Chris Lattner05804b72002-09-13 22:28:45 +00002042 $$ = new AllocaInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00002043 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002044 }
2045 | ALLOCA Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00002046 $$ = new AllocaInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00002047 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002048 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002049 | FREE ResolvedVal {
Chris Lattner9b625032002-05-06 16:15:30 +00002050 if (!isa<PointerType>($2->getType()))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002051 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00002052 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002053 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00002054 }
2055
Chris Lattner15c9c032003-09-08 18:20:29 +00002056 | OptVolatile LOAD Types ValueRef {
2057 if (!isa<PointerType>($3->get()))
Chris Lattner2079fde2001-10-13 06:41:08 +00002058 ThrowException("Can't load from nonpointer type: " +
Reid Spencer3271ed52004-07-18 00:08:11 +00002059 (*$3)->getDescription());
Chris Lattner1c1bb332004-10-09 02:18:58 +00002060 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
2061 ThrowException("Can't load from pointer of non-first-class type: " +
2062 (*$3)->getDescription());
Chris Lattner79ad13802003-09-08 20:29:46 +00002063 $$ = new LoadInst(getVal(*$3, $4), "", $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002064 delete $3;
Chris Lattner027dcc52001-07-08 21:10:27 +00002065 }
Chris Lattner15c9c032003-09-08 18:20:29 +00002066 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
2067 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002068 if (!PT)
Chris Lattner72e00252001-12-14 16:28:42 +00002069 ThrowException("Can't store to a nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00002070 (*$5)->getDescription());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002071 const Type *ElTy = PT->getElementType();
Chris Lattner15c9c032003-09-08 18:20:29 +00002072 if (ElTy != $3->getType())
2073 ThrowException("Can't store '" + $3->getType()->getDescription() +
Chris Lattner72e00252001-12-14 16:28:42 +00002074 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner0383cc42002-08-21 23:51:21 +00002075
Chris Lattner79ad13802003-09-08 20:29:46 +00002076 $$ = new StoreInst($3, getVal(*$5, $6), $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002077 delete $5;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002078 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00002079 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00002080 if (!isa<PointerType>($2->get()))
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002081 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner830b6f92004-04-05 01:30:04 +00002082
2083 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
2084 // indices to uint struct indices for compatibility.
2085 generic_gep_type_iterator<std::vector<Value*>::iterator>
2086 GTI = gep_type_begin($2->get(), $4->begin(), $4->end()),
2087 GTE = gep_type_end($2->get(), $4->begin(), $4->end());
2088 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
2089 if (isa<StructType>(*GTI)) // Only change struct indices
2090 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
2091 if (CUI->getType() == Type::UByteTy)
2092 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
2093
Chris Lattner30c89792001-09-07 16:35:17 +00002094 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner830b6f92004-04-05 01:30:04 +00002095 ThrowException("Invalid getelementptr indices for type '" +
2096 (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00002097 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
2098 delete $2; delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00002099 };
Chris Lattner027dcc52001-07-08 21:10:27 +00002100
Brian Gaeked0fde302003-11-11 22:41:34 +00002101
Chris Lattner00950542001-06-06 20:29:01 +00002102%%
Chris Lattner09083092001-07-08 04:57:15 +00002103int yyerror(const char *ErrorMsg) {
Chris Lattner9232b992003-04-22 18:42:41 +00002104 std::string where
2105 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002106 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Chris Lattner9232b992003-04-22 18:42:41 +00002107 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
Chris Lattnerbc280ab2004-03-30 20:58:25 +00002108 if (yychar == YYEMPTY || yychar == 0)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002109 errMsg += "end-of-file.";
2110 else
Chris Lattner9232b992003-04-22 18:42:41 +00002111 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002112 ThrowException(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00002113 return 0;
2114}