blob: 28562ac0b1e9ea653059882a6b2655691a950032 [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
7//
8// TODO: Parse comments and add them to an internal node... so that they may
9// be saved in the bytecode format as well as everything else. Very important
10// for a general IR format.
11//
12
13%{
14#include "ParserInternals.h"
Chris Lattner70cc3392001-09-10 07:58:01 +000015#include "llvm/Assembly/Parser.h"
Chris Lattner00950542001-06-06 20:29:01 +000016#include "llvm/SymbolTable.h"
17#include "llvm/Module.h"
Chris Lattner70cc3392001-09-10 07:58:01 +000018#include "llvm/GlobalVariable.h"
19#include "llvm/Method.h"
20#include "llvm/BasicBlock.h"
Chris Lattner00950542001-06-06 20:29:01 +000021#include "llvm/DerivedTypes.h"
Chris Lattner00950542001-06-06 20:29:01 +000022#include "llvm/iTerminators.h"
23#include "llvm/iMemory.h"
Chris Lattner30c89792001-09-07 16:35:17 +000024#include "llvm/Support/STLExtras.h"
Chris Lattner3ff43872001-09-28 22:56:31 +000025#include "llvm/Support/DepthFirstIterator.h"
Chris Lattner00950542001-06-06 20:29:01 +000026#include <list>
27#include <utility> // Get definition of pair class
Chris Lattner30c89792001-09-07 16:35:17 +000028#include <algorithm>
Chris Lattner00950542001-06-06 20:29:01 +000029#include <stdio.h> // This embarasment is due to our flex lexer...
30
Chris Lattner386a3b72001-10-16 19:54:17 +000031int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
Chris Lattner09083092001-07-08 04:57:15 +000032int yylex(); // declaration" of xxx warnings.
Chris Lattner00950542001-06-06 20:29:01 +000033int yyparse();
34
35static Module *ParserResult;
Chris Lattnera2850432001-07-22 18:36:00 +000036string CurFilename;
Chris Lattner00950542001-06-06 20:29:01 +000037
Chris Lattner30c89792001-09-07 16:35:17 +000038// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
39// relating to upreferences in the input stream.
40//
41//#define DEBUG_UPREFS 1
42#ifdef DEBUG_UPREFS
43#define UR_OUT(X) cerr << X
44#else
45#define UR_OUT(X)
46#endif
47
Chris Lattner00950542001-06-06 20:29:01 +000048// This contains info used when building the body of a method. It is destroyed
49// when the method is completed.
50//
51typedef vector<Value *> ValueList; // Numbered defs
Chris Lattner386a3b72001-10-16 19:54:17 +000052static void ResolveDefinitions(vector<ValueList> &LateResolvers,
53 vector<ValueList> *FutureLateResolvers = 0);
Chris Lattner30c89792001-09-07 16:35:17 +000054static void ResolveTypes (vector<PATypeHolder<Type> > &LateResolveTypes);
Chris Lattner00950542001-06-06 20:29:01 +000055
56static struct PerModuleInfo {
57 Module *CurrentModule;
Chris Lattner30c89792001-09-07 16:35:17 +000058 vector<ValueList> Values; // Module level numbered definitions
59 vector<ValueList> LateResolveValues;
60 vector<PATypeHolder<Type> > Types, LateResolveTypes;
Chris Lattner00950542001-06-06 20:29:01 +000061
Chris Lattner2079fde2001-10-13 06:41:08 +000062 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
63 // references to global values. Global values may be referenced before they
64 // are defined, and if so, the temporary object that they represent is held
Chris Lattnerc18545d2001-10-15 13:21:42 +000065 // here. This is used for forward references of ConstPoolPointerRefs.
Chris Lattner2079fde2001-10-13 06:41:08 +000066 //
67 typedef map<pair<const PointerType *, ValID>, GlobalVariable*> GlobalRefsType;
68 GlobalRefsType GlobalRefs;
69
Chris Lattner00950542001-06-06 20:29:01 +000070 void ModuleDone() {
Chris Lattner30c89792001-09-07 16:35:17 +000071 // If we could not resolve some methods at method compilation time (calls to
72 // methods before they are defined), resolve them now... Types are resolved
73 // when the constant pool has been completely parsed.
74 //
Chris Lattner00950542001-06-06 20:29:01 +000075 ResolveDefinitions(LateResolveValues);
76
Chris Lattner2079fde2001-10-13 06:41:08 +000077 // Check to make sure that all global value forward references have been
78 // resolved!
79 //
80 if (!GlobalRefs.empty()) {
81 // TODO: Make this more detailed! Loop over each undef value and print
82 // info
83 ThrowException("TODO: Make better error - Unresolved forward constant references exist!");
84 }
85
Chris Lattner00950542001-06-06 20:29:01 +000086 Values.clear(); // Clear out method local definitions
Chris Lattner30c89792001-09-07 16:35:17 +000087 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +000088 CurrentModule = 0;
89 }
Chris Lattner2079fde2001-10-13 06:41:08 +000090
91
92 // DeclareNewGlobalValue - Called every type a new GV has been defined. This
93 // is used to remove things from the forward declaration map, resolving them
94 // to the correct thing as needed.
95 //
96 void DeclareNewGlobalValue(GlobalValue *GV, ValID D) {
97 // Check to see if there is a forward reference to this global variable...
98 // if there is, eliminate it and patch the reference to use the new def'n.
99 GlobalRefsType::iterator I = GlobalRefs.find(make_pair(GV->getType(), D));
100
101 if (I != GlobalRefs.end()) {
102 GlobalVariable *OldGV = I->second; // Get the placeholder...
103 I->first.second.destroy(); // Free string memory if neccesary
104
105 // Loop over all of the uses of the GlobalValue. The only thing they are
Chris Lattnerc18545d2001-10-15 13:21:42 +0000106 // allowed to be at this point is ConstPoolPointerRef's.
Chris Lattner2079fde2001-10-13 06:41:08 +0000107 assert(OldGV->use_size() == 1 && "Only one reference should exist!");
108 while (!OldGV->use_empty()) {
Chris Lattnerc18545d2001-10-15 13:21:42 +0000109 User *U = OldGV->use_back(); // Must be a ConstPoolPointerRef...
110 ConstPoolPointerRef *CPPR = cast<ConstPoolPointerRef>(U);
Chris Lattner2079fde2001-10-13 06:41:08 +0000111 assert(CPPR->getValue() == OldGV && "Something isn't happy");
112
113 // Change the const pool reference to point to the real global variable
114 // now. This should drop a use from the OldGV.
115 CPPR->mutateReference(GV);
116 }
117
118 // Remove GV from the module...
119 CurrentModule->getGlobalList().remove(OldGV);
120 delete OldGV; // Delete the old placeholder
121
122 // Remove the map entry for the global now that it has been created...
123 GlobalRefs.erase(I);
124 }
125 }
126
Chris Lattner00950542001-06-06 20:29:01 +0000127} CurModule;
128
129static struct PerMethodInfo {
130 Method *CurrentMethod; // Pointer to current method being created
131
Chris Lattnere1815642001-07-15 06:35:53 +0000132 vector<ValueList> Values; // Keep track of numbered definitions
Chris Lattner00950542001-06-06 20:29:01 +0000133 vector<ValueList> LateResolveValues;
Chris Lattner30c89792001-09-07 16:35:17 +0000134 vector<PATypeHolder<Type> > Types, LateResolveTypes;
Chris Lattnere1815642001-07-15 06:35:53 +0000135 bool isDeclare; // Is this method a forward declararation?
Chris Lattner00950542001-06-06 20:29:01 +0000136
137 inline PerMethodInfo() {
138 CurrentMethod = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000139 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000140 }
141
142 inline ~PerMethodInfo() {}
143
144 inline void MethodStart(Method *M) {
145 CurrentMethod = M;
146 }
147
148 void MethodDone() {
149 // If we could not resolve some blocks at parsing time (forward branches)
150 // resolve the branches now...
Chris Lattner386a3b72001-10-16 19:54:17 +0000151 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000152
153 Values.clear(); // Clear out method local definitions
Chris Lattner30c89792001-09-07 16:35:17 +0000154 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +0000155 CurrentMethod = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000156 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000157 }
158} CurMeth; // Info for the current method...
159
Chris Lattnerb7474512001-10-03 15:39:04 +0000160static bool inMethodScope() { return CurMeth.CurrentMethod != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000161
Chris Lattner00950542001-06-06 20:29:01 +0000162
163//===----------------------------------------------------------------------===//
164// Code to handle definitions of all the types
165//===----------------------------------------------------------------------===//
166
Chris Lattner2079fde2001-10-13 06:41:08 +0000167static int InsertValue(Value *D, vector<ValueList> &ValueTab = CurMeth.Values) {
168 if (D->hasName()) return -1; // Is this a numbered definition?
169
170 // Yes, insert the value into the value table...
171 unsigned type = D->getType()->getUniqueID();
172 if (ValueTab.size() <= type)
173 ValueTab.resize(type+1, ValueList());
174 //printf("Values[%d][%d] = %d\n", type, ValueTab[type].size(), D);
175 ValueTab[type].push_back(D);
176 return ValueTab[type].size()-1;
Chris Lattner00950542001-06-06 20:29:01 +0000177}
178
Chris Lattner30c89792001-09-07 16:35:17 +0000179// TODO: FIXME when Type are not const
180static void InsertType(const Type *Ty, vector<PATypeHolder<Type> > &Types) {
181 Types.push_back(Ty);
182}
183
184static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000185 switch (D.Type) {
186 case 0: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000187 unsigned Num = (unsigned)D.Num;
188
189 // Module constants occupy the lowest numbered slots...
190 if (Num < CurModule.Types.size())
191 return CurModule.Types[Num];
192
193 Num -= CurModule.Types.size();
194
195 // Check that the number is within bounds...
196 if (Num <= CurMeth.Types.size())
197 return CurMeth.Types[Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000198 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000199 }
200 case 1: { // Is it a named definition?
201 string Name(D.Name);
202 SymbolTable *SymTab = 0;
Chris Lattnerb7474512001-10-03 15:39:04 +0000203 if (inMethodScope()) SymTab = CurMeth.CurrentMethod->getSymbolTable();
Chris Lattner30c89792001-09-07 16:35:17 +0000204 Value *N = SymTab ? SymTab->lookup(Type::TypeTy, Name) : 0;
205
206 if (N == 0) {
207 // Symbol table doesn't automatically chain yet... because the method
208 // hasn't been added to the module...
209 //
210 SymTab = CurModule.CurrentModule->getSymbolTable();
211 if (SymTab)
212 N = SymTab->lookup(Type::TypeTy, Name);
213 if (N == 0) break;
214 }
215
216 D.destroy(); // Free old strdup'd memory...
Chris Lattnercfe26c92001-10-01 18:26:53 +0000217 return cast<const Type>(N);
Chris Lattner30c89792001-09-07 16:35:17 +0000218 }
219 default:
220 ThrowException("Invalid symbol type reference!");
221 }
222
223 // If we reached here, we referenced either a symbol that we don't know about
224 // or an id number that hasn't been read yet. We may be referencing something
225 // forward, so just create an entry to be resolved later and get to it...
226 //
227 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
228
Chris Lattnerb7474512001-10-03 15:39:04 +0000229 vector<PATypeHolder<Type> > *LateResolver = inMethodScope() ?
Chris Lattner30c89792001-09-07 16:35:17 +0000230 &CurMeth.LateResolveTypes : &CurModule.LateResolveTypes;
231
232 Type *Typ = new TypePlaceHolder(Type::TypeTy, D);
233 InsertType(Typ, *LateResolver);
234 return Typ;
235}
236
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000237static Value *lookupInSymbolTable(const Type *Ty, const string &Name) {
238 SymbolTable *SymTab =
Chris Lattnerb7474512001-10-03 15:39:04 +0000239 inMethodScope() ? CurMeth.CurrentMethod->getSymbolTable() : 0;
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000240 Value *N = SymTab ? SymTab->lookup(Ty, Name) : 0;
241
242 if (N == 0) {
243 // Symbol table doesn't automatically chain yet... because the method
244 // hasn't been added to the module...
245 //
246 SymTab = CurModule.CurrentModule->getSymbolTable();
247 if (SymTab)
248 N = SymTab->lookup(Ty, Name);
249 }
250
251 return N;
252}
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 Lattner386a3b72001-10-16 19:54:17 +0000259 if (isa<MethodType>(Ty))
260 ThrowException("Methods are not values and must be referenced as pointers");
261
Chris Lattner30c89792001-09-07 16:35:17 +0000262 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000263 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000264 unsigned type = Ty->getUniqueID();
Chris Lattner00950542001-06-06 20:29:01 +0000265 unsigned Num = (unsigned)D.Num;
266
267 // Module constants occupy the lowest numbered slots...
268 if (type < CurModule.Values.size()) {
269 if (Num < CurModule.Values[type].size())
270 return CurModule.Values[type][Num];
271
272 Num -= CurModule.Values[type].size();
273 }
274
275 // Make sure that our type is within bounds
Chris Lattner2079fde2001-10-13 06:41:08 +0000276 if (CurMeth.Values.size() <= type) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000277
278 // Check that the number is within bounds...
Chris Lattner2079fde2001-10-13 06:41:08 +0000279 if (CurMeth.Values[type].size() <= Num) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000280
281 return CurMeth.Values[type][Num];
282 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000283
Chris Lattner1a1cb112001-09-30 22:46:54 +0000284 case ValID::NameVal: { // Is it a named definition?
Chris Lattner2079fde2001-10-13 06:41:08 +0000285 Value *N = lookupInSymbolTable(Ty, string(D.Name));
286 if (N == 0) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000287
288 D.destroy(); // Free old strdup'd memory...
289 return N;
290 }
291
Chris Lattner2079fde2001-10-13 06:41:08 +0000292 // Check to make sure that "Ty" is an integral type, and that our
293 // value will fit into the specified type...
294 case ValID::ConstSIntVal: // Is it a constant pool reference??
295 if (Ty == Type::BoolTy) { // Special handling for boolean data
296 return ConstPoolBool::get(D.ConstPool64 != 0);
297 } else {
298 if (!ConstPoolSInt::isValueValidForType(Ty, D.ConstPool64))
299 ThrowException("Symbolic constant pool value '" +
300 itostr(D.ConstPool64) + "' is invalid for type '" +
301 Ty->getName() + "'!");
302 return ConstPoolSInt::get(Ty, D.ConstPool64);
Chris Lattner00950542001-06-06 20:29:01 +0000303 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000304
305 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
306 if (!ConstPoolUInt::isValueValidForType(Ty, D.UConstPool64)) {
307 if (!ConstPoolSInt::isValueValidForType(Ty, D.ConstPool64)) {
308 ThrowException("Integral constant pool reference is invalid!");
309 } else { // This is really a signed reference. Transmogrify.
310 return ConstPoolSInt::get(Ty, D.ConstPool64);
311 }
312 } else {
313 return ConstPoolUInt::get(Ty, D.UConstPool64);
314 }
315
316 case ValID::ConstStringVal: // Is it a string const pool reference?
317 cerr << "FIXME: TODO: String constants [sbyte] not implemented yet!\n";
318 abort();
319 return 0;
320
321 case ValID::ConstFPVal: // Is it a floating point const pool reference?
322 if (!ConstPoolFP::isValueValidForType(Ty, D.ConstPoolFP))
323 ThrowException("FP constant invalid for type!!");
324 return ConstPoolFP::get(Ty, D.ConstPoolFP);
325
326 case ValID::ConstNullVal: // Is it a null value?
327 if (!Ty->isPointerType())
328 ThrowException("Cannot create a a non pointer null!");
329 return ConstPoolPointerNull::get(cast<PointerType>(Ty));
330
Chris Lattner30c89792001-09-07 16:35:17 +0000331 default:
332 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000333 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000334 } // End of switch
335
Chris Lattner2079fde2001-10-13 06:41:08 +0000336 assert(0 && "Unhandled case!");
337 return 0;
338}
339
340
341// getVal - This function is identical to getValNonImprovising, except that if a
342// value is not already defined, it "improvises" by creating a placeholder var
343// that looks and acts just like the requested variable. When the value is
344// defined later, all uses of the placeholder variable are replaced with the
345// real thing.
346//
347static Value *getVal(const Type *Ty, const ValID &D) {
348 assert(Ty != Type::TypeTy && "Should use getTypeVal for types!");
349
350 // See if the value has already been defined...
351 Value *V = getValNonImprovising(Ty, D);
352 if (V) return V;
Chris Lattner00950542001-06-06 20:29:01 +0000353
354 // If we reached here, we referenced either a symbol that we don't know about
355 // or an id number that hasn't been read yet. We may be referencing something
356 // forward, so just create an entry to be resolved later and get to it...
357 //
Chris Lattner00950542001-06-06 20:29:01 +0000358 Value *d = 0;
Chris Lattner30c89792001-09-07 16:35:17 +0000359 switch (Ty->getPrimitiveID()) {
360 case Type::LabelTyID: d = new BBPlaceHolder(Ty, D); break;
Chris Lattner30c89792001-09-07 16:35:17 +0000361 default: d = new ValuePlaceHolder(Ty, D); break;
Chris Lattner00950542001-06-06 20:29:01 +0000362 }
363
364 assert(d != 0 && "How did we not make something?");
Chris Lattner386a3b72001-10-16 19:54:17 +0000365 if (inMethodScope())
366 InsertValue(d, CurMeth.LateResolveValues);
367 else
368 InsertValue(d, CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000369 return d;
370}
371
372
373//===----------------------------------------------------------------------===//
374// Code to handle forward references in instructions
375//===----------------------------------------------------------------------===//
376//
377// This code handles the late binding needed with statements that reference
378// values not defined yet... for example, a forward branch, or the PHI node for
379// a loop body.
380//
381// This keeps a table (CurMeth.LateResolveValues) of all such forward references
382// and back patchs after we are done.
383//
384
385// ResolveDefinitions - If we could not resolve some defs at parsing
386// time (forward branches, phi functions for loops, etc...) resolve the
387// defs now...
388//
Chris Lattner386a3b72001-10-16 19:54:17 +0000389static void ResolveDefinitions(vector<ValueList> &LateResolvers,
390 vector<ValueList> *FutureLateResolvers = 0) {
Chris Lattner00950542001-06-06 20:29:01 +0000391 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
392 for (unsigned ty = 0; ty < LateResolvers.size(); ty++) {
393 while (!LateResolvers[ty].empty()) {
394 Value *V = LateResolvers[ty].back();
Chris Lattner386a3b72001-10-16 19:54:17 +0000395 assert(!isa<Type>(V) && "Types should be in LateResolveTypes!");
396
Chris Lattner00950542001-06-06 20:29:01 +0000397 LateResolvers[ty].pop_back();
398 ValID &DID = getValIDFromPlaceHolder(V);
399
Chris Lattner2079fde2001-10-13 06:41:08 +0000400 Value *TheRealValue = getValNonImprovising(Type::getUniqueIDType(ty),DID);
Chris Lattner386a3b72001-10-16 19:54:17 +0000401 if (TheRealValue) {
402 V->replaceAllUsesWith(TheRealValue);
403 delete V;
404 } else if (FutureLateResolvers) {
405 // Methods have their unresolved items forwarded to the module late
406 // resolver table
407 InsertValue(V, *FutureLateResolvers);
408 } else {
Chris Lattner30c89792001-09-07 16:35:17 +0000409 if (DID.Type == 1)
410 ThrowException("Reference to an invalid definition: '" +DID.getName()+
411 "' of type '" + V->getType()->getDescription() + "'",
412 getLineNumFromPlaceHolder(V));
413 else
414 ThrowException("Reference to an invalid definition: #" +
415 itostr(DID.Num) + " of type '" +
416 V->getType()->getDescription() + "'",
417 getLineNumFromPlaceHolder(V));
418 }
Chris Lattner00950542001-06-06 20:29:01 +0000419 }
420 }
421
422 LateResolvers.clear();
423}
424
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000425// ResolveType - Take a specified unresolved type and resolve it. If there is
426// nothing to resolve it to yet, return true. Otherwise resolve it and return
427// false.
428//
429static bool ResolveType(PATypeHolder<Type> &T) {
430 const Type *Ty = T;
431 ValID &DID = getValIDFromPlaceHolder(Ty);
432
433 const Type *TheRealType = getTypeVal(DID, true);
Chris Lattner23192eb2001-10-21 21:43:25 +0000434 if (TheRealType == 0 || TheRealType == Ty) return true;
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000435
436 // Refine the opaque type we had to the new type we are getting.
437 cast<DerivedType>(Ty)->refineAbstractTypeTo(TheRealType);
438 return false;
439}
440
Chris Lattner30c89792001-09-07 16:35:17 +0000441
442// ResolveTypes - This goes through the forward referenced type table and makes
443// sure that all type references are complete. This code is executed after the
444// constant pool of a method or module is completely parsed.
Chris Lattner00950542001-06-06 20:29:01 +0000445//
Chris Lattner30c89792001-09-07 16:35:17 +0000446static void ResolveTypes(vector<PATypeHolder<Type> > &LateResolveTypes) {
447 while (!LateResolveTypes.empty()) {
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000448 if (ResolveType(LateResolveTypes.back())) {
449 const Type *Ty = LateResolveTypes.back();
450 ValID &DID = getValIDFromPlaceHolder(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000451
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000452 if (DID.Type == ValID::NameVal)
Chris Lattner23192eb2001-10-21 21:43:25 +0000453 ThrowException("Reference to an invalid type: '" +DID.getName() + "'",
Chris Lattner30c89792001-09-07 16:35:17 +0000454 getLineNumFromPlaceHolder(Ty));
455 else
456 ThrowException("Reference to an invalid type: #" + itostr(DID.Num),
457 getLineNumFromPlaceHolder(Ty));
Chris Lattner00950542001-06-06 20:29:01 +0000458 }
Chris Lattner30c89792001-09-07 16:35:17 +0000459
Chris Lattner30c89792001-09-07 16:35:17 +0000460 // No need to delete type, refine does that for us.
461 LateResolveTypes.pop_back();
462 }
463}
464
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000465
466// ResolveSomeTypes - This goes through the forward referenced type table and
467// completes references that are now done. This is so that types are
468// immediately resolved to be as concrete as possible. This does not cause
469// thrown exceptions if not everything is resolved.
470//
471static void ResolveSomeTypes(vector<PATypeHolder<Type> > &LateResolveTypes) {
472 for (unsigned i = 0; i < LateResolveTypes.size(); ) {
473 if (ResolveType(LateResolveTypes[i]))
474 ++i; // Type didn't resolve
475 else
476 LateResolveTypes.erase(LateResolveTypes.begin()+i); // Type resolved!
477 }
478}
479
480
Chris Lattner1781aca2001-09-18 04:00:54 +0000481// setValueName - Set the specified value to the name given. The name may be
482// null potentially, in which case this is a noop. The string passed in is
483// assumed to be a malloc'd string buffer, and is freed by this function.
484//
Chris Lattnerb7474512001-10-03 15:39:04 +0000485// This function returns true if the value has already been defined, but is
486// allowed to be redefined in the specified context. If the name is a new name
487// for the typeplane, false is returned.
488//
489static bool setValueName(Value *V, char *NameStr) {
490 if (NameStr == 0) return false;
Chris Lattner386a3b72001-10-16 19:54:17 +0000491
Chris Lattner1781aca2001-09-18 04:00:54 +0000492 string Name(NameStr); // Copy string
493 free(NameStr); // Free old string
494
Chris Lattner2079fde2001-10-13 06:41:08 +0000495 if (V->getType() == Type::VoidTy)
496 ThrowException("Can't assign name '" + Name +
497 "' to a null valued instruction!");
498
Chris Lattnerb7474512001-10-03 15:39:04 +0000499 SymbolTable *ST = inMethodScope() ?
Chris Lattner30c89792001-09-07 16:35:17 +0000500 CurMeth.CurrentMethod->getSymbolTableSure() :
501 CurModule.CurrentModule->getSymbolTableSure();
502
503 Value *Existing = ST->lookup(V->getType(), Name);
504 if (Existing) { // Inserting a name that is already defined???
505 // There is only one case where this is allowed: when we are refining an
506 // opaque type. In this case, Existing will be an opaque type.
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000507 if (const Type *Ty = dyn_cast<const Type>(Existing)) {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000508 if (OpaqueType *OpTy = dyn_cast<OpaqueType>(Ty)) {
Chris Lattner30c89792001-09-07 16:35:17 +0000509 // We ARE replacing an opaque type!
Chris Lattnerb00c5822001-10-02 03:41:24 +0000510 OpTy->refineAbstractTypeTo(cast<Type>(V));
Chris Lattnerb7474512001-10-03 15:39:04 +0000511 return true;
Chris Lattner30c89792001-09-07 16:35:17 +0000512 }
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000513 }
Chris Lattner30c89792001-09-07 16:35:17 +0000514
Chris Lattner9636a912001-10-01 16:18:37 +0000515 // Otherwise, we are a simple redefinition of a value, check to see if it
516 // is defined the same as the old one...
517 if (const Type *Ty = dyn_cast<const Type>(Existing)) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000518 if (Ty == cast<const Type>(V)) return true; // Yes, it's equal.
519 // cerr << "Type: " << Ty->getDescription() << " != "
520 // << cast<const Type>(V)->getDescription() << "!\n";
521 } else if (GlobalVariable *EGV = dyn_cast<GlobalVariable>(Existing)) {
Chris Lattner43efcbf2001-10-03 19:35:57 +0000522 // We are allowed to redefine a global variable in two circumstances:
523 // 1. If at least one of the globals is uninitialized or
524 // 2. If both initializers have the same value.
525 //
526 // This can only be done if the const'ness of the vars is the same.
527 //
Chris Lattner89219832001-10-03 19:35:04 +0000528 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
529 if (EGV->isConstant() == GV->isConstant() &&
530 (!EGV->hasInitializer() || !GV->hasInitializer() ||
531 EGV->getInitializer() == GV->getInitializer())) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000532
Chris Lattner89219832001-10-03 19:35:04 +0000533 // Make sure the existing global version gets the initializer!
534 if (GV->hasInitializer() && !EGV->hasInitializer())
535 EGV->setInitializer(GV->getInitializer());
536
Chris Lattner2079fde2001-10-13 06:41:08 +0000537 delete GV; // Destroy the duplicate!
Chris Lattner89219832001-10-03 19:35:04 +0000538 return true; // They are equivalent!
539 }
Chris Lattnerb7474512001-10-03 15:39:04 +0000540 }
Chris Lattner9636a912001-10-01 16:18:37 +0000541 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000542 ThrowException("Redefinition of value named '" + Name + "' in the '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000543 V->getType()->getDescription() + "' type plane!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000544 }
Chris Lattner00950542001-06-06 20:29:01 +0000545
Chris Lattner30c89792001-09-07 16:35:17 +0000546 V->setName(Name, ST);
Chris Lattnerb7474512001-10-03 15:39:04 +0000547 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000548}
549
Chris Lattner8896eda2001-07-09 19:38:36 +0000550
Chris Lattner30c89792001-09-07 16:35:17 +0000551//===----------------------------------------------------------------------===//
552// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000553//
Chris Lattner8896eda2001-07-09 19:38:36 +0000554
Chris Lattner30c89792001-09-07 16:35:17 +0000555// TypeContains - Returns true if Ty contains E in it.
556//
557static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner3ff43872001-09-28 22:56:31 +0000558 return find(df_begin(Ty), df_end(Ty), E) != df_end(Ty);
Chris Lattner30c89792001-09-07 16:35:17 +0000559}
Chris Lattner698b56e2001-07-20 19:15:08 +0000560
Chris Lattner30c89792001-09-07 16:35:17 +0000561
562static vector<pair<unsigned, OpaqueType *> > UpRefs;
563
564static PATypeHolder<Type> HandleUpRefs(const Type *ty) {
565 PATypeHolder<Type> Ty(ty);
566 UR_OUT(UpRefs.size() << " upreferences active!\n");
567 for (unsigned i = 0; i < UpRefs.size(); ) {
568 UR_OUT("TypeContains(" << Ty->getDescription() << ", "
569 << UpRefs[i].second->getDescription() << ") = "
570 << TypeContains(Ty, UpRefs[i].second) << endl);
571 if (TypeContains(Ty, UpRefs[i].second)) {
572 unsigned Level = --UpRefs[i].first; // Decrement level of upreference
573 UR_OUT("Uplevel Ref Level = " << Level << endl);
574 if (Level == 0) { // Upreference should be resolved!
575 UR_OUT("About to resolve upreference!\n";
576 string OldName = UpRefs[i].second->getDescription());
577 UpRefs[i].second->refineAbstractTypeTo(Ty);
578 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
579 UR_OUT("Type '" << OldName << "' refined upreference to: "
580 << (const void*)Ty << ", " << Ty->getDescription() << endl);
581 continue;
582 }
583 }
584
585 ++i; // Otherwise, no resolve, move on...
Chris Lattner8896eda2001-07-09 19:38:36 +0000586 }
Chris Lattner30c89792001-09-07 16:35:17 +0000587 // FIXME: TODO: this should return the updated type
Chris Lattner8896eda2001-07-09 19:38:36 +0000588 return Ty;
589}
590
Chris Lattner30c89792001-09-07 16:35:17 +0000591template <class TypeTy>
592inline static void TypeDone(PATypeHolder<TypeTy> *Ty) {
593 if (UpRefs.size())
594 ThrowException("Invalid upreference in type: " + (*Ty)->getDescription());
595}
596
597// newTH - Allocate a new type holder for the specified type
598template <class TypeTy>
599inline static PATypeHolder<TypeTy> *newTH(const TypeTy *Ty) {
600 return new PATypeHolder<TypeTy>(Ty);
601}
602template <class TypeTy>
603inline static PATypeHolder<TypeTy> *newTH(const PATypeHolder<TypeTy> &TH) {
604 return new PATypeHolder<TypeTy>(TH);
605}
606
607
Chris Lattner00950542001-06-06 20:29:01 +0000608//===----------------------------------------------------------------------===//
609// RunVMAsmParser - Define an interface to this parser
610//===----------------------------------------------------------------------===//
611//
Chris Lattnera2850432001-07-22 18:36:00 +0000612Module *RunVMAsmParser(const string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000613 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000614 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000615 llvmAsmlineno = 1; // Reset the current line number...
616
617 CurModule.CurrentModule = new Module(); // Allocate a new module to read
618 yyparse(); // Parse the file.
619 Module *Result = ParserResult;
Chris Lattner00950542001-06-06 20:29:01 +0000620 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
621 ParserResult = 0;
622
623 return Result;
624}
625
626%}
627
628%union {
Chris Lattner30c89792001-09-07 16:35:17 +0000629 Module *ModuleVal;
630 Method *MethodVal;
631 MethodArgument *MethArgVal;
632 BasicBlock *BasicBlockVal;
633 TerminatorInst *TermInstVal;
634 Instruction *InstVal;
635 ConstPoolVal *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000636
Chris Lattner30c89792001-09-07 16:35:17 +0000637 const Type *PrimType;
638 PATypeHolder<Type> *TypeVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000639 Value *ValueVal;
640
641 list<MethodArgument*> *MethodArgList;
642 list<Value*> *ValueList;
643 list<PATypeHolder<Type> > *TypeList;
Chris Lattnerc24d2082001-06-11 15:04:20 +0000644 list<pair<Value*, BasicBlock*> > *PHIList; // Represent the RHS of PHI node
Chris Lattner00950542001-06-06 20:29:01 +0000645 list<pair<ConstPoolVal*, BasicBlock*> > *JumpTable;
Chris Lattner30c89792001-09-07 16:35:17 +0000646 vector<ConstPoolVal*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000647
Chris Lattner30c89792001-09-07 16:35:17 +0000648 int64_t SInt64Val;
649 uint64_t UInt64Val;
650 int SIntVal;
651 unsigned UIntVal;
652 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000653 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000654
Chris Lattner30c89792001-09-07 16:35:17 +0000655 char *StrVal; // This memory is strdup'd!
656 ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000657
Chris Lattner30c89792001-09-07 16:35:17 +0000658 Instruction::UnaryOps UnaryOpVal;
659 Instruction::BinaryOps BinaryOpVal;
660 Instruction::TermOps TermOpVal;
661 Instruction::MemoryOps MemOpVal;
662 Instruction::OtherOps OtherOpVal;
Chris Lattner00950542001-06-06 20:29:01 +0000663}
664
665%type <ModuleVal> Module MethodList
Chris Lattnere1815642001-07-15 06:35:53 +0000666%type <MethodVal> Method MethodProto MethodHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000667%type <BasicBlockVal> BasicBlock InstructionList
668%type <TermInstVal> BBTerminatorInst
669%type <InstVal> Inst InstVal MemoryInst
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000670%type <ConstVal> ConstVal
Chris Lattner027dcc52001-07-08 21:10:27 +0000671%type <ConstVector> ConstVector UByteList
Chris Lattner00950542001-06-06 20:29:01 +0000672%type <MethodArgList> ArgList ArgListH
673%type <MethArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000674%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000675%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner30c89792001-09-07 16:35:17 +0000676%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000677%type <JumpTable> JumpTable
Chris Lattner1781aca2001-09-18 04:00:54 +0000678%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner00950542001-06-06 20:29:01 +0000679
Chris Lattner2079fde2001-10-13 06:41:08 +0000680// ValueRef - Unresolved reference to a definition or BB
681%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000682%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000683// Tokens and types for handling constant integer values
684//
685// ESINT64VAL - A negative number within long long range
686%token <SInt64Val> ESINT64VAL
687
688// EUINT64VAL - A positive number within uns. long long range
689%token <UInt64Val> EUINT64VAL
690%type <SInt64Val> EINT64VAL
691
692%token <SIntVal> SINTVAL // Signed 32 bit ints...
693%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
694%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000695%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000696
697// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000698%type <TypeVal> Types TypesV UpRTypes UpRTypesV
699%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
700%token <TypeVal> OPAQUE
701%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
702%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000703
704%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
705%type <StrVal> OptVAR_ID OptAssign
706
707
Chris Lattner1781aca2001-09-18 04:00:54 +0000708%token IMPLEMENTATION TRUE FALSE BEGINTOK END DECLARE GLOBAL CONSTANT UNINIT
Chris Lattner2079fde2001-10-13 06:41:08 +0000709%token TO EXCEPT DOTDOTDOT STRING NULL_TOK CONST
Chris Lattner00950542001-06-06 20:29:01 +0000710
711// Basic Block Terminating Operators
712%token <TermOpVal> RET BR SWITCH
713
714// Unary Operators
715%type <UnaryOpVal> UnaryOps // all the unary operators
Chris Lattner71496b32001-07-08 19:03:27 +0000716%token <UnaryOpVal> NOT
Chris Lattner00950542001-06-06 20:29:01 +0000717
718// Binary Operators
719%type <BinaryOpVal> BinaryOps // all the binary operators
Chris Lattner42c9e772001-10-20 09:32:59 +0000720%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000721%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000722
723// Memory Instructions
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000724%token <MemoryOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000725
Chris Lattner027dcc52001-07-08 21:10:27 +0000726// Other Operators
727%type <OtherOpVal> ShiftOps
Chris Lattner2079fde2001-10-13 06:41:08 +0000728%token <OtherOpVal> PHI CALL INVOKE CAST SHL SHR
Chris Lattner027dcc52001-07-08 21:10:27 +0000729
Chris Lattner00950542001-06-06 20:29:01 +0000730%start Module
731%%
732
733// Handle constant integer size restriction and conversion...
734//
735
736INTVAL : SINTVAL
737INTVAL : UINTVAL {
738 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
739 ThrowException("Value too large for type!");
740 $$ = (int32_t)$1;
741}
742
743
744EINT64VAL : ESINT64VAL // These have same type and can't cause problems...
745EINT64VAL : EUINT64VAL {
746 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
747 ThrowException("Value too large for type!");
748 $$ = (int64_t)$1;
749}
750
Chris Lattner00950542001-06-06 20:29:01 +0000751// Operations that are notably excluded from this list include:
752// RET, BR, & SWITCH because they end basic blocks and are treated specially.
753//
Chris Lattner09083092001-07-08 04:57:15 +0000754UnaryOps : NOT
Chris Lattner42c9e772001-10-20 09:32:59 +0000755BinaryOps : ADD | SUB | MUL | DIV | REM | AND | OR | XOR
Chris Lattner00950542001-06-06 20:29:01 +0000756BinaryOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE
Chris Lattner027dcc52001-07-08 21:10:27 +0000757ShiftOps : SHL | SHR
Chris Lattner00950542001-06-06 20:29:01 +0000758
Chris Lattnere98dda62001-07-14 06:10:16 +0000759// These are some types that allow classification if we only want a particular
760// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner00950542001-06-06 20:29:01 +0000761SIntType : LONG | INT | SHORT | SBYTE
762UIntType : ULONG | UINT | USHORT | UBYTE
Chris Lattner30c89792001-09-07 16:35:17 +0000763IntType : SIntType | UIntType
764FPType : FLOAT | DOUBLE
Chris Lattner00950542001-06-06 20:29:01 +0000765
Chris Lattnere98dda62001-07-14 06:10:16 +0000766// OptAssign - Value producing statements have an optional assignment component
Chris Lattner00950542001-06-06 20:29:01 +0000767OptAssign : VAR_ID '=' {
768 $$ = $1;
769 }
770 | /*empty*/ {
771 $$ = 0;
772 }
773
Chris Lattner30c89792001-09-07 16:35:17 +0000774
775//===----------------------------------------------------------------------===//
776// Types includes all predefined types... except void, because it can only be
777// used in specific contexts (method returning void for example). To have
778// access to it, a user must explicitly use TypesV.
779//
780
781// TypesV includes all of 'Types', but it also includes the void type.
782TypesV : Types | VOID { $$ = newTH($1); }
783UpRTypesV : UpRTypes | VOID { $$ = newTH($1); }
784
785Types : UpRTypes {
786 TypeDone($$ = $1);
787 }
788
789
790// Derived types are added later...
791//
792PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT
793PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL
794UpRTypes : OPAQUE | PrimType { $$ = newTH($1); }
795UpRTypes : ValueRef { // Named types are also simple types...
796 $$ = newTH(getTypeVal($1));
797}
798
Chris Lattner30c89792001-09-07 16:35:17 +0000799// Include derived types in the Types production.
800//
801UpRTypes : '\\' EUINT64VAL { // Type UpReference
802 if ($2 > (uint64_t)INT64_MAX) ThrowException("Value out of range!");
803 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
804 UpRefs.push_back(make_pair((unsigned)$2, OT)); // Add to vector...
805 $$ = newTH<Type>(OT);
806 UR_OUT("New Upreference!\n");
807 }
808 | UpRTypesV '(' ArgTypeListI ')' { // Method derived type?
809 vector<const Type*> Params;
810 mapto($3->begin(), $3->end(), back_inserter(Params),
811 mem_fun_ref(&PATypeHandle<Type>::get));
Chris Lattner2079fde2001-10-13 06:41:08 +0000812 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
813 if (isVarArg) Params.pop_back();
814
815 $$ = newTH(HandleUpRefs(MethodType::get(*$1, Params, isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +0000816 delete $3; // Delete the argument list
817 delete $1; // Delete the old type handle
818 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000819 | '[' UpRTypesV ']' { // Unsized array type?
820 $$ = newTH<Type>(HandleUpRefs(ArrayType::get(*$2)));
821 delete $2;
Chris Lattner30c89792001-09-07 16:35:17 +0000822 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000823 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
824 $$ = newTH<Type>(HandleUpRefs(ArrayType::get(*$4, (int)$2)));
825 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +0000826 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000827 | '{' TypeListI '}' { // Structure type?
828 vector<const Type*> Elements;
829 mapto($2->begin(), $2->end(), back_inserter(Elements),
830 mem_fun_ref(&PATypeHandle<Type>::get));
Chris Lattner30c89792001-09-07 16:35:17 +0000831
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000832 $$ = newTH<Type>(HandleUpRefs(StructType::get(Elements)));
833 delete $2;
834 }
835 | '{' '}' { // Empty structure type?
836 $$ = newTH<Type>(StructType::get(vector<const Type*>()));
837 }
838 | UpRTypes '*' { // Pointer type?
839 $$ = newTH<Type>(HandleUpRefs(PointerType::get(*$1)));
840 delete $1;
841 }
Chris Lattner30c89792001-09-07 16:35:17 +0000842
843// TypeList - Used for struct declarations and as a basis for method type
844// declaration type lists
845//
846TypeListI : UpRTypes {
847 $$ = new list<PATypeHolder<Type> >();
848 $$->push_back(*$1); delete $1;
849 }
850 | TypeListI ',' UpRTypes {
851 ($$=$1)->push_back(*$3); delete $3;
852 }
853
854// ArgTypeList - List of types for a method type declaration...
855ArgTypeListI : TypeListI
856 | TypeListI ',' DOTDOTDOT {
857 ($$=$1)->push_back(Type::VoidTy);
858 }
859 | DOTDOTDOT {
860 ($$ = new list<PATypeHolder<Type> >())->push_back(Type::VoidTy);
861 }
862 | /*empty*/ {
863 $$ = new list<PATypeHolder<Type> >();
864 }
865
866
Chris Lattnere98dda62001-07-14 06:10:16 +0000867// ConstVal - The various declarations that go into the constant pool. This
868// includes all forward declarations of types, constants, and functions.
869//
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000870ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
871 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
872 if (ATy == 0)
873 ThrowException("Cannot make array constant with type: '" +
874 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +0000875 const Type *ETy = ATy->getElementType();
876 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +0000877
Chris Lattner30c89792001-09-07 16:35:17 +0000878 // Verify that we have the correct size...
879 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +0000880 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattner30c89792001-09-07 16:35:17 +0000881 utostr($3->size()) + " arguments, but has size of " +
882 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +0000883
Chris Lattner30c89792001-09-07 16:35:17 +0000884 // Verify all elements are correct type!
885 for (unsigned i = 0; i < $3->size(); i++) {
886 if (ETy != (*$3)[i]->getType())
Chris Lattner00950542001-06-06 20:29:01 +0000887 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000888 ETy->getName() + "' as required!\nIt is of type '" +
889 (*$3)[i]->getType()->getName() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +0000890 }
891
Chris Lattner30c89792001-09-07 16:35:17 +0000892 $$ = ConstPoolArray::get(ATy, *$3);
893 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000894 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000895 | Types '[' ']' {
896 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
897 if (ATy == 0)
898 ThrowException("Cannot make array constant with type: '" +
899 (*$1)->getDescription() + "'!");
900
901 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +0000902 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +0000903 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattner30c89792001-09-07 16:35:17 +0000904 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000905 $$ = ConstPoolArray::get(ATy, vector<ConstPoolVal*>());
Chris Lattner30c89792001-09-07 16:35:17 +0000906 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +0000907 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000908 | Types 'c' STRINGCONSTANT {
909 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
910 if (ATy == 0)
911 ThrowException("Cannot make array constant with type: '" +
912 (*$1)->getDescription() + "'!");
913
Chris Lattner30c89792001-09-07 16:35:17 +0000914 int NumElements = ATy->getNumElements();
915 const Type *ETy = ATy->getElementType();
916 char *EndStr = UnEscapeLexed($3, true);
917 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +0000918 ThrowException("Can't build string constant of size " +
Chris Lattner30c89792001-09-07 16:35:17 +0000919 itostr((int)(EndStr-$3)) +
920 " when array has size " + itostr(NumElements) + "!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000921 vector<ConstPoolVal*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +0000922 if (ETy == Type::SByteTy) {
923 for (char *C = $3; C != EndStr; ++C)
924 Vals.push_back(ConstPoolSInt::get(ETy, *C));
925 } else if (ETy == Type::UByteTy) {
926 for (char *C = $3; C != EndStr; ++C)
927 Vals.push_back(ConstPoolUInt::get(ETy, *C));
Chris Lattner93750fa2001-07-28 17:48:55 +0000928 } else {
Chris Lattner30c89792001-09-07 16:35:17 +0000929 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +0000930 ThrowException("Cannot build string arrays of non byte sized elements!");
931 }
Chris Lattner30c89792001-09-07 16:35:17 +0000932 free($3);
933 $$ = ConstPoolArray::get(ATy, Vals);
934 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +0000935 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000936 | Types '{' ConstVector '}' {
937 const StructType *STy = dyn_cast<const StructType>($1->get());
938 if (STy == 0)
939 ThrowException("Cannot make struct constant with type: '" +
940 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +0000941 // FIXME: TODO: Check to see that the constants are compatible with the type
942 // initializer!
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000943 $$ = ConstPoolStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +0000944 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000945 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000946 | Types NULL_TOK {
947 const PointerType *PTy = dyn_cast<const PointerType>($1->get());
948 if (PTy == 0)
949 ThrowException("Cannot make null pointer constant with type: '" +
950 (*$1)->getDescription() + "'!");
951
Chris Lattner2079fde2001-10-13 06:41:08 +0000952 $$ = ConstPoolPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000953 delete $1;
954 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000955 | Types SymbolicValueRef {
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000956 const PointerType *Ty = dyn_cast<const PointerType>($1->get());
957 if (Ty == 0)
958 ThrowException("Global const reference must be a pointer type!");
959
Chris Lattner2079fde2001-10-13 06:41:08 +0000960 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000961
Chris Lattner2079fde2001-10-13 06:41:08 +0000962 // If this is an initializer for a constant pointer, which is referencing a
963 // (currently) undefined variable, create a stub now that shall be replaced
964 // in the future with the right type of variable.
965 //
966 if (V == 0) {
967 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
968 const PointerType *PT = cast<PointerType>(Ty);
969
970 // First check to see if the forward references value is already created!
971 PerModuleInfo::GlobalRefsType::iterator I =
972 CurModule.GlobalRefs.find(make_pair(PT, $2));
973
974 if (I != CurModule.GlobalRefs.end()) {
975 V = I->second; // Placeholder already exists, use it...
976 } else {
977 // TODO: Include line number info by creating a subclass of
978 // TODO: GlobalVariable here that includes the said information!
979
980 // Create a placeholder for the global variable reference...
981 GlobalVariable *GV = new GlobalVariable(PT->getValueType(), false);
982 // Keep track of the fact that we have a forward ref to recycle it
983 CurModule.GlobalRefs.insert(make_pair(make_pair(PT, $2), GV));
984
985 // Must temporarily push this value into the module table...
986 CurModule.CurrentModule->getGlobalList().push_back(GV);
987 V = GV;
988 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000989 }
990
Chris Lattner2079fde2001-10-13 06:41:08 +0000991 GlobalValue *GV = cast<GlobalValue>(V);
Chris Lattnerc18545d2001-10-15 13:21:42 +0000992 $$ = ConstPoolPointerRef::get(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +0000993 delete $1; // Free the type handle
Chris Lattner00950542001-06-06 20:29:01 +0000994 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000995
Chris Lattner00950542001-06-06 20:29:01 +0000996
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000997ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000998 if (!ConstPoolSInt::isValueValidForType($1, $2))
999 ThrowException("Constant value doesn't fit in type!");
Chris Lattner30c89792001-09-07 16:35:17 +00001000 $$ = ConstPoolSInt::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001001 }
1002 | UIntType EUINT64VAL { // integral constants
1003 if (!ConstPoolUInt::isValueValidForType($1, $2))
1004 ThrowException("Constant value doesn't fit in type!");
Chris Lattner30c89792001-09-07 16:35:17 +00001005 $$ = ConstPoolUInt::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001006 }
1007 | BOOL TRUE { // Boolean constants
Chris Lattner30c89792001-09-07 16:35:17 +00001008 $$ = ConstPoolBool::True;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001009 }
1010 | BOOL FALSE { // Boolean constants
Chris Lattner30c89792001-09-07 16:35:17 +00001011 $$ = ConstPoolBool::False;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001012 }
1013 | FPType FPVAL { // Float & Double constants
Chris Lattner30c89792001-09-07 16:35:17 +00001014 $$ = ConstPoolFP::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001015 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001016
Chris Lattnere98dda62001-07-14 06:10:16 +00001017// ConstVector - A list of comma seperated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001018ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001019 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001020 }
1021 | ConstVal {
1022 $$ = new vector<ConstPoolVal*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001023 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001024 }
1025
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001026
Chris Lattner1781aca2001-09-18 04:00:54 +00001027// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
1028GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; }
1029
Chris Lattner00950542001-06-06 20:29:01 +00001030
Chris Lattnere98dda62001-07-14 06:10:16 +00001031// ConstPool - Constants with optional names assigned to them.
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001032ConstPool : ConstPool OptAssign CONST ConstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001033 if (setValueName($4, $2)) { assert(0 && "No redefinitions allowed!"); }
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001034 InsertValue($4);
Chris Lattner00950542001-06-06 20:29:01 +00001035 }
Chris Lattner30c89792001-09-07 16:35:17 +00001036 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner1781aca2001-09-18 04:00:54 +00001037 // TODO: FIXME when Type are not const
Chris Lattnerb7474512001-10-03 15:39:04 +00001038 if (!setValueName(const_cast<Type*>($4->get()), $2)) {
1039 // If this is not a redefinition of a type...
1040 if (!$2) {
1041 InsertType($4->get(),
1042 inMethodScope() ? CurMeth.Types : CurModule.Types);
1043 }
1044 delete $4;
Chris Lattner1781aca2001-09-18 04:00:54 +00001045
Chris Lattnerb7474512001-10-03 15:39:04 +00001046 ResolveSomeTypes(inMethodScope() ? CurMeth.LateResolveTypes :
1047 CurModule.LateResolveTypes);
Chris Lattner30c89792001-09-07 16:35:17 +00001048 }
Chris Lattner30c89792001-09-07 16:35:17 +00001049 }
1050 | ConstPool MethodProto { // Method prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001051 }
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001052 | ConstPool OptAssign GlobalType ConstVal {
Chris Lattner1781aca2001-09-18 04:00:54 +00001053 const Type *Ty = $4->getType();
1054 // Global declarations appear in Constant Pool
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001055 ConstPoolVal *Initializer = $4;
Chris Lattner1781aca2001-09-18 04:00:54 +00001056 if (Initializer == 0)
1057 ThrowException("Global value initializer is not a constant!");
1058
Chris Lattneref9c23f2001-10-03 14:53:21 +00001059 GlobalVariable *GV = new GlobalVariable(Ty, $3, Initializer);
Chris Lattnerb7474512001-10-03 15:39:04 +00001060 if (!setValueName(GV, $2)) { // If not redefining...
1061 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001062 int Slot = InsertValue(GV, CurModule.Values);
1063
1064 if (Slot != -1) {
1065 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1066 } else {
1067 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1068 (char*)GV->getName().c_str()));
1069 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001070 }
Chris Lattner1781aca2001-09-18 04:00:54 +00001071 }
1072 | ConstPool OptAssign UNINIT GlobalType Types {
1073 const Type *Ty = *$5;
1074 // Global declarations appear in Constant Pool
Chris Lattneref9c23f2001-10-03 14:53:21 +00001075 GlobalVariable *GV = new GlobalVariable(Ty, $4);
Chris Lattnerb7474512001-10-03 15:39:04 +00001076 if (!setValueName(GV, $2)) { // If not redefining...
1077 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001078 int Slot = InsertValue(GV, CurModule.Values);
1079
1080 if (Slot != -1) {
1081 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1082 } else {
1083 assert(GV->hasName() && "Not named and not numbered!?");
1084 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1085 (char*)GV->getName().c_str()));
1086 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001087 }
Chris Lattnere98dda62001-07-14 06:10:16 +00001088 }
Chris Lattner00950542001-06-06 20:29:01 +00001089 | /* empty: end of list */ {
1090 }
1091
1092
1093//===----------------------------------------------------------------------===//
1094// Rules to match Modules
1095//===----------------------------------------------------------------------===//
1096
1097// Module rule: Capture the result of parsing the whole file into a result
1098// variable...
1099//
1100Module : MethodList {
1101 $$ = ParserResult = $1;
1102 CurModule.ModuleDone();
1103}
1104
Chris Lattnere98dda62001-07-14 06:10:16 +00001105// MethodList - A list of methods, preceeded by a constant pool.
1106//
Chris Lattner00950542001-06-06 20:29:01 +00001107MethodList : MethodList Method {
Chris Lattner00950542001-06-06 20:29:01 +00001108 $$ = $1;
Chris Lattnere1815642001-07-15 06:35:53 +00001109 if (!$2->getParent())
1110 $1->getMethodList().push_back($2);
1111 CurMeth.MethodDone();
Chris Lattner00950542001-06-06 20:29:01 +00001112 }
Chris Lattnere1815642001-07-15 06:35:53 +00001113 | MethodList MethodProto {
1114 $$ = $1;
Chris Lattnere1815642001-07-15 06:35:53 +00001115 }
Chris Lattner00950542001-06-06 20:29:01 +00001116 | ConstPool IMPLEMENTATION {
1117 $$ = CurModule.CurrentModule;
Chris Lattner30c89792001-09-07 16:35:17 +00001118 // Resolve circular types before we parse the body of the module
1119 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner00950542001-06-06 20:29:01 +00001120 }
1121
1122
1123//===----------------------------------------------------------------------===//
1124// Rules to match Method Headers
1125//===----------------------------------------------------------------------===//
1126
1127OptVAR_ID : VAR_ID | /*empty*/ { $$ = 0; }
1128
1129ArgVal : Types OptVAR_ID {
Chris Lattner30c89792001-09-07 16:35:17 +00001130 $$ = new MethodArgument(*$1); delete $1;
Chris Lattnerb7474512001-10-03 15:39:04 +00001131 if (setValueName($$, $2)) { assert(0 && "No arg redef allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001132}
1133
1134ArgListH : ArgVal ',' ArgListH {
1135 $$ = $3;
1136 $3->push_front($1);
1137 }
1138 | ArgVal {
1139 $$ = new list<MethodArgument*>();
1140 $$->push_front($1);
1141 }
Chris Lattner8b81bf52001-07-25 22:47:46 +00001142 | DOTDOTDOT {
1143 $$ = new list<MethodArgument*>();
Chris Lattner2079fde2001-10-13 06:41:08 +00001144 $$->push_front(new MethodArgument(Type::VoidTy));
Chris Lattner8b81bf52001-07-25 22:47:46 +00001145 }
Chris Lattner00950542001-06-06 20:29:01 +00001146
1147ArgList : ArgListH {
1148 $$ = $1;
1149 }
1150 | /* empty */ {
1151 $$ = 0;
1152 }
1153
1154MethodHeaderH : TypesV STRINGCONSTANT '(' ArgList ')' {
Chris Lattner93750fa2001-07-28 17:48:55 +00001155 UnEscapeLexed($2);
Chris Lattner30c89792001-09-07 16:35:17 +00001156 vector<const Type*> ParamTypeList;
Chris Lattner00950542001-06-06 20:29:01 +00001157 if ($4)
Chris Lattner7fc9fe32001-06-27 23:41:11 +00001158 for (list<MethodArgument*>::iterator I = $4->begin(); I != $4->end(); ++I)
Chris Lattner00950542001-06-06 20:29:01 +00001159 ParamTypeList.push_back((*I)->getType());
1160
Chris Lattner2079fde2001-10-13 06:41:08 +00001161 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1162 if (isVarArg) ParamTypeList.pop_back();
1163
1164 const MethodType *MT = MethodType::get(*$1, ParamTypeList, isVarArg);
Chris Lattneref9c23f2001-10-03 14:53:21 +00001165 const PointerType *PMT = PointerType::get(MT);
Chris Lattner30c89792001-09-07 16:35:17 +00001166 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001167
Chris Lattnere1815642001-07-15 06:35:53 +00001168 Method *M = 0;
1169 if (SymbolTable *ST = CurModule.CurrentModule->getSymbolTable()) {
Chris Lattneref9c23f2001-10-03 14:53:21 +00001170 if (Value *V = ST->lookup(PMT, $2)) { // Method already in symtab?
1171 M = cast<Method>(V);
Chris Lattner00950542001-06-06 20:29:01 +00001172
Chris Lattnere1815642001-07-15 06:35:53 +00001173 // Yes it is. If this is the case, either we need to be a forward decl,
1174 // or it needs to be.
1175 if (!CurMeth.isDeclare && !M->isExternal())
1176 ThrowException("Redefinition of method '" + string($2) + "'!");
1177 }
1178 }
1179
1180 if (M == 0) { // Not already defined?
1181 M = new Method(MT, $2);
1182 InsertValue(M, CurModule.Values);
Chris Lattner2079fde2001-10-13 06:41:08 +00001183 CurModule.DeclareNewGlobalValue(M, ValID::create($2));
Chris Lattnere1815642001-07-15 06:35:53 +00001184 }
1185
1186 free($2); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +00001187
1188 CurMeth.MethodStart(M);
1189
1190 // Add all of the arguments we parsed to the method...
Chris Lattnere1815642001-07-15 06:35:53 +00001191 if ($4 && !CurMeth.isDeclare) { // Is null if empty...
Chris Lattner00950542001-06-06 20:29:01 +00001192 Method::ArgumentListType &ArgList = M->getArgumentList();
1193
Chris Lattner7fc9fe32001-06-27 23:41:11 +00001194 for (list<MethodArgument*>::iterator I = $4->begin(); I != $4->end(); ++I) {
Chris Lattner00950542001-06-06 20:29:01 +00001195 InsertValue(*I);
1196 ArgList.push_back(*I);
1197 }
1198 delete $4; // We're now done with the argument list
1199 }
1200}
1201
1202MethodHeader : MethodHeaderH ConstPool BEGINTOK {
1203 $$ = CurMeth.CurrentMethod;
Chris Lattner30c89792001-09-07 16:35:17 +00001204
1205 // Resolve circular types before we parse the body of the method.
1206 ResolveTypes(CurMeth.LateResolveTypes);
Chris Lattner00950542001-06-06 20:29:01 +00001207}
1208
1209Method : BasicBlockList END {
1210 $$ = $1;
1211}
1212
Chris Lattnere1815642001-07-15 06:35:53 +00001213MethodProto : DECLARE { CurMeth.isDeclare = true; } MethodHeaderH {
1214 $$ = CurMeth.CurrentMethod;
Chris Lattner93750fa2001-07-28 17:48:55 +00001215 if (!$$->getParent())
1216 CurModule.CurrentModule->getMethodList().push_back($$);
1217 CurMeth.MethodDone();
Chris Lattnere1815642001-07-15 06:35:53 +00001218}
Chris Lattner00950542001-06-06 20:29:01 +00001219
1220//===----------------------------------------------------------------------===//
1221// Rules to match Basic Blocks
1222//===----------------------------------------------------------------------===//
1223
1224ConstValueRef : ESINT64VAL { // A reference to a direct constant
1225 $$ = ValID::create($1);
1226 }
1227 | EUINT64VAL {
1228 $$ = ValID::create($1);
1229 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001230 | FPVAL { // Perhaps it's an FP constant?
1231 $$ = ValID::create($1);
1232 }
Chris Lattner00950542001-06-06 20:29:01 +00001233 | TRUE {
1234 $$ = ValID::create((int64_t)1);
1235 }
1236 | FALSE {
1237 $$ = ValID::create((int64_t)0);
1238 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001239 | NULL_TOK {
1240 $$ = ValID::createNull();
1241 }
1242
Chris Lattner93750fa2001-07-28 17:48:55 +00001243/*
Chris Lattner00950542001-06-06 20:29:01 +00001244 | STRINGCONSTANT { // Quoted strings work too... especially for methods
1245 $$ = ValID::create_conststr($1);
1246 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001247*/
Chris Lattner00950542001-06-06 20:29:01 +00001248
Chris Lattner2079fde2001-10-13 06:41:08 +00001249// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1250// another value.
1251//
1252SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001253 $$ = ValID::create($1);
1254 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001255 | VAR_ID { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001256 $$ = ValID::create($1);
1257 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001258
1259// ValueRef - A reference to a definition... either constant or symbolic
1260ValueRef : SymbolicValueRef | ConstValueRef
1261
Chris Lattner00950542001-06-06 20:29:01 +00001262
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001263// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1264// type immediately preceeds the value reference, and allows complex constant
1265// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001266ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001267 $$ = getVal(*$1, $2); delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +00001268 }
Chris Lattner8b81bf52001-07-25 22:47:46 +00001269
Chris Lattner00950542001-06-06 20:29:01 +00001270
1271BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner89219832001-10-03 19:35:04 +00001272 ($$ = $1)->getBasicBlocks().push_back($2);
Chris Lattner00950542001-06-06 20:29:01 +00001273 }
1274 | MethodHeader BasicBlock { // Do not allow methods with 0 basic blocks
Chris Lattner89219832001-10-03 19:35:04 +00001275 ($$ = $1)->getBasicBlocks().push_back($2);
Chris Lattner00950542001-06-06 20:29:01 +00001276 }
1277
1278
1279// Basic blocks are terminated by branching instructions:
1280// br, br/cc, switch, ret
1281//
Chris Lattner2079fde2001-10-13 06:41:08 +00001282BasicBlock : InstructionList OptAssign BBTerminatorInst {
1283 if (setValueName($3, $2)) { assert(0 && "No redefn allowed!"); }
1284 InsertValue($3);
1285
1286 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001287 InsertValue($1);
1288 $$ = $1;
1289 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001290 | LABELSTR InstructionList OptAssign BBTerminatorInst {
1291 if (setValueName($4, $3)) { assert(0 && "No redefn allowed!"); }
1292 InsertValue($4);
1293
1294 $2->getInstList().push_back($4);
Chris Lattnerb7474512001-10-03 15:39:04 +00001295 if (setValueName($2, $1)) { assert(0 && "No label redef allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001296
1297 InsertValue($2);
1298 $$ = $2;
1299 }
1300
1301InstructionList : InstructionList Inst {
1302 $1->getInstList().push_back($2);
1303 $$ = $1;
1304 }
1305 | /* empty */ {
1306 $$ = new BasicBlock();
1307 }
1308
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001309BBTerminatorInst : RET ResolvedVal { // Return with a result...
1310 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001311 }
1312 | RET VOID { // Return with no result...
1313 $$ = new ReturnInst();
1314 }
1315 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner9636a912001-10-01 16:18:37 +00001316 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $3)));
Chris Lattner00950542001-06-06 20:29:01 +00001317 } // Conditional Branch...
1318 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner9636a912001-10-01 16:18:37 +00001319 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $6)),
1320 cast<BasicBlock>(getVal(Type::LabelTy, $9)),
Chris Lattner00950542001-06-06 20:29:01 +00001321 getVal(Type::BoolTy, $3));
1322 }
1323 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1324 SwitchInst *S = new SwitchInst(getVal($2, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001325 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
Chris Lattner00950542001-06-06 20:29:01 +00001326 $$ = S;
1327
1328 list<pair<ConstPoolVal*, BasicBlock*> >::iterator I = $8->begin(),
1329 end = $8->end();
Chris Lattner7fc9fe32001-06-27 23:41:11 +00001330 for (; I != end; ++I)
Chris Lattner00950542001-06-06 20:29:01 +00001331 S->dest_push_back(I->first, I->second);
1332 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001333 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO ResolvedVal
1334 EXCEPT ResolvedVal {
1335 const PointerType *PMTy;
1336 const MethodType *Ty;
1337
1338 if (!(PMTy = dyn_cast<PointerType>($2->get())) ||
1339 !(Ty = dyn_cast<MethodType>(PMTy->getValueType()))) {
1340 // Pull out the types of all of the arguments...
1341 vector<const Type*> ParamTypes;
1342 if ($5) {
1343 for (list<Value*>::iterator I = $5->begin(), E = $5->end(); I != E; ++I)
1344 ParamTypes.push_back((*I)->getType());
1345 }
1346
1347 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1348 if (isVarArg) ParamTypes.pop_back();
1349
1350 Ty = MethodType::get($2->get(), ParamTypes, isVarArg);
1351 PMTy = PointerType::get(Ty);
1352 }
1353 delete $2;
1354
1355 Value *V = getVal(PMTy, $3); // Get the method we're calling...
1356
1357 BasicBlock *Normal = dyn_cast<BasicBlock>($8);
1358 BasicBlock *Except = dyn_cast<BasicBlock>($10);
1359
1360 if (Normal == 0 || Except == 0)
1361 ThrowException("Invoke instruction without label destinations!");
1362
1363 // Create the call node...
1364 if (!$5) { // Has no arguments?
Chris Lattner386a3b72001-10-16 19:54:17 +00001365 $$ = new InvokeInst(V, Normal, Except, vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001366 } else { // Has arguments?
1367 // Loop through MethodType's arguments and ensure they are specified
1368 // correctly!
1369 //
1370 MethodType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1371 MethodType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
1372 list<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
1373
1374 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1375 if ((*ArgI)->getType() != *I)
1376 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
1377 (*I)->getName() + "'!");
1378
1379 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1380 ThrowException("Invalid number of parameters detected!");
1381
Chris Lattner386a3b72001-10-16 19:54:17 +00001382 $$ = new InvokeInst(V, Normal, Except,
Chris Lattner2079fde2001-10-13 06:41:08 +00001383 vector<Value*>($5->begin(), $5->end()));
1384 }
1385 delete $5;
1386 }
1387
1388
Chris Lattner00950542001-06-06 20:29:01 +00001389
1390JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1391 $$ = $1;
Chris Lattner2079fde2001-10-13 06:41:08 +00001392 ConstPoolVal *V = cast<ConstPoolVal>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001393 if (V == 0)
1394 ThrowException("May only switch on a constant pool value!");
1395
Chris Lattner9636a912001-10-01 16:18:37 +00001396 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($5, $6))));
Chris Lattner00950542001-06-06 20:29:01 +00001397 }
1398 | IntType ConstValueRef ',' LABEL ValueRef {
1399 $$ = new list<pair<ConstPoolVal*, BasicBlock*> >();
Chris Lattner2079fde2001-10-13 06:41:08 +00001400 ConstPoolVal *V = cast<ConstPoolVal>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00001401
1402 if (V == 0)
1403 ThrowException("May only switch on a constant pool value!");
1404
Chris Lattner9636a912001-10-01 16:18:37 +00001405 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($4, $5))));
Chris Lattner00950542001-06-06 20:29:01 +00001406 }
1407
1408Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001409 // Is this definition named?? if so, assign the name...
1410 if (setValueName($2, $1)) { assert(0 && "No redefin allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001411 InsertValue($2);
1412 $$ = $2;
1413}
1414
Chris Lattnerc24d2082001-06-11 15:04:20 +00001415PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
1416 $$ = new list<pair<Value*, BasicBlock*> >();
Chris Lattner30c89792001-09-07 16:35:17 +00001417 $$->push_back(make_pair(getVal(*$1, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001418 cast<BasicBlock>(getVal(Type::LabelTy, $5))));
Chris Lattner30c89792001-09-07 16:35:17 +00001419 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001420 }
1421 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1422 $$ = $1;
1423 $1->push_back(make_pair(getVal($1->front().first->getType(), $4),
Chris Lattner9636a912001-10-01 16:18:37 +00001424 cast<BasicBlock>(getVal(Type::LabelTy, $6))));
Chris Lattnerc24d2082001-06-11 15:04:20 +00001425 }
1426
1427
Chris Lattner30c89792001-09-07 16:35:17 +00001428ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner00950542001-06-06 20:29:01 +00001429 $$ = new list<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001430 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001431 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001432 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001433 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001434 $1->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001435 }
1436
1437// ValueRefListE - Just like ValueRefList, except that it may also be empty!
1438ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; }
1439
1440InstVal : BinaryOps Types ValueRef ',' ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001441 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001442 if ($$ == 0)
1443 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001444 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001445 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001446 | UnaryOps ResolvedVal {
1447 $$ = UnaryOperator::create($1, $2);
Chris Lattner00950542001-06-06 20:29:01 +00001448 if ($$ == 0)
1449 ThrowException("unary operator returned null!");
Chris Lattner09083092001-07-08 04:57:15 +00001450 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001451 | ShiftOps ResolvedVal ',' ResolvedVal {
1452 if ($4->getType() != Type::UByteTy)
1453 ThrowException("Shift amount must be ubyte!");
1454 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001455 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001456 | CAST ResolvedVal TO Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001457 $$ = new CastInst($2, *$4);
1458 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001459 }
Chris Lattnerc24d2082001-06-11 15:04:20 +00001460 | PHI PHIList {
1461 const Type *Ty = $2->front().first->getType();
1462 $$ = new PHINode(Ty);
Chris Lattner00950542001-06-06 20:29:01 +00001463 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001464 if ($2->front().first->getType() != Ty)
1465 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001466 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001467 $2->pop_front();
1468 }
1469 delete $2; // Free the list...
1470 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001471 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattneref9c23f2001-10-03 14:53:21 +00001472 const PointerType *PMTy;
Chris Lattner8b81bf52001-07-25 22:47:46 +00001473 const MethodType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001474
Chris Lattneref9c23f2001-10-03 14:53:21 +00001475 if (!(PMTy = dyn_cast<PointerType>($2->get())) ||
1476 !(Ty = dyn_cast<MethodType>(PMTy->getValueType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001477 // Pull out the types of all of the arguments...
1478 vector<const Type*> ParamTypes;
Chris Lattneref9c23f2001-10-03 14:53:21 +00001479 if ($5) {
1480 for (list<Value*>::iterator I = $5->begin(), E = $5->end(); I != E; ++I)
1481 ParamTypes.push_back((*I)->getType());
1482 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001483
1484 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1485 if (isVarArg) ParamTypes.pop_back();
1486
1487 Ty = MethodType::get($2->get(), ParamTypes, isVarArg);
Chris Lattneref9c23f2001-10-03 14:53:21 +00001488 PMTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001489 }
Chris Lattner30c89792001-09-07 16:35:17 +00001490 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001491
Chris Lattneref9c23f2001-10-03 14:53:21 +00001492 Value *V = getVal(PMTy, $3); // Get the method we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001493
Chris Lattner8b81bf52001-07-25 22:47:46 +00001494 // Create the call node...
1495 if (!$5) { // Has no arguments?
Chris Lattner386a3b72001-10-16 19:54:17 +00001496 $$ = new CallInst(V, vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001497 } else { // Has arguments?
Chris Lattner00950542001-06-06 20:29:01 +00001498 // Loop through MethodType's arguments and ensure they are specified
1499 // correctly!
1500 //
1501 MethodType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001502 MethodType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
1503 list<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
1504
1505 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1506 if ((*ArgI)->getType() != *I)
1507 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner00950542001-06-06 20:29:01 +00001508 (*I)->getName() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001509
Chris Lattner8b81bf52001-07-25 22:47:46 +00001510 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner00950542001-06-06 20:29:01 +00001511 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00001512
Chris Lattner2079fde2001-10-13 06:41:08 +00001513 $$ = new CallInst(V, vector<Value*>($5->begin(), $5->end()));
Chris Lattner8b81bf52001-07-25 22:47:46 +00001514 }
1515 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00001516 }
1517 | MemoryInst {
1518 $$ = $1;
1519 }
1520
Chris Lattner027dcc52001-07-08 21:10:27 +00001521// UByteList - List of ubyte values for load and store instructions
1522UByteList : ',' ConstVector {
1523 $$ = $2;
1524} | /* empty */ {
1525 $$ = new vector<ConstPoolVal*>();
1526}
1527
Chris Lattner00950542001-06-06 20:29:01 +00001528MemoryInst : MALLOC Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001529 $$ = new MallocInst(PointerType::get(*$2));
1530 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001531 }
1532 | MALLOC Types ',' UINT ValueRef {
Chris Lattnerb00c5822001-10-02 03:41:24 +00001533 if (!(*$2)->isArrayType() || cast<const ArrayType>($2->get())->isSized())
Chris Lattner30c89792001-09-07 16:35:17 +00001534 ThrowException("Trying to allocate " + (*$2)->getName() +
Chris Lattner00950542001-06-06 20:29:01 +00001535 " as unsized array!");
Chris Lattner30c89792001-09-07 16:35:17 +00001536 const Type *Ty = PointerType::get(*$2);
Chris Lattner8896eda2001-07-09 19:38:36 +00001537 $$ = new MallocInst(Ty, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001538 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001539 }
1540 | ALLOCA Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001541 $$ = new AllocaInst(PointerType::get(*$2));
1542 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001543 }
1544 | ALLOCA Types ',' UINT ValueRef {
Chris Lattnerb00c5822001-10-02 03:41:24 +00001545 if (!(*$2)->isArrayType() || cast<const ArrayType>($2->get())->isSized())
Chris Lattner30c89792001-09-07 16:35:17 +00001546 ThrowException("Trying to allocate " + (*$2)->getName() +
Chris Lattner00950542001-06-06 20:29:01 +00001547 " as unsized array!");
Chris Lattner30c89792001-09-07 16:35:17 +00001548 const Type *Ty = PointerType::get(*$2);
Chris Lattner00950542001-06-06 20:29:01 +00001549 Value *ArrSize = getVal($4, $5);
Chris Lattnerf0d0e9c2001-07-07 08:36:30 +00001550 $$ = new AllocaInst(Ty, ArrSize);
Chris Lattner30c89792001-09-07 16:35:17 +00001551 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001552 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001553 | FREE ResolvedVal {
1554 if (!$2->getType()->isPointerType())
1555 ThrowException("Trying to free nonpointer type " +
1556 $2->getType()->getName() + "!");
1557 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001558 }
1559
Chris Lattner027dcc52001-07-08 21:10:27 +00001560 | LOAD Types ValueRef UByteList {
Chris Lattner30c89792001-09-07 16:35:17 +00001561 if (!(*$2)->isPointerType())
Chris Lattner2079fde2001-10-13 06:41:08 +00001562 ThrowException("Can't load from nonpointer type: " +
1563 (*$2)->getDescription());
Chris Lattner30c89792001-09-07 16:35:17 +00001564 if (LoadInst::getIndexedType(*$2, *$4) == 0)
Chris Lattner027dcc52001-07-08 21:10:27 +00001565 ThrowException("Invalid indices for load instruction!");
1566
Chris Lattner30c89792001-09-07 16:35:17 +00001567 $$ = new LoadInst(getVal(*$2, $3), *$4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001568 delete $4; // Free the vector...
Chris Lattner30c89792001-09-07 16:35:17 +00001569 delete $2;
Chris Lattner027dcc52001-07-08 21:10:27 +00001570 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001571 | STORE ResolvedVal ',' Types ValueRef UByteList {
Chris Lattner30c89792001-09-07 16:35:17 +00001572 if (!(*$4)->isPointerType())
1573 ThrowException("Can't store to a nonpointer type: " + (*$4)->getName());
1574 const Type *ElTy = StoreInst::getIndexedType(*$4, *$6);
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001575 if (ElTy == 0)
1576 ThrowException("Can't store into that field list!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001577 if (ElTy != $2->getType())
1578 ThrowException("Can't store '" + $2->getType()->getName() +
1579 "' into space of type '" + ElTy->getName() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001580 $$ = new StoreInst($2, getVal(*$4, $5), *$6);
1581 delete $4; delete $6;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001582 }
1583 | GETELEMENTPTR Types ValueRef UByteList {
Chris Lattner30c89792001-09-07 16:35:17 +00001584 if (!(*$2)->isPointerType())
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001585 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner30c89792001-09-07 16:35:17 +00001586 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
1587 ThrowException("Can't get element ptr '" + (*$2)->getName() + "'!");
1588 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
1589 delete $2; delete $4;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001590 }
Chris Lattner027dcc52001-07-08 21:10:27 +00001591
Chris Lattner00950542001-06-06 20:29:01 +00001592%%
Chris Lattner09083092001-07-08 04:57:15 +00001593int yyerror(const char *ErrorMsg) {
Chris Lattner00950542001-06-06 20:29:01 +00001594 ThrowException(string("Parse error: ") + ErrorMsg);
1595 return 0;
1596}