blob: cc8c1b0b711024607b5a07453c7ea60f128c1b41 [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 Lattner09083092001-07-08 04:57:15 +000031int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
32int 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
52static void ResolveDefinitions(vector<ValueList> &LateResolvers);
Chris Lattner30c89792001-09-07 16:35:17 +000053static void ResolveTypes (vector<PATypeHolder<Type> > &LateResolveTypes);
Chris Lattner00950542001-06-06 20:29:01 +000054
55static struct PerModuleInfo {
56 Module *CurrentModule;
Chris Lattner30c89792001-09-07 16:35:17 +000057 vector<ValueList> Values; // Module level numbered definitions
58 vector<ValueList> LateResolveValues;
59 vector<PATypeHolder<Type> > Types, 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
64 // here. This is used for forward references of ConstPoolPointerReferences.
65 //
66 typedef map<pair<const PointerType *, ValID>, GlobalVariable*> GlobalRefsType;
67 GlobalRefsType GlobalRefs;
68
Chris Lattner00950542001-06-06 20:29:01 +000069 void ModuleDone() {
Chris Lattner30c89792001-09-07 16:35:17 +000070 // If we could not resolve some methods at method compilation time (calls to
71 // methods before they are defined), resolve them now... Types are resolved
72 // when the constant pool has been completely parsed.
73 //
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()) {
80 // TODO: Make this more detailed! Loop over each undef value and print
81 // info
82 ThrowException("TODO: Make better error - Unresolved forward constant references exist!");
83 }
84
Chris Lattner00950542001-06-06 20:29:01 +000085 Values.clear(); // Clear out method local definitions
Chris Lattner30c89792001-09-07 16:35:17 +000086 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +000087 CurrentModule = 0;
88 }
Chris Lattner2079fde2001-10-13 06:41:08 +000089
90
91 // DeclareNewGlobalValue - Called every type a new GV has been defined. This
92 // is used to remove things from the forward declaration map, resolving them
93 // to the correct thing as needed.
94 //
95 void DeclareNewGlobalValue(GlobalValue *GV, ValID D) {
96 // Check to see if there is a forward reference to this global variable...
97 // if there is, eliminate it and patch the reference to use the new def'n.
98 GlobalRefsType::iterator I = GlobalRefs.find(make_pair(GV->getType(), D));
99
100 if (I != GlobalRefs.end()) {
101 GlobalVariable *OldGV = I->second; // Get the placeholder...
102 I->first.second.destroy(); // Free string memory if neccesary
103
104 // Loop over all of the uses of the GlobalValue. The only thing they are
105 // allowed to be at this point is ConstPoolPointerReference's.
106 assert(OldGV->use_size() == 1 && "Only one reference should exist!");
107 while (!OldGV->use_empty()) {
108 User *U = OldGV->use_back(); // Must be a ConstPoolPointerReference...
109 ConstPoolPointerReference *CPPR = cast<ConstPoolPointerReference>(U);
110 assert(CPPR->getValue() == OldGV && "Something isn't happy");
111
112 // Change the const pool reference to point to the real global variable
113 // now. This should drop a use from the OldGV.
114 CPPR->mutateReference(GV);
115 }
116
117 // Remove GV from the module...
118 CurrentModule->getGlobalList().remove(OldGV);
119 delete OldGV; // Delete the old placeholder
120
121 // Remove the map entry for the global now that it has been created...
122 GlobalRefs.erase(I);
123 }
124 }
125
Chris Lattner00950542001-06-06 20:29:01 +0000126} CurModule;
127
128static struct PerMethodInfo {
129 Method *CurrentMethod; // Pointer to current method being created
130
Chris Lattnere1815642001-07-15 06:35:53 +0000131 vector<ValueList> Values; // Keep track of numbered definitions
Chris Lattner00950542001-06-06 20:29:01 +0000132 vector<ValueList> LateResolveValues;
Chris Lattner30c89792001-09-07 16:35:17 +0000133 vector<PATypeHolder<Type> > Types, LateResolveTypes;
Chris Lattnere1815642001-07-15 06:35:53 +0000134 bool isDeclare; // Is this method a forward declararation?
Chris Lattner00950542001-06-06 20:29:01 +0000135
136 inline PerMethodInfo() {
137 CurrentMethod = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000138 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000139 }
140
141 inline ~PerMethodInfo() {}
142
143 inline void MethodStart(Method *M) {
144 CurrentMethod = M;
145 }
146
147 void MethodDone() {
148 // If we could not resolve some blocks at parsing time (forward branches)
149 // resolve the branches now...
150 ResolveDefinitions(LateResolveValues);
151
152 Values.clear(); // Clear out method local definitions
Chris Lattner30c89792001-09-07 16:35:17 +0000153 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +0000154 CurrentMethod = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000155 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000156 }
157} CurMeth; // Info for the current method...
158
Chris Lattnerb7474512001-10-03 15:39:04 +0000159static bool inMethodScope() { return CurMeth.CurrentMethod != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000160
Chris Lattner00950542001-06-06 20:29:01 +0000161
162//===----------------------------------------------------------------------===//
163// Code to handle definitions of all the types
164//===----------------------------------------------------------------------===//
165
Chris Lattner2079fde2001-10-13 06:41:08 +0000166static int InsertValue(Value *D, vector<ValueList> &ValueTab = CurMeth.Values) {
167 if (D->hasName()) return -1; // Is this a numbered definition?
168
169 // Yes, insert the value into the value table...
170 unsigned type = D->getType()->getUniqueID();
171 if (ValueTab.size() <= type)
172 ValueTab.resize(type+1, ValueList());
173 //printf("Values[%d][%d] = %d\n", type, ValueTab[type].size(), D);
174 ValueTab[type].push_back(D);
175 return ValueTab[type].size()-1;
Chris Lattner00950542001-06-06 20:29:01 +0000176}
177
Chris Lattner30c89792001-09-07 16:35:17 +0000178// TODO: FIXME when Type are not const
179static void InsertType(const Type *Ty, vector<PATypeHolder<Type> > &Types) {
180 Types.push_back(Ty);
181}
182
183static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000184 switch (D.Type) {
185 case 0: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000186 unsigned Num = (unsigned)D.Num;
187
188 // Module constants occupy the lowest numbered slots...
189 if (Num < CurModule.Types.size())
190 return CurModule.Types[Num];
191
192 Num -= CurModule.Types.size();
193
194 // Check that the number is within bounds...
195 if (Num <= CurMeth.Types.size())
196 return CurMeth.Types[Num];
197 }
198 case 1: { // Is it a named definition?
199 string Name(D.Name);
200 SymbolTable *SymTab = 0;
Chris Lattnerb7474512001-10-03 15:39:04 +0000201 if (inMethodScope()) SymTab = CurMeth.CurrentMethod->getSymbolTable();
Chris Lattner30c89792001-09-07 16:35:17 +0000202 Value *N = SymTab ? SymTab->lookup(Type::TypeTy, Name) : 0;
203
204 if (N == 0) {
205 // Symbol table doesn't automatically chain yet... because the method
206 // hasn't been added to the module...
207 //
208 SymTab = CurModule.CurrentModule->getSymbolTable();
209 if (SymTab)
210 N = SymTab->lookup(Type::TypeTy, Name);
211 if (N == 0) break;
212 }
213
214 D.destroy(); // Free old strdup'd memory...
Chris Lattnercfe26c92001-10-01 18:26:53 +0000215 return cast<const Type>(N);
Chris Lattner30c89792001-09-07 16:35:17 +0000216 }
217 default:
218 ThrowException("Invalid symbol type reference!");
219 }
220
221 // If we reached here, we referenced either a symbol that we don't know about
222 // or an id number that hasn't been read yet. We may be referencing something
223 // forward, so just create an entry to be resolved later and get to it...
224 //
225 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
226
Chris Lattnerb7474512001-10-03 15:39:04 +0000227 vector<PATypeHolder<Type> > *LateResolver = inMethodScope() ?
Chris Lattner30c89792001-09-07 16:35:17 +0000228 &CurMeth.LateResolveTypes : &CurModule.LateResolveTypes;
229
230 Type *Typ = new TypePlaceHolder(Type::TypeTy, D);
231 InsertType(Typ, *LateResolver);
232 return Typ;
233}
234
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000235static Value *lookupInSymbolTable(const Type *Ty, const string &Name) {
236 SymbolTable *SymTab =
Chris Lattnerb7474512001-10-03 15:39:04 +0000237 inMethodScope() ? CurMeth.CurrentMethod->getSymbolTable() : 0;
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000238 Value *N = SymTab ? SymTab->lookup(Ty, Name) : 0;
239
240 if (N == 0) {
241 // Symbol table doesn't automatically chain yet... because the method
242 // hasn't been added to the module...
243 //
244 SymTab = CurModule.CurrentModule->getSymbolTable();
245 if (SymTab)
246 N = SymTab->lookup(Ty, Name);
247 }
248
249 return N;
250}
251
Chris Lattner2079fde2001-10-13 06:41:08 +0000252// getValNonImprovising - Look up the value specified by the provided type and
253// the provided ValID. If the value exists and has already been defined, return
254// it. Otherwise return null.
255//
256static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Chris Lattner30c89792001-09-07 16:35:17 +0000257 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000258 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000259 unsigned type = Ty->getUniqueID();
Chris Lattner00950542001-06-06 20:29:01 +0000260 unsigned Num = (unsigned)D.Num;
261
262 // Module constants occupy the lowest numbered slots...
263 if (type < CurModule.Values.size()) {
264 if (Num < CurModule.Values[type].size())
265 return CurModule.Values[type][Num];
266
267 Num -= CurModule.Values[type].size();
268 }
269
270 // Make sure that our type is within bounds
Chris Lattner2079fde2001-10-13 06:41:08 +0000271 if (CurMeth.Values.size() <= type) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000272
273 // Check that the number is within bounds...
Chris Lattner2079fde2001-10-13 06:41:08 +0000274 if (CurMeth.Values[type].size() <= Num) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000275
276 return CurMeth.Values[type][Num];
277 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000278
Chris Lattner1a1cb112001-09-30 22:46:54 +0000279 case ValID::NameVal: { // Is it a named definition?
Chris Lattner2079fde2001-10-13 06:41:08 +0000280 Value *N = lookupInSymbolTable(Ty, string(D.Name));
281 if (N == 0) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000282
283 D.destroy(); // Free old strdup'd memory...
284 return N;
285 }
286
Chris Lattner2079fde2001-10-13 06:41:08 +0000287 // Check to make sure that "Ty" is an integral type, and that our
288 // value will fit into the specified type...
289 case ValID::ConstSIntVal: // Is it a constant pool reference??
290 if (Ty == Type::BoolTy) { // Special handling for boolean data
291 return ConstPoolBool::get(D.ConstPool64 != 0);
292 } else {
293 if (!ConstPoolSInt::isValueValidForType(Ty, D.ConstPool64))
294 ThrowException("Symbolic constant pool value '" +
295 itostr(D.ConstPool64) + "' is invalid for type '" +
296 Ty->getName() + "'!");
297 return ConstPoolSInt::get(Ty, D.ConstPool64);
Chris Lattner00950542001-06-06 20:29:01 +0000298 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000299
300 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
301 if (!ConstPoolUInt::isValueValidForType(Ty, D.UConstPool64)) {
302 if (!ConstPoolSInt::isValueValidForType(Ty, D.ConstPool64)) {
303 ThrowException("Integral constant pool reference is invalid!");
304 } else { // This is really a signed reference. Transmogrify.
305 return ConstPoolSInt::get(Ty, D.ConstPool64);
306 }
307 } else {
308 return ConstPoolUInt::get(Ty, D.UConstPool64);
309 }
310
311 case ValID::ConstStringVal: // Is it a string const pool reference?
312 cerr << "FIXME: TODO: String constants [sbyte] not implemented yet!\n";
313 abort();
314 return 0;
315
316 case ValID::ConstFPVal: // Is it a floating point const pool reference?
317 if (!ConstPoolFP::isValueValidForType(Ty, D.ConstPoolFP))
318 ThrowException("FP constant invalid for type!!");
319 return ConstPoolFP::get(Ty, D.ConstPoolFP);
320
321 case ValID::ConstNullVal: // Is it a null value?
322 if (!Ty->isPointerType())
323 ThrowException("Cannot create a a non pointer null!");
324 return ConstPoolPointerNull::get(cast<PointerType>(Ty));
325
Chris Lattner30c89792001-09-07 16:35:17 +0000326 default:
327 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000328 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000329 } // End of switch
330
Chris Lattner2079fde2001-10-13 06:41:08 +0000331 assert(0 && "Unhandled case!");
332 return 0;
333}
334
335
336// getVal - This function is identical to getValNonImprovising, except that if a
337// value is not already defined, it "improvises" by creating a placeholder var
338// that looks and acts just like the requested variable. When the value is
339// defined later, all uses of the placeholder variable are replaced with the
340// real thing.
341//
342static Value *getVal(const Type *Ty, const ValID &D) {
343 assert(Ty != Type::TypeTy && "Should use getTypeVal for types!");
344
345 // See if the value has already been defined...
346 Value *V = getValNonImprovising(Ty, D);
347 if (V) return V;
Chris Lattner00950542001-06-06 20:29:01 +0000348
349 // If we reached here, we referenced either a symbol that we don't know about
350 // or an id number that hasn't been read yet. We may be referencing something
351 // forward, so just create an entry to be resolved later and get to it...
352 //
Chris Lattner00950542001-06-06 20:29:01 +0000353 Value *d = 0;
Chris Lattnerb7474512001-10-03 15:39:04 +0000354 vector<ValueList> *LateResolver = inMethodScope() ?
Chris Lattner30c89792001-09-07 16:35:17 +0000355 &CurMeth.LateResolveValues : &CurModule.LateResolveValues;
Chris Lattner93750fa2001-07-28 17:48:55 +0000356
Chris Lattnerb973dd72001-10-03 14:59:05 +0000357 if (isa<MethodType>(Ty))
358 ThrowException("Methods are not values and must be referenced as pointers");
359
Chris Lattneref9c23f2001-10-03 14:53:21 +0000360 if (const PointerType *PTy = dyn_cast<PointerType>(Ty))
361 if (const MethodType *MTy = dyn_cast<MethodType>(PTy->getValueType()))
362 Ty = MTy; // Convert pointer to method to method type
363
Chris Lattner30c89792001-09-07 16:35:17 +0000364 switch (Ty->getPrimitiveID()) {
365 case Type::LabelTyID: d = new BBPlaceHolder(Ty, D); break;
366 case Type::MethodTyID: d = new MethPlaceHolder(Ty, D);
Chris Lattner93750fa2001-07-28 17:48:55 +0000367 LateResolver = &CurModule.LateResolveValues; break;
Chris Lattner30c89792001-09-07 16:35:17 +0000368 default: d = new ValuePlaceHolder(Ty, D); break;
Chris Lattner00950542001-06-06 20:29:01 +0000369 }
370
371 assert(d != 0 && "How did we not make something?");
Chris Lattner93750fa2001-07-28 17:48:55 +0000372 InsertValue(d, *LateResolver);
Chris Lattner00950542001-06-06 20:29:01 +0000373 return d;
374}
375
376
377//===----------------------------------------------------------------------===//
378// Code to handle forward references in instructions
379//===----------------------------------------------------------------------===//
380//
381// This code handles the late binding needed with statements that reference
382// values not defined yet... for example, a forward branch, or the PHI node for
383// a loop body.
384//
385// This keeps a table (CurMeth.LateResolveValues) of all such forward references
386// and back patchs after we are done.
387//
388
389// ResolveDefinitions - If we could not resolve some defs at parsing
390// time (forward branches, phi functions for loops, etc...) resolve the
391// defs now...
392//
393static void ResolveDefinitions(vector<ValueList> &LateResolvers) {
394 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
395 for (unsigned ty = 0; ty < LateResolvers.size(); ty++) {
396 while (!LateResolvers[ty].empty()) {
397 Value *V = LateResolvers[ty].back();
398 LateResolvers[ty].pop_back();
399 ValID &DID = getValIDFromPlaceHolder(V);
400
Chris Lattner2079fde2001-10-13 06:41:08 +0000401 Value *TheRealValue = getValNonImprovising(Type::getUniqueIDType(ty),DID);
Chris Lattner30c89792001-09-07 16:35:17 +0000402 if (TheRealValue == 0) {
403 if (DID.Type == 1)
404 ThrowException("Reference to an invalid definition: '" +DID.getName()+
405 "' of type '" + V->getType()->getDescription() + "'",
406 getLineNumFromPlaceHolder(V));
407 else
408 ThrowException("Reference to an invalid definition: #" +
409 itostr(DID.Num) + " of type '" +
410 V->getType()->getDescription() + "'",
411 getLineNumFromPlaceHolder(V));
412 }
413
Chris Lattnercfe26c92001-10-01 18:26:53 +0000414 assert(!isa<Type>(V) && "Types should be in LateResolveTypes!");
Chris Lattner00950542001-06-06 20:29:01 +0000415
416 V->replaceAllUsesWith(TheRealValue);
Chris Lattner00950542001-06-06 20:29:01 +0000417 delete V;
418 }
419 }
420
421 LateResolvers.clear();
422}
423
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000424// ResolveType - Take a specified unresolved type and resolve it. If there is
425// nothing to resolve it to yet, return true. Otherwise resolve it and return
426// false.
427//
428static bool ResolveType(PATypeHolder<Type> &T) {
429 const Type *Ty = T;
430 ValID &DID = getValIDFromPlaceHolder(Ty);
431
432 const Type *TheRealType = getTypeVal(DID, true);
433 if (TheRealType == 0) return true;
434
435 // Refine the opaque type we had to the new type we are getting.
436 cast<DerivedType>(Ty)->refineAbstractTypeTo(TheRealType);
437 return false;
438}
439
Chris Lattner30c89792001-09-07 16:35:17 +0000440
441// ResolveTypes - This goes through the forward referenced type table and makes
442// sure that all type references are complete. This code is executed after the
443// constant pool of a method or module is completely parsed.
Chris Lattner00950542001-06-06 20:29:01 +0000444//
Chris Lattner30c89792001-09-07 16:35:17 +0000445static void ResolveTypes(vector<PATypeHolder<Type> > &LateResolveTypes) {
446 while (!LateResolveTypes.empty()) {
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000447 if (ResolveType(LateResolveTypes.back())) {
448 const Type *Ty = LateResolveTypes.back();
449 ValID &DID = getValIDFromPlaceHolder(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000450
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000451 if (DID.Type == ValID::NameVal)
Chris Lattner30c89792001-09-07 16:35:17 +0000452 ThrowException("Reference to an invalid type: '" +DID.getName(),
453 getLineNumFromPlaceHolder(Ty));
454 else
455 ThrowException("Reference to an invalid type: #" + itostr(DID.Num),
456 getLineNumFromPlaceHolder(Ty));
Chris Lattner00950542001-06-06 20:29:01 +0000457 }
Chris Lattner30c89792001-09-07 16:35:17 +0000458
Chris Lattner30c89792001-09-07 16:35:17 +0000459 // No need to delete type, refine does that for us.
460 LateResolveTypes.pop_back();
461 }
462}
463
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000464
465// ResolveSomeTypes - This goes through the forward referenced type table and
466// completes references that are now done. This is so that types are
467// immediately resolved to be as concrete as possible. This does not cause
468// thrown exceptions if not everything is resolved.
469//
470static void ResolveSomeTypes(vector<PATypeHolder<Type> > &LateResolveTypes) {
471 for (unsigned i = 0; i < LateResolveTypes.size(); ) {
472 if (ResolveType(LateResolveTypes[i]))
473 ++i; // Type didn't resolve
474 else
475 LateResolveTypes.erase(LateResolveTypes.begin()+i); // Type resolved!
476 }
477}
478
479
Chris Lattner1781aca2001-09-18 04:00:54 +0000480// setValueName - Set the specified value to the name given. The name may be
481// null potentially, in which case this is a noop. The string passed in is
482// assumed to be a malloc'd string buffer, and is freed by this function.
483//
Chris Lattnerb7474512001-10-03 15:39:04 +0000484// This function returns true if the value has already been defined, but is
485// allowed to be redefined in the specified context. If the name is a new name
486// for the typeplane, false is returned.
487//
488static bool setValueName(Value *V, char *NameStr) {
489 if (NameStr == 0) return false;
Chris Lattner1781aca2001-09-18 04:00:54 +0000490 string Name(NameStr); // Copy string
491 free(NameStr); // Free old string
492
Chris Lattner2079fde2001-10-13 06:41:08 +0000493 if (V->getType() == Type::VoidTy)
494 ThrowException("Can't assign name '" + Name +
495 "' to a null valued instruction!");
496
Chris Lattnerb7474512001-10-03 15:39:04 +0000497 SymbolTable *ST = inMethodScope() ?
Chris Lattner30c89792001-09-07 16:35:17 +0000498 CurMeth.CurrentMethod->getSymbolTableSure() :
499 CurModule.CurrentModule->getSymbolTableSure();
500
501 Value *Existing = ST->lookup(V->getType(), Name);
502 if (Existing) { // Inserting a name that is already defined???
503 // There is only one case where this is allowed: when we are refining an
504 // opaque type. In this case, Existing will be an opaque type.
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000505 if (const Type *Ty = dyn_cast<const Type>(Existing)) {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000506 if (OpaqueType *OpTy = dyn_cast<OpaqueType>(Ty)) {
Chris Lattner30c89792001-09-07 16:35:17 +0000507 // We ARE replacing an opaque type!
Chris Lattnerb00c5822001-10-02 03:41:24 +0000508 OpTy->refineAbstractTypeTo(cast<Type>(V));
Chris Lattnerb7474512001-10-03 15:39:04 +0000509 return true;
Chris Lattner30c89792001-09-07 16:35:17 +0000510 }
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000511 }
Chris Lattner30c89792001-09-07 16:35:17 +0000512
Chris Lattner9636a912001-10-01 16:18:37 +0000513 // Otherwise, we are a simple redefinition of a value, check to see if it
514 // is defined the same as the old one...
515 if (const Type *Ty = dyn_cast<const Type>(Existing)) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000516 if (Ty == cast<const Type>(V)) return true; // Yes, it's equal.
517 // cerr << "Type: " << Ty->getDescription() << " != "
518 // << cast<const Type>(V)->getDescription() << "!\n";
519 } else if (GlobalVariable *EGV = dyn_cast<GlobalVariable>(Existing)) {
Chris Lattner43efcbf2001-10-03 19:35:57 +0000520 // We are allowed to redefine a global variable in two circumstances:
521 // 1. If at least one of the globals is uninitialized or
522 // 2. If both initializers have the same value.
523 //
524 // This can only be done if the const'ness of the vars is the same.
525 //
Chris Lattner89219832001-10-03 19:35:04 +0000526 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
527 if (EGV->isConstant() == GV->isConstant() &&
528 (!EGV->hasInitializer() || !GV->hasInitializer() ||
529 EGV->getInitializer() == GV->getInitializer())) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000530
Chris Lattner89219832001-10-03 19:35:04 +0000531 // Make sure the existing global version gets the initializer!
532 if (GV->hasInitializer() && !EGV->hasInitializer())
533 EGV->setInitializer(GV->getInitializer());
534
Chris Lattner2079fde2001-10-13 06:41:08 +0000535 delete GV; // Destroy the duplicate!
Chris Lattner89219832001-10-03 19:35:04 +0000536 return true; // They are equivalent!
537 }
Chris Lattnerb7474512001-10-03 15:39:04 +0000538 }
Chris Lattner9636a912001-10-01 16:18:37 +0000539 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000540 ThrowException("Redefinition of value named '" + Name + "' in the '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000541 V->getType()->getDescription() + "' type plane!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000542 }
Chris Lattner00950542001-06-06 20:29:01 +0000543
Chris Lattner30c89792001-09-07 16:35:17 +0000544 V->setName(Name, ST);
Chris Lattnerb7474512001-10-03 15:39:04 +0000545 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000546}
547
Chris Lattner8896eda2001-07-09 19:38:36 +0000548
Chris Lattner30c89792001-09-07 16:35:17 +0000549//===----------------------------------------------------------------------===//
550// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000551//
Chris Lattner8896eda2001-07-09 19:38:36 +0000552
Chris Lattner30c89792001-09-07 16:35:17 +0000553// TypeContains - Returns true if Ty contains E in it.
554//
555static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner3ff43872001-09-28 22:56:31 +0000556 return find(df_begin(Ty), df_end(Ty), E) != df_end(Ty);
Chris Lattner30c89792001-09-07 16:35:17 +0000557}
Chris Lattner698b56e2001-07-20 19:15:08 +0000558
Chris Lattner30c89792001-09-07 16:35:17 +0000559
560static vector<pair<unsigned, OpaqueType *> > UpRefs;
561
562static PATypeHolder<Type> HandleUpRefs(const Type *ty) {
563 PATypeHolder<Type> Ty(ty);
564 UR_OUT(UpRefs.size() << " upreferences active!\n");
565 for (unsigned i = 0; i < UpRefs.size(); ) {
566 UR_OUT("TypeContains(" << Ty->getDescription() << ", "
567 << UpRefs[i].second->getDescription() << ") = "
568 << TypeContains(Ty, UpRefs[i].second) << endl);
569 if (TypeContains(Ty, UpRefs[i].second)) {
570 unsigned Level = --UpRefs[i].first; // Decrement level of upreference
571 UR_OUT("Uplevel Ref Level = " << Level << endl);
572 if (Level == 0) { // Upreference should be resolved!
573 UR_OUT("About to resolve upreference!\n";
574 string OldName = UpRefs[i].second->getDescription());
575 UpRefs[i].second->refineAbstractTypeTo(Ty);
576 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
577 UR_OUT("Type '" << OldName << "' refined upreference to: "
578 << (const void*)Ty << ", " << Ty->getDescription() << endl);
579 continue;
580 }
581 }
582
583 ++i; // Otherwise, no resolve, move on...
Chris Lattner8896eda2001-07-09 19:38:36 +0000584 }
Chris Lattner30c89792001-09-07 16:35:17 +0000585 // FIXME: TODO: this should return the updated type
Chris Lattner8896eda2001-07-09 19:38:36 +0000586 return Ty;
587}
588
Chris Lattner30c89792001-09-07 16:35:17 +0000589template <class TypeTy>
590inline static void TypeDone(PATypeHolder<TypeTy> *Ty) {
591 if (UpRefs.size())
592 ThrowException("Invalid upreference in type: " + (*Ty)->getDescription());
593}
594
595// newTH - Allocate a new type holder for the specified type
596template <class TypeTy>
597inline static PATypeHolder<TypeTy> *newTH(const TypeTy *Ty) {
598 return new PATypeHolder<TypeTy>(Ty);
599}
600template <class TypeTy>
601inline static PATypeHolder<TypeTy> *newTH(const PATypeHolder<TypeTy> &TH) {
602 return new PATypeHolder<TypeTy>(TH);
603}
604
605
Chris Lattner00950542001-06-06 20:29:01 +0000606//===----------------------------------------------------------------------===//
607// RunVMAsmParser - Define an interface to this parser
608//===----------------------------------------------------------------------===//
609//
Chris Lattnera2850432001-07-22 18:36:00 +0000610Module *RunVMAsmParser(const string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000611 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000612 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000613 llvmAsmlineno = 1; // Reset the current line number...
614
615 CurModule.CurrentModule = new Module(); // Allocate a new module to read
616 yyparse(); // Parse the file.
617 Module *Result = ParserResult;
Chris Lattner00950542001-06-06 20:29:01 +0000618 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
619 ParserResult = 0;
620
621 return Result;
622}
623
624%}
625
626%union {
Chris Lattner30c89792001-09-07 16:35:17 +0000627 Module *ModuleVal;
628 Method *MethodVal;
629 MethodArgument *MethArgVal;
630 BasicBlock *BasicBlockVal;
631 TerminatorInst *TermInstVal;
632 Instruction *InstVal;
633 ConstPoolVal *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000634
Chris Lattner30c89792001-09-07 16:35:17 +0000635 const Type *PrimType;
636 PATypeHolder<Type> *TypeVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000637 Value *ValueVal;
638
639 list<MethodArgument*> *MethodArgList;
640 list<Value*> *ValueList;
641 list<PATypeHolder<Type> > *TypeList;
Chris Lattnerc24d2082001-06-11 15:04:20 +0000642 list<pair<Value*, BasicBlock*> > *PHIList; // Represent the RHS of PHI node
Chris Lattner00950542001-06-06 20:29:01 +0000643 list<pair<ConstPoolVal*, BasicBlock*> > *JumpTable;
Chris Lattner30c89792001-09-07 16:35:17 +0000644 vector<ConstPoolVal*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000645
Chris Lattner30c89792001-09-07 16:35:17 +0000646 int64_t SInt64Val;
647 uint64_t UInt64Val;
648 int SIntVal;
649 unsigned UIntVal;
650 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000651 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000652
Chris Lattner30c89792001-09-07 16:35:17 +0000653 char *StrVal; // This memory is strdup'd!
654 ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000655
Chris Lattner30c89792001-09-07 16:35:17 +0000656 Instruction::UnaryOps UnaryOpVal;
657 Instruction::BinaryOps BinaryOpVal;
658 Instruction::TermOps TermOpVal;
659 Instruction::MemoryOps MemOpVal;
660 Instruction::OtherOps OtherOpVal;
Chris Lattner00950542001-06-06 20:29:01 +0000661}
662
663%type <ModuleVal> Module MethodList
Chris Lattnere1815642001-07-15 06:35:53 +0000664%type <MethodVal> Method MethodProto MethodHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000665%type <BasicBlockVal> BasicBlock InstructionList
666%type <TermInstVal> BBTerminatorInst
667%type <InstVal> Inst InstVal MemoryInst
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000668%type <ConstVal> ConstVal
Chris Lattner027dcc52001-07-08 21:10:27 +0000669%type <ConstVector> ConstVector UByteList
Chris Lattner00950542001-06-06 20:29:01 +0000670%type <MethodArgList> ArgList ArgListH
671%type <MethArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000672%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000673%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner30c89792001-09-07 16:35:17 +0000674%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000675%type <JumpTable> JumpTable
Chris Lattner1781aca2001-09-18 04:00:54 +0000676%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner00950542001-06-06 20:29:01 +0000677
Chris Lattner2079fde2001-10-13 06:41:08 +0000678// ValueRef - Unresolved reference to a definition or BB
679%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000680%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000681// Tokens and types for handling constant integer values
682//
683// ESINT64VAL - A negative number within long long range
684%token <SInt64Val> ESINT64VAL
685
686// EUINT64VAL - A positive number within uns. long long range
687%token <UInt64Val> EUINT64VAL
688%type <SInt64Val> EINT64VAL
689
690%token <SIntVal> SINTVAL // Signed 32 bit ints...
691%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
692%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000693%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000694
695// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000696%type <TypeVal> Types TypesV UpRTypes UpRTypesV
697%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
698%token <TypeVal> OPAQUE
699%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
700%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000701
702%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
703%type <StrVal> OptVAR_ID OptAssign
704
705
Chris Lattner1781aca2001-09-18 04:00:54 +0000706%token IMPLEMENTATION TRUE FALSE BEGINTOK END DECLARE GLOBAL CONSTANT UNINIT
Chris Lattner2079fde2001-10-13 06:41:08 +0000707%token TO EXCEPT DOTDOTDOT STRING NULL_TOK CONST
Chris Lattner00950542001-06-06 20:29:01 +0000708
709// Basic Block Terminating Operators
710%token <TermOpVal> RET BR SWITCH
711
712// Unary Operators
713%type <UnaryOpVal> UnaryOps // all the unary operators
Chris Lattner71496b32001-07-08 19:03:27 +0000714%token <UnaryOpVal> NOT
Chris Lattner00950542001-06-06 20:29:01 +0000715
716// Binary Operators
717%type <BinaryOpVal> BinaryOps // all the binary operators
718%token <BinaryOpVal> ADD SUB MUL DIV REM
Chris Lattner027dcc52001-07-08 21:10:27 +0000719%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000720
721// Memory Instructions
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000722%token <MemoryOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000723
Chris Lattner027dcc52001-07-08 21:10:27 +0000724// Other Operators
725%type <OtherOpVal> ShiftOps
Chris Lattner2079fde2001-10-13 06:41:08 +0000726%token <OtherOpVal> PHI CALL INVOKE CAST SHL SHR
Chris Lattner027dcc52001-07-08 21:10:27 +0000727
Chris Lattner00950542001-06-06 20:29:01 +0000728%start Module
729%%
730
731// Handle constant integer size restriction and conversion...
732//
733
734INTVAL : SINTVAL
735INTVAL : UINTVAL {
736 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
737 ThrowException("Value too large for type!");
738 $$ = (int32_t)$1;
739}
740
741
742EINT64VAL : ESINT64VAL // These have same type and can't cause problems...
743EINT64VAL : EUINT64VAL {
744 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
745 ThrowException("Value too large for type!");
746 $$ = (int64_t)$1;
747}
748
Chris Lattner00950542001-06-06 20:29:01 +0000749// Operations that are notably excluded from this list include:
750// RET, BR, & SWITCH because they end basic blocks and are treated specially.
751//
Chris Lattner09083092001-07-08 04:57:15 +0000752UnaryOps : NOT
Chris Lattner00950542001-06-06 20:29:01 +0000753BinaryOps : ADD | SUB | MUL | DIV | REM
754BinaryOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE
Chris Lattner027dcc52001-07-08 21:10:27 +0000755ShiftOps : SHL | SHR
Chris Lattner00950542001-06-06 20:29:01 +0000756
Chris Lattnere98dda62001-07-14 06:10:16 +0000757// These are some types that allow classification if we only want a particular
758// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner00950542001-06-06 20:29:01 +0000759SIntType : LONG | INT | SHORT | SBYTE
760UIntType : ULONG | UINT | USHORT | UBYTE
Chris Lattner30c89792001-09-07 16:35:17 +0000761IntType : SIntType | UIntType
762FPType : FLOAT | DOUBLE
Chris Lattner00950542001-06-06 20:29:01 +0000763
Chris Lattnere98dda62001-07-14 06:10:16 +0000764// OptAssign - Value producing statements have an optional assignment component
Chris Lattner00950542001-06-06 20:29:01 +0000765OptAssign : VAR_ID '=' {
766 $$ = $1;
767 }
768 | /*empty*/ {
769 $$ = 0;
770 }
771
Chris Lattner30c89792001-09-07 16:35:17 +0000772
773//===----------------------------------------------------------------------===//
774// Types includes all predefined types... except void, because it can only be
775// used in specific contexts (method returning void for example). To have
776// access to it, a user must explicitly use TypesV.
777//
778
779// TypesV includes all of 'Types', but it also includes the void type.
780TypesV : Types | VOID { $$ = newTH($1); }
781UpRTypesV : UpRTypes | VOID { $$ = newTH($1); }
782
783Types : UpRTypes {
784 TypeDone($$ = $1);
785 }
786
787
788// Derived types are added later...
789//
790PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT
791PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL
792UpRTypes : OPAQUE | PrimType { $$ = newTH($1); }
793UpRTypes : ValueRef { // Named types are also simple types...
794 $$ = newTH(getTypeVal($1));
795}
796
Chris Lattner30c89792001-09-07 16:35:17 +0000797// Include derived types in the Types production.
798//
799UpRTypes : '\\' EUINT64VAL { // Type UpReference
800 if ($2 > (uint64_t)INT64_MAX) ThrowException("Value out of range!");
801 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
802 UpRefs.push_back(make_pair((unsigned)$2, OT)); // Add to vector...
803 $$ = newTH<Type>(OT);
804 UR_OUT("New Upreference!\n");
805 }
806 | UpRTypesV '(' ArgTypeListI ')' { // Method derived type?
807 vector<const Type*> Params;
808 mapto($3->begin(), $3->end(), back_inserter(Params),
809 mem_fun_ref(&PATypeHandle<Type>::get));
Chris Lattner2079fde2001-10-13 06:41:08 +0000810 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
811 if (isVarArg) Params.pop_back();
812
813 $$ = newTH(HandleUpRefs(MethodType::get(*$1, Params, isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +0000814 delete $3; // Delete the argument list
815 delete $1; // Delete the old type handle
816 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000817 | '[' UpRTypesV ']' { // Unsized array type?
818 $$ = newTH<Type>(HandleUpRefs(ArrayType::get(*$2)));
819 delete $2;
Chris Lattner30c89792001-09-07 16:35:17 +0000820 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000821 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
822 $$ = newTH<Type>(HandleUpRefs(ArrayType::get(*$4, (int)$2)));
823 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +0000824 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000825 | '{' TypeListI '}' { // Structure type?
826 vector<const Type*> Elements;
827 mapto($2->begin(), $2->end(), back_inserter(Elements),
828 mem_fun_ref(&PATypeHandle<Type>::get));
Chris Lattner30c89792001-09-07 16:35:17 +0000829
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000830 $$ = newTH<Type>(HandleUpRefs(StructType::get(Elements)));
831 delete $2;
832 }
833 | '{' '}' { // Empty structure type?
834 $$ = newTH<Type>(StructType::get(vector<const Type*>()));
835 }
836 | UpRTypes '*' { // Pointer type?
837 $$ = newTH<Type>(HandleUpRefs(PointerType::get(*$1)));
838 delete $1;
839 }
Chris Lattner30c89792001-09-07 16:35:17 +0000840
841// TypeList - Used for struct declarations and as a basis for method type
842// declaration type lists
843//
844TypeListI : UpRTypes {
845 $$ = new list<PATypeHolder<Type> >();
846 $$->push_back(*$1); delete $1;
847 }
848 | TypeListI ',' UpRTypes {
849 ($$=$1)->push_back(*$3); delete $3;
850 }
851
852// ArgTypeList - List of types for a method type declaration...
853ArgTypeListI : TypeListI
854 | TypeListI ',' DOTDOTDOT {
855 ($$=$1)->push_back(Type::VoidTy);
856 }
857 | DOTDOTDOT {
858 ($$ = new list<PATypeHolder<Type> >())->push_back(Type::VoidTy);
859 }
860 | /*empty*/ {
861 $$ = new list<PATypeHolder<Type> >();
862 }
863
864
Chris Lattnere98dda62001-07-14 06:10:16 +0000865// ConstVal - The various declarations that go into the constant pool. This
866// includes all forward declarations of types, constants, and functions.
867//
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000868ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
869 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
870 if (ATy == 0)
871 ThrowException("Cannot make array constant with type: '" +
872 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +0000873 const Type *ETy = ATy->getElementType();
874 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +0000875
Chris Lattner30c89792001-09-07 16:35:17 +0000876 // Verify that we have the correct size...
877 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +0000878 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattner30c89792001-09-07 16:35:17 +0000879 utostr($3->size()) + " arguments, but has size of " +
880 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +0000881
Chris Lattner30c89792001-09-07 16:35:17 +0000882 // Verify all elements are correct type!
883 for (unsigned i = 0; i < $3->size(); i++) {
884 if (ETy != (*$3)[i]->getType())
Chris Lattner00950542001-06-06 20:29:01 +0000885 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000886 ETy->getName() + "' as required!\nIt is of type '" +
887 (*$3)[i]->getType()->getName() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +0000888 }
889
Chris Lattner30c89792001-09-07 16:35:17 +0000890 $$ = ConstPoolArray::get(ATy, *$3);
891 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000892 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000893 | Types '[' ']' {
894 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
895 if (ATy == 0)
896 ThrowException("Cannot make array constant with type: '" +
897 (*$1)->getDescription() + "'!");
898
899 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +0000900 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +0000901 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattner30c89792001-09-07 16:35:17 +0000902 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000903 $$ = ConstPoolArray::get(ATy, vector<ConstPoolVal*>());
Chris Lattner30c89792001-09-07 16:35:17 +0000904 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +0000905 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000906 | Types 'c' STRINGCONSTANT {
907 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
908 if (ATy == 0)
909 ThrowException("Cannot make array constant with type: '" +
910 (*$1)->getDescription() + "'!");
911
Chris Lattner30c89792001-09-07 16:35:17 +0000912 int NumElements = ATy->getNumElements();
913 const Type *ETy = ATy->getElementType();
914 char *EndStr = UnEscapeLexed($3, true);
915 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +0000916 ThrowException("Can't build string constant of size " +
Chris Lattner30c89792001-09-07 16:35:17 +0000917 itostr((int)(EndStr-$3)) +
918 " when array has size " + itostr(NumElements) + "!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000919 vector<ConstPoolVal*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +0000920 if (ETy == Type::SByteTy) {
921 for (char *C = $3; C != EndStr; ++C)
922 Vals.push_back(ConstPoolSInt::get(ETy, *C));
923 } else if (ETy == Type::UByteTy) {
924 for (char *C = $3; C != EndStr; ++C)
925 Vals.push_back(ConstPoolUInt::get(ETy, *C));
Chris Lattner93750fa2001-07-28 17:48:55 +0000926 } else {
Chris Lattner30c89792001-09-07 16:35:17 +0000927 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +0000928 ThrowException("Cannot build string arrays of non byte sized elements!");
929 }
Chris Lattner30c89792001-09-07 16:35:17 +0000930 free($3);
931 $$ = ConstPoolArray::get(ATy, Vals);
932 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +0000933 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000934 | Types '{' ConstVector '}' {
935 const StructType *STy = dyn_cast<const StructType>($1->get());
936 if (STy == 0)
937 ThrowException("Cannot make struct constant with type: '" +
938 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +0000939 // FIXME: TODO: Check to see that the constants are compatible with the type
940 // initializer!
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000941 $$ = ConstPoolStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +0000942 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000943 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000944 | Types NULL_TOK {
945 const PointerType *PTy = dyn_cast<const PointerType>($1->get());
946 if (PTy == 0)
947 ThrowException("Cannot make null pointer constant with type: '" +
948 (*$1)->getDescription() + "'!");
949
Chris Lattner2079fde2001-10-13 06:41:08 +0000950 $$ = ConstPoolPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000951 delete $1;
952 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000953 | Types SymbolicValueRef {
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000954 const PointerType *Ty = dyn_cast<const PointerType>($1->get());
955 if (Ty == 0)
956 ThrowException("Global const reference must be a pointer type!");
957
Chris Lattner2079fde2001-10-13 06:41:08 +0000958 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000959
Chris Lattner2079fde2001-10-13 06:41:08 +0000960 // If this is an initializer for a constant pointer, which is referencing a
961 // (currently) undefined variable, create a stub now that shall be replaced
962 // in the future with the right type of variable.
963 //
964 if (V == 0) {
965 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
966 const PointerType *PT = cast<PointerType>(Ty);
967
968 // First check to see if the forward references value is already created!
969 PerModuleInfo::GlobalRefsType::iterator I =
970 CurModule.GlobalRefs.find(make_pair(PT, $2));
971
972 if (I != CurModule.GlobalRefs.end()) {
973 V = I->second; // Placeholder already exists, use it...
974 } else {
975 // TODO: Include line number info by creating a subclass of
976 // TODO: GlobalVariable here that includes the said information!
977
978 // Create a placeholder for the global variable reference...
979 GlobalVariable *GV = new GlobalVariable(PT->getValueType(), false);
980 // Keep track of the fact that we have a forward ref to recycle it
981 CurModule.GlobalRefs.insert(make_pair(make_pair(PT, $2), GV));
982
983 // Must temporarily push this value into the module table...
984 CurModule.CurrentModule->getGlobalList().push_back(GV);
985 V = GV;
986 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000987 }
988
Chris Lattner2079fde2001-10-13 06:41:08 +0000989 GlobalValue *GV = cast<GlobalValue>(V);
990 $$ = ConstPoolPointerReference::get(GV);
991 delete $1; // Free the type handle
Chris Lattner00950542001-06-06 20:29:01 +0000992 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000993
Chris Lattner00950542001-06-06 20:29:01 +0000994
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000995ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000996 if (!ConstPoolSInt::isValueValidForType($1, $2))
997 ThrowException("Constant value doesn't fit in type!");
Chris Lattner30c89792001-09-07 16:35:17 +0000998 $$ = ConstPoolSInt::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000999 }
1000 | UIntType EUINT64VAL { // integral constants
1001 if (!ConstPoolUInt::isValueValidForType($1, $2))
1002 ThrowException("Constant value doesn't fit in type!");
Chris Lattner30c89792001-09-07 16:35:17 +00001003 $$ = ConstPoolUInt::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001004 }
1005 | BOOL TRUE { // Boolean constants
Chris Lattner30c89792001-09-07 16:35:17 +00001006 $$ = ConstPoolBool::True;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001007 }
1008 | BOOL FALSE { // Boolean constants
Chris Lattner30c89792001-09-07 16:35:17 +00001009 $$ = ConstPoolBool::False;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001010 }
1011 | FPType FPVAL { // Float & Double constants
Chris Lattner30c89792001-09-07 16:35:17 +00001012 $$ = ConstPoolFP::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001013 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001014
Chris Lattnere98dda62001-07-14 06:10:16 +00001015// ConstVector - A list of comma seperated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001016ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001017 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001018 }
1019 | ConstVal {
1020 $$ = new vector<ConstPoolVal*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001021 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001022 }
1023
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001024
Chris Lattner1781aca2001-09-18 04:00:54 +00001025// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
1026GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; }
1027
Chris Lattner00950542001-06-06 20:29:01 +00001028
Chris Lattnere98dda62001-07-14 06:10:16 +00001029// ConstPool - Constants with optional names assigned to them.
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001030ConstPool : ConstPool OptAssign CONST ConstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001031 if (setValueName($4, $2)) { assert(0 && "No redefinitions allowed!"); }
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001032 InsertValue($4);
Chris Lattner00950542001-06-06 20:29:01 +00001033 }
Chris Lattner30c89792001-09-07 16:35:17 +00001034 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner1781aca2001-09-18 04:00:54 +00001035 // TODO: FIXME when Type are not const
Chris Lattnerb7474512001-10-03 15:39:04 +00001036 if (!setValueName(const_cast<Type*>($4->get()), $2)) {
1037 // If this is not a redefinition of a type...
1038 if (!$2) {
1039 InsertType($4->get(),
1040 inMethodScope() ? CurMeth.Types : CurModule.Types);
1041 }
1042 delete $4;
Chris Lattner1781aca2001-09-18 04:00:54 +00001043
Chris Lattnerb7474512001-10-03 15:39:04 +00001044 ResolveSomeTypes(inMethodScope() ? CurMeth.LateResolveTypes :
1045 CurModule.LateResolveTypes);
Chris Lattner30c89792001-09-07 16:35:17 +00001046 }
Chris Lattner30c89792001-09-07 16:35:17 +00001047 }
1048 | ConstPool MethodProto { // Method prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001049 }
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001050 | ConstPool OptAssign GlobalType ConstVal {
Chris Lattner1781aca2001-09-18 04:00:54 +00001051 const Type *Ty = $4->getType();
1052 // Global declarations appear in Constant Pool
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001053 ConstPoolVal *Initializer = $4;
Chris Lattner1781aca2001-09-18 04:00:54 +00001054 if (Initializer == 0)
1055 ThrowException("Global value initializer is not a constant!");
1056
Chris Lattneref9c23f2001-10-03 14:53:21 +00001057 GlobalVariable *GV = new GlobalVariable(Ty, $3, Initializer);
Chris Lattnerb7474512001-10-03 15:39:04 +00001058 if (!setValueName(GV, $2)) { // If not redefining...
1059 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001060 int Slot = InsertValue(GV, CurModule.Values);
1061
1062 if (Slot != -1) {
1063 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1064 } else {
1065 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1066 (char*)GV->getName().c_str()));
1067 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001068 }
Chris Lattner1781aca2001-09-18 04:00:54 +00001069 }
1070 | ConstPool OptAssign UNINIT GlobalType Types {
1071 const Type *Ty = *$5;
1072 // Global declarations appear in Constant Pool
Chris Lattnercfe26c92001-10-01 18:26:53 +00001073 if (isa<ArrayType>(Ty) && cast<ArrayType>(Ty)->isUnsized()) {
Chris Lattner1781aca2001-09-18 04:00:54 +00001074 ThrowException("Type '" + Ty->getDescription() +
1075 "' is not a sized type!");
Chris Lattner70cc3392001-09-10 07:58:01 +00001076 }
Chris Lattner1781aca2001-09-18 04:00:54 +00001077
Chris Lattneref9c23f2001-10-03 14:53:21 +00001078 GlobalVariable *GV = new GlobalVariable(Ty, $4);
Chris Lattnerb7474512001-10-03 15:39:04 +00001079 if (!setValueName(GV, $2)) { // If not redefining...
1080 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001081 int Slot = InsertValue(GV, CurModule.Values);
1082
1083 if (Slot != -1) {
1084 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1085 } else {
1086 assert(GV->hasName() && "Not named and not numbered!?");
1087 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1088 (char*)GV->getName().c_str()));
1089 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001090 }
Chris Lattnere98dda62001-07-14 06:10:16 +00001091 }
Chris Lattner00950542001-06-06 20:29:01 +00001092 | /* empty: end of list */ {
1093 }
1094
1095
1096//===----------------------------------------------------------------------===//
1097// Rules to match Modules
1098//===----------------------------------------------------------------------===//
1099
1100// Module rule: Capture the result of parsing the whole file into a result
1101// variable...
1102//
1103Module : MethodList {
1104 $$ = ParserResult = $1;
1105 CurModule.ModuleDone();
1106}
1107
Chris Lattnere98dda62001-07-14 06:10:16 +00001108// MethodList - A list of methods, preceeded by a constant pool.
1109//
Chris Lattner00950542001-06-06 20:29:01 +00001110MethodList : MethodList Method {
Chris Lattner00950542001-06-06 20:29:01 +00001111 $$ = $1;
Chris Lattnere1815642001-07-15 06:35:53 +00001112 if (!$2->getParent())
1113 $1->getMethodList().push_back($2);
1114 CurMeth.MethodDone();
Chris Lattner00950542001-06-06 20:29:01 +00001115 }
Chris Lattnere1815642001-07-15 06:35:53 +00001116 | MethodList MethodProto {
1117 $$ = $1;
Chris Lattnere1815642001-07-15 06:35:53 +00001118 }
Chris Lattner00950542001-06-06 20:29:01 +00001119 | ConstPool IMPLEMENTATION {
1120 $$ = CurModule.CurrentModule;
Chris Lattner30c89792001-09-07 16:35:17 +00001121 // Resolve circular types before we parse the body of the module
1122 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner00950542001-06-06 20:29:01 +00001123 }
1124
1125
1126//===----------------------------------------------------------------------===//
1127// Rules to match Method Headers
1128//===----------------------------------------------------------------------===//
1129
1130OptVAR_ID : VAR_ID | /*empty*/ { $$ = 0; }
1131
1132ArgVal : Types OptVAR_ID {
Chris Lattner30c89792001-09-07 16:35:17 +00001133 $$ = new MethodArgument(*$1); delete $1;
Chris Lattnerb7474512001-10-03 15:39:04 +00001134 if (setValueName($$, $2)) { assert(0 && "No arg redef allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001135}
1136
1137ArgListH : ArgVal ',' ArgListH {
1138 $$ = $3;
1139 $3->push_front($1);
1140 }
1141 | ArgVal {
1142 $$ = new list<MethodArgument*>();
1143 $$->push_front($1);
1144 }
Chris Lattner8b81bf52001-07-25 22:47:46 +00001145 | DOTDOTDOT {
1146 $$ = new list<MethodArgument*>();
Chris Lattner2079fde2001-10-13 06:41:08 +00001147 $$->push_front(new MethodArgument(Type::VoidTy));
Chris Lattner8b81bf52001-07-25 22:47:46 +00001148 }
Chris Lattner00950542001-06-06 20:29:01 +00001149
1150ArgList : ArgListH {
1151 $$ = $1;
1152 }
1153 | /* empty */ {
1154 $$ = 0;
1155 }
1156
1157MethodHeaderH : TypesV STRINGCONSTANT '(' ArgList ')' {
Chris Lattner93750fa2001-07-28 17:48:55 +00001158 UnEscapeLexed($2);
Chris Lattner30c89792001-09-07 16:35:17 +00001159 vector<const Type*> ParamTypeList;
Chris Lattner00950542001-06-06 20:29:01 +00001160 if ($4)
Chris Lattner7fc9fe32001-06-27 23:41:11 +00001161 for (list<MethodArgument*>::iterator I = $4->begin(); I != $4->end(); ++I)
Chris Lattner00950542001-06-06 20:29:01 +00001162 ParamTypeList.push_back((*I)->getType());
1163
Chris Lattner2079fde2001-10-13 06:41:08 +00001164 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1165 if (isVarArg) ParamTypeList.pop_back();
1166
1167 const MethodType *MT = MethodType::get(*$1, ParamTypeList, isVarArg);
Chris Lattneref9c23f2001-10-03 14:53:21 +00001168 const PointerType *PMT = PointerType::get(MT);
Chris Lattner30c89792001-09-07 16:35:17 +00001169 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001170
Chris Lattnere1815642001-07-15 06:35:53 +00001171 Method *M = 0;
1172 if (SymbolTable *ST = CurModule.CurrentModule->getSymbolTable()) {
Chris Lattneref9c23f2001-10-03 14:53:21 +00001173 if (Value *V = ST->lookup(PMT, $2)) { // Method already in symtab?
1174 M = cast<Method>(V);
Chris Lattner00950542001-06-06 20:29:01 +00001175
Chris Lattnere1815642001-07-15 06:35:53 +00001176 // Yes it is. If this is the case, either we need to be a forward decl,
1177 // or it needs to be.
1178 if (!CurMeth.isDeclare && !M->isExternal())
1179 ThrowException("Redefinition of method '" + string($2) + "'!");
1180 }
1181 }
1182
1183 if (M == 0) { // Not already defined?
1184 M = new Method(MT, $2);
1185 InsertValue(M, CurModule.Values);
Chris Lattner2079fde2001-10-13 06:41:08 +00001186 CurModule.DeclareNewGlobalValue(M, ValID::create($2));
Chris Lattnere1815642001-07-15 06:35:53 +00001187 }
1188
1189 free($2); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +00001190
1191 CurMeth.MethodStart(M);
1192
1193 // Add all of the arguments we parsed to the method...
Chris Lattnere1815642001-07-15 06:35:53 +00001194 if ($4 && !CurMeth.isDeclare) { // Is null if empty...
Chris Lattner00950542001-06-06 20:29:01 +00001195 Method::ArgumentListType &ArgList = M->getArgumentList();
1196
Chris Lattner7fc9fe32001-06-27 23:41:11 +00001197 for (list<MethodArgument*>::iterator I = $4->begin(); I != $4->end(); ++I) {
Chris Lattner00950542001-06-06 20:29:01 +00001198 InsertValue(*I);
1199 ArgList.push_back(*I);
1200 }
1201 delete $4; // We're now done with the argument list
1202 }
1203}
1204
1205MethodHeader : MethodHeaderH ConstPool BEGINTOK {
1206 $$ = CurMeth.CurrentMethod;
Chris Lattner30c89792001-09-07 16:35:17 +00001207
1208 // Resolve circular types before we parse the body of the method.
1209 ResolveTypes(CurMeth.LateResolveTypes);
Chris Lattner00950542001-06-06 20:29:01 +00001210}
1211
1212Method : BasicBlockList END {
1213 $$ = $1;
1214}
1215
Chris Lattnere1815642001-07-15 06:35:53 +00001216MethodProto : DECLARE { CurMeth.isDeclare = true; } MethodHeaderH {
1217 $$ = CurMeth.CurrentMethod;
Chris Lattner93750fa2001-07-28 17:48:55 +00001218 if (!$$->getParent())
1219 CurModule.CurrentModule->getMethodList().push_back($$);
1220 CurMeth.MethodDone();
Chris Lattnere1815642001-07-15 06:35:53 +00001221}
Chris Lattner00950542001-06-06 20:29:01 +00001222
1223//===----------------------------------------------------------------------===//
1224// Rules to match Basic Blocks
1225//===----------------------------------------------------------------------===//
1226
1227ConstValueRef : ESINT64VAL { // A reference to a direct constant
1228 $$ = ValID::create($1);
1229 }
1230 | EUINT64VAL {
1231 $$ = ValID::create($1);
1232 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001233 | FPVAL { // Perhaps it's an FP constant?
1234 $$ = ValID::create($1);
1235 }
Chris Lattner00950542001-06-06 20:29:01 +00001236 | TRUE {
1237 $$ = ValID::create((int64_t)1);
1238 }
1239 | FALSE {
1240 $$ = ValID::create((int64_t)0);
1241 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001242 | NULL_TOK {
1243 $$ = ValID::createNull();
1244 }
1245
Chris Lattner93750fa2001-07-28 17:48:55 +00001246/*
Chris Lattner00950542001-06-06 20:29:01 +00001247 | STRINGCONSTANT { // Quoted strings work too... especially for methods
1248 $$ = ValID::create_conststr($1);
1249 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001250*/
Chris Lattner00950542001-06-06 20:29:01 +00001251
Chris Lattner2079fde2001-10-13 06:41:08 +00001252// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1253// another value.
1254//
1255SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001256 $$ = ValID::create($1);
1257 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001258 | VAR_ID { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001259 $$ = ValID::create($1);
1260 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001261
1262// ValueRef - A reference to a definition... either constant or symbolic
1263ValueRef : SymbolicValueRef | ConstValueRef
1264
Chris Lattner00950542001-06-06 20:29:01 +00001265
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001266// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1267// type immediately preceeds the value reference, and allows complex constant
1268// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001269ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001270 $$ = getVal(*$1, $2); delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +00001271 }
Chris Lattner8b81bf52001-07-25 22:47:46 +00001272
Chris Lattner00950542001-06-06 20:29:01 +00001273
1274BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner89219832001-10-03 19:35:04 +00001275 ($$ = $1)->getBasicBlocks().push_back($2);
Chris Lattner00950542001-06-06 20:29:01 +00001276 }
1277 | MethodHeader BasicBlock { // Do not allow methods with 0 basic blocks
Chris Lattner89219832001-10-03 19:35:04 +00001278 ($$ = $1)->getBasicBlocks().push_back($2);
Chris Lattner00950542001-06-06 20:29:01 +00001279 }
1280
1281
1282// Basic blocks are terminated by branching instructions:
1283// br, br/cc, switch, ret
1284//
Chris Lattner2079fde2001-10-13 06:41:08 +00001285BasicBlock : InstructionList OptAssign BBTerminatorInst {
1286 if (setValueName($3, $2)) { assert(0 && "No redefn allowed!"); }
1287 InsertValue($3);
1288
1289 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001290 InsertValue($1);
1291 $$ = $1;
1292 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001293 | LABELSTR InstructionList OptAssign BBTerminatorInst {
1294 if (setValueName($4, $3)) { assert(0 && "No redefn allowed!"); }
1295 InsertValue($4);
1296
1297 $2->getInstList().push_back($4);
Chris Lattnerb7474512001-10-03 15:39:04 +00001298 if (setValueName($2, $1)) { assert(0 && "No label redef allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001299
1300 InsertValue($2);
1301 $$ = $2;
1302 }
1303
1304InstructionList : InstructionList Inst {
1305 $1->getInstList().push_back($2);
1306 $$ = $1;
1307 }
1308 | /* empty */ {
1309 $$ = new BasicBlock();
1310 }
1311
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001312BBTerminatorInst : RET ResolvedVal { // Return with a result...
1313 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001314 }
1315 | RET VOID { // Return with no result...
1316 $$ = new ReturnInst();
1317 }
1318 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner9636a912001-10-01 16:18:37 +00001319 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $3)));
Chris Lattner00950542001-06-06 20:29:01 +00001320 } // Conditional Branch...
1321 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner9636a912001-10-01 16:18:37 +00001322 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $6)),
1323 cast<BasicBlock>(getVal(Type::LabelTy, $9)),
Chris Lattner00950542001-06-06 20:29:01 +00001324 getVal(Type::BoolTy, $3));
1325 }
1326 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1327 SwitchInst *S = new SwitchInst(getVal($2, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001328 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
Chris Lattner00950542001-06-06 20:29:01 +00001329 $$ = S;
1330
1331 list<pair<ConstPoolVal*, BasicBlock*> >::iterator I = $8->begin(),
1332 end = $8->end();
Chris Lattner7fc9fe32001-06-27 23:41:11 +00001333 for (; I != end; ++I)
Chris Lattner00950542001-06-06 20:29:01 +00001334 S->dest_push_back(I->first, I->second);
1335 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001336 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO ResolvedVal
1337 EXCEPT ResolvedVal {
1338 const PointerType *PMTy;
1339 const MethodType *Ty;
1340
1341 if (!(PMTy = dyn_cast<PointerType>($2->get())) ||
1342 !(Ty = dyn_cast<MethodType>(PMTy->getValueType()))) {
1343 // Pull out the types of all of the arguments...
1344 vector<const Type*> ParamTypes;
1345 if ($5) {
1346 for (list<Value*>::iterator I = $5->begin(), E = $5->end(); I != E; ++I)
1347 ParamTypes.push_back((*I)->getType());
1348 }
1349
1350 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1351 if (isVarArg) ParamTypes.pop_back();
1352
1353 Ty = MethodType::get($2->get(), ParamTypes, isVarArg);
1354 PMTy = PointerType::get(Ty);
1355 }
1356 delete $2;
1357
1358 Value *V = getVal(PMTy, $3); // Get the method we're calling...
1359
1360 BasicBlock *Normal = dyn_cast<BasicBlock>($8);
1361 BasicBlock *Except = dyn_cast<BasicBlock>($10);
1362
1363 if (Normal == 0 || Except == 0)
1364 ThrowException("Invoke instruction without label destinations!");
1365
1366 // Create the call node...
1367 if (!$5) { // Has no arguments?
1368 $$ = new InvokeInst(cast<Method>(V), Normal, Except, vector<Value*>());
1369 } else { // Has arguments?
1370 // Loop through MethodType's arguments and ensure they are specified
1371 // correctly!
1372 //
1373 MethodType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1374 MethodType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
1375 list<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
1376
1377 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1378 if ((*ArgI)->getType() != *I)
1379 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
1380 (*I)->getName() + "'!");
1381
1382 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1383 ThrowException("Invalid number of parameters detected!");
1384
1385 $$ = new InvokeInst(cast<Method>(V), Normal, Except,
1386 vector<Value*>($5->begin(), $5->end()));
1387 }
1388 delete $5;
1389 }
1390
1391
Chris Lattner00950542001-06-06 20:29:01 +00001392
1393JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1394 $$ = $1;
Chris Lattner2079fde2001-10-13 06:41:08 +00001395 ConstPoolVal *V = cast<ConstPoolVal>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001396 if (V == 0)
1397 ThrowException("May only switch on a constant pool value!");
1398
Chris Lattner9636a912001-10-01 16:18:37 +00001399 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($5, $6))));
Chris Lattner00950542001-06-06 20:29:01 +00001400 }
1401 | IntType ConstValueRef ',' LABEL ValueRef {
1402 $$ = new list<pair<ConstPoolVal*, BasicBlock*> >();
Chris Lattner2079fde2001-10-13 06:41:08 +00001403 ConstPoolVal *V = cast<ConstPoolVal>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00001404
1405 if (V == 0)
1406 ThrowException("May only switch on a constant pool value!");
1407
Chris Lattner9636a912001-10-01 16:18:37 +00001408 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($4, $5))));
Chris Lattner00950542001-06-06 20:29:01 +00001409 }
1410
1411Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001412 // Is this definition named?? if so, assign the name...
1413 if (setValueName($2, $1)) { assert(0 && "No redefin allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001414 InsertValue($2);
1415 $$ = $2;
1416}
1417
Chris Lattnerc24d2082001-06-11 15:04:20 +00001418PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
1419 $$ = new list<pair<Value*, BasicBlock*> >();
Chris Lattner30c89792001-09-07 16:35:17 +00001420 $$->push_back(make_pair(getVal(*$1, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001421 cast<BasicBlock>(getVal(Type::LabelTy, $5))));
Chris Lattner30c89792001-09-07 16:35:17 +00001422 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001423 }
1424 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1425 $$ = $1;
1426 $1->push_back(make_pair(getVal($1->front().first->getType(), $4),
Chris Lattner9636a912001-10-01 16:18:37 +00001427 cast<BasicBlock>(getVal(Type::LabelTy, $6))));
Chris Lattnerc24d2082001-06-11 15:04:20 +00001428 }
1429
1430
Chris Lattner30c89792001-09-07 16:35:17 +00001431ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner00950542001-06-06 20:29:01 +00001432 $$ = new list<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001433 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001434 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001435 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001436 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001437 $1->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001438 }
1439
1440// ValueRefListE - Just like ValueRefList, except that it may also be empty!
1441ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; }
1442
1443InstVal : BinaryOps Types ValueRef ',' ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001444 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001445 if ($$ == 0)
1446 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001447 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001448 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001449 | UnaryOps ResolvedVal {
1450 $$ = UnaryOperator::create($1, $2);
Chris Lattner00950542001-06-06 20:29:01 +00001451 if ($$ == 0)
1452 ThrowException("unary operator returned null!");
Chris Lattner09083092001-07-08 04:57:15 +00001453 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001454 | ShiftOps ResolvedVal ',' ResolvedVal {
1455 if ($4->getType() != Type::UByteTy)
1456 ThrowException("Shift amount must be ubyte!");
1457 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001458 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001459 | CAST ResolvedVal TO Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001460 $$ = new CastInst($2, *$4);
1461 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001462 }
Chris Lattnerc24d2082001-06-11 15:04:20 +00001463 | PHI PHIList {
1464 const Type *Ty = $2->front().first->getType();
1465 $$ = new PHINode(Ty);
Chris Lattner00950542001-06-06 20:29:01 +00001466 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001467 if ($2->front().first->getType() != Ty)
1468 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001469 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001470 $2->pop_front();
1471 }
1472 delete $2; // Free the list...
1473 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001474 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattneref9c23f2001-10-03 14:53:21 +00001475 const PointerType *PMTy;
Chris Lattner8b81bf52001-07-25 22:47:46 +00001476 const MethodType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001477
Chris Lattneref9c23f2001-10-03 14:53:21 +00001478 if (!(PMTy = dyn_cast<PointerType>($2->get())) ||
1479 !(Ty = dyn_cast<MethodType>(PMTy->getValueType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001480 // Pull out the types of all of the arguments...
1481 vector<const Type*> ParamTypes;
Chris Lattneref9c23f2001-10-03 14:53:21 +00001482 if ($5) {
1483 for (list<Value*>::iterator I = $5->begin(), E = $5->end(); I != E; ++I)
1484 ParamTypes.push_back((*I)->getType());
1485 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001486
1487 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1488 if (isVarArg) ParamTypes.pop_back();
1489
1490 Ty = MethodType::get($2->get(), ParamTypes, isVarArg);
Chris Lattneref9c23f2001-10-03 14:53:21 +00001491 PMTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001492 }
Chris Lattner30c89792001-09-07 16:35:17 +00001493 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001494
Chris Lattneref9c23f2001-10-03 14:53:21 +00001495 Value *V = getVal(PMTy, $3); // Get the method we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001496
Chris Lattner8b81bf52001-07-25 22:47:46 +00001497 // Create the call node...
1498 if (!$5) { // Has no arguments?
Chris Lattner9636a912001-10-01 16:18:37 +00001499 $$ = new CallInst(cast<Method>(V), vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001500 } else { // Has arguments?
Chris Lattner00950542001-06-06 20:29:01 +00001501 // Loop through MethodType's arguments and ensure they are specified
1502 // correctly!
1503 //
1504 MethodType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001505 MethodType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
1506 list<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
1507
1508 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1509 if ((*ArgI)->getType() != *I)
1510 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner00950542001-06-06 20:29:01 +00001511 (*I)->getName() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001512
Chris Lattner8b81bf52001-07-25 22:47:46 +00001513 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner00950542001-06-06 20:29:01 +00001514 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00001515
Chris Lattner2079fde2001-10-13 06:41:08 +00001516 $$ = new CallInst(V, vector<Value*>($5->begin(), $5->end()));
Chris Lattner8b81bf52001-07-25 22:47:46 +00001517 }
1518 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00001519 }
1520 | MemoryInst {
1521 $$ = $1;
1522 }
1523
Chris Lattner027dcc52001-07-08 21:10:27 +00001524// UByteList - List of ubyte values for load and store instructions
1525UByteList : ',' ConstVector {
1526 $$ = $2;
1527} | /* empty */ {
1528 $$ = new vector<ConstPoolVal*>();
1529}
1530
Chris Lattner00950542001-06-06 20:29:01 +00001531MemoryInst : MALLOC Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001532 $$ = new MallocInst(PointerType::get(*$2));
1533 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001534 }
1535 | MALLOC Types ',' UINT ValueRef {
Chris Lattnerb00c5822001-10-02 03:41:24 +00001536 if (!(*$2)->isArrayType() || cast<const ArrayType>($2->get())->isSized())
Chris Lattner30c89792001-09-07 16:35:17 +00001537 ThrowException("Trying to allocate " + (*$2)->getName() +
Chris Lattner00950542001-06-06 20:29:01 +00001538 " as unsized array!");
Chris Lattner30c89792001-09-07 16:35:17 +00001539 const Type *Ty = PointerType::get(*$2);
Chris Lattner8896eda2001-07-09 19:38:36 +00001540 $$ = new MallocInst(Ty, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001541 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001542 }
1543 | ALLOCA Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001544 $$ = new AllocaInst(PointerType::get(*$2));
1545 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001546 }
1547 | ALLOCA Types ',' UINT ValueRef {
Chris Lattnerb00c5822001-10-02 03:41:24 +00001548 if (!(*$2)->isArrayType() || cast<const ArrayType>($2->get())->isSized())
Chris Lattner30c89792001-09-07 16:35:17 +00001549 ThrowException("Trying to allocate " + (*$2)->getName() +
Chris Lattner00950542001-06-06 20:29:01 +00001550 " as unsized array!");
Chris Lattner30c89792001-09-07 16:35:17 +00001551 const Type *Ty = PointerType::get(*$2);
Chris Lattner00950542001-06-06 20:29:01 +00001552 Value *ArrSize = getVal($4, $5);
Chris Lattnerf0d0e9c2001-07-07 08:36:30 +00001553 $$ = new AllocaInst(Ty, ArrSize);
Chris Lattner30c89792001-09-07 16:35:17 +00001554 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001555 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001556 | FREE ResolvedVal {
1557 if (!$2->getType()->isPointerType())
1558 ThrowException("Trying to free nonpointer type " +
1559 $2->getType()->getName() + "!");
1560 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001561 }
1562
Chris Lattner027dcc52001-07-08 21:10:27 +00001563 | LOAD Types ValueRef UByteList {
Chris Lattner30c89792001-09-07 16:35:17 +00001564 if (!(*$2)->isPointerType())
Chris Lattner2079fde2001-10-13 06:41:08 +00001565 ThrowException("Can't load from nonpointer type: " +
1566 (*$2)->getDescription());
Chris Lattner30c89792001-09-07 16:35:17 +00001567 if (LoadInst::getIndexedType(*$2, *$4) == 0)
Chris Lattner027dcc52001-07-08 21:10:27 +00001568 ThrowException("Invalid indices for load instruction!");
1569
Chris Lattner30c89792001-09-07 16:35:17 +00001570 $$ = new LoadInst(getVal(*$2, $3), *$4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001571 delete $4; // Free the vector...
Chris Lattner30c89792001-09-07 16:35:17 +00001572 delete $2;
Chris Lattner027dcc52001-07-08 21:10:27 +00001573 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001574 | STORE ResolvedVal ',' Types ValueRef UByteList {
Chris Lattner30c89792001-09-07 16:35:17 +00001575 if (!(*$4)->isPointerType())
1576 ThrowException("Can't store to a nonpointer type: " + (*$4)->getName());
1577 const Type *ElTy = StoreInst::getIndexedType(*$4, *$6);
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001578 if (ElTy == 0)
1579 ThrowException("Can't store into that field list!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001580 if (ElTy != $2->getType())
1581 ThrowException("Can't store '" + $2->getType()->getName() +
1582 "' into space of type '" + ElTy->getName() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001583 $$ = new StoreInst($2, getVal(*$4, $5), *$6);
1584 delete $4; delete $6;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001585 }
1586 | GETELEMENTPTR Types ValueRef UByteList {
Chris Lattner30c89792001-09-07 16:35:17 +00001587 if (!(*$2)->isPointerType())
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001588 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner30c89792001-09-07 16:35:17 +00001589 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
1590 ThrowException("Can't get element ptr '" + (*$2)->getName() + "'!");
1591 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
1592 delete $2; delete $4;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001593 }
Chris Lattner027dcc52001-07-08 21:10:27 +00001594
Chris Lattner00950542001-06-06 20:29:01 +00001595%%
Chris Lattner09083092001-07-08 04:57:15 +00001596int yyerror(const char *ErrorMsg) {
Chris Lattner00950542001-06-06 20:29:01 +00001597 ThrowException(string("Parse error: ") + ErrorMsg);
1598 return 0;
1599}