blob: 34929c0bbfb9c44e787835b88a1f500b3152e646 [file] [log] [blame]
Chris Lattner00950542001-06-06 20:29:01 +00001//===-- llvmAsmParser.y - Parser for llvm assembly files ---------*- C++ -*--=//
2//
3// This file implements the bison parser for LLVM assembly languages files.
4//
5//===------------------------------------------------------------------------=//
6
Chris Lattner00950542001-06-06 20:29:01 +00007%{
8#include "ParserInternals.h"
Chris Lattner00950542001-06-06 20:29:01 +00009#include "llvm/SymbolTable.h"
10#include "llvm/Module.h"
Chris Lattner00950542001-06-06 20:29:01 +000011#include "llvm/iTerminators.h"
12#include "llvm/iMemory.h"
Chris Lattner1cff96a2002-09-10 22:37:46 +000013#include "llvm/iOperators.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000014#include "llvm/iPHINode.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000015#include "Support/STLExtras.h"
16#include "Support/DepthFirstIterator.h"
Chris Lattner00950542001-06-06 20:29:01 +000017#include <list>
Chris Lattnerc188eeb2002-07-30 18:54:25 +000018#include <utility>
Chris Lattner30c89792001-09-07 16:35:17 +000019#include <algorithm>
Chris Lattner697954c2002-01-20 22:54:45 +000020using std::list;
21using std::vector;
22using std::pair;
23using std::map;
24using std::pair;
25using std::make_pair;
Chris Lattner697954c2002-01-20 22:54:45 +000026using std::string;
Chris Lattner00950542001-06-06 20:29:01 +000027
Chris Lattner386a3b72001-10-16 19:54:17 +000028int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
Chris Lattner09083092001-07-08 04:57:15 +000029int yylex(); // declaration" of xxx warnings.
Chris Lattner00950542001-06-06 20:29:01 +000030int yyparse();
31
32static Module *ParserResult;
Chris Lattnera2850432001-07-22 18:36:00 +000033string CurFilename;
Chris Lattner00950542001-06-06 20:29:01 +000034
Chris Lattner30c89792001-09-07 16:35:17 +000035// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
36// relating to upreferences in the input stream.
37//
38//#define DEBUG_UPREFS 1
39#ifdef DEBUG_UPREFS
Chris Lattner699f1eb2002-08-14 17:12:33 +000040#define UR_OUT(X) std::cerr << X
Chris Lattner30c89792001-09-07 16:35:17 +000041#else
42#define UR_OUT(X)
43#endif
44
Vikram S. Adved3f7eb02002-07-14 22:59:28 +000045#define YYERROR_VERBOSE 1
46
Chris Lattner0383cc42002-08-21 23:51:21 +000047// HACK ALERT: This variable is used to implement the automatic conversion of
48// load/store instructions with indexes into a load/store + getelementptr pair
49// of instructions. When this compatiblity "Feature" is removed, this should be
50// too.
51//
52static BasicBlock *CurBB;
53
54
Chris Lattner7e708292002-06-25 16:13:24 +000055// This contains info used when building the body of a function. It is
56// destroyed when the function is completed.
Chris Lattner00950542001-06-06 20:29:01 +000057//
58typedef vector<Value *> ValueList; // Numbered defs
Chris Lattner386a3b72001-10-16 19:54:17 +000059static void ResolveDefinitions(vector<ValueList> &LateResolvers,
60 vector<ValueList> *FutureLateResolvers = 0);
Chris Lattner00950542001-06-06 20:29:01 +000061
62static struct PerModuleInfo {
63 Module *CurrentModule;
Chris Lattner30c89792001-09-07 16:35:17 +000064 vector<ValueList> Values; // Module level numbered definitions
65 vector<ValueList> LateResolveValues;
Chris Lattner8b88b3b2002-04-04 19:23:55 +000066 vector<PATypeHolder> Types;
67 map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner00950542001-06-06 20:29:01 +000068
Chris Lattner2079fde2001-10-13 06:41:08 +000069 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
70 // references to global values. Global values may be referenced before they
71 // are defined, and if so, the temporary object that they represent is held
Chris Lattnere9bb2df2001-12-03 22:26:30 +000072 // here. This is used for forward references of ConstantPointerRefs.
Chris Lattner2079fde2001-10-13 06:41:08 +000073 //
74 typedef map<pair<const PointerType *, ValID>, GlobalVariable*> GlobalRefsType;
75 GlobalRefsType GlobalRefs;
76
Chris Lattner00950542001-06-06 20:29:01 +000077 void ModuleDone() {
Chris Lattner7e708292002-06-25 16:13:24 +000078 // If we could not resolve some functions at function compilation time
79 // (calls to functions before they are defined), resolve them now... Types
80 // are resolved when the constant pool has been completely parsed.
Chris Lattner30c89792001-09-07 16:35:17 +000081 //
Chris Lattner00950542001-06-06 20:29:01 +000082 ResolveDefinitions(LateResolveValues);
83
Chris Lattner2079fde2001-10-13 06:41:08 +000084 // Check to make sure that all global value forward references have been
85 // resolved!
86 //
87 if (!GlobalRefs.empty()) {
Chris Lattner749ce032002-03-11 22:12:39 +000088 string UndefinedReferences = "Unresolved global references exist:\n";
89
90 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
91 I != E; ++I) {
92 UndefinedReferences += " " + I->first.first->getDescription() + " " +
93 I->first.second.getName() + "\n";
94 }
95 ThrowException(UndefinedReferences);
Chris Lattner2079fde2001-10-13 06:41:08 +000096 }
97
Chris Lattner7e708292002-06-25 16:13:24 +000098 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +000099 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +0000100 CurrentModule = 0;
101 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000102
103
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000104 // DeclareNewGlobalValue - Called every time a new GV has been defined. This
Chris Lattner2079fde2001-10-13 06:41:08 +0000105 // is used to remove things from the forward declaration map, resolving them
106 // to the correct thing as needed.
107 //
108 void DeclareNewGlobalValue(GlobalValue *GV, ValID D) {
109 // Check to see if there is a forward reference to this global variable...
110 // if there is, eliminate it and patch the reference to use the new def'n.
111 GlobalRefsType::iterator I = GlobalRefs.find(make_pair(GV->getType(), D));
112
113 if (I != GlobalRefs.end()) {
114 GlobalVariable *OldGV = I->second; // Get the placeholder...
115 I->first.second.destroy(); // Free string memory if neccesary
116
117 // Loop over all of the uses of the GlobalValue. The only thing they are
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000118 // allowed to be is ConstantPointerRef's.
Chris Lattner2079fde2001-10-13 06:41:08 +0000119 assert(OldGV->use_size() == 1 && "Only one reference should exist!");
120 while (!OldGV->use_empty()) {
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000121 User *U = OldGV->use_back(); // Must be a ConstantPointerRef...
122 ConstantPointerRef *CPR = cast<ConstantPointerRef>(U);
123 assert(CPR->getValue() == OldGV && "Something isn't happy");
124
125 // Change the const pool reference to point to the real global variable
126 // now. This should drop a use from the OldGV.
127 CPR->mutateReferences(OldGV, GV);
Chris Lattner2079fde2001-10-13 06:41:08 +0000128 }
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000129
130 // Remove OldGV from the module...
Chris Lattner2079fde2001-10-13 06:41:08 +0000131 CurrentModule->getGlobalList().remove(OldGV);
132 delete OldGV; // Delete the old placeholder
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000133
Chris Lattner2079fde2001-10-13 06:41:08 +0000134 // Remove the map entry for the global now that it has been created...
135 GlobalRefs.erase(I);
136 }
137 }
138
Chris Lattner00950542001-06-06 20:29:01 +0000139} CurModule;
140
Chris Lattner79df7c02002-03-26 18:01:55 +0000141static struct PerFunctionInfo {
Chris Lattner7e708292002-06-25 16:13:24 +0000142 Function *CurrentFunction; // Pointer to current function being created
Chris Lattner00950542001-06-06 20:29:01 +0000143
Chris Lattnere1815642001-07-15 06:35:53 +0000144 vector<ValueList> Values; // Keep track of numbered definitions
Chris Lattner00950542001-06-06 20:29:01 +0000145 vector<ValueList> LateResolveValues;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000146 vector<PATypeHolder> Types;
147 map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner7e708292002-06-25 16:13:24 +0000148 bool isDeclare; // Is this function a forward declararation?
Chris Lattner00950542001-06-06 20:29:01 +0000149
Chris Lattner79df7c02002-03-26 18:01:55 +0000150 inline PerFunctionInfo() {
151 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000152 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000153 }
154
Chris Lattner79df7c02002-03-26 18:01:55 +0000155 inline ~PerFunctionInfo() {}
Chris Lattner00950542001-06-06 20:29:01 +0000156
Chris Lattner79df7c02002-03-26 18:01:55 +0000157 inline void FunctionStart(Function *M) {
158 CurrentFunction = M;
Chris Lattner00950542001-06-06 20:29:01 +0000159 }
160
Chris Lattner79df7c02002-03-26 18:01:55 +0000161 void FunctionDone() {
Chris Lattner00950542001-06-06 20:29:01 +0000162 // If we could not resolve some blocks at parsing time (forward branches)
163 // resolve the branches now...
Chris Lattner386a3b72001-10-16 19:54:17 +0000164 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000165
Chris Lattner7e708292002-06-25 16:13:24 +0000166 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +0000167 Types.clear();
Chris Lattner79df7c02002-03-26 18:01:55 +0000168 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000169 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000170 }
Chris Lattner7e708292002-06-25 16:13:24 +0000171} CurMeth; // Info for the current function...
Chris Lattner00950542001-06-06 20:29:01 +0000172
Chris Lattner79df7c02002-03-26 18:01:55 +0000173static bool inFunctionScope() { return CurMeth.CurrentFunction != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000174
Chris Lattner00950542001-06-06 20:29:01 +0000175
176//===----------------------------------------------------------------------===//
177// Code to handle definitions of all the types
178//===----------------------------------------------------------------------===//
179
Chris Lattner2079fde2001-10-13 06:41:08 +0000180static int InsertValue(Value *D, vector<ValueList> &ValueTab = CurMeth.Values) {
181 if (D->hasName()) return -1; // Is this a numbered definition?
182
183 // Yes, insert the value into the value table...
184 unsigned type = D->getType()->getUniqueID();
185 if (ValueTab.size() <= type)
186 ValueTab.resize(type+1, ValueList());
187 //printf("Values[%d][%d] = %d\n", type, ValueTab[type].size(), D);
188 ValueTab[type].push_back(D);
189 return ValueTab[type].size()-1;
Chris Lattner00950542001-06-06 20:29:01 +0000190}
191
Chris Lattner30c89792001-09-07 16:35:17 +0000192// TODO: FIXME when Type are not const
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000193static void InsertType(const Type *Ty, vector<PATypeHolder> &Types) {
Chris Lattner30c89792001-09-07 16:35:17 +0000194 Types.push_back(Ty);
195}
196
197static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000198 switch (D.Type) {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000199 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000200 unsigned Num = (unsigned)D.Num;
201
202 // Module constants occupy the lowest numbered slots...
203 if (Num < CurModule.Types.size())
204 return CurModule.Types[Num];
205
206 Num -= CurModule.Types.size();
207
208 // Check that the number is within bounds...
209 if (Num <= CurMeth.Types.size())
210 return CurMeth.Types[Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000211 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000212 }
Chris Lattnerf8dff732002-07-18 05:18:37 +0000213 case ValID::NameVal: { // Is it a named definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000214 string Name(D.Name);
215 SymbolTable *SymTab = 0;
Chris Lattner79df7c02002-03-26 18:01:55 +0000216 if (inFunctionScope()) SymTab = CurMeth.CurrentFunction->getSymbolTable();
Chris Lattner30c89792001-09-07 16:35:17 +0000217 Value *N = SymTab ? SymTab->lookup(Type::TypeTy, Name) : 0;
218
219 if (N == 0) {
Chris Lattner7e708292002-06-25 16:13:24 +0000220 // Symbol table doesn't automatically chain yet... because the function
Chris Lattner30c89792001-09-07 16:35:17 +0000221 // hasn't been added to the module...
222 //
223 SymTab = CurModule.CurrentModule->getSymbolTable();
224 if (SymTab)
225 N = SymTab->lookup(Type::TypeTy, Name);
226 if (N == 0) break;
227 }
228
229 D.destroy(); // Free old strdup'd memory...
Chris Lattnercfe26c92001-10-01 18:26:53 +0000230 return cast<const Type>(N);
Chris Lattner30c89792001-09-07 16:35:17 +0000231 }
232 default:
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000233 ThrowException("Internal parser error: Invalid symbol type reference!");
Chris Lattner30c89792001-09-07 16:35:17 +0000234 }
235
236 // If we reached here, we referenced either a symbol that we don't know about
237 // or an id number that hasn't been read yet. We may be referencing something
238 // forward, so just create an entry to be resolved later and get to it...
239 //
240 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
241
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000242 map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner4a42e902001-10-22 05:56:09 +0000243 CurMeth.LateResolveTypes : CurModule.LateResolveTypes;
244
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000245 map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000246 if (I != LateResolver.end()) {
247 return I->second;
248 }
Chris Lattner30c89792001-09-07 16:35:17 +0000249
Chris Lattner82269592001-10-22 06:01:08 +0000250 Type *Typ = OpaqueType::get();
Chris Lattner4a42e902001-10-22 05:56:09 +0000251 LateResolver.insert(make_pair(D, Typ));
Chris Lattner30c89792001-09-07 16:35:17 +0000252 return Typ;
253}
254
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000255static Value *lookupInSymbolTable(const Type *Ty, const string &Name) {
256 SymbolTable *SymTab =
Chris Lattner9705a152002-05-02 19:27:42 +0000257 inFunctionScope() ? CurMeth.CurrentFunction->getSymbolTable() :
258 CurModule.CurrentModule->getSymbolTable();
Chris Lattner924025e2002-04-29 18:25:33 +0000259 return SymTab ? SymTab->lookup(Ty, Name) : 0;
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000260}
261
Chris Lattner2079fde2001-10-13 06:41:08 +0000262// getValNonImprovising - Look up the value specified by the provided type and
263// the provided ValID. If the value exists and has already been defined, return
264// it. Otherwise return null.
265//
266static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000267 if (isa<FunctionType>(Ty))
268 ThrowException("Functions are not values and "
269 "must be referenced as pointers");
Chris Lattner386a3b72001-10-16 19:54:17 +0000270
Chris Lattner30c89792001-09-07 16:35:17 +0000271 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000272 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000273 unsigned type = Ty->getUniqueID();
Chris Lattner00950542001-06-06 20:29:01 +0000274 unsigned Num = (unsigned)D.Num;
275
276 // Module constants occupy the lowest numbered slots...
277 if (type < CurModule.Values.size()) {
278 if (Num < CurModule.Values[type].size())
279 return CurModule.Values[type][Num];
280
281 Num -= CurModule.Values[type].size();
282 }
283
284 // Make sure that our type is within bounds
Chris Lattner2079fde2001-10-13 06:41:08 +0000285 if (CurMeth.Values.size() <= type) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000286
287 // Check that the number is within bounds...
Chris Lattner2079fde2001-10-13 06:41:08 +0000288 if (CurMeth.Values[type].size() <= Num) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000289
290 return CurMeth.Values[type][Num];
291 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000292
Chris Lattner1a1cb112001-09-30 22:46:54 +0000293 case ValID::NameVal: { // Is it a named definition?
Chris Lattner2079fde2001-10-13 06:41:08 +0000294 Value *N = lookupInSymbolTable(Ty, string(D.Name));
295 if (N == 0) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000296
297 D.destroy(); // Free old strdup'd memory...
298 return N;
299 }
300
Chris Lattner2079fde2001-10-13 06:41:08 +0000301 // Check to make sure that "Ty" is an integral type, and that our
302 // value will fit into the specified type...
303 case ValID::ConstSIntVal: // Is it a constant pool reference??
Chris Lattnerd78700d2002-08-16 21:14:40 +0000304 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
305 ThrowException("Signed integral constant '" +
306 itostr(D.ConstPool64) + "' is invalid for type '" +
307 Ty->getDescription() + "'!");
308 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000309
310 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000311 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
312 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000313 ThrowException("Integral constant '" + utostr(D.UConstPool64) +
314 "' is invalid or out of range!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000315 } else { // This is really a signed reference. Transmogrify.
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000316 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000317 }
318 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000319 return ConstantUInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000320 }
321
Chris Lattner2079fde2001-10-13 06:41:08 +0000322 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000323 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattner2079fde2001-10-13 06:41:08 +0000324 ThrowException("FP constant invalid for type!!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000325 return ConstantFP::get(Ty, D.ConstPoolFP);
Chris Lattner2079fde2001-10-13 06:41:08 +0000326
327 case ValID::ConstNullVal: // Is it a null value?
Chris Lattner9b625032002-05-06 16:15:30 +0000328 if (!isa<PointerType>(Ty))
Chris Lattner2079fde2001-10-13 06:41:08 +0000329 ThrowException("Cannot create a a non pointer null!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000330 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner2079fde2001-10-13 06:41:08 +0000331
Chris Lattnerd78700d2002-08-16 21:14:40 +0000332 case ValID::ConstantVal: // Fully resolved constant?
333 if (D.ConstantValue->getType() != Ty)
334 ThrowException("Constant expression type different from required type!");
335 return D.ConstantValue;
336
Chris Lattner30c89792001-09-07 16:35:17 +0000337 default:
338 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000339 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000340 } // End of switch
341
Chris Lattner2079fde2001-10-13 06:41:08 +0000342 assert(0 && "Unhandled case!");
343 return 0;
344}
345
346
347// getVal - This function is identical to getValNonImprovising, except that if a
348// value is not already defined, it "improvises" by creating a placeholder var
349// that looks and acts just like the requested variable. When the value is
350// defined later, all uses of the placeholder variable are replaced with the
351// real thing.
352//
353static Value *getVal(const Type *Ty, const ValID &D) {
354 assert(Ty != Type::TypeTy && "Should use getTypeVal for types!");
355
356 // See if the value has already been defined...
357 Value *V = getValNonImprovising(Ty, D);
358 if (V) return V;
Chris Lattner00950542001-06-06 20:29:01 +0000359
360 // If we reached here, we referenced either a symbol that we don't know about
361 // or an id number that hasn't been read yet. We may be referencing something
362 // forward, so just create an entry to be resolved later and get to it...
363 //
Chris Lattner00950542001-06-06 20:29:01 +0000364 Value *d = 0;
Chris Lattner30c89792001-09-07 16:35:17 +0000365 switch (Ty->getPrimitiveID()) {
366 case Type::LabelTyID: d = new BBPlaceHolder(Ty, D); break;
Chris Lattner30c89792001-09-07 16:35:17 +0000367 default: d = new ValuePlaceHolder(Ty, D); break;
Chris Lattner00950542001-06-06 20:29:01 +0000368 }
369
370 assert(d != 0 && "How did we not make something?");
Chris Lattner79df7c02002-03-26 18:01:55 +0000371 if (inFunctionScope())
Chris Lattner386a3b72001-10-16 19:54:17 +0000372 InsertValue(d, CurMeth.LateResolveValues);
373 else
374 InsertValue(d, CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000375 return d;
376}
377
378
379//===----------------------------------------------------------------------===//
380// Code to handle forward references in instructions
381//===----------------------------------------------------------------------===//
382//
383// This code handles the late binding needed with statements that reference
384// values not defined yet... for example, a forward branch, or the PHI node for
385// a loop body.
386//
387// This keeps a table (CurMeth.LateResolveValues) of all such forward references
388// and back patchs after we are done.
389//
390
391// ResolveDefinitions - If we could not resolve some defs at parsing
392// time (forward branches, phi functions for loops, etc...) resolve the
393// defs now...
394//
Chris Lattner386a3b72001-10-16 19:54:17 +0000395static void ResolveDefinitions(vector<ValueList> &LateResolvers,
Chris Lattnerbcafcce2002-07-25 06:17:42 +0000396 vector<ValueList> *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000397 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
398 for (unsigned ty = 0; ty < LateResolvers.size(); ty++) {
399 while (!LateResolvers[ty].empty()) {
400 Value *V = LateResolvers[ty].back();
Chris Lattner386a3b72001-10-16 19:54:17 +0000401 assert(!isa<Type>(V) && "Types should be in LateResolveTypes!");
402
Chris Lattner00950542001-06-06 20:29:01 +0000403 LateResolvers[ty].pop_back();
404 ValID &DID = getValIDFromPlaceHolder(V);
405
Chris Lattner2079fde2001-10-13 06:41:08 +0000406 Value *TheRealValue = getValNonImprovising(Type::getUniqueIDType(ty),DID);
Chris Lattner386a3b72001-10-16 19:54:17 +0000407 if (TheRealValue) {
408 V->replaceAllUsesWith(TheRealValue);
409 delete V;
410 } else if (FutureLateResolvers) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000411 // Functions have their unresolved items forwarded to the module late
Chris Lattner386a3b72001-10-16 19:54:17 +0000412 // resolver table
413 InsertValue(V, *FutureLateResolvers);
414 } else {
Chris Lattner9705a152002-05-02 19:27:42 +0000415 if (DID.Type == ValID::NameVal)
Chris Lattner30c89792001-09-07 16:35:17 +0000416 ThrowException("Reference to an invalid definition: '" +DID.getName()+
417 "' of type '" + V->getType()->getDescription() + "'",
418 getLineNumFromPlaceHolder(V));
419 else
420 ThrowException("Reference to an invalid definition: #" +
421 itostr(DID.Num) + " of type '" +
422 V->getType()->getDescription() + "'",
423 getLineNumFromPlaceHolder(V));
424 }
Chris Lattner00950542001-06-06 20:29:01 +0000425 }
426 }
427
428 LateResolvers.clear();
429}
430
Chris Lattner4a42e902001-10-22 05:56:09 +0000431// ResolveTypeTo - A brand new type was just declared. This means that (if
432// name is not null) things referencing Name can be resolved. Otherwise, things
433// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000434//
Chris Lattner4a42e902001-10-22 05:56:09 +0000435static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000436 vector<PATypeHolder> &Types = inFunctionScope() ?
Chris Lattner4a42e902001-10-22 05:56:09 +0000437 CurMeth.Types : CurModule.Types;
Chris Lattner00950542001-06-06 20:29:01 +0000438
Chris Lattner4a42e902001-10-22 05:56:09 +0000439 ValID D;
440 if (Name) D = ValID::create(Name);
441 else D = ValID::create((int)Types.size());
Chris Lattner30c89792001-09-07 16:35:17 +0000442
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000443 map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner4a42e902001-10-22 05:56:09 +0000444 CurMeth.LateResolveTypes : CurModule.LateResolveTypes;
445
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000446 map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000447 if (I != LateResolver.end()) {
Chris Lattner51727be2002-06-04 21:58:56 +0000448 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattner4a42e902001-10-22 05:56:09 +0000449 LateResolver.erase(I);
450 }
451}
452
453// ResolveTypes - At this point, all types should be resolved. Any that aren't
454// are errors.
455//
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000456static void ResolveTypes(map<ValID, PATypeHolder> &LateResolveTypes) {
Chris Lattner4a42e902001-10-22 05:56:09 +0000457 if (!LateResolveTypes.empty()) {
Chris Lattner82269592001-10-22 06:01:08 +0000458 const ValID &DID = LateResolveTypes.begin()->first;
Chris Lattner4a42e902001-10-22 05:56:09 +0000459
460 if (DID.Type == ValID::NameVal)
Chris Lattner82269592001-10-22 06:01:08 +0000461 ThrowException("Reference to an invalid type: '" +DID.getName() + "'");
Chris Lattner4a42e902001-10-22 05:56:09 +0000462 else
Chris Lattner82269592001-10-22 06:01:08 +0000463 ThrowException("Reference to an invalid type: #" + itostr(DID.Num));
Chris Lattner30c89792001-09-07 16:35:17 +0000464 }
465}
466
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000467
Chris Lattner1781aca2001-09-18 04:00:54 +0000468// setValueName - Set the specified value to the name given. The name may be
469// null potentially, in which case this is a noop. The string passed in is
470// assumed to be a malloc'd string buffer, and is freed by this function.
471//
Chris Lattnerb7474512001-10-03 15:39:04 +0000472// This function returns true if the value has already been defined, but is
473// allowed to be redefined in the specified context. If the name is a new name
474// for the typeplane, false is returned.
475//
476static bool setValueName(Value *V, char *NameStr) {
477 if (NameStr == 0) return false;
Chris Lattner386a3b72001-10-16 19:54:17 +0000478
Chris Lattner1781aca2001-09-18 04:00:54 +0000479 string Name(NameStr); // Copy string
480 free(NameStr); // Free old string
481
Chris Lattner2079fde2001-10-13 06:41:08 +0000482 if (V->getType() == Type::VoidTy)
483 ThrowException("Can't assign name '" + Name +
484 "' to a null valued instruction!");
485
Chris Lattner79df7c02002-03-26 18:01:55 +0000486 SymbolTable *ST = inFunctionScope() ?
487 CurMeth.CurrentFunction->getSymbolTableSure() :
Chris Lattner30c89792001-09-07 16:35:17 +0000488 CurModule.CurrentModule->getSymbolTableSure();
489
490 Value *Existing = ST->lookup(V->getType(), Name);
491 if (Existing) { // Inserting a name that is already defined???
492 // There is only one case where this is allowed: when we are refining an
493 // opaque type. In this case, Existing will be an opaque type.
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000494 if (const Type *Ty = dyn_cast<const Type>(Existing)) {
Chris Lattner51727be2002-06-04 21:58:56 +0000495 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Ty)) {
Chris Lattner30c89792001-09-07 16:35:17 +0000496 // We ARE replacing an opaque type!
Chris Lattner51727be2002-06-04 21:58:56 +0000497 ((OpaqueType*)OpTy)->refineAbstractTypeTo(cast<Type>(V));
Chris Lattnerb7474512001-10-03 15:39:04 +0000498 return true;
Chris Lattner30c89792001-09-07 16:35:17 +0000499 }
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000500 }
Chris Lattner30c89792001-09-07 16:35:17 +0000501
Chris Lattner9636a912001-10-01 16:18:37 +0000502 // Otherwise, we are a simple redefinition of a value, check to see if it
503 // is defined the same as the old one...
504 if (const Type *Ty = dyn_cast<const Type>(Existing)) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000505 if (Ty == cast<const Type>(V)) return true; // Yes, it's equal.
Chris Lattner699f1eb2002-08-14 17:12:33 +0000506 // std::cerr << "Type: " << Ty->getDescription() << " != "
Chris Lattnerb7474512001-10-03 15:39:04 +0000507 // << cast<const Type>(V)->getDescription() << "!\n";
508 } else if (GlobalVariable *EGV = dyn_cast<GlobalVariable>(Existing)) {
Chris Lattner43efcbf2001-10-03 19:35:57 +0000509 // We are allowed to redefine a global variable in two circumstances:
510 // 1. If at least one of the globals is uninitialized or
511 // 2. If both initializers have the same value.
512 //
513 // This can only be done if the const'ness of the vars is the same.
514 //
Chris Lattner89219832001-10-03 19:35:04 +0000515 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
516 if (EGV->isConstant() == GV->isConstant() &&
517 (!EGV->hasInitializer() || !GV->hasInitializer() ||
518 EGV->getInitializer() == GV->getInitializer())) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000519
Chris Lattner89219832001-10-03 19:35:04 +0000520 // Make sure the existing global version gets the initializer!
521 if (GV->hasInitializer() && !EGV->hasInitializer())
522 EGV->setInitializer(GV->getInitializer());
523
Chris Lattner2079fde2001-10-13 06:41:08 +0000524 delete GV; // Destroy the duplicate!
Chris Lattner89219832001-10-03 19:35:04 +0000525 return true; // They are equivalent!
526 }
Chris Lattnerb7474512001-10-03 15:39:04 +0000527 }
Chris Lattner9636a912001-10-01 16:18:37 +0000528 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000529 ThrowException("Redefinition of value named '" + Name + "' in the '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000530 V->getType()->getDescription() + "' type plane!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000531 }
Chris Lattner00950542001-06-06 20:29:01 +0000532
Chris Lattner30c89792001-09-07 16:35:17 +0000533 V->setName(Name, ST);
Chris Lattnerb7474512001-10-03 15:39:04 +0000534 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000535}
536
Chris Lattner8896eda2001-07-09 19:38:36 +0000537
Chris Lattner30c89792001-09-07 16:35:17 +0000538//===----------------------------------------------------------------------===//
539// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000540//
Chris Lattner8896eda2001-07-09 19:38:36 +0000541
Chris Lattner30c89792001-09-07 16:35:17 +0000542// TypeContains - Returns true if Ty contains E in it.
543//
544static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner3ff43872001-09-28 22:56:31 +0000545 return find(df_begin(Ty), df_end(Ty), E) != df_end(Ty);
Chris Lattner30c89792001-09-07 16:35:17 +0000546}
Chris Lattner698b56e2001-07-20 19:15:08 +0000547
Chris Lattner30c89792001-09-07 16:35:17 +0000548
549static vector<pair<unsigned, OpaqueType *> > UpRefs;
550
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000551static PATypeHolder HandleUpRefs(const Type *ty) {
552 PATypeHolder Ty(ty);
Chris Lattner5084d032001-11-02 07:46:26 +0000553 UR_OUT("Type '" << ty->getDescription() <<
554 "' newly formed. Resolving upreferences.\n" <<
555 UpRefs.size() << " upreferences active!\n");
Chris Lattner30c89792001-09-07 16:35:17 +0000556 for (unsigned i = 0; i < UpRefs.size(); ) {
Chris Lattner5084d032001-11-02 07:46:26 +0000557 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Chris Lattner30c89792001-09-07 16:35:17 +0000558 << UpRefs[i].second->getDescription() << ") = "
Chris Lattner5084d032001-11-02 07:46:26 +0000559 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << endl);
Chris Lattner30c89792001-09-07 16:35:17 +0000560 if (TypeContains(Ty, UpRefs[i].second)) {
561 unsigned Level = --UpRefs[i].first; // Decrement level of upreference
Chris Lattner5084d032001-11-02 07:46:26 +0000562 UR_OUT(" Uplevel Ref Level = " << Level << endl);
Chris Lattner30c89792001-09-07 16:35:17 +0000563 if (Level == 0) { // Upreference should be resolved!
Chris Lattner5084d032001-11-02 07:46:26 +0000564 UR_OUT(" * Resolving upreference for "
565 << UpRefs[i].second->getDescription() << endl;
Chris Lattner30c89792001-09-07 16:35:17 +0000566 string OldName = UpRefs[i].second->getDescription());
567 UpRefs[i].second->refineAbstractTypeTo(Ty);
568 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
Chris Lattner5084d032001-11-02 07:46:26 +0000569 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
Chris Lattner30c89792001-09-07 16:35:17 +0000570 << (const void*)Ty << ", " << Ty->getDescription() << endl);
571 continue;
572 }
573 }
574
575 ++i; // Otherwise, no resolve, move on...
Chris Lattner8896eda2001-07-09 19:38:36 +0000576 }
Chris Lattner30c89792001-09-07 16:35:17 +0000577 // FIXME: TODO: this should return the updated type
Chris Lattner8896eda2001-07-09 19:38:36 +0000578 return Ty;
579}
580
Chris Lattner30c89792001-09-07 16:35:17 +0000581
Chris Lattner00950542001-06-06 20:29:01 +0000582//===----------------------------------------------------------------------===//
583// RunVMAsmParser - Define an interface to this parser
584//===----------------------------------------------------------------------===//
585//
Chris Lattnera2850432001-07-22 18:36:00 +0000586Module *RunVMAsmParser(const string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000587 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000588 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000589 llvmAsmlineno = 1; // Reset the current line number...
590
591 CurModule.CurrentModule = new Module(); // Allocate a new module to read
592 yyparse(); // Parse the file.
593 Module *Result = ParserResult;
Chris Lattner00950542001-06-06 20:29:01 +0000594 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
595 ParserResult = 0;
596
597 return Result;
598}
599
600%}
601
602%union {
Chris Lattner30c89792001-09-07 16:35:17 +0000603 Module *ModuleVal;
Chris Lattner79df7c02002-03-26 18:01:55 +0000604 Function *FunctionVal;
Chris Lattner46748042002-04-09 19:41:42 +0000605 std::pair<Argument*, char*> *ArgVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000606 BasicBlock *BasicBlockVal;
607 TerminatorInst *TermInstVal;
608 Instruction *InstVal;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000609 Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000610
Chris Lattner30c89792001-09-07 16:35:17 +0000611 const Type *PrimType;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000612 PATypeHolder *TypeVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000613 Value *ValueVal;
614
Chris Lattner46748042002-04-09 19:41:42 +0000615 std::list<std::pair<Argument*,char*> > *ArgList;
Chris Lattner697954c2002-01-20 22:54:45 +0000616 std::vector<Value*> *ValueList;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000617 std::list<PATypeHolder> *TypeList;
Chris Lattner697954c2002-01-20 22:54:45 +0000618 std::list<std::pair<Value*,
619 BasicBlock*> > *PHIList; // Represent the RHS of PHI node
Chris Lattner46748042002-04-09 19:41:42 +0000620 std::vector<std::pair<Constant*, BasicBlock*> > *JumpTable;
Chris Lattner697954c2002-01-20 22:54:45 +0000621 std::vector<Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000622
Chris Lattner30c89792001-09-07 16:35:17 +0000623 int64_t SInt64Val;
624 uint64_t UInt64Val;
625 int SIntVal;
626 unsigned UIntVal;
627 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000628 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000629
Chris Lattner30c89792001-09-07 16:35:17 +0000630 char *StrVal; // This memory is strdup'd!
631 ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000632
Chris Lattner30c89792001-09-07 16:35:17 +0000633 Instruction::BinaryOps BinaryOpVal;
634 Instruction::TermOps TermOpVal;
635 Instruction::MemoryOps MemOpVal;
636 Instruction::OtherOps OtherOpVal;
Chris Lattner00950542001-06-06 20:29:01 +0000637}
638
Chris Lattner79df7c02002-03-26 18:01:55 +0000639%type <ModuleVal> Module FunctionList
640%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000641%type <BasicBlockVal> BasicBlock InstructionList
642%type <TermInstVal> BBTerminatorInst
643%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000644%type <ConstVal> ConstVal ConstExpr
Chris Lattner6cdb0112001-11-26 16:54:11 +0000645%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000646%type <ArgList> ArgList ArgListH
647%type <ArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000648%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000649%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000650%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000651%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000652%type <JumpTable> JumpTable
Chris Lattnerdda71962001-11-26 18:54:16 +0000653%type <BoolVal> GlobalType OptInternal // GLOBAL or CONSTANT? Intern?
Chris Lattner00950542001-06-06 20:29:01 +0000654
Chris Lattner2079fde2001-10-13 06:41:08 +0000655// ValueRef - Unresolved reference to a definition or BB
656%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000657%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000658// Tokens and types for handling constant integer values
659//
660// ESINT64VAL - A negative number within long long range
661%token <SInt64Val> ESINT64VAL
662
663// EUINT64VAL - A positive number within uns. long long range
664%token <UInt64Val> EUINT64VAL
665%type <SInt64Val> EINT64VAL
666
667%token <SIntVal> SINTVAL // Signed 32 bit ints...
668%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
669%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000670%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000671
672// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000673%type <TypeVal> Types TypesV UpRTypes UpRTypesV
674%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattner30c89792001-09-07 16:35:17 +0000675%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
676%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000677
678%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
Chris Lattner8ebccb72002-05-22 22:33:00 +0000679%type <StrVal> OptVAR_ID OptAssign FuncName
Chris Lattner00950542001-06-06 20:29:01 +0000680
681
Chris Lattner9b02cc32002-05-03 18:23:48 +0000682%token IMPLEMENTATION TRUE FALSE BEGINTOK ENDTOK DECLARE GLOBAL CONSTANT UNINIT
Chris Lattnerd78700d2002-08-16 21:14:40 +0000683%token TO EXCEPT DOTDOTDOT NULL_TOK CONST INTERNAL OPAQUE NOT
Chris Lattner00950542001-06-06 20:29:01 +0000684
685// Basic Block Terminating Operators
686%token <TermOpVal> RET BR SWITCH
687
Chris Lattner00950542001-06-06 20:29:01 +0000688// Binary Operators
689%type <BinaryOpVal> BinaryOps // all the binary operators
Chris Lattner4a6482b2002-09-10 19:57:26 +0000690%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Chris Lattner42c9e772001-10-20 09:32:59 +0000691%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000692%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000693
694// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000695%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000696
Chris Lattner027dcc52001-07-08 21:10:27 +0000697// Other Operators
698%type <OtherOpVal> ShiftOps
Chris Lattner2079fde2001-10-13 06:41:08 +0000699%token <OtherOpVal> PHI CALL INVOKE CAST SHL SHR
Chris Lattner027dcc52001-07-08 21:10:27 +0000700
Chris Lattner00950542001-06-06 20:29:01 +0000701%start Module
702%%
703
704// Handle constant integer size restriction and conversion...
705//
706
Chris Lattner51727be2002-06-04 21:58:56 +0000707INTVAL : SINTVAL;
Chris Lattner00950542001-06-06 20:29:01 +0000708INTVAL : UINTVAL {
709 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
710 ThrowException("Value too large for type!");
711 $$ = (int32_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000712};
Chris Lattner00950542001-06-06 20:29:01 +0000713
714
Chris Lattner51727be2002-06-04 21:58:56 +0000715EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner00950542001-06-06 20:29:01 +0000716EINT64VAL : EUINT64VAL {
717 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
718 ThrowException("Value too large for type!");
719 $$ = (int64_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000720};
Chris Lattner00950542001-06-06 20:29:01 +0000721
Chris Lattner00950542001-06-06 20:29:01 +0000722// Operations that are notably excluded from this list include:
723// RET, BR, & SWITCH because they end basic blocks and are treated specially.
724//
Chris Lattner4a6482b2002-09-10 19:57:26 +0000725ArithmeticOps: ADD | SUB | MUL | DIV | REM;
726LogicalOps : AND | OR | XOR;
727SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
728BinaryOps : ArithmeticOps | LogicalOps | SetCondOps;
729
Chris Lattner51727be2002-06-04 21:58:56 +0000730ShiftOps : SHL | SHR;
Chris Lattner00950542001-06-06 20:29:01 +0000731
Chris Lattnere98dda62001-07-14 06:10:16 +0000732// These are some types that allow classification if we only want a particular
733// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner51727be2002-06-04 21:58:56 +0000734SIntType : LONG | INT | SHORT | SBYTE;
735UIntType : ULONG | UINT | USHORT | UBYTE;
736IntType : SIntType | UIntType;
737FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +0000738
Chris Lattnere98dda62001-07-14 06:10:16 +0000739// OptAssign - Value producing statements have an optional assignment component
Chris Lattner00950542001-06-06 20:29:01 +0000740OptAssign : VAR_ID '=' {
741 $$ = $1;
742 }
743 | /*empty*/ {
744 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +0000745 };
Chris Lattner00950542001-06-06 20:29:01 +0000746
Chris Lattner51727be2002-06-04 21:58:56 +0000747OptInternal : INTERNAL { $$ = true; } | /*empty*/ { $$ = false; };
Chris Lattner30c89792001-09-07 16:35:17 +0000748
749//===----------------------------------------------------------------------===//
750// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +0000751// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +0000752// access to it, a user must explicitly use TypesV.
753//
754
755// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +0000756TypesV : Types | VOID { $$ = new PATypeHolder($1); };
757UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +0000758
759Types : UpRTypes {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000760 if (UpRefs.size())
761 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
762 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000763 };
Chris Lattner30c89792001-09-07 16:35:17 +0000764
765
766// Derived types are added later...
767//
Chris Lattner51727be2002-06-04 21:58:56 +0000768PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
769PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000770UpRTypes : OPAQUE {
771 $$ = new PATypeHolder(OpaqueType::get());
772 }
773 | PrimType {
774 $$ = new PATypeHolder($1);
Chris Lattner51727be2002-06-04 21:58:56 +0000775 };
Chris Lattnerd78700d2002-08-16 21:14:40 +0000776UpRTypes : SymbolicValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000777 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner51727be2002-06-04 21:58:56 +0000778};
Chris Lattner30c89792001-09-07 16:35:17 +0000779
Chris Lattner30c89792001-09-07 16:35:17 +0000780// Include derived types in the Types production.
781//
782UpRTypes : '\\' EUINT64VAL { // Type UpReference
783 if ($2 > (uint64_t)INT64_MAX) ThrowException("Value out of range!");
784 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
785 UpRefs.push_back(make_pair((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000786 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +0000787 UR_OUT("New Upreference!\n");
788 }
Chris Lattner79df7c02002-03-26 18:01:55 +0000789 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner30c89792001-09-07 16:35:17 +0000790 vector<const Type*> Params;
Chris Lattner697954c2002-01-20 22:54:45 +0000791 mapto($3->begin(), $3->end(), std::back_inserter(Params),
792 std::mem_fun_ref(&PATypeHandle<Type>::get));
Chris Lattner2079fde2001-10-13 06:41:08 +0000793 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
794 if (isVarArg) Params.pop_back();
795
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000796 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +0000797 delete $3; // Delete the argument list
798 delete $1; // Delete the old type handle
799 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000800 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000801 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000802 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +0000803 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000804 | '{' TypeListI '}' { // Structure type?
805 vector<const Type*> Elements;
Chris Lattner697954c2002-01-20 22:54:45 +0000806 mapto($2->begin(), $2->end(), std::back_inserter(Elements),
807 std::mem_fun_ref(&PATypeHandle<Type>::get));
Chris Lattner30c89792001-09-07 16:35:17 +0000808
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000809 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000810 delete $2;
811 }
812 | '{' '}' { // Empty structure type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000813 $$ = new PATypeHolder(StructType::get(vector<const Type*>()));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000814 }
815 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000816 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000817 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000818 };
Chris Lattner30c89792001-09-07 16:35:17 +0000819
Chris Lattner7e708292002-06-25 16:13:24 +0000820// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +0000821// declaration type lists
822//
823TypeListI : UpRTypes {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000824 $$ = new list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +0000825 $$->push_back(*$1); delete $1;
826 }
827 | TypeListI ',' UpRTypes {
828 ($$=$1)->push_back(*$3); delete $3;
Chris Lattner51727be2002-06-04 21:58:56 +0000829 };
Chris Lattner30c89792001-09-07 16:35:17 +0000830
Chris Lattner7e708292002-06-25 16:13:24 +0000831// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +0000832ArgTypeListI : TypeListI
833 | TypeListI ',' DOTDOTDOT {
834 ($$=$1)->push_back(Type::VoidTy);
835 }
836 | DOTDOTDOT {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000837 ($$ = new list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattner30c89792001-09-07 16:35:17 +0000838 }
839 | /*empty*/ {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000840 $$ = new list<PATypeHolder>();
Chris Lattner51727be2002-06-04 21:58:56 +0000841 };
Chris Lattner30c89792001-09-07 16:35:17 +0000842
Chris Lattnere98dda62001-07-14 06:10:16 +0000843// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +0000844// production is used ONLY to represent constants that show up AFTER a 'const',
845// 'constant' or 'global' token at global scope. Constants that can be inlined
846// into other expressions (such as integers and constexprs) are handled by the
847// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +0000848//
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000849ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
850 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
851 if (ATy == 0)
852 ThrowException("Cannot make array constant with type: '" +
853 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +0000854 const Type *ETy = ATy->getElementType();
855 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +0000856
Chris Lattner30c89792001-09-07 16:35:17 +0000857 // Verify that we have the correct size...
858 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +0000859 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattner30c89792001-09-07 16:35:17 +0000860 utostr($3->size()) + " arguments, but has size of " +
861 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +0000862
Chris Lattner30c89792001-09-07 16:35:17 +0000863 // Verify all elements are correct type!
864 for (unsigned i = 0; i < $3->size(); i++) {
865 if (ETy != (*$3)[i]->getType())
Chris Lattner00950542001-06-06 20:29:01 +0000866 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +0000867 ETy->getDescription() +"' as required!\nIt is of type '"+
868 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +0000869 }
870
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000871 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +0000872 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000873 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000874 | Types '[' ']' {
875 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
876 if (ATy == 0)
877 ThrowException("Cannot make array constant with type: '" +
878 (*$1)->getDescription() + "'!");
879
880 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +0000881 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +0000882 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattner30c89792001-09-07 16:35:17 +0000883 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000884 $$ = ConstantArray::get(ATy, vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +0000885 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +0000886 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000887 | Types 'c' STRINGCONSTANT {
888 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
889 if (ATy == 0)
890 ThrowException("Cannot make array constant with type: '" +
891 (*$1)->getDescription() + "'!");
892
Chris Lattner30c89792001-09-07 16:35:17 +0000893 int NumElements = ATy->getNumElements();
894 const Type *ETy = ATy->getElementType();
895 char *EndStr = UnEscapeLexed($3, true);
896 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +0000897 ThrowException("Can't build string constant of size " +
Chris Lattner30c89792001-09-07 16:35:17 +0000898 itostr((int)(EndStr-$3)) +
899 " when array has size " + itostr(NumElements) + "!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000900 vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +0000901 if (ETy == Type::SByteTy) {
902 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000903 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +0000904 } else if (ETy == Type::UByteTy) {
905 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000906 Vals.push_back(ConstantUInt::get(ETy, *C));
Chris Lattner93750fa2001-07-28 17:48:55 +0000907 } else {
Chris Lattner30c89792001-09-07 16:35:17 +0000908 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +0000909 ThrowException("Cannot build string arrays of non byte sized elements!");
910 }
Chris Lattner30c89792001-09-07 16:35:17 +0000911 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000912 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +0000913 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +0000914 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000915 | Types '{' ConstVector '}' {
916 const StructType *STy = dyn_cast<const StructType>($1->get());
917 if (STy == 0)
918 ThrowException("Cannot make struct constant with type: '" +
919 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +0000920 // FIXME: TODO: Check to see that the constants are compatible with the type
921 // initializer!
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000922 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +0000923 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000924 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000925 | Types NULL_TOK {
926 const PointerType *PTy = dyn_cast<const PointerType>($1->get());
927 if (PTy == 0)
928 ThrowException("Cannot make null pointer constant with type: '" +
929 (*$1)->getDescription() + "'!");
930
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000931 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000932 delete $1;
933 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000934 | Types SymbolicValueRef {
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000935 const PointerType *Ty = dyn_cast<const PointerType>($1->get());
936 if (Ty == 0)
937 ThrowException("Global const reference must be a pointer type!");
938
Chris Lattner3101c252002-08-15 17:58:33 +0000939 // ConstExprs can exist in the body of a function, thus creating
940 // ConstantPointerRefs whenever they refer to a variable. Because we are in
941 // the context of a function, getValNonImprovising will search the functions
942 // symbol table instead of the module symbol table for the global symbol,
943 // which throws things all off. To get around this, we just tell
944 // getValNonImprovising that we are at global scope here.
945 //
946 Function *SavedCurFn = CurMeth.CurrentFunction;
947 CurMeth.CurrentFunction = 0;
948
Chris Lattner2079fde2001-10-13 06:41:08 +0000949 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000950
Chris Lattner3101c252002-08-15 17:58:33 +0000951 CurMeth.CurrentFunction = SavedCurFn;
952
953
Chris Lattner2079fde2001-10-13 06:41:08 +0000954 // If this is an initializer for a constant pointer, which is referencing a
955 // (currently) undefined variable, create a stub now that shall be replaced
956 // in the future with the right type of variable.
957 //
958 if (V == 0) {
959 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
960 const PointerType *PT = cast<PointerType>(Ty);
961
962 // First check to see if the forward references value is already created!
963 PerModuleInfo::GlobalRefsType::iterator I =
964 CurModule.GlobalRefs.find(make_pair(PT, $2));
965
966 if (I != CurModule.GlobalRefs.end()) {
967 V = I->second; // Placeholder already exists, use it...
968 } else {
969 // TODO: Include line number info by creating a subclass of
970 // TODO: GlobalVariable here that includes the said information!
971
972 // Create a placeholder for the global variable reference...
Chris Lattner7a176752001-12-04 00:03:30 +0000973 GlobalVariable *GV = new GlobalVariable(PT->getElementType(),
974 false, true);
Chris Lattner2079fde2001-10-13 06:41:08 +0000975 // Keep track of the fact that we have a forward ref to recycle it
976 CurModule.GlobalRefs.insert(make_pair(make_pair(PT, $2), GV));
977
978 // Must temporarily push this value into the module table...
979 CurModule.CurrentModule->getGlobalList().push_back(GV);
980 V = GV;
981 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000982 }
983
Chris Lattner2079fde2001-10-13 06:41:08 +0000984 GlobalValue *GV = cast<GlobalValue>(V);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000985 $$ = ConstantPointerRef::get(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +0000986 delete $1; // Free the type handle
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000987 }
Chris Lattnerd78700d2002-08-16 21:14:40 +0000988 | Types ConstExpr {
989 if ($1->get() != $2->getType())
990 ThrowException("Mismatched types for constant expression!");
991 $$ = $2;
992 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000993 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000994
Chris Lattnerd05e3592002-08-15 18:17:28 +0000995ConstVal : SIntType EINT64VAL { // integral constants
996 if (!ConstantSInt::isValueValidForType($1, $2))
997 ThrowException("Constant value doesn't fit in type!");
998 $$ = ConstantSInt::get($1, $2);
999 }
1000 | UIntType EUINT64VAL { // integral constants
1001 if (!ConstantUInt::isValueValidForType($1, $2))
1002 ThrowException("Constant value doesn't fit in type!");
1003 $$ = ConstantUInt::get($1, $2);
1004 }
1005 | BOOL TRUE { // Boolean constants
1006 $$ = ConstantBool::True;
1007 }
1008 | BOOL FALSE { // Boolean constants
1009 $$ = ConstantBool::False;
1010 }
1011 | FPType FPVAL { // Float & Double constants
1012 $$ = ConstantFP::get($1, $2);
1013 };
1014
Chris Lattner00950542001-06-06 20:29:01 +00001015
Chris Lattnerd78700d2002-08-16 21:14:40 +00001016ConstExpr: CAST '(' ConstVal TO Types ')' {
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001017 $$ = ConstantExpr::getCast($3, $5->get());
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001018 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001019 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001020 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1021 if (!isa<PointerType>($3->getType()))
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001022 ThrowException("GetElementPtr requires a pointer operand!");
1023
1024 const Type *IdxTy =
Chris Lattnerd78700d2002-08-16 21:14:40 +00001025 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001026 if (!IdxTy)
1027 ThrowException("Index list invalid for constant getelementptr!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001028
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001029 vector<Constant*> IdxVec;
Chris Lattnerd78700d2002-08-16 21:14:40 +00001030 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1031 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001032 IdxVec.push_back(C);
1033 else
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001034 ThrowException("Indices to constant getelementptr must be constants!");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001035
Chris Lattnerd78700d2002-08-16 21:14:40 +00001036 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001037
Chris Lattnerd78700d2002-08-16 21:14:40 +00001038 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001039 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001040 | BinaryOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001041 if ($3->getType() != $5->getType())
1042 ThrowException("Binary operator types must match!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001043 $$ = ConstantExpr::get($1, $3, $5);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001044 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001045 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001046 if ($5->getType() != Type::UByteTy)
1047 ThrowException("Shift count for shift constant must be unsigned byte!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001048 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattner699f1eb2002-08-14 17:12:33 +00001049 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001050
1051
Chris Lattnere98dda62001-07-14 06:10:16 +00001052// ConstVector - A list of comma seperated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001053ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001054 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001055 }
1056 | ConstVal {
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001057 $$ = new vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001058 $$->push_back($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001059 };
Chris Lattner00950542001-06-06 20:29:01 +00001060
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001061
Chris Lattner1781aca2001-09-18 04:00:54 +00001062// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001063GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001064
Chris Lattner00950542001-06-06 20:29:01 +00001065
Chris Lattner0e73ce62002-05-02 19:11:13 +00001066//===----------------------------------------------------------------------===//
1067// Rules to match Modules
1068//===----------------------------------------------------------------------===//
1069
1070// Module rule: Capture the result of parsing the whole file into a result
1071// variable...
1072//
1073Module : FunctionList {
1074 $$ = ParserResult = $1;
1075 CurModule.ModuleDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001076};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001077
Chris Lattner7e708292002-06-25 16:13:24 +00001078// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001079//
1080FunctionList : FunctionList Function {
1081 $$ = $1;
1082 assert($2->getParent() == 0 && "Function already in module!");
1083 $1->getFunctionList().push_back($2);
1084 CurMeth.FunctionDone();
1085 }
1086 | FunctionList FunctionProto {
1087 $$ = $1;
1088 }
1089 | FunctionList IMPLEMENTATION {
1090 $$ = $1;
1091 }
1092 | ConstPool {
1093 $$ = CurModule.CurrentModule;
1094 // Resolve circular types before we parse the body of the module
1095 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001096 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001097
Chris Lattnere98dda62001-07-14 06:10:16 +00001098// ConstPool - Constants with optional names assigned to them.
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001099ConstPool : ConstPool OptAssign CONST ConstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001100 if (setValueName($4, $2)) { assert(0 && "No redefinitions allowed!"); }
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001101 InsertValue($4);
Chris Lattner00950542001-06-06 20:29:01 +00001102 }
Chris Lattner30c89792001-09-07 16:35:17 +00001103 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner4a42e902001-10-22 05:56:09 +00001104 // Eagerly resolve types. This is not an optimization, this is a
1105 // requirement that is due to the fact that we could have this:
1106 //
1107 // %list = type { %list * }
1108 // %list = type { %list * } ; repeated type decl
1109 //
1110 // If types are not resolved eagerly, then the two types will not be
1111 // determined to be the same type!
1112 //
1113 ResolveTypeTo($2, $4->get());
1114
Chris Lattner1781aca2001-09-18 04:00:54 +00001115 // TODO: FIXME when Type are not const
Chris Lattnerb7474512001-10-03 15:39:04 +00001116 if (!setValueName(const_cast<Type*>($4->get()), $2)) {
1117 // If this is not a redefinition of a type...
1118 if (!$2) {
1119 InsertType($4->get(),
Chris Lattner79df7c02002-03-26 18:01:55 +00001120 inFunctionScope() ? CurMeth.Types : CurModule.Types);
Chris Lattnerb7474512001-10-03 15:39:04 +00001121 }
Chris Lattner30c89792001-09-07 16:35:17 +00001122 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001123
1124 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001125 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001126 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001127 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001128 | ConstPool OptAssign OptInternal GlobalType ConstVal {
1129 const Type *Ty = $5->getType();
Chris Lattner1781aca2001-09-18 04:00:54 +00001130 // Global declarations appear in Constant Pool
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001131 Constant *Initializer = $5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001132 if (Initializer == 0)
1133 ThrowException("Global value initializer is not a constant!");
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001134
Chris Lattnerdda71962001-11-26 18:54:16 +00001135 GlobalVariable *GV = new GlobalVariable(Ty, $4, $3, Initializer);
Chris Lattnerb7474512001-10-03 15:39:04 +00001136 if (!setValueName(GV, $2)) { // If not redefining...
1137 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001138 int Slot = InsertValue(GV, CurModule.Values);
1139
1140 if (Slot != -1) {
1141 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1142 } else {
1143 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1144 (char*)GV->getName().c_str()));
1145 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001146 }
Chris Lattner1781aca2001-09-18 04:00:54 +00001147 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001148 | ConstPool OptAssign OptInternal UNINIT GlobalType Types {
1149 const Type *Ty = *$6;
Chris Lattner1781aca2001-09-18 04:00:54 +00001150 // Global declarations appear in Constant Pool
Chris Lattnerdda71962001-11-26 18:54:16 +00001151 GlobalVariable *GV = new GlobalVariable(Ty, $5, $3);
Chris Lattnerb7474512001-10-03 15:39:04 +00001152 if (!setValueName(GV, $2)) { // If not redefining...
1153 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001154 int Slot = InsertValue(GV, CurModule.Values);
1155
1156 if (Slot != -1) {
1157 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1158 } else {
1159 assert(GV->hasName() && "Not named and not numbered!?");
1160 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1161 (char*)GV->getName().c_str()));
1162 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001163 }
Chris Lattner09c07532002-03-31 07:16:49 +00001164 delete $6;
Chris Lattnere98dda62001-07-14 06:10:16 +00001165 }
Chris Lattner00950542001-06-06 20:29:01 +00001166 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001167 };
Chris Lattner00950542001-06-06 20:29:01 +00001168
1169
1170//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001171// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001172//===----------------------------------------------------------------------===//
1173
Chris Lattner51727be2002-06-04 21:58:56 +00001174OptVAR_ID : VAR_ID | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001175
1176ArgVal : Types OptVAR_ID {
Chris Lattner46748042002-04-09 19:41:42 +00001177 $$ = new pair<Argument*, char*>(new Argument(*$1), $2);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001178 delete $1; // Delete the type handle..
Chris Lattner51727be2002-06-04 21:58:56 +00001179};
Chris Lattner00950542001-06-06 20:29:01 +00001180
1181ArgListH : ArgVal ',' ArgListH {
1182 $$ = $3;
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001183 $3->push_front(*$1);
1184 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001185 }
1186 | ArgVal {
Chris Lattner46748042002-04-09 19:41:42 +00001187 $$ = new list<pair<Argument*,char*> >();
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001188 $$->push_front(*$1);
1189 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001190 }
Chris Lattner8b81bf52001-07-25 22:47:46 +00001191 | DOTDOTDOT {
Chris Lattner46748042002-04-09 19:41:42 +00001192 $$ = new list<pair<Argument*, char*> >();
1193 $$->push_front(pair<Argument*,char*>(new Argument(Type::VoidTy), 0));
Chris Lattner51727be2002-06-04 21:58:56 +00001194 };
Chris Lattner00950542001-06-06 20:29:01 +00001195
1196ArgList : ArgListH {
1197 $$ = $1;
1198 }
1199 | /* empty */ {
1200 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001201 };
Chris Lattner00950542001-06-06 20:29:01 +00001202
Chris Lattner8ebccb72002-05-22 22:33:00 +00001203FuncName : VAR_ID | STRINGCONSTANT;
1204
1205FunctionHeaderH : OptInternal TypesV FuncName '(' ArgList ')' {
Chris Lattnerdda71962001-11-26 18:54:16 +00001206 UnEscapeLexed($3);
Chris Lattner79df7c02002-03-26 18:01:55 +00001207 string FunctionName($3);
Chris Lattnerdda71962001-11-26 18:54:16 +00001208
Chris Lattner30c89792001-09-07 16:35:17 +00001209 vector<const Type*> ParamTypeList;
Chris Lattnerdda71962001-11-26 18:54:16 +00001210 if ($5)
Chris Lattner46748042002-04-09 19:41:42 +00001211 for (list<pair<Argument*,char*> >::iterator I = $5->begin();
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001212 I != $5->end(); ++I)
1213 ParamTypeList.push_back(I->first->getType());
Chris Lattner00950542001-06-06 20:29:01 +00001214
Chris Lattner2079fde2001-10-13 06:41:08 +00001215 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1216 if (isVarArg) ParamTypeList.pop_back();
1217
Chris Lattner79df7c02002-03-26 18:01:55 +00001218 const FunctionType *MT = FunctionType::get(*$2, ParamTypeList, isVarArg);
Chris Lattneref9c23f2001-10-03 14:53:21 +00001219 const PointerType *PMT = PointerType::get(MT);
Chris Lattnerdda71962001-11-26 18:54:16 +00001220 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001221
Chris Lattner79df7c02002-03-26 18:01:55 +00001222 Function *M = 0;
Chris Lattnere1815642001-07-15 06:35:53 +00001223 if (SymbolTable *ST = CurModule.CurrentModule->getSymbolTable()) {
Chris Lattner79df7c02002-03-26 18:01:55 +00001224 // Is the function already in symtab?
1225 if (Value *V = ST->lookup(PMT, FunctionName)) {
1226 M = cast<Function>(V);
Chris Lattner00950542001-06-06 20:29:01 +00001227
Chris Lattnere1815642001-07-15 06:35:53 +00001228 // Yes it is. If this is the case, either we need to be a forward decl,
1229 // or it needs to be.
1230 if (!CurMeth.isDeclare && !M->isExternal())
Chris Lattner7e708292002-06-25 16:13:24 +00001231 ThrowException("Redefinition of function '" + FunctionName + "'!");
Chris Lattner34538142002-03-08 19:11:42 +00001232
Chris Lattner5659dd12002-07-15 00:10:33 +00001233 // Make sure that we keep track of the internal marker, even if there was
1234 // a previous "declare".
1235 if ($1)
1236 M->setInternalLinkage(true);
1237
Chris Lattner7e708292002-06-25 16:13:24 +00001238 // If we found a preexisting function prototype, remove it from the
1239 // module, so that we don't get spurious conflicts with global & local
1240 // variables.
Chris Lattner34538142002-03-08 19:11:42 +00001241 //
Chris Lattner79df7c02002-03-26 18:01:55 +00001242 CurModule.CurrentModule->getFunctionList().remove(M);
Chris Lattnere1815642001-07-15 06:35:53 +00001243 }
1244 }
1245
1246 if (M == 0) { // Not already defined?
Chris Lattner79df7c02002-03-26 18:01:55 +00001247 M = new Function(MT, $1, FunctionName);
Chris Lattnere1815642001-07-15 06:35:53 +00001248 InsertValue(M, CurModule.Values);
Chris Lattnerdda71962001-11-26 18:54:16 +00001249 CurModule.DeclareNewGlobalValue(M, ValID::create($3));
Chris Lattnere1815642001-07-15 06:35:53 +00001250 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001251 free($3); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +00001252
Chris Lattner79df7c02002-03-26 18:01:55 +00001253 CurMeth.FunctionStart(M);
Chris Lattner00950542001-06-06 20:29:01 +00001254
Chris Lattner7e708292002-06-25 16:13:24 +00001255 // Add all of the arguments we parsed to the function...
Chris Lattnerdda71962001-11-26 18:54:16 +00001256 if ($5 && !CurMeth.isDeclare) { // Is null if empty...
Chris Lattner46748042002-04-09 19:41:42 +00001257 for (list<pair<Argument*, char*> >::iterator I = $5->begin();
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001258 I != $5->end(); ++I) {
1259 if (setValueName(I->first, I->second)) { // Insert into symtab...
1260 assert(0 && "No arg redef allowed!");
1261 }
1262
1263 InsertValue(I->first);
Chris Lattner7e708292002-06-25 16:13:24 +00001264 M->getArgumentList().push_back(I->first);
Chris Lattner00950542001-06-06 20:29:01 +00001265 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001266 delete $5; // We're now done with the argument list
Chris Lattner9176fe42002-03-08 18:57:56 +00001267 } else if ($5) {
1268 // If we are a declaration, we should free the memory for the argument list!
Chris Lattner46748042002-04-09 19:41:42 +00001269 for (list<pair<Argument*, char*> >::iterator I = $5->begin(), E = $5->end();
1270 I != E; ++I) {
Chris Lattner9176fe42002-03-08 18:57:56 +00001271 if (I->second) free(I->second); // Free the memory for the name...
Chris Lattner09c07532002-03-31 07:16:49 +00001272 delete I->first; // Free the unused function argument
1273 }
Chris Lattner9176fe42002-03-08 18:57:56 +00001274 delete $5; // Free the memory for the list itself
Chris Lattner00950542001-06-06 20:29:01 +00001275 }
Chris Lattner51727be2002-06-04 21:58:56 +00001276};
Chris Lattner00950542001-06-06 20:29:01 +00001277
Chris Lattner9b02cc32002-05-03 18:23:48 +00001278BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1279
1280FunctionHeader : FunctionHeaderH BEGIN {
Chris Lattner79df7c02002-03-26 18:01:55 +00001281 $$ = CurMeth.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001282
Chris Lattner7e708292002-06-25 16:13:24 +00001283 // Resolve circular types before we parse the body of the function.
Chris Lattner30c89792001-09-07 16:35:17 +00001284 ResolveTypes(CurMeth.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001285};
Chris Lattner00950542001-06-06 20:29:01 +00001286
Chris Lattner9b02cc32002-05-03 18:23:48 +00001287END : ENDTOK | '}'; // Allow end of '}' to end a function
1288
Chris Lattner79df7c02002-03-26 18:01:55 +00001289Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001290 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001291};
Chris Lattner00950542001-06-06 20:29:01 +00001292
Chris Lattner79df7c02002-03-26 18:01:55 +00001293FunctionProto : DECLARE { CurMeth.isDeclare = true; } FunctionHeaderH {
1294 $$ = CurMeth.CurrentFunction;
1295 assert($$->getParent() == 0 && "Function already in module!");
1296 CurModule.CurrentModule->getFunctionList().push_back($$);
1297 CurMeth.FunctionDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001298};
Chris Lattner00950542001-06-06 20:29:01 +00001299
1300//===----------------------------------------------------------------------===//
1301// Rules to match Basic Blocks
1302//===----------------------------------------------------------------------===//
1303
1304ConstValueRef : ESINT64VAL { // A reference to a direct constant
1305 $$ = ValID::create($1);
1306 }
1307 | EUINT64VAL {
1308 $$ = ValID::create($1);
1309 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001310 | FPVAL { // Perhaps it's an FP constant?
1311 $$ = ValID::create($1);
1312 }
Chris Lattner00950542001-06-06 20:29:01 +00001313 | TRUE {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001314 $$ = ValID::create(ConstantBool::True);
Chris Lattner00950542001-06-06 20:29:01 +00001315 }
1316 | FALSE {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001317 $$ = ValID::create(ConstantBool::False);
Chris Lattner00950542001-06-06 20:29:01 +00001318 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001319 | NULL_TOK {
1320 $$ = ValID::createNull();
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001321 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001322 | ConstExpr {
1323 $$ = ValID::create($1);
1324 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00001325
Chris Lattner2079fde2001-10-13 06:41:08 +00001326// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1327// another value.
1328//
1329SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001330 $$ = ValID::create($1);
1331 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001332 | VAR_ID { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001333 $$ = ValID::create($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001334 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001335
1336// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00001337ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00001338
Chris Lattner00950542001-06-06 20:29:01 +00001339
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001340// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1341// type immediately preceeds the value reference, and allows complex constant
1342// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001343ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001344 $$ = getVal(*$1, $2); delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001345 };
Chris Lattner8b81bf52001-07-25 22:47:46 +00001346
Chris Lattner00950542001-06-06 20:29:01 +00001347BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner7e708292002-06-25 16:13:24 +00001348 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner00950542001-06-06 20:29:01 +00001349 }
Chris Lattner7e708292002-06-25 16:13:24 +00001350 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
1351 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner51727be2002-06-04 21:58:56 +00001352 };
Chris Lattner00950542001-06-06 20:29:01 +00001353
1354
1355// Basic blocks are terminated by branching instructions:
1356// br, br/cc, switch, ret
1357//
Chris Lattner2079fde2001-10-13 06:41:08 +00001358BasicBlock : InstructionList OptAssign BBTerminatorInst {
1359 if (setValueName($3, $2)) { assert(0 && "No redefn allowed!"); }
1360 InsertValue($3);
1361
1362 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001363 InsertValue($1);
1364 $$ = $1;
1365 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001366 | LABELSTR InstructionList OptAssign BBTerminatorInst {
1367 if (setValueName($4, $3)) { assert(0 && "No redefn allowed!"); }
1368 InsertValue($4);
1369
1370 $2->getInstList().push_back($4);
Chris Lattnerb7474512001-10-03 15:39:04 +00001371 if (setValueName($2, $1)) { assert(0 && "No label redef allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001372
1373 InsertValue($2);
1374 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001375 };
Chris Lattner00950542001-06-06 20:29:01 +00001376
1377InstructionList : InstructionList Inst {
1378 $1->getInstList().push_back($2);
1379 $$ = $1;
1380 }
1381 | /* empty */ {
Chris Lattner0383cc42002-08-21 23:51:21 +00001382 $$ = CurBB = new BasicBlock();
Chris Lattner51727be2002-06-04 21:58:56 +00001383 };
Chris Lattner00950542001-06-06 20:29:01 +00001384
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001385BBTerminatorInst : RET ResolvedVal { // Return with a result...
1386 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001387 }
1388 | RET VOID { // Return with no result...
1389 $$ = new ReturnInst();
1390 }
1391 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner9636a912001-10-01 16:18:37 +00001392 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $3)));
Chris Lattner00950542001-06-06 20:29:01 +00001393 } // Conditional Branch...
1394 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner9636a912001-10-01 16:18:37 +00001395 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $6)),
1396 cast<BasicBlock>(getVal(Type::LabelTy, $9)),
Chris Lattner00950542001-06-06 20:29:01 +00001397 getVal(Type::BoolTy, $3));
1398 }
1399 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1400 SwitchInst *S = new SwitchInst(getVal($2, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001401 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
Chris Lattner00950542001-06-06 20:29:01 +00001402 $$ = S;
1403
Chris Lattner46748042002-04-09 19:41:42 +00001404 vector<pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
1405 E = $8->end();
1406 for (; I != E; ++I)
Chris Lattner00950542001-06-06 20:29:01 +00001407 S->dest_push_back(I->first, I->second);
1408 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001409 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO ResolvedVal
1410 EXCEPT ResolvedVal {
1411 const PointerType *PMTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001412 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00001413
1414 if (!(PMTy = dyn_cast<PointerType>($2->get())) ||
Chris Lattner79df7c02002-03-26 18:01:55 +00001415 !(Ty = dyn_cast<FunctionType>(PMTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00001416 // Pull out the types of all of the arguments...
1417 vector<const Type*> ParamTypes;
1418 if ($5) {
Chris Lattner6cdb0112001-11-26 16:54:11 +00001419 for (vector<Value*>::iterator I = $5->begin(), E = $5->end(); I!=E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00001420 ParamTypes.push_back((*I)->getType());
1421 }
1422
1423 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1424 if (isVarArg) ParamTypes.pop_back();
1425
Chris Lattner79df7c02002-03-26 18:01:55 +00001426 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattner2079fde2001-10-13 06:41:08 +00001427 PMTy = PointerType::get(Ty);
1428 }
1429 delete $2;
1430
Chris Lattner7e708292002-06-25 16:13:24 +00001431 Value *V = getVal(PMTy, $3); // Get the function we're calling...
Chris Lattner2079fde2001-10-13 06:41:08 +00001432
1433 BasicBlock *Normal = dyn_cast<BasicBlock>($8);
1434 BasicBlock *Except = dyn_cast<BasicBlock>($10);
1435
1436 if (Normal == 0 || Except == 0)
1437 ThrowException("Invoke instruction without label destinations!");
1438
1439 // Create the call node...
1440 if (!$5) { // Has no arguments?
Chris Lattner386a3b72001-10-16 19:54:17 +00001441 $$ = new InvokeInst(V, Normal, Except, vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001442 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001443 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00001444 // correctly!
1445 //
Chris Lattner79df7c02002-03-26 18:01:55 +00001446 FunctionType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1447 FunctionType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
Chris Lattner6cdb0112001-11-26 16:54:11 +00001448 vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00001449
1450 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1451 if ((*ArgI)->getType() != *I)
1452 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001453 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001454
1455 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1456 ThrowException("Invalid number of parameters detected!");
1457
Chris Lattner6cdb0112001-11-26 16:54:11 +00001458 $$ = new InvokeInst(V, Normal, Except, *$5);
Chris Lattner2079fde2001-10-13 06:41:08 +00001459 }
1460 delete $5;
Chris Lattner51727be2002-06-04 21:58:56 +00001461 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001462
1463
Chris Lattner00950542001-06-06 20:29:01 +00001464
1465JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1466 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001467 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001468 if (V == 0)
1469 ThrowException("May only switch on a constant pool value!");
1470
Chris Lattner9636a912001-10-01 16:18:37 +00001471 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($5, $6))));
Chris Lattner00950542001-06-06 20:29:01 +00001472 }
1473 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner46748042002-04-09 19:41:42 +00001474 $$ = new vector<pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001475 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00001476
1477 if (V == 0)
1478 ThrowException("May only switch on a constant pool value!");
1479
Chris Lattner9636a912001-10-01 16:18:37 +00001480 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($4, $5))));
Chris Lattner51727be2002-06-04 21:58:56 +00001481 };
Chris Lattner00950542001-06-06 20:29:01 +00001482
1483Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001484 // Is this definition named?? if so, assign the name...
1485 if (setValueName($2, $1)) { assert(0 && "No redefin allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001486 InsertValue($2);
1487 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001488};
Chris Lattner00950542001-06-06 20:29:01 +00001489
Chris Lattnerc24d2082001-06-11 15:04:20 +00001490PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
1491 $$ = new list<pair<Value*, BasicBlock*> >();
Chris Lattner30c89792001-09-07 16:35:17 +00001492 $$->push_back(make_pair(getVal(*$1, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001493 cast<BasicBlock>(getVal(Type::LabelTy, $5))));
Chris Lattner30c89792001-09-07 16:35:17 +00001494 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001495 }
1496 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1497 $$ = $1;
1498 $1->push_back(make_pair(getVal($1->front().first->getType(), $4),
Chris Lattner9636a912001-10-01 16:18:37 +00001499 cast<BasicBlock>(getVal(Type::LabelTy, $6))));
Chris Lattner51727be2002-06-04 21:58:56 +00001500 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00001501
1502
Chris Lattner30c89792001-09-07 16:35:17 +00001503ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner6cdb0112001-11-26 16:54:11 +00001504 $$ = new vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001505 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001506 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001507 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001508 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001509 $1->push_back($3);
Chris Lattner51727be2002-06-04 21:58:56 +00001510 };
Chris Lattner00950542001-06-06 20:29:01 +00001511
1512// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner51727be2002-06-04 21:58:56 +00001513ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001514
Chris Lattner4a6482b2002-09-10 19:57:26 +00001515InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
1516 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint())
1517 ThrowException("Arithmetic operator requires integer or FP operands!");
1518 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1519 if ($$ == 0)
1520 ThrowException("binary operator returned null!");
1521 delete $2;
1522 }
1523 | LogicalOps Types ValueRef ',' ValueRef {
1524 if (!(*$2)->isIntegral())
1525 ThrowException("Logical operator requires integral operands!");
1526 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1527 if ($$ == 0)
1528 ThrowException("binary operator returned null!");
1529 delete $2;
1530 }
1531 | SetCondOps Types ValueRef ',' ValueRef {
Chris Lattner1cff96a2002-09-10 22:37:46 +00001532 $$ = new SetCondInst($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001533 if ($$ == 0)
1534 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001535 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001536 }
Chris Lattner699f1eb2002-08-14 17:12:33 +00001537 | NOT ResolvedVal {
1538 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
1539 << " Replacing with 'xor'.\n";
1540
1541 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
1542 if (Ones == 0)
1543 ThrowException("Expected integral type for not instruction!");
1544
1545 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner00950542001-06-06 20:29:01 +00001546 if ($$ == 0)
Chris Lattner699f1eb2002-08-14 17:12:33 +00001547 ThrowException("Could not create a xor instruction!");
Chris Lattner09083092001-07-08 04:57:15 +00001548 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001549 | ShiftOps ResolvedVal ',' ResolvedVal {
1550 if ($4->getType() != Type::UByteTy)
1551 ThrowException("Shift amount must be ubyte!");
1552 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001553 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001554 | CAST ResolvedVal TO Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001555 $$ = new CastInst($2, *$4);
1556 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001557 }
Chris Lattnerc24d2082001-06-11 15:04:20 +00001558 | PHI PHIList {
1559 const Type *Ty = $2->front().first->getType();
1560 $$ = new PHINode(Ty);
Chris Lattner00950542001-06-06 20:29:01 +00001561 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001562 if ($2->front().first->getType() != Ty)
1563 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001564 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001565 $2->pop_front();
1566 }
1567 delete $2; // Free the list...
1568 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001569 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattneref9c23f2001-10-03 14:53:21 +00001570 const PointerType *PMTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001571 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001572
Chris Lattneref9c23f2001-10-03 14:53:21 +00001573 if (!(PMTy = dyn_cast<PointerType>($2->get())) ||
Chris Lattner79df7c02002-03-26 18:01:55 +00001574 !(Ty = dyn_cast<FunctionType>(PMTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001575 // Pull out the types of all of the arguments...
1576 vector<const Type*> ParamTypes;
Chris Lattneref9c23f2001-10-03 14:53:21 +00001577 if ($5) {
Chris Lattner6cdb0112001-11-26 16:54:11 +00001578 for (vector<Value*>::iterator I = $5->begin(), E = $5->end(); I!=E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00001579 ParamTypes.push_back((*I)->getType());
1580 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001581
1582 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1583 if (isVarArg) ParamTypes.pop_back();
1584
Chris Lattner79df7c02002-03-26 18:01:55 +00001585 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattneref9c23f2001-10-03 14:53:21 +00001586 PMTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001587 }
Chris Lattner30c89792001-09-07 16:35:17 +00001588 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001589
Chris Lattner7e708292002-06-25 16:13:24 +00001590 Value *V = getVal(PMTy, $3); // Get the function we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001591
Chris Lattner8b81bf52001-07-25 22:47:46 +00001592 // Create the call node...
1593 if (!$5) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00001594 // Make sure no arguments is a good thing!
1595 if (Ty->getNumParams() != 0)
1596 ThrowException("No arguments passed to a function that "
1597 "expects arguments!");
1598
Chris Lattner386a3b72001-10-16 19:54:17 +00001599 $$ = new CallInst(V, vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001600 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001601 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00001602 // correctly!
1603 //
Chris Lattner79df7c02002-03-26 18:01:55 +00001604 FunctionType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1605 FunctionType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
Chris Lattner6cdb0112001-11-26 16:54:11 +00001606 vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001607
1608 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1609 if ((*ArgI)->getType() != *I)
1610 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001611 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001612
Chris Lattner8b81bf52001-07-25 22:47:46 +00001613 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner00950542001-06-06 20:29:01 +00001614 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00001615
Chris Lattner6cdb0112001-11-26 16:54:11 +00001616 $$ = new CallInst(V, *$5);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001617 }
1618 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00001619 }
1620 | MemoryInst {
1621 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001622 };
Chris Lattner00950542001-06-06 20:29:01 +00001623
Chris Lattner6cdb0112001-11-26 16:54:11 +00001624
1625// IndexList - List of indices for GEP based instructions...
1626IndexList : ',' ValueRefList {
Chris Lattner027dcc52001-07-08 21:10:27 +00001627 $$ = $2;
1628} | /* empty */ {
Chris Lattner6cdb0112001-11-26 16:54:11 +00001629 $$ = new vector<Value*>();
Chris Lattner51727be2002-06-04 21:58:56 +00001630};
Chris Lattner027dcc52001-07-08 21:10:27 +00001631
Chris Lattner00950542001-06-06 20:29:01 +00001632MemoryInst : MALLOC Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001633 $$ = new MallocInst(PointerType::get(*$2));
1634 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001635 }
1636 | MALLOC Types ',' UINT ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001637 const Type *Ty = PointerType::get(*$2);
Chris Lattner8896eda2001-07-09 19:38:36 +00001638 $$ = new MallocInst(Ty, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001639 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001640 }
1641 | ALLOCA Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001642 $$ = new AllocaInst(PointerType::get(*$2));
1643 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001644 }
1645 | ALLOCA Types ',' UINT ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001646 const Type *Ty = PointerType::get(*$2);
Chris Lattner00950542001-06-06 20:29:01 +00001647 Value *ArrSize = getVal($4, $5);
Chris Lattnerf0d0e9c2001-07-07 08:36:30 +00001648 $$ = new AllocaInst(Ty, ArrSize);
Chris Lattner30c89792001-09-07 16:35:17 +00001649 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001650 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001651 | FREE ResolvedVal {
Chris Lattner9b625032002-05-06 16:15:30 +00001652 if (!isa<PointerType>($2->getType()))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001653 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00001654 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001655 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001656 }
1657
Chris Lattner6cdb0112001-11-26 16:54:11 +00001658 | LOAD Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00001659 if (!isa<PointerType>($2->get()))
Chris Lattner2079fde2001-10-13 06:41:08 +00001660 ThrowException("Can't load from nonpointer type: " +
1661 (*$2)->getDescription());
Chris Lattner5dfe7672002-08-22 22:48:55 +00001662 if (GetElementPtrInst::getIndexedType(*$2, *$4) == 0)
Chris Lattner027dcc52001-07-08 21:10:27 +00001663 ThrowException("Invalid indices for load instruction!");
1664
Chris Lattner0383cc42002-08-21 23:51:21 +00001665 Value *Src = getVal(*$2, $3);
1666 if (!$4->empty()) {
1667 std::cerr << "WARNING: Use of index load instruction:"
1668 << " replacing with getelementptr/load pair.\n";
1669 // Create a getelementptr hack instruction to do the right thing for
1670 // compatibility.
1671 //
1672 Instruction *I = new GetElementPtrInst(Src, *$4);
1673 CurBB->getInstList().push_back(I);
1674 Src = I;
1675 }
1676
1677 $$ = new LoadInst(Src);
Chris Lattner027dcc52001-07-08 21:10:27 +00001678 delete $4; // Free the vector...
Chris Lattner30c89792001-09-07 16:35:17 +00001679 delete $2;
Chris Lattner027dcc52001-07-08 21:10:27 +00001680 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00001681 | STORE ResolvedVal ',' Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00001682 if (!isa<PointerType>($4->get()))
Chris Lattner72e00252001-12-14 16:28:42 +00001683 ThrowException("Can't store to a nonpointer type: " +
1684 (*$4)->getDescription());
Chris Lattner5dfe7672002-08-22 22:48:55 +00001685 const Type *ElTy = GetElementPtrInst::getIndexedType(*$4, *$6);
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001686 if (ElTy == 0)
1687 ThrowException("Can't store into that field list!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001688 if (ElTy != $2->getType())
Chris Lattner72e00252001-12-14 16:28:42 +00001689 ThrowException("Can't store '" + $2->getType()->getDescription() +
1690 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner0383cc42002-08-21 23:51:21 +00001691
1692 Value *Ptr = getVal(*$4, $5);
1693 if (!$6->empty()) {
1694 std::cerr << "WARNING: Use of index store instruction:"
1695 << " replacing with getelementptr/store pair.\n";
1696 // Create a getelementptr hack instruction to do the right thing for
1697 // compatibility.
1698 //
1699 Instruction *I = new GetElementPtrInst(Ptr, *$6);
1700 CurBB->getInstList().push_back(I);
1701 Ptr = I;
1702 }
1703
1704 $$ = new StoreInst($2, Ptr);
Chris Lattner30c89792001-09-07 16:35:17 +00001705 delete $4; delete $6;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001706 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00001707 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner0235fe22002-09-11 01:17:27 +00001708 for (unsigned i = 0, e = $4->size(); i != e; ++i) {
1709 if ((*$4)[i]->getType() == Type::UIntTy) {
1710 std::cerr << "WARNING: Use of uint type indexes to getelementptr "
1711 << "instruction: replacing with casts to long type.\n";
1712 Instruction *I = new CastInst((*$4)[i], Type::LongTy);
1713 CurBB->getInstList().push_back(I);
1714 (*$4)[i] = I;
1715 }
1716 }
1717
Chris Lattner51727be2002-06-04 21:58:56 +00001718 if (!isa<PointerType>($2->get()))
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001719 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner30c89792001-09-07 16:35:17 +00001720 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner72e00252001-12-14 16:28:42 +00001721 ThrowException("Can't get element ptr '" + (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001722 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
1723 delete $2; delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00001724 };
Chris Lattner027dcc52001-07-08 21:10:27 +00001725
Chris Lattner00950542001-06-06 20:29:01 +00001726%%
Chris Lattner09083092001-07-08 04:57:15 +00001727int yyerror(const char *ErrorMsg) {
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001728 string where = string((CurFilename == "-")? string("<stdin>") : CurFilename)
1729 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
1730 string errMsg = string(ErrorMsg) + string("\n") + where + " while reading ";
1731 if (yychar == YYEMPTY)
1732 errMsg += "end-of-file.";
1733 else
1734 errMsg += "token: '" + string(llvmAsmtext, llvmAsmleng) + "'";
1735 ThrowException(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00001736 return 0;
1737}