blob: e49ffa3a3df335669fdc5260fe0d013b6a1b6108 [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
Chris Lattner1781aca2001-09-18 04:00:54 +0000466// setValueName - Set the specified value to the name given. The name may be
467// null potentially, in which case this is a noop. The string passed in is
468// assumed to be a malloc'd string buffer, and is freed by this function.
469//
Chris Lattnerb7474512001-10-03 15:39:04 +0000470// This function returns true if the value has already been defined, but is
471// allowed to be redefined in the specified context. If the name is a new name
472// for the typeplane, false is returned.
473//
474static bool setValueName(Value *V, char *NameStr) {
475 if (NameStr == 0) return false;
Chris Lattner386a3b72001-10-16 19:54:17 +0000476
Chris Lattner1781aca2001-09-18 04:00:54 +0000477 string Name(NameStr); // Copy string
478 free(NameStr); // Free old string
479
Chris Lattner2079fde2001-10-13 06:41:08 +0000480 if (V->getType() == Type::VoidTy)
481 ThrowException("Can't assign name '" + Name +
482 "' to a null valued instruction!");
483
Chris Lattnerb7474512001-10-03 15:39:04 +0000484 SymbolTable *ST = inMethodScope() ?
Chris Lattner30c89792001-09-07 16:35:17 +0000485 CurMeth.CurrentMethod->getSymbolTableSure() :
486 CurModule.CurrentModule->getSymbolTableSure();
487
488 Value *Existing = ST->lookup(V->getType(), Name);
489 if (Existing) { // Inserting a name that is already defined???
490 // There is only one case where this is allowed: when we are refining an
491 // opaque type. In this case, Existing will be an opaque type.
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000492 if (const Type *Ty = dyn_cast<const Type>(Existing)) {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000493 if (OpaqueType *OpTy = dyn_cast<OpaqueType>(Ty)) {
Chris Lattner30c89792001-09-07 16:35:17 +0000494 // We ARE replacing an opaque type!
Chris Lattnerb00c5822001-10-02 03:41:24 +0000495 OpTy->refineAbstractTypeTo(cast<Type>(V));
Chris Lattnerb7474512001-10-03 15:39:04 +0000496 return true;
Chris Lattner30c89792001-09-07 16:35:17 +0000497 }
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000498 }
Chris Lattner30c89792001-09-07 16:35:17 +0000499
Chris Lattner9636a912001-10-01 16:18:37 +0000500 // Otherwise, we are a simple redefinition of a value, check to see if it
501 // is defined the same as the old one...
502 if (const Type *Ty = dyn_cast<const Type>(Existing)) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000503 if (Ty == cast<const Type>(V)) return true; // Yes, it's equal.
504 // cerr << "Type: " << Ty->getDescription() << " != "
505 // << cast<const Type>(V)->getDescription() << "!\n";
506 } else if (GlobalVariable *EGV = dyn_cast<GlobalVariable>(Existing)) {
Chris Lattner43efcbf2001-10-03 19:35:57 +0000507 // We are allowed to redefine a global variable in two circumstances:
508 // 1. If at least one of the globals is uninitialized or
509 // 2. If both initializers have the same value.
510 //
511 // This can only be done if the const'ness of the vars is the same.
512 //
Chris Lattner89219832001-10-03 19:35:04 +0000513 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
514 if (EGV->isConstant() == GV->isConstant() &&
515 (!EGV->hasInitializer() || !GV->hasInitializer() ||
516 EGV->getInitializer() == GV->getInitializer())) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000517
Chris Lattner89219832001-10-03 19:35:04 +0000518 // Make sure the existing global version gets the initializer!
519 if (GV->hasInitializer() && !EGV->hasInitializer())
520 EGV->setInitializer(GV->getInitializer());
521
Chris Lattner2079fde2001-10-13 06:41:08 +0000522 delete GV; // Destroy the duplicate!
Chris Lattner89219832001-10-03 19:35:04 +0000523 return true; // They are equivalent!
524 }
Chris Lattnerb7474512001-10-03 15:39:04 +0000525 }
Chris Lattner9636a912001-10-01 16:18:37 +0000526 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000527 ThrowException("Redefinition of value named '" + Name + "' in the '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000528 V->getType()->getDescription() + "' type plane!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000529 }
Chris Lattner00950542001-06-06 20:29:01 +0000530
Chris Lattner30c89792001-09-07 16:35:17 +0000531 V->setName(Name, ST);
Chris Lattnerb7474512001-10-03 15:39:04 +0000532 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000533}
534
Chris Lattner8896eda2001-07-09 19:38:36 +0000535
Chris Lattner30c89792001-09-07 16:35:17 +0000536//===----------------------------------------------------------------------===//
537// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000538//
Chris Lattner8896eda2001-07-09 19:38:36 +0000539
Chris Lattner30c89792001-09-07 16:35:17 +0000540// TypeContains - Returns true if Ty contains E in it.
541//
542static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner3ff43872001-09-28 22:56:31 +0000543 return find(df_begin(Ty), df_end(Ty), E) != df_end(Ty);
Chris Lattner30c89792001-09-07 16:35:17 +0000544}
Chris Lattner698b56e2001-07-20 19:15:08 +0000545
Chris Lattner30c89792001-09-07 16:35:17 +0000546
547static vector<pair<unsigned, OpaqueType *> > UpRefs;
548
549static PATypeHolder<Type> HandleUpRefs(const Type *ty) {
550 PATypeHolder<Type> Ty(ty);
551 UR_OUT(UpRefs.size() << " upreferences active!\n");
552 for (unsigned i = 0; i < UpRefs.size(); ) {
553 UR_OUT("TypeContains(" << Ty->getDescription() << ", "
554 << UpRefs[i].second->getDescription() << ") = "
555 << TypeContains(Ty, UpRefs[i].second) << endl);
556 if (TypeContains(Ty, UpRefs[i].second)) {
557 unsigned Level = --UpRefs[i].first; // Decrement level of upreference
558 UR_OUT("Uplevel Ref Level = " << Level << endl);
559 if (Level == 0) { // Upreference should be resolved!
560 UR_OUT("About to resolve upreference!\n";
561 string OldName = UpRefs[i].second->getDescription());
562 UpRefs[i].second->refineAbstractTypeTo(Ty);
563 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
564 UR_OUT("Type '" << OldName << "' refined upreference to: "
565 << (const void*)Ty << ", " << Ty->getDescription() << endl);
566 continue;
567 }
568 }
569
570 ++i; // Otherwise, no resolve, move on...
Chris Lattner8896eda2001-07-09 19:38:36 +0000571 }
Chris Lattner30c89792001-09-07 16:35:17 +0000572 // FIXME: TODO: this should return the updated type
Chris Lattner8896eda2001-07-09 19:38:36 +0000573 return Ty;
574}
575
Chris Lattner30c89792001-09-07 16:35:17 +0000576template <class TypeTy>
577inline static void TypeDone(PATypeHolder<TypeTy> *Ty) {
578 if (UpRefs.size())
579 ThrowException("Invalid upreference in type: " + (*Ty)->getDescription());
580}
581
582// newTH - Allocate a new type holder for the specified type
583template <class TypeTy>
584inline static PATypeHolder<TypeTy> *newTH(const TypeTy *Ty) {
585 return new PATypeHolder<TypeTy>(Ty);
586}
587template <class TypeTy>
588inline static PATypeHolder<TypeTy> *newTH(const PATypeHolder<TypeTy> &TH) {
589 return new PATypeHolder<TypeTy>(TH);
590}
591
592
Chris Lattner00950542001-06-06 20:29:01 +0000593//===----------------------------------------------------------------------===//
594// RunVMAsmParser - Define an interface to this parser
595//===----------------------------------------------------------------------===//
596//
Chris Lattnera2850432001-07-22 18:36:00 +0000597Module *RunVMAsmParser(const string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000598 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000599 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000600 llvmAsmlineno = 1; // Reset the current line number...
601
602 CurModule.CurrentModule = new Module(); // Allocate a new module to read
603 yyparse(); // Parse the file.
604 Module *Result = ParserResult;
Chris Lattner00950542001-06-06 20:29:01 +0000605 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
606 ParserResult = 0;
607
608 return Result;
609}
610
611%}
612
613%union {
Chris Lattner30c89792001-09-07 16:35:17 +0000614 Module *ModuleVal;
615 Method *MethodVal;
616 MethodArgument *MethArgVal;
617 BasicBlock *BasicBlockVal;
618 TerminatorInst *TermInstVal;
619 Instruction *InstVal;
620 ConstPoolVal *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000621
Chris Lattner30c89792001-09-07 16:35:17 +0000622 const Type *PrimType;
623 PATypeHolder<Type> *TypeVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000624 Value *ValueVal;
625
626 list<MethodArgument*> *MethodArgList;
627 list<Value*> *ValueList;
628 list<PATypeHolder<Type> > *TypeList;
Chris Lattnerc24d2082001-06-11 15:04:20 +0000629 list<pair<Value*, BasicBlock*> > *PHIList; // Represent the RHS of PHI node
Chris Lattner00950542001-06-06 20:29:01 +0000630 list<pair<ConstPoolVal*, BasicBlock*> > *JumpTable;
Chris Lattner30c89792001-09-07 16:35:17 +0000631 vector<ConstPoolVal*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000632
Chris Lattner30c89792001-09-07 16:35:17 +0000633 int64_t SInt64Val;
634 uint64_t UInt64Val;
635 int SIntVal;
636 unsigned UIntVal;
637 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000638 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000639
Chris Lattner30c89792001-09-07 16:35:17 +0000640 char *StrVal; // This memory is strdup'd!
641 ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000642
Chris Lattner30c89792001-09-07 16:35:17 +0000643 Instruction::UnaryOps UnaryOpVal;
644 Instruction::BinaryOps BinaryOpVal;
645 Instruction::TermOps TermOpVal;
646 Instruction::MemoryOps MemOpVal;
647 Instruction::OtherOps OtherOpVal;
Chris Lattner00950542001-06-06 20:29:01 +0000648}
649
650%type <ModuleVal> Module MethodList
Chris Lattnere1815642001-07-15 06:35:53 +0000651%type <MethodVal> Method MethodProto MethodHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000652%type <BasicBlockVal> BasicBlock InstructionList
653%type <TermInstVal> BBTerminatorInst
654%type <InstVal> Inst InstVal MemoryInst
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000655%type <ConstVal> ConstVal
Chris Lattner027dcc52001-07-08 21:10:27 +0000656%type <ConstVector> ConstVector UByteList
Chris Lattner00950542001-06-06 20:29:01 +0000657%type <MethodArgList> ArgList ArgListH
658%type <MethArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000659%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000660%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner30c89792001-09-07 16:35:17 +0000661%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000662%type <JumpTable> JumpTable
Chris Lattner1781aca2001-09-18 04:00:54 +0000663%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner00950542001-06-06 20:29:01 +0000664
Chris Lattner2079fde2001-10-13 06:41:08 +0000665// ValueRef - Unresolved reference to a definition or BB
666%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000667%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000668// Tokens and types for handling constant integer values
669//
670// ESINT64VAL - A negative number within long long range
671%token <SInt64Val> ESINT64VAL
672
673// EUINT64VAL - A positive number within uns. long long range
674%token <UInt64Val> EUINT64VAL
675%type <SInt64Val> EINT64VAL
676
677%token <SIntVal> SINTVAL // Signed 32 bit ints...
678%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
679%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000680%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000681
682// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000683%type <TypeVal> Types TypesV UpRTypes UpRTypesV
684%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
685%token <TypeVal> OPAQUE
686%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
687%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000688
689%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
690%type <StrVal> OptVAR_ID OptAssign
691
692
Chris Lattner1781aca2001-09-18 04:00:54 +0000693%token IMPLEMENTATION TRUE FALSE BEGINTOK END DECLARE GLOBAL CONSTANT UNINIT
Chris Lattner2079fde2001-10-13 06:41:08 +0000694%token TO EXCEPT DOTDOTDOT STRING NULL_TOK CONST
Chris Lattner00950542001-06-06 20:29:01 +0000695
696// Basic Block Terminating Operators
697%token <TermOpVal> RET BR SWITCH
698
699// Unary Operators
700%type <UnaryOpVal> UnaryOps // all the unary operators
Chris Lattner71496b32001-07-08 19:03:27 +0000701%token <UnaryOpVal> NOT
Chris Lattner00950542001-06-06 20:29:01 +0000702
703// Binary Operators
704%type <BinaryOpVal> BinaryOps // all the binary operators
Chris Lattner42c9e772001-10-20 09:32:59 +0000705%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000706%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000707
708// Memory Instructions
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000709%token <MemoryOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000710
Chris Lattner027dcc52001-07-08 21:10:27 +0000711// Other Operators
712%type <OtherOpVal> ShiftOps
Chris Lattner2079fde2001-10-13 06:41:08 +0000713%token <OtherOpVal> PHI CALL INVOKE CAST SHL SHR
Chris Lattner027dcc52001-07-08 21:10:27 +0000714
Chris Lattner00950542001-06-06 20:29:01 +0000715%start Module
716%%
717
718// Handle constant integer size restriction and conversion...
719//
720
721INTVAL : SINTVAL
722INTVAL : UINTVAL {
723 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
724 ThrowException("Value too large for type!");
725 $$ = (int32_t)$1;
726}
727
728
729EINT64VAL : ESINT64VAL // These have same type and can't cause problems...
730EINT64VAL : EUINT64VAL {
731 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
732 ThrowException("Value too large for type!");
733 $$ = (int64_t)$1;
734}
735
Chris Lattner00950542001-06-06 20:29:01 +0000736// Operations that are notably excluded from this list include:
737// RET, BR, & SWITCH because they end basic blocks and are treated specially.
738//
Chris Lattner09083092001-07-08 04:57:15 +0000739UnaryOps : NOT
Chris Lattner42c9e772001-10-20 09:32:59 +0000740BinaryOps : ADD | SUB | MUL | DIV | REM | AND | OR | XOR
Chris Lattner00950542001-06-06 20:29:01 +0000741BinaryOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE
Chris Lattner027dcc52001-07-08 21:10:27 +0000742ShiftOps : SHL | SHR
Chris Lattner00950542001-06-06 20:29:01 +0000743
Chris Lattnere98dda62001-07-14 06:10:16 +0000744// These are some types that allow classification if we only want a particular
745// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner00950542001-06-06 20:29:01 +0000746SIntType : LONG | INT | SHORT | SBYTE
747UIntType : ULONG | UINT | USHORT | UBYTE
Chris Lattner30c89792001-09-07 16:35:17 +0000748IntType : SIntType | UIntType
749FPType : FLOAT | DOUBLE
Chris Lattner00950542001-06-06 20:29:01 +0000750
Chris Lattnere98dda62001-07-14 06:10:16 +0000751// OptAssign - Value producing statements have an optional assignment component
Chris Lattner00950542001-06-06 20:29:01 +0000752OptAssign : VAR_ID '=' {
753 $$ = $1;
754 }
755 | /*empty*/ {
756 $$ = 0;
757 }
758
Chris Lattner30c89792001-09-07 16:35:17 +0000759
760//===----------------------------------------------------------------------===//
761// Types includes all predefined types... except void, because it can only be
762// used in specific contexts (method returning void for example). To have
763// access to it, a user must explicitly use TypesV.
764//
765
766// TypesV includes all of 'Types', but it also includes the void type.
767TypesV : Types | VOID { $$ = newTH($1); }
768UpRTypesV : UpRTypes | VOID { $$ = newTH($1); }
769
770Types : UpRTypes {
771 TypeDone($$ = $1);
772 }
773
774
775// Derived types are added later...
776//
777PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT
778PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL
779UpRTypes : OPAQUE | PrimType { $$ = newTH($1); }
780UpRTypes : ValueRef { // Named types are also simple types...
781 $$ = newTH(getTypeVal($1));
782}
783
Chris Lattner30c89792001-09-07 16:35:17 +0000784// Include derived types in the Types production.
785//
786UpRTypes : '\\' EUINT64VAL { // Type UpReference
787 if ($2 > (uint64_t)INT64_MAX) ThrowException("Value out of range!");
788 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
789 UpRefs.push_back(make_pair((unsigned)$2, OT)); // Add to vector...
790 $$ = newTH<Type>(OT);
791 UR_OUT("New Upreference!\n");
792 }
793 | UpRTypesV '(' ArgTypeListI ')' { // Method derived type?
794 vector<const Type*> Params;
795 mapto($3->begin(), $3->end(), back_inserter(Params),
796 mem_fun_ref(&PATypeHandle<Type>::get));
Chris Lattner2079fde2001-10-13 06:41:08 +0000797 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
798 if (isVarArg) Params.pop_back();
799
800 $$ = newTH(HandleUpRefs(MethodType::get(*$1, Params, isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +0000801 delete $3; // Delete the argument list
802 delete $1; // Delete the old type handle
803 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000804 | '[' UpRTypesV ']' { // Unsized array type?
805 $$ = newTH<Type>(HandleUpRefs(ArrayType::get(*$2)));
806 delete $2;
Chris Lattner30c89792001-09-07 16:35:17 +0000807 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000808 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
809 $$ = newTH<Type>(HandleUpRefs(ArrayType::get(*$4, (int)$2)));
810 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +0000811 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000812 | '{' TypeListI '}' { // Structure type?
813 vector<const Type*> Elements;
814 mapto($2->begin(), $2->end(), back_inserter(Elements),
815 mem_fun_ref(&PATypeHandle<Type>::get));
Chris Lattner30c89792001-09-07 16:35:17 +0000816
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000817 $$ = newTH<Type>(HandleUpRefs(StructType::get(Elements)));
818 delete $2;
819 }
820 | '{' '}' { // Empty structure type?
821 $$ = newTH<Type>(StructType::get(vector<const Type*>()));
822 }
823 | UpRTypes '*' { // Pointer type?
824 $$ = newTH<Type>(HandleUpRefs(PointerType::get(*$1)));
825 delete $1;
826 }
Chris Lattner30c89792001-09-07 16:35:17 +0000827
828// TypeList - Used for struct declarations and as a basis for method type
829// declaration type lists
830//
831TypeListI : UpRTypes {
832 $$ = new list<PATypeHolder<Type> >();
833 $$->push_back(*$1); delete $1;
834 }
835 | TypeListI ',' UpRTypes {
836 ($$=$1)->push_back(*$3); delete $3;
837 }
838
839// ArgTypeList - List of types for a method type declaration...
840ArgTypeListI : TypeListI
841 | TypeListI ',' DOTDOTDOT {
842 ($$=$1)->push_back(Type::VoidTy);
843 }
844 | DOTDOTDOT {
845 ($$ = new list<PATypeHolder<Type> >())->push_back(Type::VoidTy);
846 }
847 | /*empty*/ {
848 $$ = new list<PATypeHolder<Type> >();
849 }
850
851
Chris Lattnere98dda62001-07-14 06:10:16 +0000852// ConstVal - The various declarations that go into the constant pool. This
853// includes all forward declarations of types, constants, and functions.
854//
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000855ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
856 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
857 if (ATy == 0)
858 ThrowException("Cannot make array constant with type: '" +
859 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +0000860 const Type *ETy = ATy->getElementType();
861 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +0000862
Chris Lattner30c89792001-09-07 16:35:17 +0000863 // Verify that we have the correct size...
864 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +0000865 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattner30c89792001-09-07 16:35:17 +0000866 utostr($3->size()) + " arguments, but has size of " +
867 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +0000868
Chris Lattner30c89792001-09-07 16:35:17 +0000869 // Verify all elements are correct type!
870 for (unsigned i = 0; i < $3->size(); i++) {
871 if (ETy != (*$3)[i]->getType())
Chris Lattner00950542001-06-06 20:29:01 +0000872 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000873 ETy->getName() + "' as required!\nIt is of type '" +
874 (*$3)[i]->getType()->getName() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +0000875 }
876
Chris Lattner30c89792001-09-07 16:35:17 +0000877 $$ = ConstPoolArray::get(ATy, *$3);
878 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000879 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000880 | Types '[' ']' {
881 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
882 if (ATy == 0)
883 ThrowException("Cannot make array constant with type: '" +
884 (*$1)->getDescription() + "'!");
885
886 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +0000887 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +0000888 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattner30c89792001-09-07 16:35:17 +0000889 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000890 $$ = ConstPoolArray::get(ATy, vector<ConstPoolVal*>());
Chris Lattner30c89792001-09-07 16:35:17 +0000891 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +0000892 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000893 | Types 'c' STRINGCONSTANT {
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
Chris Lattner30c89792001-09-07 16:35:17 +0000899 int NumElements = ATy->getNumElements();
900 const Type *ETy = ATy->getElementType();
901 char *EndStr = UnEscapeLexed($3, true);
902 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +0000903 ThrowException("Can't build string constant of size " +
Chris Lattner30c89792001-09-07 16:35:17 +0000904 itostr((int)(EndStr-$3)) +
905 " when array has size " + itostr(NumElements) + "!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000906 vector<ConstPoolVal*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +0000907 if (ETy == Type::SByteTy) {
908 for (char *C = $3; C != EndStr; ++C)
909 Vals.push_back(ConstPoolSInt::get(ETy, *C));
910 } else if (ETy == Type::UByteTy) {
911 for (char *C = $3; C != EndStr; ++C)
912 Vals.push_back(ConstPoolUInt::get(ETy, *C));
Chris Lattner93750fa2001-07-28 17:48:55 +0000913 } else {
Chris Lattner30c89792001-09-07 16:35:17 +0000914 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +0000915 ThrowException("Cannot build string arrays of non byte sized elements!");
916 }
Chris Lattner30c89792001-09-07 16:35:17 +0000917 free($3);
918 $$ = ConstPoolArray::get(ATy, Vals);
919 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +0000920 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000921 | Types '{' ConstVector '}' {
922 const StructType *STy = dyn_cast<const StructType>($1->get());
923 if (STy == 0)
924 ThrowException("Cannot make struct constant with type: '" +
925 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +0000926 // FIXME: TODO: Check to see that the constants are compatible with the type
927 // initializer!
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000928 $$ = ConstPoolStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +0000929 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000930 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000931 | Types NULL_TOK {
932 const PointerType *PTy = dyn_cast<const PointerType>($1->get());
933 if (PTy == 0)
934 ThrowException("Cannot make null pointer constant with type: '" +
935 (*$1)->getDescription() + "'!");
936
Chris Lattner2079fde2001-10-13 06:41:08 +0000937 $$ = ConstPoolPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000938 delete $1;
939 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000940 | Types SymbolicValueRef {
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000941 const PointerType *Ty = dyn_cast<const PointerType>($1->get());
942 if (Ty == 0)
943 ThrowException("Global const reference must be a pointer type!");
944
Chris Lattner2079fde2001-10-13 06:41:08 +0000945 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000946
Chris Lattner2079fde2001-10-13 06:41:08 +0000947 // If this is an initializer for a constant pointer, which is referencing a
948 // (currently) undefined variable, create a stub now that shall be replaced
949 // in the future with the right type of variable.
950 //
951 if (V == 0) {
952 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
953 const PointerType *PT = cast<PointerType>(Ty);
954
955 // First check to see if the forward references value is already created!
956 PerModuleInfo::GlobalRefsType::iterator I =
957 CurModule.GlobalRefs.find(make_pair(PT, $2));
958
959 if (I != CurModule.GlobalRefs.end()) {
960 V = I->second; // Placeholder already exists, use it...
961 } else {
962 // TODO: Include line number info by creating a subclass of
963 // TODO: GlobalVariable here that includes the said information!
964
965 // Create a placeholder for the global variable reference...
966 GlobalVariable *GV = new GlobalVariable(PT->getValueType(), false);
967 // Keep track of the fact that we have a forward ref to recycle it
968 CurModule.GlobalRefs.insert(make_pair(make_pair(PT, $2), GV));
969
970 // Must temporarily push this value into the module table...
971 CurModule.CurrentModule->getGlobalList().push_back(GV);
972 V = GV;
973 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000974 }
975
Chris Lattner2079fde2001-10-13 06:41:08 +0000976 GlobalValue *GV = cast<GlobalValue>(V);
Chris Lattnerc18545d2001-10-15 13:21:42 +0000977 $$ = ConstPoolPointerRef::get(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +0000978 delete $1; // Free the type handle
Chris Lattner00950542001-06-06 20:29:01 +0000979 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000980
Chris Lattner00950542001-06-06 20:29:01 +0000981
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000982ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000983 if (!ConstPoolSInt::isValueValidForType($1, $2))
984 ThrowException("Constant value doesn't fit in type!");
Chris Lattner30c89792001-09-07 16:35:17 +0000985 $$ = ConstPoolSInt::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000986 }
987 | UIntType EUINT64VAL { // integral constants
988 if (!ConstPoolUInt::isValueValidForType($1, $2))
989 ThrowException("Constant value doesn't fit in type!");
Chris Lattner30c89792001-09-07 16:35:17 +0000990 $$ = ConstPoolUInt::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000991 }
992 | BOOL TRUE { // Boolean constants
Chris Lattner30c89792001-09-07 16:35:17 +0000993 $$ = ConstPoolBool::True;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000994 }
995 | BOOL FALSE { // Boolean constants
Chris Lattner30c89792001-09-07 16:35:17 +0000996 $$ = ConstPoolBool::False;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000997 }
998 | FPType FPVAL { // Float & Double constants
Chris Lattner30c89792001-09-07 16:35:17 +0000999 $$ = ConstPoolFP::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001000 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001001
Chris Lattnere98dda62001-07-14 06:10:16 +00001002// ConstVector - A list of comma seperated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001003ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001004 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001005 }
1006 | ConstVal {
1007 $$ = new vector<ConstPoolVal*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001008 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001009 }
1010
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001011
Chris Lattner1781aca2001-09-18 04:00:54 +00001012// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
1013GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; }
1014
Chris Lattner00950542001-06-06 20:29:01 +00001015
Chris Lattnere98dda62001-07-14 06:10:16 +00001016// ConstPool - Constants with optional names assigned to them.
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001017ConstPool : ConstPool OptAssign CONST ConstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001018 if (setValueName($4, $2)) { assert(0 && "No redefinitions allowed!"); }
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001019 InsertValue($4);
Chris Lattner00950542001-06-06 20:29:01 +00001020 }
Chris Lattner30c89792001-09-07 16:35:17 +00001021 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner1781aca2001-09-18 04:00:54 +00001022 // TODO: FIXME when Type are not const
Chris Lattnerb7474512001-10-03 15:39:04 +00001023 if (!setValueName(const_cast<Type*>($4->get()), $2)) {
1024 // If this is not a redefinition of a type...
1025 if (!$2) {
1026 InsertType($4->get(),
1027 inMethodScope() ? CurMeth.Types : CurModule.Types);
1028 }
Chris Lattner30c89792001-09-07 16:35:17 +00001029 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001030
1031 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001032 }
1033 | ConstPool MethodProto { // Method prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001034 }
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001035 | ConstPool OptAssign GlobalType ConstVal {
Chris Lattner1781aca2001-09-18 04:00:54 +00001036 const Type *Ty = $4->getType();
1037 // Global declarations appear in Constant Pool
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001038 ConstPoolVal *Initializer = $4;
Chris Lattner1781aca2001-09-18 04:00:54 +00001039 if (Initializer == 0)
1040 ThrowException("Global value initializer is not a constant!");
1041
Chris Lattneref9c23f2001-10-03 14:53:21 +00001042 GlobalVariable *GV = new GlobalVariable(Ty, $3, Initializer);
Chris Lattnerb7474512001-10-03 15:39:04 +00001043 if (!setValueName(GV, $2)) { // If not redefining...
1044 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001045 int Slot = InsertValue(GV, CurModule.Values);
1046
1047 if (Slot != -1) {
1048 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1049 } else {
1050 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1051 (char*)GV->getName().c_str()));
1052 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001053 }
Chris Lattner1781aca2001-09-18 04:00:54 +00001054 }
1055 | ConstPool OptAssign UNINIT GlobalType Types {
1056 const Type *Ty = *$5;
1057 // Global declarations appear in Constant Pool
Chris Lattneref9c23f2001-10-03 14:53:21 +00001058 GlobalVariable *GV = new GlobalVariable(Ty, $4);
Chris Lattnerb7474512001-10-03 15:39:04 +00001059 if (!setValueName(GV, $2)) { // If not redefining...
1060 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001061 int Slot = InsertValue(GV, CurModule.Values);
1062
1063 if (Slot != -1) {
1064 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1065 } else {
1066 assert(GV->hasName() && "Not named and not numbered!?");
1067 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1068 (char*)GV->getName().c_str()));
1069 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001070 }
Chris Lattnere98dda62001-07-14 06:10:16 +00001071 }
Chris Lattner00950542001-06-06 20:29:01 +00001072 | /* empty: end of list */ {
1073 }
1074
1075
1076//===----------------------------------------------------------------------===//
1077// Rules to match Modules
1078//===----------------------------------------------------------------------===//
1079
1080// Module rule: Capture the result of parsing the whole file into a result
1081// variable...
1082//
1083Module : MethodList {
1084 $$ = ParserResult = $1;
1085 CurModule.ModuleDone();
1086}
1087
Chris Lattnere98dda62001-07-14 06:10:16 +00001088// MethodList - A list of methods, preceeded by a constant pool.
1089//
Chris Lattner00950542001-06-06 20:29:01 +00001090MethodList : MethodList Method {
Chris Lattner00950542001-06-06 20:29:01 +00001091 $$ = $1;
Chris Lattnere1815642001-07-15 06:35:53 +00001092 if (!$2->getParent())
1093 $1->getMethodList().push_back($2);
1094 CurMeth.MethodDone();
Chris Lattner00950542001-06-06 20:29:01 +00001095 }
Chris Lattnere1815642001-07-15 06:35:53 +00001096 | MethodList MethodProto {
1097 $$ = $1;
Chris Lattnere1815642001-07-15 06:35:53 +00001098 }
Chris Lattner00950542001-06-06 20:29:01 +00001099 | ConstPool IMPLEMENTATION {
1100 $$ = CurModule.CurrentModule;
Chris Lattner30c89792001-09-07 16:35:17 +00001101 // Resolve circular types before we parse the body of the module
1102 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner00950542001-06-06 20:29:01 +00001103 }
1104
1105
1106//===----------------------------------------------------------------------===//
1107// Rules to match Method Headers
1108//===----------------------------------------------------------------------===//
1109
1110OptVAR_ID : VAR_ID | /*empty*/ { $$ = 0; }
1111
1112ArgVal : Types OptVAR_ID {
Chris Lattner30c89792001-09-07 16:35:17 +00001113 $$ = new MethodArgument(*$1); delete $1;
Chris Lattnerb7474512001-10-03 15:39:04 +00001114 if (setValueName($$, $2)) { assert(0 && "No arg redef allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001115}
1116
1117ArgListH : ArgVal ',' ArgListH {
1118 $$ = $3;
1119 $3->push_front($1);
1120 }
1121 | ArgVal {
1122 $$ = new list<MethodArgument*>();
1123 $$->push_front($1);
1124 }
Chris Lattner8b81bf52001-07-25 22:47:46 +00001125 | DOTDOTDOT {
1126 $$ = new list<MethodArgument*>();
Chris Lattner2079fde2001-10-13 06:41:08 +00001127 $$->push_front(new MethodArgument(Type::VoidTy));
Chris Lattner8b81bf52001-07-25 22:47:46 +00001128 }
Chris Lattner00950542001-06-06 20:29:01 +00001129
1130ArgList : ArgListH {
1131 $$ = $1;
1132 }
1133 | /* empty */ {
1134 $$ = 0;
1135 }
1136
1137MethodHeaderH : TypesV STRINGCONSTANT '(' ArgList ')' {
Chris Lattner93750fa2001-07-28 17:48:55 +00001138 UnEscapeLexed($2);
Chris Lattner30c89792001-09-07 16:35:17 +00001139 vector<const Type*> ParamTypeList;
Chris Lattner00950542001-06-06 20:29:01 +00001140 if ($4)
Chris Lattner7fc9fe32001-06-27 23:41:11 +00001141 for (list<MethodArgument*>::iterator I = $4->begin(); I != $4->end(); ++I)
Chris Lattner00950542001-06-06 20:29:01 +00001142 ParamTypeList.push_back((*I)->getType());
1143
Chris Lattner2079fde2001-10-13 06:41:08 +00001144 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1145 if (isVarArg) ParamTypeList.pop_back();
1146
1147 const MethodType *MT = MethodType::get(*$1, ParamTypeList, isVarArg);
Chris Lattneref9c23f2001-10-03 14:53:21 +00001148 const PointerType *PMT = PointerType::get(MT);
Chris Lattner30c89792001-09-07 16:35:17 +00001149 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001150
Chris Lattnere1815642001-07-15 06:35:53 +00001151 Method *M = 0;
1152 if (SymbolTable *ST = CurModule.CurrentModule->getSymbolTable()) {
Chris Lattneref9c23f2001-10-03 14:53:21 +00001153 if (Value *V = ST->lookup(PMT, $2)) { // Method already in symtab?
1154 M = cast<Method>(V);
Chris Lattner00950542001-06-06 20:29:01 +00001155
Chris Lattnere1815642001-07-15 06:35:53 +00001156 // Yes it is. If this is the case, either we need to be a forward decl,
1157 // or it needs to be.
1158 if (!CurMeth.isDeclare && !M->isExternal())
1159 ThrowException("Redefinition of method '" + string($2) + "'!");
1160 }
1161 }
1162
1163 if (M == 0) { // Not already defined?
1164 M = new Method(MT, $2);
1165 InsertValue(M, CurModule.Values);
Chris Lattner2079fde2001-10-13 06:41:08 +00001166 CurModule.DeclareNewGlobalValue(M, ValID::create($2));
Chris Lattnere1815642001-07-15 06:35:53 +00001167 }
1168
1169 free($2); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +00001170
1171 CurMeth.MethodStart(M);
1172
1173 // Add all of the arguments we parsed to the method...
Chris Lattnere1815642001-07-15 06:35:53 +00001174 if ($4 && !CurMeth.isDeclare) { // Is null if empty...
Chris Lattner00950542001-06-06 20:29:01 +00001175 Method::ArgumentListType &ArgList = M->getArgumentList();
1176
Chris Lattner7fc9fe32001-06-27 23:41:11 +00001177 for (list<MethodArgument*>::iterator I = $4->begin(); I != $4->end(); ++I) {
Chris Lattner00950542001-06-06 20:29:01 +00001178 InsertValue(*I);
1179 ArgList.push_back(*I);
1180 }
1181 delete $4; // We're now done with the argument list
1182 }
1183}
1184
1185MethodHeader : MethodHeaderH ConstPool BEGINTOK {
1186 $$ = CurMeth.CurrentMethod;
Chris Lattner30c89792001-09-07 16:35:17 +00001187
1188 // Resolve circular types before we parse the body of the method.
1189 ResolveTypes(CurMeth.LateResolveTypes);
Chris Lattner00950542001-06-06 20:29:01 +00001190}
1191
1192Method : BasicBlockList END {
1193 $$ = $1;
1194}
1195
Chris Lattnere1815642001-07-15 06:35:53 +00001196MethodProto : DECLARE { CurMeth.isDeclare = true; } MethodHeaderH {
1197 $$ = CurMeth.CurrentMethod;
Chris Lattner93750fa2001-07-28 17:48:55 +00001198 if (!$$->getParent())
1199 CurModule.CurrentModule->getMethodList().push_back($$);
1200 CurMeth.MethodDone();
Chris Lattnere1815642001-07-15 06:35:53 +00001201}
Chris Lattner00950542001-06-06 20:29:01 +00001202
1203//===----------------------------------------------------------------------===//
1204// Rules to match Basic Blocks
1205//===----------------------------------------------------------------------===//
1206
1207ConstValueRef : ESINT64VAL { // A reference to a direct constant
1208 $$ = ValID::create($1);
1209 }
1210 | EUINT64VAL {
1211 $$ = ValID::create($1);
1212 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001213 | FPVAL { // Perhaps it's an FP constant?
1214 $$ = ValID::create($1);
1215 }
Chris Lattner00950542001-06-06 20:29:01 +00001216 | TRUE {
1217 $$ = ValID::create((int64_t)1);
1218 }
1219 | FALSE {
1220 $$ = ValID::create((int64_t)0);
1221 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001222 | NULL_TOK {
1223 $$ = ValID::createNull();
1224 }
1225
Chris Lattner93750fa2001-07-28 17:48:55 +00001226/*
Chris Lattner00950542001-06-06 20:29:01 +00001227 | STRINGCONSTANT { // Quoted strings work too... especially for methods
1228 $$ = ValID::create_conststr($1);
1229 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001230*/
Chris Lattner00950542001-06-06 20:29:01 +00001231
Chris Lattner2079fde2001-10-13 06:41:08 +00001232// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1233// another value.
1234//
1235SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001236 $$ = ValID::create($1);
1237 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001238 | VAR_ID { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001239 $$ = ValID::create($1);
1240 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001241
1242// ValueRef - A reference to a definition... either constant or symbolic
1243ValueRef : SymbolicValueRef | ConstValueRef
1244
Chris Lattner00950542001-06-06 20:29:01 +00001245
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001246// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1247// type immediately preceeds the value reference, and allows complex constant
1248// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001249ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001250 $$ = getVal(*$1, $2); delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +00001251 }
Chris Lattner8b81bf52001-07-25 22:47:46 +00001252
Chris Lattner00950542001-06-06 20:29:01 +00001253
1254BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner89219832001-10-03 19:35:04 +00001255 ($$ = $1)->getBasicBlocks().push_back($2);
Chris Lattner00950542001-06-06 20:29:01 +00001256 }
1257 | MethodHeader BasicBlock { // Do not allow methods with 0 basic blocks
Chris Lattner89219832001-10-03 19:35:04 +00001258 ($$ = $1)->getBasicBlocks().push_back($2);
Chris Lattner00950542001-06-06 20:29:01 +00001259 }
1260
1261
1262// Basic blocks are terminated by branching instructions:
1263// br, br/cc, switch, ret
1264//
Chris Lattner2079fde2001-10-13 06:41:08 +00001265BasicBlock : InstructionList OptAssign BBTerminatorInst {
1266 if (setValueName($3, $2)) { assert(0 && "No redefn allowed!"); }
1267 InsertValue($3);
1268
1269 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001270 InsertValue($1);
1271 $$ = $1;
1272 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001273 | LABELSTR InstructionList OptAssign BBTerminatorInst {
1274 if (setValueName($4, $3)) { assert(0 && "No redefn allowed!"); }
1275 InsertValue($4);
1276
1277 $2->getInstList().push_back($4);
Chris Lattnerb7474512001-10-03 15:39:04 +00001278 if (setValueName($2, $1)) { assert(0 && "No label redef allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001279
1280 InsertValue($2);
1281 $$ = $2;
1282 }
1283
1284InstructionList : InstructionList Inst {
1285 $1->getInstList().push_back($2);
1286 $$ = $1;
1287 }
1288 | /* empty */ {
1289 $$ = new BasicBlock();
1290 }
1291
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001292BBTerminatorInst : RET ResolvedVal { // Return with a result...
1293 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001294 }
1295 | RET VOID { // Return with no result...
1296 $$ = new ReturnInst();
1297 }
1298 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner9636a912001-10-01 16:18:37 +00001299 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $3)));
Chris Lattner00950542001-06-06 20:29:01 +00001300 } // Conditional Branch...
1301 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner9636a912001-10-01 16:18:37 +00001302 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $6)),
1303 cast<BasicBlock>(getVal(Type::LabelTy, $9)),
Chris Lattner00950542001-06-06 20:29:01 +00001304 getVal(Type::BoolTy, $3));
1305 }
1306 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1307 SwitchInst *S = new SwitchInst(getVal($2, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001308 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
Chris Lattner00950542001-06-06 20:29:01 +00001309 $$ = S;
1310
1311 list<pair<ConstPoolVal*, BasicBlock*> >::iterator I = $8->begin(),
1312 end = $8->end();
Chris Lattner7fc9fe32001-06-27 23:41:11 +00001313 for (; I != end; ++I)
Chris Lattner00950542001-06-06 20:29:01 +00001314 S->dest_push_back(I->first, I->second);
1315 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001316 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO ResolvedVal
1317 EXCEPT ResolvedVal {
1318 const PointerType *PMTy;
1319 const MethodType *Ty;
1320
1321 if (!(PMTy = dyn_cast<PointerType>($2->get())) ||
1322 !(Ty = dyn_cast<MethodType>(PMTy->getValueType()))) {
1323 // Pull out the types of all of the arguments...
1324 vector<const Type*> ParamTypes;
1325 if ($5) {
1326 for (list<Value*>::iterator I = $5->begin(), E = $5->end(); I != E; ++I)
1327 ParamTypes.push_back((*I)->getType());
1328 }
1329
1330 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1331 if (isVarArg) ParamTypes.pop_back();
1332
1333 Ty = MethodType::get($2->get(), ParamTypes, isVarArg);
1334 PMTy = PointerType::get(Ty);
1335 }
1336 delete $2;
1337
1338 Value *V = getVal(PMTy, $3); // Get the method we're calling...
1339
1340 BasicBlock *Normal = dyn_cast<BasicBlock>($8);
1341 BasicBlock *Except = dyn_cast<BasicBlock>($10);
1342
1343 if (Normal == 0 || Except == 0)
1344 ThrowException("Invoke instruction without label destinations!");
1345
1346 // Create the call node...
1347 if (!$5) { // Has no arguments?
Chris Lattner386a3b72001-10-16 19:54:17 +00001348 $$ = new InvokeInst(V, Normal, Except, vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001349 } else { // Has arguments?
1350 // Loop through MethodType's arguments and ensure they are specified
1351 // correctly!
1352 //
1353 MethodType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1354 MethodType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
1355 list<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
1356
1357 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1358 if ((*ArgI)->getType() != *I)
1359 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
1360 (*I)->getName() + "'!");
1361
1362 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1363 ThrowException("Invalid number of parameters detected!");
1364
Chris Lattner386a3b72001-10-16 19:54:17 +00001365 $$ = new InvokeInst(V, Normal, Except,
Chris Lattner2079fde2001-10-13 06:41:08 +00001366 vector<Value*>($5->begin(), $5->end()));
1367 }
1368 delete $5;
1369 }
1370
1371
Chris Lattner00950542001-06-06 20:29:01 +00001372
1373JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1374 $$ = $1;
Chris Lattner2079fde2001-10-13 06:41:08 +00001375 ConstPoolVal *V = cast<ConstPoolVal>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001376 if (V == 0)
1377 ThrowException("May only switch on a constant pool value!");
1378
Chris Lattner9636a912001-10-01 16:18:37 +00001379 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($5, $6))));
Chris Lattner00950542001-06-06 20:29:01 +00001380 }
1381 | IntType ConstValueRef ',' LABEL ValueRef {
1382 $$ = new list<pair<ConstPoolVal*, BasicBlock*> >();
Chris Lattner2079fde2001-10-13 06:41:08 +00001383 ConstPoolVal *V = cast<ConstPoolVal>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00001384
1385 if (V == 0)
1386 ThrowException("May only switch on a constant pool value!");
1387
Chris Lattner9636a912001-10-01 16:18:37 +00001388 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($4, $5))));
Chris Lattner00950542001-06-06 20:29:01 +00001389 }
1390
1391Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001392 // Is this definition named?? if so, assign the name...
1393 if (setValueName($2, $1)) { assert(0 && "No redefin allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001394 InsertValue($2);
1395 $$ = $2;
1396}
1397
Chris Lattnerc24d2082001-06-11 15:04:20 +00001398PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
1399 $$ = new list<pair<Value*, BasicBlock*> >();
Chris Lattner30c89792001-09-07 16:35:17 +00001400 $$->push_back(make_pair(getVal(*$1, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001401 cast<BasicBlock>(getVal(Type::LabelTy, $5))));
Chris Lattner30c89792001-09-07 16:35:17 +00001402 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001403 }
1404 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1405 $$ = $1;
1406 $1->push_back(make_pair(getVal($1->front().first->getType(), $4),
Chris Lattner9636a912001-10-01 16:18:37 +00001407 cast<BasicBlock>(getVal(Type::LabelTy, $6))));
Chris Lattnerc24d2082001-06-11 15:04:20 +00001408 }
1409
1410
Chris Lattner30c89792001-09-07 16:35:17 +00001411ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner00950542001-06-06 20:29:01 +00001412 $$ = new list<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001413 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001414 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001415 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001416 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001417 $1->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001418 }
1419
1420// ValueRefListE - Just like ValueRefList, except that it may also be empty!
1421ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; }
1422
1423InstVal : BinaryOps Types ValueRef ',' ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001424 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001425 if ($$ == 0)
1426 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001427 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001428 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001429 | UnaryOps ResolvedVal {
1430 $$ = UnaryOperator::create($1, $2);
Chris Lattner00950542001-06-06 20:29:01 +00001431 if ($$ == 0)
1432 ThrowException("unary operator returned null!");
Chris Lattner09083092001-07-08 04:57:15 +00001433 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001434 | ShiftOps ResolvedVal ',' ResolvedVal {
1435 if ($4->getType() != Type::UByteTy)
1436 ThrowException("Shift amount must be ubyte!");
1437 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001438 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001439 | CAST ResolvedVal TO Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001440 $$ = new CastInst($2, *$4);
1441 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001442 }
Chris Lattnerc24d2082001-06-11 15:04:20 +00001443 | PHI PHIList {
1444 const Type *Ty = $2->front().first->getType();
1445 $$ = new PHINode(Ty);
Chris Lattner00950542001-06-06 20:29:01 +00001446 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001447 if ($2->front().first->getType() != Ty)
1448 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001449 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001450 $2->pop_front();
1451 }
1452 delete $2; // Free the list...
1453 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001454 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattneref9c23f2001-10-03 14:53:21 +00001455 const PointerType *PMTy;
Chris Lattner8b81bf52001-07-25 22:47:46 +00001456 const MethodType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001457
Chris Lattneref9c23f2001-10-03 14:53:21 +00001458 if (!(PMTy = dyn_cast<PointerType>($2->get())) ||
1459 !(Ty = dyn_cast<MethodType>(PMTy->getValueType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001460 // Pull out the types of all of the arguments...
1461 vector<const Type*> ParamTypes;
Chris Lattneref9c23f2001-10-03 14:53:21 +00001462 if ($5) {
1463 for (list<Value*>::iterator I = $5->begin(), E = $5->end(); I != E; ++I)
1464 ParamTypes.push_back((*I)->getType());
1465 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001466
1467 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1468 if (isVarArg) ParamTypes.pop_back();
1469
1470 Ty = MethodType::get($2->get(), ParamTypes, isVarArg);
Chris Lattneref9c23f2001-10-03 14:53:21 +00001471 PMTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001472 }
Chris Lattner30c89792001-09-07 16:35:17 +00001473 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001474
Chris Lattneref9c23f2001-10-03 14:53:21 +00001475 Value *V = getVal(PMTy, $3); // Get the method we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001476
Chris Lattner8b81bf52001-07-25 22:47:46 +00001477 // Create the call node...
1478 if (!$5) { // Has no arguments?
Chris Lattner386a3b72001-10-16 19:54:17 +00001479 $$ = new CallInst(V, vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001480 } else { // Has arguments?
Chris Lattner00950542001-06-06 20:29:01 +00001481 // Loop through MethodType's arguments and ensure they are specified
1482 // correctly!
1483 //
1484 MethodType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001485 MethodType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
1486 list<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
1487
1488 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1489 if ((*ArgI)->getType() != *I)
1490 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner00950542001-06-06 20:29:01 +00001491 (*I)->getName() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001492
Chris Lattner8b81bf52001-07-25 22:47:46 +00001493 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner00950542001-06-06 20:29:01 +00001494 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00001495
Chris Lattner2079fde2001-10-13 06:41:08 +00001496 $$ = new CallInst(V, vector<Value*>($5->begin(), $5->end()));
Chris Lattner8b81bf52001-07-25 22:47:46 +00001497 }
1498 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00001499 }
1500 | MemoryInst {
1501 $$ = $1;
1502 }
1503
Chris Lattner027dcc52001-07-08 21:10:27 +00001504// UByteList - List of ubyte values for load and store instructions
1505UByteList : ',' ConstVector {
1506 $$ = $2;
1507} | /* empty */ {
1508 $$ = new vector<ConstPoolVal*>();
1509}
1510
Chris Lattner00950542001-06-06 20:29:01 +00001511MemoryInst : MALLOC Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001512 $$ = new MallocInst(PointerType::get(*$2));
1513 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001514 }
1515 | MALLOC Types ',' UINT ValueRef {
Chris Lattnerb00c5822001-10-02 03:41:24 +00001516 if (!(*$2)->isArrayType() || cast<const ArrayType>($2->get())->isSized())
Chris Lattner30c89792001-09-07 16:35:17 +00001517 ThrowException("Trying to allocate " + (*$2)->getName() +
Chris Lattner00950542001-06-06 20:29:01 +00001518 " as unsized array!");
Chris Lattner30c89792001-09-07 16:35:17 +00001519 const Type *Ty = PointerType::get(*$2);
Chris Lattner8896eda2001-07-09 19:38:36 +00001520 $$ = new MallocInst(Ty, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001521 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001522 }
1523 | ALLOCA Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001524 $$ = new AllocaInst(PointerType::get(*$2));
1525 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001526 }
1527 | ALLOCA Types ',' UINT ValueRef {
Chris Lattnerb00c5822001-10-02 03:41:24 +00001528 if (!(*$2)->isArrayType() || cast<const ArrayType>($2->get())->isSized())
Chris Lattner30c89792001-09-07 16:35:17 +00001529 ThrowException("Trying to allocate " + (*$2)->getName() +
Chris Lattner00950542001-06-06 20:29:01 +00001530 " as unsized array!");
Chris Lattner30c89792001-09-07 16:35:17 +00001531 const Type *Ty = PointerType::get(*$2);
Chris Lattner00950542001-06-06 20:29:01 +00001532 Value *ArrSize = getVal($4, $5);
Chris Lattnerf0d0e9c2001-07-07 08:36:30 +00001533 $$ = new AllocaInst(Ty, ArrSize);
Chris Lattner30c89792001-09-07 16:35:17 +00001534 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001535 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001536 | FREE ResolvedVal {
1537 if (!$2->getType()->isPointerType())
1538 ThrowException("Trying to free nonpointer type " +
1539 $2->getType()->getName() + "!");
1540 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001541 }
1542
Chris Lattner027dcc52001-07-08 21:10:27 +00001543 | LOAD Types ValueRef UByteList {
Chris Lattner30c89792001-09-07 16:35:17 +00001544 if (!(*$2)->isPointerType())
Chris Lattner2079fde2001-10-13 06:41:08 +00001545 ThrowException("Can't load from nonpointer type: " +
1546 (*$2)->getDescription());
Chris Lattner30c89792001-09-07 16:35:17 +00001547 if (LoadInst::getIndexedType(*$2, *$4) == 0)
Chris Lattner027dcc52001-07-08 21:10:27 +00001548 ThrowException("Invalid indices for load instruction!");
1549
Chris Lattner30c89792001-09-07 16:35:17 +00001550 $$ = new LoadInst(getVal(*$2, $3), *$4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001551 delete $4; // Free the vector...
Chris Lattner30c89792001-09-07 16:35:17 +00001552 delete $2;
Chris Lattner027dcc52001-07-08 21:10:27 +00001553 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001554 | STORE ResolvedVal ',' Types ValueRef UByteList {
Chris Lattner30c89792001-09-07 16:35:17 +00001555 if (!(*$4)->isPointerType())
1556 ThrowException("Can't store to a nonpointer type: " + (*$4)->getName());
1557 const Type *ElTy = StoreInst::getIndexedType(*$4, *$6);
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001558 if (ElTy == 0)
1559 ThrowException("Can't store into that field list!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001560 if (ElTy != $2->getType())
1561 ThrowException("Can't store '" + $2->getType()->getName() +
1562 "' into space of type '" + ElTy->getName() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001563 $$ = new StoreInst($2, getVal(*$4, $5), *$6);
1564 delete $4; delete $6;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001565 }
1566 | GETELEMENTPTR Types ValueRef UByteList {
Chris Lattner30c89792001-09-07 16:35:17 +00001567 if (!(*$2)->isPointerType())
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001568 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner30c89792001-09-07 16:35:17 +00001569 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
1570 ThrowException("Can't get element ptr '" + (*$2)->getName() + "'!");
1571 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
1572 delete $2; delete $4;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001573 }
Chris Lattner027dcc52001-07-08 21:10:27 +00001574
Chris Lattner00950542001-06-06 20:29:01 +00001575%%
Chris Lattner09083092001-07-08 04:57:15 +00001576int yyerror(const char *ErrorMsg) {
Chris Lattner00950542001-06-06 20:29:01 +00001577 ThrowException(string("Parse error: ") + ErrorMsg);
1578 return 0;
1579}