blob: d40a8706d446ff982e2e8e249c7ab93ea9546b41 [file] [log] [blame]
Chris Lattner00950542001-06-06 20:29:01 +00001//===-- llvmAsmParser.y - Parser for llvm assembly files ---------*- C++ -*--=//
2//
3// This file implements the bison parser for LLVM assembly languages files.
4//
5//===------------------------------------------------------------------------=//
6
Chris Lattner00950542001-06-06 20:29:01 +00007%{
8#include "ParserInternals.h"
Chris Lattner00950542001-06-06 20:29:01 +00009#include "llvm/SymbolTable.h"
10#include "llvm/Module.h"
Chris Lattner00950542001-06-06 20:29:01 +000011#include "llvm/iTerminators.h"
12#include "llvm/iMemory.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000013#include "llvm/iPHINode.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000014#include "Support/STLExtras.h"
15#include "Support/DepthFirstIterator.h"
Chris Lattner00950542001-06-06 20:29:01 +000016#include <list>
Chris Lattnerc188eeb2002-07-30 18:54:25 +000017#include <utility>
Chris Lattner30c89792001-09-07 16:35:17 +000018#include <algorithm>
Chris Lattner697954c2002-01-20 22:54:45 +000019using std::list;
20using std::vector;
21using std::pair;
22using std::map;
23using std::pair;
24using std::make_pair;
Chris Lattner697954c2002-01-20 22:54:45 +000025using std::string;
Chris Lattner00950542001-06-06 20:29:01 +000026
Chris Lattner386a3b72001-10-16 19:54:17 +000027int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
Chris Lattner09083092001-07-08 04:57:15 +000028int yylex(); // declaration" of xxx warnings.
Chris Lattner00950542001-06-06 20:29:01 +000029int yyparse();
30
31static Module *ParserResult;
Chris Lattnera2850432001-07-22 18:36:00 +000032string CurFilename;
Chris Lattner00950542001-06-06 20:29:01 +000033
Chris Lattner30c89792001-09-07 16:35:17 +000034// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
35// relating to upreferences in the input stream.
36//
37//#define DEBUG_UPREFS 1
38#ifdef DEBUG_UPREFS
Chris Lattner699f1eb2002-08-14 17:12:33 +000039#define UR_OUT(X) std::cerr << X
Chris Lattner30c89792001-09-07 16:35:17 +000040#else
41#define UR_OUT(X)
42#endif
43
Vikram S. Adved3f7eb02002-07-14 22:59:28 +000044#define YYERROR_VERBOSE 1
45
Chris Lattner0383cc42002-08-21 23:51:21 +000046// HACK ALERT: This variable is used to implement the automatic conversion of
47// load/store instructions with indexes into a load/store + getelementptr pair
48// of instructions. When this compatiblity "Feature" is removed, this should be
49// too.
50//
51static BasicBlock *CurBB;
52
53
Chris Lattner7e708292002-06-25 16:13:24 +000054// This contains info used when building the body of a function. It is
55// destroyed when the function is completed.
Chris Lattner00950542001-06-06 20:29:01 +000056//
57typedef vector<Value *> ValueList; // Numbered defs
Chris Lattner386a3b72001-10-16 19:54:17 +000058static void ResolveDefinitions(vector<ValueList> &LateResolvers,
59 vector<ValueList> *FutureLateResolvers = 0);
Chris Lattner00950542001-06-06 20:29:01 +000060
61static struct PerModuleInfo {
62 Module *CurrentModule;
Chris Lattner30c89792001-09-07 16:35:17 +000063 vector<ValueList> Values; // Module level numbered definitions
64 vector<ValueList> LateResolveValues;
Chris Lattner8b88b3b2002-04-04 19:23:55 +000065 vector<PATypeHolder> Types;
66 map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner00950542001-06-06 20:29:01 +000067
Chris Lattner2079fde2001-10-13 06:41:08 +000068 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
69 // references to global values. Global values may be referenced before they
70 // are defined, and if so, the temporary object that they represent is held
Chris Lattnere9bb2df2001-12-03 22:26:30 +000071 // here. This is used for forward references of ConstantPointerRefs.
Chris Lattner2079fde2001-10-13 06:41:08 +000072 //
73 typedef map<pair<const PointerType *, ValID>, GlobalVariable*> GlobalRefsType;
74 GlobalRefsType GlobalRefs;
75
Chris Lattner00950542001-06-06 20:29:01 +000076 void ModuleDone() {
Chris Lattner7e708292002-06-25 16:13:24 +000077 // If we could not resolve some functions at function compilation time
78 // (calls to functions before they are defined), resolve them now... Types
79 // are resolved when the constant pool has been completely parsed.
Chris Lattner30c89792001-09-07 16:35:17 +000080 //
Chris Lattner00950542001-06-06 20:29:01 +000081 ResolveDefinitions(LateResolveValues);
82
Chris Lattner2079fde2001-10-13 06:41:08 +000083 // Check to make sure that all global value forward references have been
84 // resolved!
85 //
86 if (!GlobalRefs.empty()) {
Chris Lattner749ce032002-03-11 22:12:39 +000087 string UndefinedReferences = "Unresolved global references exist:\n";
88
89 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
90 I != E; ++I) {
91 UndefinedReferences += " " + I->first.first->getDescription() + " " +
92 I->first.second.getName() + "\n";
93 }
94 ThrowException(UndefinedReferences);
Chris Lattner2079fde2001-10-13 06:41:08 +000095 }
96
Chris Lattner7e708292002-06-25 16:13:24 +000097 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +000098 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +000099 CurrentModule = 0;
100 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000101
102
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000103 // DeclareNewGlobalValue - Called every time a new GV has been defined. This
Chris Lattner2079fde2001-10-13 06:41:08 +0000104 // is used to remove things from the forward declaration map, resolving them
105 // to the correct thing as needed.
106 //
107 void DeclareNewGlobalValue(GlobalValue *GV, ValID D) {
108 // Check to see if there is a forward reference to this global variable...
109 // if there is, eliminate it and patch the reference to use the new def'n.
110 GlobalRefsType::iterator I = GlobalRefs.find(make_pair(GV->getType(), D));
111
112 if (I != GlobalRefs.end()) {
113 GlobalVariable *OldGV = I->second; // Get the placeholder...
114 I->first.second.destroy(); // Free string memory if neccesary
115
116 // Loop over all of the uses of the GlobalValue. The only thing they are
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000117 // allowed to be is ConstantPointerRef's.
Chris Lattner2079fde2001-10-13 06:41:08 +0000118 assert(OldGV->use_size() == 1 && "Only one reference should exist!");
119 while (!OldGV->use_empty()) {
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000120 User *U = OldGV->use_back(); // Must be a ConstantPointerRef...
121 ConstantPointerRef *CPR = cast<ConstantPointerRef>(U);
122 assert(CPR->getValue() == OldGV && "Something isn't happy");
123
124 // Change the const pool reference to point to the real global variable
125 // now. This should drop a use from the OldGV.
126 CPR->mutateReferences(OldGV, GV);
Chris Lattner2079fde2001-10-13 06:41:08 +0000127 }
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000128
129 // Remove OldGV from the module...
Chris Lattner2079fde2001-10-13 06:41:08 +0000130 CurrentModule->getGlobalList().remove(OldGV);
131 delete OldGV; // Delete the old placeholder
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000132
Chris Lattner2079fde2001-10-13 06:41:08 +0000133 // Remove the map entry for the global now that it has been created...
134 GlobalRefs.erase(I);
135 }
136 }
137
Chris Lattner00950542001-06-06 20:29:01 +0000138} CurModule;
139
Chris Lattner79df7c02002-03-26 18:01:55 +0000140static struct PerFunctionInfo {
Chris Lattner7e708292002-06-25 16:13:24 +0000141 Function *CurrentFunction; // Pointer to current function being created
Chris Lattner00950542001-06-06 20:29:01 +0000142
Chris Lattnere1815642001-07-15 06:35:53 +0000143 vector<ValueList> Values; // Keep track of numbered definitions
Chris Lattner00950542001-06-06 20:29:01 +0000144 vector<ValueList> LateResolveValues;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000145 vector<PATypeHolder> Types;
146 map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner7e708292002-06-25 16:13:24 +0000147 bool isDeclare; // Is this function a forward declararation?
Chris Lattner00950542001-06-06 20:29:01 +0000148
Chris Lattner79df7c02002-03-26 18:01:55 +0000149 inline PerFunctionInfo() {
150 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000151 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000152 }
153
Chris Lattner79df7c02002-03-26 18:01:55 +0000154 inline ~PerFunctionInfo() {}
Chris Lattner00950542001-06-06 20:29:01 +0000155
Chris Lattner79df7c02002-03-26 18:01:55 +0000156 inline void FunctionStart(Function *M) {
157 CurrentFunction = M;
Chris Lattner00950542001-06-06 20:29:01 +0000158 }
159
Chris Lattner79df7c02002-03-26 18:01:55 +0000160 void FunctionDone() {
Chris Lattner00950542001-06-06 20:29:01 +0000161 // If we could not resolve some blocks at parsing time (forward branches)
162 // resolve the branches now...
Chris Lattner386a3b72001-10-16 19:54:17 +0000163 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000164
Chris Lattner7e708292002-06-25 16:13:24 +0000165 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +0000166 Types.clear();
Chris Lattner79df7c02002-03-26 18:01:55 +0000167 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000168 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000169 }
Chris Lattner7e708292002-06-25 16:13:24 +0000170} CurMeth; // Info for the current function...
Chris Lattner00950542001-06-06 20:29:01 +0000171
Chris Lattner79df7c02002-03-26 18:01:55 +0000172static bool inFunctionScope() { return CurMeth.CurrentFunction != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000173
Chris Lattner00950542001-06-06 20:29:01 +0000174
175//===----------------------------------------------------------------------===//
176// Code to handle definitions of all the types
177//===----------------------------------------------------------------------===//
178
Chris Lattner2079fde2001-10-13 06:41:08 +0000179static int InsertValue(Value *D, vector<ValueList> &ValueTab = CurMeth.Values) {
180 if (D->hasName()) return -1; // Is this a numbered definition?
181
182 // Yes, insert the value into the value table...
183 unsigned type = D->getType()->getUniqueID();
184 if (ValueTab.size() <= type)
185 ValueTab.resize(type+1, ValueList());
186 //printf("Values[%d][%d] = %d\n", type, ValueTab[type].size(), D);
187 ValueTab[type].push_back(D);
188 return ValueTab[type].size()-1;
Chris Lattner00950542001-06-06 20:29:01 +0000189}
190
Chris Lattner30c89792001-09-07 16:35:17 +0000191// TODO: FIXME when Type are not const
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000192static void InsertType(const Type *Ty, vector<PATypeHolder> &Types) {
Chris Lattner30c89792001-09-07 16:35:17 +0000193 Types.push_back(Ty);
194}
195
196static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000197 switch (D.Type) {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000198 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000199 unsigned Num = (unsigned)D.Num;
200
201 // Module constants occupy the lowest numbered slots...
202 if (Num < CurModule.Types.size())
203 return CurModule.Types[Num];
204
205 Num -= CurModule.Types.size();
206
207 // Check that the number is within bounds...
208 if (Num <= CurMeth.Types.size())
209 return CurMeth.Types[Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000210 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000211 }
Chris Lattnerf8dff732002-07-18 05:18:37 +0000212 case ValID::NameVal: { // Is it a named definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000213 string Name(D.Name);
214 SymbolTable *SymTab = 0;
Chris Lattner79df7c02002-03-26 18:01:55 +0000215 if (inFunctionScope()) SymTab = CurMeth.CurrentFunction->getSymbolTable();
Chris Lattner30c89792001-09-07 16:35:17 +0000216 Value *N = SymTab ? SymTab->lookup(Type::TypeTy, Name) : 0;
217
218 if (N == 0) {
Chris Lattner7e708292002-06-25 16:13:24 +0000219 // Symbol table doesn't automatically chain yet... because the function
Chris Lattner30c89792001-09-07 16:35:17 +0000220 // hasn't been added to the module...
221 //
222 SymTab = CurModule.CurrentModule->getSymbolTable();
223 if (SymTab)
224 N = SymTab->lookup(Type::TypeTy, Name);
225 if (N == 0) break;
226 }
227
228 D.destroy(); // Free old strdup'd memory...
Chris Lattnercfe26c92001-10-01 18:26:53 +0000229 return cast<const Type>(N);
Chris Lattner30c89792001-09-07 16:35:17 +0000230 }
231 default:
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000232 ThrowException("Internal parser error: Invalid symbol type reference!");
Chris Lattner30c89792001-09-07 16:35:17 +0000233 }
234
235 // If we reached here, we referenced either a symbol that we don't know about
236 // or an id number that hasn't been read yet. We may be referencing something
237 // forward, so just create an entry to be resolved later and get to it...
238 //
239 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
240
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000241 map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner4a42e902001-10-22 05:56:09 +0000242 CurMeth.LateResolveTypes : CurModule.LateResolveTypes;
243
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000244 map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000245 if (I != LateResolver.end()) {
246 return I->second;
247 }
Chris Lattner30c89792001-09-07 16:35:17 +0000248
Chris Lattner82269592001-10-22 06:01:08 +0000249 Type *Typ = OpaqueType::get();
Chris Lattner4a42e902001-10-22 05:56:09 +0000250 LateResolver.insert(make_pair(D, Typ));
Chris Lattner30c89792001-09-07 16:35:17 +0000251 return Typ;
252}
253
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000254static Value *lookupInSymbolTable(const Type *Ty, const string &Name) {
255 SymbolTable *SymTab =
Chris Lattner9705a152002-05-02 19:27:42 +0000256 inFunctionScope() ? CurMeth.CurrentFunction->getSymbolTable() :
257 CurModule.CurrentModule->getSymbolTable();
Chris Lattner924025e2002-04-29 18:25:33 +0000258 return SymTab ? SymTab->lookup(Ty, Name) : 0;
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000259}
260
Chris Lattner2079fde2001-10-13 06:41:08 +0000261// getValNonImprovising - Look up the value specified by the provided type and
262// the provided ValID. If the value exists and has already been defined, return
263// it. Otherwise return null.
264//
265static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000266 if (isa<FunctionType>(Ty))
267 ThrowException("Functions are not values and "
268 "must be referenced as pointers");
Chris Lattner386a3b72001-10-16 19:54:17 +0000269
Chris Lattner30c89792001-09-07 16:35:17 +0000270 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000271 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000272 unsigned type = Ty->getUniqueID();
Chris Lattner00950542001-06-06 20:29:01 +0000273 unsigned Num = (unsigned)D.Num;
274
275 // Module constants occupy the lowest numbered slots...
276 if (type < CurModule.Values.size()) {
277 if (Num < CurModule.Values[type].size())
278 return CurModule.Values[type][Num];
279
280 Num -= CurModule.Values[type].size();
281 }
282
283 // Make sure that our type is within bounds
Chris Lattner2079fde2001-10-13 06:41:08 +0000284 if (CurMeth.Values.size() <= type) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000285
286 // Check that the number is within bounds...
Chris Lattner2079fde2001-10-13 06:41:08 +0000287 if (CurMeth.Values[type].size() <= Num) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000288
289 return CurMeth.Values[type][Num];
290 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000291
Chris Lattner1a1cb112001-09-30 22:46:54 +0000292 case ValID::NameVal: { // Is it a named definition?
Chris Lattner2079fde2001-10-13 06:41:08 +0000293 Value *N = lookupInSymbolTable(Ty, string(D.Name));
294 if (N == 0) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000295
296 D.destroy(); // Free old strdup'd memory...
297 return N;
298 }
299
Chris Lattner2079fde2001-10-13 06:41:08 +0000300 // Check to make sure that "Ty" is an integral type, and that our
301 // value will fit into the specified type...
302 case ValID::ConstSIntVal: // Is it a constant pool reference??
Chris Lattnerd78700d2002-08-16 21:14:40 +0000303 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
304 ThrowException("Signed integral constant '" +
305 itostr(D.ConstPool64) + "' is invalid for type '" +
306 Ty->getDescription() + "'!");
307 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000308
309 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000310 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
311 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000312 ThrowException("Integral constant '" + utostr(D.UConstPool64) +
313 "' is invalid or out of range!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000314 } else { // This is really a signed reference. Transmogrify.
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000315 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000316 }
317 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000318 return ConstantUInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000319 }
320
Chris Lattner2079fde2001-10-13 06:41:08 +0000321 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000322 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattner2079fde2001-10-13 06:41:08 +0000323 ThrowException("FP constant invalid for type!!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000324 return ConstantFP::get(Ty, D.ConstPoolFP);
Chris Lattner2079fde2001-10-13 06:41:08 +0000325
326 case ValID::ConstNullVal: // Is it a null value?
Chris Lattner9b625032002-05-06 16:15:30 +0000327 if (!isa<PointerType>(Ty))
Chris Lattner2079fde2001-10-13 06:41:08 +0000328 ThrowException("Cannot create a a non pointer null!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000329 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner2079fde2001-10-13 06:41:08 +0000330
Chris Lattnerd78700d2002-08-16 21:14:40 +0000331 case ValID::ConstantVal: // Fully resolved constant?
332 if (D.ConstantValue->getType() != Ty)
333 ThrowException("Constant expression type different from required type!");
334 return D.ConstantValue;
335
Chris Lattner30c89792001-09-07 16:35:17 +0000336 default:
337 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000338 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000339 } // End of switch
340
Chris Lattner2079fde2001-10-13 06:41:08 +0000341 assert(0 && "Unhandled case!");
342 return 0;
343}
344
345
346// getVal - This function is identical to getValNonImprovising, except that if a
347// value is not already defined, it "improvises" by creating a placeholder var
348// that looks and acts just like the requested variable. When the value is
349// defined later, all uses of the placeholder variable are replaced with the
350// real thing.
351//
352static Value *getVal(const Type *Ty, const ValID &D) {
353 assert(Ty != Type::TypeTy && "Should use getTypeVal for types!");
354
355 // See if the value has already been defined...
356 Value *V = getValNonImprovising(Ty, D);
357 if (V) return V;
Chris Lattner00950542001-06-06 20:29:01 +0000358
359 // If we reached here, we referenced either a symbol that we don't know about
360 // or an id number that hasn't been read yet. We may be referencing something
361 // forward, so just create an entry to be resolved later and get to it...
362 //
Chris Lattner00950542001-06-06 20:29:01 +0000363 Value *d = 0;
Chris Lattner30c89792001-09-07 16:35:17 +0000364 switch (Ty->getPrimitiveID()) {
365 case Type::LabelTyID: d = new BBPlaceHolder(Ty, D); break;
Chris Lattner30c89792001-09-07 16:35:17 +0000366 default: d = new ValuePlaceHolder(Ty, D); break;
Chris Lattner00950542001-06-06 20:29:01 +0000367 }
368
369 assert(d != 0 && "How did we not make something?");
Chris Lattner79df7c02002-03-26 18:01:55 +0000370 if (inFunctionScope())
Chris Lattner386a3b72001-10-16 19:54:17 +0000371 InsertValue(d, CurMeth.LateResolveValues);
372 else
373 InsertValue(d, CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000374 return d;
375}
376
377
378//===----------------------------------------------------------------------===//
379// Code to handle forward references in instructions
380//===----------------------------------------------------------------------===//
381//
382// This code handles the late binding needed with statements that reference
383// values not defined yet... for example, a forward branch, or the PHI node for
384// a loop body.
385//
386// This keeps a table (CurMeth.LateResolveValues) of all such forward references
387// and back patchs after we are done.
388//
389
390// ResolveDefinitions - If we could not resolve some defs at parsing
391// time (forward branches, phi functions for loops, etc...) resolve the
392// defs now...
393//
Chris Lattner386a3b72001-10-16 19:54:17 +0000394static void ResolveDefinitions(vector<ValueList> &LateResolvers,
Chris Lattnerbcafcce2002-07-25 06:17:42 +0000395 vector<ValueList> *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000396 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
397 for (unsigned ty = 0; ty < LateResolvers.size(); ty++) {
398 while (!LateResolvers[ty].empty()) {
399 Value *V = LateResolvers[ty].back();
Chris Lattner386a3b72001-10-16 19:54:17 +0000400 assert(!isa<Type>(V) && "Types should be in LateResolveTypes!");
401
Chris Lattner00950542001-06-06 20:29:01 +0000402 LateResolvers[ty].pop_back();
403 ValID &DID = getValIDFromPlaceHolder(V);
404
Chris Lattner2079fde2001-10-13 06:41:08 +0000405 Value *TheRealValue = getValNonImprovising(Type::getUniqueIDType(ty),DID);
Chris Lattner386a3b72001-10-16 19:54:17 +0000406 if (TheRealValue) {
407 V->replaceAllUsesWith(TheRealValue);
408 delete V;
409 } else if (FutureLateResolvers) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000410 // Functions have their unresolved items forwarded to the module late
Chris Lattner386a3b72001-10-16 19:54:17 +0000411 // resolver table
412 InsertValue(V, *FutureLateResolvers);
413 } else {
Chris Lattner9705a152002-05-02 19:27:42 +0000414 if (DID.Type == ValID::NameVal)
Chris Lattner30c89792001-09-07 16:35:17 +0000415 ThrowException("Reference to an invalid definition: '" +DID.getName()+
416 "' of type '" + V->getType()->getDescription() + "'",
417 getLineNumFromPlaceHolder(V));
418 else
419 ThrowException("Reference to an invalid definition: #" +
420 itostr(DID.Num) + " of type '" +
421 V->getType()->getDescription() + "'",
422 getLineNumFromPlaceHolder(V));
423 }
Chris Lattner00950542001-06-06 20:29:01 +0000424 }
425 }
426
427 LateResolvers.clear();
428}
429
Chris Lattner4a42e902001-10-22 05:56:09 +0000430// ResolveTypeTo - A brand new type was just declared. This means that (if
431// name is not null) things referencing Name can be resolved. Otherwise, things
432// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000433//
Chris Lattner4a42e902001-10-22 05:56:09 +0000434static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000435 vector<PATypeHolder> &Types = inFunctionScope() ?
Chris Lattner4a42e902001-10-22 05:56:09 +0000436 CurMeth.Types : CurModule.Types;
Chris Lattner00950542001-06-06 20:29:01 +0000437
Chris Lattner4a42e902001-10-22 05:56:09 +0000438 ValID D;
439 if (Name) D = ValID::create(Name);
440 else D = ValID::create((int)Types.size());
Chris Lattner30c89792001-09-07 16:35:17 +0000441
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000442 map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner4a42e902001-10-22 05:56:09 +0000443 CurMeth.LateResolveTypes : CurModule.LateResolveTypes;
444
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000445 map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000446 if (I != LateResolver.end()) {
Chris Lattner51727be2002-06-04 21:58:56 +0000447 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattner4a42e902001-10-22 05:56:09 +0000448 LateResolver.erase(I);
449 }
450}
451
452// ResolveTypes - At this point, all types should be resolved. Any that aren't
453// are errors.
454//
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000455static void ResolveTypes(map<ValID, PATypeHolder> &LateResolveTypes) {
Chris Lattner4a42e902001-10-22 05:56:09 +0000456 if (!LateResolveTypes.empty()) {
Chris Lattner82269592001-10-22 06:01:08 +0000457 const ValID &DID = LateResolveTypes.begin()->first;
Chris Lattner4a42e902001-10-22 05:56:09 +0000458
459 if (DID.Type == ValID::NameVal)
Chris Lattner82269592001-10-22 06:01:08 +0000460 ThrowException("Reference to an invalid type: '" +DID.getName() + "'");
Chris Lattner4a42e902001-10-22 05:56:09 +0000461 else
Chris Lattner82269592001-10-22 06:01:08 +0000462 ThrowException("Reference to an invalid type: #" + itostr(DID.Num));
Chris Lattner30c89792001-09-07 16:35:17 +0000463 }
464}
465
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000466
Chris Lattner1781aca2001-09-18 04:00:54 +0000467// setValueName - Set the specified value to the name given. The name may be
468// null potentially, in which case this is a noop. The string passed in is
469// assumed to be a malloc'd string buffer, and is freed by this function.
470//
Chris Lattnerb7474512001-10-03 15:39:04 +0000471// This function returns true if the value has already been defined, but is
472// allowed to be redefined in the specified context. If the name is a new name
473// for the typeplane, false is returned.
474//
475static bool setValueName(Value *V, char *NameStr) {
476 if (NameStr == 0) return false;
Chris Lattner386a3b72001-10-16 19:54:17 +0000477
Chris Lattner1781aca2001-09-18 04:00:54 +0000478 string Name(NameStr); // Copy string
479 free(NameStr); // Free old string
480
Chris Lattner2079fde2001-10-13 06:41:08 +0000481 if (V->getType() == Type::VoidTy)
482 ThrowException("Can't assign name '" + Name +
483 "' to a null valued instruction!");
484
Chris Lattner79df7c02002-03-26 18:01:55 +0000485 SymbolTable *ST = inFunctionScope() ?
486 CurMeth.CurrentFunction->getSymbolTableSure() :
Chris Lattner30c89792001-09-07 16:35:17 +0000487 CurModule.CurrentModule->getSymbolTableSure();
488
489 Value *Existing = ST->lookup(V->getType(), Name);
490 if (Existing) { // Inserting a name that is already defined???
491 // There is only one case where this is allowed: when we are refining an
492 // opaque type. In this case, Existing will be an opaque type.
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000493 if (const Type *Ty = dyn_cast<const Type>(Existing)) {
Chris Lattner51727be2002-06-04 21:58:56 +0000494 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Ty)) {
Chris Lattner30c89792001-09-07 16:35:17 +0000495 // We ARE replacing an opaque type!
Chris Lattner51727be2002-06-04 21:58:56 +0000496 ((OpaqueType*)OpTy)->refineAbstractTypeTo(cast<Type>(V));
Chris Lattnerb7474512001-10-03 15:39:04 +0000497 return true;
Chris Lattner30c89792001-09-07 16:35:17 +0000498 }
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000499 }
Chris Lattner30c89792001-09-07 16:35:17 +0000500
Chris Lattner9636a912001-10-01 16:18:37 +0000501 // Otherwise, we are a simple redefinition of a value, check to see if it
502 // is defined the same as the old one...
503 if (const Type *Ty = dyn_cast<const Type>(Existing)) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000504 if (Ty == cast<const Type>(V)) return true; // Yes, it's equal.
Chris Lattner699f1eb2002-08-14 17:12:33 +0000505 // std::cerr << "Type: " << Ty->getDescription() << " != "
Chris Lattnerb7474512001-10-03 15:39:04 +0000506 // << cast<const Type>(V)->getDescription() << "!\n";
507 } else if (GlobalVariable *EGV = dyn_cast<GlobalVariable>(Existing)) {
Chris Lattner43efcbf2001-10-03 19:35:57 +0000508 // We are allowed to redefine a global variable in two circumstances:
509 // 1. If at least one of the globals is uninitialized or
510 // 2. If both initializers have the same value.
511 //
512 // This can only be done if the const'ness of the vars is the same.
513 //
Chris Lattner89219832001-10-03 19:35:04 +0000514 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
515 if (EGV->isConstant() == GV->isConstant() &&
516 (!EGV->hasInitializer() || !GV->hasInitializer() ||
517 EGV->getInitializer() == GV->getInitializer())) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000518
Chris Lattner89219832001-10-03 19:35:04 +0000519 // Make sure the existing global version gets the initializer!
520 if (GV->hasInitializer() && !EGV->hasInitializer())
521 EGV->setInitializer(GV->getInitializer());
522
Chris Lattner2079fde2001-10-13 06:41:08 +0000523 delete GV; // Destroy the duplicate!
Chris Lattner89219832001-10-03 19:35:04 +0000524 return true; // They are equivalent!
525 }
Chris Lattnerb7474512001-10-03 15:39:04 +0000526 }
Chris Lattner9636a912001-10-01 16:18:37 +0000527 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000528 ThrowException("Redefinition of value named '" + Name + "' in the '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000529 V->getType()->getDescription() + "' type plane!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000530 }
Chris Lattner00950542001-06-06 20:29:01 +0000531
Chris Lattner30c89792001-09-07 16:35:17 +0000532 V->setName(Name, ST);
Chris Lattnerb7474512001-10-03 15:39:04 +0000533 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000534}
535
Chris Lattner8896eda2001-07-09 19:38:36 +0000536
Chris Lattner30c89792001-09-07 16:35:17 +0000537//===----------------------------------------------------------------------===//
538// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000539//
Chris Lattner8896eda2001-07-09 19:38:36 +0000540
Chris Lattner30c89792001-09-07 16:35:17 +0000541// TypeContains - Returns true if Ty contains E in it.
542//
543static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner3ff43872001-09-28 22:56:31 +0000544 return find(df_begin(Ty), df_end(Ty), E) != df_end(Ty);
Chris Lattner30c89792001-09-07 16:35:17 +0000545}
Chris Lattner698b56e2001-07-20 19:15:08 +0000546
Chris Lattner30c89792001-09-07 16:35:17 +0000547
548static vector<pair<unsigned, OpaqueType *> > UpRefs;
549
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000550static PATypeHolder HandleUpRefs(const Type *ty) {
551 PATypeHolder Ty(ty);
Chris Lattner5084d032001-11-02 07:46:26 +0000552 UR_OUT("Type '" << ty->getDescription() <<
553 "' newly formed. Resolving upreferences.\n" <<
554 UpRefs.size() << " upreferences active!\n");
Chris Lattner30c89792001-09-07 16:35:17 +0000555 for (unsigned i = 0; i < UpRefs.size(); ) {
Chris Lattner5084d032001-11-02 07:46:26 +0000556 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Chris Lattner30c89792001-09-07 16:35:17 +0000557 << UpRefs[i].second->getDescription() << ") = "
Chris Lattner5084d032001-11-02 07:46:26 +0000558 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << endl);
Chris Lattner30c89792001-09-07 16:35:17 +0000559 if (TypeContains(Ty, UpRefs[i].second)) {
560 unsigned Level = --UpRefs[i].first; // Decrement level of upreference
Chris Lattner5084d032001-11-02 07:46:26 +0000561 UR_OUT(" Uplevel Ref Level = " << Level << endl);
Chris Lattner30c89792001-09-07 16:35:17 +0000562 if (Level == 0) { // Upreference should be resolved!
Chris Lattner5084d032001-11-02 07:46:26 +0000563 UR_OUT(" * Resolving upreference for "
564 << UpRefs[i].second->getDescription() << endl;
Chris Lattner30c89792001-09-07 16:35:17 +0000565 string OldName = UpRefs[i].second->getDescription());
566 UpRefs[i].second->refineAbstractTypeTo(Ty);
567 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
Chris Lattner5084d032001-11-02 07:46:26 +0000568 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
Chris Lattner30c89792001-09-07 16:35:17 +0000569 << (const void*)Ty << ", " << Ty->getDescription() << endl);
570 continue;
571 }
572 }
573
574 ++i; // Otherwise, no resolve, move on...
Chris Lattner8896eda2001-07-09 19:38:36 +0000575 }
Chris Lattner30c89792001-09-07 16:35:17 +0000576 // FIXME: TODO: this should return the updated type
Chris Lattner8896eda2001-07-09 19:38:36 +0000577 return Ty;
578}
579
Chris Lattner30c89792001-09-07 16:35:17 +0000580
Chris Lattner00950542001-06-06 20:29:01 +0000581//===----------------------------------------------------------------------===//
582// RunVMAsmParser - Define an interface to this parser
583//===----------------------------------------------------------------------===//
584//
Chris Lattnera2850432001-07-22 18:36:00 +0000585Module *RunVMAsmParser(const string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000586 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000587 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000588 llvmAsmlineno = 1; // Reset the current line number...
589
590 CurModule.CurrentModule = new Module(); // Allocate a new module to read
591 yyparse(); // Parse the file.
592 Module *Result = ParserResult;
Chris Lattner00950542001-06-06 20:29:01 +0000593 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
594 ParserResult = 0;
595
596 return Result;
597}
598
599%}
600
601%union {
Chris Lattner30c89792001-09-07 16:35:17 +0000602 Module *ModuleVal;
Chris Lattner79df7c02002-03-26 18:01:55 +0000603 Function *FunctionVal;
Chris Lattner46748042002-04-09 19:41:42 +0000604 std::pair<Argument*, char*> *ArgVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000605 BasicBlock *BasicBlockVal;
606 TerminatorInst *TermInstVal;
607 Instruction *InstVal;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000608 Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000609
Chris Lattner30c89792001-09-07 16:35:17 +0000610 const Type *PrimType;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000611 PATypeHolder *TypeVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000612 Value *ValueVal;
613
Chris Lattner46748042002-04-09 19:41:42 +0000614 std::list<std::pair<Argument*,char*> > *ArgList;
Chris Lattner697954c2002-01-20 22:54:45 +0000615 std::vector<Value*> *ValueList;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000616 std::list<PATypeHolder> *TypeList;
Chris Lattner697954c2002-01-20 22:54:45 +0000617 std::list<std::pair<Value*,
618 BasicBlock*> > *PHIList; // Represent the RHS of PHI node
Chris Lattner46748042002-04-09 19:41:42 +0000619 std::vector<std::pair<Constant*, BasicBlock*> > *JumpTable;
Chris Lattner697954c2002-01-20 22:54:45 +0000620 std::vector<Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000621
Chris Lattner30c89792001-09-07 16:35:17 +0000622 int64_t SInt64Val;
623 uint64_t UInt64Val;
624 int SIntVal;
625 unsigned UIntVal;
626 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000627 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000628
Chris Lattner30c89792001-09-07 16:35:17 +0000629 char *StrVal; // This memory is strdup'd!
630 ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000631
Chris Lattner30c89792001-09-07 16:35:17 +0000632 Instruction::BinaryOps BinaryOpVal;
633 Instruction::TermOps TermOpVal;
634 Instruction::MemoryOps MemOpVal;
635 Instruction::OtherOps OtherOpVal;
Chris Lattner00950542001-06-06 20:29:01 +0000636}
637
Chris Lattner79df7c02002-03-26 18:01:55 +0000638%type <ModuleVal> Module FunctionList
639%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000640%type <BasicBlockVal> BasicBlock InstructionList
641%type <TermInstVal> BBTerminatorInst
642%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000643%type <ConstVal> ConstVal ConstExpr
Chris Lattner6cdb0112001-11-26 16:54:11 +0000644%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000645%type <ArgList> ArgList ArgListH
646%type <ArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000647%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000648%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000649%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000650%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000651%type <JumpTable> JumpTable
Chris Lattnerdda71962001-11-26 18:54:16 +0000652%type <BoolVal> GlobalType OptInternal // GLOBAL or CONSTANT? Intern?
Chris Lattner00950542001-06-06 20:29:01 +0000653
Chris Lattner2079fde2001-10-13 06:41:08 +0000654// ValueRef - Unresolved reference to a definition or BB
655%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000656%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000657// Tokens and types for handling constant integer values
658//
659// ESINT64VAL - A negative number within long long range
660%token <SInt64Val> ESINT64VAL
661
662// EUINT64VAL - A positive number within uns. long long range
663%token <UInt64Val> EUINT64VAL
664%type <SInt64Val> EINT64VAL
665
666%token <SIntVal> SINTVAL // Signed 32 bit ints...
667%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
668%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000669%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000670
671// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000672%type <TypeVal> Types TypesV UpRTypes UpRTypesV
673%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattner30c89792001-09-07 16:35:17 +0000674%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
675%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000676
677%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
Chris Lattner8ebccb72002-05-22 22:33:00 +0000678%type <StrVal> OptVAR_ID OptAssign FuncName
Chris Lattner00950542001-06-06 20:29:01 +0000679
680
Chris Lattner9b02cc32002-05-03 18:23:48 +0000681%token IMPLEMENTATION TRUE FALSE BEGINTOK ENDTOK DECLARE GLOBAL CONSTANT UNINIT
Chris Lattnerd78700d2002-08-16 21:14:40 +0000682%token TO EXCEPT DOTDOTDOT NULL_TOK CONST INTERNAL OPAQUE NOT
Chris Lattner00950542001-06-06 20:29:01 +0000683
684// Basic Block Terminating Operators
685%token <TermOpVal> RET BR SWITCH
686
Chris Lattner00950542001-06-06 20:29:01 +0000687// Binary Operators
688%type <BinaryOpVal> BinaryOps // all the binary operators
Chris Lattner42c9e772001-10-20 09:32:59 +0000689%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000690%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000691
692// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000693%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000694
Chris Lattner027dcc52001-07-08 21:10:27 +0000695// Other Operators
696%type <OtherOpVal> ShiftOps
Chris Lattner2079fde2001-10-13 06:41:08 +0000697%token <OtherOpVal> PHI CALL INVOKE CAST SHL SHR
Chris Lattner027dcc52001-07-08 21:10:27 +0000698
Chris Lattner00950542001-06-06 20:29:01 +0000699%start Module
700%%
701
702// Handle constant integer size restriction and conversion...
703//
704
Chris Lattner51727be2002-06-04 21:58:56 +0000705INTVAL : SINTVAL;
Chris Lattner00950542001-06-06 20:29:01 +0000706INTVAL : UINTVAL {
707 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
708 ThrowException("Value too large for type!");
709 $$ = (int32_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000710};
Chris Lattner00950542001-06-06 20:29:01 +0000711
712
Chris Lattner51727be2002-06-04 21:58:56 +0000713EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner00950542001-06-06 20:29:01 +0000714EINT64VAL : EUINT64VAL {
715 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
716 ThrowException("Value too large for type!");
717 $$ = (int64_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000718};
Chris Lattner00950542001-06-06 20:29:01 +0000719
Chris Lattner00950542001-06-06 20:29:01 +0000720// Operations that are notably excluded from this list include:
721// RET, BR, & SWITCH because they end basic blocks and are treated specially.
722//
Chris Lattner51727be2002-06-04 21:58:56 +0000723BinaryOps : ADD | SUB | MUL | DIV | REM | AND | OR | XOR;
724BinaryOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
725ShiftOps : SHL | SHR;
Chris Lattner00950542001-06-06 20:29:01 +0000726
Chris Lattnere98dda62001-07-14 06:10:16 +0000727// These are some types that allow classification if we only want a particular
728// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner51727be2002-06-04 21:58:56 +0000729SIntType : LONG | INT | SHORT | SBYTE;
730UIntType : ULONG | UINT | USHORT | UBYTE;
731IntType : SIntType | UIntType;
732FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +0000733
Chris Lattnere98dda62001-07-14 06:10:16 +0000734// OptAssign - Value producing statements have an optional assignment component
Chris Lattner00950542001-06-06 20:29:01 +0000735OptAssign : VAR_ID '=' {
736 $$ = $1;
737 }
738 | /*empty*/ {
739 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +0000740 };
Chris Lattner00950542001-06-06 20:29:01 +0000741
Chris Lattner51727be2002-06-04 21:58:56 +0000742OptInternal : INTERNAL { $$ = true; } | /*empty*/ { $$ = false; };
Chris Lattner30c89792001-09-07 16:35:17 +0000743
744//===----------------------------------------------------------------------===//
745// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +0000746// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +0000747// access to it, a user must explicitly use TypesV.
748//
749
750// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +0000751TypesV : Types | VOID { $$ = new PATypeHolder($1); };
752UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +0000753
754Types : UpRTypes {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000755 if (UpRefs.size())
756 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
757 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000758 };
Chris Lattner30c89792001-09-07 16:35:17 +0000759
760
761// Derived types are added later...
762//
Chris Lattner51727be2002-06-04 21:58:56 +0000763PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
764PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000765UpRTypes : OPAQUE {
766 $$ = new PATypeHolder(OpaqueType::get());
767 }
768 | PrimType {
769 $$ = new PATypeHolder($1);
Chris Lattner51727be2002-06-04 21:58:56 +0000770 };
Chris Lattnerd78700d2002-08-16 21:14:40 +0000771UpRTypes : SymbolicValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000772 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner51727be2002-06-04 21:58:56 +0000773};
Chris Lattner30c89792001-09-07 16:35:17 +0000774
Chris Lattner30c89792001-09-07 16:35:17 +0000775// Include derived types in the Types production.
776//
777UpRTypes : '\\' EUINT64VAL { // Type UpReference
778 if ($2 > (uint64_t)INT64_MAX) ThrowException("Value out of range!");
779 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
780 UpRefs.push_back(make_pair((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000781 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +0000782 UR_OUT("New Upreference!\n");
783 }
Chris Lattner79df7c02002-03-26 18:01:55 +0000784 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner30c89792001-09-07 16:35:17 +0000785 vector<const Type*> Params;
Chris Lattner697954c2002-01-20 22:54:45 +0000786 mapto($3->begin(), $3->end(), std::back_inserter(Params),
787 std::mem_fun_ref(&PATypeHandle<Type>::get));
Chris Lattner2079fde2001-10-13 06:41:08 +0000788 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
789 if (isVarArg) Params.pop_back();
790
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000791 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +0000792 delete $3; // Delete the argument list
793 delete $1; // Delete the old type handle
794 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000795 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000796 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000797 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +0000798 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000799 | '{' TypeListI '}' { // Structure type?
800 vector<const Type*> Elements;
Chris Lattner697954c2002-01-20 22:54:45 +0000801 mapto($2->begin(), $2->end(), std::back_inserter(Elements),
802 std::mem_fun_ref(&PATypeHandle<Type>::get));
Chris Lattner30c89792001-09-07 16:35:17 +0000803
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000804 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000805 delete $2;
806 }
807 | '{' '}' { // Empty structure type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000808 $$ = new PATypeHolder(StructType::get(vector<const Type*>()));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000809 }
810 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000811 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000812 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000813 };
Chris Lattner30c89792001-09-07 16:35:17 +0000814
Chris Lattner7e708292002-06-25 16:13:24 +0000815// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +0000816// declaration type lists
817//
818TypeListI : UpRTypes {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000819 $$ = new list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +0000820 $$->push_back(*$1); delete $1;
821 }
822 | TypeListI ',' UpRTypes {
823 ($$=$1)->push_back(*$3); delete $3;
Chris Lattner51727be2002-06-04 21:58:56 +0000824 };
Chris Lattner30c89792001-09-07 16:35:17 +0000825
Chris Lattner7e708292002-06-25 16:13:24 +0000826// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +0000827ArgTypeListI : TypeListI
828 | TypeListI ',' DOTDOTDOT {
829 ($$=$1)->push_back(Type::VoidTy);
830 }
831 | DOTDOTDOT {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000832 ($$ = new list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattner30c89792001-09-07 16:35:17 +0000833 }
834 | /*empty*/ {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000835 $$ = new list<PATypeHolder>();
Chris Lattner51727be2002-06-04 21:58:56 +0000836 };
Chris Lattner30c89792001-09-07 16:35:17 +0000837
Chris Lattnere98dda62001-07-14 06:10:16 +0000838// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +0000839// production is used ONLY to represent constants that show up AFTER a 'const',
840// 'constant' or 'global' token at global scope. Constants that can be inlined
841// into other expressions (such as integers and constexprs) are handled by the
842// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +0000843//
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000844ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
845 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
846 if (ATy == 0)
847 ThrowException("Cannot make array constant with type: '" +
848 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +0000849 const Type *ETy = ATy->getElementType();
850 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +0000851
Chris Lattner30c89792001-09-07 16:35:17 +0000852 // Verify that we have the correct size...
853 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +0000854 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattner30c89792001-09-07 16:35:17 +0000855 utostr($3->size()) + " arguments, but has size of " +
856 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +0000857
Chris Lattner30c89792001-09-07 16:35:17 +0000858 // Verify all elements are correct type!
859 for (unsigned i = 0; i < $3->size(); i++) {
860 if (ETy != (*$3)[i]->getType())
Chris Lattner00950542001-06-06 20:29:01 +0000861 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +0000862 ETy->getDescription() +"' as required!\nIt is of type '"+
863 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +0000864 }
865
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000866 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +0000867 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000868 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000869 | Types '[' ']' {
870 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
871 if (ATy == 0)
872 ThrowException("Cannot make array constant with type: '" +
873 (*$1)->getDescription() + "'!");
874
875 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +0000876 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +0000877 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattner30c89792001-09-07 16:35:17 +0000878 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000879 $$ = ConstantArray::get(ATy, vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +0000880 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +0000881 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000882 | Types 'c' STRINGCONSTANT {
883 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
884 if (ATy == 0)
885 ThrowException("Cannot make array constant with type: '" +
886 (*$1)->getDescription() + "'!");
887
Chris Lattner30c89792001-09-07 16:35:17 +0000888 int NumElements = ATy->getNumElements();
889 const Type *ETy = ATy->getElementType();
890 char *EndStr = UnEscapeLexed($3, true);
891 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +0000892 ThrowException("Can't build string constant of size " +
Chris Lattner30c89792001-09-07 16:35:17 +0000893 itostr((int)(EndStr-$3)) +
894 " when array has size " + itostr(NumElements) + "!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000895 vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +0000896 if (ETy == Type::SByteTy) {
897 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000898 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +0000899 } else if (ETy == Type::UByteTy) {
900 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000901 Vals.push_back(ConstantUInt::get(ETy, *C));
Chris Lattner93750fa2001-07-28 17:48:55 +0000902 } else {
Chris Lattner30c89792001-09-07 16:35:17 +0000903 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +0000904 ThrowException("Cannot build string arrays of non byte sized elements!");
905 }
Chris Lattner30c89792001-09-07 16:35:17 +0000906 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000907 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +0000908 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +0000909 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000910 | Types '{' ConstVector '}' {
911 const StructType *STy = dyn_cast<const StructType>($1->get());
912 if (STy == 0)
913 ThrowException("Cannot make struct constant with type: '" +
914 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +0000915 // FIXME: TODO: Check to see that the constants are compatible with the type
916 // initializer!
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000917 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +0000918 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000919 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000920 | Types NULL_TOK {
921 const PointerType *PTy = dyn_cast<const PointerType>($1->get());
922 if (PTy == 0)
923 ThrowException("Cannot make null pointer constant with type: '" +
924 (*$1)->getDescription() + "'!");
925
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000926 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000927 delete $1;
928 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000929 | Types SymbolicValueRef {
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000930 const PointerType *Ty = dyn_cast<const PointerType>($1->get());
931 if (Ty == 0)
932 ThrowException("Global const reference must be a pointer type!");
933
Chris Lattner3101c252002-08-15 17:58:33 +0000934 // ConstExprs can exist in the body of a function, thus creating
935 // ConstantPointerRefs whenever they refer to a variable. Because we are in
936 // the context of a function, getValNonImprovising will search the functions
937 // symbol table instead of the module symbol table for the global symbol,
938 // which throws things all off. To get around this, we just tell
939 // getValNonImprovising that we are at global scope here.
940 //
941 Function *SavedCurFn = CurMeth.CurrentFunction;
942 CurMeth.CurrentFunction = 0;
943
Chris Lattner2079fde2001-10-13 06:41:08 +0000944 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000945
Chris Lattner3101c252002-08-15 17:58:33 +0000946 CurMeth.CurrentFunction = SavedCurFn;
947
948
Chris Lattner2079fde2001-10-13 06:41:08 +0000949 // If this is an initializer for a constant pointer, which is referencing a
950 // (currently) undefined variable, create a stub now that shall be replaced
951 // in the future with the right type of variable.
952 //
953 if (V == 0) {
954 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
955 const PointerType *PT = cast<PointerType>(Ty);
956
957 // First check to see if the forward references value is already created!
958 PerModuleInfo::GlobalRefsType::iterator I =
959 CurModule.GlobalRefs.find(make_pair(PT, $2));
960
961 if (I != CurModule.GlobalRefs.end()) {
962 V = I->second; // Placeholder already exists, use it...
963 } else {
964 // TODO: Include line number info by creating a subclass of
965 // TODO: GlobalVariable here that includes the said information!
966
967 // Create a placeholder for the global variable reference...
Chris Lattner7a176752001-12-04 00:03:30 +0000968 GlobalVariable *GV = new GlobalVariable(PT->getElementType(),
969 false, true);
Chris Lattner2079fde2001-10-13 06:41:08 +0000970 // Keep track of the fact that we have a forward ref to recycle it
971 CurModule.GlobalRefs.insert(make_pair(make_pair(PT, $2), GV));
972
973 // Must temporarily push this value into the module table...
974 CurModule.CurrentModule->getGlobalList().push_back(GV);
975 V = GV;
976 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000977 }
978
Chris Lattner2079fde2001-10-13 06:41:08 +0000979 GlobalValue *GV = cast<GlobalValue>(V);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000980 $$ = ConstantPointerRef::get(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +0000981 delete $1; // Free the type handle
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000982 }
Chris Lattnerd78700d2002-08-16 21:14:40 +0000983 | Types ConstExpr {
984 if ($1->get() != $2->getType())
985 ThrowException("Mismatched types for constant expression!");
986 $$ = $2;
987 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000988 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000989
Chris Lattnerd05e3592002-08-15 18:17:28 +0000990ConstVal : SIntType EINT64VAL { // integral constants
991 if (!ConstantSInt::isValueValidForType($1, $2))
992 ThrowException("Constant value doesn't fit in type!");
993 $$ = ConstantSInt::get($1, $2);
994 }
995 | UIntType EUINT64VAL { // integral constants
996 if (!ConstantUInt::isValueValidForType($1, $2))
997 ThrowException("Constant value doesn't fit in type!");
998 $$ = ConstantUInt::get($1, $2);
999 }
1000 | BOOL TRUE { // Boolean constants
1001 $$ = ConstantBool::True;
1002 }
1003 | BOOL FALSE { // Boolean constants
1004 $$ = ConstantBool::False;
1005 }
1006 | FPType FPVAL { // Float & Double constants
1007 $$ = ConstantFP::get($1, $2);
1008 };
1009
Chris Lattner00950542001-06-06 20:29:01 +00001010
Chris Lattnerd78700d2002-08-16 21:14:40 +00001011ConstExpr: CAST '(' ConstVal TO Types ')' {
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001012 $$ = ConstantExpr::getCast($3, $5->get());
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001013 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001014 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001015 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1016 if (!isa<PointerType>($3->getType()))
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001017 ThrowException("GetElementPtr requires a pointer operand!");
1018
1019 const Type *IdxTy =
Chris Lattnerd78700d2002-08-16 21:14:40 +00001020 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001021 if (!IdxTy)
1022 ThrowException("Index list invalid for constant getelementptr!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001023
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001024 vector<Constant*> IdxVec;
Chris Lattnerd78700d2002-08-16 21:14:40 +00001025 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1026 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001027 IdxVec.push_back(C);
1028 else
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001029 ThrowException("Indices to constant getelementptr must be constants!");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001030
Chris Lattnerd78700d2002-08-16 21:14:40 +00001031 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001032
Chris Lattnerd78700d2002-08-16 21:14:40 +00001033 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001034 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001035 | BinaryOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001036 if ($3->getType() != $5->getType())
1037 ThrowException("Binary operator types must match!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001038 $$ = ConstantExpr::get($1, $3, $5);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001039 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001040 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001041 if ($5->getType() != Type::UByteTy)
1042 ThrowException("Shift count for shift constant must be unsigned byte!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001043 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattner699f1eb2002-08-14 17:12:33 +00001044 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001045
1046
Chris Lattnere98dda62001-07-14 06:10:16 +00001047// ConstVector - A list of comma seperated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001048ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001049 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001050 }
1051 | ConstVal {
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001052 $$ = new vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001053 $$->push_back($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001054 };
Chris Lattner00950542001-06-06 20:29:01 +00001055
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001056
Chris Lattner1781aca2001-09-18 04:00:54 +00001057// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001058GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001059
Chris Lattner00950542001-06-06 20:29:01 +00001060
Chris Lattner0e73ce62002-05-02 19:11:13 +00001061//===----------------------------------------------------------------------===//
1062// Rules to match Modules
1063//===----------------------------------------------------------------------===//
1064
1065// Module rule: Capture the result of parsing the whole file into a result
1066// variable...
1067//
1068Module : FunctionList {
1069 $$ = ParserResult = $1;
1070 CurModule.ModuleDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001071};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001072
Chris Lattner7e708292002-06-25 16:13:24 +00001073// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001074//
1075FunctionList : FunctionList Function {
1076 $$ = $1;
1077 assert($2->getParent() == 0 && "Function already in module!");
1078 $1->getFunctionList().push_back($2);
1079 CurMeth.FunctionDone();
1080 }
1081 | FunctionList FunctionProto {
1082 $$ = $1;
1083 }
1084 | FunctionList IMPLEMENTATION {
1085 $$ = $1;
1086 }
1087 | ConstPool {
1088 $$ = CurModule.CurrentModule;
1089 // Resolve circular types before we parse the body of the module
1090 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001091 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001092
Chris Lattnere98dda62001-07-14 06:10:16 +00001093// ConstPool - Constants with optional names assigned to them.
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001094ConstPool : ConstPool OptAssign CONST ConstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001095 if (setValueName($4, $2)) { assert(0 && "No redefinitions allowed!"); }
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001096 InsertValue($4);
Chris Lattner00950542001-06-06 20:29:01 +00001097 }
Chris Lattner30c89792001-09-07 16:35:17 +00001098 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner4a42e902001-10-22 05:56:09 +00001099 // Eagerly resolve types. This is not an optimization, this is a
1100 // requirement that is due to the fact that we could have this:
1101 //
1102 // %list = type { %list * }
1103 // %list = type { %list * } ; repeated type decl
1104 //
1105 // If types are not resolved eagerly, then the two types will not be
1106 // determined to be the same type!
1107 //
1108 ResolveTypeTo($2, $4->get());
1109
Chris Lattner1781aca2001-09-18 04:00:54 +00001110 // TODO: FIXME when Type are not const
Chris Lattnerb7474512001-10-03 15:39:04 +00001111 if (!setValueName(const_cast<Type*>($4->get()), $2)) {
1112 // If this is not a redefinition of a type...
1113 if (!$2) {
1114 InsertType($4->get(),
Chris Lattner79df7c02002-03-26 18:01:55 +00001115 inFunctionScope() ? CurMeth.Types : CurModule.Types);
Chris Lattnerb7474512001-10-03 15:39:04 +00001116 }
Chris Lattner30c89792001-09-07 16:35:17 +00001117 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001118
1119 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001120 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001121 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001122 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001123 | ConstPool OptAssign OptInternal GlobalType ConstVal {
1124 const Type *Ty = $5->getType();
Chris Lattner1781aca2001-09-18 04:00:54 +00001125 // Global declarations appear in Constant Pool
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001126 Constant *Initializer = $5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001127 if (Initializer == 0)
1128 ThrowException("Global value initializer is not a constant!");
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001129
Chris Lattnerdda71962001-11-26 18:54:16 +00001130 GlobalVariable *GV = new GlobalVariable(Ty, $4, $3, Initializer);
Chris Lattnerb7474512001-10-03 15:39:04 +00001131 if (!setValueName(GV, $2)) { // If not redefining...
1132 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001133 int Slot = InsertValue(GV, CurModule.Values);
1134
1135 if (Slot != -1) {
1136 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1137 } else {
1138 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1139 (char*)GV->getName().c_str()));
1140 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001141 }
Chris Lattner1781aca2001-09-18 04:00:54 +00001142 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001143 | ConstPool OptAssign OptInternal UNINIT GlobalType Types {
1144 const Type *Ty = *$6;
Chris Lattner1781aca2001-09-18 04:00:54 +00001145 // Global declarations appear in Constant Pool
Chris Lattnerdda71962001-11-26 18:54:16 +00001146 GlobalVariable *GV = new GlobalVariable(Ty, $5, $3);
Chris Lattnerb7474512001-10-03 15:39:04 +00001147 if (!setValueName(GV, $2)) { // If not redefining...
1148 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001149 int Slot = InsertValue(GV, CurModule.Values);
1150
1151 if (Slot != -1) {
1152 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1153 } else {
1154 assert(GV->hasName() && "Not named and not numbered!?");
1155 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1156 (char*)GV->getName().c_str()));
1157 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001158 }
Chris Lattner09c07532002-03-31 07:16:49 +00001159 delete $6;
Chris Lattnere98dda62001-07-14 06:10:16 +00001160 }
Chris Lattner00950542001-06-06 20:29:01 +00001161 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001162 };
Chris Lattner00950542001-06-06 20:29:01 +00001163
1164
1165//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001166// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001167//===----------------------------------------------------------------------===//
1168
Chris Lattner51727be2002-06-04 21:58:56 +00001169OptVAR_ID : VAR_ID | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001170
1171ArgVal : Types OptVAR_ID {
Chris Lattner46748042002-04-09 19:41:42 +00001172 $$ = new pair<Argument*, char*>(new Argument(*$1), $2);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001173 delete $1; // Delete the type handle..
Chris Lattner51727be2002-06-04 21:58:56 +00001174};
Chris Lattner00950542001-06-06 20:29:01 +00001175
1176ArgListH : ArgVal ',' ArgListH {
1177 $$ = $3;
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001178 $3->push_front(*$1);
1179 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001180 }
1181 | ArgVal {
Chris Lattner46748042002-04-09 19:41:42 +00001182 $$ = new list<pair<Argument*,char*> >();
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001183 $$->push_front(*$1);
1184 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001185 }
Chris Lattner8b81bf52001-07-25 22:47:46 +00001186 | DOTDOTDOT {
Chris Lattner46748042002-04-09 19:41:42 +00001187 $$ = new list<pair<Argument*, char*> >();
1188 $$->push_front(pair<Argument*,char*>(new Argument(Type::VoidTy), 0));
Chris Lattner51727be2002-06-04 21:58:56 +00001189 };
Chris Lattner00950542001-06-06 20:29:01 +00001190
1191ArgList : ArgListH {
1192 $$ = $1;
1193 }
1194 | /* empty */ {
1195 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001196 };
Chris Lattner00950542001-06-06 20:29:01 +00001197
Chris Lattner8ebccb72002-05-22 22:33:00 +00001198FuncName : VAR_ID | STRINGCONSTANT;
1199
1200FunctionHeaderH : OptInternal TypesV FuncName '(' ArgList ')' {
Chris Lattnerdda71962001-11-26 18:54:16 +00001201 UnEscapeLexed($3);
Chris Lattner79df7c02002-03-26 18:01:55 +00001202 string FunctionName($3);
Chris Lattnerdda71962001-11-26 18:54:16 +00001203
Chris Lattner30c89792001-09-07 16:35:17 +00001204 vector<const Type*> ParamTypeList;
Chris Lattnerdda71962001-11-26 18:54:16 +00001205 if ($5)
Chris Lattner46748042002-04-09 19:41:42 +00001206 for (list<pair<Argument*,char*> >::iterator I = $5->begin();
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001207 I != $5->end(); ++I)
1208 ParamTypeList.push_back(I->first->getType());
Chris Lattner00950542001-06-06 20:29:01 +00001209
Chris Lattner2079fde2001-10-13 06:41:08 +00001210 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1211 if (isVarArg) ParamTypeList.pop_back();
1212
Chris Lattner79df7c02002-03-26 18:01:55 +00001213 const FunctionType *MT = FunctionType::get(*$2, ParamTypeList, isVarArg);
Chris Lattneref9c23f2001-10-03 14:53:21 +00001214 const PointerType *PMT = PointerType::get(MT);
Chris Lattnerdda71962001-11-26 18:54:16 +00001215 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001216
Chris Lattner79df7c02002-03-26 18:01:55 +00001217 Function *M = 0;
Chris Lattnere1815642001-07-15 06:35:53 +00001218 if (SymbolTable *ST = CurModule.CurrentModule->getSymbolTable()) {
Chris Lattner79df7c02002-03-26 18:01:55 +00001219 // Is the function already in symtab?
1220 if (Value *V = ST->lookup(PMT, FunctionName)) {
1221 M = cast<Function>(V);
Chris Lattner00950542001-06-06 20:29:01 +00001222
Chris Lattnere1815642001-07-15 06:35:53 +00001223 // Yes it is. If this is the case, either we need to be a forward decl,
1224 // or it needs to be.
1225 if (!CurMeth.isDeclare && !M->isExternal())
Chris Lattner7e708292002-06-25 16:13:24 +00001226 ThrowException("Redefinition of function '" + FunctionName + "'!");
Chris Lattner34538142002-03-08 19:11:42 +00001227
Chris Lattner5659dd12002-07-15 00:10:33 +00001228 // Make sure that we keep track of the internal marker, even if there was
1229 // a previous "declare".
1230 if ($1)
1231 M->setInternalLinkage(true);
1232
Chris Lattner7e708292002-06-25 16:13:24 +00001233 // If we found a preexisting function prototype, remove it from the
1234 // module, so that we don't get spurious conflicts with global & local
1235 // variables.
Chris Lattner34538142002-03-08 19:11:42 +00001236 //
Chris Lattner79df7c02002-03-26 18:01:55 +00001237 CurModule.CurrentModule->getFunctionList().remove(M);
Chris Lattnere1815642001-07-15 06:35:53 +00001238 }
1239 }
1240
1241 if (M == 0) { // Not already defined?
Chris Lattner79df7c02002-03-26 18:01:55 +00001242 M = new Function(MT, $1, FunctionName);
Chris Lattnere1815642001-07-15 06:35:53 +00001243 InsertValue(M, CurModule.Values);
Chris Lattnerdda71962001-11-26 18:54:16 +00001244 CurModule.DeclareNewGlobalValue(M, ValID::create($3));
Chris Lattnere1815642001-07-15 06:35:53 +00001245 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001246 free($3); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +00001247
Chris Lattner79df7c02002-03-26 18:01:55 +00001248 CurMeth.FunctionStart(M);
Chris Lattner00950542001-06-06 20:29:01 +00001249
Chris Lattner7e708292002-06-25 16:13:24 +00001250 // Add all of the arguments we parsed to the function...
Chris Lattnerdda71962001-11-26 18:54:16 +00001251 if ($5 && !CurMeth.isDeclare) { // Is null if empty...
Chris Lattner46748042002-04-09 19:41:42 +00001252 for (list<pair<Argument*, char*> >::iterator I = $5->begin();
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001253 I != $5->end(); ++I) {
1254 if (setValueName(I->first, I->second)) { // Insert into symtab...
1255 assert(0 && "No arg redef allowed!");
1256 }
1257
1258 InsertValue(I->first);
Chris Lattner7e708292002-06-25 16:13:24 +00001259 M->getArgumentList().push_back(I->first);
Chris Lattner00950542001-06-06 20:29:01 +00001260 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001261 delete $5; // We're now done with the argument list
Chris Lattner9176fe42002-03-08 18:57:56 +00001262 } else if ($5) {
1263 // If we are a declaration, we should free the memory for the argument list!
Chris Lattner46748042002-04-09 19:41:42 +00001264 for (list<pair<Argument*, char*> >::iterator I = $5->begin(), E = $5->end();
1265 I != E; ++I) {
Chris Lattner9176fe42002-03-08 18:57:56 +00001266 if (I->second) free(I->second); // Free the memory for the name...
Chris Lattner09c07532002-03-31 07:16:49 +00001267 delete I->first; // Free the unused function argument
1268 }
Chris Lattner9176fe42002-03-08 18:57:56 +00001269 delete $5; // Free the memory for the list itself
Chris Lattner00950542001-06-06 20:29:01 +00001270 }
Chris Lattner51727be2002-06-04 21:58:56 +00001271};
Chris Lattner00950542001-06-06 20:29:01 +00001272
Chris Lattner9b02cc32002-05-03 18:23:48 +00001273BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1274
1275FunctionHeader : FunctionHeaderH BEGIN {
Chris Lattner79df7c02002-03-26 18:01:55 +00001276 $$ = CurMeth.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001277
Chris Lattner7e708292002-06-25 16:13:24 +00001278 // Resolve circular types before we parse the body of the function.
Chris Lattner30c89792001-09-07 16:35:17 +00001279 ResolveTypes(CurMeth.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001280};
Chris Lattner00950542001-06-06 20:29:01 +00001281
Chris Lattner9b02cc32002-05-03 18:23:48 +00001282END : ENDTOK | '}'; // Allow end of '}' to end a function
1283
Chris Lattner79df7c02002-03-26 18:01:55 +00001284Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001285 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001286};
Chris Lattner00950542001-06-06 20:29:01 +00001287
Chris Lattner79df7c02002-03-26 18:01:55 +00001288FunctionProto : DECLARE { CurMeth.isDeclare = true; } FunctionHeaderH {
1289 $$ = CurMeth.CurrentFunction;
1290 assert($$->getParent() == 0 && "Function already in module!");
1291 CurModule.CurrentModule->getFunctionList().push_back($$);
1292 CurMeth.FunctionDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001293};
Chris Lattner00950542001-06-06 20:29:01 +00001294
1295//===----------------------------------------------------------------------===//
1296// Rules to match Basic Blocks
1297//===----------------------------------------------------------------------===//
1298
1299ConstValueRef : ESINT64VAL { // A reference to a direct constant
1300 $$ = ValID::create($1);
1301 }
1302 | EUINT64VAL {
1303 $$ = ValID::create($1);
1304 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001305 | FPVAL { // Perhaps it's an FP constant?
1306 $$ = ValID::create($1);
1307 }
Chris Lattner00950542001-06-06 20:29:01 +00001308 | TRUE {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001309 $$ = ValID::create(ConstantBool::True);
Chris Lattner00950542001-06-06 20:29:01 +00001310 }
1311 | FALSE {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001312 $$ = ValID::create(ConstantBool::False);
Chris Lattner00950542001-06-06 20:29:01 +00001313 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001314 | NULL_TOK {
1315 $$ = ValID::createNull();
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001316 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001317 | ConstExpr {
1318 $$ = ValID::create($1);
1319 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00001320
Chris Lattner2079fde2001-10-13 06:41:08 +00001321// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1322// another value.
1323//
1324SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001325 $$ = ValID::create($1);
1326 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001327 | VAR_ID { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001328 $$ = ValID::create($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001329 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001330
1331// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00001332ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00001333
Chris Lattner00950542001-06-06 20:29:01 +00001334
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001335// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1336// type immediately preceeds the value reference, and allows complex constant
1337// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001338ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001339 $$ = getVal(*$1, $2); delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001340 };
Chris Lattner8b81bf52001-07-25 22:47:46 +00001341
Chris Lattner00950542001-06-06 20:29:01 +00001342BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner7e708292002-06-25 16:13:24 +00001343 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner00950542001-06-06 20:29:01 +00001344 }
Chris Lattner7e708292002-06-25 16:13:24 +00001345 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
1346 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner51727be2002-06-04 21:58:56 +00001347 };
Chris Lattner00950542001-06-06 20:29:01 +00001348
1349
1350// Basic blocks are terminated by branching instructions:
1351// br, br/cc, switch, ret
1352//
Chris Lattner2079fde2001-10-13 06:41:08 +00001353BasicBlock : InstructionList OptAssign BBTerminatorInst {
1354 if (setValueName($3, $2)) { assert(0 && "No redefn allowed!"); }
1355 InsertValue($3);
1356
1357 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001358 InsertValue($1);
1359 $$ = $1;
1360 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001361 | LABELSTR InstructionList OptAssign BBTerminatorInst {
1362 if (setValueName($4, $3)) { assert(0 && "No redefn allowed!"); }
1363 InsertValue($4);
1364
1365 $2->getInstList().push_back($4);
Chris Lattnerb7474512001-10-03 15:39:04 +00001366 if (setValueName($2, $1)) { assert(0 && "No label redef allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001367
1368 InsertValue($2);
1369 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001370 };
Chris Lattner00950542001-06-06 20:29:01 +00001371
1372InstructionList : InstructionList Inst {
1373 $1->getInstList().push_back($2);
1374 $$ = $1;
1375 }
1376 | /* empty */ {
Chris Lattner0383cc42002-08-21 23:51:21 +00001377 $$ = CurBB = new BasicBlock();
Chris Lattner51727be2002-06-04 21:58:56 +00001378 };
Chris Lattner00950542001-06-06 20:29:01 +00001379
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001380BBTerminatorInst : RET ResolvedVal { // Return with a result...
1381 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001382 }
1383 | RET VOID { // Return with no result...
1384 $$ = new ReturnInst();
1385 }
1386 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner9636a912001-10-01 16:18:37 +00001387 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $3)));
Chris Lattner00950542001-06-06 20:29:01 +00001388 } // Conditional Branch...
1389 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner9636a912001-10-01 16:18:37 +00001390 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $6)),
1391 cast<BasicBlock>(getVal(Type::LabelTy, $9)),
Chris Lattner00950542001-06-06 20:29:01 +00001392 getVal(Type::BoolTy, $3));
1393 }
1394 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1395 SwitchInst *S = new SwitchInst(getVal($2, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001396 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
Chris Lattner00950542001-06-06 20:29:01 +00001397 $$ = S;
1398
Chris Lattner46748042002-04-09 19:41:42 +00001399 vector<pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
1400 E = $8->end();
1401 for (; I != E; ++I)
Chris Lattner00950542001-06-06 20:29:01 +00001402 S->dest_push_back(I->first, I->second);
1403 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001404 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO ResolvedVal
1405 EXCEPT ResolvedVal {
1406 const PointerType *PMTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001407 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00001408
1409 if (!(PMTy = dyn_cast<PointerType>($2->get())) ||
Chris Lattner79df7c02002-03-26 18:01:55 +00001410 !(Ty = dyn_cast<FunctionType>(PMTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00001411 // Pull out the types of all of the arguments...
1412 vector<const Type*> ParamTypes;
1413 if ($5) {
Chris Lattner6cdb0112001-11-26 16:54:11 +00001414 for (vector<Value*>::iterator I = $5->begin(), E = $5->end(); I!=E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00001415 ParamTypes.push_back((*I)->getType());
1416 }
1417
1418 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1419 if (isVarArg) ParamTypes.pop_back();
1420
Chris Lattner79df7c02002-03-26 18:01:55 +00001421 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattner2079fde2001-10-13 06:41:08 +00001422 PMTy = PointerType::get(Ty);
1423 }
1424 delete $2;
1425
Chris Lattner7e708292002-06-25 16:13:24 +00001426 Value *V = getVal(PMTy, $3); // Get the function we're calling...
Chris Lattner2079fde2001-10-13 06:41:08 +00001427
1428 BasicBlock *Normal = dyn_cast<BasicBlock>($8);
1429 BasicBlock *Except = dyn_cast<BasicBlock>($10);
1430
1431 if (Normal == 0 || Except == 0)
1432 ThrowException("Invoke instruction without label destinations!");
1433
1434 // Create the call node...
1435 if (!$5) { // Has no arguments?
Chris Lattner386a3b72001-10-16 19:54:17 +00001436 $$ = new InvokeInst(V, Normal, Except, vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001437 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001438 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00001439 // correctly!
1440 //
Chris Lattner79df7c02002-03-26 18:01:55 +00001441 FunctionType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1442 FunctionType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
Chris Lattner6cdb0112001-11-26 16:54:11 +00001443 vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00001444
1445 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1446 if ((*ArgI)->getType() != *I)
1447 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001448 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001449
1450 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1451 ThrowException("Invalid number of parameters detected!");
1452
Chris Lattner6cdb0112001-11-26 16:54:11 +00001453 $$ = new InvokeInst(V, Normal, Except, *$5);
Chris Lattner2079fde2001-10-13 06:41:08 +00001454 }
1455 delete $5;
Chris Lattner51727be2002-06-04 21:58:56 +00001456 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001457
1458
Chris Lattner00950542001-06-06 20:29:01 +00001459
1460JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1461 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001462 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001463 if (V == 0)
1464 ThrowException("May only switch on a constant pool value!");
1465
Chris Lattner9636a912001-10-01 16:18:37 +00001466 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($5, $6))));
Chris Lattner00950542001-06-06 20:29:01 +00001467 }
1468 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner46748042002-04-09 19:41:42 +00001469 $$ = new vector<pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001470 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00001471
1472 if (V == 0)
1473 ThrowException("May only switch on a constant pool value!");
1474
Chris Lattner9636a912001-10-01 16:18:37 +00001475 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($4, $5))));
Chris Lattner51727be2002-06-04 21:58:56 +00001476 };
Chris Lattner00950542001-06-06 20:29:01 +00001477
1478Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001479 // Is this definition named?? if so, assign the name...
1480 if (setValueName($2, $1)) { assert(0 && "No redefin allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001481 InsertValue($2);
1482 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001483};
Chris Lattner00950542001-06-06 20:29:01 +00001484
Chris Lattnerc24d2082001-06-11 15:04:20 +00001485PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
1486 $$ = new list<pair<Value*, BasicBlock*> >();
Chris Lattner30c89792001-09-07 16:35:17 +00001487 $$->push_back(make_pair(getVal(*$1, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001488 cast<BasicBlock>(getVal(Type::LabelTy, $5))));
Chris Lattner30c89792001-09-07 16:35:17 +00001489 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001490 }
1491 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1492 $$ = $1;
1493 $1->push_back(make_pair(getVal($1->front().first->getType(), $4),
Chris Lattner9636a912001-10-01 16:18:37 +00001494 cast<BasicBlock>(getVal(Type::LabelTy, $6))));
Chris Lattner51727be2002-06-04 21:58:56 +00001495 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00001496
1497
Chris Lattner30c89792001-09-07 16:35:17 +00001498ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner6cdb0112001-11-26 16:54:11 +00001499 $$ = new vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001500 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001501 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001502 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001503 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001504 $1->push_back($3);
Chris Lattner51727be2002-06-04 21:58:56 +00001505 };
Chris Lattner00950542001-06-06 20:29:01 +00001506
1507// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner51727be2002-06-04 21:58:56 +00001508ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001509
1510InstVal : BinaryOps Types ValueRef ',' ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001511 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001512 if ($$ == 0)
1513 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001514 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001515 }
Chris Lattner699f1eb2002-08-14 17:12:33 +00001516 | NOT ResolvedVal {
1517 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
1518 << " Replacing with 'xor'.\n";
1519
1520 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
1521 if (Ones == 0)
1522 ThrowException("Expected integral type for not instruction!");
1523
1524 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner00950542001-06-06 20:29:01 +00001525 if ($$ == 0)
Chris Lattner699f1eb2002-08-14 17:12:33 +00001526 ThrowException("Could not create a xor instruction!");
Chris Lattner09083092001-07-08 04:57:15 +00001527 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001528 | ShiftOps ResolvedVal ',' ResolvedVal {
1529 if ($4->getType() != Type::UByteTy)
1530 ThrowException("Shift amount must be ubyte!");
1531 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001532 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001533 | CAST ResolvedVal TO Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001534 $$ = new CastInst($2, *$4);
1535 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001536 }
Chris Lattnerc24d2082001-06-11 15:04:20 +00001537 | PHI PHIList {
1538 const Type *Ty = $2->front().first->getType();
1539 $$ = new PHINode(Ty);
Chris Lattner00950542001-06-06 20:29:01 +00001540 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001541 if ($2->front().first->getType() != Ty)
1542 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001543 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001544 $2->pop_front();
1545 }
1546 delete $2; // Free the list...
1547 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001548 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattneref9c23f2001-10-03 14:53:21 +00001549 const PointerType *PMTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001550 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001551
Chris Lattneref9c23f2001-10-03 14:53:21 +00001552 if (!(PMTy = dyn_cast<PointerType>($2->get())) ||
Chris Lattner79df7c02002-03-26 18:01:55 +00001553 !(Ty = dyn_cast<FunctionType>(PMTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001554 // Pull out the types of all of the arguments...
1555 vector<const Type*> ParamTypes;
Chris Lattneref9c23f2001-10-03 14:53:21 +00001556 if ($5) {
Chris Lattner6cdb0112001-11-26 16:54:11 +00001557 for (vector<Value*>::iterator I = $5->begin(), E = $5->end(); I!=E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00001558 ParamTypes.push_back((*I)->getType());
1559 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001560
1561 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1562 if (isVarArg) ParamTypes.pop_back();
1563
Chris Lattner79df7c02002-03-26 18:01:55 +00001564 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattneref9c23f2001-10-03 14:53:21 +00001565 PMTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001566 }
Chris Lattner30c89792001-09-07 16:35:17 +00001567 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001568
Chris Lattner7e708292002-06-25 16:13:24 +00001569 Value *V = getVal(PMTy, $3); // Get the function we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001570
Chris Lattner8b81bf52001-07-25 22:47:46 +00001571 // Create the call node...
1572 if (!$5) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00001573 // Make sure no arguments is a good thing!
1574 if (Ty->getNumParams() != 0)
1575 ThrowException("No arguments passed to a function that "
1576 "expects arguments!");
1577
Chris Lattner386a3b72001-10-16 19:54:17 +00001578 $$ = new CallInst(V, vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001579 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001580 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00001581 // correctly!
1582 //
Chris Lattner79df7c02002-03-26 18:01:55 +00001583 FunctionType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1584 FunctionType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
Chris Lattner6cdb0112001-11-26 16:54:11 +00001585 vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001586
1587 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1588 if ((*ArgI)->getType() != *I)
1589 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001590 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001591
Chris Lattner8b81bf52001-07-25 22:47:46 +00001592 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner00950542001-06-06 20:29:01 +00001593 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00001594
Chris Lattner6cdb0112001-11-26 16:54:11 +00001595 $$ = new CallInst(V, *$5);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001596 }
1597 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00001598 }
1599 | MemoryInst {
1600 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001601 };
Chris Lattner00950542001-06-06 20:29:01 +00001602
Chris Lattner6cdb0112001-11-26 16:54:11 +00001603
1604// IndexList - List of indices for GEP based instructions...
1605IndexList : ',' ValueRefList {
Chris Lattner027dcc52001-07-08 21:10:27 +00001606 $$ = $2;
1607} | /* empty */ {
Chris Lattner6cdb0112001-11-26 16:54:11 +00001608 $$ = new vector<Value*>();
Chris Lattner51727be2002-06-04 21:58:56 +00001609};
Chris Lattner027dcc52001-07-08 21:10:27 +00001610
Chris Lattner00950542001-06-06 20:29:01 +00001611MemoryInst : MALLOC Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001612 $$ = new MallocInst(PointerType::get(*$2));
1613 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001614 }
1615 | MALLOC Types ',' UINT ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001616 const Type *Ty = PointerType::get(*$2);
Chris Lattner8896eda2001-07-09 19:38:36 +00001617 $$ = new MallocInst(Ty, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001618 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001619 }
1620 | ALLOCA Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001621 $$ = new AllocaInst(PointerType::get(*$2));
1622 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001623 }
1624 | ALLOCA Types ',' UINT ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001625 const Type *Ty = PointerType::get(*$2);
Chris Lattner00950542001-06-06 20:29:01 +00001626 Value *ArrSize = getVal($4, $5);
Chris Lattnerf0d0e9c2001-07-07 08:36:30 +00001627 $$ = new AllocaInst(Ty, ArrSize);
Chris Lattner30c89792001-09-07 16:35:17 +00001628 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001629 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001630 | FREE ResolvedVal {
Chris Lattner9b625032002-05-06 16:15:30 +00001631 if (!isa<PointerType>($2->getType()))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001632 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00001633 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001634 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001635 }
1636
Chris Lattner6cdb0112001-11-26 16:54:11 +00001637 | LOAD Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00001638 if (!isa<PointerType>($2->get()))
Chris Lattner2079fde2001-10-13 06:41:08 +00001639 ThrowException("Can't load from nonpointer type: " +
1640 (*$2)->getDescription());
Chris Lattner5dfe7672002-08-22 22:48:55 +00001641 if (GetElementPtrInst::getIndexedType(*$2, *$4) == 0)
Chris Lattner027dcc52001-07-08 21:10:27 +00001642 ThrowException("Invalid indices for load instruction!");
1643
Chris Lattner0383cc42002-08-21 23:51:21 +00001644 Value *Src = getVal(*$2, $3);
1645 if (!$4->empty()) {
1646 std::cerr << "WARNING: Use of index load instruction:"
1647 << " replacing with getelementptr/load pair.\n";
1648 // Create a getelementptr hack instruction to do the right thing for
1649 // compatibility.
1650 //
1651 Instruction *I = new GetElementPtrInst(Src, *$4);
1652 CurBB->getInstList().push_back(I);
1653 Src = I;
1654 }
1655
1656 $$ = new LoadInst(Src);
Chris Lattner027dcc52001-07-08 21:10:27 +00001657 delete $4; // Free the vector...
Chris Lattner30c89792001-09-07 16:35:17 +00001658 delete $2;
Chris Lattner027dcc52001-07-08 21:10:27 +00001659 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00001660 | STORE ResolvedVal ',' Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00001661 if (!isa<PointerType>($4->get()))
Chris Lattner72e00252001-12-14 16:28:42 +00001662 ThrowException("Can't store to a nonpointer type: " +
1663 (*$4)->getDescription());
Chris Lattner5dfe7672002-08-22 22:48:55 +00001664 const Type *ElTy = GetElementPtrInst::getIndexedType(*$4, *$6);
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001665 if (ElTy == 0)
1666 ThrowException("Can't store into that field list!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001667 if (ElTy != $2->getType())
Chris Lattner72e00252001-12-14 16:28:42 +00001668 ThrowException("Can't store '" + $2->getType()->getDescription() +
1669 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner0383cc42002-08-21 23:51:21 +00001670
1671 Value *Ptr = getVal(*$4, $5);
1672 if (!$6->empty()) {
1673 std::cerr << "WARNING: Use of index store instruction:"
1674 << " replacing with getelementptr/store pair.\n";
1675 // Create a getelementptr hack instruction to do the right thing for
1676 // compatibility.
1677 //
1678 Instruction *I = new GetElementPtrInst(Ptr, *$6);
1679 CurBB->getInstList().push_back(I);
1680 Ptr = I;
1681 }
1682
1683 $$ = new StoreInst($2, Ptr);
Chris Lattner30c89792001-09-07 16:35:17 +00001684 delete $4; delete $6;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001685 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00001686 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00001687 if (!isa<PointerType>($2->get()))
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001688 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner30c89792001-09-07 16:35:17 +00001689 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner72e00252001-12-14 16:28:42 +00001690 ThrowException("Can't get element ptr '" + (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001691 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
1692 delete $2; delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00001693 };
Chris Lattner027dcc52001-07-08 21:10:27 +00001694
Chris Lattner00950542001-06-06 20:29:01 +00001695%%
Chris Lattner09083092001-07-08 04:57:15 +00001696int yyerror(const char *ErrorMsg) {
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001697 string where = string((CurFilename == "-")? string("<stdin>") : CurFilename)
1698 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
1699 string errMsg = string(ErrorMsg) + string("\n") + where + " while reading ";
1700 if (yychar == YYEMPTY)
1701 errMsg += "end-of-file.";
1702 else
1703 errMsg += "token: '" + string(llvmAsmtext, llvmAsmleng) + "'";
1704 ThrowException(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00001705 return 0;
1706}