blob: c317ff3220058b5f4ce494c7a7e394055ece57ff [file] [log] [blame]
Chris Lattner00950542001-06-06 20:29:01 +00001//===-- llvmAsmParser.y - Parser for llvm assembly files ---------*- C++ -*--=//
2//
3// This file implements the bison parser for LLVM assembly languages files.
4//
5//===------------------------------------------------------------------------=//
6
Chris Lattner00950542001-06-06 20:29:01 +00007%{
8#include "ParserInternals.h"
Chris Lattner70cc3392001-09-10 07:58:01 +00009#include "llvm/Assembly/Parser.h"
Chris Lattner00950542001-06-06 20:29:01 +000010#include "llvm/SymbolTable.h"
11#include "llvm/Module.h"
Chris Lattner70cc3392001-09-10 07:58:01 +000012#include "llvm/GlobalVariable.h"
13#include "llvm/Method.h"
14#include "llvm/BasicBlock.h"
Chris Lattner00950542001-06-06 20:29:01 +000015#include "llvm/DerivedTypes.h"
Chris Lattner00950542001-06-06 20:29:01 +000016#include "llvm/iTerminators.h"
17#include "llvm/iMemory.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000018#include "llvm/iPHINode.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000019#include "Support/STLExtras.h"
20#include "Support/DepthFirstIterator.h"
Chris Lattner00950542001-06-06 20:29:01 +000021#include <list>
22#include <utility> // Get definition of pair class
Chris Lattner30c89792001-09-07 16:35:17 +000023#include <algorithm>
Chris Lattner00950542001-06-06 20:29:01 +000024#include <stdio.h> // This embarasment is due to our flex lexer...
Chris Lattner697954c2002-01-20 22:54:45 +000025#include <iostream>
26using std::list;
27using std::vector;
28using std::pair;
29using std::map;
30using std::pair;
31using std::make_pair;
32using std::cerr;
33using std::string;
Chris Lattner00950542001-06-06 20:29:01 +000034
Chris Lattner386a3b72001-10-16 19:54:17 +000035int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
Chris Lattner09083092001-07-08 04:57:15 +000036int yylex(); // declaration" of xxx warnings.
Chris Lattner00950542001-06-06 20:29:01 +000037int yyparse();
38
39static Module *ParserResult;
Chris Lattnera2850432001-07-22 18:36:00 +000040string CurFilename;
Chris Lattner00950542001-06-06 20:29:01 +000041
Chris Lattner30c89792001-09-07 16:35:17 +000042// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
43// relating to upreferences in the input stream.
44//
45//#define DEBUG_UPREFS 1
46#ifdef DEBUG_UPREFS
47#define UR_OUT(X) cerr << X
48#else
49#define UR_OUT(X)
50#endif
51
Chris Lattner00950542001-06-06 20:29:01 +000052// This contains info used when building the body of a method. It is destroyed
53// when the method is completed.
54//
55typedef vector<Value *> ValueList; // Numbered defs
Chris Lattner386a3b72001-10-16 19:54:17 +000056static void ResolveDefinitions(vector<ValueList> &LateResolvers,
57 vector<ValueList> *FutureLateResolvers = 0);
Chris Lattner00950542001-06-06 20:29:01 +000058
59static struct PerModuleInfo {
60 Module *CurrentModule;
Chris Lattner30c89792001-09-07 16:35:17 +000061 vector<ValueList> Values; // Module level numbered definitions
62 vector<ValueList> LateResolveValues;
Chris Lattner4a42e902001-10-22 05:56:09 +000063 vector<PATypeHolder<Type> > Types;
64 map<ValID, PATypeHolder<Type> > LateResolveTypes;
Chris Lattner00950542001-06-06 20:29:01 +000065
Chris Lattner2079fde2001-10-13 06:41:08 +000066 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
67 // references to global values. Global values may be referenced before they
68 // are defined, and if so, the temporary object that they represent is held
Chris Lattnere9bb2df2001-12-03 22:26:30 +000069 // here. This is used for forward references of ConstantPointerRefs.
Chris Lattner2079fde2001-10-13 06:41:08 +000070 //
71 typedef map<pair<const PointerType *, ValID>, GlobalVariable*> GlobalRefsType;
72 GlobalRefsType GlobalRefs;
73
Chris Lattner00950542001-06-06 20:29:01 +000074 void ModuleDone() {
Chris Lattner30c89792001-09-07 16:35:17 +000075 // If we could not resolve some methods at method compilation time (calls to
76 // methods before they are defined), resolve them now... Types are resolved
77 // when the constant pool has been completely parsed.
78 //
Chris Lattner00950542001-06-06 20:29:01 +000079 ResolveDefinitions(LateResolveValues);
80
Chris Lattner2079fde2001-10-13 06:41:08 +000081 // Check to make sure that all global value forward references have been
82 // resolved!
83 //
84 if (!GlobalRefs.empty()) {
Chris Lattner749ce032002-03-11 22:12:39 +000085 string UndefinedReferences = "Unresolved global references exist:\n";
86
87 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
88 I != E; ++I) {
89 UndefinedReferences += " " + I->first.first->getDescription() + " " +
90 I->first.second.getName() + "\n";
91 }
92 ThrowException(UndefinedReferences);
Chris Lattner2079fde2001-10-13 06:41:08 +000093 }
94
Chris Lattner00950542001-06-06 20:29:01 +000095 Values.clear(); // Clear out method local definitions
Chris Lattner30c89792001-09-07 16:35:17 +000096 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +000097 CurrentModule = 0;
98 }
Chris Lattner2079fde2001-10-13 06:41:08 +000099
100
101 // DeclareNewGlobalValue - Called every type a new GV has been defined. This
102 // is used to remove things from the forward declaration map, resolving them
103 // to the correct thing as needed.
104 //
105 void DeclareNewGlobalValue(GlobalValue *GV, ValID D) {
106 // Check to see if there is a forward reference to this global variable...
107 // if there is, eliminate it and patch the reference to use the new def'n.
108 GlobalRefsType::iterator I = GlobalRefs.find(make_pair(GV->getType(), D));
109
110 if (I != GlobalRefs.end()) {
111 GlobalVariable *OldGV = I->second; // Get the placeholder...
112 I->first.second.destroy(); // Free string memory if neccesary
113
114 // Loop over all of the uses of the GlobalValue. The only thing they are
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000115 // allowed to be at this point is ConstantPointerRef's.
Chris Lattner2079fde2001-10-13 06:41:08 +0000116 assert(OldGV->use_size() == 1 && "Only one reference should exist!");
117 while (!OldGV->use_empty()) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000118 User *U = OldGV->use_back(); // Must be a ConstantPointerRef...
119 ConstantPointerRef *CPPR = cast<ConstantPointerRef>(U);
Chris Lattner2079fde2001-10-13 06:41:08 +0000120 assert(CPPR->getValue() == OldGV && "Something isn't happy");
121
122 // Change the const pool reference to point to the real global variable
123 // now. This should drop a use from the OldGV.
124 CPPR->mutateReference(GV);
125 }
126
127 // Remove GV from the module...
128 CurrentModule->getGlobalList().remove(OldGV);
129 delete OldGV; // Delete the old placeholder
130
131 // Remove the map entry for the global now that it has been created...
132 GlobalRefs.erase(I);
133 }
134 }
135
Chris Lattner00950542001-06-06 20:29:01 +0000136} CurModule;
137
138static struct PerMethodInfo {
139 Method *CurrentMethod; // Pointer to current method being created
140
Chris Lattnere1815642001-07-15 06:35:53 +0000141 vector<ValueList> Values; // Keep track of numbered definitions
Chris Lattner00950542001-06-06 20:29:01 +0000142 vector<ValueList> LateResolveValues;
Chris Lattner4a42e902001-10-22 05:56:09 +0000143 vector<PATypeHolder<Type> > Types;
144 map<ValID, PATypeHolder<Type> > LateResolveTypes;
Chris Lattnere1815642001-07-15 06:35:53 +0000145 bool isDeclare; // Is this method a forward declararation?
Chris Lattner00950542001-06-06 20:29:01 +0000146
147 inline PerMethodInfo() {
148 CurrentMethod = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000149 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000150 }
151
152 inline ~PerMethodInfo() {}
153
154 inline void MethodStart(Method *M) {
155 CurrentMethod = M;
156 }
157
158 void MethodDone() {
159 // If we could not resolve some blocks at parsing time (forward branches)
160 // resolve the branches now...
Chris Lattner386a3b72001-10-16 19:54:17 +0000161 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000162
163 Values.clear(); // Clear out method local definitions
Chris Lattner30c89792001-09-07 16:35:17 +0000164 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +0000165 CurrentMethod = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000166 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000167 }
168} CurMeth; // Info for the current method...
169
Chris Lattnerb7474512001-10-03 15:39:04 +0000170static bool inMethodScope() { return CurMeth.CurrentMethod != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000171
Chris Lattner00950542001-06-06 20:29:01 +0000172
173//===----------------------------------------------------------------------===//
174// Code to handle definitions of all the types
175//===----------------------------------------------------------------------===//
176
Chris Lattner2079fde2001-10-13 06:41:08 +0000177static int InsertValue(Value *D, vector<ValueList> &ValueTab = CurMeth.Values) {
178 if (D->hasName()) return -1; // Is this a numbered definition?
179
180 // Yes, insert the value into the value table...
181 unsigned type = D->getType()->getUniqueID();
182 if (ValueTab.size() <= type)
183 ValueTab.resize(type+1, ValueList());
184 //printf("Values[%d][%d] = %d\n", type, ValueTab[type].size(), D);
185 ValueTab[type].push_back(D);
186 return ValueTab[type].size()-1;
Chris Lattner00950542001-06-06 20:29:01 +0000187}
188
Chris Lattner30c89792001-09-07 16:35:17 +0000189// TODO: FIXME when Type are not const
190static void InsertType(const Type *Ty, vector<PATypeHolder<Type> > &Types) {
191 Types.push_back(Ty);
192}
193
194static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000195 switch (D.Type) {
196 case 0: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000197 unsigned Num = (unsigned)D.Num;
198
199 // Module constants occupy the lowest numbered slots...
200 if (Num < CurModule.Types.size())
201 return CurModule.Types[Num];
202
203 Num -= CurModule.Types.size();
204
205 // Check that the number is within bounds...
206 if (Num <= CurMeth.Types.size())
207 return CurMeth.Types[Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000208 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000209 }
210 case 1: { // Is it a named definition?
211 string Name(D.Name);
212 SymbolTable *SymTab = 0;
Chris Lattnerb7474512001-10-03 15:39:04 +0000213 if (inMethodScope()) SymTab = CurMeth.CurrentMethod->getSymbolTable();
Chris Lattner30c89792001-09-07 16:35:17 +0000214 Value *N = SymTab ? SymTab->lookup(Type::TypeTy, Name) : 0;
215
216 if (N == 0) {
217 // Symbol table doesn't automatically chain yet... because the method
218 // hasn't been added to the module...
219 //
220 SymTab = CurModule.CurrentModule->getSymbolTable();
221 if (SymTab)
222 N = SymTab->lookup(Type::TypeTy, Name);
223 if (N == 0) break;
224 }
225
226 D.destroy(); // Free old strdup'd memory...
Chris Lattnercfe26c92001-10-01 18:26:53 +0000227 return cast<const Type>(N);
Chris Lattner30c89792001-09-07 16:35:17 +0000228 }
229 default:
230 ThrowException("Invalid symbol type reference!");
231 }
232
233 // If we reached here, we referenced either a symbol that we don't know about
234 // or an id number that hasn't been read yet. We may be referencing something
235 // forward, so just create an entry to be resolved later and get to it...
236 //
237 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
238
Chris Lattner4a42e902001-10-22 05:56:09 +0000239 map<ValID, PATypeHolder<Type> > &LateResolver = inMethodScope() ?
240 CurMeth.LateResolveTypes : CurModule.LateResolveTypes;
241
242 map<ValID, PATypeHolder<Type> >::iterator I = LateResolver.find(D);
243 if (I != LateResolver.end()) {
244 return I->second;
245 }
Chris Lattner30c89792001-09-07 16:35:17 +0000246
Chris Lattner82269592001-10-22 06:01:08 +0000247 Type *Typ = OpaqueType::get();
Chris Lattner4a42e902001-10-22 05:56:09 +0000248 LateResolver.insert(make_pair(D, Typ));
Chris Lattner30c89792001-09-07 16:35:17 +0000249 return Typ;
250}
251
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000252static Value *lookupInSymbolTable(const Type *Ty, const string &Name) {
253 SymbolTable *SymTab =
Chris Lattnerb7474512001-10-03 15:39:04 +0000254 inMethodScope() ? CurMeth.CurrentMethod->getSymbolTable() : 0;
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000255 Value *N = SymTab ? SymTab->lookup(Ty, Name) : 0;
256
257 if (N == 0) {
258 // Symbol table doesn't automatically chain yet... because the method
259 // hasn't been added to the module...
260 //
261 SymTab = CurModule.CurrentModule->getSymbolTable();
262 if (SymTab)
263 N = SymTab->lookup(Ty, Name);
264 }
265
266 return N;
267}
268
Chris Lattner2079fde2001-10-13 06:41:08 +0000269// getValNonImprovising - Look up the value specified by the provided type and
270// the provided ValID. If the value exists and has already been defined, return
271// it. Otherwise return null.
272//
273static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Chris Lattner386a3b72001-10-16 19:54:17 +0000274 if (isa<MethodType>(Ty))
275 ThrowException("Methods are not values and must be referenced as pointers");
276
Chris Lattner30c89792001-09-07 16:35:17 +0000277 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000278 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000279 unsigned type = Ty->getUniqueID();
Chris Lattner00950542001-06-06 20:29:01 +0000280 unsigned Num = (unsigned)D.Num;
281
282 // Module constants occupy the lowest numbered slots...
283 if (type < CurModule.Values.size()) {
284 if (Num < CurModule.Values[type].size())
285 return CurModule.Values[type][Num];
286
287 Num -= CurModule.Values[type].size();
288 }
289
290 // Make sure that our type is within bounds
Chris Lattner2079fde2001-10-13 06:41:08 +0000291 if (CurMeth.Values.size() <= type) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000292
293 // Check that the number is within bounds...
Chris Lattner2079fde2001-10-13 06:41:08 +0000294 if (CurMeth.Values[type].size() <= Num) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000295
296 return CurMeth.Values[type][Num];
297 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000298
Chris Lattner1a1cb112001-09-30 22:46:54 +0000299 case ValID::NameVal: { // Is it a named definition?
Chris Lattner2079fde2001-10-13 06:41:08 +0000300 Value *N = lookupInSymbolTable(Ty, string(D.Name));
301 if (N == 0) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000302
303 D.destroy(); // Free old strdup'd memory...
304 return N;
305 }
306
Chris Lattner2079fde2001-10-13 06:41:08 +0000307 // Check to make sure that "Ty" is an integral type, and that our
308 // value will fit into the specified type...
309 case ValID::ConstSIntVal: // Is it a constant pool reference??
310 if (Ty == Type::BoolTy) { // Special handling for boolean data
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000311 return ConstantBool::get(D.ConstPool64 != 0);
Chris Lattner2079fde2001-10-13 06:41:08 +0000312 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000313 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
Chris Lattner2079fde2001-10-13 06:41:08 +0000314 ThrowException("Symbolic constant pool value '" +
315 itostr(D.ConstPool64) + "' is invalid for type '" +
Chris Lattner72e00252001-12-14 16:28:42 +0000316 Ty->getDescription() + "'!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000317 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner00950542001-06-06 20:29:01 +0000318 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000319
320 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000321 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
322 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
Chris Lattner2079fde2001-10-13 06:41:08 +0000323 ThrowException("Integral constant pool reference is invalid!");
324 } else { // This is really a signed reference. Transmogrify.
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000325 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000326 }
327 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000328 return ConstantUInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000329 }
330
331 case ValID::ConstStringVal: // Is it a string const pool reference?
332 cerr << "FIXME: TODO: String constants [sbyte] not implemented yet!\n";
333 abort();
334 return 0;
335
336 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000337 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattner2079fde2001-10-13 06:41:08 +0000338 ThrowException("FP constant invalid for type!!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000339 return ConstantFP::get(Ty, D.ConstPoolFP);
Chris Lattner2079fde2001-10-13 06:41:08 +0000340
341 case ValID::ConstNullVal: // Is it a null value?
342 if (!Ty->isPointerType())
343 ThrowException("Cannot create a a non pointer null!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000344 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner2079fde2001-10-13 06:41:08 +0000345
Chris Lattner30c89792001-09-07 16:35:17 +0000346 default:
347 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000348 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000349 } // End of switch
350
Chris Lattner2079fde2001-10-13 06:41:08 +0000351 assert(0 && "Unhandled case!");
352 return 0;
353}
354
355
356// getVal - This function is identical to getValNonImprovising, except that if a
357// value is not already defined, it "improvises" by creating a placeholder var
358// that looks and acts just like the requested variable. When the value is
359// defined later, all uses of the placeholder variable are replaced with the
360// real thing.
361//
362static Value *getVal(const Type *Ty, const ValID &D) {
363 assert(Ty != Type::TypeTy && "Should use getTypeVal for types!");
364
365 // See if the value has already been defined...
366 Value *V = getValNonImprovising(Ty, D);
367 if (V) return V;
Chris Lattner00950542001-06-06 20:29:01 +0000368
369 // If we reached here, we referenced either a symbol that we don't know about
370 // or an id number that hasn't been read yet. We may be referencing something
371 // forward, so just create an entry to be resolved later and get to it...
372 //
Chris Lattner00950542001-06-06 20:29:01 +0000373 Value *d = 0;
Chris Lattner30c89792001-09-07 16:35:17 +0000374 switch (Ty->getPrimitiveID()) {
375 case Type::LabelTyID: d = new BBPlaceHolder(Ty, D); break;
Chris Lattner30c89792001-09-07 16:35:17 +0000376 default: d = new ValuePlaceHolder(Ty, D); break;
Chris Lattner00950542001-06-06 20:29:01 +0000377 }
378
379 assert(d != 0 && "How did we not make something?");
Chris Lattner386a3b72001-10-16 19:54:17 +0000380 if (inMethodScope())
381 InsertValue(d, CurMeth.LateResolveValues);
382 else
383 InsertValue(d, CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000384 return d;
385}
386
387
388//===----------------------------------------------------------------------===//
389// Code to handle forward references in instructions
390//===----------------------------------------------------------------------===//
391//
392// This code handles the late binding needed with statements that reference
393// values not defined yet... for example, a forward branch, or the PHI node for
394// a loop body.
395//
396// This keeps a table (CurMeth.LateResolveValues) of all such forward references
397// and back patchs after we are done.
398//
399
400// ResolveDefinitions - If we could not resolve some defs at parsing
401// time (forward branches, phi functions for loops, etc...) resolve the
402// defs now...
403//
Chris Lattner386a3b72001-10-16 19:54:17 +0000404static void ResolveDefinitions(vector<ValueList> &LateResolvers,
405 vector<ValueList> *FutureLateResolvers = 0) {
Chris Lattner00950542001-06-06 20:29:01 +0000406 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
407 for (unsigned ty = 0; ty < LateResolvers.size(); ty++) {
408 while (!LateResolvers[ty].empty()) {
409 Value *V = LateResolvers[ty].back();
Chris Lattner386a3b72001-10-16 19:54:17 +0000410 assert(!isa<Type>(V) && "Types should be in LateResolveTypes!");
411
Chris Lattner00950542001-06-06 20:29:01 +0000412 LateResolvers[ty].pop_back();
413 ValID &DID = getValIDFromPlaceHolder(V);
414
Chris Lattner2079fde2001-10-13 06:41:08 +0000415 Value *TheRealValue = getValNonImprovising(Type::getUniqueIDType(ty),DID);
Chris Lattner386a3b72001-10-16 19:54:17 +0000416 if (TheRealValue) {
417 V->replaceAllUsesWith(TheRealValue);
418 delete V;
419 } else if (FutureLateResolvers) {
420 // Methods have their unresolved items forwarded to the module late
421 // resolver table
422 InsertValue(V, *FutureLateResolvers);
423 } else {
Chris Lattner30c89792001-09-07 16:35:17 +0000424 if (DID.Type == 1)
425 ThrowException("Reference to an invalid definition: '" +DID.getName()+
426 "' of type '" + V->getType()->getDescription() + "'",
427 getLineNumFromPlaceHolder(V));
428 else
429 ThrowException("Reference to an invalid definition: #" +
430 itostr(DID.Num) + " of type '" +
431 V->getType()->getDescription() + "'",
432 getLineNumFromPlaceHolder(V));
433 }
Chris Lattner00950542001-06-06 20:29:01 +0000434 }
435 }
436
437 LateResolvers.clear();
438}
439
Chris Lattner4a42e902001-10-22 05:56:09 +0000440// ResolveTypeTo - A brand new type was just declared. This means that (if
441// name is not null) things referencing Name can be resolved. Otherwise, things
442// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000443//
Chris Lattner4a42e902001-10-22 05:56:09 +0000444static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattnerdda71962001-11-26 18:54:16 +0000445 vector<PATypeHolder<Type> > &Types = inMethodScope() ?
Chris Lattner4a42e902001-10-22 05:56:09 +0000446 CurMeth.Types : CurModule.Types;
Chris Lattner00950542001-06-06 20:29:01 +0000447
Chris Lattner4a42e902001-10-22 05:56:09 +0000448 ValID D;
449 if (Name) D = ValID::create(Name);
450 else D = ValID::create((int)Types.size());
Chris Lattner30c89792001-09-07 16:35:17 +0000451
Chris Lattner4a42e902001-10-22 05:56:09 +0000452 map<ValID, PATypeHolder<Type> > &LateResolver = inMethodScope() ?
453 CurMeth.LateResolveTypes : CurModule.LateResolveTypes;
454
455 map<ValID, PATypeHolder<Type> >::iterator I = LateResolver.find(D);
456 if (I != LateResolver.end()) {
457 cast<DerivedType>(I->second.get())->refineAbstractTypeTo(ToTy);
458 LateResolver.erase(I);
459 }
460}
461
462// ResolveTypes - At this point, all types should be resolved. Any that aren't
463// are errors.
464//
465static void ResolveTypes(map<ValID, PATypeHolder<Type> > &LateResolveTypes) {
466 if (!LateResolveTypes.empty()) {
Chris Lattner82269592001-10-22 06:01:08 +0000467 const ValID &DID = LateResolveTypes.begin()->first;
Chris Lattner4a42e902001-10-22 05:56:09 +0000468
469 if (DID.Type == ValID::NameVal)
Chris Lattner82269592001-10-22 06:01:08 +0000470 ThrowException("Reference to an invalid type: '" +DID.getName() + "'");
Chris Lattner4a42e902001-10-22 05:56:09 +0000471 else
Chris Lattner82269592001-10-22 06:01:08 +0000472 ThrowException("Reference to an invalid type: #" + itostr(DID.Num));
Chris Lattner30c89792001-09-07 16:35:17 +0000473 }
474}
475
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000476
Chris Lattner1781aca2001-09-18 04:00:54 +0000477// setValueName - Set the specified value to the name given. The name may be
478// null potentially, in which case this is a noop. The string passed in is
479// assumed to be a malloc'd string buffer, and is freed by this function.
480//
Chris Lattnerb7474512001-10-03 15:39:04 +0000481// This function returns true if the value has already been defined, but is
482// allowed to be redefined in the specified context. If the name is a new name
483// for the typeplane, false is returned.
484//
485static bool setValueName(Value *V, char *NameStr) {
486 if (NameStr == 0) return false;
Chris Lattner386a3b72001-10-16 19:54:17 +0000487
Chris Lattner1781aca2001-09-18 04:00:54 +0000488 string Name(NameStr); // Copy string
489 free(NameStr); // Free old string
490
Chris Lattner2079fde2001-10-13 06:41:08 +0000491 if (V->getType() == Type::VoidTy)
492 ThrowException("Can't assign name '" + Name +
493 "' to a null valued instruction!");
494
Chris Lattnerb7474512001-10-03 15:39:04 +0000495 SymbolTable *ST = inMethodScope() ?
Chris Lattner30c89792001-09-07 16:35:17 +0000496 CurMeth.CurrentMethod->getSymbolTableSure() :
497 CurModule.CurrentModule->getSymbolTableSure();
498
499 Value *Existing = ST->lookup(V->getType(), Name);
500 if (Existing) { // Inserting a name that is already defined???
501 // There is only one case where this is allowed: when we are refining an
502 // opaque type. In this case, Existing will be an opaque type.
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000503 if (const Type *Ty = dyn_cast<const Type>(Existing)) {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000504 if (OpaqueType *OpTy = dyn_cast<OpaqueType>(Ty)) {
Chris Lattner30c89792001-09-07 16:35:17 +0000505 // We ARE replacing an opaque type!
Chris Lattnerb00c5822001-10-02 03:41:24 +0000506 OpTy->refineAbstractTypeTo(cast<Type>(V));
Chris Lattnerb7474512001-10-03 15:39:04 +0000507 return true;
Chris Lattner30c89792001-09-07 16:35:17 +0000508 }
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000509 }
Chris Lattner30c89792001-09-07 16:35:17 +0000510
Chris Lattner9636a912001-10-01 16:18:37 +0000511 // Otherwise, we are a simple redefinition of a value, check to see if it
512 // is defined the same as the old one...
513 if (const Type *Ty = dyn_cast<const Type>(Existing)) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000514 if (Ty == cast<const Type>(V)) return true; // Yes, it's equal.
515 // cerr << "Type: " << Ty->getDescription() << " != "
516 // << cast<const Type>(V)->getDescription() << "!\n";
517 } else if (GlobalVariable *EGV = dyn_cast<GlobalVariable>(Existing)) {
Chris Lattner43efcbf2001-10-03 19:35:57 +0000518 // We are allowed to redefine a global variable in two circumstances:
519 // 1. If at least one of the globals is uninitialized or
520 // 2. If both initializers have the same value.
521 //
522 // This can only be done if the const'ness of the vars is the same.
523 //
Chris Lattner89219832001-10-03 19:35:04 +0000524 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
525 if (EGV->isConstant() == GV->isConstant() &&
526 (!EGV->hasInitializer() || !GV->hasInitializer() ||
527 EGV->getInitializer() == GV->getInitializer())) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000528
Chris Lattner89219832001-10-03 19:35:04 +0000529 // Make sure the existing global version gets the initializer!
530 if (GV->hasInitializer() && !EGV->hasInitializer())
531 EGV->setInitializer(GV->getInitializer());
532
Chris Lattner2079fde2001-10-13 06:41:08 +0000533 delete GV; // Destroy the duplicate!
Chris Lattner89219832001-10-03 19:35:04 +0000534 return true; // They are equivalent!
535 }
Chris Lattnerb7474512001-10-03 15:39:04 +0000536 }
Chris Lattner9636a912001-10-01 16:18:37 +0000537 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000538 ThrowException("Redefinition of value named '" + Name + "' in the '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000539 V->getType()->getDescription() + "' type plane!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000540 }
Chris Lattner00950542001-06-06 20:29:01 +0000541
Chris Lattner30c89792001-09-07 16:35:17 +0000542 V->setName(Name, ST);
Chris Lattnerb7474512001-10-03 15:39:04 +0000543 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000544}
545
Chris Lattner8896eda2001-07-09 19:38:36 +0000546
Chris Lattner30c89792001-09-07 16:35:17 +0000547//===----------------------------------------------------------------------===//
548// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000549//
Chris Lattner8896eda2001-07-09 19:38:36 +0000550
Chris Lattner30c89792001-09-07 16:35:17 +0000551// TypeContains - Returns true if Ty contains E in it.
552//
553static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner3ff43872001-09-28 22:56:31 +0000554 return find(df_begin(Ty), df_end(Ty), E) != df_end(Ty);
Chris Lattner30c89792001-09-07 16:35:17 +0000555}
Chris Lattner698b56e2001-07-20 19:15:08 +0000556
Chris Lattner30c89792001-09-07 16:35:17 +0000557
558static vector<pair<unsigned, OpaqueType *> > UpRefs;
559
560static PATypeHolder<Type> HandleUpRefs(const Type *ty) {
561 PATypeHolder<Type> Ty(ty);
Chris Lattner5084d032001-11-02 07:46:26 +0000562 UR_OUT("Type '" << ty->getDescription() <<
563 "' newly formed. Resolving upreferences.\n" <<
564 UpRefs.size() << " upreferences active!\n");
Chris Lattner30c89792001-09-07 16:35:17 +0000565 for (unsigned i = 0; i < UpRefs.size(); ) {
Chris Lattner5084d032001-11-02 07:46:26 +0000566 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Chris Lattner30c89792001-09-07 16:35:17 +0000567 << UpRefs[i].second->getDescription() << ") = "
Chris Lattner5084d032001-11-02 07:46:26 +0000568 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << endl);
Chris Lattner30c89792001-09-07 16:35:17 +0000569 if (TypeContains(Ty, UpRefs[i].second)) {
570 unsigned Level = --UpRefs[i].first; // Decrement level of upreference
Chris Lattner5084d032001-11-02 07:46:26 +0000571 UR_OUT(" Uplevel Ref Level = " << Level << endl);
Chris Lattner30c89792001-09-07 16:35:17 +0000572 if (Level == 0) { // Upreference should be resolved!
Chris Lattner5084d032001-11-02 07:46:26 +0000573 UR_OUT(" * Resolving upreference for "
574 << UpRefs[i].second->getDescription() << endl;
Chris Lattner30c89792001-09-07 16:35:17 +0000575 string OldName = UpRefs[i].second->getDescription());
576 UpRefs[i].second->refineAbstractTypeTo(Ty);
577 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
Chris Lattner5084d032001-11-02 07:46:26 +0000578 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
Chris Lattner30c89792001-09-07 16:35:17 +0000579 << (const void*)Ty << ", " << Ty->getDescription() << endl);
580 continue;
581 }
582 }
583
584 ++i; // Otherwise, no resolve, move on...
Chris Lattner8896eda2001-07-09 19:38:36 +0000585 }
Chris Lattner30c89792001-09-07 16:35:17 +0000586 // FIXME: TODO: this should return the updated type
Chris Lattner8896eda2001-07-09 19:38:36 +0000587 return Ty;
588}
589
Chris Lattner30c89792001-09-07 16:35:17 +0000590template <class TypeTy>
591inline static void TypeDone(PATypeHolder<TypeTy> *Ty) {
592 if (UpRefs.size())
593 ThrowException("Invalid upreference in type: " + (*Ty)->getDescription());
594}
595
596// newTH - Allocate a new type holder for the specified type
597template <class TypeTy>
598inline static PATypeHolder<TypeTy> *newTH(const TypeTy *Ty) {
599 return new PATypeHolder<TypeTy>(Ty);
600}
601template <class TypeTy>
602inline static PATypeHolder<TypeTy> *newTH(const PATypeHolder<TypeTy> &TH) {
603 return new PATypeHolder<TypeTy>(TH);
604}
605
606
Chris Lattner00950542001-06-06 20:29:01 +0000607//===----------------------------------------------------------------------===//
608// RunVMAsmParser - Define an interface to this parser
609//===----------------------------------------------------------------------===//
610//
Chris Lattnera2850432001-07-22 18:36:00 +0000611Module *RunVMAsmParser(const string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000612 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000613 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000614 llvmAsmlineno = 1; // Reset the current line number...
615
616 CurModule.CurrentModule = new Module(); // Allocate a new module to read
617 yyparse(); // Parse the file.
618 Module *Result = ParserResult;
Chris Lattner00950542001-06-06 20:29:01 +0000619 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
620 ParserResult = 0;
621
622 return Result;
623}
624
625%}
626
627%union {
Chris Lattner30c89792001-09-07 16:35:17 +0000628 Module *ModuleVal;
629 Method *MethodVal;
Chris Lattnerf28d6c92002-03-08 18:41:32 +0000630 std::pair<MethodArgument*,char*> *MethArgVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000631 BasicBlock *BasicBlockVal;
632 TerminatorInst *TermInstVal;
633 Instruction *InstVal;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000634 Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000635
Chris Lattner30c89792001-09-07 16:35:17 +0000636 const Type *PrimType;
637 PATypeHolder<Type> *TypeVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000638 Value *ValueVal;
639
Chris Lattnerf28d6c92002-03-08 18:41:32 +0000640 std::list<std::pair<MethodArgument*,char*> > *MethodArgList;
Chris Lattner697954c2002-01-20 22:54:45 +0000641 std::vector<Value*> *ValueList;
642 std::list<PATypeHolder<Type> > *TypeList;
643 std::list<std::pair<Value*,
644 BasicBlock*> > *PHIList; // Represent the RHS of PHI node
645 std::list<std::pair<Constant*, BasicBlock*> > *JumpTable;
646 std::vector<Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000647
Chris Lattner30c89792001-09-07 16:35:17 +0000648 int64_t SInt64Val;
649 uint64_t UInt64Val;
650 int SIntVal;
651 unsigned UIntVal;
652 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000653 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000654
Chris Lattner30c89792001-09-07 16:35:17 +0000655 char *StrVal; // This memory is strdup'd!
656 ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000657
Chris Lattner30c89792001-09-07 16:35:17 +0000658 Instruction::UnaryOps UnaryOpVal;
659 Instruction::BinaryOps BinaryOpVal;
660 Instruction::TermOps TermOpVal;
661 Instruction::MemoryOps MemOpVal;
662 Instruction::OtherOps OtherOpVal;
Chris Lattner00950542001-06-06 20:29:01 +0000663}
664
665%type <ModuleVal> Module MethodList
Chris Lattnere1815642001-07-15 06:35:53 +0000666%type <MethodVal> Method MethodProto MethodHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000667%type <BasicBlockVal> BasicBlock InstructionList
668%type <TermInstVal> BBTerminatorInst
669%type <InstVal> Inst InstVal MemoryInst
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000670%type <ConstVal> ConstVal
Chris Lattner6cdb0112001-11-26 16:54:11 +0000671%type <ConstVector> ConstVector
Chris Lattner00950542001-06-06 20:29:01 +0000672%type <MethodArgList> ArgList ArgListH
673%type <MethArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000674%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000675%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000676%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000677%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000678%type <JumpTable> JumpTable
Chris Lattnerdda71962001-11-26 18:54:16 +0000679%type <BoolVal> GlobalType OptInternal // GLOBAL or CONSTANT? Intern?
Chris Lattner00950542001-06-06 20:29:01 +0000680
Chris Lattner2079fde2001-10-13 06:41:08 +0000681// ValueRef - Unresolved reference to a definition or BB
682%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000683%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000684// Tokens and types for handling constant integer values
685//
686// ESINT64VAL - A negative number within long long range
687%token <SInt64Val> ESINT64VAL
688
689// EUINT64VAL - A positive number within uns. long long range
690%token <UInt64Val> EUINT64VAL
691%type <SInt64Val> EINT64VAL
692
693%token <SIntVal> SINTVAL // Signed 32 bit ints...
694%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
695%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000696%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000697
698// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000699%type <TypeVal> Types TypesV UpRTypes UpRTypesV
700%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
701%token <TypeVal> OPAQUE
702%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
703%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000704
705%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
706%type <StrVal> OptVAR_ID OptAssign
707
708
Chris Lattner1781aca2001-09-18 04:00:54 +0000709%token IMPLEMENTATION TRUE FALSE BEGINTOK END DECLARE GLOBAL CONSTANT UNINIT
Chris Lattnerdda71962001-11-26 18:54:16 +0000710%token TO EXCEPT DOTDOTDOT STRING NULL_TOK CONST INTERNAL
Chris Lattner00950542001-06-06 20:29:01 +0000711
712// Basic Block Terminating Operators
713%token <TermOpVal> RET BR SWITCH
714
715// Unary Operators
716%type <UnaryOpVal> UnaryOps // all the unary operators
Chris Lattner71496b32001-07-08 19:03:27 +0000717%token <UnaryOpVal> NOT
Chris Lattner00950542001-06-06 20:29:01 +0000718
719// Binary Operators
720%type <BinaryOpVal> BinaryOps // all the binary operators
Chris Lattner42c9e772001-10-20 09:32:59 +0000721%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000722%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000723
724// Memory Instructions
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000725%token <MemoryOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000726
Chris Lattner027dcc52001-07-08 21:10:27 +0000727// Other Operators
728%type <OtherOpVal> ShiftOps
Chris Lattner2079fde2001-10-13 06:41:08 +0000729%token <OtherOpVal> PHI CALL INVOKE CAST SHL SHR
Chris Lattner027dcc52001-07-08 21:10:27 +0000730
Chris Lattner00950542001-06-06 20:29:01 +0000731%start Module
732%%
733
734// Handle constant integer size restriction and conversion...
735//
736
737INTVAL : SINTVAL
738INTVAL : UINTVAL {
739 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
740 ThrowException("Value too large for type!");
741 $$ = (int32_t)$1;
742}
743
744
745EINT64VAL : ESINT64VAL // These have same type and can't cause problems...
746EINT64VAL : EUINT64VAL {
747 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
748 ThrowException("Value too large for type!");
749 $$ = (int64_t)$1;
750}
751
Chris Lattner00950542001-06-06 20:29:01 +0000752// Operations that are notably excluded from this list include:
753// RET, BR, & SWITCH because they end basic blocks and are treated specially.
754//
Chris Lattner09083092001-07-08 04:57:15 +0000755UnaryOps : NOT
Chris Lattner42c9e772001-10-20 09:32:59 +0000756BinaryOps : ADD | SUB | MUL | DIV | REM | AND | OR | XOR
Chris Lattner00950542001-06-06 20:29:01 +0000757BinaryOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE
Chris Lattner027dcc52001-07-08 21:10:27 +0000758ShiftOps : SHL | SHR
Chris Lattner00950542001-06-06 20:29:01 +0000759
Chris Lattnere98dda62001-07-14 06:10:16 +0000760// These are some types that allow classification if we only want a particular
761// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner00950542001-06-06 20:29:01 +0000762SIntType : LONG | INT | SHORT | SBYTE
763UIntType : ULONG | UINT | USHORT | UBYTE
Chris Lattner30c89792001-09-07 16:35:17 +0000764IntType : SIntType | UIntType
765FPType : FLOAT | DOUBLE
Chris Lattner00950542001-06-06 20:29:01 +0000766
Chris Lattnere98dda62001-07-14 06:10:16 +0000767// OptAssign - Value producing statements have an optional assignment component
Chris Lattner00950542001-06-06 20:29:01 +0000768OptAssign : VAR_ID '=' {
769 $$ = $1;
770 }
771 | /*empty*/ {
772 $$ = 0;
773 }
774
Chris Lattnerdda71962001-11-26 18:54:16 +0000775OptInternal : INTERNAL { $$ = true; } | /*empty*/ { $$ = false; }
Chris Lattner30c89792001-09-07 16:35:17 +0000776
777//===----------------------------------------------------------------------===//
778// Types includes all predefined types... except void, because it can only be
779// used in specific contexts (method returning void for example). To have
780// access to it, a user must explicitly use TypesV.
781//
782
783// TypesV includes all of 'Types', but it also includes the void type.
784TypesV : Types | VOID { $$ = newTH($1); }
785UpRTypesV : UpRTypes | VOID { $$ = newTH($1); }
786
787Types : UpRTypes {
788 TypeDone($$ = $1);
789 }
790
791
792// Derived types are added later...
793//
794PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT
795PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL
796UpRTypes : OPAQUE | PrimType { $$ = newTH($1); }
797UpRTypes : ValueRef { // Named types are also simple types...
798 $$ = newTH(getTypeVal($1));
799}
800
Chris Lattner30c89792001-09-07 16:35:17 +0000801// Include derived types in the Types production.
802//
803UpRTypes : '\\' EUINT64VAL { // Type UpReference
804 if ($2 > (uint64_t)INT64_MAX) ThrowException("Value out of range!");
805 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
806 UpRefs.push_back(make_pair((unsigned)$2, OT)); // Add to vector...
807 $$ = newTH<Type>(OT);
808 UR_OUT("New Upreference!\n");
809 }
810 | UpRTypesV '(' ArgTypeListI ')' { // Method derived type?
811 vector<const Type*> Params;
Chris Lattner697954c2002-01-20 22:54:45 +0000812 mapto($3->begin(), $3->end(), std::back_inserter(Params),
813 std::mem_fun_ref(&PATypeHandle<Type>::get));
Chris Lattner2079fde2001-10-13 06:41:08 +0000814 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
815 if (isVarArg) Params.pop_back();
816
817 $$ = newTH(HandleUpRefs(MethodType::get(*$1, Params, isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +0000818 delete $3; // Delete the argument list
819 delete $1; // Delete the old type handle
820 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000821 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner72e00252001-12-14 16:28:42 +0000822 $$ = newTH<Type>(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000823 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;
Chris Lattner697954c2002-01-20 22:54:45 +0000827 mapto($2->begin(), $2->end(), std::back_inserter(Elements),
828 std::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 Lattner72e00252001-12-14 16:28:42 +0000886 ETy->getDescription() +"' as required!\nIt is of type '"+
887 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +0000888 }
889
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000890 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +0000891 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 Lattnere9bb2df2001-12-03 22:26:30 +0000903 $$ = ConstantArray::get(ATy, vector<Constant*>());
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 Lattnere9bb2df2001-12-03 22:26:30 +0000919 vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +0000920 if (ETy == Type::SByteTy) {
921 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000922 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +0000923 } else if (ETy == Type::UByteTy) {
924 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000925 Vals.push_back(ConstantUInt::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);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000931 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +0000932 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 Lattnere9bb2df2001-12-03 22:26:30 +0000941 $$ = ConstantStruct::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 Lattnere9bb2df2001-12-03 22:26:30 +0000950 $$ = ConstantPointerNull::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...
Chris Lattner7a176752001-12-04 00:03:30 +0000979 GlobalVariable *GV = new GlobalVariable(PT->getElementType(),
980 false, true);
Chris Lattner2079fde2001-10-13 06:41:08 +0000981 // Keep track of the fact that we have a forward ref to recycle it
982 CurModule.GlobalRefs.insert(make_pair(make_pair(PT, $2), GV));
983
984 // Must temporarily push this value into the module table...
985 CurModule.CurrentModule->getGlobalList().push_back(GV);
986 V = GV;
987 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000988 }
989
Chris Lattner2079fde2001-10-13 06:41:08 +0000990 GlobalValue *GV = cast<GlobalValue>(V);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000991 $$ = ConstantPointerRef::get(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +0000992 delete $1; // Free the type handle
Chris Lattner00950542001-06-06 20:29:01 +0000993 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000994
Chris Lattner00950542001-06-06 20:29:01 +0000995
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000996ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000997 if (!ConstantSInt::isValueValidForType($1, $2))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000998 ThrowException("Constant value doesn't fit in type!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000999 $$ = ConstantSInt::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001000 }
1001 | UIntType EUINT64VAL { // integral constants
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001002 if (!ConstantUInt::isValueValidForType($1, $2))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001003 ThrowException("Constant value doesn't fit in type!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001004 $$ = ConstantUInt::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001005 }
1006 | BOOL TRUE { // Boolean constants
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001007 $$ = ConstantBool::True;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001008 }
1009 | BOOL FALSE { // Boolean constants
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001010 $$ = ConstantBool::False;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001011 }
1012 | FPType FPVAL { // Float & Double constants
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001013 $$ = ConstantFP::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001014 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001015
Chris Lattnere98dda62001-07-14 06:10:16 +00001016// ConstVector - A list of comma seperated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001017ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001018 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001019 }
1020 | ConstVal {
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001021 $$ = new vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001022 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001023 }
1024
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001025
Chris Lattner1781aca2001-09-18 04:00:54 +00001026// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
1027GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; }
1028
Chris Lattner00950542001-06-06 20:29:01 +00001029
Chris Lattnere98dda62001-07-14 06:10:16 +00001030// ConstPool - Constants with optional names assigned to them.
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001031ConstPool : ConstPool OptAssign CONST ConstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001032 if (setValueName($4, $2)) { assert(0 && "No redefinitions allowed!"); }
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001033 InsertValue($4);
Chris Lattner00950542001-06-06 20:29:01 +00001034 }
Chris Lattner30c89792001-09-07 16:35:17 +00001035 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner4a42e902001-10-22 05:56:09 +00001036 // Eagerly resolve types. This is not an optimization, this is a
1037 // requirement that is due to the fact that we could have this:
1038 //
1039 // %list = type { %list * }
1040 // %list = type { %list * } ; repeated type decl
1041 //
1042 // If types are not resolved eagerly, then the two types will not be
1043 // determined to be the same type!
1044 //
1045 ResolveTypeTo($2, $4->get());
1046
Chris Lattner1781aca2001-09-18 04:00:54 +00001047 // TODO: FIXME when Type are not const
Chris Lattnerb7474512001-10-03 15:39:04 +00001048 if (!setValueName(const_cast<Type*>($4->get()), $2)) {
1049 // If this is not a redefinition of a type...
1050 if (!$2) {
1051 InsertType($4->get(),
1052 inMethodScope() ? CurMeth.Types : CurModule.Types);
1053 }
Chris Lattner30c89792001-09-07 16:35:17 +00001054 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001055
1056 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001057 }
1058 | ConstPool MethodProto { // Method prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001059 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001060 | ConstPool OptAssign OptInternal GlobalType ConstVal {
1061 const Type *Ty = $5->getType();
Chris Lattner1781aca2001-09-18 04:00:54 +00001062 // Global declarations appear in Constant Pool
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001063 Constant *Initializer = $5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001064 if (Initializer == 0)
1065 ThrowException("Global value initializer is not a constant!");
1066
Chris Lattnerdda71962001-11-26 18:54:16 +00001067 GlobalVariable *GV = new GlobalVariable(Ty, $4, $3, Initializer);
Chris Lattnerb7474512001-10-03 15:39:04 +00001068 if (!setValueName(GV, $2)) { // If not redefining...
1069 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001070 int Slot = InsertValue(GV, CurModule.Values);
1071
1072 if (Slot != -1) {
1073 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1074 } else {
1075 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1076 (char*)GV->getName().c_str()));
1077 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001078 }
Chris Lattner1781aca2001-09-18 04:00:54 +00001079 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001080 | ConstPool OptAssign OptInternal UNINIT GlobalType Types {
1081 const Type *Ty = *$6;
Chris Lattner1781aca2001-09-18 04:00:54 +00001082 // Global declarations appear in Constant Pool
Chris Lattnerdda71962001-11-26 18:54:16 +00001083 GlobalVariable *GV = new GlobalVariable(Ty, $5, $3);
Chris Lattnerb7474512001-10-03 15:39:04 +00001084 if (!setValueName(GV, $2)) { // If not redefining...
1085 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001086 int Slot = InsertValue(GV, CurModule.Values);
1087
1088 if (Slot != -1) {
1089 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1090 } else {
1091 assert(GV->hasName() && "Not named and not numbered!?");
1092 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1093 (char*)GV->getName().c_str()));
1094 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001095 }
Chris Lattnere98dda62001-07-14 06:10:16 +00001096 }
Chris Lattner00950542001-06-06 20:29:01 +00001097 | /* empty: end of list */ {
1098 }
1099
1100
1101//===----------------------------------------------------------------------===//
1102// Rules to match Modules
1103//===----------------------------------------------------------------------===//
1104
1105// Module rule: Capture the result of parsing the whole file into a result
1106// variable...
1107//
1108Module : MethodList {
1109 $$ = ParserResult = $1;
1110 CurModule.ModuleDone();
1111}
1112
Chris Lattnere98dda62001-07-14 06:10:16 +00001113// MethodList - A list of methods, preceeded by a constant pool.
1114//
Chris Lattner00950542001-06-06 20:29:01 +00001115MethodList : MethodList Method {
Chris Lattner00950542001-06-06 20:29:01 +00001116 $$ = $1;
Chris Lattner34538142002-03-08 19:11:42 +00001117 assert($2->getParent() == 0 && "Method already in module!");
1118 $1->getMethodList().push_back($2);
Chris Lattnere1815642001-07-15 06:35:53 +00001119 CurMeth.MethodDone();
Chris Lattner00950542001-06-06 20:29:01 +00001120 }
Chris Lattnere1815642001-07-15 06:35:53 +00001121 | MethodList MethodProto {
1122 $$ = $1;
Chris Lattnere1815642001-07-15 06:35:53 +00001123 }
Chris Lattner00950542001-06-06 20:29:01 +00001124 | ConstPool IMPLEMENTATION {
1125 $$ = CurModule.CurrentModule;
Chris Lattner30c89792001-09-07 16:35:17 +00001126 // Resolve circular types before we parse the body of the module
1127 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner00950542001-06-06 20:29:01 +00001128 }
1129
1130
1131//===----------------------------------------------------------------------===//
1132// Rules to match Method Headers
1133//===----------------------------------------------------------------------===//
1134
1135OptVAR_ID : VAR_ID | /*empty*/ { $$ = 0; }
1136
1137ArgVal : Types OptVAR_ID {
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001138 $$ = new pair<MethodArgument*,char*>(new MethodArgument(*$1), $2);
1139 delete $1; // Delete the type handle..
Chris Lattner00950542001-06-06 20:29:01 +00001140}
1141
1142ArgListH : ArgVal ',' ArgListH {
1143 $$ = $3;
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001144 $3->push_front(*$1);
1145 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001146 }
1147 | ArgVal {
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001148 $$ = new list<pair<MethodArgument*,char*> >();
1149 $$->push_front(*$1);
1150 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001151 }
Chris Lattner8b81bf52001-07-25 22:47:46 +00001152 | DOTDOTDOT {
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001153 $$ = new list<pair<MethodArgument*, char*> >();
1154 $$->push_front(pair<MethodArgument*,char*>(
1155 new MethodArgument(Type::VoidTy), 0));
Chris Lattner8b81bf52001-07-25 22:47:46 +00001156 }
Chris Lattner00950542001-06-06 20:29:01 +00001157
1158ArgList : ArgListH {
1159 $$ = $1;
1160 }
1161 | /* empty */ {
1162 $$ = 0;
1163 }
1164
Chris Lattnerdda71962001-11-26 18:54:16 +00001165MethodHeaderH : OptInternal TypesV STRINGCONSTANT '(' ArgList ')' {
1166 UnEscapeLexed($3);
1167 string MethodName($3);
1168
Chris Lattner30c89792001-09-07 16:35:17 +00001169 vector<const Type*> ParamTypeList;
Chris Lattnerdda71962001-11-26 18:54:16 +00001170 if ($5)
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001171 for (list<pair<MethodArgument*,char*> >::iterator I = $5->begin();
1172 I != $5->end(); ++I)
1173 ParamTypeList.push_back(I->first->getType());
Chris Lattner00950542001-06-06 20:29:01 +00001174
Chris Lattner2079fde2001-10-13 06:41:08 +00001175 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1176 if (isVarArg) ParamTypeList.pop_back();
1177
Chris Lattnerdda71962001-11-26 18:54:16 +00001178 const MethodType *MT = MethodType::get(*$2, ParamTypeList, isVarArg);
Chris Lattneref9c23f2001-10-03 14:53:21 +00001179 const PointerType *PMT = PointerType::get(MT);
Chris Lattnerdda71962001-11-26 18:54:16 +00001180 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001181
Chris Lattnere1815642001-07-15 06:35:53 +00001182 Method *M = 0;
1183 if (SymbolTable *ST = CurModule.CurrentModule->getSymbolTable()) {
Chris Lattnerdda71962001-11-26 18:54:16 +00001184 if (Value *V = ST->lookup(PMT, MethodName)) { // Method already in symtab?
Chris Lattneref9c23f2001-10-03 14:53:21 +00001185 M = cast<Method>(V);
Chris Lattner00950542001-06-06 20:29:01 +00001186
Chris Lattnere1815642001-07-15 06:35:53 +00001187 // Yes it is. If this is the case, either we need to be a forward decl,
1188 // or it needs to be.
1189 if (!CurMeth.isDeclare && !M->isExternal())
Chris Lattnerdda71962001-11-26 18:54:16 +00001190 ThrowException("Redefinition of method '" + MethodName + "'!");
Chris Lattner34538142002-03-08 19:11:42 +00001191
1192 // If we found a preexisting method prototype, remove it from the module,
1193 // so that we don't get spurious conflicts with global & local variables.
1194 //
1195 CurModule.CurrentModule->getMethodList().remove(M);
Chris Lattnere1815642001-07-15 06:35:53 +00001196 }
1197 }
1198
1199 if (M == 0) { // Not already defined?
Chris Lattnerdda71962001-11-26 18:54:16 +00001200 M = new Method(MT, $1, MethodName);
Chris Lattnere1815642001-07-15 06:35:53 +00001201 InsertValue(M, CurModule.Values);
Chris Lattnerdda71962001-11-26 18:54:16 +00001202 CurModule.DeclareNewGlobalValue(M, ValID::create($3));
Chris Lattnere1815642001-07-15 06:35:53 +00001203 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001204 free($3); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +00001205
1206 CurMeth.MethodStart(M);
1207
1208 // Add all of the arguments we parsed to the method...
Chris Lattnerdda71962001-11-26 18:54:16 +00001209 if ($5 && !CurMeth.isDeclare) { // Is null if empty...
Chris Lattner00950542001-06-06 20:29:01 +00001210 Method::ArgumentListType &ArgList = M->getArgumentList();
1211
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001212 for (list<pair<MethodArgument*, char*> >::iterator I = $5->begin();
1213 I != $5->end(); ++I) {
1214 if (setValueName(I->first, I->second)) { // Insert into symtab...
1215 assert(0 && "No arg redef allowed!");
1216 }
1217
1218 InsertValue(I->first);
1219 ArgList.push_back(I->first);
Chris Lattner00950542001-06-06 20:29:01 +00001220 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001221 delete $5; // We're now done with the argument list
Chris Lattner9176fe42002-03-08 18:57:56 +00001222 } else if ($5) {
1223 // If we are a declaration, we should free the memory for the argument list!
1224 for (list<pair<MethodArgument*, char*> >::iterator I = $5->begin();
1225 I != $5->end(); ++I)
1226 if (I->second) free(I->second); // Free the memory for the name...
1227 delete $5; // Free the memory for the list itself
Chris Lattner00950542001-06-06 20:29:01 +00001228 }
1229}
1230
1231MethodHeader : MethodHeaderH ConstPool BEGINTOK {
1232 $$ = CurMeth.CurrentMethod;
Chris Lattner30c89792001-09-07 16:35:17 +00001233
1234 // Resolve circular types before we parse the body of the method.
1235 ResolveTypes(CurMeth.LateResolveTypes);
Chris Lattner00950542001-06-06 20:29:01 +00001236}
1237
1238Method : BasicBlockList END {
1239 $$ = $1;
1240}
1241
Chris Lattnere1815642001-07-15 06:35:53 +00001242MethodProto : DECLARE { CurMeth.isDeclare = true; } MethodHeaderH {
1243 $$ = CurMeth.CurrentMethod;
Chris Lattner34538142002-03-08 19:11:42 +00001244 assert($$->getParent() == 0 && "Method already in module!");
1245 CurModule.CurrentModule->getMethodList().push_back($$);
Chris Lattner93750fa2001-07-28 17:48:55 +00001246 CurMeth.MethodDone();
Chris Lattnere1815642001-07-15 06:35:53 +00001247}
Chris Lattner00950542001-06-06 20:29:01 +00001248
1249//===----------------------------------------------------------------------===//
1250// Rules to match Basic Blocks
1251//===----------------------------------------------------------------------===//
1252
1253ConstValueRef : ESINT64VAL { // A reference to a direct constant
1254 $$ = ValID::create($1);
1255 }
1256 | EUINT64VAL {
1257 $$ = ValID::create($1);
1258 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001259 | FPVAL { // Perhaps it's an FP constant?
1260 $$ = ValID::create($1);
1261 }
Chris Lattner00950542001-06-06 20:29:01 +00001262 | TRUE {
1263 $$ = ValID::create((int64_t)1);
1264 }
1265 | FALSE {
1266 $$ = ValID::create((int64_t)0);
1267 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001268 | NULL_TOK {
1269 $$ = ValID::createNull();
1270 }
1271
Chris Lattner93750fa2001-07-28 17:48:55 +00001272/*
Chris Lattner00950542001-06-06 20:29:01 +00001273 | STRINGCONSTANT { // Quoted strings work too... especially for methods
1274 $$ = ValID::create_conststr($1);
1275 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001276*/
Chris Lattner00950542001-06-06 20:29:01 +00001277
Chris Lattner2079fde2001-10-13 06:41:08 +00001278// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1279// another value.
1280//
1281SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001282 $$ = ValID::create($1);
1283 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001284 | VAR_ID { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001285 $$ = ValID::create($1);
1286 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001287
1288// ValueRef - A reference to a definition... either constant or symbolic
1289ValueRef : SymbolicValueRef | ConstValueRef
1290
Chris Lattner00950542001-06-06 20:29:01 +00001291
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001292// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1293// type immediately preceeds the value reference, and allows complex constant
1294// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001295ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001296 $$ = getVal(*$1, $2); delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +00001297 }
Chris Lattner8b81bf52001-07-25 22:47:46 +00001298
Chris Lattner00950542001-06-06 20:29:01 +00001299
1300BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner89219832001-10-03 19:35:04 +00001301 ($$ = $1)->getBasicBlocks().push_back($2);
Chris Lattner00950542001-06-06 20:29:01 +00001302 }
1303 | MethodHeader BasicBlock { // Do not allow methods with 0 basic blocks
Chris Lattner89219832001-10-03 19:35:04 +00001304 ($$ = $1)->getBasicBlocks().push_back($2);
Chris Lattner00950542001-06-06 20:29:01 +00001305 }
1306
1307
1308// Basic blocks are terminated by branching instructions:
1309// br, br/cc, switch, ret
1310//
Chris Lattner2079fde2001-10-13 06:41:08 +00001311BasicBlock : InstructionList OptAssign BBTerminatorInst {
1312 if (setValueName($3, $2)) { assert(0 && "No redefn allowed!"); }
1313 InsertValue($3);
1314
1315 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001316 InsertValue($1);
1317 $$ = $1;
1318 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001319 | LABELSTR InstructionList OptAssign BBTerminatorInst {
1320 if (setValueName($4, $3)) { assert(0 && "No redefn allowed!"); }
1321 InsertValue($4);
1322
1323 $2->getInstList().push_back($4);
Chris Lattnerb7474512001-10-03 15:39:04 +00001324 if (setValueName($2, $1)) { assert(0 && "No label redef allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001325
1326 InsertValue($2);
1327 $$ = $2;
1328 }
1329
1330InstructionList : InstructionList Inst {
1331 $1->getInstList().push_back($2);
1332 $$ = $1;
1333 }
1334 | /* empty */ {
1335 $$ = new BasicBlock();
1336 }
1337
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001338BBTerminatorInst : RET ResolvedVal { // Return with a result...
1339 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001340 }
1341 | RET VOID { // Return with no result...
1342 $$ = new ReturnInst();
1343 }
1344 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner9636a912001-10-01 16:18:37 +00001345 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $3)));
Chris Lattner00950542001-06-06 20:29:01 +00001346 } // Conditional Branch...
1347 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner9636a912001-10-01 16:18:37 +00001348 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $6)),
1349 cast<BasicBlock>(getVal(Type::LabelTy, $9)),
Chris Lattner00950542001-06-06 20:29:01 +00001350 getVal(Type::BoolTy, $3));
1351 }
1352 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1353 SwitchInst *S = new SwitchInst(getVal($2, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001354 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
Chris Lattner00950542001-06-06 20:29:01 +00001355 $$ = S;
1356
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001357 list<pair<Constant*, BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner00950542001-06-06 20:29:01 +00001358 end = $8->end();
Chris Lattner7fc9fe32001-06-27 23:41:11 +00001359 for (; I != end; ++I)
Chris Lattner00950542001-06-06 20:29:01 +00001360 S->dest_push_back(I->first, I->second);
1361 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001362 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO ResolvedVal
1363 EXCEPT ResolvedVal {
1364 const PointerType *PMTy;
1365 const MethodType *Ty;
1366
1367 if (!(PMTy = dyn_cast<PointerType>($2->get())) ||
Chris Lattner7a176752001-12-04 00:03:30 +00001368 !(Ty = dyn_cast<MethodType>(PMTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00001369 // Pull out the types of all of the arguments...
1370 vector<const Type*> ParamTypes;
1371 if ($5) {
Chris Lattner6cdb0112001-11-26 16:54:11 +00001372 for (vector<Value*>::iterator I = $5->begin(), E = $5->end(); I!=E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00001373 ParamTypes.push_back((*I)->getType());
1374 }
1375
1376 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1377 if (isVarArg) ParamTypes.pop_back();
1378
1379 Ty = MethodType::get($2->get(), ParamTypes, isVarArg);
1380 PMTy = PointerType::get(Ty);
1381 }
1382 delete $2;
1383
1384 Value *V = getVal(PMTy, $3); // Get the method we're calling...
1385
1386 BasicBlock *Normal = dyn_cast<BasicBlock>($8);
1387 BasicBlock *Except = dyn_cast<BasicBlock>($10);
1388
1389 if (Normal == 0 || Except == 0)
1390 ThrowException("Invoke instruction without label destinations!");
1391
1392 // Create the call node...
1393 if (!$5) { // Has no arguments?
Chris Lattner386a3b72001-10-16 19:54:17 +00001394 $$ = new InvokeInst(V, Normal, Except, vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001395 } else { // Has arguments?
1396 // Loop through MethodType's arguments and ensure they are specified
1397 // correctly!
1398 //
1399 MethodType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1400 MethodType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
Chris Lattner6cdb0112001-11-26 16:54:11 +00001401 vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00001402
1403 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1404 if ((*ArgI)->getType() != *I)
1405 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001406 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001407
1408 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1409 ThrowException("Invalid number of parameters detected!");
1410
Chris Lattner6cdb0112001-11-26 16:54:11 +00001411 $$ = new InvokeInst(V, Normal, Except, *$5);
Chris Lattner2079fde2001-10-13 06:41:08 +00001412 }
1413 delete $5;
1414 }
1415
1416
Chris Lattner00950542001-06-06 20:29:01 +00001417
1418JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1419 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001420 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001421 if (V == 0)
1422 ThrowException("May only switch on a constant pool value!");
1423
Chris Lattner9636a912001-10-01 16:18:37 +00001424 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($5, $6))));
Chris Lattner00950542001-06-06 20:29:01 +00001425 }
1426 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001427 $$ = new list<pair<Constant*, BasicBlock*> >();
1428 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00001429
1430 if (V == 0)
1431 ThrowException("May only switch on a constant pool value!");
1432
Chris Lattner9636a912001-10-01 16:18:37 +00001433 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($4, $5))));
Chris Lattner00950542001-06-06 20:29:01 +00001434 }
1435
1436Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001437 // Is this definition named?? if so, assign the name...
1438 if (setValueName($2, $1)) { assert(0 && "No redefin allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001439 InsertValue($2);
1440 $$ = $2;
1441}
1442
Chris Lattnerc24d2082001-06-11 15:04:20 +00001443PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
1444 $$ = new list<pair<Value*, BasicBlock*> >();
Chris Lattner30c89792001-09-07 16:35:17 +00001445 $$->push_back(make_pair(getVal(*$1, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001446 cast<BasicBlock>(getVal(Type::LabelTy, $5))));
Chris Lattner30c89792001-09-07 16:35:17 +00001447 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001448 }
1449 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1450 $$ = $1;
1451 $1->push_back(make_pair(getVal($1->front().first->getType(), $4),
Chris Lattner9636a912001-10-01 16:18:37 +00001452 cast<BasicBlock>(getVal(Type::LabelTy, $6))));
Chris Lattnerc24d2082001-06-11 15:04:20 +00001453 }
1454
1455
Chris Lattner30c89792001-09-07 16:35:17 +00001456ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner6cdb0112001-11-26 16:54:11 +00001457 $$ = new vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001458 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001459 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001460 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001461 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001462 $1->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001463 }
1464
1465// ValueRefListE - Just like ValueRefList, except that it may also be empty!
1466ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; }
1467
1468InstVal : BinaryOps Types ValueRef ',' ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001469 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001470 if ($$ == 0)
1471 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001472 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001473 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001474 | UnaryOps ResolvedVal {
1475 $$ = UnaryOperator::create($1, $2);
Chris Lattner00950542001-06-06 20:29:01 +00001476 if ($$ == 0)
1477 ThrowException("unary operator returned null!");
Chris Lattner09083092001-07-08 04:57:15 +00001478 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001479 | ShiftOps ResolvedVal ',' ResolvedVal {
1480 if ($4->getType() != Type::UByteTy)
1481 ThrowException("Shift amount must be ubyte!");
1482 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001483 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001484 | CAST ResolvedVal TO Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001485 $$ = new CastInst($2, *$4);
1486 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001487 }
Chris Lattnerc24d2082001-06-11 15:04:20 +00001488 | PHI PHIList {
1489 const Type *Ty = $2->front().first->getType();
1490 $$ = new PHINode(Ty);
Chris Lattner00950542001-06-06 20:29:01 +00001491 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001492 if ($2->front().first->getType() != Ty)
1493 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001494 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001495 $2->pop_front();
1496 }
1497 delete $2; // Free the list...
1498 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001499 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattneref9c23f2001-10-03 14:53:21 +00001500 const PointerType *PMTy;
Chris Lattner8b81bf52001-07-25 22:47:46 +00001501 const MethodType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001502
Chris Lattneref9c23f2001-10-03 14:53:21 +00001503 if (!(PMTy = dyn_cast<PointerType>($2->get())) ||
Chris Lattner7a176752001-12-04 00:03:30 +00001504 !(Ty = dyn_cast<MethodType>(PMTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001505 // Pull out the types of all of the arguments...
1506 vector<const Type*> ParamTypes;
Chris Lattneref9c23f2001-10-03 14:53:21 +00001507 if ($5) {
Chris Lattner6cdb0112001-11-26 16:54:11 +00001508 for (vector<Value*>::iterator I = $5->begin(), E = $5->end(); I!=E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00001509 ParamTypes.push_back((*I)->getType());
1510 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001511
1512 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1513 if (isVarArg) ParamTypes.pop_back();
1514
1515 Ty = MethodType::get($2->get(), ParamTypes, isVarArg);
Chris Lattneref9c23f2001-10-03 14:53:21 +00001516 PMTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001517 }
Chris Lattner30c89792001-09-07 16:35:17 +00001518 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001519
Chris Lattneref9c23f2001-10-03 14:53:21 +00001520 Value *V = getVal(PMTy, $3); // Get the method we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001521
Chris Lattner8b81bf52001-07-25 22:47:46 +00001522 // Create the call node...
1523 if (!$5) { // Has no arguments?
Chris Lattner386a3b72001-10-16 19:54:17 +00001524 $$ = new CallInst(V, vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001525 } else { // Has arguments?
Chris Lattner00950542001-06-06 20:29:01 +00001526 // Loop through MethodType's arguments and ensure they are specified
1527 // correctly!
1528 //
1529 MethodType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001530 MethodType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
Chris Lattner6cdb0112001-11-26 16:54:11 +00001531 vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001532
1533 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1534 if ((*ArgI)->getType() != *I)
1535 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001536 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001537
Chris Lattner8b81bf52001-07-25 22:47:46 +00001538 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner00950542001-06-06 20:29:01 +00001539 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00001540
Chris Lattner6cdb0112001-11-26 16:54:11 +00001541 $$ = new CallInst(V, *$5);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001542 }
1543 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00001544 }
1545 | MemoryInst {
1546 $$ = $1;
1547 }
1548
Chris Lattner6cdb0112001-11-26 16:54:11 +00001549
1550// IndexList - List of indices for GEP based instructions...
1551IndexList : ',' ValueRefList {
Chris Lattner027dcc52001-07-08 21:10:27 +00001552 $$ = $2;
1553} | /* empty */ {
Chris Lattner6cdb0112001-11-26 16:54:11 +00001554 $$ = new vector<Value*>();
Chris Lattner027dcc52001-07-08 21:10:27 +00001555}
1556
Chris Lattner00950542001-06-06 20:29:01 +00001557MemoryInst : MALLOC Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001558 $$ = new MallocInst(PointerType::get(*$2));
1559 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001560 }
1561 | MALLOC Types ',' UINT ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001562 const Type *Ty = PointerType::get(*$2);
Chris Lattner8896eda2001-07-09 19:38:36 +00001563 $$ = new MallocInst(Ty, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001564 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001565 }
1566 | ALLOCA Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001567 $$ = new AllocaInst(PointerType::get(*$2));
1568 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001569 }
1570 | ALLOCA Types ',' UINT ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001571 const Type *Ty = PointerType::get(*$2);
Chris Lattner00950542001-06-06 20:29:01 +00001572 Value *ArrSize = getVal($4, $5);
Chris Lattnerf0d0e9c2001-07-07 08:36:30 +00001573 $$ = new AllocaInst(Ty, ArrSize);
Chris Lattner30c89792001-09-07 16:35:17 +00001574 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001575 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001576 | FREE ResolvedVal {
1577 if (!$2->getType()->isPointerType())
1578 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00001579 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001580 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001581 }
1582
Chris Lattner6cdb0112001-11-26 16:54:11 +00001583 | LOAD Types ValueRef IndexList {
Chris Lattner30c89792001-09-07 16:35:17 +00001584 if (!(*$2)->isPointerType())
Chris Lattner2079fde2001-10-13 06:41:08 +00001585 ThrowException("Can't load from nonpointer type: " +
1586 (*$2)->getDescription());
Chris Lattner30c89792001-09-07 16:35:17 +00001587 if (LoadInst::getIndexedType(*$2, *$4) == 0)
Chris Lattner027dcc52001-07-08 21:10:27 +00001588 ThrowException("Invalid indices for load instruction!");
1589
Chris Lattner30c89792001-09-07 16:35:17 +00001590 $$ = new LoadInst(getVal(*$2, $3), *$4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001591 delete $4; // Free the vector...
Chris Lattner30c89792001-09-07 16:35:17 +00001592 delete $2;
Chris Lattner027dcc52001-07-08 21:10:27 +00001593 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00001594 | STORE ResolvedVal ',' Types ValueRef IndexList {
Chris Lattner30c89792001-09-07 16:35:17 +00001595 if (!(*$4)->isPointerType())
Chris Lattner72e00252001-12-14 16:28:42 +00001596 ThrowException("Can't store to a nonpointer type: " +
1597 (*$4)->getDescription());
Chris Lattner30c89792001-09-07 16:35:17 +00001598 const Type *ElTy = StoreInst::getIndexedType(*$4, *$6);
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001599 if (ElTy == 0)
1600 ThrowException("Can't store into that field list!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001601 if (ElTy != $2->getType())
Chris Lattner72e00252001-12-14 16:28:42 +00001602 ThrowException("Can't store '" + $2->getType()->getDescription() +
1603 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001604 $$ = new StoreInst($2, getVal(*$4, $5), *$6);
1605 delete $4; delete $6;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001606 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00001607 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner30c89792001-09-07 16:35:17 +00001608 if (!(*$2)->isPointerType())
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001609 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner30c89792001-09-07 16:35:17 +00001610 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner72e00252001-12-14 16:28:42 +00001611 ThrowException("Can't get element ptr '" + (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001612 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
1613 delete $2; delete $4;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001614 }
Chris Lattner027dcc52001-07-08 21:10:27 +00001615
Chris Lattner00950542001-06-06 20:29:01 +00001616%%
Chris Lattner09083092001-07-08 04:57:15 +00001617int yyerror(const char *ErrorMsg) {
Chris Lattner00950542001-06-06 20:29:01 +00001618 ThrowException(string("Parse error: ") + ErrorMsg);
1619 return 0;
1620}