blob: f2135a7ad25383cf41aa7a15c5e6c327e8b784d7 [file] [log] [blame]
Chris Lattner2f7c9632001-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 Lattner2f7c9632001-06-06 20:29:01 +00007%{
8#include "ParserInternals.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +00009#include "llvm/SymbolTable.h"
10#include "llvm/Module.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000011#include "llvm/iTerminators.h"
12#include "llvm/iMemory.h"
Chris Lattnera6834c12002-09-10 22:37:46 +000013#include "llvm/iOperators.h"
Chris Lattnerfb5ae022001-12-03 18:02:31 +000014#include "llvm/iPHINode.h"
Chris Lattner5de22042001-11-27 00:03:19 +000015#include "Support/STLExtras.h"
16#include "Support/DepthFirstIterator.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000017#include <list>
Chris Lattner3cd8c562002-07-30 18:54:25 +000018#include <utility>
Chris Lattneraa534bf2001-09-07 16:35:17 +000019#include <algorithm>
Chris Lattner7f74a562002-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 Lattner7f74a562002-01-20 22:54:45 +000026using std::string;
Chris Lattner2f7c9632001-06-06 20:29:01 +000027
Chris Lattner322e49a2001-10-16 19:54:17 +000028int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
Chris Lattnera6821822001-07-08 04:57:15 +000029int yylex(); // declaration" of xxx warnings.
Chris Lattner2f7c9632001-06-06 20:29:01 +000030int yyparse();
31
32static Module *ParserResult;
Chris Lattnerf2d1e792001-07-22 18:36:00 +000033string CurFilename;
Chris Lattner2f7c9632001-06-06 20:29:01 +000034
Chris Lattneraa534bf2001-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 Lattnerdb3b2022002-08-14 17:12:33 +000040#define UR_OUT(X) std::cerr << X
Chris Lattneraa534bf2001-09-07 16:35:17 +000041#else
42#define UR_OUT(X)
43#endif
44
Vikram S. Adve7064eaf2002-07-14 22:59:28 +000045#define YYERROR_VERBOSE 1
46
Chris Lattner319c47a2002-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 Lattner113f4f42002-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 Lattner2f7c9632001-06-06 20:29:01 +000057//
58typedef vector<Value *> ValueList; // Numbered defs
Chris Lattner322e49a2001-10-16 19:54:17 +000059static void ResolveDefinitions(vector<ValueList> &LateResolvers,
60 vector<ValueList> *FutureLateResolvers = 0);
Chris Lattner2f7c9632001-06-06 20:29:01 +000061
62static struct PerModuleInfo {
63 Module *CurrentModule;
Chris Lattneraa534bf2001-09-07 16:35:17 +000064 vector<ValueList> Values; // Module level numbered definitions
65 vector<ValueList> LateResolveValues;
Chris Lattner30752bd92002-04-04 19:23:55 +000066 vector<PATypeHolder> Types;
67 map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner2f7c9632001-06-06 20:29:01 +000068
Chris Lattnerf26e0d92001-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 Lattner3462ae32001-12-03 22:26:30 +000072 // here. This is used for forward references of ConstantPointerRefs.
Chris Lattnerf26e0d92001-10-13 06:41:08 +000073 //
74 typedef map<pair<const PointerType *, ValID>, GlobalVariable*> GlobalRefsType;
75 GlobalRefsType GlobalRefs;
76
Chris Lattner2f7c9632001-06-06 20:29:01 +000077 void ModuleDone() {
Chris Lattner113f4f42002-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 Lattneraa534bf2001-09-07 16:35:17 +000081 //
Chris Lattner2f7c9632001-06-06 20:29:01 +000082 ResolveDefinitions(LateResolveValues);
83
Chris Lattnerf26e0d92001-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 Lattner1a8ea8a2002-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 Lattnerf26e0d92001-10-13 06:41:08 +000096 }
97
Chris Lattner113f4f42002-06-25 16:13:24 +000098 Values.clear(); // Clear out function local definitions
Chris Lattneraa534bf2001-09-07 16:35:17 +000099 Types.clear();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000100 CurrentModule = 0;
101 }
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000102
103
Vikram S. Adve7064eaf2002-07-14 22:59:28 +0000104 // DeclareNewGlobalValue - Called every time a new GV has been defined. This
Chris Lattnerf26e0d92001-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. Adve7064eaf2002-07-14 22:59:28 +0000118 // allowed to be is ConstantPointerRef's.
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000119 assert(OldGV->use_size() == 1 && "Only one reference should exist!");
Chris Lattner8022a0f2002-10-14 03:28:42 +0000120 User *U = OldGV->use_back(); // Must be a ConstantPointerRef...
121 ConstantPointerRef *CPR = cast<ConstantPointerRef>(U);
Vikram S. Adve7064eaf2002-07-14 22:59:28 +0000122
Chris Lattner8022a0f2002-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. Adve7064eaf2002-07-14 22:59:28 +0000127
128 // Remove OldGV from the module...
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000129 CurrentModule->getGlobalList().remove(OldGV);
130 delete OldGV; // Delete the old placeholder
Vikram S. Adve7064eaf2002-07-14 22:59:28 +0000131
Chris Lattnerf26e0d92001-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 Lattner2f7c9632001-06-06 20:29:01 +0000137} CurModule;
138
Chris Lattner57698e22002-03-26 18:01:55 +0000139static struct PerFunctionInfo {
Chris Lattner113f4f42002-06-25 16:13:24 +0000140 Function *CurrentFunction; // Pointer to current function being created
Chris Lattner2f7c9632001-06-06 20:29:01 +0000141
Chris Lattner17f729e2001-07-15 06:35:53 +0000142 vector<ValueList> Values; // Keep track of numbered definitions
Chris Lattner2f7c9632001-06-06 20:29:01 +0000143 vector<ValueList> LateResolveValues;
Chris Lattner30752bd92002-04-04 19:23:55 +0000144 vector<PATypeHolder> Types;
145 map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner113f4f42002-06-25 16:13:24 +0000146 bool isDeclare; // Is this function a forward declararation?
Chris Lattner2f7c9632001-06-06 20:29:01 +0000147
Chris Lattner57698e22002-03-26 18:01:55 +0000148 inline PerFunctionInfo() {
149 CurrentFunction = 0;
Chris Lattner17f729e2001-07-15 06:35:53 +0000150 isDeclare = false;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000151 }
152
Chris Lattner57698e22002-03-26 18:01:55 +0000153 inline ~PerFunctionInfo() {}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000154
Chris Lattner57698e22002-03-26 18:01:55 +0000155 inline void FunctionStart(Function *M) {
156 CurrentFunction = M;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000157 }
158
Chris Lattner57698e22002-03-26 18:01:55 +0000159 void FunctionDone() {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000160 // If we could not resolve some blocks at parsing time (forward branches)
161 // resolve the branches now...
Chris Lattner322e49a2001-10-16 19:54:17 +0000162 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000163
Chris Lattner113f4f42002-06-25 16:13:24 +0000164 Values.clear(); // Clear out function local definitions
Chris Lattneraa534bf2001-09-07 16:35:17 +0000165 Types.clear();
Chris Lattner57698e22002-03-26 18:01:55 +0000166 CurrentFunction = 0;
Chris Lattner17f729e2001-07-15 06:35:53 +0000167 isDeclare = false;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000168 }
Chris Lattner113f4f42002-06-25 16:13:24 +0000169} CurMeth; // Info for the current function...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000170
Chris Lattner57698e22002-03-26 18:01:55 +0000171static bool inFunctionScope() { return CurMeth.CurrentFunction != 0; }
Chris Lattnerfb3e5492001-10-03 15:39:04 +0000172
Chris Lattner2f7c9632001-06-06 20:29:01 +0000173
174//===----------------------------------------------------------------------===//
175// Code to handle definitions of all the types
176//===----------------------------------------------------------------------===//
177
Chris Lattnerf26e0d92001-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 Lattner2f7c9632001-06-06 20:29:01 +0000188}
189
Chris Lattneraa534bf2001-09-07 16:35:17 +0000190// TODO: FIXME when Type are not const
Chris Lattner30752bd92002-04-04 19:23:55 +0000191static void InsertType(const Type *Ty, vector<PATypeHolder> &Types) {
Chris Lattneraa534bf2001-09-07 16:35:17 +0000192 Types.push_back(Ty);
193}
194
195static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000196 switch (D.Type) {
Chris Lattner8eedb942002-07-18 05:18:37 +0000197 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattneraa534bf2001-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 Lattnerc27b1d72001-10-20 09:32:59 +0000209 break;
Chris Lattneraa534bf2001-09-07 16:35:17 +0000210 }
Chris Lattner8eedb942002-07-18 05:18:37 +0000211 case ValID::NameVal: { // Is it a named definition?
Chris Lattneraa534bf2001-09-07 16:35:17 +0000212 string Name(D.Name);
213 SymbolTable *SymTab = 0;
Chris Lattner98cf1f52002-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 Lattneraa534bf2001-09-07 16:35:17 +0000219
220 if (N == 0) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000221 // Symbol table doesn't automatically chain yet... because the function
Chris Lattneraa534bf2001-09-07 16:35:17 +0000222 // hasn't been added to the module...
223 //
Chris Lattner98cf1f52002-11-20 18:36:02 +0000224 SymTab = &CurModule.CurrentModule->getSymbolTable();
225 N = SymTab->lookup(Type::TypeTy, Name);
Chris Lattneraa534bf2001-09-07 16:35:17 +0000226 if (N == 0) break;
227 }
228
229 D.destroy(); // Free old strdup'd memory...
Chris Lattner8f191122001-10-01 18:26:53 +0000230 return cast<const Type>(N);
Chris Lattneraa534bf2001-09-07 16:35:17 +0000231 }
232 default:
Vikram S. Adve7064eaf2002-07-14 22:59:28 +0000233 ThrowException("Internal parser error: Invalid symbol type reference!");
Chris Lattneraa534bf2001-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 Lattner30752bd92002-04-04 19:23:55 +0000242 map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattnerd50fa5a2001-10-22 05:56:09 +0000243 CurMeth.LateResolveTypes : CurModule.LateResolveTypes;
244
Chris Lattner30752bd92002-04-04 19:23:55 +0000245 map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattnerd50fa5a2001-10-22 05:56:09 +0000246 if (I != LateResolver.end()) {
247 return I->second;
248 }
Chris Lattneraa534bf2001-09-07 16:35:17 +0000249
Chris Lattnere6b470c2001-10-22 06:01:08 +0000250 Type *Typ = OpaqueType::get();
Chris Lattnerd50fa5a2001-10-22 05:56:09 +0000251 LateResolver.insert(make_pair(D, Typ));
Chris Lattneraa534bf2001-09-07 16:35:17 +0000252 return Typ;
253}
254
Chris Lattner60e0dd72001-10-03 06:12:09 +0000255static Value *lookupInSymbolTable(const Type *Ty, const string &Name) {
Chris Lattner98cf1f52002-11-20 18:36:02 +0000256 SymbolTable &SymTab =
Chris Lattner18489fb62002-05-02 19:27:42 +0000257 inFunctionScope() ? CurMeth.CurrentFunction->getSymbolTable() :
258 CurModule.CurrentModule->getSymbolTable();
Chris Lattner98cf1f52002-11-20 18:36:02 +0000259 return SymTab.lookup(Ty, Name);
Chris Lattner60e0dd72001-10-03 06:12:09 +0000260}
261
Chris Lattnerf26e0d92001-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 Lattner57698e22002-03-26 18:01:55 +0000267 if (isa<FunctionType>(Ty))
268 ThrowException("Functions are not values and "
269 "must be referenced as pointers");
Chris Lattner322e49a2001-10-16 19:54:17 +0000270
Chris Lattneraa534bf2001-09-07 16:35:17 +0000271 switch (D.Type) {
Chris Lattnerfbdec252001-09-30 22:46:54 +0000272 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattneraa534bf2001-09-07 16:35:17 +0000273 unsigned type = Ty->getUniqueID();
Chris Lattner2f7c9632001-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 Lattnerf26e0d92001-10-13 06:41:08 +0000285 if (CurMeth.Values.size() <= type) return 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000286
287 // Check that the number is within bounds...
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000288 if (CurMeth.Values[type].size() <= Num) return 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000289
290 return CurMeth.Values[type][Num];
291 }
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000292
Chris Lattnerfbdec252001-09-30 22:46:54 +0000293 case ValID::NameVal: { // Is it a named definition?
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000294 Value *N = lookupInSymbolTable(Ty, string(D.Name));
295 if (N == 0) return 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000296
297 D.destroy(); // Free old strdup'd memory...
298 return N;
299 }
300
Chris Lattnerf26e0d92001-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 Lattner7f325cd2002-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 Lattnerf26e0d92001-10-13 06:41:08 +0000309
310 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattner3462ae32001-12-03 22:26:30 +0000311 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
312 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
Chris Lattner8eedb942002-07-18 05:18:37 +0000313 ThrowException("Integral constant '" + utostr(D.UConstPool64) +
314 "' is invalid or out of range!");
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000315 } else { // This is really a signed reference. Transmogrify.
Chris Lattner3462ae32001-12-03 22:26:30 +0000316 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000317 }
318 } else {
Chris Lattner3462ae32001-12-03 22:26:30 +0000319 return ConstantUInt::get(Ty, D.UConstPool64);
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000320 }
321
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000322 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattner3462ae32001-12-03 22:26:30 +0000323 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000324 ThrowException("FP constant invalid for type!!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000325 return ConstantFP::get(Ty, D.ConstPoolFP);
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000326
327 case ValID::ConstNullVal: // Is it a null value?
Chris Lattner181cc322002-05-06 16:15:30 +0000328 if (!isa<PointerType>(Ty))
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000329 ThrowException("Cannot create a a non pointer null!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000330 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000331
Chris Lattner7f325cd2002-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 Lattneraa534bf2001-09-07 16:35:17 +0000337 default:
338 assert(0 && "Unhandled case!");
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000339 return 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000340 } // End of switch
341
Chris Lattnerf26e0d92001-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 Lattner2f7c9632001-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 Lattner2f7c9632001-06-06 20:29:01 +0000364 Value *d = 0;
Chris Lattneraa534bf2001-09-07 16:35:17 +0000365 switch (Ty->getPrimitiveID()) {
366 case Type::LabelTyID: d = new BBPlaceHolder(Ty, D); break;
Chris Lattneraa534bf2001-09-07 16:35:17 +0000367 default: d = new ValuePlaceHolder(Ty, D); break;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000368 }
369
370 assert(d != 0 && "How did we not make something?");
Chris Lattner57698e22002-03-26 18:01:55 +0000371 if (inFunctionScope())
Chris Lattner322e49a2001-10-16 19:54:17 +0000372 InsertValue(d, CurMeth.LateResolveValues);
373 else
374 InsertValue(d, CurModule.LateResolveValues);
Chris Lattner2f7c9632001-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 Lattner322e49a2001-10-16 19:54:17 +0000395static void ResolveDefinitions(vector<ValueList> &LateResolvers,
Chris Lattnere6801392002-07-25 06:17:42 +0000396 vector<ValueList> *FutureLateResolvers) {
Chris Lattner2f7c9632001-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 Lattner322e49a2001-10-16 19:54:17 +0000401 assert(!isa<Type>(V) && "Types should be in LateResolveTypes!");
402
Chris Lattner2f7c9632001-06-06 20:29:01 +0000403 LateResolvers[ty].pop_back();
404 ValID &DID = getValIDFromPlaceHolder(V);
405
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000406 Value *TheRealValue = getValNonImprovising(Type::getUniqueIDType(ty),DID);
Chris Lattner322e49a2001-10-16 19:54:17 +0000407 if (TheRealValue) {
408 V->replaceAllUsesWith(TheRealValue);
409 delete V;
410 } else if (FutureLateResolvers) {
Chris Lattner57698e22002-03-26 18:01:55 +0000411 // Functions have their unresolved items forwarded to the module late
Chris Lattner322e49a2001-10-16 19:54:17 +0000412 // resolver table
413 InsertValue(V, *FutureLateResolvers);
414 } else {
Chris Lattner18489fb62002-05-02 19:27:42 +0000415 if (DID.Type == ValID::NameVal)
Chris Lattneraa534bf2001-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 Lattner2f7c9632001-06-06 20:29:01 +0000425 }
426 }
427
428 LateResolvers.clear();
429}
430
Chris Lattnerd50fa5a2001-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 Lattner2f7c9632001-06-06 20:29:01 +0000434//
Chris Lattnerd50fa5a2001-10-22 05:56:09 +0000435static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner30752bd92002-04-04 19:23:55 +0000436 vector<PATypeHolder> &Types = inFunctionScope() ?
Chris Lattnerd50fa5a2001-10-22 05:56:09 +0000437 CurMeth.Types : CurModule.Types;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000438
Chris Lattnerd50fa5a2001-10-22 05:56:09 +0000439 ValID D;
440 if (Name) D = ValID::create(Name);
441 else D = ValID::create((int)Types.size());
Chris Lattneraa534bf2001-09-07 16:35:17 +0000442
Chris Lattner30752bd92002-04-04 19:23:55 +0000443 map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattnerd50fa5a2001-10-22 05:56:09 +0000444 CurMeth.LateResolveTypes : CurModule.LateResolveTypes;
445
Chris Lattner30752bd92002-04-04 19:23:55 +0000446 map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattnerd50fa5a2001-10-22 05:56:09 +0000447 if (I != LateResolver.end()) {
Chris Lattner8cb1dfb2002-06-04 21:58:56 +0000448 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattnerd50fa5a2001-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 Lattner30752bd92002-04-04 19:23:55 +0000456static void ResolveTypes(map<ValID, PATypeHolder> &LateResolveTypes) {
Chris Lattnerd50fa5a2001-10-22 05:56:09 +0000457 if (!LateResolveTypes.empty()) {
Chris Lattnere6b470c2001-10-22 06:01:08 +0000458 const ValID &DID = LateResolveTypes.begin()->first;
Chris Lattnerd50fa5a2001-10-22 05:56:09 +0000459
460 if (DID.Type == ValID::NameVal)
Chris Lattnere6b470c2001-10-22 06:01:08 +0000461 ThrowException("Reference to an invalid type: '" +DID.getName() + "'");
Chris Lattnerd50fa5a2001-10-22 05:56:09 +0000462 else
Chris Lattnere6b470c2001-10-22 06:01:08 +0000463 ThrowException("Reference to an invalid type: #" + itostr(DID.Num));
Chris Lattneraa534bf2001-09-07 16:35:17 +0000464 }
465}
466
Chris Lattner571a6b02001-10-03 01:49:25 +0000467
Chris Lattner7fb14f52001-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 Lattnerfb3e5492001-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 Lattner322e49a2001-10-16 19:54:17 +0000478
Chris Lattner7fb14f52001-09-18 04:00:54 +0000479 string Name(NameStr); // Copy string
480 free(NameStr); // Free old string
481
Chris Lattnerf26e0d92001-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 Lattner98cf1f52002-11-20 18:36:02 +0000486 SymbolTable &ST = inFunctionScope() ?
487 CurMeth.CurrentFunction->getSymbolTable() :
488 CurModule.CurrentModule->getSymbolTable();
Chris Lattneraa534bf2001-09-07 16:35:17 +0000489
Chris Lattner98cf1f52002-11-20 18:36:02 +0000490 Value *Existing = ST.lookup(V->getType(), Name);
Chris Lattneraa534bf2001-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 Lattner571a6b02001-10-03 01:49:25 +0000494 if (const Type *Ty = dyn_cast<const Type>(Existing)) {
Chris Lattner8cb1dfb2002-06-04 21:58:56 +0000495 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Ty)) {
Chris Lattneraa534bf2001-09-07 16:35:17 +0000496 // We ARE replacing an opaque type!
Chris Lattner8cb1dfb2002-06-04 21:58:56 +0000497 ((OpaqueType*)OpTy)->refineAbstractTypeTo(cast<Type>(V));
Chris Lattnerfb3e5492001-10-03 15:39:04 +0000498 return true;
Chris Lattneraa534bf2001-09-07 16:35:17 +0000499 }
Chris Lattner571a6b02001-10-03 01:49:25 +0000500 }
Chris Lattneraa534bf2001-09-07 16:35:17 +0000501
Chris Lattner4b717c02001-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 Lattner8ce4e792003-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 Lattnerdb3b2022002-08-14 17:12:33 +0000506 // std::cerr << "Type: " << Ty->getDescription() << " != "
Chris Lattnerfb3e5492001-10-03 15:39:04 +0000507 // << cast<const Type>(V)->getDescription() << "!\n";
Chris Lattner8ce4e792003-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 Lattnerfb3e5492001-10-03 15:39:04 +0000510 } else if (GlobalVariable *EGV = dyn_cast<GlobalVariable>(Existing)) {
Chris Lattner78326d22001-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 Lattnere504b432001-10-03 19:35:04 +0000515 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
Chris Lattnerb90181e2003-02-02 16:40:20 +0000516 if (!EGV->hasInitializer() || !GV->hasInitializer() ||
517 EGV->getInitializer() == GV->getInitializer()) {
Chris Lattnerfb3e5492001-10-03 15:39:04 +0000518
Chris Lattnerb90181e2003-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 Lattnere504b432001-10-03 19:35:04 +0000521 if (GV->hasInitializer() && !EGV->hasInitializer())
522 EGV->setInitializer(GV->getInitializer());
Chris Lattnerb90181e2003-02-02 16:40:20 +0000523 if (GV->isConstant())
524 EGV->setConstant(true);
Chris Lattnere504b432001-10-03 19:35:04 +0000525
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000526 delete GV; // Destroy the duplicate!
Chris Lattnere504b432001-10-03 19:35:04 +0000527 return true; // They are equivalent!
528 }
Chris Lattnerfb3e5492001-10-03 15:39:04 +0000529 }
Chris Lattner4b717c02001-10-01 16:18:37 +0000530 }
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000531 ThrowException("Redefinition of value named '" + Name + "' in the '" +
Chris Lattneraa534bf2001-09-07 16:35:17 +0000532 V->getType()->getDescription() + "' type plane!");
Chris Lattner26e50dc2001-07-28 17:48:55 +0000533 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000534
Chris Lattner98cf1f52002-11-20 18:36:02 +0000535 V->setName(Name, &ST);
Chris Lattnerfb3e5492001-10-03 15:39:04 +0000536 return false;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000537}
538
Chris Lattnerd9c40e32001-07-09 19:38:36 +0000539
Chris Lattneraa534bf2001-09-07 16:35:17 +0000540//===----------------------------------------------------------------------===//
541// Code for handling upreferences in type names...
Chris Lattnerd9c40e32001-07-09 19:38:36 +0000542//
Chris Lattnerd9c40e32001-07-09 19:38:36 +0000543
Chris Lattneraa534bf2001-09-07 16:35:17 +0000544// TypeContains - Returns true if Ty contains E in it.
545//
546static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattnerbb09a102001-09-28 22:56:31 +0000547 return find(df_begin(Ty), df_end(Ty), E) != df_end(Ty);
Chris Lattneraa534bf2001-09-07 16:35:17 +0000548}
Chris Lattner1caf0bb2001-07-20 19:15:08 +0000549
Chris Lattneraa534bf2001-09-07 16:35:17 +0000550
551static vector<pair<unsigned, OpaqueType *> > UpRefs;
552
Chris Lattner30752bd92002-04-04 19:23:55 +0000553static PATypeHolder HandleUpRefs(const Type *ty) {
554 PATypeHolder Ty(ty);
Chris Lattnerf29b2312001-11-02 07:46:26 +0000555 UR_OUT("Type '" << ty->getDescription() <<
556 "' newly formed. Resolving upreferences.\n" <<
557 UpRefs.size() << " upreferences active!\n");
Chris Lattneraa534bf2001-09-07 16:35:17 +0000558 for (unsigned i = 0; i < UpRefs.size(); ) {
Chris Lattnerf29b2312001-11-02 07:46:26 +0000559 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Chris Lattneraa534bf2001-09-07 16:35:17 +0000560 << UpRefs[i].second->getDescription() << ") = "
Chris Lattnerf29b2312001-11-02 07:46:26 +0000561 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << endl);
Chris Lattneraa534bf2001-09-07 16:35:17 +0000562 if (TypeContains(Ty, UpRefs[i].second)) {
563 unsigned Level = --UpRefs[i].first; // Decrement level of upreference
Chris Lattnerf29b2312001-11-02 07:46:26 +0000564 UR_OUT(" Uplevel Ref Level = " << Level << endl);
Chris Lattneraa534bf2001-09-07 16:35:17 +0000565 if (Level == 0) { // Upreference should be resolved!
Chris Lattnerf29b2312001-11-02 07:46:26 +0000566 UR_OUT(" * Resolving upreference for "
567 << UpRefs[i].second->getDescription() << endl;
Chris Lattneraa534bf2001-09-07 16:35:17 +0000568 string OldName = UpRefs[i].second->getDescription());
569 UpRefs[i].second->refineAbstractTypeTo(Ty);
570 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
Chris Lattnerf29b2312001-11-02 07:46:26 +0000571 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
Chris Lattneraa534bf2001-09-07 16:35:17 +0000572 << (const void*)Ty << ", " << Ty->getDescription() << endl);
573 continue;
574 }
575 }
576
577 ++i; // Otherwise, no resolve, move on...
Chris Lattnerd9c40e32001-07-09 19:38:36 +0000578 }
Chris Lattneraa534bf2001-09-07 16:35:17 +0000579 // FIXME: TODO: this should return the updated type
Chris Lattnerd9c40e32001-07-09 19:38:36 +0000580 return Ty;
581}
582
Chris Lattneraa534bf2001-09-07 16:35:17 +0000583
Chris Lattner2f7c9632001-06-06 20:29:01 +0000584//===----------------------------------------------------------------------===//
585// RunVMAsmParser - Define an interface to this parser
586//===----------------------------------------------------------------------===//
587//
Chris Lattnerf2d1e792001-07-22 18:36:00 +0000588Module *RunVMAsmParser(const string &Filename, FILE *F) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000589 llvmAsmin = F;
Chris Lattnerf2d1e792001-07-22 18:36:00 +0000590 CurFilename = Filename;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000591 llvmAsmlineno = 1; // Reset the current line number...
592
593 CurModule.CurrentModule = new Module(); // Allocate a new module to read
594 yyparse(); // Parse the file.
595 Module *Result = ParserResult;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000596 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
597 ParserResult = 0;
598
599 return Result;
600}
601
602%}
603
604%union {
Chris Lattneraa534bf2001-09-07 16:35:17 +0000605 Module *ModuleVal;
Chris Lattner57698e22002-03-26 18:01:55 +0000606 Function *FunctionVal;
Chris Lattner149376d2002-10-13 20:57:00 +0000607 std::pair<PATypeHolder*, char*> *ArgVal;
Chris Lattneraa534bf2001-09-07 16:35:17 +0000608 BasicBlock *BasicBlockVal;
609 TerminatorInst *TermInstVal;
610 Instruction *InstVal;
Chris Lattner3462ae32001-12-03 22:26:30 +0000611 Constant *ConstVal;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000612
Chris Lattneraa534bf2001-09-07 16:35:17 +0000613 const Type *PrimType;
Chris Lattner30752bd92002-04-04 19:23:55 +0000614 PATypeHolder *TypeVal;
Chris Lattneraa534bf2001-09-07 16:35:17 +0000615 Value *ValueVal;
616
Chris Lattner149376d2002-10-13 20:57:00 +0000617 std::vector<std::pair<PATypeHolder*,char*> > *ArgList;
Chris Lattner7f74a562002-01-20 22:54:45 +0000618 std::vector<Value*> *ValueList;
Chris Lattner30752bd92002-04-04 19:23:55 +0000619 std::list<PATypeHolder> *TypeList;
Chris Lattner7f74a562002-01-20 22:54:45 +0000620 std::list<std::pair<Value*,
621 BasicBlock*> > *PHIList; // Represent the RHS of PHI node
Chris Lattnerae234e12002-04-09 19:41:42 +0000622 std::vector<std::pair<Constant*, BasicBlock*> > *JumpTable;
Chris Lattner7f74a562002-01-20 22:54:45 +0000623 std::vector<Constant*> *ConstVector;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000624
Chris Lattneraa534bf2001-09-07 16:35:17 +0000625 int64_t SInt64Val;
626 uint64_t UInt64Val;
627 int SIntVal;
628 unsigned UIntVal;
629 double FPVal;
Chris Lattner7fb14f52001-09-18 04:00:54 +0000630 bool BoolVal;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000631
Chris Lattneraa534bf2001-09-07 16:35:17 +0000632 char *StrVal; // This memory is strdup'd!
633 ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000634
Chris Lattneraa534bf2001-09-07 16:35:17 +0000635 Instruction::BinaryOps BinaryOpVal;
636 Instruction::TermOps TermOpVal;
637 Instruction::MemoryOps MemOpVal;
638 Instruction::OtherOps OtherOpVal;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000639}
640
Chris Lattner57698e22002-03-26 18:01:55 +0000641%type <ModuleVal> Module FunctionList
642%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner2f7c9632001-06-06 20:29:01 +0000643%type <BasicBlockVal> BasicBlock InstructionList
644%type <TermInstVal> BBTerminatorInst
645%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adve7064eaf2002-07-14 22:59:28 +0000646%type <ConstVal> ConstVal ConstExpr
Chris Lattner476148f2001-11-26 16:54:11 +0000647%type <ConstVector> ConstVector
Chris Lattnerae234e12002-04-09 19:41:42 +0000648%type <ArgList> ArgList ArgListH
649%type <ArgVal> ArgVal
Chris Lattner931ef3b2001-06-11 15:04:20 +0000650%type <PHIList> PHIList
Chris Lattner62ecb4a2001-07-08 23:22:50 +0000651%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner476148f2001-11-26 16:54:11 +0000652%type <ValueList> IndexList // For GEP derived indices
Chris Lattneraa534bf2001-09-07 16:35:17 +0000653%type <TypeList> TypeListI ArgTypeListI
Chris Lattner2f7c9632001-06-06 20:29:01 +0000654%type <JumpTable> JumpTable
Chris Lattner841d8b92001-11-26 18:54:16 +0000655%type <BoolVal> GlobalType OptInternal // GLOBAL or CONSTANT? Intern?
Chris Lattner2f7c9632001-06-06 20:29:01 +0000656
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000657// ValueRef - Unresolved reference to a definition or BB
658%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattner252afba2001-07-26 16:29:15 +0000659%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner2f7c9632001-06-06 20:29:01 +0000660// Tokens and types for handling constant integer values
661//
662// ESINT64VAL - A negative number within long long range
663%token <SInt64Val> ESINT64VAL
664
665// EUINT64VAL - A positive number within uns. long long range
666%token <UInt64Val> EUINT64VAL
667%type <SInt64Val> EINT64VAL
668
669%token <SIntVal> SINTVAL // Signed 32 bit ints...
670%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
671%type <SIntVal> INTVAL
Chris Lattner212f70d2001-07-15 00:17:01 +0000672%token <FPVal> FPVAL // Float or Double constant
Chris Lattner2f7c9632001-06-06 20:29:01 +0000673
674// Built in types...
Chris Lattneraa534bf2001-09-07 16:35:17 +0000675%type <TypeVal> Types TypesV UpRTypes UpRTypesV
676%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattneraa534bf2001-09-07 16:35:17 +0000677%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
678%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner2f7c9632001-06-06 20:29:01 +0000679
680%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
Chris Lattnerf4c7dd92002-05-22 22:33:00 +0000681%type <StrVal> OptVAR_ID OptAssign FuncName
Chris Lattner2f7c9632001-06-06 20:29:01 +0000682
683
Chris Lattnere2dd92b2002-10-06 22:45:09 +0000684%token IMPLEMENTATION TRUE FALSE BEGINTOK ENDTOK DECLARE GLOBAL CONSTANT
685%token TO EXCEPT DOTDOTDOT NULL_TOK CONST INTERNAL OPAQUE NOT EXTERNAL
Chris Lattner2f7c9632001-06-06 20:29:01 +0000686
687// Basic Block Terminating Operators
688%token <TermOpVal> RET BR SWITCH
689
Chris Lattner2f7c9632001-06-06 20:29:01 +0000690// Binary Operators
691%type <BinaryOpVal> BinaryOps // all the binary operators
Chris Lattnerc0686fe2002-09-10 19:57:26 +0000692%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Chris Lattnerc27b1d72001-10-20 09:32:59 +0000693%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattnerd8bebcd2001-07-08 21:10:27 +0000694%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner2f7c9632001-06-06 20:29:01 +0000695
696// Memory Instructions
Vikram S. Adve7064eaf2002-07-14 22:59:28 +0000697%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner2f7c9632001-06-06 20:29:01 +0000698
Chris Lattnerd8bebcd2001-07-08 21:10:27 +0000699// Other Operators
700%type <OtherOpVal> ShiftOps
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000701%token <OtherOpVal> PHI CALL INVOKE CAST SHL SHR
Chris Lattnerd8bebcd2001-07-08 21:10:27 +0000702
Chris Lattner2f7c9632001-06-06 20:29:01 +0000703%start Module
704%%
705
706// Handle constant integer size restriction and conversion...
707//
708
Chris Lattner8cb1dfb2002-06-04 21:58:56 +0000709INTVAL : SINTVAL;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000710INTVAL : UINTVAL {
711 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
712 ThrowException("Value too large for type!");
713 $$ = (int32_t)$1;
Chris Lattner8cb1dfb2002-06-04 21:58:56 +0000714};
Chris Lattner2f7c9632001-06-06 20:29:01 +0000715
716
Chris Lattner8cb1dfb2002-06-04 21:58:56 +0000717EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000718EINT64VAL : EUINT64VAL {
719 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
720 ThrowException("Value too large for type!");
721 $$ = (int64_t)$1;
Chris Lattner8cb1dfb2002-06-04 21:58:56 +0000722};
Chris Lattner2f7c9632001-06-06 20:29:01 +0000723
Chris Lattner2f7c9632001-06-06 20:29:01 +0000724// Operations that are notably excluded from this list include:
725// RET, BR, & SWITCH because they end basic blocks and are treated specially.
726//
Chris Lattnerc0686fe2002-09-10 19:57:26 +0000727ArithmeticOps: ADD | SUB | MUL | DIV | REM;
728LogicalOps : AND | OR | XOR;
729SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
730BinaryOps : ArithmeticOps | LogicalOps | SetCondOps;
731
Chris Lattner8cb1dfb2002-06-04 21:58:56 +0000732ShiftOps : SHL | SHR;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000733
Chris Lattner56f73d42001-07-14 06:10:16 +0000734// These are some types that allow classification if we only want a particular
735// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner8cb1dfb2002-06-04 21:58:56 +0000736SIntType : LONG | INT | SHORT | SBYTE;
737UIntType : ULONG | UINT | USHORT | UBYTE;
738IntType : SIntType | UIntType;
739FPType : FLOAT | DOUBLE;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000740
Chris Lattner56f73d42001-07-14 06:10:16 +0000741// OptAssign - Value producing statements have an optional assignment component
Chris Lattner2f7c9632001-06-06 20:29:01 +0000742OptAssign : VAR_ID '=' {
743 $$ = $1;
744 }
745 | /*empty*/ {
746 $$ = 0;
Chris Lattner8cb1dfb2002-06-04 21:58:56 +0000747 };
Chris Lattner2f7c9632001-06-06 20:29:01 +0000748
Chris Lattner8cb1dfb2002-06-04 21:58:56 +0000749OptInternal : INTERNAL { $$ = true; } | /*empty*/ { $$ = false; };
Chris Lattneraa534bf2001-09-07 16:35:17 +0000750
751//===----------------------------------------------------------------------===//
752// Types includes all predefined types... except void, because it can only be
Chris Lattner113f4f42002-06-25 16:13:24 +0000753// used in specific contexts (function returning void for example). To have
Chris Lattneraa534bf2001-09-07 16:35:17 +0000754// access to it, a user must explicitly use TypesV.
755//
756
757// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner8cb1dfb2002-06-04 21:58:56 +0000758TypesV : Types | VOID { $$ = new PATypeHolder($1); };
759UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattneraa534bf2001-09-07 16:35:17 +0000760
761Types : UpRTypes {
Chris Lattner30752bd92002-04-04 19:23:55 +0000762 if (UpRefs.size())
763 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
764 $$ = $1;
Chris Lattner8cb1dfb2002-06-04 21:58:56 +0000765 };
Chris Lattneraa534bf2001-09-07 16:35:17 +0000766
767
768// Derived types are added later...
769//
Chris Lattner8cb1dfb2002-06-04 21:58:56 +0000770PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
771PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner30752bd92002-04-04 19:23:55 +0000772UpRTypes : OPAQUE {
773 $$ = new PATypeHolder(OpaqueType::get());
774 }
775 | PrimType {
776 $$ = new PATypeHolder($1);
Chris Lattner8cb1dfb2002-06-04 21:58:56 +0000777 };
Chris Lattner7f325cd2002-08-16 21:14:40 +0000778UpRTypes : SymbolicValueRef { // Named types are also simple types...
Chris Lattner30752bd92002-04-04 19:23:55 +0000779 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner8cb1dfb2002-06-04 21:58:56 +0000780};
Chris Lattneraa534bf2001-09-07 16:35:17 +0000781
Chris Lattneraa534bf2001-09-07 16:35:17 +0000782// Include derived types in the Types production.
783//
784UpRTypes : '\\' EUINT64VAL { // Type UpReference
785 if ($2 > (uint64_t)INT64_MAX) ThrowException("Value out of range!");
786 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
787 UpRefs.push_back(make_pair((unsigned)$2, OT)); // Add to vector...
Chris Lattner30752bd92002-04-04 19:23:55 +0000788 $$ = new PATypeHolder(OT);
Chris Lattneraa534bf2001-09-07 16:35:17 +0000789 UR_OUT("New Upreference!\n");
790 }
Chris Lattner57698e22002-03-26 18:01:55 +0000791 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattneraa534bf2001-09-07 16:35:17 +0000792 vector<const Type*> Params;
Chris Lattner7f74a562002-01-20 22:54:45 +0000793 mapto($3->begin(), $3->end(), std::back_inserter(Params),
794 std::mem_fun_ref(&PATypeHandle<Type>::get));
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000795 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
796 if (isVarArg) Params.pop_back();
797
Chris Lattner30752bd92002-04-04 19:23:55 +0000798 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattneraa534bf2001-09-07 16:35:17 +0000799 delete $3; // Delete the argument list
800 delete $1; // Delete the old type handle
801 }
Chris Lattnere63ddd8df2001-10-03 03:19:33 +0000802 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner30752bd92002-04-04 19:23:55 +0000803 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnere63ddd8df2001-10-03 03:19:33 +0000804 delete $4;
Chris Lattneraa534bf2001-09-07 16:35:17 +0000805 }
Chris Lattnere63ddd8df2001-10-03 03:19:33 +0000806 | '{' TypeListI '}' { // Structure type?
807 vector<const Type*> Elements;
Chris Lattner7f74a562002-01-20 22:54:45 +0000808 mapto($2->begin(), $2->end(), std::back_inserter(Elements),
809 std::mem_fun_ref(&PATypeHandle<Type>::get));
Chris Lattneraa534bf2001-09-07 16:35:17 +0000810
Chris Lattner30752bd92002-04-04 19:23:55 +0000811 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnere63ddd8df2001-10-03 03:19:33 +0000812 delete $2;
813 }
814 | '{' '}' { // Empty structure type?
Chris Lattner30752bd92002-04-04 19:23:55 +0000815 $$ = new PATypeHolder(StructType::get(vector<const Type*>()));
Chris Lattnere63ddd8df2001-10-03 03:19:33 +0000816 }
817 | UpRTypes '*' { // Pointer type?
Chris Lattner30752bd92002-04-04 19:23:55 +0000818 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnere63ddd8df2001-10-03 03:19:33 +0000819 delete $1;
Chris Lattner8cb1dfb2002-06-04 21:58:56 +0000820 };
Chris Lattneraa534bf2001-09-07 16:35:17 +0000821
Chris Lattner113f4f42002-06-25 16:13:24 +0000822// TypeList - Used for struct declarations and as a basis for function type
Chris Lattneraa534bf2001-09-07 16:35:17 +0000823// declaration type lists
824//
825TypeListI : UpRTypes {
Chris Lattner30752bd92002-04-04 19:23:55 +0000826 $$ = new list<PATypeHolder>();
Chris Lattneraa534bf2001-09-07 16:35:17 +0000827 $$->push_back(*$1); delete $1;
828 }
829 | TypeListI ',' UpRTypes {
830 ($$=$1)->push_back(*$3); delete $3;
Chris Lattner8cb1dfb2002-06-04 21:58:56 +0000831 };
Chris Lattneraa534bf2001-09-07 16:35:17 +0000832
Chris Lattner113f4f42002-06-25 16:13:24 +0000833// ArgTypeList - List of types for a function type declaration...
Chris Lattneraa534bf2001-09-07 16:35:17 +0000834ArgTypeListI : TypeListI
835 | TypeListI ',' DOTDOTDOT {
836 ($$=$1)->push_back(Type::VoidTy);
837 }
838 | DOTDOTDOT {
Chris Lattner30752bd92002-04-04 19:23:55 +0000839 ($$ = new list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattneraa534bf2001-09-07 16:35:17 +0000840 }
841 | /*empty*/ {
Chris Lattner30752bd92002-04-04 19:23:55 +0000842 $$ = new list<PATypeHolder>();
Chris Lattner8cb1dfb2002-06-04 21:58:56 +0000843 };
Chris Lattneraa534bf2001-09-07 16:35:17 +0000844
Chris Lattner56f73d42001-07-14 06:10:16 +0000845// ConstVal - The various declarations that go into the constant pool. This
Chris Lattner7f325cd2002-08-16 21:14:40 +0000846// production is used ONLY to represent constants that show up AFTER a 'const',
847// 'constant' or 'global' token at global scope. Constants that can be inlined
848// into other expressions (such as integers and constexprs) are handled by the
849// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattner56f73d42001-07-14 06:10:16 +0000850//
Chris Lattnere63ddd8df2001-10-03 03:19:33 +0000851ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
852 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
853 if (ATy == 0)
854 ThrowException("Cannot make array constant with type: '" +
855 (*$1)->getDescription() + "'!");
Chris Lattneraa534bf2001-09-07 16:35:17 +0000856 const Type *ETy = ATy->getElementType();
857 int NumElements = ATy->getNumElements();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000858
Chris Lattneraa534bf2001-09-07 16:35:17 +0000859 // Verify that we have the correct size...
860 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner2f7c9632001-06-06 20:29:01 +0000861 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattneraa534bf2001-09-07 16:35:17 +0000862 utostr($3->size()) + " arguments, but has size of " +
863 itostr(NumElements) + "!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000864
Chris Lattneraa534bf2001-09-07 16:35:17 +0000865 // Verify all elements are correct type!
866 for (unsigned i = 0; i < $3->size(); i++) {
867 if (ETy != (*$3)[i]->getType())
Chris Lattner2f7c9632001-06-06 20:29:01 +0000868 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattneref99d3c2001-12-14 16:28:42 +0000869 ETy->getDescription() +"' as required!\nIt is of type '"+
870 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000871 }
872
Chris Lattner3462ae32001-12-03 22:26:30 +0000873 $$ = ConstantArray::get(ATy, *$3);
Chris Lattneraa534bf2001-09-07 16:35:17 +0000874 delete $1; delete $3;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000875 }
Chris Lattnere63ddd8df2001-10-03 03:19:33 +0000876 | Types '[' ']' {
877 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
878 if (ATy == 0)
879 ThrowException("Cannot make array constant with type: '" +
880 (*$1)->getDescription() + "'!");
881
882 int NumElements = ATy->getNumElements();
Chris Lattneraa534bf2001-09-07 16:35:17 +0000883 if (NumElements != -1 && NumElements != 0)
Chris Lattner2f7c9632001-06-06 20:29:01 +0000884 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattneraa534bf2001-09-07 16:35:17 +0000885 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000886 $$ = ConstantArray::get(ATy, vector<Constant*>());
Chris Lattneraa534bf2001-09-07 16:35:17 +0000887 delete $1;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000888 }
Chris Lattnere63ddd8df2001-10-03 03:19:33 +0000889 | Types 'c' STRINGCONSTANT {
890 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
891 if (ATy == 0)
892 ThrowException("Cannot make array constant with type: '" +
893 (*$1)->getDescription() + "'!");
894
Chris Lattneraa534bf2001-09-07 16:35:17 +0000895 int NumElements = ATy->getNumElements();
896 const Type *ETy = ATy->getElementType();
897 char *EndStr = UnEscapeLexed($3, true);
898 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner26e50dc2001-07-28 17:48:55 +0000899 ThrowException("Can't build string constant of size " +
Chris Lattneraa534bf2001-09-07 16:35:17 +0000900 itostr((int)(EndStr-$3)) +
901 " when array has size " + itostr(NumElements) + "!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000902 vector<Constant*> Vals;
Chris Lattneraa534bf2001-09-07 16:35:17 +0000903 if (ETy == Type::SByteTy) {
904 for (char *C = $3; C != EndStr; ++C)
Chris Lattner3462ae32001-12-03 22:26:30 +0000905 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattneraa534bf2001-09-07 16:35:17 +0000906 } else if (ETy == Type::UByteTy) {
907 for (char *C = $3; C != EndStr; ++C)
Chris Lattner63a25242003-01-30 22:24:26 +0000908 Vals.push_back(ConstantUInt::get(ETy, (unsigned char)*C));
Chris Lattner26e50dc2001-07-28 17:48:55 +0000909 } else {
Chris Lattneraa534bf2001-09-07 16:35:17 +0000910 free($3);
Chris Lattner26e50dc2001-07-28 17:48:55 +0000911 ThrowException("Cannot build string arrays of non byte sized elements!");
912 }
Chris Lattneraa534bf2001-09-07 16:35:17 +0000913 free($3);
Chris Lattner3462ae32001-12-03 22:26:30 +0000914 $$ = ConstantArray::get(ATy, Vals);
Chris Lattneraa534bf2001-09-07 16:35:17 +0000915 delete $1;
Chris Lattner26e50dc2001-07-28 17:48:55 +0000916 }
Chris Lattnere63ddd8df2001-10-03 03:19:33 +0000917 | Types '{' ConstVector '}' {
918 const StructType *STy = dyn_cast<const StructType>($1->get());
919 if (STy == 0)
920 ThrowException("Cannot make struct constant with type: '" +
921 (*$1)->getDescription() + "'!");
Chris Lattneraa534bf2001-09-07 16:35:17 +0000922 // FIXME: TODO: Check to see that the constants are compatible with the type
923 // initializer!
Chris Lattner3462ae32001-12-03 22:26:30 +0000924 $$ = ConstantStruct::get(STy, *$3);
Chris Lattneraa534bf2001-09-07 16:35:17 +0000925 delete $1; delete $3;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000926 }
Chris Lattnere63ddd8df2001-10-03 03:19:33 +0000927 | Types NULL_TOK {
928 const PointerType *PTy = dyn_cast<const PointerType>($1->get());
929 if (PTy == 0)
930 ThrowException("Cannot make null pointer constant with type: '" +
931 (*$1)->getDescription() + "'!");
932
Chris Lattner3462ae32001-12-03 22:26:30 +0000933 $$ = ConstantPointerNull::get(PTy);
Chris Lattner571a6b02001-10-03 01:49:25 +0000934 delete $1;
935 }
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000936 | Types SymbolicValueRef {
Chris Lattner60e0dd72001-10-03 06:12:09 +0000937 const PointerType *Ty = dyn_cast<const PointerType>($1->get());
938 if (Ty == 0)
939 ThrowException("Global const reference must be a pointer type!");
940
Chris Lattner61643a02002-08-15 17:58:33 +0000941 // ConstExprs can exist in the body of a function, thus creating
942 // ConstantPointerRefs whenever they refer to a variable. Because we are in
943 // the context of a function, getValNonImprovising will search the functions
944 // symbol table instead of the module symbol table for the global symbol,
945 // which throws things all off. To get around this, we just tell
946 // getValNonImprovising that we are at global scope here.
947 //
948 Function *SavedCurFn = CurMeth.CurrentFunction;
949 CurMeth.CurrentFunction = 0;
950
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000951 Value *V = getValNonImprovising(Ty, $2);
Chris Lattner60e0dd72001-10-03 06:12:09 +0000952
Chris Lattner61643a02002-08-15 17:58:33 +0000953 CurMeth.CurrentFunction = SavedCurFn;
954
955
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000956 // If this is an initializer for a constant pointer, which is referencing a
957 // (currently) undefined variable, create a stub now that shall be replaced
958 // in the future with the right type of variable.
959 //
960 if (V == 0) {
961 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
962 const PointerType *PT = cast<PointerType>(Ty);
963
964 // First check to see if the forward references value is already created!
965 PerModuleInfo::GlobalRefsType::iterator I =
966 CurModule.GlobalRefs.find(make_pair(PT, $2));
967
968 if (I != CurModule.GlobalRefs.end()) {
969 V = I->second; // Placeholder already exists, use it...
970 } else {
971 // TODO: Include line number info by creating a subclass of
972 // TODO: GlobalVariable here that includes the said information!
973
974 // Create a placeholder for the global variable reference...
Chris Lattner2413b162001-12-04 00:03:30 +0000975 GlobalVariable *GV = new GlobalVariable(PT->getElementType(),
976 false, true);
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000977 // Keep track of the fact that we have a forward ref to recycle it
978 CurModule.GlobalRefs.insert(make_pair(make_pair(PT, $2), GV));
979
980 // Must temporarily push this value into the module table...
981 CurModule.CurrentModule->getGlobalList().push_back(GV);
982 V = GV;
983 }
Chris Lattner60e0dd72001-10-03 06:12:09 +0000984 }
985
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000986 GlobalValue *GV = cast<GlobalValue>(V);
Chris Lattner3462ae32001-12-03 22:26:30 +0000987 $$ = ConstantPointerRef::get(GV);
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000988 delete $1; // Free the type handle
Vikram S. Adve7064eaf2002-07-14 22:59:28 +0000989 }
Chris Lattner7f325cd2002-08-16 21:14:40 +0000990 | Types ConstExpr {
991 if ($1->get() != $2->getType())
992 ThrowException("Mismatched types for constant expression!");
993 $$ = $2;
994 delete $1;
Chris Lattner8cb1dfb2002-06-04 21:58:56 +0000995 };
Chris Lattner60e0dd72001-10-03 06:12:09 +0000996
Chris Lattnerfa01a522002-10-09 00:25:32 +0000997ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattner7f1c98d2002-08-15 18:17:28 +0000998 if (!ConstantSInt::isValueValidForType($1, $2))
999 ThrowException("Constant value doesn't fit in type!");
1000 $$ = ConstantSInt::get($1, $2);
Chris Lattnerfa01a522002-10-09 00:25:32 +00001001 }
1002 | UIntType EUINT64VAL { // integral constants
Chris Lattner7f1c98d2002-08-15 18:17:28 +00001003 if (!ConstantUInt::isValueValidForType($1, $2))
1004 ThrowException("Constant value doesn't fit in type!");
1005 $$ = ConstantUInt::get($1, $2);
Chris Lattnerfa01a522002-10-09 00:25:32 +00001006 }
1007 | BOOL TRUE { // Boolean constants
Chris Lattner7f1c98d2002-08-15 18:17:28 +00001008 $$ = ConstantBool::True;
1009 }
Chris Lattnerfa01a522002-10-09 00:25:32 +00001010 | BOOL FALSE { // Boolean constants
Chris Lattner7f1c98d2002-08-15 18:17:28 +00001011 $$ = ConstantBool::False;
1012 }
1013 | FPType FPVAL { // Float & Double constants
1014 $$ = ConstantFP::get($1, $2);
1015 };
1016
Chris Lattner2f7c9632001-06-06 20:29:01 +00001017
Chris Lattner7f325cd2002-08-16 21:14:40 +00001018ConstExpr: CAST '(' ConstVal TO Types ')' {
Chris Lattnerd8ecff72002-08-15 19:37:11 +00001019 $$ = ConstantExpr::getCast($3, $5->get());
Chris Lattnerd8ecff72002-08-15 19:37:11 +00001020 delete $5;
Vikram S. Adve7064eaf2002-07-14 22:59:28 +00001021 }
Chris Lattner7f325cd2002-08-16 21:14:40 +00001022 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1023 if (!isa<PointerType>($3->getType()))
Chris Lattner3cd8c562002-07-30 18:54:25 +00001024 ThrowException("GetElementPtr requires a pointer operand!");
1025
1026 const Type *IdxTy =
Chris Lattner7f325cd2002-08-16 21:14:40 +00001027 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
Chris Lattner3cd8c562002-07-30 18:54:25 +00001028 if (!IdxTy)
1029 ThrowException("Index list invalid for constant getelementptr!");
Chris Lattner3cd8c562002-07-30 18:54:25 +00001030
Chris Lattner980ddf52002-07-18 00:14:27 +00001031 vector<Constant*> IdxVec;
Chris Lattner7f325cd2002-08-16 21:14:40 +00001032 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1033 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattner980ddf52002-07-18 00:14:27 +00001034 IdxVec.push_back(C);
1035 else
Chris Lattner3cd8c562002-07-30 18:54:25 +00001036 ThrowException("Indices to constant getelementptr must be constants!");
Chris Lattner980ddf52002-07-18 00:14:27 +00001037
Chris Lattner7f325cd2002-08-16 21:14:40 +00001038 delete $4;
Chris Lattner980ddf52002-07-18 00:14:27 +00001039
Chris Lattner7f325cd2002-08-16 21:14:40 +00001040 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Vikram S. Adve7064eaf2002-07-14 22:59:28 +00001041 }
Chris Lattner7f325cd2002-08-16 21:14:40 +00001042 | BinaryOps '(' ConstVal ',' ConstVal ')' {
Chris Lattner3cd8c562002-07-30 18:54:25 +00001043 if ($3->getType() != $5->getType())
1044 ThrowException("Binary operator types must match!");
Chris Lattner7f325cd2002-08-16 21:14:40 +00001045 $$ = ConstantExpr::get($1, $3, $5);
Vikram S. Adve7064eaf2002-07-14 22:59:28 +00001046 }
Chris Lattner7f325cd2002-08-16 21:14:40 +00001047 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Chris Lattner3cd8c562002-07-30 18:54:25 +00001048 if ($5->getType() != Type::UByteTy)
1049 ThrowException("Shift count for shift constant must be unsigned byte!");
Chris Lattner7f325cd2002-08-16 21:14:40 +00001050 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattnerdb3b2022002-08-14 17:12:33 +00001051 };
Vikram S. Adve7064eaf2002-07-14 22:59:28 +00001052
1053
Chris Lattner56f73d42001-07-14 06:10:16 +00001054// ConstVector - A list of comma seperated constants.
Chris Lattner2f7c9632001-06-06 20:29:01 +00001055ConstVector : ConstVector ',' ConstVal {
Chris Lattneraa534bf2001-09-07 16:35:17 +00001056 ($$ = $1)->push_back($3);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001057 }
1058 | ConstVal {
Chris Lattner3462ae32001-12-03 22:26:30 +00001059 $$ = new vector<Constant*>();
Chris Lattneraa534bf2001-09-07 16:35:17 +00001060 $$->push_back($1);
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001061 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00001062
Chris Lattner252afba2001-07-26 16:29:15 +00001063
Chris Lattner7fb14f52001-09-18 04:00:54 +00001064// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001065GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner7fb14f52001-09-18 04:00:54 +00001066
Chris Lattner2f7c9632001-06-06 20:29:01 +00001067
Chris Lattner7b804d62002-05-02 19:11:13 +00001068//===----------------------------------------------------------------------===//
1069// Rules to match Modules
1070//===----------------------------------------------------------------------===//
1071
1072// Module rule: Capture the result of parsing the whole file into a result
1073// variable...
1074//
1075Module : FunctionList {
1076 $$ = ParserResult = $1;
1077 CurModule.ModuleDone();
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001078};
Chris Lattner7b804d62002-05-02 19:11:13 +00001079
Chris Lattner113f4f42002-06-25 16:13:24 +00001080// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner7b804d62002-05-02 19:11:13 +00001081//
1082FunctionList : FunctionList Function {
1083 $$ = $1;
1084 assert($2->getParent() == 0 && "Function already in module!");
1085 $1->getFunctionList().push_back($2);
1086 CurMeth.FunctionDone();
1087 }
1088 | FunctionList FunctionProto {
1089 $$ = $1;
1090 }
1091 | FunctionList IMPLEMENTATION {
1092 $$ = $1;
1093 }
1094 | ConstPool {
1095 $$ = CurModule.CurrentModule;
1096 // Resolve circular types before we parse the body of the module
1097 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001098 };
Chris Lattner7b804d62002-05-02 19:11:13 +00001099
Chris Lattner56f73d42001-07-14 06:10:16 +00001100// ConstPool - Constants with optional names assigned to them.
Chris Lattner571a6b02001-10-03 01:49:25 +00001101ConstPool : ConstPool OptAssign CONST ConstVal {
Chris Lattner8ce4e792003-03-03 23:28:55 +00001102 if (!setValueName($4, $2))
1103 InsertValue($4);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001104 }
Chris Lattneraa534bf2001-09-07 16:35:17 +00001105 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattnerd50fa5a2001-10-22 05:56:09 +00001106 // Eagerly resolve types. This is not an optimization, this is a
1107 // requirement that is due to the fact that we could have this:
1108 //
1109 // %list = type { %list * }
1110 // %list = type { %list * } ; repeated type decl
1111 //
1112 // If types are not resolved eagerly, then the two types will not be
1113 // determined to be the same type!
1114 //
1115 ResolveTypeTo($2, $4->get());
1116
Chris Lattner7fb14f52001-09-18 04:00:54 +00001117 // TODO: FIXME when Type are not const
Chris Lattnerfb3e5492001-10-03 15:39:04 +00001118 if (!setValueName(const_cast<Type*>($4->get()), $2)) {
1119 // If this is not a redefinition of a type...
1120 if (!$2) {
1121 InsertType($4->get(),
Chris Lattner57698e22002-03-26 18:01:55 +00001122 inFunctionScope() ? CurMeth.Types : CurModule.Types);
Chris Lattnerfb3e5492001-10-03 15:39:04 +00001123 }
Chris Lattneraa534bf2001-09-07 16:35:17 +00001124 }
Chris Lattner6265d792001-10-21 23:02:41 +00001125
1126 delete $4;
Chris Lattneraa534bf2001-09-07 16:35:17 +00001127 }
Chris Lattner57698e22002-03-26 18:01:55 +00001128 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner26e50dc2001-07-28 17:48:55 +00001129 }
Chris Lattner841d8b92001-11-26 18:54:16 +00001130 | ConstPool OptAssign OptInternal GlobalType ConstVal {
1131 const Type *Ty = $5->getType();
Chris Lattner7fb14f52001-09-18 04:00:54 +00001132 // Global declarations appear in Constant Pool
Chris Lattner3462ae32001-12-03 22:26:30 +00001133 Constant *Initializer = $5;
Chris Lattner7fb14f52001-09-18 04:00:54 +00001134 if (Initializer == 0)
1135 ThrowException("Global value initializer is not a constant!");
Vikram S. Adve7064eaf2002-07-14 22:59:28 +00001136
Chris Lattner841d8b92001-11-26 18:54:16 +00001137 GlobalVariable *GV = new GlobalVariable(Ty, $4, $3, Initializer);
Chris Lattnerfb3e5492001-10-03 15:39:04 +00001138 if (!setValueName(GV, $2)) { // If not redefining...
1139 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001140 int Slot = InsertValue(GV, CurModule.Values);
1141
1142 if (Slot != -1) {
1143 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1144 } else {
1145 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1146 (char*)GV->getName().c_str()));
1147 }
Chris Lattnerfb3e5492001-10-03 15:39:04 +00001148 }
Chris Lattner7fb14f52001-09-18 04:00:54 +00001149 }
Chris Lattnere2dd92b2002-10-06 22:45:09 +00001150 | ConstPool OptAssign OptInternal EXTERNAL GlobalType Types {
Chris Lattner841d8b92001-11-26 18:54:16 +00001151 const Type *Ty = *$6;
Chris Lattner7fb14f52001-09-18 04:00:54 +00001152 // Global declarations appear in Constant Pool
Chris Lattner841d8b92001-11-26 18:54:16 +00001153 GlobalVariable *GV = new GlobalVariable(Ty, $5, $3);
Chris Lattnerfb3e5492001-10-03 15:39:04 +00001154 if (!setValueName(GV, $2)) { // If not redefining...
1155 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001156 int Slot = InsertValue(GV, CurModule.Values);
1157
1158 if (Slot != -1) {
1159 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1160 } else {
1161 assert(GV->hasName() && "Not named and not numbered!?");
1162 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1163 (char*)GV->getName().c_str()));
1164 }
Chris Lattnerfb3e5492001-10-03 15:39:04 +00001165 }
Chris Lattner041468f2002-03-31 07:16:49 +00001166 delete $6;
Chris Lattner56f73d42001-07-14 06:10:16 +00001167 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001168 | /* empty: end of list */ {
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001169 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00001170
1171
1172//===----------------------------------------------------------------------===//
Chris Lattner57698e22002-03-26 18:01:55 +00001173// Rules to match Function Headers
Chris Lattner2f7c9632001-06-06 20:29:01 +00001174//===----------------------------------------------------------------------===//
1175
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001176OptVAR_ID : VAR_ID | /*empty*/ { $$ = 0; };
Chris Lattner2f7c9632001-06-06 20:29:01 +00001177
1178ArgVal : Types OptVAR_ID {
Chris Lattner149376d2002-10-13 20:57:00 +00001179 if (*$1 == Type::VoidTy)
1180 ThrowException("void typed arguments are invalid!");
1181 $$ = new pair<PATypeHolder*, char*>($1, $2);
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001182};
Chris Lattner2f7c9632001-06-06 20:29:01 +00001183
Chris Lattner149376d2002-10-13 20:57:00 +00001184ArgListH : ArgListH ',' ArgVal {
1185 $$ = $1;
1186 $1->push_back(*$3);
1187 delete $3;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001188 }
1189 | ArgVal {
Chris Lattner149376d2002-10-13 20:57:00 +00001190 $$ = new vector<pair<PATypeHolder*,char*> >();
1191 $$->push_back(*$1);
Chris Lattner29f67092002-03-08 18:41:32 +00001192 delete $1;
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001193 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00001194
1195ArgList : ArgListH {
1196 $$ = $1;
1197 }
Chris Lattner149376d2002-10-13 20:57:00 +00001198 | ArgListH ',' DOTDOTDOT {
1199 $$ = $1;
1200 $$->push_back(pair<PATypeHolder*, char*>(new PATypeHolder(Type::VoidTy),0));
1201 }
1202 | DOTDOTDOT {
1203 $$ = new vector<pair<PATypeHolder*,char*> >();
1204 $$->push_back(pair<PATypeHolder*, char*>(new PATypeHolder(Type::VoidTy),0));
1205 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001206 | /* empty */ {
1207 $$ = 0;
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001208 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00001209
Chris Lattnerf4c7dd92002-05-22 22:33:00 +00001210FuncName : VAR_ID | STRINGCONSTANT;
1211
1212FunctionHeaderH : OptInternal TypesV FuncName '(' ArgList ')' {
Chris Lattner841d8b92001-11-26 18:54:16 +00001213 UnEscapeLexed($3);
Chris Lattner57698e22002-03-26 18:01:55 +00001214 string FunctionName($3);
Chris Lattner841d8b92001-11-26 18:54:16 +00001215
Chris Lattneraa534bf2001-09-07 16:35:17 +00001216 vector<const Type*> ParamTypeList;
Chris Lattner19613292002-10-15 21:41:14 +00001217 if ($5) { // If there are arguments...
Chris Lattner149376d2002-10-13 20:57:00 +00001218 for (vector<pair<PATypeHolder*,char*> >::iterator I = $5->begin();
Chris Lattner29f67092002-03-08 18:41:32 +00001219 I != $5->end(); ++I)
Chris Lattner149376d2002-10-13 20:57:00 +00001220 ParamTypeList.push_back(I->first->get());
Chris Lattner19613292002-10-15 21:41:14 +00001221 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001222
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001223 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1224 if (isVarArg) ParamTypeList.pop_back();
1225
Chris Lattner19613292002-10-15 21:41:14 +00001226 const FunctionType *FT = FunctionType::get(*$2, ParamTypeList, isVarArg);
1227 const PointerType *PFT = PointerType::get(FT);
Chris Lattner841d8b92001-11-26 18:54:16 +00001228 delete $2;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001229
Chris Lattner19613292002-10-15 21:41:14 +00001230 Function *Fn = 0;
1231 // Is the function already in symtab?
1232 if ((Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1233 // Yes it is. If this is the case, either we need to be a forward decl,
1234 // or it needs to be.
1235 if (!CurMeth.isDeclare && !Fn->isExternal())
1236 ThrowException("Redefinition of function '" + FunctionName + "'!");
1237
1238 // Make sure that we keep track of the internal marker, even if there was
1239 // a previous "declare".
1240 if ($1)
1241 Fn->setInternalLinkage(true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001242
Chris Lattner19613292002-10-15 21:41:14 +00001243 // If we found a preexisting function prototype, remove it from the
1244 // module, so that we don't get spurious conflicts with global & local
1245 // variables.
1246 //
1247 CurModule.CurrentModule->getFunctionList().remove(Fn);
Chris Lattner108308a2002-03-08 19:11:42 +00001248
Chris Lattner19613292002-10-15 21:41:14 +00001249 // Make sure to strip off any argument names so we can't get conflicts...
1250 for (Function::aiterator AI = Fn->abegin(), AE = Fn->aend(); AI != AE; ++AI)
1251 AI->setName("");
Chris Lattner8445b412002-07-15 00:10:33 +00001252
Chris Lattner19613292002-10-15 21:41:14 +00001253 } else { // Not already defined?
1254 Fn = new Function(FT, $1, FunctionName);
1255 InsertValue(Fn, CurModule.Values);
1256 CurModule.DeclareNewGlobalValue(Fn, ValID::create($3));
Chris Lattner17f729e2001-07-15 06:35:53 +00001257 }
Chris Lattner841d8b92001-11-26 18:54:16 +00001258 free($3); // Free strdup'd memory!
Chris Lattner2f7c9632001-06-06 20:29:01 +00001259
Chris Lattner19613292002-10-15 21:41:14 +00001260 CurMeth.FunctionStart(Fn);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001261
Chris Lattner113f4f42002-06-25 16:13:24 +00001262 // Add all of the arguments we parsed to the function...
Chris Lattner149376d2002-10-13 20:57:00 +00001263 if ($5) { // Is null if empty...
1264 if (isVarArg) { // Nuke the last entry
1265 assert($5->back().first->get() == Type::VoidTy && $5->back().second == 0&&
1266 "Not a varargs marker!");
1267 delete $5->back().first;
1268 $5->pop_back(); // Delete the last entry
1269 }
Chris Lattner19613292002-10-15 21:41:14 +00001270 Function::aiterator ArgIt = Fn->abegin();
Chris Lattner149376d2002-10-13 20:57:00 +00001271 for (vector<pair<PATypeHolder*, char*> >::iterator I = $5->begin();
1272 I != $5->end(); ++I, ++ArgIt) {
1273 delete I->first; // Delete the typeholder...
1274
1275 if (setValueName(ArgIt, I->second)) // Insert arg into symtab...
Chris Lattner29f67092002-03-08 18:41:32 +00001276 assert(0 && "No arg redef allowed!");
Chris Lattner29f67092002-03-08 18:41:32 +00001277
Chris Lattner149376d2002-10-13 20:57:00 +00001278 InsertValue(ArgIt);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001279 }
Chris Lattner149376d2002-10-13 20:57:00 +00001280
Chris Lattner841d8b92001-11-26 18:54:16 +00001281 delete $5; // We're now done with the argument list
Chris Lattner2f7c9632001-06-06 20:29:01 +00001282 }
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001283};
Chris Lattner2f7c9632001-06-06 20:29:01 +00001284
Chris Lattner1e1a9b42002-05-03 18:23:48 +00001285BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1286
1287FunctionHeader : FunctionHeaderH BEGIN {
Chris Lattner57698e22002-03-26 18:01:55 +00001288 $$ = CurMeth.CurrentFunction;
Chris Lattneraa534bf2001-09-07 16:35:17 +00001289
Chris Lattner113f4f42002-06-25 16:13:24 +00001290 // Resolve circular types before we parse the body of the function.
Chris Lattneraa534bf2001-09-07 16:35:17 +00001291 ResolveTypes(CurMeth.LateResolveTypes);
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001292};
Chris Lattner2f7c9632001-06-06 20:29:01 +00001293
Chris Lattner1e1a9b42002-05-03 18:23:48 +00001294END : ENDTOK | '}'; // Allow end of '}' to end a function
1295
Chris Lattner57698e22002-03-26 18:01:55 +00001296Function : BasicBlockList END {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001297 $$ = $1;
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001298};
Chris Lattner2f7c9632001-06-06 20:29:01 +00001299
Chris Lattner57698e22002-03-26 18:01:55 +00001300FunctionProto : DECLARE { CurMeth.isDeclare = true; } FunctionHeaderH {
1301 $$ = CurMeth.CurrentFunction;
1302 assert($$->getParent() == 0 && "Function already in module!");
1303 CurModule.CurrentModule->getFunctionList().push_back($$);
1304 CurMeth.FunctionDone();
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001305};
Chris Lattner2f7c9632001-06-06 20:29:01 +00001306
1307//===----------------------------------------------------------------------===//
1308// Rules to match Basic Blocks
1309//===----------------------------------------------------------------------===//
1310
1311ConstValueRef : ESINT64VAL { // A reference to a direct constant
1312 $$ = ValID::create($1);
1313 }
1314 | EUINT64VAL {
1315 $$ = ValID::create($1);
1316 }
Chris Lattner212f70d2001-07-15 00:17:01 +00001317 | FPVAL { // Perhaps it's an FP constant?
1318 $$ = ValID::create($1);
1319 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001320 | TRUE {
Chris Lattner7f325cd2002-08-16 21:14:40 +00001321 $$ = ValID::create(ConstantBool::True);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001322 }
1323 | FALSE {
Chris Lattner7f325cd2002-08-16 21:14:40 +00001324 $$ = ValID::create(ConstantBool::False);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001325 }
Chris Lattnerfbdec252001-09-30 22:46:54 +00001326 | NULL_TOK {
1327 $$ = ValID::createNull();
Vikram S. Adve7064eaf2002-07-14 22:59:28 +00001328 }
Chris Lattner7f325cd2002-08-16 21:14:40 +00001329 | ConstExpr {
1330 $$ = ValID::create($1);
1331 };
Chris Lattnerfbdec252001-09-30 22:46:54 +00001332
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001333// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1334// another value.
1335//
1336SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner2f7c9632001-06-06 20:29:01 +00001337 $$ = ValID::create($1);
1338 }
Chris Lattner212f70d2001-07-15 00:17:01 +00001339 | VAR_ID { // Is it a named reference...?
Chris Lattner2f7c9632001-06-06 20:29:01 +00001340 $$ = ValID::create($1);
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001341 };
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001342
1343// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001344ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001345
Chris Lattner2f7c9632001-06-06 20:29:01 +00001346
Chris Lattner252afba2001-07-26 16:29:15 +00001347// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1348// type immediately preceeds the value reference, and allows complex constant
1349// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattner571a6b02001-10-03 01:49:25 +00001350ResolvedVal : Types ValueRef {
Chris Lattneraa534bf2001-09-07 16:35:17 +00001351 $$ = getVal(*$1, $2); delete $1;
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001352 };
Chris Lattner42b5a8a2001-07-25 22:47:46 +00001353
Chris Lattner2f7c9632001-06-06 20:29:01 +00001354BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner113f4f42002-06-25 16:13:24 +00001355 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001356 }
Chris Lattner113f4f42002-06-25 16:13:24 +00001357 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
1358 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001359 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00001360
1361
1362// Basic blocks are terminated by branching instructions:
1363// br, br/cc, switch, ret
1364//
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001365BasicBlock : InstructionList OptAssign BBTerminatorInst {
1366 if (setValueName($3, $2)) { assert(0 && "No redefn allowed!"); }
1367 InsertValue($3);
1368
1369 $1->getInstList().push_back($3);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001370 InsertValue($1);
1371 $$ = $1;
1372 }
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001373 | LABELSTR InstructionList OptAssign BBTerminatorInst {
1374 if (setValueName($4, $3)) { assert(0 && "No redefn allowed!"); }
1375 InsertValue($4);
1376
1377 $2->getInstList().push_back($4);
Chris Lattnerfb3e5492001-10-03 15:39:04 +00001378 if (setValueName($2, $1)) { assert(0 && "No label redef allowed!"); }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001379
1380 InsertValue($2);
1381 $$ = $2;
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001382 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00001383
1384InstructionList : InstructionList Inst {
1385 $1->getInstList().push_back($2);
1386 $$ = $1;
1387 }
1388 | /* empty */ {
Chris Lattner319c47a2002-08-21 23:51:21 +00001389 $$ = CurBB = new BasicBlock();
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001390 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00001391
Chris Lattner252afba2001-07-26 16:29:15 +00001392BBTerminatorInst : RET ResolvedVal { // Return with a result...
1393 $$ = new ReturnInst($2);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001394 }
1395 | RET VOID { // Return with no result...
1396 $$ = new ReturnInst();
1397 }
1398 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner4b717c02001-10-01 16:18:37 +00001399 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $3)));
Chris Lattner2f7c9632001-06-06 20:29:01 +00001400 } // Conditional Branch...
1401 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner4b717c02001-10-01 16:18:37 +00001402 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $6)),
1403 cast<BasicBlock>(getVal(Type::LabelTy, $9)),
Chris Lattner2f7c9632001-06-06 20:29:01 +00001404 getVal(Type::BoolTy, $3));
1405 }
1406 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1407 SwitchInst *S = new SwitchInst(getVal($2, $3),
Chris Lattner4b717c02001-10-01 16:18:37 +00001408 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
Chris Lattner2f7c9632001-06-06 20:29:01 +00001409 $$ = S;
1410
Chris Lattnerae234e12002-04-09 19:41:42 +00001411 vector<pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
1412 E = $8->end();
1413 for (; I != E; ++I)
Chris Lattner2f7c9632001-06-06 20:29:01 +00001414 S->dest_push_back(I->first, I->second);
1415 }
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001416 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO ResolvedVal
1417 EXCEPT ResolvedVal {
Chris Lattner19613292002-10-15 21:41:14 +00001418 const PointerType *PFTy;
Chris Lattner57698e22002-03-26 18:01:55 +00001419 const FunctionType *Ty;
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001420
Chris Lattner19613292002-10-15 21:41:14 +00001421 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1422 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001423 // Pull out the types of all of the arguments...
1424 vector<const Type*> ParamTypes;
1425 if ($5) {
Chris Lattner476148f2001-11-26 16:54:11 +00001426 for (vector<Value*>::iterator I = $5->begin(), E = $5->end(); I!=E; ++I)
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001427 ParamTypes.push_back((*I)->getType());
1428 }
1429
1430 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1431 if (isVarArg) ParamTypes.pop_back();
1432
Chris Lattner57698e22002-03-26 18:01:55 +00001433 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattner19613292002-10-15 21:41:14 +00001434 PFTy = PointerType::get(Ty);
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001435 }
1436 delete $2;
1437
Chris Lattner19613292002-10-15 21:41:14 +00001438 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001439
1440 BasicBlock *Normal = dyn_cast<BasicBlock>($8);
1441 BasicBlock *Except = dyn_cast<BasicBlock>($10);
1442
1443 if (Normal == 0 || Except == 0)
1444 ThrowException("Invoke instruction without label destinations!");
1445
1446 // Create the call node...
1447 if (!$5) { // Has no arguments?
Chris Lattner322e49a2001-10-16 19:54:17 +00001448 $$ = new InvokeInst(V, Normal, Except, vector<Value*>());
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001449 } else { // Has arguments?
Chris Lattner57698e22002-03-26 18:01:55 +00001450 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001451 // correctly!
1452 //
Chris Lattner57698e22002-03-26 18:01:55 +00001453 FunctionType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1454 FunctionType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
Chris Lattner476148f2001-11-26 16:54:11 +00001455 vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001456
1457 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1458 if ((*ArgI)->getType() != *I)
1459 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattneref99d3c2001-12-14 16:28:42 +00001460 (*I)->getDescription() + "'!");
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001461
1462 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1463 ThrowException("Invalid number of parameters detected!");
1464
Chris Lattner476148f2001-11-26 16:54:11 +00001465 $$ = new InvokeInst(V, Normal, Except, *$5);
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001466 }
1467 delete $5;
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001468 };
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001469
1470
Chris Lattner2f7c9632001-06-06 20:29:01 +00001471
1472JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1473 $$ = $1;
Chris Lattner3462ae32001-12-03 22:26:30 +00001474 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner2f7c9632001-06-06 20:29:01 +00001475 if (V == 0)
1476 ThrowException("May only switch on a constant pool value!");
1477
Chris Lattner4b717c02001-10-01 16:18:37 +00001478 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($5, $6))));
Chris Lattner2f7c9632001-06-06 20:29:01 +00001479 }
1480 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattnerae234e12002-04-09 19:41:42 +00001481 $$ = new vector<pair<Constant*, BasicBlock*> >();
Chris Lattner3462ae32001-12-03 22:26:30 +00001482 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner2f7c9632001-06-06 20:29:01 +00001483
1484 if (V == 0)
1485 ThrowException("May only switch on a constant pool value!");
1486
Chris Lattner4b717c02001-10-01 16:18:37 +00001487 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($4, $5))));
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001488 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00001489
1490Inst : OptAssign InstVal {
Chris Lattnerfb3e5492001-10-03 15:39:04 +00001491 // Is this definition named?? if so, assign the name...
1492 if (setValueName($2, $1)) { assert(0 && "No redefin allowed!"); }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001493 InsertValue($2);
1494 $$ = $2;
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001495};
Chris Lattner2f7c9632001-06-06 20:29:01 +00001496
Chris Lattner931ef3b2001-06-11 15:04:20 +00001497PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
1498 $$ = new list<pair<Value*, BasicBlock*> >();
Chris Lattneraa534bf2001-09-07 16:35:17 +00001499 $$->push_back(make_pair(getVal(*$1, $3),
Chris Lattner4b717c02001-10-01 16:18:37 +00001500 cast<BasicBlock>(getVal(Type::LabelTy, $5))));
Chris Lattneraa534bf2001-09-07 16:35:17 +00001501 delete $1;
Chris Lattner931ef3b2001-06-11 15:04:20 +00001502 }
1503 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1504 $$ = $1;
1505 $1->push_back(make_pair(getVal($1->front().first->getType(), $4),
Chris Lattner4b717c02001-10-01 16:18:37 +00001506 cast<BasicBlock>(getVal(Type::LabelTy, $6))));
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001507 };
Chris Lattner931ef3b2001-06-11 15:04:20 +00001508
1509
Chris Lattneraa534bf2001-09-07 16:35:17 +00001510ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner476148f2001-11-26 16:54:11 +00001511 $$ = new vector<Value*>();
Chris Lattner252afba2001-07-26 16:29:15 +00001512 $$->push_back($1);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001513 }
Chris Lattner252afba2001-07-26 16:29:15 +00001514 | ValueRefList ',' ResolvedVal {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001515 $$ = $1;
Chris Lattner252afba2001-07-26 16:29:15 +00001516 $1->push_back($3);
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001517 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00001518
1519// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001520ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner2f7c9632001-06-06 20:29:01 +00001521
Chris Lattnerc0686fe2002-09-10 19:57:26 +00001522InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
1523 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint())
1524 ThrowException("Arithmetic operator requires integer or FP operands!");
1525 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1526 if ($$ == 0)
1527 ThrowException("binary operator returned null!");
1528 delete $2;
1529 }
1530 | LogicalOps Types ValueRef ',' ValueRef {
1531 if (!(*$2)->isIntegral())
1532 ThrowException("Logical operator requires integral operands!");
1533 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1534 if ($$ == 0)
1535 ThrowException("binary operator returned null!");
1536 delete $2;
1537 }
1538 | SetCondOps Types ValueRef ',' ValueRef {
Chris Lattnera6834c12002-09-10 22:37:46 +00001539 $$ = new SetCondInst($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner2f7c9632001-06-06 20:29:01 +00001540 if ($$ == 0)
1541 ThrowException("binary operator returned null!");
Chris Lattneraa534bf2001-09-07 16:35:17 +00001542 delete $2;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001543 }
Chris Lattnerdb3b2022002-08-14 17:12:33 +00001544 | NOT ResolvedVal {
1545 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
1546 << " Replacing with 'xor'.\n";
1547
1548 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
1549 if (Ones == 0)
1550 ThrowException("Expected integral type for not instruction!");
1551
1552 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001553 if ($$ == 0)
Chris Lattnerdb3b2022002-08-14 17:12:33 +00001554 ThrowException("Could not create a xor instruction!");
Chris Lattnera6821822001-07-08 04:57:15 +00001555 }
Chris Lattner252afba2001-07-26 16:29:15 +00001556 | ShiftOps ResolvedVal ',' ResolvedVal {
1557 if ($4->getType() != Type::UByteTy)
1558 ThrowException("Shift amount must be ubyte!");
1559 $$ = new ShiftInst($1, $2, $4);
Chris Lattnerd8bebcd2001-07-08 21:10:27 +00001560 }
Chris Lattner252afba2001-07-26 16:29:15 +00001561 | CAST ResolvedVal TO Types {
Chris Lattneraa534bf2001-09-07 16:35:17 +00001562 $$ = new CastInst($2, *$4);
1563 delete $4;
Chris Lattnera6821822001-07-08 04:57:15 +00001564 }
Chris Lattner931ef3b2001-06-11 15:04:20 +00001565 | PHI PHIList {
1566 const Type *Ty = $2->front().first->getType();
1567 $$ = new PHINode(Ty);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001568 while ($2->begin() != $2->end()) {
Chris Lattner931ef3b2001-06-11 15:04:20 +00001569 if ($2->front().first->getType() != Ty)
1570 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerda558102001-10-02 03:41:24 +00001571 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001572 $2->pop_front();
1573 }
1574 delete $2; // Free the list...
1575 }
Chris Lattner26e50dc2001-07-28 17:48:55 +00001576 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattner19613292002-10-15 21:41:14 +00001577 const PointerType *PFTy;
Chris Lattner57698e22002-03-26 18:01:55 +00001578 const FunctionType *Ty;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001579
Chris Lattner19613292002-10-15 21:41:14 +00001580 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1581 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner42b5a8a2001-07-25 22:47:46 +00001582 // Pull out the types of all of the arguments...
1583 vector<const Type*> ParamTypes;
Chris Lattner7fac0702001-10-03 14:53:21 +00001584 if ($5) {
Chris Lattner476148f2001-11-26 16:54:11 +00001585 for (vector<Value*>::iterator I = $5->begin(), E = $5->end(); I!=E; ++I)
Chris Lattner7fac0702001-10-03 14:53:21 +00001586 ParamTypes.push_back((*I)->getType());
1587 }
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001588
1589 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1590 if (isVarArg) ParamTypes.pop_back();
1591
Chris Lattner57698e22002-03-26 18:01:55 +00001592 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattner19613292002-10-15 21:41:14 +00001593 PFTy = PointerType::get(Ty);
Chris Lattner42b5a8a2001-07-25 22:47:46 +00001594 }
Chris Lattneraa534bf2001-09-07 16:35:17 +00001595 delete $2;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001596
Chris Lattner19613292002-10-15 21:41:14 +00001597 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner2f7c9632001-06-06 20:29:01 +00001598
Chris Lattner42b5a8a2001-07-25 22:47:46 +00001599 // Create the call node...
1600 if (!$5) { // Has no arguments?
Chris Lattner91e08322002-07-25 20:52:56 +00001601 // Make sure no arguments is a good thing!
1602 if (Ty->getNumParams() != 0)
1603 ThrowException("No arguments passed to a function that "
1604 "expects arguments!");
1605
Chris Lattner322e49a2001-10-16 19:54:17 +00001606 $$ = new CallInst(V, vector<Value*>());
Chris Lattner42b5a8a2001-07-25 22:47:46 +00001607 } else { // Has arguments?
Chris Lattner57698e22002-03-26 18:01:55 +00001608 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2f7c9632001-06-06 20:29:01 +00001609 // correctly!
1610 //
Chris Lattner57698e22002-03-26 18:01:55 +00001611 FunctionType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1612 FunctionType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
Chris Lattner476148f2001-11-26 16:54:11 +00001613 vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner42b5a8a2001-07-25 22:47:46 +00001614
1615 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1616 if ((*ArgI)->getType() != *I)
1617 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattneref99d3c2001-12-14 16:28:42 +00001618 (*I)->getDescription() + "'!");
Chris Lattner2f7c9632001-06-06 20:29:01 +00001619
Chris Lattner42b5a8a2001-07-25 22:47:46 +00001620 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner2f7c9632001-06-06 20:29:01 +00001621 ThrowException("Invalid number of parameters detected!");
Chris Lattner2f7c9632001-06-06 20:29:01 +00001622
Chris Lattner476148f2001-11-26 16:54:11 +00001623 $$ = new CallInst(V, *$5);
Chris Lattner42b5a8a2001-07-25 22:47:46 +00001624 }
1625 delete $5;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001626 }
1627 | MemoryInst {
1628 $$ = $1;
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001629 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00001630
Chris Lattner476148f2001-11-26 16:54:11 +00001631
1632// IndexList - List of indices for GEP based instructions...
1633IndexList : ',' ValueRefList {
Chris Lattnerd8bebcd2001-07-08 21:10:27 +00001634 $$ = $2;
1635} | /* empty */ {
Chris Lattner476148f2001-11-26 16:54:11 +00001636 $$ = new vector<Value*>();
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001637};
Chris Lattnerd8bebcd2001-07-08 21:10:27 +00001638
Chris Lattner2f7c9632001-06-06 20:29:01 +00001639MemoryInst : MALLOC Types {
Chris Lattner322bf4f2002-09-13 22:28:45 +00001640 $$ = new MallocInst(*$2);
Chris Lattneraa534bf2001-09-07 16:35:17 +00001641 delete $2;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001642 }
1643 | MALLOC Types ',' UINT ValueRef {
Chris Lattner322bf4f2002-09-13 22:28:45 +00001644 $$ = new MallocInst(*$2, getVal($4, $5));
Chris Lattneraa534bf2001-09-07 16:35:17 +00001645 delete $2;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001646 }
1647 | ALLOCA Types {
Chris Lattner322bf4f2002-09-13 22:28:45 +00001648 $$ = new AllocaInst(*$2);
Chris Lattneraa534bf2001-09-07 16:35:17 +00001649 delete $2;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001650 }
1651 | ALLOCA Types ',' UINT ValueRef {
Chris Lattner322bf4f2002-09-13 22:28:45 +00001652 $$ = new AllocaInst(*$2, getVal($4, $5));
Chris Lattneraa534bf2001-09-07 16:35:17 +00001653 delete $2;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001654 }
Chris Lattner252afba2001-07-26 16:29:15 +00001655 | FREE ResolvedVal {
Chris Lattner181cc322002-05-06 16:15:30 +00001656 if (!isa<PointerType>($2->getType()))
Chris Lattner252afba2001-07-26 16:29:15 +00001657 ThrowException("Trying to free nonpointer type " +
Chris Lattneref99d3c2001-12-14 16:28:42 +00001658 $2->getType()->getDescription() + "!");
Chris Lattner252afba2001-07-26 16:29:15 +00001659 $$ = new FreeInst($2);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001660 }
1661
Chris Lattner476148f2001-11-26 16:54:11 +00001662 | LOAD Types ValueRef IndexList {
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001663 if (!isa<PointerType>($2->get()))
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001664 ThrowException("Can't load from nonpointer type: " +
1665 (*$2)->getDescription());
Chris Lattner030effa2002-08-22 22:48:55 +00001666 if (GetElementPtrInst::getIndexedType(*$2, *$4) == 0)
Chris Lattnerd8bebcd2001-07-08 21:10:27 +00001667 ThrowException("Invalid indices for load instruction!");
1668
Chris Lattner319c47a2002-08-21 23:51:21 +00001669 Value *Src = getVal(*$2, $3);
1670 if (!$4->empty()) {
1671 std::cerr << "WARNING: Use of index load instruction:"
1672 << " replacing with getelementptr/load pair.\n";
1673 // Create a getelementptr hack instruction to do the right thing for
1674 // compatibility.
1675 //
1676 Instruction *I = new GetElementPtrInst(Src, *$4);
1677 CurBB->getInstList().push_back(I);
1678 Src = I;
1679 }
1680
1681 $$ = new LoadInst(Src);
Chris Lattnerd8bebcd2001-07-08 21:10:27 +00001682 delete $4; // Free the vector...
Chris Lattneraa534bf2001-09-07 16:35:17 +00001683 delete $2;
Chris Lattnerd8bebcd2001-07-08 21:10:27 +00001684 }
Chris Lattner476148f2001-11-26 16:54:11 +00001685 | STORE ResolvedVal ',' Types ValueRef IndexList {
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001686 if (!isa<PointerType>($4->get()))
Chris Lattneref99d3c2001-12-14 16:28:42 +00001687 ThrowException("Can't store to a nonpointer type: " +
1688 (*$4)->getDescription());
Chris Lattner030effa2002-08-22 22:48:55 +00001689 const Type *ElTy = GetElementPtrInst::getIndexedType(*$4, *$6);
Chris Lattner62ecb4a2001-07-08 23:22:50 +00001690 if (ElTy == 0)
1691 ThrowException("Can't store into that field list!");
Chris Lattner252afba2001-07-26 16:29:15 +00001692 if (ElTy != $2->getType())
Chris Lattneref99d3c2001-12-14 16:28:42 +00001693 ThrowException("Can't store '" + $2->getType()->getDescription() +
1694 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner319c47a2002-08-21 23:51:21 +00001695
1696 Value *Ptr = getVal(*$4, $5);
1697 if (!$6->empty()) {
1698 std::cerr << "WARNING: Use of index store instruction:"
1699 << " replacing with getelementptr/store pair.\n";
1700 // Create a getelementptr hack instruction to do the right thing for
1701 // compatibility.
1702 //
1703 Instruction *I = new GetElementPtrInst(Ptr, *$6);
1704 CurBB->getInstList().push_back(I);
1705 Ptr = I;
1706 }
1707
1708 $$ = new StoreInst($2, Ptr);
Chris Lattneraa534bf2001-09-07 16:35:17 +00001709 delete $4; delete $6;
Chris Lattner62ecb4a2001-07-08 23:22:50 +00001710 }
Chris Lattner476148f2001-11-26 16:54:11 +00001711 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattnerbb4fe2c2002-09-11 01:17:27 +00001712 for (unsigned i = 0, e = $4->size(); i != e; ++i) {
1713 if ((*$4)[i]->getType() == Type::UIntTy) {
1714 std::cerr << "WARNING: Use of uint type indexes to getelementptr "
1715 << "instruction: replacing with casts to long type.\n";
1716 Instruction *I = new CastInst((*$4)[i], Type::LongTy);
1717 CurBB->getInstList().push_back(I);
1718 (*$4)[i] = I;
1719 }
1720 }
1721
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001722 if (!isa<PointerType>($2->get()))
Chris Lattner62ecb4a2001-07-08 23:22:50 +00001723 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattneraa534bf2001-09-07 16:35:17 +00001724 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattneref99d3c2001-12-14 16:28:42 +00001725 ThrowException("Can't get element ptr '" + (*$2)->getDescription()+ "'!");
Chris Lattneraa534bf2001-09-07 16:35:17 +00001726 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
1727 delete $2; delete $4;
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001728 };
Chris Lattnerd8bebcd2001-07-08 21:10:27 +00001729
Chris Lattner2f7c9632001-06-06 20:29:01 +00001730%%
Chris Lattnera6821822001-07-08 04:57:15 +00001731int yyerror(const char *ErrorMsg) {
Vikram S. Adve7064eaf2002-07-14 22:59:28 +00001732 string where = string((CurFilename == "-")? string("<stdin>") : CurFilename)
1733 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
1734 string errMsg = string(ErrorMsg) + string("\n") + where + " while reading ";
1735 if (yychar == YYEMPTY)
1736 errMsg += "end-of-file.";
1737 else
1738 errMsg += "token: '" + string(llvmAsmtext, llvmAsmleng) + "'";
1739 ThrowException(errMsg);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001740 return 0;
1741}