blob: dcb53eda58b5e8e80140b512ca07b3d45670fc87 [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
Chris Lattner75f20532003-04-22 18:02:52 +0000594 // Allocate a new module to read
595 CurModule.CurrentModule = new Module(Filename);
Chris Lattner00950542001-06-06 20:29:01 +0000596 yyparse(); // Parse the file.
597 Module *Result = ParserResult;
Chris Lattner00950542001-06-06 20:29:01 +0000598 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
599 ParserResult = 0;
600
601 return Result;
602}
603
604%}
605
606%union {
Chris Lattner30c89792001-09-07 16:35:17 +0000607 Module *ModuleVal;
Chris Lattner79df7c02002-03-26 18:01:55 +0000608 Function *FunctionVal;
Chris Lattner69da5cf2002-10-13 20:57:00 +0000609 std::pair<PATypeHolder*, char*> *ArgVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000610 BasicBlock *BasicBlockVal;
611 TerminatorInst *TermInstVal;
612 Instruction *InstVal;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000613 Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000614
Chris Lattner30c89792001-09-07 16:35:17 +0000615 const Type *PrimType;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000616 PATypeHolder *TypeVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000617 Value *ValueVal;
618
Chris Lattner69da5cf2002-10-13 20:57:00 +0000619 std::vector<std::pair<PATypeHolder*,char*> > *ArgList;
Chris Lattner697954c2002-01-20 22:54:45 +0000620 std::vector<Value*> *ValueList;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000621 std::list<PATypeHolder> *TypeList;
Chris Lattner697954c2002-01-20 22:54:45 +0000622 std::list<std::pair<Value*,
623 BasicBlock*> > *PHIList; // Represent the RHS of PHI node
Chris Lattner46748042002-04-09 19:41:42 +0000624 std::vector<std::pair<Constant*, BasicBlock*> > *JumpTable;
Chris Lattner697954c2002-01-20 22:54:45 +0000625 std::vector<Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000626
Chris Lattner4ad02e72003-04-16 20:28:45 +0000627 GlobalValue::LinkageTypes Linkage;
Chris Lattner30c89792001-09-07 16:35:17 +0000628 int64_t SInt64Val;
629 uint64_t UInt64Val;
630 int SIntVal;
631 unsigned UIntVal;
632 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000633 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000634
Chris Lattner30c89792001-09-07 16:35:17 +0000635 char *StrVal; // This memory is strdup'd!
636 ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000637
Chris Lattner30c89792001-09-07 16:35:17 +0000638 Instruction::BinaryOps BinaryOpVal;
639 Instruction::TermOps TermOpVal;
640 Instruction::MemoryOps MemOpVal;
641 Instruction::OtherOps OtherOpVal;
Chris Lattner00950542001-06-06 20:29:01 +0000642}
643
Chris Lattner79df7c02002-03-26 18:01:55 +0000644%type <ModuleVal> Module FunctionList
645%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000646%type <BasicBlockVal> BasicBlock InstructionList
647%type <TermInstVal> BBTerminatorInst
648%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000649%type <ConstVal> ConstVal ConstExpr
Chris Lattner6cdb0112001-11-26 16:54:11 +0000650%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000651%type <ArgList> ArgList ArgListH
652%type <ArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000653%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000654%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000655%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000656%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000657%type <JumpTable> JumpTable
Chris Lattner4ad02e72003-04-16 20:28:45 +0000658%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
659%type <Linkage> OptLinkage
Chris Lattner00950542001-06-06 20:29:01 +0000660
Chris Lattner2079fde2001-10-13 06:41:08 +0000661// ValueRef - Unresolved reference to a definition or BB
662%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000663%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000664// Tokens and types for handling constant integer values
665//
666// ESINT64VAL - A negative number within long long range
667%token <SInt64Val> ESINT64VAL
668
669// EUINT64VAL - A positive number within uns. long long range
670%token <UInt64Val> EUINT64VAL
671%type <SInt64Val> EINT64VAL
672
673%token <SIntVal> SINTVAL // Signed 32 bit ints...
674%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
675%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000676%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000677
678// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000679%type <TypeVal> Types TypesV UpRTypes UpRTypesV
680%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattner30c89792001-09-07 16:35:17 +0000681%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
682%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000683
684%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
Chris Lattner8ebccb72002-05-22 22:33:00 +0000685%type <StrVal> OptVAR_ID OptAssign FuncName
Chris Lattner00950542001-06-06 20:29:01 +0000686
687
Chris Lattner08c0e6a2002-10-06 22:45:09 +0000688%token IMPLEMENTATION TRUE FALSE BEGINTOK ENDTOK DECLARE GLOBAL CONSTANT
Chris Lattner4ad02e72003-04-16 20:28:45 +0000689%token TO EXCEPT DOTDOTDOT NULL_TOK CONST INTERNAL LINKONCE APPENDING
690%token OPAQUE NOT EXTERNAL
Chris Lattner00950542001-06-06 20:29:01 +0000691
692// Basic Block Terminating Operators
693%token <TermOpVal> RET BR SWITCH
694
Chris Lattner00950542001-06-06 20:29:01 +0000695// Binary Operators
696%type <BinaryOpVal> BinaryOps // all the binary operators
Chris Lattner4a6482b2002-09-10 19:57:26 +0000697%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Chris Lattner42c9e772001-10-20 09:32:59 +0000698%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000699%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000700
701// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000702%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000703
Chris Lattner027dcc52001-07-08 21:10:27 +0000704// Other Operators
705%type <OtherOpVal> ShiftOps
Chris Lattner2079fde2001-10-13 06:41:08 +0000706%token <OtherOpVal> PHI CALL INVOKE CAST SHL SHR
Chris Lattner027dcc52001-07-08 21:10:27 +0000707
Chris Lattner00950542001-06-06 20:29:01 +0000708%start Module
709%%
710
711// Handle constant integer size restriction and conversion...
712//
713
Chris Lattner51727be2002-06-04 21:58:56 +0000714INTVAL : SINTVAL;
Chris Lattner00950542001-06-06 20:29:01 +0000715INTVAL : UINTVAL {
716 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
717 ThrowException("Value too large for type!");
718 $$ = (int32_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000719};
Chris Lattner00950542001-06-06 20:29:01 +0000720
721
Chris Lattner51727be2002-06-04 21:58:56 +0000722EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner00950542001-06-06 20:29:01 +0000723EINT64VAL : EUINT64VAL {
724 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
725 ThrowException("Value too large for type!");
726 $$ = (int64_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000727};
Chris Lattner00950542001-06-06 20:29:01 +0000728
Chris Lattner00950542001-06-06 20:29:01 +0000729// Operations that are notably excluded from this list include:
730// RET, BR, & SWITCH because they end basic blocks and are treated specially.
731//
Chris Lattner4a6482b2002-09-10 19:57:26 +0000732ArithmeticOps: ADD | SUB | MUL | DIV | REM;
733LogicalOps : AND | OR | XOR;
734SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
735BinaryOps : ArithmeticOps | LogicalOps | SetCondOps;
736
Chris Lattner51727be2002-06-04 21:58:56 +0000737ShiftOps : SHL | SHR;
Chris Lattner00950542001-06-06 20:29:01 +0000738
Chris Lattnere98dda62001-07-14 06:10:16 +0000739// These are some types that allow classification if we only want a particular
740// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner51727be2002-06-04 21:58:56 +0000741SIntType : LONG | INT | SHORT | SBYTE;
742UIntType : ULONG | UINT | USHORT | UBYTE;
743IntType : SIntType | UIntType;
744FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +0000745
Chris Lattnere98dda62001-07-14 06:10:16 +0000746// OptAssign - Value producing statements have an optional assignment component
Chris Lattner00950542001-06-06 20:29:01 +0000747OptAssign : VAR_ID '=' {
748 $$ = $1;
749 }
750 | /*empty*/ {
751 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +0000752 };
Chris Lattner00950542001-06-06 20:29:01 +0000753
Chris Lattner4ad02e72003-04-16 20:28:45 +0000754OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
755 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
756 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
757 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
Chris Lattner30c89792001-09-07 16:35:17 +0000758
759//===----------------------------------------------------------------------===//
760// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +0000761// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +0000762// access to it, a user must explicitly use TypesV.
763//
764
765// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +0000766TypesV : Types | VOID { $$ = new PATypeHolder($1); };
767UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +0000768
769Types : UpRTypes {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000770 if (UpRefs.size())
771 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
772 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000773 };
Chris Lattner30c89792001-09-07 16:35:17 +0000774
775
776// Derived types are added later...
777//
Chris Lattner51727be2002-06-04 21:58:56 +0000778PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
779PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000780UpRTypes : OPAQUE {
781 $$ = new PATypeHolder(OpaqueType::get());
782 }
783 | PrimType {
784 $$ = new PATypeHolder($1);
Chris Lattner51727be2002-06-04 21:58:56 +0000785 };
Chris Lattnerd78700d2002-08-16 21:14:40 +0000786UpRTypes : SymbolicValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000787 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner51727be2002-06-04 21:58:56 +0000788};
Chris Lattner30c89792001-09-07 16:35:17 +0000789
Chris Lattner30c89792001-09-07 16:35:17 +0000790// Include derived types in the Types production.
791//
792UpRTypes : '\\' EUINT64VAL { // Type UpReference
793 if ($2 > (uint64_t)INT64_MAX) ThrowException("Value out of range!");
794 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
795 UpRefs.push_back(make_pair((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000796 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +0000797 UR_OUT("New Upreference!\n");
798 }
Chris Lattner79df7c02002-03-26 18:01:55 +0000799 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner30c89792001-09-07 16:35:17 +0000800 vector<const Type*> Params;
Chris Lattner697954c2002-01-20 22:54:45 +0000801 mapto($3->begin(), $3->end(), std::back_inserter(Params),
802 std::mem_fun_ref(&PATypeHandle<Type>::get));
Chris Lattner2079fde2001-10-13 06:41:08 +0000803 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
804 if (isVarArg) Params.pop_back();
805
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000806 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +0000807 delete $3; // Delete the argument list
808 delete $1; // Delete the old type handle
809 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000810 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000811 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000812 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +0000813 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000814 | '{' TypeListI '}' { // Structure type?
815 vector<const Type*> Elements;
Chris Lattner697954c2002-01-20 22:54:45 +0000816 mapto($2->begin(), $2->end(), std::back_inserter(Elements),
817 std::mem_fun_ref(&PATypeHandle<Type>::get));
Chris Lattner30c89792001-09-07 16:35:17 +0000818
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000819 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000820 delete $2;
821 }
822 | '{' '}' { // Empty structure type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000823 $$ = new PATypeHolder(StructType::get(vector<const Type*>()));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000824 }
825 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000826 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000827 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000828 };
Chris Lattner30c89792001-09-07 16:35:17 +0000829
Chris Lattner7e708292002-06-25 16:13:24 +0000830// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +0000831// declaration type lists
832//
833TypeListI : UpRTypes {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000834 $$ = new list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +0000835 $$->push_back(*$1); delete $1;
836 }
837 | TypeListI ',' UpRTypes {
838 ($$=$1)->push_back(*$3); delete $3;
Chris Lattner51727be2002-06-04 21:58:56 +0000839 };
Chris Lattner30c89792001-09-07 16:35:17 +0000840
Chris Lattner7e708292002-06-25 16:13:24 +0000841// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +0000842ArgTypeListI : TypeListI
843 | TypeListI ',' DOTDOTDOT {
844 ($$=$1)->push_back(Type::VoidTy);
845 }
846 | DOTDOTDOT {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000847 ($$ = new list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattner30c89792001-09-07 16:35:17 +0000848 }
849 | /*empty*/ {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000850 $$ = new list<PATypeHolder>();
Chris Lattner51727be2002-06-04 21:58:56 +0000851 };
Chris Lattner30c89792001-09-07 16:35:17 +0000852
Chris Lattnere98dda62001-07-14 06:10:16 +0000853// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +0000854// production is used ONLY to represent constants that show up AFTER a 'const',
855// 'constant' or 'global' token at global scope. Constants that can be inlined
856// into other expressions (such as integers and constexprs) are handled by the
857// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +0000858//
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000859ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
860 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
861 if (ATy == 0)
862 ThrowException("Cannot make array constant with type: '" +
863 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +0000864 const Type *ETy = ATy->getElementType();
865 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +0000866
Chris Lattner30c89792001-09-07 16:35:17 +0000867 // Verify that we have the correct size...
868 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +0000869 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattner30c89792001-09-07 16:35:17 +0000870 utostr($3->size()) + " arguments, but has size of " +
871 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +0000872
Chris Lattner30c89792001-09-07 16:35:17 +0000873 // Verify all elements are correct type!
874 for (unsigned i = 0; i < $3->size(); i++) {
875 if (ETy != (*$3)[i]->getType())
Chris Lattner00950542001-06-06 20:29:01 +0000876 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +0000877 ETy->getDescription() +"' as required!\nIt is of type '"+
878 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +0000879 }
880
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000881 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +0000882 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000883 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000884 | Types '[' ']' {
885 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
886 if (ATy == 0)
887 ThrowException("Cannot make array constant with type: '" +
888 (*$1)->getDescription() + "'!");
889
890 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +0000891 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +0000892 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattner30c89792001-09-07 16:35:17 +0000893 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000894 $$ = ConstantArray::get(ATy, vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +0000895 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +0000896 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000897 | Types 'c' STRINGCONSTANT {
898 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
899 if (ATy == 0)
900 ThrowException("Cannot make array constant with type: '" +
901 (*$1)->getDescription() + "'!");
902
Chris Lattner30c89792001-09-07 16:35:17 +0000903 int NumElements = ATy->getNumElements();
904 const Type *ETy = ATy->getElementType();
905 char *EndStr = UnEscapeLexed($3, true);
906 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +0000907 ThrowException("Can't build string constant of size " +
Chris Lattner30c89792001-09-07 16:35:17 +0000908 itostr((int)(EndStr-$3)) +
909 " when array has size " + itostr(NumElements) + "!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000910 vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +0000911 if (ETy == Type::SByteTy) {
912 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000913 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +0000914 } else if (ETy == Type::UByteTy) {
915 for (char *C = $3; C != EndStr; ++C)
Chris Lattnerbae362f2003-01-30 22:24:26 +0000916 Vals.push_back(ConstantUInt::get(ETy, (unsigned char)*C));
Chris Lattner93750fa2001-07-28 17:48:55 +0000917 } else {
Chris Lattner30c89792001-09-07 16:35:17 +0000918 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +0000919 ThrowException("Cannot build string arrays of non byte sized elements!");
920 }
Chris Lattner30c89792001-09-07 16:35:17 +0000921 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000922 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +0000923 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +0000924 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000925 | Types '{' ConstVector '}' {
926 const StructType *STy = dyn_cast<const StructType>($1->get());
927 if (STy == 0)
928 ThrowException("Cannot make struct constant with type: '" +
929 (*$1)->getDescription() + "'!");
Chris Lattneraf76d0e2003-04-15 16:09:31 +0000930
931 // Check to ensure that constants are compatible with the type initializer!
932 for (unsigned i = 0, e = $3->size(); i != e; ++i)
933 if ((*$3)[i]->getType() != STy->getElementTypes()[i])
934 ThrowException("Expected type '" +
935 STy->getElementTypes()[i]->getDescription() +
936 "' for element #" + utostr(i) +
937 " of structure initializer!");
938
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000939 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +0000940 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000941 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000942 | Types NULL_TOK {
943 const PointerType *PTy = dyn_cast<const PointerType>($1->get());
944 if (PTy == 0)
945 ThrowException("Cannot make null pointer constant with type: '" +
946 (*$1)->getDescription() + "'!");
947
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000948 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000949 delete $1;
950 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000951 | Types SymbolicValueRef {
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000952 const PointerType *Ty = dyn_cast<const PointerType>($1->get());
953 if (Ty == 0)
954 ThrowException("Global const reference must be a pointer type!");
955
Chris Lattner3101c252002-08-15 17:58:33 +0000956 // ConstExprs can exist in the body of a function, thus creating
957 // ConstantPointerRefs whenever they refer to a variable. Because we are in
958 // the context of a function, getValNonImprovising will search the functions
959 // symbol table instead of the module symbol table for the global symbol,
960 // which throws things all off. To get around this, we just tell
961 // getValNonImprovising that we are at global scope here.
962 //
963 Function *SavedCurFn = CurMeth.CurrentFunction;
964 CurMeth.CurrentFunction = 0;
965
Chris Lattner2079fde2001-10-13 06:41:08 +0000966 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000967
Chris Lattner3101c252002-08-15 17:58:33 +0000968 CurMeth.CurrentFunction = SavedCurFn;
969
970
Chris Lattner2079fde2001-10-13 06:41:08 +0000971 // If this is an initializer for a constant pointer, which is referencing a
972 // (currently) undefined variable, create a stub now that shall be replaced
973 // in the future with the right type of variable.
974 //
975 if (V == 0) {
976 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
977 const PointerType *PT = cast<PointerType>(Ty);
978
979 // First check to see if the forward references value is already created!
980 PerModuleInfo::GlobalRefsType::iterator I =
981 CurModule.GlobalRefs.find(make_pair(PT, $2));
982
983 if (I != CurModule.GlobalRefs.end()) {
984 V = I->second; // Placeholder already exists, use it...
985 } else {
986 // TODO: Include line number info by creating a subclass of
987 // TODO: GlobalVariable here that includes the said information!
988
989 // Create a placeholder for the global variable reference...
Chris Lattner7a176752001-12-04 00:03:30 +0000990 GlobalVariable *GV = new GlobalVariable(PT->getElementType(),
Chris Lattner4ad02e72003-04-16 20:28:45 +0000991 false,
992 GlobalValue::ExternalLinkage);
Chris Lattner2079fde2001-10-13 06:41:08 +0000993 // Keep track of the fact that we have a forward ref to recycle it
994 CurModule.GlobalRefs.insert(make_pair(make_pair(PT, $2), GV));
995
996 // Must temporarily push this value into the module table...
997 CurModule.CurrentModule->getGlobalList().push_back(GV);
998 V = GV;
999 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001000 }
1001
Chris Lattner2079fde2001-10-13 06:41:08 +00001002 GlobalValue *GV = cast<GlobalValue>(V);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001003 $$ = ConstantPointerRef::get(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001004 delete $1; // Free the type handle
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001005 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001006 | Types ConstExpr {
1007 if ($1->get() != $2->getType())
1008 ThrowException("Mismatched types for constant expression!");
1009 $$ = $2;
1010 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001011 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001012
Chris Lattnere43f40b2002-10-09 00:25:32 +00001013ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001014 if (!ConstantSInt::isValueValidForType($1, $2))
1015 ThrowException("Constant value doesn't fit in type!");
1016 $$ = ConstantSInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001017 }
1018 | UIntType EUINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001019 if (!ConstantUInt::isValueValidForType($1, $2))
1020 ThrowException("Constant value doesn't fit in type!");
1021 $$ = ConstantUInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001022 }
1023 | BOOL TRUE { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001024 $$ = ConstantBool::True;
1025 }
Chris Lattnere43f40b2002-10-09 00:25:32 +00001026 | BOOL FALSE { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001027 $$ = ConstantBool::False;
1028 }
1029 | FPType FPVAL { // Float & Double constants
1030 $$ = ConstantFP::get($1, $2);
1031 };
1032
Chris Lattner00950542001-06-06 20:29:01 +00001033
Chris Lattnerd78700d2002-08-16 21:14:40 +00001034ConstExpr: CAST '(' ConstVal TO Types ')' {
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001035 $$ = ConstantExpr::getCast($3, $5->get());
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001036 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001037 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001038 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1039 if (!isa<PointerType>($3->getType()))
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001040 ThrowException("GetElementPtr requires a pointer operand!");
1041
1042 const Type *IdxTy =
Chris Lattnerd78700d2002-08-16 21:14:40 +00001043 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001044 if (!IdxTy)
1045 ThrowException("Index list invalid for constant getelementptr!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001046
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001047 vector<Constant*> IdxVec;
Chris Lattnerd78700d2002-08-16 21:14:40 +00001048 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1049 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001050 IdxVec.push_back(C);
1051 else
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001052 ThrowException("Indices to constant getelementptr must be constants!");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001053
Chris Lattnerd78700d2002-08-16 21:14:40 +00001054 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001055
Chris Lattnerd78700d2002-08-16 21:14:40 +00001056 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001057 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001058 | BinaryOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001059 if ($3->getType() != $5->getType())
1060 ThrowException("Binary operator types must match!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001061 $$ = ConstantExpr::get($1, $3, $5);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001062 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001063 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001064 if ($5->getType() != Type::UByteTy)
1065 ThrowException("Shift count for shift constant must be unsigned byte!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001066 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattner699f1eb2002-08-14 17:12:33 +00001067 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001068
1069
Chris Lattnere98dda62001-07-14 06:10:16 +00001070// ConstVector - A list of comma seperated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001071ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001072 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001073 }
1074 | ConstVal {
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001075 $$ = new vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001076 $$->push_back($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001077 };
Chris Lattner00950542001-06-06 20:29:01 +00001078
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001079
Chris Lattner1781aca2001-09-18 04:00:54 +00001080// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001081GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001082
Chris Lattner00950542001-06-06 20:29:01 +00001083
Chris Lattner0e73ce62002-05-02 19:11:13 +00001084//===----------------------------------------------------------------------===//
1085// Rules to match Modules
1086//===----------------------------------------------------------------------===//
1087
1088// Module rule: Capture the result of parsing the whole file into a result
1089// variable...
1090//
1091Module : FunctionList {
1092 $$ = ParserResult = $1;
1093 CurModule.ModuleDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001094};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001095
Chris Lattner7e708292002-06-25 16:13:24 +00001096// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001097//
1098FunctionList : FunctionList Function {
1099 $$ = $1;
1100 assert($2->getParent() == 0 && "Function already in module!");
1101 $1->getFunctionList().push_back($2);
1102 CurMeth.FunctionDone();
1103 }
1104 | FunctionList FunctionProto {
1105 $$ = $1;
1106 }
1107 | FunctionList IMPLEMENTATION {
1108 $$ = $1;
1109 }
1110 | ConstPool {
1111 $$ = CurModule.CurrentModule;
1112 // Resolve circular types before we parse the body of the module
1113 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001114 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001115
Chris Lattnere98dda62001-07-14 06:10:16 +00001116// ConstPool - Constants with optional names assigned to them.
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001117ConstPool : ConstPool OptAssign CONST ConstVal {
Chris Lattneradf99702003-03-03 23:28:55 +00001118 if (!setValueName($4, $2))
1119 InsertValue($4);
Chris Lattner00950542001-06-06 20:29:01 +00001120 }
Chris Lattner30c89792001-09-07 16:35:17 +00001121 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner4a42e902001-10-22 05:56:09 +00001122 // Eagerly resolve types. This is not an optimization, this is a
1123 // requirement that is due to the fact that we could have this:
1124 //
1125 // %list = type { %list * }
1126 // %list = type { %list * } ; repeated type decl
1127 //
1128 // If types are not resolved eagerly, then the two types will not be
1129 // determined to be the same type!
1130 //
1131 ResolveTypeTo($2, $4->get());
1132
Chris Lattner1781aca2001-09-18 04:00:54 +00001133 // TODO: FIXME when Type are not const
Chris Lattnerb7474512001-10-03 15:39:04 +00001134 if (!setValueName(const_cast<Type*>($4->get()), $2)) {
1135 // If this is not a redefinition of a type...
1136 if (!$2) {
1137 InsertType($4->get(),
Chris Lattner79df7c02002-03-26 18:01:55 +00001138 inFunctionScope() ? CurMeth.Types : CurModule.Types);
Chris Lattnerb7474512001-10-03 15:39:04 +00001139 }
Chris Lattner30c89792001-09-07 16:35:17 +00001140 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001141
1142 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001143 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001144 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001145 }
Chris Lattner4ad02e72003-04-16 20:28:45 +00001146 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
Chris Lattnerdda71962001-11-26 18:54:16 +00001147 const Type *Ty = $5->getType();
Chris Lattner1781aca2001-09-18 04:00:54 +00001148 // Global declarations appear in Constant Pool
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001149 Constant *Initializer = $5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001150 if (Initializer == 0)
1151 ThrowException("Global value initializer is not a constant!");
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001152
Chris Lattnerdda71962001-11-26 18:54:16 +00001153 GlobalVariable *GV = new GlobalVariable(Ty, $4, $3, Initializer);
Chris Lattnerb7474512001-10-03 15:39:04 +00001154 if (!setValueName(GV, $2)) { // If not redefining...
1155 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001156 int Slot = InsertValue(GV, CurModule.Values);
1157
1158 if (Slot != -1) {
1159 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1160 } else {
1161 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1162 (char*)GV->getName().c_str()));
1163 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001164 }
Chris Lattner1781aca2001-09-18 04:00:54 +00001165 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001166 | ConstPool OptAssign EXTERNAL GlobalType Types {
1167 const Type *Ty = *$5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001168 // Global declarations appear in Constant Pool
Chris Lattner4ad02e72003-04-16 20:28:45 +00001169 GlobalVariable *GV = new GlobalVariable(Ty,$4,GlobalValue::ExternalLinkage);
Chris Lattnerb7474512001-10-03 15:39:04 +00001170 if (!setValueName(GV, $2)) { // If not redefining...
1171 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001172 int Slot = InsertValue(GV, CurModule.Values);
1173
1174 if (Slot != -1) {
1175 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1176 } else {
1177 assert(GV->hasName() && "Not named and not numbered!?");
1178 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1179 (char*)GV->getName().c_str()));
1180 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001181 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001182 delete $5;
Chris Lattnere98dda62001-07-14 06:10:16 +00001183 }
Chris Lattner00950542001-06-06 20:29:01 +00001184 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001185 };
Chris Lattner00950542001-06-06 20:29:01 +00001186
1187
1188//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001189// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001190//===----------------------------------------------------------------------===//
1191
Chris Lattner51727be2002-06-04 21:58:56 +00001192OptVAR_ID : VAR_ID | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001193
1194ArgVal : Types OptVAR_ID {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001195 if (*$1 == Type::VoidTy)
1196 ThrowException("void typed arguments are invalid!");
1197 $$ = new pair<PATypeHolder*, char*>($1, $2);
Chris Lattner51727be2002-06-04 21:58:56 +00001198};
Chris Lattner00950542001-06-06 20:29:01 +00001199
Chris Lattner69da5cf2002-10-13 20:57:00 +00001200ArgListH : ArgListH ',' ArgVal {
1201 $$ = $1;
1202 $1->push_back(*$3);
1203 delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001204 }
1205 | ArgVal {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001206 $$ = new vector<pair<PATypeHolder*,char*> >();
1207 $$->push_back(*$1);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001208 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001209 };
Chris Lattner00950542001-06-06 20:29:01 +00001210
1211ArgList : ArgListH {
1212 $$ = $1;
1213 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001214 | ArgListH ',' DOTDOTDOT {
1215 $$ = $1;
1216 $$->push_back(pair<PATypeHolder*, char*>(new PATypeHolder(Type::VoidTy),0));
1217 }
1218 | DOTDOTDOT {
1219 $$ = new vector<pair<PATypeHolder*,char*> >();
1220 $$->push_back(pair<PATypeHolder*, char*>(new PATypeHolder(Type::VoidTy),0));
1221 }
Chris Lattner00950542001-06-06 20:29:01 +00001222 | /* empty */ {
1223 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001224 };
Chris Lattner00950542001-06-06 20:29:01 +00001225
Chris Lattner8ebccb72002-05-22 22:33:00 +00001226FuncName : VAR_ID | STRINGCONSTANT;
1227
Chris Lattner1f862af2003-04-16 18:13:57 +00001228FunctionHeaderH : TypesV FuncName '(' ArgList ')' {
1229 UnEscapeLexed($2);
1230 string FunctionName($2);
Chris Lattnerdda71962001-11-26 18:54:16 +00001231
Chris Lattner30c89792001-09-07 16:35:17 +00001232 vector<const Type*> ParamTypeList;
Chris Lattner1f862af2003-04-16 18:13:57 +00001233 if ($4) { // If there are arguments...
1234 for (vector<pair<PATypeHolder*,char*> >::iterator I = $4->begin();
1235 I != $4->end(); ++I)
Chris Lattner69da5cf2002-10-13 20:57:00 +00001236 ParamTypeList.push_back(I->first->get());
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001237 }
Chris Lattner00950542001-06-06 20:29:01 +00001238
Chris Lattner2079fde2001-10-13 06:41:08 +00001239 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1240 if (isVarArg) ParamTypeList.pop_back();
1241
Chris Lattner1f862af2003-04-16 18:13:57 +00001242 const FunctionType *FT = FunctionType::get(*$1, ParamTypeList, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001243 const PointerType *PFT = PointerType::get(FT);
Chris Lattner1f862af2003-04-16 18:13:57 +00001244 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001245
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001246 Function *Fn = 0;
1247 // Is the function already in symtab?
1248 if ((Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1249 // Yes it is. If this is the case, either we need to be a forward decl,
1250 // or it needs to be.
1251 if (!CurMeth.isDeclare && !Fn->isExternal())
1252 ThrowException("Redefinition of function '" + FunctionName + "'!");
1253
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001254 // If we found a preexisting function prototype, remove it from the
1255 // module, so that we don't get spurious conflicts with global & local
1256 // variables.
1257 //
1258 CurModule.CurrentModule->getFunctionList().remove(Fn);
Chris Lattner34538142002-03-08 19:11:42 +00001259
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001260 // Make sure to strip off any argument names so we can't get conflicts...
1261 for (Function::aiterator AI = Fn->abegin(), AE = Fn->aend(); AI != AE; ++AI)
1262 AI->setName("");
Chris Lattner5659dd12002-07-15 00:10:33 +00001263
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001264 } else { // Not already defined?
Chris Lattner4ad02e72003-04-16 20:28:45 +00001265 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001266 InsertValue(Fn, CurModule.Values);
Chris Lattner1f862af2003-04-16 18:13:57 +00001267 CurModule.DeclareNewGlobalValue(Fn, ValID::create($2));
Chris Lattnere1815642001-07-15 06:35:53 +00001268 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001269 free($2); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +00001270
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001271 CurMeth.FunctionStart(Fn);
Chris Lattner00950542001-06-06 20:29:01 +00001272
Chris Lattner7e708292002-06-25 16:13:24 +00001273 // Add all of the arguments we parsed to the function...
Chris Lattner1f862af2003-04-16 18:13:57 +00001274 if ($4) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001275 if (isVarArg) { // Nuke the last entry
Chris Lattner1f862af2003-04-16 18:13:57 +00001276 assert($4->back().first->get() == Type::VoidTy && $4->back().second == 0&&
Chris Lattner69da5cf2002-10-13 20:57:00 +00001277 "Not a varargs marker!");
Chris Lattner1f862af2003-04-16 18:13:57 +00001278 delete $4->back().first;
1279 $4->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00001280 }
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001281 Function::aiterator ArgIt = Fn->abegin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001282 for (vector<pair<PATypeHolder*, char*> >::iterator I = $4->begin();
1283 I != $4->end(); ++I, ++ArgIt) {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001284 delete I->first; // Delete the typeholder...
1285
1286 if (setValueName(ArgIt, I->second)) // Insert arg into symtab...
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001287 assert(0 && "No arg redef allowed!");
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001288
Chris Lattner69da5cf2002-10-13 20:57:00 +00001289 InsertValue(ArgIt);
Chris Lattner00950542001-06-06 20:29:01 +00001290 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001291
Chris Lattner1f862af2003-04-16 18:13:57 +00001292 delete $4; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00001293 }
Chris Lattner51727be2002-06-04 21:58:56 +00001294};
Chris Lattner00950542001-06-06 20:29:01 +00001295
Chris Lattner9b02cc32002-05-03 18:23:48 +00001296BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1297
Chris Lattner4ad02e72003-04-16 20:28:45 +00001298FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
Chris Lattner79df7c02002-03-26 18:01:55 +00001299 $$ = CurMeth.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001300
Chris Lattner4ad02e72003-04-16 20:28:45 +00001301 // Make sure that we keep track of the linkage type even if there was a
1302 // previous "declare".
1303 $$->setLinkage($1);
Chris Lattner1f862af2003-04-16 18:13:57 +00001304
Chris Lattner7e708292002-06-25 16:13:24 +00001305 // Resolve circular types before we parse the body of the function.
Chris Lattner30c89792001-09-07 16:35:17 +00001306 ResolveTypes(CurMeth.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001307};
Chris Lattner00950542001-06-06 20:29:01 +00001308
Chris Lattner9b02cc32002-05-03 18:23:48 +00001309END : ENDTOK | '}'; // Allow end of '}' to end a function
1310
Chris Lattner79df7c02002-03-26 18:01:55 +00001311Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001312 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001313};
Chris Lattner00950542001-06-06 20:29:01 +00001314
Chris Lattner79df7c02002-03-26 18:01:55 +00001315FunctionProto : DECLARE { CurMeth.isDeclare = true; } FunctionHeaderH {
1316 $$ = CurMeth.CurrentFunction;
1317 assert($$->getParent() == 0 && "Function already in module!");
1318 CurModule.CurrentModule->getFunctionList().push_back($$);
1319 CurMeth.FunctionDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001320};
Chris Lattner00950542001-06-06 20:29:01 +00001321
1322//===----------------------------------------------------------------------===//
1323// Rules to match Basic Blocks
1324//===----------------------------------------------------------------------===//
1325
1326ConstValueRef : ESINT64VAL { // A reference to a direct constant
1327 $$ = ValID::create($1);
1328 }
1329 | EUINT64VAL {
1330 $$ = ValID::create($1);
1331 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001332 | FPVAL { // Perhaps it's an FP constant?
1333 $$ = ValID::create($1);
1334 }
Chris Lattner00950542001-06-06 20:29:01 +00001335 | TRUE {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001336 $$ = ValID::create(ConstantBool::True);
Chris Lattner00950542001-06-06 20:29:01 +00001337 }
1338 | FALSE {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001339 $$ = ValID::create(ConstantBool::False);
Chris Lattner00950542001-06-06 20:29:01 +00001340 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001341 | NULL_TOK {
1342 $$ = ValID::createNull();
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001343 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001344 | ConstExpr {
1345 $$ = ValID::create($1);
1346 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00001347
Chris Lattner2079fde2001-10-13 06:41:08 +00001348// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1349// another value.
1350//
1351SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001352 $$ = ValID::create($1);
1353 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001354 | VAR_ID { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001355 $$ = ValID::create($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001356 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001357
1358// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00001359ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00001360
Chris Lattner00950542001-06-06 20:29:01 +00001361
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001362// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1363// type immediately preceeds the value reference, and allows complex constant
1364// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001365ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001366 $$ = getVal(*$1, $2); delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001367 };
Chris Lattner8b81bf52001-07-25 22:47:46 +00001368
Chris Lattner00950542001-06-06 20:29:01 +00001369BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner7e708292002-06-25 16:13:24 +00001370 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner00950542001-06-06 20:29:01 +00001371 }
Chris Lattner7e708292002-06-25 16:13:24 +00001372 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
1373 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner51727be2002-06-04 21:58:56 +00001374 };
Chris Lattner00950542001-06-06 20:29:01 +00001375
1376
1377// Basic blocks are terminated by branching instructions:
1378// br, br/cc, switch, ret
1379//
Chris Lattner2079fde2001-10-13 06:41:08 +00001380BasicBlock : InstructionList OptAssign BBTerminatorInst {
1381 if (setValueName($3, $2)) { assert(0 && "No redefn allowed!"); }
1382 InsertValue($3);
1383
1384 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001385 InsertValue($1);
1386 $$ = $1;
1387 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001388 | LABELSTR InstructionList OptAssign BBTerminatorInst {
1389 if (setValueName($4, $3)) { assert(0 && "No redefn allowed!"); }
1390 InsertValue($4);
1391
1392 $2->getInstList().push_back($4);
Chris Lattnerb7474512001-10-03 15:39:04 +00001393 if (setValueName($2, $1)) { assert(0 && "No label redef allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001394
1395 InsertValue($2);
1396 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001397 };
Chris Lattner00950542001-06-06 20:29:01 +00001398
1399InstructionList : InstructionList Inst {
1400 $1->getInstList().push_back($2);
1401 $$ = $1;
1402 }
1403 | /* empty */ {
Chris Lattner0383cc42002-08-21 23:51:21 +00001404 $$ = CurBB = new BasicBlock();
Chris Lattner51727be2002-06-04 21:58:56 +00001405 };
Chris Lattner00950542001-06-06 20:29:01 +00001406
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001407BBTerminatorInst : RET ResolvedVal { // Return with a result...
1408 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001409 }
1410 | RET VOID { // Return with no result...
1411 $$ = new ReturnInst();
1412 }
1413 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner9636a912001-10-01 16:18:37 +00001414 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $3)));
Chris Lattner00950542001-06-06 20:29:01 +00001415 } // Conditional Branch...
1416 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner9636a912001-10-01 16:18:37 +00001417 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $6)),
1418 cast<BasicBlock>(getVal(Type::LabelTy, $9)),
Chris Lattner00950542001-06-06 20:29:01 +00001419 getVal(Type::BoolTy, $3));
1420 }
1421 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1422 SwitchInst *S = new SwitchInst(getVal($2, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001423 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
Chris Lattner00950542001-06-06 20:29:01 +00001424 $$ = S;
1425
Chris Lattner46748042002-04-09 19:41:42 +00001426 vector<pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
1427 E = $8->end();
1428 for (; I != E; ++I)
Chris Lattner00950542001-06-06 20:29:01 +00001429 S->dest_push_back(I->first, I->second);
1430 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001431 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO ResolvedVal
1432 EXCEPT ResolvedVal {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001433 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001434 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00001435
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001436 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1437 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00001438 // Pull out the types of all of the arguments...
1439 vector<const Type*> ParamTypes;
1440 if ($5) {
Chris Lattner6cdb0112001-11-26 16:54:11 +00001441 for (vector<Value*>::iterator I = $5->begin(), E = $5->end(); I!=E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00001442 ParamTypes.push_back((*I)->getType());
1443 }
1444
1445 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1446 if (isVarArg) ParamTypes.pop_back();
1447
Chris Lattner79df7c02002-03-26 18:01:55 +00001448 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001449 PFTy = PointerType::get(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00001450 }
1451 delete $2;
1452
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001453 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner2079fde2001-10-13 06:41:08 +00001454
1455 BasicBlock *Normal = dyn_cast<BasicBlock>($8);
1456 BasicBlock *Except = dyn_cast<BasicBlock>($10);
1457
1458 if (Normal == 0 || Except == 0)
1459 ThrowException("Invoke instruction without label destinations!");
1460
1461 // Create the call node...
1462 if (!$5) { // Has no arguments?
Chris Lattner386a3b72001-10-16 19:54:17 +00001463 $$ = new InvokeInst(V, Normal, Except, vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001464 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001465 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00001466 // correctly!
1467 //
Chris Lattner79df7c02002-03-26 18:01:55 +00001468 FunctionType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1469 FunctionType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
Chris Lattner6cdb0112001-11-26 16:54:11 +00001470 vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00001471
1472 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1473 if ((*ArgI)->getType() != *I)
1474 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001475 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001476
1477 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1478 ThrowException("Invalid number of parameters detected!");
1479
Chris Lattner6cdb0112001-11-26 16:54:11 +00001480 $$ = new InvokeInst(V, Normal, Except, *$5);
Chris Lattner2079fde2001-10-13 06:41:08 +00001481 }
1482 delete $5;
Chris Lattner51727be2002-06-04 21:58:56 +00001483 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001484
1485
Chris Lattner00950542001-06-06 20:29:01 +00001486
1487JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1488 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001489 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001490 if (V == 0)
1491 ThrowException("May only switch on a constant pool value!");
1492
Chris Lattner9636a912001-10-01 16:18:37 +00001493 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($5, $6))));
Chris Lattner00950542001-06-06 20:29:01 +00001494 }
1495 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner46748042002-04-09 19:41:42 +00001496 $$ = new vector<pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001497 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00001498
1499 if (V == 0)
1500 ThrowException("May only switch on a constant pool value!");
1501
Chris Lattner9636a912001-10-01 16:18:37 +00001502 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($4, $5))));
Chris Lattner51727be2002-06-04 21:58:56 +00001503 };
Chris Lattner00950542001-06-06 20:29:01 +00001504
1505Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001506 // Is this definition named?? if so, assign the name...
1507 if (setValueName($2, $1)) { assert(0 && "No redefin allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001508 InsertValue($2);
1509 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001510};
Chris Lattner00950542001-06-06 20:29:01 +00001511
Chris Lattnerc24d2082001-06-11 15:04:20 +00001512PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
1513 $$ = new list<pair<Value*, BasicBlock*> >();
Chris Lattner30c89792001-09-07 16:35:17 +00001514 $$->push_back(make_pair(getVal(*$1, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001515 cast<BasicBlock>(getVal(Type::LabelTy, $5))));
Chris Lattner30c89792001-09-07 16:35:17 +00001516 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001517 }
1518 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1519 $$ = $1;
1520 $1->push_back(make_pair(getVal($1->front().first->getType(), $4),
Chris Lattner9636a912001-10-01 16:18:37 +00001521 cast<BasicBlock>(getVal(Type::LabelTy, $6))));
Chris Lattner51727be2002-06-04 21:58:56 +00001522 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00001523
1524
Chris Lattner30c89792001-09-07 16:35:17 +00001525ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner6cdb0112001-11-26 16:54:11 +00001526 $$ = new vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001527 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001528 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001529 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001530 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001531 $1->push_back($3);
Chris Lattner51727be2002-06-04 21:58:56 +00001532 };
Chris Lattner00950542001-06-06 20:29:01 +00001533
1534// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner51727be2002-06-04 21:58:56 +00001535ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001536
Chris Lattner4a6482b2002-09-10 19:57:26 +00001537InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
1538 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint())
1539 ThrowException("Arithmetic operator requires integer or FP operands!");
1540 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1541 if ($$ == 0)
1542 ThrowException("binary operator returned null!");
1543 delete $2;
1544 }
1545 | LogicalOps Types ValueRef ',' ValueRef {
1546 if (!(*$2)->isIntegral())
1547 ThrowException("Logical operator requires integral operands!");
1548 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1549 if ($$ == 0)
1550 ThrowException("binary operator returned null!");
1551 delete $2;
1552 }
1553 | SetCondOps Types ValueRef ',' ValueRef {
Chris Lattner1cff96a2002-09-10 22:37:46 +00001554 $$ = new SetCondInst($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001555 if ($$ == 0)
1556 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001557 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001558 }
Chris Lattner699f1eb2002-08-14 17:12:33 +00001559 | NOT ResolvedVal {
1560 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
1561 << " Replacing with 'xor'.\n";
1562
1563 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
1564 if (Ones == 0)
1565 ThrowException("Expected integral type for not instruction!");
1566
1567 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner00950542001-06-06 20:29:01 +00001568 if ($$ == 0)
Chris Lattner699f1eb2002-08-14 17:12:33 +00001569 ThrowException("Could not create a xor instruction!");
Chris Lattner09083092001-07-08 04:57:15 +00001570 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001571 | ShiftOps ResolvedVal ',' ResolvedVal {
1572 if ($4->getType() != Type::UByteTy)
1573 ThrowException("Shift amount must be ubyte!");
1574 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001575 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001576 | CAST ResolvedVal TO Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001577 $$ = new CastInst($2, *$4);
1578 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001579 }
Chris Lattnerc24d2082001-06-11 15:04:20 +00001580 | PHI PHIList {
1581 const Type *Ty = $2->front().first->getType();
1582 $$ = new PHINode(Ty);
Chris Lattner00950542001-06-06 20:29:01 +00001583 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001584 if ($2->front().first->getType() != Ty)
1585 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001586 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001587 $2->pop_front();
1588 }
1589 delete $2; // Free the list...
1590 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001591 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001592 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001593 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001594
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001595 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1596 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001597 // Pull out the types of all of the arguments...
1598 vector<const Type*> ParamTypes;
Chris Lattneref9c23f2001-10-03 14:53:21 +00001599 if ($5) {
Chris Lattner6cdb0112001-11-26 16:54:11 +00001600 for (vector<Value*>::iterator I = $5->begin(), E = $5->end(); I!=E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00001601 ParamTypes.push_back((*I)->getType());
1602 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001603
1604 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1605 if (isVarArg) ParamTypes.pop_back();
1606
Chris Lattner79df7c02002-03-26 18:01:55 +00001607 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001608 PFTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001609 }
Chris Lattner30c89792001-09-07 16:35:17 +00001610 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001611
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001612 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001613
Chris Lattner8b81bf52001-07-25 22:47:46 +00001614 // Create the call node...
1615 if (!$5) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00001616 // Make sure no arguments is a good thing!
1617 if (Ty->getNumParams() != 0)
1618 ThrowException("No arguments passed to a function that "
1619 "expects arguments!");
1620
Chris Lattner386a3b72001-10-16 19:54:17 +00001621 $$ = new CallInst(V, vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001622 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001623 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00001624 // correctly!
1625 //
Chris Lattner79df7c02002-03-26 18:01:55 +00001626 FunctionType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1627 FunctionType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
Chris Lattner6cdb0112001-11-26 16:54:11 +00001628 vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001629
1630 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1631 if ((*ArgI)->getType() != *I)
1632 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001633 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001634
Chris Lattner8b81bf52001-07-25 22:47:46 +00001635 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner00950542001-06-06 20:29:01 +00001636 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00001637
Chris Lattner6cdb0112001-11-26 16:54:11 +00001638 $$ = new CallInst(V, *$5);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001639 }
1640 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00001641 }
1642 | MemoryInst {
1643 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001644 };
Chris Lattner00950542001-06-06 20:29:01 +00001645
Chris Lattner6cdb0112001-11-26 16:54:11 +00001646
1647// IndexList - List of indices for GEP based instructions...
1648IndexList : ',' ValueRefList {
Chris Lattner027dcc52001-07-08 21:10:27 +00001649 $$ = $2;
1650} | /* empty */ {
Chris Lattner6cdb0112001-11-26 16:54:11 +00001651 $$ = new vector<Value*>();
Chris Lattner51727be2002-06-04 21:58:56 +00001652};
Chris Lattner027dcc52001-07-08 21:10:27 +00001653
Chris Lattner00950542001-06-06 20:29:01 +00001654MemoryInst : MALLOC Types {
Chris Lattner05804b72002-09-13 22:28:45 +00001655 $$ = new MallocInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00001656 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001657 }
1658 | MALLOC Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00001659 $$ = new MallocInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001660 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001661 }
1662 | ALLOCA Types {
Chris Lattner05804b72002-09-13 22:28:45 +00001663 $$ = new AllocaInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00001664 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001665 }
1666 | ALLOCA Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00001667 $$ = new AllocaInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001668 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001669 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001670 | FREE ResolvedVal {
Chris Lattner9b625032002-05-06 16:15:30 +00001671 if (!isa<PointerType>($2->getType()))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001672 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00001673 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001674 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001675 }
1676
Chris Lattner6cdb0112001-11-26 16:54:11 +00001677 | LOAD Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00001678 if (!isa<PointerType>($2->get()))
Chris Lattner2079fde2001-10-13 06:41:08 +00001679 ThrowException("Can't load from nonpointer type: " +
1680 (*$2)->getDescription());
Chris Lattner5dfe7672002-08-22 22:48:55 +00001681 if (GetElementPtrInst::getIndexedType(*$2, *$4) == 0)
Chris Lattner027dcc52001-07-08 21:10:27 +00001682 ThrowException("Invalid indices for load instruction!");
1683
Chris Lattner0383cc42002-08-21 23:51:21 +00001684 Value *Src = getVal(*$2, $3);
1685 if (!$4->empty()) {
1686 std::cerr << "WARNING: Use of index load instruction:"
1687 << " replacing with getelementptr/load pair.\n";
1688 // Create a getelementptr hack instruction to do the right thing for
1689 // compatibility.
1690 //
1691 Instruction *I = new GetElementPtrInst(Src, *$4);
1692 CurBB->getInstList().push_back(I);
1693 Src = I;
1694 }
1695
1696 $$ = new LoadInst(Src);
Chris Lattner027dcc52001-07-08 21:10:27 +00001697 delete $4; // Free the vector...
Chris Lattner30c89792001-09-07 16:35:17 +00001698 delete $2;
Chris Lattner027dcc52001-07-08 21:10:27 +00001699 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00001700 | STORE ResolvedVal ',' Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00001701 if (!isa<PointerType>($4->get()))
Chris Lattner72e00252001-12-14 16:28:42 +00001702 ThrowException("Can't store to a nonpointer type: " +
1703 (*$4)->getDescription());
Chris Lattner5dfe7672002-08-22 22:48:55 +00001704 const Type *ElTy = GetElementPtrInst::getIndexedType(*$4, *$6);
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001705 if (ElTy == 0)
1706 ThrowException("Can't store into that field list!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001707 if (ElTy != $2->getType())
Chris Lattner72e00252001-12-14 16:28:42 +00001708 ThrowException("Can't store '" + $2->getType()->getDescription() +
1709 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner0383cc42002-08-21 23:51:21 +00001710
1711 Value *Ptr = getVal(*$4, $5);
1712 if (!$6->empty()) {
1713 std::cerr << "WARNING: Use of index store instruction:"
1714 << " replacing with getelementptr/store pair.\n";
1715 // Create a getelementptr hack instruction to do the right thing for
1716 // compatibility.
1717 //
1718 Instruction *I = new GetElementPtrInst(Ptr, *$6);
1719 CurBB->getInstList().push_back(I);
1720 Ptr = I;
1721 }
1722
1723 $$ = new StoreInst($2, Ptr);
Chris Lattner30c89792001-09-07 16:35:17 +00001724 delete $4; delete $6;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001725 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00001726 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner0235fe22002-09-11 01:17:27 +00001727 for (unsigned i = 0, e = $4->size(); i != e; ++i) {
1728 if ((*$4)[i]->getType() == Type::UIntTy) {
1729 std::cerr << "WARNING: Use of uint type indexes to getelementptr "
1730 << "instruction: replacing with casts to long type.\n";
1731 Instruction *I = new CastInst((*$4)[i], Type::LongTy);
1732 CurBB->getInstList().push_back(I);
1733 (*$4)[i] = I;
1734 }
1735 }
1736
Chris Lattner51727be2002-06-04 21:58:56 +00001737 if (!isa<PointerType>($2->get()))
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001738 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner30c89792001-09-07 16:35:17 +00001739 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner72e00252001-12-14 16:28:42 +00001740 ThrowException("Can't get element ptr '" + (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001741 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
1742 delete $2; delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00001743 };
Chris Lattner027dcc52001-07-08 21:10:27 +00001744
Chris Lattner00950542001-06-06 20:29:01 +00001745%%
Chris Lattner09083092001-07-08 04:57:15 +00001746int yyerror(const char *ErrorMsg) {
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001747 string where = string((CurFilename == "-")? string("<stdin>") : CurFilename)
1748 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
1749 string errMsg = string(ErrorMsg) + string("\n") + where + " while reading ";
1750 if (yychar == YYEMPTY)
1751 errMsg += "end-of-file.";
1752 else
1753 errMsg += "token: '" + string(llvmAsmtext, llvmAsmleng) + "'";
1754 ThrowException(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00001755 return 0;
1756}