blob: 58de57aaf79c8a05738ea21029fbaf64b8c8bda8 [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 Lattner4a6482b2002-09-10 19:57:26 +0000689%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Chris Lattner42c9e772001-10-20 09:32:59 +0000690%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000691%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000692
693// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000694%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000695
Chris Lattner027dcc52001-07-08 21:10:27 +0000696// Other Operators
697%type <OtherOpVal> ShiftOps
Chris Lattner2079fde2001-10-13 06:41:08 +0000698%token <OtherOpVal> PHI CALL INVOKE CAST SHL SHR
Chris Lattner027dcc52001-07-08 21:10:27 +0000699
Chris Lattner00950542001-06-06 20:29:01 +0000700%start Module
701%%
702
703// Handle constant integer size restriction and conversion...
704//
705
Chris Lattner51727be2002-06-04 21:58:56 +0000706INTVAL : SINTVAL;
Chris Lattner00950542001-06-06 20:29:01 +0000707INTVAL : UINTVAL {
708 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
709 ThrowException("Value too large for type!");
710 $$ = (int32_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000711};
Chris Lattner00950542001-06-06 20:29:01 +0000712
713
Chris Lattner51727be2002-06-04 21:58:56 +0000714EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner00950542001-06-06 20:29:01 +0000715EINT64VAL : EUINT64VAL {
716 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
717 ThrowException("Value too large for type!");
718 $$ = (int64_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000719};
Chris Lattner00950542001-06-06 20:29:01 +0000720
Chris Lattner00950542001-06-06 20:29:01 +0000721// Operations that are notably excluded from this list include:
722// RET, BR, & SWITCH because they end basic blocks and are treated specially.
723//
Chris Lattner4a6482b2002-09-10 19:57:26 +0000724ArithmeticOps: ADD | SUB | MUL | DIV | REM;
725LogicalOps : AND | OR | XOR;
726SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
727BinaryOps : ArithmeticOps | LogicalOps | SetCondOps;
728
Chris Lattner51727be2002-06-04 21:58:56 +0000729ShiftOps : SHL | SHR;
Chris Lattner00950542001-06-06 20:29:01 +0000730
Chris Lattnere98dda62001-07-14 06:10:16 +0000731// These are some types that allow classification if we only want a particular
732// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner51727be2002-06-04 21:58:56 +0000733SIntType : LONG | INT | SHORT | SBYTE;
734UIntType : ULONG | UINT | USHORT | UBYTE;
735IntType : SIntType | UIntType;
736FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +0000737
Chris Lattnere98dda62001-07-14 06:10:16 +0000738// OptAssign - Value producing statements have an optional assignment component
Chris Lattner00950542001-06-06 20:29:01 +0000739OptAssign : VAR_ID '=' {
740 $$ = $1;
741 }
742 | /*empty*/ {
743 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +0000744 };
Chris Lattner00950542001-06-06 20:29:01 +0000745
Chris Lattner51727be2002-06-04 21:58:56 +0000746OptInternal : INTERNAL { $$ = true; } | /*empty*/ { $$ = false; };
Chris Lattner30c89792001-09-07 16:35:17 +0000747
748//===----------------------------------------------------------------------===//
749// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +0000750// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +0000751// access to it, a user must explicitly use TypesV.
752//
753
754// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +0000755TypesV : Types | VOID { $$ = new PATypeHolder($1); };
756UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +0000757
758Types : UpRTypes {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000759 if (UpRefs.size())
760 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
761 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000762 };
Chris Lattner30c89792001-09-07 16:35:17 +0000763
764
765// Derived types are added later...
766//
Chris Lattner51727be2002-06-04 21:58:56 +0000767PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
768PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000769UpRTypes : OPAQUE {
770 $$ = new PATypeHolder(OpaqueType::get());
771 }
772 | PrimType {
773 $$ = new PATypeHolder($1);
Chris Lattner51727be2002-06-04 21:58:56 +0000774 };
Chris Lattnerd78700d2002-08-16 21:14:40 +0000775UpRTypes : SymbolicValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000776 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner51727be2002-06-04 21:58:56 +0000777};
Chris Lattner30c89792001-09-07 16:35:17 +0000778
Chris Lattner30c89792001-09-07 16:35:17 +0000779// Include derived types in the Types production.
780//
781UpRTypes : '\\' EUINT64VAL { // Type UpReference
782 if ($2 > (uint64_t)INT64_MAX) ThrowException("Value out of range!");
783 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
784 UpRefs.push_back(make_pair((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000785 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +0000786 UR_OUT("New Upreference!\n");
787 }
Chris Lattner79df7c02002-03-26 18:01:55 +0000788 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner30c89792001-09-07 16:35:17 +0000789 vector<const Type*> Params;
Chris Lattner697954c2002-01-20 22:54:45 +0000790 mapto($3->begin(), $3->end(), std::back_inserter(Params),
791 std::mem_fun_ref(&PATypeHandle<Type>::get));
Chris Lattner2079fde2001-10-13 06:41:08 +0000792 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
793 if (isVarArg) Params.pop_back();
794
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000795 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +0000796 delete $3; // Delete the argument list
797 delete $1; // Delete the old type handle
798 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000799 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000800 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000801 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +0000802 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000803 | '{' TypeListI '}' { // Structure type?
804 vector<const Type*> Elements;
Chris Lattner697954c2002-01-20 22:54:45 +0000805 mapto($2->begin(), $2->end(), std::back_inserter(Elements),
806 std::mem_fun_ref(&PATypeHandle<Type>::get));
Chris Lattner30c89792001-09-07 16:35:17 +0000807
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000808 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000809 delete $2;
810 }
811 | '{' '}' { // Empty structure type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000812 $$ = new PATypeHolder(StructType::get(vector<const Type*>()));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000813 }
814 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000815 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000816 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000817 };
Chris Lattner30c89792001-09-07 16:35:17 +0000818
Chris Lattner7e708292002-06-25 16:13:24 +0000819// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +0000820// declaration type lists
821//
822TypeListI : UpRTypes {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000823 $$ = new list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +0000824 $$->push_back(*$1); delete $1;
825 }
826 | TypeListI ',' UpRTypes {
827 ($$=$1)->push_back(*$3); delete $3;
Chris Lattner51727be2002-06-04 21:58:56 +0000828 };
Chris Lattner30c89792001-09-07 16:35:17 +0000829
Chris Lattner7e708292002-06-25 16:13:24 +0000830// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +0000831ArgTypeListI : TypeListI
832 | TypeListI ',' DOTDOTDOT {
833 ($$=$1)->push_back(Type::VoidTy);
834 }
835 | DOTDOTDOT {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000836 ($$ = new list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattner30c89792001-09-07 16:35:17 +0000837 }
838 | /*empty*/ {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000839 $$ = new list<PATypeHolder>();
Chris Lattner51727be2002-06-04 21:58:56 +0000840 };
Chris Lattner30c89792001-09-07 16:35:17 +0000841
Chris Lattnere98dda62001-07-14 06:10:16 +0000842// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +0000843// production is used ONLY to represent constants that show up AFTER a 'const',
844// 'constant' or 'global' token at global scope. Constants that can be inlined
845// into other expressions (such as integers and constexprs) are handled by the
846// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +0000847//
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000848ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
849 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
850 if (ATy == 0)
851 ThrowException("Cannot make array constant with type: '" +
852 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +0000853 const Type *ETy = ATy->getElementType();
854 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +0000855
Chris Lattner30c89792001-09-07 16:35:17 +0000856 // Verify that we have the correct size...
857 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +0000858 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattner30c89792001-09-07 16:35:17 +0000859 utostr($3->size()) + " arguments, but has size of " +
860 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +0000861
Chris Lattner30c89792001-09-07 16:35:17 +0000862 // Verify all elements are correct type!
863 for (unsigned i = 0; i < $3->size(); i++) {
864 if (ETy != (*$3)[i]->getType())
Chris Lattner00950542001-06-06 20:29:01 +0000865 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +0000866 ETy->getDescription() +"' as required!\nIt is of type '"+
867 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +0000868 }
869
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000870 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +0000871 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000872 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000873 | Types '[' ']' {
874 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
875 if (ATy == 0)
876 ThrowException("Cannot make array constant with type: '" +
877 (*$1)->getDescription() + "'!");
878
879 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +0000880 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +0000881 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattner30c89792001-09-07 16:35:17 +0000882 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000883 $$ = ConstantArray::get(ATy, vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +0000884 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +0000885 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000886 | Types 'c' STRINGCONSTANT {
887 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
888 if (ATy == 0)
889 ThrowException("Cannot make array constant with type: '" +
890 (*$1)->getDescription() + "'!");
891
Chris Lattner30c89792001-09-07 16:35:17 +0000892 int NumElements = ATy->getNumElements();
893 const Type *ETy = ATy->getElementType();
894 char *EndStr = UnEscapeLexed($3, true);
895 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +0000896 ThrowException("Can't build string constant of size " +
Chris Lattner30c89792001-09-07 16:35:17 +0000897 itostr((int)(EndStr-$3)) +
898 " when array has size " + itostr(NumElements) + "!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000899 vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +0000900 if (ETy == Type::SByteTy) {
901 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000902 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +0000903 } else if (ETy == Type::UByteTy) {
904 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000905 Vals.push_back(ConstantUInt::get(ETy, *C));
Chris Lattner93750fa2001-07-28 17:48:55 +0000906 } else {
Chris Lattner30c89792001-09-07 16:35:17 +0000907 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +0000908 ThrowException("Cannot build string arrays of non byte sized elements!");
909 }
Chris Lattner30c89792001-09-07 16:35:17 +0000910 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000911 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +0000912 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +0000913 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000914 | Types '{' ConstVector '}' {
915 const StructType *STy = dyn_cast<const StructType>($1->get());
916 if (STy == 0)
917 ThrowException("Cannot make struct constant with type: '" +
918 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +0000919 // FIXME: TODO: Check to see that the constants are compatible with the type
920 // initializer!
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000921 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +0000922 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000923 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000924 | Types NULL_TOK {
925 const PointerType *PTy = dyn_cast<const PointerType>($1->get());
926 if (PTy == 0)
927 ThrowException("Cannot make null pointer constant with type: '" +
928 (*$1)->getDescription() + "'!");
929
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000930 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000931 delete $1;
932 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000933 | Types SymbolicValueRef {
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000934 const PointerType *Ty = dyn_cast<const PointerType>($1->get());
935 if (Ty == 0)
936 ThrowException("Global const reference must be a pointer type!");
937
Chris Lattner3101c252002-08-15 17:58:33 +0000938 // ConstExprs can exist in the body of a function, thus creating
939 // ConstantPointerRefs whenever they refer to a variable. Because we are in
940 // the context of a function, getValNonImprovising will search the functions
941 // symbol table instead of the module symbol table for the global symbol,
942 // which throws things all off. To get around this, we just tell
943 // getValNonImprovising that we are at global scope here.
944 //
945 Function *SavedCurFn = CurMeth.CurrentFunction;
946 CurMeth.CurrentFunction = 0;
947
Chris Lattner2079fde2001-10-13 06:41:08 +0000948 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000949
Chris Lattner3101c252002-08-15 17:58:33 +0000950 CurMeth.CurrentFunction = SavedCurFn;
951
952
Chris Lattner2079fde2001-10-13 06:41:08 +0000953 // If this is an initializer for a constant pointer, which is referencing a
954 // (currently) undefined variable, create a stub now that shall be replaced
955 // in the future with the right type of variable.
956 //
957 if (V == 0) {
958 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
959 const PointerType *PT = cast<PointerType>(Ty);
960
961 // First check to see if the forward references value is already created!
962 PerModuleInfo::GlobalRefsType::iterator I =
963 CurModule.GlobalRefs.find(make_pair(PT, $2));
964
965 if (I != CurModule.GlobalRefs.end()) {
966 V = I->second; // Placeholder already exists, use it...
967 } else {
968 // TODO: Include line number info by creating a subclass of
969 // TODO: GlobalVariable here that includes the said information!
970
971 // Create a placeholder for the global variable reference...
Chris Lattner7a176752001-12-04 00:03:30 +0000972 GlobalVariable *GV = new GlobalVariable(PT->getElementType(),
973 false, true);
Chris Lattner2079fde2001-10-13 06:41:08 +0000974 // Keep track of the fact that we have a forward ref to recycle it
975 CurModule.GlobalRefs.insert(make_pair(make_pair(PT, $2), GV));
976
977 // Must temporarily push this value into the module table...
978 CurModule.CurrentModule->getGlobalList().push_back(GV);
979 V = GV;
980 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000981 }
982
Chris Lattner2079fde2001-10-13 06:41:08 +0000983 GlobalValue *GV = cast<GlobalValue>(V);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000984 $$ = ConstantPointerRef::get(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +0000985 delete $1; // Free the type handle
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000986 }
Chris Lattnerd78700d2002-08-16 21:14:40 +0000987 | Types ConstExpr {
988 if ($1->get() != $2->getType())
989 ThrowException("Mismatched types for constant expression!");
990 $$ = $2;
991 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000992 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000993
Chris Lattnerd05e3592002-08-15 18:17:28 +0000994ConstVal : SIntType EINT64VAL { // integral constants
995 if (!ConstantSInt::isValueValidForType($1, $2))
996 ThrowException("Constant value doesn't fit in type!");
997 $$ = ConstantSInt::get($1, $2);
998 }
999 | UIntType EUINT64VAL { // integral constants
1000 if (!ConstantUInt::isValueValidForType($1, $2))
1001 ThrowException("Constant value doesn't fit in type!");
1002 $$ = ConstantUInt::get($1, $2);
1003 }
1004 | BOOL TRUE { // Boolean constants
1005 $$ = ConstantBool::True;
1006 }
1007 | BOOL FALSE { // Boolean constants
1008 $$ = ConstantBool::False;
1009 }
1010 | FPType FPVAL { // Float & Double constants
1011 $$ = ConstantFP::get($1, $2);
1012 };
1013
Chris Lattner00950542001-06-06 20:29:01 +00001014
Chris Lattnerd78700d2002-08-16 21:14:40 +00001015ConstExpr: CAST '(' ConstVal TO Types ')' {
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001016 $$ = ConstantExpr::getCast($3, $5->get());
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001017 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001018 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001019 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1020 if (!isa<PointerType>($3->getType()))
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001021 ThrowException("GetElementPtr requires a pointer operand!");
1022
1023 const Type *IdxTy =
Chris Lattnerd78700d2002-08-16 21:14:40 +00001024 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001025 if (!IdxTy)
1026 ThrowException("Index list invalid for constant getelementptr!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001027
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001028 vector<Constant*> IdxVec;
Chris Lattnerd78700d2002-08-16 21:14:40 +00001029 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1030 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001031 IdxVec.push_back(C);
1032 else
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001033 ThrowException("Indices to constant getelementptr must be constants!");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001034
Chris Lattnerd78700d2002-08-16 21:14:40 +00001035 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001036
Chris Lattnerd78700d2002-08-16 21:14:40 +00001037 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001038 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001039 | BinaryOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001040 if ($3->getType() != $5->getType())
1041 ThrowException("Binary operator types must match!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001042 $$ = ConstantExpr::get($1, $3, $5);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001043 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001044 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001045 if ($5->getType() != Type::UByteTy)
1046 ThrowException("Shift count for shift constant must be unsigned byte!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001047 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattner699f1eb2002-08-14 17:12:33 +00001048 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001049
1050
Chris Lattnere98dda62001-07-14 06:10:16 +00001051// ConstVector - A list of comma seperated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001052ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001053 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001054 }
1055 | ConstVal {
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001056 $$ = new vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001057 $$->push_back($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001058 };
Chris Lattner00950542001-06-06 20:29:01 +00001059
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001060
Chris Lattner1781aca2001-09-18 04:00:54 +00001061// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001062GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001063
Chris Lattner00950542001-06-06 20:29:01 +00001064
Chris Lattner0e73ce62002-05-02 19:11:13 +00001065//===----------------------------------------------------------------------===//
1066// Rules to match Modules
1067//===----------------------------------------------------------------------===//
1068
1069// Module rule: Capture the result of parsing the whole file into a result
1070// variable...
1071//
1072Module : FunctionList {
1073 $$ = ParserResult = $1;
1074 CurModule.ModuleDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001075};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001076
Chris Lattner7e708292002-06-25 16:13:24 +00001077// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001078//
1079FunctionList : FunctionList Function {
1080 $$ = $1;
1081 assert($2->getParent() == 0 && "Function already in module!");
1082 $1->getFunctionList().push_back($2);
1083 CurMeth.FunctionDone();
1084 }
1085 | FunctionList FunctionProto {
1086 $$ = $1;
1087 }
1088 | FunctionList IMPLEMENTATION {
1089 $$ = $1;
1090 }
1091 | ConstPool {
1092 $$ = CurModule.CurrentModule;
1093 // Resolve circular types before we parse the body of the module
1094 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001095 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001096
Chris Lattnere98dda62001-07-14 06:10:16 +00001097// ConstPool - Constants with optional names assigned to them.
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001098ConstPool : ConstPool OptAssign CONST ConstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001099 if (setValueName($4, $2)) { assert(0 && "No redefinitions allowed!"); }
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001100 InsertValue($4);
Chris Lattner00950542001-06-06 20:29:01 +00001101 }
Chris Lattner30c89792001-09-07 16:35:17 +00001102 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner4a42e902001-10-22 05:56:09 +00001103 // Eagerly resolve types. This is not an optimization, this is a
1104 // requirement that is due to the fact that we could have this:
1105 //
1106 // %list = type { %list * }
1107 // %list = type { %list * } ; repeated type decl
1108 //
1109 // If types are not resolved eagerly, then the two types will not be
1110 // determined to be the same type!
1111 //
1112 ResolveTypeTo($2, $4->get());
1113
Chris Lattner1781aca2001-09-18 04:00:54 +00001114 // TODO: FIXME when Type are not const
Chris Lattnerb7474512001-10-03 15:39:04 +00001115 if (!setValueName(const_cast<Type*>($4->get()), $2)) {
1116 // If this is not a redefinition of a type...
1117 if (!$2) {
1118 InsertType($4->get(),
Chris Lattner79df7c02002-03-26 18:01:55 +00001119 inFunctionScope() ? CurMeth.Types : CurModule.Types);
Chris Lattnerb7474512001-10-03 15:39:04 +00001120 }
Chris Lattner30c89792001-09-07 16:35:17 +00001121 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001122
1123 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001124 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001125 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001126 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001127 | ConstPool OptAssign OptInternal GlobalType ConstVal {
1128 const Type *Ty = $5->getType();
Chris Lattner1781aca2001-09-18 04:00:54 +00001129 // Global declarations appear in Constant Pool
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001130 Constant *Initializer = $5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001131 if (Initializer == 0)
1132 ThrowException("Global value initializer is not a constant!");
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001133
Chris Lattnerdda71962001-11-26 18:54:16 +00001134 GlobalVariable *GV = new GlobalVariable(Ty, $4, $3, Initializer);
Chris Lattnerb7474512001-10-03 15:39:04 +00001135 if (!setValueName(GV, $2)) { // If not redefining...
1136 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001137 int Slot = InsertValue(GV, CurModule.Values);
1138
1139 if (Slot != -1) {
1140 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1141 } else {
1142 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1143 (char*)GV->getName().c_str()));
1144 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001145 }
Chris Lattner1781aca2001-09-18 04:00:54 +00001146 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001147 | ConstPool OptAssign OptInternal UNINIT GlobalType Types {
1148 const Type *Ty = *$6;
Chris Lattner1781aca2001-09-18 04:00:54 +00001149 // Global declarations appear in Constant Pool
Chris Lattnerdda71962001-11-26 18:54:16 +00001150 GlobalVariable *GV = new GlobalVariable(Ty, $5, $3);
Chris Lattnerb7474512001-10-03 15:39:04 +00001151 if (!setValueName(GV, $2)) { // If not redefining...
1152 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001153 int Slot = InsertValue(GV, CurModule.Values);
1154
1155 if (Slot != -1) {
1156 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1157 } else {
1158 assert(GV->hasName() && "Not named and not numbered!?");
1159 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1160 (char*)GV->getName().c_str()));
1161 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001162 }
Chris Lattner09c07532002-03-31 07:16:49 +00001163 delete $6;
Chris Lattnere98dda62001-07-14 06:10:16 +00001164 }
Chris Lattner00950542001-06-06 20:29:01 +00001165 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001166 };
Chris Lattner00950542001-06-06 20:29:01 +00001167
1168
1169//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001170// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001171//===----------------------------------------------------------------------===//
1172
Chris Lattner51727be2002-06-04 21:58:56 +00001173OptVAR_ID : VAR_ID | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001174
1175ArgVal : Types OptVAR_ID {
Chris Lattner46748042002-04-09 19:41:42 +00001176 $$ = new pair<Argument*, char*>(new Argument(*$1), $2);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001177 delete $1; // Delete the type handle..
Chris Lattner51727be2002-06-04 21:58:56 +00001178};
Chris Lattner00950542001-06-06 20:29:01 +00001179
1180ArgListH : ArgVal ',' ArgListH {
1181 $$ = $3;
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001182 $3->push_front(*$1);
1183 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001184 }
1185 | ArgVal {
Chris Lattner46748042002-04-09 19:41:42 +00001186 $$ = new list<pair<Argument*,char*> >();
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001187 $$->push_front(*$1);
1188 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001189 }
Chris Lattner8b81bf52001-07-25 22:47:46 +00001190 | DOTDOTDOT {
Chris Lattner46748042002-04-09 19:41:42 +00001191 $$ = new list<pair<Argument*, char*> >();
1192 $$->push_front(pair<Argument*,char*>(new Argument(Type::VoidTy), 0));
Chris Lattner51727be2002-06-04 21:58:56 +00001193 };
Chris Lattner00950542001-06-06 20:29:01 +00001194
1195ArgList : ArgListH {
1196 $$ = $1;
1197 }
1198 | /* empty */ {
1199 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001200 };
Chris Lattner00950542001-06-06 20:29:01 +00001201
Chris Lattner8ebccb72002-05-22 22:33:00 +00001202FuncName : VAR_ID | STRINGCONSTANT;
1203
1204FunctionHeaderH : OptInternal TypesV FuncName '(' ArgList ')' {
Chris Lattnerdda71962001-11-26 18:54:16 +00001205 UnEscapeLexed($3);
Chris Lattner79df7c02002-03-26 18:01:55 +00001206 string FunctionName($3);
Chris Lattnerdda71962001-11-26 18:54:16 +00001207
Chris Lattner30c89792001-09-07 16:35:17 +00001208 vector<const Type*> ParamTypeList;
Chris Lattnerdda71962001-11-26 18:54:16 +00001209 if ($5)
Chris Lattner46748042002-04-09 19:41:42 +00001210 for (list<pair<Argument*,char*> >::iterator I = $5->begin();
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001211 I != $5->end(); ++I)
1212 ParamTypeList.push_back(I->first->getType());
Chris Lattner00950542001-06-06 20:29:01 +00001213
Chris Lattner2079fde2001-10-13 06:41:08 +00001214 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1215 if (isVarArg) ParamTypeList.pop_back();
1216
Chris Lattner79df7c02002-03-26 18:01:55 +00001217 const FunctionType *MT = FunctionType::get(*$2, ParamTypeList, isVarArg);
Chris Lattneref9c23f2001-10-03 14:53:21 +00001218 const PointerType *PMT = PointerType::get(MT);
Chris Lattnerdda71962001-11-26 18:54:16 +00001219 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001220
Chris Lattner79df7c02002-03-26 18:01:55 +00001221 Function *M = 0;
Chris Lattnere1815642001-07-15 06:35:53 +00001222 if (SymbolTable *ST = CurModule.CurrentModule->getSymbolTable()) {
Chris Lattner79df7c02002-03-26 18:01:55 +00001223 // Is the function already in symtab?
1224 if (Value *V = ST->lookup(PMT, FunctionName)) {
1225 M = cast<Function>(V);
Chris Lattner00950542001-06-06 20:29:01 +00001226
Chris Lattnere1815642001-07-15 06:35:53 +00001227 // Yes it is. If this is the case, either we need to be a forward decl,
1228 // or it needs to be.
1229 if (!CurMeth.isDeclare && !M->isExternal())
Chris Lattner7e708292002-06-25 16:13:24 +00001230 ThrowException("Redefinition of function '" + FunctionName + "'!");
Chris Lattner34538142002-03-08 19:11:42 +00001231
Chris Lattner5659dd12002-07-15 00:10:33 +00001232 // Make sure that we keep track of the internal marker, even if there was
1233 // a previous "declare".
1234 if ($1)
1235 M->setInternalLinkage(true);
1236
Chris Lattner7e708292002-06-25 16:13:24 +00001237 // If we found a preexisting function prototype, remove it from the
1238 // module, so that we don't get spurious conflicts with global & local
1239 // variables.
Chris Lattner34538142002-03-08 19:11:42 +00001240 //
Chris Lattner79df7c02002-03-26 18:01:55 +00001241 CurModule.CurrentModule->getFunctionList().remove(M);
Chris Lattnere1815642001-07-15 06:35:53 +00001242 }
1243 }
1244
1245 if (M == 0) { // Not already defined?
Chris Lattner79df7c02002-03-26 18:01:55 +00001246 M = new Function(MT, $1, FunctionName);
Chris Lattnere1815642001-07-15 06:35:53 +00001247 InsertValue(M, CurModule.Values);
Chris Lattnerdda71962001-11-26 18:54:16 +00001248 CurModule.DeclareNewGlobalValue(M, ValID::create($3));
Chris Lattnere1815642001-07-15 06:35:53 +00001249 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001250 free($3); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +00001251
Chris Lattner79df7c02002-03-26 18:01:55 +00001252 CurMeth.FunctionStart(M);
Chris Lattner00950542001-06-06 20:29:01 +00001253
Chris Lattner7e708292002-06-25 16:13:24 +00001254 // Add all of the arguments we parsed to the function...
Chris Lattnerdda71962001-11-26 18:54:16 +00001255 if ($5 && !CurMeth.isDeclare) { // Is null if empty...
Chris Lattner46748042002-04-09 19:41:42 +00001256 for (list<pair<Argument*, char*> >::iterator I = $5->begin();
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001257 I != $5->end(); ++I) {
1258 if (setValueName(I->first, I->second)) { // Insert into symtab...
1259 assert(0 && "No arg redef allowed!");
1260 }
1261
1262 InsertValue(I->first);
Chris Lattner7e708292002-06-25 16:13:24 +00001263 M->getArgumentList().push_back(I->first);
Chris Lattner00950542001-06-06 20:29:01 +00001264 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001265 delete $5; // We're now done with the argument list
Chris Lattner9176fe42002-03-08 18:57:56 +00001266 } else if ($5) {
1267 // If we are a declaration, we should free the memory for the argument list!
Chris Lattner46748042002-04-09 19:41:42 +00001268 for (list<pair<Argument*, char*> >::iterator I = $5->begin(), E = $5->end();
1269 I != E; ++I) {
Chris Lattner9176fe42002-03-08 18:57:56 +00001270 if (I->second) free(I->second); // Free the memory for the name...
Chris Lattner09c07532002-03-31 07:16:49 +00001271 delete I->first; // Free the unused function argument
1272 }
Chris Lattner9176fe42002-03-08 18:57:56 +00001273 delete $5; // Free the memory for the list itself
Chris Lattner00950542001-06-06 20:29:01 +00001274 }
Chris Lattner51727be2002-06-04 21:58:56 +00001275};
Chris Lattner00950542001-06-06 20:29:01 +00001276
Chris Lattner9b02cc32002-05-03 18:23:48 +00001277BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1278
1279FunctionHeader : FunctionHeaderH BEGIN {
Chris Lattner79df7c02002-03-26 18:01:55 +00001280 $$ = CurMeth.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001281
Chris Lattner7e708292002-06-25 16:13:24 +00001282 // Resolve circular types before we parse the body of the function.
Chris Lattner30c89792001-09-07 16:35:17 +00001283 ResolveTypes(CurMeth.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001284};
Chris Lattner00950542001-06-06 20:29:01 +00001285
Chris Lattner9b02cc32002-05-03 18:23:48 +00001286END : ENDTOK | '}'; // Allow end of '}' to end a function
1287
Chris Lattner79df7c02002-03-26 18:01:55 +00001288Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001289 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001290};
Chris Lattner00950542001-06-06 20:29:01 +00001291
Chris Lattner79df7c02002-03-26 18:01:55 +00001292FunctionProto : DECLARE { CurMeth.isDeclare = true; } FunctionHeaderH {
1293 $$ = CurMeth.CurrentFunction;
1294 assert($$->getParent() == 0 && "Function already in module!");
1295 CurModule.CurrentModule->getFunctionList().push_back($$);
1296 CurMeth.FunctionDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001297};
Chris Lattner00950542001-06-06 20:29:01 +00001298
1299//===----------------------------------------------------------------------===//
1300// Rules to match Basic Blocks
1301//===----------------------------------------------------------------------===//
1302
1303ConstValueRef : ESINT64VAL { // A reference to a direct constant
1304 $$ = ValID::create($1);
1305 }
1306 | EUINT64VAL {
1307 $$ = ValID::create($1);
1308 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001309 | FPVAL { // Perhaps it's an FP constant?
1310 $$ = ValID::create($1);
1311 }
Chris Lattner00950542001-06-06 20:29:01 +00001312 | TRUE {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001313 $$ = ValID::create(ConstantBool::True);
Chris Lattner00950542001-06-06 20:29:01 +00001314 }
1315 | FALSE {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001316 $$ = ValID::create(ConstantBool::False);
Chris Lattner00950542001-06-06 20:29:01 +00001317 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001318 | NULL_TOK {
1319 $$ = ValID::createNull();
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001320 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001321 | ConstExpr {
1322 $$ = ValID::create($1);
1323 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00001324
Chris Lattner2079fde2001-10-13 06:41:08 +00001325// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1326// another value.
1327//
1328SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001329 $$ = ValID::create($1);
1330 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001331 | VAR_ID { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001332 $$ = ValID::create($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001333 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001334
1335// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00001336ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00001337
Chris Lattner00950542001-06-06 20:29:01 +00001338
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001339// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1340// type immediately preceeds the value reference, and allows complex constant
1341// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001342ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001343 $$ = getVal(*$1, $2); delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001344 };
Chris Lattner8b81bf52001-07-25 22:47:46 +00001345
Chris Lattner00950542001-06-06 20:29:01 +00001346BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner7e708292002-06-25 16:13:24 +00001347 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner00950542001-06-06 20:29:01 +00001348 }
Chris Lattner7e708292002-06-25 16:13:24 +00001349 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
1350 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner51727be2002-06-04 21:58:56 +00001351 };
Chris Lattner00950542001-06-06 20:29:01 +00001352
1353
1354// Basic blocks are terminated by branching instructions:
1355// br, br/cc, switch, ret
1356//
Chris Lattner2079fde2001-10-13 06:41:08 +00001357BasicBlock : InstructionList OptAssign BBTerminatorInst {
1358 if (setValueName($3, $2)) { assert(0 && "No redefn allowed!"); }
1359 InsertValue($3);
1360
1361 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001362 InsertValue($1);
1363 $$ = $1;
1364 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001365 | LABELSTR InstructionList OptAssign BBTerminatorInst {
1366 if (setValueName($4, $3)) { assert(0 && "No redefn allowed!"); }
1367 InsertValue($4);
1368
1369 $2->getInstList().push_back($4);
Chris Lattnerb7474512001-10-03 15:39:04 +00001370 if (setValueName($2, $1)) { assert(0 && "No label redef allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001371
1372 InsertValue($2);
1373 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001374 };
Chris Lattner00950542001-06-06 20:29:01 +00001375
1376InstructionList : InstructionList Inst {
1377 $1->getInstList().push_back($2);
1378 $$ = $1;
1379 }
1380 | /* empty */ {
Chris Lattner0383cc42002-08-21 23:51:21 +00001381 $$ = CurBB = new BasicBlock();
Chris Lattner51727be2002-06-04 21:58:56 +00001382 };
Chris Lattner00950542001-06-06 20:29:01 +00001383
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001384BBTerminatorInst : RET ResolvedVal { // Return with a result...
1385 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001386 }
1387 | RET VOID { // Return with no result...
1388 $$ = new ReturnInst();
1389 }
1390 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner9636a912001-10-01 16:18:37 +00001391 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $3)));
Chris Lattner00950542001-06-06 20:29:01 +00001392 } // Conditional Branch...
1393 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner9636a912001-10-01 16:18:37 +00001394 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $6)),
1395 cast<BasicBlock>(getVal(Type::LabelTy, $9)),
Chris Lattner00950542001-06-06 20:29:01 +00001396 getVal(Type::BoolTy, $3));
1397 }
1398 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1399 SwitchInst *S = new SwitchInst(getVal($2, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001400 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
Chris Lattner00950542001-06-06 20:29:01 +00001401 $$ = S;
1402
Chris Lattner46748042002-04-09 19:41:42 +00001403 vector<pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
1404 E = $8->end();
1405 for (; I != E; ++I)
Chris Lattner00950542001-06-06 20:29:01 +00001406 S->dest_push_back(I->first, I->second);
1407 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001408 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO ResolvedVal
1409 EXCEPT ResolvedVal {
1410 const PointerType *PMTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001411 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00001412
1413 if (!(PMTy = dyn_cast<PointerType>($2->get())) ||
Chris Lattner79df7c02002-03-26 18:01:55 +00001414 !(Ty = dyn_cast<FunctionType>(PMTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00001415 // Pull out the types of all of the arguments...
1416 vector<const Type*> ParamTypes;
1417 if ($5) {
Chris Lattner6cdb0112001-11-26 16:54:11 +00001418 for (vector<Value*>::iterator I = $5->begin(), E = $5->end(); I!=E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00001419 ParamTypes.push_back((*I)->getType());
1420 }
1421
1422 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1423 if (isVarArg) ParamTypes.pop_back();
1424
Chris Lattner79df7c02002-03-26 18:01:55 +00001425 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattner2079fde2001-10-13 06:41:08 +00001426 PMTy = PointerType::get(Ty);
1427 }
1428 delete $2;
1429
Chris Lattner7e708292002-06-25 16:13:24 +00001430 Value *V = getVal(PMTy, $3); // Get the function we're calling...
Chris Lattner2079fde2001-10-13 06:41:08 +00001431
1432 BasicBlock *Normal = dyn_cast<BasicBlock>($8);
1433 BasicBlock *Except = dyn_cast<BasicBlock>($10);
1434
1435 if (Normal == 0 || Except == 0)
1436 ThrowException("Invoke instruction without label destinations!");
1437
1438 // Create the call node...
1439 if (!$5) { // Has no arguments?
Chris Lattner386a3b72001-10-16 19:54:17 +00001440 $$ = new InvokeInst(V, Normal, Except, vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001441 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001442 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00001443 // correctly!
1444 //
Chris Lattner79df7c02002-03-26 18:01:55 +00001445 FunctionType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1446 FunctionType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
Chris Lattner6cdb0112001-11-26 16:54:11 +00001447 vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00001448
1449 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1450 if ((*ArgI)->getType() != *I)
1451 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001452 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001453
1454 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1455 ThrowException("Invalid number of parameters detected!");
1456
Chris Lattner6cdb0112001-11-26 16:54:11 +00001457 $$ = new InvokeInst(V, Normal, Except, *$5);
Chris Lattner2079fde2001-10-13 06:41:08 +00001458 }
1459 delete $5;
Chris Lattner51727be2002-06-04 21:58:56 +00001460 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001461
1462
Chris Lattner00950542001-06-06 20:29:01 +00001463
1464JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1465 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001466 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001467 if (V == 0)
1468 ThrowException("May only switch on a constant pool value!");
1469
Chris Lattner9636a912001-10-01 16:18:37 +00001470 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($5, $6))));
Chris Lattner00950542001-06-06 20:29:01 +00001471 }
1472 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner46748042002-04-09 19:41:42 +00001473 $$ = new vector<pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001474 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00001475
1476 if (V == 0)
1477 ThrowException("May only switch on a constant pool value!");
1478
Chris Lattner9636a912001-10-01 16:18:37 +00001479 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($4, $5))));
Chris Lattner51727be2002-06-04 21:58:56 +00001480 };
Chris Lattner00950542001-06-06 20:29:01 +00001481
1482Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001483 // Is this definition named?? if so, assign the name...
1484 if (setValueName($2, $1)) { assert(0 && "No redefin allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001485 InsertValue($2);
1486 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001487};
Chris Lattner00950542001-06-06 20:29:01 +00001488
Chris Lattnerc24d2082001-06-11 15:04:20 +00001489PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
1490 $$ = new list<pair<Value*, BasicBlock*> >();
Chris Lattner30c89792001-09-07 16:35:17 +00001491 $$->push_back(make_pair(getVal(*$1, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001492 cast<BasicBlock>(getVal(Type::LabelTy, $5))));
Chris Lattner30c89792001-09-07 16:35:17 +00001493 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001494 }
1495 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1496 $$ = $1;
1497 $1->push_back(make_pair(getVal($1->front().first->getType(), $4),
Chris Lattner9636a912001-10-01 16:18:37 +00001498 cast<BasicBlock>(getVal(Type::LabelTy, $6))));
Chris Lattner51727be2002-06-04 21:58:56 +00001499 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00001500
1501
Chris Lattner30c89792001-09-07 16:35:17 +00001502ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner6cdb0112001-11-26 16:54:11 +00001503 $$ = new vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001504 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001505 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001506 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001507 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001508 $1->push_back($3);
Chris Lattner51727be2002-06-04 21:58:56 +00001509 };
Chris Lattner00950542001-06-06 20:29:01 +00001510
1511// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner51727be2002-06-04 21:58:56 +00001512ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001513
Chris Lattner4a6482b2002-09-10 19:57:26 +00001514InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
1515 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint())
1516 ThrowException("Arithmetic operator requires integer or FP operands!");
1517 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1518 if ($$ == 0)
1519 ThrowException("binary operator returned null!");
1520 delete $2;
1521 }
1522 | LogicalOps Types ValueRef ',' ValueRef {
1523 if (!(*$2)->isIntegral())
1524 ThrowException("Logical operator requires integral operands!");
1525 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1526 if ($$ == 0)
1527 ThrowException("binary operator returned null!");
1528 delete $2;
1529 }
1530 | SetCondOps Types ValueRef ',' ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001531 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001532 if ($$ == 0)
1533 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001534 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001535 }
Chris Lattner699f1eb2002-08-14 17:12:33 +00001536 | NOT ResolvedVal {
1537 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
1538 << " Replacing with 'xor'.\n";
1539
1540 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
1541 if (Ones == 0)
1542 ThrowException("Expected integral type for not instruction!");
1543
1544 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner00950542001-06-06 20:29:01 +00001545 if ($$ == 0)
Chris Lattner699f1eb2002-08-14 17:12:33 +00001546 ThrowException("Could not create a xor instruction!");
Chris Lattner09083092001-07-08 04:57:15 +00001547 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001548 | ShiftOps ResolvedVal ',' ResolvedVal {
1549 if ($4->getType() != Type::UByteTy)
1550 ThrowException("Shift amount must be ubyte!");
1551 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001552 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001553 | CAST ResolvedVal TO Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001554 $$ = new CastInst($2, *$4);
1555 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001556 }
Chris Lattnerc24d2082001-06-11 15:04:20 +00001557 | PHI PHIList {
1558 const Type *Ty = $2->front().first->getType();
1559 $$ = new PHINode(Ty);
Chris Lattner00950542001-06-06 20:29:01 +00001560 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001561 if ($2->front().first->getType() != Ty)
1562 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001563 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001564 $2->pop_front();
1565 }
1566 delete $2; // Free the list...
1567 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001568 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattneref9c23f2001-10-03 14:53:21 +00001569 const PointerType *PMTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001570 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001571
Chris Lattneref9c23f2001-10-03 14:53:21 +00001572 if (!(PMTy = dyn_cast<PointerType>($2->get())) ||
Chris Lattner79df7c02002-03-26 18:01:55 +00001573 !(Ty = dyn_cast<FunctionType>(PMTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001574 // Pull out the types of all of the arguments...
1575 vector<const Type*> ParamTypes;
Chris Lattneref9c23f2001-10-03 14:53:21 +00001576 if ($5) {
Chris Lattner6cdb0112001-11-26 16:54:11 +00001577 for (vector<Value*>::iterator I = $5->begin(), E = $5->end(); I!=E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00001578 ParamTypes.push_back((*I)->getType());
1579 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001580
1581 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1582 if (isVarArg) ParamTypes.pop_back();
1583
Chris Lattner79df7c02002-03-26 18:01:55 +00001584 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattneref9c23f2001-10-03 14:53:21 +00001585 PMTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001586 }
Chris Lattner30c89792001-09-07 16:35:17 +00001587 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001588
Chris Lattner7e708292002-06-25 16:13:24 +00001589 Value *V = getVal(PMTy, $3); // Get the function we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001590
Chris Lattner8b81bf52001-07-25 22:47:46 +00001591 // Create the call node...
1592 if (!$5) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00001593 // Make sure no arguments is a good thing!
1594 if (Ty->getNumParams() != 0)
1595 ThrowException("No arguments passed to a function that "
1596 "expects arguments!");
1597
Chris Lattner386a3b72001-10-16 19:54:17 +00001598 $$ = new CallInst(V, vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001599 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001600 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00001601 // correctly!
1602 //
Chris Lattner79df7c02002-03-26 18:01:55 +00001603 FunctionType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1604 FunctionType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
Chris Lattner6cdb0112001-11-26 16:54:11 +00001605 vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001606
1607 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1608 if ((*ArgI)->getType() != *I)
1609 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001610 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001611
Chris Lattner8b81bf52001-07-25 22:47:46 +00001612 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner00950542001-06-06 20:29:01 +00001613 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00001614
Chris Lattner6cdb0112001-11-26 16:54:11 +00001615 $$ = new CallInst(V, *$5);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001616 }
1617 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00001618 }
1619 | MemoryInst {
1620 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001621 };
Chris Lattner00950542001-06-06 20:29:01 +00001622
Chris Lattner6cdb0112001-11-26 16:54:11 +00001623
1624// IndexList - List of indices for GEP based instructions...
1625IndexList : ',' ValueRefList {
Chris Lattner027dcc52001-07-08 21:10:27 +00001626 $$ = $2;
1627} | /* empty */ {
Chris Lattner6cdb0112001-11-26 16:54:11 +00001628 $$ = new vector<Value*>();
Chris Lattner51727be2002-06-04 21:58:56 +00001629};
Chris Lattner027dcc52001-07-08 21:10:27 +00001630
Chris Lattner00950542001-06-06 20:29:01 +00001631MemoryInst : MALLOC Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001632 $$ = new MallocInst(PointerType::get(*$2));
1633 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001634 }
1635 | MALLOC Types ',' UINT ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001636 const Type *Ty = PointerType::get(*$2);
Chris Lattner8896eda2001-07-09 19:38:36 +00001637 $$ = new MallocInst(Ty, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001638 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001639 }
1640 | ALLOCA Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001641 $$ = new AllocaInst(PointerType::get(*$2));
1642 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001643 }
1644 | ALLOCA Types ',' UINT ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001645 const Type *Ty = PointerType::get(*$2);
Chris Lattner00950542001-06-06 20:29:01 +00001646 Value *ArrSize = getVal($4, $5);
Chris Lattnerf0d0e9c2001-07-07 08:36:30 +00001647 $$ = new AllocaInst(Ty, ArrSize);
Chris Lattner30c89792001-09-07 16:35:17 +00001648 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001649 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001650 | FREE ResolvedVal {
Chris Lattner9b625032002-05-06 16:15:30 +00001651 if (!isa<PointerType>($2->getType()))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001652 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00001653 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001654 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001655 }
1656
Chris Lattner6cdb0112001-11-26 16:54:11 +00001657 | LOAD Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00001658 if (!isa<PointerType>($2->get()))
Chris Lattner2079fde2001-10-13 06:41:08 +00001659 ThrowException("Can't load from nonpointer type: " +
1660 (*$2)->getDescription());
Chris Lattner5dfe7672002-08-22 22:48:55 +00001661 if (GetElementPtrInst::getIndexedType(*$2, *$4) == 0)
Chris Lattner027dcc52001-07-08 21:10:27 +00001662 ThrowException("Invalid indices for load instruction!");
1663
Chris Lattner0383cc42002-08-21 23:51:21 +00001664 Value *Src = getVal(*$2, $3);
1665 if (!$4->empty()) {
1666 std::cerr << "WARNING: Use of index load instruction:"
1667 << " replacing with getelementptr/load pair.\n";
1668 // Create a getelementptr hack instruction to do the right thing for
1669 // compatibility.
1670 //
1671 Instruction *I = new GetElementPtrInst(Src, *$4);
1672 CurBB->getInstList().push_back(I);
1673 Src = I;
1674 }
1675
1676 $$ = new LoadInst(Src);
Chris Lattner027dcc52001-07-08 21:10:27 +00001677 delete $4; // Free the vector...
Chris Lattner30c89792001-09-07 16:35:17 +00001678 delete $2;
Chris Lattner027dcc52001-07-08 21:10:27 +00001679 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00001680 | STORE ResolvedVal ',' Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00001681 if (!isa<PointerType>($4->get()))
Chris Lattner72e00252001-12-14 16:28:42 +00001682 ThrowException("Can't store to a nonpointer type: " +
1683 (*$4)->getDescription());
Chris Lattner5dfe7672002-08-22 22:48:55 +00001684 const Type *ElTy = GetElementPtrInst::getIndexedType(*$4, *$6);
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001685 if (ElTy == 0)
1686 ThrowException("Can't store into that field list!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001687 if (ElTy != $2->getType())
Chris Lattner72e00252001-12-14 16:28:42 +00001688 ThrowException("Can't store '" + $2->getType()->getDescription() +
1689 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner0383cc42002-08-21 23:51:21 +00001690
1691 Value *Ptr = getVal(*$4, $5);
1692 if (!$6->empty()) {
1693 std::cerr << "WARNING: Use of index store instruction:"
1694 << " replacing with getelementptr/store pair.\n";
1695 // Create a getelementptr hack instruction to do the right thing for
1696 // compatibility.
1697 //
1698 Instruction *I = new GetElementPtrInst(Ptr, *$6);
1699 CurBB->getInstList().push_back(I);
1700 Ptr = I;
1701 }
1702
1703 $$ = new StoreInst($2, Ptr);
Chris Lattner30c89792001-09-07 16:35:17 +00001704 delete $4; delete $6;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001705 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00001706 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00001707 if (!isa<PointerType>($2->get()))
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001708 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner30c89792001-09-07 16:35:17 +00001709 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner72e00252001-12-14 16:28:42 +00001710 ThrowException("Can't get element ptr '" + (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001711 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
1712 delete $2; delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00001713 };
Chris Lattner027dcc52001-07-08 21:10:27 +00001714
Chris Lattner00950542001-06-06 20:29:01 +00001715%%
Chris Lattner09083092001-07-08 04:57:15 +00001716int yyerror(const char *ErrorMsg) {
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001717 string where = string((CurFilename == "-")? string("<stdin>") : CurFilename)
1718 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
1719 string errMsg = string(ErrorMsg) + string("\n") + where + " while reading ";
1720 if (yychar == YYEMPTY)
1721 errMsg += "end-of-file.";
1722 else
1723 errMsg += "token: '" + string(llvmAsmtext, llvmAsmleng) + "'";
1724 ThrowException(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00001725 return 0;
1726}