blob: 28de8e1ef09bd6345050fb1735eeedadb5299c53 [file] [log] [blame]
Chris Lattner00950542001-06-06 20:29:01 +00001//===-- llvmAsmParser.y - Parser for llvm assembly files ---------*- C++ -*--=//
2//
3// This file implements the bison parser for LLVM assembly languages files.
4//
5//===------------------------------------------------------------------------=//
6
7//
8// TODO: Parse comments and add them to an internal node... so that they may
9// be saved in the bytecode format as well as everything else. Very important
10// for a general IR format.
11//
12
13%{
14#include "ParserInternals.h"
Chris Lattner70cc3392001-09-10 07:58:01 +000015#include "llvm/Assembly/Parser.h"
Chris Lattner00950542001-06-06 20:29:01 +000016#include "llvm/SymbolTable.h"
17#include "llvm/Module.h"
Chris Lattner70cc3392001-09-10 07:58:01 +000018#include "llvm/GlobalVariable.h"
19#include "llvm/Method.h"
20#include "llvm/BasicBlock.h"
Chris Lattner00950542001-06-06 20:29:01 +000021#include "llvm/DerivedTypes.h"
Chris Lattner00950542001-06-06 20:29:01 +000022#include "llvm/iTerminators.h"
23#include "llvm/iMemory.h"
Chris Lattner30c89792001-09-07 16:35:17 +000024#include "llvm/CFG.h" // TODO: Change this when we have a DF.h
25#include "llvm/Support/STLExtras.h"
Chris Lattner00950542001-06-06 20:29:01 +000026#include <list>
27#include <utility> // Get definition of pair class
Chris Lattner30c89792001-09-07 16:35:17 +000028#include <algorithm>
Chris Lattner00950542001-06-06 20:29:01 +000029#include <stdio.h> // This embarasment is due to our flex lexer...
30
Chris Lattner09083092001-07-08 04:57:15 +000031int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
32int yylex(); // declaration" of xxx warnings.
Chris Lattner00950542001-06-06 20:29:01 +000033int yyparse();
34
35static Module *ParserResult;
Chris Lattnera2850432001-07-22 18:36:00 +000036string CurFilename;
Chris Lattner00950542001-06-06 20:29:01 +000037
Chris Lattner30c89792001-09-07 16:35:17 +000038// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
39// relating to upreferences in the input stream.
40//
41//#define DEBUG_UPREFS 1
42#ifdef DEBUG_UPREFS
43#define UR_OUT(X) cerr << X
44#else
45#define UR_OUT(X)
46#endif
47
Chris Lattner00950542001-06-06 20:29:01 +000048// This contains info used when building the body of a method. It is destroyed
49// when the method is completed.
50//
51typedef vector<Value *> ValueList; // Numbered defs
52static void ResolveDefinitions(vector<ValueList> &LateResolvers);
Chris Lattner30c89792001-09-07 16:35:17 +000053static void ResolveTypes (vector<PATypeHolder<Type> > &LateResolveTypes);
Chris Lattner00950542001-06-06 20:29:01 +000054
55static struct PerModuleInfo {
56 Module *CurrentModule;
Chris Lattner30c89792001-09-07 16:35:17 +000057 vector<ValueList> Values; // Module level numbered definitions
58 vector<ValueList> LateResolveValues;
59 vector<PATypeHolder<Type> > Types, LateResolveTypes;
Chris Lattner00950542001-06-06 20:29:01 +000060
61 void ModuleDone() {
Chris Lattner30c89792001-09-07 16:35:17 +000062 // If we could not resolve some methods at method compilation time (calls to
63 // methods before they are defined), resolve them now... Types are resolved
64 // when the constant pool has been completely parsed.
65 //
Chris Lattner00950542001-06-06 20:29:01 +000066 ResolveDefinitions(LateResolveValues);
67
68 Values.clear(); // Clear out method local definitions
Chris Lattner30c89792001-09-07 16:35:17 +000069 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +000070 CurrentModule = 0;
71 }
72} CurModule;
73
74static struct PerMethodInfo {
75 Method *CurrentMethod; // Pointer to current method being created
76
Chris Lattnere1815642001-07-15 06:35:53 +000077 vector<ValueList> Values; // Keep track of numbered definitions
Chris Lattner00950542001-06-06 20:29:01 +000078 vector<ValueList> LateResolveValues;
Chris Lattner30c89792001-09-07 16:35:17 +000079 vector<PATypeHolder<Type> > Types, LateResolveTypes;
Chris Lattnere1815642001-07-15 06:35:53 +000080 bool isDeclare; // Is this method a forward declararation?
Chris Lattner00950542001-06-06 20:29:01 +000081
82 inline PerMethodInfo() {
83 CurrentMethod = 0;
Chris Lattnere1815642001-07-15 06:35:53 +000084 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +000085 }
86
87 inline ~PerMethodInfo() {}
88
89 inline void MethodStart(Method *M) {
90 CurrentMethod = M;
91 }
92
93 void MethodDone() {
94 // If we could not resolve some blocks at parsing time (forward branches)
95 // resolve the branches now...
96 ResolveDefinitions(LateResolveValues);
97
98 Values.clear(); // Clear out method local definitions
Chris Lattner30c89792001-09-07 16:35:17 +000099 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +0000100 CurrentMethod = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000101 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000102 }
103} CurMeth; // Info for the current method...
104
105
106//===----------------------------------------------------------------------===//
107// Code to handle definitions of all the types
108//===----------------------------------------------------------------------===//
109
Chris Lattner93750fa2001-07-28 17:48:55 +0000110static void InsertValue(Value *D, vector<ValueList> &ValueTab = CurMeth.Values){
Chris Lattner00950542001-06-06 20:29:01 +0000111 if (!D->hasName()) { // Is this a numbered definition?
112 unsigned type = D->getType()->getUniqueID();
113 if (ValueTab.size() <= type)
114 ValueTab.resize(type+1, ValueList());
115 //printf("Values[%d][%d] = %d\n", type, ValueTab[type].size(), D);
116 ValueTab[type].push_back(D);
117 }
118}
119
Chris Lattner30c89792001-09-07 16:35:17 +0000120// TODO: FIXME when Type are not const
121static void InsertType(const Type *Ty, vector<PATypeHolder<Type> > &Types) {
122 Types.push_back(Ty);
123}
124
125static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000126 switch (D.Type) {
127 case 0: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000128 unsigned Num = (unsigned)D.Num;
129
130 // Module constants occupy the lowest numbered slots...
131 if (Num < CurModule.Types.size())
132 return CurModule.Types[Num];
133
134 Num -= CurModule.Types.size();
135
136 // Check that the number is within bounds...
137 if (Num <= CurMeth.Types.size())
138 return CurMeth.Types[Num];
139 }
140 case 1: { // Is it a named definition?
141 string Name(D.Name);
142 SymbolTable *SymTab = 0;
143 if (CurMeth.CurrentMethod)
144 SymTab = CurMeth.CurrentMethod->getSymbolTable();
145 Value *N = SymTab ? SymTab->lookup(Type::TypeTy, Name) : 0;
146
147 if (N == 0) {
148 // Symbol table doesn't automatically chain yet... because the method
149 // hasn't been added to the module...
150 //
151 SymTab = CurModule.CurrentModule->getSymbolTable();
152 if (SymTab)
153 N = SymTab->lookup(Type::TypeTy, Name);
154 if (N == 0) break;
155 }
156
157 D.destroy(); // Free old strdup'd memory...
158 return N->castTypeAsserting();
159 }
160 default:
161 ThrowException("Invalid symbol type reference!");
162 }
163
164 // If we reached here, we referenced either a symbol that we don't know about
165 // or an id number that hasn't been read yet. We may be referencing something
166 // forward, so just create an entry to be resolved later and get to it...
167 //
168 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
169
170 vector<PATypeHolder<Type> > *LateResolver = CurMeth.CurrentMethod ?
171 &CurMeth.LateResolveTypes : &CurModule.LateResolveTypes;
172
173 Type *Typ = new TypePlaceHolder(Type::TypeTy, D);
174 InsertType(Typ, *LateResolver);
175 return Typ;
176}
177
178static Value *getVal(const Type *Ty, const ValID &D,
179 bool DoNotImprovise = false) {
180 assert(Ty != Type::TypeTy && "Should use getTypeVal for types!");
181
182 switch (D.Type) {
183 case 0: { // Is it a numbered definition?
184 unsigned type = Ty->getUniqueID();
Chris Lattner00950542001-06-06 20:29:01 +0000185 unsigned Num = (unsigned)D.Num;
186
187 // Module constants occupy the lowest numbered slots...
188 if (type < CurModule.Values.size()) {
189 if (Num < CurModule.Values[type].size())
190 return CurModule.Values[type][Num];
191
192 Num -= CurModule.Values[type].size();
193 }
194
195 // Make sure that our type is within bounds
196 if (CurMeth.Values.size() <= type)
197 break;
198
199 // Check that the number is within bounds...
200 if (CurMeth.Values[type].size() <= Num)
201 break;
202
203 return CurMeth.Values[type][Num];
204 }
205 case 1: { // Is it a named definition?
206 string Name(D.Name);
207 SymbolTable *SymTab = 0;
208 if (CurMeth.CurrentMethod)
209 SymTab = CurMeth.CurrentMethod->getSymbolTable();
Chris Lattner30c89792001-09-07 16:35:17 +0000210 Value *N = SymTab ? SymTab->lookup(Ty, Name) : 0;
Chris Lattner00950542001-06-06 20:29:01 +0000211
212 if (N == 0) {
Chris Lattner30c89792001-09-07 16:35:17 +0000213 // Symbol table doesn't automatically chain yet... because the method
214 // hasn't been added to the module...
215 //
Chris Lattner00950542001-06-06 20:29:01 +0000216 SymTab = CurModule.CurrentModule->getSymbolTable();
217 if (SymTab)
Chris Lattner30c89792001-09-07 16:35:17 +0000218 N = SymTab->lookup(Ty, Name);
Chris Lattner00950542001-06-06 20:29:01 +0000219 if (N == 0) break;
220 }
221
222 D.destroy(); // Free old strdup'd memory...
223 return N;
224 }
225
226 case 2: // Is it a constant pool reference??
227 case 3: // Is it an unsigned const pool reference?
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000228 case 4: // Is it a string const pool reference?
229 case 5:{ // Is it a floating point const pool reference?
Chris Lattner00950542001-06-06 20:29:01 +0000230 ConstPoolVal *CPV = 0;
231
Chris Lattner30c89792001-09-07 16:35:17 +0000232 // Check to make sure that "Ty" is an integral type, and that our
Chris Lattner00950542001-06-06 20:29:01 +0000233 // value will fit into the specified type...
234 switch (D.Type) {
235 case 2:
Chris Lattner30c89792001-09-07 16:35:17 +0000236 if (Ty == Type::BoolTy) { // Special handling for boolean data
237 CPV = ConstPoolBool::get(D.ConstPool64 != 0);
Chris Lattner00950542001-06-06 20:29:01 +0000238 } else {
Chris Lattner30c89792001-09-07 16:35:17 +0000239 if (!ConstPoolSInt::isValueValidForType(Ty, D.ConstPool64))
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000240 ThrowException("Symbolic constant pool value '" +
241 itostr(D.ConstPool64) + "' is invalid for type '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000242 Ty->getName() + "'!");
243 CPV = ConstPoolSInt::get(Ty, D.ConstPool64);
Chris Lattner00950542001-06-06 20:29:01 +0000244 }
245 break;
246 case 3:
Chris Lattner30c89792001-09-07 16:35:17 +0000247 if (!ConstPoolUInt::isValueValidForType(Ty, D.UConstPool64)) {
248 if (!ConstPoolSInt::isValueValidForType(Ty, D.ConstPool64)) {
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000249 ThrowException("Integral constant pool reference is invalid!");
Chris Lattner00950542001-06-06 20:29:01 +0000250 } else { // This is really a signed reference. Transmogrify.
Chris Lattner30c89792001-09-07 16:35:17 +0000251 CPV = ConstPoolSInt::get(Ty, D.ConstPool64);
Chris Lattner00950542001-06-06 20:29:01 +0000252 }
253 } else {
Chris Lattner30c89792001-09-07 16:35:17 +0000254 CPV = ConstPoolUInt::get(Ty, D.UConstPool64);
Chris Lattner00950542001-06-06 20:29:01 +0000255 }
256 break;
257 case 4:
258 cerr << "FIXME: TODO: String constants [sbyte] not implemented yet!\n";
259 abort();
Chris Lattner00950542001-06-06 20:29:01 +0000260 break;
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000261 case 5:
Chris Lattner30c89792001-09-07 16:35:17 +0000262 if (!ConstPoolFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000263 ThrowException("FP constant invalid for type!!");
264 else
Chris Lattner30c89792001-09-07 16:35:17 +0000265 CPV = ConstPoolFP::get(Ty, D.ConstPoolFP);
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000266 break;
Chris Lattner00950542001-06-06 20:29:01 +0000267 }
268 assert(CPV && "How did we escape creating a constant??");
Chris Lattner00950542001-06-06 20:29:01 +0000269 return CPV;
270 } // End of case 2,3,4
Chris Lattner30c89792001-09-07 16:35:17 +0000271 default:
272 assert(0 && "Unhandled case!");
Chris Lattner00950542001-06-06 20:29:01 +0000273 } // End of switch
274
275
276 // If we reached here, we referenced either a symbol that we don't know about
277 // or an id number that hasn't been read yet. We may be referencing something
278 // forward, so just create an entry to be resolved later and get to it...
279 //
280 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
281
Chris Lattner00950542001-06-06 20:29:01 +0000282 Value *d = 0;
Chris Lattner30c89792001-09-07 16:35:17 +0000283 vector<ValueList> *LateResolver = (CurMeth.CurrentMethod) ?
284 &CurMeth.LateResolveValues : &CurModule.LateResolveValues;
Chris Lattner93750fa2001-07-28 17:48:55 +0000285
Chris Lattner30c89792001-09-07 16:35:17 +0000286 switch (Ty->getPrimitiveID()) {
287 case Type::LabelTyID: d = new BBPlaceHolder(Ty, D); break;
288 case Type::MethodTyID: d = new MethPlaceHolder(Ty, D);
Chris Lattner93750fa2001-07-28 17:48:55 +0000289 LateResolver = &CurModule.LateResolveValues; break;
Chris Lattner30c89792001-09-07 16:35:17 +0000290 default: d = new ValuePlaceHolder(Ty, D); break;
Chris Lattner00950542001-06-06 20:29:01 +0000291 }
292
293 assert(d != 0 && "How did we not make something?");
Chris Lattner93750fa2001-07-28 17:48:55 +0000294 InsertValue(d, *LateResolver);
Chris Lattner00950542001-06-06 20:29:01 +0000295 return d;
296}
297
298
299//===----------------------------------------------------------------------===//
300// Code to handle forward references in instructions
301//===----------------------------------------------------------------------===//
302//
303// This code handles the late binding needed with statements that reference
304// values not defined yet... for example, a forward branch, or the PHI node for
305// a loop body.
306//
307// This keeps a table (CurMeth.LateResolveValues) of all such forward references
308// and back patchs after we are done.
309//
310
311// ResolveDefinitions - If we could not resolve some defs at parsing
312// time (forward branches, phi functions for loops, etc...) resolve the
313// defs now...
314//
315static void ResolveDefinitions(vector<ValueList> &LateResolvers) {
316 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
317 for (unsigned ty = 0; ty < LateResolvers.size(); ty++) {
318 while (!LateResolvers[ty].empty()) {
319 Value *V = LateResolvers[ty].back();
320 LateResolvers[ty].pop_back();
321 ValID &DID = getValIDFromPlaceHolder(V);
322
323 Value *TheRealValue = getVal(Type::getUniqueIDType(ty), DID, true);
324
Chris Lattner30c89792001-09-07 16:35:17 +0000325 if (TheRealValue == 0) {
326 if (DID.Type == 1)
327 ThrowException("Reference to an invalid definition: '" +DID.getName()+
328 "' of type '" + V->getType()->getDescription() + "'",
329 getLineNumFromPlaceHolder(V));
330 else
331 ThrowException("Reference to an invalid definition: #" +
332 itostr(DID.Num) + " of type '" +
333 V->getType()->getDescription() + "'",
334 getLineNumFromPlaceHolder(V));
335 }
336
337 assert(!V->isType() && "Types should be in LateResolveTypes!");
Chris Lattner00950542001-06-06 20:29:01 +0000338
339 V->replaceAllUsesWith(TheRealValue);
Chris Lattner00950542001-06-06 20:29:01 +0000340 delete V;
341 }
342 }
343
344 LateResolvers.clear();
345}
346
Chris Lattner30c89792001-09-07 16:35:17 +0000347
348// ResolveTypes - This goes through the forward referenced type table and makes
349// sure that all type references are complete. This code is executed after the
350// constant pool of a method or module is completely parsed.
Chris Lattner00950542001-06-06 20:29:01 +0000351//
Chris Lattner30c89792001-09-07 16:35:17 +0000352static void ResolveTypes(vector<PATypeHolder<Type> > &LateResolveTypes) {
353 while (!LateResolveTypes.empty()) {
354 const Type *Ty = LateResolveTypes.back();
355 ValID &DID = getValIDFromPlaceHolder(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000356
Chris Lattner30c89792001-09-07 16:35:17 +0000357 const Type *TheRealType = getTypeVal(DID, true);
358 if (TheRealType == 0) {
359 if (DID.Type == 1)
360 ThrowException("Reference to an invalid type: '" +DID.getName(),
361 getLineNumFromPlaceHolder(Ty));
362 else
363 ThrowException("Reference to an invalid type: #" + itostr(DID.Num),
364 getLineNumFromPlaceHolder(Ty));
Chris Lattner00950542001-06-06 20:29:01 +0000365 }
Chris Lattner30c89792001-09-07 16:35:17 +0000366
367 // FIXME: When types are not const
368 DerivedType *DTy = const_cast<DerivedType*>(Ty->castDerivedTypeAsserting());
369
370 // Refine the opaque type we had to the new type we are getting.
371 DTy->refineAbstractTypeTo(TheRealType);
372
373 // No need to delete type, refine does that for us.
374 LateResolveTypes.pop_back();
375 }
376}
377
Chris Lattner1781aca2001-09-18 04:00:54 +0000378// setValueName - Set the specified value to the name given. The name may be
379// null potentially, in which case this is a noop. The string passed in is
380// assumed to be a malloc'd string buffer, and is freed by this function.
381//
382static void setValueName(Value *V, char *NameStr) {
383 if (NameStr == 0) return;
384 string Name(NameStr); // Copy string
385 free(NameStr); // Free old string
386
Chris Lattner30c89792001-09-07 16:35:17 +0000387 SymbolTable *ST = CurMeth.CurrentMethod ?
388 CurMeth.CurrentMethod->getSymbolTableSure() :
389 CurModule.CurrentModule->getSymbolTableSure();
390
391 Value *Existing = ST->lookup(V->getType(), Name);
392 if (Existing) { // Inserting a name that is already defined???
393 // There is only one case where this is allowed: when we are refining an
394 // opaque type. In this case, Existing will be an opaque type.
395 if (const Type *Ty = Existing->castType())
396 if (Ty->isOpaqueType()) {
397 // We ARE replacing an opaque type!
398
399 // TODO: FIXME when types are not const!
400 const_cast<DerivedType*>(Ty->castDerivedTypeAsserting())->refineAbstractTypeTo(V->castTypeAsserting());
401 return;
402 }
403
404 // Otherwise, we are a simple redefinition of a value, baaad
405 ThrowException("Redefinition of value name '" + Name + "' in the '" +
406 V->getType()->getDescription() + "' type plane!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000407 }
Chris Lattner00950542001-06-06 20:29:01 +0000408
Chris Lattner30c89792001-09-07 16:35:17 +0000409 V->setName(Name, ST);
Chris Lattner00950542001-06-06 20:29:01 +0000410}
411
Chris Lattner8896eda2001-07-09 19:38:36 +0000412
Chris Lattner30c89792001-09-07 16:35:17 +0000413//===----------------------------------------------------------------------===//
414// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000415//
Chris Lattner8896eda2001-07-09 19:38:36 +0000416
Chris Lattner30c89792001-09-07 16:35:17 +0000417// TypeContains - Returns true if Ty contains E in it.
418//
419static bool TypeContains(const Type *Ty, const Type *E) {
420 return find(cfg::tdf_begin(Ty), cfg::tdf_end(Ty), E) != cfg::tdf_end(Ty);
421}
Chris Lattner698b56e2001-07-20 19:15:08 +0000422
Chris Lattner30c89792001-09-07 16:35:17 +0000423
424static vector<pair<unsigned, OpaqueType *> > UpRefs;
425
426static PATypeHolder<Type> HandleUpRefs(const Type *ty) {
427 PATypeHolder<Type> Ty(ty);
428 UR_OUT(UpRefs.size() << " upreferences active!\n");
429 for (unsigned i = 0; i < UpRefs.size(); ) {
430 UR_OUT("TypeContains(" << Ty->getDescription() << ", "
431 << UpRefs[i].second->getDescription() << ") = "
432 << TypeContains(Ty, UpRefs[i].second) << endl);
433 if (TypeContains(Ty, UpRefs[i].second)) {
434 unsigned Level = --UpRefs[i].first; // Decrement level of upreference
435 UR_OUT("Uplevel Ref Level = " << Level << endl);
436 if (Level == 0) { // Upreference should be resolved!
437 UR_OUT("About to resolve upreference!\n";
438 string OldName = UpRefs[i].second->getDescription());
439 UpRefs[i].second->refineAbstractTypeTo(Ty);
440 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
441 UR_OUT("Type '" << OldName << "' refined upreference to: "
442 << (const void*)Ty << ", " << Ty->getDescription() << endl);
443 continue;
444 }
445 }
446
447 ++i; // Otherwise, no resolve, move on...
Chris Lattner8896eda2001-07-09 19:38:36 +0000448 }
Chris Lattner30c89792001-09-07 16:35:17 +0000449 // FIXME: TODO: this should return the updated type
Chris Lattner8896eda2001-07-09 19:38:36 +0000450 return Ty;
451}
452
Chris Lattner30c89792001-09-07 16:35:17 +0000453template <class TypeTy>
454inline static void TypeDone(PATypeHolder<TypeTy> *Ty) {
455 if (UpRefs.size())
456 ThrowException("Invalid upreference in type: " + (*Ty)->getDescription());
457}
458
459// newTH - Allocate a new type holder for the specified type
460template <class TypeTy>
461inline static PATypeHolder<TypeTy> *newTH(const TypeTy *Ty) {
462 return new PATypeHolder<TypeTy>(Ty);
463}
464template <class TypeTy>
465inline static PATypeHolder<TypeTy> *newTH(const PATypeHolder<TypeTy> &TH) {
466 return new PATypeHolder<TypeTy>(TH);
467}
468
469
470// newTHC - Allocate a new type holder for the specified type that can be
471// casted to a new Type type.
472template <class TypeTy, class OldTy>
473inline static PATypeHolder<TypeTy> *newTHC(const PATypeHolder<OldTy> &Old) {
474 return new PATypeHolder<TypeTy>((const TypeTy*)Old.get());
475}
476
Chris Lattner8896eda2001-07-09 19:38:36 +0000477
Chris Lattner00950542001-06-06 20:29:01 +0000478//===----------------------------------------------------------------------===//
479// RunVMAsmParser - Define an interface to this parser
480//===----------------------------------------------------------------------===//
481//
Chris Lattnera2850432001-07-22 18:36:00 +0000482Module *RunVMAsmParser(const string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000483 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000484 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000485 llvmAsmlineno = 1; // Reset the current line number...
486
487 CurModule.CurrentModule = new Module(); // Allocate a new module to read
488 yyparse(); // Parse the file.
489 Module *Result = ParserResult;
Chris Lattner00950542001-06-06 20:29:01 +0000490 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
491 ParserResult = 0;
492
493 return Result;
494}
495
496%}
497
498%union {
Chris Lattner30c89792001-09-07 16:35:17 +0000499 Module *ModuleVal;
500 Method *MethodVal;
501 MethodArgument *MethArgVal;
502 BasicBlock *BasicBlockVal;
503 TerminatorInst *TermInstVal;
504 Instruction *InstVal;
505 ConstPoolVal *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000506
Chris Lattner30c89792001-09-07 16:35:17 +0000507 const Type *PrimType;
508 PATypeHolder<Type> *TypeVal;
509 PATypeHolder<ArrayType> *ArrayTypeTy;
510 PATypeHolder<StructType> *StructTypeTy;
511 Value *ValueVal;
512
513 list<MethodArgument*> *MethodArgList;
514 list<Value*> *ValueList;
515 list<PATypeHolder<Type> > *TypeList;
Chris Lattnerc24d2082001-06-11 15:04:20 +0000516 list<pair<Value*, BasicBlock*> > *PHIList; // Represent the RHS of PHI node
Chris Lattner00950542001-06-06 20:29:01 +0000517 list<pair<ConstPoolVal*, BasicBlock*> > *JumpTable;
Chris Lattner30c89792001-09-07 16:35:17 +0000518 vector<ConstPoolVal*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000519
Chris Lattner30c89792001-09-07 16:35:17 +0000520 int64_t SInt64Val;
521 uint64_t UInt64Val;
522 int SIntVal;
523 unsigned UIntVal;
524 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000525 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000526
Chris Lattner30c89792001-09-07 16:35:17 +0000527 char *StrVal; // This memory is strdup'd!
528 ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000529
Chris Lattner30c89792001-09-07 16:35:17 +0000530 Instruction::UnaryOps UnaryOpVal;
531 Instruction::BinaryOps BinaryOpVal;
532 Instruction::TermOps TermOpVal;
533 Instruction::MemoryOps MemOpVal;
534 Instruction::OtherOps OtherOpVal;
Chris Lattner00950542001-06-06 20:29:01 +0000535}
536
537%type <ModuleVal> Module MethodList
Chris Lattnere1815642001-07-15 06:35:53 +0000538%type <MethodVal> Method MethodProto MethodHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000539%type <BasicBlockVal> BasicBlock InstructionList
540%type <TermInstVal> BBTerminatorInst
541%type <InstVal> Inst InstVal MemoryInst
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000542%type <ConstVal> ConstVal ExtendedConstVal
Chris Lattner027dcc52001-07-08 21:10:27 +0000543%type <ConstVector> ConstVector UByteList
Chris Lattner00950542001-06-06 20:29:01 +0000544%type <MethodArgList> ArgList ArgListH
545%type <MethArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000546%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000547%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner30c89792001-09-07 16:35:17 +0000548%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000549%type <JumpTable> JumpTable
Chris Lattner1781aca2001-09-18 04:00:54 +0000550%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner00950542001-06-06 20:29:01 +0000551
552%type <ValIDVal> ValueRef ConstValueRef // Reference to a definition or BB
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000553%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000554// Tokens and types for handling constant integer values
555//
556// ESINT64VAL - A negative number within long long range
557%token <SInt64Val> ESINT64VAL
558
559// EUINT64VAL - A positive number within uns. long long range
560%token <UInt64Val> EUINT64VAL
561%type <SInt64Val> EINT64VAL
562
563%token <SIntVal> SINTVAL // Signed 32 bit ints...
564%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
565%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000566%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000567
568// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000569%type <TypeVal> Types TypesV UpRTypes UpRTypesV
570%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
571%token <TypeVal> OPAQUE
572%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
573%token <PrimType> FLOAT DOUBLE TYPE LABEL
574%type <ArrayTypeTy> ArrayType ArrayTypeI
575%type <StructTypeTy> StructType StructTypeI
Chris Lattner00950542001-06-06 20:29:01 +0000576
577%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
578%type <StrVal> OptVAR_ID OptAssign
579
580
Chris Lattner1781aca2001-09-18 04:00:54 +0000581%token IMPLEMENTATION TRUE FALSE BEGINTOK END DECLARE GLOBAL CONSTANT UNINIT
582%token TO DOTDOTDOT STRING
Chris Lattner00950542001-06-06 20:29:01 +0000583
584// Basic Block Terminating Operators
585%token <TermOpVal> RET BR SWITCH
586
587// Unary Operators
588%type <UnaryOpVal> UnaryOps // all the unary operators
Chris Lattner71496b32001-07-08 19:03:27 +0000589%token <UnaryOpVal> NOT
Chris Lattner00950542001-06-06 20:29:01 +0000590
591// Binary Operators
592%type <BinaryOpVal> BinaryOps // all the binary operators
593%token <BinaryOpVal> ADD SUB MUL DIV REM
Chris Lattner027dcc52001-07-08 21:10:27 +0000594%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000595
596// Memory Instructions
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000597%token <MemoryOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000598
Chris Lattner027dcc52001-07-08 21:10:27 +0000599// Other Operators
600%type <OtherOpVal> ShiftOps
601%token <OtherOpVal> PHI CALL CAST SHL SHR
602
Chris Lattner00950542001-06-06 20:29:01 +0000603%start Module
604%%
605
606// Handle constant integer size restriction and conversion...
607//
608
609INTVAL : SINTVAL
610INTVAL : UINTVAL {
611 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
612 ThrowException("Value too large for type!");
613 $$ = (int32_t)$1;
614}
615
616
617EINT64VAL : ESINT64VAL // These have same type and can't cause problems...
618EINT64VAL : EUINT64VAL {
619 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
620 ThrowException("Value too large for type!");
621 $$ = (int64_t)$1;
622}
623
Chris Lattner00950542001-06-06 20:29:01 +0000624// Operations that are notably excluded from this list include:
625// RET, BR, & SWITCH because they end basic blocks and are treated specially.
626//
Chris Lattner09083092001-07-08 04:57:15 +0000627UnaryOps : NOT
Chris Lattner00950542001-06-06 20:29:01 +0000628BinaryOps : ADD | SUB | MUL | DIV | REM
629BinaryOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE
Chris Lattner027dcc52001-07-08 21:10:27 +0000630ShiftOps : SHL | SHR
Chris Lattner00950542001-06-06 20:29:01 +0000631
Chris Lattnere98dda62001-07-14 06:10:16 +0000632// These are some types that allow classification if we only want a particular
633// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner00950542001-06-06 20:29:01 +0000634SIntType : LONG | INT | SHORT | SBYTE
635UIntType : ULONG | UINT | USHORT | UBYTE
Chris Lattner30c89792001-09-07 16:35:17 +0000636IntType : SIntType | UIntType
637FPType : FLOAT | DOUBLE
Chris Lattner00950542001-06-06 20:29:01 +0000638
Chris Lattnere98dda62001-07-14 06:10:16 +0000639// OptAssign - Value producing statements have an optional assignment component
Chris Lattner00950542001-06-06 20:29:01 +0000640OptAssign : VAR_ID '=' {
641 $$ = $1;
642 }
643 | /*empty*/ {
644 $$ = 0;
645 }
646
Chris Lattner30c89792001-09-07 16:35:17 +0000647
648//===----------------------------------------------------------------------===//
649// Types includes all predefined types... except void, because it can only be
650// used in specific contexts (method returning void for example). To have
651// access to it, a user must explicitly use TypesV.
652//
653
654// TypesV includes all of 'Types', but it also includes the void type.
655TypesV : Types | VOID { $$ = newTH($1); }
656UpRTypesV : UpRTypes | VOID { $$ = newTH($1); }
657
658Types : UpRTypes {
659 TypeDone($$ = $1);
660 }
661
662
663// Derived types are added later...
664//
665PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT
666PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL
667UpRTypes : OPAQUE | PrimType { $$ = newTH($1); }
668UpRTypes : ValueRef { // Named types are also simple types...
669 $$ = newTH(getTypeVal($1));
670}
671
672// ArrayTypeI - Internal version of ArrayType that can have incomplete uprefs
673//
674ArrayTypeI : '[' UpRTypesV ']' { // Unsized array type?
675 $$ = newTHC<ArrayType>(HandleUpRefs(ArrayType::get(*$2)));
676 delete $2;
677 }
678 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
679 $$ = newTHC<ArrayType>(HandleUpRefs(ArrayType::get(*$4, (int)$2)));
680 delete $4;
681 }
682
683StructTypeI : '{' TypeListI '}' { // Structure type?
684 vector<const Type*> Elements;
685 mapto($2->begin(), $2->end(), back_inserter(Elements),
686 mem_fun_ref(&PATypeHandle<Type>::get));
687
688 $$ = newTHC<StructType>(HandleUpRefs(StructType::get(Elements)));
689 delete $2;
690 }
691 | '{' '}' { // Empty structure type?
692 $$ = newTH(StructType::get(vector<const Type*>()));
693 }
694
695
696// Include derived types in the Types production.
697//
698UpRTypes : '\\' EUINT64VAL { // Type UpReference
699 if ($2 > (uint64_t)INT64_MAX) ThrowException("Value out of range!");
700 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
701 UpRefs.push_back(make_pair((unsigned)$2, OT)); // Add to vector...
702 $$ = newTH<Type>(OT);
703 UR_OUT("New Upreference!\n");
704 }
705 | UpRTypesV '(' ArgTypeListI ')' { // Method derived type?
706 vector<const Type*> Params;
707 mapto($3->begin(), $3->end(), back_inserter(Params),
708 mem_fun_ref(&PATypeHandle<Type>::get));
709 $$ = newTH(HandleUpRefs(MethodType::get(*$1, Params)));
710 delete $3; // Delete the argument list
711 delete $1; // Delete the old type handle
712 }
713 | ArrayTypeI { // [Un]sized array type?
714 $$ = newTHC<Type>(*$1); delete $1;
715 }
716 | StructTypeI { // Structure type?
717 $$ = newTHC<Type>(*$1); delete $1;
718 }
719 | UpRTypes '*' { // Pointer type?
720 $$ = newTH(HandleUpRefs(PointerType::get(*$1)));
721 delete $1; // Delete the type handle
722 }
723
724// Define some helpful top level types that do not allow UpReferences to escape
725//
726ArrayType : ArrayTypeI { TypeDone($$ = $1); }
727StructType : StructTypeI { TypeDone($$ = $1); }
728
729
730
731// TypeList - Used for struct declarations and as a basis for method type
732// declaration type lists
733//
734TypeListI : UpRTypes {
735 $$ = new list<PATypeHolder<Type> >();
736 $$->push_back(*$1); delete $1;
737 }
738 | TypeListI ',' UpRTypes {
739 ($$=$1)->push_back(*$3); delete $3;
740 }
741
742// ArgTypeList - List of types for a method type declaration...
743ArgTypeListI : TypeListI
744 | TypeListI ',' DOTDOTDOT {
745 ($$=$1)->push_back(Type::VoidTy);
746 }
747 | DOTDOTDOT {
748 ($$ = new list<PATypeHolder<Type> >())->push_back(Type::VoidTy);
749 }
750 | /*empty*/ {
751 $$ = new list<PATypeHolder<Type> >();
752 }
753
754
Chris Lattnere98dda62001-07-14 06:10:16 +0000755// ConstVal - The various declarations that go into the constant pool. This
756// includes all forward declarations of types, constants, and functions.
757//
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000758// This is broken into two sections: ExtendedConstVal and ConstVal
759//
Chris Lattner30c89792001-09-07 16:35:17 +0000760ExtendedConstVal: ArrayType '[' ConstVector ']' { // Nonempty unsized arr
761 const ArrayType *ATy = *$1;
762 const Type *ETy = ATy->getElementType();
763 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +0000764
Chris Lattner30c89792001-09-07 16:35:17 +0000765 // Verify that we have the correct size...
766 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +0000767 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattner30c89792001-09-07 16:35:17 +0000768 utostr($3->size()) + " arguments, but has size of " +
769 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +0000770
Chris Lattner30c89792001-09-07 16:35:17 +0000771 // Verify all elements are correct type!
772 for (unsigned i = 0; i < $3->size(); i++) {
773 if (ETy != (*$3)[i]->getType())
Chris Lattner00950542001-06-06 20:29:01 +0000774 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000775 ETy->getName() + "' as required!\nIt is of type '" +
776 (*$3)[i]->getType()->getName() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +0000777 }
778
Chris Lattner30c89792001-09-07 16:35:17 +0000779 $$ = ConstPoolArray::get(ATy, *$3);
780 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000781 }
Chris Lattner30c89792001-09-07 16:35:17 +0000782 | ArrayType '[' ']' {
783 int NumElements = (*$1)->getNumElements();
784 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +0000785 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattner30c89792001-09-07 16:35:17 +0000786 " arguments, but has size of " + itostr(NumElements) +"!");
787 $$ = ConstPoolArray::get((*$1), vector<ConstPoolVal*>());
788 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +0000789 }
Chris Lattner30c89792001-09-07 16:35:17 +0000790 | ArrayType 'c' STRINGCONSTANT {
791 const ArrayType *ATy = *$1;
792 int NumElements = ATy->getNumElements();
793 const Type *ETy = ATy->getElementType();
794 char *EndStr = UnEscapeLexed($3, true);
795 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +0000796 ThrowException("Can't build string constant of size " +
Chris Lattner30c89792001-09-07 16:35:17 +0000797 itostr((int)(EndStr-$3)) +
798 " when array has size " + itostr(NumElements) + "!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000799 vector<ConstPoolVal*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +0000800 if (ETy == Type::SByteTy) {
801 for (char *C = $3; C != EndStr; ++C)
802 Vals.push_back(ConstPoolSInt::get(ETy, *C));
803 } else if (ETy == Type::UByteTy) {
804 for (char *C = $3; C != EndStr; ++C)
805 Vals.push_back(ConstPoolUInt::get(ETy, *C));
Chris Lattner93750fa2001-07-28 17:48:55 +0000806 } else {
Chris Lattner30c89792001-09-07 16:35:17 +0000807 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +0000808 ThrowException("Cannot build string arrays of non byte sized elements!");
809 }
Chris Lattner30c89792001-09-07 16:35:17 +0000810 free($3);
811 $$ = ConstPoolArray::get(ATy, Vals);
812 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +0000813 }
Chris Lattner30c89792001-09-07 16:35:17 +0000814 | StructType '{' ConstVector '}' {
815 // FIXME: TODO: Check to see that the constants are compatible with the type
816 // initializer!
817 $$ = ConstPoolStruct::get(*$1, *$3);
818 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000819 }
820/*
821 | Types '*' ConstVal {
822 assert(0);
823 $$ = 0;
824 }
825*/
826
Chris Lattner93750fa2001-07-28 17:48:55 +0000827ConstVal : ExtendedConstVal {
828 $$ = $1;
829 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000830 | SIntType EINT64VAL { // integral constants
831 if (!ConstPoolSInt::isValueValidForType($1, $2))
832 ThrowException("Constant value doesn't fit in type!");
Chris Lattner30c89792001-09-07 16:35:17 +0000833 $$ = ConstPoolSInt::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000834 }
835 | UIntType EUINT64VAL { // integral constants
836 if (!ConstPoolUInt::isValueValidForType($1, $2))
837 ThrowException("Constant value doesn't fit in type!");
Chris Lattner30c89792001-09-07 16:35:17 +0000838 $$ = ConstPoolUInt::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000839 }
840 | BOOL TRUE { // Boolean constants
Chris Lattner30c89792001-09-07 16:35:17 +0000841 $$ = ConstPoolBool::True;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000842 }
843 | BOOL FALSE { // Boolean constants
Chris Lattner30c89792001-09-07 16:35:17 +0000844 $$ = ConstPoolBool::False;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000845 }
846 | FPType FPVAL { // Float & Double constants
Chris Lattner30c89792001-09-07 16:35:17 +0000847 $$ = ConstPoolFP::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000848 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000849
Chris Lattnere98dda62001-07-14 06:10:16 +0000850// ConstVector - A list of comma seperated constants.
Chris Lattner00950542001-06-06 20:29:01 +0000851ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +0000852 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +0000853 }
854 | ConstVal {
855 $$ = new vector<ConstPoolVal*>();
Chris Lattner30c89792001-09-07 16:35:17 +0000856 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +0000857 }
858
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000859
Chris Lattner1781aca2001-09-18 04:00:54 +0000860// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
861GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; }
862
Chris Lattner00950542001-06-06 20:29:01 +0000863
Chris Lattnere98dda62001-07-14 06:10:16 +0000864// ConstPool - Constants with optional names assigned to them.
Chris Lattner00950542001-06-06 20:29:01 +0000865ConstPool : ConstPool OptAssign ConstVal {
Chris Lattner1781aca2001-09-18 04:00:54 +0000866 setValueName($3, $2);
Chris Lattner30c89792001-09-07 16:35:17 +0000867 InsertValue($3);
Chris Lattner00950542001-06-06 20:29:01 +0000868 }
Chris Lattner30c89792001-09-07 16:35:17 +0000869 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner1781aca2001-09-18 04:00:54 +0000870 // TODO: FIXME when Type are not const
871 setValueName(const_cast<Type*>($4->get()), $2);
872
873 if (!$2) {
Chris Lattner30c89792001-09-07 16:35:17 +0000874 InsertType($4->get(),
875 CurMeth.CurrentMethod ? CurMeth.Types : CurModule.Types);
876 }
877 delete $4;
878 }
879 | ConstPool MethodProto { // Method prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +0000880 }
Chris Lattner1781aca2001-09-18 04:00:54 +0000881 | ConstPool OptAssign GlobalType ResolvedVal {
882 const Type *Ty = $4->getType();
883 // Global declarations appear in Constant Pool
884 if (Ty->isArrayType() && Ty->castArrayType()->isUnsized()) {
885 ThrowException("Type '" + Ty->getDescription() +
886 "' is not a sized type!");
Chris Lattnere98dda62001-07-14 06:10:16 +0000887 }
Chris Lattner70cc3392001-09-10 07:58:01 +0000888
Chris Lattner1781aca2001-09-18 04:00:54 +0000889 ConstPoolVal *Initializer = $4->castConstant();
890 if (Initializer == 0)
891 ThrowException("Global value initializer is not a constant!");
892
893 GlobalVariable *GV = new GlobalVariable(PointerType::get(Ty), $3,
894 Initializer);
895 setValueName(GV, $2);
896
897 CurModule.CurrentModule->getGlobalList().push_back(GV);
898 InsertValue(GV, CurModule.Values);
899 }
900 | ConstPool OptAssign UNINIT GlobalType Types {
901 const Type *Ty = *$5;
902 // Global declarations appear in Constant Pool
903 if (Ty->isArrayType() && Ty->castArrayType()->isUnsized()) {
904 ThrowException("Type '" + Ty->getDescription() +
905 "' is not a sized type!");
Chris Lattner70cc3392001-09-10 07:58:01 +0000906 }
Chris Lattner1781aca2001-09-18 04:00:54 +0000907
908 GlobalVariable *GV = new GlobalVariable(PointerType::get(Ty), $4);
909 setValueName(GV, $2);
910
Chris Lattner70cc3392001-09-10 07:58:01 +0000911 CurModule.CurrentModule->getGlobalList().push_back(GV);
912 InsertValue(GV, CurModule.Values);
Chris Lattnere98dda62001-07-14 06:10:16 +0000913 }
Chris Lattner00950542001-06-06 20:29:01 +0000914 | /* empty: end of list */ {
915 }
916
917
918//===----------------------------------------------------------------------===//
919// Rules to match Modules
920//===----------------------------------------------------------------------===//
921
922// Module rule: Capture the result of parsing the whole file into a result
923// variable...
924//
925Module : MethodList {
926 $$ = ParserResult = $1;
927 CurModule.ModuleDone();
928}
929
Chris Lattnere98dda62001-07-14 06:10:16 +0000930// MethodList - A list of methods, preceeded by a constant pool.
931//
Chris Lattner00950542001-06-06 20:29:01 +0000932MethodList : MethodList Method {
Chris Lattner00950542001-06-06 20:29:01 +0000933 $$ = $1;
Chris Lattnere1815642001-07-15 06:35:53 +0000934 if (!$2->getParent())
935 $1->getMethodList().push_back($2);
936 CurMeth.MethodDone();
Chris Lattner00950542001-06-06 20:29:01 +0000937 }
Chris Lattnere1815642001-07-15 06:35:53 +0000938 | MethodList MethodProto {
939 $$ = $1;
Chris Lattnere1815642001-07-15 06:35:53 +0000940 }
Chris Lattner00950542001-06-06 20:29:01 +0000941 | ConstPool IMPLEMENTATION {
942 $$ = CurModule.CurrentModule;
Chris Lattner30c89792001-09-07 16:35:17 +0000943 // Resolve circular types before we parse the body of the module
944 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner00950542001-06-06 20:29:01 +0000945 }
946
947
948//===----------------------------------------------------------------------===//
949// Rules to match Method Headers
950//===----------------------------------------------------------------------===//
951
952OptVAR_ID : VAR_ID | /*empty*/ { $$ = 0; }
953
954ArgVal : Types OptVAR_ID {
Chris Lattner30c89792001-09-07 16:35:17 +0000955 $$ = new MethodArgument(*$1); delete $1;
Chris Lattner1781aca2001-09-18 04:00:54 +0000956 setValueName($$, $2);
Chris Lattner00950542001-06-06 20:29:01 +0000957}
958
959ArgListH : ArgVal ',' ArgListH {
960 $$ = $3;
961 $3->push_front($1);
962 }
963 | ArgVal {
964 $$ = new list<MethodArgument*>();
965 $$->push_front($1);
966 }
Chris Lattner8b81bf52001-07-25 22:47:46 +0000967 | DOTDOTDOT {
968 $$ = new list<MethodArgument*>();
969 $$->push_back(new MethodArgument(Type::VoidTy));
970 }
Chris Lattner00950542001-06-06 20:29:01 +0000971
972ArgList : ArgListH {
973 $$ = $1;
974 }
975 | /* empty */ {
976 $$ = 0;
977 }
978
979MethodHeaderH : TypesV STRINGCONSTANT '(' ArgList ')' {
Chris Lattner93750fa2001-07-28 17:48:55 +0000980 UnEscapeLexed($2);
Chris Lattner30c89792001-09-07 16:35:17 +0000981 vector<const Type*> ParamTypeList;
Chris Lattner00950542001-06-06 20:29:01 +0000982 if ($4)
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000983 for (list<MethodArgument*>::iterator I = $4->begin(); I != $4->end(); ++I)
Chris Lattner00950542001-06-06 20:29:01 +0000984 ParamTypeList.push_back((*I)->getType());
985
Chris Lattner30c89792001-09-07 16:35:17 +0000986 const MethodType *MT = MethodType::get(*$1, ParamTypeList);
987 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +0000988
Chris Lattnere1815642001-07-15 06:35:53 +0000989 Method *M = 0;
990 if (SymbolTable *ST = CurModule.CurrentModule->getSymbolTable()) {
991 if (Value *V = ST->lookup(MT, $2)) { // Method already in symtab?
992 M = V->castMethodAsserting();
Chris Lattner00950542001-06-06 20:29:01 +0000993
Chris Lattnere1815642001-07-15 06:35:53 +0000994 // Yes it is. If this is the case, either we need to be a forward decl,
995 // or it needs to be.
996 if (!CurMeth.isDeclare && !M->isExternal())
997 ThrowException("Redefinition of method '" + string($2) + "'!");
998 }
999 }
1000
1001 if (M == 0) { // Not already defined?
1002 M = new Method(MT, $2);
1003 InsertValue(M, CurModule.Values);
1004 }
1005
1006 free($2); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +00001007
1008 CurMeth.MethodStart(M);
1009
1010 // Add all of the arguments we parsed to the method...
Chris Lattnere1815642001-07-15 06:35:53 +00001011 if ($4 && !CurMeth.isDeclare) { // Is null if empty...
Chris Lattner00950542001-06-06 20:29:01 +00001012 Method::ArgumentListType &ArgList = M->getArgumentList();
1013
Chris Lattner7fc9fe32001-06-27 23:41:11 +00001014 for (list<MethodArgument*>::iterator I = $4->begin(); I != $4->end(); ++I) {
Chris Lattner00950542001-06-06 20:29:01 +00001015 InsertValue(*I);
1016 ArgList.push_back(*I);
1017 }
1018 delete $4; // We're now done with the argument list
1019 }
1020}
1021
1022MethodHeader : MethodHeaderH ConstPool BEGINTOK {
1023 $$ = CurMeth.CurrentMethod;
Chris Lattner30c89792001-09-07 16:35:17 +00001024
1025 // Resolve circular types before we parse the body of the method.
1026 ResolveTypes(CurMeth.LateResolveTypes);
Chris Lattner00950542001-06-06 20:29:01 +00001027}
1028
1029Method : BasicBlockList END {
1030 $$ = $1;
1031}
1032
Chris Lattnere1815642001-07-15 06:35:53 +00001033MethodProto : DECLARE { CurMeth.isDeclare = true; } MethodHeaderH {
1034 $$ = CurMeth.CurrentMethod;
Chris Lattner93750fa2001-07-28 17:48:55 +00001035 if (!$$->getParent())
1036 CurModule.CurrentModule->getMethodList().push_back($$);
1037 CurMeth.MethodDone();
Chris Lattnere1815642001-07-15 06:35:53 +00001038}
Chris Lattner00950542001-06-06 20:29:01 +00001039
1040//===----------------------------------------------------------------------===//
1041// Rules to match Basic Blocks
1042//===----------------------------------------------------------------------===//
1043
1044ConstValueRef : ESINT64VAL { // A reference to a direct constant
1045 $$ = ValID::create($1);
1046 }
1047 | EUINT64VAL {
1048 $$ = ValID::create($1);
1049 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001050 | FPVAL { // Perhaps it's an FP constant?
1051 $$ = ValID::create($1);
1052 }
Chris Lattner00950542001-06-06 20:29:01 +00001053 | TRUE {
1054 $$ = ValID::create((int64_t)1);
1055 }
1056 | FALSE {
1057 $$ = ValID::create((int64_t)0);
1058 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001059/*
Chris Lattner00950542001-06-06 20:29:01 +00001060 | STRINGCONSTANT { // Quoted strings work too... especially for methods
1061 $$ = ValID::create_conststr($1);
1062 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001063*/
Chris Lattner00950542001-06-06 20:29:01 +00001064
1065// ValueRef - A reference to a definition...
1066ValueRef : INTVAL { // Is it an integer reference...?
1067 $$ = ValID::create($1);
1068 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001069 | VAR_ID { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001070 $$ = ValID::create($1);
1071 }
1072 | ConstValueRef {
1073 $$ = $1;
1074 }
1075
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001076// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1077// type immediately preceeds the value reference, and allows complex constant
1078// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
1079ResolvedVal : ExtendedConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001080 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001081 }
1082 | Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001083 $$ = getVal(*$1, $2); delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +00001084 }
Chris Lattner8b81bf52001-07-25 22:47:46 +00001085
Chris Lattner00950542001-06-06 20:29:01 +00001086
1087BasicBlockList : BasicBlockList BasicBlock {
1088 $1->getBasicBlocks().push_back($2);
1089 $$ = $1;
1090 }
1091 | MethodHeader BasicBlock { // Do not allow methods with 0 basic blocks
1092 $$ = $1; // in them...
1093 $1->getBasicBlocks().push_back($2);
1094 }
1095
1096
1097// Basic blocks are terminated by branching instructions:
1098// br, br/cc, switch, ret
1099//
1100BasicBlock : InstructionList BBTerminatorInst {
1101 $1->getInstList().push_back($2);
1102 InsertValue($1);
1103 $$ = $1;
1104 }
1105 | LABELSTR InstructionList BBTerminatorInst {
1106 $2->getInstList().push_back($3);
Chris Lattner30c89792001-09-07 16:35:17 +00001107 setValueName($2, $1);
Chris Lattner00950542001-06-06 20:29:01 +00001108
1109 InsertValue($2);
1110 $$ = $2;
1111 }
1112
1113InstructionList : InstructionList Inst {
1114 $1->getInstList().push_back($2);
1115 $$ = $1;
1116 }
1117 | /* empty */ {
1118 $$ = new BasicBlock();
1119 }
1120
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001121BBTerminatorInst : RET ResolvedVal { // Return with a result...
1122 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001123 }
1124 | RET VOID { // Return with no result...
1125 $$ = new ReturnInst();
1126 }
1127 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001128 $$ = new BranchInst(getVal(Type::LabelTy, $3)->castBasicBlockAsserting());
Chris Lattner00950542001-06-06 20:29:01 +00001129 } // Conditional Branch...
1130 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001131 $$ = new BranchInst(getVal(Type::LabelTy, $6)->castBasicBlockAsserting(),
1132 getVal(Type::LabelTy, $9)->castBasicBlockAsserting(),
Chris Lattner00950542001-06-06 20:29:01 +00001133 getVal(Type::BoolTy, $3));
1134 }
1135 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1136 SwitchInst *S = new SwitchInst(getVal($2, $3),
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001137 getVal(Type::LabelTy, $6)->castBasicBlockAsserting());
Chris Lattner00950542001-06-06 20:29:01 +00001138 $$ = S;
1139
1140 list<pair<ConstPoolVal*, BasicBlock*> >::iterator I = $8->begin(),
1141 end = $8->end();
Chris Lattner7fc9fe32001-06-27 23:41:11 +00001142 for (; I != end; ++I)
Chris Lattner00950542001-06-06 20:29:01 +00001143 S->dest_push_back(I->first, I->second);
1144 }
1145
1146JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1147 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001148 ConstPoolVal *V = getVal($2, $3, true)->castConstantAsserting();
Chris Lattner00950542001-06-06 20:29:01 +00001149 if (V == 0)
1150 ThrowException("May only switch on a constant pool value!");
1151
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001152 $$->push_back(make_pair(V, getVal($5, $6)->castBasicBlockAsserting()));
Chris Lattner00950542001-06-06 20:29:01 +00001153 }
1154 | IntType ConstValueRef ',' LABEL ValueRef {
1155 $$ = new list<pair<ConstPoolVal*, BasicBlock*> >();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001156 ConstPoolVal *V = getVal($1, $2, true)->castConstantAsserting();
Chris Lattner00950542001-06-06 20:29:01 +00001157
1158 if (V == 0)
1159 ThrowException("May only switch on a constant pool value!");
1160
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001161 $$->push_back(make_pair(V, getVal($4, $5)->castBasicBlockAsserting()));
Chris Lattner00950542001-06-06 20:29:01 +00001162 }
1163
1164Inst : OptAssign InstVal {
Chris Lattner1781aca2001-09-18 04:00:54 +00001165 setValueName($2, $1); // Is this definition named?? if so, assign the name...
Chris Lattner00950542001-06-06 20:29:01 +00001166
1167 InsertValue($2);
1168 $$ = $2;
1169}
1170
Chris Lattnerc24d2082001-06-11 15:04:20 +00001171PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
1172 $$ = new list<pair<Value*, BasicBlock*> >();
Chris Lattner30c89792001-09-07 16:35:17 +00001173 $$->push_back(make_pair(getVal(*$1, $3),
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001174 getVal(Type::LabelTy, $5)->castBasicBlockAsserting()));
Chris Lattner30c89792001-09-07 16:35:17 +00001175 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001176 }
1177 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1178 $$ = $1;
1179 $1->push_back(make_pair(getVal($1->front().first->getType(), $4),
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001180 getVal(Type::LabelTy, $6)->castBasicBlockAsserting()));
Chris Lattnerc24d2082001-06-11 15:04:20 +00001181 }
1182
1183
Chris Lattner30c89792001-09-07 16:35:17 +00001184ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner00950542001-06-06 20:29:01 +00001185 $$ = new list<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001186 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001187 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001188 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001189 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001190 $1->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001191 }
1192
1193// ValueRefListE - Just like ValueRefList, except that it may also be empty!
1194ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; }
1195
1196InstVal : BinaryOps Types ValueRef ',' ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001197 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001198 if ($$ == 0)
1199 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001200 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001201 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001202 | UnaryOps ResolvedVal {
1203 $$ = UnaryOperator::create($1, $2);
Chris Lattner00950542001-06-06 20:29:01 +00001204 if ($$ == 0)
1205 ThrowException("unary operator returned null!");
Chris Lattner09083092001-07-08 04:57:15 +00001206 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001207 | ShiftOps ResolvedVal ',' ResolvedVal {
1208 if ($4->getType() != Type::UByteTy)
1209 ThrowException("Shift amount must be ubyte!");
1210 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001211 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001212 | CAST ResolvedVal TO Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001213 $$ = new CastInst($2, *$4);
1214 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001215 }
Chris Lattnerc24d2082001-06-11 15:04:20 +00001216 | PHI PHIList {
1217 const Type *Ty = $2->front().first->getType();
1218 $$ = new PHINode(Ty);
Chris Lattner00950542001-06-06 20:29:01 +00001219 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001220 if ($2->front().first->getType() != Ty)
1221 ThrowException("All elements of a PHI node must be of the same type!");
1222 ((PHINode*)$$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001223 $2->pop_front();
1224 }
1225 delete $2; // Free the list...
1226 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001227 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001228 const MethodType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001229
Chris Lattnerb2d22f62001-09-10 20:08:08 +00001230 if (!(Ty = (*$2)->dyncastMethodType())) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001231 // Pull out the types of all of the arguments...
1232 vector<const Type*> ParamTypes;
1233 for (list<Value*>::iterator I = $5->begin(), E = $5->end(); I != E; ++I)
1234 ParamTypes.push_back((*I)->getType());
Chris Lattner30c89792001-09-07 16:35:17 +00001235 Ty = MethodType::get(*$2, ParamTypes);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001236 }
Chris Lattner30c89792001-09-07 16:35:17 +00001237 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001238
Chris Lattner8b81bf52001-07-25 22:47:46 +00001239 Value *V = getVal(Ty, $3); // Get the method we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001240
Chris Lattner8b81bf52001-07-25 22:47:46 +00001241 // Create the call node...
1242 if (!$5) { // Has no arguments?
1243 $$ = new CallInst(V->castMethodAsserting(), vector<Value*>());
1244 } else { // Has arguments?
Chris Lattner00950542001-06-06 20:29:01 +00001245 // Loop through MethodType's arguments and ensure they are specified
1246 // correctly!
1247 //
1248 MethodType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001249 MethodType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
1250 list<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
1251
1252 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1253 if ((*ArgI)->getType() != *I)
1254 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner00950542001-06-06 20:29:01 +00001255 (*I)->getName() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001256
Chris Lattner8b81bf52001-07-25 22:47:46 +00001257 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner00950542001-06-06 20:29:01 +00001258 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00001259
Chris Lattner8b81bf52001-07-25 22:47:46 +00001260 $$ = new CallInst(V->castMethodAsserting(),
1261 vector<Value*>($5->begin(), $5->end()));
1262 }
1263 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00001264 }
1265 | MemoryInst {
1266 $$ = $1;
1267 }
1268
Chris Lattner027dcc52001-07-08 21:10:27 +00001269// UByteList - List of ubyte values for load and store instructions
1270UByteList : ',' ConstVector {
1271 $$ = $2;
1272} | /* empty */ {
1273 $$ = new vector<ConstPoolVal*>();
1274}
1275
Chris Lattner00950542001-06-06 20:29:01 +00001276MemoryInst : MALLOC Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001277 $$ = new MallocInst(PointerType::get(*$2));
1278 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001279 }
1280 | MALLOC Types ',' UINT ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001281 if (!(*$2)->isArrayType() || ((const ArrayType*)$2->get())->isSized())
1282 ThrowException("Trying to allocate " + (*$2)->getName() +
Chris Lattner00950542001-06-06 20:29:01 +00001283 " as unsized array!");
Chris Lattner30c89792001-09-07 16:35:17 +00001284 const Type *Ty = PointerType::get(*$2);
Chris Lattner8896eda2001-07-09 19:38:36 +00001285 $$ = new MallocInst(Ty, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001286 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001287 }
1288 | ALLOCA Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001289 $$ = new AllocaInst(PointerType::get(*$2));
1290 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001291 }
1292 | ALLOCA Types ',' UINT ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001293 if (!(*$2)->isArrayType() || ((const ArrayType*)$2->get())->isSized())
1294 ThrowException("Trying to allocate " + (*$2)->getName() +
Chris Lattner00950542001-06-06 20:29:01 +00001295 " as unsized array!");
Chris Lattner30c89792001-09-07 16:35:17 +00001296 const Type *Ty = PointerType::get(*$2);
Chris Lattner00950542001-06-06 20:29:01 +00001297 Value *ArrSize = getVal($4, $5);
Chris Lattnerf0d0e9c2001-07-07 08:36:30 +00001298 $$ = new AllocaInst(Ty, ArrSize);
Chris Lattner30c89792001-09-07 16:35:17 +00001299 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001300 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001301 | FREE ResolvedVal {
1302 if (!$2->getType()->isPointerType())
1303 ThrowException("Trying to free nonpointer type " +
1304 $2->getType()->getName() + "!");
1305 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001306 }
1307
Chris Lattner027dcc52001-07-08 21:10:27 +00001308 | LOAD Types ValueRef UByteList {
Chris Lattner30c89792001-09-07 16:35:17 +00001309 if (!(*$2)->isPointerType())
1310 ThrowException("Can't load from nonpointer type: " + (*$2)->getName());
1311 if (LoadInst::getIndexedType(*$2, *$4) == 0)
Chris Lattner027dcc52001-07-08 21:10:27 +00001312 ThrowException("Invalid indices for load instruction!");
1313
Chris Lattner30c89792001-09-07 16:35:17 +00001314 $$ = new LoadInst(getVal(*$2, $3), *$4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001315 delete $4; // Free the vector...
Chris Lattner30c89792001-09-07 16:35:17 +00001316 delete $2;
Chris Lattner027dcc52001-07-08 21:10:27 +00001317 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001318 | STORE ResolvedVal ',' Types ValueRef UByteList {
Chris Lattner30c89792001-09-07 16:35:17 +00001319 if (!(*$4)->isPointerType())
1320 ThrowException("Can't store to a nonpointer type: " + (*$4)->getName());
1321 const Type *ElTy = StoreInst::getIndexedType(*$4, *$6);
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001322 if (ElTy == 0)
1323 ThrowException("Can't store into that field list!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001324 if (ElTy != $2->getType())
1325 ThrowException("Can't store '" + $2->getType()->getName() +
1326 "' into space of type '" + ElTy->getName() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001327 $$ = new StoreInst($2, getVal(*$4, $5), *$6);
1328 delete $4; delete $6;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001329 }
1330 | GETELEMENTPTR Types ValueRef UByteList {
Chris Lattner30c89792001-09-07 16:35:17 +00001331 if (!(*$2)->isPointerType())
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001332 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner30c89792001-09-07 16:35:17 +00001333 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
1334 ThrowException("Can't get element ptr '" + (*$2)->getName() + "'!");
1335 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
1336 delete $2; delete $4;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001337 }
Chris Lattner027dcc52001-07-08 21:10:27 +00001338
Chris Lattner00950542001-06-06 20:29:01 +00001339%%
Chris Lattner09083092001-07-08 04:57:15 +00001340int yyerror(const char *ErrorMsg) {
Chris Lattner00950542001-06-06 20:29:01 +00001341 ThrowException(string("Parse error: ") + ErrorMsg);
1342 return 0;
1343}