blob: a25aaf805de3abd29f8b843c091288b1b28ea3fb [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 Lattner1f862af2003-04-16 18:13:57 +0000525 if (GV->hasInternalLinkage())
526 EGV->setInternalLinkage(true);
Chris Lattner89219832001-10-03 19:35:04 +0000527
Chris Lattner2079fde2001-10-13 06:41:08 +0000528 delete GV; // Destroy the duplicate!
Chris Lattner89219832001-10-03 19:35:04 +0000529 return true; // They are equivalent!
530 }
Chris Lattnerb7474512001-10-03 15:39:04 +0000531 }
Chris Lattner9636a912001-10-01 16:18:37 +0000532 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000533 ThrowException("Redefinition of value named '" + Name + "' in the '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000534 V->getType()->getDescription() + "' type plane!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000535 }
Chris Lattner00950542001-06-06 20:29:01 +0000536
Chris Lattner6e6026b2002-11-20 18:36:02 +0000537 V->setName(Name, &ST);
Chris Lattnerb7474512001-10-03 15:39:04 +0000538 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000539}
540
Chris Lattner8896eda2001-07-09 19:38:36 +0000541
Chris Lattner30c89792001-09-07 16:35:17 +0000542//===----------------------------------------------------------------------===//
543// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000544//
Chris Lattner8896eda2001-07-09 19:38:36 +0000545
Chris Lattner30c89792001-09-07 16:35:17 +0000546// TypeContains - Returns true if Ty contains E in it.
547//
548static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner3ff43872001-09-28 22:56:31 +0000549 return find(df_begin(Ty), df_end(Ty), E) != df_end(Ty);
Chris Lattner30c89792001-09-07 16:35:17 +0000550}
Chris Lattner698b56e2001-07-20 19:15:08 +0000551
Chris Lattner30c89792001-09-07 16:35:17 +0000552
553static vector<pair<unsigned, OpaqueType *> > UpRefs;
554
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000555static PATypeHolder HandleUpRefs(const Type *ty) {
556 PATypeHolder Ty(ty);
Chris Lattner5084d032001-11-02 07:46:26 +0000557 UR_OUT("Type '" << ty->getDescription() <<
558 "' newly formed. Resolving upreferences.\n" <<
559 UpRefs.size() << " upreferences active!\n");
Chris Lattner30c89792001-09-07 16:35:17 +0000560 for (unsigned i = 0; i < UpRefs.size(); ) {
Chris Lattner5084d032001-11-02 07:46:26 +0000561 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Chris Lattner30c89792001-09-07 16:35:17 +0000562 << UpRefs[i].second->getDescription() << ") = "
Chris Lattner5084d032001-11-02 07:46:26 +0000563 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << endl);
Chris Lattner30c89792001-09-07 16:35:17 +0000564 if (TypeContains(Ty, UpRefs[i].second)) {
565 unsigned Level = --UpRefs[i].first; // Decrement level of upreference
Chris Lattner5084d032001-11-02 07:46:26 +0000566 UR_OUT(" Uplevel Ref Level = " << Level << endl);
Chris Lattner30c89792001-09-07 16:35:17 +0000567 if (Level == 0) { // Upreference should be resolved!
Chris Lattner5084d032001-11-02 07:46:26 +0000568 UR_OUT(" * Resolving upreference for "
569 << UpRefs[i].second->getDescription() << endl;
Chris Lattner30c89792001-09-07 16:35:17 +0000570 string OldName = UpRefs[i].second->getDescription());
571 UpRefs[i].second->refineAbstractTypeTo(Ty);
572 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
Chris Lattner5084d032001-11-02 07:46:26 +0000573 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
Chris Lattner30c89792001-09-07 16:35:17 +0000574 << (const void*)Ty << ", " << Ty->getDescription() << endl);
575 continue;
576 }
577 }
578
579 ++i; // Otherwise, no resolve, move on...
Chris Lattner8896eda2001-07-09 19:38:36 +0000580 }
Chris Lattner30c89792001-09-07 16:35:17 +0000581 // FIXME: TODO: this should return the updated type
Chris Lattner8896eda2001-07-09 19:38:36 +0000582 return Ty;
583}
584
Chris Lattner30c89792001-09-07 16:35:17 +0000585
Chris Lattner00950542001-06-06 20:29:01 +0000586//===----------------------------------------------------------------------===//
587// RunVMAsmParser - Define an interface to this parser
588//===----------------------------------------------------------------------===//
589//
Chris Lattnera2850432001-07-22 18:36:00 +0000590Module *RunVMAsmParser(const string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000591 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000592 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000593 llvmAsmlineno = 1; // Reset the current line number...
594
595 CurModule.CurrentModule = new Module(); // Allocate a new module to read
596 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 Lattner30c89792001-09-07 16:35:17 +0000627 int64_t SInt64Val;
628 uint64_t UInt64Val;
629 int SIntVal;
630 unsigned UIntVal;
631 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000632 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000633
Chris Lattner30c89792001-09-07 16:35:17 +0000634 char *StrVal; // This memory is strdup'd!
635 ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000636
Chris Lattner30c89792001-09-07 16:35:17 +0000637 Instruction::BinaryOps BinaryOpVal;
638 Instruction::TermOps TermOpVal;
639 Instruction::MemoryOps MemOpVal;
640 Instruction::OtherOps OtherOpVal;
Chris Lattner00950542001-06-06 20:29:01 +0000641}
642
Chris Lattner79df7c02002-03-26 18:01:55 +0000643%type <ModuleVal> Module FunctionList
644%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000645%type <BasicBlockVal> BasicBlock InstructionList
646%type <TermInstVal> BBTerminatorInst
647%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000648%type <ConstVal> ConstVal ConstExpr
Chris Lattner6cdb0112001-11-26 16:54:11 +0000649%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000650%type <ArgList> ArgList ArgListH
651%type <ArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000652%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000653%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000654%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000655%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000656%type <JumpTable> JumpTable
Chris Lattnerdda71962001-11-26 18:54:16 +0000657%type <BoolVal> GlobalType OptInternal // GLOBAL or CONSTANT? Intern?
Chris Lattner00950542001-06-06 20:29:01 +0000658
Chris Lattner2079fde2001-10-13 06:41:08 +0000659// ValueRef - Unresolved reference to a definition or BB
660%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000661%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000662// Tokens and types for handling constant integer values
663//
664// ESINT64VAL - A negative number within long long range
665%token <SInt64Val> ESINT64VAL
666
667// EUINT64VAL - A positive number within uns. long long range
668%token <UInt64Val> EUINT64VAL
669%type <SInt64Val> EINT64VAL
670
671%token <SIntVal> SINTVAL // Signed 32 bit ints...
672%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
673%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000674%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000675
676// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000677%type <TypeVal> Types TypesV UpRTypes UpRTypesV
678%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattner30c89792001-09-07 16:35:17 +0000679%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
680%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000681
682%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
Chris Lattner8ebccb72002-05-22 22:33:00 +0000683%type <StrVal> OptVAR_ID OptAssign FuncName
Chris Lattner00950542001-06-06 20:29:01 +0000684
685
Chris Lattner08c0e6a2002-10-06 22:45:09 +0000686%token IMPLEMENTATION TRUE FALSE BEGINTOK ENDTOK DECLARE GLOBAL CONSTANT
687%token TO EXCEPT DOTDOTDOT NULL_TOK CONST INTERNAL OPAQUE NOT EXTERNAL
Chris Lattner00950542001-06-06 20:29:01 +0000688
689// Basic Block Terminating Operators
690%token <TermOpVal> RET BR SWITCH
691
Chris Lattner00950542001-06-06 20:29:01 +0000692// Binary Operators
693%type <BinaryOpVal> BinaryOps // all the binary operators
Chris Lattner4a6482b2002-09-10 19:57:26 +0000694%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Chris Lattner42c9e772001-10-20 09:32:59 +0000695%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000696%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000697
698// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000699%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000700
Chris Lattner027dcc52001-07-08 21:10:27 +0000701// Other Operators
702%type <OtherOpVal> ShiftOps
Chris Lattner2079fde2001-10-13 06:41:08 +0000703%token <OtherOpVal> PHI CALL INVOKE CAST SHL SHR
Chris Lattner027dcc52001-07-08 21:10:27 +0000704
Chris Lattner00950542001-06-06 20:29:01 +0000705%start Module
706%%
707
708// Handle constant integer size restriction and conversion...
709//
710
Chris Lattner51727be2002-06-04 21:58:56 +0000711INTVAL : SINTVAL;
Chris Lattner00950542001-06-06 20:29:01 +0000712INTVAL : UINTVAL {
713 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
714 ThrowException("Value too large for type!");
715 $$ = (int32_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000716};
Chris Lattner00950542001-06-06 20:29:01 +0000717
718
Chris Lattner51727be2002-06-04 21:58:56 +0000719EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner00950542001-06-06 20:29:01 +0000720EINT64VAL : EUINT64VAL {
721 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
722 ThrowException("Value too large for type!");
723 $$ = (int64_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000724};
Chris Lattner00950542001-06-06 20:29:01 +0000725
Chris Lattner00950542001-06-06 20:29:01 +0000726// Operations that are notably excluded from this list include:
727// RET, BR, & SWITCH because they end basic blocks and are treated specially.
728//
Chris Lattner4a6482b2002-09-10 19:57:26 +0000729ArithmeticOps: ADD | SUB | MUL | DIV | REM;
730LogicalOps : AND | OR | XOR;
731SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
732BinaryOps : ArithmeticOps | LogicalOps | SetCondOps;
733
Chris Lattner51727be2002-06-04 21:58:56 +0000734ShiftOps : SHL | SHR;
Chris Lattner00950542001-06-06 20:29:01 +0000735
Chris Lattnere98dda62001-07-14 06:10:16 +0000736// These are some types that allow classification if we only want a particular
737// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner51727be2002-06-04 21:58:56 +0000738SIntType : LONG | INT | SHORT | SBYTE;
739UIntType : ULONG | UINT | USHORT | UBYTE;
740IntType : SIntType | UIntType;
741FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +0000742
Chris Lattnere98dda62001-07-14 06:10:16 +0000743// OptAssign - Value producing statements have an optional assignment component
Chris Lattner00950542001-06-06 20:29:01 +0000744OptAssign : VAR_ID '=' {
745 $$ = $1;
746 }
747 | /*empty*/ {
748 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +0000749 };
Chris Lattner00950542001-06-06 20:29:01 +0000750
Chris Lattner51727be2002-06-04 21:58:56 +0000751OptInternal : INTERNAL { $$ = true; } | /*empty*/ { $$ = false; };
Chris Lattner30c89792001-09-07 16:35:17 +0000752
753//===----------------------------------------------------------------------===//
754// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +0000755// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +0000756// access to it, a user must explicitly use TypesV.
757//
758
759// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +0000760TypesV : Types | VOID { $$ = new PATypeHolder($1); };
761UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +0000762
763Types : UpRTypes {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000764 if (UpRefs.size())
765 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
766 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000767 };
Chris Lattner30c89792001-09-07 16:35:17 +0000768
769
770// Derived types are added later...
771//
Chris Lattner51727be2002-06-04 21:58:56 +0000772PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
773PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000774UpRTypes : OPAQUE {
775 $$ = new PATypeHolder(OpaqueType::get());
776 }
777 | PrimType {
778 $$ = new PATypeHolder($1);
Chris Lattner51727be2002-06-04 21:58:56 +0000779 };
Chris Lattnerd78700d2002-08-16 21:14:40 +0000780UpRTypes : SymbolicValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000781 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner51727be2002-06-04 21:58:56 +0000782};
Chris Lattner30c89792001-09-07 16:35:17 +0000783
Chris Lattner30c89792001-09-07 16:35:17 +0000784// Include derived types in the Types production.
785//
786UpRTypes : '\\' EUINT64VAL { // Type UpReference
787 if ($2 > (uint64_t)INT64_MAX) ThrowException("Value out of range!");
788 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
789 UpRefs.push_back(make_pair((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000790 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +0000791 UR_OUT("New Upreference!\n");
792 }
Chris Lattner79df7c02002-03-26 18:01:55 +0000793 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner30c89792001-09-07 16:35:17 +0000794 vector<const Type*> Params;
Chris Lattner697954c2002-01-20 22:54:45 +0000795 mapto($3->begin(), $3->end(), std::back_inserter(Params),
796 std::mem_fun_ref(&PATypeHandle<Type>::get));
Chris Lattner2079fde2001-10-13 06:41:08 +0000797 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
798 if (isVarArg) Params.pop_back();
799
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000800 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +0000801 delete $3; // Delete the argument list
802 delete $1; // Delete the old type handle
803 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000804 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000805 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000806 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +0000807 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000808 | '{' TypeListI '}' { // Structure type?
809 vector<const Type*> Elements;
Chris Lattner697954c2002-01-20 22:54:45 +0000810 mapto($2->begin(), $2->end(), std::back_inserter(Elements),
811 std::mem_fun_ref(&PATypeHandle<Type>::get));
Chris Lattner30c89792001-09-07 16:35:17 +0000812
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000813 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000814 delete $2;
815 }
816 | '{' '}' { // Empty structure type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000817 $$ = new PATypeHolder(StructType::get(vector<const Type*>()));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000818 }
819 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000820 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000821 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000822 };
Chris Lattner30c89792001-09-07 16:35:17 +0000823
Chris Lattner7e708292002-06-25 16:13:24 +0000824// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +0000825// declaration type lists
826//
827TypeListI : UpRTypes {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000828 $$ = new list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +0000829 $$->push_back(*$1); delete $1;
830 }
831 | TypeListI ',' UpRTypes {
832 ($$=$1)->push_back(*$3); delete $3;
Chris Lattner51727be2002-06-04 21:58:56 +0000833 };
Chris Lattner30c89792001-09-07 16:35:17 +0000834
Chris Lattner7e708292002-06-25 16:13:24 +0000835// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +0000836ArgTypeListI : TypeListI
837 | TypeListI ',' DOTDOTDOT {
838 ($$=$1)->push_back(Type::VoidTy);
839 }
840 | DOTDOTDOT {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000841 ($$ = new list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattner30c89792001-09-07 16:35:17 +0000842 }
843 | /*empty*/ {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000844 $$ = new list<PATypeHolder>();
Chris Lattner51727be2002-06-04 21:58:56 +0000845 };
Chris Lattner30c89792001-09-07 16:35:17 +0000846
Chris Lattnere98dda62001-07-14 06:10:16 +0000847// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +0000848// production is used ONLY to represent constants that show up AFTER a 'const',
849// 'constant' or 'global' token at global scope. Constants that can be inlined
850// into other expressions (such as integers and constexprs) are handled by the
851// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +0000852//
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000853ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
854 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
855 if (ATy == 0)
856 ThrowException("Cannot make array constant with type: '" +
857 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +0000858 const Type *ETy = ATy->getElementType();
859 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +0000860
Chris Lattner30c89792001-09-07 16:35:17 +0000861 // Verify that we have the correct size...
862 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +0000863 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattner30c89792001-09-07 16:35:17 +0000864 utostr($3->size()) + " arguments, but has size of " +
865 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +0000866
Chris Lattner30c89792001-09-07 16:35:17 +0000867 // Verify all elements are correct type!
868 for (unsigned i = 0; i < $3->size(); i++) {
869 if (ETy != (*$3)[i]->getType())
Chris Lattner00950542001-06-06 20:29:01 +0000870 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +0000871 ETy->getDescription() +"' as required!\nIt is of type '"+
872 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +0000873 }
874
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000875 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +0000876 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000877 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000878 | Types '[' ']' {
879 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
880 if (ATy == 0)
881 ThrowException("Cannot make array constant with type: '" +
882 (*$1)->getDescription() + "'!");
883
884 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +0000885 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +0000886 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattner30c89792001-09-07 16:35:17 +0000887 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000888 $$ = ConstantArray::get(ATy, vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +0000889 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +0000890 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000891 | Types 'c' STRINGCONSTANT {
892 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
893 if (ATy == 0)
894 ThrowException("Cannot make array constant with type: '" +
895 (*$1)->getDescription() + "'!");
896
Chris Lattner30c89792001-09-07 16:35:17 +0000897 int NumElements = ATy->getNumElements();
898 const Type *ETy = ATy->getElementType();
899 char *EndStr = UnEscapeLexed($3, true);
900 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +0000901 ThrowException("Can't build string constant of size " +
Chris Lattner30c89792001-09-07 16:35:17 +0000902 itostr((int)(EndStr-$3)) +
903 " when array has size " + itostr(NumElements) + "!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000904 vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +0000905 if (ETy == Type::SByteTy) {
906 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000907 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +0000908 } else if (ETy == Type::UByteTy) {
909 for (char *C = $3; C != EndStr; ++C)
Chris Lattnerbae362f2003-01-30 22:24:26 +0000910 Vals.push_back(ConstantUInt::get(ETy, (unsigned char)*C));
Chris Lattner93750fa2001-07-28 17:48:55 +0000911 } else {
Chris Lattner30c89792001-09-07 16:35:17 +0000912 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +0000913 ThrowException("Cannot build string arrays of non byte sized elements!");
914 }
Chris Lattner30c89792001-09-07 16:35:17 +0000915 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000916 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +0000917 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +0000918 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000919 | Types '{' ConstVector '}' {
920 const StructType *STy = dyn_cast<const StructType>($1->get());
921 if (STy == 0)
922 ThrowException("Cannot make struct constant with type: '" +
923 (*$1)->getDescription() + "'!");
Chris Lattneraf76d0e2003-04-15 16:09:31 +0000924
925 // Check to ensure that constants are compatible with the type initializer!
926 for (unsigned i = 0, e = $3->size(); i != e; ++i)
927 if ((*$3)[i]->getType() != STy->getElementTypes()[i])
928 ThrowException("Expected type '" +
929 STy->getElementTypes()[i]->getDescription() +
930 "' for element #" + utostr(i) +
931 " of structure initializer!");
932
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000933 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +0000934 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000935 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000936 | Types NULL_TOK {
937 const PointerType *PTy = dyn_cast<const PointerType>($1->get());
938 if (PTy == 0)
939 ThrowException("Cannot make null pointer constant with type: '" +
940 (*$1)->getDescription() + "'!");
941
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000942 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000943 delete $1;
944 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000945 | Types SymbolicValueRef {
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000946 const PointerType *Ty = dyn_cast<const PointerType>($1->get());
947 if (Ty == 0)
948 ThrowException("Global const reference must be a pointer type!");
949
Chris Lattner3101c252002-08-15 17:58:33 +0000950 // ConstExprs can exist in the body of a function, thus creating
951 // ConstantPointerRefs whenever they refer to a variable. Because we are in
952 // the context of a function, getValNonImprovising will search the functions
953 // symbol table instead of the module symbol table for the global symbol,
954 // which throws things all off. To get around this, we just tell
955 // getValNonImprovising that we are at global scope here.
956 //
957 Function *SavedCurFn = CurMeth.CurrentFunction;
958 CurMeth.CurrentFunction = 0;
959
Chris Lattner2079fde2001-10-13 06:41:08 +0000960 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000961
Chris Lattner3101c252002-08-15 17:58:33 +0000962 CurMeth.CurrentFunction = SavedCurFn;
963
964
Chris Lattner2079fde2001-10-13 06:41:08 +0000965 // If this is an initializer for a constant pointer, which is referencing a
966 // (currently) undefined variable, create a stub now that shall be replaced
967 // in the future with the right type of variable.
968 //
969 if (V == 0) {
970 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
971 const PointerType *PT = cast<PointerType>(Ty);
972
973 // First check to see if the forward references value is already created!
974 PerModuleInfo::GlobalRefsType::iterator I =
975 CurModule.GlobalRefs.find(make_pair(PT, $2));
976
977 if (I != CurModule.GlobalRefs.end()) {
978 V = I->second; // Placeholder already exists, use it...
979 } else {
980 // TODO: Include line number info by creating a subclass of
981 // TODO: GlobalVariable here that includes the said information!
982
983 // Create a placeholder for the global variable reference...
Chris Lattner7a176752001-12-04 00:03:30 +0000984 GlobalVariable *GV = new GlobalVariable(PT->getElementType(),
985 false, true);
Chris Lattner2079fde2001-10-13 06:41:08 +0000986 // Keep track of the fact that we have a forward ref to recycle it
987 CurModule.GlobalRefs.insert(make_pair(make_pair(PT, $2), GV));
988
989 // Must temporarily push this value into the module table...
990 CurModule.CurrentModule->getGlobalList().push_back(GV);
991 V = GV;
992 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000993 }
994
Chris Lattner2079fde2001-10-13 06:41:08 +0000995 GlobalValue *GV = cast<GlobalValue>(V);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000996 $$ = ConstantPointerRef::get(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +0000997 delete $1; // Free the type handle
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000998 }
Chris Lattnerd78700d2002-08-16 21:14:40 +0000999 | Types ConstExpr {
1000 if ($1->get() != $2->getType())
1001 ThrowException("Mismatched types for constant expression!");
1002 $$ = $2;
1003 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001004 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001005
Chris Lattnere43f40b2002-10-09 00:25:32 +00001006ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001007 if (!ConstantSInt::isValueValidForType($1, $2))
1008 ThrowException("Constant value doesn't fit in type!");
1009 $$ = ConstantSInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001010 }
1011 | UIntType EUINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001012 if (!ConstantUInt::isValueValidForType($1, $2))
1013 ThrowException("Constant value doesn't fit in type!");
1014 $$ = ConstantUInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001015 }
1016 | BOOL TRUE { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001017 $$ = ConstantBool::True;
1018 }
Chris Lattnere43f40b2002-10-09 00:25:32 +00001019 | BOOL FALSE { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001020 $$ = ConstantBool::False;
1021 }
1022 | FPType FPVAL { // Float & Double constants
1023 $$ = ConstantFP::get($1, $2);
1024 };
1025
Chris Lattner00950542001-06-06 20:29:01 +00001026
Chris Lattnerd78700d2002-08-16 21:14:40 +00001027ConstExpr: CAST '(' ConstVal TO Types ')' {
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001028 $$ = ConstantExpr::getCast($3, $5->get());
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001029 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001030 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001031 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1032 if (!isa<PointerType>($3->getType()))
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001033 ThrowException("GetElementPtr requires a pointer operand!");
1034
1035 const Type *IdxTy =
Chris Lattnerd78700d2002-08-16 21:14:40 +00001036 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001037 if (!IdxTy)
1038 ThrowException("Index list invalid for constant getelementptr!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001039
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001040 vector<Constant*> IdxVec;
Chris Lattnerd78700d2002-08-16 21:14:40 +00001041 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1042 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001043 IdxVec.push_back(C);
1044 else
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001045 ThrowException("Indices to constant getelementptr must be constants!");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001046
Chris Lattnerd78700d2002-08-16 21:14:40 +00001047 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001048
Chris Lattnerd78700d2002-08-16 21:14:40 +00001049 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001050 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001051 | BinaryOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001052 if ($3->getType() != $5->getType())
1053 ThrowException("Binary operator types must match!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001054 $$ = ConstantExpr::get($1, $3, $5);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001055 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001056 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001057 if ($5->getType() != Type::UByteTy)
1058 ThrowException("Shift count for shift constant must be unsigned byte!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001059 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattner699f1eb2002-08-14 17:12:33 +00001060 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001061
1062
Chris Lattnere98dda62001-07-14 06:10:16 +00001063// ConstVector - A list of comma seperated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001064ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001065 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001066 }
1067 | ConstVal {
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001068 $$ = new vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001069 $$->push_back($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001070 };
Chris Lattner00950542001-06-06 20:29:01 +00001071
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001072
Chris Lattner1781aca2001-09-18 04:00:54 +00001073// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001074GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001075
Chris Lattner00950542001-06-06 20:29:01 +00001076
Chris Lattner0e73ce62002-05-02 19:11:13 +00001077//===----------------------------------------------------------------------===//
1078// Rules to match Modules
1079//===----------------------------------------------------------------------===//
1080
1081// Module rule: Capture the result of parsing the whole file into a result
1082// variable...
1083//
1084Module : FunctionList {
1085 $$ = ParserResult = $1;
1086 CurModule.ModuleDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001087};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001088
Chris Lattner7e708292002-06-25 16:13:24 +00001089// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001090//
1091FunctionList : FunctionList Function {
1092 $$ = $1;
1093 assert($2->getParent() == 0 && "Function already in module!");
1094 $1->getFunctionList().push_back($2);
1095 CurMeth.FunctionDone();
1096 }
1097 | FunctionList FunctionProto {
1098 $$ = $1;
1099 }
1100 | FunctionList IMPLEMENTATION {
1101 $$ = $1;
1102 }
1103 | ConstPool {
1104 $$ = CurModule.CurrentModule;
1105 // Resolve circular types before we parse the body of the module
1106 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001107 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001108
Chris Lattnere98dda62001-07-14 06:10:16 +00001109// ConstPool - Constants with optional names assigned to them.
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001110ConstPool : ConstPool OptAssign CONST ConstVal {
Chris Lattneradf99702003-03-03 23:28:55 +00001111 if (!setValueName($4, $2))
1112 InsertValue($4);
Chris Lattner00950542001-06-06 20:29:01 +00001113 }
Chris Lattner30c89792001-09-07 16:35:17 +00001114 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner4a42e902001-10-22 05:56:09 +00001115 // Eagerly resolve types. This is not an optimization, this is a
1116 // requirement that is due to the fact that we could have this:
1117 //
1118 // %list = type { %list * }
1119 // %list = type { %list * } ; repeated type decl
1120 //
1121 // If types are not resolved eagerly, then the two types will not be
1122 // determined to be the same type!
1123 //
1124 ResolveTypeTo($2, $4->get());
1125
Chris Lattner1781aca2001-09-18 04:00:54 +00001126 // TODO: FIXME when Type are not const
Chris Lattnerb7474512001-10-03 15:39:04 +00001127 if (!setValueName(const_cast<Type*>($4->get()), $2)) {
1128 // If this is not a redefinition of a type...
1129 if (!$2) {
1130 InsertType($4->get(),
Chris Lattner79df7c02002-03-26 18:01:55 +00001131 inFunctionScope() ? CurMeth.Types : CurModule.Types);
Chris Lattnerb7474512001-10-03 15:39:04 +00001132 }
Chris Lattner30c89792001-09-07 16:35:17 +00001133 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001134
1135 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001136 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001137 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001138 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001139 | ConstPool OptAssign OptInternal GlobalType ConstVal {
1140 const Type *Ty = $5->getType();
Chris Lattner1781aca2001-09-18 04:00:54 +00001141 // Global declarations appear in Constant Pool
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001142 Constant *Initializer = $5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001143 if (Initializer == 0)
1144 ThrowException("Global value initializer is not a constant!");
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001145
Chris Lattnerdda71962001-11-26 18:54:16 +00001146 GlobalVariable *GV = new GlobalVariable(Ty, $4, $3, Initializer);
Chris Lattnerb7474512001-10-03 15:39:04 +00001147 if (!setValueName(GV, $2)) { // If not redefining...
1148 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001149 int Slot = InsertValue(GV, CurModule.Values);
1150
1151 if (Slot != -1) {
1152 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1153 } else {
1154 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1155 (char*)GV->getName().c_str()));
1156 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001157 }
Chris Lattner1781aca2001-09-18 04:00:54 +00001158 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001159 | ConstPool OptAssign EXTERNAL GlobalType Types {
1160 const Type *Ty = *$5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001161 // Global declarations appear in Constant Pool
Chris Lattner1f862af2003-04-16 18:13:57 +00001162 GlobalVariable *GV = new GlobalVariable(Ty, $4, false);
Chris Lattnerb7474512001-10-03 15:39:04 +00001163 if (!setValueName(GV, $2)) { // If not redefining...
1164 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001165 int Slot = InsertValue(GV, CurModule.Values);
1166
1167 if (Slot != -1) {
1168 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1169 } else {
1170 assert(GV->hasName() && "Not named and not numbered!?");
1171 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1172 (char*)GV->getName().c_str()));
1173 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001174 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001175 delete $5;
Chris Lattnere98dda62001-07-14 06:10:16 +00001176 }
Chris Lattner00950542001-06-06 20:29:01 +00001177 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001178 };
Chris Lattner00950542001-06-06 20:29:01 +00001179
1180
1181//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001182// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001183//===----------------------------------------------------------------------===//
1184
Chris Lattner51727be2002-06-04 21:58:56 +00001185OptVAR_ID : VAR_ID | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001186
1187ArgVal : Types OptVAR_ID {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001188 if (*$1 == Type::VoidTy)
1189 ThrowException("void typed arguments are invalid!");
1190 $$ = new pair<PATypeHolder*, char*>($1, $2);
Chris Lattner51727be2002-06-04 21:58:56 +00001191};
Chris Lattner00950542001-06-06 20:29:01 +00001192
Chris Lattner69da5cf2002-10-13 20:57:00 +00001193ArgListH : ArgListH ',' ArgVal {
1194 $$ = $1;
1195 $1->push_back(*$3);
1196 delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001197 }
1198 | ArgVal {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001199 $$ = new vector<pair<PATypeHolder*,char*> >();
1200 $$->push_back(*$1);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001201 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001202 };
Chris Lattner00950542001-06-06 20:29:01 +00001203
1204ArgList : ArgListH {
1205 $$ = $1;
1206 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001207 | ArgListH ',' DOTDOTDOT {
1208 $$ = $1;
1209 $$->push_back(pair<PATypeHolder*, char*>(new PATypeHolder(Type::VoidTy),0));
1210 }
1211 | DOTDOTDOT {
1212 $$ = new vector<pair<PATypeHolder*,char*> >();
1213 $$->push_back(pair<PATypeHolder*, char*>(new PATypeHolder(Type::VoidTy),0));
1214 }
Chris Lattner00950542001-06-06 20:29:01 +00001215 | /* empty */ {
1216 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001217 };
Chris Lattner00950542001-06-06 20:29:01 +00001218
Chris Lattner8ebccb72002-05-22 22:33:00 +00001219FuncName : VAR_ID | STRINGCONSTANT;
1220
Chris Lattner1f862af2003-04-16 18:13:57 +00001221FunctionHeaderH : TypesV FuncName '(' ArgList ')' {
1222 UnEscapeLexed($2);
1223 string FunctionName($2);
Chris Lattnerdda71962001-11-26 18:54:16 +00001224
Chris Lattner30c89792001-09-07 16:35:17 +00001225 vector<const Type*> ParamTypeList;
Chris Lattner1f862af2003-04-16 18:13:57 +00001226 if ($4) { // If there are arguments...
1227 for (vector<pair<PATypeHolder*,char*> >::iterator I = $4->begin();
1228 I != $4->end(); ++I)
Chris Lattner69da5cf2002-10-13 20:57:00 +00001229 ParamTypeList.push_back(I->first->get());
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001230 }
Chris Lattner00950542001-06-06 20:29:01 +00001231
Chris Lattner2079fde2001-10-13 06:41:08 +00001232 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1233 if (isVarArg) ParamTypeList.pop_back();
1234
Chris Lattner1f862af2003-04-16 18:13:57 +00001235 const FunctionType *FT = FunctionType::get(*$1, ParamTypeList, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001236 const PointerType *PFT = PointerType::get(FT);
Chris Lattner1f862af2003-04-16 18:13:57 +00001237 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001238
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001239 Function *Fn = 0;
1240 // Is the function already in symtab?
1241 if ((Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1242 // Yes it is. If this is the case, either we need to be a forward decl,
1243 // or it needs to be.
1244 if (!CurMeth.isDeclare && !Fn->isExternal())
1245 ThrowException("Redefinition of function '" + FunctionName + "'!");
1246
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001247 // If we found a preexisting function prototype, remove it from the
1248 // module, so that we don't get spurious conflicts with global & local
1249 // variables.
1250 //
1251 CurModule.CurrentModule->getFunctionList().remove(Fn);
Chris Lattner34538142002-03-08 19:11:42 +00001252
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001253 // Make sure to strip off any argument names so we can't get conflicts...
1254 for (Function::aiterator AI = Fn->abegin(), AE = Fn->aend(); AI != AE; ++AI)
1255 AI->setName("");
Chris Lattner5659dd12002-07-15 00:10:33 +00001256
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001257 } else { // Not already defined?
Chris Lattner1f862af2003-04-16 18:13:57 +00001258 Fn = new Function(FT, false, FunctionName);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001259 InsertValue(Fn, CurModule.Values);
Chris Lattner1f862af2003-04-16 18:13:57 +00001260 CurModule.DeclareNewGlobalValue(Fn, ValID::create($2));
Chris Lattnere1815642001-07-15 06:35:53 +00001261 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001262 free($2); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +00001263
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001264 CurMeth.FunctionStart(Fn);
Chris Lattner00950542001-06-06 20:29:01 +00001265
Chris Lattner7e708292002-06-25 16:13:24 +00001266 // Add all of the arguments we parsed to the function...
Chris Lattner1f862af2003-04-16 18:13:57 +00001267 if ($4) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001268 if (isVarArg) { // Nuke the last entry
Chris Lattner1f862af2003-04-16 18:13:57 +00001269 assert($4->back().first->get() == Type::VoidTy && $4->back().second == 0&&
Chris Lattner69da5cf2002-10-13 20:57:00 +00001270 "Not a varargs marker!");
Chris Lattner1f862af2003-04-16 18:13:57 +00001271 delete $4->back().first;
1272 $4->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00001273 }
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001274 Function::aiterator ArgIt = Fn->abegin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001275 for (vector<pair<PATypeHolder*, char*> >::iterator I = $4->begin();
1276 I != $4->end(); ++I, ++ArgIt) {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001277 delete I->first; // Delete the typeholder...
1278
1279 if (setValueName(ArgIt, I->second)) // Insert arg into symtab...
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001280 assert(0 && "No arg redef allowed!");
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001281
Chris Lattner69da5cf2002-10-13 20:57:00 +00001282 InsertValue(ArgIt);
Chris Lattner00950542001-06-06 20:29:01 +00001283 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001284
Chris Lattner1f862af2003-04-16 18:13:57 +00001285 delete $4; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00001286 }
Chris Lattner51727be2002-06-04 21:58:56 +00001287};
Chris Lattner00950542001-06-06 20:29:01 +00001288
Chris Lattner9b02cc32002-05-03 18:23:48 +00001289BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1290
Chris Lattner1f862af2003-04-16 18:13:57 +00001291FunctionHeader : OptInternal FunctionHeaderH BEGIN {
Chris Lattner79df7c02002-03-26 18:01:55 +00001292 $$ = CurMeth.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001293
Chris Lattner1f862af2003-04-16 18:13:57 +00001294 // Make sure that we keep track of the internal marker, even if there was
1295 // a previous "declare".
1296 if ($1)
1297 $$->setInternalLinkage(true);
1298
Chris Lattner7e708292002-06-25 16:13:24 +00001299 // Resolve circular types before we parse the body of the function.
Chris Lattner30c89792001-09-07 16:35:17 +00001300 ResolveTypes(CurMeth.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001301};
Chris Lattner00950542001-06-06 20:29:01 +00001302
Chris Lattner9b02cc32002-05-03 18:23:48 +00001303END : ENDTOK | '}'; // Allow end of '}' to end a function
1304
Chris Lattner79df7c02002-03-26 18:01:55 +00001305Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001306 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001307};
Chris Lattner00950542001-06-06 20:29:01 +00001308
Chris Lattner79df7c02002-03-26 18:01:55 +00001309FunctionProto : DECLARE { CurMeth.isDeclare = true; } FunctionHeaderH {
1310 $$ = CurMeth.CurrentFunction;
1311 assert($$->getParent() == 0 && "Function already in module!");
1312 CurModule.CurrentModule->getFunctionList().push_back($$);
1313 CurMeth.FunctionDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001314};
Chris Lattner00950542001-06-06 20:29:01 +00001315
1316//===----------------------------------------------------------------------===//
1317// Rules to match Basic Blocks
1318//===----------------------------------------------------------------------===//
1319
1320ConstValueRef : ESINT64VAL { // A reference to a direct constant
1321 $$ = ValID::create($1);
1322 }
1323 | EUINT64VAL {
1324 $$ = ValID::create($1);
1325 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001326 | FPVAL { // Perhaps it's an FP constant?
1327 $$ = ValID::create($1);
1328 }
Chris Lattner00950542001-06-06 20:29:01 +00001329 | TRUE {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001330 $$ = ValID::create(ConstantBool::True);
Chris Lattner00950542001-06-06 20:29:01 +00001331 }
1332 | FALSE {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001333 $$ = ValID::create(ConstantBool::False);
Chris Lattner00950542001-06-06 20:29:01 +00001334 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001335 | NULL_TOK {
1336 $$ = ValID::createNull();
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001337 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001338 | ConstExpr {
1339 $$ = ValID::create($1);
1340 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00001341
Chris Lattner2079fde2001-10-13 06:41:08 +00001342// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1343// another value.
1344//
1345SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001346 $$ = ValID::create($1);
1347 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001348 | VAR_ID { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001349 $$ = ValID::create($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001350 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001351
1352// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00001353ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00001354
Chris Lattner00950542001-06-06 20:29:01 +00001355
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001356// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1357// type immediately preceeds the value reference, and allows complex constant
1358// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001359ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001360 $$ = getVal(*$1, $2); delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001361 };
Chris Lattner8b81bf52001-07-25 22:47:46 +00001362
Chris Lattner00950542001-06-06 20:29:01 +00001363BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner7e708292002-06-25 16:13:24 +00001364 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner00950542001-06-06 20:29:01 +00001365 }
Chris Lattner7e708292002-06-25 16:13:24 +00001366 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
1367 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner51727be2002-06-04 21:58:56 +00001368 };
Chris Lattner00950542001-06-06 20:29:01 +00001369
1370
1371// Basic blocks are terminated by branching instructions:
1372// br, br/cc, switch, ret
1373//
Chris Lattner2079fde2001-10-13 06:41:08 +00001374BasicBlock : InstructionList OptAssign BBTerminatorInst {
1375 if (setValueName($3, $2)) { assert(0 && "No redefn allowed!"); }
1376 InsertValue($3);
1377
1378 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001379 InsertValue($1);
1380 $$ = $1;
1381 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001382 | LABELSTR InstructionList OptAssign BBTerminatorInst {
1383 if (setValueName($4, $3)) { assert(0 && "No redefn allowed!"); }
1384 InsertValue($4);
1385
1386 $2->getInstList().push_back($4);
Chris Lattnerb7474512001-10-03 15:39:04 +00001387 if (setValueName($2, $1)) { assert(0 && "No label redef allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001388
1389 InsertValue($2);
1390 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001391 };
Chris Lattner00950542001-06-06 20:29:01 +00001392
1393InstructionList : InstructionList Inst {
1394 $1->getInstList().push_back($2);
1395 $$ = $1;
1396 }
1397 | /* empty */ {
Chris Lattner0383cc42002-08-21 23:51:21 +00001398 $$ = CurBB = new BasicBlock();
Chris Lattner51727be2002-06-04 21:58:56 +00001399 };
Chris Lattner00950542001-06-06 20:29:01 +00001400
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001401BBTerminatorInst : RET ResolvedVal { // Return with a result...
1402 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001403 }
1404 | RET VOID { // Return with no result...
1405 $$ = new ReturnInst();
1406 }
1407 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner9636a912001-10-01 16:18:37 +00001408 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $3)));
Chris Lattner00950542001-06-06 20:29:01 +00001409 } // Conditional Branch...
1410 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner9636a912001-10-01 16:18:37 +00001411 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $6)),
1412 cast<BasicBlock>(getVal(Type::LabelTy, $9)),
Chris Lattner00950542001-06-06 20:29:01 +00001413 getVal(Type::BoolTy, $3));
1414 }
1415 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1416 SwitchInst *S = new SwitchInst(getVal($2, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001417 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
Chris Lattner00950542001-06-06 20:29:01 +00001418 $$ = S;
1419
Chris Lattner46748042002-04-09 19:41:42 +00001420 vector<pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
1421 E = $8->end();
1422 for (; I != E; ++I)
Chris Lattner00950542001-06-06 20:29:01 +00001423 S->dest_push_back(I->first, I->second);
1424 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001425 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO ResolvedVal
1426 EXCEPT ResolvedVal {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001427 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001428 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00001429
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001430 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1431 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00001432 // Pull out the types of all of the arguments...
1433 vector<const Type*> ParamTypes;
1434 if ($5) {
Chris Lattner6cdb0112001-11-26 16:54:11 +00001435 for (vector<Value*>::iterator I = $5->begin(), E = $5->end(); I!=E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00001436 ParamTypes.push_back((*I)->getType());
1437 }
1438
1439 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1440 if (isVarArg) ParamTypes.pop_back();
1441
Chris Lattner79df7c02002-03-26 18:01:55 +00001442 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001443 PFTy = PointerType::get(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00001444 }
1445 delete $2;
1446
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001447 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner2079fde2001-10-13 06:41:08 +00001448
1449 BasicBlock *Normal = dyn_cast<BasicBlock>($8);
1450 BasicBlock *Except = dyn_cast<BasicBlock>($10);
1451
1452 if (Normal == 0 || Except == 0)
1453 ThrowException("Invoke instruction without label destinations!");
1454
1455 // Create the call node...
1456 if (!$5) { // Has no arguments?
Chris Lattner386a3b72001-10-16 19:54:17 +00001457 $$ = new InvokeInst(V, Normal, Except, vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001458 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001459 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00001460 // correctly!
1461 //
Chris Lattner79df7c02002-03-26 18:01:55 +00001462 FunctionType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1463 FunctionType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
Chris Lattner6cdb0112001-11-26 16:54:11 +00001464 vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00001465
1466 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1467 if ((*ArgI)->getType() != *I)
1468 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001469 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001470
1471 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1472 ThrowException("Invalid number of parameters detected!");
1473
Chris Lattner6cdb0112001-11-26 16:54:11 +00001474 $$ = new InvokeInst(V, Normal, Except, *$5);
Chris Lattner2079fde2001-10-13 06:41:08 +00001475 }
1476 delete $5;
Chris Lattner51727be2002-06-04 21:58:56 +00001477 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001478
1479
Chris Lattner00950542001-06-06 20:29:01 +00001480
1481JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1482 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001483 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001484 if (V == 0)
1485 ThrowException("May only switch on a constant pool value!");
1486
Chris Lattner9636a912001-10-01 16:18:37 +00001487 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($5, $6))));
Chris Lattner00950542001-06-06 20:29:01 +00001488 }
1489 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner46748042002-04-09 19:41:42 +00001490 $$ = new vector<pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001491 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00001492
1493 if (V == 0)
1494 ThrowException("May only switch on a constant pool value!");
1495
Chris Lattner9636a912001-10-01 16:18:37 +00001496 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($4, $5))));
Chris Lattner51727be2002-06-04 21:58:56 +00001497 };
Chris Lattner00950542001-06-06 20:29:01 +00001498
1499Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001500 // Is this definition named?? if so, assign the name...
1501 if (setValueName($2, $1)) { assert(0 && "No redefin allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001502 InsertValue($2);
1503 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001504};
Chris Lattner00950542001-06-06 20:29:01 +00001505
Chris Lattnerc24d2082001-06-11 15:04:20 +00001506PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
1507 $$ = new list<pair<Value*, BasicBlock*> >();
Chris Lattner30c89792001-09-07 16:35:17 +00001508 $$->push_back(make_pair(getVal(*$1, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001509 cast<BasicBlock>(getVal(Type::LabelTy, $5))));
Chris Lattner30c89792001-09-07 16:35:17 +00001510 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001511 }
1512 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1513 $$ = $1;
1514 $1->push_back(make_pair(getVal($1->front().first->getType(), $4),
Chris Lattner9636a912001-10-01 16:18:37 +00001515 cast<BasicBlock>(getVal(Type::LabelTy, $6))));
Chris Lattner51727be2002-06-04 21:58:56 +00001516 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00001517
1518
Chris Lattner30c89792001-09-07 16:35:17 +00001519ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner6cdb0112001-11-26 16:54:11 +00001520 $$ = new vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001521 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001522 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001523 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001524 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001525 $1->push_back($3);
Chris Lattner51727be2002-06-04 21:58:56 +00001526 };
Chris Lattner00950542001-06-06 20:29:01 +00001527
1528// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner51727be2002-06-04 21:58:56 +00001529ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001530
Chris Lattner4a6482b2002-09-10 19:57:26 +00001531InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
1532 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint())
1533 ThrowException("Arithmetic operator requires integer or FP operands!");
1534 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1535 if ($$ == 0)
1536 ThrowException("binary operator returned null!");
1537 delete $2;
1538 }
1539 | LogicalOps Types ValueRef ',' ValueRef {
1540 if (!(*$2)->isIntegral())
1541 ThrowException("Logical operator requires integral operands!");
1542 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1543 if ($$ == 0)
1544 ThrowException("binary operator returned null!");
1545 delete $2;
1546 }
1547 | SetCondOps Types ValueRef ',' ValueRef {
Chris Lattner1cff96a2002-09-10 22:37:46 +00001548 $$ = new SetCondInst($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001549 if ($$ == 0)
1550 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001551 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001552 }
Chris Lattner699f1eb2002-08-14 17:12:33 +00001553 | NOT ResolvedVal {
1554 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
1555 << " Replacing with 'xor'.\n";
1556
1557 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
1558 if (Ones == 0)
1559 ThrowException("Expected integral type for not instruction!");
1560
1561 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner00950542001-06-06 20:29:01 +00001562 if ($$ == 0)
Chris Lattner699f1eb2002-08-14 17:12:33 +00001563 ThrowException("Could not create a xor instruction!");
Chris Lattner09083092001-07-08 04:57:15 +00001564 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001565 | ShiftOps ResolvedVal ',' ResolvedVal {
1566 if ($4->getType() != Type::UByteTy)
1567 ThrowException("Shift amount must be ubyte!");
1568 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001569 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001570 | CAST ResolvedVal TO Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001571 $$ = new CastInst($2, *$4);
1572 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001573 }
Chris Lattnerc24d2082001-06-11 15:04:20 +00001574 | PHI PHIList {
1575 const Type *Ty = $2->front().first->getType();
1576 $$ = new PHINode(Ty);
Chris Lattner00950542001-06-06 20:29:01 +00001577 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001578 if ($2->front().first->getType() != Ty)
1579 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001580 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001581 $2->pop_front();
1582 }
1583 delete $2; // Free the list...
1584 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001585 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001586 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001587 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001588
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001589 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1590 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001591 // Pull out the types of all of the arguments...
1592 vector<const Type*> ParamTypes;
Chris Lattneref9c23f2001-10-03 14:53:21 +00001593 if ($5) {
Chris Lattner6cdb0112001-11-26 16:54:11 +00001594 for (vector<Value*>::iterator I = $5->begin(), E = $5->end(); I!=E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00001595 ParamTypes.push_back((*I)->getType());
1596 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001597
1598 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1599 if (isVarArg) ParamTypes.pop_back();
1600
Chris Lattner79df7c02002-03-26 18:01:55 +00001601 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001602 PFTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001603 }
Chris Lattner30c89792001-09-07 16:35:17 +00001604 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001605
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001606 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001607
Chris Lattner8b81bf52001-07-25 22:47:46 +00001608 // Create the call node...
1609 if (!$5) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00001610 // Make sure no arguments is a good thing!
1611 if (Ty->getNumParams() != 0)
1612 ThrowException("No arguments passed to a function that "
1613 "expects arguments!");
1614
Chris Lattner386a3b72001-10-16 19:54:17 +00001615 $$ = new CallInst(V, vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001616 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001617 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00001618 // correctly!
1619 //
Chris Lattner79df7c02002-03-26 18:01:55 +00001620 FunctionType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1621 FunctionType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
Chris Lattner6cdb0112001-11-26 16:54:11 +00001622 vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001623
1624 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1625 if ((*ArgI)->getType() != *I)
1626 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001627 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001628
Chris Lattner8b81bf52001-07-25 22:47:46 +00001629 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner00950542001-06-06 20:29:01 +00001630 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00001631
Chris Lattner6cdb0112001-11-26 16:54:11 +00001632 $$ = new CallInst(V, *$5);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001633 }
1634 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00001635 }
1636 | MemoryInst {
1637 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001638 };
Chris Lattner00950542001-06-06 20:29:01 +00001639
Chris Lattner6cdb0112001-11-26 16:54:11 +00001640
1641// IndexList - List of indices for GEP based instructions...
1642IndexList : ',' ValueRefList {
Chris Lattner027dcc52001-07-08 21:10:27 +00001643 $$ = $2;
1644} | /* empty */ {
Chris Lattner6cdb0112001-11-26 16:54:11 +00001645 $$ = new vector<Value*>();
Chris Lattner51727be2002-06-04 21:58:56 +00001646};
Chris Lattner027dcc52001-07-08 21:10:27 +00001647
Chris Lattner00950542001-06-06 20:29:01 +00001648MemoryInst : MALLOC Types {
Chris Lattner05804b72002-09-13 22:28:45 +00001649 $$ = new MallocInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00001650 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001651 }
1652 | MALLOC Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00001653 $$ = new MallocInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001654 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001655 }
1656 | ALLOCA Types {
Chris Lattner05804b72002-09-13 22:28:45 +00001657 $$ = new AllocaInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00001658 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001659 }
1660 | ALLOCA Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00001661 $$ = new AllocaInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001662 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001663 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001664 | FREE ResolvedVal {
Chris Lattner9b625032002-05-06 16:15:30 +00001665 if (!isa<PointerType>($2->getType()))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001666 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00001667 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001668 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001669 }
1670
Chris Lattner6cdb0112001-11-26 16:54:11 +00001671 | LOAD Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00001672 if (!isa<PointerType>($2->get()))
Chris Lattner2079fde2001-10-13 06:41:08 +00001673 ThrowException("Can't load from nonpointer type: " +
1674 (*$2)->getDescription());
Chris Lattner5dfe7672002-08-22 22:48:55 +00001675 if (GetElementPtrInst::getIndexedType(*$2, *$4) == 0)
Chris Lattner027dcc52001-07-08 21:10:27 +00001676 ThrowException("Invalid indices for load instruction!");
1677
Chris Lattner0383cc42002-08-21 23:51:21 +00001678 Value *Src = getVal(*$2, $3);
1679 if (!$4->empty()) {
1680 std::cerr << "WARNING: Use of index load instruction:"
1681 << " replacing with getelementptr/load pair.\n";
1682 // Create a getelementptr hack instruction to do the right thing for
1683 // compatibility.
1684 //
1685 Instruction *I = new GetElementPtrInst(Src, *$4);
1686 CurBB->getInstList().push_back(I);
1687 Src = I;
1688 }
1689
1690 $$ = new LoadInst(Src);
Chris Lattner027dcc52001-07-08 21:10:27 +00001691 delete $4; // Free the vector...
Chris Lattner30c89792001-09-07 16:35:17 +00001692 delete $2;
Chris Lattner027dcc52001-07-08 21:10:27 +00001693 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00001694 | STORE ResolvedVal ',' Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00001695 if (!isa<PointerType>($4->get()))
Chris Lattner72e00252001-12-14 16:28:42 +00001696 ThrowException("Can't store to a nonpointer type: " +
1697 (*$4)->getDescription());
Chris Lattner5dfe7672002-08-22 22:48:55 +00001698 const Type *ElTy = GetElementPtrInst::getIndexedType(*$4, *$6);
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001699 if (ElTy == 0)
1700 ThrowException("Can't store into that field list!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001701 if (ElTy != $2->getType())
Chris Lattner72e00252001-12-14 16:28:42 +00001702 ThrowException("Can't store '" + $2->getType()->getDescription() +
1703 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner0383cc42002-08-21 23:51:21 +00001704
1705 Value *Ptr = getVal(*$4, $5);
1706 if (!$6->empty()) {
1707 std::cerr << "WARNING: Use of index store instruction:"
1708 << " replacing with getelementptr/store pair.\n";
1709 // Create a getelementptr hack instruction to do the right thing for
1710 // compatibility.
1711 //
1712 Instruction *I = new GetElementPtrInst(Ptr, *$6);
1713 CurBB->getInstList().push_back(I);
1714 Ptr = I;
1715 }
1716
1717 $$ = new StoreInst($2, Ptr);
Chris Lattner30c89792001-09-07 16:35:17 +00001718 delete $4; delete $6;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001719 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00001720 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner0235fe22002-09-11 01:17:27 +00001721 for (unsigned i = 0, e = $4->size(); i != e; ++i) {
1722 if ((*$4)[i]->getType() == Type::UIntTy) {
1723 std::cerr << "WARNING: Use of uint type indexes to getelementptr "
1724 << "instruction: replacing with casts to long type.\n";
1725 Instruction *I = new CastInst((*$4)[i], Type::LongTy);
1726 CurBB->getInstList().push_back(I);
1727 (*$4)[i] = I;
1728 }
1729 }
1730
Chris Lattner51727be2002-06-04 21:58:56 +00001731 if (!isa<PointerType>($2->get()))
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001732 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner30c89792001-09-07 16:35:17 +00001733 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner72e00252001-12-14 16:28:42 +00001734 ThrowException("Can't get element ptr '" + (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001735 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
1736 delete $2; delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00001737 };
Chris Lattner027dcc52001-07-08 21:10:27 +00001738
Chris Lattner00950542001-06-06 20:29:01 +00001739%%
Chris Lattner09083092001-07-08 04:57:15 +00001740int yyerror(const char *ErrorMsg) {
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001741 string where = string((CurFilename == "-")? string("<stdin>") : CurFilename)
1742 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
1743 string errMsg = string(ErrorMsg) + string("\n") + where + " while reading ";
1744 if (yychar == YYEMPTY)
1745 errMsg += "end-of-file.";
1746 else
1747 errMsg += "token: '" + string(llvmAsmtext, llvmAsmleng) + "'";
1748 ThrowException(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00001749 return 0;
1750}