blob: 0f5c11e1eae514ad55b904a0ba05884722b78e36 [file] [log] [blame]
Chris Lattner00950542001-06-06 20:29:01 +00001//===-- llvmAsmParser.y - Parser for llvm assembly files ---------*- C++ -*--=//
2//
3// This file implements the bison parser for LLVM assembly languages files.
4//
5//===------------------------------------------------------------------------=//
6
7//
8// TODO: Parse comments and add them to an internal node... so that they may
9// be saved in the bytecode format as well as everything else. Very important
10// for a general IR format.
11//
12
13%{
14#include "ParserInternals.h"
Chris Lattner70cc3392001-09-10 07:58:01 +000015#include "llvm/Assembly/Parser.h"
Chris Lattner00950542001-06-06 20:29:01 +000016#include "llvm/SymbolTable.h"
17#include "llvm/Module.h"
Chris Lattner70cc3392001-09-10 07:58:01 +000018#include "llvm/GlobalVariable.h"
19#include "llvm/Method.h"
20#include "llvm/BasicBlock.h"
Chris Lattner00950542001-06-06 20:29:01 +000021#include "llvm/DerivedTypes.h"
Chris Lattner00950542001-06-06 20:29:01 +000022#include "llvm/iTerminators.h"
23#include "llvm/iMemory.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000024#include "Support/STLExtras.h"
25#include "Support/DepthFirstIterator.h"
Chris Lattner00950542001-06-06 20:29:01 +000026#include <list>
27#include <utility> // Get definition of pair class
Chris Lattner30c89792001-09-07 16:35:17 +000028#include <algorithm>
Chris Lattner00950542001-06-06 20:29:01 +000029#include <stdio.h> // This embarasment is due to our flex lexer...
30
Chris Lattner386a3b72001-10-16 19:54:17 +000031int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
Chris Lattner09083092001-07-08 04:57:15 +000032int yylex(); // declaration" of xxx warnings.
Chris Lattner00950542001-06-06 20:29:01 +000033int yyparse();
34
35static Module *ParserResult;
Chris Lattnera2850432001-07-22 18:36:00 +000036string CurFilename;
Chris Lattner00950542001-06-06 20:29:01 +000037
Chris Lattner30c89792001-09-07 16:35:17 +000038// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
39// relating to upreferences in the input stream.
40//
41//#define DEBUG_UPREFS 1
42#ifdef DEBUG_UPREFS
43#define UR_OUT(X) cerr << X
44#else
45#define UR_OUT(X)
46#endif
47
Chris Lattner00950542001-06-06 20:29:01 +000048// This contains info used when building the body of a method. It is destroyed
49// when the method is completed.
50//
51typedef vector<Value *> ValueList; // Numbered defs
Chris Lattner386a3b72001-10-16 19:54:17 +000052static void ResolveDefinitions(vector<ValueList> &LateResolvers,
53 vector<ValueList> *FutureLateResolvers = 0);
Chris Lattner30c89792001-09-07 16:35:17 +000054static void ResolveTypes (vector<PATypeHolder<Type> > &LateResolveTypes);
Chris Lattner00950542001-06-06 20:29:01 +000055
56static struct PerModuleInfo {
57 Module *CurrentModule;
Chris Lattner30c89792001-09-07 16:35:17 +000058 vector<ValueList> Values; // Module level numbered definitions
59 vector<ValueList> LateResolveValues;
Chris Lattner4a42e902001-10-22 05:56:09 +000060 vector<PATypeHolder<Type> > Types;
61 map<ValID, PATypeHolder<Type> > LateResolveTypes;
Chris Lattner00950542001-06-06 20:29:01 +000062
Chris Lattner2079fde2001-10-13 06:41:08 +000063 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
64 // references to global values. Global values may be referenced before they
65 // are defined, and if so, the temporary object that they represent is held
Chris Lattnerc18545d2001-10-15 13:21:42 +000066 // here. This is used for forward references of ConstPoolPointerRefs.
Chris Lattner2079fde2001-10-13 06:41:08 +000067 //
68 typedef map<pair<const PointerType *, ValID>, GlobalVariable*> GlobalRefsType;
69 GlobalRefsType GlobalRefs;
70
Chris Lattner00950542001-06-06 20:29:01 +000071 void ModuleDone() {
Chris Lattner30c89792001-09-07 16:35:17 +000072 // If we could not resolve some methods at method compilation time (calls to
73 // methods before they are defined), resolve them now... Types are resolved
74 // when the constant pool has been completely parsed.
75 //
Chris Lattner00950542001-06-06 20:29:01 +000076 ResolveDefinitions(LateResolveValues);
77
Chris Lattner2079fde2001-10-13 06:41:08 +000078 // Check to make sure that all global value forward references have been
79 // resolved!
80 //
81 if (!GlobalRefs.empty()) {
82 // TODO: Make this more detailed! Loop over each undef value and print
83 // info
Chris Lattner4a42e902001-10-22 05:56:09 +000084 ThrowException("TODO: Make better error - Unresolved forward constant "
85 "references exist!");
Chris Lattner2079fde2001-10-13 06:41:08 +000086 }
87
Chris Lattner00950542001-06-06 20:29:01 +000088 Values.clear(); // Clear out method local definitions
Chris Lattner30c89792001-09-07 16:35:17 +000089 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +000090 CurrentModule = 0;
91 }
Chris Lattner2079fde2001-10-13 06:41:08 +000092
93
94 // DeclareNewGlobalValue - Called every type a new GV has been defined. This
95 // is used to remove things from the forward declaration map, resolving them
96 // to the correct thing as needed.
97 //
98 void DeclareNewGlobalValue(GlobalValue *GV, ValID D) {
99 // Check to see if there is a forward reference to this global variable...
100 // if there is, eliminate it and patch the reference to use the new def'n.
101 GlobalRefsType::iterator I = GlobalRefs.find(make_pair(GV->getType(), D));
102
103 if (I != GlobalRefs.end()) {
104 GlobalVariable *OldGV = I->second; // Get the placeholder...
105 I->first.second.destroy(); // Free string memory if neccesary
106
107 // Loop over all of the uses of the GlobalValue. The only thing they are
Chris Lattnerc18545d2001-10-15 13:21:42 +0000108 // allowed to be at this point is ConstPoolPointerRef's.
Chris Lattner2079fde2001-10-13 06:41:08 +0000109 assert(OldGV->use_size() == 1 && "Only one reference should exist!");
110 while (!OldGV->use_empty()) {
Chris Lattnerc18545d2001-10-15 13:21:42 +0000111 User *U = OldGV->use_back(); // Must be a ConstPoolPointerRef...
112 ConstPoolPointerRef *CPPR = cast<ConstPoolPointerRef>(U);
Chris Lattner2079fde2001-10-13 06:41:08 +0000113 assert(CPPR->getValue() == OldGV && "Something isn't happy");
114
115 // Change the const pool reference to point to the real global variable
116 // now. This should drop a use from the OldGV.
117 CPPR->mutateReference(GV);
118 }
119
120 // Remove GV from the module...
121 CurrentModule->getGlobalList().remove(OldGV);
122 delete OldGV; // Delete the old placeholder
123
124 // Remove the map entry for the global now that it has been created...
125 GlobalRefs.erase(I);
126 }
127 }
128
Chris Lattner00950542001-06-06 20:29:01 +0000129} CurModule;
130
131static struct PerMethodInfo {
132 Method *CurrentMethod; // Pointer to current method being created
133
Chris Lattnere1815642001-07-15 06:35:53 +0000134 vector<ValueList> Values; // Keep track of numbered definitions
Chris Lattner00950542001-06-06 20:29:01 +0000135 vector<ValueList> LateResolveValues;
Chris Lattner4a42e902001-10-22 05:56:09 +0000136 vector<PATypeHolder<Type> > Types;
137 map<ValID, PATypeHolder<Type> > LateResolveTypes;
Chris Lattnere1815642001-07-15 06:35:53 +0000138 bool isDeclare; // Is this method a forward declararation?
Chris Lattner00950542001-06-06 20:29:01 +0000139
140 inline PerMethodInfo() {
141 CurrentMethod = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000142 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000143 }
144
145 inline ~PerMethodInfo() {}
146
147 inline void MethodStart(Method *M) {
148 CurrentMethod = M;
149 }
150
151 void MethodDone() {
152 // If we could not resolve some blocks at parsing time (forward branches)
153 // resolve the branches now...
Chris Lattner386a3b72001-10-16 19:54:17 +0000154 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000155
156 Values.clear(); // Clear out method local definitions
Chris Lattner30c89792001-09-07 16:35:17 +0000157 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +0000158 CurrentMethod = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000159 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000160 }
161} CurMeth; // Info for the current method...
162
Chris Lattnerb7474512001-10-03 15:39:04 +0000163static bool inMethodScope() { return CurMeth.CurrentMethod != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000164
Chris Lattner00950542001-06-06 20:29:01 +0000165
166//===----------------------------------------------------------------------===//
167// Code to handle definitions of all the types
168//===----------------------------------------------------------------------===//
169
Chris Lattner2079fde2001-10-13 06:41:08 +0000170static int InsertValue(Value *D, vector<ValueList> &ValueTab = CurMeth.Values) {
171 if (D->hasName()) return -1; // Is this a numbered definition?
172
173 // Yes, insert the value into the value table...
174 unsigned type = D->getType()->getUniqueID();
175 if (ValueTab.size() <= type)
176 ValueTab.resize(type+1, ValueList());
177 //printf("Values[%d][%d] = %d\n", type, ValueTab[type].size(), D);
178 ValueTab[type].push_back(D);
179 return ValueTab[type].size()-1;
Chris Lattner00950542001-06-06 20:29:01 +0000180}
181
Chris Lattner30c89792001-09-07 16:35:17 +0000182// TODO: FIXME when Type are not const
183static void InsertType(const Type *Ty, vector<PATypeHolder<Type> > &Types) {
184 Types.push_back(Ty);
185}
186
187static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000188 switch (D.Type) {
189 case 0: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000190 unsigned Num = (unsigned)D.Num;
191
192 // Module constants occupy the lowest numbered slots...
193 if (Num < CurModule.Types.size())
194 return CurModule.Types[Num];
195
196 Num -= CurModule.Types.size();
197
198 // Check that the number is within bounds...
199 if (Num <= CurMeth.Types.size())
200 return CurMeth.Types[Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000201 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000202 }
203 case 1: { // Is it a named definition?
204 string Name(D.Name);
205 SymbolTable *SymTab = 0;
Chris Lattnerb7474512001-10-03 15:39:04 +0000206 if (inMethodScope()) SymTab = CurMeth.CurrentMethod->getSymbolTable();
Chris Lattner30c89792001-09-07 16:35:17 +0000207 Value *N = SymTab ? SymTab->lookup(Type::TypeTy, Name) : 0;
208
209 if (N == 0) {
210 // Symbol table doesn't automatically chain yet... because the method
211 // hasn't been added to the module...
212 //
213 SymTab = CurModule.CurrentModule->getSymbolTable();
214 if (SymTab)
215 N = SymTab->lookup(Type::TypeTy, Name);
216 if (N == 0) break;
217 }
218
219 D.destroy(); // Free old strdup'd memory...
Chris Lattnercfe26c92001-10-01 18:26:53 +0000220 return cast<const Type>(N);
Chris Lattner30c89792001-09-07 16:35:17 +0000221 }
222 default:
223 ThrowException("Invalid symbol type reference!");
224 }
225
226 // If we reached here, we referenced either a symbol that we don't know about
227 // or an id number that hasn't been read yet. We may be referencing something
228 // forward, so just create an entry to be resolved later and get to it...
229 //
230 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
231
Chris Lattner4a42e902001-10-22 05:56:09 +0000232 map<ValID, PATypeHolder<Type> > &LateResolver = inMethodScope() ?
233 CurMeth.LateResolveTypes : CurModule.LateResolveTypes;
234
235 map<ValID, PATypeHolder<Type> >::iterator I = LateResolver.find(D);
236 if (I != LateResolver.end()) {
237 return I->second;
238 }
Chris Lattner30c89792001-09-07 16:35:17 +0000239
Chris Lattner82269592001-10-22 06:01:08 +0000240 Type *Typ = OpaqueType::get();
Chris Lattner4a42e902001-10-22 05:56:09 +0000241 LateResolver.insert(make_pair(D, Typ));
Chris Lattner30c89792001-09-07 16:35:17 +0000242 return Typ;
243}
244
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000245static Value *lookupInSymbolTable(const Type *Ty, const string &Name) {
246 SymbolTable *SymTab =
Chris Lattnerb7474512001-10-03 15:39:04 +0000247 inMethodScope() ? CurMeth.CurrentMethod->getSymbolTable() : 0;
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000248 Value *N = SymTab ? SymTab->lookup(Ty, Name) : 0;
249
250 if (N == 0) {
251 // Symbol table doesn't automatically chain yet... because the method
252 // hasn't been added to the module...
253 //
254 SymTab = CurModule.CurrentModule->getSymbolTable();
255 if (SymTab)
256 N = SymTab->lookup(Ty, Name);
257 }
258
259 return N;
260}
261
Chris Lattner2079fde2001-10-13 06:41:08 +0000262// getValNonImprovising - Look up the value specified by the provided type and
263// the provided ValID. If the value exists and has already been defined, return
264// it. Otherwise return null.
265//
266static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Chris Lattner386a3b72001-10-16 19:54:17 +0000267 if (isa<MethodType>(Ty))
268 ThrowException("Methods are not values and must be referenced as pointers");
269
Chris Lattner30c89792001-09-07 16:35:17 +0000270 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000271 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000272 unsigned type = Ty->getUniqueID();
Chris Lattner00950542001-06-06 20:29:01 +0000273 unsigned Num = (unsigned)D.Num;
274
275 // Module constants occupy the lowest numbered slots...
276 if (type < CurModule.Values.size()) {
277 if (Num < CurModule.Values[type].size())
278 return CurModule.Values[type][Num];
279
280 Num -= CurModule.Values[type].size();
281 }
282
283 // Make sure that our type is within bounds
Chris Lattner2079fde2001-10-13 06:41:08 +0000284 if (CurMeth.Values.size() <= type) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000285
286 // Check that the number is within bounds...
Chris Lattner2079fde2001-10-13 06:41:08 +0000287 if (CurMeth.Values[type].size() <= Num) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000288
289 return CurMeth.Values[type][Num];
290 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000291
Chris Lattner1a1cb112001-09-30 22:46:54 +0000292 case ValID::NameVal: { // Is it a named definition?
Chris Lattner2079fde2001-10-13 06:41:08 +0000293 Value *N = lookupInSymbolTable(Ty, string(D.Name));
294 if (N == 0) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000295
296 D.destroy(); // Free old strdup'd memory...
297 return N;
298 }
299
Chris Lattner2079fde2001-10-13 06:41:08 +0000300 // Check to make sure that "Ty" is an integral type, and that our
301 // value will fit into the specified type...
302 case ValID::ConstSIntVal: // Is it a constant pool reference??
303 if (Ty == Type::BoolTy) { // Special handling for boolean data
304 return ConstPoolBool::get(D.ConstPool64 != 0);
305 } else {
306 if (!ConstPoolSInt::isValueValidForType(Ty, D.ConstPool64))
307 ThrowException("Symbolic constant pool value '" +
308 itostr(D.ConstPool64) + "' is invalid for type '" +
309 Ty->getName() + "'!");
310 return ConstPoolSInt::get(Ty, D.ConstPool64);
Chris Lattner00950542001-06-06 20:29:01 +0000311 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000312
313 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
314 if (!ConstPoolUInt::isValueValidForType(Ty, D.UConstPool64)) {
315 if (!ConstPoolSInt::isValueValidForType(Ty, D.ConstPool64)) {
316 ThrowException("Integral constant pool reference is invalid!");
317 } else { // This is really a signed reference. Transmogrify.
318 return ConstPoolSInt::get(Ty, D.ConstPool64);
319 }
320 } else {
321 return ConstPoolUInt::get(Ty, D.UConstPool64);
322 }
323
324 case ValID::ConstStringVal: // Is it a string const pool reference?
325 cerr << "FIXME: TODO: String constants [sbyte] not implemented yet!\n";
326 abort();
327 return 0;
328
329 case ValID::ConstFPVal: // Is it a floating point const pool reference?
330 if (!ConstPoolFP::isValueValidForType(Ty, D.ConstPoolFP))
331 ThrowException("FP constant invalid for type!!");
332 return ConstPoolFP::get(Ty, D.ConstPoolFP);
333
334 case ValID::ConstNullVal: // Is it a null value?
335 if (!Ty->isPointerType())
336 ThrowException("Cannot create a a non pointer null!");
337 return ConstPoolPointerNull::get(cast<PointerType>(Ty));
338
Chris Lattner30c89792001-09-07 16:35:17 +0000339 default:
340 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000341 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000342 } // End of switch
343
Chris Lattner2079fde2001-10-13 06:41:08 +0000344 assert(0 && "Unhandled case!");
345 return 0;
346}
347
348
349// getVal - This function is identical to getValNonImprovising, except that if a
350// value is not already defined, it "improvises" by creating a placeholder var
351// that looks and acts just like the requested variable. When the value is
352// defined later, all uses of the placeholder variable are replaced with the
353// real thing.
354//
355static Value *getVal(const Type *Ty, const ValID &D) {
356 assert(Ty != Type::TypeTy && "Should use getTypeVal for types!");
357
358 // See if the value has already been defined...
359 Value *V = getValNonImprovising(Ty, D);
360 if (V) return V;
Chris Lattner00950542001-06-06 20:29:01 +0000361
362 // If we reached here, we referenced either a symbol that we don't know about
363 // or an id number that hasn't been read yet. We may be referencing something
364 // forward, so just create an entry to be resolved later and get to it...
365 //
Chris Lattner00950542001-06-06 20:29:01 +0000366 Value *d = 0;
Chris Lattner30c89792001-09-07 16:35:17 +0000367 switch (Ty->getPrimitiveID()) {
368 case Type::LabelTyID: d = new BBPlaceHolder(Ty, D); break;
Chris Lattner30c89792001-09-07 16:35:17 +0000369 default: d = new ValuePlaceHolder(Ty, D); break;
Chris Lattner00950542001-06-06 20:29:01 +0000370 }
371
372 assert(d != 0 && "How did we not make something?");
Chris Lattner386a3b72001-10-16 19:54:17 +0000373 if (inMethodScope())
374 InsertValue(d, CurMeth.LateResolveValues);
375 else
376 InsertValue(d, CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000377 return d;
378}
379
380
381//===----------------------------------------------------------------------===//
382// Code to handle forward references in instructions
383//===----------------------------------------------------------------------===//
384//
385// This code handles the late binding needed with statements that reference
386// values not defined yet... for example, a forward branch, or the PHI node for
387// a loop body.
388//
389// This keeps a table (CurMeth.LateResolveValues) of all such forward references
390// and back patchs after we are done.
391//
392
393// ResolveDefinitions - If we could not resolve some defs at parsing
394// time (forward branches, phi functions for loops, etc...) resolve the
395// defs now...
396//
Chris Lattner386a3b72001-10-16 19:54:17 +0000397static void ResolveDefinitions(vector<ValueList> &LateResolvers,
398 vector<ValueList> *FutureLateResolvers = 0) {
Chris Lattner00950542001-06-06 20:29:01 +0000399 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
400 for (unsigned ty = 0; ty < LateResolvers.size(); ty++) {
401 while (!LateResolvers[ty].empty()) {
402 Value *V = LateResolvers[ty].back();
Chris Lattner386a3b72001-10-16 19:54:17 +0000403 assert(!isa<Type>(V) && "Types should be in LateResolveTypes!");
404
Chris Lattner00950542001-06-06 20:29:01 +0000405 LateResolvers[ty].pop_back();
406 ValID &DID = getValIDFromPlaceHolder(V);
407
Chris Lattner2079fde2001-10-13 06:41:08 +0000408 Value *TheRealValue = getValNonImprovising(Type::getUniqueIDType(ty),DID);
Chris Lattner386a3b72001-10-16 19:54:17 +0000409 if (TheRealValue) {
410 V->replaceAllUsesWith(TheRealValue);
411 delete V;
412 } else if (FutureLateResolvers) {
413 // Methods have their unresolved items forwarded to the module late
414 // resolver table
415 InsertValue(V, *FutureLateResolvers);
416 } else {
Chris Lattner30c89792001-09-07 16:35:17 +0000417 if (DID.Type == 1)
418 ThrowException("Reference to an invalid definition: '" +DID.getName()+
419 "' of type '" + V->getType()->getDescription() + "'",
420 getLineNumFromPlaceHolder(V));
421 else
422 ThrowException("Reference to an invalid definition: #" +
423 itostr(DID.Num) + " of type '" +
424 V->getType()->getDescription() + "'",
425 getLineNumFromPlaceHolder(V));
426 }
Chris Lattner00950542001-06-06 20:29:01 +0000427 }
428 }
429
430 LateResolvers.clear();
431}
432
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000433// ResolveType - Take a specified unresolved type and resolve it. If there is
434// nothing to resolve it to yet, return true. Otherwise resolve it and return
435// false.
436//
437static bool ResolveType(PATypeHolder<Type> &T) {
438 const Type *Ty = T;
439 ValID &DID = getValIDFromPlaceHolder(Ty);
440
441 const Type *TheRealType = getTypeVal(DID, true);
Chris Lattner23192eb2001-10-21 21:43:25 +0000442 if (TheRealType == 0 || TheRealType == Ty) return true;
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000443
444 // Refine the opaque type we had to the new type we are getting.
445 cast<DerivedType>(Ty)->refineAbstractTypeTo(TheRealType);
446 return false;
447}
448
Chris Lattner4a42e902001-10-22 05:56:09 +0000449// ResolveTypeTo - A brand new type was just declared. This means that (if
450// name is not null) things referencing Name can be resolved. Otherwise, things
451// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000452//
Chris Lattner4a42e902001-10-22 05:56:09 +0000453static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattnerdda71962001-11-26 18:54:16 +0000454 vector<PATypeHolder<Type> > &Types = inMethodScope() ?
Chris Lattner4a42e902001-10-22 05:56:09 +0000455 CurMeth.Types : CurModule.Types;
Chris Lattner00950542001-06-06 20:29:01 +0000456
Chris Lattner4a42e902001-10-22 05:56:09 +0000457 ValID D;
458 if (Name) D = ValID::create(Name);
459 else D = ValID::create((int)Types.size());
Chris Lattner30c89792001-09-07 16:35:17 +0000460
Chris Lattner4a42e902001-10-22 05:56:09 +0000461 map<ValID, PATypeHolder<Type> > &LateResolver = inMethodScope() ?
462 CurMeth.LateResolveTypes : CurModule.LateResolveTypes;
463
464 map<ValID, PATypeHolder<Type> >::iterator I = LateResolver.find(D);
465 if (I != LateResolver.end()) {
466 cast<DerivedType>(I->second.get())->refineAbstractTypeTo(ToTy);
467 LateResolver.erase(I);
468 }
469}
470
471// ResolveTypes - At this point, all types should be resolved. Any that aren't
472// are errors.
473//
474static void ResolveTypes(map<ValID, PATypeHolder<Type> > &LateResolveTypes) {
475 if (!LateResolveTypes.empty()) {
Chris Lattner82269592001-10-22 06:01:08 +0000476 const ValID &DID = LateResolveTypes.begin()->first;
Chris Lattner4a42e902001-10-22 05:56:09 +0000477
478 if (DID.Type == ValID::NameVal)
Chris Lattner82269592001-10-22 06:01:08 +0000479 ThrowException("Reference to an invalid type: '" +DID.getName() + "'");
Chris Lattner4a42e902001-10-22 05:56:09 +0000480 else
Chris Lattner82269592001-10-22 06:01:08 +0000481 ThrowException("Reference to an invalid type: #" + itostr(DID.Num));
Chris Lattner30c89792001-09-07 16:35:17 +0000482 }
483}
484
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000485
Chris Lattner1781aca2001-09-18 04:00:54 +0000486// setValueName - Set the specified value to the name given. The name may be
487// null potentially, in which case this is a noop. The string passed in is
488// assumed to be a malloc'd string buffer, and is freed by this function.
489//
Chris Lattnerb7474512001-10-03 15:39:04 +0000490// This function returns true if the value has already been defined, but is
491// allowed to be redefined in the specified context. If the name is a new name
492// for the typeplane, false is returned.
493//
494static bool setValueName(Value *V, char *NameStr) {
495 if (NameStr == 0) return false;
Chris Lattner386a3b72001-10-16 19:54:17 +0000496
Chris Lattner1781aca2001-09-18 04:00:54 +0000497 string Name(NameStr); // Copy string
498 free(NameStr); // Free old string
499
Chris Lattner2079fde2001-10-13 06:41:08 +0000500 if (V->getType() == Type::VoidTy)
501 ThrowException("Can't assign name '" + Name +
502 "' to a null valued instruction!");
503
Chris Lattnerb7474512001-10-03 15:39:04 +0000504 SymbolTable *ST = inMethodScope() ?
Chris Lattner30c89792001-09-07 16:35:17 +0000505 CurMeth.CurrentMethod->getSymbolTableSure() :
506 CurModule.CurrentModule->getSymbolTableSure();
507
508 Value *Existing = ST->lookup(V->getType(), Name);
509 if (Existing) { // Inserting a name that is already defined???
510 // There is only one case where this is allowed: when we are refining an
511 // opaque type. In this case, Existing will be an opaque type.
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000512 if (const Type *Ty = dyn_cast<const Type>(Existing)) {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000513 if (OpaqueType *OpTy = dyn_cast<OpaqueType>(Ty)) {
Chris Lattner30c89792001-09-07 16:35:17 +0000514 // We ARE replacing an opaque type!
Chris Lattnerb00c5822001-10-02 03:41:24 +0000515 OpTy->refineAbstractTypeTo(cast<Type>(V));
Chris Lattnerb7474512001-10-03 15:39:04 +0000516 return true;
Chris Lattner30c89792001-09-07 16:35:17 +0000517 }
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000518 }
Chris Lattner30c89792001-09-07 16:35:17 +0000519
Chris Lattner9636a912001-10-01 16:18:37 +0000520 // Otherwise, we are a simple redefinition of a value, check to see if it
521 // is defined the same as the old one...
522 if (const Type *Ty = dyn_cast<const Type>(Existing)) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000523 if (Ty == cast<const Type>(V)) return true; // Yes, it's equal.
524 // cerr << "Type: " << Ty->getDescription() << " != "
525 // << cast<const Type>(V)->getDescription() << "!\n";
526 } else if (GlobalVariable *EGV = dyn_cast<GlobalVariable>(Existing)) {
Chris Lattner43efcbf2001-10-03 19:35:57 +0000527 // We are allowed to redefine a global variable in two circumstances:
528 // 1. If at least one of the globals is uninitialized or
529 // 2. If both initializers have the same value.
530 //
531 // This can only be done if the const'ness of the vars is the same.
532 //
Chris Lattner89219832001-10-03 19:35:04 +0000533 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
534 if (EGV->isConstant() == GV->isConstant() &&
535 (!EGV->hasInitializer() || !GV->hasInitializer() ||
536 EGV->getInitializer() == GV->getInitializer())) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000537
Chris Lattner89219832001-10-03 19:35:04 +0000538 // Make sure the existing global version gets the initializer!
539 if (GV->hasInitializer() && !EGV->hasInitializer())
540 EGV->setInitializer(GV->getInitializer());
541
Chris Lattner2079fde2001-10-13 06:41:08 +0000542 delete GV; // Destroy the duplicate!
Chris Lattner89219832001-10-03 19:35:04 +0000543 return true; // They are equivalent!
544 }
Chris Lattnerb7474512001-10-03 15:39:04 +0000545 }
Chris Lattner9636a912001-10-01 16:18:37 +0000546 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000547 ThrowException("Redefinition of value named '" + Name + "' in the '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000548 V->getType()->getDescription() + "' type plane!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000549 }
Chris Lattner00950542001-06-06 20:29:01 +0000550
Chris Lattner30c89792001-09-07 16:35:17 +0000551 V->setName(Name, ST);
Chris Lattnerb7474512001-10-03 15:39:04 +0000552 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000553}
554
Chris Lattner8896eda2001-07-09 19:38:36 +0000555
Chris Lattner30c89792001-09-07 16:35:17 +0000556//===----------------------------------------------------------------------===//
557// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000558//
Chris Lattner8896eda2001-07-09 19:38:36 +0000559
Chris Lattner30c89792001-09-07 16:35:17 +0000560// TypeContains - Returns true if Ty contains E in it.
561//
562static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner3ff43872001-09-28 22:56:31 +0000563 return find(df_begin(Ty), df_end(Ty), E) != df_end(Ty);
Chris Lattner30c89792001-09-07 16:35:17 +0000564}
Chris Lattner698b56e2001-07-20 19:15:08 +0000565
Chris Lattner30c89792001-09-07 16:35:17 +0000566
567static vector<pair<unsigned, OpaqueType *> > UpRefs;
568
569static PATypeHolder<Type> HandleUpRefs(const Type *ty) {
570 PATypeHolder<Type> Ty(ty);
Chris Lattner5084d032001-11-02 07:46:26 +0000571 UR_OUT("Type '" << ty->getDescription() <<
572 "' newly formed. Resolving upreferences.\n" <<
573 UpRefs.size() << " upreferences active!\n");
Chris Lattner30c89792001-09-07 16:35:17 +0000574 for (unsigned i = 0; i < UpRefs.size(); ) {
Chris Lattner5084d032001-11-02 07:46:26 +0000575 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Chris Lattner30c89792001-09-07 16:35:17 +0000576 << UpRefs[i].second->getDescription() << ") = "
Chris Lattner5084d032001-11-02 07:46:26 +0000577 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << endl);
Chris Lattner30c89792001-09-07 16:35:17 +0000578 if (TypeContains(Ty, UpRefs[i].second)) {
579 unsigned Level = --UpRefs[i].first; // Decrement level of upreference
Chris Lattner5084d032001-11-02 07:46:26 +0000580 UR_OUT(" Uplevel Ref Level = " << Level << endl);
Chris Lattner30c89792001-09-07 16:35:17 +0000581 if (Level == 0) { // Upreference should be resolved!
Chris Lattner5084d032001-11-02 07:46:26 +0000582 UR_OUT(" * Resolving upreference for "
583 << UpRefs[i].second->getDescription() << endl;
Chris Lattner30c89792001-09-07 16:35:17 +0000584 string OldName = UpRefs[i].second->getDescription());
585 UpRefs[i].second->refineAbstractTypeTo(Ty);
586 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
Chris Lattner5084d032001-11-02 07:46:26 +0000587 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
Chris Lattner30c89792001-09-07 16:35:17 +0000588 << (const void*)Ty << ", " << Ty->getDescription() << endl);
589 continue;
590 }
591 }
592
593 ++i; // Otherwise, no resolve, move on...
Chris Lattner8896eda2001-07-09 19:38:36 +0000594 }
Chris Lattner30c89792001-09-07 16:35:17 +0000595 // FIXME: TODO: this should return the updated type
Chris Lattner8896eda2001-07-09 19:38:36 +0000596 return Ty;
597}
598
Chris Lattner30c89792001-09-07 16:35:17 +0000599template <class TypeTy>
600inline static void TypeDone(PATypeHolder<TypeTy> *Ty) {
601 if (UpRefs.size())
602 ThrowException("Invalid upreference in type: " + (*Ty)->getDescription());
603}
604
605// newTH - Allocate a new type holder for the specified type
606template <class TypeTy>
607inline static PATypeHolder<TypeTy> *newTH(const TypeTy *Ty) {
608 return new PATypeHolder<TypeTy>(Ty);
609}
610template <class TypeTy>
611inline static PATypeHolder<TypeTy> *newTH(const PATypeHolder<TypeTy> &TH) {
612 return new PATypeHolder<TypeTy>(TH);
613}
614
615
Chris Lattner00950542001-06-06 20:29:01 +0000616//===----------------------------------------------------------------------===//
617// RunVMAsmParser - Define an interface to this parser
618//===----------------------------------------------------------------------===//
619//
Chris Lattnera2850432001-07-22 18:36:00 +0000620Module *RunVMAsmParser(const string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000621 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000622 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000623 llvmAsmlineno = 1; // Reset the current line number...
624
625 CurModule.CurrentModule = new Module(); // Allocate a new module to read
626 yyparse(); // Parse the file.
627 Module *Result = ParserResult;
Chris Lattner00950542001-06-06 20:29:01 +0000628 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
629 ParserResult = 0;
630
631 return Result;
632}
633
634%}
635
636%union {
Chris Lattner30c89792001-09-07 16:35:17 +0000637 Module *ModuleVal;
638 Method *MethodVal;
639 MethodArgument *MethArgVal;
640 BasicBlock *BasicBlockVal;
641 TerminatorInst *TermInstVal;
642 Instruction *InstVal;
643 ConstPoolVal *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000644
Chris Lattner30c89792001-09-07 16:35:17 +0000645 const Type *PrimType;
646 PATypeHolder<Type> *TypeVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000647 Value *ValueVal;
648
649 list<MethodArgument*> *MethodArgList;
Chris Lattner6cdb0112001-11-26 16:54:11 +0000650 vector<Value*> *ValueList;
Chris Lattner30c89792001-09-07 16:35:17 +0000651 list<PATypeHolder<Type> > *TypeList;
Chris Lattnerc24d2082001-06-11 15:04:20 +0000652 list<pair<Value*, BasicBlock*> > *PHIList; // Represent the RHS of PHI node
Chris Lattner00950542001-06-06 20:29:01 +0000653 list<pair<ConstPoolVal*, BasicBlock*> > *JumpTable;
Chris Lattner30c89792001-09-07 16:35:17 +0000654 vector<ConstPoolVal*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000655
Chris Lattner30c89792001-09-07 16:35:17 +0000656 int64_t SInt64Val;
657 uint64_t UInt64Val;
658 int SIntVal;
659 unsigned UIntVal;
660 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000661 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000662
Chris Lattner30c89792001-09-07 16:35:17 +0000663 char *StrVal; // This memory is strdup'd!
664 ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000665
Chris Lattner30c89792001-09-07 16:35:17 +0000666 Instruction::UnaryOps UnaryOpVal;
667 Instruction::BinaryOps BinaryOpVal;
668 Instruction::TermOps TermOpVal;
669 Instruction::MemoryOps MemOpVal;
670 Instruction::OtherOps OtherOpVal;
Chris Lattner00950542001-06-06 20:29:01 +0000671}
672
673%type <ModuleVal> Module MethodList
Chris Lattnere1815642001-07-15 06:35:53 +0000674%type <MethodVal> Method MethodProto MethodHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000675%type <BasicBlockVal> BasicBlock InstructionList
676%type <TermInstVal> BBTerminatorInst
677%type <InstVal> Inst InstVal MemoryInst
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000678%type <ConstVal> ConstVal
Chris Lattner6cdb0112001-11-26 16:54:11 +0000679%type <ConstVector> ConstVector
Chris Lattner00950542001-06-06 20:29:01 +0000680%type <MethodArgList> ArgList ArgListH
681%type <MethArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000682%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000683%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000684%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000685%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000686%type <JumpTable> JumpTable
Chris Lattnerdda71962001-11-26 18:54:16 +0000687%type <BoolVal> GlobalType OptInternal // GLOBAL or CONSTANT? Intern?
Chris Lattner00950542001-06-06 20:29:01 +0000688
Chris Lattner2079fde2001-10-13 06:41:08 +0000689// ValueRef - Unresolved reference to a definition or BB
690%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000691%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000692// Tokens and types for handling constant integer values
693//
694// ESINT64VAL - A negative number within long long range
695%token <SInt64Val> ESINT64VAL
696
697// EUINT64VAL - A positive number within uns. long long range
698%token <UInt64Val> EUINT64VAL
699%type <SInt64Val> EINT64VAL
700
701%token <SIntVal> SINTVAL // Signed 32 bit ints...
702%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
703%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000704%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000705
706// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000707%type <TypeVal> Types TypesV UpRTypes UpRTypesV
708%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
709%token <TypeVal> OPAQUE
710%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
711%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000712
713%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
714%type <StrVal> OptVAR_ID OptAssign
715
716
Chris Lattner1781aca2001-09-18 04:00:54 +0000717%token IMPLEMENTATION TRUE FALSE BEGINTOK END DECLARE GLOBAL CONSTANT UNINIT
Chris Lattnerdda71962001-11-26 18:54:16 +0000718%token TO EXCEPT DOTDOTDOT STRING NULL_TOK CONST INTERNAL
Chris Lattner00950542001-06-06 20:29:01 +0000719
720// Basic Block Terminating Operators
721%token <TermOpVal> RET BR SWITCH
722
723// Unary Operators
724%type <UnaryOpVal> UnaryOps // all the unary operators
Chris Lattner71496b32001-07-08 19:03:27 +0000725%token <UnaryOpVal> NOT
Chris Lattner00950542001-06-06 20:29:01 +0000726
727// Binary Operators
728%type <BinaryOpVal> BinaryOps // all the binary operators
Chris Lattner42c9e772001-10-20 09:32:59 +0000729%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000730%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000731
732// Memory Instructions
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000733%token <MemoryOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000734
Chris Lattner027dcc52001-07-08 21:10:27 +0000735// Other Operators
736%type <OtherOpVal> ShiftOps
Chris Lattner2079fde2001-10-13 06:41:08 +0000737%token <OtherOpVal> PHI CALL INVOKE CAST SHL SHR
Chris Lattner027dcc52001-07-08 21:10:27 +0000738
Chris Lattner00950542001-06-06 20:29:01 +0000739%start Module
740%%
741
742// Handle constant integer size restriction and conversion...
743//
744
745INTVAL : SINTVAL
746INTVAL : UINTVAL {
747 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
748 ThrowException("Value too large for type!");
749 $$ = (int32_t)$1;
750}
751
752
753EINT64VAL : ESINT64VAL // These have same type and can't cause problems...
754EINT64VAL : EUINT64VAL {
755 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
756 ThrowException("Value too large for type!");
757 $$ = (int64_t)$1;
758}
759
Chris Lattner00950542001-06-06 20:29:01 +0000760// Operations that are notably excluded from this list include:
761// RET, BR, & SWITCH because they end basic blocks and are treated specially.
762//
Chris Lattner09083092001-07-08 04:57:15 +0000763UnaryOps : NOT
Chris Lattner42c9e772001-10-20 09:32:59 +0000764BinaryOps : ADD | SUB | MUL | DIV | REM | AND | OR | XOR
Chris Lattner00950542001-06-06 20:29:01 +0000765BinaryOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE
Chris Lattner027dcc52001-07-08 21:10:27 +0000766ShiftOps : SHL | SHR
Chris Lattner00950542001-06-06 20:29:01 +0000767
Chris Lattnere98dda62001-07-14 06:10:16 +0000768// These are some types that allow classification if we only want a particular
769// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner00950542001-06-06 20:29:01 +0000770SIntType : LONG | INT | SHORT | SBYTE
771UIntType : ULONG | UINT | USHORT | UBYTE
Chris Lattner30c89792001-09-07 16:35:17 +0000772IntType : SIntType | UIntType
773FPType : FLOAT | DOUBLE
Chris Lattner00950542001-06-06 20:29:01 +0000774
Chris Lattnere98dda62001-07-14 06:10:16 +0000775// OptAssign - Value producing statements have an optional assignment component
Chris Lattner00950542001-06-06 20:29:01 +0000776OptAssign : VAR_ID '=' {
777 $$ = $1;
778 }
779 | /*empty*/ {
780 $$ = 0;
781 }
782
Chris Lattnerdda71962001-11-26 18:54:16 +0000783OptInternal : INTERNAL { $$ = true; } | /*empty*/ { $$ = false; }
Chris Lattner30c89792001-09-07 16:35:17 +0000784
785//===----------------------------------------------------------------------===//
786// Types includes all predefined types... except void, because it can only be
787// used in specific contexts (method returning void for example). To have
788// access to it, a user must explicitly use TypesV.
789//
790
791// TypesV includes all of 'Types', but it also includes the void type.
792TypesV : Types | VOID { $$ = newTH($1); }
793UpRTypesV : UpRTypes | VOID { $$ = newTH($1); }
794
795Types : UpRTypes {
796 TypeDone($$ = $1);
797 }
798
799
800// Derived types are added later...
801//
802PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT
803PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL
804UpRTypes : OPAQUE | PrimType { $$ = newTH($1); }
805UpRTypes : ValueRef { // Named types are also simple types...
806 $$ = newTH(getTypeVal($1));
807}
808
Chris Lattner30c89792001-09-07 16:35:17 +0000809// Include derived types in the Types production.
810//
811UpRTypes : '\\' EUINT64VAL { // Type UpReference
812 if ($2 > (uint64_t)INT64_MAX) ThrowException("Value out of range!");
813 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
814 UpRefs.push_back(make_pair((unsigned)$2, OT)); // Add to vector...
815 $$ = newTH<Type>(OT);
816 UR_OUT("New Upreference!\n");
817 }
818 | UpRTypesV '(' ArgTypeListI ')' { // Method derived type?
819 vector<const Type*> Params;
820 mapto($3->begin(), $3->end(), back_inserter(Params),
821 mem_fun_ref(&PATypeHandle<Type>::get));
Chris Lattner2079fde2001-10-13 06:41:08 +0000822 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
823 if (isVarArg) Params.pop_back();
824
825 $$ = newTH(HandleUpRefs(MethodType::get(*$1, Params, isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +0000826 delete $3; // Delete the argument list
827 delete $1; // Delete the old type handle
828 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000829 | '[' UpRTypesV ']' { // Unsized array type?
830 $$ = newTH<Type>(HandleUpRefs(ArrayType::get(*$2)));
831 delete $2;
Chris Lattner30c89792001-09-07 16:35:17 +0000832 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000833 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
834 $$ = newTH<Type>(HandleUpRefs(ArrayType::get(*$4, (int)$2)));
835 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +0000836 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000837 | '{' TypeListI '}' { // Structure type?
838 vector<const Type*> Elements;
839 mapto($2->begin(), $2->end(), back_inserter(Elements),
840 mem_fun_ref(&PATypeHandle<Type>::get));
Chris Lattner30c89792001-09-07 16:35:17 +0000841
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000842 $$ = newTH<Type>(HandleUpRefs(StructType::get(Elements)));
843 delete $2;
844 }
845 | '{' '}' { // Empty structure type?
846 $$ = newTH<Type>(StructType::get(vector<const Type*>()));
847 }
848 | UpRTypes '*' { // Pointer type?
849 $$ = newTH<Type>(HandleUpRefs(PointerType::get(*$1)));
850 delete $1;
851 }
Chris Lattner30c89792001-09-07 16:35:17 +0000852
853// TypeList - Used for struct declarations and as a basis for method type
854// declaration type lists
855//
856TypeListI : UpRTypes {
857 $$ = new list<PATypeHolder<Type> >();
858 $$->push_back(*$1); delete $1;
859 }
860 | TypeListI ',' UpRTypes {
861 ($$=$1)->push_back(*$3); delete $3;
862 }
863
864// ArgTypeList - List of types for a method type declaration...
865ArgTypeListI : TypeListI
866 | TypeListI ',' DOTDOTDOT {
867 ($$=$1)->push_back(Type::VoidTy);
868 }
869 | DOTDOTDOT {
870 ($$ = new list<PATypeHolder<Type> >())->push_back(Type::VoidTy);
871 }
872 | /*empty*/ {
873 $$ = new list<PATypeHolder<Type> >();
874 }
875
876
Chris Lattnere98dda62001-07-14 06:10:16 +0000877// ConstVal - The various declarations that go into the constant pool. This
878// includes all forward declarations of types, constants, and functions.
879//
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000880ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
881 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
882 if (ATy == 0)
883 ThrowException("Cannot make array constant with type: '" +
884 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +0000885 const Type *ETy = ATy->getElementType();
886 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +0000887
Chris Lattner30c89792001-09-07 16:35:17 +0000888 // Verify that we have the correct size...
889 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +0000890 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattner30c89792001-09-07 16:35:17 +0000891 utostr($3->size()) + " arguments, but has size of " +
892 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +0000893
Chris Lattner30c89792001-09-07 16:35:17 +0000894 // Verify all elements are correct type!
895 for (unsigned i = 0; i < $3->size(); i++) {
896 if (ETy != (*$3)[i]->getType())
Chris Lattner00950542001-06-06 20:29:01 +0000897 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000898 ETy->getName() + "' as required!\nIt is of type '" +
899 (*$3)[i]->getType()->getName() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +0000900 }
901
Chris Lattner30c89792001-09-07 16:35:17 +0000902 $$ = ConstPoolArray::get(ATy, *$3);
903 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000904 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000905 | Types '[' ']' {
906 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
907 if (ATy == 0)
908 ThrowException("Cannot make array constant with type: '" +
909 (*$1)->getDescription() + "'!");
910
911 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +0000912 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +0000913 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattner30c89792001-09-07 16:35:17 +0000914 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000915 $$ = ConstPoolArray::get(ATy, vector<ConstPoolVal*>());
Chris Lattner30c89792001-09-07 16:35:17 +0000916 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +0000917 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000918 | Types 'c' STRINGCONSTANT {
919 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
920 if (ATy == 0)
921 ThrowException("Cannot make array constant with type: '" +
922 (*$1)->getDescription() + "'!");
923
Chris Lattner30c89792001-09-07 16:35:17 +0000924 int NumElements = ATy->getNumElements();
925 const Type *ETy = ATy->getElementType();
926 char *EndStr = UnEscapeLexed($3, true);
927 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +0000928 ThrowException("Can't build string constant of size " +
Chris Lattner30c89792001-09-07 16:35:17 +0000929 itostr((int)(EndStr-$3)) +
930 " when array has size " + itostr(NumElements) + "!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000931 vector<ConstPoolVal*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +0000932 if (ETy == Type::SByteTy) {
933 for (char *C = $3; C != EndStr; ++C)
934 Vals.push_back(ConstPoolSInt::get(ETy, *C));
935 } else if (ETy == Type::UByteTy) {
936 for (char *C = $3; C != EndStr; ++C)
937 Vals.push_back(ConstPoolUInt::get(ETy, *C));
Chris Lattner93750fa2001-07-28 17:48:55 +0000938 } else {
Chris Lattner30c89792001-09-07 16:35:17 +0000939 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +0000940 ThrowException("Cannot build string arrays of non byte sized elements!");
941 }
Chris Lattner30c89792001-09-07 16:35:17 +0000942 free($3);
943 $$ = ConstPoolArray::get(ATy, Vals);
944 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +0000945 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000946 | Types '{' ConstVector '}' {
947 const StructType *STy = dyn_cast<const StructType>($1->get());
948 if (STy == 0)
949 ThrowException("Cannot make struct constant with type: '" +
950 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +0000951 // FIXME: TODO: Check to see that the constants are compatible with the type
952 // initializer!
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000953 $$ = ConstPoolStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +0000954 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000955 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000956 | Types NULL_TOK {
957 const PointerType *PTy = dyn_cast<const PointerType>($1->get());
958 if (PTy == 0)
959 ThrowException("Cannot make null pointer constant with type: '" +
960 (*$1)->getDescription() + "'!");
961
Chris Lattner2079fde2001-10-13 06:41:08 +0000962 $$ = ConstPoolPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000963 delete $1;
964 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000965 | Types SymbolicValueRef {
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000966 const PointerType *Ty = dyn_cast<const PointerType>($1->get());
967 if (Ty == 0)
968 ThrowException("Global const reference must be a pointer type!");
969
Chris Lattner2079fde2001-10-13 06:41:08 +0000970 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000971
Chris Lattner2079fde2001-10-13 06:41:08 +0000972 // If this is an initializer for a constant pointer, which is referencing a
973 // (currently) undefined variable, create a stub now that shall be replaced
974 // in the future with the right type of variable.
975 //
976 if (V == 0) {
977 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
978 const PointerType *PT = cast<PointerType>(Ty);
979
980 // First check to see if the forward references value is already created!
981 PerModuleInfo::GlobalRefsType::iterator I =
982 CurModule.GlobalRefs.find(make_pair(PT, $2));
983
984 if (I != CurModule.GlobalRefs.end()) {
985 V = I->second; // Placeholder already exists, use it...
986 } else {
987 // TODO: Include line number info by creating a subclass of
988 // TODO: GlobalVariable here that includes the said information!
989
990 // Create a placeholder for the global variable reference...
Chris Lattnerdda71962001-11-26 18:54:16 +0000991 GlobalVariable *GV = new GlobalVariable(PT->getValueType(), false,true);
Chris Lattner2079fde2001-10-13 06:41:08 +0000992 // Keep track of the fact that we have a forward ref to recycle it
993 CurModule.GlobalRefs.insert(make_pair(make_pair(PT, $2), GV));
994
995 // Must temporarily push this value into the module table...
996 CurModule.CurrentModule->getGlobalList().push_back(GV);
997 V = GV;
998 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000999 }
1000
Chris Lattner2079fde2001-10-13 06:41:08 +00001001 GlobalValue *GV = cast<GlobalValue>(V);
Chris Lattnerc18545d2001-10-15 13:21:42 +00001002 $$ = ConstPoolPointerRef::get(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001003 delete $1; // Free the type handle
Chris Lattner00950542001-06-06 20:29:01 +00001004 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001005
Chris Lattner00950542001-06-06 20:29:01 +00001006
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001007ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001008 if (!ConstPoolSInt::isValueValidForType($1, $2))
1009 ThrowException("Constant value doesn't fit in type!");
Chris Lattner30c89792001-09-07 16:35:17 +00001010 $$ = ConstPoolSInt::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001011 }
1012 | UIntType EUINT64VAL { // integral constants
1013 if (!ConstPoolUInt::isValueValidForType($1, $2))
1014 ThrowException("Constant value doesn't fit in type!");
Chris Lattner30c89792001-09-07 16:35:17 +00001015 $$ = ConstPoolUInt::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001016 }
1017 | BOOL TRUE { // Boolean constants
Chris Lattner30c89792001-09-07 16:35:17 +00001018 $$ = ConstPoolBool::True;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001019 }
1020 | BOOL FALSE { // Boolean constants
Chris Lattner30c89792001-09-07 16:35:17 +00001021 $$ = ConstPoolBool::False;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001022 }
1023 | FPType FPVAL { // Float & Double constants
Chris Lattner30c89792001-09-07 16:35:17 +00001024 $$ = ConstPoolFP::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001025 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001026
Chris Lattnere98dda62001-07-14 06:10:16 +00001027// ConstVector - A list of comma seperated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001028ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001029 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001030 }
1031 | ConstVal {
1032 $$ = new vector<ConstPoolVal*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001033 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001034 }
1035
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001036
Chris Lattner1781aca2001-09-18 04:00:54 +00001037// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
1038GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; }
1039
Chris Lattner00950542001-06-06 20:29:01 +00001040
Chris Lattnere98dda62001-07-14 06:10:16 +00001041// ConstPool - Constants with optional names assigned to them.
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001042ConstPool : ConstPool OptAssign CONST ConstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001043 if (setValueName($4, $2)) { assert(0 && "No redefinitions allowed!"); }
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001044 InsertValue($4);
Chris Lattner00950542001-06-06 20:29:01 +00001045 }
Chris Lattner30c89792001-09-07 16:35:17 +00001046 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner4a42e902001-10-22 05:56:09 +00001047 // Eagerly resolve types. This is not an optimization, this is a
1048 // requirement that is due to the fact that we could have this:
1049 //
1050 // %list = type { %list * }
1051 // %list = type { %list * } ; repeated type decl
1052 //
1053 // If types are not resolved eagerly, then the two types will not be
1054 // determined to be the same type!
1055 //
1056 ResolveTypeTo($2, $4->get());
1057
Chris Lattner1781aca2001-09-18 04:00:54 +00001058 // TODO: FIXME when Type are not const
Chris Lattnerb7474512001-10-03 15:39:04 +00001059 if (!setValueName(const_cast<Type*>($4->get()), $2)) {
1060 // If this is not a redefinition of a type...
1061 if (!$2) {
1062 InsertType($4->get(),
1063 inMethodScope() ? CurMeth.Types : CurModule.Types);
1064 }
Chris Lattner30c89792001-09-07 16:35:17 +00001065 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001066
1067 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001068 }
1069 | ConstPool MethodProto { // Method prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001070 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001071 | ConstPool OptAssign OptInternal GlobalType ConstVal {
1072 const Type *Ty = $5->getType();
Chris Lattner1781aca2001-09-18 04:00:54 +00001073 // Global declarations appear in Constant Pool
Chris Lattnerdda71962001-11-26 18:54:16 +00001074 ConstPoolVal *Initializer = $5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001075 if (Initializer == 0)
1076 ThrowException("Global value initializer is not a constant!");
1077
Chris Lattnerdda71962001-11-26 18:54:16 +00001078 GlobalVariable *GV = new GlobalVariable(Ty, $4, $3, Initializer);
Chris Lattnerb7474512001-10-03 15:39:04 +00001079 if (!setValueName(GV, $2)) { // If not redefining...
1080 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001081 int Slot = InsertValue(GV, CurModule.Values);
1082
1083 if (Slot != -1) {
1084 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1085 } else {
1086 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1087 (char*)GV->getName().c_str()));
1088 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001089 }
Chris Lattner1781aca2001-09-18 04:00:54 +00001090 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001091 | ConstPool OptAssign OptInternal UNINIT GlobalType Types {
1092 const Type *Ty = *$6;
Chris Lattner1781aca2001-09-18 04:00:54 +00001093 // Global declarations appear in Constant Pool
Chris Lattnerdda71962001-11-26 18:54:16 +00001094 GlobalVariable *GV = new GlobalVariable(Ty, $5, $3);
Chris Lattnerb7474512001-10-03 15:39:04 +00001095 if (!setValueName(GV, $2)) { // If not redefining...
1096 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001097 int Slot = InsertValue(GV, CurModule.Values);
1098
1099 if (Slot != -1) {
1100 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1101 } else {
1102 assert(GV->hasName() && "Not named and not numbered!?");
1103 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1104 (char*)GV->getName().c_str()));
1105 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001106 }
Chris Lattnere98dda62001-07-14 06:10:16 +00001107 }
Chris Lattner00950542001-06-06 20:29:01 +00001108 | /* empty: end of list */ {
1109 }
1110
1111
1112//===----------------------------------------------------------------------===//
1113// Rules to match Modules
1114//===----------------------------------------------------------------------===//
1115
1116// Module rule: Capture the result of parsing the whole file into a result
1117// variable...
1118//
1119Module : MethodList {
1120 $$ = ParserResult = $1;
1121 CurModule.ModuleDone();
1122}
1123
Chris Lattnere98dda62001-07-14 06:10:16 +00001124// MethodList - A list of methods, preceeded by a constant pool.
1125//
Chris Lattner00950542001-06-06 20:29:01 +00001126MethodList : MethodList Method {
Chris Lattner00950542001-06-06 20:29:01 +00001127 $$ = $1;
Chris Lattnere1815642001-07-15 06:35:53 +00001128 if (!$2->getParent())
1129 $1->getMethodList().push_back($2);
1130 CurMeth.MethodDone();
Chris Lattner00950542001-06-06 20:29:01 +00001131 }
Chris Lattnere1815642001-07-15 06:35:53 +00001132 | MethodList MethodProto {
1133 $$ = $1;
Chris Lattnere1815642001-07-15 06:35:53 +00001134 }
Chris Lattner00950542001-06-06 20:29:01 +00001135 | ConstPool IMPLEMENTATION {
1136 $$ = CurModule.CurrentModule;
Chris Lattner30c89792001-09-07 16:35:17 +00001137 // Resolve circular types before we parse the body of the module
1138 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner00950542001-06-06 20:29:01 +00001139 }
1140
1141
1142//===----------------------------------------------------------------------===//
1143// Rules to match Method Headers
1144//===----------------------------------------------------------------------===//
1145
1146OptVAR_ID : VAR_ID | /*empty*/ { $$ = 0; }
1147
1148ArgVal : Types OptVAR_ID {
Chris Lattner30c89792001-09-07 16:35:17 +00001149 $$ = new MethodArgument(*$1); delete $1;
Chris Lattnerb7474512001-10-03 15:39:04 +00001150 if (setValueName($$, $2)) { assert(0 && "No arg redef allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001151}
1152
1153ArgListH : ArgVal ',' ArgListH {
1154 $$ = $3;
1155 $3->push_front($1);
1156 }
1157 | ArgVal {
1158 $$ = new list<MethodArgument*>();
1159 $$->push_front($1);
1160 }
Chris Lattner8b81bf52001-07-25 22:47:46 +00001161 | DOTDOTDOT {
1162 $$ = new list<MethodArgument*>();
Chris Lattner2079fde2001-10-13 06:41:08 +00001163 $$->push_front(new MethodArgument(Type::VoidTy));
Chris Lattner8b81bf52001-07-25 22:47:46 +00001164 }
Chris Lattner00950542001-06-06 20:29:01 +00001165
1166ArgList : ArgListH {
1167 $$ = $1;
1168 }
1169 | /* empty */ {
1170 $$ = 0;
1171 }
1172
Chris Lattnerdda71962001-11-26 18:54:16 +00001173MethodHeaderH : OptInternal TypesV STRINGCONSTANT '(' ArgList ')' {
1174 UnEscapeLexed($3);
1175 string MethodName($3);
1176
Chris Lattner30c89792001-09-07 16:35:17 +00001177 vector<const Type*> ParamTypeList;
Chris Lattnerdda71962001-11-26 18:54:16 +00001178 if ($5)
1179 for (list<MethodArgument*>::iterator I = $5->begin(); I != $5->end(); ++I)
Chris Lattner00950542001-06-06 20:29:01 +00001180 ParamTypeList.push_back((*I)->getType());
1181
Chris Lattner2079fde2001-10-13 06:41:08 +00001182 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1183 if (isVarArg) ParamTypeList.pop_back();
1184
Chris Lattnerdda71962001-11-26 18:54:16 +00001185 const MethodType *MT = MethodType::get(*$2, ParamTypeList, isVarArg);
Chris Lattneref9c23f2001-10-03 14:53:21 +00001186 const PointerType *PMT = PointerType::get(MT);
Chris Lattnerdda71962001-11-26 18:54:16 +00001187 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001188
Chris Lattnere1815642001-07-15 06:35:53 +00001189 Method *M = 0;
1190 if (SymbolTable *ST = CurModule.CurrentModule->getSymbolTable()) {
Chris Lattnerdda71962001-11-26 18:54:16 +00001191 if (Value *V = ST->lookup(PMT, MethodName)) { // Method already in symtab?
Chris Lattneref9c23f2001-10-03 14:53:21 +00001192 M = cast<Method>(V);
Chris Lattner00950542001-06-06 20:29:01 +00001193
Chris Lattnere1815642001-07-15 06:35:53 +00001194 // Yes it is. If this is the case, either we need to be a forward decl,
1195 // or it needs to be.
1196 if (!CurMeth.isDeclare && !M->isExternal())
Chris Lattnerdda71962001-11-26 18:54:16 +00001197 ThrowException("Redefinition of method '" + MethodName + "'!");
Chris Lattnere1815642001-07-15 06:35:53 +00001198 }
1199 }
1200
1201 if (M == 0) { // Not already defined?
Chris Lattnerdda71962001-11-26 18:54:16 +00001202 M = new Method(MT, $1, MethodName);
Chris Lattnere1815642001-07-15 06:35:53 +00001203 InsertValue(M, CurModule.Values);
Chris Lattnerdda71962001-11-26 18:54:16 +00001204 CurModule.DeclareNewGlobalValue(M, ValID::create($3));
Chris Lattnere1815642001-07-15 06:35:53 +00001205 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001206 free($3); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +00001207
1208 CurMeth.MethodStart(M);
1209
1210 // Add all of the arguments we parsed to the method...
Chris Lattnerdda71962001-11-26 18:54:16 +00001211 if ($5 && !CurMeth.isDeclare) { // Is null if empty...
Chris Lattner00950542001-06-06 20:29:01 +00001212 Method::ArgumentListType &ArgList = M->getArgumentList();
1213
Chris Lattnerdda71962001-11-26 18:54:16 +00001214 for (list<MethodArgument*>::iterator I = $5->begin(); I != $5->end(); ++I) {
Chris Lattner00950542001-06-06 20:29:01 +00001215 InsertValue(*I);
1216 ArgList.push_back(*I);
1217 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001218 delete $5; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00001219 }
1220}
1221
1222MethodHeader : MethodHeaderH ConstPool BEGINTOK {
1223 $$ = CurMeth.CurrentMethod;
Chris Lattner30c89792001-09-07 16:35:17 +00001224
1225 // Resolve circular types before we parse the body of the method.
1226 ResolveTypes(CurMeth.LateResolveTypes);
Chris Lattner00950542001-06-06 20:29:01 +00001227}
1228
1229Method : BasicBlockList END {
1230 $$ = $1;
1231}
1232
Chris Lattnere1815642001-07-15 06:35:53 +00001233MethodProto : DECLARE { CurMeth.isDeclare = true; } MethodHeaderH {
1234 $$ = CurMeth.CurrentMethod;
Chris Lattner93750fa2001-07-28 17:48:55 +00001235 if (!$$->getParent())
1236 CurModule.CurrentModule->getMethodList().push_back($$);
1237 CurMeth.MethodDone();
Chris Lattnere1815642001-07-15 06:35:53 +00001238}
Chris Lattner00950542001-06-06 20:29:01 +00001239
1240//===----------------------------------------------------------------------===//
1241// Rules to match Basic Blocks
1242//===----------------------------------------------------------------------===//
1243
1244ConstValueRef : ESINT64VAL { // A reference to a direct constant
1245 $$ = ValID::create($1);
1246 }
1247 | EUINT64VAL {
1248 $$ = ValID::create($1);
1249 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001250 | FPVAL { // Perhaps it's an FP constant?
1251 $$ = ValID::create($1);
1252 }
Chris Lattner00950542001-06-06 20:29:01 +00001253 | TRUE {
1254 $$ = ValID::create((int64_t)1);
1255 }
1256 | FALSE {
1257 $$ = ValID::create((int64_t)0);
1258 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001259 | NULL_TOK {
1260 $$ = ValID::createNull();
1261 }
1262
Chris Lattner93750fa2001-07-28 17:48:55 +00001263/*
Chris Lattner00950542001-06-06 20:29:01 +00001264 | STRINGCONSTANT { // Quoted strings work too... especially for methods
1265 $$ = ValID::create_conststr($1);
1266 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001267*/
Chris Lattner00950542001-06-06 20:29:01 +00001268
Chris Lattner2079fde2001-10-13 06:41:08 +00001269// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1270// another value.
1271//
1272SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001273 $$ = ValID::create($1);
1274 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001275 | VAR_ID { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001276 $$ = ValID::create($1);
1277 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001278
1279// ValueRef - A reference to a definition... either constant or symbolic
1280ValueRef : SymbolicValueRef | ConstValueRef
1281
Chris Lattner00950542001-06-06 20:29:01 +00001282
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001283// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1284// type immediately preceeds the value reference, and allows complex constant
1285// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001286ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001287 $$ = getVal(*$1, $2); delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +00001288 }
Chris Lattner8b81bf52001-07-25 22:47:46 +00001289
Chris Lattner00950542001-06-06 20:29:01 +00001290
1291BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner89219832001-10-03 19:35:04 +00001292 ($$ = $1)->getBasicBlocks().push_back($2);
Chris Lattner00950542001-06-06 20:29:01 +00001293 }
1294 | MethodHeader BasicBlock { // Do not allow methods with 0 basic blocks
Chris Lattner89219832001-10-03 19:35:04 +00001295 ($$ = $1)->getBasicBlocks().push_back($2);
Chris Lattner00950542001-06-06 20:29:01 +00001296 }
1297
1298
1299// Basic blocks are terminated by branching instructions:
1300// br, br/cc, switch, ret
1301//
Chris Lattner2079fde2001-10-13 06:41:08 +00001302BasicBlock : InstructionList OptAssign BBTerminatorInst {
1303 if (setValueName($3, $2)) { assert(0 && "No redefn allowed!"); }
1304 InsertValue($3);
1305
1306 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001307 InsertValue($1);
1308 $$ = $1;
1309 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001310 | LABELSTR InstructionList OptAssign BBTerminatorInst {
1311 if (setValueName($4, $3)) { assert(0 && "No redefn allowed!"); }
1312 InsertValue($4);
1313
1314 $2->getInstList().push_back($4);
Chris Lattnerb7474512001-10-03 15:39:04 +00001315 if (setValueName($2, $1)) { assert(0 && "No label redef allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001316
1317 InsertValue($2);
1318 $$ = $2;
1319 }
1320
1321InstructionList : InstructionList Inst {
1322 $1->getInstList().push_back($2);
1323 $$ = $1;
1324 }
1325 | /* empty */ {
1326 $$ = new BasicBlock();
1327 }
1328
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001329BBTerminatorInst : RET ResolvedVal { // Return with a result...
1330 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001331 }
1332 | RET VOID { // Return with no result...
1333 $$ = new ReturnInst();
1334 }
1335 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner9636a912001-10-01 16:18:37 +00001336 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $3)));
Chris Lattner00950542001-06-06 20:29:01 +00001337 } // Conditional Branch...
1338 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner9636a912001-10-01 16:18:37 +00001339 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $6)),
1340 cast<BasicBlock>(getVal(Type::LabelTy, $9)),
Chris Lattner00950542001-06-06 20:29:01 +00001341 getVal(Type::BoolTy, $3));
1342 }
1343 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1344 SwitchInst *S = new SwitchInst(getVal($2, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001345 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
Chris Lattner00950542001-06-06 20:29:01 +00001346 $$ = S;
1347
1348 list<pair<ConstPoolVal*, BasicBlock*> >::iterator I = $8->begin(),
1349 end = $8->end();
Chris Lattner7fc9fe32001-06-27 23:41:11 +00001350 for (; I != end; ++I)
Chris Lattner00950542001-06-06 20:29:01 +00001351 S->dest_push_back(I->first, I->second);
1352 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001353 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO ResolvedVal
1354 EXCEPT ResolvedVal {
1355 const PointerType *PMTy;
1356 const MethodType *Ty;
1357
1358 if (!(PMTy = dyn_cast<PointerType>($2->get())) ||
1359 !(Ty = dyn_cast<MethodType>(PMTy->getValueType()))) {
1360 // Pull out the types of all of the arguments...
1361 vector<const Type*> ParamTypes;
1362 if ($5) {
Chris Lattner6cdb0112001-11-26 16:54:11 +00001363 for (vector<Value*>::iterator I = $5->begin(), E = $5->end(); I!=E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00001364 ParamTypes.push_back((*I)->getType());
1365 }
1366
1367 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1368 if (isVarArg) ParamTypes.pop_back();
1369
1370 Ty = MethodType::get($2->get(), ParamTypes, isVarArg);
1371 PMTy = PointerType::get(Ty);
1372 }
1373 delete $2;
1374
1375 Value *V = getVal(PMTy, $3); // Get the method we're calling...
1376
1377 BasicBlock *Normal = dyn_cast<BasicBlock>($8);
1378 BasicBlock *Except = dyn_cast<BasicBlock>($10);
1379
1380 if (Normal == 0 || Except == 0)
1381 ThrowException("Invoke instruction without label destinations!");
1382
1383 // Create the call node...
1384 if (!$5) { // Has no arguments?
Chris Lattner386a3b72001-10-16 19:54:17 +00001385 $$ = new InvokeInst(V, Normal, Except, vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001386 } else { // Has arguments?
1387 // Loop through MethodType's arguments and ensure they are specified
1388 // correctly!
1389 //
1390 MethodType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1391 MethodType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
Chris Lattner6cdb0112001-11-26 16:54:11 +00001392 vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00001393
1394 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1395 if ((*ArgI)->getType() != *I)
1396 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
1397 (*I)->getName() + "'!");
1398
1399 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1400 ThrowException("Invalid number of parameters detected!");
1401
Chris Lattner6cdb0112001-11-26 16:54:11 +00001402 $$ = new InvokeInst(V, Normal, Except, *$5);
Chris Lattner2079fde2001-10-13 06:41:08 +00001403 }
1404 delete $5;
1405 }
1406
1407
Chris Lattner00950542001-06-06 20:29:01 +00001408
1409JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1410 $$ = $1;
Chris Lattner2079fde2001-10-13 06:41:08 +00001411 ConstPoolVal *V = cast<ConstPoolVal>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001412 if (V == 0)
1413 ThrowException("May only switch on a constant pool value!");
1414
Chris Lattner9636a912001-10-01 16:18:37 +00001415 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($5, $6))));
Chris Lattner00950542001-06-06 20:29:01 +00001416 }
1417 | IntType ConstValueRef ',' LABEL ValueRef {
1418 $$ = new list<pair<ConstPoolVal*, BasicBlock*> >();
Chris Lattner2079fde2001-10-13 06:41:08 +00001419 ConstPoolVal *V = cast<ConstPoolVal>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00001420
1421 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($4, $5))));
Chris Lattner00950542001-06-06 20:29:01 +00001425 }
1426
1427Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001428 // Is this definition named?? if so, assign the name...
1429 if (setValueName($2, $1)) { assert(0 && "No redefin allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001430 InsertValue($2);
1431 $$ = $2;
1432}
1433
Chris Lattnerc24d2082001-06-11 15:04:20 +00001434PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
1435 $$ = new list<pair<Value*, BasicBlock*> >();
Chris Lattner30c89792001-09-07 16:35:17 +00001436 $$->push_back(make_pair(getVal(*$1, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001437 cast<BasicBlock>(getVal(Type::LabelTy, $5))));
Chris Lattner30c89792001-09-07 16:35:17 +00001438 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001439 }
1440 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1441 $$ = $1;
1442 $1->push_back(make_pair(getVal($1->front().first->getType(), $4),
Chris Lattner9636a912001-10-01 16:18:37 +00001443 cast<BasicBlock>(getVal(Type::LabelTy, $6))));
Chris Lattnerc24d2082001-06-11 15:04:20 +00001444 }
1445
1446
Chris Lattner30c89792001-09-07 16:35:17 +00001447ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner6cdb0112001-11-26 16:54:11 +00001448 $$ = new vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001449 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001450 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001451 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001452 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001453 $1->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001454 }
1455
1456// ValueRefListE - Just like ValueRefList, except that it may also be empty!
1457ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; }
1458
1459InstVal : BinaryOps Types ValueRef ',' ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001460 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001461 if ($$ == 0)
1462 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001463 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001464 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001465 | UnaryOps ResolvedVal {
1466 $$ = UnaryOperator::create($1, $2);
Chris Lattner00950542001-06-06 20:29:01 +00001467 if ($$ == 0)
1468 ThrowException("unary operator returned null!");
Chris Lattner09083092001-07-08 04:57:15 +00001469 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001470 | ShiftOps ResolvedVal ',' ResolvedVal {
1471 if ($4->getType() != Type::UByteTy)
1472 ThrowException("Shift amount must be ubyte!");
1473 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001474 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001475 | CAST ResolvedVal TO Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001476 $$ = new CastInst($2, *$4);
1477 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001478 }
Chris Lattnerc24d2082001-06-11 15:04:20 +00001479 | PHI PHIList {
1480 const Type *Ty = $2->front().first->getType();
1481 $$ = new PHINode(Ty);
Chris Lattner00950542001-06-06 20:29:01 +00001482 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001483 if ($2->front().first->getType() != Ty)
1484 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001485 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001486 $2->pop_front();
1487 }
1488 delete $2; // Free the list...
1489 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001490 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattneref9c23f2001-10-03 14:53:21 +00001491 const PointerType *PMTy;
Chris Lattner8b81bf52001-07-25 22:47:46 +00001492 const MethodType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001493
Chris Lattneref9c23f2001-10-03 14:53:21 +00001494 if (!(PMTy = dyn_cast<PointerType>($2->get())) ||
1495 !(Ty = dyn_cast<MethodType>(PMTy->getValueType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001496 // Pull out the types of all of the arguments...
1497 vector<const Type*> ParamTypes;
Chris Lattneref9c23f2001-10-03 14:53:21 +00001498 if ($5) {
Chris Lattner6cdb0112001-11-26 16:54:11 +00001499 for (vector<Value*>::iterator I = $5->begin(), E = $5->end(); I!=E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00001500 ParamTypes.push_back((*I)->getType());
1501 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001502
1503 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1504 if (isVarArg) ParamTypes.pop_back();
1505
1506 Ty = MethodType::get($2->get(), ParamTypes, isVarArg);
Chris Lattneref9c23f2001-10-03 14:53:21 +00001507 PMTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001508 }
Chris Lattner30c89792001-09-07 16:35:17 +00001509 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001510
Chris Lattneref9c23f2001-10-03 14:53:21 +00001511 Value *V = getVal(PMTy, $3); // Get the method we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001512
Chris Lattner8b81bf52001-07-25 22:47:46 +00001513 // Create the call node...
1514 if (!$5) { // Has no arguments?
Chris Lattner386a3b72001-10-16 19:54:17 +00001515 $$ = new CallInst(V, vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001516 } else { // Has arguments?
Chris Lattner00950542001-06-06 20:29:01 +00001517 // Loop through MethodType's arguments and ensure they are specified
1518 // correctly!
1519 //
1520 MethodType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001521 MethodType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
Chris Lattner6cdb0112001-11-26 16:54:11 +00001522 vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001523
1524 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1525 if ((*ArgI)->getType() != *I)
1526 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner00950542001-06-06 20:29:01 +00001527 (*I)->getName() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001528
Chris Lattner8b81bf52001-07-25 22:47:46 +00001529 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner00950542001-06-06 20:29:01 +00001530 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00001531
Chris Lattner6cdb0112001-11-26 16:54:11 +00001532 $$ = new CallInst(V, *$5);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001533 }
1534 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00001535 }
1536 | MemoryInst {
1537 $$ = $1;
1538 }
1539
Chris Lattner6cdb0112001-11-26 16:54:11 +00001540
1541// IndexList - List of indices for GEP based instructions...
1542IndexList : ',' ValueRefList {
Chris Lattner027dcc52001-07-08 21:10:27 +00001543 $$ = $2;
1544} | /* empty */ {
Chris Lattner6cdb0112001-11-26 16:54:11 +00001545 $$ = new vector<Value*>();
Chris Lattner027dcc52001-07-08 21:10:27 +00001546}
1547
Chris Lattner00950542001-06-06 20:29:01 +00001548MemoryInst : MALLOC Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001549 $$ = new MallocInst(PointerType::get(*$2));
1550 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001551 }
1552 | MALLOC Types ',' UINT ValueRef {
Chris Lattnerb00c5822001-10-02 03:41:24 +00001553 if (!(*$2)->isArrayType() || cast<const ArrayType>($2->get())->isSized())
Chris Lattner30c89792001-09-07 16:35:17 +00001554 ThrowException("Trying to allocate " + (*$2)->getName() +
Chris Lattner00950542001-06-06 20:29:01 +00001555 " as unsized array!");
Chris Lattner30c89792001-09-07 16:35:17 +00001556 const Type *Ty = PointerType::get(*$2);
Chris Lattner8896eda2001-07-09 19:38:36 +00001557 $$ = new MallocInst(Ty, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001558 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001559 }
1560 | ALLOCA Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001561 $$ = new AllocaInst(PointerType::get(*$2));
1562 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001563 }
1564 | ALLOCA Types ',' UINT ValueRef {
Chris Lattnerb00c5822001-10-02 03:41:24 +00001565 if (!(*$2)->isArrayType() || cast<const ArrayType>($2->get())->isSized())
Chris Lattner30c89792001-09-07 16:35:17 +00001566 ThrowException("Trying to allocate " + (*$2)->getName() +
Chris Lattner00950542001-06-06 20:29:01 +00001567 " as unsized array!");
Chris Lattner30c89792001-09-07 16:35:17 +00001568 const Type *Ty = PointerType::get(*$2);
Chris Lattner00950542001-06-06 20:29:01 +00001569 Value *ArrSize = getVal($4, $5);
Chris Lattnerf0d0e9c2001-07-07 08:36:30 +00001570 $$ = new AllocaInst(Ty, ArrSize);
Chris Lattner30c89792001-09-07 16:35:17 +00001571 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001572 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001573 | FREE ResolvedVal {
1574 if (!$2->getType()->isPointerType())
1575 ThrowException("Trying to free nonpointer type " +
1576 $2->getType()->getName() + "!");
1577 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001578 }
1579
Chris Lattner6cdb0112001-11-26 16:54:11 +00001580 | LOAD Types ValueRef IndexList {
Chris Lattner30c89792001-09-07 16:35:17 +00001581 if (!(*$2)->isPointerType())
Chris Lattner2079fde2001-10-13 06:41:08 +00001582 ThrowException("Can't load from nonpointer type: " +
1583 (*$2)->getDescription());
Chris Lattner30c89792001-09-07 16:35:17 +00001584 if (LoadInst::getIndexedType(*$2, *$4) == 0)
Chris Lattner027dcc52001-07-08 21:10:27 +00001585 ThrowException("Invalid indices for load instruction!");
1586
Chris Lattner30c89792001-09-07 16:35:17 +00001587 $$ = new LoadInst(getVal(*$2, $3), *$4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001588 delete $4; // Free the vector...
Chris Lattner30c89792001-09-07 16:35:17 +00001589 delete $2;
Chris Lattner027dcc52001-07-08 21:10:27 +00001590 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00001591 | STORE ResolvedVal ',' Types ValueRef IndexList {
Chris Lattner30c89792001-09-07 16:35:17 +00001592 if (!(*$4)->isPointerType())
1593 ThrowException("Can't store to a nonpointer type: " + (*$4)->getName());
1594 const Type *ElTy = StoreInst::getIndexedType(*$4, *$6);
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001595 if (ElTy == 0)
1596 ThrowException("Can't store into that field list!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001597 if (ElTy != $2->getType())
1598 ThrowException("Can't store '" + $2->getType()->getName() +
1599 "' into space of type '" + ElTy->getName() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001600 $$ = new StoreInst($2, getVal(*$4, $5), *$6);
1601 delete $4; delete $6;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001602 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00001603 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner30c89792001-09-07 16:35:17 +00001604 if (!(*$2)->isPointerType())
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001605 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner30c89792001-09-07 16:35:17 +00001606 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
1607 ThrowException("Can't get element ptr '" + (*$2)->getName() + "'!");
1608 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
1609 delete $2; delete $4;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001610 }
Chris Lattner027dcc52001-07-08 21:10:27 +00001611
Chris Lattner00950542001-06-06 20:29:01 +00001612%%
Chris Lattner09083092001-07-08 04:57:15 +00001613int yyerror(const char *ErrorMsg) {
Chris Lattner00950542001-06-06 20:29:01 +00001614 ThrowException(string("Parse error: ") + ErrorMsg);
1615 return 0;
1616}