blob: b26565180bbc3966300b15670c47e0dcc2e2aabb [file] [log] [blame]
Chris Lattner00950542001-06-06 20:29:01 +00001//===-- llvmAsmParser.y - Parser for llvm assembly files ---------*- C++ -*--=//
2//
3// This file implements the bison parser for LLVM assembly languages files.
4//
5//===------------------------------------------------------------------------=//
6
Chris Lattner00950542001-06-06 20:29:01 +00007%{
8#include "ParserInternals.h"
Chris Lattner00950542001-06-06 20:29:01 +00009#include "llvm/SymbolTable.h"
10#include "llvm/Module.h"
Chris Lattner00950542001-06-06 20:29:01 +000011#include "llvm/iTerminators.h"
12#include "llvm/iMemory.h"
Chris Lattner1cff96a2002-09-10 22:37:46 +000013#include "llvm/iOperators.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000014#include "llvm/iPHINode.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000015#include "Support/STLExtras.h"
16#include "Support/DepthFirstIterator.h"
Chris Lattner00950542001-06-06 20:29:01 +000017#include <list>
Chris Lattnerc188eeb2002-07-30 18:54:25 +000018#include <utility>
Chris Lattner30c89792001-09-07 16:35:17 +000019#include <algorithm>
Chris Lattner697954c2002-01-20 22:54:45 +000020using std::list;
21using std::vector;
22using std::pair;
23using std::map;
24using std::pair;
25using std::make_pair;
Chris Lattner697954c2002-01-20 22:54:45 +000026using std::string;
Chris Lattner00950542001-06-06 20:29:01 +000027
Chris Lattner386a3b72001-10-16 19:54:17 +000028int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
Chris Lattner09083092001-07-08 04:57:15 +000029int yylex(); // declaration" of xxx warnings.
Chris Lattner00950542001-06-06 20:29:01 +000030int yyparse();
31
32static Module *ParserResult;
Chris Lattnera2850432001-07-22 18:36:00 +000033string CurFilename;
Chris Lattner00950542001-06-06 20:29:01 +000034
Chris Lattner30c89792001-09-07 16:35:17 +000035// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
36// relating to upreferences in the input stream.
37//
38//#define DEBUG_UPREFS 1
39#ifdef DEBUG_UPREFS
Chris Lattner699f1eb2002-08-14 17:12:33 +000040#define UR_OUT(X) std::cerr << X
Chris Lattner30c89792001-09-07 16:35:17 +000041#else
42#define UR_OUT(X)
43#endif
44
Vikram S. Adved3f7eb02002-07-14 22:59:28 +000045#define YYERROR_VERBOSE 1
46
Chris Lattner0383cc42002-08-21 23:51:21 +000047// HACK ALERT: This variable is used to implement the automatic conversion of
48// load/store instructions with indexes into a load/store + getelementptr pair
49// of instructions. When this compatiblity "Feature" is removed, this should be
50// too.
51//
52static BasicBlock *CurBB;
53
54
Chris Lattner7e708292002-06-25 16:13:24 +000055// This contains info used when building the body of a function. It is
56// destroyed when the function is completed.
Chris Lattner00950542001-06-06 20:29:01 +000057//
58typedef vector<Value *> ValueList; // Numbered defs
Chris Lattner386a3b72001-10-16 19:54:17 +000059static void ResolveDefinitions(vector<ValueList> &LateResolvers,
60 vector<ValueList> *FutureLateResolvers = 0);
Chris Lattner00950542001-06-06 20:29:01 +000061
62static struct PerModuleInfo {
63 Module *CurrentModule;
Chris Lattner30c89792001-09-07 16:35:17 +000064 vector<ValueList> Values; // Module level numbered definitions
65 vector<ValueList> LateResolveValues;
Chris Lattner8b88b3b2002-04-04 19:23:55 +000066 vector<PATypeHolder> Types;
67 map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner00950542001-06-06 20:29:01 +000068
Chris Lattner2079fde2001-10-13 06:41:08 +000069 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
70 // references to global values. Global values may be referenced before they
71 // are defined, and if so, the temporary object that they represent is held
Chris Lattnere9bb2df2001-12-03 22:26:30 +000072 // here. This is used for forward references of ConstantPointerRefs.
Chris Lattner2079fde2001-10-13 06:41:08 +000073 //
74 typedef map<pair<const PointerType *, ValID>, GlobalVariable*> GlobalRefsType;
75 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 Lattner749ce032002-03-11 22:12:39 +000088 string UndefinedReferences = "Unresolved global references exist:\n";
89
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
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000104 // DeclareNewGlobalValue - Called every time a new GV has been defined. This
Chris Lattner2079fde2001-10-13 06:41:08 +0000105 // is used to remove things from the forward declaration map, resolving them
106 // to the correct thing as needed.
107 //
108 void DeclareNewGlobalValue(GlobalValue *GV, ValID D) {
109 // Check to see if there is a forward reference to this global variable...
110 // if there is, eliminate it and patch the reference to use the new def'n.
111 GlobalRefsType::iterator I = GlobalRefs.find(make_pair(GV->getType(), D));
112
113 if (I != GlobalRefs.end()) {
114 GlobalVariable *OldGV = I->second; // Get the placeholder...
115 I->first.second.destroy(); // Free string memory if neccesary
116
117 // Loop over all of the uses of the GlobalValue. The only thing they are
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000118 // allowed to be is ConstantPointerRef's.
Chris Lattner2079fde2001-10-13 06:41:08 +0000119 assert(OldGV->use_size() == 1 && "Only one reference should exist!");
Chris Lattner9e932bd2002-10-14 03:28:42 +0000120 User *U = OldGV->use_back(); // Must be a ConstantPointerRef...
121 ConstantPointerRef *CPR = cast<ConstantPointerRef>(U);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000122
Chris Lattner9e932bd2002-10-14 03:28:42 +0000123 // Change the const pool reference to point to the real global variable
124 // now. This should drop a use from the OldGV.
125 CPR->mutateReferences(OldGV, GV);
126 assert(OldGV->use_empty() && "All uses should be gone now!");
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000127
128 // Remove OldGV from the module...
Chris Lattner2079fde2001-10-13 06:41:08 +0000129 CurrentModule->getGlobalList().remove(OldGV);
130 delete OldGV; // Delete the old placeholder
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000131
Chris Lattner2079fde2001-10-13 06:41:08 +0000132 // Remove the map entry for the global now that it has been created...
133 GlobalRefs.erase(I);
134 }
135 }
136
Chris Lattner00950542001-06-06 20:29:01 +0000137} CurModule;
138
Chris Lattner79df7c02002-03-26 18:01:55 +0000139static struct PerFunctionInfo {
Chris Lattner7e708292002-06-25 16:13:24 +0000140 Function *CurrentFunction; // Pointer to current function being created
Chris Lattner00950542001-06-06 20:29:01 +0000141
Chris Lattnere1815642001-07-15 06:35:53 +0000142 vector<ValueList> Values; // Keep track of numbered definitions
Chris Lattner00950542001-06-06 20:29:01 +0000143 vector<ValueList> LateResolveValues;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000144 vector<PATypeHolder> Types;
145 map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner7e708292002-06-25 16:13:24 +0000146 bool isDeclare; // Is this function a forward declararation?
Chris Lattner00950542001-06-06 20:29:01 +0000147
Chris Lattner79df7c02002-03-26 18:01:55 +0000148 inline PerFunctionInfo() {
149 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000150 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000151 }
152
Chris Lattner79df7c02002-03-26 18:01:55 +0000153 inline ~PerFunctionInfo() {}
Chris Lattner00950542001-06-06 20:29:01 +0000154
Chris Lattner79df7c02002-03-26 18:01:55 +0000155 inline void FunctionStart(Function *M) {
156 CurrentFunction = M;
Chris Lattner00950542001-06-06 20:29:01 +0000157 }
158
Chris Lattner79df7c02002-03-26 18:01:55 +0000159 void FunctionDone() {
Chris Lattner00950542001-06-06 20:29:01 +0000160 // If we could not resolve some blocks at parsing time (forward branches)
161 // resolve the branches now...
Chris Lattner386a3b72001-10-16 19:54:17 +0000162 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000163
Chris Lattner7e708292002-06-25 16:13:24 +0000164 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +0000165 Types.clear();
Chris Lattner79df7c02002-03-26 18:01:55 +0000166 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000167 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000168 }
Chris Lattner7e708292002-06-25 16:13:24 +0000169} CurMeth; // Info for the current function...
Chris Lattner00950542001-06-06 20:29:01 +0000170
Chris Lattner79df7c02002-03-26 18:01:55 +0000171static bool inFunctionScope() { return CurMeth.CurrentFunction != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000172
Chris Lattner00950542001-06-06 20:29:01 +0000173
174//===----------------------------------------------------------------------===//
175// Code to handle definitions of all the types
176//===----------------------------------------------------------------------===//
177
Chris Lattner2079fde2001-10-13 06:41:08 +0000178static int InsertValue(Value *D, vector<ValueList> &ValueTab = CurMeth.Values) {
179 if (D->hasName()) return -1; // Is this a numbered definition?
180
181 // Yes, insert the value into the value table...
182 unsigned type = D->getType()->getUniqueID();
183 if (ValueTab.size() <= type)
184 ValueTab.resize(type+1, ValueList());
185 //printf("Values[%d][%d] = %d\n", type, ValueTab[type].size(), D);
186 ValueTab[type].push_back(D);
187 return ValueTab[type].size()-1;
Chris Lattner00950542001-06-06 20:29:01 +0000188}
189
Chris Lattner30c89792001-09-07 16:35:17 +0000190// TODO: FIXME when Type are not const
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000191static void InsertType(const Type *Ty, vector<PATypeHolder> &Types) {
Chris Lattner30c89792001-09-07 16:35:17 +0000192 Types.push_back(Ty);
193}
194
195static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000196 switch (D.Type) {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000197 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000198 unsigned Num = (unsigned)D.Num;
199
200 // Module constants occupy the lowest numbered slots...
201 if (Num < CurModule.Types.size())
202 return CurModule.Types[Num];
203
204 Num -= CurModule.Types.size();
205
206 // Check that the number is within bounds...
207 if (Num <= CurMeth.Types.size())
208 return CurMeth.Types[Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000209 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000210 }
Chris Lattnerf8dff732002-07-18 05:18:37 +0000211 case ValID::NameVal: { // Is it a named definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000212 string Name(D.Name);
213 SymbolTable *SymTab = 0;
Chris Lattner6e6026b2002-11-20 18:36:02 +0000214 Value *N = 0;
215 if (inFunctionScope()) {
216 SymTab = &CurMeth.CurrentFunction->getSymbolTable();
217 N = SymTab->lookup(Type::TypeTy, Name);
218 }
Chris Lattner30c89792001-09-07 16:35:17 +0000219
220 if (N == 0) {
Chris Lattner7e708292002-06-25 16:13:24 +0000221 // Symbol table doesn't automatically chain yet... because the function
Chris Lattner30c89792001-09-07 16:35:17 +0000222 // hasn't been added to the module...
223 //
Chris Lattner6e6026b2002-11-20 18:36:02 +0000224 SymTab = &CurModule.CurrentModule->getSymbolTable();
225 N = SymTab->lookup(Type::TypeTy, Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000226 if (N == 0) break;
227 }
228
229 D.destroy(); // Free old strdup'd memory...
Chris Lattnercfe26c92001-10-01 18:26:53 +0000230 return cast<const Type>(N);
Chris Lattner30c89792001-09-07 16:35:17 +0000231 }
232 default:
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000233 ThrowException("Internal parser error: Invalid symbol type reference!");
Chris Lattner30c89792001-09-07 16:35:17 +0000234 }
235
236 // If we reached here, we referenced either a symbol that we don't know about
237 // or an id number that hasn't been read yet. We may be referencing something
238 // forward, so just create an entry to be resolved later and get to it...
239 //
240 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
241
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000242 map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner4a42e902001-10-22 05:56:09 +0000243 CurMeth.LateResolveTypes : CurModule.LateResolveTypes;
244
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000245 map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000246 if (I != LateResolver.end()) {
247 return I->second;
248 }
Chris Lattner30c89792001-09-07 16:35:17 +0000249
Chris Lattner82269592001-10-22 06:01:08 +0000250 Type *Typ = OpaqueType::get();
Chris Lattner4a42e902001-10-22 05:56:09 +0000251 LateResolver.insert(make_pair(D, Typ));
Chris Lattner30c89792001-09-07 16:35:17 +0000252 return Typ;
253}
254
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000255static Value *lookupInSymbolTable(const Type *Ty, const string &Name) {
Chris Lattner6e6026b2002-11-20 18:36:02 +0000256 SymbolTable &SymTab =
Chris Lattner9705a152002-05-02 19:27:42 +0000257 inFunctionScope() ? CurMeth.CurrentFunction->getSymbolTable() :
258 CurModule.CurrentModule->getSymbolTable();
Chris Lattner6e6026b2002-11-20 18:36:02 +0000259 return SymTab.lookup(Ty, Name);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000260}
261
Chris Lattner2079fde2001-10-13 06:41:08 +0000262// getValNonImprovising - Look up the value specified by the provided type and
263// the provided ValID. If the value exists and has already been defined, return
264// it. Otherwise return null.
265//
266static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000267 if (isa<FunctionType>(Ty))
268 ThrowException("Functions are not values and "
269 "must be referenced as pointers");
Chris Lattner386a3b72001-10-16 19:54:17 +0000270
Chris Lattner30c89792001-09-07 16:35:17 +0000271 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000272 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000273 unsigned type = Ty->getUniqueID();
Chris Lattner00950542001-06-06 20:29:01 +0000274 unsigned Num = (unsigned)D.Num;
275
276 // Module constants occupy the lowest numbered slots...
277 if (type < CurModule.Values.size()) {
278 if (Num < CurModule.Values[type].size())
279 return CurModule.Values[type][Num];
280
281 Num -= CurModule.Values[type].size();
282 }
283
284 // Make sure that our type is within bounds
Chris Lattner2079fde2001-10-13 06:41:08 +0000285 if (CurMeth.Values.size() <= type) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000286
287 // Check that the number is within bounds...
Chris Lattner2079fde2001-10-13 06:41:08 +0000288 if (CurMeth.Values[type].size() <= Num) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000289
290 return CurMeth.Values[type][Num];
291 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000292
Chris Lattner1a1cb112001-09-30 22:46:54 +0000293 case ValID::NameVal: { // Is it a named definition?
Chris Lattner2079fde2001-10-13 06:41:08 +0000294 Value *N = lookupInSymbolTable(Ty, string(D.Name));
295 if (N == 0) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000296
297 D.destroy(); // Free old strdup'd memory...
298 return N;
299 }
300
Chris Lattner2079fde2001-10-13 06:41:08 +0000301 // Check to make sure that "Ty" is an integral type, and that our
302 // value will fit into the specified type...
303 case ValID::ConstSIntVal: // Is it a constant pool reference??
Chris Lattnerd78700d2002-08-16 21:14:40 +0000304 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
305 ThrowException("Signed integral constant '" +
306 itostr(D.ConstPool64) + "' is invalid for type '" +
307 Ty->getDescription() + "'!");
308 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000309
310 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000311 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
312 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000313 ThrowException("Integral constant '" + utostr(D.UConstPool64) +
314 "' is invalid or out of range!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000315 } else { // This is really a signed reference. Transmogrify.
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000316 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000317 }
318 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000319 return ConstantUInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000320 }
321
Chris Lattner2079fde2001-10-13 06:41:08 +0000322 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000323 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattner2079fde2001-10-13 06:41:08 +0000324 ThrowException("FP constant invalid for type!!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000325 return ConstantFP::get(Ty, D.ConstPoolFP);
Chris Lattner2079fde2001-10-13 06:41:08 +0000326
327 case ValID::ConstNullVal: // Is it a null value?
Chris Lattner9b625032002-05-06 16:15:30 +0000328 if (!isa<PointerType>(Ty))
Chris Lattner2079fde2001-10-13 06:41:08 +0000329 ThrowException("Cannot create a a non pointer null!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000330 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner2079fde2001-10-13 06:41:08 +0000331
Chris Lattnerd78700d2002-08-16 21:14:40 +0000332 case ValID::ConstantVal: // Fully resolved constant?
333 if (D.ConstantValue->getType() != Ty)
334 ThrowException("Constant expression type different from required type!");
335 return D.ConstantValue;
336
Chris Lattner30c89792001-09-07 16:35:17 +0000337 default:
338 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000339 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000340 } // End of switch
341
Chris Lattner2079fde2001-10-13 06:41:08 +0000342 assert(0 && "Unhandled case!");
343 return 0;
344}
345
346
347// getVal - This function is identical to getValNonImprovising, except that if a
348// value is not already defined, it "improvises" by creating a placeholder var
349// that looks and acts just like the requested variable. When the value is
350// defined later, all uses of the placeholder variable are replaced with the
351// real thing.
352//
353static Value *getVal(const Type *Ty, const ValID &D) {
354 assert(Ty != Type::TypeTy && "Should use getTypeVal for types!");
355
356 // See if the value has already been defined...
357 Value *V = getValNonImprovising(Ty, D);
358 if (V) return V;
Chris Lattner00950542001-06-06 20:29:01 +0000359
360 // If we reached here, we referenced either a symbol that we don't know about
361 // or an id number that hasn't been read yet. We may be referencing something
362 // forward, so just create an entry to be resolved later and get to it...
363 //
Chris Lattner00950542001-06-06 20:29:01 +0000364 Value *d = 0;
Chris Lattner30c89792001-09-07 16:35:17 +0000365 switch (Ty->getPrimitiveID()) {
366 case Type::LabelTyID: d = new BBPlaceHolder(Ty, D); break;
Chris Lattner30c89792001-09-07 16:35:17 +0000367 default: d = new ValuePlaceHolder(Ty, D); break;
Chris Lattner00950542001-06-06 20:29:01 +0000368 }
369
370 assert(d != 0 && "How did we not make something?");
Chris Lattner79df7c02002-03-26 18:01:55 +0000371 if (inFunctionScope())
Chris Lattner386a3b72001-10-16 19:54:17 +0000372 InsertValue(d, CurMeth.LateResolveValues);
373 else
374 InsertValue(d, CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000375 return d;
376}
377
378
379//===----------------------------------------------------------------------===//
380// Code to handle forward references in instructions
381//===----------------------------------------------------------------------===//
382//
383// This code handles the late binding needed with statements that reference
384// values not defined yet... for example, a forward branch, or the PHI node for
385// a loop body.
386//
387// This keeps a table (CurMeth.LateResolveValues) of all such forward references
388// and back patchs after we are done.
389//
390
391// ResolveDefinitions - If we could not resolve some defs at parsing
392// time (forward branches, phi functions for loops, etc...) resolve the
393// defs now...
394//
Chris Lattner386a3b72001-10-16 19:54:17 +0000395static void ResolveDefinitions(vector<ValueList> &LateResolvers,
Chris Lattnerbcafcce2002-07-25 06:17:42 +0000396 vector<ValueList> *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000397 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
398 for (unsigned ty = 0; ty < LateResolvers.size(); ty++) {
399 while (!LateResolvers[ty].empty()) {
400 Value *V = LateResolvers[ty].back();
Chris Lattner386a3b72001-10-16 19:54:17 +0000401 assert(!isa<Type>(V) && "Types should be in LateResolveTypes!");
402
Chris Lattner00950542001-06-06 20:29:01 +0000403 LateResolvers[ty].pop_back();
404 ValID &DID = getValIDFromPlaceHolder(V);
405
Chris Lattner2079fde2001-10-13 06:41:08 +0000406 Value *TheRealValue = getValNonImprovising(Type::getUniqueIDType(ty),DID);
Chris Lattner386a3b72001-10-16 19:54:17 +0000407 if (TheRealValue) {
408 V->replaceAllUsesWith(TheRealValue);
409 delete V;
410 } else if (FutureLateResolvers) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000411 // Functions have their unresolved items forwarded to the module late
Chris Lattner386a3b72001-10-16 19:54:17 +0000412 // resolver table
413 InsertValue(V, *FutureLateResolvers);
414 } else {
Chris Lattner9705a152002-05-02 19:27:42 +0000415 if (DID.Type == ValID::NameVal)
Chris Lattner30c89792001-09-07 16:35:17 +0000416 ThrowException("Reference to an invalid definition: '" +DID.getName()+
417 "' of type '" + V->getType()->getDescription() + "'",
418 getLineNumFromPlaceHolder(V));
419 else
420 ThrowException("Reference to an invalid definition: #" +
421 itostr(DID.Num) + " of type '" +
422 V->getType()->getDescription() + "'",
423 getLineNumFromPlaceHolder(V));
424 }
Chris Lattner00950542001-06-06 20:29:01 +0000425 }
426 }
427
428 LateResolvers.clear();
429}
430
Chris Lattner4a42e902001-10-22 05:56:09 +0000431// ResolveTypeTo - A brand new type was just declared. This means that (if
432// name is not null) things referencing Name can be resolved. Otherwise, things
433// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000434//
Chris Lattner4a42e902001-10-22 05:56:09 +0000435static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000436 vector<PATypeHolder> &Types = inFunctionScope() ?
Chris Lattner4a42e902001-10-22 05:56:09 +0000437 CurMeth.Types : CurModule.Types;
Chris Lattner00950542001-06-06 20:29:01 +0000438
Chris Lattner4a42e902001-10-22 05:56:09 +0000439 ValID D;
440 if (Name) D = ValID::create(Name);
441 else D = ValID::create((int)Types.size());
Chris Lattner30c89792001-09-07 16:35:17 +0000442
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000443 map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner4a42e902001-10-22 05:56:09 +0000444 CurMeth.LateResolveTypes : CurModule.LateResolveTypes;
445
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000446 map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000447 if (I != LateResolver.end()) {
Chris Lattner51727be2002-06-04 21:58:56 +0000448 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattner4a42e902001-10-22 05:56:09 +0000449 LateResolver.erase(I);
450 }
451}
452
453// ResolveTypes - At this point, all types should be resolved. Any that aren't
454// are errors.
455//
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000456static void ResolveTypes(map<ValID, PATypeHolder> &LateResolveTypes) {
Chris Lattner4a42e902001-10-22 05:56:09 +0000457 if (!LateResolveTypes.empty()) {
Chris Lattner82269592001-10-22 06:01:08 +0000458 const ValID &DID = LateResolveTypes.begin()->first;
Chris Lattner4a42e902001-10-22 05:56:09 +0000459
460 if (DID.Type == ValID::NameVal)
Chris Lattner82269592001-10-22 06:01:08 +0000461 ThrowException("Reference to an invalid type: '" +DID.getName() + "'");
Chris Lattner4a42e902001-10-22 05:56:09 +0000462 else
Chris Lattner82269592001-10-22 06:01:08 +0000463 ThrowException("Reference to an invalid type: #" + itostr(DID.Num));
Chris Lattner30c89792001-09-07 16:35:17 +0000464 }
465}
466
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000467
Chris Lattner1781aca2001-09-18 04:00:54 +0000468// setValueName - Set the specified value to the name given. The name may be
469// null potentially, in which case this is a noop. The string passed in is
470// assumed to be a malloc'd string buffer, and is freed by this function.
471//
Chris Lattnerb7474512001-10-03 15:39:04 +0000472// This function returns true if the value has already been defined, but is
473// allowed to be redefined in the specified context. If the name is a new name
474// for the typeplane, false is returned.
475//
476static bool setValueName(Value *V, char *NameStr) {
477 if (NameStr == 0) return false;
Chris Lattner386a3b72001-10-16 19:54:17 +0000478
Chris Lattner1781aca2001-09-18 04:00:54 +0000479 string Name(NameStr); // Copy string
480 free(NameStr); // Free old string
481
Chris Lattner2079fde2001-10-13 06:41:08 +0000482 if (V->getType() == Type::VoidTy)
483 ThrowException("Can't assign name '" + Name +
484 "' to a null valued instruction!");
485
Chris Lattner6e6026b2002-11-20 18:36:02 +0000486 SymbolTable &ST = inFunctionScope() ?
487 CurMeth.CurrentFunction->getSymbolTable() :
488 CurModule.CurrentModule->getSymbolTable();
Chris Lattner30c89792001-09-07 16:35:17 +0000489
Chris Lattner6e6026b2002-11-20 18:36:02 +0000490 Value *Existing = ST.lookup(V->getType(), Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000491 if (Existing) { // Inserting a name that is already defined???
492 // There is only one case where this is allowed: when we are refining an
493 // opaque type. In this case, Existing will be an opaque type.
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000494 if (const Type *Ty = dyn_cast<const Type>(Existing)) {
Chris Lattner51727be2002-06-04 21:58:56 +0000495 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Ty)) {
Chris Lattner30c89792001-09-07 16:35:17 +0000496 // We ARE replacing an opaque type!
Chris Lattner51727be2002-06-04 21:58:56 +0000497 ((OpaqueType*)OpTy)->refineAbstractTypeTo(cast<Type>(V));
Chris Lattnerb7474512001-10-03 15:39:04 +0000498 return true;
Chris Lattner30c89792001-09-07 16:35:17 +0000499 }
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000500 }
Chris Lattner30c89792001-09-07 16:35:17 +0000501
Chris Lattner9636a912001-10-01 16:18:37 +0000502 // Otherwise, we are a simple redefinition of a value, check to see if it
503 // is defined the same as the old one...
Chris Lattneradf99702003-03-03 23:28:55 +0000504 if (const Type *Ty = dyn_cast<Type>(Existing)) {
505 if (Ty == cast<Type>(V)) return true; // Yes, it's equal.
Chris Lattner699f1eb2002-08-14 17:12:33 +0000506 // std::cerr << "Type: " << Ty->getDescription() << " != "
Chris Lattnerb7474512001-10-03 15:39:04 +0000507 // << cast<const Type>(V)->getDescription() << "!\n";
Chris Lattneradf99702003-03-03 23:28:55 +0000508 } else if (const Constant *C = dyn_cast<Constant>(Existing)) {
509 if (C == V) return true; // Constants are equal to themselves
Chris Lattnerb7474512001-10-03 15:39:04 +0000510 } else if (GlobalVariable *EGV = dyn_cast<GlobalVariable>(Existing)) {
Chris Lattner43efcbf2001-10-03 19:35:57 +0000511 // We are allowed to redefine a global variable in two circumstances:
512 // 1. If at least one of the globals is uninitialized or
513 // 2. If both initializers have the same value.
514 //
Chris Lattner89219832001-10-03 19:35:04 +0000515 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000516 if (!EGV->hasInitializer() || !GV->hasInitializer() ||
517 EGV->getInitializer() == GV->getInitializer()) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000518
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000519 // Make sure the existing global version gets the initializer! Make
520 // sure that it also gets marked const if the new version is.
Chris Lattner89219832001-10-03 19:35:04 +0000521 if (GV->hasInitializer() && !EGV->hasInitializer())
522 EGV->setInitializer(GV->getInitializer());
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000523 if (GV->isConstant())
524 EGV->setConstant(true);
Chris Lattner4ad02e72003-04-16 20:28:45 +0000525 EGV->setLinkage(GV->getLinkage());
Chris Lattner89219832001-10-03 19:35:04 +0000526
Chris Lattner2079fde2001-10-13 06:41:08 +0000527 delete GV; // Destroy the duplicate!
Chris Lattner89219832001-10-03 19:35:04 +0000528 return true; // They are equivalent!
529 }
Chris Lattnerb7474512001-10-03 15:39:04 +0000530 }
Chris Lattner9636a912001-10-01 16:18:37 +0000531 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000532 ThrowException("Redefinition of value named '" + Name + "' in the '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000533 V->getType()->getDescription() + "' type plane!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000534 }
Chris Lattner00950542001-06-06 20:29:01 +0000535
Chris Lattner6e6026b2002-11-20 18:36:02 +0000536 V->setName(Name, &ST);
Chris Lattnerb7474512001-10-03 15:39:04 +0000537 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000538}
539
Chris Lattner8896eda2001-07-09 19:38:36 +0000540
Chris Lattner30c89792001-09-07 16:35:17 +0000541//===----------------------------------------------------------------------===//
542// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000543//
Chris Lattner8896eda2001-07-09 19:38:36 +0000544
Chris Lattner30c89792001-09-07 16:35:17 +0000545// TypeContains - Returns true if Ty contains E in it.
546//
547static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner3ff43872001-09-28 22:56:31 +0000548 return find(df_begin(Ty), df_end(Ty), E) != df_end(Ty);
Chris Lattner30c89792001-09-07 16:35:17 +0000549}
Chris Lattner698b56e2001-07-20 19:15:08 +0000550
Chris Lattner30c89792001-09-07 16:35:17 +0000551
552static vector<pair<unsigned, OpaqueType *> > UpRefs;
553
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000554static PATypeHolder HandleUpRefs(const Type *ty) {
555 PATypeHolder Ty(ty);
Chris Lattner5084d032001-11-02 07:46:26 +0000556 UR_OUT("Type '" << ty->getDescription() <<
557 "' newly formed. Resolving upreferences.\n" <<
558 UpRefs.size() << " upreferences active!\n");
Chris Lattner30c89792001-09-07 16:35:17 +0000559 for (unsigned i = 0; i < UpRefs.size(); ) {
Chris Lattner5084d032001-11-02 07:46:26 +0000560 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Chris Lattner30c89792001-09-07 16:35:17 +0000561 << UpRefs[i].second->getDescription() << ") = "
Chris Lattner5084d032001-11-02 07:46:26 +0000562 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << endl);
Chris Lattner30c89792001-09-07 16:35:17 +0000563 if (TypeContains(Ty, UpRefs[i].second)) {
564 unsigned Level = --UpRefs[i].first; // Decrement level of upreference
Chris Lattner5084d032001-11-02 07:46:26 +0000565 UR_OUT(" Uplevel Ref Level = " << Level << endl);
Chris Lattner30c89792001-09-07 16:35:17 +0000566 if (Level == 0) { // Upreference should be resolved!
Chris Lattner5084d032001-11-02 07:46:26 +0000567 UR_OUT(" * Resolving upreference for "
568 << UpRefs[i].second->getDescription() << endl;
Chris Lattner30c89792001-09-07 16:35:17 +0000569 string OldName = UpRefs[i].second->getDescription());
570 UpRefs[i].second->refineAbstractTypeTo(Ty);
571 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
Chris Lattner5084d032001-11-02 07:46:26 +0000572 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
Chris Lattner30c89792001-09-07 16:35:17 +0000573 << (const void*)Ty << ", " << Ty->getDescription() << endl);
574 continue;
575 }
576 }
577
578 ++i; // Otherwise, no resolve, move on...
Chris Lattner8896eda2001-07-09 19:38:36 +0000579 }
Chris Lattner30c89792001-09-07 16:35:17 +0000580 // FIXME: TODO: this should return the updated type
Chris Lattner8896eda2001-07-09 19:38:36 +0000581 return Ty;
582}
583
Chris Lattner30c89792001-09-07 16:35:17 +0000584
Chris Lattner00950542001-06-06 20:29:01 +0000585//===----------------------------------------------------------------------===//
586// RunVMAsmParser - Define an interface to this parser
587//===----------------------------------------------------------------------===//
588//
Chris Lattnera2850432001-07-22 18:36:00 +0000589Module *RunVMAsmParser(const string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000590 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000591 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000592 llvmAsmlineno = 1; // Reset the current line number...
593
594 CurModule.CurrentModule = new Module(); // Allocate a new module to read
595 yyparse(); // Parse the file.
596 Module *Result = ParserResult;
Chris Lattner00950542001-06-06 20:29:01 +0000597 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
598 ParserResult = 0;
599
600 return Result;
601}
602
603%}
604
605%union {
Chris Lattner30c89792001-09-07 16:35:17 +0000606 Module *ModuleVal;
Chris Lattner79df7c02002-03-26 18:01:55 +0000607 Function *FunctionVal;
Chris Lattner69da5cf2002-10-13 20:57:00 +0000608 std::pair<PATypeHolder*, char*> *ArgVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000609 BasicBlock *BasicBlockVal;
610 TerminatorInst *TermInstVal;
611 Instruction *InstVal;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000612 Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000613
Chris Lattner30c89792001-09-07 16:35:17 +0000614 const Type *PrimType;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000615 PATypeHolder *TypeVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000616 Value *ValueVal;
617
Chris Lattner69da5cf2002-10-13 20:57:00 +0000618 std::vector<std::pair<PATypeHolder*,char*> > *ArgList;
Chris Lattner697954c2002-01-20 22:54:45 +0000619 std::vector<Value*> *ValueList;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000620 std::list<PATypeHolder> *TypeList;
Chris Lattner697954c2002-01-20 22:54:45 +0000621 std::list<std::pair<Value*,
622 BasicBlock*> > *PHIList; // Represent the RHS of PHI node
Chris Lattner46748042002-04-09 19:41:42 +0000623 std::vector<std::pair<Constant*, BasicBlock*> > *JumpTable;
Chris Lattner697954c2002-01-20 22:54:45 +0000624 std::vector<Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000625
Chris Lattner4ad02e72003-04-16 20:28:45 +0000626 GlobalValue::LinkageTypes Linkage;
Chris Lattner30c89792001-09-07 16:35:17 +0000627 int64_t SInt64Val;
628 uint64_t UInt64Val;
629 int SIntVal;
630 unsigned UIntVal;
631 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000632 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000633
Chris Lattner30c89792001-09-07 16:35:17 +0000634 char *StrVal; // This memory is strdup'd!
635 ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000636
Chris Lattner30c89792001-09-07 16:35:17 +0000637 Instruction::BinaryOps BinaryOpVal;
638 Instruction::TermOps TermOpVal;
639 Instruction::MemoryOps MemOpVal;
640 Instruction::OtherOps OtherOpVal;
Chris Lattner00950542001-06-06 20:29:01 +0000641}
642
Chris Lattner79df7c02002-03-26 18:01:55 +0000643%type <ModuleVal> Module FunctionList
644%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000645%type <BasicBlockVal> BasicBlock InstructionList
646%type <TermInstVal> BBTerminatorInst
647%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000648%type <ConstVal> ConstVal ConstExpr
Chris Lattner6cdb0112001-11-26 16:54:11 +0000649%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000650%type <ArgList> ArgList ArgListH
651%type <ArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000652%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000653%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000654%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000655%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000656%type <JumpTable> JumpTable
Chris Lattner4ad02e72003-04-16 20:28:45 +0000657%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
658%type <Linkage> OptLinkage
Chris Lattner00950542001-06-06 20:29:01 +0000659
Chris Lattner2079fde2001-10-13 06:41:08 +0000660// ValueRef - Unresolved reference to a definition or BB
661%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000662%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000663// Tokens and types for handling constant integer values
664//
665// ESINT64VAL - A negative number within long long range
666%token <SInt64Val> ESINT64VAL
667
668// EUINT64VAL - A positive number within uns. long long range
669%token <UInt64Val> EUINT64VAL
670%type <SInt64Val> EINT64VAL
671
672%token <SIntVal> SINTVAL // Signed 32 bit ints...
673%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
674%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000675%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000676
677// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000678%type <TypeVal> Types TypesV UpRTypes UpRTypesV
679%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattner30c89792001-09-07 16:35:17 +0000680%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
681%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000682
683%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
Chris Lattner8ebccb72002-05-22 22:33:00 +0000684%type <StrVal> OptVAR_ID OptAssign FuncName
Chris Lattner00950542001-06-06 20:29:01 +0000685
686
Chris Lattner08c0e6a2002-10-06 22:45:09 +0000687%token IMPLEMENTATION TRUE FALSE BEGINTOK ENDTOK DECLARE GLOBAL CONSTANT
Chris Lattner4ad02e72003-04-16 20:28:45 +0000688%token TO EXCEPT DOTDOTDOT NULL_TOK CONST INTERNAL LINKONCE APPENDING
689%token OPAQUE NOT EXTERNAL
Chris Lattner00950542001-06-06 20:29:01 +0000690
691// Basic Block Terminating Operators
692%token <TermOpVal> RET BR SWITCH
693
Chris Lattner00950542001-06-06 20:29:01 +0000694// Binary Operators
695%type <BinaryOpVal> BinaryOps // all the binary operators
Chris Lattner4a6482b2002-09-10 19:57:26 +0000696%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Chris Lattner42c9e772001-10-20 09:32:59 +0000697%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000698%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000699
700// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000701%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000702
Chris Lattner027dcc52001-07-08 21:10:27 +0000703// Other Operators
704%type <OtherOpVal> ShiftOps
Chris Lattner2079fde2001-10-13 06:41:08 +0000705%token <OtherOpVal> PHI CALL INVOKE CAST SHL SHR
Chris Lattner027dcc52001-07-08 21:10:27 +0000706
Chris Lattner00950542001-06-06 20:29:01 +0000707%start Module
708%%
709
710// Handle constant integer size restriction and conversion...
711//
712
Chris Lattner51727be2002-06-04 21:58:56 +0000713INTVAL : SINTVAL;
Chris Lattner00950542001-06-06 20:29:01 +0000714INTVAL : UINTVAL {
715 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
716 ThrowException("Value too large for type!");
717 $$ = (int32_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000718};
Chris Lattner00950542001-06-06 20:29:01 +0000719
720
Chris Lattner51727be2002-06-04 21:58:56 +0000721EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner00950542001-06-06 20:29:01 +0000722EINT64VAL : EUINT64VAL {
723 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
724 ThrowException("Value too large for type!");
725 $$ = (int64_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000726};
Chris Lattner00950542001-06-06 20:29:01 +0000727
Chris Lattner00950542001-06-06 20:29:01 +0000728// Operations that are notably excluded from this list include:
729// RET, BR, & SWITCH because they end basic blocks and are treated specially.
730//
Chris Lattner4a6482b2002-09-10 19:57:26 +0000731ArithmeticOps: ADD | SUB | MUL | DIV | REM;
732LogicalOps : AND | OR | XOR;
733SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
734BinaryOps : ArithmeticOps | LogicalOps | SetCondOps;
735
Chris Lattner51727be2002-06-04 21:58:56 +0000736ShiftOps : SHL | SHR;
Chris Lattner00950542001-06-06 20:29:01 +0000737
Chris Lattnere98dda62001-07-14 06:10:16 +0000738// These are some types that allow classification if we only want a particular
739// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner51727be2002-06-04 21:58:56 +0000740SIntType : LONG | INT | SHORT | SBYTE;
741UIntType : ULONG | UINT | USHORT | UBYTE;
742IntType : SIntType | UIntType;
743FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +0000744
Chris Lattnere98dda62001-07-14 06:10:16 +0000745// OptAssign - Value producing statements have an optional assignment component
Chris Lattner00950542001-06-06 20:29:01 +0000746OptAssign : VAR_ID '=' {
747 $$ = $1;
748 }
749 | /*empty*/ {
750 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +0000751 };
Chris Lattner00950542001-06-06 20:29:01 +0000752
Chris Lattner4ad02e72003-04-16 20:28:45 +0000753OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
754 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
755 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
756 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
Chris Lattner30c89792001-09-07 16:35:17 +0000757
758//===----------------------------------------------------------------------===//
759// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +0000760// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +0000761// access to it, a user must explicitly use TypesV.
762//
763
764// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +0000765TypesV : Types | VOID { $$ = new PATypeHolder($1); };
766UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +0000767
768Types : UpRTypes {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000769 if (UpRefs.size())
770 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
771 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000772 };
Chris Lattner30c89792001-09-07 16:35:17 +0000773
774
775// Derived types are added later...
776//
Chris Lattner51727be2002-06-04 21:58:56 +0000777PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
778PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000779UpRTypes : OPAQUE {
780 $$ = new PATypeHolder(OpaqueType::get());
781 }
782 | PrimType {
783 $$ = new PATypeHolder($1);
Chris Lattner51727be2002-06-04 21:58:56 +0000784 };
Chris Lattnerd78700d2002-08-16 21:14:40 +0000785UpRTypes : SymbolicValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000786 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner51727be2002-06-04 21:58:56 +0000787};
Chris Lattner30c89792001-09-07 16:35:17 +0000788
Chris Lattner30c89792001-09-07 16:35:17 +0000789// Include derived types in the Types production.
790//
791UpRTypes : '\\' EUINT64VAL { // Type UpReference
792 if ($2 > (uint64_t)INT64_MAX) ThrowException("Value out of range!");
793 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
794 UpRefs.push_back(make_pair((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000795 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +0000796 UR_OUT("New Upreference!\n");
797 }
Chris Lattner79df7c02002-03-26 18:01:55 +0000798 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner30c89792001-09-07 16:35:17 +0000799 vector<const Type*> Params;
Chris Lattner697954c2002-01-20 22:54:45 +0000800 mapto($3->begin(), $3->end(), std::back_inserter(Params),
801 std::mem_fun_ref(&PATypeHandle<Type>::get));
Chris Lattner2079fde2001-10-13 06:41:08 +0000802 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
803 if (isVarArg) Params.pop_back();
804
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000805 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +0000806 delete $3; // Delete the argument list
807 delete $1; // Delete the old type handle
808 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000809 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000810 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000811 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +0000812 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000813 | '{' TypeListI '}' { // Structure type?
814 vector<const Type*> Elements;
Chris Lattner697954c2002-01-20 22:54:45 +0000815 mapto($2->begin(), $2->end(), std::back_inserter(Elements),
816 std::mem_fun_ref(&PATypeHandle<Type>::get));
Chris Lattner30c89792001-09-07 16:35:17 +0000817
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000818 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000819 delete $2;
820 }
821 | '{' '}' { // Empty structure type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000822 $$ = new PATypeHolder(StructType::get(vector<const Type*>()));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000823 }
824 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000825 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000826 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000827 };
Chris Lattner30c89792001-09-07 16:35:17 +0000828
Chris Lattner7e708292002-06-25 16:13:24 +0000829// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +0000830// declaration type lists
831//
832TypeListI : UpRTypes {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000833 $$ = new list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +0000834 $$->push_back(*$1); delete $1;
835 }
836 | TypeListI ',' UpRTypes {
837 ($$=$1)->push_back(*$3); delete $3;
Chris Lattner51727be2002-06-04 21:58:56 +0000838 };
Chris Lattner30c89792001-09-07 16:35:17 +0000839
Chris Lattner7e708292002-06-25 16:13:24 +0000840// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +0000841ArgTypeListI : TypeListI
842 | TypeListI ',' DOTDOTDOT {
843 ($$=$1)->push_back(Type::VoidTy);
844 }
845 | DOTDOTDOT {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000846 ($$ = new list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattner30c89792001-09-07 16:35:17 +0000847 }
848 | /*empty*/ {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000849 $$ = new list<PATypeHolder>();
Chris Lattner51727be2002-06-04 21:58:56 +0000850 };
Chris Lattner30c89792001-09-07 16:35:17 +0000851
Chris Lattnere98dda62001-07-14 06:10:16 +0000852// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +0000853// production is used ONLY to represent constants that show up AFTER a 'const',
854// 'constant' or 'global' token at global scope. Constants that can be inlined
855// into other expressions (such as integers and constexprs) are handled by the
856// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +0000857//
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000858ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
859 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
860 if (ATy == 0)
861 ThrowException("Cannot make array constant with type: '" +
862 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +0000863 const Type *ETy = ATy->getElementType();
864 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +0000865
Chris Lattner30c89792001-09-07 16:35:17 +0000866 // Verify that we have the correct size...
867 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +0000868 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattner30c89792001-09-07 16:35:17 +0000869 utostr($3->size()) + " arguments, but has size of " +
870 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +0000871
Chris Lattner30c89792001-09-07 16:35:17 +0000872 // Verify all elements are correct type!
873 for (unsigned i = 0; i < $3->size(); i++) {
874 if (ETy != (*$3)[i]->getType())
Chris Lattner00950542001-06-06 20:29:01 +0000875 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +0000876 ETy->getDescription() +"' as required!\nIt is of type '"+
877 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +0000878 }
879
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000880 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +0000881 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000882 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000883 | Types '[' ']' {
884 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
885 if (ATy == 0)
886 ThrowException("Cannot make array constant with type: '" +
887 (*$1)->getDescription() + "'!");
888
889 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +0000890 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +0000891 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattner30c89792001-09-07 16:35:17 +0000892 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000893 $$ = ConstantArray::get(ATy, vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +0000894 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +0000895 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000896 | Types 'c' STRINGCONSTANT {
897 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
898 if (ATy == 0)
899 ThrowException("Cannot make array constant with type: '" +
900 (*$1)->getDescription() + "'!");
901
Chris Lattner30c89792001-09-07 16:35:17 +0000902 int NumElements = ATy->getNumElements();
903 const Type *ETy = ATy->getElementType();
904 char *EndStr = UnEscapeLexed($3, true);
905 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +0000906 ThrowException("Can't build string constant of size " +
Chris Lattner30c89792001-09-07 16:35:17 +0000907 itostr((int)(EndStr-$3)) +
908 " when array has size " + itostr(NumElements) + "!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000909 vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +0000910 if (ETy == Type::SByteTy) {
911 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000912 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +0000913 } else if (ETy == Type::UByteTy) {
914 for (char *C = $3; C != EndStr; ++C)
Chris Lattnerbae362f2003-01-30 22:24:26 +0000915 Vals.push_back(ConstantUInt::get(ETy, (unsigned char)*C));
Chris Lattner93750fa2001-07-28 17:48:55 +0000916 } else {
Chris Lattner30c89792001-09-07 16:35:17 +0000917 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +0000918 ThrowException("Cannot build string arrays of non byte sized elements!");
919 }
Chris Lattner30c89792001-09-07 16:35:17 +0000920 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000921 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +0000922 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +0000923 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000924 | Types '{' ConstVector '}' {
925 const StructType *STy = dyn_cast<const StructType>($1->get());
926 if (STy == 0)
927 ThrowException("Cannot make struct constant with type: '" +
928 (*$1)->getDescription() + "'!");
Chris Lattneraf76d0e2003-04-15 16:09:31 +0000929
930 // Check to ensure that constants are compatible with the type initializer!
931 for (unsigned i = 0, e = $3->size(); i != e; ++i)
932 if ((*$3)[i]->getType() != STy->getElementTypes()[i])
933 ThrowException("Expected type '" +
934 STy->getElementTypes()[i]->getDescription() +
935 "' for element #" + utostr(i) +
936 " of structure initializer!");
937
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000938 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +0000939 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000940 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000941 | Types NULL_TOK {
942 const PointerType *PTy = dyn_cast<const PointerType>($1->get());
943 if (PTy == 0)
944 ThrowException("Cannot make null pointer constant with type: '" +
945 (*$1)->getDescription() + "'!");
946
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000947 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000948 delete $1;
949 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000950 | Types SymbolicValueRef {
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000951 const PointerType *Ty = dyn_cast<const PointerType>($1->get());
952 if (Ty == 0)
953 ThrowException("Global const reference must be a pointer type!");
954
Chris Lattner3101c252002-08-15 17:58:33 +0000955 // ConstExprs can exist in the body of a function, thus creating
956 // ConstantPointerRefs whenever they refer to a variable. Because we are in
957 // the context of a function, getValNonImprovising will search the functions
958 // symbol table instead of the module symbol table for the global symbol,
959 // which throws things all off. To get around this, we just tell
960 // getValNonImprovising that we are at global scope here.
961 //
962 Function *SavedCurFn = CurMeth.CurrentFunction;
963 CurMeth.CurrentFunction = 0;
964
Chris Lattner2079fde2001-10-13 06:41:08 +0000965 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000966
Chris Lattner3101c252002-08-15 17:58:33 +0000967 CurMeth.CurrentFunction = SavedCurFn;
968
969
Chris Lattner2079fde2001-10-13 06:41:08 +0000970 // If this is an initializer for a constant pointer, which is referencing a
971 // (currently) undefined variable, create a stub now that shall be replaced
972 // in the future with the right type of variable.
973 //
974 if (V == 0) {
975 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
976 const PointerType *PT = cast<PointerType>(Ty);
977
978 // First check to see if the forward references value is already created!
979 PerModuleInfo::GlobalRefsType::iterator I =
980 CurModule.GlobalRefs.find(make_pair(PT, $2));
981
982 if (I != CurModule.GlobalRefs.end()) {
983 V = I->second; // Placeholder already exists, use it...
984 } else {
985 // TODO: Include line number info by creating a subclass of
986 // TODO: GlobalVariable here that includes the said information!
987
988 // Create a placeholder for the global variable reference...
Chris Lattner7a176752001-12-04 00:03:30 +0000989 GlobalVariable *GV = new GlobalVariable(PT->getElementType(),
Chris Lattner4ad02e72003-04-16 20:28:45 +0000990 false,
991 GlobalValue::ExternalLinkage);
Chris Lattner2079fde2001-10-13 06:41:08 +0000992 // Keep track of the fact that we have a forward ref to recycle it
993 CurModule.GlobalRefs.insert(make_pair(make_pair(PT, $2), GV));
994
995 // Must temporarily push this value into the module table...
996 CurModule.CurrentModule->getGlobalList().push_back(GV);
997 V = GV;
998 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000999 }
1000
Chris Lattner2079fde2001-10-13 06:41:08 +00001001 GlobalValue *GV = cast<GlobalValue>(V);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001002 $$ = ConstantPointerRef::get(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001003 delete $1; // Free the type handle
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001004 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001005 | Types ConstExpr {
1006 if ($1->get() != $2->getType())
1007 ThrowException("Mismatched types for constant expression!");
1008 $$ = $2;
1009 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001010 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001011
Chris Lattnere43f40b2002-10-09 00:25:32 +00001012ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001013 if (!ConstantSInt::isValueValidForType($1, $2))
1014 ThrowException("Constant value doesn't fit in type!");
1015 $$ = ConstantSInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001016 }
1017 | UIntType EUINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001018 if (!ConstantUInt::isValueValidForType($1, $2))
1019 ThrowException("Constant value doesn't fit in type!");
1020 $$ = ConstantUInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001021 }
1022 | BOOL TRUE { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001023 $$ = ConstantBool::True;
1024 }
Chris Lattnere43f40b2002-10-09 00:25:32 +00001025 | BOOL FALSE { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001026 $$ = ConstantBool::False;
1027 }
1028 | FPType FPVAL { // Float & Double constants
1029 $$ = ConstantFP::get($1, $2);
1030 };
1031
Chris Lattner00950542001-06-06 20:29:01 +00001032
Chris Lattnerd78700d2002-08-16 21:14:40 +00001033ConstExpr: CAST '(' ConstVal TO Types ')' {
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001034 $$ = ConstantExpr::getCast($3, $5->get());
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001035 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001036 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001037 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1038 if (!isa<PointerType>($3->getType()))
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001039 ThrowException("GetElementPtr requires a pointer operand!");
1040
1041 const Type *IdxTy =
Chris Lattnerd78700d2002-08-16 21:14:40 +00001042 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001043 if (!IdxTy)
1044 ThrowException("Index list invalid for constant getelementptr!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001045
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001046 vector<Constant*> IdxVec;
Chris Lattnerd78700d2002-08-16 21:14:40 +00001047 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1048 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001049 IdxVec.push_back(C);
1050 else
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001051 ThrowException("Indices to constant getelementptr must be constants!");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001052
Chris Lattnerd78700d2002-08-16 21:14:40 +00001053 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001054
Chris Lattnerd78700d2002-08-16 21:14:40 +00001055 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001056 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001057 | BinaryOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001058 if ($3->getType() != $5->getType())
1059 ThrowException("Binary operator types must match!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001060 $$ = ConstantExpr::get($1, $3, $5);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001061 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001062 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001063 if ($5->getType() != Type::UByteTy)
1064 ThrowException("Shift count for shift constant must be unsigned byte!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001065 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattner699f1eb2002-08-14 17:12:33 +00001066 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001067
1068
Chris Lattnere98dda62001-07-14 06:10:16 +00001069// ConstVector - A list of comma seperated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001070ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001071 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001072 }
1073 | ConstVal {
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001074 $$ = new vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001075 $$->push_back($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001076 };
Chris Lattner00950542001-06-06 20:29:01 +00001077
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001078
Chris Lattner1781aca2001-09-18 04:00:54 +00001079// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001080GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001081
Chris Lattner00950542001-06-06 20:29:01 +00001082
Chris Lattner0e73ce62002-05-02 19:11:13 +00001083//===----------------------------------------------------------------------===//
1084// Rules to match Modules
1085//===----------------------------------------------------------------------===//
1086
1087// Module rule: Capture the result of parsing the whole file into a result
1088// variable...
1089//
1090Module : FunctionList {
1091 $$ = ParserResult = $1;
1092 CurModule.ModuleDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001093};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001094
Chris Lattner7e708292002-06-25 16:13:24 +00001095// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001096//
1097FunctionList : FunctionList Function {
1098 $$ = $1;
1099 assert($2->getParent() == 0 && "Function already in module!");
1100 $1->getFunctionList().push_back($2);
1101 CurMeth.FunctionDone();
1102 }
1103 | FunctionList FunctionProto {
1104 $$ = $1;
1105 }
1106 | FunctionList IMPLEMENTATION {
1107 $$ = $1;
1108 }
1109 | ConstPool {
1110 $$ = CurModule.CurrentModule;
1111 // Resolve circular types before we parse the body of the module
1112 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001113 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001114
Chris Lattnere98dda62001-07-14 06:10:16 +00001115// ConstPool - Constants with optional names assigned to them.
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001116ConstPool : ConstPool OptAssign CONST ConstVal {
Chris Lattneradf99702003-03-03 23:28:55 +00001117 if (!setValueName($4, $2))
1118 InsertValue($4);
Chris Lattner00950542001-06-06 20:29:01 +00001119 }
Chris Lattner30c89792001-09-07 16:35:17 +00001120 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner4a42e902001-10-22 05:56:09 +00001121 // Eagerly resolve types. This is not an optimization, this is a
1122 // requirement that is due to the fact that we could have this:
1123 //
1124 // %list = type { %list * }
1125 // %list = type { %list * } ; repeated type decl
1126 //
1127 // If types are not resolved eagerly, then the two types will not be
1128 // determined to be the same type!
1129 //
1130 ResolveTypeTo($2, $4->get());
1131
Chris Lattner1781aca2001-09-18 04:00:54 +00001132 // TODO: FIXME when Type are not const
Chris Lattnerb7474512001-10-03 15:39:04 +00001133 if (!setValueName(const_cast<Type*>($4->get()), $2)) {
1134 // If this is not a redefinition of a type...
1135 if (!$2) {
1136 InsertType($4->get(),
Chris Lattner79df7c02002-03-26 18:01:55 +00001137 inFunctionScope() ? CurMeth.Types : CurModule.Types);
Chris Lattnerb7474512001-10-03 15:39:04 +00001138 }
Chris Lattner30c89792001-09-07 16:35:17 +00001139 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001140
1141 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001142 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001143 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001144 }
Chris Lattner4ad02e72003-04-16 20:28:45 +00001145 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
Chris Lattnerdda71962001-11-26 18:54:16 +00001146 const Type *Ty = $5->getType();
Chris Lattner1781aca2001-09-18 04:00:54 +00001147 // Global declarations appear in Constant Pool
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001148 Constant *Initializer = $5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001149 if (Initializer == 0)
1150 ThrowException("Global value initializer is not a constant!");
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001151
Chris Lattnerdda71962001-11-26 18:54:16 +00001152 GlobalVariable *GV = new GlobalVariable(Ty, $4, $3, Initializer);
Chris Lattnerb7474512001-10-03 15:39:04 +00001153 if (!setValueName(GV, $2)) { // If not redefining...
1154 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001155 int Slot = InsertValue(GV, CurModule.Values);
1156
1157 if (Slot != -1) {
1158 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1159 } else {
1160 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1161 (char*)GV->getName().c_str()));
1162 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001163 }
Chris Lattner1781aca2001-09-18 04:00:54 +00001164 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001165 | ConstPool OptAssign EXTERNAL GlobalType Types {
1166 const Type *Ty = *$5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001167 // Global declarations appear in Constant Pool
Chris Lattner4ad02e72003-04-16 20:28:45 +00001168 GlobalVariable *GV = new GlobalVariable(Ty,$4,GlobalValue::ExternalLinkage);
Chris Lattnerb7474512001-10-03 15:39:04 +00001169 if (!setValueName(GV, $2)) { // If not redefining...
1170 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001171 int Slot = InsertValue(GV, CurModule.Values);
1172
1173 if (Slot != -1) {
1174 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1175 } else {
1176 assert(GV->hasName() && "Not named and not numbered!?");
1177 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1178 (char*)GV->getName().c_str()));
1179 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001180 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001181 delete $5;
Chris Lattnere98dda62001-07-14 06:10:16 +00001182 }
Chris Lattner00950542001-06-06 20:29:01 +00001183 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001184 };
Chris Lattner00950542001-06-06 20:29:01 +00001185
1186
1187//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001188// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001189//===----------------------------------------------------------------------===//
1190
Chris Lattner51727be2002-06-04 21:58:56 +00001191OptVAR_ID : VAR_ID | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001192
1193ArgVal : Types OptVAR_ID {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001194 if (*$1 == Type::VoidTy)
1195 ThrowException("void typed arguments are invalid!");
1196 $$ = new pair<PATypeHolder*, char*>($1, $2);
Chris Lattner51727be2002-06-04 21:58:56 +00001197};
Chris Lattner00950542001-06-06 20:29:01 +00001198
Chris Lattner69da5cf2002-10-13 20:57:00 +00001199ArgListH : ArgListH ',' ArgVal {
1200 $$ = $1;
1201 $1->push_back(*$3);
1202 delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001203 }
1204 | ArgVal {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001205 $$ = new vector<pair<PATypeHolder*,char*> >();
1206 $$->push_back(*$1);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001207 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001208 };
Chris Lattner00950542001-06-06 20:29:01 +00001209
1210ArgList : ArgListH {
1211 $$ = $1;
1212 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001213 | ArgListH ',' DOTDOTDOT {
1214 $$ = $1;
1215 $$->push_back(pair<PATypeHolder*, char*>(new PATypeHolder(Type::VoidTy),0));
1216 }
1217 | DOTDOTDOT {
1218 $$ = new vector<pair<PATypeHolder*,char*> >();
1219 $$->push_back(pair<PATypeHolder*, char*>(new PATypeHolder(Type::VoidTy),0));
1220 }
Chris Lattner00950542001-06-06 20:29:01 +00001221 | /* empty */ {
1222 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001223 };
Chris Lattner00950542001-06-06 20:29:01 +00001224
Chris Lattner8ebccb72002-05-22 22:33:00 +00001225FuncName : VAR_ID | STRINGCONSTANT;
1226
Chris Lattner1f862af2003-04-16 18:13:57 +00001227FunctionHeaderH : TypesV FuncName '(' ArgList ')' {
1228 UnEscapeLexed($2);
1229 string FunctionName($2);
Chris Lattnerdda71962001-11-26 18:54:16 +00001230
Chris Lattner30c89792001-09-07 16:35:17 +00001231 vector<const Type*> ParamTypeList;
Chris Lattner1f862af2003-04-16 18:13:57 +00001232 if ($4) { // If there are arguments...
1233 for (vector<pair<PATypeHolder*,char*> >::iterator I = $4->begin();
1234 I != $4->end(); ++I)
Chris Lattner69da5cf2002-10-13 20:57:00 +00001235 ParamTypeList.push_back(I->first->get());
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001236 }
Chris Lattner00950542001-06-06 20:29:01 +00001237
Chris Lattner2079fde2001-10-13 06:41:08 +00001238 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1239 if (isVarArg) ParamTypeList.pop_back();
1240
Chris Lattner1f862af2003-04-16 18:13:57 +00001241 const FunctionType *FT = FunctionType::get(*$1, ParamTypeList, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001242 const PointerType *PFT = PointerType::get(FT);
Chris Lattner1f862af2003-04-16 18:13:57 +00001243 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001244
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001245 Function *Fn = 0;
1246 // Is the function already in symtab?
1247 if ((Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1248 // Yes it is. If this is the case, either we need to be a forward decl,
1249 // or it needs to be.
1250 if (!CurMeth.isDeclare && !Fn->isExternal())
1251 ThrowException("Redefinition of function '" + FunctionName + "'!");
1252
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001253 // If we found a preexisting function prototype, remove it from the
1254 // module, so that we don't get spurious conflicts with global & local
1255 // variables.
1256 //
1257 CurModule.CurrentModule->getFunctionList().remove(Fn);
Chris Lattner34538142002-03-08 19:11:42 +00001258
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001259 // Make sure to strip off any argument names so we can't get conflicts...
1260 for (Function::aiterator AI = Fn->abegin(), AE = Fn->aend(); AI != AE; ++AI)
1261 AI->setName("");
Chris Lattner5659dd12002-07-15 00:10:33 +00001262
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001263 } else { // Not already defined?
Chris Lattner4ad02e72003-04-16 20:28:45 +00001264 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001265 InsertValue(Fn, CurModule.Values);
Chris Lattner1f862af2003-04-16 18:13:57 +00001266 CurModule.DeclareNewGlobalValue(Fn, ValID::create($2));
Chris Lattnere1815642001-07-15 06:35:53 +00001267 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001268 free($2); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +00001269
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001270 CurMeth.FunctionStart(Fn);
Chris Lattner00950542001-06-06 20:29:01 +00001271
Chris Lattner7e708292002-06-25 16:13:24 +00001272 // Add all of the arguments we parsed to the function...
Chris Lattner1f862af2003-04-16 18:13:57 +00001273 if ($4) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001274 if (isVarArg) { // Nuke the last entry
Chris Lattner1f862af2003-04-16 18:13:57 +00001275 assert($4->back().first->get() == Type::VoidTy && $4->back().second == 0&&
Chris Lattner69da5cf2002-10-13 20:57:00 +00001276 "Not a varargs marker!");
Chris Lattner1f862af2003-04-16 18:13:57 +00001277 delete $4->back().first;
1278 $4->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00001279 }
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001280 Function::aiterator ArgIt = Fn->abegin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001281 for (vector<pair<PATypeHolder*, char*> >::iterator I = $4->begin();
1282 I != $4->end(); ++I, ++ArgIt) {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001283 delete I->first; // Delete the typeholder...
1284
1285 if (setValueName(ArgIt, I->second)) // Insert arg into symtab...
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001286 assert(0 && "No arg redef allowed!");
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001287
Chris Lattner69da5cf2002-10-13 20:57:00 +00001288 InsertValue(ArgIt);
Chris Lattner00950542001-06-06 20:29:01 +00001289 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001290
Chris Lattner1f862af2003-04-16 18:13:57 +00001291 delete $4; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00001292 }
Chris Lattner51727be2002-06-04 21:58:56 +00001293};
Chris Lattner00950542001-06-06 20:29:01 +00001294
Chris Lattner9b02cc32002-05-03 18:23:48 +00001295BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1296
Chris Lattner4ad02e72003-04-16 20:28:45 +00001297FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
Chris Lattner79df7c02002-03-26 18:01:55 +00001298 $$ = CurMeth.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001299
Chris Lattner4ad02e72003-04-16 20:28:45 +00001300 // Make sure that we keep track of the linkage type even if there was a
1301 // previous "declare".
1302 $$->setLinkage($1);
Chris Lattner1f862af2003-04-16 18:13:57 +00001303
Chris Lattner7e708292002-06-25 16:13:24 +00001304 // Resolve circular types before we parse the body of the function.
Chris Lattner30c89792001-09-07 16:35:17 +00001305 ResolveTypes(CurMeth.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001306};
Chris Lattner00950542001-06-06 20:29:01 +00001307
Chris Lattner9b02cc32002-05-03 18:23:48 +00001308END : ENDTOK | '}'; // Allow end of '}' to end a function
1309
Chris Lattner79df7c02002-03-26 18:01:55 +00001310Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001311 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001312};
Chris Lattner00950542001-06-06 20:29:01 +00001313
Chris Lattner79df7c02002-03-26 18:01:55 +00001314FunctionProto : DECLARE { CurMeth.isDeclare = true; } FunctionHeaderH {
1315 $$ = CurMeth.CurrentFunction;
1316 assert($$->getParent() == 0 && "Function already in module!");
1317 CurModule.CurrentModule->getFunctionList().push_back($$);
1318 CurMeth.FunctionDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001319};
Chris Lattner00950542001-06-06 20:29:01 +00001320
1321//===----------------------------------------------------------------------===//
1322// Rules to match Basic Blocks
1323//===----------------------------------------------------------------------===//
1324
1325ConstValueRef : ESINT64VAL { // A reference to a direct constant
1326 $$ = ValID::create($1);
1327 }
1328 | EUINT64VAL {
1329 $$ = ValID::create($1);
1330 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001331 | FPVAL { // Perhaps it's an FP constant?
1332 $$ = ValID::create($1);
1333 }
Chris Lattner00950542001-06-06 20:29:01 +00001334 | TRUE {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001335 $$ = ValID::create(ConstantBool::True);
Chris Lattner00950542001-06-06 20:29:01 +00001336 }
1337 | FALSE {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001338 $$ = ValID::create(ConstantBool::False);
Chris Lattner00950542001-06-06 20:29:01 +00001339 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001340 | NULL_TOK {
1341 $$ = ValID::createNull();
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001342 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001343 | ConstExpr {
1344 $$ = ValID::create($1);
1345 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00001346
Chris Lattner2079fde2001-10-13 06:41:08 +00001347// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1348// another value.
1349//
1350SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001351 $$ = ValID::create($1);
1352 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001353 | VAR_ID { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001354 $$ = ValID::create($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001355 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001356
1357// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00001358ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00001359
Chris Lattner00950542001-06-06 20:29:01 +00001360
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001361// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1362// type immediately preceeds the value reference, and allows complex constant
1363// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001364ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001365 $$ = getVal(*$1, $2); delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001366 };
Chris Lattner8b81bf52001-07-25 22:47:46 +00001367
Chris Lattner00950542001-06-06 20:29:01 +00001368BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner7e708292002-06-25 16:13:24 +00001369 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner00950542001-06-06 20:29:01 +00001370 }
Chris Lattner7e708292002-06-25 16:13:24 +00001371 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
1372 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner51727be2002-06-04 21:58:56 +00001373 };
Chris Lattner00950542001-06-06 20:29:01 +00001374
1375
1376// Basic blocks are terminated by branching instructions:
1377// br, br/cc, switch, ret
1378//
Chris Lattner2079fde2001-10-13 06:41:08 +00001379BasicBlock : InstructionList OptAssign BBTerminatorInst {
1380 if (setValueName($3, $2)) { assert(0 && "No redefn allowed!"); }
1381 InsertValue($3);
1382
1383 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001384 InsertValue($1);
1385 $$ = $1;
1386 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001387 | LABELSTR InstructionList OptAssign BBTerminatorInst {
1388 if (setValueName($4, $3)) { assert(0 && "No redefn allowed!"); }
1389 InsertValue($4);
1390
1391 $2->getInstList().push_back($4);
Chris Lattnerb7474512001-10-03 15:39:04 +00001392 if (setValueName($2, $1)) { assert(0 && "No label redef allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001393
1394 InsertValue($2);
1395 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001396 };
Chris Lattner00950542001-06-06 20:29:01 +00001397
1398InstructionList : InstructionList Inst {
1399 $1->getInstList().push_back($2);
1400 $$ = $1;
1401 }
1402 | /* empty */ {
Chris Lattner0383cc42002-08-21 23:51:21 +00001403 $$ = CurBB = new BasicBlock();
Chris Lattner51727be2002-06-04 21:58:56 +00001404 };
Chris Lattner00950542001-06-06 20:29:01 +00001405
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001406BBTerminatorInst : RET ResolvedVal { // Return with a result...
1407 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001408 }
1409 | RET VOID { // Return with no result...
1410 $$ = new ReturnInst();
1411 }
1412 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner9636a912001-10-01 16:18:37 +00001413 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $3)));
Chris Lattner00950542001-06-06 20:29:01 +00001414 } // Conditional Branch...
1415 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner9636a912001-10-01 16:18:37 +00001416 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $6)),
1417 cast<BasicBlock>(getVal(Type::LabelTy, $9)),
Chris Lattner00950542001-06-06 20:29:01 +00001418 getVal(Type::BoolTy, $3));
1419 }
1420 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1421 SwitchInst *S = new SwitchInst(getVal($2, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001422 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
Chris Lattner00950542001-06-06 20:29:01 +00001423 $$ = S;
1424
Chris Lattner46748042002-04-09 19:41:42 +00001425 vector<pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
1426 E = $8->end();
1427 for (; I != E; ++I)
Chris Lattner00950542001-06-06 20:29:01 +00001428 S->dest_push_back(I->first, I->second);
1429 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001430 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO ResolvedVal
1431 EXCEPT ResolvedVal {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001432 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001433 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00001434
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001435 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1436 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00001437 // Pull out the types of all of the arguments...
1438 vector<const Type*> ParamTypes;
1439 if ($5) {
Chris Lattner6cdb0112001-11-26 16:54:11 +00001440 for (vector<Value*>::iterator I = $5->begin(), E = $5->end(); I!=E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00001441 ParamTypes.push_back((*I)->getType());
1442 }
1443
1444 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1445 if (isVarArg) ParamTypes.pop_back();
1446
Chris Lattner79df7c02002-03-26 18:01:55 +00001447 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001448 PFTy = PointerType::get(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00001449 }
1450 delete $2;
1451
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001452 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner2079fde2001-10-13 06:41:08 +00001453
1454 BasicBlock *Normal = dyn_cast<BasicBlock>($8);
1455 BasicBlock *Except = dyn_cast<BasicBlock>($10);
1456
1457 if (Normal == 0 || Except == 0)
1458 ThrowException("Invoke instruction without label destinations!");
1459
1460 // Create the call node...
1461 if (!$5) { // Has no arguments?
Chris Lattner386a3b72001-10-16 19:54:17 +00001462 $$ = new InvokeInst(V, Normal, Except, vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001463 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001464 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00001465 // correctly!
1466 //
Chris Lattner79df7c02002-03-26 18:01:55 +00001467 FunctionType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1468 FunctionType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
Chris Lattner6cdb0112001-11-26 16:54:11 +00001469 vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00001470
1471 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1472 if ((*ArgI)->getType() != *I)
1473 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001474 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001475
1476 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1477 ThrowException("Invalid number of parameters detected!");
1478
Chris Lattner6cdb0112001-11-26 16:54:11 +00001479 $$ = new InvokeInst(V, Normal, Except, *$5);
Chris Lattner2079fde2001-10-13 06:41:08 +00001480 }
1481 delete $5;
Chris Lattner51727be2002-06-04 21:58:56 +00001482 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001483
1484
Chris Lattner00950542001-06-06 20:29:01 +00001485
1486JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1487 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001488 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001489 if (V == 0)
1490 ThrowException("May only switch on a constant pool value!");
1491
Chris Lattner9636a912001-10-01 16:18:37 +00001492 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($5, $6))));
Chris Lattner00950542001-06-06 20:29:01 +00001493 }
1494 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner46748042002-04-09 19:41:42 +00001495 $$ = new vector<pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001496 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00001497
1498 if (V == 0)
1499 ThrowException("May only switch on a constant pool value!");
1500
Chris Lattner9636a912001-10-01 16:18:37 +00001501 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($4, $5))));
Chris Lattner51727be2002-06-04 21:58:56 +00001502 };
Chris Lattner00950542001-06-06 20:29:01 +00001503
1504Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001505 // Is this definition named?? if so, assign the name...
1506 if (setValueName($2, $1)) { assert(0 && "No redefin allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001507 InsertValue($2);
1508 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001509};
Chris Lattner00950542001-06-06 20:29:01 +00001510
Chris Lattnerc24d2082001-06-11 15:04:20 +00001511PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
1512 $$ = new list<pair<Value*, BasicBlock*> >();
Chris Lattner30c89792001-09-07 16:35:17 +00001513 $$->push_back(make_pair(getVal(*$1, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001514 cast<BasicBlock>(getVal(Type::LabelTy, $5))));
Chris Lattner30c89792001-09-07 16:35:17 +00001515 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001516 }
1517 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1518 $$ = $1;
1519 $1->push_back(make_pair(getVal($1->front().first->getType(), $4),
Chris Lattner9636a912001-10-01 16:18:37 +00001520 cast<BasicBlock>(getVal(Type::LabelTy, $6))));
Chris Lattner51727be2002-06-04 21:58:56 +00001521 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00001522
1523
Chris Lattner30c89792001-09-07 16:35:17 +00001524ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner6cdb0112001-11-26 16:54:11 +00001525 $$ = new vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001526 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001527 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001528 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001529 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001530 $1->push_back($3);
Chris Lattner51727be2002-06-04 21:58:56 +00001531 };
Chris Lattner00950542001-06-06 20:29:01 +00001532
1533// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner51727be2002-06-04 21:58:56 +00001534ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001535
Chris Lattner4a6482b2002-09-10 19:57:26 +00001536InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
1537 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint())
1538 ThrowException("Arithmetic operator requires integer or FP operands!");
1539 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1540 if ($$ == 0)
1541 ThrowException("binary operator returned null!");
1542 delete $2;
1543 }
1544 | LogicalOps Types ValueRef ',' ValueRef {
1545 if (!(*$2)->isIntegral())
1546 ThrowException("Logical operator requires integral operands!");
1547 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1548 if ($$ == 0)
1549 ThrowException("binary operator returned null!");
1550 delete $2;
1551 }
1552 | SetCondOps Types ValueRef ',' ValueRef {
Chris Lattner1cff96a2002-09-10 22:37:46 +00001553 $$ = new SetCondInst($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001554 if ($$ == 0)
1555 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001556 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001557 }
Chris Lattner699f1eb2002-08-14 17:12:33 +00001558 | NOT ResolvedVal {
1559 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
1560 << " Replacing with 'xor'.\n";
1561
1562 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
1563 if (Ones == 0)
1564 ThrowException("Expected integral type for not instruction!");
1565
1566 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner00950542001-06-06 20:29:01 +00001567 if ($$ == 0)
Chris Lattner699f1eb2002-08-14 17:12:33 +00001568 ThrowException("Could not create a xor instruction!");
Chris Lattner09083092001-07-08 04:57:15 +00001569 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001570 | ShiftOps ResolvedVal ',' ResolvedVal {
1571 if ($4->getType() != Type::UByteTy)
1572 ThrowException("Shift amount must be ubyte!");
1573 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001574 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001575 | CAST ResolvedVal TO Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001576 $$ = new CastInst($2, *$4);
1577 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001578 }
Chris Lattnerc24d2082001-06-11 15:04:20 +00001579 | PHI PHIList {
1580 const Type *Ty = $2->front().first->getType();
1581 $$ = new PHINode(Ty);
Chris Lattner00950542001-06-06 20:29:01 +00001582 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001583 if ($2->front().first->getType() != Ty)
1584 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001585 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001586 $2->pop_front();
1587 }
1588 delete $2; // Free the list...
1589 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001590 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001591 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001592 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001593
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001594 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1595 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001596 // Pull out the types of all of the arguments...
1597 vector<const Type*> ParamTypes;
Chris Lattneref9c23f2001-10-03 14:53:21 +00001598 if ($5) {
Chris Lattner6cdb0112001-11-26 16:54:11 +00001599 for (vector<Value*>::iterator I = $5->begin(), E = $5->end(); I!=E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00001600 ParamTypes.push_back((*I)->getType());
1601 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001602
1603 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1604 if (isVarArg) ParamTypes.pop_back();
1605
Chris Lattner79df7c02002-03-26 18:01:55 +00001606 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001607 PFTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001608 }
Chris Lattner30c89792001-09-07 16:35:17 +00001609 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001610
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001611 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001612
Chris Lattner8b81bf52001-07-25 22:47:46 +00001613 // Create the call node...
1614 if (!$5) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00001615 // Make sure no arguments is a good thing!
1616 if (Ty->getNumParams() != 0)
1617 ThrowException("No arguments passed to a function that "
1618 "expects arguments!");
1619
Chris Lattner386a3b72001-10-16 19:54:17 +00001620 $$ = new CallInst(V, vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001621 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001622 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00001623 // correctly!
1624 //
Chris Lattner79df7c02002-03-26 18:01:55 +00001625 FunctionType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1626 FunctionType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
Chris Lattner6cdb0112001-11-26 16:54:11 +00001627 vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001628
1629 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1630 if ((*ArgI)->getType() != *I)
1631 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001632 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001633
Chris Lattner8b81bf52001-07-25 22:47:46 +00001634 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner00950542001-06-06 20:29:01 +00001635 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00001636
Chris Lattner6cdb0112001-11-26 16:54:11 +00001637 $$ = new CallInst(V, *$5);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001638 }
1639 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00001640 }
1641 | MemoryInst {
1642 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001643 };
Chris Lattner00950542001-06-06 20:29:01 +00001644
Chris Lattner6cdb0112001-11-26 16:54:11 +00001645
1646// IndexList - List of indices for GEP based instructions...
1647IndexList : ',' ValueRefList {
Chris Lattner027dcc52001-07-08 21:10:27 +00001648 $$ = $2;
1649} | /* empty */ {
Chris Lattner6cdb0112001-11-26 16:54:11 +00001650 $$ = new vector<Value*>();
Chris Lattner51727be2002-06-04 21:58:56 +00001651};
Chris Lattner027dcc52001-07-08 21:10:27 +00001652
Chris Lattner00950542001-06-06 20:29:01 +00001653MemoryInst : MALLOC Types {
Chris Lattner05804b72002-09-13 22:28:45 +00001654 $$ = new MallocInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00001655 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001656 }
1657 | MALLOC Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00001658 $$ = new MallocInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001659 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001660 }
1661 | ALLOCA Types {
Chris Lattner05804b72002-09-13 22:28:45 +00001662 $$ = new AllocaInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00001663 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001664 }
1665 | ALLOCA Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00001666 $$ = new AllocaInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001667 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001668 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001669 | FREE ResolvedVal {
Chris Lattner9b625032002-05-06 16:15:30 +00001670 if (!isa<PointerType>($2->getType()))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001671 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00001672 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001673 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001674 }
1675
Chris Lattner6cdb0112001-11-26 16:54:11 +00001676 | LOAD Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00001677 if (!isa<PointerType>($2->get()))
Chris Lattner2079fde2001-10-13 06:41:08 +00001678 ThrowException("Can't load from nonpointer type: " +
1679 (*$2)->getDescription());
Chris Lattner5dfe7672002-08-22 22:48:55 +00001680 if (GetElementPtrInst::getIndexedType(*$2, *$4) == 0)
Chris Lattner027dcc52001-07-08 21:10:27 +00001681 ThrowException("Invalid indices for load instruction!");
1682
Chris Lattner0383cc42002-08-21 23:51:21 +00001683 Value *Src = getVal(*$2, $3);
1684 if (!$4->empty()) {
1685 std::cerr << "WARNING: Use of index load instruction:"
1686 << " replacing with getelementptr/load pair.\n";
1687 // Create a getelementptr hack instruction to do the right thing for
1688 // compatibility.
1689 //
1690 Instruction *I = new GetElementPtrInst(Src, *$4);
1691 CurBB->getInstList().push_back(I);
1692 Src = I;
1693 }
1694
1695 $$ = new LoadInst(Src);
Chris Lattner027dcc52001-07-08 21:10:27 +00001696 delete $4; // Free the vector...
Chris Lattner30c89792001-09-07 16:35:17 +00001697 delete $2;
Chris Lattner027dcc52001-07-08 21:10:27 +00001698 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00001699 | STORE ResolvedVal ',' Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00001700 if (!isa<PointerType>($4->get()))
Chris Lattner72e00252001-12-14 16:28:42 +00001701 ThrowException("Can't store to a nonpointer type: " +
1702 (*$4)->getDescription());
Chris Lattner5dfe7672002-08-22 22:48:55 +00001703 const Type *ElTy = GetElementPtrInst::getIndexedType(*$4, *$6);
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001704 if (ElTy == 0)
1705 ThrowException("Can't store into that field list!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001706 if (ElTy != $2->getType())
Chris Lattner72e00252001-12-14 16:28:42 +00001707 ThrowException("Can't store '" + $2->getType()->getDescription() +
1708 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner0383cc42002-08-21 23:51:21 +00001709
1710 Value *Ptr = getVal(*$4, $5);
1711 if (!$6->empty()) {
1712 std::cerr << "WARNING: Use of index store instruction:"
1713 << " replacing with getelementptr/store pair.\n";
1714 // Create a getelementptr hack instruction to do the right thing for
1715 // compatibility.
1716 //
1717 Instruction *I = new GetElementPtrInst(Ptr, *$6);
1718 CurBB->getInstList().push_back(I);
1719 Ptr = I;
1720 }
1721
1722 $$ = new StoreInst($2, Ptr);
Chris Lattner30c89792001-09-07 16:35:17 +00001723 delete $4; delete $6;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001724 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00001725 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner0235fe22002-09-11 01:17:27 +00001726 for (unsigned i = 0, e = $4->size(); i != e; ++i) {
1727 if ((*$4)[i]->getType() == Type::UIntTy) {
1728 std::cerr << "WARNING: Use of uint type indexes to getelementptr "
1729 << "instruction: replacing with casts to long type.\n";
1730 Instruction *I = new CastInst((*$4)[i], Type::LongTy);
1731 CurBB->getInstList().push_back(I);
1732 (*$4)[i] = I;
1733 }
1734 }
1735
Chris Lattner51727be2002-06-04 21:58:56 +00001736 if (!isa<PointerType>($2->get()))
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001737 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner30c89792001-09-07 16:35:17 +00001738 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner72e00252001-12-14 16:28:42 +00001739 ThrowException("Can't get element ptr '" + (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001740 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
1741 delete $2; delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00001742 };
Chris Lattner027dcc52001-07-08 21:10:27 +00001743
Chris Lattner00950542001-06-06 20:29:01 +00001744%%
Chris Lattner09083092001-07-08 04:57:15 +00001745int yyerror(const char *ErrorMsg) {
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001746 string where = string((CurFilename == "-")? string("<stdin>") : CurFilename)
1747 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
1748 string errMsg = string(ErrorMsg) + string("\n") + where + " while reading ";
1749 if (yychar == YYEMPTY)
1750 errMsg += "end-of-file.";
1751 else
1752 errMsg += "token: '" + string(llvmAsmtext, llvmAsmleng) + "'";
1753 ThrowException(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00001754 return 0;
1755}