blob: 6808f5ff9b464ccae5599aeefaa7889ba659b14e [file] [log] [blame]
Chris Lattner00950542001-06-06 20:29:01 +00001//===-- llvmAsmParser.y - Parser for llvm assembly files ---------*- C++ -*--=//
2//
3// This file implements the bison parser for LLVM assembly languages files.
4//
5//===------------------------------------------------------------------------=//
6
7//
8// TODO: Parse comments and add them to an internal node... so that they may
9// be saved in the bytecode format as well as everything else. Very important
10// for a general IR format.
11//
12
13%{
14#include "ParserInternals.h"
Chris Lattner70cc3392001-09-10 07:58:01 +000015#include "llvm/Assembly/Parser.h"
Chris Lattner00950542001-06-06 20:29:01 +000016#include "llvm/SymbolTable.h"
17#include "llvm/Module.h"
Chris Lattner70cc3392001-09-10 07:58:01 +000018#include "llvm/GlobalVariable.h"
19#include "llvm/Method.h"
20#include "llvm/BasicBlock.h"
Chris Lattner00950542001-06-06 20:29:01 +000021#include "llvm/DerivedTypes.h"
Chris Lattner00950542001-06-06 20:29:01 +000022#include "llvm/iTerminators.h"
23#include "llvm/iMemory.h"
Chris Lattner30c89792001-09-07 16:35:17 +000024#include "llvm/Support/STLExtras.h"
Chris Lattner3ff43872001-09-28 22:56:31 +000025#include "llvm/Support/DepthFirstIterator.h"
Chris Lattner00950542001-06-06 20:29:01 +000026#include <list>
27#include <utility> // Get definition of pair class
Chris Lattner30c89792001-09-07 16:35:17 +000028#include <algorithm>
Chris Lattner00950542001-06-06 20:29:01 +000029#include <stdio.h> // This embarasment is due to our flex lexer...
30
Chris 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...
Chris Lattnercfe26c92001-10-01 18:26:53 +0000158 return cast<const Type>(N);
Chris Lattner30c89792001-09-07 16:35:17 +0000159 }
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
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000178static Value *lookupInSymbolTable(const Type *Ty, const string &Name) {
179 SymbolTable *SymTab =
180 CurMeth.CurrentMethod ? CurMeth.CurrentMethod->getSymbolTable() : 0;
181 Value *N = SymTab ? SymTab->lookup(Ty, Name) : 0;
182
183 if (N == 0) {
184 // Symbol table doesn't automatically chain yet... because the method
185 // hasn't been added to the module...
186 //
187 SymTab = CurModule.CurrentModule->getSymbolTable();
188 if (SymTab)
189 N = SymTab->lookup(Ty, Name);
190 }
191
192 return N;
193}
194
Chris Lattner30c89792001-09-07 16:35:17 +0000195static Value *getVal(const Type *Ty, const ValID &D,
196 bool DoNotImprovise = false) {
197 assert(Ty != Type::TypeTy && "Should use getTypeVal for types!");
198
199 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000200 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000201 unsigned type = Ty->getUniqueID();
Chris Lattner00950542001-06-06 20:29:01 +0000202 unsigned Num = (unsigned)D.Num;
203
204 // Module constants occupy the lowest numbered slots...
205 if (type < CurModule.Values.size()) {
206 if (Num < CurModule.Values[type].size())
207 return CurModule.Values[type][Num];
208
209 Num -= CurModule.Values[type].size();
210 }
211
212 // Make sure that our type is within bounds
213 if (CurMeth.Values.size() <= type)
214 break;
215
216 // Check that the number is within bounds...
217 if (CurMeth.Values[type].size() <= Num)
218 break;
219
220 return CurMeth.Values[type][Num];
221 }
Chris Lattner1a1cb112001-09-30 22:46:54 +0000222 case ValID::NameVal: { // Is it a named definition?
Chris Lattner00950542001-06-06 20:29:01 +0000223 string Name(D.Name);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000224 Value *N = lookupInSymbolTable(Ty, Name);
225 if (N == 0) break;
Chris Lattner00950542001-06-06 20:29:01 +0000226
227 D.destroy(); // Free old strdup'd memory...
228 return N;
229 }
230
Chris Lattner1a1cb112001-09-30 22:46:54 +0000231 case ValID::ConstSIntVal: // Is it a constant pool reference??
232 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
233 case ValID::ConstStringVal: // Is it a string const pool reference?
234 case ValID::ConstFPVal: // Is it a floating point const pool reference?
235 case ValID::ConstNullVal: { // Is it a null value?
Chris Lattner00950542001-06-06 20:29:01 +0000236 ConstPoolVal *CPV = 0;
237
Chris Lattner30c89792001-09-07 16:35:17 +0000238 // Check to make sure that "Ty" is an integral type, and that our
Chris Lattner00950542001-06-06 20:29:01 +0000239 // value will fit into the specified type...
240 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000241 case ValID::ConstSIntVal:
Chris Lattner30c89792001-09-07 16:35:17 +0000242 if (Ty == Type::BoolTy) { // Special handling for boolean data
243 CPV = ConstPoolBool::get(D.ConstPool64 != 0);
Chris Lattner00950542001-06-06 20:29:01 +0000244 } else {
Chris Lattner30c89792001-09-07 16:35:17 +0000245 if (!ConstPoolSInt::isValueValidForType(Ty, D.ConstPool64))
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000246 ThrowException("Symbolic constant pool value '" +
247 itostr(D.ConstPool64) + "' is invalid for type '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000248 Ty->getName() + "'!");
249 CPV = ConstPoolSInt::get(Ty, D.ConstPool64);
Chris Lattner00950542001-06-06 20:29:01 +0000250 }
251 break;
Chris Lattner1a1cb112001-09-30 22:46:54 +0000252 case ValID::ConstUIntVal:
Chris Lattner30c89792001-09-07 16:35:17 +0000253 if (!ConstPoolUInt::isValueValidForType(Ty, D.UConstPool64)) {
254 if (!ConstPoolSInt::isValueValidForType(Ty, D.ConstPool64)) {
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000255 ThrowException("Integral constant pool reference is invalid!");
Chris Lattner00950542001-06-06 20:29:01 +0000256 } else { // This is really a signed reference. Transmogrify.
Chris Lattner30c89792001-09-07 16:35:17 +0000257 CPV = ConstPoolSInt::get(Ty, D.ConstPool64);
Chris Lattner00950542001-06-06 20:29:01 +0000258 }
259 } else {
Chris Lattner30c89792001-09-07 16:35:17 +0000260 CPV = ConstPoolUInt::get(Ty, D.UConstPool64);
Chris Lattner00950542001-06-06 20:29:01 +0000261 }
262 break;
Chris Lattner1a1cb112001-09-30 22:46:54 +0000263 case ValID::ConstStringVal:
Chris Lattner00950542001-06-06 20:29:01 +0000264 cerr << "FIXME: TODO: String constants [sbyte] not implemented yet!\n";
265 abort();
Chris Lattner00950542001-06-06 20:29:01 +0000266 break;
Chris Lattner1a1cb112001-09-30 22:46:54 +0000267 case ValID::ConstFPVal:
Chris Lattner30c89792001-09-07 16:35:17 +0000268 if (!ConstPoolFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000269 ThrowException("FP constant invalid for type!!");
Chris Lattner1a1cb112001-09-30 22:46:54 +0000270 CPV = ConstPoolFP::get(Ty, D.ConstPoolFP);
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000271 break;
Chris Lattner1a1cb112001-09-30 22:46:54 +0000272 case ValID::ConstNullVal:
273 if (!Ty->isPointerType())
274 ThrowException("Cannot create a a non pointer null!");
Chris Lattnercfe26c92001-10-01 18:26:53 +0000275 CPV = ConstPoolPointer::getNullPointer(cast<PointerType>(Ty));
Chris Lattner1a1cb112001-09-30 22:46:54 +0000276 break;
277 default:
278 assert(0 && "Unhandled case!");
Chris Lattner00950542001-06-06 20:29:01 +0000279 }
280 assert(CPV && "How did we escape creating a constant??");
Chris Lattner00950542001-06-06 20:29:01 +0000281 return CPV;
282 } // End of case 2,3,4
Chris Lattner30c89792001-09-07 16:35:17 +0000283 default:
284 assert(0 && "Unhandled case!");
Chris Lattner00950542001-06-06 20:29:01 +0000285 } // End of switch
286
287
288 // If we reached here, we referenced either a symbol that we don't know about
289 // or an id number that hasn't been read yet. We may be referencing something
290 // forward, so just create an entry to be resolved later and get to it...
291 //
292 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
293
Chris Lattner00950542001-06-06 20:29:01 +0000294 Value *d = 0;
Chris Lattner30c89792001-09-07 16:35:17 +0000295 vector<ValueList> *LateResolver = (CurMeth.CurrentMethod) ?
296 &CurMeth.LateResolveValues : &CurModule.LateResolveValues;
Chris Lattner93750fa2001-07-28 17:48:55 +0000297
Chris Lattnerb973dd72001-10-03 14:59:05 +0000298 if (isa<MethodType>(Ty))
299 ThrowException("Methods are not values and must be referenced as pointers");
300
Chris Lattneref9c23f2001-10-03 14:53:21 +0000301 if (const PointerType *PTy = dyn_cast<PointerType>(Ty))
302 if (const MethodType *MTy = dyn_cast<MethodType>(PTy->getValueType()))
303 Ty = MTy; // Convert pointer to method to method type
304
Chris Lattner30c89792001-09-07 16:35:17 +0000305 switch (Ty->getPrimitiveID()) {
306 case Type::LabelTyID: d = new BBPlaceHolder(Ty, D); break;
307 case Type::MethodTyID: d = new MethPlaceHolder(Ty, D);
Chris Lattner93750fa2001-07-28 17:48:55 +0000308 LateResolver = &CurModule.LateResolveValues; break;
Chris Lattner30c89792001-09-07 16:35:17 +0000309 default: d = new ValuePlaceHolder(Ty, D); break;
Chris Lattner00950542001-06-06 20:29:01 +0000310 }
311
312 assert(d != 0 && "How did we not make something?");
Chris Lattner93750fa2001-07-28 17:48:55 +0000313 InsertValue(d, *LateResolver);
Chris Lattner00950542001-06-06 20:29:01 +0000314 return d;
315}
316
317
318//===----------------------------------------------------------------------===//
319// Code to handle forward references in instructions
320//===----------------------------------------------------------------------===//
321//
322// This code handles the late binding needed with statements that reference
323// values not defined yet... for example, a forward branch, or the PHI node for
324// a loop body.
325//
326// This keeps a table (CurMeth.LateResolveValues) of all such forward references
327// and back patchs after we are done.
328//
329
330// ResolveDefinitions - If we could not resolve some defs at parsing
331// time (forward branches, phi functions for loops, etc...) resolve the
332// defs now...
333//
334static void ResolveDefinitions(vector<ValueList> &LateResolvers) {
335 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
336 for (unsigned ty = 0; ty < LateResolvers.size(); ty++) {
337 while (!LateResolvers[ty].empty()) {
338 Value *V = LateResolvers[ty].back();
339 LateResolvers[ty].pop_back();
340 ValID &DID = getValIDFromPlaceHolder(V);
341
342 Value *TheRealValue = getVal(Type::getUniqueIDType(ty), DID, true);
343
Chris Lattner30c89792001-09-07 16:35:17 +0000344 if (TheRealValue == 0) {
345 if (DID.Type == 1)
346 ThrowException("Reference to an invalid definition: '" +DID.getName()+
347 "' of type '" + V->getType()->getDescription() + "'",
348 getLineNumFromPlaceHolder(V));
349 else
350 ThrowException("Reference to an invalid definition: #" +
351 itostr(DID.Num) + " of type '" +
352 V->getType()->getDescription() + "'",
353 getLineNumFromPlaceHolder(V));
354 }
355
Chris Lattnercfe26c92001-10-01 18:26:53 +0000356 assert(!isa<Type>(V) && "Types should be in LateResolveTypes!");
Chris Lattner00950542001-06-06 20:29:01 +0000357
358 V->replaceAllUsesWith(TheRealValue);
Chris Lattner00950542001-06-06 20:29:01 +0000359 delete V;
360 }
361 }
362
363 LateResolvers.clear();
364}
365
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000366// ResolveType - Take a specified unresolved type and resolve it. If there is
367// nothing to resolve it to yet, return true. Otherwise resolve it and return
368// false.
369//
370static bool ResolveType(PATypeHolder<Type> &T) {
371 const Type *Ty = T;
372 ValID &DID = getValIDFromPlaceHolder(Ty);
373
374 const Type *TheRealType = getTypeVal(DID, true);
375 if (TheRealType == 0) return true;
376
377 // Refine the opaque type we had to the new type we are getting.
378 cast<DerivedType>(Ty)->refineAbstractTypeTo(TheRealType);
379 return false;
380}
381
Chris Lattner30c89792001-09-07 16:35:17 +0000382
383// ResolveTypes - This goes through the forward referenced type table and makes
384// sure that all type references are complete. This code is executed after the
385// constant pool of a method or module is completely parsed.
Chris Lattner00950542001-06-06 20:29:01 +0000386//
Chris Lattner30c89792001-09-07 16:35:17 +0000387static void ResolveTypes(vector<PATypeHolder<Type> > &LateResolveTypes) {
388 while (!LateResolveTypes.empty()) {
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000389 if (ResolveType(LateResolveTypes.back())) {
390 const Type *Ty = LateResolveTypes.back();
391 ValID &DID = getValIDFromPlaceHolder(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000392
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000393 if (DID.Type == ValID::NameVal)
Chris Lattner30c89792001-09-07 16:35:17 +0000394 ThrowException("Reference to an invalid type: '" +DID.getName(),
395 getLineNumFromPlaceHolder(Ty));
396 else
397 ThrowException("Reference to an invalid type: #" + itostr(DID.Num),
398 getLineNumFromPlaceHolder(Ty));
Chris Lattner00950542001-06-06 20:29:01 +0000399 }
Chris Lattner30c89792001-09-07 16:35:17 +0000400
Chris Lattner30c89792001-09-07 16:35:17 +0000401 // No need to delete type, refine does that for us.
402 LateResolveTypes.pop_back();
403 }
404}
405
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000406
407// ResolveSomeTypes - This goes through the forward referenced type table and
408// completes references that are now done. This is so that types are
409// immediately resolved to be as concrete as possible. This does not cause
410// thrown exceptions if not everything is resolved.
411//
412static void ResolveSomeTypes(vector<PATypeHolder<Type> > &LateResolveTypes) {
413 for (unsigned i = 0; i < LateResolveTypes.size(); ) {
414 if (ResolveType(LateResolveTypes[i]))
415 ++i; // Type didn't resolve
416 else
417 LateResolveTypes.erase(LateResolveTypes.begin()+i); // Type resolved!
418 }
419}
420
421
Chris Lattner1781aca2001-09-18 04:00:54 +0000422// setValueName - Set the specified value to the name given. The name may be
423// null potentially, in which case this is a noop. The string passed in is
424// assumed to be a malloc'd string buffer, and is freed by this function.
425//
426static void setValueName(Value *V, char *NameStr) {
427 if (NameStr == 0) return;
428 string Name(NameStr); // Copy string
429 free(NameStr); // Free old string
430
Chris Lattner30c89792001-09-07 16:35:17 +0000431 SymbolTable *ST = CurMeth.CurrentMethod ?
432 CurMeth.CurrentMethod->getSymbolTableSure() :
433 CurModule.CurrentModule->getSymbolTableSure();
434
435 Value *Existing = ST->lookup(V->getType(), Name);
436 if (Existing) { // Inserting a name that is already defined???
437 // There is only one case where this is allowed: when we are refining an
438 // opaque type. In this case, Existing will be an opaque type.
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000439 if (const Type *Ty = dyn_cast<const Type>(Existing)) {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000440 if (OpaqueType *OpTy = dyn_cast<OpaqueType>(Ty)) {
Chris Lattner30c89792001-09-07 16:35:17 +0000441 // We ARE replacing an opaque type!
Chris Lattnerb00c5822001-10-02 03:41:24 +0000442 OpTy->refineAbstractTypeTo(cast<Type>(V));
Chris Lattner30c89792001-09-07 16:35:17 +0000443 return;
444 }
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000445 }
Chris Lattner30c89792001-09-07 16:35:17 +0000446
Chris Lattner9636a912001-10-01 16:18:37 +0000447 // Otherwise, we are a simple redefinition of a value, check to see if it
448 // is defined the same as the old one...
449 if (const Type *Ty = dyn_cast<const Type>(Existing)) {
450 if (Ty == cast<const Type>(V)) return; // Yes, it's equal.
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000451 cerr << "Type: " << Ty->getDescription() << " != "
452 << cast<const Type>(V)->getDescription() << "!\n";
Chris Lattner9636a912001-10-01 16:18:37 +0000453 } else {
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000454
Chris Lattner9636a912001-10-01 16:18:37 +0000455 }
Chris Lattner30c89792001-09-07 16:35:17 +0000456 ThrowException("Redefinition of value name '" + Name + "' in the '" +
457 V->getType()->getDescription() + "' type plane!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000458 }
Chris Lattner00950542001-06-06 20:29:01 +0000459
Chris Lattner30c89792001-09-07 16:35:17 +0000460 V->setName(Name, ST);
Chris Lattner00950542001-06-06 20:29:01 +0000461}
462
Chris Lattner8896eda2001-07-09 19:38:36 +0000463
Chris Lattner30c89792001-09-07 16:35:17 +0000464//===----------------------------------------------------------------------===//
465// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000466//
Chris Lattner8896eda2001-07-09 19:38:36 +0000467
Chris Lattner30c89792001-09-07 16:35:17 +0000468// TypeContains - Returns true if Ty contains E in it.
469//
470static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner3ff43872001-09-28 22:56:31 +0000471 return find(df_begin(Ty), df_end(Ty), E) != df_end(Ty);
Chris Lattner30c89792001-09-07 16:35:17 +0000472}
Chris Lattner698b56e2001-07-20 19:15:08 +0000473
Chris Lattner30c89792001-09-07 16:35:17 +0000474
475static vector<pair<unsigned, OpaqueType *> > UpRefs;
476
477static PATypeHolder<Type> HandleUpRefs(const Type *ty) {
478 PATypeHolder<Type> Ty(ty);
479 UR_OUT(UpRefs.size() << " upreferences active!\n");
480 for (unsigned i = 0; i < UpRefs.size(); ) {
481 UR_OUT("TypeContains(" << Ty->getDescription() << ", "
482 << UpRefs[i].second->getDescription() << ") = "
483 << TypeContains(Ty, UpRefs[i].second) << endl);
484 if (TypeContains(Ty, UpRefs[i].second)) {
485 unsigned Level = --UpRefs[i].first; // Decrement level of upreference
486 UR_OUT("Uplevel Ref Level = " << Level << endl);
487 if (Level == 0) { // Upreference should be resolved!
488 UR_OUT("About to resolve upreference!\n";
489 string OldName = UpRefs[i].second->getDescription());
490 UpRefs[i].second->refineAbstractTypeTo(Ty);
491 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
492 UR_OUT("Type '" << OldName << "' refined upreference to: "
493 << (const void*)Ty << ", " << Ty->getDescription() << endl);
494 continue;
495 }
496 }
497
498 ++i; // Otherwise, no resolve, move on...
Chris Lattner8896eda2001-07-09 19:38:36 +0000499 }
Chris Lattner30c89792001-09-07 16:35:17 +0000500 // FIXME: TODO: this should return the updated type
Chris Lattner8896eda2001-07-09 19:38:36 +0000501 return Ty;
502}
503
Chris Lattner30c89792001-09-07 16:35:17 +0000504template <class TypeTy>
505inline static void TypeDone(PATypeHolder<TypeTy> *Ty) {
506 if (UpRefs.size())
507 ThrowException("Invalid upreference in type: " + (*Ty)->getDescription());
508}
509
510// newTH - Allocate a new type holder for the specified type
511template <class TypeTy>
512inline static PATypeHolder<TypeTy> *newTH(const TypeTy *Ty) {
513 return new PATypeHolder<TypeTy>(Ty);
514}
515template <class TypeTy>
516inline static PATypeHolder<TypeTy> *newTH(const PATypeHolder<TypeTy> &TH) {
517 return new PATypeHolder<TypeTy>(TH);
518}
519
520
Chris Lattner00950542001-06-06 20:29:01 +0000521//===----------------------------------------------------------------------===//
522// RunVMAsmParser - Define an interface to this parser
523//===----------------------------------------------------------------------===//
524//
Chris Lattnera2850432001-07-22 18:36:00 +0000525Module *RunVMAsmParser(const string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000526 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000527 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000528 llvmAsmlineno = 1; // Reset the current line number...
529
530 CurModule.CurrentModule = new Module(); // Allocate a new module to read
531 yyparse(); // Parse the file.
532 Module *Result = ParserResult;
Chris Lattner00950542001-06-06 20:29:01 +0000533 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
534 ParserResult = 0;
535
536 return Result;
537}
538
539%}
540
541%union {
Chris Lattner30c89792001-09-07 16:35:17 +0000542 Module *ModuleVal;
543 Method *MethodVal;
544 MethodArgument *MethArgVal;
545 BasicBlock *BasicBlockVal;
546 TerminatorInst *TermInstVal;
547 Instruction *InstVal;
548 ConstPoolVal *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000549
Chris Lattner30c89792001-09-07 16:35:17 +0000550 const Type *PrimType;
551 PATypeHolder<Type> *TypeVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000552 Value *ValueVal;
553
554 list<MethodArgument*> *MethodArgList;
555 list<Value*> *ValueList;
556 list<PATypeHolder<Type> > *TypeList;
Chris Lattnerc24d2082001-06-11 15:04:20 +0000557 list<pair<Value*, BasicBlock*> > *PHIList; // Represent the RHS of PHI node
Chris Lattner00950542001-06-06 20:29:01 +0000558 list<pair<ConstPoolVal*, BasicBlock*> > *JumpTable;
Chris Lattner30c89792001-09-07 16:35:17 +0000559 vector<ConstPoolVal*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000560
Chris Lattner30c89792001-09-07 16:35:17 +0000561 int64_t SInt64Val;
562 uint64_t UInt64Val;
563 int SIntVal;
564 unsigned UIntVal;
565 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000566 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000567
Chris Lattner30c89792001-09-07 16:35:17 +0000568 char *StrVal; // This memory is strdup'd!
569 ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000570
Chris Lattner30c89792001-09-07 16:35:17 +0000571 Instruction::UnaryOps UnaryOpVal;
572 Instruction::BinaryOps BinaryOpVal;
573 Instruction::TermOps TermOpVal;
574 Instruction::MemoryOps MemOpVal;
575 Instruction::OtherOps OtherOpVal;
Chris Lattner00950542001-06-06 20:29:01 +0000576}
577
578%type <ModuleVal> Module MethodList
Chris Lattnere1815642001-07-15 06:35:53 +0000579%type <MethodVal> Method MethodProto MethodHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000580%type <BasicBlockVal> BasicBlock InstructionList
581%type <TermInstVal> BBTerminatorInst
582%type <InstVal> Inst InstVal MemoryInst
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000583%type <ConstVal> ConstVal
Chris Lattner027dcc52001-07-08 21:10:27 +0000584%type <ConstVector> ConstVector UByteList
Chris Lattner00950542001-06-06 20:29:01 +0000585%type <MethodArgList> ArgList ArgListH
586%type <MethArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000587%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000588%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner30c89792001-09-07 16:35:17 +0000589%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000590%type <JumpTable> JumpTable
Chris Lattner1781aca2001-09-18 04:00:54 +0000591%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner00950542001-06-06 20:29:01 +0000592
593%type <ValIDVal> ValueRef ConstValueRef // Reference to a definition or BB
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000594%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000595// Tokens and types for handling constant integer values
596//
597// ESINT64VAL - A negative number within long long range
598%token <SInt64Val> ESINT64VAL
599
600// EUINT64VAL - A positive number within uns. long long range
601%token <UInt64Val> EUINT64VAL
602%type <SInt64Val> EINT64VAL
603
604%token <SIntVal> SINTVAL // Signed 32 bit ints...
605%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
606%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000607%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000608
609// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000610%type <TypeVal> Types TypesV UpRTypes UpRTypesV
611%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
612%token <TypeVal> OPAQUE
613%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
614%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000615
616%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
617%type <StrVal> OptVAR_ID OptAssign
618
619
Chris Lattner1781aca2001-09-18 04:00:54 +0000620%token IMPLEMENTATION TRUE FALSE BEGINTOK END DECLARE GLOBAL CONSTANT UNINIT
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000621%token TO DOTDOTDOT STRING NULL_TOK CONST
Chris Lattner00950542001-06-06 20:29:01 +0000622
623// Basic Block Terminating Operators
624%token <TermOpVal> RET BR SWITCH
625
626// Unary Operators
627%type <UnaryOpVal> UnaryOps // all the unary operators
Chris Lattner71496b32001-07-08 19:03:27 +0000628%token <UnaryOpVal> NOT
Chris Lattner00950542001-06-06 20:29:01 +0000629
630// Binary Operators
631%type <BinaryOpVal> BinaryOps // all the binary operators
632%token <BinaryOpVal> ADD SUB MUL DIV REM
Chris Lattner027dcc52001-07-08 21:10:27 +0000633%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000634
635// Memory Instructions
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000636%token <MemoryOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000637
Chris Lattner027dcc52001-07-08 21:10:27 +0000638// Other Operators
639%type <OtherOpVal> ShiftOps
640%token <OtherOpVal> PHI CALL CAST SHL SHR
641
Chris Lattner00950542001-06-06 20:29:01 +0000642%start Module
643%%
644
645// Handle constant integer size restriction and conversion...
646//
647
648INTVAL : SINTVAL
649INTVAL : UINTVAL {
650 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
651 ThrowException("Value too large for type!");
652 $$ = (int32_t)$1;
653}
654
655
656EINT64VAL : ESINT64VAL // These have same type and can't cause problems...
657EINT64VAL : EUINT64VAL {
658 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
659 ThrowException("Value too large for type!");
660 $$ = (int64_t)$1;
661}
662
Chris Lattner00950542001-06-06 20:29:01 +0000663// Operations that are notably excluded from this list include:
664// RET, BR, & SWITCH because they end basic blocks and are treated specially.
665//
Chris Lattner09083092001-07-08 04:57:15 +0000666UnaryOps : NOT
Chris Lattner00950542001-06-06 20:29:01 +0000667BinaryOps : ADD | SUB | MUL | DIV | REM
668BinaryOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE
Chris Lattner027dcc52001-07-08 21:10:27 +0000669ShiftOps : SHL | SHR
Chris Lattner00950542001-06-06 20:29:01 +0000670
Chris Lattnere98dda62001-07-14 06:10:16 +0000671// These are some types that allow classification if we only want a particular
672// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner00950542001-06-06 20:29:01 +0000673SIntType : LONG | INT | SHORT | SBYTE
674UIntType : ULONG | UINT | USHORT | UBYTE
Chris Lattner30c89792001-09-07 16:35:17 +0000675IntType : SIntType | UIntType
676FPType : FLOAT | DOUBLE
Chris Lattner00950542001-06-06 20:29:01 +0000677
Chris Lattnere98dda62001-07-14 06:10:16 +0000678// OptAssign - Value producing statements have an optional assignment component
Chris Lattner00950542001-06-06 20:29:01 +0000679OptAssign : VAR_ID '=' {
680 $$ = $1;
681 }
682 | /*empty*/ {
683 $$ = 0;
684 }
685
Chris Lattner30c89792001-09-07 16:35:17 +0000686
687//===----------------------------------------------------------------------===//
688// Types includes all predefined types... except void, because it can only be
689// used in specific contexts (method returning void for example). To have
690// access to it, a user must explicitly use TypesV.
691//
692
693// TypesV includes all of 'Types', but it also includes the void type.
694TypesV : Types | VOID { $$ = newTH($1); }
695UpRTypesV : UpRTypes | VOID { $$ = newTH($1); }
696
697Types : UpRTypes {
698 TypeDone($$ = $1);
699 }
700
701
702// Derived types are added later...
703//
704PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT
705PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL
706UpRTypes : OPAQUE | PrimType { $$ = newTH($1); }
707UpRTypes : ValueRef { // Named types are also simple types...
708 $$ = newTH(getTypeVal($1));
709}
710
Chris Lattner30c89792001-09-07 16:35:17 +0000711// Include derived types in the Types production.
712//
713UpRTypes : '\\' EUINT64VAL { // Type UpReference
714 if ($2 > (uint64_t)INT64_MAX) ThrowException("Value out of range!");
715 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
716 UpRefs.push_back(make_pair((unsigned)$2, OT)); // Add to vector...
717 $$ = newTH<Type>(OT);
718 UR_OUT("New Upreference!\n");
719 }
720 | UpRTypesV '(' ArgTypeListI ')' { // Method derived type?
721 vector<const Type*> Params;
722 mapto($3->begin(), $3->end(), back_inserter(Params),
723 mem_fun_ref(&PATypeHandle<Type>::get));
724 $$ = newTH(HandleUpRefs(MethodType::get(*$1, Params)));
725 delete $3; // Delete the argument list
726 delete $1; // Delete the old type handle
727 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000728 | '[' UpRTypesV ']' { // Unsized array type?
729 $$ = newTH<Type>(HandleUpRefs(ArrayType::get(*$2)));
730 delete $2;
Chris Lattner30c89792001-09-07 16:35:17 +0000731 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000732 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
733 $$ = newTH<Type>(HandleUpRefs(ArrayType::get(*$4, (int)$2)));
734 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +0000735 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000736 | '{' TypeListI '}' { // Structure type?
737 vector<const Type*> Elements;
738 mapto($2->begin(), $2->end(), back_inserter(Elements),
739 mem_fun_ref(&PATypeHandle<Type>::get));
Chris Lattner30c89792001-09-07 16:35:17 +0000740
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000741 $$ = newTH<Type>(HandleUpRefs(StructType::get(Elements)));
742 delete $2;
743 }
744 | '{' '}' { // Empty structure type?
745 $$ = newTH<Type>(StructType::get(vector<const Type*>()));
746 }
747 | UpRTypes '*' { // Pointer type?
748 $$ = newTH<Type>(HandleUpRefs(PointerType::get(*$1)));
749 delete $1;
750 }
Chris Lattner30c89792001-09-07 16:35:17 +0000751
752// TypeList - Used for struct declarations and as a basis for method type
753// declaration type lists
754//
755TypeListI : UpRTypes {
756 $$ = new list<PATypeHolder<Type> >();
757 $$->push_back(*$1); delete $1;
758 }
759 | TypeListI ',' UpRTypes {
760 ($$=$1)->push_back(*$3); delete $3;
761 }
762
763// ArgTypeList - List of types for a method type declaration...
764ArgTypeListI : TypeListI
765 | TypeListI ',' DOTDOTDOT {
766 ($$=$1)->push_back(Type::VoidTy);
767 }
768 | DOTDOTDOT {
769 ($$ = new list<PATypeHolder<Type> >())->push_back(Type::VoidTy);
770 }
771 | /*empty*/ {
772 $$ = new list<PATypeHolder<Type> >();
773 }
774
775
Chris Lattnere98dda62001-07-14 06:10:16 +0000776// ConstVal - The various declarations that go into the constant pool. This
777// includes all forward declarations of types, constants, and functions.
778//
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000779ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
780 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
781 if (ATy == 0)
782 ThrowException("Cannot make array constant with type: '" +
783 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +0000784 const Type *ETy = ATy->getElementType();
785 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +0000786
Chris Lattner30c89792001-09-07 16:35:17 +0000787 // Verify that we have the correct size...
788 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +0000789 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattner30c89792001-09-07 16:35:17 +0000790 utostr($3->size()) + " arguments, but has size of " +
791 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +0000792
Chris Lattner30c89792001-09-07 16:35:17 +0000793 // Verify all elements are correct type!
794 for (unsigned i = 0; i < $3->size(); i++) {
795 if (ETy != (*$3)[i]->getType())
Chris Lattner00950542001-06-06 20:29:01 +0000796 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000797 ETy->getName() + "' as required!\nIt is of type '" +
798 (*$3)[i]->getType()->getName() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +0000799 }
800
Chris Lattner30c89792001-09-07 16:35:17 +0000801 $$ = ConstPoolArray::get(ATy, *$3);
802 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000803 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000804 | Types '[' ']' {
805 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
806 if (ATy == 0)
807 ThrowException("Cannot make array constant with type: '" +
808 (*$1)->getDescription() + "'!");
809
810 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +0000811 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +0000812 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattner30c89792001-09-07 16:35:17 +0000813 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000814 $$ = ConstPoolArray::get(ATy, vector<ConstPoolVal*>());
Chris Lattner30c89792001-09-07 16:35:17 +0000815 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +0000816 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000817 | Types 'c' STRINGCONSTANT {
818 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
819 if (ATy == 0)
820 ThrowException("Cannot make array constant with type: '" +
821 (*$1)->getDescription() + "'!");
822
Chris Lattner30c89792001-09-07 16:35:17 +0000823 int NumElements = ATy->getNumElements();
824 const Type *ETy = ATy->getElementType();
825 char *EndStr = UnEscapeLexed($3, true);
826 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +0000827 ThrowException("Can't build string constant of size " +
Chris Lattner30c89792001-09-07 16:35:17 +0000828 itostr((int)(EndStr-$3)) +
829 " when array has size " + itostr(NumElements) + "!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000830 vector<ConstPoolVal*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +0000831 if (ETy == Type::SByteTy) {
832 for (char *C = $3; C != EndStr; ++C)
833 Vals.push_back(ConstPoolSInt::get(ETy, *C));
834 } else if (ETy == Type::UByteTy) {
835 for (char *C = $3; C != EndStr; ++C)
836 Vals.push_back(ConstPoolUInt::get(ETy, *C));
Chris Lattner93750fa2001-07-28 17:48:55 +0000837 } else {
Chris Lattner30c89792001-09-07 16:35:17 +0000838 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +0000839 ThrowException("Cannot build string arrays of non byte sized elements!");
840 }
Chris Lattner30c89792001-09-07 16:35:17 +0000841 free($3);
842 $$ = ConstPoolArray::get(ATy, Vals);
843 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +0000844 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000845 | Types '{' ConstVector '}' {
846 const StructType *STy = dyn_cast<const StructType>($1->get());
847 if (STy == 0)
848 ThrowException("Cannot make struct constant with type: '" +
849 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +0000850 // FIXME: TODO: Check to see that the constants are compatible with the type
851 // initializer!
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000852 $$ = ConstPoolStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +0000853 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000854 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000855 | Types NULL_TOK {
856 const PointerType *PTy = dyn_cast<const PointerType>($1->get());
857 if (PTy == 0)
858 ThrowException("Cannot make null pointer constant with type: '" +
859 (*$1)->getDescription() + "'!");
860
861 $$ = ConstPoolPointer::getNullPointer(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000862 delete $1;
863 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000864 | Types VAR_ID {
865 string Name($2); free($2); // Change to a responsible mem manager
866 const PointerType *Ty = dyn_cast<const PointerType>($1->get());
867 if (Ty == 0)
868 ThrowException("Global const reference must be a pointer type!");
869
870 Value *N = lookupInSymbolTable(Ty, Name);
871 if (N == 0)
872 ThrowException("Global pointer reference '%" + Name +
873 "' must be defined before use!");
874
875 // TODO FIXME: This should also allow methods... when common baseclass
876 // exists
877 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(N)) {
878 $$ = ConstPoolPointerReference::get(GV);
879 } else {
880 ThrowException("'%" + Name + "' is not a global value reference!");
881 }
882
883 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +0000884 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000885
Chris Lattner00950542001-06-06 20:29:01 +0000886
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000887ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000888 if (!ConstPoolSInt::isValueValidForType($1, $2))
889 ThrowException("Constant value doesn't fit in type!");
Chris Lattner30c89792001-09-07 16:35:17 +0000890 $$ = ConstPoolSInt::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000891 }
892 | UIntType EUINT64VAL { // integral constants
893 if (!ConstPoolUInt::isValueValidForType($1, $2))
894 ThrowException("Constant value doesn't fit in type!");
Chris Lattner30c89792001-09-07 16:35:17 +0000895 $$ = ConstPoolUInt::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000896 }
897 | BOOL TRUE { // Boolean constants
Chris Lattner30c89792001-09-07 16:35:17 +0000898 $$ = ConstPoolBool::True;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000899 }
900 | BOOL FALSE { // Boolean constants
Chris Lattner30c89792001-09-07 16:35:17 +0000901 $$ = ConstPoolBool::False;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000902 }
903 | FPType FPVAL { // Float & Double constants
Chris Lattner30c89792001-09-07 16:35:17 +0000904 $$ = ConstPoolFP::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000905 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000906
Chris Lattnere98dda62001-07-14 06:10:16 +0000907// ConstVector - A list of comma seperated constants.
Chris Lattner00950542001-06-06 20:29:01 +0000908ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +0000909 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +0000910 }
911 | ConstVal {
912 $$ = new vector<ConstPoolVal*>();
Chris Lattner30c89792001-09-07 16:35:17 +0000913 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +0000914 }
915
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000916
Chris Lattner1781aca2001-09-18 04:00:54 +0000917// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
918GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; }
919
Chris Lattner00950542001-06-06 20:29:01 +0000920
Chris Lattnere98dda62001-07-14 06:10:16 +0000921// ConstPool - Constants with optional names assigned to them.
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000922ConstPool : ConstPool OptAssign CONST ConstVal {
923 setValueName($4, $2);
924 InsertValue($4);
Chris Lattner00950542001-06-06 20:29:01 +0000925 }
Chris Lattner30c89792001-09-07 16:35:17 +0000926 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner1781aca2001-09-18 04:00:54 +0000927 // TODO: FIXME when Type are not const
928 setValueName(const_cast<Type*>($4->get()), $2);
929
930 if (!$2) {
Chris Lattner30c89792001-09-07 16:35:17 +0000931 InsertType($4->get(),
932 CurMeth.CurrentMethod ? CurMeth.Types : CurModule.Types);
933 }
934 delete $4;
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000935
936 ResolveSomeTypes(CurMeth.CurrentMethod ? CurMeth.LateResolveTypes :
937 CurModule.LateResolveTypes);
Chris Lattner30c89792001-09-07 16:35:17 +0000938 }
939 | ConstPool MethodProto { // Method prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +0000940 }
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000941 | ConstPool OptAssign GlobalType ConstVal {
Chris Lattner1781aca2001-09-18 04:00:54 +0000942 const Type *Ty = $4->getType();
943 // Global declarations appear in Constant Pool
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000944 ConstPoolVal *Initializer = $4;
Chris Lattner1781aca2001-09-18 04:00:54 +0000945 if (Initializer == 0)
946 ThrowException("Global value initializer is not a constant!");
947
Chris Lattneref9c23f2001-10-03 14:53:21 +0000948 GlobalVariable *GV = new GlobalVariable(Ty, $3, Initializer);
Chris Lattner1781aca2001-09-18 04:00:54 +0000949 setValueName(GV, $2);
950
951 CurModule.CurrentModule->getGlobalList().push_back(GV);
952 InsertValue(GV, CurModule.Values);
953 }
954 | ConstPool OptAssign UNINIT GlobalType Types {
955 const Type *Ty = *$5;
956 // Global declarations appear in Constant Pool
Chris Lattnercfe26c92001-10-01 18:26:53 +0000957 if (isa<ArrayType>(Ty) && cast<ArrayType>(Ty)->isUnsized()) {
Chris Lattner1781aca2001-09-18 04:00:54 +0000958 ThrowException("Type '" + Ty->getDescription() +
959 "' is not a sized type!");
Chris Lattner70cc3392001-09-10 07:58:01 +0000960 }
Chris Lattner1781aca2001-09-18 04:00:54 +0000961
Chris Lattneref9c23f2001-10-03 14:53:21 +0000962 GlobalVariable *GV = new GlobalVariable(Ty, $4);
Chris Lattner1781aca2001-09-18 04:00:54 +0000963 setValueName(GV, $2);
964
Chris Lattner70cc3392001-09-10 07:58:01 +0000965 CurModule.CurrentModule->getGlobalList().push_back(GV);
966 InsertValue(GV, CurModule.Values);
Chris Lattnere98dda62001-07-14 06:10:16 +0000967 }
Chris Lattner00950542001-06-06 20:29:01 +0000968 | /* empty: end of list */ {
969 }
970
971
972//===----------------------------------------------------------------------===//
973// Rules to match Modules
974//===----------------------------------------------------------------------===//
975
976// Module rule: Capture the result of parsing the whole file into a result
977// variable...
978//
979Module : MethodList {
980 $$ = ParserResult = $1;
981 CurModule.ModuleDone();
982}
983
Chris Lattnere98dda62001-07-14 06:10:16 +0000984// MethodList - A list of methods, preceeded by a constant pool.
985//
Chris Lattner00950542001-06-06 20:29:01 +0000986MethodList : MethodList Method {
Chris Lattner00950542001-06-06 20:29:01 +0000987 $$ = $1;
Chris Lattnere1815642001-07-15 06:35:53 +0000988 if (!$2->getParent())
989 $1->getMethodList().push_back($2);
990 CurMeth.MethodDone();
Chris Lattner00950542001-06-06 20:29:01 +0000991 }
Chris Lattnere1815642001-07-15 06:35:53 +0000992 | MethodList MethodProto {
993 $$ = $1;
Chris Lattnere1815642001-07-15 06:35:53 +0000994 }
Chris Lattner00950542001-06-06 20:29:01 +0000995 | ConstPool IMPLEMENTATION {
996 $$ = CurModule.CurrentModule;
Chris Lattner30c89792001-09-07 16:35:17 +0000997 // Resolve circular types before we parse the body of the module
998 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner00950542001-06-06 20:29:01 +0000999 }
1000
1001
1002//===----------------------------------------------------------------------===//
1003// Rules to match Method Headers
1004//===----------------------------------------------------------------------===//
1005
1006OptVAR_ID : VAR_ID | /*empty*/ { $$ = 0; }
1007
1008ArgVal : Types OptVAR_ID {
Chris Lattner30c89792001-09-07 16:35:17 +00001009 $$ = new MethodArgument(*$1); delete $1;
Chris Lattner1781aca2001-09-18 04:00:54 +00001010 setValueName($$, $2);
Chris Lattner00950542001-06-06 20:29:01 +00001011}
1012
1013ArgListH : ArgVal ',' ArgListH {
1014 $$ = $3;
1015 $3->push_front($1);
1016 }
1017 | ArgVal {
1018 $$ = new list<MethodArgument*>();
1019 $$->push_front($1);
1020 }
Chris Lattner8b81bf52001-07-25 22:47:46 +00001021 | DOTDOTDOT {
1022 $$ = new list<MethodArgument*>();
1023 $$->push_back(new MethodArgument(Type::VoidTy));
1024 }
Chris Lattner00950542001-06-06 20:29:01 +00001025
1026ArgList : ArgListH {
1027 $$ = $1;
1028 }
1029 | /* empty */ {
1030 $$ = 0;
1031 }
1032
1033MethodHeaderH : TypesV STRINGCONSTANT '(' ArgList ')' {
Chris Lattner93750fa2001-07-28 17:48:55 +00001034 UnEscapeLexed($2);
Chris Lattner30c89792001-09-07 16:35:17 +00001035 vector<const Type*> ParamTypeList;
Chris Lattner00950542001-06-06 20:29:01 +00001036 if ($4)
Chris Lattner7fc9fe32001-06-27 23:41:11 +00001037 for (list<MethodArgument*>::iterator I = $4->begin(); I != $4->end(); ++I)
Chris Lattner00950542001-06-06 20:29:01 +00001038 ParamTypeList.push_back((*I)->getType());
1039
Chris Lattneref9c23f2001-10-03 14:53:21 +00001040 const MethodType *MT = MethodType::get(*$1, ParamTypeList);
1041 const PointerType *PMT = PointerType::get(MT);
Chris Lattner30c89792001-09-07 16:35:17 +00001042 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001043
Chris Lattnere1815642001-07-15 06:35:53 +00001044 Method *M = 0;
1045 if (SymbolTable *ST = CurModule.CurrentModule->getSymbolTable()) {
Chris Lattneref9c23f2001-10-03 14:53:21 +00001046 if (Value *V = ST->lookup(PMT, $2)) { // Method already in symtab?
1047 M = cast<Method>(V);
Chris Lattner00950542001-06-06 20:29:01 +00001048
Chris Lattnere1815642001-07-15 06:35:53 +00001049 // Yes it is. If this is the case, either we need to be a forward decl,
1050 // or it needs to be.
1051 if (!CurMeth.isDeclare && !M->isExternal())
1052 ThrowException("Redefinition of method '" + string($2) + "'!");
1053 }
1054 }
1055
1056 if (M == 0) { // Not already defined?
1057 M = new Method(MT, $2);
1058 InsertValue(M, CurModule.Values);
1059 }
1060
1061 free($2); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +00001062
1063 CurMeth.MethodStart(M);
1064
1065 // Add all of the arguments we parsed to the method...
Chris Lattnere1815642001-07-15 06:35:53 +00001066 if ($4 && !CurMeth.isDeclare) { // Is null if empty...
Chris Lattner00950542001-06-06 20:29:01 +00001067 Method::ArgumentListType &ArgList = M->getArgumentList();
1068
Chris Lattner7fc9fe32001-06-27 23:41:11 +00001069 for (list<MethodArgument*>::iterator I = $4->begin(); I != $4->end(); ++I) {
Chris Lattner00950542001-06-06 20:29:01 +00001070 InsertValue(*I);
1071 ArgList.push_back(*I);
1072 }
1073 delete $4; // We're now done with the argument list
1074 }
1075}
1076
1077MethodHeader : MethodHeaderH ConstPool BEGINTOK {
1078 $$ = CurMeth.CurrentMethod;
Chris Lattner30c89792001-09-07 16:35:17 +00001079
1080 // Resolve circular types before we parse the body of the method.
1081 ResolveTypes(CurMeth.LateResolveTypes);
Chris Lattner00950542001-06-06 20:29:01 +00001082}
1083
1084Method : BasicBlockList END {
1085 $$ = $1;
1086}
1087
Chris Lattnere1815642001-07-15 06:35:53 +00001088MethodProto : DECLARE { CurMeth.isDeclare = true; } MethodHeaderH {
1089 $$ = CurMeth.CurrentMethod;
Chris Lattner93750fa2001-07-28 17:48:55 +00001090 if (!$$->getParent())
1091 CurModule.CurrentModule->getMethodList().push_back($$);
1092 CurMeth.MethodDone();
Chris Lattnere1815642001-07-15 06:35:53 +00001093}
Chris Lattner00950542001-06-06 20:29:01 +00001094
1095//===----------------------------------------------------------------------===//
1096// Rules to match Basic Blocks
1097//===----------------------------------------------------------------------===//
1098
1099ConstValueRef : ESINT64VAL { // A reference to a direct constant
1100 $$ = ValID::create($1);
1101 }
1102 | EUINT64VAL {
1103 $$ = ValID::create($1);
1104 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001105 | FPVAL { // Perhaps it's an FP constant?
1106 $$ = ValID::create($1);
1107 }
Chris Lattner00950542001-06-06 20:29:01 +00001108 | TRUE {
1109 $$ = ValID::create((int64_t)1);
1110 }
1111 | FALSE {
1112 $$ = ValID::create((int64_t)0);
1113 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001114 | NULL_TOK {
1115 $$ = ValID::createNull();
1116 }
1117
Chris Lattner93750fa2001-07-28 17:48:55 +00001118/*
Chris Lattner00950542001-06-06 20:29:01 +00001119 | STRINGCONSTANT { // Quoted strings work too... especially for methods
1120 $$ = ValID::create_conststr($1);
1121 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001122*/
Chris Lattner00950542001-06-06 20:29:01 +00001123
1124// ValueRef - A reference to a definition...
1125ValueRef : INTVAL { // Is it an integer reference...?
1126 $$ = ValID::create($1);
1127 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001128 | VAR_ID { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001129 $$ = ValID::create($1);
1130 }
1131 | ConstValueRef {
1132 $$ = $1;
1133 }
1134
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001135// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1136// type immediately preceeds the value reference, and allows complex constant
1137// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001138ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001139 $$ = getVal(*$1, $2); delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +00001140 }
Chris Lattner8b81bf52001-07-25 22:47:46 +00001141
Chris Lattner00950542001-06-06 20:29:01 +00001142
1143BasicBlockList : BasicBlockList BasicBlock {
1144 $1->getBasicBlocks().push_back($2);
1145 $$ = $1;
1146 }
1147 | MethodHeader BasicBlock { // Do not allow methods with 0 basic blocks
1148 $$ = $1; // in them...
1149 $1->getBasicBlocks().push_back($2);
1150 }
1151
1152
1153// Basic blocks are terminated by branching instructions:
1154// br, br/cc, switch, ret
1155//
1156BasicBlock : InstructionList BBTerminatorInst {
1157 $1->getInstList().push_back($2);
1158 InsertValue($1);
1159 $$ = $1;
1160 }
1161 | LABELSTR InstructionList BBTerminatorInst {
1162 $2->getInstList().push_back($3);
Chris Lattner30c89792001-09-07 16:35:17 +00001163 setValueName($2, $1);
Chris Lattner00950542001-06-06 20:29:01 +00001164
1165 InsertValue($2);
1166 $$ = $2;
1167 }
1168
1169InstructionList : InstructionList Inst {
1170 $1->getInstList().push_back($2);
1171 $$ = $1;
1172 }
1173 | /* empty */ {
1174 $$ = new BasicBlock();
1175 }
1176
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001177BBTerminatorInst : RET ResolvedVal { // Return with a result...
1178 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001179 }
1180 | RET VOID { // Return with no result...
1181 $$ = new ReturnInst();
1182 }
1183 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner9636a912001-10-01 16:18:37 +00001184 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $3)));
Chris Lattner00950542001-06-06 20:29:01 +00001185 } // Conditional Branch...
1186 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner9636a912001-10-01 16:18:37 +00001187 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $6)),
1188 cast<BasicBlock>(getVal(Type::LabelTy, $9)),
Chris Lattner00950542001-06-06 20:29:01 +00001189 getVal(Type::BoolTy, $3));
1190 }
1191 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1192 SwitchInst *S = new SwitchInst(getVal($2, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001193 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
Chris Lattner00950542001-06-06 20:29:01 +00001194 $$ = S;
1195
1196 list<pair<ConstPoolVal*, BasicBlock*> >::iterator I = $8->begin(),
1197 end = $8->end();
Chris Lattner7fc9fe32001-06-27 23:41:11 +00001198 for (; I != end; ++I)
Chris Lattner00950542001-06-06 20:29:01 +00001199 S->dest_push_back(I->first, I->second);
1200 }
1201
1202JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1203 $$ = $1;
Chris Lattnercfe26c92001-10-01 18:26:53 +00001204 ConstPoolVal *V = cast<ConstPoolVal>(getVal($2, $3, true));
Chris Lattner00950542001-06-06 20:29:01 +00001205 if (V == 0)
1206 ThrowException("May only switch on a constant pool value!");
1207
Chris Lattner9636a912001-10-01 16:18:37 +00001208 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($5, $6))));
Chris Lattner00950542001-06-06 20:29:01 +00001209 }
1210 | IntType ConstValueRef ',' LABEL ValueRef {
1211 $$ = new list<pair<ConstPoolVal*, BasicBlock*> >();
Chris Lattnercfe26c92001-10-01 18:26:53 +00001212 ConstPoolVal *V = cast<ConstPoolVal>(getVal($1, $2, true));
Chris Lattner00950542001-06-06 20:29:01 +00001213
1214 if (V == 0)
1215 ThrowException("May only switch on a constant pool value!");
1216
Chris Lattner9636a912001-10-01 16:18:37 +00001217 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($4, $5))));
Chris Lattner00950542001-06-06 20:29:01 +00001218 }
1219
1220Inst : OptAssign InstVal {
Chris Lattner1781aca2001-09-18 04:00:54 +00001221 setValueName($2, $1); // Is this definition named?? if so, assign the name...
Chris Lattner00950542001-06-06 20:29:01 +00001222
1223 InsertValue($2);
1224 $$ = $2;
1225}
1226
Chris Lattnerc24d2082001-06-11 15:04:20 +00001227PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
1228 $$ = new list<pair<Value*, BasicBlock*> >();
Chris Lattner30c89792001-09-07 16:35:17 +00001229 $$->push_back(make_pair(getVal(*$1, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001230 cast<BasicBlock>(getVal(Type::LabelTy, $5))));
Chris Lattner30c89792001-09-07 16:35:17 +00001231 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001232 }
1233 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1234 $$ = $1;
1235 $1->push_back(make_pair(getVal($1->front().first->getType(), $4),
Chris Lattner9636a912001-10-01 16:18:37 +00001236 cast<BasicBlock>(getVal(Type::LabelTy, $6))));
Chris Lattnerc24d2082001-06-11 15:04:20 +00001237 }
1238
1239
Chris Lattner30c89792001-09-07 16:35:17 +00001240ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner00950542001-06-06 20:29:01 +00001241 $$ = new list<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001242 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001243 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001244 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001245 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001246 $1->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001247 }
1248
1249// ValueRefListE - Just like ValueRefList, except that it may also be empty!
1250ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; }
1251
1252InstVal : BinaryOps Types ValueRef ',' ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001253 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001254 if ($$ == 0)
1255 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001256 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001257 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001258 | UnaryOps ResolvedVal {
1259 $$ = UnaryOperator::create($1, $2);
Chris Lattner00950542001-06-06 20:29:01 +00001260 if ($$ == 0)
1261 ThrowException("unary operator returned null!");
Chris Lattner09083092001-07-08 04:57:15 +00001262 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001263 | ShiftOps ResolvedVal ',' ResolvedVal {
1264 if ($4->getType() != Type::UByteTy)
1265 ThrowException("Shift amount must be ubyte!");
1266 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001267 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001268 | CAST ResolvedVal TO Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001269 $$ = new CastInst($2, *$4);
1270 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001271 }
Chris Lattnerc24d2082001-06-11 15:04:20 +00001272 | PHI PHIList {
1273 const Type *Ty = $2->front().first->getType();
1274 $$ = new PHINode(Ty);
Chris Lattner00950542001-06-06 20:29:01 +00001275 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001276 if ($2->front().first->getType() != Ty)
1277 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001278 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001279 $2->pop_front();
1280 }
1281 delete $2; // Free the list...
1282 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001283 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattneref9c23f2001-10-03 14:53:21 +00001284 const PointerType *PMTy;
Chris Lattner8b81bf52001-07-25 22:47:46 +00001285 const MethodType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001286
Chris Lattneref9c23f2001-10-03 14:53:21 +00001287 if (!(PMTy = dyn_cast<PointerType>($2->get())) ||
1288 !(Ty = dyn_cast<MethodType>(PMTy->getValueType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001289 // Pull out the types of all of the arguments...
1290 vector<const Type*> ParamTypes;
Chris Lattneref9c23f2001-10-03 14:53:21 +00001291 if ($5) {
1292 for (list<Value*>::iterator I = $5->begin(), E = $5->end(); I != E; ++I)
1293 ParamTypes.push_back((*I)->getType());
1294 }
1295 Ty = MethodType::get($2->get(), ParamTypes);
1296 PMTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001297 }
Chris Lattner30c89792001-09-07 16:35:17 +00001298 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001299
Chris Lattneref9c23f2001-10-03 14:53:21 +00001300 Value *V = getVal(PMTy, $3); // Get the method we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001301
Chris Lattner8b81bf52001-07-25 22:47:46 +00001302 // Create the call node...
1303 if (!$5) { // Has no arguments?
Chris Lattner9636a912001-10-01 16:18:37 +00001304 $$ = new CallInst(cast<Method>(V), vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001305 } else { // Has arguments?
Chris Lattner00950542001-06-06 20:29:01 +00001306 // Loop through MethodType's arguments and ensure they are specified
1307 // correctly!
1308 //
1309 MethodType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001310 MethodType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
1311 list<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
1312
1313 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1314 if ((*ArgI)->getType() != *I)
1315 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner00950542001-06-06 20:29:01 +00001316 (*I)->getName() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001317
Chris Lattner8b81bf52001-07-25 22:47:46 +00001318 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner00950542001-06-06 20:29:01 +00001319 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00001320
Chris Lattner9636a912001-10-01 16:18:37 +00001321 $$ = new CallInst(cast<Method>(V),
Chris Lattner8b81bf52001-07-25 22:47:46 +00001322 vector<Value*>($5->begin(), $5->end()));
1323 }
1324 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00001325 }
1326 | MemoryInst {
1327 $$ = $1;
1328 }
1329
Chris Lattner027dcc52001-07-08 21:10:27 +00001330// UByteList - List of ubyte values for load and store instructions
1331UByteList : ',' ConstVector {
1332 $$ = $2;
1333} | /* empty */ {
1334 $$ = new vector<ConstPoolVal*>();
1335}
1336
Chris Lattner00950542001-06-06 20:29:01 +00001337MemoryInst : MALLOC Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001338 $$ = new MallocInst(PointerType::get(*$2));
1339 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001340 }
1341 | MALLOC Types ',' UINT ValueRef {
Chris Lattnerb00c5822001-10-02 03:41:24 +00001342 if (!(*$2)->isArrayType() || cast<const ArrayType>($2->get())->isSized())
Chris Lattner30c89792001-09-07 16:35:17 +00001343 ThrowException("Trying to allocate " + (*$2)->getName() +
Chris Lattner00950542001-06-06 20:29:01 +00001344 " as unsized array!");
Chris Lattner30c89792001-09-07 16:35:17 +00001345 const Type *Ty = PointerType::get(*$2);
Chris Lattner8896eda2001-07-09 19:38:36 +00001346 $$ = new MallocInst(Ty, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001347 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001348 }
1349 | ALLOCA Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001350 $$ = new AllocaInst(PointerType::get(*$2));
1351 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001352 }
1353 | ALLOCA Types ',' UINT ValueRef {
Chris Lattnerb00c5822001-10-02 03:41:24 +00001354 if (!(*$2)->isArrayType() || cast<const ArrayType>($2->get())->isSized())
Chris Lattner30c89792001-09-07 16:35:17 +00001355 ThrowException("Trying to allocate " + (*$2)->getName() +
Chris Lattner00950542001-06-06 20:29:01 +00001356 " as unsized array!");
Chris Lattner30c89792001-09-07 16:35:17 +00001357 const Type *Ty = PointerType::get(*$2);
Chris Lattner00950542001-06-06 20:29:01 +00001358 Value *ArrSize = getVal($4, $5);
Chris Lattnerf0d0e9c2001-07-07 08:36:30 +00001359 $$ = new AllocaInst(Ty, ArrSize);
Chris Lattner30c89792001-09-07 16:35:17 +00001360 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001361 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001362 | FREE ResolvedVal {
1363 if (!$2->getType()->isPointerType())
1364 ThrowException("Trying to free nonpointer type " +
1365 $2->getType()->getName() + "!");
1366 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001367 }
1368
Chris Lattner027dcc52001-07-08 21:10:27 +00001369 | LOAD Types ValueRef UByteList {
Chris Lattner30c89792001-09-07 16:35:17 +00001370 if (!(*$2)->isPointerType())
1371 ThrowException("Can't load from nonpointer type: " + (*$2)->getName());
1372 if (LoadInst::getIndexedType(*$2, *$4) == 0)
Chris Lattner027dcc52001-07-08 21:10:27 +00001373 ThrowException("Invalid indices for load instruction!");
1374
Chris Lattner30c89792001-09-07 16:35:17 +00001375 $$ = new LoadInst(getVal(*$2, $3), *$4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001376 delete $4; // Free the vector...
Chris Lattner30c89792001-09-07 16:35:17 +00001377 delete $2;
Chris Lattner027dcc52001-07-08 21:10:27 +00001378 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001379 | STORE ResolvedVal ',' Types ValueRef UByteList {
Chris Lattner30c89792001-09-07 16:35:17 +00001380 if (!(*$4)->isPointerType())
1381 ThrowException("Can't store to a nonpointer type: " + (*$4)->getName());
1382 const Type *ElTy = StoreInst::getIndexedType(*$4, *$6);
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001383 if (ElTy == 0)
1384 ThrowException("Can't store into that field list!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001385 if (ElTy != $2->getType())
1386 ThrowException("Can't store '" + $2->getType()->getName() +
1387 "' into space of type '" + ElTy->getName() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001388 $$ = new StoreInst($2, getVal(*$4, $5), *$6);
1389 delete $4; delete $6;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001390 }
1391 | GETELEMENTPTR Types ValueRef UByteList {
Chris Lattner30c89792001-09-07 16:35:17 +00001392 if (!(*$2)->isPointerType())
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001393 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner30c89792001-09-07 16:35:17 +00001394 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
1395 ThrowException("Can't get element ptr '" + (*$2)->getName() + "'!");
1396 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
1397 delete $2; delete $4;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001398 }
Chris Lattner027dcc52001-07-08 21:10:27 +00001399
Chris Lattner00950542001-06-06 20:29:01 +00001400%%
Chris Lattner09083092001-07-08 04:57:15 +00001401int yyerror(const char *ErrorMsg) {
Chris Lattner00950542001-06-06 20:29:01 +00001402 ThrowException(string("Parse error: ") + ErrorMsg);
1403 return 0;
1404}