blob: 285e78acefb51c467415297574525cbf8382b881 [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...
25
Chris Lattner386a3b72001-10-16 19:54:17 +000026int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
Chris Lattner09083092001-07-08 04:57:15 +000027int yylex(); // declaration" of xxx warnings.
Chris Lattner00950542001-06-06 20:29:01 +000028int yyparse();
29
30static Module *ParserResult;
Chris Lattnera2850432001-07-22 18:36:00 +000031string CurFilename;
Chris Lattner00950542001-06-06 20:29:01 +000032
Chris Lattner30c89792001-09-07 16:35:17 +000033// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
34// relating to upreferences in the input stream.
35//
36//#define DEBUG_UPREFS 1
37#ifdef DEBUG_UPREFS
38#define UR_OUT(X) cerr << X
39#else
40#define UR_OUT(X)
41#endif
42
Chris Lattner00950542001-06-06 20:29:01 +000043// This contains info used when building the body of a method. It is destroyed
44// when the method is completed.
45//
46typedef vector<Value *> ValueList; // Numbered defs
Chris Lattner386a3b72001-10-16 19:54:17 +000047static void ResolveDefinitions(vector<ValueList> &LateResolvers,
48 vector<ValueList> *FutureLateResolvers = 0);
Chris Lattner30c89792001-09-07 16:35:17 +000049static void ResolveTypes (vector<PATypeHolder<Type> > &LateResolveTypes);
Chris Lattner00950542001-06-06 20:29:01 +000050
51static struct PerModuleInfo {
52 Module *CurrentModule;
Chris Lattner30c89792001-09-07 16:35:17 +000053 vector<ValueList> Values; // Module level numbered definitions
54 vector<ValueList> LateResolveValues;
Chris Lattner4a42e902001-10-22 05:56:09 +000055 vector<PATypeHolder<Type> > Types;
56 map<ValID, PATypeHolder<Type> > LateResolveTypes;
Chris Lattner00950542001-06-06 20:29:01 +000057
Chris Lattner2079fde2001-10-13 06:41:08 +000058 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
59 // references to global values. Global values may be referenced before they
60 // are defined, and if so, the temporary object that they represent is held
Chris Lattnere9bb2df2001-12-03 22:26:30 +000061 // here. This is used for forward references of ConstantPointerRefs.
Chris Lattner2079fde2001-10-13 06:41:08 +000062 //
63 typedef map<pair<const PointerType *, ValID>, GlobalVariable*> GlobalRefsType;
64 GlobalRefsType GlobalRefs;
65
Chris Lattner00950542001-06-06 20:29:01 +000066 void ModuleDone() {
Chris Lattner30c89792001-09-07 16:35:17 +000067 // If we could not resolve some methods at method compilation time (calls to
68 // methods before they are defined), resolve them now... Types are resolved
69 // when the constant pool has been completely parsed.
70 //
Chris Lattner00950542001-06-06 20:29:01 +000071 ResolveDefinitions(LateResolveValues);
72
Chris Lattner2079fde2001-10-13 06:41:08 +000073 // Check to make sure that all global value forward references have been
74 // resolved!
75 //
76 if (!GlobalRefs.empty()) {
77 // TODO: Make this more detailed! Loop over each undef value and print
78 // info
Chris Lattner4a42e902001-10-22 05:56:09 +000079 ThrowException("TODO: Make better error - Unresolved forward constant "
80 "references exist!");
Chris Lattner2079fde2001-10-13 06:41:08 +000081 }
82
Chris Lattner00950542001-06-06 20:29:01 +000083 Values.clear(); // Clear out method local definitions
Chris Lattner30c89792001-09-07 16:35:17 +000084 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +000085 CurrentModule = 0;
86 }
Chris Lattner2079fde2001-10-13 06:41:08 +000087
88
89 // DeclareNewGlobalValue - Called every type a new GV has been defined. This
90 // is used to remove things from the forward declaration map, resolving them
91 // to the correct thing as needed.
92 //
93 void DeclareNewGlobalValue(GlobalValue *GV, ValID D) {
94 // Check to see if there is a forward reference to this global variable...
95 // if there is, eliminate it and patch the reference to use the new def'n.
96 GlobalRefsType::iterator I = GlobalRefs.find(make_pair(GV->getType(), D));
97
98 if (I != GlobalRefs.end()) {
99 GlobalVariable *OldGV = I->second; // Get the placeholder...
100 I->first.second.destroy(); // Free string memory if neccesary
101
102 // Loop over all of the uses of the GlobalValue. The only thing they are
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000103 // allowed to be at this point is ConstantPointerRef's.
Chris Lattner2079fde2001-10-13 06:41:08 +0000104 assert(OldGV->use_size() == 1 && "Only one reference should exist!");
105 while (!OldGV->use_empty()) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000106 User *U = OldGV->use_back(); // Must be a ConstantPointerRef...
107 ConstantPointerRef *CPPR = cast<ConstantPointerRef>(U);
Chris Lattner2079fde2001-10-13 06:41:08 +0000108 assert(CPPR->getValue() == OldGV && "Something isn't happy");
109
110 // Change the const pool reference to point to the real global variable
111 // now. This should drop a use from the OldGV.
112 CPPR->mutateReference(GV);
113 }
114
115 // Remove GV from the module...
116 CurrentModule->getGlobalList().remove(OldGV);
117 delete OldGV; // Delete the old placeholder
118
119 // Remove the map entry for the global now that it has been created...
120 GlobalRefs.erase(I);
121 }
122 }
123
Chris Lattner00950542001-06-06 20:29:01 +0000124} CurModule;
125
126static struct PerMethodInfo {
127 Method *CurrentMethod; // Pointer to current method being created
128
Chris Lattnere1815642001-07-15 06:35:53 +0000129 vector<ValueList> Values; // Keep track of numbered definitions
Chris Lattner00950542001-06-06 20:29:01 +0000130 vector<ValueList> LateResolveValues;
Chris Lattner4a42e902001-10-22 05:56:09 +0000131 vector<PATypeHolder<Type> > Types;
132 map<ValID, PATypeHolder<Type> > LateResolveTypes;
Chris Lattnere1815642001-07-15 06:35:53 +0000133 bool isDeclare; // Is this method a forward declararation?
Chris Lattner00950542001-06-06 20:29:01 +0000134
135 inline PerMethodInfo() {
136 CurrentMethod = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000137 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000138 }
139
140 inline ~PerMethodInfo() {}
141
142 inline void MethodStart(Method *M) {
143 CurrentMethod = M;
144 }
145
146 void MethodDone() {
147 // If we could not resolve some blocks at parsing time (forward branches)
148 // resolve the branches now...
Chris Lattner386a3b72001-10-16 19:54:17 +0000149 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000150
151 Values.clear(); // Clear out method local definitions
Chris Lattner30c89792001-09-07 16:35:17 +0000152 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +0000153 CurrentMethod = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000154 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000155 }
156} CurMeth; // Info for the current method...
157
Chris Lattnerb7474512001-10-03 15:39:04 +0000158static bool inMethodScope() { return CurMeth.CurrentMethod != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000159
Chris Lattner00950542001-06-06 20:29:01 +0000160
161//===----------------------------------------------------------------------===//
162// Code to handle definitions of all the types
163//===----------------------------------------------------------------------===//
164
Chris Lattner2079fde2001-10-13 06:41:08 +0000165static int InsertValue(Value *D, vector<ValueList> &ValueTab = CurMeth.Values) {
166 if (D->hasName()) return -1; // Is this a numbered definition?
167
168 // Yes, insert the value into the value table...
169 unsigned type = D->getType()->getUniqueID();
170 if (ValueTab.size() <= type)
171 ValueTab.resize(type+1, ValueList());
172 //printf("Values[%d][%d] = %d\n", type, ValueTab[type].size(), D);
173 ValueTab[type].push_back(D);
174 return ValueTab[type].size()-1;
Chris Lattner00950542001-06-06 20:29:01 +0000175}
176
Chris Lattner30c89792001-09-07 16:35:17 +0000177// TODO: FIXME when Type are not const
178static void InsertType(const Type *Ty, vector<PATypeHolder<Type> > &Types) {
179 Types.push_back(Ty);
180}
181
182static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000183 switch (D.Type) {
184 case 0: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000185 unsigned Num = (unsigned)D.Num;
186
187 // Module constants occupy the lowest numbered slots...
188 if (Num < CurModule.Types.size())
189 return CurModule.Types[Num];
190
191 Num -= CurModule.Types.size();
192
193 // Check that the number is within bounds...
194 if (Num <= CurMeth.Types.size())
195 return CurMeth.Types[Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000196 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000197 }
198 case 1: { // Is it a named definition?
199 string Name(D.Name);
200 SymbolTable *SymTab = 0;
Chris Lattnerb7474512001-10-03 15:39:04 +0000201 if (inMethodScope()) SymTab = CurMeth.CurrentMethod->getSymbolTable();
Chris Lattner30c89792001-09-07 16:35:17 +0000202 Value *N = SymTab ? SymTab->lookup(Type::TypeTy, Name) : 0;
203
204 if (N == 0) {
205 // Symbol table doesn't automatically chain yet... because the method
206 // hasn't been added to the module...
207 //
208 SymTab = CurModule.CurrentModule->getSymbolTable();
209 if (SymTab)
210 N = SymTab->lookup(Type::TypeTy, Name);
211 if (N == 0) break;
212 }
213
214 D.destroy(); // Free old strdup'd memory...
Chris Lattnercfe26c92001-10-01 18:26:53 +0000215 return cast<const Type>(N);
Chris Lattner30c89792001-09-07 16:35:17 +0000216 }
217 default:
218 ThrowException("Invalid symbol type reference!");
219 }
220
221 // If we reached here, we referenced either a symbol that we don't know about
222 // or an id number that hasn't been read yet. We may be referencing something
223 // forward, so just create an entry to be resolved later and get to it...
224 //
225 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
226
Chris Lattner4a42e902001-10-22 05:56:09 +0000227 map<ValID, PATypeHolder<Type> > &LateResolver = inMethodScope() ?
228 CurMeth.LateResolveTypes : CurModule.LateResolveTypes;
229
230 map<ValID, PATypeHolder<Type> >::iterator I = LateResolver.find(D);
231 if (I != LateResolver.end()) {
232 return I->second;
233 }
Chris Lattner30c89792001-09-07 16:35:17 +0000234
Chris Lattner82269592001-10-22 06:01:08 +0000235 Type *Typ = OpaqueType::get();
Chris Lattner4a42e902001-10-22 05:56:09 +0000236 LateResolver.insert(make_pair(D, Typ));
Chris Lattner30c89792001-09-07 16:35:17 +0000237 return Typ;
238}
239
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000240static Value *lookupInSymbolTable(const Type *Ty, const string &Name) {
241 SymbolTable *SymTab =
Chris Lattnerb7474512001-10-03 15:39:04 +0000242 inMethodScope() ? CurMeth.CurrentMethod->getSymbolTable() : 0;
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000243 Value *N = SymTab ? SymTab->lookup(Ty, Name) : 0;
244
245 if (N == 0) {
246 // Symbol table doesn't automatically chain yet... because the method
247 // hasn't been added to the module...
248 //
249 SymTab = CurModule.CurrentModule->getSymbolTable();
250 if (SymTab)
251 N = SymTab->lookup(Ty, Name);
252 }
253
254 return N;
255}
256
Chris Lattner2079fde2001-10-13 06:41:08 +0000257// getValNonImprovising - Look up the value specified by the provided type and
258// the provided ValID. If the value exists and has already been defined, return
259// it. Otherwise return null.
260//
261static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Chris Lattner386a3b72001-10-16 19:54:17 +0000262 if (isa<MethodType>(Ty))
263 ThrowException("Methods are not values and must be referenced as pointers");
264
Chris Lattner30c89792001-09-07 16:35:17 +0000265 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000266 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000267 unsigned type = Ty->getUniqueID();
Chris Lattner00950542001-06-06 20:29:01 +0000268 unsigned Num = (unsigned)D.Num;
269
270 // Module constants occupy the lowest numbered slots...
271 if (type < CurModule.Values.size()) {
272 if (Num < CurModule.Values[type].size())
273 return CurModule.Values[type][Num];
274
275 Num -= CurModule.Values[type].size();
276 }
277
278 // Make sure that our type is within bounds
Chris Lattner2079fde2001-10-13 06:41:08 +0000279 if (CurMeth.Values.size() <= type) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000280
281 // Check that the number is within bounds...
Chris Lattner2079fde2001-10-13 06:41:08 +0000282 if (CurMeth.Values[type].size() <= Num) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000283
284 return CurMeth.Values[type][Num];
285 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000286
Chris Lattner1a1cb112001-09-30 22:46:54 +0000287 case ValID::NameVal: { // Is it a named definition?
Chris Lattner2079fde2001-10-13 06:41:08 +0000288 Value *N = lookupInSymbolTable(Ty, string(D.Name));
289 if (N == 0) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000290
291 D.destroy(); // Free old strdup'd memory...
292 return N;
293 }
294
Chris Lattner2079fde2001-10-13 06:41:08 +0000295 // Check to make sure that "Ty" is an integral type, and that our
296 // value will fit into the specified type...
297 case ValID::ConstSIntVal: // Is it a constant pool reference??
298 if (Ty == Type::BoolTy) { // Special handling for boolean data
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000299 return ConstantBool::get(D.ConstPool64 != 0);
Chris Lattner2079fde2001-10-13 06:41:08 +0000300 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000301 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
Chris Lattner2079fde2001-10-13 06:41:08 +0000302 ThrowException("Symbolic constant pool value '" +
303 itostr(D.ConstPool64) + "' is invalid for type '" +
Chris Lattner72e00252001-12-14 16:28:42 +0000304 Ty->getDescription() + "'!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000305 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner00950542001-06-06 20:29:01 +0000306 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000307
308 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000309 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
310 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
Chris Lattner2079fde2001-10-13 06:41:08 +0000311 ThrowException("Integral constant pool reference is invalid!");
312 } else { // This is really a signed reference. Transmogrify.
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000313 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000314 }
315 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000316 return ConstantUInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000317 }
318
319 case ValID::ConstStringVal: // Is it a string const pool reference?
320 cerr << "FIXME: TODO: String constants [sbyte] not implemented yet!\n";
321 abort();
322 return 0;
323
324 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000325 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattner2079fde2001-10-13 06:41:08 +0000326 ThrowException("FP constant invalid for type!!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000327 return ConstantFP::get(Ty, D.ConstPoolFP);
Chris Lattner2079fde2001-10-13 06:41:08 +0000328
329 case ValID::ConstNullVal: // Is it a null value?
330 if (!Ty->isPointerType())
331 ThrowException("Cannot create a a non pointer null!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000332 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner2079fde2001-10-13 06:41:08 +0000333
Chris Lattner30c89792001-09-07 16:35:17 +0000334 default:
335 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000336 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000337 } // End of switch
338
Chris Lattner2079fde2001-10-13 06:41:08 +0000339 assert(0 && "Unhandled case!");
340 return 0;
341}
342
343
344// getVal - This function is identical to getValNonImprovising, except that if a
345// value is not already defined, it "improvises" by creating a placeholder var
346// that looks and acts just like the requested variable. When the value is
347// defined later, all uses of the placeholder variable are replaced with the
348// real thing.
349//
350static Value *getVal(const Type *Ty, const ValID &D) {
351 assert(Ty != Type::TypeTy && "Should use getTypeVal for types!");
352
353 // See if the value has already been defined...
354 Value *V = getValNonImprovising(Ty, D);
355 if (V) return V;
Chris Lattner00950542001-06-06 20:29:01 +0000356
357 // If we reached here, we referenced either a symbol that we don't know about
358 // or an id number that hasn't been read yet. We may be referencing something
359 // forward, so just create an entry to be resolved later and get to it...
360 //
Chris Lattner00950542001-06-06 20:29:01 +0000361 Value *d = 0;
Chris Lattner30c89792001-09-07 16:35:17 +0000362 switch (Ty->getPrimitiveID()) {
363 case Type::LabelTyID: d = new BBPlaceHolder(Ty, D); break;
Chris Lattner30c89792001-09-07 16:35:17 +0000364 default: d = new ValuePlaceHolder(Ty, D); break;
Chris Lattner00950542001-06-06 20:29:01 +0000365 }
366
367 assert(d != 0 && "How did we not make something?");
Chris Lattner386a3b72001-10-16 19:54:17 +0000368 if (inMethodScope())
369 InsertValue(d, CurMeth.LateResolveValues);
370 else
371 InsertValue(d, CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000372 return d;
373}
374
375
376//===----------------------------------------------------------------------===//
377// Code to handle forward references in instructions
378//===----------------------------------------------------------------------===//
379//
380// This code handles the late binding needed with statements that reference
381// values not defined yet... for example, a forward branch, or the PHI node for
382// a loop body.
383//
384// This keeps a table (CurMeth.LateResolveValues) of all such forward references
385// and back patchs after we are done.
386//
387
388// ResolveDefinitions - If we could not resolve some defs at parsing
389// time (forward branches, phi functions for loops, etc...) resolve the
390// defs now...
391//
Chris Lattner386a3b72001-10-16 19:54:17 +0000392static void ResolveDefinitions(vector<ValueList> &LateResolvers,
393 vector<ValueList> *FutureLateResolvers = 0) {
Chris Lattner00950542001-06-06 20:29:01 +0000394 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
395 for (unsigned ty = 0; ty < LateResolvers.size(); ty++) {
396 while (!LateResolvers[ty].empty()) {
397 Value *V = LateResolvers[ty].back();
Chris Lattner386a3b72001-10-16 19:54:17 +0000398 assert(!isa<Type>(V) && "Types should be in LateResolveTypes!");
399
Chris Lattner00950542001-06-06 20:29:01 +0000400 LateResolvers[ty].pop_back();
401 ValID &DID = getValIDFromPlaceHolder(V);
402
Chris Lattner2079fde2001-10-13 06:41:08 +0000403 Value *TheRealValue = getValNonImprovising(Type::getUniqueIDType(ty),DID);
Chris Lattner386a3b72001-10-16 19:54:17 +0000404 if (TheRealValue) {
405 V->replaceAllUsesWith(TheRealValue);
406 delete V;
407 } else if (FutureLateResolvers) {
408 // Methods have their unresolved items forwarded to the module late
409 // resolver table
410 InsertValue(V, *FutureLateResolvers);
411 } else {
Chris Lattner30c89792001-09-07 16:35:17 +0000412 if (DID.Type == 1)
413 ThrowException("Reference to an invalid definition: '" +DID.getName()+
414 "' of type '" + V->getType()->getDescription() + "'",
415 getLineNumFromPlaceHolder(V));
416 else
417 ThrowException("Reference to an invalid definition: #" +
418 itostr(DID.Num) + " of type '" +
419 V->getType()->getDescription() + "'",
420 getLineNumFromPlaceHolder(V));
421 }
Chris Lattner00950542001-06-06 20:29:01 +0000422 }
423 }
424
425 LateResolvers.clear();
426}
427
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000428// ResolveType - Take a specified unresolved type and resolve it. If there is
429// nothing to resolve it to yet, return true. Otherwise resolve it and return
430// false.
431//
432static bool ResolveType(PATypeHolder<Type> &T) {
433 const Type *Ty = T;
434 ValID &DID = getValIDFromPlaceHolder(Ty);
435
436 const Type *TheRealType = getTypeVal(DID, true);
Chris Lattner23192eb2001-10-21 21:43:25 +0000437 if (TheRealType == 0 || TheRealType == Ty) return true;
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000438
439 // Refine the opaque type we had to the new type we are getting.
440 cast<DerivedType>(Ty)->refineAbstractTypeTo(TheRealType);
441 return false;
442}
443
Chris Lattner4a42e902001-10-22 05:56:09 +0000444// ResolveTypeTo - A brand new type was just declared. This means that (if
445// name is not null) things referencing Name can be resolved. Otherwise, things
446// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000447//
Chris Lattner4a42e902001-10-22 05:56:09 +0000448static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattnerdda71962001-11-26 18:54:16 +0000449 vector<PATypeHolder<Type> > &Types = inMethodScope() ?
Chris Lattner4a42e902001-10-22 05:56:09 +0000450 CurMeth.Types : CurModule.Types;
Chris Lattner00950542001-06-06 20:29:01 +0000451
Chris Lattner4a42e902001-10-22 05:56:09 +0000452 ValID D;
453 if (Name) D = ValID::create(Name);
454 else D = ValID::create((int)Types.size());
Chris Lattner30c89792001-09-07 16:35:17 +0000455
Chris Lattner4a42e902001-10-22 05:56:09 +0000456 map<ValID, PATypeHolder<Type> > &LateResolver = inMethodScope() ?
457 CurMeth.LateResolveTypes : CurModule.LateResolveTypes;
458
459 map<ValID, PATypeHolder<Type> >::iterator I = LateResolver.find(D);
460 if (I != LateResolver.end()) {
461 cast<DerivedType>(I->second.get())->refineAbstractTypeTo(ToTy);
462 LateResolver.erase(I);
463 }
464}
465
466// ResolveTypes - At this point, all types should be resolved. Any that aren't
467// are errors.
468//
469static void ResolveTypes(map<ValID, PATypeHolder<Type> > &LateResolveTypes) {
470 if (!LateResolveTypes.empty()) {
Chris Lattner82269592001-10-22 06:01:08 +0000471 const ValID &DID = LateResolveTypes.begin()->first;
Chris Lattner4a42e902001-10-22 05:56:09 +0000472
473 if (DID.Type == ValID::NameVal)
Chris Lattner82269592001-10-22 06:01:08 +0000474 ThrowException("Reference to an invalid type: '" +DID.getName() + "'");
Chris Lattner4a42e902001-10-22 05:56:09 +0000475 else
Chris Lattner82269592001-10-22 06:01:08 +0000476 ThrowException("Reference to an invalid type: #" + itostr(DID.Num));
Chris Lattner30c89792001-09-07 16:35:17 +0000477 }
478}
479
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000480
Chris Lattner1781aca2001-09-18 04:00:54 +0000481// setValueName - Set the specified value to the name given. The name may be
482// null potentially, in which case this is a noop. The string passed in is
483// assumed to be a malloc'd string buffer, and is freed by this function.
484//
Chris Lattnerb7474512001-10-03 15:39:04 +0000485// This function returns true if the value has already been defined, but is
486// allowed to be redefined in the specified context. If the name is a new name
487// for the typeplane, false is returned.
488//
489static bool setValueName(Value *V, char *NameStr) {
490 if (NameStr == 0) return false;
Chris Lattner386a3b72001-10-16 19:54:17 +0000491
Chris Lattner1781aca2001-09-18 04:00:54 +0000492 string Name(NameStr); // Copy string
493 free(NameStr); // Free old string
494
Chris Lattner2079fde2001-10-13 06:41:08 +0000495 if (V->getType() == Type::VoidTy)
496 ThrowException("Can't assign name '" + Name +
497 "' to a null valued instruction!");
498
Chris Lattnerb7474512001-10-03 15:39:04 +0000499 SymbolTable *ST = inMethodScope() ?
Chris Lattner30c89792001-09-07 16:35:17 +0000500 CurMeth.CurrentMethod->getSymbolTableSure() :
501 CurModule.CurrentModule->getSymbolTableSure();
502
503 Value *Existing = ST->lookup(V->getType(), Name);
504 if (Existing) { // Inserting a name that is already defined???
505 // There is only one case where this is allowed: when we are refining an
506 // opaque type. In this case, Existing will be an opaque type.
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000507 if (const Type *Ty = dyn_cast<const Type>(Existing)) {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000508 if (OpaqueType *OpTy = dyn_cast<OpaqueType>(Ty)) {
Chris Lattner30c89792001-09-07 16:35:17 +0000509 // We ARE replacing an opaque type!
Chris Lattnerb00c5822001-10-02 03:41:24 +0000510 OpTy->refineAbstractTypeTo(cast<Type>(V));
Chris Lattnerb7474512001-10-03 15:39:04 +0000511 return true;
Chris Lattner30c89792001-09-07 16:35:17 +0000512 }
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000513 }
Chris Lattner30c89792001-09-07 16:35:17 +0000514
Chris Lattner9636a912001-10-01 16:18:37 +0000515 // Otherwise, we are a simple redefinition of a value, check to see if it
516 // is defined the same as the old one...
517 if (const Type *Ty = dyn_cast<const Type>(Existing)) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000518 if (Ty == cast<const Type>(V)) return true; // Yes, it's equal.
519 // cerr << "Type: " << Ty->getDescription() << " != "
520 // << cast<const Type>(V)->getDescription() << "!\n";
521 } else if (GlobalVariable *EGV = dyn_cast<GlobalVariable>(Existing)) {
Chris Lattner43efcbf2001-10-03 19:35:57 +0000522 // We are allowed to redefine a global variable in two circumstances:
523 // 1. If at least one of the globals is uninitialized or
524 // 2. If both initializers have the same value.
525 //
526 // This can only be done if the const'ness of the vars is the same.
527 //
Chris Lattner89219832001-10-03 19:35:04 +0000528 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
529 if (EGV->isConstant() == GV->isConstant() &&
530 (!EGV->hasInitializer() || !GV->hasInitializer() ||
531 EGV->getInitializer() == GV->getInitializer())) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000532
Chris Lattner89219832001-10-03 19:35:04 +0000533 // Make sure the existing global version gets the initializer!
534 if (GV->hasInitializer() && !EGV->hasInitializer())
535 EGV->setInitializer(GV->getInitializer());
536
Chris Lattner2079fde2001-10-13 06:41:08 +0000537 delete GV; // Destroy the duplicate!
Chris Lattner89219832001-10-03 19:35:04 +0000538 return true; // They are equivalent!
539 }
Chris Lattnerb7474512001-10-03 15:39:04 +0000540 }
Chris Lattner9636a912001-10-01 16:18:37 +0000541 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000542 ThrowException("Redefinition of value named '" + Name + "' in the '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000543 V->getType()->getDescription() + "' type plane!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000544 }
Chris Lattner00950542001-06-06 20:29:01 +0000545
Chris Lattner30c89792001-09-07 16:35:17 +0000546 V->setName(Name, ST);
Chris Lattnerb7474512001-10-03 15:39:04 +0000547 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000548}
549
Chris Lattner8896eda2001-07-09 19:38:36 +0000550
Chris Lattner30c89792001-09-07 16:35:17 +0000551//===----------------------------------------------------------------------===//
552// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000553//
Chris Lattner8896eda2001-07-09 19:38:36 +0000554
Chris Lattner30c89792001-09-07 16:35:17 +0000555// TypeContains - Returns true if Ty contains E in it.
556//
557static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner3ff43872001-09-28 22:56:31 +0000558 return find(df_begin(Ty), df_end(Ty), E) != df_end(Ty);
Chris Lattner30c89792001-09-07 16:35:17 +0000559}
Chris Lattner698b56e2001-07-20 19:15:08 +0000560
Chris Lattner30c89792001-09-07 16:35:17 +0000561
562static vector<pair<unsigned, OpaqueType *> > UpRefs;
563
564static PATypeHolder<Type> HandleUpRefs(const Type *ty) {
565 PATypeHolder<Type> Ty(ty);
Chris Lattner5084d032001-11-02 07:46:26 +0000566 UR_OUT("Type '" << ty->getDescription() <<
567 "' newly formed. Resolving upreferences.\n" <<
568 UpRefs.size() << " upreferences active!\n");
Chris Lattner30c89792001-09-07 16:35:17 +0000569 for (unsigned i = 0; i < UpRefs.size(); ) {
Chris Lattner5084d032001-11-02 07:46:26 +0000570 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Chris Lattner30c89792001-09-07 16:35:17 +0000571 << UpRefs[i].second->getDescription() << ") = "
Chris Lattner5084d032001-11-02 07:46:26 +0000572 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << endl);
Chris Lattner30c89792001-09-07 16:35:17 +0000573 if (TypeContains(Ty, UpRefs[i].second)) {
574 unsigned Level = --UpRefs[i].first; // Decrement level of upreference
Chris Lattner5084d032001-11-02 07:46:26 +0000575 UR_OUT(" Uplevel Ref Level = " << Level << endl);
Chris Lattner30c89792001-09-07 16:35:17 +0000576 if (Level == 0) { // Upreference should be resolved!
Chris Lattner5084d032001-11-02 07:46:26 +0000577 UR_OUT(" * Resolving upreference for "
578 << UpRefs[i].second->getDescription() << endl;
Chris Lattner30c89792001-09-07 16:35:17 +0000579 string OldName = UpRefs[i].second->getDescription());
580 UpRefs[i].second->refineAbstractTypeTo(Ty);
581 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
Chris Lattner5084d032001-11-02 07:46:26 +0000582 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
Chris Lattner30c89792001-09-07 16:35:17 +0000583 << (const void*)Ty << ", " << Ty->getDescription() << endl);
584 continue;
585 }
586 }
587
588 ++i; // Otherwise, no resolve, move on...
Chris Lattner8896eda2001-07-09 19:38:36 +0000589 }
Chris Lattner30c89792001-09-07 16:35:17 +0000590 // FIXME: TODO: this should return the updated type
Chris Lattner8896eda2001-07-09 19:38:36 +0000591 return Ty;
592}
593
Chris Lattner30c89792001-09-07 16:35:17 +0000594template <class TypeTy>
595inline static void TypeDone(PATypeHolder<TypeTy> *Ty) {
596 if (UpRefs.size())
597 ThrowException("Invalid upreference in type: " + (*Ty)->getDescription());
598}
599
600// newTH - Allocate a new type holder for the specified type
601template <class TypeTy>
602inline static PATypeHolder<TypeTy> *newTH(const TypeTy *Ty) {
603 return new PATypeHolder<TypeTy>(Ty);
604}
605template <class TypeTy>
606inline static PATypeHolder<TypeTy> *newTH(const PATypeHolder<TypeTy> &TH) {
607 return new PATypeHolder<TypeTy>(TH);
608}
609
610
Chris Lattner00950542001-06-06 20:29:01 +0000611//===----------------------------------------------------------------------===//
612// RunVMAsmParser - Define an interface to this parser
613//===----------------------------------------------------------------------===//
614//
Chris Lattnera2850432001-07-22 18:36:00 +0000615Module *RunVMAsmParser(const string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000616 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000617 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000618 llvmAsmlineno = 1; // Reset the current line number...
619
620 CurModule.CurrentModule = new Module(); // Allocate a new module to read
621 yyparse(); // Parse the file.
622 Module *Result = ParserResult;
Chris Lattner00950542001-06-06 20:29:01 +0000623 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
624 ParserResult = 0;
625
626 return Result;
627}
628
629%}
630
631%union {
Chris Lattner30c89792001-09-07 16:35:17 +0000632 Module *ModuleVal;
633 Method *MethodVal;
634 MethodArgument *MethArgVal;
635 BasicBlock *BasicBlockVal;
636 TerminatorInst *TermInstVal;
637 Instruction *InstVal;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000638 Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000639
Chris Lattner30c89792001-09-07 16:35:17 +0000640 const Type *PrimType;
641 PATypeHolder<Type> *TypeVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000642 Value *ValueVal;
643
644 list<MethodArgument*> *MethodArgList;
Chris Lattner6cdb0112001-11-26 16:54:11 +0000645 vector<Value*> *ValueList;
Chris Lattner30c89792001-09-07 16:35:17 +0000646 list<PATypeHolder<Type> > *TypeList;
Chris Lattnerc24d2082001-06-11 15:04:20 +0000647 list<pair<Value*, BasicBlock*> > *PHIList; // Represent the RHS of PHI node
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000648 list<pair<Constant*, BasicBlock*> > *JumpTable;
649 vector<Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000650
Chris Lattner30c89792001-09-07 16:35:17 +0000651 int64_t SInt64Val;
652 uint64_t UInt64Val;
653 int SIntVal;
654 unsigned UIntVal;
655 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000656 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000657
Chris Lattner30c89792001-09-07 16:35:17 +0000658 char *StrVal; // This memory is strdup'd!
659 ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000660
Chris Lattner30c89792001-09-07 16:35:17 +0000661 Instruction::UnaryOps UnaryOpVal;
662 Instruction::BinaryOps BinaryOpVal;
663 Instruction::TermOps TermOpVal;
664 Instruction::MemoryOps MemOpVal;
665 Instruction::OtherOps OtherOpVal;
Chris Lattner00950542001-06-06 20:29:01 +0000666}
667
668%type <ModuleVal> Module MethodList
Chris Lattnere1815642001-07-15 06:35:53 +0000669%type <MethodVal> Method MethodProto MethodHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000670%type <BasicBlockVal> BasicBlock InstructionList
671%type <TermInstVal> BBTerminatorInst
672%type <InstVal> Inst InstVal MemoryInst
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000673%type <ConstVal> ConstVal
Chris Lattner6cdb0112001-11-26 16:54:11 +0000674%type <ConstVector> ConstVector
Chris Lattner00950542001-06-06 20:29:01 +0000675%type <MethodArgList> ArgList ArgListH
676%type <MethArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000677%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000678%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000679%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000680%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000681%type <JumpTable> JumpTable
Chris Lattnerdda71962001-11-26 18:54:16 +0000682%type <BoolVal> GlobalType OptInternal // GLOBAL or CONSTANT? Intern?
Chris Lattner00950542001-06-06 20:29:01 +0000683
Chris Lattner2079fde2001-10-13 06:41:08 +0000684// ValueRef - Unresolved reference to a definition or BB
685%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000686%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000687// Tokens and types for handling constant integer values
688//
689// ESINT64VAL - A negative number within long long range
690%token <SInt64Val> ESINT64VAL
691
692// EUINT64VAL - A positive number within uns. long long range
693%token <UInt64Val> EUINT64VAL
694%type <SInt64Val> EINT64VAL
695
696%token <SIntVal> SINTVAL // Signed 32 bit ints...
697%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
698%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000699%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000700
701// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000702%type <TypeVal> Types TypesV UpRTypes UpRTypesV
703%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
704%token <TypeVal> OPAQUE
705%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
706%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000707
708%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
709%type <StrVal> OptVAR_ID OptAssign
710
711
Chris Lattner1781aca2001-09-18 04:00:54 +0000712%token IMPLEMENTATION TRUE FALSE BEGINTOK END DECLARE GLOBAL CONSTANT UNINIT
Chris Lattnerdda71962001-11-26 18:54:16 +0000713%token TO EXCEPT DOTDOTDOT STRING NULL_TOK CONST INTERNAL
Chris Lattner00950542001-06-06 20:29:01 +0000714
715// Basic Block Terminating Operators
716%token <TermOpVal> RET BR SWITCH
717
718// Unary Operators
719%type <UnaryOpVal> UnaryOps // all the unary operators
Chris Lattner71496b32001-07-08 19:03:27 +0000720%token <UnaryOpVal> NOT
Chris Lattner00950542001-06-06 20:29:01 +0000721
722// Binary Operators
723%type <BinaryOpVal> BinaryOps // all the binary operators
Chris Lattner42c9e772001-10-20 09:32:59 +0000724%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000725%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000726
727// Memory Instructions
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000728%token <MemoryOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000729
Chris Lattner027dcc52001-07-08 21:10:27 +0000730// Other Operators
731%type <OtherOpVal> ShiftOps
Chris Lattner2079fde2001-10-13 06:41:08 +0000732%token <OtherOpVal> PHI CALL INVOKE CAST SHL SHR
Chris Lattner027dcc52001-07-08 21:10:27 +0000733
Chris Lattner00950542001-06-06 20:29:01 +0000734%start Module
735%%
736
737// Handle constant integer size restriction and conversion...
738//
739
740INTVAL : SINTVAL
741INTVAL : UINTVAL {
742 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
743 ThrowException("Value too large for type!");
744 $$ = (int32_t)$1;
745}
746
747
748EINT64VAL : ESINT64VAL // These have same type and can't cause problems...
749EINT64VAL : EUINT64VAL {
750 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
751 ThrowException("Value too large for type!");
752 $$ = (int64_t)$1;
753}
754
Chris Lattner00950542001-06-06 20:29:01 +0000755// Operations that are notably excluded from this list include:
756// RET, BR, & SWITCH because they end basic blocks and are treated specially.
757//
Chris Lattner09083092001-07-08 04:57:15 +0000758UnaryOps : NOT
Chris Lattner42c9e772001-10-20 09:32:59 +0000759BinaryOps : ADD | SUB | MUL | DIV | REM | AND | OR | XOR
Chris Lattner00950542001-06-06 20:29:01 +0000760BinaryOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE
Chris Lattner027dcc52001-07-08 21:10:27 +0000761ShiftOps : SHL | SHR
Chris Lattner00950542001-06-06 20:29:01 +0000762
Chris Lattnere98dda62001-07-14 06:10:16 +0000763// These are some types that allow classification if we only want a particular
764// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner00950542001-06-06 20:29:01 +0000765SIntType : LONG | INT | SHORT | SBYTE
766UIntType : ULONG | UINT | USHORT | UBYTE
Chris Lattner30c89792001-09-07 16:35:17 +0000767IntType : SIntType | UIntType
768FPType : FLOAT | DOUBLE
Chris Lattner00950542001-06-06 20:29:01 +0000769
Chris Lattnere98dda62001-07-14 06:10:16 +0000770// OptAssign - Value producing statements have an optional assignment component
Chris Lattner00950542001-06-06 20:29:01 +0000771OptAssign : VAR_ID '=' {
772 $$ = $1;
773 }
774 | /*empty*/ {
775 $$ = 0;
776 }
777
Chris Lattnerdda71962001-11-26 18:54:16 +0000778OptInternal : INTERNAL { $$ = true; } | /*empty*/ { $$ = false; }
Chris Lattner30c89792001-09-07 16:35:17 +0000779
780//===----------------------------------------------------------------------===//
781// Types includes all predefined types... except void, because it can only be
782// used in specific contexts (method returning void for example). To have
783// access to it, a user must explicitly use TypesV.
784//
785
786// TypesV includes all of 'Types', but it also includes the void type.
787TypesV : Types | VOID { $$ = newTH($1); }
788UpRTypesV : UpRTypes | VOID { $$ = newTH($1); }
789
790Types : UpRTypes {
791 TypeDone($$ = $1);
792 }
793
794
795// Derived types are added later...
796//
797PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT
798PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL
799UpRTypes : OPAQUE | PrimType { $$ = newTH($1); }
800UpRTypes : ValueRef { // Named types are also simple types...
801 $$ = newTH(getTypeVal($1));
802}
803
Chris Lattner30c89792001-09-07 16:35:17 +0000804// Include derived types in the Types production.
805//
806UpRTypes : '\\' EUINT64VAL { // Type UpReference
807 if ($2 > (uint64_t)INT64_MAX) ThrowException("Value out of range!");
808 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
809 UpRefs.push_back(make_pair((unsigned)$2, OT)); // Add to vector...
810 $$ = newTH<Type>(OT);
811 UR_OUT("New Upreference!\n");
812 }
813 | UpRTypesV '(' ArgTypeListI ')' { // Method derived type?
814 vector<const Type*> Params;
815 mapto($3->begin(), $3->end(), back_inserter(Params),
816 mem_fun_ref(&PATypeHandle<Type>::get));
Chris Lattner2079fde2001-10-13 06:41:08 +0000817 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
818 if (isVarArg) Params.pop_back();
819
820 $$ = newTH(HandleUpRefs(MethodType::get(*$1, Params, isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +0000821 delete $3; // Delete the argument list
822 delete $1; // Delete the old type handle
823 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000824 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner72e00252001-12-14 16:28:42 +0000825 $$ = newTH<Type>(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000826 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +0000827 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000828 | '{' TypeListI '}' { // Structure type?
829 vector<const Type*> Elements;
830 mapto($2->begin(), $2->end(), back_inserter(Elements),
831 mem_fun_ref(&PATypeHandle<Type>::get));
Chris Lattner30c89792001-09-07 16:35:17 +0000832
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000833 $$ = newTH<Type>(HandleUpRefs(StructType::get(Elements)));
834 delete $2;
835 }
836 | '{' '}' { // Empty structure type?
837 $$ = newTH<Type>(StructType::get(vector<const Type*>()));
838 }
839 | UpRTypes '*' { // Pointer type?
840 $$ = newTH<Type>(HandleUpRefs(PointerType::get(*$1)));
841 delete $1;
842 }
Chris Lattner30c89792001-09-07 16:35:17 +0000843
844// TypeList - Used for struct declarations and as a basis for method type
845// declaration type lists
846//
847TypeListI : UpRTypes {
848 $$ = new list<PATypeHolder<Type> >();
849 $$->push_back(*$1); delete $1;
850 }
851 | TypeListI ',' UpRTypes {
852 ($$=$1)->push_back(*$3); delete $3;
853 }
854
855// ArgTypeList - List of types for a method type declaration...
856ArgTypeListI : TypeListI
857 | TypeListI ',' DOTDOTDOT {
858 ($$=$1)->push_back(Type::VoidTy);
859 }
860 | DOTDOTDOT {
861 ($$ = new list<PATypeHolder<Type> >())->push_back(Type::VoidTy);
862 }
863 | /*empty*/ {
864 $$ = new list<PATypeHolder<Type> >();
865 }
866
867
Chris Lattnere98dda62001-07-14 06:10:16 +0000868// ConstVal - The various declarations that go into the constant pool. This
869// includes all forward declarations of types, constants, and functions.
870//
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000871ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
872 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
873 if (ATy == 0)
874 ThrowException("Cannot make array constant with type: '" +
875 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +0000876 const Type *ETy = ATy->getElementType();
877 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +0000878
Chris Lattner30c89792001-09-07 16:35:17 +0000879 // Verify that we have the correct size...
880 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +0000881 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattner30c89792001-09-07 16:35:17 +0000882 utostr($3->size()) + " arguments, but has size of " +
883 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +0000884
Chris Lattner30c89792001-09-07 16:35:17 +0000885 // Verify all elements are correct type!
886 for (unsigned i = 0; i < $3->size(); i++) {
887 if (ETy != (*$3)[i]->getType())
Chris Lattner00950542001-06-06 20:29:01 +0000888 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +0000889 ETy->getDescription() +"' as required!\nIt is of type '"+
890 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +0000891 }
892
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000893 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +0000894 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000895 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000896 | Types '[' ']' {
897 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
898 if (ATy == 0)
899 ThrowException("Cannot make array constant with type: '" +
900 (*$1)->getDescription() + "'!");
901
902 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +0000903 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +0000904 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattner30c89792001-09-07 16:35:17 +0000905 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000906 $$ = ConstantArray::get(ATy, vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +0000907 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +0000908 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000909 | Types 'c' STRINGCONSTANT {
910 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
911 if (ATy == 0)
912 ThrowException("Cannot make array constant with type: '" +
913 (*$1)->getDescription() + "'!");
914
Chris Lattner30c89792001-09-07 16:35:17 +0000915 int NumElements = ATy->getNumElements();
916 const Type *ETy = ATy->getElementType();
917 char *EndStr = UnEscapeLexed($3, true);
918 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +0000919 ThrowException("Can't build string constant of size " +
Chris Lattner30c89792001-09-07 16:35:17 +0000920 itostr((int)(EndStr-$3)) +
921 " when array has size " + itostr(NumElements) + "!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000922 vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +0000923 if (ETy == Type::SByteTy) {
924 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000925 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +0000926 } else if (ETy == Type::UByteTy) {
927 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000928 Vals.push_back(ConstantUInt::get(ETy, *C));
Chris Lattner93750fa2001-07-28 17:48:55 +0000929 } else {
Chris Lattner30c89792001-09-07 16:35:17 +0000930 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +0000931 ThrowException("Cannot build string arrays of non byte sized elements!");
932 }
Chris Lattner30c89792001-09-07 16:35:17 +0000933 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000934 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +0000935 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +0000936 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000937 | Types '{' ConstVector '}' {
938 const StructType *STy = dyn_cast<const StructType>($1->get());
939 if (STy == 0)
940 ThrowException("Cannot make struct constant with type: '" +
941 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +0000942 // FIXME: TODO: Check to see that the constants are compatible with the type
943 // initializer!
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000944 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +0000945 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000946 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000947 | Types NULL_TOK {
948 const PointerType *PTy = dyn_cast<const PointerType>($1->get());
949 if (PTy == 0)
950 ThrowException("Cannot make null pointer constant with type: '" +
951 (*$1)->getDescription() + "'!");
952
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000953 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000954 delete $1;
955 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000956 | Types SymbolicValueRef {
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000957 const PointerType *Ty = dyn_cast<const PointerType>($1->get());
958 if (Ty == 0)
959 ThrowException("Global const reference must be a pointer type!");
960
Chris Lattner2079fde2001-10-13 06:41:08 +0000961 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000962
Chris Lattner2079fde2001-10-13 06:41:08 +0000963 // If this is an initializer for a constant pointer, which is referencing a
964 // (currently) undefined variable, create a stub now that shall be replaced
965 // in the future with the right type of variable.
966 //
967 if (V == 0) {
968 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
969 const PointerType *PT = cast<PointerType>(Ty);
970
971 // First check to see if the forward references value is already created!
972 PerModuleInfo::GlobalRefsType::iterator I =
973 CurModule.GlobalRefs.find(make_pair(PT, $2));
974
975 if (I != CurModule.GlobalRefs.end()) {
976 V = I->second; // Placeholder already exists, use it...
977 } else {
978 // TODO: Include line number info by creating a subclass of
979 // TODO: GlobalVariable here that includes the said information!
980
981 // Create a placeholder for the global variable reference...
Chris Lattner7a176752001-12-04 00:03:30 +0000982 GlobalVariable *GV = new GlobalVariable(PT->getElementType(),
983 false, true);
Chris Lattner2079fde2001-10-13 06:41:08 +0000984 // Keep track of the fact that we have a forward ref to recycle it
985 CurModule.GlobalRefs.insert(make_pair(make_pair(PT, $2), GV));
986
987 // Must temporarily push this value into the module table...
988 CurModule.CurrentModule->getGlobalList().push_back(GV);
989 V = GV;
990 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000991 }
992
Chris Lattner2079fde2001-10-13 06:41:08 +0000993 GlobalValue *GV = cast<GlobalValue>(V);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000994 $$ = ConstantPointerRef::get(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +0000995 delete $1; // Free the type handle
Chris Lattner00950542001-06-06 20:29:01 +0000996 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000997
Chris Lattner00950542001-06-06 20:29:01 +0000998
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000999ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001000 if (!ConstantSInt::isValueValidForType($1, $2))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001001 ThrowException("Constant value doesn't fit in type!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001002 $$ = ConstantSInt::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001003 }
1004 | UIntType EUINT64VAL { // integral constants
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001005 if (!ConstantUInt::isValueValidForType($1, $2))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001006 ThrowException("Constant value doesn't fit in type!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001007 $$ = ConstantUInt::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001008 }
1009 | BOOL TRUE { // Boolean constants
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001010 $$ = ConstantBool::True;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001011 }
1012 | BOOL FALSE { // Boolean constants
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001013 $$ = ConstantBool::False;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001014 }
1015 | FPType FPVAL { // Float & Double constants
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001016 $$ = ConstantFP::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001017 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001018
Chris Lattnere98dda62001-07-14 06:10:16 +00001019// ConstVector - A list of comma seperated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001020ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001021 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001022 }
1023 | ConstVal {
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001024 $$ = new vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001025 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001026 }
1027
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001028
Chris Lattner1781aca2001-09-18 04:00:54 +00001029// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
1030GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; }
1031
Chris Lattner00950542001-06-06 20:29:01 +00001032
Chris Lattnere98dda62001-07-14 06:10:16 +00001033// ConstPool - Constants with optional names assigned to them.
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001034ConstPool : ConstPool OptAssign CONST ConstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001035 if (setValueName($4, $2)) { assert(0 && "No redefinitions allowed!"); }
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001036 InsertValue($4);
Chris Lattner00950542001-06-06 20:29:01 +00001037 }
Chris Lattner30c89792001-09-07 16:35:17 +00001038 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner4a42e902001-10-22 05:56:09 +00001039 // Eagerly resolve types. This is not an optimization, this is a
1040 // requirement that is due to the fact that we could have this:
1041 //
1042 // %list = type { %list * }
1043 // %list = type { %list * } ; repeated type decl
1044 //
1045 // If types are not resolved eagerly, then the two types will not be
1046 // determined to be the same type!
1047 //
1048 ResolveTypeTo($2, $4->get());
1049
Chris Lattner1781aca2001-09-18 04:00:54 +00001050 // TODO: FIXME when Type are not const
Chris Lattnerb7474512001-10-03 15:39:04 +00001051 if (!setValueName(const_cast<Type*>($4->get()), $2)) {
1052 // If this is not a redefinition of a type...
1053 if (!$2) {
1054 InsertType($4->get(),
1055 inMethodScope() ? CurMeth.Types : CurModule.Types);
1056 }
Chris Lattner30c89792001-09-07 16:35:17 +00001057 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001058
1059 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001060 }
1061 | ConstPool MethodProto { // Method prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001062 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001063 | ConstPool OptAssign OptInternal GlobalType ConstVal {
1064 const Type *Ty = $5->getType();
Chris Lattner1781aca2001-09-18 04:00:54 +00001065 // Global declarations appear in Constant Pool
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001066 Constant *Initializer = $5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001067 if (Initializer == 0)
1068 ThrowException("Global value initializer is not a constant!");
1069
Chris Lattnerdda71962001-11-26 18:54:16 +00001070 GlobalVariable *GV = new GlobalVariable(Ty, $4, $3, Initializer);
Chris Lattnerb7474512001-10-03 15:39:04 +00001071 if (!setValueName(GV, $2)) { // If not redefining...
1072 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001073 int Slot = InsertValue(GV, CurModule.Values);
1074
1075 if (Slot != -1) {
1076 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1077 } else {
1078 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1079 (char*)GV->getName().c_str()));
1080 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001081 }
Chris Lattner1781aca2001-09-18 04:00:54 +00001082 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001083 | ConstPool OptAssign OptInternal UNINIT GlobalType Types {
1084 const Type *Ty = *$6;
Chris Lattner1781aca2001-09-18 04:00:54 +00001085 // Global declarations appear in Constant Pool
Chris Lattnerdda71962001-11-26 18:54:16 +00001086 GlobalVariable *GV = new GlobalVariable(Ty, $5, $3);
Chris Lattnerb7474512001-10-03 15:39:04 +00001087 if (!setValueName(GV, $2)) { // If not redefining...
1088 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001089 int Slot = InsertValue(GV, CurModule.Values);
1090
1091 if (Slot != -1) {
1092 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1093 } else {
1094 assert(GV->hasName() && "Not named and not numbered!?");
1095 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1096 (char*)GV->getName().c_str()));
1097 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001098 }
Chris Lattnere98dda62001-07-14 06:10:16 +00001099 }
Chris Lattner00950542001-06-06 20:29:01 +00001100 | /* empty: end of list */ {
1101 }
1102
1103
1104//===----------------------------------------------------------------------===//
1105// Rules to match Modules
1106//===----------------------------------------------------------------------===//
1107
1108// Module rule: Capture the result of parsing the whole file into a result
1109// variable...
1110//
1111Module : MethodList {
1112 $$ = ParserResult = $1;
1113 CurModule.ModuleDone();
1114}
1115
Chris Lattnere98dda62001-07-14 06:10:16 +00001116// MethodList - A list of methods, preceeded by a constant pool.
1117//
Chris Lattner00950542001-06-06 20:29:01 +00001118MethodList : MethodList Method {
Chris Lattner00950542001-06-06 20:29:01 +00001119 $$ = $1;
Chris Lattnere1815642001-07-15 06:35:53 +00001120 if (!$2->getParent())
1121 $1->getMethodList().push_back($2);
1122 CurMeth.MethodDone();
Chris Lattner00950542001-06-06 20:29:01 +00001123 }
Chris Lattnere1815642001-07-15 06:35:53 +00001124 | MethodList MethodProto {
1125 $$ = $1;
Chris Lattnere1815642001-07-15 06:35:53 +00001126 }
Chris Lattner00950542001-06-06 20:29:01 +00001127 | ConstPool IMPLEMENTATION {
1128 $$ = CurModule.CurrentModule;
Chris Lattner30c89792001-09-07 16:35:17 +00001129 // Resolve circular types before we parse the body of the module
1130 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner00950542001-06-06 20:29:01 +00001131 }
1132
1133
1134//===----------------------------------------------------------------------===//
1135// Rules to match Method Headers
1136//===----------------------------------------------------------------------===//
1137
1138OptVAR_ID : VAR_ID | /*empty*/ { $$ = 0; }
1139
1140ArgVal : Types OptVAR_ID {
Chris Lattner30c89792001-09-07 16:35:17 +00001141 $$ = new MethodArgument(*$1); delete $1;
Chris Lattnerb7474512001-10-03 15:39:04 +00001142 if (setValueName($$, $2)) { assert(0 && "No arg redef allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001143}
1144
1145ArgListH : ArgVal ',' ArgListH {
1146 $$ = $3;
1147 $3->push_front($1);
1148 }
1149 | ArgVal {
1150 $$ = new list<MethodArgument*>();
1151 $$->push_front($1);
1152 }
Chris Lattner8b81bf52001-07-25 22:47:46 +00001153 | DOTDOTDOT {
1154 $$ = new list<MethodArgument*>();
Chris Lattner2079fde2001-10-13 06:41:08 +00001155 $$->push_front(new MethodArgument(Type::VoidTy));
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)
1171 for (list<MethodArgument*>::iterator I = $5->begin(); I != $5->end(); ++I)
Chris Lattner00950542001-06-06 20:29:01 +00001172 ParamTypeList.push_back((*I)->getType());
1173
Chris Lattner2079fde2001-10-13 06:41:08 +00001174 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1175 if (isVarArg) ParamTypeList.pop_back();
1176
Chris Lattnerdda71962001-11-26 18:54:16 +00001177 const MethodType *MT = MethodType::get(*$2, ParamTypeList, isVarArg);
Chris Lattneref9c23f2001-10-03 14:53:21 +00001178 const PointerType *PMT = PointerType::get(MT);
Chris Lattnerdda71962001-11-26 18:54:16 +00001179 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001180
Chris Lattnere1815642001-07-15 06:35:53 +00001181 Method *M = 0;
1182 if (SymbolTable *ST = CurModule.CurrentModule->getSymbolTable()) {
Chris Lattnerdda71962001-11-26 18:54:16 +00001183 if (Value *V = ST->lookup(PMT, MethodName)) { // Method already in symtab?
Chris Lattneref9c23f2001-10-03 14:53:21 +00001184 M = cast<Method>(V);
Chris Lattner00950542001-06-06 20:29:01 +00001185
Chris Lattnere1815642001-07-15 06:35:53 +00001186 // Yes it is. If this is the case, either we need to be a forward decl,
1187 // or it needs to be.
1188 if (!CurMeth.isDeclare && !M->isExternal())
Chris Lattnerdda71962001-11-26 18:54:16 +00001189 ThrowException("Redefinition of method '" + MethodName + "'!");
Chris Lattnere1815642001-07-15 06:35:53 +00001190 }
1191 }
1192
1193 if (M == 0) { // Not already defined?
Chris Lattnerdda71962001-11-26 18:54:16 +00001194 M = new Method(MT, $1, MethodName);
Chris Lattnere1815642001-07-15 06:35:53 +00001195 InsertValue(M, CurModule.Values);
Chris Lattnerdda71962001-11-26 18:54:16 +00001196 CurModule.DeclareNewGlobalValue(M, ValID::create($3));
Chris Lattnere1815642001-07-15 06:35:53 +00001197 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001198 free($3); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +00001199
1200 CurMeth.MethodStart(M);
1201
1202 // Add all of the arguments we parsed to the method...
Chris Lattnerdda71962001-11-26 18:54:16 +00001203 if ($5 && !CurMeth.isDeclare) { // Is null if empty...
Chris Lattner00950542001-06-06 20:29:01 +00001204 Method::ArgumentListType &ArgList = M->getArgumentList();
1205
Chris Lattnerdda71962001-11-26 18:54:16 +00001206 for (list<MethodArgument*>::iterator I = $5->begin(); I != $5->end(); ++I) {
Chris Lattner00950542001-06-06 20:29:01 +00001207 InsertValue(*I);
1208 ArgList.push_back(*I);
1209 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001210 delete $5; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00001211 }
1212}
1213
1214MethodHeader : MethodHeaderH ConstPool BEGINTOK {
1215 $$ = CurMeth.CurrentMethod;
Chris Lattner30c89792001-09-07 16:35:17 +00001216
1217 // Resolve circular types before we parse the body of the method.
1218 ResolveTypes(CurMeth.LateResolveTypes);
Chris Lattner00950542001-06-06 20:29:01 +00001219}
1220
1221Method : BasicBlockList END {
1222 $$ = $1;
1223}
1224
Chris Lattnere1815642001-07-15 06:35:53 +00001225MethodProto : DECLARE { CurMeth.isDeclare = true; } MethodHeaderH {
1226 $$ = CurMeth.CurrentMethod;
Chris Lattner93750fa2001-07-28 17:48:55 +00001227 if (!$$->getParent())
1228 CurModule.CurrentModule->getMethodList().push_back($$);
1229 CurMeth.MethodDone();
Chris Lattnere1815642001-07-15 06:35:53 +00001230}
Chris Lattner00950542001-06-06 20:29:01 +00001231
1232//===----------------------------------------------------------------------===//
1233// Rules to match Basic Blocks
1234//===----------------------------------------------------------------------===//
1235
1236ConstValueRef : ESINT64VAL { // A reference to a direct constant
1237 $$ = ValID::create($1);
1238 }
1239 | EUINT64VAL {
1240 $$ = ValID::create($1);
1241 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001242 | FPVAL { // Perhaps it's an FP constant?
1243 $$ = ValID::create($1);
1244 }
Chris Lattner00950542001-06-06 20:29:01 +00001245 | TRUE {
1246 $$ = ValID::create((int64_t)1);
1247 }
1248 | FALSE {
1249 $$ = ValID::create((int64_t)0);
1250 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001251 | NULL_TOK {
1252 $$ = ValID::createNull();
1253 }
1254
Chris Lattner93750fa2001-07-28 17:48:55 +00001255/*
Chris Lattner00950542001-06-06 20:29:01 +00001256 | STRINGCONSTANT { // Quoted strings work too... especially for methods
1257 $$ = ValID::create_conststr($1);
1258 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001259*/
Chris Lattner00950542001-06-06 20:29:01 +00001260
Chris Lattner2079fde2001-10-13 06:41:08 +00001261// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1262// another value.
1263//
1264SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001265 $$ = ValID::create($1);
1266 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001267 | VAR_ID { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001268 $$ = ValID::create($1);
1269 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001270
1271// ValueRef - A reference to a definition... either constant or symbolic
1272ValueRef : SymbolicValueRef | ConstValueRef
1273
Chris Lattner00950542001-06-06 20:29:01 +00001274
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001275// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1276// type immediately preceeds the value reference, and allows complex constant
1277// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001278ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001279 $$ = getVal(*$1, $2); delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +00001280 }
Chris Lattner8b81bf52001-07-25 22:47:46 +00001281
Chris Lattner00950542001-06-06 20:29:01 +00001282
1283BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner89219832001-10-03 19:35:04 +00001284 ($$ = $1)->getBasicBlocks().push_back($2);
Chris Lattner00950542001-06-06 20:29:01 +00001285 }
1286 | MethodHeader BasicBlock { // Do not allow methods with 0 basic blocks
Chris Lattner89219832001-10-03 19:35:04 +00001287 ($$ = $1)->getBasicBlocks().push_back($2);
Chris Lattner00950542001-06-06 20:29:01 +00001288 }
1289
1290
1291// Basic blocks are terminated by branching instructions:
1292// br, br/cc, switch, ret
1293//
Chris Lattner2079fde2001-10-13 06:41:08 +00001294BasicBlock : InstructionList OptAssign BBTerminatorInst {
1295 if (setValueName($3, $2)) { assert(0 && "No redefn allowed!"); }
1296 InsertValue($3);
1297
1298 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001299 InsertValue($1);
1300 $$ = $1;
1301 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001302 | LABELSTR InstructionList OptAssign BBTerminatorInst {
1303 if (setValueName($4, $3)) { assert(0 && "No redefn allowed!"); }
1304 InsertValue($4);
1305
1306 $2->getInstList().push_back($4);
Chris Lattnerb7474512001-10-03 15:39:04 +00001307 if (setValueName($2, $1)) { assert(0 && "No label redef allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001308
1309 InsertValue($2);
1310 $$ = $2;
1311 }
1312
1313InstructionList : InstructionList Inst {
1314 $1->getInstList().push_back($2);
1315 $$ = $1;
1316 }
1317 | /* empty */ {
1318 $$ = new BasicBlock();
1319 }
1320
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001321BBTerminatorInst : RET ResolvedVal { // Return with a result...
1322 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001323 }
1324 | RET VOID { // Return with no result...
1325 $$ = new ReturnInst();
1326 }
1327 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner9636a912001-10-01 16:18:37 +00001328 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $3)));
Chris Lattner00950542001-06-06 20:29:01 +00001329 } // Conditional Branch...
1330 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner9636a912001-10-01 16:18:37 +00001331 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $6)),
1332 cast<BasicBlock>(getVal(Type::LabelTy, $9)),
Chris Lattner00950542001-06-06 20:29:01 +00001333 getVal(Type::BoolTy, $3));
1334 }
1335 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1336 SwitchInst *S = new SwitchInst(getVal($2, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001337 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
Chris Lattner00950542001-06-06 20:29:01 +00001338 $$ = S;
1339
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001340 list<pair<Constant*, BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner00950542001-06-06 20:29:01 +00001341 end = $8->end();
Chris Lattner7fc9fe32001-06-27 23:41:11 +00001342 for (; I != end; ++I)
Chris Lattner00950542001-06-06 20:29:01 +00001343 S->dest_push_back(I->first, I->second);
1344 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001345 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO ResolvedVal
1346 EXCEPT ResolvedVal {
1347 const PointerType *PMTy;
1348 const MethodType *Ty;
1349
1350 if (!(PMTy = dyn_cast<PointerType>($2->get())) ||
Chris Lattner7a176752001-12-04 00:03:30 +00001351 !(Ty = dyn_cast<MethodType>(PMTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00001352 // Pull out the types of all of the arguments...
1353 vector<const Type*> ParamTypes;
1354 if ($5) {
Chris Lattner6cdb0112001-11-26 16:54:11 +00001355 for (vector<Value*>::iterator I = $5->begin(), E = $5->end(); I!=E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00001356 ParamTypes.push_back((*I)->getType());
1357 }
1358
1359 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1360 if (isVarArg) ParamTypes.pop_back();
1361
1362 Ty = MethodType::get($2->get(), ParamTypes, isVarArg);
1363 PMTy = PointerType::get(Ty);
1364 }
1365 delete $2;
1366
1367 Value *V = getVal(PMTy, $3); // Get the method we're calling...
1368
1369 BasicBlock *Normal = dyn_cast<BasicBlock>($8);
1370 BasicBlock *Except = dyn_cast<BasicBlock>($10);
1371
1372 if (Normal == 0 || Except == 0)
1373 ThrowException("Invoke instruction without label destinations!");
1374
1375 // Create the call node...
1376 if (!$5) { // Has no arguments?
Chris Lattner386a3b72001-10-16 19:54:17 +00001377 $$ = new InvokeInst(V, Normal, Except, vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001378 } else { // Has arguments?
1379 // Loop through MethodType's arguments and ensure they are specified
1380 // correctly!
1381 //
1382 MethodType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1383 MethodType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
Chris Lattner6cdb0112001-11-26 16:54:11 +00001384 vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00001385
1386 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1387 if ((*ArgI)->getType() != *I)
1388 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001389 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001390
1391 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1392 ThrowException("Invalid number of parameters detected!");
1393
Chris Lattner6cdb0112001-11-26 16:54:11 +00001394 $$ = new InvokeInst(V, Normal, Except, *$5);
Chris Lattner2079fde2001-10-13 06:41:08 +00001395 }
1396 delete $5;
1397 }
1398
1399
Chris Lattner00950542001-06-06 20:29:01 +00001400
1401JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1402 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001403 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001404 if (V == 0)
1405 ThrowException("May only switch on a constant pool value!");
1406
Chris Lattner9636a912001-10-01 16:18:37 +00001407 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($5, $6))));
Chris Lattner00950542001-06-06 20:29:01 +00001408 }
1409 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001410 $$ = new list<pair<Constant*, BasicBlock*> >();
1411 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00001412
1413 if (V == 0)
1414 ThrowException("May only switch on a constant pool value!");
1415
Chris Lattner9636a912001-10-01 16:18:37 +00001416 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($4, $5))));
Chris Lattner00950542001-06-06 20:29:01 +00001417 }
1418
1419Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001420 // Is this definition named?? if so, assign the name...
1421 if (setValueName($2, $1)) { assert(0 && "No redefin allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001422 InsertValue($2);
1423 $$ = $2;
1424}
1425
Chris Lattnerc24d2082001-06-11 15:04:20 +00001426PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
1427 $$ = new list<pair<Value*, BasicBlock*> >();
Chris Lattner30c89792001-09-07 16:35:17 +00001428 $$->push_back(make_pair(getVal(*$1, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001429 cast<BasicBlock>(getVal(Type::LabelTy, $5))));
Chris Lattner30c89792001-09-07 16:35:17 +00001430 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001431 }
1432 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1433 $$ = $1;
1434 $1->push_back(make_pair(getVal($1->front().first->getType(), $4),
Chris Lattner9636a912001-10-01 16:18:37 +00001435 cast<BasicBlock>(getVal(Type::LabelTy, $6))));
Chris Lattnerc24d2082001-06-11 15:04:20 +00001436 }
1437
1438
Chris Lattner30c89792001-09-07 16:35:17 +00001439ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner6cdb0112001-11-26 16:54:11 +00001440 $$ = new vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001441 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001442 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001443 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001444 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001445 $1->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001446 }
1447
1448// ValueRefListE - Just like ValueRefList, except that it may also be empty!
1449ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; }
1450
1451InstVal : BinaryOps Types ValueRef ',' ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001452 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001453 if ($$ == 0)
1454 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001455 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001456 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001457 | UnaryOps ResolvedVal {
1458 $$ = UnaryOperator::create($1, $2);
Chris Lattner00950542001-06-06 20:29:01 +00001459 if ($$ == 0)
1460 ThrowException("unary operator returned null!");
Chris Lattner09083092001-07-08 04:57:15 +00001461 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001462 | ShiftOps ResolvedVal ',' ResolvedVal {
1463 if ($4->getType() != Type::UByteTy)
1464 ThrowException("Shift amount must be ubyte!");
1465 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001466 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001467 | CAST ResolvedVal TO Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001468 $$ = new CastInst($2, *$4);
1469 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001470 }
Chris Lattnerc24d2082001-06-11 15:04:20 +00001471 | PHI PHIList {
1472 const Type *Ty = $2->front().first->getType();
1473 $$ = new PHINode(Ty);
Chris Lattner00950542001-06-06 20:29:01 +00001474 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001475 if ($2->front().first->getType() != Ty)
1476 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001477 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001478 $2->pop_front();
1479 }
1480 delete $2; // Free the list...
1481 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001482 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattneref9c23f2001-10-03 14:53:21 +00001483 const PointerType *PMTy;
Chris Lattner8b81bf52001-07-25 22:47:46 +00001484 const MethodType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001485
Chris Lattneref9c23f2001-10-03 14:53:21 +00001486 if (!(PMTy = dyn_cast<PointerType>($2->get())) ||
Chris Lattner7a176752001-12-04 00:03:30 +00001487 !(Ty = dyn_cast<MethodType>(PMTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001488 // Pull out the types of all of the arguments...
1489 vector<const Type*> ParamTypes;
Chris Lattneref9c23f2001-10-03 14:53:21 +00001490 if ($5) {
Chris Lattner6cdb0112001-11-26 16:54:11 +00001491 for (vector<Value*>::iterator I = $5->begin(), E = $5->end(); I!=E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00001492 ParamTypes.push_back((*I)->getType());
1493 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001494
1495 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1496 if (isVarArg) ParamTypes.pop_back();
1497
1498 Ty = MethodType::get($2->get(), ParamTypes, isVarArg);
Chris Lattneref9c23f2001-10-03 14:53:21 +00001499 PMTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001500 }
Chris Lattner30c89792001-09-07 16:35:17 +00001501 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001502
Chris Lattneref9c23f2001-10-03 14:53:21 +00001503 Value *V = getVal(PMTy, $3); // Get the method we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001504
Chris Lattner8b81bf52001-07-25 22:47:46 +00001505 // Create the call node...
1506 if (!$5) { // Has no arguments?
Chris Lattner386a3b72001-10-16 19:54:17 +00001507 $$ = new CallInst(V, vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001508 } else { // Has arguments?
Chris Lattner00950542001-06-06 20:29:01 +00001509 // Loop through MethodType's arguments and ensure they are specified
1510 // correctly!
1511 //
1512 MethodType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001513 MethodType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
Chris Lattner6cdb0112001-11-26 16:54:11 +00001514 vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001515
1516 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1517 if ((*ArgI)->getType() != *I)
1518 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001519 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001520
Chris Lattner8b81bf52001-07-25 22:47:46 +00001521 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner00950542001-06-06 20:29:01 +00001522 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00001523
Chris Lattner6cdb0112001-11-26 16:54:11 +00001524 $$ = new CallInst(V, *$5);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001525 }
1526 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00001527 }
1528 | MemoryInst {
1529 $$ = $1;
1530 }
1531
Chris Lattner6cdb0112001-11-26 16:54:11 +00001532
1533// IndexList - List of indices for GEP based instructions...
1534IndexList : ',' ValueRefList {
Chris Lattner027dcc52001-07-08 21:10:27 +00001535 $$ = $2;
1536} | /* empty */ {
Chris Lattner6cdb0112001-11-26 16:54:11 +00001537 $$ = new vector<Value*>();
Chris Lattner027dcc52001-07-08 21:10:27 +00001538}
1539
Chris Lattner00950542001-06-06 20:29:01 +00001540MemoryInst : MALLOC Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001541 $$ = new MallocInst(PointerType::get(*$2));
1542 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001543 }
1544 | MALLOC Types ',' UINT ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001545 const Type *Ty = PointerType::get(*$2);
Chris Lattner8896eda2001-07-09 19:38:36 +00001546 $$ = new MallocInst(Ty, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001547 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001548 }
1549 | ALLOCA Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001550 $$ = new AllocaInst(PointerType::get(*$2));
1551 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001552 }
1553 | ALLOCA Types ',' UINT ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001554 const Type *Ty = PointerType::get(*$2);
Chris Lattner00950542001-06-06 20:29:01 +00001555 Value *ArrSize = getVal($4, $5);
Chris Lattnerf0d0e9c2001-07-07 08:36:30 +00001556 $$ = new AllocaInst(Ty, ArrSize);
Chris Lattner30c89792001-09-07 16:35:17 +00001557 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001558 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001559 | FREE ResolvedVal {
1560 if (!$2->getType()->isPointerType())
1561 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00001562 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001563 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001564 }
1565
Chris Lattner6cdb0112001-11-26 16:54:11 +00001566 | LOAD Types ValueRef IndexList {
Chris Lattner30c89792001-09-07 16:35:17 +00001567 if (!(*$2)->isPointerType())
Chris Lattner2079fde2001-10-13 06:41:08 +00001568 ThrowException("Can't load from nonpointer type: " +
1569 (*$2)->getDescription());
Chris Lattner30c89792001-09-07 16:35:17 +00001570 if (LoadInst::getIndexedType(*$2, *$4) == 0)
Chris Lattner027dcc52001-07-08 21:10:27 +00001571 ThrowException("Invalid indices for load instruction!");
1572
Chris Lattner30c89792001-09-07 16:35:17 +00001573 $$ = new LoadInst(getVal(*$2, $3), *$4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001574 delete $4; // Free the vector...
Chris Lattner30c89792001-09-07 16:35:17 +00001575 delete $2;
Chris Lattner027dcc52001-07-08 21:10:27 +00001576 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00001577 | STORE ResolvedVal ',' Types ValueRef IndexList {
Chris Lattner30c89792001-09-07 16:35:17 +00001578 if (!(*$4)->isPointerType())
Chris Lattner72e00252001-12-14 16:28:42 +00001579 ThrowException("Can't store to a nonpointer type: " +
1580 (*$4)->getDescription());
Chris Lattner30c89792001-09-07 16:35:17 +00001581 const Type *ElTy = StoreInst::getIndexedType(*$4, *$6);
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001582 if (ElTy == 0)
1583 ThrowException("Can't store into that field list!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001584 if (ElTy != $2->getType())
Chris Lattner72e00252001-12-14 16:28:42 +00001585 ThrowException("Can't store '" + $2->getType()->getDescription() +
1586 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001587 $$ = new StoreInst($2, getVal(*$4, $5), *$6);
1588 delete $4; delete $6;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001589 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00001590 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner30c89792001-09-07 16:35:17 +00001591 if (!(*$2)->isPointerType())
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001592 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner30c89792001-09-07 16:35:17 +00001593 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner72e00252001-12-14 16:28:42 +00001594 ThrowException("Can't get element ptr '" + (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001595 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
1596 delete $2; delete $4;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001597 }
Chris Lattner027dcc52001-07-08 21:10:27 +00001598
Chris Lattner00950542001-06-06 20:29:01 +00001599%%
Chris Lattner09083092001-07-08 04:57:15 +00001600int yyerror(const char *ErrorMsg) {
Chris Lattner00950542001-06-06 20:29:01 +00001601 ThrowException(string("Parse error: ") + ErrorMsg);
1602 return 0;
1603}