blob: 828c41a97a0372393d70a3729e3e5890805c628d [file] [log] [blame]
Chris Lattner00950542001-06-06 20:29:01 +00001//===-- llvmAsmParser.y - Parser for llvm assembly files ---------*- C++ -*--=//
2//
3// This file implements the bison parser for LLVM assembly languages files.
4//
5//===------------------------------------------------------------------------=//
6
Chris Lattner00950542001-06-06 20:29:01 +00007%{
8#include "ParserInternals.h"
Chris Lattner00950542001-06-06 20:29:01 +00009#include "llvm/SymbolTable.h"
10#include "llvm/Module.h"
Chris Lattner00950542001-06-06 20:29:01 +000011#include "llvm/iTerminators.h"
12#include "llvm/iMemory.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000013#include "llvm/iPHINode.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000014#include "Support/STLExtras.h"
15#include "Support/DepthFirstIterator.h"
Chris Lattner00950542001-06-06 20:29:01 +000016#include <list>
Chris Lattnerc188eeb2002-07-30 18:54:25 +000017#include <utility>
Chris Lattner30c89792001-09-07 16:35:17 +000018#include <algorithm>
Chris Lattner697954c2002-01-20 22:54:45 +000019using std::list;
20using std::vector;
21using std::pair;
22using std::map;
23using std::pair;
24using std::make_pair;
25using std::cerr;
26using 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
40#define UR_OUT(X) cerr << X
41#else
42#define UR_OUT(X)
43#endif
44
Vikram S. Adved3f7eb02002-07-14 22:59:28 +000045#define YYERROR_VERBOSE 1
46
Chris Lattner7e708292002-06-25 16:13:24 +000047// This contains info used when building the body of a function. It is
48// destroyed when the function is completed.
Chris Lattner00950542001-06-06 20:29:01 +000049//
50typedef vector<Value *> ValueList; // Numbered defs
Chris Lattner386a3b72001-10-16 19:54:17 +000051static void ResolveDefinitions(vector<ValueList> &LateResolvers,
52 vector<ValueList> *FutureLateResolvers = 0);
Chris Lattner00950542001-06-06 20:29:01 +000053
54static struct PerModuleInfo {
55 Module *CurrentModule;
Chris Lattner30c89792001-09-07 16:35:17 +000056 vector<ValueList> Values; // Module level numbered definitions
57 vector<ValueList> LateResolveValues;
Chris Lattner8b88b3b2002-04-04 19:23:55 +000058 vector<PATypeHolder> Types;
59 map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner00950542001-06-06 20:29:01 +000060
Chris Lattner2079fde2001-10-13 06:41:08 +000061 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
62 // references to global values. Global values may be referenced before they
63 // are defined, and if so, the temporary object that they represent is held
Chris Lattnere9bb2df2001-12-03 22:26:30 +000064 // here. This is used for forward references of ConstantPointerRefs.
Chris Lattner2079fde2001-10-13 06:41:08 +000065 //
66 typedef map<pair<const PointerType *, ValID>, GlobalVariable*> GlobalRefsType;
67 GlobalRefsType GlobalRefs;
68
Chris Lattner00950542001-06-06 20:29:01 +000069 void ModuleDone() {
Chris Lattner7e708292002-06-25 16:13:24 +000070 // If we could not resolve some functions at function compilation time
71 // (calls to functions before they are defined), resolve them now... Types
72 // are resolved when the constant pool has been completely parsed.
Chris Lattner30c89792001-09-07 16:35:17 +000073 //
Chris Lattner00950542001-06-06 20:29:01 +000074 ResolveDefinitions(LateResolveValues);
75
Chris Lattner2079fde2001-10-13 06:41:08 +000076 // Check to make sure that all global value forward references have been
77 // resolved!
78 //
79 if (!GlobalRefs.empty()) {
Chris Lattner749ce032002-03-11 22:12:39 +000080 string UndefinedReferences = "Unresolved global references exist:\n";
81
82 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
83 I != E; ++I) {
84 UndefinedReferences += " " + I->first.first->getDescription() + " " +
85 I->first.second.getName() + "\n";
86 }
87 ThrowException(UndefinedReferences);
Chris Lattner2079fde2001-10-13 06:41:08 +000088 }
89
Chris Lattner7e708292002-06-25 16:13:24 +000090 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +000091 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +000092 CurrentModule = 0;
93 }
Chris Lattner2079fde2001-10-13 06:41:08 +000094
95
Vikram S. Adved3f7eb02002-07-14 22:59:28 +000096 // DeclareNewGlobalValue - Called every time a new GV has been defined. This
Chris Lattner2079fde2001-10-13 06:41:08 +000097 // is used to remove things from the forward declaration map, resolving them
98 // to the correct thing as needed.
99 //
100 void DeclareNewGlobalValue(GlobalValue *GV, ValID D) {
101 // Check to see if there is a forward reference to this global variable...
102 // if there is, eliminate it and patch the reference to use the new def'n.
103 GlobalRefsType::iterator I = GlobalRefs.find(make_pair(GV->getType(), D));
104
105 if (I != GlobalRefs.end()) {
106 GlobalVariable *OldGV = I->second; // Get the placeholder...
107 I->first.second.destroy(); // Free string memory if neccesary
108
109 // Loop over all of the uses of the GlobalValue. The only thing they are
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000110 // allowed to be is ConstantPointerRef's.
Chris Lattner2079fde2001-10-13 06:41:08 +0000111 assert(OldGV->use_size() == 1 && "Only one reference should exist!");
112 while (!OldGV->use_empty()) {
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000113 User *U = OldGV->use_back(); // Must be a ConstantPointerRef...
114 ConstantPointerRef *CPR = cast<ConstantPointerRef>(U);
115 assert(CPR->getValue() == OldGV && "Something isn't happy");
116
117 // Change the const pool reference to point to the real global variable
118 // now. This should drop a use from the OldGV.
119 CPR->mutateReferences(OldGV, GV);
Chris Lattner2079fde2001-10-13 06:41:08 +0000120 }
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000121
122 // Remove OldGV from the module...
Chris Lattner2079fde2001-10-13 06:41:08 +0000123 CurrentModule->getGlobalList().remove(OldGV);
124 delete OldGV; // Delete the old placeholder
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000125
Chris Lattner2079fde2001-10-13 06:41:08 +0000126 // Remove the map entry for the global now that it has been created...
127 GlobalRefs.erase(I);
128 }
129 }
130
Chris Lattner00950542001-06-06 20:29:01 +0000131} CurModule;
132
Chris Lattner79df7c02002-03-26 18:01:55 +0000133static struct PerFunctionInfo {
Chris Lattner7e708292002-06-25 16:13:24 +0000134 Function *CurrentFunction; // Pointer to current function being created
Chris Lattner00950542001-06-06 20:29:01 +0000135
Chris Lattnere1815642001-07-15 06:35:53 +0000136 vector<ValueList> Values; // Keep track of numbered definitions
Chris Lattner00950542001-06-06 20:29:01 +0000137 vector<ValueList> LateResolveValues;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000138 vector<PATypeHolder> Types;
139 map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner7e708292002-06-25 16:13:24 +0000140 bool isDeclare; // Is this function a forward declararation?
Chris Lattner00950542001-06-06 20:29:01 +0000141
Chris Lattner79df7c02002-03-26 18:01:55 +0000142 inline PerFunctionInfo() {
143 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000144 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000145 }
146
Chris Lattner79df7c02002-03-26 18:01:55 +0000147 inline ~PerFunctionInfo() {}
Chris Lattner00950542001-06-06 20:29:01 +0000148
Chris Lattner79df7c02002-03-26 18:01:55 +0000149 inline void FunctionStart(Function *M) {
150 CurrentFunction = M;
Chris Lattner00950542001-06-06 20:29:01 +0000151 }
152
Chris Lattner79df7c02002-03-26 18:01:55 +0000153 void FunctionDone() {
Chris Lattner00950542001-06-06 20:29:01 +0000154 // If we could not resolve some blocks at parsing time (forward branches)
155 // resolve the branches now...
Chris Lattner386a3b72001-10-16 19:54:17 +0000156 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000157
Chris Lattner7e708292002-06-25 16:13:24 +0000158 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +0000159 Types.clear();
Chris Lattner79df7c02002-03-26 18:01:55 +0000160 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000161 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000162 }
Chris Lattner7e708292002-06-25 16:13:24 +0000163} CurMeth; // Info for the current function...
Chris Lattner00950542001-06-06 20:29:01 +0000164
Chris Lattner79df7c02002-03-26 18:01:55 +0000165static bool inFunctionScope() { return CurMeth.CurrentFunction != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000166
Chris Lattner00950542001-06-06 20:29:01 +0000167
168//===----------------------------------------------------------------------===//
169// Code to handle definitions of all the types
170//===----------------------------------------------------------------------===//
171
Chris Lattner2079fde2001-10-13 06:41:08 +0000172static int InsertValue(Value *D, vector<ValueList> &ValueTab = CurMeth.Values) {
173 if (D->hasName()) return -1; // Is this a numbered definition?
174
175 // Yes, insert the value into the value table...
176 unsigned type = D->getType()->getUniqueID();
177 if (ValueTab.size() <= type)
178 ValueTab.resize(type+1, ValueList());
179 //printf("Values[%d][%d] = %d\n", type, ValueTab[type].size(), D);
180 ValueTab[type].push_back(D);
181 return ValueTab[type].size()-1;
Chris Lattner00950542001-06-06 20:29:01 +0000182}
183
Chris Lattner30c89792001-09-07 16:35:17 +0000184// TODO: FIXME when Type are not const
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000185static void InsertType(const Type *Ty, vector<PATypeHolder> &Types) {
Chris Lattner30c89792001-09-07 16:35:17 +0000186 Types.push_back(Ty);
187}
188
189static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000190 switch (D.Type) {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000191 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000192 unsigned Num = (unsigned)D.Num;
193
194 // Module constants occupy the lowest numbered slots...
195 if (Num < CurModule.Types.size())
196 return CurModule.Types[Num];
197
198 Num -= CurModule.Types.size();
199
200 // Check that the number is within bounds...
201 if (Num <= CurMeth.Types.size())
202 return CurMeth.Types[Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000203 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000204 }
Chris Lattnerf8dff732002-07-18 05:18:37 +0000205 case ValID::NameVal: { // Is it a named definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000206 string Name(D.Name);
207 SymbolTable *SymTab = 0;
Chris Lattner79df7c02002-03-26 18:01:55 +0000208 if (inFunctionScope()) SymTab = CurMeth.CurrentFunction->getSymbolTable();
Chris Lattner30c89792001-09-07 16:35:17 +0000209 Value *N = SymTab ? SymTab->lookup(Type::TypeTy, Name) : 0;
210
211 if (N == 0) {
Chris Lattner7e708292002-06-25 16:13:24 +0000212 // Symbol table doesn't automatically chain yet... because the function
Chris Lattner30c89792001-09-07 16:35:17 +0000213 // hasn't been added to the module...
214 //
215 SymTab = CurModule.CurrentModule->getSymbolTable();
216 if (SymTab)
217 N = SymTab->lookup(Type::TypeTy, Name);
218 if (N == 0) break;
219 }
220
221 D.destroy(); // Free old strdup'd memory...
Chris Lattnercfe26c92001-10-01 18:26:53 +0000222 return cast<const Type>(N);
Chris Lattner30c89792001-09-07 16:35:17 +0000223 }
224 default:
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000225 ThrowException("Internal parser error: Invalid symbol type reference!");
Chris Lattner30c89792001-09-07 16:35:17 +0000226 }
227
228 // If we reached here, we referenced either a symbol that we don't know about
229 // or an id number that hasn't been read yet. We may be referencing something
230 // forward, so just create an entry to be resolved later and get to it...
231 //
232 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
233
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000234 map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner4a42e902001-10-22 05:56:09 +0000235 CurMeth.LateResolveTypes : CurModule.LateResolveTypes;
236
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000237 map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000238 if (I != LateResolver.end()) {
239 return I->second;
240 }
Chris Lattner30c89792001-09-07 16:35:17 +0000241
Chris Lattner82269592001-10-22 06:01:08 +0000242 Type *Typ = OpaqueType::get();
Chris Lattner4a42e902001-10-22 05:56:09 +0000243 LateResolver.insert(make_pair(D, Typ));
Chris Lattner30c89792001-09-07 16:35:17 +0000244 return Typ;
245}
246
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000247static Value *lookupInSymbolTable(const Type *Ty, const string &Name) {
248 SymbolTable *SymTab =
Chris Lattner9705a152002-05-02 19:27:42 +0000249 inFunctionScope() ? CurMeth.CurrentFunction->getSymbolTable() :
250 CurModule.CurrentModule->getSymbolTable();
Chris Lattner924025e2002-04-29 18:25:33 +0000251 return SymTab ? SymTab->lookup(Ty, Name) : 0;
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000252}
253
Chris Lattner2079fde2001-10-13 06:41:08 +0000254// getValNonImprovising - Look up the value specified by the provided type and
255// the provided ValID. If the value exists and has already been defined, return
256// it. Otherwise return null.
257//
258static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000259 if (isa<FunctionType>(Ty))
260 ThrowException("Functions are not values and "
261 "must be referenced as pointers");
Chris Lattner386a3b72001-10-16 19:54:17 +0000262
Chris Lattner30c89792001-09-07 16:35:17 +0000263 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000264 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000265 unsigned type = Ty->getUniqueID();
Chris Lattner00950542001-06-06 20:29:01 +0000266 unsigned Num = (unsigned)D.Num;
267
268 // Module constants occupy the lowest numbered slots...
269 if (type < CurModule.Values.size()) {
270 if (Num < CurModule.Values[type].size())
271 return CurModule.Values[type][Num];
272
273 Num -= CurModule.Values[type].size();
274 }
275
276 // Make sure that our type is within bounds
Chris Lattner2079fde2001-10-13 06:41:08 +0000277 if (CurMeth.Values.size() <= type) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000278
279 // Check that the number is within bounds...
Chris Lattner2079fde2001-10-13 06:41:08 +0000280 if (CurMeth.Values[type].size() <= Num) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000281
282 return CurMeth.Values[type][Num];
283 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000284
Chris Lattner1a1cb112001-09-30 22:46:54 +0000285 case ValID::NameVal: { // Is it a named definition?
Chris Lattner2079fde2001-10-13 06:41:08 +0000286 Value *N = lookupInSymbolTable(Ty, string(D.Name));
287 if (N == 0) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000288
289 D.destroy(); // Free old strdup'd memory...
290 return N;
291 }
292
Chris Lattner2079fde2001-10-13 06:41:08 +0000293 // Check to make sure that "Ty" is an integral type, and that our
294 // value will fit into the specified type...
295 case ValID::ConstSIntVal: // Is it a constant pool reference??
296 if (Ty == Type::BoolTy) { // Special handling for boolean data
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000297 return ConstantBool::get(D.ConstPool64 != 0);
Chris Lattner2079fde2001-10-13 06:41:08 +0000298 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000299 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
Chris Lattnerf8dff732002-07-18 05:18:37 +0000300 ThrowException("Signed integral constant '" +
Chris Lattner2079fde2001-10-13 06:41:08 +0000301 itostr(D.ConstPool64) + "' is invalid for type '" +
Chris Lattner72e00252001-12-14 16:28:42 +0000302 Ty->getDescription() + "'!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000303 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner00950542001-06-06 20:29:01 +0000304 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000305
306 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000307 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
308 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000309 ThrowException("Integral constant '" + utostr(D.UConstPool64) +
310 "' is invalid or out of range!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000311 } else { // This is really a signed reference. Transmogrify.
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000312 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000313 }
314 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000315 return ConstantUInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000316 }
317
Chris Lattner2079fde2001-10-13 06:41:08 +0000318 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000319 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattner2079fde2001-10-13 06:41:08 +0000320 ThrowException("FP constant invalid for type!!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000321 return ConstantFP::get(Ty, D.ConstPoolFP);
Chris Lattner2079fde2001-10-13 06:41:08 +0000322
323 case ValID::ConstNullVal: // Is it a null value?
Chris Lattner9b625032002-05-06 16:15:30 +0000324 if (!isa<PointerType>(Ty))
Chris Lattner2079fde2001-10-13 06:41:08 +0000325 ThrowException("Cannot create a a non pointer null!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000326 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner2079fde2001-10-13 06:41:08 +0000327
Chris Lattner30c89792001-09-07 16:35:17 +0000328 default:
329 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000330 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000331 } // End of switch
332
Chris Lattner2079fde2001-10-13 06:41:08 +0000333 assert(0 && "Unhandled case!");
334 return 0;
335}
336
337
338// getVal - This function is identical to getValNonImprovising, except that if a
339// value is not already defined, it "improvises" by creating a placeholder var
340// that looks and acts just like the requested variable. When the value is
341// defined later, all uses of the placeholder variable are replaced with the
342// real thing.
343//
344static Value *getVal(const Type *Ty, const ValID &D) {
345 assert(Ty != Type::TypeTy && "Should use getTypeVal for types!");
346
347 // See if the value has already been defined...
348 Value *V = getValNonImprovising(Ty, D);
349 if (V) return V;
Chris Lattner00950542001-06-06 20:29:01 +0000350
351 // If we reached here, we referenced either a symbol that we don't know about
352 // or an id number that hasn't been read yet. We may be referencing something
353 // forward, so just create an entry to be resolved later and get to it...
354 //
Chris Lattner00950542001-06-06 20:29:01 +0000355 Value *d = 0;
Chris Lattner30c89792001-09-07 16:35:17 +0000356 switch (Ty->getPrimitiveID()) {
357 case Type::LabelTyID: d = new BBPlaceHolder(Ty, D); break;
Chris Lattner30c89792001-09-07 16:35:17 +0000358 default: d = new ValuePlaceHolder(Ty, D); break;
Chris Lattner00950542001-06-06 20:29:01 +0000359 }
360
361 assert(d != 0 && "How did we not make something?");
Chris Lattner79df7c02002-03-26 18:01:55 +0000362 if (inFunctionScope())
Chris Lattner386a3b72001-10-16 19:54:17 +0000363 InsertValue(d, CurMeth.LateResolveValues);
364 else
365 InsertValue(d, CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000366 return d;
367}
368
369
370//===----------------------------------------------------------------------===//
371// Code to handle forward references in instructions
372//===----------------------------------------------------------------------===//
373//
374// This code handles the late binding needed with statements that reference
375// values not defined yet... for example, a forward branch, or the PHI node for
376// a loop body.
377//
378// This keeps a table (CurMeth.LateResolveValues) of all such forward references
379// and back patchs after we are done.
380//
381
382// ResolveDefinitions - If we could not resolve some defs at parsing
383// time (forward branches, phi functions for loops, etc...) resolve the
384// defs now...
385//
Chris Lattner386a3b72001-10-16 19:54:17 +0000386static void ResolveDefinitions(vector<ValueList> &LateResolvers,
Chris Lattnerbcafcce2002-07-25 06:17:42 +0000387 vector<ValueList> *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000388 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
389 for (unsigned ty = 0; ty < LateResolvers.size(); ty++) {
390 while (!LateResolvers[ty].empty()) {
391 Value *V = LateResolvers[ty].back();
Chris Lattner386a3b72001-10-16 19:54:17 +0000392 assert(!isa<Type>(V) && "Types should be in LateResolveTypes!");
393
Chris Lattner00950542001-06-06 20:29:01 +0000394 LateResolvers[ty].pop_back();
395 ValID &DID = getValIDFromPlaceHolder(V);
396
Chris Lattner2079fde2001-10-13 06:41:08 +0000397 Value *TheRealValue = getValNonImprovising(Type::getUniqueIDType(ty),DID);
Chris Lattner386a3b72001-10-16 19:54:17 +0000398 if (TheRealValue) {
399 V->replaceAllUsesWith(TheRealValue);
400 delete V;
401 } else if (FutureLateResolvers) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000402 // Functions have their unresolved items forwarded to the module late
Chris Lattner386a3b72001-10-16 19:54:17 +0000403 // resolver table
404 InsertValue(V, *FutureLateResolvers);
405 } else {
Chris Lattner9705a152002-05-02 19:27:42 +0000406 if (DID.Type == ValID::NameVal)
Chris Lattner30c89792001-09-07 16:35:17 +0000407 ThrowException("Reference to an invalid definition: '" +DID.getName()+
408 "' of type '" + V->getType()->getDescription() + "'",
409 getLineNumFromPlaceHolder(V));
410 else
411 ThrowException("Reference to an invalid definition: #" +
412 itostr(DID.Num) + " of type '" +
413 V->getType()->getDescription() + "'",
414 getLineNumFromPlaceHolder(V));
415 }
Chris Lattner00950542001-06-06 20:29:01 +0000416 }
417 }
418
419 LateResolvers.clear();
420}
421
Chris Lattner4a42e902001-10-22 05:56:09 +0000422// ResolveTypeTo - A brand new type was just declared. This means that (if
423// name is not null) things referencing Name can be resolved. Otherwise, things
424// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000425//
Chris Lattner4a42e902001-10-22 05:56:09 +0000426static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000427 vector<PATypeHolder> &Types = inFunctionScope() ?
Chris Lattner4a42e902001-10-22 05:56:09 +0000428 CurMeth.Types : CurModule.Types;
Chris Lattner00950542001-06-06 20:29:01 +0000429
Chris Lattner4a42e902001-10-22 05:56:09 +0000430 ValID D;
431 if (Name) D = ValID::create(Name);
432 else D = ValID::create((int)Types.size());
Chris Lattner30c89792001-09-07 16:35:17 +0000433
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000434 map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner4a42e902001-10-22 05:56:09 +0000435 CurMeth.LateResolveTypes : CurModule.LateResolveTypes;
436
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000437 map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000438 if (I != LateResolver.end()) {
Chris Lattner51727be2002-06-04 21:58:56 +0000439 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattner4a42e902001-10-22 05:56:09 +0000440 LateResolver.erase(I);
441 }
442}
443
444// ResolveTypes - At this point, all types should be resolved. Any that aren't
445// are errors.
446//
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000447static void ResolveTypes(map<ValID, PATypeHolder> &LateResolveTypes) {
Chris Lattner4a42e902001-10-22 05:56:09 +0000448 if (!LateResolveTypes.empty()) {
Chris Lattner82269592001-10-22 06:01:08 +0000449 const ValID &DID = LateResolveTypes.begin()->first;
Chris Lattner4a42e902001-10-22 05:56:09 +0000450
451 if (DID.Type == ValID::NameVal)
Chris Lattner82269592001-10-22 06:01:08 +0000452 ThrowException("Reference to an invalid type: '" +DID.getName() + "'");
Chris Lattner4a42e902001-10-22 05:56:09 +0000453 else
Chris Lattner82269592001-10-22 06:01:08 +0000454 ThrowException("Reference to an invalid type: #" + itostr(DID.Num));
Chris Lattner30c89792001-09-07 16:35:17 +0000455 }
456}
457
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000458
Chris Lattner1781aca2001-09-18 04:00:54 +0000459// setValueName - Set the specified value to the name given. The name may be
460// null potentially, in which case this is a noop. The string passed in is
461// assumed to be a malloc'd string buffer, and is freed by this function.
462//
Chris Lattnerb7474512001-10-03 15:39:04 +0000463// This function returns true if the value has already been defined, but is
464// allowed to be redefined in the specified context. If the name is a new name
465// for the typeplane, false is returned.
466//
467static bool setValueName(Value *V, char *NameStr) {
468 if (NameStr == 0) return false;
Chris Lattner386a3b72001-10-16 19:54:17 +0000469
Chris Lattner1781aca2001-09-18 04:00:54 +0000470 string Name(NameStr); // Copy string
471 free(NameStr); // Free old string
472
Chris Lattner2079fde2001-10-13 06:41:08 +0000473 if (V->getType() == Type::VoidTy)
474 ThrowException("Can't assign name '" + Name +
475 "' to a null valued instruction!");
476
Chris Lattner79df7c02002-03-26 18:01:55 +0000477 SymbolTable *ST = inFunctionScope() ?
478 CurMeth.CurrentFunction->getSymbolTableSure() :
Chris Lattner30c89792001-09-07 16:35:17 +0000479 CurModule.CurrentModule->getSymbolTableSure();
480
481 Value *Existing = ST->lookup(V->getType(), Name);
482 if (Existing) { // Inserting a name that is already defined???
483 // There is only one case where this is allowed: when we are refining an
484 // opaque type. In this case, Existing will be an opaque type.
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000485 if (const Type *Ty = dyn_cast<const Type>(Existing)) {
Chris Lattner51727be2002-06-04 21:58:56 +0000486 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Ty)) {
Chris Lattner30c89792001-09-07 16:35:17 +0000487 // We ARE replacing an opaque type!
Chris Lattner51727be2002-06-04 21:58:56 +0000488 ((OpaqueType*)OpTy)->refineAbstractTypeTo(cast<Type>(V));
Chris Lattnerb7474512001-10-03 15:39:04 +0000489 return true;
Chris Lattner30c89792001-09-07 16:35:17 +0000490 }
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000491 }
Chris Lattner30c89792001-09-07 16:35:17 +0000492
Chris Lattner9636a912001-10-01 16:18:37 +0000493 // Otherwise, we are a simple redefinition of a value, check to see if it
494 // is defined the same as the old one...
495 if (const Type *Ty = dyn_cast<const Type>(Existing)) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000496 if (Ty == cast<const Type>(V)) return true; // Yes, it's equal.
497 // cerr << "Type: " << Ty->getDescription() << " != "
498 // << cast<const Type>(V)->getDescription() << "!\n";
499 } else if (GlobalVariable *EGV = dyn_cast<GlobalVariable>(Existing)) {
Chris Lattner43efcbf2001-10-03 19:35:57 +0000500 // We are allowed to redefine a global variable in two circumstances:
501 // 1. If at least one of the globals is uninitialized or
502 // 2. If both initializers have the same value.
503 //
504 // This can only be done if the const'ness of the vars is the same.
505 //
Chris Lattner89219832001-10-03 19:35:04 +0000506 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
507 if (EGV->isConstant() == GV->isConstant() &&
508 (!EGV->hasInitializer() || !GV->hasInitializer() ||
509 EGV->getInitializer() == GV->getInitializer())) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000510
Chris Lattner89219832001-10-03 19:35:04 +0000511 // Make sure the existing global version gets the initializer!
512 if (GV->hasInitializer() && !EGV->hasInitializer())
513 EGV->setInitializer(GV->getInitializer());
514
Chris Lattner2079fde2001-10-13 06:41:08 +0000515 delete GV; // Destroy the duplicate!
Chris Lattner89219832001-10-03 19:35:04 +0000516 return true; // They are equivalent!
517 }
Chris Lattnerb7474512001-10-03 15:39:04 +0000518 }
Chris Lattner9636a912001-10-01 16:18:37 +0000519 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000520 ThrowException("Redefinition of value named '" + Name + "' in the '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000521 V->getType()->getDescription() + "' type plane!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000522 }
Chris Lattner00950542001-06-06 20:29:01 +0000523
Chris Lattner30c89792001-09-07 16:35:17 +0000524 V->setName(Name, ST);
Chris Lattnerb7474512001-10-03 15:39:04 +0000525 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000526}
527
Chris Lattner8896eda2001-07-09 19:38:36 +0000528
Chris Lattner30c89792001-09-07 16:35:17 +0000529//===----------------------------------------------------------------------===//
530// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000531//
Chris Lattner8896eda2001-07-09 19:38:36 +0000532
Chris Lattner30c89792001-09-07 16:35:17 +0000533// TypeContains - Returns true if Ty contains E in it.
534//
535static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner3ff43872001-09-28 22:56:31 +0000536 return find(df_begin(Ty), df_end(Ty), E) != df_end(Ty);
Chris Lattner30c89792001-09-07 16:35:17 +0000537}
Chris Lattner698b56e2001-07-20 19:15:08 +0000538
Chris Lattner30c89792001-09-07 16:35:17 +0000539
540static vector<pair<unsigned, OpaqueType *> > UpRefs;
541
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000542static PATypeHolder HandleUpRefs(const Type *ty) {
543 PATypeHolder Ty(ty);
Chris Lattner5084d032001-11-02 07:46:26 +0000544 UR_OUT("Type '" << ty->getDescription() <<
545 "' newly formed. Resolving upreferences.\n" <<
546 UpRefs.size() << " upreferences active!\n");
Chris Lattner30c89792001-09-07 16:35:17 +0000547 for (unsigned i = 0; i < UpRefs.size(); ) {
Chris Lattner5084d032001-11-02 07:46:26 +0000548 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Chris Lattner30c89792001-09-07 16:35:17 +0000549 << UpRefs[i].second->getDescription() << ") = "
Chris Lattner5084d032001-11-02 07:46:26 +0000550 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << endl);
Chris Lattner30c89792001-09-07 16:35:17 +0000551 if (TypeContains(Ty, UpRefs[i].second)) {
552 unsigned Level = --UpRefs[i].first; // Decrement level of upreference
Chris Lattner5084d032001-11-02 07:46:26 +0000553 UR_OUT(" Uplevel Ref Level = " << Level << endl);
Chris Lattner30c89792001-09-07 16:35:17 +0000554 if (Level == 0) { // Upreference should be resolved!
Chris Lattner5084d032001-11-02 07:46:26 +0000555 UR_OUT(" * Resolving upreference for "
556 << UpRefs[i].second->getDescription() << endl;
Chris Lattner30c89792001-09-07 16:35:17 +0000557 string OldName = UpRefs[i].second->getDescription());
558 UpRefs[i].second->refineAbstractTypeTo(Ty);
559 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
Chris Lattner5084d032001-11-02 07:46:26 +0000560 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
Chris Lattner30c89792001-09-07 16:35:17 +0000561 << (const void*)Ty << ", " << Ty->getDescription() << endl);
562 continue;
563 }
564 }
565
566 ++i; // Otherwise, no resolve, move on...
Chris Lattner8896eda2001-07-09 19:38:36 +0000567 }
Chris Lattner30c89792001-09-07 16:35:17 +0000568 // FIXME: TODO: this should return the updated type
Chris Lattner8896eda2001-07-09 19:38:36 +0000569 return Ty;
570}
571
Chris Lattner30c89792001-09-07 16:35:17 +0000572
Chris Lattner00950542001-06-06 20:29:01 +0000573//===----------------------------------------------------------------------===//
574// RunVMAsmParser - Define an interface to this parser
575//===----------------------------------------------------------------------===//
576//
Chris Lattnera2850432001-07-22 18:36:00 +0000577Module *RunVMAsmParser(const string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000578 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000579 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000580 llvmAsmlineno = 1; // Reset the current line number...
581
582 CurModule.CurrentModule = new Module(); // Allocate a new module to read
583 yyparse(); // Parse the file.
584 Module *Result = ParserResult;
Chris Lattner00950542001-06-06 20:29:01 +0000585 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
586 ParserResult = 0;
587
588 return Result;
589}
590
591%}
592
593%union {
Chris Lattner30c89792001-09-07 16:35:17 +0000594 Module *ModuleVal;
Chris Lattner79df7c02002-03-26 18:01:55 +0000595 Function *FunctionVal;
Chris Lattner46748042002-04-09 19:41:42 +0000596 std::pair<Argument*, char*> *ArgVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000597 BasicBlock *BasicBlockVal;
598 TerminatorInst *TermInstVal;
599 Instruction *InstVal;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000600 Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000601
Chris Lattner30c89792001-09-07 16:35:17 +0000602 const Type *PrimType;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000603 PATypeHolder *TypeVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000604 Value *ValueVal;
605
Chris Lattner46748042002-04-09 19:41:42 +0000606 std::list<std::pair<Argument*,char*> > *ArgList;
Chris Lattner697954c2002-01-20 22:54:45 +0000607 std::vector<Value*> *ValueList;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000608 std::list<PATypeHolder> *TypeList;
Chris Lattner697954c2002-01-20 22:54:45 +0000609 std::list<std::pair<Value*,
610 BasicBlock*> > *PHIList; // Represent the RHS of PHI node
Chris Lattner46748042002-04-09 19:41:42 +0000611 std::vector<std::pair<Constant*, BasicBlock*> > *JumpTable;
Chris Lattner697954c2002-01-20 22:54:45 +0000612 std::vector<Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000613
Chris Lattner30c89792001-09-07 16:35:17 +0000614 int64_t SInt64Val;
615 uint64_t UInt64Val;
616 int SIntVal;
617 unsigned UIntVal;
618 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000619 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000620
Chris Lattner30c89792001-09-07 16:35:17 +0000621 char *StrVal; // This memory is strdup'd!
622 ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000623
Chris Lattner30c89792001-09-07 16:35:17 +0000624 Instruction::UnaryOps UnaryOpVal;
625 Instruction::BinaryOps BinaryOpVal;
626 Instruction::TermOps TermOpVal;
627 Instruction::MemoryOps MemOpVal;
628 Instruction::OtherOps OtherOpVal;
Chris Lattner00950542001-06-06 20:29:01 +0000629}
630
Chris Lattner79df7c02002-03-26 18:01:55 +0000631%type <ModuleVal> Module FunctionList
632%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000633%type <BasicBlockVal> BasicBlock InstructionList
634%type <TermInstVal> BBTerminatorInst
635%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000636%type <ConstVal> ConstVal ConstExpr
Chris Lattner6cdb0112001-11-26 16:54:11 +0000637%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000638%type <ArgList> ArgList ArgListH
639%type <ArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000640%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000641%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000642%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000643%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000644%type <JumpTable> JumpTable
Chris Lattnerdda71962001-11-26 18:54:16 +0000645%type <BoolVal> GlobalType OptInternal // GLOBAL or CONSTANT? Intern?
Chris Lattner00950542001-06-06 20:29:01 +0000646
Chris Lattner2079fde2001-10-13 06:41:08 +0000647// ValueRef - Unresolved reference to a definition or BB
648%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000649%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000650// Tokens and types for handling constant integer values
651//
652// ESINT64VAL - A negative number within long long range
653%token <SInt64Val> ESINT64VAL
654
655// EUINT64VAL - A positive number within uns. long long range
656%token <UInt64Val> EUINT64VAL
657%type <SInt64Val> EINT64VAL
658
659%token <SIntVal> SINTVAL // Signed 32 bit ints...
660%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
661%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000662%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000663
664// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000665%type <TypeVal> Types TypesV UpRTypes UpRTypesV
666%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattner30c89792001-09-07 16:35:17 +0000667%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
668%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000669
670%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
Chris Lattner8ebccb72002-05-22 22:33:00 +0000671%type <StrVal> OptVAR_ID OptAssign FuncName
Chris Lattner00950542001-06-06 20:29:01 +0000672
673
Chris Lattner9b02cc32002-05-03 18:23:48 +0000674%token IMPLEMENTATION TRUE FALSE BEGINTOK ENDTOK DECLARE GLOBAL CONSTANT UNINIT
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000675%token TO EXCEPT DOTDOTDOT STRING NULL_TOK CONST INTERNAL OPAQUE
Chris Lattner00950542001-06-06 20:29:01 +0000676
677// Basic Block Terminating Operators
678%token <TermOpVal> RET BR SWITCH
679
680// Unary Operators
681%type <UnaryOpVal> UnaryOps // all the unary operators
Chris Lattner71496b32001-07-08 19:03:27 +0000682%token <UnaryOpVal> NOT
Chris Lattner00950542001-06-06 20:29:01 +0000683
684// Binary Operators
685%type <BinaryOpVal> BinaryOps // all the binary operators
Chris Lattner42c9e772001-10-20 09:32:59 +0000686%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000687%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000688
689// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000690%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000691
Chris Lattner027dcc52001-07-08 21:10:27 +0000692// Other Operators
693%type <OtherOpVal> ShiftOps
Chris Lattner2079fde2001-10-13 06:41:08 +0000694%token <OtherOpVal> PHI CALL INVOKE CAST SHL SHR
Chris Lattner027dcc52001-07-08 21:10:27 +0000695
Chris Lattner00950542001-06-06 20:29:01 +0000696%start Module
697%%
698
699// Handle constant integer size restriction and conversion...
700//
701
Chris Lattner51727be2002-06-04 21:58:56 +0000702INTVAL : SINTVAL;
Chris Lattner00950542001-06-06 20:29:01 +0000703INTVAL : UINTVAL {
704 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
705 ThrowException("Value too large for type!");
706 $$ = (int32_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000707};
Chris Lattner00950542001-06-06 20:29:01 +0000708
709
Chris Lattner51727be2002-06-04 21:58:56 +0000710EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner00950542001-06-06 20:29:01 +0000711EINT64VAL : EUINT64VAL {
712 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
713 ThrowException("Value too large for type!");
714 $$ = (int64_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000715};
Chris Lattner00950542001-06-06 20:29:01 +0000716
Chris Lattner00950542001-06-06 20:29:01 +0000717// Operations that are notably excluded from this list include:
718// RET, BR, & SWITCH because they end basic blocks and are treated specially.
719//
Chris Lattner51727be2002-06-04 21:58:56 +0000720UnaryOps : NOT;
721BinaryOps : ADD | SUB | MUL | DIV | REM | AND | OR | XOR;
722BinaryOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
723ShiftOps : SHL | SHR;
Chris Lattner00950542001-06-06 20:29:01 +0000724
Chris Lattnere98dda62001-07-14 06:10:16 +0000725// These are some types that allow classification if we only want a particular
726// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner51727be2002-06-04 21:58:56 +0000727SIntType : LONG | INT | SHORT | SBYTE;
728UIntType : ULONG | UINT | USHORT | UBYTE;
729IntType : SIntType | UIntType;
730FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +0000731
Chris Lattnere98dda62001-07-14 06:10:16 +0000732// OptAssign - Value producing statements have an optional assignment component
Chris Lattner00950542001-06-06 20:29:01 +0000733OptAssign : VAR_ID '=' {
734 $$ = $1;
735 }
736 | /*empty*/ {
737 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +0000738 };
Chris Lattner00950542001-06-06 20:29:01 +0000739
Chris Lattner51727be2002-06-04 21:58:56 +0000740OptInternal : INTERNAL { $$ = true; } | /*empty*/ { $$ = false; };
Chris Lattner30c89792001-09-07 16:35:17 +0000741
742//===----------------------------------------------------------------------===//
743// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +0000744// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +0000745// access to it, a user must explicitly use TypesV.
746//
747
748// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +0000749TypesV : Types | VOID { $$ = new PATypeHolder($1); };
750UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +0000751
752Types : UpRTypes {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000753 if (UpRefs.size())
754 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
755 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000756 };
Chris Lattner30c89792001-09-07 16:35:17 +0000757
758
759// Derived types are added later...
760//
Chris Lattner51727be2002-06-04 21:58:56 +0000761PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
762PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000763UpRTypes : OPAQUE {
764 $$ = new PATypeHolder(OpaqueType::get());
765 }
766 | PrimType {
767 $$ = new PATypeHolder($1);
Chris Lattner51727be2002-06-04 21:58:56 +0000768 };
Chris Lattner30c89792001-09-07 16:35:17 +0000769UpRTypes : ValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000770 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner51727be2002-06-04 21:58:56 +0000771};
Chris Lattner30c89792001-09-07 16:35:17 +0000772
Chris Lattner30c89792001-09-07 16:35:17 +0000773// Include derived types in the Types production.
774//
775UpRTypes : '\\' EUINT64VAL { // Type UpReference
776 if ($2 > (uint64_t)INT64_MAX) ThrowException("Value out of range!");
777 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
778 UpRefs.push_back(make_pair((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000779 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +0000780 UR_OUT("New Upreference!\n");
781 }
Chris Lattner79df7c02002-03-26 18:01:55 +0000782 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner30c89792001-09-07 16:35:17 +0000783 vector<const Type*> Params;
Chris Lattner697954c2002-01-20 22:54:45 +0000784 mapto($3->begin(), $3->end(), std::back_inserter(Params),
785 std::mem_fun_ref(&PATypeHandle<Type>::get));
Chris Lattner2079fde2001-10-13 06:41:08 +0000786 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
787 if (isVarArg) Params.pop_back();
788
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000789 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +0000790 delete $3; // Delete the argument list
791 delete $1; // Delete the old type handle
792 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000793 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000794 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000795 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +0000796 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000797 | '{' TypeListI '}' { // Structure type?
798 vector<const Type*> Elements;
Chris Lattner697954c2002-01-20 22:54:45 +0000799 mapto($2->begin(), $2->end(), std::back_inserter(Elements),
800 std::mem_fun_ref(&PATypeHandle<Type>::get));
Chris Lattner30c89792001-09-07 16:35:17 +0000801
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000802 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000803 delete $2;
804 }
805 | '{' '}' { // Empty structure type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000806 $$ = new PATypeHolder(StructType::get(vector<const Type*>()));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000807 }
808 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000809 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000810 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000811 };
Chris Lattner30c89792001-09-07 16:35:17 +0000812
Chris Lattner7e708292002-06-25 16:13:24 +0000813// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +0000814// declaration type lists
815//
816TypeListI : UpRTypes {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000817 $$ = new list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +0000818 $$->push_back(*$1); delete $1;
819 }
820 | TypeListI ',' UpRTypes {
821 ($$=$1)->push_back(*$3); delete $3;
Chris Lattner51727be2002-06-04 21:58:56 +0000822 };
Chris Lattner30c89792001-09-07 16:35:17 +0000823
Chris Lattner7e708292002-06-25 16:13:24 +0000824// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +0000825ArgTypeListI : TypeListI
826 | TypeListI ',' DOTDOTDOT {
827 ($$=$1)->push_back(Type::VoidTy);
828 }
829 | DOTDOTDOT {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000830 ($$ = new list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattner30c89792001-09-07 16:35:17 +0000831 }
832 | /*empty*/ {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000833 $$ = new list<PATypeHolder>();
Chris Lattner51727be2002-06-04 21:58:56 +0000834 };
Chris Lattner30c89792001-09-07 16:35:17 +0000835
Chris Lattnere98dda62001-07-14 06:10:16 +0000836// ConstVal - The various declarations that go into the constant pool. This
837// includes all forward declarations of types, constants, and functions.
838//
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000839ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
840 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
841 if (ATy == 0)
842 ThrowException("Cannot make array constant with type: '" +
843 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +0000844 const Type *ETy = ATy->getElementType();
845 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +0000846
Chris Lattner30c89792001-09-07 16:35:17 +0000847 // Verify that we have the correct size...
848 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +0000849 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattner30c89792001-09-07 16:35:17 +0000850 utostr($3->size()) + " arguments, but has size of " +
851 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +0000852
Chris Lattner30c89792001-09-07 16:35:17 +0000853 // Verify all elements are correct type!
854 for (unsigned i = 0; i < $3->size(); i++) {
855 if (ETy != (*$3)[i]->getType())
Chris Lattner00950542001-06-06 20:29:01 +0000856 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +0000857 ETy->getDescription() +"' as required!\nIt is of type '"+
858 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +0000859 }
860
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000861 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +0000862 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000863 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000864 | Types '[' ']' {
865 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
866 if (ATy == 0)
867 ThrowException("Cannot make array constant with type: '" +
868 (*$1)->getDescription() + "'!");
869
870 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +0000871 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +0000872 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattner30c89792001-09-07 16:35:17 +0000873 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000874 $$ = ConstantArray::get(ATy, vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +0000875 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +0000876 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000877 | Types 'c' STRINGCONSTANT {
878 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
879 if (ATy == 0)
880 ThrowException("Cannot make array constant with type: '" +
881 (*$1)->getDescription() + "'!");
882
Chris Lattner30c89792001-09-07 16:35:17 +0000883 int NumElements = ATy->getNumElements();
884 const Type *ETy = ATy->getElementType();
885 char *EndStr = UnEscapeLexed($3, true);
886 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +0000887 ThrowException("Can't build string constant of size " +
Chris Lattner30c89792001-09-07 16:35:17 +0000888 itostr((int)(EndStr-$3)) +
889 " when array has size " + itostr(NumElements) + "!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000890 vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +0000891 if (ETy == Type::SByteTy) {
892 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000893 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +0000894 } else if (ETy == Type::UByteTy) {
895 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000896 Vals.push_back(ConstantUInt::get(ETy, *C));
Chris Lattner93750fa2001-07-28 17:48:55 +0000897 } else {
Chris Lattner30c89792001-09-07 16:35:17 +0000898 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +0000899 ThrowException("Cannot build string arrays of non byte sized elements!");
900 }
Chris Lattner30c89792001-09-07 16:35:17 +0000901 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000902 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +0000903 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +0000904 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000905 | Types '{' ConstVector '}' {
906 const StructType *STy = dyn_cast<const StructType>($1->get());
907 if (STy == 0)
908 ThrowException("Cannot make struct constant with type: '" +
909 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +0000910 // FIXME: TODO: Check to see that the constants are compatible with the type
911 // initializer!
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000912 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +0000913 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000914 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000915 | Types NULL_TOK {
916 const PointerType *PTy = dyn_cast<const PointerType>($1->get());
917 if (PTy == 0)
918 ThrowException("Cannot make null pointer constant with type: '" +
919 (*$1)->getDescription() + "'!");
920
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000921 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000922 delete $1;
923 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000924 | Types SymbolicValueRef {
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000925 const PointerType *Ty = dyn_cast<const PointerType>($1->get());
926 if (Ty == 0)
927 ThrowException("Global const reference must be a pointer type!");
928
Chris Lattner2079fde2001-10-13 06:41:08 +0000929 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000930
Chris Lattner2079fde2001-10-13 06:41:08 +0000931 // If this is an initializer for a constant pointer, which is referencing a
932 // (currently) undefined variable, create a stub now that shall be replaced
933 // in the future with the right type of variable.
934 //
935 if (V == 0) {
936 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
937 const PointerType *PT = cast<PointerType>(Ty);
938
939 // First check to see if the forward references value is already created!
940 PerModuleInfo::GlobalRefsType::iterator I =
941 CurModule.GlobalRefs.find(make_pair(PT, $2));
942
943 if (I != CurModule.GlobalRefs.end()) {
944 V = I->second; // Placeholder already exists, use it...
945 } else {
946 // TODO: Include line number info by creating a subclass of
947 // TODO: GlobalVariable here that includes the said information!
948
949 // Create a placeholder for the global variable reference...
Chris Lattner7a176752001-12-04 00:03:30 +0000950 GlobalVariable *GV = new GlobalVariable(PT->getElementType(),
951 false, true);
Chris Lattner2079fde2001-10-13 06:41:08 +0000952 // Keep track of the fact that we have a forward ref to recycle it
953 CurModule.GlobalRefs.insert(make_pair(make_pair(PT, $2), GV));
954
955 // Must temporarily push this value into the module table...
956 CurModule.CurrentModule->getGlobalList().push_back(GV);
957 V = GV;
958 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000959 }
960
Chris Lattner2079fde2001-10-13 06:41:08 +0000961 GlobalValue *GV = cast<GlobalValue>(V);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000962 $$ = ConstantPointerRef::get(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +0000963 delete $1; // Free the type handle
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000964 }
965 | ConstExpr {
966 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000967 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000968
Chris Lattner00950542001-06-06 20:29:01 +0000969
Chris Lattnerf8dff732002-07-18 05:18:37 +0000970// FIXME: ConstExpr::get never return null! Do checking here in the parser.
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000971ConstExpr: Types CAST ConstVal {
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000972 $$ = ConstantExpr::get(Instruction::Cast, $3, $1->get());
Chris Lattnerf8dff732002-07-18 05:18:37 +0000973 if ($$ == 0) ThrowException("constant expression builder returned null!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000974 delete $1;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000975 }
976 | Types GETELEMENTPTR '(' ConstVal IndexList ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000977 if (!isa<PointerType>($4->getType()))
978 ThrowException("GetElementPtr requires a pointer operand!");
979
980 const Type *IdxTy =
981 GetElementPtrInst::getIndexedType($4->getType(), *$5, true);
982 if (!IdxTy)
983 ThrowException("Index list invalid for constant getelementptr!");
984 if (PointerType::get(IdxTy) != $1->get())
985 ThrowException("Declared type of constant getelementptr is incorrect!");
986
Chris Lattnercc4b6ec2002-07-18 00:14:27 +0000987 vector<Constant*> IdxVec;
988 for (unsigned i = 0, e = $5->size(); i != e; ++i)
989 if (Constant *C = dyn_cast<Constant>((*$5)[i]))
990 IdxVec.push_back(C);
991 else
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000992 ThrowException("Indices to constant getelementptr must be constants!");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +0000993
994 delete $5;
995
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000996 $$ = ConstantExpr::getGetElementPtr($4, IdxVec);
997 delete $1;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000998 }
999 | Types UnaryOps ConstVal {
Chris Lattnerf8dff732002-07-18 05:18:37 +00001000 $$ = ConstantExpr::get($2, $3, $1->get());
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001001 delete $1;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001002 }
1003 | Types BinaryOps ConstVal ',' ConstVal {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001004 if ($3->getType() != $5->getType())
1005 ThrowException("Binary operator types must match!");
1006 if ($1->get() != $3->getType())
1007 ThrowException("Return type of binary constant must match arguments!");
1008 $$ = ConstantExpr::get($2, $3, $5);
1009 delete $1;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001010 }
1011 | Types ShiftOps ConstVal ',' ConstVal {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001012 if ($1->get() != $3->getType())
1013 ThrowException("Return type of shift constant must match argument!");
1014 if ($5->getType() != Type::UByteTy)
1015 ThrowException("Shift count for shift constant must be unsigned byte!");
1016
1017 $$ = ConstantExpr::get($2, $3, $5);
1018 delete $1;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001019 }
1020 ;
1021
1022
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001023ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001024 if (!ConstantSInt::isValueValidForType($1, $2))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001025 ThrowException("Constant value doesn't fit in type!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001026 $$ = ConstantSInt::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001027 }
1028 | UIntType EUINT64VAL { // integral constants
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001029 if (!ConstantUInt::isValueValidForType($1, $2))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001030 ThrowException("Constant value doesn't fit in type!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001031 $$ = ConstantUInt::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001032 }
1033 | BOOL TRUE { // Boolean constants
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001034 $$ = ConstantBool::True;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001035 }
1036 | BOOL FALSE { // Boolean constants
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001037 $$ = ConstantBool::False;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001038 }
1039 | FPType FPVAL { // Float & Double constants
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001040 $$ = ConstantFP::get($1, $2);
Chris Lattner51727be2002-06-04 21:58:56 +00001041 };
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001042
Chris Lattnere98dda62001-07-14 06:10:16 +00001043// ConstVector - A list of comma seperated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001044ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001045 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001046 }
1047 | ConstVal {
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001048 $$ = new vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001049 $$->push_back($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001050 };
Chris Lattner00950542001-06-06 20:29:01 +00001051
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001052
Chris Lattner1781aca2001-09-18 04:00:54 +00001053// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001054GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001055
Chris Lattner00950542001-06-06 20:29:01 +00001056
Chris Lattner0e73ce62002-05-02 19:11:13 +00001057//===----------------------------------------------------------------------===//
1058// Rules to match Modules
1059//===----------------------------------------------------------------------===//
1060
1061// Module rule: Capture the result of parsing the whole file into a result
1062// variable...
1063//
1064Module : FunctionList {
1065 $$ = ParserResult = $1;
1066 CurModule.ModuleDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001067};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001068
Chris Lattner7e708292002-06-25 16:13:24 +00001069// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001070//
1071FunctionList : FunctionList Function {
1072 $$ = $1;
1073 assert($2->getParent() == 0 && "Function already in module!");
1074 $1->getFunctionList().push_back($2);
1075 CurMeth.FunctionDone();
1076 }
1077 | FunctionList FunctionProto {
1078 $$ = $1;
1079 }
1080 | FunctionList IMPLEMENTATION {
1081 $$ = $1;
1082 }
1083 | ConstPool {
1084 $$ = CurModule.CurrentModule;
1085 // Resolve circular types before we parse the body of the module
1086 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001087 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001088
Chris Lattnere98dda62001-07-14 06:10:16 +00001089// ConstPool - Constants with optional names assigned to them.
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001090ConstPool : ConstPool OptAssign CONST ConstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001091 if (setValueName($4, $2)) { assert(0 && "No redefinitions allowed!"); }
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001092 InsertValue($4);
Chris Lattner00950542001-06-06 20:29:01 +00001093 }
Chris Lattner30c89792001-09-07 16:35:17 +00001094 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner4a42e902001-10-22 05:56:09 +00001095 // Eagerly resolve types. This is not an optimization, this is a
1096 // requirement that is due to the fact that we could have this:
1097 //
1098 // %list = type { %list * }
1099 // %list = type { %list * } ; repeated type decl
1100 //
1101 // If types are not resolved eagerly, then the two types will not be
1102 // determined to be the same type!
1103 //
1104 ResolveTypeTo($2, $4->get());
1105
Chris Lattner1781aca2001-09-18 04:00:54 +00001106 // TODO: FIXME when Type are not const
Chris Lattnerb7474512001-10-03 15:39:04 +00001107 if (!setValueName(const_cast<Type*>($4->get()), $2)) {
1108 // If this is not a redefinition of a type...
1109 if (!$2) {
1110 InsertType($4->get(),
Chris Lattner79df7c02002-03-26 18:01:55 +00001111 inFunctionScope() ? CurMeth.Types : CurModule.Types);
Chris Lattnerb7474512001-10-03 15:39:04 +00001112 }
Chris Lattner30c89792001-09-07 16:35:17 +00001113 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001114
1115 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001116 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001117 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001118 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001119 | ConstPool OptAssign OptInternal GlobalType ConstVal {
1120 const Type *Ty = $5->getType();
Chris Lattner1781aca2001-09-18 04:00:54 +00001121 // Global declarations appear in Constant Pool
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001122 Constant *Initializer = $5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001123 if (Initializer == 0)
1124 ThrowException("Global value initializer is not a constant!");
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001125
Chris Lattnerdda71962001-11-26 18:54:16 +00001126 GlobalVariable *GV = new GlobalVariable(Ty, $4, $3, Initializer);
Chris Lattnerb7474512001-10-03 15:39:04 +00001127 if (!setValueName(GV, $2)) { // If not redefining...
1128 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001129 int Slot = InsertValue(GV, CurModule.Values);
1130
1131 if (Slot != -1) {
1132 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1133 } else {
1134 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1135 (char*)GV->getName().c_str()));
1136 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001137 }
Chris Lattner1781aca2001-09-18 04:00:54 +00001138 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001139 | ConstPool OptAssign OptInternal UNINIT GlobalType Types {
1140 const Type *Ty = *$6;
Chris Lattner1781aca2001-09-18 04:00:54 +00001141 // Global declarations appear in Constant Pool
Chris Lattnerdda71962001-11-26 18:54:16 +00001142 GlobalVariable *GV = new GlobalVariable(Ty, $5, $3);
Chris Lattnerb7474512001-10-03 15:39:04 +00001143 if (!setValueName(GV, $2)) { // If not redefining...
1144 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001145 int Slot = InsertValue(GV, CurModule.Values);
1146
1147 if (Slot != -1) {
1148 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1149 } else {
1150 assert(GV->hasName() && "Not named and not numbered!?");
1151 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1152 (char*)GV->getName().c_str()));
1153 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001154 }
Chris Lattner09c07532002-03-31 07:16:49 +00001155 delete $6;
Chris Lattnere98dda62001-07-14 06:10:16 +00001156 }
Chris Lattner00950542001-06-06 20:29:01 +00001157 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001158 };
Chris Lattner00950542001-06-06 20:29:01 +00001159
1160
1161//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001162// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001163//===----------------------------------------------------------------------===//
1164
Chris Lattner51727be2002-06-04 21:58:56 +00001165OptVAR_ID : VAR_ID | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001166
1167ArgVal : Types OptVAR_ID {
Chris Lattner46748042002-04-09 19:41:42 +00001168 $$ = new pair<Argument*, char*>(new Argument(*$1), $2);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001169 delete $1; // Delete the type handle..
Chris Lattner51727be2002-06-04 21:58:56 +00001170};
Chris Lattner00950542001-06-06 20:29:01 +00001171
1172ArgListH : ArgVal ',' ArgListH {
1173 $$ = $3;
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001174 $3->push_front(*$1);
1175 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001176 }
1177 | ArgVal {
Chris Lattner46748042002-04-09 19:41:42 +00001178 $$ = new list<pair<Argument*,char*> >();
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001179 $$->push_front(*$1);
1180 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001181 }
Chris Lattner8b81bf52001-07-25 22:47:46 +00001182 | DOTDOTDOT {
Chris Lattner46748042002-04-09 19:41:42 +00001183 $$ = new list<pair<Argument*, char*> >();
1184 $$->push_front(pair<Argument*,char*>(new Argument(Type::VoidTy), 0));
Chris Lattner51727be2002-06-04 21:58:56 +00001185 };
Chris Lattner00950542001-06-06 20:29:01 +00001186
1187ArgList : ArgListH {
1188 $$ = $1;
1189 }
1190 | /* empty */ {
1191 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001192 };
Chris Lattner00950542001-06-06 20:29:01 +00001193
Chris Lattner8ebccb72002-05-22 22:33:00 +00001194FuncName : VAR_ID | STRINGCONSTANT;
1195
1196FunctionHeaderH : OptInternal TypesV FuncName '(' ArgList ')' {
Chris Lattnerdda71962001-11-26 18:54:16 +00001197 UnEscapeLexed($3);
Chris Lattner79df7c02002-03-26 18:01:55 +00001198 string FunctionName($3);
Chris Lattnerdda71962001-11-26 18:54:16 +00001199
Chris Lattner30c89792001-09-07 16:35:17 +00001200 vector<const Type*> ParamTypeList;
Chris Lattnerdda71962001-11-26 18:54:16 +00001201 if ($5)
Chris Lattner46748042002-04-09 19:41:42 +00001202 for (list<pair<Argument*,char*> >::iterator I = $5->begin();
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001203 I != $5->end(); ++I)
1204 ParamTypeList.push_back(I->first->getType());
Chris Lattner00950542001-06-06 20:29:01 +00001205
Chris Lattner2079fde2001-10-13 06:41:08 +00001206 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1207 if (isVarArg) ParamTypeList.pop_back();
1208
Chris Lattner79df7c02002-03-26 18:01:55 +00001209 const FunctionType *MT = FunctionType::get(*$2, ParamTypeList, isVarArg);
Chris Lattneref9c23f2001-10-03 14:53:21 +00001210 const PointerType *PMT = PointerType::get(MT);
Chris Lattnerdda71962001-11-26 18:54:16 +00001211 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001212
Chris Lattner79df7c02002-03-26 18:01:55 +00001213 Function *M = 0;
Chris Lattnere1815642001-07-15 06:35:53 +00001214 if (SymbolTable *ST = CurModule.CurrentModule->getSymbolTable()) {
Chris Lattner79df7c02002-03-26 18:01:55 +00001215 // Is the function already in symtab?
1216 if (Value *V = ST->lookup(PMT, FunctionName)) {
1217 M = cast<Function>(V);
Chris Lattner00950542001-06-06 20:29:01 +00001218
Chris Lattnere1815642001-07-15 06:35:53 +00001219 // Yes it is. If this is the case, either we need to be a forward decl,
1220 // or it needs to be.
1221 if (!CurMeth.isDeclare && !M->isExternal())
Chris Lattner7e708292002-06-25 16:13:24 +00001222 ThrowException("Redefinition of function '" + FunctionName + "'!");
Chris Lattner34538142002-03-08 19:11:42 +00001223
Chris Lattner5659dd12002-07-15 00:10:33 +00001224 // Make sure that we keep track of the internal marker, even if there was
1225 // a previous "declare".
1226 if ($1)
1227 M->setInternalLinkage(true);
1228
Chris Lattner7e708292002-06-25 16:13:24 +00001229 // If we found a preexisting function prototype, remove it from the
1230 // module, so that we don't get spurious conflicts with global & local
1231 // variables.
Chris Lattner34538142002-03-08 19:11:42 +00001232 //
Chris Lattner79df7c02002-03-26 18:01:55 +00001233 CurModule.CurrentModule->getFunctionList().remove(M);
Chris Lattnere1815642001-07-15 06:35:53 +00001234 }
1235 }
1236
1237 if (M == 0) { // Not already defined?
Chris Lattner79df7c02002-03-26 18:01:55 +00001238 M = new Function(MT, $1, FunctionName);
Chris Lattnere1815642001-07-15 06:35:53 +00001239 InsertValue(M, CurModule.Values);
Chris Lattnerdda71962001-11-26 18:54:16 +00001240 CurModule.DeclareNewGlobalValue(M, ValID::create($3));
Chris Lattnere1815642001-07-15 06:35:53 +00001241 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001242 free($3); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +00001243
Chris Lattner79df7c02002-03-26 18:01:55 +00001244 CurMeth.FunctionStart(M);
Chris Lattner00950542001-06-06 20:29:01 +00001245
Chris Lattner7e708292002-06-25 16:13:24 +00001246 // Add all of the arguments we parsed to the function...
Chris Lattnerdda71962001-11-26 18:54:16 +00001247 if ($5 && !CurMeth.isDeclare) { // Is null if empty...
Chris Lattner46748042002-04-09 19:41:42 +00001248 for (list<pair<Argument*, char*> >::iterator I = $5->begin();
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001249 I != $5->end(); ++I) {
1250 if (setValueName(I->first, I->second)) { // Insert into symtab...
1251 assert(0 && "No arg redef allowed!");
1252 }
1253
1254 InsertValue(I->first);
Chris Lattner7e708292002-06-25 16:13:24 +00001255 M->getArgumentList().push_back(I->first);
Chris Lattner00950542001-06-06 20:29:01 +00001256 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001257 delete $5; // We're now done with the argument list
Chris Lattner9176fe42002-03-08 18:57:56 +00001258 } else if ($5) {
1259 // If we are a declaration, we should free the memory for the argument list!
Chris Lattner46748042002-04-09 19:41:42 +00001260 for (list<pair<Argument*, char*> >::iterator I = $5->begin(), E = $5->end();
1261 I != E; ++I) {
Chris Lattner9176fe42002-03-08 18:57:56 +00001262 if (I->second) free(I->second); // Free the memory for the name...
Chris Lattner09c07532002-03-31 07:16:49 +00001263 delete I->first; // Free the unused function argument
1264 }
Chris Lattner9176fe42002-03-08 18:57:56 +00001265 delete $5; // Free the memory for the list itself
Chris Lattner00950542001-06-06 20:29:01 +00001266 }
Chris Lattner51727be2002-06-04 21:58:56 +00001267};
Chris Lattner00950542001-06-06 20:29:01 +00001268
Chris Lattner9b02cc32002-05-03 18:23:48 +00001269BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1270
1271FunctionHeader : FunctionHeaderH BEGIN {
Chris Lattner79df7c02002-03-26 18:01:55 +00001272 $$ = CurMeth.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001273
Chris Lattner7e708292002-06-25 16:13:24 +00001274 // Resolve circular types before we parse the body of the function.
Chris Lattner30c89792001-09-07 16:35:17 +00001275 ResolveTypes(CurMeth.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001276};
Chris Lattner00950542001-06-06 20:29:01 +00001277
Chris Lattner9b02cc32002-05-03 18:23:48 +00001278END : ENDTOK | '}'; // Allow end of '}' to end a function
1279
Chris Lattner79df7c02002-03-26 18:01:55 +00001280Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001281 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001282};
Chris Lattner00950542001-06-06 20:29:01 +00001283
Chris Lattner79df7c02002-03-26 18:01:55 +00001284FunctionProto : DECLARE { CurMeth.isDeclare = true; } FunctionHeaderH {
1285 $$ = CurMeth.CurrentFunction;
1286 assert($$->getParent() == 0 && "Function already in module!");
1287 CurModule.CurrentModule->getFunctionList().push_back($$);
1288 CurMeth.FunctionDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001289};
Chris Lattner00950542001-06-06 20:29:01 +00001290
1291//===----------------------------------------------------------------------===//
1292// Rules to match Basic Blocks
1293//===----------------------------------------------------------------------===//
1294
1295ConstValueRef : ESINT64VAL { // A reference to a direct constant
1296 $$ = ValID::create($1);
1297 }
1298 | EUINT64VAL {
1299 $$ = ValID::create($1);
1300 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001301 | FPVAL { // Perhaps it's an FP constant?
1302 $$ = ValID::create($1);
1303 }
Chris Lattner00950542001-06-06 20:29:01 +00001304 | TRUE {
1305 $$ = ValID::create((int64_t)1);
1306 }
1307 | FALSE {
1308 $$ = ValID::create((int64_t)0);
1309 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001310 | NULL_TOK {
1311 $$ = ValID::createNull();
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001312 }
1313 ;
Chris Lattner1a1cb112001-09-30 22:46:54 +00001314
Chris Lattner2079fde2001-10-13 06:41:08 +00001315// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1316// another value.
1317//
1318SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001319 $$ = ValID::create($1);
1320 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001321 | VAR_ID { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001322 $$ = ValID::create($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001323 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001324
1325// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00001326ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00001327
Chris Lattner00950542001-06-06 20:29:01 +00001328
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001329// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1330// type immediately preceeds the value reference, and allows complex constant
1331// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001332ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001333 $$ = getVal(*$1, $2); delete $1;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001334 }
1335 | ConstExpr {
1336 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001337 };
Chris Lattner8b81bf52001-07-25 22:47:46 +00001338
Chris Lattner00950542001-06-06 20:29:01 +00001339BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner7e708292002-06-25 16:13:24 +00001340 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner00950542001-06-06 20:29:01 +00001341 }
Chris Lattner7e708292002-06-25 16:13:24 +00001342 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
1343 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner51727be2002-06-04 21:58:56 +00001344 };
Chris Lattner00950542001-06-06 20:29:01 +00001345
1346
1347// Basic blocks are terminated by branching instructions:
1348// br, br/cc, switch, ret
1349//
Chris Lattner2079fde2001-10-13 06:41:08 +00001350BasicBlock : InstructionList OptAssign BBTerminatorInst {
1351 if (setValueName($3, $2)) { assert(0 && "No redefn allowed!"); }
1352 InsertValue($3);
1353
1354 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001355 InsertValue($1);
1356 $$ = $1;
1357 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001358 | LABELSTR InstructionList OptAssign BBTerminatorInst {
1359 if (setValueName($4, $3)) { assert(0 && "No redefn allowed!"); }
1360 InsertValue($4);
1361
1362 $2->getInstList().push_back($4);
Chris Lattnerb7474512001-10-03 15:39:04 +00001363 if (setValueName($2, $1)) { assert(0 && "No label redef allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001364
1365 InsertValue($2);
1366 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001367 };
Chris Lattner00950542001-06-06 20:29:01 +00001368
1369InstructionList : InstructionList Inst {
1370 $1->getInstList().push_back($2);
1371 $$ = $1;
1372 }
1373 | /* empty */ {
1374 $$ = new BasicBlock();
Chris Lattner51727be2002-06-04 21:58:56 +00001375 };
Chris Lattner00950542001-06-06 20:29:01 +00001376
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001377BBTerminatorInst : RET ResolvedVal { // Return with a result...
1378 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001379 }
1380 | RET VOID { // Return with no result...
1381 $$ = new ReturnInst();
1382 }
1383 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner9636a912001-10-01 16:18:37 +00001384 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $3)));
Chris Lattner00950542001-06-06 20:29:01 +00001385 } // Conditional Branch...
1386 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner9636a912001-10-01 16:18:37 +00001387 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $6)),
1388 cast<BasicBlock>(getVal(Type::LabelTy, $9)),
Chris Lattner00950542001-06-06 20:29:01 +00001389 getVal(Type::BoolTy, $3));
1390 }
1391 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1392 SwitchInst *S = new SwitchInst(getVal($2, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001393 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
Chris Lattner00950542001-06-06 20:29:01 +00001394 $$ = S;
1395
Chris Lattner46748042002-04-09 19:41:42 +00001396 vector<pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
1397 E = $8->end();
1398 for (; I != E; ++I)
Chris Lattner00950542001-06-06 20:29:01 +00001399 S->dest_push_back(I->first, I->second);
1400 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001401 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO ResolvedVal
1402 EXCEPT ResolvedVal {
1403 const PointerType *PMTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001404 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00001405
1406 if (!(PMTy = dyn_cast<PointerType>($2->get())) ||
Chris Lattner79df7c02002-03-26 18:01:55 +00001407 !(Ty = dyn_cast<FunctionType>(PMTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00001408 // Pull out the types of all of the arguments...
1409 vector<const Type*> ParamTypes;
1410 if ($5) {
Chris Lattner6cdb0112001-11-26 16:54:11 +00001411 for (vector<Value*>::iterator I = $5->begin(), E = $5->end(); I!=E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00001412 ParamTypes.push_back((*I)->getType());
1413 }
1414
1415 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1416 if (isVarArg) ParamTypes.pop_back();
1417
Chris Lattner79df7c02002-03-26 18:01:55 +00001418 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattner2079fde2001-10-13 06:41:08 +00001419 PMTy = PointerType::get(Ty);
1420 }
1421 delete $2;
1422
Chris Lattner7e708292002-06-25 16:13:24 +00001423 Value *V = getVal(PMTy, $3); // Get the function we're calling...
Chris Lattner2079fde2001-10-13 06:41:08 +00001424
1425 BasicBlock *Normal = dyn_cast<BasicBlock>($8);
1426 BasicBlock *Except = dyn_cast<BasicBlock>($10);
1427
1428 if (Normal == 0 || Except == 0)
1429 ThrowException("Invoke instruction without label destinations!");
1430
1431 // Create the call node...
1432 if (!$5) { // Has no arguments?
Chris Lattner386a3b72001-10-16 19:54:17 +00001433 $$ = new InvokeInst(V, Normal, Except, vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001434 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001435 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00001436 // correctly!
1437 //
Chris Lattner79df7c02002-03-26 18:01:55 +00001438 FunctionType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1439 FunctionType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
Chris Lattner6cdb0112001-11-26 16:54:11 +00001440 vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00001441
1442 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1443 if ((*ArgI)->getType() != *I)
1444 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001445 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001446
1447 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1448 ThrowException("Invalid number of parameters detected!");
1449
Chris Lattner6cdb0112001-11-26 16:54:11 +00001450 $$ = new InvokeInst(V, Normal, Except, *$5);
Chris Lattner2079fde2001-10-13 06:41:08 +00001451 }
1452 delete $5;
Chris Lattner51727be2002-06-04 21:58:56 +00001453 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001454
1455
Chris Lattner00950542001-06-06 20:29:01 +00001456
1457JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1458 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001459 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001460 if (V == 0)
1461 ThrowException("May only switch on a constant pool value!");
1462
Chris Lattner9636a912001-10-01 16:18:37 +00001463 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($5, $6))));
Chris Lattner00950542001-06-06 20:29:01 +00001464 }
1465 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner46748042002-04-09 19:41:42 +00001466 $$ = new vector<pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001467 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00001468
1469 if (V == 0)
1470 ThrowException("May only switch on a constant pool value!");
1471
Chris Lattner9636a912001-10-01 16:18:37 +00001472 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($4, $5))));
Chris Lattner51727be2002-06-04 21:58:56 +00001473 };
Chris Lattner00950542001-06-06 20:29:01 +00001474
1475Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001476 // Is this definition named?? if so, assign the name...
1477 if (setValueName($2, $1)) { assert(0 && "No redefin allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001478 InsertValue($2);
1479 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001480};
Chris Lattner00950542001-06-06 20:29:01 +00001481
Chris Lattnerc24d2082001-06-11 15:04:20 +00001482PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
1483 $$ = new list<pair<Value*, BasicBlock*> >();
Chris Lattner30c89792001-09-07 16:35:17 +00001484 $$->push_back(make_pair(getVal(*$1, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001485 cast<BasicBlock>(getVal(Type::LabelTy, $5))));
Chris Lattner30c89792001-09-07 16:35:17 +00001486 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001487 }
1488 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1489 $$ = $1;
1490 $1->push_back(make_pair(getVal($1->front().first->getType(), $4),
Chris Lattner9636a912001-10-01 16:18:37 +00001491 cast<BasicBlock>(getVal(Type::LabelTy, $6))));
Chris Lattner51727be2002-06-04 21:58:56 +00001492 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00001493
1494
Chris Lattner30c89792001-09-07 16:35:17 +00001495ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner6cdb0112001-11-26 16:54:11 +00001496 $$ = new vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001497 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001498 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001499 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001500 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001501 $1->push_back($3);
Chris Lattner51727be2002-06-04 21:58:56 +00001502 };
Chris Lattner00950542001-06-06 20:29:01 +00001503
1504// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner51727be2002-06-04 21:58:56 +00001505ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001506
1507InstVal : BinaryOps Types ValueRef ',' ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001508 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001509 if ($$ == 0)
1510 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001511 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001512 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001513 | UnaryOps ResolvedVal {
1514 $$ = UnaryOperator::create($1, $2);
Chris Lattner00950542001-06-06 20:29:01 +00001515 if ($$ == 0)
1516 ThrowException("unary operator returned null!");
Chris Lattner09083092001-07-08 04:57:15 +00001517 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001518 | ShiftOps ResolvedVal ',' ResolvedVal {
1519 if ($4->getType() != Type::UByteTy)
1520 ThrowException("Shift amount must be ubyte!");
1521 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001522 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001523 | CAST ResolvedVal TO Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001524 $$ = new CastInst($2, *$4);
1525 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001526 }
Chris Lattnerc24d2082001-06-11 15:04:20 +00001527 | PHI PHIList {
1528 const Type *Ty = $2->front().first->getType();
1529 $$ = new PHINode(Ty);
Chris Lattner00950542001-06-06 20:29:01 +00001530 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001531 if ($2->front().first->getType() != Ty)
1532 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001533 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001534 $2->pop_front();
1535 }
1536 delete $2; // Free the list...
1537 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001538 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattneref9c23f2001-10-03 14:53:21 +00001539 const PointerType *PMTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001540 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001541
Chris Lattneref9c23f2001-10-03 14:53:21 +00001542 if (!(PMTy = dyn_cast<PointerType>($2->get())) ||
Chris Lattner79df7c02002-03-26 18:01:55 +00001543 !(Ty = dyn_cast<FunctionType>(PMTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001544 // Pull out the types of all of the arguments...
1545 vector<const Type*> ParamTypes;
Chris Lattneref9c23f2001-10-03 14:53:21 +00001546 if ($5) {
Chris Lattner6cdb0112001-11-26 16:54:11 +00001547 for (vector<Value*>::iterator I = $5->begin(), E = $5->end(); I!=E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00001548 ParamTypes.push_back((*I)->getType());
1549 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001550
1551 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1552 if (isVarArg) ParamTypes.pop_back();
1553
Chris Lattner79df7c02002-03-26 18:01:55 +00001554 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattneref9c23f2001-10-03 14:53:21 +00001555 PMTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001556 }
Chris Lattner30c89792001-09-07 16:35:17 +00001557 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001558
Chris Lattner7e708292002-06-25 16:13:24 +00001559 Value *V = getVal(PMTy, $3); // Get the function we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001560
Chris Lattner8b81bf52001-07-25 22:47:46 +00001561 // Create the call node...
1562 if (!$5) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00001563 // Make sure no arguments is a good thing!
1564 if (Ty->getNumParams() != 0)
1565 ThrowException("No arguments passed to a function that "
1566 "expects arguments!");
1567
Chris Lattner386a3b72001-10-16 19:54:17 +00001568 $$ = new CallInst(V, vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001569 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001570 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00001571 // correctly!
1572 //
Chris Lattner79df7c02002-03-26 18:01:55 +00001573 FunctionType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1574 FunctionType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
Chris Lattner6cdb0112001-11-26 16:54:11 +00001575 vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001576
1577 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1578 if ((*ArgI)->getType() != *I)
1579 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001580 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001581
Chris Lattner8b81bf52001-07-25 22:47:46 +00001582 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner00950542001-06-06 20:29:01 +00001583 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00001584
Chris Lattner6cdb0112001-11-26 16:54:11 +00001585 $$ = new CallInst(V, *$5);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001586 }
1587 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00001588 }
1589 | MemoryInst {
1590 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001591 };
Chris Lattner00950542001-06-06 20:29:01 +00001592
Chris Lattner6cdb0112001-11-26 16:54:11 +00001593
1594// IndexList - List of indices for GEP based instructions...
1595IndexList : ',' ValueRefList {
Chris Lattner027dcc52001-07-08 21:10:27 +00001596 $$ = $2;
1597} | /* empty */ {
Chris Lattner6cdb0112001-11-26 16:54:11 +00001598 $$ = new vector<Value*>();
Chris Lattner51727be2002-06-04 21:58:56 +00001599};
Chris Lattner027dcc52001-07-08 21:10:27 +00001600
Chris Lattner00950542001-06-06 20:29:01 +00001601MemoryInst : MALLOC Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001602 $$ = new MallocInst(PointerType::get(*$2));
1603 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001604 }
1605 | MALLOC Types ',' UINT ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001606 const Type *Ty = PointerType::get(*$2);
Chris Lattner8896eda2001-07-09 19:38:36 +00001607 $$ = new MallocInst(Ty, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001608 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001609 }
1610 | ALLOCA Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001611 $$ = new AllocaInst(PointerType::get(*$2));
1612 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001613 }
1614 | ALLOCA Types ',' UINT ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001615 const Type *Ty = PointerType::get(*$2);
Chris Lattner00950542001-06-06 20:29:01 +00001616 Value *ArrSize = getVal($4, $5);
Chris Lattnerf0d0e9c2001-07-07 08:36:30 +00001617 $$ = new AllocaInst(Ty, ArrSize);
Chris Lattner30c89792001-09-07 16:35:17 +00001618 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001619 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001620 | FREE ResolvedVal {
Chris Lattner9b625032002-05-06 16:15:30 +00001621 if (!isa<PointerType>($2->getType()))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001622 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00001623 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001624 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001625 }
1626
Chris Lattner6cdb0112001-11-26 16:54:11 +00001627 | LOAD Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00001628 if (!isa<PointerType>($2->get()))
Chris Lattner2079fde2001-10-13 06:41:08 +00001629 ThrowException("Can't load from nonpointer type: " +
1630 (*$2)->getDescription());
Chris Lattner30c89792001-09-07 16:35:17 +00001631 if (LoadInst::getIndexedType(*$2, *$4) == 0)
Chris Lattner027dcc52001-07-08 21:10:27 +00001632 ThrowException("Invalid indices for load instruction!");
1633
Chris Lattner30c89792001-09-07 16:35:17 +00001634 $$ = new LoadInst(getVal(*$2, $3), *$4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001635 delete $4; // Free the vector...
Chris Lattner30c89792001-09-07 16:35:17 +00001636 delete $2;
Chris Lattner027dcc52001-07-08 21:10:27 +00001637 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00001638 | STORE ResolvedVal ',' Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00001639 if (!isa<PointerType>($4->get()))
Chris Lattner72e00252001-12-14 16:28:42 +00001640 ThrowException("Can't store to a nonpointer type: " +
1641 (*$4)->getDescription());
Chris Lattner30c89792001-09-07 16:35:17 +00001642 const Type *ElTy = StoreInst::getIndexedType(*$4, *$6);
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001643 if (ElTy == 0)
1644 ThrowException("Can't store into that field list!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001645 if (ElTy != $2->getType())
Chris Lattner72e00252001-12-14 16:28:42 +00001646 ThrowException("Can't store '" + $2->getType()->getDescription() +
1647 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001648 $$ = new StoreInst($2, getVal(*$4, $5), *$6);
1649 delete $4; delete $6;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001650 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00001651 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00001652 if (!isa<PointerType>($2->get()))
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001653 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner30c89792001-09-07 16:35:17 +00001654 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner72e00252001-12-14 16:28:42 +00001655 ThrowException("Can't get element ptr '" + (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001656 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
1657 delete $2; delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00001658 };
Chris Lattner027dcc52001-07-08 21:10:27 +00001659
Chris Lattner00950542001-06-06 20:29:01 +00001660%%
Chris Lattner09083092001-07-08 04:57:15 +00001661int yyerror(const char *ErrorMsg) {
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001662 string where = string((CurFilename == "-")? string("<stdin>") : CurFilename)
1663 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
1664 string errMsg = string(ErrorMsg) + string("\n") + where + " while reading ";
1665 if (yychar == YYEMPTY)
1666 errMsg += "end-of-file.";
1667 else
1668 errMsg += "token: '" + string(llvmAsmtext, llvmAsmleng) + "'";
1669 ThrowException(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00001670 return 0;
1671}